Completed
Push — branch-4.0 ( fc0d6f...bf547c )
by
unknown
08:22
created

sharing-service.php ➔ sharing_display()   F

Complexity

Conditions 44
Paths > 20000

Size

Total Lines 187
Code Lines 93

Duplication

Lines 7
Ratio 3.74 %

Importance

Changes 0
Metric Value
cc 44
eloc 93
nc 23434
nop 2
dl 7
loc 187
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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