Completed
Push — add/sharing-config ( 5512da )
by
unknown
88:53 queued 77:22
created

Sharing_Service::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
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
		if ( !is_array( $options ) )
174
			$options = array( 'global' => $this->get_global_options() );
175
176
		$global = $options['global'];
177
178
		// Default services
179
		if ( ! is_array( $enabled ) ) {
180
			$enabled = array(
181
				'visible' => array(),
182
				'hidden' => array()
183
			);
184
185
			/**
186
			 * Filters the list of default Sharing Services.
187
			 *
188
			 * @module sharedaddy
189
			 *
190
			 * @since 1.1.0
191
			 *
192
			 * @param array $enabled Array of default Sharing Services.
193
			 */
194
			$enabled = apply_filters( 'sharing_default_services', $enabled );
195
		}
196
197
		// Cleanup after any filters that may have produced duplicate services
198
		$enabled['visible'] = array_unique( $enabled['visible'] );
199
		$enabled['hidden']  = array_unique( $enabled['hidden'] );
200
201
		// Form the enabled services
202
		$blog = array( 'visible' => array(), 'hidden' => array() );
203
204
		foreach ( $blog AS $area => $stuff ) {
205
			foreach ( (array)$enabled[$area] AS $service ) {
206
				if ( isset( $services[$service] ) ) {
207
					$blog[$area][$service] = new $services[$service]( $service, array_merge( $global, isset( $options[$service] ) ? $options[$service] : array() ) );
208
				}
209
			}
210
		}
211
212
		/**
213
		 * Filters the list of enabled Sharing Services.
214
		 *
215
		 * @module sharedaddy
216
		 *
217
		 * @since 1.1.0
218
		 *
219
		 * @param array $blog Array of enabled Sharing Services.
220
		 */
221
		$blog = apply_filters( 'sharing_services_enabled', $blog );
222
223
		// Add CSS for NASCAR
224
		if ( count( $blog['visible'] ) || count( $blog['hidden'] ) )
225
			add_filter( 'post_flair_block_css', 'post_flair_service_enabled_sharing' );
226
227
		// Convenience for checking if a service is present
228
		$blog['all'] = array_flip( array_merge( array_keys( $blog['visible'] ), array_keys( $blog['hidden'] ) ) );
229
		return $blog;
230
	}
231
232
	public function get_formatted_services() {
233
234
		$visibilities = array( 'visible', 'hidden' );
235
236
		// Discover enabled services
237
		$buttons = array();
238
		$enabled_services = $this->get_blog_services();
239
		$all_services = $this->get_all_services_blog();
240
241
		// Include buttons of desired visibility
242
		foreach ( $visibilities as $visibility ) {
243
			$buttons = array_merge( $buttons, $enabled_services[ $visibility ] );
244
		}
245
246
		// Append the remaining buttons to the end of the array
247
		foreach ( $all_services as $id => $button ) {
248
			if ( ! array_key_exists( $id, $buttons ) ) {
249
				$buttons[ $id ] = $button;
250
			}
251
		}
252
253
		// Format each button in the response
254
		$response = array();
255
		foreach ( $buttons as $button ) {
256
			$visibility =
257
				isset( $enabled_services[ 'visible' ][ $button->get_id() ] ) ?
258
				'visible' : false;
259
			$enabled = ( $visibility || isset ( $enabled_services[ 'hidden' ][ $button->get_id() ] ) );
0 ignored issues
show
Bug Best Practice introduced by
The expression $visibility of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

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