Completed
Push — merge-1feb2017-sharedaddy ( 8466f6 )
by George
56:40 queued 46:55
created

Sharing_Service::set_service()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 30
rs 8.8571
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 )  ) {
179
			$options = array( 'global' => $this->get_global_options() );
180
		}
181
182
		if ( ! isset( $options['global'] ) || ! is_array( $options['global'] ) ) {
183
			$options['global'] = $this->get_global_options();
184
		}
185
186
		$global = $options['global'];
187
188
		// Default services
189
		if ( ! is_array( $enabled ) ) {
190
			$enabled = array(
191
				'visible' => array(),
192
				'hidden' => array()
193
			);
194
195
			/**
196
			 * Filters the list of default Sharing Services.
197
			 *
198
			 * @module sharedaddy
199
			 *
200
			 * @since 1.1.0
201
			 *
202
			 * @param array $enabled Array of default Sharing Services.
203
			 */
204
			$enabled = apply_filters( 'sharing_default_services', $enabled );
205
		}
206
207
		// Cleanup after any filters that may have produced duplicate services
208 View Code Duplication
		if ( is_array( $enabled['visible'] ) ) {
209
			$enabled['visible'] = array_unique( $enabled['visible'] );
210
		} else {
211
			$enabled['visible'] = array();
212
		}
213
		
214 View Code Duplication
		if ( is_array( $enabled['hidden'] ) ) {
215
			$enabled['hidden']  = array_unique( $enabled['hidden'] );
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 2 spaces

This check looks for improperly formatted assignments.

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

To illustrate:

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

will have no issues, while

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

will report issues in lines 1 and 2.

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

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

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

Loading history...
340
			} else {
341
				$this->global = $this->set_global_options( $options['global'] );
342
			}
343
		}
344
345
		if ( ! isset( $this->global['show'] ) ) {
346
			$this->global['show'] = array( 'post', 'page' );
347
		} elseif ( is_scalar( $this->global['show'] ) ) {
348
			switch ( $this->global['show'] ) {
349
			case 'posts' :
350
				$this->global['show'] = array( 'post', 'page' );
351
				break;
352
			case 'index' :
353
				$this->global['show'] = array( 'index' );
354
				break;
355
			case 'posts-index' :
356
				$this->global['show'] = array( 'post', 'page', 'index' );
357
				break;
358
			}
359
		}
360
361
		if ( false === $this->global['sharing_label'] ) {
362
			$this->global['sharing_label'] = $this->default_sharing_label;
363
		}
364
365
		return $this->global;
366
	}
367
368
	public function set_service( $id, Sharing_Source $service ) {
369
		// Update the options for this service
370
		$options = get_option( 'sharing-options' );
371
372
		// No options yet
373
		if ( ! is_array( $options ) ) {
374
			$options = array();
375
		}
376
377
		/**
378
		 * Get the state of a sharing button.
379
		 *
380
		 * @module sharedaddy
381
		 *
382
		 * @since 1.1.0
383
		 *
384
		 * @param array $args {
385
		 *	State of a sharing button.
386
		 *
387
		 *	@type string $id Service ID.
388
		 *	@type array $options Array of all sharing options.
389
		 *	@type array $service Details about a service.
390
		 * }
391
		 */
392
		do_action( 'sharing_get_button_state', array( 'id' => $id, 'options' => $options, 'service' => $service ) );
393
394
		$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...
395
396
		update_option( 'sharing-options', array_filter( $options ) );
397
	}
398
399
	// Soon to come to a .org plugin near you!
400
	public function get_total( $service_name = false, $post_id = false, $_blog_id = false ) {
401
		global $wpdb, $blog_id;
402
		if ( !$_blog_id ) {
403
			$_blog_id = $blog_id;
404
		}
405 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...
406
			if ( $post_id > 0 ) {
407
				// total number of shares for this post
408
				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 ) );
409
			} else {
410
				// total number of shares for this blog
411
				return (int) $wpdb->get_var( $wpdb->prepare( "SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d", $_blog_id ) );
412
			}
413
		}
414
415 View Code Duplication
		if ( $post_id > 0 )
416
			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 ) );
417
		else
418
			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 ) );
419
	}
420
421
	public function get_services_total( $post_id = false ) {
422
		$totals = array();
423
		$services = $this->get_blog_services();
424
425
		if ( !empty( $services ) && isset( $services[ 'all' ] ) )
426
			foreach( $services[ 'all' ] as $key => $value ) {
427
				$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...
428
			}
429
		usort( $totals, array( 'Sharing_Service_Total', 'cmp' ) );
430
431
		return $totals;
432
	}
433
434 View Code Duplication
	public function get_posts_total() {
435
		$totals = array();
436
		global $wpdb, $blog_id;
437
438
		$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 ) );
439
440
		if ( !empty( $my_data ) )
441
			foreach( $my_data as $row )
442
				$totals[] = new Sharing_Post_Total( $row->id, $row->total );
443
444
		usort( $totals, array( 'Sharing_Post_Total', 'cmp' ) );
445
446
		return $totals;
447
	}
448
}
449
450
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...
451
	public $id 		= '';
452
	public $name 		= '';
453
	public $service	= '';
454
	public $total 		= 0;
455
456
	public function __construct( $id, $total ) {
457
		$services 		= new Sharing_Service();
458
		$this->id 		= esc_html( $id );
459
		$this->service 	= $services->get_service( $id );
460
		$this->total 	= (int) $total;
461
462
		$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...
463
	}
464
465
	static function cmp( $a, $b ) {
466
		if ( $a->total == $b->total )
467
			return $a->name < $b->name;
468
		return $a->total < $b->total;
469
	}
470
}
471
472
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...
473
	public $id    = 0;
474
	public $total = 0;
475
	public $title = '';
476
	public $url   = '';
477
478
	public function __construct( $id, $total ) {
479
		$this->id 		= (int) $id;
480
		$this->total 	= (int) $total;
481
		$this->title	= get_the_title( $this->id );
482
		$this->url		= get_permalink( $this->id );
483
	}
484
485
	static function cmp( $a, $b ) {
486
		if ( $a->total == $b->total )
487
			return $a->id < $b->id;
488
		return $a->total < $b->total;
489
	}
490
}
491
492
function sharing_register_post_for_share_counts( $post_id ) {
493
	global $jetpack_sharing_counts;
494
495
	if ( ! isset( $jetpack_sharing_counts ) || ! is_array( $jetpack_sharing_counts ) )
496
		$jetpack_sharing_counts = array();
497
498
	$jetpack_sharing_counts[ (int) $post_id ] = get_permalink( $post_id );
499
}
500
501
function sharing_maybe_enqueue_scripts() {
502
	$sharer          = new Sharing_Service();
503
	$global_options  = $sharer->get_global_options();
504
505
	$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...
506 View Code Duplication
	if ( is_singular() && in_array( get_post_type(), $global_options['show'] ) ) {
507
		$enqueue = true;
508
	} elseif ( in_array( 'index', $global_options['show'] ) && ( is_home() || is_front_page() || is_archive() || is_search() || in_array( get_post_type(), $global_options['show'] ) ) ) {
509
		$enqueue = true;
510
	}
511
512
	/**
513
	 * Filter to decide when sharing scripts should be enqueued.
514
	 *
515
	 * @module sharedaddy
516
	 *
517
	 * @since 3.2.0
518
	 *
519
	 * @param bool $enqueue Decide if the sharing scripts should be enqueued.
520
	 */
521
	return (bool) apply_filters( 'sharing_enqueue_scripts', $enqueue );
522
}
523
524
function sharing_add_footer() {
525
	global $jetpack_sharing_counts;
526
527
	/**
528
	 * Filter all JavaScript output by the sharing module.
529
	 *
530
	 * @module sharedaddy
531
	 *
532
	 * @since 1.1.0
533
	 *
534
	 * @param bool true Control whether the sharing module should add any JavaScript to the site. Default to true.
535
	 */
536
	if ( apply_filters( 'sharing_js', true ) && sharing_maybe_enqueue_scripts() ) {
537
538
		/**
539
		 * Filter the display of sharing counts next to the sharing buttons.
540
		 *
541
		 * @module sharedaddy
542
		 *
543
		 * @since 3.2.0
544
		 *
545
		 * @param bool true Control the display of counters next to the sharing buttons. Default to true.
546
		 */
547
		if ( apply_filters( 'jetpack_sharing_counts', true ) && is_array( $jetpack_sharing_counts ) && count( $jetpack_sharing_counts ) ) :
548
			$sharing_post_urls = array_filter( $jetpack_sharing_counts );
549
			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...
550
?>
551
552
	<script type="text/javascript">
553
		window.WPCOM_sharing_counts = <?php echo json_encode( array_flip( $sharing_post_urls ) ); ?>;
554
	</script>
555
<?php
556
			endif;
557
		endif;
558
559
		wp_enqueue_script( 'sharing-js' );
560
		$sharing_js_options = array(
561
			'lang'   => get_base_recaptcha_lang_code(),
562
			/** This filter is documented in modules/sharedaddy/sharing-service.php */
563
			'counts' => apply_filters( 'jetpack_sharing_counts', true )
564
		);
565
		wp_localize_script( 'sharing-js', 'sharing_js_options', $sharing_js_options);
566
	}
567
568
	$sharer = new Sharing_Service();
569
	$enabled = $sharer->get_blog_services();
570
	foreach ( array_merge( $enabled['visible'], $enabled['hidden'] ) AS $service ) {
571
		$service->display_footer();
572
	}
573
}
574
575
function sharing_add_header() {
576
	$sharer = new Sharing_Service();
577
	$enabled = $sharer->get_blog_services();
578
579
	foreach ( array_merge( $enabled['visible'], $enabled['hidden'] ) AS $service ) {
580
		$service->display_header();
581
	}
582
583
	if ( count( $enabled['all'] ) > 0 && sharing_maybe_enqueue_scripts() ) {
584
		wp_enqueue_style( 'sharedaddy', plugin_dir_url( __FILE__ ) .'sharing.css', array(), JETPACK__VERSION );
585
		wp_enqueue_style( 'social-logos' );
586
	}
587
588
}
589
add_action( 'wp_head', 'sharing_add_header', 1 );
590
591
function sharing_process_requests() {
592
	global $post;
593
594
	// Only process if: single post and share=X defined
595
	if ( ( is_page() || is_single() ) && isset( $_GET['share'] ) ) {
596
		$sharer = new Sharing_Service();
597
598
		$service = $sharer->get_service( $_GET['share'] );
599
		if ( $service ) {
600
			$service->process_request( $post, $_POST );
601
		}
602
	}
603
}
604
add_action( 'template_redirect', 'sharing_process_requests', 9 );
605
606
function sharing_display( $text = '', $echo = false ) {
607
	global $post, $wp_current_filter;
608
609
	require_once JETPACK__PLUGIN_DIR . '/sync/class.jetpack-sync-settings.php';
610
	if ( Jetpack_Sync_Settings::is_syncing() ) {
611
		return $text;
612
	}
613
614
	if ( empty( $post ) )
615
		return $text;
616
617
	if ( ( is_preview() || is_admin() ) && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
618
		return $text;
619
	}
620
621
	// Don't output flair on excerpts
622
	if ( in_array( 'get_the_excerpt', (array) $wp_current_filter ) ) {
623
		return $text;
624
	}
625
626
	// Don't allow flair to be added to the_content more than once (prevent infinite loops)
627
	$done = false;
628
	foreach ( $wp_current_filter as $filter ) {
629
		if ( 'the_content' == $filter ) {
630
			if ( $done )
631
				return $text;
632
			else
633
				$done = true;
634
		}
635
	}
636
637
	// check whether we are viewing the front page and whether the front page option is checked
638
	$options = get_option( 'sharing-options' );
639
	$display_options = $options['global']['show'];
640
641
	if ( is_front_page() && ( is_array( $display_options ) && ! in_array( 'index', $display_options ) ) )
642
		return $text;
643
644
	if ( is_attachment() && in_array( 'the_excerpt', (array) $wp_current_filter ) ) {
645
		// Many themes run the_excerpt() conditionally on an attachment page, then run the_content().
646
		// We only want to output the sharing buttons once.  Let's stick with the_content().
647
		return $text;
648
	}
649
650
	$sharer = new Sharing_Service();
651
	$global = $sharer->get_global_options();
652
653
	$show = false;
654 View Code Duplication
	if ( !is_feed() ) {
655
		if ( is_singular() && in_array( get_post_type(), $global['show'] ) ) {
656
			$show = true;
657
		} elseif ( in_array( 'index', $global['show'] ) && ( is_home() || is_front_page() || is_archive() || is_search() || in_array( get_post_type(), $global['show'] ) ) ) {
658
			$show = true;
659
		}
660
	}
661
662
	/**
663
	 * Filter to decide if sharing buttons should be displayed.
664
	 *
665
	 * @module sharedaddy
666
	 *
667
	 * @since 1.1.0
668
	 *
669
	 * @param bool $show Should the sharing buttons be displayed.
670
	 * @param WP_Post $post The post to share.
671
	 */
672
	$show = apply_filters( 'sharing_show', $show, $post );
673
674
	// Disabled for this post?
675
	$switched_status = get_post_meta( $post->ID, 'sharing_disabled', false );
676
677
	if ( !empty( $switched_status ) )
678
		$show = false;
679
680
	// Private post?
681
	$post_status = get_post_status( $post->ID );
682
683
	if ( 'private' === $post_status ) {
684
		$show = false;
685
	}
686
687
	// Allow to be used on P2 ajax requests for latest posts.
688
	if ( defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_REQUEST['action'] ) && 'get_latest_posts' == $_REQUEST['action'] )
689
		$show = true;
690
691
	$sharing_content = '';
692
693
	if ( $show ) {
694
		/**
695
		 * Filters the list of enabled Sharing Services.
696
		 *
697
		 * @module sharedaddy
698
		 *
699
		 * @since 2.2.3
700
		 *
701
		 * @param array $sharer->get_blog_services() Array of Sharing Services currently enabled.
702
		 */
703
		$enabled = apply_filters( 'sharing_enabled', $sharer->get_blog_services() );
704
705
		if ( count( $enabled['all'] ) > 0 ) {
706
			global $post;
707
708
			$dir = get_option( 'text_direction' );
709
710
			// Wrapper
711
			$sharing_content .= '<div class="sharedaddy sd-sharing-enabled"><div class="robots-nocontent sd-block sd-social sd-social-' . $global['button_style'] . ' sd-sharing">';
712
			if ( $global['sharing_label'] != '' ) {
713
				$sharing_content .= sprintf(
714
					/**
715
					 * Filter the sharing buttons' headline structure.
716
					 *
717
					 * @module sharedaddy
718
					 *
719
					 * @since 4.4.0
720
					 *
721
					 * @param string $sharing_headline Sharing headline structure.
722
					 * @param string $global['sharing_label'] Sharing title.
723
					 * @param string $sharing Module name.
724
					 */
725
					apply_filters( 'jetpack_sharing_headline_html', '<h3 class="sd-title">%s</h3>', $global['sharing_label'], 'sharing' ),
726
					esc_html( $global['sharing_label'] )
727
				);
728
			}
729
			$sharing_content .= '<div class="sd-content"><ul>';
730
731
			// Visible items
732
			$visible = '';
733
			foreach ( $enabled['visible'] as $id => $service ) {
734
				// Individual HTML for sharing service
735
				$visible .= '<li class="share-' . $service->get_class() . '">' . $service->get_display( $post ) . '</li>';
736
			}
737
738
			$parts = array();
739
			$parts[] = $visible;
740
			if ( count( $enabled['hidden'] ) > 0 ) {
741
				if ( count( $enabled['visible'] ) > 0 )
742
					$expand = __( 'More', 'jetpack' );
743
				else
744
					$expand = __( 'Share', 'jetpack' );
745
				$parts[] = '<li><a href="#" class="sharing-anchor sd-button share-more"><span>'.$expand.'</span></a></li>';
746
			}
747
748
			if ( $dir == 'rtl' )
749
				$parts = array_reverse( $parts );
750
751
			$sharing_content .= implode( '', $parts );
752
			$sharing_content .= '<li class="share-end"></li></ul>';
753
754
			if ( count( $enabled['hidden'] ) > 0 ) {
755
				$sharing_content .= '<div class="sharing-hidden"><div class="inner" style="display: none;';
756
757
				if ( count( $enabled['hidden'] ) == 1 )
758
					$sharing_content .= 'width:150px;';
759
760
				$sharing_content .= '">';
761
762
				if ( count( $enabled['hidden'] ) == 1 )
763
					$sharing_content .= '<ul style="background-image:none;">';
764
				else
765
					$sharing_content .= '<ul>';
766
767
				$count = 1;
768
				foreach ( $enabled['hidden'] as $id => $service ) {
769
					// Individual HTML for sharing service
770
					$sharing_content .= '<li class="share-'.$service->get_class().'">';
771
					$sharing_content .= $service->get_display( $post );
772
					$sharing_content .= '</li>';
773
774
					if ( ( $count % 2 ) == 0 )
775
						$sharing_content .= '<li class="share-end"></li>';
776
777
					$count ++;
778
				}
779
780
				// End of wrapper
781
				$sharing_content .= '<li class="share-end"></li></ul></div></div>';
782
			}
783
784
			$sharing_content .= '</div></div></div>';
785
786
			// Register our JS
787
			if ( defined( 'JETPACK__VERSION' ) ) {
788
				$ver = JETPACK__VERSION;
789
			} else {
790
				$ver = '20141212';
791
			}
792
			wp_register_script( 'sharing-js', plugin_dir_url( __FILE__ ).'sharing.js', array( 'jquery' ), $ver );
793
			add_action( 'wp_footer', 'sharing_add_footer' );
794
		}
795
	}
796
797
	/**
798
	 * Filters the content markup of the Jetpack sharing links
799
	 *
800
	 * @module sharedaddy
801
	 *
802
	 * @since 3.8.0
803
	 *
804
	 * @param string $sharing_content Content markup of the Jetpack sharing links
805
	 */
806
	$sharing_markup = apply_filters( 'jetpack_sharing_display_markup', $sharing_content );
807
808
	if ( $echo )
809
		echo $text . $sharing_markup;
810
	else
811
		return $text . $sharing_markup;
812
}
813
814
add_filter( 'the_content', 'sharing_display', 19 );
815
add_filter( 'the_excerpt', 'sharing_display', 19 );
816
function get_base_recaptcha_lang_code() {
817
818
	$base_recaptcha_lang_code_mapping = array(
819
		'en'    => 'en',
820
		'nl'    => 'nl',
821
		'fr'    => 'fr',
822
		'fr-be' => 'fr',
823
		'fr-ca' => 'fr',
824
		'fr-ch' => 'fr',
825
		'de'    => 'de',
826
		'pt'    => 'pt',
827
		'pt-br' => 'pt',
828
		'ru'    => 'ru',
829
		'es'    => 'es',
830
		'tr'    => 'tr'
831
	);
832
833
	$blog_lang_code = function_exists( 'get_blog_lang_code' ) ? get_blog_lang_code() : get_bloginfo( 'language' );
834
	if( isset( $base_recaptcha_lang_code_mapping[ $blog_lang_code ] ) )
835
		return $base_recaptcha_lang_code_mapping[ $blog_lang_code ];
836
837
	// if no base mapping is found return default 'en'
838
	return 'en';
839
}
840