summaryrefslogtreecommitdiff
blob: ac860e242cc8fa39a7cd7243d729923d2ba45fcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
/**
 * Module Name: Publicize
 * Module Description: Publicize makes it easy to share your site’s posts on several social media networks automatically when you publish a new post.
 * Sort Order: 10
 * Recommendation Order: 7
 * First Introduced: 2.0
 * Requires Connection: Yes
 * Requires User Connection: Yes
 * Auto Activate: No
 * Module Tags: Social, Recommended
 * Feature: Engagement
 * Additional Search Queries: facebook, jetpack publicize, twitter, tumblr, linkedin, social, tweet, connections, sharing, social media, automated, automated sharing, auto publish, auto tweet and like, auto tweet, facebook auto post, facebook posting
 *
 * @package automattic/jetpack
 */

/**
 * Class Jetpack_Publicize
 */
class Jetpack_Publicize {

	/**
	 * If Publicize is executing within Jetpack.
	 *
	 * @var bool
	 */
	public $in_jetpack = true;

	/**
	 * Jetpack_Publicize constructor.
	 */
	public function __construct() {
		global $publicize_ui;

		$this->modules    = new Automattic\Jetpack\Modules();
		$this->in_jetpack = ( class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'enable_module_configurable' ) ) ? true : false;

		if ( $this->in_jetpack ) {
			Jetpack::enable_module_configurable( __FILE__ );

			if ( $this->modules->is_active( 'publicize' ) ) {
				add_action(
					'jetpack_register_gutenberg_extensions',
					function () {
						global $publicize;
						if ( $publicize->current_user_can_access_publicize_data() ) {
							Jetpack_Gutenberg::set_extension_available( 'jetpack/publicize' );
						} else {
							Jetpack_Gutenberg::set_extension_unavailable( 'jetpack/publicize', 'unauthorized' );
						}
					}
				);
			}

			// if sharedaddy isn't active, the sharing menu hasn't been added yet.
			if ( $this->modules->is_active( 'publicize' ) && ! $this->modules->is_active( 'sharedaddy' ) ) {
				add_action( 'admin_menu', array( &$publicize_ui, 'sharing_menu' ) );
			}

			/*
			 * The Publicize Options array does not currently have UI since it is being added
			 * for a specific purpose and not part of a broader Publicize sprint.
			 *
			 * In order to pass the settings up to WordPress.com, we are updating an option to Sync will pass it up.
			 * To make it relatively easy for use, we are creating a filter that checks if the option and filter match.
			 *
			 * This only runs when a post is saved to avoid it running too much.
			 */
			add_action(
				'save_post',
				function () {
					$publicize_options = get_option( 'jetpack_publicize_options', array() );

					/**
					 * Filters the options for Publicize.
					 *
					 * As of Jetpack 8.5, the array keys could be:
					 * attach_media bool If Publicize should send the image to the social media platform. Default false.
					 *
					 * @module publicize
					 *
					 * @since 8.5.0
					 *
					 * @param array $options Array of Publicize options.
					 */
					$filtered = (array) apply_filters( 'jetpack_publicize_options', $publicize_options );

					if ( $publicize_options !== $filtered ) {
						update_option( 'jetpack_publicize_options', $filtered, false );
					}
				}
			);
		} else {
			require_once __DIR__ . '/publicize/publicize.php';
			require_once dirname( __DIR__ ) . '/mu-plugins/keyring/keyring.php';
			require_once __DIR__ . '/publicize/publicize-wpcom.php';
			require_once __DIR__ . '/publicize/ui.php';
			$publicize_ui = new Publicize_UI();
		}

		$publicize_ui->in_jetpack = $this->in_jetpack;
	}
}

// On Jetpack, we instantiate Jetpack_Publicize only if the Publicize module is active.
if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) {

	$modules = new Automattic\Jetpack\Modules();

	if ( $modules->is_active( 'publicize' ) ) {
		new Jetpack_Publicize();
	}

	if ( ! function_exists( 'publicize_init' ) ) {
		/**
		 * Helper for grabbing a Publicize object from the "front-end" (non-admin) of
		 * a site. Normally Publicize is only loaded in wp-admin, so there's a little
		 * set up that you might need to do if you want to use it on the front end.
		 * Just call this function and it returns a Publicize object.
		 *
		 * @return Publicize Object
		 */
		function publicize_init() {
			global $publicize;

			return $publicize;
		}
	}
} else {
	// On wpcom, instantiate Jetpack_Publicize without any other checks.
	new Jetpack_Publicize();
}