Completed
Push — update/add-ons-promo ( b2ef2b...f10f88 )
by
unknown
16:00 queued 08:08
created

Publicize_UI::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 15
rs 9.4286
cc 2
eloc 8
nc 2
nop 0
1
<?php
2
3
/**
4
* Only user facing pieces of Publicize are found here.
5
*/
6
class Publicize_UI {
7
8
	/**
9
	* Contains an instance of class 'publicize' which loads Keyring, sets up services, etc.
10
	*/
11
	public $publicize;
12
13
	/**
14
	* 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 create/delete global connections.
26
		if ( ! current_user_can( $this->publicize->GLOBAL_CAP ) ) {
27
			return;
28
		}
29
30
		// assets (css, js)
31
		add_action( 'load-settings_page_sharing', array( &$this, 'load_assets' ) );
32
		add_action( 'admin_head-post.php', array( &$this, 'post_page_metabox_assets' ) );
33
		add_action( 'admin_head-post-new.php', array( &$this, 'post_page_metabox_assets' ) );
34
35
		// management of publicize (sharing screen, ajax/lightbox popup, and metabox on post screen)
36
		add_action( 'pre_admin_screen_sharing', array( &$this, 'admin_page' ) );
37
		add_action( 'post_submitbox_misc_actions', array( &$this, 'post_page_metabox' ) );
38
	}
39
40
	/**
41
	* If the ShareDaddy plugin is not active we need to add the sharing settings page to the menu still
42
	*/
43
	function sharing_menu() {
44
		add_submenu_page( 'options-general.php', __( 'Sharing Settings', 'jetpack' ), __( 'Sharing', 'jetpack' ), 'publish_posts', 'sharing', array( &$this, 'management_page' ) );
45
	}
46
47
48
	/**
49
	* Management page to load if Sharedaddy is not active so the 'pre_admin_screen_sharing' action exists.
50
	*/
51
	function management_page() { ?>
52
		<div class="wrap">
53
			<div class="icon32" id="icon-options-general"><br /></div>
54
			<h1><?php _e( 'Sharing Settings', 'jetpack' ); ?></h1>
55
56
				<?php
57
				/** This action is documented in modules/sharedaddy/sharing.php */
58
				do_action( 'pre_admin_screen_sharing' );
59
				?>
60
61
		</div> <?php
62
	}
63
64
	/**
65
	* styling for the sharing screen and popups
66
	* JS for the options and switching
67
	*/
68 View Code Duplication
	function load_assets() {
69
		wp_enqueue_script(
70
			'publicize',
71
			plugins_url( 'assets/publicize.js', __FILE__ ),
72
			array( 'jquery', 'thickbox' ),
73
			'20121019'
74
		);
75
		if( is_rtl() ) {
76
			wp_enqueue_style( 'publicize', plugins_url( 'assets/rtl/publicize-rtl.css', __FILE__ ), array(), '20120925' );
77
		} else {
78
			wp_enqueue_style( 'publicize', plugins_url( 'assets/publicize.css', __FILE__ ), array(), '20120925' );
79
		}
80
81
82
		add_thickbox();
83
	}
84
85
	public static function connected_notice( $service_name ) { ?>
86
		<div class='updated'>
87
			<p><?php
88
89
			if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
90
				$platform =  __( 'WordPress.com', 'jetpack' );
91
			} else {
92
				$platform = __( 'Jetpack', 'jetpack' );
93
			}
94
95
			printf(
96
				__( 'You have successfully connected your %1$s account with %2$s.', '1: Service Name (Facebook, Twitter, ...), 2. WordPress.com or Jetpack', 'jetpack' ),
97
				Publicize::get_service_label( $service_name ),
98
				$platform
99
			); ?></p>
100
		</div><?php
101
	}
102
103
	public static function denied_notice() { ?>
104
		<div class='updated'>
105
			<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>
106
		</div><?php
107
	}
108
109
	/**
110
	* Lists the current user's publicized accounts for the blog
111
	* looks exactly like Publicize v1 for now, UI and functionality updates will come after the move to keyring
112
	*/
113
	function admin_page() {
114
		$_blog_id = get_current_blog_id();
115
		?>
116
117
		<form action="" id="publicize-form">
118
			<h2 id="publicize"><?php _e( 'Publicize', 'jetpack' ) ?></h2>
119
120
			<?php
121
				if ( ! empty( $_GET['action'] ) && 'deny' == $_GET['action'] ) {
122
					$this->denied_notice();
123
				}
124
			?>
125
126
			<p>
127
				<?php esc_html_e( 'Connect your blog to popular social networking sites and automatically share new posts with your friends.', 'jetpack' ) ?>
128
				<?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' ); ?>
129
			</p>
130
131
			<?php
132
			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...
133
				$doc_link = "http://jetpack.me/support/publicize/";
134
			} else {
135
				$doc_link = "http://en.support.wordpress.com/publicize/";
136
			}
137
			?>
138
139
			<p>&rarr; <a href="<?php echo esc_url( $doc_link ); ?>" target="_blank"><?php esc_html_e( 'More information on using Publicize.', 'jetpack' ); ?></a></p>
140
141
			<div id="publicize-services-block">
142
				<?php
143
				$services = $this->publicize->get_services( 'all' );
144
				$total_num_of_services = count ( $services );
145
				$service_num = 0;?>
146
147
				<div class='left'>
148
149
				<?php
150
				foreach ( $services as $name => $service ) :
151
					$connect_url = $this->publicize->connect_url( $name );
152
					if ( $service_num == ( round ( ( $total_num_of_services / 2 ), 0 ) ) )
153
						echo "</div><div class='right'>";
154
					$service_num++;
155
					?>
156
					<div class="publicize-service-entry" <?php if ( $service_num > 0 ): ?>class="connected"<?php endif; ?> >
157
						<div id="<?php echo esc_attr( $name ); ?>" class="publicize-service-left">
158
							<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>
159
						</div>
160
161
162
						<div class="publicize-service-right">
163
							<?php if ( $this->publicize->is_enabled( $name ) && $connections = $this->publicize->get_connections( $name ) ) : ?>
164
								<ul>
165
									<?php
166
									foreach( $connections as $c ) :
167
										$id = $this->publicize->get_connection_id( $c );
168
										$disconnect_url = $this->publicize->disconnect_url( $name, $id );
169
170
										$cmeta = $this->publicize->get_connection_meta( $c );
171
										$profile_link = $this->publicize->get_profile_link( $name, $c );
172
										$connection_display = $this->publicize->get_display_name( $name, $c );
173
174
										$options_nonce = wp_create_nonce( 'options_page_' . $name . '_' . $id ); ?>
175
176
										<?php if ( $this->publicize->show_options_popup( $name, $c ) ): ?>
177
										<script type="text/javascript">
178
										jQuery(document).ready( function($) {
179
											showOptionsPage.call(
180
											this,
181
											'<?php echo esc_js( $name ); ?>',
182
											'<?php echo esc_js( $options_nonce ); ?>',
183
											'<?php echo esc_js( $id ); ?>'
184
											);
185
										} );
186
										</script>
187
										<?php endif; ?>
188
189
										<li class="publicize-connection" data-connection-id="<?php echo esc_attr( $id ); ?>">
190
											<?php esc_html_e( 'Connected as:', 'jetpack' ); ?>
191
											<?php
192
											if ( !empty( $profile_link ) ) : ?>
193
												<a class="publicize-profile-link" href="<?php echo esc_url( $profile_link ); ?>" target="_top">
194
													<?php echo esc_html( $connection_display ); ?>
195
												</a><?php
196
											else :
197
												echo esc_html( $connection_display );
198
											endif;
199
											?>
200
201
											<?php if ( 0 == $cmeta['connection_data']['user_id'] ) : ?>
202
												<small>(<?php esc_html_e( 'Shared', 'jetpack' ); ?>)</small>
203
204
												<?php if ( current_user_can( $this->publicize->GLOBAL_CAP ) ) : ?>
205
													<a class="pub-disconnect-button" title="<?php esc_html_e( 'Disconnect', 'jetpack' ); ?>" href="<?php echo esc_url( $disconnect_url ); ?>" target="_top">×</a>
206
												<?php endif; ?>
207
208
											<?php else : ?>
209
												<a class="pub-disconnect-button" title="<?php esc_html_e( 'Disconnect', 'jetpack' ); ?>" href="<?php echo esc_url( $disconnect_url ); ?>" target="_top">×</a>
210
											<?php endif; ?>
211
212
											<br/>
213
											<div class="pub-connection-test test-in-progress" id="pub-connection-test-<?php echo esc_attr( $id ); ?>" >
214
											</div>
215
										</li>
216
217
										<?php
218
									endforeach;
219
									?>
220
								</ul>
221
							<?php endif; ?>
222
223
224
225
							<?php
226
								$connections = $this->publicize->get_connections( $name );
227
								if ( empty ( $connections ) ) { ?>
228
									<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>
229
								<?php } else { ?>
230
									<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>
231
			  					<?php } ?>
232
			  			</div>
233
			  		</div>
234
				<?php endforeach; ?>
235
				</div>
236
				<script>
237
				(function($){
238
					$('.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' ) ); ?>' ) ) {
239
								return true;
240
							} else {
241
							e.preventDefault();
242
							return false;
243
						}
244
					})
245
				})(jQuery);
246
				</script>
247
			</div>
248
249
			<?php wp_nonce_field( "wpas_posts_{$_blog_id}", "_wpas_posts_{$_blog_id}_nonce" ); ?>
250
			<input type="hidden" id="wpas_ajax_blog_id" name="wpas_ajax_blog_id" value="<?php echo $_blog_id; ?>" />
251
		</form><?php
252
253
	}
254
255
	public static function global_checkbox( $service_name, $id ) {
256
		global $publicize;
257
		if ( current_user_can( $publicize->GLOBAL_CAP ) ) : ?>
258
			<p>
259
				<input id="globalize_<?php echo $service_name; ?>" type="checkbox" name="global" value="<?php echo wp_create_nonce( 'publicize-globalize-' . $id ) ?>" />
260
				<label for="globalize_<?php echo $service_name; ?>"><?php _e( 'Make this connection available to all users of this blog?', 'jetpack' ); ?></label>
261
			</p>
262
		<?php endif;
263
	}
264
265
	function broken_connection( $service_name, $id ) { ?>
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
266
		<div id="thickbox-content">
267
			<div class='error'>
268
				<p><?php printf( __( 'There was a problem connecting to %s. Please disconnect and try again.', 'jetpack' ), Publicize::get_service_label( $service_name ) ); ?></p>
269
			</div>
270
		</div><?php
271
	}
272
273
	public static function options_page_other( $service_name ) {
274
		// Nonce check
275
		check_admin_referer( "options_page_{$service_name}_" . $_REQUEST['connection'] );
276
		?>
277
		<div id="thickbox-content">
278
			<?php
279
			ob_start();
280
			Publicize_UI::connected_notice( $service_name );
281
			$update_notice = ob_get_clean();
282
			if ( ! empty( $update_notice ) )
283
				echo $update_notice;
284
			?>
285
286
			<?php Publicize_UI::global_checkbox( $service_name, $_REQUEST['connection'] ); ?>
287
288
			<p style="text-align: center;">
289
				<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'] ) ?>" />
290
			</p> <br />
291
		</div>
292
		<?php
293
	}
294
295
	/**
296
	* CSS for styling the publicize message box and counter that displays on the post page.
297
	* There is also some JavaScript for length counting and some basic display effects.
298
	*/
299
	function post_page_metabox_assets() {
300
		global $post;
301
		$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...
302
303
		$default_prefix = $this->publicize->default_prefix;
304
		$default_prefix = preg_replace( '/%([0-9])\$s/', "' + %\\1\$s + '", esc_js( $default_prefix ) );
305
306
		$default_message = $this->publicize->default_message;
307
		$default_message = preg_replace( '/%([0-9])\$s/', "' + %\\1\$s + '", esc_js( $default_message ) );
308
309
		$default_suffix = $this->publicize->default_suffix;
310
		$default_suffix = preg_replace( '/%([0-9])\$s/', "' + %\\1\$s + '", esc_js( $default_suffix ) ); ?>
311
312
<script type="text/javascript">
313
jQuery( function($) {
314
	var wpasTitleCounter    = $( '#wpas-title-counter' ),
315
		wpasTwitterCheckbox = $( '.wpas-submit-twitter' ).size(),
316
		wpasTitle = $('#wpas-title').keyup( function() {
317
		var length = wpasTitle.val().length;
318
		wpasTitleCounter.text( length );
319
		if ( wpasTwitterCheckbox && length > 140 ) {
320
			wpasTitleCounter.addClass( 'wpas-twitter-length-limit' );
321
		} else {
322
			wpasTitleCounter.removeClass( 'wpas-twitter-length-limit' );
323
		}
324
		} ),
325
		authClick = false;
326
327
	$('#publicize-disconnected-form-show').click( function() {
328
		$('#publicize-form').slideDown( 'fast' );
329
		$(this).hide();
330
	} );
331
332
	$('#publicize-disconnected-form-hide').click( function() {
333
		$('#publicize-form').slideUp( 'fast' );
334
		$('#publicize-disconnected-form-show').show();
335
	} );
336
337
	$('#publicize-form-edit').click( function() {
338
		$('#publicize-form').slideDown( 'fast', function() {
339
			wpasTitle.focus();
340
			if ( !wpasTitle.text() ) {
341
				var url = $('#shortlink').size() ? $('#shortlink').val() : '';
342
343
				var defaultMessage = $.trim( '<?php printf( $default_prefix, 'url' ); printf( $default_message, '$("#title").val()', 'url' ); printf( $default_suffix, 'url' ); ?>' );
0 ignored issues
show
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
344
345
				wpasTitle.append( defaultMessage.replace( /<[^>]+>/g,'') );
346
347
				var selBeg = defaultMessage.indexOf( $("#title").val() );
348
				if ( selBeg < 0 ) {
349
					selBeg = 0;
350
					selEnd = 0;
351
				} else {
352
					selEnd = selBeg + $("#title").val().length;
353
				}
354
355
				var domObj = wpasTitle.get(0);
356
				if ( domObj.setSelectionRange ) {
357
					domObj.setSelectionRange( selBeg, selEnd );
358
				} else if ( domObj.createTextRange ) {
359
					var r = domObj.createTextRange();
360
					r.moveStart( 'character', selBeg );
361
					r.moveEnd( 'character', selEnd );
362
					r.select();
363
				}
364
			}
365
			wpasTitle.keyup();
366
		} );
367
		$('#publicize-defaults').hide();
368
		$(this).hide();
369
		return false;
370
	} );
371
372
	$('#publicize-form-hide').click( function() {
373
		var newList = $.map( $('#publicize-form').slideUp( 'fast' ).find( ':checked' ), function( el ) {
374
			return $.trim( $(el).parent( 'label' ).text() );
375
		} );
376
		$('#publicize-defaults').html( '<strong>' + newList.join( '</strong>, <strong>' ) + '</strong>' ).show();
377
		$('#publicize-form-edit').show();
378
		return false;
379
	} );
380
381
	$('.authorize-link').click( function() {
382
		if ( authClick ) {
383
			return false;
384
		}
385
		authClick = true;
386
		$(this).after( '<img src="images/loading.gif" class="alignleft" style="margin: 0 .5em" />' );
387
		$.ajaxSetup( { async: false } );
388
389
		if ( window.wp && window.wp.autosave ) {
390
			window.wp.autosave.server.triggerSave();
391
		} else {
392
			autosave();
393
		}
394
395
		return true;
396
	} );
397
398
	$( '.pub-service' ).click( function() {
399
		var service = $(this).data( 'service' ),
400
			fakebox = '<input id="wpas-submit-' + service + '" type="hidden" value="1" name="wpas[submit][' + service + ']" />';
401
		$( '#add-publicize-check' ).append( fakebox );
402
	} );
403
404
	publicizeConnTestStart = function() {
405
		$( '#pub-connection-tests' )
406
			.removeClass( 'below-h2' )
407
			.removeClass( 'error' )
408
			.removeClass( 'publicize-token-refresh-message' )
409
			.addClass( 'test-in-progress' )
410
			.html( '' );
411
		$.post( ajaxurl, { action: 'test_publicize_conns' }, publicizeConnTestComplete );
412
	}
413
414
	publicizeConnRefreshClick = function( event ) {
415
		event.preventDefault();
416
		var popupURL = event.currentTarget.href;
417
		var popupTitle = event.currentTarget.title;
418
		// open a popup window
419
		// when it is closed, kick off the tests again
420
		var popupWin = window.open( popupURL, popupTitle, '' );
421
		var popupWinTimer= window.setInterval( function() {
422
			if ( popupWin.closed !== false ) {
423
				window.clearInterval( popupWinTimer );
424
				publicizeConnTestStart();
425
			}
426
		}, 500 );
427
	}
428
429
	publicizeConnTestComplete = function( response ) {
430
		var testsSelector = $( '#pub-connection-tests' );
431
		testsSelector
432
			.removeClass( 'test-in-progress' )
433
			.removeClass( 'below-h2' )
434
			.removeClass( 'error' )
435
			.removeClass( 'publicize-token-refresh-message' )
436
			.html( '' );
437
438
		// If any of the tests failed, show some stuff
439
		var somethingShownAlready = false;
440
		$.each( response.data, function( index, testResult ) {
441
			// find the li for this connection
442
			if ( ! testResult.connectionTestPassed ) {
443
				if ( ! somethingShownAlready ) {
444
					testsSelector
445
						.addClass( 'below-h2' )
446
						.addClass( 'error' )
447
						.addClass( 'publicize-token-refresh-message' )
448
						.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>" );
449
					somethingShownAlready = true;
450
				}
451
452
				if ( testResult.userCanRefresh ) {
453
					testsSelector.append( '<p/>' );
454
					$( '<a/>', {
455
						'class'  : 'pub-refresh-button button',
456
						'title'  : testResult.refreshText,
457
						'href'   : testResult.refreshURL,
458
						'text'   : testResult.refreshText,
459
						'target' : '_refresh_' + testResult.serviceName
460
					} )
461
						.appendTo( testsSelector.children().last() )
462
						.click( publicizeConnRefreshClick );
463
				}
464
			}
465
		} );
466
	}
467
468
	$( document ).ready( function() {
469
		// If we have the #pub-connection-tests div present, kick off the connection test
470
		if ( $( '#pub-connection-tests' ).length ) {
471
			publicizeConnTestStart();
472
		}
473
	} );
474
475
} );
476
</script>
477
478
<style type="text/css">
479
#publicize {
480
	line-height: 1.5;
481
}
482
#publicize ul {
483
	margin: 4px 0 4px 6px;
484
}
485
#publicize li {
486
	margin: 0;
487
}
488
#publicize textarea {
489
	margin: 4px 0 0;
490
	width: 100%
491
}
492
#publicize ul.not-connected {
493
	list-style: square;
494
	padding-left: 1em;
495
}
496
.post-new-php .authorize-link, .post-php .authorize-link {
497
	line-height: 1.5em;
498
}
499
.post-new-php .authorize-message, .post-php .authorize-message {
500
	margin-bottom: 0;
501
}
502
#poststuff #publicize .updated p {
503
	margin: .5em 0;
504
}
505
.wpas-twitter-length-limit {
506
	color: red;
507
}
508
</style><?php
509
	}
510
511
	/**
512
	* Controls the metabox that is displayed on the post page
513
	* Allows the user to customize the message that will be sent out to the social network, as well as pick which
514
	* networks to publish to. Also displays the character counter and some other information.
515
	*/
516
	function post_page_metabox() {
517
		global $post;
518
519
		if ( ! $this->publicize->post_type_is_publicizeable( $post->post_type ) )
520
			return;
521
522
		$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...
523
		$services = $this->publicize->get_services( 'connected' );
524
		$available_services = $this->publicize->get_services( 'all' );
525
526
		if ( ! is_array( $available_services ) )
527
			$available_services = array();
528
529
		if ( ! is_array( $services ) )
530
			$services = array();
531
532
		$active = array(); ?>
533
534
		<div id="publicize" class="misc-pub-section misc-pub-section-last">
535
			<?php
536
			_e( 'Publicize:', 'jetpack' );
537
538
			if ( 0 < count( $services ) ) :
539
					ob_start();
540
				?>
541
542
				<div id="publicize-form" class="hide-if-js">
543
					<ul>
544
545
					<?php
546
					// We can set an _all flag to indicate that this post is completely done as
547
					// far as Publicize is concerned. Jetpack uses this approach. All published posts in Jetpack
548
					// have Publicize disabled.
549
					$all_done = get_post_meta( $post->ID, $this->publicize->POST_DONE . 'all', true ) || ( $this->in_jetpack && 'publish' == $post->post_status );
550
551
					// We don't allow Publicizing to the same external id twice, to prevent spam
552
					$service_id_done = (array) get_post_meta( $post->ID, $this->publicize->POST_SERVICE_DONE, true );
553
554
					foreach ( $services as $name => $connections ) {
555
						foreach ( $connections as $connection ) {
556
							$connection_data = '';
557 View Code Duplication
							if ( method_exists( $connection, 'get_meta' ) )
558
								$connection_data = $connection->get_meta( 'connection_data' );
559
							elseif ( ! empty( $connection['connection_data'] ) )
560
								$connection_data = $connection['connection_data'];
561
562
							/**
563
							 * Filter whether a post should be publicized to a given service.
564
							 *
565
							 * @module publicize
566
							 *
567
							 * @since 2.0.0
568
							 *
569
							 * @param bool true Should the post be publicized to a given service? Default to true.
570
							 * @param int $post->ID Post ID.
571
							 * @param string $name Service name.
572
							 * @param array $connection_data Array of information about all Publicize details for the site.
573
							 */
574
							if ( ! $continue = apply_filters( 'wpas_submit_post?', true, $post->ID, $name, $connection_data ) ) {
575
								continue;
576
							}
577
578 View Code Duplication
							if ( ! empty( $connection->unique_id ) ) {
579
								$unique_id = $connection->unique_id;
580
							} else if ( ! empty( $connection['connection_data']['token_id'] ) ) {
581
								$unique_id = $connection['connection_data']['token_id'];
582
							}
583
584
							// Should we be skipping this one?
585
							$skip = (
586
								(
587
									in_array( $post->post_status, array( 'publish', 'draft', 'future' ) )
588
									&&
589
									get_post_meta( $post->ID, $this->publicize->POST_SKIP . $unique_id, true )
0 ignored issues
show
Bug introduced by
The variable $unique_id does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
590
								)
591
								||
592
								(
593
									is_array( $connection )
594
									&&
595
									(
596
										( isset( $connection['meta']['external_id'] ) && ! empty( $service_id_done[ $name ][ $connection['meta']['external_id'] ] ) )
597
										||
598
										// Jetpack's connection data looks a little different.
599
										( isset( $connection['external_id'] ) && ! empty( $service_id_done[ $name ][ $connection['external_id'] ] ) )
600
									)
601
								)
602
							);
603
604
							// Was this connections (OR, old-format service) already Publicized to?
605
							$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
606
607
							// If this one has already been publicized to, don't let it happen again
608
							$disabled = '';
609
							if ( $done )
610
								$disabled = ' disabled="disabled"';
611
612
							// If this is a global connection and this user doesn't have enough permissions to modify
613
							// those connections, don't let them change it
614
							$cmeta = $this->publicize->get_connection_meta( $connection );
615
							$hidden_checkbox = false;
616
							if ( !$done && ( 0 == $cmeta['connection_data']['user_id'] && !current_user_can( $this->publicize->GLOBAL_CAP ) ) ) {
617
								$disabled = ' disabled="disabled"';
618
								/**
619
								 * Filters the checkboxes for global connections with non-prilvedged users.
620
								 *
621
								 * @module publicize
622
								 *
623
								 * @since 3.7.0
624
								 *
625
								 * @param bool   $checked Indicates if this connection should be enabled. Default true.
626
								 * @param int    $post->ID ID of the current post
627
								 * @param string $name Name of the connection (Facebook, Twitter, etc)
628
								 * @param array  $connection Array of data about the connection.
629
								 */
630
								$hidden_checkbox = apply_filters( 'publicize_checkbox_global_default', true, $post->ID, $name, $connection );
631
							}
632
633
							// Determine the state of the checkbox (on/off) and allow filtering
634
							$checked = $skip != 1 || $done;
635
							/**
636
							 * Filter the checkbox state of each Publicize connection appearing in the post editor.
637
							 *
638
							 * @module publicize
639
							 *
640
							 * @since 2.0.1
641
							 *
642
							 * @param bool $checked Should the Publicize checkbox be enabled for a given service.
643
							 * @param int $post->ID Post ID.
644
							 * @param string $name Service name.
645
							 * @param array $connection Array of connection details.
646
							 */
647
							$checked = apply_filters( 'publicize_checkbox_default', $checked, $post->ID, $name, $connection );
648
649
							// Force the checkbox to be checked if the post was DONE, regardless of what the filter does
650
							if ( $done ) {
651
								$checked = true;
652
							}
653
654
							// This post has been handled, so disable everything
655
							if ( $all_done ) {
656
								$disabled = ' disabled="disabled"';
657
							}
658
659
							$label = sprintf(
660
								_x( '%1$s: %2$s', 'Service: Account connected as', 'jetpack' ),
661
								esc_html( $this->publicize->get_service_label( $name ) ),
662
								esc_html( $this->publicize->get_display_name( $name, $connection ) )
663
							);
664
							if ( !$skip || $done ) {
665
								$active[] = $label;
666
							}
667
							?>
668
							<li>
669
								<label for="wpas-submit-<?php echo esc_attr( $unique_id ); ?>">
670
									<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
671
										checked( true, $checked );
672
										echo $disabled;
673
									?> />
674
									<?php
675
									if ( $hidden_checkbox ) {
676
										// Need to submit a value to force a global connection to post
677
										echo '<input type="hidden" name="wpas[submit][' . $unique_id . ']" value="1" />';
678
									}
679
									echo esc_html( $label );
680
									?>
681
								</label>
682
							</li>
683
							<?php
684
						}
685
					}
686
687
					if ( $title = get_post_meta( $post->ID, $this->publicize->POST_MESS, true ) ) {
688
						$title = esc_html( $title );
689
					} else {
690
						$title = '';
691
					}
692
					?>
693
694
					</ul>
695
696
					<label for="wpas-title"><?php _e( 'Custom Message:', 'jetpack' ); ?></label>
697
					<span id="wpas-title-counter" class="alignright hide-if-no-js">0</span>
698
699
					<textarea name="wpas_title" id="wpas-title"<?php disabled( $all_done ); ?>><?php echo $title; ?></textarea>
700
701
					<a href="#" class="hide-if-no-js" id="publicize-form-hide"><?php _e( 'Hide', 'jetpack' ); ?></a>
702
					<input type="hidden" name="wpas[0]" value="1" />
703
704
				</div>
705
				<div id="pub-connection-tests"></div>
706
				<?php // #publicize-form
707
708
				$publicize_form = ob_get_clean();
709
			else :
710
				echo "&nbsp;" . __( 'Not Connected', 'jetpack' );
711
					ob_start();
712
				?>
713
714
				<div id="publicize-form" class="hide-if-js">
715
					<div id="add-publicize-check" style="display: none;"></div>
716
717
					<strong><?php _e( 'Connect to', 'jetpack' ); ?>:</strong>
718
719
					<ul class="not-connected">
720
						<?php foreach ( $available_services as $service_name => $service ) : ?>
721
						<li>
722
							<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 ) ) ); ?>" target="_blank" href="<?php echo $this->publicize->connect_url( $service_name ); ?>">
723
								<?php echo esc_html( $this->publicize->get_service_label( $service_name ) ); ?>
724
							</a>
725
						</li>
726
						<?php endforeach; ?>
727
					</ul>
728
729
					<?php if ( 0 < count( $services ) ) : ?>
730
						<a href="#" class="hide-if-no-js" id="publicize-form-hide"><?php _e( 'Hide', 'jetpack' ); ?></a>
731
					<?php else : ?>
732
						<a href="#" class="hide-if-no-js" id="publicize-disconnected-form-hide"><?php _e( 'Hide', 'jetpack' ); ?></a>
733
					<?php endif; ?>
734
				</div> <?php // #publicize-form
735
736
				$publicize_form = ob_get_clean();
737
			endif;
738
			?>
739
740
			<span id="publicize-defaults"><strong><?php echo join( '</strong>, <strong>', array_map( 'esc_html', $active ) ); ?></strong></span><br />
741
742
			<?php if ( 0 < count( $services ) ) : ?>
743
				<a href="#" id="publicize-form-edit"><?php _e( 'Edit Details', 'jetpack' ); ?></a>&nbsp;<a href="<?php echo admin_url( 'options-general.php?page=sharing' ); ?>" target="_blank"><?php _e( 'Settings', 'jetpack' ); ?></a><br />
744
			<?php else : ?>
745
				<a href="#" id="publicize-disconnected-form-show"><?php _e( 'Show', 'jetpack' ); ?></a><br />
746
			<?php endif; ?>
747
748
			<?php
749
			/**
750
			 * Filter the Publicize details form.
751
			 *
752
			 * @module publicize
753
			 *
754
			 * @since 2.0.0
755
			 *
756
			 * @param string $publicize_form Publicize Details form appearing above Publish button in the editor.
757
			 */
758
			echo apply_filters( 'publicize_form', $publicize_form );
759
			?>
760
761
		</div> <?php // #publicize
762
	}
763
764
}
765