Completed
Push — add/changelog70 ( 0b1482...18b486 )
by Jeremy
12:28
created

Publicize_UI   C

Complexity

Total Complexity 56

Size/Duplication

Total Lines 811
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 811
rs 5.309
c 0
b 0
f 0
wmc 56
lcom 1
cbo 3

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A connected_notice() 0 18 3
A denied_notice() 0 5 1
A init() 0 25 3
A sharing_menu() 0 10 1
A wrapper_admin_page() 0 3 1
A management_page() 0 12 1
A load_assets() 0 21 2
D admin_page() 0 154 16
A global_checkbox() 0 9 2
A broken_connection() 0 11 1
A options_page_other() 0 21 2
B post_page_metabox_assets() 0 274 3
A connection_label() 0 8 1
B post_page_metabox() 0 67 8
B get_metabox_form_connected() 0 70 8
A get_metabox_form_disconnected() 0 20 2

How to fix   Complexity   

Complex Class

Complex classes like Publicize_UI often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Publicize_UI, and based on these observations, apply Extract Interface, too.

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