Completed
Push — update/security-widget-languag... ( 75ac2c...0fa1b8 )
by
unknown
126:04 queued 116:11
created

Publicize_UI::get_metabox_form_connected()   F

Complexity

Conditions 29
Paths > 20000

Size

Total Lines 175
Code Lines 80

Duplication

Lines 9
Ratio 5.14 %

Importance

Changes 0
Metric Value
cc 29
eloc 80
nc 359484
nop 1
dl 9
loc 175
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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