Completed
Push — add/connect-splash-content ( ec1611...3bc2bd )
by
unknown
22:10 queued 11:17
created

Sharing_Service::get_services_total()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 2
nop 1
dl 0
loc 12
rs 9.2
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
			'print'             => 'Share_Print',
49
			'facebook'          => 'Share_Facebook',
50
			'linkedin'          => 'Share_LinkedIn',
51
			'reddit'            => 'Share_Reddit',
52
			'twitter'           => 'Share_Twitter',
53
			'google-plus-1'     => 'Share_GooglePlus1',
54
			'tumblr'            => 'Share_Tumblr',
55
			'pinterest'         => 'Share_Pinterest',
56
			'pocket'            => 'Share_Pocket',
57
			'telegram'          => 'Share_Telegram',
58
			'jetpack-whatsapp'  => 'Jetpack_Share_WhatsApp',
59
			'skype'             => 'Share_Skype',
60
		);
61
62
		/**
63
		 * Filters if Email Sharing is enabled.
64
		 *
65
		 * E-Mail sharing is often problematic due to spam concerns, so this filter enables it to be quickly and simply toggled.
66
		 * @module sharedaddy
67
		 *
68
		 * @since 5.1.0
69
		 *
70
		 * @param bool $email Is e-mail sharing enabled? Default false if Akismet is not active or true if Akismet is active.
71
		 */
72
		if ( apply_filters( 'sharing_services_email', Jetpack::is_akismet_active() ) ) {
73
			$services['email'] = 'Share_Email';
74
		}
75
76
		if ( is_multisite() && ( version_compare( $wp_version, '4.9-RC1-42107', '<' ) || is_plugin_active( 'press-this/press-this-plugin.php' ) ) ) {
77
			$services['press-this'] = 'Share_PressThis';
78
		}
79
80
		if ( $include_custom ) {
81
			// Add any custom services in
82
			$options = $this->get_global_options();
83
			foreach ( (array) $options['custom'] AS $custom_id ) {
84
				$services[$custom_id] = 'Share_Custom';
85
			}
86
		}
87
88
		/**
89
		 * Filters the list of available Sharing Services.
90
		 *
91
		 * @module sharedaddy
92
		 *
93
		 * @since 1.1.0
94
		 *
95
		 * @param array $services Array of all available Sharing Services.
96
		 */
97
		return apply_filters( 'sharing_services', $services );
98
	}
99
100
	public function new_service( $label, $url, $icon ) {
101
		// Validate
102
		$label = trim( wp_html_excerpt( wp_kses( $label, array() ), 30 ) );
103
		$url   = trim( esc_url_raw( $url ) );
104
		$icon  = trim( esc_url_raw( $icon ) );
105
106
		if ( $label && $url && $icon ) {
107
			$options = get_option( 'sharing-options' );
108
			if ( !is_array( $options ) )
109
				$options = array();
110
111
			$service_id = 'custom-'.time();
112
113
			// Add a new custom service
114
			$options['global']['custom'][] = $service_id;
115
			if ( false !== $this->global ) {
116
				$this->global['custom'][] = $service_id;
117
			}
118
119
			update_option( 'sharing-options', $options );
120
121
			// Create a custom service and set the options for it
122
			$service = new Share_Custom( $service_id, array( 'name' => $label, 'url' => $url, 'icon' => $icon ) );
123
			$this->set_service( $service_id, $service );
124
125
			// Return the service
126
			return $service;
127
		}
128
129
		return false;
130
	}
131
132
	public function delete_service( $service_id ) {
133
		$options = get_option( 'sharing-options' );
134
		if ( isset( $options[$service_id] ) )
135
			unset( $options[$service_id] );
136
137
		$key = array_search( $service_id, $options['global']['custom'] );
138
		if ( $key !== false )
139
			unset( $options['global']['custom'][$key] );
140
141
		update_option( 'sharing-options', $options );
142
		return true;
143
	}
144
145
	public function set_blog_services( array $visible, array $hidden ) {
146
		$services =  $this->get_all_services();
147
		// Validate the services
148
		$available = array_keys( $services );
149
150
		// Only allow services that we have defined
151
		$hidden  = array_intersect( $hidden, $available );
152
		$visible = array_intersect( $visible, $available );
153
154
		// Ensure we don't have the same ones in hidden and visible
155
		$hidden = array_diff( $hidden, $visible );
156
157
		/**
158
		 * Control the state of the list of sharing services.
159
		 *
160
		 * @module sharedaddy
161
		 *
162
		 * @since 1.1.0
163
		 *
164
		 * @param array $args {
165
		 *	Array of options describing the state of the sharing services.
166
		 *
167
		 *	@type array $services List of all available service names and classes.
168
		 *	@type array $available Validated list of all available service names and classes.
169
		 *	@type array $hidden List of services hidden behind a "More" button.
170
		 *	@type array $visible List of visible services.
171
		 *	@type array $this->get_blog_services() Array of Sharing Services currently enabled.
172
		 * }
173
		 */
174
		do_action( 'sharing_get_services_state', array(
175
			'services'			=> $services,
176
			'available' 		=> $available,
177
			'hidden' 			=> $hidden,
178
			'visible' 			=> $visible,
179
			'currently_enabled' => $this->get_blog_services()
180
		) );
181
182
		return update_option( 'sharing-services', array( 'visible' => $visible, 'hidden' => $hidden ) );
183
	}
184
185
	public function get_blog_services() {
186
		$options  = get_option( 'sharing-options' );
187
		$enabled  = get_option( 'sharing-services' );
188
		$services = $this->get_all_services();
189
190
		/**
191
		 * Check if options exist and are well formatted.
192
		 * This avoids issues on sites with corrupted options.
193
		 * @see https://github.com/Automattic/jetpack/issues/6121
194
		 */
195
		if ( ! is_array( $options ) || ! isset( $options['button_style'], $options['global'] ) ) {
196
			$global_options = array( 'global' => $this->get_global_options() );
197
			$options = is_array( $options )
198
				? array_merge( $options, $global_options )
199
				: $global_options;
200
		}
201
202
		$global = $options['global'];
203
204
		// Default services
205
		if ( ! is_array( $enabled ) ) {
206
			$enabled = array(
207
				'visible' => array(),
208
				'hidden' => array()
209
			);
210
211
			/**
212
			 * Filters the list of default Sharing Services.
213
			 *
214
			 * @module sharedaddy
215
			 *
216
			 * @since 1.1.0
217
			 *
218
			 * @param array $enabled Array of default Sharing Services.
219
			 */
220
			$enabled = apply_filters( 'sharing_default_services', $enabled );
221
		}
222
223
		// Cleanup after any filters that may have produced duplicate services
224
		$enabled['visible'] = array_unique( $enabled['visible'] );
225
		$enabled['hidden']  = array_unique( $enabled['hidden'] );
226
227
		// Form the enabled services
228
		$blog = array( 'visible' => array(), 'hidden' => array() );
229
230
		foreach ( $blog AS $area => $stuff ) {
231
			foreach ( (array)$enabled[$area] AS $service ) {
232
				if ( isset( $services[$service] ) ) {
233
					$blog[$area][$service] = new $services[$service]( $service, array_merge( $global, isset( $options[$service] ) ? $options[$service] : array() ) );
234
				}
235
			}
236
		}
237
238
		/**
239
		 * Filters the list of enabled Sharing Services.
240
		 *
241
		 * @module sharedaddy
242
		 *
243
		 * @since 1.1.0
244
		 *
245
		 * @param array $blog Array of enabled Sharing Services.
246
		 */
247
		$blog = apply_filters( 'sharing_services_enabled', $blog );
248
249
		// Add CSS for NASCAR
250
		if ( count( $blog['visible'] ) || count( $blog['hidden'] ) )
251
			add_filter( 'post_flair_block_css', 'post_flair_service_enabled_sharing' );
252
253
		// Convenience for checking if a service is present
254
		$blog['all'] = array_flip( array_merge( array_keys( $blog['visible'] ), array_keys( $blog['hidden'] ) ) );
255
		return $blog;
256
	}
257
258
	public function get_service( $service_name ) {
259
		$services = $this->get_blog_services();
260
261
		if ( isset( $services['visible'][$service_name] ) )
262
			return $services['visible'][$service_name];
263
264
		if ( isset( $services['hidden'][$service_name] ) )
265
			return $services['hidden'][$service_name];
266
267
		return false;
268
	}
269
270
	public function set_global_options( $data ) {
271
		$options = get_option( 'sharing-options' );
272
273
		// No options yet
274
		if ( !is_array( $options ) )
275
			$options = array();
276
277
		// Defaults
278
		$options['global'] = array(
279
			'button_style'  => 'icon-text',
280
			'sharing_label' => $this->default_sharing_label,
281
			'open_links'    => 'same',
282
			'show'          => array(),
283
			'custom'        => isset( $options['global']['custom'] ) ? $options['global']['custom'] : array()
284
		);
285
286
		/**
287
		 * Filters global sharing settings.
288
		 *
289
		 * @module sharedaddy
290
		 *
291
		 * @since 1.1.0
292
		 *
293
		 * @param array $options['global'] Array of global sharing settings.
294
		 */
295
		$options['global'] = apply_filters( 'sharing_default_global', $options['global'] );
296
297
		// Validate options and set from our data
298
		if ( isset( $data['button_style'] ) && in_array( $data['button_style'], array( 'icon-text', 'icon', 'text', 'official' ) ) )
299
			$options['global']['button_style'] = $data['button_style'];
300
301
		if ( isset( $data['sharing_label'] ) ) {
302
			if ( $this->default_sharing_label === $data['sharing_label'] ) {
303
				$options['global']['sharing_label'] = false;
304
			} else {
305
				$options['global']['sharing_label'] = trim( wp_kses( stripslashes( $data['sharing_label'] ), array() ) );
306
			}
307
		}
308
309
		if ( isset( $data['open_links'] ) && in_array( $data['open_links'], array( 'new', 'same' ) ) )
310
			$options['global']['open_links'] = $data['open_links'];
311
312
		$shows = array_values( get_post_types( array( 'public' => true ) ) );
313
		$shows[] = 'index';
314
		if ( isset( $data['show'] ) ) {
315 View Code Duplication
			if ( is_scalar( $data['show'] ) ) {
316
				switch ( $data['show'] ) {
317
				case 'posts' :
318
					$data['show'] = array( 'post', 'page' );
319
					break;
320
				case 'index' :
321
					$data['show'] = array( 'index' );
322
					break;
323
				case 'posts-index' :
324
					$data['show'] = array( 'post', 'page', 'index' );
325
					break;
326
				}
327
			}
328
329 View Code Duplication
			if ( $data['show'] = array_intersect( $data['show'], $shows ) ) {
330
				$options['global']['show'] = $data['show'];
331
			}
332
		}
333
334
		update_option( 'sharing-options', $options );
335
		return $options['global'];
336
	}
337
338
	public function get_global_options() {
339
		if ( $this->global === false ) {
340
			$options = get_option( 'sharing-options' );
341
342
			if ( is_array( $options ) && isset( $options['global'] ) )
343
				$this->global = $options['global'];
344
			else
345
				$this->global = $this->set_global_options( $options['global'] );
346
		}
347
348
		if ( ! isset( $this->global['show'] ) ) {
349
			$this->global['show'] = array( 'post', 'page' );
350
		} elseif ( is_scalar( $this->global['show'] ) ) {
351
			switch ( $this->global['show'] ) {
352
			case 'posts' :
353
				$this->global['show'] = array( 'post', 'page' );
354
				break;
355
			case 'index' :
356
				$this->global['show'] = array( 'index' );
357
				break;
358
			case 'posts-index' :
359
				$this->global['show'] = array( 'post', 'page', 'index' );
360
				break;
361
			}
362
		}
363
364
		if ( false === $this->global['sharing_label'] ) {
365
			$this->global['sharing_label'] = $this->default_sharing_label;
366
		}
367
368
		return $this->global;
369
	}
370
371
	public function set_service( $id, Sharing_Source $service ) {
372
		// Update the options for this service
373
		$options = get_option( 'sharing-options' );
374
375
		// No options yet
376
		if ( ! is_array( $options ) ) {
377
			$options = array();
378
		}
379
380
		/**
381
		 * Get the state of a sharing button.
382
		 *
383
		 * @module sharedaddy
384
		 *
385
		 * @since 1.1.0
386
		 *
387
		 * @param array $args {
388
		 *	State of a sharing button.
389
		 *
390
		 *	@type string $id Service ID.
391
		 *	@type array $options Array of all sharing options.
392
		 *	@type array $service Details about a service.
393
		 * }
394
		 */
395
		do_action( 'sharing_get_button_state', array( 'id' => $id, 'options' => $options, 'service' => $service ) );
396
397
		$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...
398
399
		update_option( 'sharing-options', array_filter( $options ) );
400
	}
401
402
	// Soon to come to a .org plugin near you!
403
	public function get_total( $service_name = false, $post_id = false, $_blog_id = false ) {
404
		global $wpdb, $blog_id;
405
		if ( !$_blog_id ) {
406
			$_blog_id = $blog_id;
407
		}
408 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...
409
			if ( $post_id > 0 ) {
410
				// total number of shares for this post
411
				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 ) );
412
			} else {
413
				// total number of shares for this blog
414
				return (int) $wpdb->get_var( $wpdb->prepare( "SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d", $_blog_id ) );
415
			}
416
		}
417
418 View Code Duplication
		if ( $post_id > 0 )
419
			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 ) );
420
		else
421
			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 ) );
422
	}
423
424
	public function get_services_total( $post_id = false ) {
425
		$totals = array();
426
		$services = $this->get_blog_services();
427
428
		if ( !empty( $services ) && isset( $services[ 'all' ] ) )
429
			foreach( $services[ 'all' ] as $key => $value ) {
430
				$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...
431
			}
432
		usort( $totals, array( 'Sharing_Service_Total', 'cmp' ) );
433
434
		return $totals;
435
	}
436
437 View Code Duplication
	public function get_posts_total() {
438
		$totals = array();
439
		global $wpdb, $blog_id;
440
441
		$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 ) );
442
443
		if ( !empty( $my_data ) )
444
			foreach( $my_data as $row )
445
				$totals[] = new Sharing_Post_Total( $row->id, $row->total );
446
447
		usort( $totals, array( 'Sharing_Post_Total', 'cmp' ) );
448
449
		return $totals;
450
	}
451
}
452
453
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...
454
	public $id 		= '';
455
	public $name 		= '';
456
	public $service	= '';
457
	public $total 		= 0;
458
459
	public function __construct( $id, $total ) {
460
		$services 		= new Sharing_Service();
461
		$this->id 		= esc_html( $id );
462
		$this->service 	= $services->get_service( $id );
463
		$this->total 	= (int) $total;
464
465
		$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...
466
	}
467
468
	static function cmp( $a, $b ) {
469
		if ( $a->total == $b->total )
470
			return $a->name < $b->name;
471
		return $a->total < $b->total;
472
	}
473
}
474
475
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...
476
	public $id    = 0;
477
	public $total = 0;
478
	public $title = '';
479
	public $url   = '';
480
481
	public function __construct( $id, $total ) {
482
		$this->id 		= (int) $id;
483
		$this->total 	= (int) $total;
484
		$this->title	= get_the_title( $this->id );
485
		$this->url		= get_permalink( $this->id );
486
	}
487
488
	static function cmp( $a, $b ) {
489
		if ( $a->total == $b->total )
490
			return $a->id < $b->id;
491
		return $a->total < $b->total;
492
	}
493
}
494
495
function sharing_register_post_for_share_counts( $post_id ) {
496
	global $jetpack_sharing_counts;
497
498
	if ( ! isset( $jetpack_sharing_counts ) || ! is_array( $jetpack_sharing_counts ) )
499
		$jetpack_sharing_counts = array();
500
501
	$jetpack_sharing_counts[ (int) $post_id ] = get_permalink( $post_id );
502
}
503
504
function sharing_maybe_enqueue_scripts() {
505
	$sharer          = new Sharing_Service();
506
	$global_options  = $sharer->get_global_options();
507
508
	$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...
509 View Code Duplication
	if ( is_singular() && in_array( get_post_type(), $global_options['show'] ) ) {
510
		$enqueue = true;
511
	} elseif ( in_array( 'index', $global_options['show'] ) && ( is_home() || is_front_page() || is_archive() || is_search() || in_array( get_post_type(), $global_options['show'] ) ) ) {
512
		$enqueue = true;
513
	}
514
515
	/**
516
	 * Filter to decide when sharing scripts should be enqueued.
517
	 *
518
	 * @module sharedaddy
519
	 *
520
	 * @since 3.2.0
521
	 *
522
	 * @param bool $enqueue Decide if the sharing scripts should be enqueued.
523
	 */
524
	return (bool) apply_filters( 'sharing_enqueue_scripts', $enqueue );
525
}
526
527
function sharing_add_footer() {
528
	global $jetpack_sharing_counts;
529
530
	/**
531
	 * Filter all JavaScript output by the sharing module.
532
	 *
533
	 * @module sharedaddy
534
	 *
535
	 * @since 1.1.0
536
	 *
537
	 * @param bool true Control whether the sharing module should add any JavaScript to the site. Default to true.
538
	 */
539
	if ( apply_filters( 'sharing_js', true ) && sharing_maybe_enqueue_scripts() ) {
540
541
		/**
542
		 * Filter the display of sharing counts next to the sharing buttons.
543
		 *
544
		 * @module sharedaddy
545
		 *
546
		 * @since 3.2.0
547
		 *
548
		 * @param bool true Control the display of counters next to the sharing buttons. Default to true.
549
		 */
550
		if ( apply_filters( 'jetpack_sharing_counts', true ) && is_array( $jetpack_sharing_counts ) && count( $jetpack_sharing_counts ) ) :
551
			$sharing_post_urls = array_filter( $jetpack_sharing_counts );
552
			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...
553
?>
554
555
	<script type="text/javascript">
556
		window.WPCOM_sharing_counts = <?php echo json_encode( array_flip( $sharing_post_urls ) ); ?>;
557
	</script>
558
<?php
559
			endif;
560
		endif;
561
562
		wp_enqueue_script( 'sharing-js' );
563
		$sharing_js_options = array(
564
			'lang'   => get_base_recaptcha_lang_code(),
565
			/** This filter is documented in modules/sharedaddy/sharing-service.php */
566
			'counts' => apply_filters( 'jetpack_sharing_counts', true )
567
		);
568
		wp_localize_script( 'sharing-js', 'sharing_js_options', $sharing_js_options);
569
	}
570
	$sharer = new Sharing_Service();
571
	$enabled = $sharer->get_blog_services();
572
	foreach ( array_merge( $enabled['visible'], $enabled['hidden'] ) AS $service ) {
573
		$service->display_footer();
574
	}
575
}
576
577
function sharing_add_header() {
578
	$sharer = new Sharing_Service();
579
	$enabled = $sharer->get_blog_services();
580
581
	foreach ( array_merge( $enabled['visible'], $enabled['hidden'] ) AS $service ) {
582
		$service->display_header();
583
	}
584
585
	if ( count( $enabled['all'] ) > 0 && sharing_maybe_enqueue_scripts() ) {
586
		wp_enqueue_style( 'sharedaddy', plugin_dir_url( __FILE__ ) .'sharing.css', array(), JETPACK__VERSION );
587
		wp_enqueue_style( 'social-logos' );
588
	}
589
590
}
591
add_action( 'wp_head', 'sharing_add_header', 1 );
592
593
function sharing_process_requests() {
594
	global $post;
595
596
	// Only process if: single post and share=X defined
597
	if ( ( is_page() || is_single() ) && isset( $_GET['share'] ) ) {
598
		$sharer = new Sharing_Service();
599
600
		$service = $sharer->get_service( $_GET['share'] );
601
		if ( $service ) {
602
			$service->process_request( $post, $_POST );
603
		}
604
	}
605
}
606
add_action( 'template_redirect', 'sharing_process_requests', 9 );
607
608
function sharing_display( $text = '', $echo = false ) {
609
	global $post, $wp_current_filter;
610
611
	require_once JETPACK__PLUGIN_DIR . '/sync/class.jetpack-sync-settings.php';
612
	if ( Jetpack_Sync_Settings::is_syncing() ) {
613
		return $text;
614
	}
615
616
	if ( empty( $post ) )
617
		return $text;
618
619
	if ( ( is_preview() || is_admin() ) && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
620
		return $text;
621
	}
622
623
	// Don't output flair on excerpts
624
	if ( in_array( 'get_the_excerpt', (array) $wp_current_filter ) ) {
625
		return $text;
626
	}
627
628
	// Don't allow flair to be added to the_content more than once (prevent infinite loops)
629
	$done = false;
630
	foreach ( $wp_current_filter as $filter ) {
631
		if ( 'the_content' == $filter ) {
632
			if ( $done )
633
				return $text;
634
			else
635
				$done = true;
636
		}
637
	}
638
639
	// check whether we are viewing the front page and whether the front page option is checked
640
	$options = get_option( 'sharing-options' );
641
	$display_options = $options['global']['show'];
642
643
	if ( is_front_page() && ( is_array( $display_options ) && ! in_array( 'index', $display_options ) ) )
644
		return $text;
645
646
	if ( is_attachment() && in_array( 'the_excerpt', (array) $wp_current_filter ) ) {
647
		// Many themes run the_excerpt() conditionally on an attachment page, then run the_content().
648
		// We only want to output the sharing buttons once.  Let's stick with the_content().
649
		return $text;
650
	}
651
652
	$sharer = new Sharing_Service();
653
	$global = $sharer->get_global_options();
654
655
	$show = false;
656 View Code Duplication
	if ( !is_feed() ) {
657
		if ( is_singular() && in_array( get_post_type(), $global['show'] ) ) {
658
			$show = true;
659
		} elseif ( in_array( 'index', $global['show'] ) && ( is_home() || is_front_page() || is_archive() || is_search() || in_array( get_post_type(), $global['show'] ) ) ) {
660
			$show = true;
661
		}
662
	}
663
664
	/**
665
	 * Filter to decide if sharing buttons should be displayed.
666
	 *
667
	 * @module sharedaddy
668
	 *
669
	 * @since 1.1.0
670
	 *
671
	 * @param bool $show Should the sharing buttons be displayed.
672
	 * @param WP_Post $post The post to share.
673
	 */
674
	$show = apply_filters( 'sharing_show', $show, $post );
675
676
	// Disabled for this post?
677
	$switched_status = get_post_meta( $post->ID, 'sharing_disabled', false );
678
679
	if ( !empty( $switched_status ) )
680
		$show = false;
681
682
	// Private post?
683
	$post_status = get_post_status( $post->ID );
684
685
	if ( 'private' === $post_status ) {
686
		$show = false;
687
	}
688
689
	// Allow to be used on P2 ajax requests for latest posts.
690
	if ( defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_REQUEST['action'] ) && 'get_latest_posts' == $_REQUEST['action'] )
691
		$show = true;
692
693
	$sharing_content = '';
694
695
	if ( $show ) {
696
		/**
697
		 * Filters the list of enabled Sharing Services.
698
		 *
699
		 * @module sharedaddy
700
		 *
701
		 * @since 2.2.3
702
		 *
703
		 * @param array $sharer->get_blog_services() Array of Sharing Services currently enabled.
704
		 */
705
		$enabled = apply_filters( 'sharing_enabled', $sharer->get_blog_services() );
706
707
		if ( count( $enabled['all'] ) > 0 ) {
708
			global $post;
709
710
			$dir = get_option( 'text_direction' );
711
712
			// Wrapper
713
			$sharing_content .= '<div class="sharedaddy sd-sharing-enabled"><div class="robots-nocontent sd-block sd-social sd-social-' . $global['button_style'] . ' sd-sharing">';
714
			if ( $global['sharing_label'] != '' ) {
715
				$sharing_content .= sprintf(
716
					/**
717
					 * Filter the sharing buttons' headline structure.
718
					 *
719
					 * @module sharedaddy
720
					 *
721
					 * @since 4.4.0
722
					 *
723
					 * @param string $sharing_headline Sharing headline structure.
724
					 * @param string $global['sharing_label'] Sharing title.
725
					 * @param string $sharing Module name.
726
					 */
727
					apply_filters( 'jetpack_sharing_headline_html', '<h3 class="sd-title">%s</h3>', $global['sharing_label'], 'sharing' ),
728
					esc_html( $global['sharing_label'] )
729
				);
730
			}
731
			$sharing_content .= '<div class="sd-content"><ul>';
732
733
			// Visible items
734
			$visible = '';
735
			foreach ( $enabled['visible'] as $id => $service ) {
736
				// Individual HTML for sharing service
737
				$visible .= '<li class="share-' . $service->get_class() . '">' . $service->get_display( $post ) . '</li>';
738
			}
739
740
			$parts = array();
741
			$parts[] = $visible;
742
			if ( count( $enabled['hidden'] ) > 0 ) {
743
				if ( count( $enabled['visible'] ) > 0 )
744
					$expand = __( 'More', 'jetpack' );
745
				else
746
					$expand = __( 'Share', 'jetpack' );
747
				$parts[] = '<li><a href="#" class="sharing-anchor sd-button share-more"><span>'.$expand.'</span></a></li>';
748
			}
749
750
			if ( $dir == 'rtl' )
751
				$parts = array_reverse( $parts );
752
753
			$sharing_content .= implode( '', $parts );
754
			$sharing_content .= '<li class="share-end"></li></ul>';
755
756
			if ( count( $enabled['hidden'] ) > 0 ) {
757
				$sharing_content .= '<div class="sharing-hidden"><div class="inner" style="display: none;';
758
759
				if ( count( $enabled['hidden'] ) == 1 )
760
					$sharing_content .= 'width:150px;';
761
762
				$sharing_content .= '">';
763
764
				if ( count( $enabled['hidden'] ) == 1 )
765
					$sharing_content .= '<ul style="background-image:none;">';
766
				else
767
					$sharing_content .= '<ul>';
768
769
				$count = 1;
770
				foreach ( $enabled['hidden'] as $id => $service ) {
771
					// Individual HTML for sharing service
772
					$sharing_content .= '<li class="share-'.$service->get_class().'">';
773
					$sharing_content .= $service->get_display( $post );
774
					$sharing_content .= '</li>';
775
776
					if ( ( $count % 2 ) == 0 )
777
						$sharing_content .= '<li class="share-end"></li>';
778
779
					$count ++;
780
				}
781
782
				// End of wrapper
783
				$sharing_content .= '<li class="share-end"></li></ul></div></div>';
784
			}
785
786
			$sharing_content .= '</div></div></div>';
787
788
			// Register our JS
789
			if ( defined( 'JETPACK__VERSION' ) ) {
790
				$ver = JETPACK__VERSION;
791
			} else {
792
				$ver = '20141212';
793
			}
794
			wp_register_script(
795
				'sharing-js',
796
				Jetpack::get_file_url_for_environment(
797
					'_inc/build/sharedaddy/sharing.min.js',
798
					'modules/sharedaddy/sharing.js'
799
				),
800
				array( 'jquery' ),
801
				$ver
802
			);
803
804
			// Enqueue scripts for the footer
805
			add_action( 'wp_footer', 'sharing_add_footer' );
806
		}
807
	}
808
809
	/**
810
	 * Filters the content markup of the Jetpack sharing links
811
	 *
812
	 * @module sharedaddy
813
	 *
814
	 * @since 3.8.0
815
	 *
816
	 * @param string $sharing_content Content markup of the Jetpack sharing links
817
	 */
818
	$sharing_markup = apply_filters( 'jetpack_sharing_display_markup', $sharing_content );
819
820
	if ( $echo )
821
		echo $text . $sharing_markup;
822
	else
823
		return $text . $sharing_markup;
824
}
825
826
add_filter( 'the_content', 'sharing_display', 19 );
827
add_filter( 'the_excerpt', 'sharing_display', 19 );
828
function get_base_recaptcha_lang_code() {
829
830
	$base_recaptcha_lang_code_mapping = array(
831
		'en'    => 'en',
832
		'nl'    => 'nl',
833
		'fr'    => 'fr',
834
		'fr-be' => 'fr',
835
		'fr-ca' => 'fr',
836
		'fr-ch' => 'fr',
837
		'de'    => 'de',
838
		'pt'    => 'pt',
839
		'pt-br' => 'pt',
840
		'ru'    => 'ru',
841
		'es'    => 'es',
842
		'tr'    => 'tr'
843
	);
844
845
	$blog_lang_code = function_exists( 'get_blog_lang_code' ) ? get_blog_lang_code() : get_bloginfo( 'language' );
846
	if( isset( $base_recaptcha_lang_code_mapping[ $blog_lang_code ] ) )
847
		return $base_recaptcha_lang_code_mapping[ $blog_lang_code ];
848
849
	// if no base mapping is found return default 'en'
850
	return 'en';
851
}
852