Completed
Push — master-stable ( 123e21...46eadb )
by
unknown
62:16 queued 49:39
created

modules/publicize/publicize-jetpack.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
class Publicize extends Publicize_Base {
4
5
	function __construct() {
6
		parent::__construct();
7
8
		add_filter( 'jetpack_xmlrpc_methods', array( $this, 'register_update_publicize_connections_xmlrpc_method' ) );
9
10
		add_action( 'load-settings_page_sharing', array( $this, 'force_user_connection' ) );
11
12
		add_filter( 'publicize_checkbox_default', array( $this, 'publicize_checkbox_default' ), 10, 4 );
13
14
		add_action( 'transition_post_status', array( $this, 'save_publicized' ), 10, 3 );
15
16
		add_filter( 'jetpack_twitter_cards_site_tag', array( $this, 'enhaced_twitter_cards_site_tag' ) );
17
18
		add_action( 'publicize_save_meta', array( $this, 'save_publicized_twitter_account' ), 10, 4 );
19
		add_action( 'publicize_save_meta', array( $this, 'save_publicized_facebook_account' ), 10, 4 );
20
21
		add_filter( 'jetpack_sharing_twitter_via', array( $this, 'get_publicized_twitter_account' ), 10, 2 );
22
23
		include_once ( JETPACK__PLUGIN_DIR . 'modules/publicize/enhanced-open-graph.php' );
24
	}
25
26
	function force_user_connection() {
27
		global $current_user;
28
		$user_token = Jetpack_Data::get_access_token( $current_user->ID );
29
		$is_user_connected = $user_token && !is_wp_error( $user_token );
30
31
		// If the user is already connected via Jetpack, then we're good
32
		if ( $is_user_connected )
33
			return;
34
35
		// If they're not connected, then remove the Publicize UI and tell them they need to connect first
36
		global $publicize_ui;
37
		remove_action( 'pre_admin_screen_sharing', array( $publicize_ui, 'admin_page' ) );
38
		add_action( 'pre_admin_screen_sharing', array( $this, 'admin_page_warning' ), 1 );
39
	}
40
41
	function admin_page_warning() {
42
		$jetpack = Jetpack::init();
43
		$blog_name = get_bloginfo( 'blogname' );
44
		if ( empty( $blog_name ) ) {
45
			$blog_name = home_url( '/' );
46
		}
47
48
		?>
49
		<div id="message" class="updated jetpack-message jp-connect">
50
			<div class="jetpack-wrap-container">
51
				<div class="jetpack-text-container">
52
					<p><?php printf(
53
						esc_html( wptexturize( __( "To use Publicize, you'll need to link your %s account to your WordPress.com account using the link below.", 'jetpack' ) ) ),
54
						'<strong>' . esc_html( $blog_name ) . '</strong>'
55
					); ?></p>
56
					<p><?php echo esc_html( wptexturize( __( "If you don't have a WordPress.com account yet, you can sign up for free in just a few seconds.", 'jetpack' ) ) ); ?></p>
57
				</div>
58
				<div class="jetpack-install-container">
59
					<p class="submit"><a href="<?php echo $jetpack->build_connect_url( false, menu_page_url( 'sharing', false ) ); ?>" class="button-connector" id="wpcom-connect"><?php esc_html_e( 'Link account with WordPress.com', 'jetpack' ); ?></a></p>
60
				</div>
61
			</div>
62
		</div>
63
		<?php
64
	}
65
66
67
	function receive_updated_publicize_connections( $publicize_connections ) {
68
		Jetpack_Options::update_option( 'publicize_connections', $publicize_connections );
69
		return true;
70
	}
71
72
	function register_update_publicize_connections_xmlrpc_method( $methods ) {
73
		return array_merge( $methods, array(
74
			'jetpack.updatePublicizeConnections' => array( $this, 'receive_updated_publicize_connections' ),
75
		) );
76
	}
77
78
	function get_connections( $service_name, $_blog_id = false, $_user_id = false ) {
79
		$connections = Jetpack_Options::get_option( 'publicize_connections' );
80
		$connections_to_return = array();
81
		if ( !empty( $connections ) && is_array( $connections ) ) {
82
			if ( !empty( $connections[$service_name] ) ) {
83
				foreach( $connections[$service_name] as $id => $connection ) {
84
					if ( 0 == $connection['connection_data']['user_id'] || $this->user_id() == $connection['connection_data']['user_id'] ) {
85
						$connections_to_return[$id] = $connection;
86
					}
87
				}
88
			}
89
			return $connections_to_return;
90
		}
91
		return false;
92
	}
93
94
	function get_connection_id( $connection ) {
95
		return $connection['connection_data']['id'];
96
	}
97
98
	function get_connection_meta( $connection ) {
99
		$connection['user_id'] = $connection['connection_data']['user_id']; // Allows for shared connections
100
		return $connection;
101
	}
102
103
	function get_services( $filter ) {
104
		if ( !in_array( $filter, array( 'all', 'connected' ) ) )
105
			$filter = 'all';
106
107
		$services = array(
108
				'facebook'        => array(),
109
				'twitter'         => array(),
110
				'linkedin'        => array(),
111
				'tumblr'          => array(),
112
				'path'            => array(),
113
				'google_plus'     => array(),
114
		);
115
116
		if ( 'all' == $filter ) {
117
			return $services;
118
		} else {
119
			$connected_services = array();
120
			foreach ( $services as $service => $empty ) {
121
				$connections = $this->get_connections( $service );
122
				if ( $connections )
123
					$connected_services[$service] = $connections;
124
			}
125
			return $connected_services;
126
		}
127
	}
128
129
	function get_connection( $service, $id, $_blog_id = false, $_user_id = false ) {
130
		// Stub
131
	}
132
133
	function flag_post_for_publicize( $new_status, $old_status, $post ) {
134
		// Stub only. Doesn't need to do anything on Jetpack Client
135
	}
136
137
	function test_connection( $service_name, $connection ) {
138
		$connection_test_passed = true;
139
		$connection_test_message = '';
140
		$user_can_refresh = false;
141
142
		$id = $this->get_connection_id( $connection );
143
144
		Jetpack::load_xml_rpc_client();
145
		$xml = new Jetpack_IXR_Client();
146
		$xml->query( 'jetpack.testPublicizeConnection', $id );
147
148
		if ( $xml->isError() ) {
149
			$xml_response = $xml->getResponse();
150
			$connection_test_message = $xml_response['faultString'];
151
			$connection_test_passed = false;
152
		}
153
154
		// Bail if all is well
155
		if ( $connection_test_passed ) {
156
			return true;
157
		}
158
159
		// Set up refresh if the user can
160
		$user_can_refresh = current_user_can( $this->GLOBAL_CAP );
161
		if ( $user_can_refresh ) {
162
			$nonce = wp_create_nonce( "keyring-request-" . $service_name );
163
			$refresh_text = sprintf( _x( 'Refresh connection with %s', 'Refresh connection with {social media service}', 'jetpack' ), $this->get_service_label( $service_name ) );
164
			$refresh_url = $this->refresh_url( $service_name );
0 ignored issues
show
The method refresh_url() does not seem to exist on object<Publicize>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
165
		}
166
167
		$error_data = array(
168
			'user_can_refresh' => $user_can_refresh,
169
			'refresh_text' => $refresh_text,
170
			'refresh_url' => $refresh_url
171
		);
172
173
		return new WP_Error( 'pub_conn_test_failed', $connection_test_message, $error_data );
174
	}
175
176
	/**
177
	 * Save a flag locally to indicate that this post has already been Publicized via the selected
178
	 * connections.
179
	 */
180
	function save_publicized( $new_status, $old_status, $post ) {
181
		// Only do this when a post transitions to being published
182
		if ( 'publish' == $new_status && 'publish' != $old_status ) {
183
			update_post_meta( $post->ID, $this->POST_DONE . 'all', true );
184
		}
185
	}
186
187
	/**
188
	* Already-published posts should not be Publicized by default. This filter sets checked to
189
	* false if a post has already been published.
190
	*/
191
	function publicize_checkbox_default( $checked, $post_id, $name, $connection ) {
192
		if ( 'publish' == get_post_status( $post_id ) )
193
			return false;
194
195
		return $checked;
196
	}
197
198
	/**
199
	* If there's only one shared connection to Twitter set it as twitter:site tag.
200
	*/
201
	function enhaced_twitter_cards_site_tag( $tag ) {
202
		$custom_site_tag = get_option( 'jetpack-twitter-cards-site-tag' );
203
		if( ! empty( $custom_site_tag ) )
204
			return $tag;
205
		if ( ! $this->is_enabled('twitter') )
206
			return $tag;
207
		$connections = $this->get_connections( 'twitter' );
208
		foreach ( $connections as $connection ) {
209
			$connection_meta = $this->get_connection_meta( $connection );
210
			if ( 0 == $connection_meta['connection_data']['user_id'] ) {
211
				// If the connection is shared
212
				return $this->get_display_name( 'twitter', $connection );
213
			}
214
		}
215
		return $tag;
216
	}
217
218
	function save_publicized_twitter_account( $submit_post, $post_id, $service_name, $connection ) {
219
		if ( 'twitter' == $service_name && $submit_post ) {
220
			$connection_meta = $this->get_connection_meta( $connection );
221
			$publicize_twitter_user = get_post_meta( $post_id, '_publicize_twitter_user' );
222
			if ( empty( $publicize_twitter_user ) || 0 != $connection_meta['connection_data']['user_id'] ) {
223
				update_post_meta( $post_id, '_publicize_twitter_user', $this->get_display_name( 'twitter', $connection ) );
224
			}
225
		}
226
	}
227
228
	function get_publicized_twitter_account( $account, $post_id ) {
229
		if ( ! empty( $account ) ) {
230
			return $account;
231
		}
232
		$account = get_post_meta( $post_id, '_publicize_twitter_user', true );
233
		if ( ! empty( $account ) ) {
234
			return $account;
235
		}
236
		return '';
237
	}
238
239
	/**
240
	* Save the Publicized Facebook account when publishing a post
241
	* Use only Personal accounts, not Facebook Pages
242
	*/
243
	function save_publicized_facebook_account( $submit_post, $post_id, $service_name, $connection ) {
244
		$connection_meta = $this->get_connection_meta( $connection );
245
		if ( 'facebook' == $service_name && isset( $connection_meta['connection_data']['meta']['facebook_profile'] ) && $submit_post ) {
246
			$publicize_facebook_user = get_post_meta( $post_id, '_publicize_facebook_user' );
247
			if ( empty( $publicize_facebook_user ) || 0 != $connection_meta['connection_data']['user_id'] ) {
248
				$profile_link = $this->get_profile_link( 'facebook', $connection );
249
250
				if ( false !== $profile_link ) {
251
					update_post_meta( $post_id, '_publicize_facebook_user', $profile_link );
252
				}
253
			}
254
		}
255
	}
256
}
257