Completed
Push — branch-7.0 ( 596193...9628b8 )
by Jeremy
21:24 queued 12:50
created

Publicize_UI::get_must_reauth_connections()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 2
nop 0
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
/**
4
* Only user facing pieces of Publicize are found here.
5
*/
6
class Publicize_UI {
7
8
	/**
9
	* Contains an instance of class 'publicize' which loads Keyring, sets up services, etc.
10
	*/
11
	public $publicize;
12
13
	protected $publicize_settings_url = '';
14
15
	/**
16
	* Hooks into WordPress to display the various pieces of UI and load our assets
17
	*/
18
	function __construct() {
19
		global $publicize;
20
21
		$this->publicize = $publicize = new Publicize;
22
23
		add_action( 'init', array( $this, 'init' ) );
24
	}
25
26
	function init() {
27
		$this->publicize_settings_url = apply_filters_deprecated(
28
			'jetpack_override_publicize_settings_url',
29
			array( admin_url( 'options-general.php?page=sharing' ) ),
30
			'6.7',
31
			false,
32
			__( 'This filter will be removed in a future version of Jetpack', 'jetpack' )
33
		);
34
35
		// Show only to users with the capability required to manage their Publicize connections.
36
		if ( ! $this->publicize->current_user_can_access_publicize_data() ) {
37
			return;
38
		}
39
40
		// assets (css, js)
41
		if ( $this->in_jetpack ) {
0 ignored issues
show
Bug introduced by
The property in_jetpack does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
42
			add_action( 'load-settings_page_sharing', array( $this, 'load_assets' ) );
43
		}
44
		add_action( 'admin_head-post.php', array( $this, 'post_page_metabox_assets' ) );
45
		add_action( 'admin_head-post-new.php', array( $this, 'post_page_metabox_assets' ) );
46
47
		// management of publicize (sharing screen, ajax/lightbox popup, and metabox on post screen)
48
		add_action( 'pre_admin_screen_sharing', array( $this, 'admin_page' ) );
49
		add_action( 'post_submitbox_misc_actions', array( $this, 'post_page_metabox' ) );
50
	}
51
52
	/**
53
	* If the ShareDaddy plugin is not active we need to add the sharing settings page to the menu still
54
	*/
55
	function sharing_menu() {
56
		add_submenu_page(
57
			'options-general.php',
58
			__( 'Sharing Settings', 'jetpack' ),
59
			__( 'Sharing', 'jetpack' ),
60
			'publish_posts',
61
			'sharing',
62
			array( $this, 'wrapper_admin_page' )
63
		);
64
	}
65
66
	function wrapper_admin_page() {
67
		Jetpack_Admin_Page::wrap_ui( array( $this, 'management_page' ), array( 'is-wide' => true ) );
68
	}
69
	/**
70
	* Management page to load if Sharedaddy is not active so the 'pre_admin_screen_sharing' action exists.
71
	*/
72
	function management_page() { ?>
73
		<div class="wrap">
74
			<div class="icon32" id="icon-options-general"><br /></div>
75
			<h1><?php _e( 'Sharing Settings', 'jetpack' ); ?></h1>
76
77
				<?php
78
				/** This action is documented in modules/sharedaddy/sharing.php */
79
				do_action( 'pre_admin_screen_sharing' );
80
				?>
81
82
		</div> <?php
83
	}
84
85
	/**
86
	 * styling for the sharing screen and popups
87
	 * JS for the options and switching
88
	 */
89
	function load_assets() {
90
		wp_enqueue_script(
91
			'publicize',
92
			Jetpack::get_file_url_for_environment(
93
				'_inc/build/publicize/assets/publicize.min.js',
94
				'modules/publicize/assets/publicize.js'
95
			),
96
			array( 'jquery', 'thickbox' ),
97
			'20121019'
98
		);
99
		if ( is_rtl() ) {
100
			wp_enqueue_style( 'publicize', plugins_url( 'assets/rtl/publicize-rtl.css', __FILE__ ), array(), '20180301' );
101
		} else {
102
			wp_enqueue_style( 'publicize', plugins_url( 'assets/publicize.css', __FILE__ ), array(), '20180301' );
103
		}
104
105
		Jetpack_Admin_Page::load_wrapper_styles();
106
		wp_enqueue_style( 'social-logos' );
107
108
		// for deprecation tooltip
109
		wp_enqueue_style( 'wp-pointer' );
110
		wp_enqueue_script( 'wp-pointer' );
111
112
		add_thickbox();
113
	}
114
115
	public static function connected_notice( $service_name ) { ?>
116
		<div class='updated'>
117
			<p><?php
118
119
			if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
120
				$platform =  'WordPress.com';
121
			} else {
122
				$platform = 'Jetpack';
123
			}
124
125
			printf(
126
				/* translators: %1$s: Service Name (Facebook, Twitter, ...), %2$s: Site type (WordPress.com or Jetpack) */
127
				__( 'You have successfully connected your %1$s account with %2$s.', 'jetpack' ),
128
				Publicize::get_service_label( $service_name ),
129
				$platform
130
			); ?></p>
131
		</div><?php
132
	}
133
134
	public static function denied_notice() { ?>
135
		<div class='updated'>
136
			<p><?php _e ( "You have chosen not to connect your blog. Please click 'accept' when prompted if you wish to connect your accounts.", 'jetpack' ); ?></p>
137
		</div><?php
138
	}
139
140
	/**
141
	 * Lists the current user's publicized accounts for the blog
142
	 * looks exactly like Publicize v1 for now, UI and functionality updates will come after the move to keyring
143
	 */
144
	function admin_page() {
145
		$override_publicize_settings_page = apply_filters_deprecated(
146
			'jetpack_override_publicize_settings_page',
147
			array( false ),
148
			'6.7',
149
			false,
150
			__( 'This filter will be removed in a future version of Jetpack', 'jetpack' )
151
		);
152
153
		if ( $override_publicize_settings_page ) {
154
			echo $override_publicize_settings_page;
155
			return;
156
		}
157
158
		$_blog_id = get_current_blog_id();
159
		?>
160
161
		<form action="" id="publicize-form">
162
			<h2 id="publicize"><?php _e( 'Publicize', 'jetpack' ) ?></h2>
163
164
			<?php
165
				if ( ! empty( $_GET['action'] ) && 'deny' == $_GET['action'] ) {
166
					$this->denied_notice();
167
				}
168
			?>
169
170
			<p>
171
				<?php esc_html_e( 'Connect your blog to popular social networking sites and automatically share new posts with your friends.', 'jetpack' ) ?>
172
				<?php esc_html_e( 'You can make a connection for just yourself or for all users on your blog. Shared connections are marked with the (Shared) text.', 'jetpack' ); ?>
173
			</p>
174
175
			<?php
176
			if ( $this->in_jetpack ) {
177
				$doc_link = "http://jetpack.com/support/publicize/";
178
			} else {
179
				$doc_link = "http://en.support.wordpress.com/publicize/";
180
			}
181
			?>
182
183
			<p>&rarr; <a href="<?php echo esc_url( $doc_link ); ?>" rel="noopener noreferrer" target="_blank"><?php esc_html_e( 'More information on using Publicize.', 'jetpack' ); ?></a></p>
184
185
			<div id="publicize-services-block">
186
				<?php
187
				$services = $this->publicize->get_services( 'all' );
188
				$total_num_of_services = count ( $services );
189
				$service_num = 0;?>
190
191
				<div class='left'>
192
193
				<?php
194
				foreach ( $services as $service_name => $service ) :
195
					// do not display any service that cannot be connected anymore (deprecated).
196
					$connect_url = $this->publicize->can_connect_service( $service_name ) ?
197
						$this->publicize->connect_url( $service_name ) :
198
						null;
199
200
					if ( $service_num == ( round ( ( $total_num_of_services / 2 ), 0 ) ) )
201
						echo "</div><div class='right'>";
202
					$service_num++;
203
					?>
204
					<div class="publicize-service-entry" <?php if ( $service_num > 0 ): ?>class="connected"<?php endif; ?> >
205
						<div id="<?php echo esc_attr( $service_name ); ?>" class="publicize-service-left">
206
							<?php if ( $connect_url ): ?>
0 ignored issues
show
Bug Best Practice introduced by
The expression $connect_url of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
207
								<a href="<?php echo esc_url( $connect_url ); ?>" id="service-link-<?php echo esc_attr( $service_name ); ?>" target="_top"><?php echo $this->publicize->get_service_label( $service_name ); ?></a>
208
							<?php else: ?>
209
								<span><?php echo $this->publicize->get_service_label( $service_name ); ?></span>
210
							<?php endif; ?>
211
						</div>
212
213
214
						<div class="publicize-service-right">
215
							<?php if ( $this->publicize->is_enabled( $service_name ) && $connections = $this->publicize->get_connections( $service_name ) ) : ?>
216
								<ul>
217
									<?php
218
									foreach( $connections as $c ) :
219
										$id = $this->publicize->get_connection_id( $c );
220
										$disconnect_url = $this->publicize->disconnect_url( $service_name, $id );
221
222
										$cmeta = $this->publicize->get_connection_meta( $c );
223
										$profile_link = $this->publicize->get_profile_link( $service_name, $c );
224
										$connection_display = $this->publicize->get_display_name( $service_name, $c );
225
226
										$options_nonce = wp_create_nonce( 'options_page_' . $service_name . '_' . $id ); ?>
227
228
										<?php if ( $this->publicize->show_options_popup( $service_name, $c ) ): ?>
229
										<script type="text/javascript">
230
										jQuery(document).ready( function($) {
231
											showOptionsPage.call(
232
											this,
233
											'<?php echo esc_js( $service_name ); ?>',
234
											'<?php echo esc_js( $options_nonce ); ?>',
235
											'<?php echo esc_js( $id ); ?>'
236
											);
237
										} );
238
										</script>
239
										<?php endif; ?>
240
241
										<li class="publicize-connection" data-connection-id="<?php echo esc_attr( $id ); ?>">
242
											<?php esc_html_e( 'Connected as:', 'jetpack' ); ?>
243
											<?php
244
											if ( !empty( $profile_link ) ) : ?>
245
												<a class="publicize-profile-link" href="<?php echo esc_url( $profile_link ); ?>" target="_top">
246
													<?php echo esc_html( $connection_display ); ?>
247
												</a><?php
248
											else :
249
												echo esc_html( $connection_display );
250
											endif;
251
											?>
252
253
											<?php if ( 0 == $cmeta['connection_data']['user_id'] ) : ?>
254
												<small>(<?php esc_html_e( 'Shared', 'jetpack' ); ?>)</small>
255
256
												<?php if ( current_user_can( $this->publicize->GLOBAL_CAP ) ) : ?>
257
													<a class="pub-disconnect-button" title="<?php esc_html_e( 'Disconnect', 'jetpack' ); ?>" href="<?php echo esc_url( $disconnect_url ); ?>" target="_top">×</a>
258
												<?php endif; ?>
259
260
											<?php else : ?>
261
												<a class="pub-disconnect-button" title="<?php esc_html_e( 'Disconnect', 'jetpack' ); ?>" href="<?php echo esc_url( $disconnect_url ); ?>" target="_top">×</a>
262
											<?php endif; ?>
263
264
											<br/>
265
											<div class="pub-connection-test test-in-progress" id="pub-connection-test-<?php echo esc_attr( $id ); ?>" >
266
											</div>
267
										</li>
268
269
										<?php
270
									endforeach;
271
									?>
272
								</ul>
273
							<?php endif; ?>
274
275
							<?php
276
							if ( $connect_url ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $connect_url of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
277
								$connections = $this->publicize->get_connections( $service_name );
278
								if ( empty( $connections ) ) {
279
									printf(
280
										'<a id="%1$s" class="publicize-add-connection button" href="%2$s" target="_top">%3$s</a>',
281
										esc_attr( $service_name ),
282
										esc_url( $connect_url ),
283
										esc_html__( 'Connect', 'jetpack' )
284
									);
285
								} else {
286
									printf(
287
										'<a id="%1$s" class="publicize-add-connection button add-new" href="%2$s" target="_top">%3$s</a>',
288
										esc_attr( $service_name ),
289
										esc_url( $connect_url ),
290
										esc_html__( 'Add New', 'jetpack' )
291
									);
292
								}
293
							} else {
294
								echo '<div class="publicize-disabled-service-message">';
295
								esc_html_e( 'Google+ support is being removed.', 'jetpack' );
296
								printf(
297
									' <a href="javascript:void(0)" id="jetpack-gplus-deprecated-notice">%1$s<span class="dashicons dashicons-info"></span></a>',
298
									esc_html__( 'Why?', 'jetpack' )
299
								);
300
								echo '</div>';
301
							}
302
							?>
303
						</div>
304
					</div>
305
				<?php endforeach; ?>
306
				</div>
307
				<script>
308
				(function($){
309
					$('.pub-disconnect-button').on('click', function(e){ if ( confirm( '<?php echo esc_js( __( 'Are you sure you want to stop Publicizing posts to this connection?', 'jetpack' ) ); ?>' ) ) {
310
								return true;
311
							} else {
312
							e.preventDefault();
313
							return false;
314
						}
315
					});
316
				})(jQuery);
317
				</script>
318
				<?php $this->google_plus_shut_down_tooltip_script(); ?>
319
			</div>
320
321
			<?php wp_nonce_field( "wpas_posts_{$_blog_id}", "_wpas_posts_{$_blog_id}_nonce" ); ?>
322
			<input type="hidden" id="wpas_ajax_blog_id" name="wpas_ajax_blog_id" value="<?php echo $_blog_id; ?>" />
323
		</form><?php
324
325
	}
326
327
	public static function global_checkbox( $service_name, $id ) {
328
		global $publicize;
329
		if ( current_user_can( $publicize->GLOBAL_CAP ) ) : ?>
330
			<p>
331
				<input id="globalize_<?php echo $service_name; ?>" type="checkbox" name="global" value="<?php echo wp_create_nonce( 'publicize-globalize-' . $id ) ?>" />
332
				<label for="globalize_<?php echo $service_name; ?>"><?php _e( 'Make this connection available to all users of this blog?', 'jetpack' ); ?></label>
333
			</p>
334
		<?php endif;
335
	}
336
337
	function broken_connection( $service_name, $id ) { ?>
338
		<div id="thickbox-content">
339
			<div class='error'>
340
				<p><?php printf(
341
					/* translators: %s: Service Name (Facebook, Twitter, ...) */
342
					__( 'There was a problem connecting to %s. Please disconnect and try again.', 'jetpack' ),
343
					Publicize::get_service_label( $service_name )
344
				); ?></p>
345
			</div>
346
		</div><?php
347
	}
348
349
	public static function options_page_other( $service_name ) {
350
		// Nonce check
351
		check_admin_referer( "options_page_{$service_name}_" . $_REQUEST['connection'] );
352
		?>
353
		<div id="thickbox-content">
354
			<?php
355
			ob_start();
356
			Publicize_UI::connected_notice( $service_name );
357
			$update_notice = ob_get_clean();
358
			if ( ! empty( $update_notice ) )
359
				echo $update_notice;
360
			?>
361
362
			<?php Publicize_UI::global_checkbox( $service_name, $_REQUEST['connection'] ); ?>
363
364
			<p style="text-align: center;">
365
				<input type="submit" value="<?php esc_attr_e( 'OK', 'jetpack' ) ?>" class="button <?php echo $service_name; ?>-options save-options" name="save" data-connection="<?php echo esc_attr( $_REQUEST['connection'] ); ?>" rel="<?php echo wp_create_nonce( 'save_'.$service_name.'_token_' . $_REQUEST['connection'] ) ?>" />
366
			</p> <br />
367
		</div>
368
		<?php
369
	}
370
371
	/**
372
	 * CSS for styling the publicize message box and counter that displays on the post page.
373
	 * There is also some JavaScript for length counting and some basic display effects.
374
	 */
375
	function post_page_metabox_assets() {
376
		global $post;
377
		$user_id = empty( $post->post_author ) ? $GLOBALS['user_ID'] : $post->post_author;
0 ignored issues
show
Unused Code introduced by
$user_id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
378
379
		$default_prefix = $this->publicize->default_prefix;
380
		$default_prefix = preg_replace( '/%([0-9])\$s/', "' + %\\1\$s + '", esc_js( $default_prefix ) );
381
382
		$default_message = $this->publicize->default_message;
383
		$default_message = preg_replace( '/%([0-9])\$s/', "' + %\\1\$s + '", esc_js( $default_message ) );
384
385
		$default_suffix = $this->publicize->default_suffix;
386
		$default_suffix = preg_replace( '/%([0-9])\$s/', "' + %\\1\$s + '", esc_js( $default_suffix ) );
387
388
		$max_length = defined( 'JETPACK_PUBLICIZE_TWITTER_LENGTH' ) ? JETPACK_PUBLICIZE_TWITTER_LENGTH : 280;
389
		$max_length = $max_length - 24; // t.co link, space
390
391
		// for deprecation tooltip
392
		wp_enqueue_style( 'wp-pointer' );
393
		wp_enqueue_script( 'wp-pointer' );
394
395
		$this->google_plus_shut_down_tooltip_script();
396
397
		?>
398
399
<script type="text/javascript">
400
jQuery( function($) {
401
	var wpasTitleCounter    = $( '#wpas-title-counter' ),
402
		wpasTwitterCheckbox = $( '.wpas-submit-twitter' ).length,
403
		postTitle = $( '#title' ),
404
		wpasTitle = $( '#wpas-title' ).keyup( function() {
405
			var postTitleVal,
406
				length = wpasTitle.val().length;
407
408
			if ( ! length ) {
409
				length = wpasTitle.attr( 'placeholder' ).length;
410
			}
411
412
			wpasTitleCounter.text( length ).trigger( 'change' );
413
		} ),
414
		authClick = false;
415
416
	wpasTitleCounter.on( 'change', function( e ) {
417
		if ( wpasTwitterCheckbox && parseInt( $( e.currentTarget ).text(), 10 ) > <?php echo (int) $max_length; ?> ) {
418
			wpasTitleCounter.addClass( 'wpas-twitter-length-limit' );
419
		} else {
420
			wpasTitleCounter.removeClass( 'wpas-twitter-length-limit' );
421
		}
422
	} );
423
424
	// Keep the postTitle and the placeholder in sync
425
	postTitle.on( 'keyup', function( e ) {
426
		var url = $( '#sample-permalink' ).text();
427
		var defaultMessage = $.trim( '<?php printf( $default_prefix, 'url' ); printf( $default_message, 'e.currentTarget.value', 'url' ); printf( $default_suffix, 'url' ); ?>' )
428
			.replace( /<[^>]+>/g,'');
429
430
		wpasTitle.attr( 'placeholder', defaultMessage );
431
		wpasTitle.trigger( 'keyup' );
432
	} );
433
434
	// set the initial placeholder
435
	postTitle.trigger( 'keyup' );
436
437
	// If a custom message has been provided, open the UI so the author remembers
438
	if ( wpasTitle.val() && ! wpasTitle.prop( 'disabled' ) && wpasTitle.attr( 'placeholder' ) !== wpasTitle.val() ) {
439
		$( '#publicize-form' ).show();
440
		$( '#publicize-defaults' ).hide();
441
		$( '#publicize-form-edit' ).hide();
442
	}
443
444
	$('#publicize-disconnected-form-show').click( function() {
445
		$('#publicize-form').slideDown( 'fast' );
446
		$(this).hide();
447
	} );
448
449
	$('#publicize-disconnected-form-hide').click( function() {
450
		$('#publicize-form').slideUp( 'fast' );
451
		$('#publicize-disconnected-form-show').show();
452
	} );
453
454
	$('#publicize-form-edit').click( function() {
455
		$('#publicize-form').slideDown( 'fast', function() {
456
			var selBeg = 0, selEnd = 0;
457
			wpasTitle.focus();
458
459
			if ( ! wpasTitle.text() ) {
460
				wpasTitle.text( wpasTitle.attr( 'placeholder' ) );
461
462
				selBeg = wpasTitle.text().indexOf( postTitle.val() );
463
				if ( selBeg < 0 ) {
464
					selBeg = 0;
465
				} else {
466
					selEnd = selBeg + postTitle.val().length;
467
				}
468
469
				var domObj = wpasTitle.get(0);
470
				if ( domObj.setSelectionRange ) {
471
					domObj.setSelectionRange( selBeg, selEnd );
472
				} else if ( domObj.createTextRange ) {
473
					var r = domObj.createTextRange();
474
					r.moveStart( 'character', selBeg );
475
					r.moveEnd( 'character', selEnd );
476
					r.select();
477
				}
478
			}
479
		} );
480
481
		$('#publicize-defaults').hide();
482
		$(this).hide();
483
		return false;
484
	} );
485
486
	$('#publicize-form-hide').click( function() {
487
		var newList = $.map( $('#publicize-form').slideUp( 'fast' ).find( ':checked' ), function( el ) {
488
			return $.trim( $(el).parent( 'label' ).text() );
489
		} );
490
		$('#publicize-defaults').html( '<strong>' + newList.join( '</strong>, <strong>' ) + '</strong>' ).show();
491
		$('#publicize-form-edit').show();
492
		return false;
493
	} );
494
495
	$('.authorize-link').click( function() {
496
		if ( authClick ) {
497
			return false;
498
		}
499
		authClick = true;
500
		$(this).after( '<img src="images/loading.gif" class="alignleft" style="margin: 0 .5em" />' );
501
		$.ajaxSetup( { async: false } );
502
503
		if ( window.wp && window.wp.autosave ) {
504
			window.wp.autosave.server.triggerSave();
505
		} else {
506
			autosave();
507
		}
508
509
		return true;
510
	} );
511
512
	$( '.pub-service' ).click( function() {
513
		var service = $(this).data( 'service' ),
514
			fakebox = '<input id="wpas-submit-' + service + '" type="hidden" value="1" name="wpas[submit][' + service + ']" />';
515
		$( '#add-publicize-check' ).append( fakebox );
516
	} );
517
518
	publicizeConnTestStart = function() {
519
		$( '#pub-connection-tests' )
520
			.removeClass( 'below-h2' )
521
			.removeClass( 'error' )
522
			.removeClass( 'publicize-token-refresh-message' )
523
			.addClass( 'test-in-progress' )
524
			.html( '' );
525
		$.post( ajaxurl, { action: 'test_publicize_conns' }, publicizeConnTestComplete );
526
	}
527
528
	publicizeConnRefreshClick = function( event ) {
529
		event.preventDefault();
530
		var popupURL = event.currentTarget.href;
531
		var popupTitle = event.currentTarget.title;
532
		// open a popup window
533
		// when it is closed, kick off the tests again
534
		var popupWin = window.open( popupURL, popupTitle, '' );
535
		var popupWinTimer= window.setInterval( function() {
536
			if ( popupWin.closed !== false ) {
537
				window.clearInterval( popupWinTimer );
538
				publicizeConnTestStart();
539
			}
540
		}, 500 );
541
	}
542
543
	publicizeConnTestComplete = function( response ) {
544
		var testsSelector = $( '#pub-connection-tests' );
545
		testsSelector
546
			.removeClass( 'test-in-progress' )
547
			.removeClass( 'below-h2' )
548
			.removeClass( 'error' )
549
			.removeClass( 'publicize-token-refresh-message' )
550
			.html( '' );
551
552
		// If any of the tests failed, show some stuff
553
		var somethingShownAlready = false;
554
		var facebookNotice = false;
555
		$.each( response.data, function( index, testResult ) {
556
			// find the li for this connection
557
			if ( ! testResult.connectionTestPassed && testResult.userCanRefresh ) {
558
				if ( ! somethingShownAlready ) {
559
					testsSelector
560
						.addClass( 'below-h2' )
561
						.addClass( 'error' )
562
						.addClass( 'publicize-token-refresh-message' )
563
						.append( "<p><?php echo esc_html( __( 'Before you hit Publish, please refresh the following connection(s) to make sure we can Publicize your post:', 'jetpack' ) ); ?></p>" );
564
					somethingShownAlready = true;
565
				}
566
567
				if ( testResult.userCanRefresh ) {
568
					testsSelector.append( '<p/>' );
569
					$( '<a/>', {
570
						'class'  : 'pub-refresh-button button',
571
						'title'  : testResult.refreshText,
572
						'href'   : testResult.refreshURL,
573
						'text'   : testResult.refreshText,
574
						'target' : '_refresh_' + testResult.serviceName
575
					} )
576
						.appendTo( testsSelector.children().last() )
577
						.click( publicizeConnRefreshClick );
578
				}
579
			}
580
581
			if( ! testResult.connectionTestPassed && ! testResult.userCanRefresh ) {
582
				$( '#wpas-submit-' + testResult.unique_id ).prop( "checked", false ).prop( "disabled", true );
583
				if ( ! facebookNotice ) {
584
					var message = '<p>'
585
						+ testResult.connectionTestMessage
586
						+ '</p><p>'
587
						+ ' <a class="button" href="<?php echo esc_url( $this->publicize_settings_url ); ?>" rel="noopener noreferrer" target="_blank">'
588
						+ '<?php echo esc_html( __( 'Update Your Sharing Settings' ,'jetpack' ) ); ?>'
589
						+ '</a>'
590
						+ '<p>';
591
592
					testsSelector
593
						.addClass( 'below-h2' )
594
						.addClass( 'error' )
595
						.addClass( 'publicize-token-refresh-message' )
596
						.append( message );
597
					facebookNotice = true;
598
				}
599
			}
600
		} );
601
	}
602
603
	$( document ).ready( function() {
604
		// If we have the #pub-connection-tests div present, kick off the connection test
605
		if ( $( '#pub-connection-tests' ).length ) {
606
			publicizeConnTestStart();
607
		}
608
	} );
609
610
} );
611
</script>
612
613
<style type="text/css">
614
#publicize {
615
	line-height: 1.5;
616
}
617
#publicize ul {
618
	margin: 4px 0 4px 6px;
619
}
620
#publicize li {
621
	margin: 0;
622
}
623
#publicize textarea {
624
	margin: 4px 0 0;
625
	width: 100%
626
}
627
#publicize ul.not-connected {
628
	list-style: square;
629
	padding-left: 1em;
630
}
631
.publicize__notice-warning {
632
	display: inline-block;
633
	padding: 7px 10px;
634
	margin: 5px 0;
635
	border-left-width: 4px;
636
	border-left-style: solid;
637
	font-size: 12px;
638
	box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1);
639
}
640
.publicize__sharing-settings {
641
	display: block;
642
	text-decoration: none;
643
	margin-top: 8px;
644
}
645
.publicize__sharing-settings:after {
646
	content: "\f504";
647
	font: normal 18px/.5em dashicons;
648
	speak: none;
649
	margin-left: 5px;
650
	vertical-align: middle;
651
}
652
#publicize-title:before {
653
	content: "\f237";
654
	font: normal 20px/1 dashicons;
655
	speak: none;
656
	margin-left: -1px;
657
	padding-right: 3px;
658
	vertical-align: top;
659
	-webkit-font-smoothing: antialiased;
660
	color: #82878c;
661
}
662
.post-new-php .authorize-link, .post-php .authorize-link {
663
	line-height: 1.5em;
664
}
665
.post-new-php .authorize-message, .post-php .authorize-message {
666
	margin-bottom: 0;
667
}
668
#poststuff #publicize .updated p {
669
	margin: .5em 0;
670
}
671
.wpas-twitter-length-limit {
672
	color: red;
673
}
674
.publicize-disabled-service-message .dashicons {
675
	font-size: 16px;
676
	text-decoration: none;
677
}
678
</style><?php
679
	}
680
681
	/**
682
	 * @param string $service_label Service's human-readable Label ("Facebook", "Twitter", ...)
683
	 * @param string $display_name Connection's human-readable Username ("@jetpack", ...)
684
	 * @return string
685
	 */
686
	private function connection_label( $service_label, $display_name ) {
687
		return sprintf(
688
			/* translators: %1$s: Service Name (Facebook, Twitter, ...), %2$s: Username on Service (@jetpack, ...) */
689
			__( '%1$s: %2$s', 'jetpack' ),
690
			$service_label,
691
			$display_name
692
		);
693
	}
694
695
	/**
696
	 * Extracts the connections that require reauthentication, for example, LinkedIn, when it switched v1 to v2 of its API.
697
	 *
698
	 * @return array Connections that must be reauthenticated
699
	 */
700
	function get_must_reauth_connections() {
701
		$must_reauth = array();
702
		$connections = $this->publicize->get_connections( 'linkedin' );
703
		if ( is_array( $connections ) ) {
704
			foreach ( $connections as $index => $connection ) {
705
				if ( $this->publicize->is_invalid_linkedin_connection( $connection ) ) {
706
					$must_reauth[ $index ] = 'LinkedIn';
707
				}
708
			}
709
		}
710
		return $must_reauth;
711
	}
712
713
	/**
714
	* Controls the metabox that is displayed on the post page
715
	* Allows the user to customize the message that will be sent out to the social network, as well as pick which
716
	* networks to publish to. Also displays the character counter and some other information.
717
	*/
718
	function post_page_metabox() {
719
		global $post;
720
721
		if ( ! $this->publicize->post_type_is_publicizeable( $post->post_type ) )
722
			return;
723
724
		$user_id = empty( $post->post_author ) ? $GLOBALS['user_ID'] : $post->post_author;
0 ignored issues
show
Unused Code introduced by
$user_id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
725
		$connections_data = $this->publicize->get_filtered_connection_data();
726
727
		$available_services = $this->publicize->get_services( 'all' );
728
729
		if ( ! is_array( $available_services ) )
730
			$available_services = array();
731
732
		if ( ! is_array( $connections_data ) )
733
			$connections_data = array();
734
		?>
735
		<div id="publicize" class="misc-pub-section misc-pub-section-last">
736
			<span id="publicize-title">
737
			<?php
738
				esc_html_e( 'Publicize:', 'jetpack' );
739
740
				if ( 0 < count( $connections_data ) ) :
741
					$publicize_form = $this->get_metabox_form_connected( $connections_data );
742
743
					$must_reauth = $this->get_must_reauth_connections();
744
					if ( ! empty( $must_reauth ) ) {
745
						foreach ( $must_reauth as $connection_name ) {
746
							?>
747
							<span class="notice-warning publicize__notice-warning">
748
				                <?php
749
				                printf( esc_html__(
750
					                'Your %s connection needs to be reauthenticated to continue working – head to Sharing to take care of it.',
751
							'jetpack'
752
				                ), $connection_name );
753
				                ?>
754
								<a
755
										class="publicize__sharing-settings"
756
										href="<?php echo publicize_calypso_url() ?>"
757
								><?php esc_html_e( 'Go to Sharing settings', 'jetpack' ) ?></a>
758
							</span>
759
							<?php
760
						}
761
						?>
762
						<?php
763
					}
764
765
					$labels = array();
766
					$has_google_plus = false;
767
					foreach ( $connections_data as $connection_data ) {
768
						if ( ! $connection_data['enabled'] ) {
769
							continue;
770
						}
771
772
						if ( 'google_plus' === $connection_data['service_name'] ) {
773
							$has_google_plus = true;
774
						}
775
776
						$labels[] = sprintf(
777
							'<strong>%s</strong>',
778
							esc_html( $this->connection_label( $connection_data['service_label'], $connection_data['display_name'] ) )
779
						);
780
					}
781
782
				?>
783
					<span id="publicize-defaults"><?php echo join( ', ', $labels ); ?></span>
784
				<?php if ( $has_google_plus ) : ?>
785
					<div class="notice inline notice-warning publicize-disabled-service-message">
786
						<p>
787
							<strong><?php esc_html_e( 'Google+ support is being removed', 'jetpack' ); ?></strong>
788
							<a href="javascript:void(0)" id="jetpack-gplus-deprecated-notice">
789
								<?php esc_html_e( 'Why?', 'jetpack' ); ?>
790
								<span class="dashicons dashicons-info"></span>
791
							</a>
792
						</p>
793
					</div>
794
				<?php endif; ?>
795
					<a href="#" id="publicize-form-edit"><?php esc_html_e( 'Edit', 'jetpack' ); ?></a>&nbsp;<a href="<?php echo esc_url( $this->publicize_settings_url ); ?>" rel="noopener noreferrer" target="_blank"><?php _e( 'Settings', 'jetpack' ); ?></a><br />
796
				<?php
797
798
				else :
799
					$publicize_form = $this->get_metabox_form_disconnected( $available_services );
800
801
				?>
802
					<strong><?php echo __( 'Not Connected', 'jetpack' ); ?></strong>
803
					<a href="#" id="publicize-disconnected-form-show"><?php esc_html_e( 'Edit', 'jetpack' ); ?></a><br />
804
				<?php
805
806
				endif;
807
			?>
808
			</span>
809
			<?php
810
			/**
811
			 * Filter the Publicize details form.
812
			 *
813
			 * @module publicize
814
			 *
815
			 * @since 2.0.0
816
			 *
817
			 * @param string $publicize_form Publicize Details form appearing above Publish button in the editor.
818
			 */
819
			echo apply_filters( 'publicize_form', $publicize_form );
820
			?>
821
		</div> <?php // #publicize
822
	}
823
824
	/**
825
	 * Generates HTML content for connections form.
826
	 *
827
	 * @since 6.7
828
	 *
829
	 * @global WP_Post $post The current post instance being published.
830
	 *
831
	 * @param array $connections_data
832
	 *
833
	 * @return array {
834
	 *     Array of content for generating connection form.
835
	 *
836
	 *     @type string HTML content of form
837
	 *     @type array {
838
	 *     		Array of connection labels for active connections only.
839
	 *
840
	 *          @type string Connection label string.
841
	 *     }
842
	 * }
843
	 */
844
	private function get_metabox_form_connected( $connections_data ) {
845
		global $post;
846
847
		$all_done = $this->publicize->post_is_done_sharing();
848
		$all_connections_done = true;
849
850
		ob_start();
851
852
		?>
853
		<div id="publicize-form" class="hide-if-js">
854
			<ul>
855
		<?php
856
857
		foreach ( $connections_data as $connection_data ) {
858
			$all_connections_done = $all_connections_done && $connection_data['done'];
859
		?>
860
861
				<li>
862
					<label for="wpas-submit-<?php echo esc_attr( $connection_data['unique_id'] ); ?>">
863
						<input
864
							type="checkbox"
865
							name="wpas[submit][<?php echo esc_attr( $connection_data['unique_id'] ); ?>]"
866
							id="wpas-submit-<?php echo esc_attr( $connection_data['unique_id'] ); ?>"
867
							class="wpas-submit-<?php echo esc_attr( $connection_data['service_name'] ); ?>"
868
							value="1"
869
						<?php
870
							checked( true, $connection_data['enabled'] );
871
							disabled( false, $connection_data['toggleable'] );
872
						?>
873
						/>
874
					<?php if ( $connection_data['enabled'] && ! $connection_data['toggleable'] ) : // Need to submit a value to force a global connection to POST ?>
875
						<input
876
							type="hidden"
877
							name="wpas[submit][<?php echo esc_attr( $connection_data['unique_id'] ); ?>]"
878
							value="1"
879
						/>
880
					<?php endif; ?>
881
882
						<?php echo esc_html( $this->connection_label( $connection_data['service_label'], $connection_data['display_name'] ) ); ?>
883
884
					</label>
885
				</li>
886
		<?php
887
		}
888
889
		$title = get_post_meta( $post->ID, $this->publicize->POST_MESS, true );
890
		if ( ! $title ) {
891
			$title = '';
892
		}
893
894
		$all_done = $all_done || $all_connections_done;
895
896
		?>
897
898
			</ul>
899
900
			<label for="wpas-title"><?php _e( 'Custom Message:', 'jetpack' ); ?></label>
901
			<span id="wpas-title-counter" class="alignright hide-if-no-js">0</span>
902
			<textarea name="wpas_title" id="wpas-title"<?php disabled( $all_done ); ?>><?php echo esc_textarea( $title ); ?></textarea>
903
			<a href="#" class="hide-if-no-js button" id="publicize-form-hide"><?php esc_html_e( 'OK', 'jetpack' ); ?></a>
904
			<input type="hidden" name="wpas[0]" value="1" />
905
		</div>
906
907
		<?php if ( ! $all_done ) : ?>
908
			<div id="pub-connection-tests"></div>
909
		<?php endif; ?>
910
		<?php // #publicize-form
911
912
		return ob_get_clean();
913
	}
914
915
	private function get_metabox_form_disconnected( $available_services ) {
916
		ob_start();
917
		?><div id="publicize-form" class="hide-if-js">
918
			<div id="add-publicize-check" style="display: none;"></div>
919
920
			<?php _e( 'Connect to', 'jetpack' ); ?>:
921
922
			<ul class="not-connected">
923
				<?php foreach ( $available_services as $service_name => $service ) : ?>
924
				<li>
925
					<a class="pub-service" data-service="<?php echo esc_attr( $service_name ); ?>" title="<?php echo esc_attr( sprintf( __( 'Connect and share your posts on %s', 'jetpack' ), $this->publicize->get_service_label( $service_name ) ) ); ?>" rel="noopener noreferrer" target="_blank" href="<?php echo esc_url( $this->publicize->connect_url( $service_name ) ); ?>">
926
						<?php echo esc_html( $this->publicize->get_service_label( $service_name ) ); ?>
927
					</a>
928
				</li>
929
				<?php endforeach; ?>
930
			</ul>
931
			<a href="#" class="hide-if-no-js button" id="publicize-disconnected-form-hide"><?php esc_html_e( 'OK', 'jetpack' ); ?></a>
932
		</div><?php // #publicize-form
933
		return ob_get_clean();
934
	}
935
936
	private function google_plus_shut_down_notice() {
937
		return wp_kses(
938
			sprintf(
939
				/* Translators: placeholder is a link to an announcement post on Google's blog. */
940
				__(
941
					'<h3>Google+ Support is being removed</h3><p>Google recently <a href="%1$s" target="_blank">announced</a> that Google+ is shutting down in April 2019, and access via third-party tools like Jetpack will cease in March 2019.</p><p>For now, you can still post to Google+ using existing connections, but you cannot add new connections. The ability to post will be removed in early 2019.</p>',
942
					'jetpack'
943
				),
944
				esc_url( 'https://www.blog.google/technology/safety-security/expediting-changes-google-plus/' )
945
			),
946
			array(
947
				'a'  => array(
948
					'href' => true,
949
					'target' => true,
950
				),
951
				'h3' => true,
952
				'p'  => true,
953
			)
954
		);
955
	}
956
957
	private function google_plus_shut_down_tooltip_script() {
958
		$google_plus_exp_msg = $this->google_plus_shut_down_notice();
959
	?>
960
		<script>
961
		// deprecation tooltip
962
		(function($){
963
			var setup = function() {
964
				$('#jetpack-gplus-deprecated-notice').first().pointer(
965
					{
966
						content: decodeURIComponent( "<?php echo rawurlencode( $google_plus_exp_msg ); ?>" ),
967
						position: {
968
							edge: "right",
969
							align: "bottom"
970
						},
971
						pointerClass: "wp-pointer arrow-bottom",
972
						pointerWidth: 420
973
					}
974
				).click( function( e ) {
975
					e.preventDefault();
976
					$( this ).pointer( 'open' );
977
				} );
978
			};
979
			$(document).ready( setup );
980
		})(jQuery);
981
		</script>
982
	<?php
983
	}
984
}
985