Completed
Push — fix/restore-publicize-ui ( 84c4b3 )
by
unknown
06:15
created

Publicize_UI::options_page_other()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 21
rs 9.584
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
	/**
14
	 * @var string URL to Sharing settings page in wordpress.com
15
	 */
16
	protected $publicize_settings_url = '';
17
18
	/**
19
	* Hooks into WordPress to display the various pieces of UI and load our assets
20
	*/
21
	function __construct() {
22
		global $publicize;
23
24
		$this->publicize = $publicize = new Publicize;
25
26
		add_action( 'init', array( $this, 'init' ) );
27
	}
28
29
	function init() {
30
		$this->publicize_settings_url = publicize_calypso_url();
31
32
		// Show only to users with the capability required to manage their Publicize connections.
33
		if ( ! $this->publicize->current_user_can_access_publicize_data() ) {
34
			return;
35
		}
36
37
		// assets (css, js)
38
		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...
39
			add_action( 'load-settings_page_sharing', array( $this, 'load_assets' ) );
40
		}
41
		add_action( 'admin_head-post.php', array( $this, 'post_page_metabox_assets' ) );
42
		add_action( 'admin_head-post-new.php', array( $this, 'post_page_metabox_assets' ) );
43
44
		// management of publicize (sharing screen, ajax/lightbox popup, and metabox on post screen)
45
		add_action( 'pre_admin_screen_sharing', array( $this, 'admin_page' ) );
46
		add_action( 'post_submitbox_misc_actions', array( $this, 'post_page_metabox' ) );
47
	}
48
49
	/**
50
	 * If the ShareDaddy plugin is not active we need to add the sharing settings page to the menu still
51
	 */
52
	function sharing_menu() {
53
		add_submenu_page(
54
			'options-general.php',
55
			esc_html__( 'Sharing Settings', 'jetpack' ),
56
			esc_html__( 'Sharing', 'jetpack' ),
57
			'publish_posts',
58
			'sharing',
59
			array( $this, 'wrapper_admin_page' )
60
		);
61
	}
62
63
	function wrapper_admin_page() {
64
		Jetpack_Admin_Page::wrap_ui( array( $this, 'management_page' ), array( 'is-wide' => true ) );
65
	}
66
67
	/**
68
	 * Management page to load if Sharedaddy is not active so the 'pre_admin_screen_sharing' action exists.
69
	 */
70
	function management_page() { ?>
71
		<div class="wrap">
72
			<div class="icon32" id="icon-options-general"><br /></div>
73
			<h1><?php esc_html_e( 'Sharing Settings', 'jetpack' ); ?></h1>
74
75
			<?php
76
			/** This action is documented in modules/sharedaddy/sharing.php */
77
			do_action( 'pre_admin_screen_sharing' );
78
			?>
79
80
		</div> <?php
81
	}
82
83
	/**
84
	 * styling for the sharing screen and popups
85
	 * JS for the options and switching
86
	 */
87
	function load_assets() {
88
		wp_enqueue_script(
89
			'publicize',
90
			Jetpack::get_file_url_for_environment(
91
				'_inc/build/publicize/assets/publicize.min.js',
92
				'modules/publicize/assets/publicize.js'
93
			),
94
			array( 'jquery', 'thickbox' ),
95
			'20121019'
96
		);
97
		if ( is_rtl() ) {
98
			wp_enqueue_style( 'publicize', plugins_url( 'assets/rtl/publicize-rtl.css', __FILE__ ), array(), '20180301' );
99
		} else {
100
			wp_enqueue_style( 'publicize', plugins_url( 'assets/publicize.css', __FILE__ ), array(), '20180301' );
101
		}
102
 		Jetpack_Admin_Page::load_wrapper_styles();
103
		wp_enqueue_style( 'social-logos' );
104
 		// for deprecation tooltip
105
		wp_enqueue_style( 'wp-pointer' );
106
		wp_enqueue_script( 'wp-pointer' );
107
 		add_thickbox();
108
	}
109
110
	public static function connected_notice( $service_name ) { ?>
111
		<div class='updated'>
112
			<p><?php
113
 			if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
114
				$platform =  'WordPress.com';
115
			} else {
116
				$platform = 'Jetpack';
117
			}
118
 			printf(
119
				/* translators: %1$s: Service Name (Facebook, Twitter, ...), %2$s: Site type (WordPress.com or Jetpack) */
120
				__( 'You have successfully connected your %1$s account with %2$s.', 'jetpack' ),
121
				Publicize::get_service_label( $service_name ),
122
				$platform
123
			); ?></p>
124
		</div><?php
125
	}
126
127
	/**
128
	 * Lists the current user's publicized accounts for the blog
129
	 * looks exactly like Publicize v1 for now, UI and functionality updates will come after the move to keyring
130
	 */
131
	function admin_page() {
132
		$_blog_id = get_current_blog_id();
133
		?>
134
135
 		<form action="" id="publicize-form">
136
			<h2 id="publicize"><?php _e( 'Publicize', 'jetpack' ) ?></h2>
137
138
 			<?php
139
				if ( ! empty( $_GET['action'] ) && 'deny' == $_GET['action'] ) {
140
					$this->denied_notice();
0 ignored issues
show
Bug introduced by
The method denied_notice() does not seem to exist on object<Publicize_UI>.

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

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

Loading history...
141
				}
142
			?>
143
144
 			<p>
145
				<?php esc_html_e( 'Connect your blog to popular social networking sites and automatically share new posts with your friends.', 'jetpack' ) ?>
146
				<?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' ); ?>
147
			</p>
148
149
 			<?php
150
			if ( $this->in_jetpack ) {
151
				$doc_link = "http://jetpack.com/support/publicize/";
152
			} else {
153
				$doc_link = "http://en.support.wordpress.com/publicize/";
154
			}
155
			?>
156
157
 			<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>
158
159
 			<div id="publicize-services-block">
160
				<?php
161
				$services = $this->publicize->get_services( 'all' );
162
				$total_num_of_services = count ( $services );
163
				$service_num = 0;?>
164
165
 				<div class='left'>
166
167
 				<?php
168
				foreach ( $services as $service_name => $service ) :
169
					// do not display any service that cannot be connected anymore (deprecated).
170
					$connect_url = $this->publicize->can_connect_service( $service_name ) ?
171
						$this->publicize->connect_url( $service_name ) :
172
						null;
173
 					if ( $service_num == ( round ( ( $total_num_of_services / 2 ), 0 ) ) )
174
						echo "</div><div class='right'>";
175
					$service_num++;
176
					?>
177
					<div class="publicize-service-entry" <?php if ( $service_num > 0 ): ?>class="connected"<?php endif; ?> >
178
						<div id="<?php echo esc_attr( $service_name ); ?>" class="publicize-service-left">
179
							<?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...
180
								<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>
181
							<?php else: ?>
182
								<span><?php echo $this->publicize->get_service_label( $service_name ); ?></span>
183
							<?php endif; ?>
184
						</div>
185
186
187
 						<div class="publicize-service-right">
188
							<?php if ( $this->publicize->is_enabled( $service_name ) && $connections = $this->publicize->get_connections( $service_name ) ) : ?>
189
								<ul>
190
									<?php
191
									foreach( $connections as $c ) :
192
										$id = $this->publicize->get_connection_id( $c );
193
										$disconnect_url = $this->publicize->disconnect_url( $service_name, $id );
194
 										$cmeta = $this->publicize->get_connection_meta( $c );
195
										$profile_link = $this->publicize->get_profile_link( $service_name, $c );
196
										$connection_display = $this->publicize->get_display_name( $service_name, $c );
197
 										$options_nonce = wp_create_nonce( 'options_page_' . $service_name . '_' . $id ); ?>
198
199
 										<?php if ( $this->publicize->show_options_popup( $service_name, $c ) ): ?>
200
										<script type="text/javascript">
201
										jQuery(document).ready( function($) {
202
											showOptionsPage.call(
203
											this,
204
											'<?php echo esc_js( $service_name ); ?>',
205
											'<?php echo esc_js( $options_nonce ); ?>',
206
											'<?php echo esc_js( $id ); ?>'
207
											);
208
										} );
209
										</script>
210
										<?php endif; ?>
211
212
 										<li class="publicize-connection" data-connection-id="<?php echo esc_attr( $id ); ?>">
213
											<?php esc_html_e( 'Connected as:', 'jetpack' ); ?>
214
											<?php
215
											if ( !empty( $profile_link ) ) : ?>
216
												<a class="publicize-profile-link" href="<?php echo esc_url( $profile_link ); ?>" target="_top">
217
													<?php echo esc_html( $connection_display ); ?>
218
												</a><?php
219
											else :
220
												echo esc_html( $connection_display );
221
											endif;
222
											?>
223
224
 											<?php if ( 0 == $cmeta['connection_data']['user_id'] ) : ?>
225
												<small>(<?php esc_html_e( 'Shared', 'jetpack' ); ?>)</small>
226
227
 												<?php if ( current_user_can( $this->publicize->GLOBAL_CAP ) ) : ?>
228
													<a class="pub-disconnect-button" title="<?php esc_html_e( 'Disconnect', 'jetpack' ); ?>" href="<?php echo esc_url( $disconnect_url ); ?>" target="_top">×</a>
229
												<?php endif; ?>
230
231
 											<?php else : ?>
232
												<a class="pub-disconnect-button" title="<?php esc_html_e( 'Disconnect', 'jetpack' ); ?>" href="<?php echo esc_url( $disconnect_url ); ?>" target="_top">×</a>
233
											<?php endif; ?>
234
235
 											<br/>
236
											<div class="pub-connection-test test-in-progress" id="pub-connection-test-<?php echo esc_attr( $id ); ?>" >
237
											</div>
238
										</li>
239
240
 										<?php
241
									endforeach;
242
									?>
243
								</ul>
244
							<?php endif; ?>
245
246
 							<?php
247
							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...
248
								$connections = $this->publicize->get_connections( $service_name );
249
								if ( empty( $connections ) ) {
250
									printf(
251
										'<a id="%1$s" class="publicize-add-connection button" href="%2$s" target="_top">%3$s</a>',
252
										esc_attr( $service_name ),
253
										esc_url( $connect_url ),
254
										esc_html__( 'Connect', 'jetpack' )
255
									);
256
								} else {
257
									printf(
258
										'<a id="%1$s" class="publicize-add-connection button add-new" href="%2$s" target="_top">%3$s</a>',
259
										esc_attr( $service_name ),
260
										esc_url( $connect_url ),
261
										esc_html__( 'Add New', 'jetpack' )
262
									);
263
								}
264
							}
265
							?>
266
						</div>
267
					</div>
268
				<?php endforeach; ?>
269
				</div>
270
				<script>
271
				(function($){
272
					$('.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' ) ); ?>' ) ) {
273
								return true;
274
							} else {
275
							e.preventDefault();
276
							return false;
277
						}
278
					});
279
				})(jQuery);
280
				</script>
281
			</div>
282
283
 			<?php wp_nonce_field( "wpas_posts_{$_blog_id}", "_wpas_posts_{$_blog_id}_nonce" ); ?>
284
			<input type="hidden" id="wpas_ajax_blog_id" name="wpas_ajax_blog_id" value="<?php echo $_blog_id; ?>" />
285
		</form><?php
286
	}
287
288
	public static function global_checkbox( $service_name, $id ) {
289
		global $publicize;
290
		if ( current_user_can( $publicize->GLOBAL_CAP ) ) : ?>
291
			<p>
292
				<input id="globalize_<?php echo $service_name; ?>" type="checkbox" name="global" value="<?php echo wp_create_nonce( 'publicize-globalize-' . $id ) ?>" />
293
				<label for="globalize_<?php echo $service_name; ?>"><?php _e( 'Make this connection available to all users of this blog?', 'jetpack' ); ?></label>
294
			</p>
295
		<?php endif;
296
	}
297
298
	public static function options_page_other( $service_name ) {
299
		// Nonce check
300
		check_admin_referer( "options_page_{$service_name}_" . $_REQUEST['connection'] );
301
		?>
302
		<div id="thickbox-content">
303
			<?php
304
			ob_start();
305
			Publicize_UI::connected_notice( $service_name );
306
			$update_notice = ob_get_clean();
307
			if ( ! empty( $update_notice ) )
308
				echo $update_notice;
309
			?>
310
311
 			<?php Publicize_UI::global_checkbox( $service_name, $_REQUEST['connection'] ); ?>
312
313
 			<p style="text-align: center;">
314
				<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'] ) ?>" />
315
			</p> <br />
316
		</div>
317
		<?php
318
	}
319
320
	/**
321
	 * CSS for styling the publicize message box and counter that displays on the post page.
322
	 * There is also some JavaScript for length counting and some basic display effects.
323
	 */
324
	function post_page_metabox_assets() {
325
		global $post;
326
		$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...
327
328
		$default_prefix = $this->publicize->default_prefix;
329
		$default_prefix = preg_replace( '/%([0-9])\$s/', "' + %\\1\$s + '", esc_js( $default_prefix ) );
330
331
		$default_message = $this->publicize->default_message;
332
		$default_message = preg_replace( '/%([0-9])\$s/', "' + %\\1\$s + '", esc_js( $default_message ) );
333
334
		$default_suffix = $this->publicize->default_suffix;
335
		$default_suffix = preg_replace( '/%([0-9])\$s/', "' + %\\1\$s + '", esc_js( $default_suffix ) );
336
337
		$max_length = defined( 'JETPACK_PUBLICIZE_TWITTER_LENGTH' ) ? JETPACK_PUBLICIZE_TWITTER_LENGTH : 280;
338
		$max_length = $max_length - 24; // t.co link, space
339
340
		?>
341
342
<script type="text/javascript">
343
jQuery( function($) {
344
	var wpasTitleCounter    = $( '#wpas-title-counter' ),
345
		wpasTwitterCheckbox = $( '.wpas-submit-twitter' ).length,
346
		postTitle = $( '#title' ),
347
		wpasTitle = $( '#wpas-title' ).keyup( function() {
348
			var postTitleVal,
349
				length = wpasTitle.val().length;
350
351
			if ( ! length ) {
352
				length = wpasTitle.attr( 'placeholder' ).length;
353
			}
354
355
			wpasTitleCounter.text( length ).trigger( 'change' );
356
		} ),
357
		authClick = false;
358
359
	wpasTitleCounter.on( 'change', function( e ) {
360
		if ( wpasTwitterCheckbox && parseInt( $( e.currentTarget ).text(), 10 ) > <?php echo (int) $max_length; ?> ) {
361
			wpasTitleCounter.addClass( 'wpas-twitter-length-limit' );
362
		} else {
363
			wpasTitleCounter.removeClass( 'wpas-twitter-length-limit' );
364
		}
365
	} );
366
367
	// Keep the postTitle and the placeholder in sync
368
	postTitle.on( 'keyup', function( e ) {
369
		var url = $( '#sample-permalink' ).text();
370
		var defaultMessage = $.trim( '<?php printf( $default_prefix, 'url' ); printf( $default_message, 'e.currentTarget.value', 'url' ); printf( $default_suffix, 'url' ); ?>' )
371
			.replace( /<[^>]+>/g,'');
372
373
		wpasTitle.attr( 'placeholder', defaultMessage );
374
		wpasTitle.trigger( 'keyup' );
375
	} );
376
377
	// set the initial placeholder
378
	postTitle.trigger( 'keyup' );
379
380
	// If a custom message has been provided, open the UI so the author remembers
381
	if ( wpasTitle.val() && ! wpasTitle.prop( 'disabled' ) && wpasTitle.attr( 'placeholder' ) !== wpasTitle.val() ) {
382
		$( '#publicize-form' ).show();
383
		$( '#publicize-defaults' ).hide();
384
		$( '#publicize-form-edit' ).hide();
385
	}
386
387
	$('#publicize-disconnected-form-show').click( function() {
388
		$('#publicize-form').slideDown( 'fast' );
389
		$(this).hide();
390
	} );
391
392
	$('#publicize-disconnected-form-hide').click( function() {
393
		$('#publicize-form').slideUp( 'fast' );
394
		$('#publicize-disconnected-form-show').show();
395
	} );
396
397
	$('#publicize-form-edit').click( function() {
398
		$('#publicize-form').slideDown( 'fast', function() {
399
			var selBeg = 0, selEnd = 0;
400
			wpasTitle.focus();
401
402
			if ( ! wpasTitle.text() ) {
403
				wpasTitle.text( wpasTitle.attr( 'placeholder' ) );
404
405
				selBeg = wpasTitle.text().indexOf( postTitle.val() );
406
				if ( selBeg < 0 ) {
407
					selBeg = 0;
408
				} else {
409
					selEnd = selBeg + postTitle.val().length;
410
				}
411
412
				var domObj = wpasTitle.get(0);
413
				if ( domObj.setSelectionRange ) {
414
					domObj.setSelectionRange( selBeg, selEnd );
415
				} else if ( domObj.createTextRange ) {
416
					var r = domObj.createTextRange();
417
					r.moveStart( 'character', selBeg );
418
					r.moveEnd( 'character', selEnd );
419
					r.select();
420
				}
421
			}
422
		} );
423
424
		$('#publicize-defaults').hide();
425
		$(this).hide();
426
		return false;
427
	} );
428
429
	$('#publicize-form-hide').click( function() {
430
		var newList = $.map( $('#publicize-form').slideUp( 'fast' ).find( ':checked' ), function( el ) {
431
			return $.trim( $(el).parent( 'label' ).text() );
432
		} );
433
		$('#publicize-defaults').html( '<strong>' + newList.join( '</strong>, <strong>' ) + '</strong>' ).show();
434
		$('#publicize-form-edit').show();
435
		return false;
436
	} );
437
438
	$('.authorize-link').click( function() {
439
		if ( authClick ) {
440
			return false;
441
		}
442
		authClick = true;
443
		$(this).after( '<img src="images/loading.gif" class="alignleft" style="margin: 0 .5em" />' );
444
		$.ajaxSetup( { async: false } );
445
446
		if ( window.wp && window.wp.autosave ) {
447
			window.wp.autosave.server.triggerSave();
448
		} else {
449
			autosave();
450
		}
451
452
		return true;
453
	} );
454
455
	$( '.pub-service' ).click( function() {
456
		var service = $(this).data( 'service' ),
457
			fakebox = '<input id="wpas-submit-' + service + '" type="hidden" value="1" name="wpas[submit][' + service + ']" />';
458
		$( '#add-publicize-check' ).append( fakebox );
459
	} );
460
461
	publicizeConnTestStart = function() {
462
		$( '#pub-connection-tests' )
463
			.removeClass( 'below-h2' )
464
			.removeClass( 'error' )
465
			.removeClass( 'publicize-token-refresh-message' )
466
			.addClass( 'test-in-progress' )
467
			.html( '' );
468
		$.post( ajaxurl, { action: 'test_publicize_conns' }, publicizeConnTestComplete );
469
	}
470
471
	publicizeConnRefreshClick = function( event ) {
472
		event.preventDefault();
473
		var popupURL = event.currentTarget.href;
474
		var popupTitle = event.currentTarget.title;
475
		// open a popup window
476
		// when it is closed, kick off the tests again
477
		var popupWin = window.open( popupURL, popupTitle, '' );
478
		var popupWinTimer= window.setInterval( function() {
479
			if ( popupWin.closed !== false ) {
480
				window.clearInterval( popupWinTimer );
481
				publicizeConnTestStart();
482
			}
483
		}, 500 );
484
	}
485
486
	publicizeConnTestComplete = function( response ) {
487
		var testsSelector = $( '#pub-connection-tests' );
488
		testsSelector
489
			.removeClass( 'test-in-progress' )
490
			.removeClass( 'below-h2' )
491
			.removeClass( 'error' )
492
			.removeClass( 'publicize-token-refresh-message' )
493
			.html( '' );
494
495
		// If any of the tests failed, show some stuff
496
		var somethingShownAlready = false;
497
		var facebookNotice = false;
498
		$.each( response.data, function( index, testResult ) {
499
			// find the li for this connection
500
			if ( ! testResult.connectionTestPassed && testResult.userCanRefresh ) {
501
				if ( ! somethingShownAlready ) {
502
					testsSelector
503
						.addClass( 'below-h2' )
504
						.addClass( 'error' )
505
						.addClass( 'publicize-token-refresh-message' )
506
						.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>" );
507
					somethingShownAlready = true;
508
				}
509
510
				if ( testResult.userCanRefresh ) {
511
					testsSelector.append( '<p/>' );
512
					$( '<a/>', {
513
						'class'  : 'pub-refresh-button button',
514
						'title'  : testResult.refreshText,
515
						'href'   : testResult.refreshURL,
516
						'text'   : testResult.refreshText,
517
						'target' : '_refresh_' + testResult.serviceName
518
					} )
519
						.appendTo( testsSelector.children().last() )
520
						.click( publicizeConnRefreshClick );
521
				}
522
			}
523
524
			if( ! testResult.connectionTestPassed && ! testResult.userCanRefresh ) {
525
				$( '#wpas-submit-' + testResult.unique_id ).prop( "checked", false ).prop( "disabled", true );
526
				if ( ! facebookNotice ) {
527
					var message = '<p>'
528
						+ testResult.connectionTestMessage
529
						+ '</p><p>'
530
						+ ' <a class="button" href="<?php echo esc_url( $this->publicize_settings_url ); ?>" rel="noopener noreferrer" target="_blank">'
531
						+ '<?php echo esc_html( __( 'Update Your Sharing Settings' ,'jetpack' ) ); ?>'
532
						+ '</a>'
533
						+ '<p>';
534
535
					testsSelector
536
						.addClass( 'below-h2' )
537
						.addClass( 'error' )
538
						.addClass( 'publicize-token-refresh-message' )
539
						.append( message );
540
					facebookNotice = true;
541
				}
542
			}
543
		} );
544
	}
545
546
	$( document ).ready( function() {
547
		// If we have the #pub-connection-tests div present, kick off the connection test
548
		if ( $( '#pub-connection-tests' ).length ) {
549
			publicizeConnTestStart();
550
		}
551
	} );
552
553
} );
554
</script>
555
556
<style type="text/css">
557
#publicize {
558
	line-height: 1.5;
559
}
560
#publicize ul {
561
	margin: 4px 0 4px 6px;
562
}
563
#publicize li {
564
	margin: 0;
565
}
566
#publicize textarea {
567
	margin: 4px 0 0;
568
	width: 100%
569
}
570
#publicize ul.not-connected {
571
	list-style: square;
572
	padding-left: 1em;
573
}
574
.publicize__notice-warning {
575
	display: block;
576
	padding: 7px 10px;
577
	margin: 5px 0;
578
	border-left-width: 4px;
579
	border-left-style: solid;
580
	font-size: 12px;
581
	box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1);
582
}
583
.publicize-external-link {
584
	display: block;
585
	text-decoration: none;
586
	margin-top: 8px;
587
}
588
.publicize-external-link__text {
589
	text-decoration: underline;
590
}
591
#publicize-title:before {
592
	content: "\f237";
593
	font: normal 20px/1 dashicons;
594
	speak: none;
595
	margin-left: -1px;
596
	padding-right: 3px;
597
	vertical-align: top;
598
	-webkit-font-smoothing: antialiased;
599
	color: #82878c;
600
}
601
.post-new-php .authorize-link, .post-php .authorize-link {
602
	line-height: 1.5em;
603
}
604
.post-new-php .authorize-message, .post-php .authorize-message {
605
	margin-bottom: 0;
606
}
607
#poststuff #publicize .updated p {
608
	margin: .5em 0;
609
}
610
.wpas-twitter-length-limit {
611
	color: red;
612
}
613
.publicize__notice-warning .dashicons {
614
	font-size: 16px;
615
	text-decoration: none;
616
}
617
</style><?php
618
	}
619
620
	/**
621
	 * @param string $service_label Service's human-readable Label ("Facebook", "Twitter", ...)
622
	 * @param string $display_name Connection's human-readable Username ("@jetpack", ...)
623
	 * @return string
624
	 */
625
	private function connection_label( $service_label, $display_name ) {
626
		return sprintf(
627
			/* translators: %1$s: Service Name (Facebook, Twitter, ...), %2$s: Username on Service (@jetpack, ...) */
628
			__( '%1$s: %2$s', 'jetpack' ),
629
			$service_label,
630
			$display_name
631
		);
632
	}
633
634
	/**
635
	 * Extracts the connections that require reauthentication, for example, LinkedIn, when it switched v1 to v2 of its API.
636
	 *
637
	 * @return array Connections that must be reauthenticated
638
	 */
639
	function get_must_reauth_connections() {
640
		$must_reauth = array();
641
		$connections = $this->publicize->get_connections( 'linkedin' );
642
		if ( is_array( $connections ) ) {
643
			foreach ( $connections as $index => $connection ) {
644
				if ( $this->publicize->is_invalid_linkedin_connection( $connection ) ) {
645
					$must_reauth[ $index ] = 'LinkedIn';
646
				}
647
			}
648
		}
649
		return $must_reauth;
650
	}
651
652
	/**
653
	* Controls the metabox that is displayed on the post page
654
	* Allows the user to customize the message that will be sent out to the social network, as well as pick which
655
	* networks to publish to. Also displays the character counter and some other information.
656
	*/
657
	function post_page_metabox() {
658
		global $post;
659
660
		if ( ! $this->publicize->post_type_is_publicizeable( $post->post_type ) )
661
			return;
662
663
		$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...
664
		$connections_data = $this->publicize->get_filtered_connection_data();
665
666
		$available_services = $this->publicize->get_services( 'all' );
667
668
		if ( ! is_array( $available_services ) )
669
			$available_services = array();
670
671
		if ( ! is_array( $connections_data ) )
672
			$connections_data = array();
673
		?>
674
		<div id="publicize" class="misc-pub-section misc-pub-section-last">
675
			<span id="publicize-title">
676
			<?php
677
				esc_html_e( 'Publicize:', 'jetpack' );
678
679
				if ( 0 < count( $connections_data ) ) :
680
					$publicize_form = $this->get_metabox_form_connected( $connections_data );
681
682
					$must_reauth = $this->get_must_reauth_connections();
683
					if ( ! empty( $must_reauth ) ) {
684
						foreach ( $must_reauth as $connection_name ) {
685
							?>
686
							<span class="notice-warning publicize__notice-warning">
687
								<?php
688
									/* translators: %s is the name of a Pubilicize service like "LinkedIn" */
689
									printf( esc_html__(
690
										'Your %s connection needs to be reauthenticated to continue working – head to Sharing to take care of it.',
691
										'jetpack'
692
									), $connection_name );
693
								?>
694
								<a
695
									class="publicize-external-link"
696
									href="<?php echo publicize_calypso_url() ?>"
697
									target="_blank"
698
								>
699
									<span class="publicize-external-link__text"><?php esc_html_e( 'Go to Sharing settings', 'jetpack' ); ?></span>
700
									<span class="dashicons dashicons-external"></span>
701
								</a>
702
							</span>
703
							<?php
704
						}
705
						?>
706
						<?php
707
					}
708
709
					$labels = array();
710
					foreach ( $connections_data as $connection_data ) {
711
						if ( ! $connection_data['enabled'] ) {
712
							continue;
713
						}
714
715
						$labels[] = sprintf(
716
							'<strong>%s</strong>',
717
							esc_html( $this->connection_label( $connection_data['service_label'], $connection_data['display_name'] ) )
718
						);
719
					}
720
721
				?>
722
					<span id="publicize-defaults"><?php echo join( ', ', $labels ); ?></span>
723
					<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 />
724
				<?php
725
726
				else :
727
					$publicize_form = $this->get_metabox_form_disconnected( $available_services );
728
729
				?>
730
					<strong><?php echo __( 'Not Connected', 'jetpack' ); ?></strong>
731
					<a href="#" id="publicize-disconnected-form-show"><?php esc_html_e( 'Edit', 'jetpack' ); ?></a><br />
732
				<?php
733
734
				endif;
735
			?>
736
			</span>
737
			<?php
738
			/**
739
			 * Filter the Publicize details form.
740
			 *
741
			 * @module publicize
742
			 *
743
			 * @since 2.0.0
744
			 *
745
			 * @param string $publicize_form Publicize Details form appearing above Publish button in the editor.
746
			 */
747
			echo apply_filters( 'publicize_form', $publicize_form );
748
			?>
749
		</div> <?php // #publicize
750
	}
751
752
	/**
753
	 * Generates HTML content for connections form.
754
	 *
755
	 * @since 6.7
756
	 *
757
	 * @global WP_Post $post The current post instance being published.
758
	 *
759
	 * @param array $connections_data
760
	 *
761
	 * @return array {
762
	 *     Array of content for generating connection form.
763
	 *
764
	 *     @type string HTML content of form
765
	 *     @type array {
766
	 *     		Array of connection labels for active connections only.
767
	 *
768
	 *          @type string Connection label string.
769
	 *     }
770
	 * }
771
	 */
772
	private function get_metabox_form_connected( $connections_data ) {
773
		global $post;
774
775
		$all_done = $this->publicize->post_is_done_sharing();
776
		$all_connections_done = true;
777
778
		ob_start();
779
780
		?>
781
		<div id="publicize-form" class="hide-if-js">
782
			<ul>
783
		<?php
784
785
		foreach ( $connections_data as $connection_data ) {
786
			$all_connections_done = $all_connections_done && $connection_data['done'];
787
		?>
788
789
				<li>
790
					<label for="wpas-submit-<?php echo esc_attr( $connection_data['unique_id'] ); ?>">
791
						<input
792
							type="checkbox"
793
							name="wpas[submit][<?php echo esc_attr( $connection_data['unique_id'] ); ?>]"
794
							id="wpas-submit-<?php echo esc_attr( $connection_data['unique_id'] ); ?>"
795
							class="wpas-submit-<?php echo esc_attr( $connection_data['service_name'] ); ?>"
796
							value="1"
797
						<?php
798
							checked( true, $connection_data['enabled'] );
799
							disabled( false, $connection_data['toggleable'] );
800
						?>
801
						/>
802
					<?php if ( $connection_data['enabled'] && ! $connection_data['toggleable'] ) : // Need to submit a value to force a global connection to POST ?>
803
						<input
804
							type="hidden"
805
							name="wpas[submit][<?php echo esc_attr( $connection_data['unique_id'] ); ?>]"
806
							value="1"
807
						/>
808
					<?php endif; ?>
809
810
						<?php echo esc_html( $this->connection_label( $connection_data['service_label'], $connection_data['display_name'] ) ); ?>
811
812
					</label>
813
				</li>
814
		<?php
815
		}
816
817
		$title = get_post_meta( $post->ID, $this->publicize->POST_MESS, true );
818
		if ( ! $title ) {
819
			$title = '';
820
		}
821
822
		$all_done = $all_done || $all_connections_done;
823
824
		?>
825
826
			</ul>
827
828
			<label for="wpas-title"><?php _e( 'Custom Message:', 'jetpack' ); ?></label>
829
			<span id="wpas-title-counter" class="alignright hide-if-no-js">0</span>
830
			<textarea name="wpas_title" id="wpas-title"<?php disabled( $all_done ); ?>><?php echo esc_textarea( $title ); ?></textarea>
831
			<a href="#" class="hide-if-no-js button" id="publicize-form-hide"><?php esc_html_e( 'OK', 'jetpack' ); ?></a>
832
			<input type="hidden" name="wpas[0]" value="1" />
833
		</div>
834
835
		<?php if ( ! $all_done ) : ?>
836
			<div id="pub-connection-tests"></div>
837
		<?php endif; ?>
838
		<?php // #publicize-form
839
840
		return ob_get_clean();
841
	}
842
843
	private function get_metabox_form_disconnected( $available_services ) {
844
		ob_start();
845
		?><div id="publicize-form" class="hide-if-js">
846
			<div id="add-publicize-check" style="display: none;"></div>
847
848
			<?php _e( 'Connect to', 'jetpack' ); ?>:
849
850
			<ul class="not-connected">
851
				<?php foreach ( $available_services as $service_name => $service ) : ?>
852
				<li>
853
					<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 ) ); ?>">
854
						<?php echo esc_html( $this->publicize->get_service_label( $service_name ) ); ?>
855
					</a>
856
				</li>
857
				<?php endforeach; ?>
858
			</ul>
859
			<a href="#" class="hide-if-no-js button" id="publicize-disconnected-form-hide"><?php esc_html_e( 'OK', 'jetpack' ); ?></a>
860
		</div><?php // #publicize-form
861
		return ob_get_clean();
862
	}
863
}
864