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