Completed
Push — fix/always-fire-publicize-if-c... ( 982828 )
by
unknown
09:02
created

Publicize::options_page_tumblr()   C

Complexity

Conditions 9
Paths 32

Size

Total Lines 76
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 47
nc 32
nop 0
dl 0
loc 76
rs 5.8219
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
					if ( is_wp_error( $verification ) ) {
163
						$url = Jetpack::admin_url( 'jetpack#/settings' );
164
						wp_die( sprintf( __( "Jetpack is not connected. Please connect Jetpack by visiting <a href='%s'>Settings</a>.", 'jetpack' ), $url ) );
165
166
					}
167
					$stats_options = get_option( 'stats_options' );
168
					$wpcom_blog_id = Jetpack_Options::get_option( 'id' );
169
					$wpcom_blog_id = ! empty( $wpcom_blog_id ) ? $wpcom_blog_id : $stats_options['blog_id'];
170
171
					$user     = wp_get_current_user();
172
					$redirect = $this->api_url( $service_name, urlencode_deep( array(
173
						'action'       => 'request',
174
						'redirect_uri' => add_query_arg( array( 'action' => 'done' ), menu_page_url( 'sharing', false ) ),
175
						'for'          => 'publicize',
176
						// required flag that says this connection is intended for publicize
177
						'siteurl'      => site_url(),
178
						'state'        => $user->ID,
179
						'blog_id'      => $wpcom_blog_id,
180
						'secret_1'     => $verification['secret_1'],
181
						'secret_2'     => $verification['secret_2'],
182
						'eol'          => $verification['eol'],
183
					) ) );
184
					wp_redirect( $redirect );
185
					exit;
186
					break;
187
188
				case 'completed':
189
					Jetpack::load_xml_rpc_client();
190
					$xml = new Jetpack_IXR_Client();
191
					$xml->query( 'jetpack.fetchPublicizeConnections' );
192
193
					if ( ! $xml->isError() ) {
194
						$response = $xml->getResponse();
195
						Jetpack_Options::update_option( 'publicize_connections', $response );
196
					}
197
198
					break;
199
200
				case 'delete':
201
					$id = $_GET['id'];
202
203
					check_admin_referer( 'keyring-request', 'kr_nonce' );
204
					check_admin_referer( "keyring-request-$service_name", 'nonce' );
205
206
					$this->disconnect( $service_name, $id );
207
208
					add_action( 'admin_notices', array( $this, 'display_disconnected' ) );
209
					break;
210
			}
211
		}
212
213
		// Do we really need `admin_styles`? With the new admin UI, it's breaking some bits.
214
		// Errors encountered on WordPress.com's end are passed back as a code
215
		/*
216
		if ( isset( $_GET['action'] ) && 'error' == $_GET['action'] ) {
217
			// Load Jetpack's styles to handle the box
218
			Jetpack::init()->admin_styles();
219
		}
220
		*/
221
	}
222
223
	function display_connection_error() {
224
		$code = false;
225
		if ( isset( $_GET['service'] ) ) {
226
			$service_name = $_GET['service'];
227
			$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 ) );
228
		} else {
229
			if ( isset( $_GET['publicize_error'] ) ) {
230
				$code = strtolower( $_GET['publicize_error'] );
231
				switch ( $code ) {
232
					case '400':
233
						$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' );
234
						break;
235
					case 'secret_mismatch':
236
						$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' );
237
						break;
238
					case 'empty_blog_id':
239
						$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' );
240
						break;
241
					case 'empty_state':
242
						$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() );
243
						break;
244
					default:
245
						$error = __( 'Something which should never happen, happened. Sorry about that. If you try again, maybe it will work.', 'jetpack' );
246
						break;
247
				}
248
			} else {
249
				$error = __( 'There was a problem connecting with Publicize. Please try again in a moment.', 'jetpack' );
250
			}
251
		}
252
		// Using the same formatting/style as Jetpack::admin_notices() error
253
		?>
254
		<div id="message" class="jetpack-message jetpack-err">
255
			<div class="squeezer">
256
				<h2><?php echo wp_kses( $error, array( 'a'      => array( 'href' => true ),
257
				                                       'code'   => true,
258
				                                       'strong' => true,
259
				                                       'br'     => true,
260
				                                       'b'      => true
261
					) ); ?></h2>
262
				<?php if ( $code ) : ?>
263
					<p><?php printf( __( 'Error code: %s', 'jetpack' ), esc_html( stripslashes( $code ) ) ); ?></p>
264
				<?php endif; ?>
265
			</div>
266
		</div>
267
		<?php
268
	}
269
270
	function display_disconnected() {
271
		echo "<div class='updated'>\n";
272
		echo '<p>' . esc_html( __( 'That connection has been removed.', 'jetpack' ) ) . "</p>\n";
273
		echo "</div>\n\n";
274
	}
275
276
	function globalization() {
277
		if ( 'on' == $_REQUEST['global'] ) {
278
			$id = $_REQUEST['connection'];
279
280
			if ( ! current_user_can( $this->GLOBAL_CAP ) ) {
281
				return;
282
			}
283
284
			Jetpack::load_xml_rpc_client();
285
			$xml = new Jetpack_IXR_Client();
286
			$xml->query( 'jetpack.globalizePublicizeConnection', $id, 'globalize' );
287
288
			if ( ! $xml->isError() ) {
289
				$response = $xml->getResponse();
290
				Jetpack_Options::update_option( 'publicize_connections', $response );
291
			}
292
		}
293
	}
294
295
	/**
296
	 * Gets a URL to the public-api actions. Works like WP's admin_url
297
	 *
298
	 * @param string $service Shortname of a specific service.
299
	 *
300
	 * @return URL to specific public-api process
301
	 */
302
	// on WordPress.com this is/calls Keyring::admin_url
303
	function api_url( $service = false, $params = array() ) {
304
		/**
305
		 * Filters the API URL used to interact with WordPress.com.
306
		 *
307
		 * @module publicize
308
		 *
309
		 * @since 2.0.0
310
		 *
311
		 * @param string https://public-api.wordpress.com/connect/?jetpack=publicize Default Publicize API URL.
312
		 */
313
		$url = apply_filters( 'publicize_api_url', 'https://public-api.wordpress.com/connect/?jetpack=publicize' );
314
315
		if ( $service ) {
316
			$url = add_query_arg( array( 'service' => $service ), $url );
317
		}
318
319
		if ( count( $params ) ) {
320
			$url = add_query_arg( $params, $url );
321
		}
322
323
		return $url;
324
	}
325
326
	function connect_url( $service_name ) {
327
		return add_query_arg( array(
328
			'action'   => 'request',
329
			'service'  => $service_name,
330
			'kr_nonce' => wp_create_nonce( 'keyring-request' ),
331
			'nonce'    => wp_create_nonce( "keyring-request-$service_name" ),
332
		), menu_page_url( 'sharing', false ) );
333
	}
334
335
	function refresh_url( $service_name ) {
336
		return add_query_arg( array(
337
			'action'   => 'request',
338
			'service'  => $service_name,
339
			'kr_nonce' => wp_create_nonce( 'keyring-request' ),
340
			'refresh'  => 1,
341
			'for'      => 'publicize',
342
			'nonce'    => wp_create_nonce( "keyring-request-$service_name" ),
343
		), admin_url( 'options-general.php?page=sharing' ) );
344
	}
345
346
	function disconnect_url( $service_name, $id ) {
347
		return add_query_arg( array(
348
			'action'   => 'delete',
349
			'service'  => $service_name,
350
			'id'       => $id,
351
			'kr_nonce' => wp_create_nonce( 'keyring-request' ),
352
			'nonce'    => wp_create_nonce( "keyring-request-$service_name" ),
353
		), menu_page_url( 'sharing', false ) );
354
	}
355
356
	function get_services( $filter ) {
357
		if ( ! in_array( $filter, array( 'all', 'connected' ) ) ) {
358
			$filter = 'all';
359
		}
360
361
		$services = array(
362
			'facebook'    => array(),
363
			'twitter'     => array(),
364
			'linkedin'    => array(),
365
			'tumblr'      => array(),
366
			'path'        => array(),
367
			'google_plus' => array(),
368
		);
369
370
		if ( 'all' == $filter ) {
371
			return $services;
372
		} else {
373
			$connected_services = array();
374
			foreach ( $services as $service => $empty ) {
375
				$connections = $this->get_connections( $service );
376
				if ( $connections ) {
377
					$connected_services[ $service ] = $connections;
378
				}
379
			}
380
381
			return $connected_services;
382
		}
383
	}
384
385
	function get_connection( $service, $id, $_blog_id = false, $_user_id = false ) {
386
		// Stub
387
	}
388
389
	function flag_post_for_publicize( $new_status, $old_status, $post ) {
390
		if ( 'publish' == $new_status && 'publish' != $old_status ) {
391
			/**
392
			 * Determines whether a post being published gets publicized.
393
			 *
394
			 * Side-note: Possibly our most alliterative filter name.
395
			 *
396
			 * @module publicize
397
			 *
398
			 * @since 4.1.0
399
			 *
400
			 * @param bool $should_publicize Should the post be publicized? Default to true.
401
			 * @param WP_POST $post Current Post object.
402
			 */
403
			$should_publicize = apply_filters( 'publicize_should_publicize_published_post', true, $post );
404
405
			if ( $should_publicize ) {
406
				update_post_meta( $post->ID, $this->PENDING, true );
407
			}
408
		}
409
	}
410
411
	function test_connection( $service_name, $connection ) {
412
		$connection_test_passed  = true;
413
		$connection_test_message = '';
414
		$user_can_refresh        = false;
415
416
		$id = $this->get_connection_id( $connection );
417
418
		Jetpack::load_xml_rpc_client();
419
		$xml = new Jetpack_IXR_Client();
420
		$xml->query( 'jetpack.testPublicizeConnection', $id );
421
422
		if ( $xml->isError() ) {
423
			$xml_response            = $xml->getResponse();
424
			$connection_test_message = $xml_response['faultString'];
425
			$connection_test_passed  = false;
426
		}
427
428
		// Bail if all is well
429
		if ( $connection_test_passed ) {
430
			return true;
431
		}
432
433
		// Set up refresh if the user can
434
		$user_can_refresh = current_user_can( $this->GLOBAL_CAP );
435
		if ( $user_can_refresh ) {
436
			$nonce        = wp_create_nonce( "keyring-request-" . $service_name );
437
			$refresh_text = sprintf( _x( 'Refresh connection with %s', 'Refresh connection with {social media service}', 'jetpack' ), $this->get_service_label( $service_name ) );
438
			$refresh_url  = $this->refresh_url( $service_name );
439
		}
440
441
		$error_data = array(
442
			'user_can_refresh' => $user_can_refresh,
443
			'refresh_text'     => $refresh_text,
444
			'refresh_url'      => $refresh_url
445
		);
446
447
		return new WP_Error( 'pub_conn_test_failed', $connection_test_message, $error_data );
448
	}
449
450
	/**
451
	 * Save a flag locally to indicate that this post has already been Publicized via the selected
452
	 * connections.
453
	 */
454
	function save_publicized( $post_ID, $post, $update ) {
455
		// Only do this when a post transitions to being published
456
		if ( get_post_meta( $post->ID, $this->PENDING ) && $this->post_type_is_publicizeable( $post->post_type ) ) {
457
			$connected_services = Jetpack_Options::get_option( 'publicize_connections' );
458
			if ( ! empty( $connected_services ) ) {
459
				/**
460
				 * Fires when a post is saved that has is marked as pending publicizing
461
				 *
462
				 * @since 4.1.0
463
				 *
464
				 * @param int The post ID
465
				 */
466
				do_action( 'jetpack_publicize_post', $post->ID );
467
			}
468
			delete_post_meta( $post->ID, $this->PENDING );
469
			update_post_meta( $post->ID, $this->POST_DONE . 'all', true );
470
		}
471
	}
472
473
	function set_post_flags( $flags, $post ) {
474
		$flags['publicize_post'] = false;
475
		if ( ! $this->post_type_is_publicizeable( $post->post_type ) ) {
476
			return $flags;
477
		}
478
		/** This filter is already documented in modules/publicize/publicize-jetpack.php */
479
		if ( ! apply_filters( 'publicize_should_publicize_published_post', true, $post ) ) {
480
			return $flags;
481
		}
482
483
		$connected_services = Jetpack_Options::get_option( 'publicize_connections' )
484
485
		if ( empty( $connected_services ) ) {
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 T_IF
Loading history...
486
			return $flags;
487
		}
488
489
		$flags['publicize_post'] = true;
490
491
		return $flags;
492
	}
493
494
	/**
495
	 * Options Code
496
	 */
497
498
	function options_page_facebook() {
499
		$connected_services = Jetpack_Options::get_option( 'publicize_connections' );
500
		$connection         = $connected_services['facebook'][ $_REQUEST['connection'] ];
501
		$options_to_show    = ( ! empty( $connection['connection_data']['meta']['options_responses'] ) ? $connection['connection_data']['meta']['options_responses'] : false );
502
503
		// Nonce check
504
		check_admin_referer( 'options_page_facebook_' . $_REQUEST['connection'] );
505
506
		$me    = ( ! empty( $options_to_show[0] ) ? $options_to_show[0] : false );
507
		$pages = ( ! empty( $options_to_show[1]['data'] ) ? $options_to_show[1]['data'] : false );
508
509
		$profile_checked = true;
510
		$page_selected   = false;
511
512
		if ( ! empty( $connection['connection_data']['meta']['facebook_page'] ) ) {
513
			$found = false;
514
			if ( is_array( $pages->data ) ) {
515
				foreach ( $pages->data as $page ) {
516
					if ( $page->id == $connection['connection_data']['meta']['facebook_page'] ) {
517
						$found = true;
518
						break;
519
					}
520
				}
521
			}
522
523
			if ( $found ) {
524
				$profile_checked = false;
525
				$page_selected   = $connection['connection_data']['meta']['facebook_page'];
526
			}
527
		}
528
529
		?>
530
531
		<div id="thickbox-content">
532
533
			<?php
534
			ob_start();
535
			Publicize_UI::connected_notice( 'Facebook' );
536
			$update_notice = ob_get_clean();
537
538
			if ( ! empty( $update_notice ) ) {
539
				echo $update_notice;
540
			}
541
			?>
542
543
			<?php if ( ! empty( $me['name'] ) ) : ?>
544
				<p><?php printf(
545
						esc_html__( 'Publicize to my %s:', 'jetpack' ),
546
						'<strong>' . esc_html__( 'Facebook Wall', 'jetpack' ) . '</strong>'
547
					); ?></p>
548
				<table id="option-profile">
549
					<tbody>
550
					<tr>
551
						<td class="radio"><input type="radio" name="option" data-type="profile"
552
						                         id="<?php echo esc_attr( $me['id'] ) ?>"
553
						                         value="" <?php checked( $profile_checked, true ); ?> /></td>
554
						<td class="thumbnail"><label for="<?php echo esc_attr( $me['id'] ) ?>"><img
555
									src="<?php echo esc_url( $me['picture']['data']['url'] ) ?>" width="50"
556
									height="50"/></label></td>
557
						<td class="details"><label
558
								for="<?php echo esc_attr( $me['id'] ) ?>"><?php echo esc_html( $me['name'] ) ?></label>
559
						</td>
560
					</tr>
561
					</tbody>
562
				</table>
563
			<?php endif; ?>
564
565
			<?php if ( $pages ) : ?>
566
567
				<p><?php printf(
568
						esc_html__( 'Publicize to my %s:', 'jetpack' ),
569
						'<strong>' . esc_html__( 'Facebook Page', 'jetpack' ) . '</strong>'
570
					); ?></p>
571
				<table id="option-fb-fanpage">
572
					<tbody>
573
574
					<?php foreach ( $pages as $i => $page ) : ?>
575
						<?php if ( ! ( $i % 2 ) ) : ?>
576
							<tr>
577
						<?php endif; ?>
578
						<td class="radio"><input type="radio" name="option" data-type="page"
579
						                         id="<?php echo esc_attr( $page['id'] ) ?>"
580
						                         value="<?php echo esc_attr( $page['id'] ) ?>" <?php checked( $page_selected && $page_selected == $page['id'], true ); ?> />
581
						</td>
582
						<td class="thumbnail"><label for="<?php echo esc_attr( $page['id'] ) ?>"><img
583
									src="<?php echo esc_url( str_replace( '_s', '_q', $page['picture']['data']['url'] ) ) ?>"
584
									width="50" height="50"/></label></td>
585
						<td class="details">
586
							<label for="<?php echo esc_attr( $page['id'] ) ?>">
587
								<span class="name"><?php echo esc_html( $page['name'] ) ?></span><br/>
588
								<span class="category"><?php echo esc_html( $page['category'] ) ?></span>
589
							</label>
590
						</td>
591
						<?php if ( ( $i % 2 ) || ( $i == count( $pages ) - 1 ) ): ?>
592
							</tr>
593
						<?php endif; ?>
594
					<?php endforeach; ?>
595
596
					</tbody>
597
				</table>
598
599
			<?php endif; ?>
600
601
			<?php Publicize_UI::global_checkbox( 'facebook', $_REQUEST['connection'] ); ?>
602
603
			<p style="text-align: center;">
604
				<input type="submit" value="<?php esc_attr_e( 'OK', 'jetpack' ) ?>"
605
				       class="button fb-options save-options" name="save"
606
				       data-connection="<?php echo esc_attr( $_REQUEST['connection'] ); ?>"
607
				       rel="<?php echo wp_create_nonce( 'save_fb_token_' . $_REQUEST['connection'] ) ?>"/>
608
			</p><br/>
609
		</div>
610
611
		<?php
612
	}
613
614
	function options_save_facebook() {
615
		// Nonce check
616
		check_admin_referer( 'save_fb_token_' . $_REQUEST['connection'] );
617
618
		$id = $_POST['connection'];
619
620
		// Check for a numeric page ID
621
		$page_id = $_POST['selected_id'];
622
		if ( ! ctype_digit( $page_id ) ) {
623
			die( 'Security check' );
624
		}
625
626
		if ( isset( $_POST['selected_id'] ) && 'profile' == $_POST['type'] ) {
627
			// Publish to User Wall/Profile
628
			$options = array(
629
				'facebook_page'    => null,
630
				'facebook_profile' => true
631
			);
632
633
		} else {
634
			if ( 'page' != $_POST['type'] || ! isset( $_POST['selected_id'] ) ) {
635
				return;
636
			}
637
638
			// Publish to Page
639
			$options = array(
640
				'facebook_page'    => $page_id,
641
				'facebook_profile' => null
642
			);
643
		}
644
645
		Jetpack::load_xml_rpc_client();
646
		$xml = new Jetpack_IXR_Client();
647
		$xml->query( 'jetpack.setPublicizeOptions', $id, $options );
648
649
		if ( ! $xml->isError() ) {
650
			$response = $xml->getResponse();
651
			Jetpack_Options::update_option( 'publicize_connections', $response );
652
		}
653
654
		$this->globalization();
655
	}
656
657
	function options_page_tumblr() {
658
		// Nonce check
659
		check_admin_referer( 'options_page_tumblr_' . $_REQUEST['connection'] );
660
661
		$connected_services = Jetpack_Options::get_option( 'publicize_connections' );
662
		$connection         = $connected_services['tumblr'][ $_POST['connection'] ];
663
		$options_to_show    = $connection['connection_data']['meta']['options_responses'];
664
		$request            = $options_to_show[0];
665
666
		$blogs = $request['response']['user']['blogs'];
667
668
		$blog_selected = false;
669
670
		if ( ! empty( $connection['connection_data']['meta']['tumblr_base_hostname'] ) ) {
671
			foreach ( $blogs as $blog ) {
672
				if ( $connection['connection_data']['meta']['tumblr_base_hostname'] == $this->get_basehostname( $blog['url'] ) ) {
673
					$blog_selected = $connection['connection_data']['meta']['tumblr_base_hostname'];
674
					break;
675
				}
676
			}
677
678
		}
679
680
		// Use their Primary blog if they haven't selected one yet
681
		if ( ! $blog_selected ) {
682
			foreach ( $blogs as $blog ) {
683
				if ( $blog['primary'] ) {
684
					$blog_selected = $this->get_basehostname( $blog['url'] );
685
				}
686
			}
687
		} ?>
688
689
		<div id="thickbox-content">
690
691
			<?php
692
			ob_start();
693
			Publicize_UI::connected_notice( 'Tumblr' );
694
			$update_notice = ob_get_clean();
695
696
			if ( ! empty( $update_notice ) ) {
697
				echo $update_notice;
698
			}
699
			?>
700
701
			<p><?php printf(
702
					esc_html__( 'Publicize to my %s:', 'jetpack' ),
703
					'<strong>' . esc_html__( 'Tumblr blog', 'jetpack' ) . '</strong>'
704
				); ?></p>
705
706
			<ul id="option-tumblr-blog">
707
708
				<?php
709
				foreach ( $blogs as $blog ) {
710
					$url = $this->get_basehostname( $blog['url'] ); ?>
711
					<li>
712
						<input type="radio" name="option" data-type="blog" id="<?php echo esc_attr( $url ) ?>"
713
						       value="<?php echo esc_attr( $url ) ?>" <?php checked( $blog_selected == $url, true ); ?> />
714
						<label for="<?php echo esc_attr( $url ) ?>"><span
715
								class="name"><?php echo esc_html( $blog['title'] ) ?></span></label>
716
					</li>
717
				<?php } ?>
718
719
			</ul>
720
721
			<?php Publicize_UI::global_checkbox( 'tumblr', $_REQUEST['connection'] ); ?>
722
723
			<p style="text-align: center;">
724
				<input type="submit" value="<?php esc_attr_e( 'OK', 'jetpack' ) ?>"
725
				       class="button tumblr-options save-options" name="save"
726
				       data-connection="<?php echo esc_attr( $_REQUEST['connection'] ); ?>"
727
				       rel="<?php echo wp_create_nonce( 'save_tumblr_blog_' . $_REQUEST['connection'] ) ?>"/>
728
			</p> <br/>
729
		</div>
730
731
		<?php
732
	}
733
734
	function get_basehostname( $url ) {
735
		return parse_url( $url, PHP_URL_HOST );
736
	}
737
738
	function options_save_tumblr() {
739
		// Nonce check
740
		check_admin_referer( 'save_tumblr_blog_' . $_REQUEST['connection'] );
741
742
		$id = $_POST['connection'];
743
744
		$options = array( 'tumblr_base_hostname' => $_POST['selected_id'] );
745
746
		Jetpack::load_xml_rpc_client();
747
		$xml = new Jetpack_IXR_Client();
748
		$xml->query( 'jetpack.setPublicizeOptions', $id, $options );
749
750
		if ( ! $xml->isError() ) {
751
			$response = $xml->getResponse();
752
			Jetpack_Options::update_option( 'publicize_connections', $response );
753
		}
754
755
		$this->globalization();
756
	}
757
758
	function options_page_twitter() {
759
		Publicize_UI::options_page_other( 'twitter' );
760
	}
761
762
	function options_page_linkedin() {
763
		Publicize_UI::options_page_other( 'linkedin' );
764
	}
765
766
	function options_page_path() {
767
		Publicize_UI::options_page_other( 'path' );
768
	}
769
770
	function options_page_google_plus() {
771
		Publicize_UI::options_page_other( 'google_plus' );
772
	}
773
774
	function options_save_twitter() {
775
		$this->options_save_other( 'twitter' );
776
	}
777
778
	function options_save_linkedin() {
779
		$this->options_save_other( 'linkedin' );
780
	}
781
782
	function options_save_path() {
783
		$this->options_save_other( 'path' );
784
	}
785
786
	function options_save_google_plus() {
787
		$this->options_save_other( 'google_plus' );
788
	}
789
790
	function options_save_other( $service_name ) {
791
		// Nonce check
792
		check_admin_referer( 'save_' . $service_name . '_token_' . $_REQUEST['connection'] );
793
		$this->globalization();
794
	}
795
796
	/**
797
	 * Already-published posts should not be Publicized by default. This filter sets checked to
798
	 * false if a post has already been published.
799
	 */
800
	function publicize_checkbox_default( $checked, $post_id, $name, $connection ) {
801
		if ( 'publish' == get_post_status( $post_id ) ) {
802
			return false;
803
		}
804
805
		return $checked;
806
	}
807
808
	/**
809
	 * If there's only one shared connection to Twitter set it as twitter:site tag.
810
	 */
811
	function enhaced_twitter_cards_site_tag( $tag ) {
812
		$custom_site_tag = get_option( 'jetpack-twitter-cards-site-tag' );
813
		if ( ! empty( $custom_site_tag ) ) {
814
			return $tag;
815
		}
816
		if ( ! $this->is_enabled( 'twitter' ) ) {
817
			return $tag;
818
		}
819
		$connections = $this->get_connections( 'twitter' );
820
		foreach ( $connections as $connection ) {
821
			$connection_meta = $this->get_connection_meta( $connection );
822
			if ( 0 == $connection_meta['connection_data']['user_id'] ) {
823
				// If the connection is shared
824
				return $this->get_display_name( 'twitter', $connection );
825
			}
826
		}
827
828
		return $tag;
829
	}
830
831
	function save_publicized_twitter_account( $submit_post, $post_id, $service_name, $connection ) {
832
		if ( 'twitter' == $service_name && $submit_post ) {
833
			$connection_meta        = $this->get_connection_meta( $connection );
834
			$publicize_twitter_user = get_post_meta( $post_id, '_publicize_twitter_user' );
835
			if ( empty( $publicize_twitter_user ) || 0 != $connection_meta['connection_data']['user_id'] ) {
836
				update_post_meta( $post_id, '_publicize_twitter_user', $this->get_display_name( 'twitter', $connection ) );
837
			}
838
		}
839
	}
840
841
	function get_publicized_twitter_account( $account, $post_id ) {
842
		if ( ! empty( $account ) ) {
843
			return $account;
844
		}
845
		$account = get_post_meta( $post_id, '_publicize_twitter_user', true );
846
		if ( ! empty( $account ) ) {
847
			return $account;
848
		}
849
850
		return '';
851
	}
852
853
	/**
854
	 * Save the Publicized Facebook account when publishing a post
855
	 * Use only Personal accounts, not Facebook Pages
856
	 */
857
	function save_publicized_facebook_account( $submit_post, $post_id, $service_name, $connection ) {
858
		$connection_meta = $this->get_connection_meta( $connection );
859
		if ( 'facebook' == $service_name && isset( $connection_meta['connection_data']['meta']['facebook_profile'] ) && $submit_post ) {
860
			$publicize_facebook_user = get_post_meta( $post_id, '_publicize_facebook_user' );
861
			if ( empty( $publicize_facebook_user ) || 0 != $connection_meta['connection_data']['user_id'] ) {
862
				$profile_link = $this->get_profile_link( 'facebook', $connection );
863
864
				if ( false !== $profile_link ) {
865
					update_post_meta( $post_id, '_publicize_facebook_user', $profile_link );
866
				}
867
			}
868
		}
869
	}
870
}
871