Completed
Push — fix/9129-allow-analytics-js-wi... ( 41fabb...20f3a6 )
by
unknown
27:44 queued 11:52
created

modules/publicize/ui.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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