Completed
Push — update/publicize-uses-publish-... ( 0d898d...acfb67 )
by
unknown
07:38
created

Publicize::options_save_facebook()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 41
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 23
nc 6
nop 0
dl 0
loc 41
rs 6.7272
c 0
b 0
f 0
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, 'admin_page_load' ), 9 );
11
12
		add_action( 'wp_ajax_publicize_tumblr_options_page', array( $this, 'options_page_tumblr' ) );
13
		add_action( 'wp_ajax_publicize_facebook_options_page', array( $this, 'options_page_facebook' ) );
14
		add_action( 'wp_ajax_publicize_twitter_options_page', array( $this, 'options_page_twitter' ) );
15
		add_action( 'wp_ajax_publicize_linkedin_options_page', array( $this, 'options_page_linkedin' ) );
16
		add_action( 'wp_ajax_publicize_path_options_page', array( $this, 'options_page_path' ) );
17
		add_action( 'wp_ajax_publicize_google_plus_options_page', array( $this, 'options_page_google_plus' ) );
18
19
		add_action( 'wp_ajax_publicize_tumblr_options_save', array( $this, 'options_save_tumblr' ) );
20
		add_action( 'wp_ajax_publicize_facebook_options_save', array( $this, 'options_save_facebook' ) );
21
		add_action( 'wp_ajax_publicize_twitter_options_save', array( $this, 'options_save_twitter' ) );
22
		add_action( 'wp_ajax_publicize_linkedin_options_save', array( $this, 'options_save_linkedin' ) );
23
		add_action( 'wp_ajax_publicize_path_options_save', array( $this, 'options_save_path' ) );
24
		add_action( 'wp_ajax_publicize_google_plus_options_save', array( $this, 'options_save_google_plus' ) );
25
26
		add_action( 'load-settings_page_sharing', array( $this, 'force_user_connection' ) );
27
28
		add_filter( 'publicize_checkbox_default', array( $this, 'publicize_checkbox_default' ), 10, 4 );
29
30
		add_filter( 'jetpack_published_post_flags', array( $this, 'set_post_flags' ), 10, 2 );
31
32
		add_action( 'wp_insert_post', array( $this, 'save_publicized' ), 11, 3 );
33
34
		add_filter( 'jetpack_twitter_cards_site_tag', array( $this, 'enhaced_twitter_cards_site_tag' ) );
35
36
		add_action( 'publicize_save_meta', array( $this, 'save_publicized_twitter_account' ), 10, 4 );
37
		add_action( 'publicize_save_meta', array( $this, 'save_publicized_facebook_account' ), 10, 4 );
38
39
		add_filter( 'jetpack_sharing_twitter_via', array( $this, 'get_publicized_twitter_account' ), 10, 2 );
40
41
		include_once( JETPACK__PLUGIN_DIR . 'modules/publicize/enhanced-open-graph.php' );
42
	}
43
44
	function force_user_connection() {
45
		global $current_user;
46
		$user_token        = Jetpack_Data::get_access_token( $current_user->ID );
47
		$is_user_connected = $user_token && ! is_wp_error( $user_token );
48
49
		// If the user is already connected via Jetpack, then we're good
50
		if ( $is_user_connected ) {
51
			return;
52
		}
53
54
		// If they're not connected, then remove the Publicize UI and tell them they need to connect first
55
		global $publicize_ui;
56
		remove_action( 'pre_admin_screen_sharing', array( $publicize_ui, 'admin_page' ) );
57
58
		// Do we really need `admin_styles`? With the new admin UI, it's breaking some bits.
59
		// Jetpack::init()->admin_styles();
60
		add_action( 'pre_admin_screen_sharing', array( $this, 'admin_page_warning' ), 1 );
61
	}
62
63
	function admin_page_warning() {
64
		$jetpack   = Jetpack::init();
65
		$blog_name = get_bloginfo( 'blogname' );
66
		if ( empty( $blog_name ) ) {
67
			$blog_name = home_url( '/' );
68
		}
69
70
		?>
71
		<div id="message" class="updated jetpack-message jp-connect">
72
			<div class="jetpack-wrap-container">
73
				<div class="jetpack-text-container">
74
					<p><?php printf(
75
							esc_html( wptexturize( __( "To use Publicize, you'll need to link your %s account to your WordPress.com account using the link below.", 'jetpack' ) ) ),
76
							'<strong>' . esc_html( $blog_name ) . '</strong>'
77
						); ?></p>
78
					<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>
79
				</div>
80
				<div class="jetpack-install-container">
81
					<p class="submit"><a
82
							href="<?php echo $jetpack->build_connect_url( false, menu_page_url( 'sharing', false ) ); ?>"
83
							class="button-connector"
84
							id="wpcom-connect"><?php esc_html_e( 'Link account with WordPress.com', 'jetpack' ); ?></a>
85
					</p>
86
				</div>
87
			</div>
88
		</div>
89
		<?php
90
	}
91
92
	/**
93
	 * Remove a Publicize connection
94
	 */
95
	function disconnect( $service_name, $connection_id, $_blog_id = false, $_user_id = false, $force_delete = false ) {
96
		Jetpack::load_xml_rpc_client();
97
		$xml = new Jetpack_IXR_Client();
98
		$xml->query( 'jetpack.deletePublicizeConnection', $connection_id );
99
100
		if ( ! $xml->isError() ) {
101
			Jetpack_Options::update_option( 'publicize_connections', $xml->getResponse() );
102
		} else {
103
			return false;
104
		}
105
	}
106
107
	function receive_updated_publicize_connections( $publicize_connections ) {
108
		Jetpack_Options::update_option( 'publicize_connections', $publicize_connections );
109
110
		return true;
111
	}
112
113
	function register_update_publicize_connections_xmlrpc_method( $methods ) {
114
		return array_merge( $methods, array(
115
			'jetpack.updatePublicizeConnections' => array( $this, 'receive_updated_publicize_connections' ),
116
		) );
117
	}
118
119
	function get_connections( $service_name, $_blog_id = false, $_user_id = false ) {
120
		$connections           = Jetpack_Options::get_option( 'publicize_connections' );
121
		$connections_to_return = array();
122
		if ( ! empty( $connections ) && is_array( $connections ) ) {
123
			if ( ! empty( $connections[ $service_name ] ) ) {
124
				foreach ( $connections[ $service_name ] as $id => $connection ) {
125
					if ( 0 == $connection['connection_data']['user_id'] || $this->user_id() == $connection['connection_data']['user_id'] ) {
126
						$connections_to_return[ $id ] = $connection;
127
					}
128
				}
129
			}
130
131
			return $connections_to_return;
132
		}
133
134
		return false;
135
	}
136
137
	function get_connection_id( $connection ) {
138
		return $connection['connection_data']['id'];
139
	}
140
141
	function get_connection_meta( $connection ) {
142
		$connection['user_id'] = $connection['connection_data']['user_id']; // Allows for shared connections
143
		return $connection;
144
	}
145
146
	function admin_page_load() {
147
		if ( isset( $_GET['action'] ) ) {
148
			if ( isset( $_GET['service'] ) ) {
149
				$service_name = $_GET['service'];
150
			}
151
152
			switch ( $_GET['action'] ) {
153
				case 'error':
154
					add_action( 'pre_admin_screen_sharing', array( $this, 'display_connection_error' ), 9 );
155
					break;
156
157
				case 'request':
158
					check_admin_referer( 'keyring-request', 'kr_nonce' );
159
					check_admin_referer( "keyring-request-$service_name", 'nonce' );
160
161
					$verification = Jetpack::create_nonce( 'publicize' );
162
163
					$stats_options = get_option( 'stats_options' );
164
					$wpcom_blog_id = Jetpack_Options::get_option( 'id' );
165
					$wpcom_blog_id = ! empty( $wpcom_blog_id ) ? $wpcom_blog_id : $stats_options['blog_id'];
166
167
					$user     = wp_get_current_user();
168
					$redirect = $this->api_url( $service_name, urlencode_deep( array(
169
						'action'       => 'request',
170
						'redirect_uri' => add_query_arg( array( 'action' => 'done' ), menu_page_url( 'sharing', false ) ),
171
						'for'          => 'publicize',
172
						// required flag that says this connection is intended for publicize
173
						'siteurl'      => site_url(),
174
						'state'        => $user->ID,
175
						'blog_id'      => $wpcom_blog_id,
176
						'secret_1'     => $verification['secret_1'],
177
						'secret_2'     => $verification['secret_2'],
178
						'eol'          => $verification['eol'],
179
					) ) );
180
					wp_redirect( $redirect );
181
					exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method admin_page_load() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
182
					break;
183
184
				case 'completed':
185
					Jetpack::load_xml_rpc_client();
186
					$xml = new Jetpack_IXR_Client();
187
					$xml->query( 'jetpack.fetchPublicizeConnections' );
188
189
					if ( ! $xml->isError() ) {
190
						$response = $xml->getResponse();
191
						Jetpack_Options::update_option( 'publicize_connections', $response );
192
					}
193
194
					break;
195
196
				case 'delete':
197
					$id = $_GET['id'];
198
199
					check_admin_referer( 'keyring-request', 'kr_nonce' );
200
					check_admin_referer( "keyring-request-$service_name", 'nonce' );
201
202
					$this->disconnect( $service_name, $id );
203
204
					add_action( 'admin_notices', array( $this, 'display_disconnected' ) );
205
					break;
206
			}
207
		}
208
209
		// Do we really need `admin_styles`? With the new admin UI, it's breaking some bits.
210
		// Errors encountered on WordPress.com's end are passed back as a code
211
		/*
212
		if ( isset( $_GET['action'] ) && 'error' == $_GET['action'] ) {
213
			// Load Jetpack's styles to handle the box
214
			Jetpack::init()->admin_styles();
215
		}
216
		*/
217
	}
218
219
	function display_connection_error() {
220
		$code = false;
221
		if ( isset( $_GET['service'] ) ) {
222
			$service_name = $_GET['service'];
223
			$error        = sprintf( __( 'There was a problem connecting to %s to create an authorized connection. Please try again in a moment.', 'jetpack' ), Publicize::get_service_label( $service_name ) );
224
		} else {
225
			if ( isset( $_GET['publicize_error'] ) ) {
226
				$code = strtolower( $_GET['publicize_error'] );
227
				switch ( $code ) {
228
					case '400':
229
						$error = __( 'An invalid request was made. This normally means that something intercepted or corrupted the request from your server to the Jetpack Server. Try again and see if it works this time.', 'jetpack' );
230
						break;
231
					case 'secret_mismatch':
232
						$error = __( 'We could not verify that your server is making an authorized request. Please try again, and make sure there is nothing interfering with requests from your server to the Jetpack Server.', 'jetpack' );
233
						break;
234
					case 'empty_blog_id':
235
						$error = __( 'No blog_id was included in your request. Please try disconnecting Jetpack from WordPress.com and then reconnecting it. Once you have done that, try connecting Publicize again.', 'jetpack' );
236
						break;
237
					case 'empty_state':
238
						$error = sprintf( __( 'No user information was included in your request. Please make sure that your user account has connected to Jetpack. Connect your user account by going to the <a href="%s">Jetpack page</a> within wp-admin.', 'jetpack' ), Jetpack::admin_url() );
239
						break;
240
					default:
241
						$error = __( 'Something which should never happen, happened. Sorry about that. If you try again, maybe it will work.', 'jetpack' );
242
						break;
243
				}
244
			} else {
245
				$error = __( 'There was a problem connecting with Publicize. Please try again in a moment.', 'jetpack' );
246
			}
247
		}
248
		// Using the same formatting/style as Jetpack::admin_notices() error
249
		?>
250
		<div id="message" class="jetpack-message jetpack-err">
251
			<div class="squeezer">
252
				<h2><?php echo wp_kses( $error, array( 'a'      => array( 'href' => true ),
253
				                                       'code'   => true,
254
				                                       'strong' => true,
255
				                                       'br'     => true,
256
				                                       'b'      => true
257
					) ); ?></h2>
258
				<?php if ( $code ) : ?>
259
					<p><?php printf( __( 'Error code: %s', 'jetpack' ), esc_html( stripslashes( $code ) ) ); ?></p>
260
				<?php endif; ?>
261
			</div>
262
		</div>
263
		<?php
264
	}
265
266
	function display_disconnected() {
267
		echo "<div class='updated'>\n";
268
		echo '<p>' . esc_html( __( 'That connection has been removed.', 'jetpack' ) ) . "</p>\n";
269
		echo "</div>\n\n";
270
	}
271
272
	function globalization() {
273
		if ( 'on' == $_REQUEST['global'] ) {
274
			$id = $_REQUEST['connection'];
275
276
			if ( ! current_user_can( $this->GLOBAL_CAP ) ) {
277
				return;
278
			}
279
280
			Jetpack::load_xml_rpc_client();
281
			$xml = new Jetpack_IXR_Client();
282
			$xml->query( 'jetpack.globalizePublicizeConnection', $id, 'globalize' );
283
284
			if ( ! $xml->isError() ) {
285
				$response = $xml->getResponse();
286
				Jetpack_Options::update_option( 'publicize_connections', $response );
287
			}
288
		}
289
	}
290
291
	/**
292
	 * Gets a URL to the public-api actions. Works like WP's admin_url
293
	 *
294
	 * @param string $service Shortname of a specific service.
295
	 *
296
	 * @return URL to specific public-api process
297
	 */
298
	// on WordPress.com this is/calls Keyring::admin_url
299
	function api_url( $service = false, $params = array() ) {
300
		/**
301
		 * Filters the API URL used to interact with WordPress.com.
302
		 *
303
		 * @module publicize
304
		 *
305
		 * @since 2.0.0
306
		 *
307
		 * @param string https://public-api.wordpress.com/connect/?jetpack=publicize Default Publicize API URL.
308
		 */
309
		$url = apply_filters( 'publicize_api_url', 'https://public-api.wordpress.com/connect/?jetpack=publicize' );
310
311
		if ( $service ) {
312
			$url = add_query_arg( array( 'service' => $service ), $url );
313
		}
314
315
		if ( count( $params ) ) {
316
			$url = add_query_arg( $params, $url );
317
		}
318
319
		return $url;
320
	}
321
322
	function connect_url( $service_name ) {
323
		return add_query_arg( array(
324
			'action'   => 'request',
325
			'service'  => $service_name,
326
			'kr_nonce' => wp_create_nonce( 'keyring-request' ),
327
			'nonce'    => wp_create_nonce( "keyring-request-$service_name" ),
328
		), menu_page_url( 'sharing', false ) );
329
	}
330
331
	function refresh_url( $service_name ) {
332
		return add_query_arg( array(
333
			'action'   => 'request',
334
			'service'  => $service_name,
335
			'kr_nonce' => wp_create_nonce( 'keyring-request' ),
336
			'refresh'  => 1,
337
			'for'      => 'publicize',
338
			'nonce'    => wp_create_nonce( "keyring-request-$service_name" ),
339
		), admin_url( 'options-general.php?page=sharing' ) );
340
	}
341
342
	function disconnect_url( $service_name, $id ) {
343
		return add_query_arg( array(
344
			'action'   => 'delete',
345
			'service'  => $service_name,
346
			'id'       => $id,
347
			'kr_nonce' => wp_create_nonce( 'keyring-request' ),
348
			'nonce'    => wp_create_nonce( "keyring-request-$service_name" ),
349
		), menu_page_url( 'sharing', false ) );
350
	}
351
352
	function get_services( $filter ) {
353
		if ( ! in_array( $filter, array( 'all', 'connected' ) ) ) {
354
			$filter = 'all';
355
		}
356
357
		$services = array(
358
			'facebook'    => array(),
359
			'twitter'     => array(),
360
			'linkedin'    => array(),
361
			'tumblr'      => array(),
362
			'path'        => array(),
363
			'google_plus' => array(),
364
		);
365
366
		if ( 'all' == $filter ) {
367
			return $services;
368
		} else {
369
			$connected_services = array();
370
			foreach ( $services as $service => $empty ) {
371
				$connections = $this->get_connections( $service );
372
				if ( $connections ) {
373
					$connected_services[ $service ] = $connections;
374
				}
375
			}
376
377
			return $connected_services;
378
		}
379
	}
380
381
	function get_connection( $service, $id, $_blog_id = false, $_user_id = false ) {
382
		// Stub
383
	}
384
385
	function flag_post_for_publicize( $new_status, $old_status, $post ) {
386
		if ( 'publish' == $new_status && 'publish' != $old_status ) {
387
			/**
388
			 * Determines whether a post being published gets publicized.
389
			 *
390
			 * Side-note: Possibly our most alliterative filter name.
391
			 *
392
			 * @module publicize
393
			 *
394
			 * @since 4.1.0
395
			 *
396
			 * @param bool $should_publicize Should the post be publicized? Default to true.
397
			 * @param WP_POST $post Current Post object.
398
			 */
399
			$should_publicize = apply_filters( 'publicize_should_publicize_published_post', true, $post );
400
401
			if ( $should_publicize ) {
402
				update_post_meta( $post->ID, $this->PENDING, true );
403
			}
404
		}
405
	}
406
407
	function test_connection( $service_name, $connection ) {
408
		$connection_test_passed  = true;
409
		$connection_test_message = '';
410
		$user_can_refresh        = false;
411
412
		$id = $this->get_connection_id( $connection );
413
414
		Jetpack::load_xml_rpc_client();
415
		$xml = new Jetpack_IXR_Client();
416
		$xml->query( 'jetpack.testPublicizeConnection', $id );
417
418
		if ( $xml->isError() ) {
419
			$xml_response            = $xml->getResponse();
420
			$connection_test_message = $xml_response['faultString'];
421
			$connection_test_passed  = false;
422
		}
423
424
		// Bail if all is well
425
		if ( $connection_test_passed ) {
426
			return true;
427
		}
428
429
		// Set up refresh if the user can
430
		$user_can_refresh = current_user_can( $this->GLOBAL_CAP );
431
		if ( $user_can_refresh ) {
432
			$nonce        = wp_create_nonce( "keyring-request-" . $service_name );
433
			$refresh_text = sprintf( _x( 'Refresh connection with %s', 'Refresh connection with {social media service}', 'jetpack' ), $this->get_service_label( $service_name ) );
434
			$refresh_url  = $this->refresh_url( $service_name );
435
		}
436
437
		$error_data = array(
438
			'user_can_refresh' => $user_can_refresh,
439
			'refresh_text'     => $refresh_text,
440
			'refresh_url'      => $refresh_url
441
		);
442
443
		return new WP_Error( 'pub_conn_test_failed', $connection_test_message, $error_data );
444
	}
445
446
	/**
447
	 * Save a flag locally to indicate that this post has already been Publicized via the selected
448
	 * connections.
449
	 */
450
	function save_publicized( $post_ID, $post, $update ) {
451
		// Only do this when a post transitions to being published
452
		if ( get_post_meta( $post->ID, $this->PENDING ) && $this->post_type_is_publicizeable( $post->post_type ) ) {
453
			$connected_services = Jetpack_Options::get_option( 'publicize_connections' );
454
			if ( ! empty( $connected_services ) ) {
455
				/**
456
				 * Fires when a post is saved that has is marked as pending publicizing
457
				 *
458
				 * @since 4.1.0
459
				 *
460
				 * @param int The post ID
461
				 */
462
				do_action( 'jetpack_publicize_post', $post->ID );
463
			}
464
			delete_post_meta( $post->ID, $this->PENDING );
465
			update_post_meta( $post->ID, $this->POST_DONE . 'all', true );
466
		}
467
	}
468
469
	function set_post_flags( $flags, $post ) {
470
		$flags['publicize_post'] = false;
471
		if ( ! $this->post_type_is_publicizeable( $post->post_type ) ) {
472
			return $flags;
473
		}
474
		/** This filter is already documented in modules/publicize/publicize-jetpack.php */
475
		if ( ! apply_filters( 'publicize_should_publicize_published_post', true, $post ) ) {
476
			return $flags;
477
		}
478
479
		$connected_services = Jetpack_Options::get_option( 'publicize_connections' );
480
		if ( empty( $connected_services ) ) {
481
			return $flags;
482
		}
483
484
		if( )
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected ')'
Loading history...
Bug introduced by
Avoid IF statements that are always true or false
Loading history...
485
486
		$flags['publicize_post'] = true;
487
488
		return $flags;
489
	}
490
491
	/**
492
	 * Options Code
493
	 */
494
495
	function options_page_facebook() {
496
		$connected_services = Jetpack_Options::get_option( 'publicize_connections' );
497
		$connection         = $connected_services['facebook'][ $_REQUEST['connection'] ];
498
		$options_to_show    = ( ! empty( $connection['connection_data']['meta']['options_responses'] ) ? $connection['connection_data']['meta']['options_responses'] : false );
499
500
		// Nonce check
501
		check_admin_referer( 'options_page_facebook_' . $_REQUEST['connection'] );
502
503
		$me    = ( ! empty( $options_to_show[0] ) ? $options_to_show[0] : false );
504
		$pages = ( ! empty( $options_to_show[1]['data'] ) ? $options_to_show[1]['data'] : false );
505
506
		$profile_checked = true;
507
		$page_selected   = false;
508
509
		if ( ! empty( $connection['connection_data']['meta']['facebook_page'] ) ) {
510
			$found = false;
511
			if ( is_array( $pages->data ) ) {
512
				foreach ( $pages->data as $page ) {
513
					if ( $page->id == $connection['connection_data']['meta']['facebook_page'] ) {
514
						$found = true;
515
						break;
516
					}
517
				}
518
			}
519
520
			if ( $found ) {
521
				$profile_checked = false;
522
				$page_selected   = $connection['connection_data']['meta']['facebook_page'];
523
			}
524
		}
525
526
		?>
527
528
		<div id="thickbox-content">
529
530
			<?php
531
			ob_start();
532
			Publicize_UI::connected_notice( 'Facebook' );
533
			$update_notice = ob_get_clean();
534
535
			if ( ! empty( $update_notice ) ) {
536
				echo $update_notice;
537
			}
538
			?>
539
540
			<?php if ( ! empty( $me['name'] ) ) : ?>
541
				<p><?php printf(
542
						esc_html__( 'Publicize to my %s:', 'jetpack' ),
543
						'<strong>' . esc_html__( 'Facebook Wall', 'jetpack' ) . '</strong>'
544
					); ?></p>
545
				<table id="option-profile">
546
					<tbody>
547
					<tr>
548
						<td class="radio"><input type="radio" name="option" data-type="profile"
549
						                         id="<?php echo esc_attr( $me['id'] ) ?>"
550
						                         value="" <?php checked( $profile_checked, true ); ?> /></td>
551
						<td class="thumbnail"><label for="<?php echo esc_attr( $me['id'] ) ?>"><img
552
									src="<?php echo esc_url( $me['picture']['data']['url'] ) ?>" width="50"
553
									height="50"/></label></td>
554
						<td class="details"><label
555
								for="<?php echo esc_attr( $me['id'] ) ?>"><?php echo esc_html( $me['name'] ) ?></label>
556
						</td>
557
					</tr>
558
					</tbody>
559
				</table>
560
			<?php endif; ?>
561
562
			<?php if ( $pages ) : ?>
563
564
				<p><?php printf(
565
						esc_html__( 'Publicize to my %s:', 'jetpack' ),
566
						'<strong>' . esc_html__( 'Facebook Page', 'jetpack' ) . '</strong>'
567
					); ?></p>
568
				<table id="option-fb-fanpage">
569
					<tbody>
570
571
					<?php foreach ( $pages as $i => $page ) : ?>
572
						<?php if ( ! ( $i % 2 ) ) : ?>
573
							<tr>
574
						<?php endif; ?>
575
						<td class="radio"><input type="radio" name="option" data-type="page"
576
						                         id="<?php echo esc_attr( $page['id'] ) ?>"
577
						                         value="<?php echo esc_attr( $page['id'] ) ?>" <?php checked( $page_selected && $page_selected == $page['id'], true ); ?> />
578
						</td>
579
						<td class="thumbnail"><label for="<?php echo esc_attr( $page['id'] ) ?>"><img
580
									src="<?php echo esc_url( str_replace( '_s', '_q', $page['picture']['data']['url'] ) ) ?>"
581
									width="50" height="50"/></label></td>
582
						<td class="details">
583
							<label for="<?php echo esc_attr( $page['id'] ) ?>">
584
								<span class="name"><?php echo esc_html( $page['name'] ) ?></span><br/>
585
								<span class="category"><?php echo esc_html( $page['category'] ) ?></span>
586
							</label>
587
						</td>
588
						<?php if ( ( $i % 2 ) || ( $i == count( $pages ) - 1 ) ): ?>
589
							</tr>
590
						<?php endif; ?>
591
					<?php endforeach; ?>
592
593
					</tbody>
594
				</table>
595
596
			<?php endif; ?>
597
598
			<?php Publicize_UI::global_checkbox( 'facebook', $_REQUEST['connection'] ); ?>
599
600
			<p style="text-align: center;">
601
				<input type="submit" value="<?php esc_attr_e( 'OK', 'jetpack' ) ?>"
602
				       class="button fb-options save-options" name="save"
603
				       data-connection="<?php echo esc_attr( $_REQUEST['connection'] ); ?>"
604
				       rel="<?php echo wp_create_nonce( 'save_fb_token_' . $_REQUEST['connection'] ) ?>"/>
605
			</p><br/>
606
		</div>
607
608
		<?php
609
	}
610
611
	function options_save_facebook() {
612
		// Nonce check
613
		check_admin_referer( 'save_fb_token_' . $_REQUEST['connection'] );
614
615
		$id = $_POST['connection'];
616
617
		// Check for a numeric page ID
618
		$page_id = $_POST['selected_id'];
619
		if ( ! ctype_digit( $page_id ) ) {
620
			die( 'Security check' );
0 ignored issues
show
Coding Style Compatibility introduced by
The method options_save_facebook() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
621
		}
622
623
		if ( isset( $_POST['selected_id'] ) && 'profile' == $_POST['type'] ) {
624
			// Publish to User Wall/Profile
625
			$options = array(
626
				'facebook_page'    => null,
627
				'facebook_profile' => true
628
			);
629
630
		} else {
631
			if ( 'page' != $_POST['type'] || ! isset( $_POST['selected_id'] ) ) {
632
				return;
633
			}
634
635
			// Publish to Page
636
			$options = array(
637
				'facebook_page'    => $page_id,
638
				'facebook_profile' => null
639
			);
640
		}
641
642
		Jetpack::load_xml_rpc_client();
643
		$xml = new Jetpack_IXR_Client();
644
		$xml->query( 'jetpack.setPublicizeOptions', $id, $options );
645
646
		if ( ! $xml->isError() ) {
647
			$response = $xml->getResponse();
648
			Jetpack_Options::update_option( 'publicize_connections', $response );
649
		}
650
651
		$this->globalization();
652
	}
653
654
	function options_page_tumblr() {
655
		// Nonce check
656
		check_admin_referer( 'options_page_tumblr_' . $_REQUEST['connection'] );
657
658
		$connected_services = Jetpack_Options::get_option( 'publicize_connections' );
659
		$connection         = $connected_services['tumblr'][ $_POST['connection'] ];
660
		$options_to_show    = $connection['connection_data']['meta']['options_responses'];
661
		$request            = $options_to_show[0];
662
663
		$blogs = $request['response']['user']['blogs'];
664
665
		$blog_selected = false;
666
667
		if ( ! empty( $connection['connection_data']['meta']['tumblr_base_hostname'] ) ) {
668
			foreach ( $blogs as $blog ) {
669
				if ( $connection['connection_data']['meta']['tumblr_base_hostname'] == $this->get_basehostname( $blog['url'] ) ) {
670
					$blog_selected = $connection['connection_data']['meta']['tumblr_base_hostname'];
671
					break;
672
				}
673
			}
674
675
		}
676
677
		// Use their Primary blog if they haven't selected one yet
678
		if ( ! $blog_selected ) {
679
			foreach ( $blogs as $blog ) {
680
				if ( $blog['primary'] ) {
681
					$blog_selected = $this->get_basehostname( $blog['url'] );
682
				}
683
			}
684
		} ?>
685
686
		<div id="thickbox-content">
687
688
			<?php
689
			ob_start();
690
			Publicize_UI::connected_notice( 'Tumblr' );
691
			$update_notice = ob_get_clean();
692
693
			if ( ! empty( $update_notice ) ) {
694
				echo $update_notice;
695
			}
696
			?>
697
698
			<p><?php printf(
699
					esc_html__( 'Publicize to my %s:', 'jetpack' ),
700
					'<strong>' . esc_html__( 'Tumblr blog', 'jetpack' ) . '</strong>'
701
				); ?></p>
702
703
			<ul id="option-tumblr-blog">
704
705
				<?php
706
				foreach ( $blogs as $blog ) {
707
					$url = $this->get_basehostname( $blog['url'] ); ?>
708
					<li>
709
						<input type="radio" name="option" data-type="blog" id="<?php echo esc_attr( $url ) ?>"
710
						       value="<?php echo esc_attr( $url ) ?>" <?php checked( $blog_selected == $url, true ); ?> />
711
						<label for="<?php echo esc_attr( $url ) ?>"><span
712
								class="name"><?php echo esc_html( $blog['title'] ) ?></span></label>
713
					</li>
714
				<?php } ?>
715
716
			</ul>
717
718
			<?php Publicize_UI::global_checkbox( 'tumblr', $_REQUEST['connection'] ); ?>
719
720
			<p style="text-align: center;">
721
				<input type="submit" value="<?php esc_attr_e( 'OK', 'jetpack' ) ?>"
722
				       class="button tumblr-options save-options" name="save"
723
				       data-connection="<?php echo esc_attr( $_REQUEST['connection'] ); ?>"
724
				       rel="<?php echo wp_create_nonce( 'save_tumblr_blog_' . $_REQUEST['connection'] ) ?>"/>
725
			</p> <br/>
726
		</div>
727
728
		<?php
729
	}
730
731
	function get_basehostname( $url ) {
732
		return parse_url( $url, PHP_URL_HOST );
733
	}
734
735
	function options_save_tumblr() {
736
		// Nonce check
737
		check_admin_referer( 'save_tumblr_blog_' . $_REQUEST['connection'] );
738
739
		$id = $_POST['connection'];
740
741
		$options = array( 'tumblr_base_hostname' => $_POST['selected_id'] );
742
743
		Jetpack::load_xml_rpc_client();
744
		$xml = new Jetpack_IXR_Client();
745
		$xml->query( 'jetpack.setPublicizeOptions', $id, $options );
746
747
		if ( ! $xml->isError() ) {
748
			$response = $xml->getResponse();
749
			Jetpack_Options::update_option( 'publicize_connections', $response );
750
		}
751
752
		$this->globalization();
753
	}
754
755
	function options_page_twitter() {
756
		Publicize_UI::options_page_other( 'twitter' );
757
	}
758
759
	function options_page_linkedin() {
760
		Publicize_UI::options_page_other( 'linkedin' );
761
	}
762
763
	function options_page_path() {
764
		Publicize_UI::options_page_other( 'path' );
765
	}
766
767
	function options_page_google_plus() {
768
		Publicize_UI::options_page_other( 'google_plus' );
769
	}
770
771
	function options_save_twitter() {
772
		$this->options_save_other( 'twitter' );
773
	}
774
775
	function options_save_linkedin() {
776
		$this->options_save_other( 'linkedin' );
777
	}
778
779
	function options_save_path() {
780
		$this->options_save_other( 'path' );
781
	}
782
783
	function options_save_google_plus() {
784
		$this->options_save_other( 'google_plus' );
785
	}
786
787
	function options_save_other( $service_name ) {
788
		// Nonce check
789
		check_admin_referer( 'save_' . $service_name . '_token_' . $_REQUEST['connection'] );
790
		$this->globalization();
791
	}
792
793
	/**
794
	 * Already-published posts should not be Publicized by default. This filter sets checked to
795
	 * false if a post has already been published.
796
	 */
797
	function publicize_checkbox_default( $checked, $post_id, $name, $connection ) {
798
		if ( 'publish' == get_post_status( $post_id ) ) {
799
			return false;
800
		}
801
802
		return $checked;
803
	}
804
805
	/**
806
	 * If there's only one shared connection to Twitter set it as twitter:site tag.
807
	 */
808
	function enhaced_twitter_cards_site_tag( $tag ) {
809
		$custom_site_tag = get_option( 'jetpack-twitter-cards-site-tag' );
810
		if ( ! empty( $custom_site_tag ) ) {
811
			return $tag;
812
		}
813
		if ( ! $this->is_enabled( 'twitter' ) ) {
814
			return $tag;
815
		}
816
		$connections = $this->get_connections( 'twitter' );
817
		foreach ( $connections as $connection ) {
818
			$connection_meta = $this->get_connection_meta( $connection );
819
			if ( 0 == $connection_meta['connection_data']['user_id'] ) {
820
				// If the connection is shared
821
				return $this->get_display_name( 'twitter', $connection );
822
			}
823
		}
824
825
		return $tag;
826
	}
827
828
	function save_publicized_twitter_account( $submit_post, $post_id, $service_name, $connection ) {
829
		if ( 'twitter' == $service_name && $submit_post ) {
830
			$connection_meta        = $this->get_connection_meta( $connection );
831
			$publicize_twitter_user = get_post_meta( $post_id, '_publicize_twitter_user' );
832
			if ( empty( $publicize_twitter_user ) || 0 != $connection_meta['connection_data']['user_id'] ) {
833
				update_post_meta( $post_id, '_publicize_twitter_user', $this->get_display_name( 'twitter', $connection ) );
834
			}
835
		}
836
	}
837
838
	function get_publicized_twitter_account( $account, $post_id ) {
839
		if ( ! empty( $account ) ) {
840
			return $account;
841
		}
842
		$account = get_post_meta( $post_id, '_publicize_twitter_user', true );
843
		if ( ! empty( $account ) ) {
844
			return $account;
845
		}
846
847
		return '';
848
	}
849
850
	/**
851
	 * Save the Publicized Facebook account when publishing a post
852
	 * Use only Personal accounts, not Facebook Pages
853
	 */
854
	function save_publicized_facebook_account( $submit_post, $post_id, $service_name, $connection ) {
855
		$connection_meta = $this->get_connection_meta( $connection );
856
		if ( 'facebook' == $service_name && isset( $connection_meta['connection_data']['meta']['facebook_profile'] ) && $submit_post ) {
857
			$publicize_facebook_user = get_post_meta( $post_id, '_publicize_facebook_user' );
858
			if ( empty( $publicize_facebook_user ) || 0 != $connection_meta['connection_data']['user_id'] ) {
859
				$profile_link = $this->get_profile_link( 'facebook', $connection );
860
861
				if ( false !== $profile_link ) {
862
					update_post_meta( $post_id, '_publicize_facebook_user', $profile_link );
863
				}
864
			}
865
		}
866
	}
867
}
868