Completed
Push — iterate/calypsoify ( 2ba5fb...4d78ff )
by George
146:23 queued 138:29
created

Publicize_UI::connection_label()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 8
rs 10
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
		/**
37
		 * Filter what user capability is required to use the publicize form on the edit post page. Useful if publish post capability has been removed from role.
38
		 *
39
		 * @module publicize
40
		 *
41
		 * @since 4.1.0
42
		 *
43
		 * @param string $capability User capability needed to use publicize
44
		 */
45
		$capability = apply_filters( 'jetpack_publicize_capability', 'publish_posts' );
46
		if ( ! current_user_can( $capability ) ) {
47
			return;
48
		}
49
50
		// assets (css, js)
51
		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...
52
			add_action( 'load-settings_page_sharing', array( $this, 'load_assets' ) );
53
		}
54
		add_action( 'admin_head-post.php', array( $this, 'post_page_metabox_assets' ) );
55
		add_action( 'admin_head-post-new.php', array( $this, 'post_page_metabox_assets' ) );
56
57
		// management of publicize (sharing screen, ajax/lightbox popup, and metabox on post screen)
58
		add_action( 'pre_admin_screen_sharing', array( $this, 'admin_page' ) );
59
		add_action( 'post_submitbox_misc_actions', array( $this, 'post_page_metabox' ) );
60
	}
61
62
	/**
63
	* If the ShareDaddy plugin is not active we need to add the sharing settings page to the menu still
64
	*/
65
	function sharing_menu() {
66
		add_submenu_page(
67
			'options-general.php',
68
			__( 'Sharing Settings', 'jetpack' ),
69
			__( 'Sharing', 'jetpack' ),
70
			'publish_posts',
71
			'sharing',
72
			array( $this, 'wrapper_admin_page' )
73
		);
74
	}
75
76
	function wrapper_admin_page() {
77
		Jetpack_Admin_Page::wrap_ui( array( $this, 'management_page' ), array( 'is-wide' => true ) );
78
	}
79
	/**
80
	* Management page to load if Sharedaddy is not active so the 'pre_admin_screen_sharing' action exists.
81
	*/
82
	function management_page() { ?>
83
		<div class="wrap">
84
			<div class="icon32" id="icon-options-general"><br /></div>
85
			<h1><?php _e( 'Sharing Settings', 'jetpack' ); ?></h1>
86
87
				<?php
88
				/** This action is documented in modules/sharedaddy/sharing.php */
89
				do_action( 'pre_admin_screen_sharing' );
90
				?>
91
92
		</div> <?php
93
	}
94
95
	/**
96
	 * styling for the sharing screen and popups
97
	 * JS for the options and switching
98
	 */
99
	function load_assets() {
100
		wp_enqueue_script(
101
			'publicize',
102
			Jetpack::get_file_url_for_environment(
103
				'_inc/build/publicize/assets/publicize.min.js',
104
				'modules/publicize/assets/publicize.js'
105
			),
106
			array( 'jquery', 'thickbox' ),
107
			'20121019'
108
		);
109
		if ( is_rtl() ) {
110
			wp_enqueue_style( 'publicize', plugins_url( 'assets/rtl/publicize-rtl.css', __FILE__ ), array(), '20180301' );
111
		} else {
112
			wp_enqueue_style( 'publicize', plugins_url( 'assets/publicize.css', __FILE__ ), array(), '20180301' );
113
		}
114
115
		Jetpack_Admin_Page::load_wrapper_styles();
116
		wp_enqueue_style( 'social-logos' );
117
118
		add_thickbox();
119
	}
120
121
	public static function connected_notice( $service_name ) { ?>
122
		<div class='updated'>
123
			<p><?php
124
125
			if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
126
				$platform =  'WordPress.com';
127
			} else {
128
				$platform = 'Jetpack';
129
			}
130
131
			printf(
132
				/* translators: %1$s: Service Name (Facebook, Twitter, ...), %2$s: Site type (WordPress.com or Jetpack) */
133
				__( 'You have successfully connected your %1$s account with %2$s.', 'jetpack' ),
134
				Publicize::get_service_label( $service_name ),
135
				$platform
136
			); ?></p>
137
		</div><?php
138
	}
139
140
	public static function denied_notice() { ?>
141
		<div class='updated'>
142
			<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>
143
		</div><?php
144
	}
145
146
	/**
147
	 * Lists the current user's publicized accounts for the blog
148
	 * looks exactly like Publicize v1 for now, UI and functionality updates will come after the move to keyring
149
	 */
150
	function admin_page() {
151
		$override_publicize_settings_page = apply_filters_deprecated(
152
			'jetpack_override_publicize_settings_page',
153
			array( false ),
154
			'6.7',
155
			false,
156
			__( 'This filter will be removed in a future version of Jetpack', 'jetpack' )
157
		);
158
159
		if ( $override_publicize_settings_page ) {
160
			echo $override_publicize_settings_page;
161
			return;
162
		}
163
164
		$_blog_id = get_current_blog_id();
165
		?>
166
167
		<form action="" id="publicize-form">
168
			<h2 id="publicize"><?php _e( 'Publicize', 'jetpack' ) ?></h2>
169
170
			<?php
171
				if ( ! empty( $_GET['action'] ) && 'deny' == $_GET['action'] ) {
172
					$this->denied_notice();
173
				}
174
			?>
175
176
			<p>
177
				<?php esc_html_e( 'Connect your blog to popular social networking sites and automatically share new posts with your friends.', 'jetpack' ) ?>
178
				<?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' ); ?>
179
			</p>
180
181
			<?php
182
			if ( $this->in_jetpack ) {
183
				$doc_link = "http://jetpack.com/support/publicize/";
184
			} else {
185
				$doc_link = "http://en.support.wordpress.com/publicize/";
186
			}
187
			?>
188
189
			<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>
190
191
			<div id="publicize-services-block">
192
				<?php
193
				$services = $this->publicize->get_services( 'all' );
194
				$total_num_of_services = count ( $services );
195
				$service_num = 0;?>
196
197
				<div class='left'>
198
199
				<?php
200
				foreach ( $services as $service_name => $service ) :
201
					$connect_url = $this->publicize->connect_url( $service_name );
202
					if ( $service_num == ( round ( ( $total_num_of_services / 2 ), 0 ) ) )
203
						echo "</div><div class='right'>";
204
					$service_num++;
205
					?>
206
					<div class="publicize-service-entry" <?php if ( $service_num > 0 ): ?>class="connected"<?php endif; ?> >
207
						<div id="<?php echo esc_attr( $service_name ); ?>" class="publicize-service-left">
208
							<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>
209
						</div>
210
211
212
						<div class="publicize-service-right">
213
							<?php if ( $this->publicize->is_enabled( $service_name ) && $connections = $this->publicize->get_connections( $service_name ) ) : ?>
214
								<ul>
215
									<?php
216
									foreach( $connections as $c ) :
217
										$id = $this->publicize->get_connection_id( $c );
218
										$disconnect_url = $this->publicize->disconnect_url( $service_name, $id );
219
220
										$cmeta = $this->publicize->get_connection_meta( $c );
221
										$profile_link = $this->publicize->get_profile_link( $service_name, $c );
222
										$connection_display = $this->publicize->get_display_name( $service_name, $c );
223
224
										$options_nonce = wp_create_nonce( 'options_page_' . $service_name . '_' . $id ); ?>
225
226
										<?php if ( $this->publicize->show_options_popup( $service_name, $c ) ): ?>
227
										<script type="text/javascript">
228
										jQuery(document).ready( function($) {
229
											showOptionsPage.call(
230
											this,
231
											'<?php echo esc_js( $service_name ); ?>',
232
											'<?php echo esc_js( $options_nonce ); ?>',
233
											'<?php echo esc_js( $id ); ?>'
234
											);
235
										} );
236
										</script>
237
										<?php endif; ?>
238
239
										<li class="publicize-connection" data-connection-id="<?php echo esc_attr( $id ); ?>">
240
											<?php esc_html_e( 'Connected as:', 'jetpack' ); ?>
241
											<?php
242
											if ( !empty( $profile_link ) ) : ?>
243
												<a class="publicize-profile-link" href="<?php echo esc_url( $profile_link ); ?>" target="_top">
244
													<?php echo esc_html( $connection_display ); ?>
245
												</a><?php
246
											else :
247
												echo esc_html( $connection_display );
248
											endif;
249
											?>
250
251
											<?php if ( 0 == $cmeta['connection_data']['user_id'] ) : ?>
252
												<small>(<?php esc_html_e( 'Shared', 'jetpack' ); ?>)</small>
253
254
												<?php if ( current_user_can( $this->publicize->GLOBAL_CAP ) ) : ?>
255
													<a class="pub-disconnect-button" title="<?php esc_html_e( 'Disconnect', 'jetpack' ); ?>" href="<?php echo esc_url( $disconnect_url ); ?>" target="_top">×</a>
256
												<?php endif; ?>
257
258
											<?php else : ?>
259
												<a class="pub-disconnect-button" title="<?php esc_html_e( 'Disconnect', 'jetpack' ); ?>" href="<?php echo esc_url( $disconnect_url ); ?>" target="_top">×</a>
260
											<?php endif; ?>
261
262
											<br/>
263
											<div class="pub-connection-test test-in-progress" id="pub-connection-test-<?php echo esc_attr( $id ); ?>" >
264
											</div>
265
										</li>
266
267
										<?php
268
									endforeach;
269
									?>
270
								</ul>
271
							<?php endif; ?>
272
273
274
275
							<?php
276
								$connections = $this->publicize->get_connections( $service_name );
277
								if ( empty ( $connections ) ) { ?>
278
									<a id="<?php echo esc_attr( $service_name ); ?>" class="publicize-add-connection button" href="<?php echo esc_url( $connect_url ); ?>" target="_top"><?php echo esc_html( __( 'Connect', 'jetpack' ) ); ?></a>
279
								<?php } else { ?>
280
									<a id="<?php echo esc_attr( $service_name ); ?>" class="publicize-add-connection button add-new" href="<?php echo esc_url( $connect_url ); ?>" target="_top"><?php echo esc_html( __( 'Add New', 'jetpack' ) ); ?></a>
281
			  					<?php } ?>
282
			  			</div>
283
			  		</div>
284
				<?php endforeach; ?>
285
				</div>
286
				<script>
287
				(function($){
288
					$('.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' ) ); ?>' ) ) {
289
								return true;
290
							} else {
291
							e.preventDefault();
292
							return false;
293
						}
294
					})
295
				})(jQuery);
296
				</script>
297
			</div>
298
299
			<?php wp_nonce_field( "wpas_posts_{$_blog_id}", "_wpas_posts_{$_blog_id}_nonce" ); ?>
300
			<input type="hidden" id="wpas_ajax_blog_id" name="wpas_ajax_blog_id" value="<?php echo $_blog_id; ?>" />
301
		</form><?php
302
303
	}
304
305
	public static function global_checkbox( $service_name, $id ) {
306
		global $publicize;
307
		if ( current_user_can( $publicize->GLOBAL_CAP ) ) : ?>
308
			<p>
309
				<input id="globalize_<?php echo $service_name; ?>" type="checkbox" name="global" value="<?php echo wp_create_nonce( 'publicize-globalize-' . $id ) ?>" />
310
				<label for="globalize_<?php echo $service_name; ?>"><?php _e( 'Make this connection available to all users of this blog?', 'jetpack' ); ?></label>
311
			</p>
312
		<?php endif;
313
	}
314
315
	function broken_connection( $service_name, $id ) { ?>
316
		<div id="thickbox-content">
317
			<div class='error'>
318
				<p><?php printf(
319
					/* translators: %s: Service Name (Facebook, Twitter, ...) */
320
					__( 'There was a problem connecting to %s. Please disconnect and try again.', 'jetpack' ),
321
					Publicize::get_service_label( $service_name )
322
				); ?></p>
323
			</div>
324
		</div><?php
325
	}
326
327
	public static function options_page_other( $service_name ) {
328
		// Nonce check
329
		check_admin_referer( "options_page_{$service_name}_" . $_REQUEST['connection'] );
330
		?>
331
		<div id="thickbox-content">
332
			<?php
333
			ob_start();
334
			Publicize_UI::connected_notice( $service_name );
335
			$update_notice = ob_get_clean();
336
			if ( ! empty( $update_notice ) )
337
				echo $update_notice;
338
			?>
339
340
			<?php Publicize_UI::global_checkbox( $service_name, $_REQUEST['connection'] ); ?>
341
342
			<p style="text-align: center;">
343
				<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'] ) ?>" />
344
			</p> <br />
345
		</div>
346
		<?php
347
	}
348
349
	/**
350
	 * CSS for styling the publicize message box and counter that displays on the post page.
351
	 * There is also some JavaScript for length counting and some basic display effects.
352
	 */
353
	function post_page_metabox_assets() {
354
		global $post;
355
		$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...
356
357
		$default_prefix = $this->publicize->default_prefix;
358
		$default_prefix = preg_replace( '/%([0-9])\$s/', "' + %\\1\$s + '", esc_js( $default_prefix ) );
359
360
		$default_message = $this->publicize->default_message;
361
		$default_message = preg_replace( '/%([0-9])\$s/', "' + %\\1\$s + '", esc_js( $default_message ) );
362
363
		$default_suffix = $this->publicize->default_suffix;
364
		$default_suffix = preg_replace( '/%([0-9])\$s/', "' + %\\1\$s + '", esc_js( $default_suffix ) );
365
366
		$max_length = defined( 'JETPACK_PUBLICIZE_TWITTER_LENGTH' ) ? JETPACK_PUBLICIZE_TWITTER_LENGTH : 280;
367
		$max_length = $max_length - 24; // t.co link, space
368
369
		?>
370
371
<script type="text/javascript">
372
jQuery( function($) {
373
	var wpasTitleCounter    = $( '#wpas-title-counter' ),
374
		wpasTwitterCheckbox = $( '.wpas-submit-twitter' ).length,
375
		postTitle = $( '#title' ),
376
		wpasTitle = $( '#wpas-title' ).keyup( function() {
377
			var postTitleVal,
378
				length = wpasTitle.val().length;
379
380
			if ( ! length ) {
381
				length = wpasTitle.attr( 'placeholder' ).length;
382
			}
383
384
			wpasTitleCounter.text( length ).trigger( 'change' );
385
		} ),
386
		authClick = false;
387
388
	wpasTitleCounter.on( 'change', function( e ) {
389
		if ( wpasTwitterCheckbox && parseInt( $( e.currentTarget ).text(), 10 ) > <?php echo (int) $max_length; ?> ) {
390
			wpasTitleCounter.addClass( 'wpas-twitter-length-limit' );
391
		} else {
392
			wpasTitleCounter.removeClass( 'wpas-twitter-length-limit' );
393
		}
394
	} );
395
396
	// Keep the postTitle and the placeholder in sync
397
	postTitle.on( 'keyup', function( e ) {
398
		var url = $( '#sample-permalink' ).text();
399
		var defaultMessage = $.trim( '<?php printf( $default_prefix, 'url' ); printf( $default_message, 'e.currentTarget.value', 'url' ); printf( $default_suffix, 'url' ); ?>' )
400
			.replace( /<[^>]+>/g,'');
401
402
		wpasTitle.attr( 'placeholder', defaultMessage );
403
		wpasTitle.trigger( 'keyup' );
404
	} );
405
406
	// set the initial placeholder
407
	postTitle.trigger( 'keyup' );
408
409
	// If a custom message has been provided, open the UI so the author remembers
410
	if ( wpasTitle.val() && ! wpasTitle.prop( 'disabled' ) && wpasTitle.attr( 'placeholder' ) !== wpasTitle.val() ) {
411
		$( '#publicize-form' ).show();
412
		$( '#publicize-defaults' ).hide();
413
		$( '#publicize-form-edit' ).hide();
414
	}
415
416
	$('#publicize-disconnected-form-show').click( function() {
417
		$('#publicize-form').slideDown( 'fast' );
418
		$(this).hide();
419
	} );
420
421
	$('#publicize-disconnected-form-hide').click( function() {
422
		$('#publicize-form').slideUp( 'fast' );
423
		$('#publicize-disconnected-form-show').show();
424
	} );
425
426
	$('#publicize-form-edit').click( function() {
427
		$('#publicize-form').slideDown( 'fast', function() {
428
			var selBeg = 0, selEnd = 0;
429
			wpasTitle.focus();
430
431
			if ( ! wpasTitle.text() ) {
432
				wpasTitle.text( wpasTitle.attr( 'placeholder' ) );
433
434
				selBeg = wpasTitle.text().indexOf( postTitle.val() );
435
				if ( selBeg < 0 ) {
436
					selBeg = 0;
437
				} else {
438
					selEnd = selBeg + postTitle.val().length;
439
				}
440
441
				var domObj = wpasTitle.get(0);
442
				if ( domObj.setSelectionRange ) {
443
					domObj.setSelectionRange( selBeg, selEnd );
444
				} else if ( domObj.createTextRange ) {
445
					var r = domObj.createTextRange();
446
					r.moveStart( 'character', selBeg );
447
					r.moveEnd( 'character', selEnd );
448
					r.select();
449
				}
450
			}
451
		} );
452
453
		$('#publicize-defaults').hide();
454
		$(this).hide();
455
		return false;
456
	} );
457
458
	$('#publicize-form-hide').click( function() {
459
		var newList = $.map( $('#publicize-form').slideUp( 'fast' ).find( ':checked' ), function( el ) {
460
			return $.trim( $(el).parent( 'label' ).text() );
461
		} );
462
		$('#publicize-defaults').html( '<strong>' + newList.join( '</strong>, <strong>' ) + '</strong>' ).show();
463
		$('#publicize-form-edit').show();
464
		return false;
465
	} );
466
467
	$('.authorize-link').click( function() {
468
		if ( authClick ) {
469
			return false;
470
		}
471
		authClick = true;
472
		$(this).after( '<img src="images/loading.gif" class="alignleft" style="margin: 0 .5em" />' );
473
		$.ajaxSetup( { async: false } );
474
475
		if ( window.wp && window.wp.autosave ) {
476
			window.wp.autosave.server.triggerSave();
477
		} else {
478
			autosave();
479
		}
480
481
		return true;
482
	} );
483
484
	$( '.pub-service' ).click( function() {
485
		var service = $(this).data( 'service' ),
486
			fakebox = '<input id="wpas-submit-' + service + '" type="hidden" value="1" name="wpas[submit][' + service + ']" />';
487
		$( '#add-publicize-check' ).append( fakebox );
488
	} );
489
490
	publicizeConnTestStart = function() {
491
		$( '#pub-connection-tests' )
492
			.removeClass( 'below-h2' )
493
			.removeClass( 'error' )
494
			.removeClass( 'publicize-token-refresh-message' )
495
			.addClass( 'test-in-progress' )
496
			.html( '' );
497
		$.post( ajaxurl, { action: 'test_publicize_conns' }, publicizeConnTestComplete );
498
	}
499
500
	publicizeConnRefreshClick = function( event ) {
501
		event.preventDefault();
502
		var popupURL = event.currentTarget.href;
503
		var popupTitle = event.currentTarget.title;
504
		// open a popup window
505
		// when it is closed, kick off the tests again
506
		var popupWin = window.open( popupURL, popupTitle, '' );
507
		var popupWinTimer= window.setInterval( function() {
508
			if ( popupWin.closed !== false ) {
509
				window.clearInterval( popupWinTimer );
510
				publicizeConnTestStart();
511
			}
512
		}, 500 );
513
	}
514
515
	publicizeConnTestComplete = function( response ) {
516
		var testsSelector = $( '#pub-connection-tests' );
517
		testsSelector
518
			.removeClass( 'test-in-progress' )
519
			.removeClass( 'below-h2' )
520
			.removeClass( 'error' )
521
			.removeClass( 'publicize-token-refresh-message' )
522
			.html( '' );
523
524
		// If any of the tests failed, show some stuff
525
		var somethingShownAlready = false;
526
		var facebookNotice = false;
527
		$.each( response.data, function( index, testResult ) {
528
			// find the li for this connection
529
			if ( ! testResult.connectionTestPassed && testResult.userCanRefresh ) {
530
				if ( ! somethingShownAlready ) {
531
					testsSelector
532
						.addClass( 'below-h2' )
533
						.addClass( 'error' )
534
						.addClass( 'publicize-token-refresh-message' )
535
						.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>" );
536
					somethingShownAlready = true;
537
				}
538
539
				if ( testResult.userCanRefresh ) {
540
					testsSelector.append( '<p/>' );
541
					$( '<a/>', {
542
						'class'  : 'pub-refresh-button button',
543
						'title'  : testResult.refreshText,
544
						'href'   : testResult.refreshURL,
545
						'text'   : testResult.refreshText,
546
						'target' : '_refresh_' + testResult.serviceName
547
					} )
548
						.appendTo( testsSelector.children().last() )
549
						.click( publicizeConnRefreshClick );
550
				}
551
			}
552
553
			if( ! testResult.connectionTestPassed && ! testResult.userCanRefresh ) {
554
				$( '#wpas-submit-' + testResult.unique_id ).prop( "checked", false ).prop( "disabled", true );
555
				if ( ! facebookNotice ) {
556
					var message = '<p>'
557
						+ testResult.connectionTestMessage
558
						+ '</p><p>'
559
						+ ' <a class="button" href="<?php echo esc_url( $this->publicize_settings_url ); ?>" rel="noopener noreferrer" target="_blank">'
560
						+ '<?php echo esc_html( __( 'Update Your Sharing Settings' ,'jetpack' ) ); ?>'
561
						+ '</a>'
562
						+ '<p>';
563
564
					testsSelector
565
						.addClass( 'below-h2' )
566
						.addClass( 'error' )
567
						.addClass( 'publicize-token-refresh-message' )
568
						.append( message );
569
					facebookNotice = true;
570
				}
571
			}
572
		} );
573
	}
574
575
	$( document ).ready( function() {
576
		// If we have the #pub-connection-tests div present, kick off the connection test
577
		if ( $( '#pub-connection-tests' ).length ) {
578
			publicizeConnTestStart();
579
		}
580
	} );
581
582
} );
583
</script>
584
585
<style type="text/css">
586
#publicize {
587
	line-height: 1.5;
588
}
589
#publicize ul {
590
	margin: 4px 0 4px 6px;
591
}
592
#publicize li {
593
	margin: 0;
594
}
595
#publicize textarea {
596
	margin: 4px 0 0;
597
	width: 100%
598
}
599
#publicize ul.not-connected {
600
	list-style: square;
601
	padding-left: 1em;
602
}
603
#publicize-title:before {
604
	content: "\f237";
605
	font: normal 20px/1 dashicons;
606
	speak: none;
607
	margin-left: -1px;
608
	padding-right: 3px;
609
	vertical-align: top;
610
	-webkit-font-smoothing: antialiased;
611
	color: #82878c;
612
}
613
.post-new-php .authorize-link, .post-php .authorize-link {
614
	line-height: 1.5em;
615
}
616
.post-new-php .authorize-message, .post-php .authorize-message {
617
	margin-bottom: 0;
618
}
619
#poststuff #publicize .updated p {
620
	margin: .5em 0;
621
}
622
.wpas-twitter-length-limit {
623
	color: red;
624
}
625
</style><?php
626
	}
627
628
	/**
629
	 * @param string $service_label Service's human-readable Label ("Facebook", "Twitter", ...)
630
	 * @param string $display_name Connection's human-readable Username ("@jetpack", ...)
631
	 * @return string
632
	 */
633
	private function connection_label( $service_label, $display_name ) {
634
		return sprintf(
635
			/* translators: %1$s: Service Name (Facebook, Twitter, ...), %2$s: Username on Service (@jetpack, ...) */
636
			__( '%1$s: %2$s', 'jetpack' ),
637
			$service_label,
638
			$display_name
639
		);
640
	}
641
642
	/**
643
	* Controls the metabox that is displayed on the post page
644
	* Allows the user to customize the message that will be sent out to the social network, as well as pick which
645
	* networks to publish to. Also displays the character counter and some other information.
646
	*/
647
	function post_page_metabox() {
648
		global $post;
649
650
		if ( ! $this->publicize->post_type_is_publicizeable( $post->post_type ) )
651
			return;
652
653
		$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...
654
		$connections_data = $this->publicize->get_filtered_connection_data();
655
656
		$available_services = $this->publicize->get_services( 'all' );
657
658
		if ( ! is_array( $available_services ) )
659
			$available_services = array();
660
661
		if ( ! is_array( $connections_data ) )
662
			$connections_data = array();
663
		?>
664
		<div id="publicize" class="misc-pub-section misc-pub-section-last">
665
			<span id="publicize-title">
666
			<?php
667
				esc_html_e( 'Publicize:', 'jetpack' );
668
669
				if ( 0 < count( $connections_data ) ) :
670
					$publicize_form = $this->get_metabox_form_connected( $connections_data );
671
672
					$labels = array();
673
					foreach ( $connections_data as $connection_data ) {
674
						if ( ! $connection_data['enabled'] ) {
675
							continue;
676
						}
677
678
						$labels[] = sprintf(
679
							'<strong>%s</strong>',
680
							esc_html( $this->connection_label( $connection_data['service_label'], $connection_data['display_name'] ) )
681
						);
682
					}
683
684
				?>
685
					<span id="publicize-defaults"><?php echo join( ', ', $labels ); ?></span>
686
					<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 />
687
				<?php
688
689
				else :
690
					$publicize_form = $this->get_metabox_form_disconnected( $available_services );
691
692
				?>
693
					<strong><?php echo __( 'Not Connected', 'jetpack' ); ?></strong>
694
					<a href="#" id="publicize-disconnected-form-show"><?php esc_html_e( 'Edit', 'jetpack' ); ?></a><br />
695
				<?php
696
697
				endif;
698
			?>
699
			</span>
700
			<?php
701
			/**
702
			 * Filter the Publicize details form.
703
			 *
704
			 * @module publicize
705
			 *
706
			 * @since 2.0.0
707
			 *
708
			 * @param string $publicize_form Publicize Details form appearing above Publish button in the editor.
709
			 */
710
			echo apply_filters( 'publicize_form', $publicize_form );
711
			?>
712
		</div> <?php // #publicize
713
	}
714
715
	/**
716
	 * Generates HTML content for connections form.
717
	 *
718
	 * @since 6.7
719
	 *
720
	 * @global WP_Post $post The current post instance being published.
721
	 *
722
	 * @param array $connections_data
723
	 *
724
	 * @return array {
725
	 *     Array of content for generating connection form.
726
	 *
727
	 *     @type string HTML content of form
728
	 *     @type array {
729
	 *     		Array of connection labels for active connections only.
730
	 *
731
	 *          @type string Connection label string.
732
	 *     }
733
	 * }
734
	 */
735
	private function get_metabox_form_connected( $connections_data ) {
736
		global $post;
737
738
		$all_done = $this->publicize->post_is_done_sharing();
739
		$all_connections_done = true;
740
741
		ob_start();
742
743
		?>
744
		<div id="publicize-form" class="hide-if-js">
745
			<ul>
746
		<?php
747
748
		foreach ( $connections_data as $connection_data ) {
749
			$all_connections_done = $all_connections_done && $connection_data['done'];
750
		?>
751
752
				<li>
753
					<label for="wpas-submit-<?php echo esc_attr( $connection_data['unique_id'] ); ?>">
754
						<input
755
							type="checkbox"
756
							name="wpas[submit][<?php echo esc_attr( $connection_data['unique_id'] ); ?>]"
757
							id="wpas-submit-<?php echo esc_attr( $connection_data['unique_id'] ); ?>"
758
							class="wpas-submit-<?php echo esc_attr( $connection_data['service_name'] ); ?>"
759
							value="1"
760
						<?php
761
							checked( true, $connection_data['enabled'] );
762
							disabled( false, $connection_data['toggleable'] );
763
						?>
764
						/>
765
					<?php if ( $connection_data['enabled'] && ! $connection_data['toggleable'] ) : // Need to submit a value to force a global connection to POST ?>
766
						<input
767
							type="hidden"
768
							name="wpas[submit][<?php echo esc_attr( $connection_data['unique_id'] ); ?>]"
769
							value="1"
770
						/>
771
					<?php endif; ?>
772
773
						<?php echo esc_html( $this->connection_label( $connection_data['service_label'], $connection_data['display_name'] ) ); ?>
774
775
					</label>
776
				</li>
777
		<?php
778
		}
779
780
		$title = get_post_meta( $post->ID, $this->publicize->POST_MESS, true );
781
		if ( ! $title ) {
782
			$title = '';
783
		}
784
785
		$all_done = $all_done || $all_connections_done;
786
787
		?>
788
789
			</ul>
790
791
			<label for="wpas-title"><?php _e( 'Custom Message:', 'jetpack' ); ?></label>
792
			<span id="wpas-title-counter" class="alignright hide-if-no-js">0</span>
793
			<textarea name="wpas_title" id="wpas-title"<?php disabled( $all_done ); ?>><?php echo esc_textarea( $title ); ?></textarea>
794
			<a href="#" class="hide-if-no-js button" id="publicize-form-hide"><?php esc_html_e( 'OK', 'jetpack' ); ?></a>
795
			<input type="hidden" name="wpas[0]" value="1" />
796
		</div>
797
798
		<?php if ( ! $all_done ) : ?>
799
			<div id="pub-connection-tests"></div>
800
		<?php endif; ?>
801
		<?php // #publicize-form
802
803
		return ob_get_clean();
804
	}
805
806
	private function get_metabox_form_disconnected( $available_services ) {
807
		ob_start();
808
		?><div id="publicize-form" class="hide-if-js">
809
			<div id="add-publicize-check" style="display: none;"></div>
810
811
			<?php _e( 'Connect to', 'jetpack' ); ?>:
812
813
			<ul class="not-connected">
814
				<?php foreach ( $available_services as $service_name => $service ) : ?>
815
				<li>
816
					<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 ) ); ?>">
817
						<?php echo esc_html( $this->publicize->get_service_label( $service_name ) ); ?>
818
					</a>
819
				</li>
820
				<?php endforeach; ?>
821
			</ul>
822
			<a href="#" class="hide-if-no-js button" id="publicize-disconnected-form-hide"><?php esc_html_e( 'OK', 'jetpack' ); ?></a>
823
		</div><?php // #publicize-form
824
		return ob_get_clean();
825
	}
826
}
827