Completed
Push — update/admin-page-readme-more-... ( 8f2301...4565b6 )
by
unknown
11:51
created

Publicize_UI::post_page_metabox()   F

Complexity

Conditions 37
Paths 257

Size

Total Lines 249
Code Lines 129

Duplication

Lines 9
Ratio 3.61 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 37
eloc 129
c 1
b 0
f 0
nc 257
nop 0
dl 9
loc 249
rs 3.3711

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