Completed
Push — update/woocommerce-services-ji... ( ba84f5 )
by Jeff
116:18 queued 106:24
created

sharing-service.php ➔ sharing_add_footer_scripts_inline()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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