Completed
Push — sync/module-sharing-rebased ( 34df7a )
by
unknown
19:05 queued 09:32
created

Sharing_Service::new_service()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 17
nc 5
nop 3
dl 0
loc 31
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
include_once dirname( __FILE__ ).'/sharing-sources.php';
4
5
define( 'WP_SHARING_PLUGIN_VERSION', JETPACK__VERSION );
6
7
class Sharing_Service {
8
	private $global = false;
9
	public $default_sharing_label = '';
10
11
	public function __construct() {
12
		$this->default_sharing_label = __( 'Share this:', 'jetpack' );
13
	}
14
15
	/**
16
	 * Gets a generic list of all services, without any config
17
	 */
18
	public function get_all_services_blog() {
19
		$options  = get_option( 'sharing-options' );
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 2 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
20
21
		$all = $this->get_all_services();
22
		$services = array();
23
24
		foreach ( $all AS $id => $name ) {
25
			if ( isset( $all[$id] ) ) {
26
				$config = array();
27
28
				// Pre-load custom modules otherwise they won't know who they are
29
				if ( substr( $id, 0, 7 ) == 'custom-' && is_array( $options[$id] ) )
30
					$config = $options[$id];
31
32
				$services[$id] = new $all[$id]( $id, $config );
33
			}
34
		}
35
36
		return $services;
37
	}
38
39
	/**
40
	 * Gets a list of all available service names and classes
41
	 */
42
	public function get_all_services( $include_custom = true ) {
43
		global $wp_version;
44
		// Default services
45
		// if you update this list, please update the REST API tests
46
		// in bin/tests/api/suites/SharingTest.php
47
		$services = array(
48
			'email'             => 'Share_Email',
49
			'print'             => 'Share_Print',
50
			'facebook'          => 'Share_Facebook',
51
			'linkedin'          => 'Share_LinkedIn',
52
			'reddit'            => 'Share_Reddit',
53
			'twitter'           => 'Share_Twitter',
54
			'press-this'        => 'Share_PressThis',
55
			'google-plus-1'     => 'Share_GooglePlus1',
56
			'tumblr'            => 'Share_Tumblr',
57
			'pinterest'         => 'Share_Pinterest',
58
			'pocket'            => 'Share_Pocket',
59
			'telegram'		    => 'Share_Telegram',
60
			'jetpack-whatsapp'  => 'Jetpack_Share_WhatsApp',
61
			'skype'             => 'Share_Skype',
62
		);
63
64
		/**
65
		 * Filters if Email Sharing is enabled.
66
		 *
67
		 * E-Mail sharing is often problematic due to spam concerns, so this filter enables it to be quickly and simply toggled.
68
		 * @module sharedaddy
69
		 *
70
		 * @since 5.1.0
71
		 *
72
		 * @param bool $email Is e-mail sharing enabled? Default false if Akismet is not active or true if Akismet is active.
73
		 */
74
		if ( apply_filters( 'sharing_services_email', Jetpack::is_akismet_active() ) ) {
75
			$services['email'] = 'Share_Email';
76
		}
77
78
		if ( is_multisite() && ( version_compare( $wp_version, '4.9-RC1-42107', '<' ) || is_plugin_active( 'press-this/press-this-plugin.php' ) ) ) {
79
			$services['press-this'] = 'Share_PressThis';
80
		}
81
82
		if ( $include_custom ) {
83
			// Add any custom services in
84
			$options = $this->get_global_options();
85
			foreach ( (array) $options['custom'] AS $custom_id ) {
86
				$services[$custom_id] = 'Share_Custom';
87
			}
88
		}
89
90
		/**
91
		 * Filters the list of available Sharing Services.
92
		 *
93
		 * @module sharedaddy
94
		 *
95
		 * @since 1.1.0
96
		 *
97
		 * @param array $services Array of all available Sharing Services.
98
		 */
99
		return apply_filters( 'sharing_services', $services );
100
	}
101
102
	public function new_service( $label, $url, $icon ) {
103
		// Validate
104
		$label = trim( wp_html_excerpt( wp_kses( $label, array() ), 30 ) );
105
		$url   = trim( esc_url_raw( $url ) );
106
		$icon  = trim( esc_url_raw( $icon ) );
107
108
		if ( $label && $url && $icon ) {
109
			$options = get_option( 'sharing-options' );
110
			if ( !is_array( $options ) )
111
				$options = array();
112
113
			$service_id = 'custom-'.time();
114
115
			// Add a new custom service
116
			$options['global']['custom'][] = $service_id;
117
			if ( false !== $this->global ) {
118
				$this->global['custom'][] = $service_id;
119
			}
120
121
			update_option( 'sharing-options', $options );
122
123
			// Create a custom service and set the options for it
124
			$service = new Share_Custom( $service_id, array( 'name' => $label, 'url' => $url, 'icon' => $icon ) );
125
			$this->set_service( $service_id, $service );
126
127
			// Return the service
128
			return $service;
129
		}
130
131
		return false;
132
	}
133
134
	public function delete_service( $service_id ) {
135
		$options = get_option( 'sharing-options' );
136
		if ( isset( $options[$service_id] ) )
137
			unset( $options[$service_id] );
138
139
		$key = array_search( $service_id, $options['global']['custom'] );
140
		if ( $key !== false )
141
			unset( $options['global']['custom'][$key] );
142
143
		update_option( 'sharing-options', $options );
144
		return true;
145
	}
146
147
	public function set_blog_services( array $visible, array $hidden ) {
148
		$services =  $this->get_all_services();
149
		// Validate the services
150
		$available = array_keys( $services );
151
152
		// Only allow services that we have defined
153
		$hidden  = array_intersect( $hidden, $available );
154
		$visible = array_intersect( $visible, $available );
155
156
		// Ensure we don't have the same ones in hidden and visible
157
		$hidden = array_diff( $hidden, $visible );
158
159
		/**
160
		 * Control the state of the list of sharing services.
161
		 *
162
		 * @module sharedaddy
163
		 *
164
		 * @since 1.1.0
165
		 *
166
		 * @param array $args {
167
		 *	Array of options describing the state of the sharing services.
168
		 *
169
		 *	@type array $services List of all available service names and classes.
170
		 *	@type array $available Validated list of all available service names and classes.
171
		 *	@type array $hidden List of services hidden behind a "More" button.
172
		 *	@type array $visible List of visible services.
173
		 *	@type array $this->get_blog_services() Array of Sharing Services currently enabled.
174
		 * }
175
		 */
176
		do_action( 'sharing_get_services_state', array(
177
			'services'			=> $services,
178
			'available' 		=> $available,
179
			'hidden' 			=> $hidden,
180
			'visible' 			=> $visible,
181
			'currently_enabled' => $this->get_blog_services()
182
		) );
183
184
		return update_option( 'sharing-services', array( 'visible' => $visible, 'hidden' => $hidden ) );
185
	}
186
187
	public function get_blog_services() {
188
		$options  = get_option( 'sharing-options' );
189
		$enabled  = get_option( 'sharing-services' );
190
		$services = $this->get_all_services();
191
192
		/**
193
		 * Check if options exist and are well formatted.
194
		 * This avoids issues on sites with corrupted options.
195
		 * @see https://github.com/Automattic/jetpack/issues/6121
196
		 */
197
		if ( ! is_array( $options ) || ! isset( $options['button_style'], $options['global'] ) ) {
198
			$global_options = array( 'global' => $this->get_global_options() );
199
			$options = is_array( $options )
200
				? array_merge( $options, $global_options )
201
				: $global_options;
202
		}
203
204
		$global = $options['global'];
205
206
		// Default services
207
		if ( ! is_array( $enabled ) ) {
208
			$enabled = array(
209
				'visible' => array(),
210
				'hidden' => array()
211
			);
212
213
			/**
214
			 * Filters the list of default Sharing Services.
215
			 *
216
			 * @module sharedaddy
217
			 *
218
			 * @since 1.1.0
219
			 *
220
			 * @param array $enabled Array of default Sharing Services.
221
			 */
222
			$enabled = apply_filters( 'sharing_default_services', $enabled );
223
		}
224
225
		// Cleanup after any filters that may have produced duplicate services
226 View Code Duplication
		if ( is_array( $enabled['visible'] ) ) {
227
			$enabled['visible'] = array_unique( $enabled['visible'] );
228
		} else {
229
			$enabled['visible'] = array();
230
		}
231
232 View Code Duplication
		if ( is_array( $enabled['hidden'] ) ) {
233
			$enabled['hidden']  = array_unique( $enabled['hidden'] );
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 2 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
234
		} else {
235
			$enabled['hidden'] = array();
236
		}
237
238
		// Form the enabled services
239
		$blog = array( 'visible' => array(), 'hidden' => array() );
240
241
		foreach ( $blog AS $area => $stuff ) {
242
			foreach ( (array)$enabled[$area] AS $service ) {
243
				if ( isset( $services[$service] ) ) {
244
					if ( ! isset( $options[ $service ] ) || ! is_array( $options[ $service ] ) ) {
245
						$options[ $service ] = array();
246
					}
247
					$blog[ $area ][ $service ] = new $services[ $service ]( $service, array_merge( $global, $options[ $service ] ) );
248
				}
249
			}
250
		}
251
252
		/**
253
		 * Filters the list of enabled Sharing Services.
254
		 *
255
		 * @module sharedaddy
256
		 *
257
		 * @since 1.1.0
258
		 *
259
		 * @param array $blog Array of enabled Sharing Services.
260
		 */
261
		$blog = apply_filters( 'sharing_services_enabled', $blog );
262
263
		// Add CSS for NASCAR
264
		if ( count( $blog['visible'] ) || count( $blog['hidden'] ) )
265
			add_filter( 'post_flair_block_css', 'post_flair_service_enabled_sharing' );
266
267
		// Convenience for checking if a service is present
268
		$blog['all'] = array_flip( array_merge( array_keys( $blog['visible'] ), array_keys( $blog['hidden'] ) ) );
269
		return $blog;
270
	}
271
272
	public function get_service( $service_name ) {
273
		$services = $this->get_blog_services();
274
275
		if ( isset( $services['visible'][$service_name] ) )
276
			return $services['visible'][$service_name];
277
278
		if ( isset( $services['hidden'][$service_name] ) )
279
			return $services['hidden'][$service_name];
280
281
		return false;
282
	}
283
284
	public function set_global_options( $data ) {
285
		$options = get_option( 'sharing-options' );
286
287
		// No options yet
288
		if ( !is_array( $options ) )
289
			$options = array();
290
291
		// Defaults
292
		$options['global'] = array(
293
			'button_style'  => 'icon-text',
294
			'sharing_label' => $this->default_sharing_label,
295
			'open_links'    => 'same',
296
			'show'          => array(),
297
			'custom'        => isset( $options['global']['custom'] ) ? $options['global']['custom'] : array()
298
		);
299
300
		/**
301
		 * Filters global sharing settings.
302
		 *
303
		 * @module sharedaddy
304
		 *
305
		 * @since 1.1.0
306
		 *
307
		 * @param array $options['global'] Array of global sharing settings.
308
		 */
309
		$options['global'] = apply_filters( 'sharing_default_global', $options['global'] );
310
311
		// Validate options and set from our data
312
		if ( isset( $data['button_style'] ) && in_array( $data['button_style'], array( 'icon-text', 'icon', 'text', 'official' ) ) )
313
			$options['global']['button_style'] = $data['button_style'];
314
315
		if ( isset( $data['sharing_label'] ) ) {
316
			if ( $this->default_sharing_label === $data['sharing_label'] ) {
317
				$options['global']['sharing_label'] = false;
318
			} else {
319
				$options['global']['sharing_label'] = trim( wp_kses( stripslashes( $data['sharing_label'] ), array() ) );
320
			}
321
		}
322
323
		if ( isset( $data['open_links'] ) && in_array( $data['open_links'], array( 'new', 'same' ) ) )
324
			$options['global']['open_links'] = $data['open_links'];
325
326
		$shows = array_values( get_post_types( array( 'public' => true ) ) );
327
		$shows[] = 'index';
328
		if ( isset( $data['show'] ) ) {
329 View Code Duplication
			if ( is_scalar( $data['show'] ) ) {
330
				switch ( $data['show'] ) {
331
				case 'posts' :
332
					$data['show'] = array( 'post', 'page' );
333
					break;
334
				case 'index' :
335
					$data['show'] = array( 'index' );
336
					break;
337
				case 'posts-index' :
338
					$data['show'] = array( 'post', 'page', 'index' );
339
					break;
340
				}
341
			}
342
343 View Code Duplication
			if ( $data['show'] = array_intersect( $data['show'], $shows ) ) {
344
				$options['global']['show'] = $data['show'];
345
			}
346
		}
347
348
		update_option( 'sharing-options', $options );
349
		return $options['global'];
350
	}
351
352
	public function get_global_options() {
353
		if ( $this->global === false ) {
354
			$options = get_option( 'sharing-options' );
355
356
			if ( is_array( $options ) && isset( $options['global'] ) && is_array( $options['global'] ) ) {
357
				$this->global = $options['global'];
0 ignored issues
show
Documentation Bug introduced by
It seems like $options['global'] of type array is incompatible with the declared type boolean of property $global.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
358
			} else {
359
				$this->global = $this->set_global_options( $options['global'] );
360
			}
361
		}
362
363
		if ( ! isset( $this->global['show'] ) ) {
364
			$this->global['show'] = array( 'post', 'page' );
365
		} elseif ( is_scalar( $this->global['show'] ) ) {
366
			switch ( $this->global['show'] ) {
367
			case 'posts' :
368
				$this->global['show'] = array( 'post', 'page' );
369
				break;
370
			case 'index' :
371
				$this->global['show'] = array( 'index' );
372
				break;
373
			case 'posts-index' :
374
				$this->global['show'] = array( 'post', 'page', 'index' );
375
				break;
376
			}
377
		}
378
379
		if ( false === $this->global['sharing_label'] ) {
380
			$this->global['sharing_label'] = $this->default_sharing_label;
381
		}
382
383
		return $this->global;
384
	}
385
386
	public function set_service( $id, Sharing_Source $service ) {
387
		// Update the options for this service
388
		$options = get_option( 'sharing-options' );
389
390
		// No options yet
391
		if ( ! is_array( $options ) ) {
392
			$options = array();
393
		}
394
395
		/**
396
		 * Get the state of a sharing button.
397
		 *
398
		 * @module sharedaddy
399
		 *
400
		 * @since 1.1.0
401
		 *
402
		 * @param array $args {
403
		 *	State of a sharing button.
404
		 *
405
		 *	@type string $id Service ID.
406
		 *	@type array $options Array of all sharing options.
407
		 *	@type array $service Details about a service.
408
		 * }
409
		 */
410
		do_action( 'sharing_get_button_state', array( 'id' => $id, 'options' => $options, 'service' => $service ) );
411
412
		$options[$id] = $service->get_options();
0 ignored issues
show
Bug introduced by
The method get_options cannot be called on $service (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
413
414
		update_option( 'sharing-options', array_filter( $options ) );
415
	}
416
417
	// Soon to come to a .org plugin near you!
418
	public function get_total( $service_name = false, $post_id = false, $_blog_id = false ) {
419
		global $wpdb, $blog_id;
420
		if ( !$_blog_id ) {
421
			$_blog_id = $blog_id;
422
		}
423 View Code Duplication
		if ( $service_name == false ) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
424
			if ( $post_id > 0 ) {
425
				// total number of shares for this post
426
				return (int) $wpdb->get_var( $wpdb->prepare( "SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d AND post_id = %d", $_blog_id, $post_id ) );
427
			} else {
428
				// total number of shares for this blog
429
				return (int) $wpdb->get_var( $wpdb->prepare( "SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d", $_blog_id ) );
430
			}
431
		}
432
433 View Code Duplication
		if ( $post_id > 0 )
434
			return (int) $wpdb->get_var( $wpdb->prepare( "SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d AND post_id = %d AND share_service = %s", $_blog_id, $post_id, $service_name ) );
435
		else
436
			return (int) $wpdb->get_var( $wpdb->prepare( "SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d AND share_service = %s", $_blog_id, $service_name ) );
437
	}
438
439
	public function get_services_total( $post_id = false ) {
440
		$totals = array();
441
		$services = $this->get_blog_services();
442
443
		if ( !empty( $services ) && isset( $services[ 'all' ] ) )
444
			foreach( $services[ 'all' ] as $key => $value ) {
445
				$totals[$key] = new Sharing_Service_Total( $key, $this->get_total( $key, $post_id ) );
0 ignored issues
show
Documentation introduced by
$key is of type integer|string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
446
			}
447
		usort( $totals, array( 'Sharing_Service_Total', 'cmp' ) );
448
449
		return $totals;
450
	}
451
452 View Code Duplication
	public function get_posts_total() {
453
		$totals = array();
454
		global $wpdb, $blog_id;
455
456
		$my_data = $wpdb->get_results( $wpdb->prepare( "SELECT post_id as id, SUM( count ) as total FROM sharing_stats WHERE blog_id = %d GROUP BY post_id ORDER BY count DESC ", $blog_id ) );
457
458
		if ( !empty( $my_data ) )
459
			foreach( $my_data as $row )
460
				$totals[] = new Sharing_Post_Total( $row->id, $row->total );
461
462
		usort( $totals, array( 'Sharing_Post_Total', 'cmp' ) );
463
464
		return $totals;
465
	}
466
}
467
468
class Sharing_Service_Total {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
469
	public $id 		= '';
470
	public $name 		= '';
471
	public $service	= '';
472
	public $total 		= 0;
473
474
	public function __construct( $id, $total ) {
475
		$services 		= new Sharing_Service();
476
		$this->id 		= esc_html( $id );
477
		$this->service 	= $services->get_service( $id );
478
		$this->total 	= (int) $total;
479
480
		$this->name 	= $this->service->get_name();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 2 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
481
	}
482
483
	static function cmp( $a, $b ) {
484
		if ( $a->total == $b->total )
485
			return $a->name < $b->name;
486
		return $a->total < $b->total;
487
	}
488
}
489
490
class Sharing_Post_Total {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
491
	public $id    = 0;
492
	public $total = 0;
493
	public $title = '';
494
	public $url   = '';
495
496
	public function __construct( $id, $total ) {
497
		$this->id 		= (int) $id;
498
		$this->total 	= (int) $total;
499
		$this->title	= get_the_title( $this->id );
500
		$this->url		= get_permalink( $this->id );
501
	}
502
503
	static function cmp( $a, $b ) {
504
		if ( $a->total == $b->total )
505
			return $a->id < $b->id;
506
		return $a->total < $b->total;
507
	}
508
}
509
510
function sharing_register_post_for_share_counts( $post_id ) {
511
	global $jetpack_sharing_counts;
512
513
	if ( ! isset( $jetpack_sharing_counts ) || ! is_array( $jetpack_sharing_counts ) )
514
		$jetpack_sharing_counts = array();
515
516
	$jetpack_sharing_counts[ (int) $post_id ] = get_permalink( $post_id );
517
}
518
519
function sharing_maybe_enqueue_scripts() {
520
	$sharer          = new Sharing_Service();
521
	$global_options  = $sharer->get_global_options();
522
523
	$enqueue         = false;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 9 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
524 View Code Duplication
	if ( is_singular() && in_array( get_post_type(), $global_options['show'] ) ) {
525
		$enqueue = true;
526
	} elseif ( in_array( 'index', $global_options['show'] ) && ( is_home() || is_front_page() || is_archive() || is_search() || in_array( get_post_type(), $global_options['show'] ) ) ) {
527
		$enqueue = true;
528
	}
529
530
	/**
531
	 * Filter to decide when sharing scripts should be enqueued.
532
	 *
533
	 * @module sharedaddy
534
	 *
535
	 * @since 3.2.0
536
	 *
537
	 * @param bool $enqueue Decide if the sharing scripts should be enqueued.
538
	 */
539
	return (bool) apply_filters( 'sharing_enqueue_scripts', $enqueue );
540
}
541
542
function sharing_add_footer() {
543
	global $jetpack_sharing_counts;
544
545
	/**
546
	 * Filter all JavaScript output by the sharing module.
547
	 *
548
	 * @module sharedaddy
549
	 *
550
	 * @since 1.1.0
551
	 *
552
	 * @param bool true Control whether the sharing module should add any JavaScript to the site. Default to true.
553
	 */
554
	if ( apply_filters( 'sharing_js', true ) && sharing_maybe_enqueue_scripts() ) {
555
556
		/**
557
		 * Filter the display of sharing counts next to the sharing buttons.
558
		 *
559
		 * @module sharedaddy
560
		 *
561
		 * @since 3.2.0
562
		 *
563
		 * @param bool true Control the display of counters next to the sharing buttons. Default to true.
564
		 */
565
		if ( apply_filters( 'jetpack_sharing_counts', true ) && is_array( $jetpack_sharing_counts ) && count( $jetpack_sharing_counts ) ) :
566
			$sharing_post_urls = array_filter( $jetpack_sharing_counts );
567
			if ( $sharing_post_urls ) :
0 ignored issues
show
Bug Best Practice introduced by
The expression $sharing_post_urls of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
568
?>
569
570
	<script type="text/javascript">
571
		window.WPCOM_sharing_counts = <?php echo json_encode( array_flip( $sharing_post_urls ) ); ?>;
572
	</script>
573
<?php
574
			endif;
575
		endif;
576
577
		wp_enqueue_script( 'sharing-js' );
578
		$sharing_js_options = array(
579
			'lang'   => get_base_recaptcha_lang_code(),
580
			/** This filter is documented in modules/sharedaddy/sharing-service.php */
581
			'counts' => apply_filters( 'jetpack_sharing_counts', true )
582
		);
583
		wp_localize_script( 'sharing-js', 'sharing_js_options', $sharing_js_options);
584
	}
585
	$sharer = new Sharing_Service();
586
	$enabled = $sharer->get_blog_services();
587
	foreach ( array_merge( $enabled['visible'], $enabled['hidden'] ) AS $service ) {
588
		$service->display_footer();
589
	}
590
}
591
592
function sharing_add_header() {
593
	$sharer = new Sharing_Service();
594
	$enabled = $sharer->get_blog_services();
595
596
	foreach ( array_merge( $enabled['visible'], $enabled['hidden'] ) AS $service ) {
597
		$service->display_header();
598
	}
599
600
	if ( count( $enabled['all'] ) > 0 && sharing_maybe_enqueue_scripts() ) {
601
		wp_enqueue_style( 'sharedaddy', plugin_dir_url( __FILE__ ) .'sharing.css', array(), JETPACK__VERSION );
602
		wp_enqueue_style( 'social-logos' );
603
	}
604
605
}
606
add_action( 'wp_head', 'sharing_add_header', 1 );
607
608
function sharing_process_requests() {
609
	global $post;
610
611
	// Only process if: single post and share=X defined
612
	if ( ( is_page() || is_single() ) && isset( $_GET['share'] ) ) {
613
		$sharer = new Sharing_Service();
614
615
		$service = $sharer->get_service( $_GET['share'] );
616
		if ( $service ) {
617
			$service->process_request( $post, $_POST );
618
		}
619
	}
620
}
621
add_action( 'template_redirect', 'sharing_process_requests', 9 );
622
623
function sharing_display( $text = '', $echo = false ) {
624
	global $post, $wp_current_filter;
625
626
	require_once JETPACK__PLUGIN_DIR . '/sync/class.jetpack-sync-settings.php';
627
	if ( Jetpack_Sync_Settings::is_syncing() ) {
628
		return $text;
629
	}
630
631
	if ( empty( $post ) )
632
		return $text;
633
634
	if ( ( is_preview() || is_admin() ) && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
635
		return $text;
636
	}
637
638
	// Don't output flair on excerpts
639
	if ( in_array( 'get_the_excerpt', (array) $wp_current_filter ) ) {
640
		return $text;
641
	}
642
643
	// Don't allow flair to be added to the_content more than once (prevent infinite loops)
644
	$done = false;
645
	foreach ( $wp_current_filter as $filter ) {
646
		if ( 'the_content' == $filter ) {
647
			if ( $done )
648
				return $text;
649
			else
650
				$done = true;
651
		}
652
	}
653
654
	// check whether we are viewing the front page and whether the front page option is checked
655
	$options = get_option( 'sharing-options' );
656
	$display_options = $options['global']['show'];
657
658
	if ( is_front_page() && ( is_array( $display_options ) && ! in_array( 'index', $display_options ) ) )
659
		return $text;
660
661
	if ( is_attachment() && in_array( 'the_excerpt', (array) $wp_current_filter ) ) {
662
		// Many themes run the_excerpt() conditionally on an attachment page, then run the_content().
663
		// We only want to output the sharing buttons once.  Let's stick with the_content().
664
		return $text;
665
	}
666
667
	$sharer = new Sharing_Service();
668
	$global = $sharer->get_global_options();
669
670
	$show = false;
671 View Code Duplication
	if ( !is_feed() ) {
672
		if ( is_singular() && in_array( get_post_type(), $global['show'] ) ) {
673
			$show = true;
674
		} elseif ( in_array( 'index', $global['show'] ) && ( is_home() || is_front_page() || is_archive() || is_search() || in_array( get_post_type(), $global['show'] ) ) ) {
675
			$show = true;
676
		}
677
	}
678
679
	/**
680
	 * Filter to decide if sharing buttons should be displayed.
681
	 *
682
	 * @module sharedaddy
683
	 *
684
	 * @since 1.1.0
685
	 *
686
	 * @param bool $show Should the sharing buttons be displayed.
687
	 * @param WP_Post $post The post to share.
688
	 */
689
	$show = apply_filters( 'sharing_show', $show, $post );
690
691
	// Disabled for this post?
692
	$switched_status = get_post_meta( $post->ID, 'sharing_disabled', false );
693
694
	if ( !empty( $switched_status ) )
695
		$show = false;
696
697
	// Private post?
698
	$post_status = get_post_status( $post->ID );
699
700
	if ( 'private' === $post_status ) {
701
		$show = false;
702
	}
703
704
	// Allow to be used on P2 ajax requests for latest posts.
705
	if ( defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_REQUEST['action'] ) && 'get_latest_posts' == $_REQUEST['action'] )
706
		$show = true;
707
708
	$sharing_content = '';
709
710
	if ( $show ) {
711
		/**
712
		 * Filters the list of enabled Sharing Services.
713
		 *
714
		 * @module sharedaddy
715
		 *
716
		 * @since 2.2.3
717
		 *
718
		 * @param array $sharer->get_blog_services() Array of Sharing Services currently enabled.
719
		 */
720
		$enabled = apply_filters( 'sharing_enabled', $sharer->get_blog_services() );
721
722
		if ( count( $enabled['all'] ) > 0 ) {
723
			global $post;
724
725
			$dir = get_option( 'text_direction' );
726
727
			// Wrapper
728
			$sharing_content .= '<div class="sharedaddy sd-sharing-enabled"><div class="robots-nocontent sd-block sd-social sd-social-' . $global['button_style'] . ' sd-sharing">';
729
			if ( $global['sharing_label'] != '' ) {
730
				$sharing_content .= sprintf(
731
					/**
732
					 * Filter the sharing buttons' headline structure.
733
					 *
734
					 * @module sharedaddy
735
					 *
736
					 * @since 4.4.0
737
					 *
738
					 * @param string $sharing_headline Sharing headline structure.
739
					 * @param string $global['sharing_label'] Sharing title.
740
					 * @param string $sharing Module name.
741
					 */
742
					apply_filters( 'jetpack_sharing_headline_html', '<h3 class="sd-title">%s</h3>', $global['sharing_label'], 'sharing' ),
743
					esc_html( $global['sharing_label'] )
744
				);
745
			}
746
			$sharing_content .= '<div class="sd-content"><ul>';
747
748
			// Visible items
749
			$visible = '';
750
			foreach ( $enabled['visible'] as $id => $service ) {
751
				// Individual HTML for sharing service
752
				$visible .= '<li class="share-' . $service->get_class() . '">' . $service->get_display( $post ) . '</li>';
753
			}
754
755
			$parts = array();
756
			$parts[] = $visible;
757
			if ( count( $enabled['hidden'] ) > 0 ) {
758
				if ( count( $enabled['visible'] ) > 0 )
759
					$expand = __( 'More', 'jetpack' );
760
				else
761
					$expand = __( 'Share', 'jetpack' );
762
				$parts[] = '<li><a href="#" class="sharing-anchor sd-button share-more"><span>'.$expand.'</span></a></li>';
763
			}
764
765
			if ( $dir == 'rtl' )
766
				$parts = array_reverse( $parts );
767
768
			$sharing_content .= implode( '', $parts );
769
			$sharing_content .= '<li class="share-end"></li></ul>';
770
771
			if ( count( $enabled['hidden'] ) > 0 ) {
772
				$sharing_content .= '<div class="sharing-hidden"><div class="inner" style="display: none;';
773
774
				if ( count( $enabled['hidden'] ) == 1 )
775
					$sharing_content .= 'width:150px;';
776
777
				$sharing_content .= '">';
778
779
				if ( count( $enabled['hidden'] ) == 1 )
780
					$sharing_content .= '<ul style="background-image:none;">';
781
				else
782
					$sharing_content .= '<ul>';
783
784
				$count = 1;
785
				foreach ( $enabled['hidden'] as $id => $service ) {
786
					// Individual HTML for sharing service
787
					$sharing_content .= '<li class="share-'.$service->get_class().'">';
788
					$sharing_content .= $service->get_display( $post );
789
					$sharing_content .= '</li>';
790
791
					if ( ( $count % 2 ) == 0 )
792
						$sharing_content .= '<li class="share-end"></li>';
793
794
					$count ++;
795
				}
796
797
				// End of wrapper
798
				$sharing_content .= '<li class="share-end"></li></ul></div></div>';
799
			}
800
801
			$sharing_content .= '</div></div></div>';
802
803
			// Register our JS
804
			if ( defined( 'JETPACK__VERSION' ) ) {
805
				$ver = JETPACK__VERSION;
806
			} else {
807
				$ver = '20141212';
808
			}
809
			wp_register_script(
810
				'sharing-js',
811
				Jetpack::get_file_url_for_environment(
812
					'_inc/build/sharedaddy/sharing.min.js',
813
					'modules/sharedaddy/sharing.js'
814
				),
815
				array( 'jquery' ),
816
				$ver
817
			);
818
819
			// Enqueue scripts for the footer
820
			add_action( 'wp_footer', 'sharing_add_footer' );
821
		}
822
	}
823
824
	/**
825
	 * Filters the content markup of the Jetpack sharing links
826
	 *
827
	 * @module sharedaddy
828
	 *
829
	 * @since 3.8.0
830
	 *
831
	 * @param string $sharing_content Content markup of the Jetpack sharing links
832
	 */
833
	$sharing_markup = apply_filters( 'jetpack_sharing_display_markup', $sharing_content );
834
835
	if ( $echo )
836
		echo $text . $sharing_markup;
837
	else
838
		return $text . $sharing_markup;
839
}
840
841
add_filter( 'the_content', 'sharing_display', 19 );
842
add_filter( 'the_excerpt', 'sharing_display', 19 );
843
function get_base_recaptcha_lang_code() {
844
845
	$base_recaptcha_lang_code_mapping = array(
846
		'en'    => 'en',
847
		'nl'    => 'nl',
848
		'fr'    => 'fr',
849
		'fr-be' => 'fr',
850
		'fr-ca' => 'fr',
851
		'fr-ch' => 'fr',
852
		'de'    => 'de',
853
		'pt'    => 'pt',
854
		'pt-br' => 'pt',
855
		'ru'    => 'ru',
856
		'es'    => 'es',
857
		'tr'    => 'tr'
858
	);
859
860
	$blog_lang_code = function_exists( 'get_blog_lang_code' ) ? get_blog_lang_code() : get_bloginfo( 'language' );
861
	if( isset( $base_recaptcha_lang_code_mapping[ $blog_lang_code ] ) )
862
		return $base_recaptcha_lang_code_mapping[ $blog_lang_code ];
863
864
	// if no base mapping is found return default 'en'
865
	return 'en';
866
}
867