Completed
Push — deprecate/google-plus-sharing-... ( 952dd4 )
by
unknown
06:39
created

Sharing_Source::display_preview()   F

Complexity

Conditions 14
Paths 384

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
nc 384
nop 3
dl 0
loc 48
rs 3.1333
c 0
b 0
f 0

How to fix   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
abstract class Sharing_Source {
4
	public	  $button_style;
5
	public	  $smart;
6
	protected $open_link_in_new;
7
	protected $id;
8
9
	public function __construct( $id, array $settings ) {
10
		$this->id = $id;
11
		/**
12
		 * Filter the way sharing links open.
13
		 *
14
		 * By default, sharing links open in a new window.
15
		 *
16
		 * @module sharedaddy
17
		 *
18
		 * @since 3.4.0
19
		 *
20
		 * @param bool true Should Sharing links open in a new window. Default to true.
21
		 */
22
		$this->open_link_in_new = apply_filters( 'jetpack_open_sharing_in_new_window', true );
23
24
		if ( isset( $settings['button_style'] ) ) {
25
			$this->button_style = $settings['button_style'];
26
		}
27
28
		if ( isset( $settings['smart'] ) ) {
29
			$this->smart = $settings['smart'];
30
		}
31
	}
32
33
	public function is_deprecated() {
34
		return false;
35
	}
36
37
	public function http() {
38
		return is_ssl() ? 'https' : 'http';
39
	}
40
41
	public function get_id() {
42
		return $this->id;
43
	}
44
45
	public function get_class() {
46
		return $this->id;
47
	}
48
49
	public function get_share_url( $post_id ) {
50
		/**
51
		 * Filter the sharing permalink.
52
		 *
53
		 * @module sharedaddy
54
		 *
55
		 * @since 1.2.0
56
		 *
57
		 * @param string get_permalink( $post_id ) Post Permalink.
58
		 * @param int $post_id Post ID.
59
		 * @param int $this->id Sharing ID.
60
		 */
61
		return apply_filters( 'sharing_permalink', get_permalink( $post_id ), $post_id, $this->id );
62
	}
63
64
	public function get_share_title( $post_id ) {
65
		$post = get_post( $post_id );
66
		/**
67
		 * Filter the sharing title.
68
		 *
69
		 * @module sharedaddy
70
		 *
71
		 * @since 2.8.0
72
		 *
73
		 * @param string $post->post_title Post Title.
74
		 * @param int $post_id Post ID.
75
		 * @param int $this->id Sharing ID.
76
		 */
77
		$title = apply_filters( 'sharing_title', $post->post_title, $post_id, $this->id );
78
79
		return html_entity_decode( wp_kses( $title, null ) );
80
	}
81
82
	public function has_custom_button_style() {
83
		return false;
84
	}
85
86
	public function get_link( $url, $text, $title, $query = '', $id = false ) {
87
		$args = func_get_args();
88
		$klasses = array( 'share-' . $this->get_class(), 'sd-button' );
89
90
		if ( 'icon' == $this->button_style || 'icon-text' == $this->button_style ) {
91
			$klasses[] = 'share-icon';
92
		}
93
94
		if ( 'icon' == $this->button_style ) {
95
			$text = $title;
96
			$klasses[] = 'no-text';
97
98
			if ( true == $this->open_link_in_new ) {
99
				$text .= __( ' (Opens in new window)', 'jetpack' );
100
			}
101
		}
102
103
		/**
104
		 * Filter the sharing display ID.
105
		 *
106
		 * @module sharedaddy
107
		 *
108
		 * @since 3.4.0
109
		 *
110
		 * @param int|false $id Sharing ID.
111
		 * @param object $this Sharing service properties.
112
		 * @param array $args Array of sharing service options.
113
		 */
114
		$id = apply_filters( 'jetpack_sharing_display_id', $id, $this, $args );
115
		/**
116
		 * Filter the sharing display link.
117
		 *
118
		 * @module sharedaddy
119
		 *
120
		 * @since 2.8.0
121
		 *
122
		 * @param string $url Post URL.
123
		 * @param object $this Sharing service properties.
124
		 * @param int|false $id Sharing ID.
125
		 * @param array $args Array of sharing service options.
126
		 */
127
		$url = apply_filters( 'sharing_display_link', $url, $this, $id, $args ); // backwards compatibility
128
		/**
129
		 * Filter the sharing display link.
130
		 *
131
		 * @module sharedaddy
132
		 *
133
		 * @since 2.8.0
134
		 *
135
		 * @param string $url Post URL.
136
		 * @param object $this Sharing service properties.
137
		 * @param int|false $id Sharing ID.
138
		 * @param array $args Array of sharing service options.
139
		 */
140
		$url = apply_filters( 'jetpack_sharing_display_link', $url, $this, $id, $args );
141
		/**
142
		 * Filter the sharing display query.
143
		 *
144
		 * @module sharedaddy
145
		 *
146
		 * @since 2.8.0
147
		 *
148
		 * @param string $query Sharing service URL parameter.
149
		 * @param object $this Sharing service properties.
150
		 * @param int|false $id Sharing ID.
151
		 * @param array $args Array of sharing service options.
152
		 */
153
		$query = apply_filters( 'jetpack_sharing_display_query', $query, $this, $id, $args );
154
155
		if ( ! empty( $query ) ) {
156
			if ( false === stripos( $url, '?' ) ) {
157
				$url .= '?' . $query;
158
			} else {
159
				$url .= '&amp;' . $query;
160
			}
161
		}
162
163
		if ( 'text' == $this->button_style ) {
164
			$klasses[] = 'no-icon';
165
		}
166
167
		/**
168
		 * Filter the sharing display classes.
169
		 *
170
		 * @module sharedaddy
171
		 *
172
		 * @since 3.4.0
173
		 *
174
		 * @param array $klasses Sharing service classes.
175
		 * @param object $this Sharing service properties.
176
		 * @param int|false $id Sharing ID.
177
		 * @param array $args Array of sharing service options.
178
		 */
179
		$klasses = apply_filters( 'jetpack_sharing_display_classes', $klasses, $this, $id, $args );
180
		/**
181
		 * Filter the sharing display title.
182
		 *
183
		 * @module sharedaddy
184
		 *
185
		 * @since 3.4.0
186
		 *
187
		 * @param string $title Sharing service title.
188
		 * @param object $this Sharing service properties.
189
		 * @param int|false $id Sharing ID.
190
		 * @param array $args Array of sharing service options.
191
		 */
192
		$title = apply_filters( 'jetpack_sharing_display_title', $title, $this, $id, $args );
193
		/**
194
		 * Filter the sharing display text.
195
		 *
196
		 * @module sharedaddy
197
		 *
198
		 * @since 3.4.0
199
		 *
200
		 * @param string $text Sharing service text.
201
		 * @param object $this Sharing service properties.
202
		 * @param int|false $id Sharing ID.
203
		 * @param array $args Array of sharing service options.
204
		 */
205
		$text = apply_filters( 'jetpack_sharing_display_text', $text, $this, $id, $args );
206
207
		return sprintf(
208
			'<a rel="nofollow%s" data-shared="%s" class="%s" href="%s"%s title="%s"><span%s>%s</span></a>',
209
			( true == $this->open_link_in_new ) ? ' noopener noreferrer' : '',
210
			( $id ? esc_attr( $id ) : '' ),
211
			implode( ' ', $klasses ),
212
			$url,
213
			( true == $this->open_link_in_new ) ? ' target="_blank"' : '',
214
			$title,
215
			( 'icon' == $this->button_style ) ? '></span><span class="sharing-screen-reader-text"' : '',
216
			$text
217
		);
218
	}
219
220
	/**
221
	 * Get an unfiltered post permalink to use when generating a sharing URL with get_link.
222
	 * Use instead of get_share_url for non-official styles as get_permalink ensures that process_request
223
	 * will be executed more reliably, in the case that the filtered URL uses a service that strips query parameters.
224
	 *
225
	 * @since 3.7.0
226
	 * @param int $post_id Post ID.
227
	 * @uses get_permalink
228
	 * @return string get_permalink( $post_id ) Post permalink.
229
	 */
230
	public function get_process_request_url( $post_id ) {
231
		return get_permalink( $post_id );
232
	}
233
234
	abstract public function get_name();
235
	abstract public function get_display( $post );
236
237
	public function display_header() {
238
	}
239
240
	public function display_footer() {
241
	}
242
243
	public function has_advanced_options() {
244
		return false;
245
	}
246
247
	public function display_preview( $echo = true, $force_smart = false, $button_style = null ) {
248
		$text = '&nbsp;';
249
		$button_style = ( ! empty( $button_style ) ) ? $button_style : $this->button_style;
250
		if ( ! $this->smart && ! $force_smart ) {
251
			if ( $button_style != 'icon' ) {
252
				$text = $this->get_name();
253
			}
254
		}
255
256
		$klasses = array( 'share-' . $this->get_class(), 'sd-button' );
257
258
		if ( $button_style == 'icon' || $button_style == 'icon-text' ) {
259
			$klasses[] = 'share-icon';
260
		}
261
262
		if ( $button_style == 'icon' ) {
263
			$klasses[] = 'no-text';
264
		}
265
266
		if ( $button_style == 'text' ) {
267
			$klasses[] = 'no-icon';
268
		}
269
270
		$is_deprecated = $this->is_deprecated();
271
272
		$link = sprintf(
273
			'<a rel="nofollow" class="%s" href="javascript:void(0)" title="%s"><span>%s</span></a>',
274
			implode( ' ', $klasses ),
275
			esc_attr(
276
				$is_deprecated
277
					? sprintf( __( 'The %1$s service has shut down. This sharing button is not displayed to your visitors and should be removed.', 'jetpack' ), $this->get_name() )
278
					: $this->get_name()
279
			),
280
			esc_html(
281
				$is_deprecated
282
					? sprintf( __( '%1$s has shut down', 'jetpack' ), $this->get_name() )
283
					: $text
284
			)
285
		);
286
287
		$smart = ( $this->smart || $force_smart ) ? 'on' : 'off';
288
		$return = "<div class='option option-smart-$smart'>$link</div>";
289
		if ( $echo ) {
290
			echo $return;
291
		}
292
293
		return $return;
294
	}
295
296
	public function get_total( $post = false ) {
297
		global $wpdb, $blog_id;
298
299
		$name = strtolower( $this->get_id() );
300
301
		if ( $post == 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...
302
			// get total number of shares for service
303
			return (int) $wpdb->get_var( $wpdb->prepare( 'SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d AND share_service = %s', $blog_id, $name ) );
304
		}
305
306
		// get total shares for a post
307
		return (int) $wpdb->get_var( $wpdb->prepare( 'SELECT count FROM sharing_stats WHERE blog_id = %d AND post_id = %d AND share_service = %s', $blog_id, $post->ID, $name ) );
308
	}
309
310 View Code Duplication
	public function get_posts_total() {
311
		global $wpdb, $blog_id;
312
313
		$totals = array();
314
		$name	= strtolower( $this->get_id() );
315
316
		$my_data = $wpdb->get_results( $wpdb->prepare( 'SELECT post_id as id, SUM( count ) as total FROM sharing_stats WHERE blog_id = %d AND share_service = %s GROUP BY post_id ORDER BY count DESC ', $blog_id, $name ) );
317
318
		if ( ! empty( $my_data ) ) {
319
			foreach ( $my_data as $row ) {
320
				$totals[] = new Sharing_Post_Total( $row->id, $row->total );
321
			}
322
		}
323
324
		usort( $totals, array( 'Sharing_Post_Total', 'cmp' ) );
325
326
		return $totals;
327
	}
328
329
	public function process_request( $post, array $post_data ) {
330
		/**
331
		 * Fires when a post is shared via one of the sharing buttons.
332
		 *
333
		 * @module sharedaddy
334
		 *
335
		 * @since 1.1.0
336
		 *
337
		 * @param array $args Aray of information about the sharing service.
338
		 */
339
		do_action( 'sharing_bump_stats', array( 'service' => $this, 'post' => $post ) );
340
	}
341
342
	public function js_dialog( $name, $params = array() ) {
343
		if ( true !== $this->open_link_in_new ) {
344
			return;
345
		}
346
347
		$defaults = array(
348
			'menubar'	=> 1,
349
			'resizable' => 1,
350
			'width'		=> 600,
351
			'height'	=> 400,
352
		);
353
		$params = array_merge( $defaults, $params );
354
		$opts = array();
355
		foreach ( $params as $key => $val ) {
356
			$opts[] = "$key=$val";
357
		}
358
		$opts = implode( ',', $opts );
359
360
		// Add JS after sharing-js has been enqueued.
361
		wp_add_inline_script( 'sharing-js',
362
			"var windowOpen;
363
			jQuery( document.body ).on( 'click', 'a.share-$name', function() {
364
				// If there's another sharing window open, close it.
365
				if ( 'undefined' !== typeof windowOpen ) {
366
					windowOpen.close();
367
				}
368
				windowOpen = window.open( jQuery( this ).attr( 'href' ), 'wpcom$name', '$opts' );
369
				return false;
370
			});"
371
		);
372
	}
373
}
374
375
abstract class Deprecated_Sharing_Source extends Sharing_Source {
376
	public	  $button_style = 'text';
377
	public	  $smart = false;
378
	protected $open_link_in_new = false;
379
	protected $id;
380
	protected $deprecated = true;
381
382
	final public function __construct( $id, array $settings ) {
383
		$this->id = $id;
384
	}
385
386
	final public function is_deprecated() {
387
		return true;
388
	}
389
390
	final public function get_share_url( $post_id ) {
391
		return get_permalink( $post_id );
392
	}
393
394
	final public function display_preview( $echo = true, $force_smart = false, $button_style = null ) {
395
		return parent::display_preview( $echo, false, $button_style );
396
	}
397
398
	final public function get_total() {
399
		return 0;
400
	}
401
402
	final public function get_posts_total() {
403
		return 0;
404
	}
405
406
	final public function process_request( $post, array $post_data ) {
407
		parent::process_request( $post, $post_data );
408
	}
409
410
	final public function get_display( $post ) {
411
		if ( current_user_can( 'manage_options' ) ) {
412
			return $this->display_deprecated( $post );
413
		}
414
415
		return '';
416
	}
417
418
	public function display_deprecated( $post ) {
419
		return $this->get_link(
420
			$this->get_share_url( $post->ID ),
421
			sprintf( __( '%1$s has shut down', 'jetpack' ), $this->get_name() ),
422
			sprintf( __( 'The %1$s service has shut down. This sharing button is not displayed to your visitors and should be removed.', 'jetpack' ), $this->get_name() )
423
		);
424
	}
425
}
426
427
abstract class Sharing_Advanced_Source extends Sharing_Source {
428
	public function has_advanced_options() {
429
		return true;
430
	}
431
432
	abstract public function display_options();
433
	abstract public function update_options( array $data );
434
	abstract public function get_options();
435
}
436
437
class Share_Email extends Sharing_Source {
438
	public $shortname = 'email';
439
	public $icon = '\f410';
440 View Code Duplication
	public function __construct( $id, array $settings ) {
441
		parent::__construct( $id, $settings );
442
443
		if ( 'official' == $this->button_style ) {
444
			$this->smart = true;
445
		} else {
446
			$this->smart = false;
447
		}
448
	}
449
450
	public function get_name() {
451
		return _x( 'Email', 'as sharing source', 'jetpack' );
452
	}
453
454
	// Default does nothing
455
	public function process_request( $post, array $post_data ) {
456
		$ajax = false;
457
		if ( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && strtolower( $_SERVER['HTTP_X_REQUESTED_WITH'] ) == 'xmlhttprequest' ) {
458
			$ajax = true;
459
		}
460
461
		$source_email = $target_email = $source_name = false;
0 ignored issues
show
Unused Code introduced by
$source_name is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
462
463
		if ( isset( $post_data['source_email'] ) && is_email( $post_data['source_email'] ) ) {
464
			$source_email = $post_data['source_email'];
465
		}
466
467
		if ( isset( $post_data['target_email'] ) && is_email( $post_data['target_email'] ) ) {
468
			$target_email = $post_data['target_email'];
469
		}
470
471
		if ( isset( $post_data['source_name'] ) && strlen( $post_data['source_name'] ) < 200 ) {
472
			$source_name = $post_data['source_name'];
473
		} elseif ( isset( $post_data['source_name'] ) ) {
474
			$source_name = substr( $post_data['source_name'], 0, 200 );
475
		} else {
476
			$source_name = '';
477
		}
478
479
		// Test email
480
		$error = 1;	  // Failure in data
481
		if ( empty( $post_data['source_f_name'] ) && $source_email && $target_email && $source_name ) {
482
			/**
483
			 * Allow plugins to stop the email sharing button from running the shared message through Akismet.
484
			 *
485
			 * @module sharedaddy
486
			 *
487
			 * @since 1.1.0
488
			 *
489
			 * @param bool true Should we check if the message isn't spam?
490
			 * @param object $post Post information.
491
			 * @param array $post_data Information about the shared message.
492
			 */
493
			if ( apply_filters( 'sharing_email_check', true, $post, $post_data ) ) {
494
				$data = array(
495
					'post'           => $post,
496
					'source'         => $source_email,
497
					'target'         => $target_email,
498
					'name'           => $source_name,
499
					'sharing_source' => $this,
500
				);
501
				// todo: implement an error message when email doesn't get sent.
502
				/**
503
				 * Filter whether an email can be sent from the Email sharing button.
504
				 *
505
				 * @module sharedaddy
506
				 *
507
				 * @since 1.1.0
508
				 *
509
				 * @param array $data Array of information about the shared message.
510
				 */
511
				if ( ( $data = apply_filters( 'sharing_email_can_send', $data ) ) !== false ) {
512
					// Record stats
513
					parent::process_request( $data['post'], $post_data );
514
515
					/**
516
					 * Fires when an email is sent via the Email sharing button.
517
					 *
518
					 * @module sharedaddy
519
					 *
520
					 * @since 1.1.0
521
					 *
522
					 * @param array $data Array of information about the shared message.
523
					 */
524
					do_action( 'sharing_email_send_post', $data );
525
				}
526
527
				// Return a positive regardless of whether the user is subscribed or not
528
				if ( $ajax ) {
529
?>
530
<div class="response">
531
	<div class="response-title"><?php _e( 'This post has been shared!', 'jetpack' ); ?></div>
532
	<div class="response-sub"><?php printf( __( 'You have shared this post with %s', 'jetpack' ), esc_html( $target_email ) ); ?></div>
533
	<div class="response-close"><a href="#" class="sharing_cancel"><?php _e( 'Close', 'jetpack' ); ?></a></div>
534
</div>
535
<?php
536
				} else {
537
					wp_safe_redirect( get_permalink( $post->ID ) . '?shared=email' );
538
				}
539
540
				die();
541
			} else {
542
				$error = 2;	  // Email check failed
543
			}
544
		}
545
546
		if ( $ajax ) {
547
			echo $error;
548
		} else {
549
			wp_safe_redirect( get_permalink( $post->ID ) . '?shared=email&msg=fail' );
550
		}
551
552
		die();
553
	}
554
555
	public function get_display( $post ) {
556
		return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Email', 'share to', 'jetpack' ), __( 'Click to email this to a friend', 'jetpack' ), 'share=email' );
557
	}
558
559
	/**
560
	 * Outputs the hidden email dialog
561
	 */
562
	public function display_footer() {
563
		global $current_user;
564
565
		$visible = $status = false;
0 ignored issues
show
Unused Code introduced by
$status is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Unused Code introduced by
$visible is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
566
?>
567
	<div id="sharing_email" style="display: none;">
568
		<form action="<?php echo esc_url( $_SERVER['REQUEST_URI'] ); ?>" method="post">
569
			<label for="target_email"><?php _e( 'Send to Email Address', 'jetpack' ) ?></label>
570
			<input type="email" name="target_email" id="target_email" value="" />
571
572
			<?php if ( is_user_logged_in() ) : ?>
573
				<input type="hidden" name="source_name" value="<?php echo esc_attr( $current_user->display_name ); ?>" />
574
				<input type="hidden" name="source_email" value="<?php echo esc_attr( $current_user->user_email ); ?>" />
575
			<?php else : ?>
576
577
				<label for="source_name"><?php _e( 'Your Name', 'jetpack' ) ?></label>
578
				<input type="text" name="source_name" id="source_name" value="" />
579
580
				<label for="source_email"><?php _e( 'Your Email Address', 'jetpack' ) ?></label>
581
				<input type="email" name="source_email" id="source_email" value="" />
582
583
			<?php endif; ?>
584
			<input type="text" id="jetpack-source_f_name" name="source_f_name" class="input" value="" size="25" autocomplete="off" title="<?php esc_attr_e( 'This field is for validation and should not be changed', 'jetpack' ); ?>" />
585
			<?php
586
				/**
587
				 * Fires when the Email sharing dialog is loaded.
588
				 *
589
				 * @module sharedaddy
590
				 *
591
				 * @since 1.1.0
592
				 *
593
				 * @param string jetpack Eail sharing source.
594
				 */
595
				do_action( 'sharing_email_dialog', 'jetpack' );
596
			?>
597
598
			<img style="float: right; display: none" class="loading" src="<?php
599
			/** This filter is documented in modules/stats.php */
600
			echo apply_filters( 'jetpack_static_url', plugin_dir_url( __FILE__ ) . 'images/loading.gif' ); ?>" alt="loading" width="16" height="16" />
601
			<input type="submit" value="<?php esc_attr_e( 'Send Email', 'jetpack' ); ?>" class="sharing_send" />
602
			<a rel="nofollow" href="#cancel" class="sharing_cancel" role="button"><?php _e( 'Cancel', 'jetpack' ); ?></a>
603
604
			<div class="errors errors-1" style="display: none;">
605
				<?php _e( 'Post was not sent - check your email addresses!', 'jetpack' ); ?>
606
			</div>
607
608
			<div class="errors errors-2" style="display: none;">
609
				<?php _e( 'Email check failed, please try again', 'jetpack' ); ?>
610
			</div>
611
612
			<div class="errors errors-3" style="display: none;">
613
				<?php _e( 'Sorry, your blog cannot share posts by email.', 'jetpack' ); ?>
614
			</div>
615
		</form>
616
	</div>
617
<?php
618
	}
619
}
620
621
class Share_Twitter extends Sharing_Source {
622
	public $shortname = 'twitter';
623
	public $icon = '\f202';
624
	// 'https://dev.twitter.com/rest/reference/get/help/configuration' ( 2015/02/06 ) short_url_length is 22, short_url_length_https is 23
625
	public $short_url_length = 24;
626
627 View Code Duplication
	public function __construct( $id, array $settings ) {
628
		parent::__construct( $id, $settings );
629
630
		if ( 'official' == $this->button_style ) {
631
			$this->smart = true;
632
		} else {
633
			$this->smart = false;
634
		}
635
	}
636
637
	public function get_name() {
638
		return __( 'Twitter', 'jetpack' );
639
	}
640
641
	/**
642
	 * Determine the Twitter 'via' value for a post.
643
	 *
644
	 * @param  WP_Post|int $post Post object or post ID.
645
	 * @return string Twitter handle without the preceding @.
646
	 **/
647
	public static function sharing_twitter_via( $post ) {
648
		$post = get_post( $post );
649
		/**
650
		 * Allow third-party plugins to customize the Twitter username used as "twitter:site" Twitter Card Meta Tag.
651
		 *
652
		 * @module sharedaddy
653
		 *
654
		 * @since 3.0.0
655
		 *
656
		 * @param string $string Twitter Username.
657
		 * @param array $args Array of Open Graph Meta Tags and Twitter Cards tags.
658
		 */
659
		$twitter_site_tag_value = apply_filters(
660
			'jetpack_twitter_cards_site_tag',
661
			'',
662
			/** This action is documented in modules/sharedaddy/sharing-sources.php */
663
			array( 'twitter:creator' => apply_filters( 'jetpack_sharing_twitter_via', '', $post->ID ) )
664
		);
665
666
		/*
667
		 * Hack to remove the unwanted behavior of adding 'via @jetpack' which
668
		 * was introduced with the adding of the Twitter cards.
669
		 * This should be a temporary solution until a better method is setup.
670
		 */
671
		if ( 'jetpack' == $twitter_site_tag_value ) {
672
			$twitter_site_tag_value = '';
673
		}
674
675
		/**
676
		 * Filters the Twitter username used as "via" in the Twitter sharing button.
677
		 *
678
		 * @module sharedaddy
679
		 *
680
		 * @since 1.7.0
681
		 *
682
		 * @param string $twitter_site_tag_value Twitter Username.
683
		 * @param int $post->ID Post ID.
684
		 */
685
		$twitter_site_tag_value = apply_filters( 'jetpack_sharing_twitter_via', $twitter_site_tag_value, $post->ID );
686
687
		// Strip out anything other than a letter, number, or underscore.
688
		// This will prevent the inadvertent inclusion of an extra @, as well as normalizing the handle.
689
		return preg_replace( '/[^\da-z_]+/i', '', $twitter_site_tag_value );
690
	}
691
692
	/**
693
	 * Determine the 'related' Twitter accounts for a post.
694
	 *
695
	 * @param  WP_Post|int $post Post object or post ID.
696
	 * @return string Comma-separated list of Twitter handles.
697
	 **/
698
	public static function get_related_accounts( $post ) {
699
		$post = get_post( $post );
700
		/**
701
		 * Filter the list of related Twitter accounts added to the Twitter sharing button.
702
		 *
703
		 * @module sharedaddy
704
		 *
705
		 * @since 1.7.0
706
		 *
707
		 * @param array $args Array of Twitter usernames. Format is 'username' => 'Optional description'
708
		 * @param int $post->ID Post ID.
709
		 */
710
		$related_accounts = apply_filters( 'jetpack_sharing_twitter_related', array(), $post->ID );
711
712
		// Example related string: account1,account2:Account 2 description,account3
713
		$related = array();
714
715
		foreach ( $related_accounts as $related_account_username => $related_account_description ) {
716
			// Join the description onto the end of the username
717
			if ( $related_account_description ) {
718
				$related_account_username .= ':' . $related_account_description;
719
			}
720
721
			$related[] = $related_account_username;
722
		}
723
724
		return implode( ',', $related );
725
	}
726
727
	public function get_display( $post ) {
728
		$via = $this->sharing_twitter_via( $post );
729
730
		if ( $via ) {
731
			$via = 'data-via="' . esc_attr( $via ) . '"';
732
		} else {
733
			$via = '';
734
		}
735
736
		$related = $this->get_related_accounts( $post );
737
		if ( ! empty( $related ) && $related !== $via ) {
738
			$related = 'data-related="' . esc_attr( $related ) . '"';
739
		} else {
740
			$related = '';
741
		}
742
743
		if ( $this->smart ) {
744
			$share_url = $this->get_share_url( $post->ID );
745
			$post_title = $this->get_share_title( $post->ID );
746
			return sprintf(
747
				'<a href="https://twitter.com/share" class="twitter-share-button" data-url="%1$s" data-text="%2$s" %3$s %4$s>Tweet</a>',
748
				esc_url( $share_url ),
749
				esc_attr( $post_title ),
750
				$via,
751
				$related
752
			);
753
		} else {
754
			if (
755
				/**
756
				 * Allow plugins to disable sharing counts for specific sharing services.
757
				 *
758
				 * @module sharedaddy
759
				 *
760
				 * @since 3.0.0
761
				 *
762
				 * @param bool true Should sharing counts be enabled for this specific service. Default to true.
763
				 * @param int $post->ID Post ID.
764
				 * @param string $str Sharing service name.
765
				 */
766
				apply_filters( 'jetpack_register_post_for_share_counts', true, $post->ID, 'twitter' )
767
			) {
768
				sharing_register_post_for_share_counts( $post->ID );
769
			}
770
			return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Twitter', 'share to', 'jetpack' ), __( 'Click to share on Twitter', 'jetpack' ), 'share=twitter', 'sharing-twitter-' . $post->ID );
771
		}
772
	}
773
774
	public function process_request( $post, array $post_data ) {
775
		$post_title = $this->get_share_title( $post->ID );
776
		$post_link = $this->get_share_url( $post->ID );
777
778
		if ( function_exists( 'mb_stripos' ) ) {
779
			$strlen = 'mb_strlen';
780
			$substr = 'mb_substr';
781
		} else {
782
			$strlen = 'strlen';
783
			$substr = 'substr';
784
		}
785
786
		$via = $this->sharing_twitter_via( $post );
787
		$related = $this->get_related_accounts( $post );
788
		if ( $via ) {
789
			$sig = " via @$via";
790
			if ( $related === $via ) {
791
				$related = false;
792
			}
793
		} else {
794
			$via = false;
795
			$sig = '';
796
		}
797
798
		$suffix_length = $this->short_url_length + $strlen( $sig );
799
		// $sig is handled by twitter in their 'via' argument.
800
		// $post_link is handled by twitter in their 'url' argument.
801
		if ( 280 < $strlen( $post_title ) + $suffix_length ) {
802
			// The -1 is for "\xE2\x80\xA6", a UTF-8 ellipsis.
803
			$text = $substr( $post_title, 0, 280 - $suffix_length - 1 ) . "\xE2\x80\xA6";
804
		} else {
805
			$text = $post_title;
806
		}
807
808
		// Record stats
809
		parent::process_request( $post, $post_data );
810
811
		$url = $post_link;
812
		$twitter_url = add_query_arg(
813
			rawurlencode_deep( array_filter( compact( 'via', 'related', 'text', 'url' ) ) ),
814
			'https://twitter.com/intent/tweet'
815
		);
816
817
		// Redirect to Twitter
818
		wp_redirect( $twitter_url );
819
		die();
820
	}
821
822
	public function has_custom_button_style() {
823
		return $this->smart;
824
	}
825
826
	public function display_footer() {
827
		if ( $this->smart ) {
828
			?>
829
			<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
830
			<?php
831
		} else {
832
			$this->js_dialog( $this->shortname, array( 'height' => 350 ) );
833
		}
834
	}
835
}
836
837
838
class Share_Reddit extends Sharing_Source {
839
	public $shortname = 'reddit';
840
	public $icon = '\f222';
841 View Code Duplication
	public function __construct( $id, array $settings ) {
842
		parent::__construct( $id, $settings );
843
844
		if ( 'official' == $this->button_style ) {
845
			$this->smart = true;
846
		} else {
847
			$this->smart = false;
848
		}
849
	}
850
851
	public function get_name() {
852
		return __( 'Reddit', 'jetpack' );
853
	}
854
855
	public function get_display( $post ) {
856
		if ( $this->smart ) {
857
			return '<div class="reddit_button"><iframe src="' . $this->http() . '://www.reddit.com/static/button/button1.html?newwindow=true&width=120&amp;url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&amp;title=' . rawurlencode( $this->get_share_title( $post->ID ) ) . '" height="22" width="120" scrolling="no" frameborder="0"></iframe></div>';
858 View Code Duplication
		} else {
859
			return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Reddit', 'share to', 'jetpack' ), __( 'Click to share on Reddit', 'jetpack' ), 'share=reddit' );
860
		}
861
	}
862
863 View Code Duplication
	public function process_request( $post, array $post_data ) {
864
		$reddit_url = $this->http() . '://reddit.com/submit?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&title=' . rawurlencode( $this->get_share_title( $post->ID ) );
865
866
		// Record stats
867
		parent::process_request( $post, $post_data );
868
869
		// Redirect to Reddit
870
		wp_redirect( $reddit_url );
871
		die();
872
	}
873
}
874
875
class Share_LinkedIn extends Sharing_Source {
876
	public $shortname = 'linkedin';
877
	public $icon = '\f207';
878 View Code Duplication
	public function __construct( $id, array $settings ) {
879
		parent::__construct( $id, $settings );
880
881
		if ( 'official' == $this->button_style ) {
882
			$this->smart = true;
883
		} else {
884
			$this->smart = false;
885
		}
886
	}
887
888
	public function get_name() {
889
		return __( 'LinkedIn', 'jetpack' );
890
	}
891
892
	public function has_custom_button_style() {
893
		return $this->smart;
894
	}
895
896
	public function get_display( $post ) {
897
		$display = '';
898
899
		if ( $this->smart ) {
900
			$share_url = $this->get_share_url( $post->ID );
901
			$display .= sprintf( '<div class="linkedin_button"><script type="in/share" data-url="%s" data-counter="right"></script></div>', esc_url( $share_url ) );
902 View Code Duplication
		} else {
903
			$display = $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'LinkedIn', 'share to', 'jetpack' ), __( 'Click to share on LinkedIn', 'jetpack' ), 'share=linkedin', 'sharing-linkedin-' . $post->ID );
904
		}
905
906
		/** This filter is already documented in modules/sharedaddy/sharing-sources.php */
907
		if ( apply_filters( 'jetpack_register_post_for_share_counts', true, $post->ID, 'linkedin' ) ) {
908
			sharing_register_post_for_share_counts( $post->ID );
909
		}
910
911
		return $display;
912
	}
913
914 View Code Duplication
	public function process_request( $post, array $post_data ) {
915
916
		$post_link = $this->get_share_url( $post->ID );
917
918
		// Using the same URL as the official button, which is *not* LinkedIn's documented sharing link
919
		// https://www.linkedin.com/cws/share?url={url}&token=&isFramed=false
920
		$linkedin_url = add_query_arg( array(
921
			'url' => rawurlencode( $post_link ),
922
		), 'https://www.linkedin.com/cws/share?token=&isFramed=false' );
923
924
		// Record stats
925
		parent::process_request( $post, $post_data );
926
927
		// Redirect to LinkedIn
928
		wp_redirect( $linkedin_url );
929
		die();
930
	}
931
932 View Code Duplication
	public function display_footer() {
933
		if ( ! $this->smart ) {
934
			$this->js_dialog( $this->shortname, array( 'width' => 580, 'height' => 450 ) );
935
		} else {
936
			?><script type="text/javascript">
937
			jQuery( document ).ready( function() {
938
				jQuery.getScript( 'https://platform.linkedin.com/in.js?async=true', function success() {
939
					IN.init();
940
				});
941
			});
942
			jQuery( document.body ).on( 'post-load', function() {
943
				if ( typeof IN != 'undefined' )
944
					IN.parse();
945
			});
946
			</script><?php
947
		}
948
	}
949
}
950
951
class Share_Facebook extends Sharing_Source {
952
	public $shortname = 'facebook';
953
	public $icon = '\f204';
954
	private $share_type = 'default';
955
956 View Code Duplication
	public function __construct( $id, array $settings ) {
957
		parent::__construct( $id, $settings );
958
959
		if ( isset( $settings['share_type'] ) ) {
960
			$this->share_type = $settings['share_type'];
961
		}
962
963
		if ( 'official' == $this->button_style ) {
964
			$this->smart = true;
965
		} else {
966
			$this->smart = false;
967
		}
968
	}
969
970
	public function get_name() {
971
		return __( 'Facebook', 'jetpack' );
972
	}
973
974
	public function display_header() {
975
	}
976
977 View Code Duplication
	function guess_locale_from_lang( $lang ) {
978
		if ( 'en' == $lang || 'en_US' == $lang || ! $lang ) {
979
			return 'en_US';
980
		}
981
982
		if ( ! class_exists( 'GP_Locales' ) ) {
983
			if ( ! defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) || ! file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) {
984
				return false;
985
			}
986
987
			require JETPACK__GLOTPRESS_LOCALES_PATH;
988
		}
989
990
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
991
			// WP.com: get_locale() returns 'it'
992
			$locale = GP_Locales::by_slug( $lang );
993
		} else {
994
			// Jetpack: get_locale() returns 'it_IT';
995
			$locale = GP_Locales::by_field( 'wp_locale', $lang );
996
		}
997
998
		if ( ! $locale ) {
999
			return false;
1000
		}
1001
1002
		if ( empty( $locale->facebook_locale ) ) {
1003
			if ( empty( $locale->wp_locale ) ) {
1004
				return false;
1005
			} else {
1006
				// Facebook SDK is smart enough to fall back to en_US if a
1007
				// locale isn't supported. Since supported Facebook locales
1008
				// can fall out of sync, we'll attempt to use the known
1009
				// wp_locale value and rely on said fallback.
1010
				return $locale->wp_locale;
1011
			}
1012
		}
1013
1014
		return $locale->facebook_locale;
1015
	}
1016
1017
	public function get_display( $post ) {
1018
		if ( $this->smart ) {
1019
			$share_url = $this->get_share_url( $post->ID );
1020
			$fb_share_html = '<div class="fb-share-button" data-href="' . esc_attr( $share_url ) . '" data-layout="button_count"></div>';
1021
			/**
1022
			 * Filter the output of the Facebook Sharing button.
1023
			 *
1024
			 * @module sharedaddy
1025
			 *
1026
			 * @since 3.6.0
1027
			 *
1028
			 * @param string $fb_share_html Facebook Sharing button HTML.
1029
			 * @param string $share_url URL of the post to share.
1030
			 */
1031
			return apply_filters( 'jetpack_sharing_facebook_official_button_output', $fb_share_html, $share_url );
1032
		}
1033
1034
		/** This filter is already documented in modules/sharedaddy/sharing-sources.php */
1035
		if ( apply_filters( 'jetpack_register_post_for_share_counts', true, $post->ID, 'facebook' ) ) {
1036
			sharing_register_post_for_share_counts( $post->ID );
1037
		}
1038
		return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Facebook', 'share to', 'jetpack' ), __( 'Click to share on Facebook', 'jetpack' ), 'share=facebook', 'sharing-facebook-' . $post->ID );
1039
	}
1040
1041 View Code Duplication
	public function process_request( $post, array $post_data ) {
1042
		$fb_url = $this->http() . '://www.facebook.com/sharer.php?u=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&t=' . rawurlencode( $this->get_share_title( $post->ID ) );
1043
1044
		// Record stats
1045
		parent::process_request( $post, $post_data );
1046
1047
		// Redirect to Facebook
1048
		wp_redirect( $fb_url );
1049
		die();
1050
	}
1051
1052
	public function display_footer() {
1053
		$this->js_dialog( $this->shortname );
1054
		if ( $this->smart ) {
1055
			$locale = $this->guess_locale_from_lang( get_locale() );
1056
			if ( ! $locale ) {
1057
				$locale = 'en_US';
1058
			}
1059
			/**
1060
			 * Filter the App ID used in the official Facebook Share button.
1061
			 *
1062
			 * @since 3.8.0
1063
			 *
1064
			 * @param int $fb_app_id Facebook App ID. Default to 249643311490 (WordPress.com's App ID).
1065
			 */
1066
			$fb_app_id = apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' );
1067
			if ( is_numeric( $fb_app_id ) ) {
1068
				$fb_app_id = '&appId=' . $fb_app_id;
1069
			} else {
1070
				$fb_app_id = '';
1071
			}
1072
			?><div id="fb-root"></div>
1073
			<script>(function(d, s, id) { var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) return; js = d.createElement(s); js.id = id; js.src = 'https://connect.facebook.net/<?php echo $locale; ?>/sdk.js#xfbml=1<?php echo $fb_app_id; ?>&version=v2.3'; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk'));</script>
1074
			<script>
1075
			jQuery( document.body ).on( 'post-load', function() {
1076
				if ( 'undefined' !== typeof FB ) {
1077
					FB.XFBML.parse();
1078
				}
1079
			} );
1080
			</script>
1081
			<?php
1082
		}
1083
	}
1084
}
1085
1086
class Share_Print extends Sharing_Source {
1087
	public $shortname = 'print';
1088
	public $icon = '\f469';
1089 View Code Duplication
	public function __construct( $id, array $settings ) {
1090
		parent::__construct( $id, $settings );
1091
1092
		if ( 'official' == $this->button_style ) {
1093
			$this->smart = true;
1094
		} else {
1095
			$this->smart = false;
1096
		}
1097
	}
1098
1099
	public function get_name() {
1100
		return __( 'Print', 'jetpack' );
1101
	}
1102
1103
	public function get_display( $post ) {
1104
		return $this->get_link( $this->get_process_request_url( $post->ID ) . ( ( is_single() || is_page() ) ? '#print': '' ), _x( 'Print', 'share to', 'jetpack' ), __( 'Click to print', 'jetpack' ) );
1105
	}
1106
}
1107
1108
class Share_PressThis extends Sharing_Source {
1109
	public $shortname = 'pressthis';
1110
	public $icon = '\f205';
1111 View Code Duplication
	public function __construct( $id, array $settings ) {
1112
		parent::__construct( $id, $settings );
1113
1114
		if ( 'official' == $this->button_style ) {
1115
			$this->smart = true;
1116
		} else {
1117
			$this->smart = false;
1118
		}
1119
	}
1120
1121
	public function get_name() {
1122
		return __( 'Press This', 'jetpack' );
1123
	}
1124
1125
	public function process_request( $post, array $post_data ) {
1126
		global $current_user, $wp_version;
1127
1128
		$primary_blog = (int) get_user_meta( $current_user->ID, 'primary_blog', true );
1129
		if ( $primary_blog ) {
1130
			$primary_blog_details = get_blog_details( $primary_blog );
1131
		} else {
1132
			$primary_blog_details = false;
1133
		}
1134
1135
		if ( $primary_blog_details ) {
1136
			$blogs = array( $primary_blog_details );
1137
		} elseif ( function_exists( 'get_active_blogs_for_user' ) ) {
1138
			$blogs = get_active_blogs_for_user();
1139
			if ( empty( $blogs ) ) {
1140
				$blogs = get_blogs_of_user( $current_user->ID );
1141
			}
1142
		} else {
1143
			$blogs = get_blogs_of_user( $current_user->ID );
1144
		}
1145
1146
		if ( empty( $blogs ) ) {
1147
			wp_safe_redirect( get_permalink( $post->ID ) );
1148
			die();
1149
		}
1150
1151
		$blog = current( $blogs );
1152
1153
		$args = array(
1154
			'u' => rawurlencode( $this->get_share_url( $post->ID ) ),
1155
			);
1156
1157
		if ( version_compare( $wp_version, '4.9-RC1-42107', '>=' ) ) {
1158
			$args[ 'url-scan-submit' ] = 'Scan';
1159
			$args[ '_wpnonce' ]        = wp_create_nonce( 'scan-site' );
1160
1161
		} else { // Remove once 4.9 is the minimum.
1162
			$args['t'] = rawurlencode( $this->get_share_title( $post->ID ) );
1163
			if ( isset( $_GET['sel'] ) ) {
1164
				$args['s'] = rawurlencode( $_GET['sel'] );
1165
			}
1166
		}
1167
1168
		$url = $blog->siteurl . '/wp-admin/press-this.php';
1169
		$url = add_query_arg( $args, $url );
1170
1171
		// Record stats
1172
		parent::process_request( $post, $post_data );
1173
1174
		// Redirect to Press This
1175
		wp_redirect( $url );
1176
		die();
1177
	}
1178
1179
	public function get_display( $post ) {
1180
		return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Press This', 'share to', 'jetpack' ), __( 'Click to Press This!', 'jetpack' ), 'share=press-this' );
1181
	}
1182
}
1183
1184
class Share_GooglePlus1 extends Deprecated_Sharing_Source {
1185
	public function get_name() {
1186
		return __( 'Google+', 'jetpack' );
1187
	}
1188
}
1189
1190
class Share_Custom extends Sharing_Advanced_Source {
1191
	private $name;
1192
	private $icon;
1193
	private $url;
1194
	public $smart = true;
1195
	public $shortname;
1196
1197
	public function get_class() {
1198
		return 'custom share-custom-' . sanitize_html_class( strtolower( $this->name ) );
1199
	}
1200
1201
	public function __construct( $id, array $settings ) {
1202
		parent::__construct( $id, $settings );
1203
1204
		$opts = $this->get_options();
0 ignored issues
show
Unused Code introduced by
$opts is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1205
1206
		if ( isset( $settings['name'] ) ) {
1207
			$this->name = $settings['name'];
1208
			$this->shortname = preg_replace( '/[^a-z0-9]*/', '', $settings['name'] );
1209
		}
1210
1211
		if ( isset( $settings['icon'] ) ) {
1212
			$this->icon = $settings['icon'];
1213
1214
			$new_icon = esc_url_raw( wp_specialchars_decode( $this->icon, ENT_QUOTES ) );
1215
			$i = 0;
1216
			while ( $new_icon != $this->icon ) {
1217
				if ( $i > 5 ) {
1218
					$this->icon = false;
1219
					break;
1220
				} else {
1221
					$this->icon = $new_icon;
1222
					$new_icon = esc_url_raw( wp_specialchars_decode( $this->icon, ENT_QUOTES ) );
1223
				}
1224
				$i++;
1225
			}
1226
		}
1227
1228
		if ( isset( $settings['url'] ) ) {
1229
			$this->url = $settings['url'];
1230
		}
1231
	}
1232
1233
	public function get_name() {
1234
		return $this->name;
1235
	}
1236
1237
	public function get_display( $post ) {
1238
		$str = $this->get_link( $this->get_process_request_url( $post->ID ), esc_html( $this->name ), sprintf( __( 'Click to share on %s', 'jetpack' ), esc_attr( $this->name ) ), 'share=' . $this->id );
1239
		return str_replace( '<span>', '<span style="' . esc_attr( 'background-image:url("' . addcslashes( esc_url_raw( $this->icon ), '"' ) . '");' ) . '">', $str );
1240
	}
1241
1242
	public function process_request( $post, array $post_data ) {
1243
		$url = str_replace( '&amp;', '&', $this->url );
1244
		$url = str_replace( '%post_id%', rawurlencode( $post->ID ), $url );
1245
		$url = str_replace( '%post_url%', rawurlencode( $this->get_share_url( $post->ID ) ), $url );
1246
		$url = str_replace( '%post_full_url%', rawurlencode( get_permalink( $post->ID ) ), $url );
1247
		$url = str_replace( '%post_title%', rawurlencode( $this->get_share_title( $post->ID ) ), $url );
1248
		$url = str_replace( '%home_url%', rawurlencode( home_url() ), $url );
1249
		$url = str_replace( '%post_slug%', rawurlencode( $post->post_name ), $url );
1250
1251
		if ( strpos( $url, '%post_tags%' ) !== false ) {
1252
			$tags	= get_the_tags( $post->ID );
1253
			$tagged = '';
1254
1255
			if ( $tags ) {
1256
				$tagged_raw = array();
1257
				foreach ( $tags as $tag ) {
1258
					$tagged_raw[] = rawurlencode( $tag->name );
1259
				}
1260
1261
				$tagged = implode( ',', $tagged_raw );
1262
			}
1263
1264
			$url = str_replace( '%post_tags%', $tagged, $url );
1265
		}
1266
1267
		if ( strpos( $url, '%post_excerpt%' ) !== false ) {
1268
			$url_excerpt = $post->post_excerpt;
1269
			if ( empty( $url_excerpt ) ) {
1270
				$url_excerpt = $post->post_content;
1271
			}
1272
1273
			$url_excerpt = strip_tags( strip_shortcodes( $url_excerpt ) );
1274
			$url_excerpt = wp_html_excerpt( $url_excerpt, 100 );
1275
			$url_excerpt = rtrim( preg_replace( '/[^ .]*$/', '', $url_excerpt ) );
1276
			$url = str_replace( '%post_excerpt%', rawurlencode( $url_excerpt ), $url );
1277
		}
1278
1279
		// Record stats
1280
		parent::process_request( $post, $post_data );
1281
1282
		// Redirect
1283
		wp_redirect( $url );
1284
		die();
1285
	}
1286
1287
	public function display_options() {
1288
?>
1289
<div class="input">
1290
	<table class="form-table">
1291
		<tbody>
1292
			<tr>
1293
				<th scope="row"><?php _e( 'Label', 'jetpack' ); ?></th>
1294
				<td><input type="text" name="name" value="<?php echo esc_attr( $this->name ); ?>" /></td>
1295
			</tr>
1296
1297
			<tr>
1298
				<th scope="row"><?php _e( 'URL', 'jetpack' ); ?></th>
1299
				<td><input type="text" name="url" value="<?php echo esc_attr( $this->url ); ?>" /></td>
1300
			</tr>
1301
1302
			<tr>
1303
				<th scope="row"><?php _e( 'Icon', 'jetpack' ); ?></th>
1304
				<td><input type="text" name="icon" value="<?php echo esc_attr( $this->icon ); ?>" /></td>
1305
			</tr>
1306
1307
			<tr>
1308
				<th scope="row"></th>
1309
				<td>
1310
					<input class="button-secondary" type="submit" value="<?php esc_attr_e( 'Save', 'jetpack' ); ?>" />
1311
					<a href="#" class="remove"><small><?php _e( 'Remove Service', 'jetpack' ); ?></small></a>
1312
				</td>
1313
			</tr>
1314
		</tbody>
1315
	</table>
1316
</div>
1317
<?php
1318
	}
1319
1320
	public function update_options( array $data ) {
1321
		$name  = trim( wp_html_excerpt( wp_kses( stripslashes( $data['name'] ), array() ), 30 ) );
1322
		$url   = trim( esc_url_raw( $data['url'] ) );
1323
		$icon  = trim( esc_url_raw( $data['icon'] ) );
1324
1325
		if ( $name ) {
1326
			$this->name = $name;
1327
		}
1328
1329
		if ( $url ) {
1330
			$this->url	= $url;
1331
		}
1332
1333
		if ( $icon ) {
1334
			$this->icon = $icon;
1335
		}
1336
	}
1337
1338
	public function get_options() {
1339
		return array(
1340
			'name' => $this->name,
1341
			'icon' => $this->icon,
1342
			'url'  => $this->url,
1343
		);
1344
	}
1345
1346
	public function display_preview( $echo = true, $force_smart = false, $button_style = null ) {
1347
		$opts = $this->get_options();
1348
1349
		$text = '&nbsp;';
1350
		if ( ! $this->smart ) {
1351
			if ( $this->button_style != 'icon' ) {
1352
				$text = $this->get_name();
1353
			}
1354
		}
1355
1356
		$klasses = array( 'share-' . $this->shortname );
1357
1358
		if ( $this->button_style == 'icon' || $this->button_style == 'icon-text' ) {
1359
			$klasses[] = 'share-icon';
1360
		}
1361
1362
		if ( $this->button_style == 'icon' ) {
1363
			$text = '';
1364
			$klasses[] = 'no-text';
1365
		}
1366
1367
		if ( $this->button_style == 'text' ) {
1368
			$klasses[] = 'no-icon';
1369
		}
1370
1371
		$link = sprintf(
1372
			'<a rel="nofollow" class="%s" href="javascript:void(0)" title="%s"><span style="background-image:url(&quot;%s&quot;) !important;background-position:left center;background-repeat:no-repeat;">%s</span></a>',
1373
			implode( ' ', $klasses ),
1374
			$this->get_name(),
1375
			addcslashes( esc_url_raw( $opts['icon'] ), '"' ),
1376
			$text
1377
		);
1378
		?>
1379
		<div class="option option-smart-off">
1380
		<?php echo $link ; ?>
1381
		</div><?php
1382
	}
1383
}
1384
1385
class Share_Tumblr extends Sharing_Source {
1386
	public $shortname = 'tumblr';
1387
	public $icon = '\f214';
1388 View Code Duplication
	public function __construct( $id, array $settings ) {
1389
		parent::__construct( $id, $settings );
1390
		if ( 'official' == $this->button_style ) {
1391
			$this->smart = true;
1392
		} else {
1393
			$this->smart = false;
1394
		}
1395
	}
1396
1397
	public function get_name() {
1398
		return __( 'Tumblr', 'jetpack' );
1399
	}
1400
1401
	public function get_display( $post ) {
1402
		if ( $this->smart ) {
1403
			$target = '';
1404
			if ( true == $this->open_link_in_new ) {
1405
				$target = '_blank';
1406
			}
1407
1408
			/**
1409
			 * If we are looking at a single post, let Tumblr figure out the post type (text, photo, link, quote, chat, or video)
1410
			 * based on the content available on the page.
1411
			 * If we are not looking at a single post, content from other posts can appear on the page and Tumblr will pick that up.
1412
			 * In this case, we want Tumblr to focus on our current post, so we will limit the post type to link, where we can give Tumblr a link to our post.
1413
			 */
1414
			if ( ! is_single() ) {
1415
				$posttype = 'data-posttype="link"';
1416
			} else {
1417
				$posttype = '';
1418
			}
1419
1420
			// Documentation: https://www.tumblr.com/docs/en/share_button
1421
			return sprintf(
1422
				'<a class="tumblr-share-button" target="%1$s" href="%2$s" data-title="%3$s" data-content="%4$s" title="%5$s"%6$s>%5$s</a>',
1423
				$target,
1424
				'https://www.tumblr.com/share',
1425
				$this->get_share_title( $post->ID ),
1426
				$this->get_share_url( $post->ID ),
1427
				__( 'Share on Tumblr', 'jetpack' ),
1428
				$posttype
1429
			);
1430 View Code Duplication
		 } else {
1431
			return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Tumblr', 'share to', 'jetpack' ), __( 'Click to share on Tumblr', 'jetpack' ), 'share=tumblr' );
1432
		}
1433
	}
1434
1435 View Code Duplication
	public function process_request( $post, array $post_data ) {
1436
		// Record stats
1437
		parent::process_request( $post, $post_data );
1438
1439
		// Redirect to Tumblr's sharing endpoint (a la their bookmarklet)
1440
		$url = 'https://www.tumblr.com/share?v=3&u=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&t=' . rawurlencode( $this->get_share_title( $post->ID ) ) . '&s=';
1441
		wp_redirect( $url );
1442
		die();
1443
	}
1444
1445 View Code Duplication
	public function display_footer() {
1446
		if ( $this->smart ) {
1447
			?><script id="tumblr-js" type="text/javascript" src="https://assets.tumblr.com/share-button.js"></script><?php
1448
		} else {
1449
			$this->js_dialog( $this->shortname, array( 'width' => 450, 'height' => 450 ) );
1450
		}
1451
	}
1452
}
1453
1454
class Share_Pinterest extends Sharing_Source {
1455
	public $shortname = 'pinterest';
1456
	public $icon = '\f209';
1457
1458 View Code Duplication
	public function __construct( $id, array $settings ) {
1459
		parent::__construct( $id, $settings );
1460
		if ( 'official' == $this->button_style ) {
1461
			$this->smart = true;
1462
		} else {
1463
			$this->smart = false;
1464
		}
1465
	}
1466
1467
	public function get_name() {
1468
		return __( 'Pinterest', 'jetpack' );
1469
	}
1470
1471
	public function get_image( $post ) {
1472
		if ( class_exists( 'Jetpack_PostImages' ) ) {
1473
			$image = Jetpack_PostImages::get_image( $post->ID, array( 'fallback_to_avatars' => true ) );
1474
			if ( ! empty( $image ) ) {
1475
				return $image['src'];
1476
			}
1477
		}
1478
1479
		/**
1480
		 * Filters the default image used by the Pinterest Pin It share button.
1481
		 *
1482
		 * @module sharedaddy
1483
		 *
1484
		 * @since 3.6.0
1485
		 *
1486
		 * @param string $url Default image URL.
1487
		 */
1488
		return apply_filters( 'jetpack_sharing_pinterest_default_image', 'https://s0.wp.com/i/blank.jpg' );
1489
	}
1490
1491
	public function get_external_url( $post ) {
1492
		$url = 'https://www.pinterest.com/pin/create/button/?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&media=' . rawurlencode( $this->get_image( $post ) ) . '&description=' . rawurlencode( $post->post_title );
1493
1494
		/**
1495
		 * Filters the Pinterest share URL used in sharing button output.
1496
		 *
1497
		 * @module sharedaddy
1498
		 *
1499
		 * @since 3.6.0
1500
		 *
1501
		 * @param string $url Pinterest share URL.
1502
		 */
1503
		return apply_filters( 'jetpack_sharing_pinterest_share_url', $url );
1504
	}
1505
1506
	public function get_widget_type() {
1507
		/**
1508
		 * Filters the Pinterest widget type.
1509
		 *
1510
		 * @see https://business.pinterest.com/en/widget-builder
1511
		 *
1512
		 * @module sharedaddy
1513
		 *
1514
		 * @since 3.6.0
1515
		 *
1516
		 * @param string $type Pinterest widget type. Default of 'buttonPin' for single-image selection. 'buttonBookmark' for multi-image modal.
1517
		 */
1518
		return apply_filters( 'jetpack_sharing_pinterest_widget_type', 'buttonPin' );
1519
	}
1520
1521
	public function get_display( $post ) {
1522
		$display = '';
0 ignored issues
show
Unused Code introduced by
$display is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1523
1524
		if ( $this->smart ) {
1525
			$display = sprintf(
1526
				'<div class="pinterest_button"><a href="%s" data-pin-do="%s" data-pin-config="beside"><img src="//assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png" /></a></div>',
1527
				esc_url( $this->get_external_url( $post ) ),
1528
				esc_attr( $this->get_widget_type() )
1529
			);
1530 View Code Duplication
		} else {
1531
			$display = $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Pinterest', 'share to', 'jetpack' ), __( 'Click to share on Pinterest', 'jetpack' ), 'share=pinterest', 'sharing-pinterest-' . $post->ID );
1532
		}
1533
1534
		/** This filter is already documented in modules/sharedaddy/sharing-sources.php */
1535
		if ( apply_filters( 'jetpack_register_post_for_share_counts', true, $post->ID, 'linkedin' ) ) {
1536
			sharing_register_post_for_share_counts( $post->ID );
1537
		}
1538
1539
		return $display;
1540
	}
1541
1542
	public function process_request( $post, array $post_data ) {
1543
		// Record stats
1544
		parent::process_request( $post, $post_data );
1545
		// If we're triggering the multi-select panel, then we don't need to redirect to Pinterest
1546
		if ( ! isset( $_GET['js_only'] ) ) {
1547
			$pinterest_url = esc_url_raw( $this->get_external_url( $post ) );
1548
			wp_redirect( $pinterest_url );
1549
		} else {
1550
			echo '// share count bumped';
1551
		}
1552
		die();
1553
	}
1554
1555
	public function display_footer() {
1556
		/**
1557
		 * Filter the Pin it button appearing when hovering over images when using the official button style.
1558
		 *
1559
		 * @module sharedaddy
1560
		 *
1561
		 * @since 3.6.0
1562
		 *
1563
		 * @param bool $jetpack_pinit_over True by default, displays the Pin it button when hovering over images.
1564
		 */
1565
		$jetpack_pinit_over = apply_filters( 'jetpack_pinit_over_button', true );
1566
		?>
1567
		<?php if ( $this->smart ) : ?>
1568
			<script type="text/javascript">
1569
				// Pinterest shared resources
1570
				var s = document.createElement("script");
1571
				s.type = "text/javascript";
1572
				s.async = true;
1573
				<?php if ( $jetpack_pinit_over ) {
1574
				echo "s.setAttribute('data-pin-hover', true);";
1575
				} ?>
1576
				s.src = window.location.protocol + "//assets.pinterest.com/js/pinit.js";
1577
				var x = document.getElementsByTagName("script")[0];
1578
				x.parentNode.insertBefore(s, x);
1579
				// if 'Pin it' button has 'counts' make container wider
1580
				jQuery(window).load( function(){ jQuery( 'li.share-pinterest a span:visible' ).closest( '.share-pinterest' ).width( '80px' ); } );
1581
			</script>
1582
		<?php elseif ( 'buttonPin' != $this->get_widget_type() ) : ?>
1583
			<script type="text/javascript">
1584
				jQuery(document).ready( function(){
1585
					jQuery('body').on('click', 'a.share-pinterest', function(e){
1586
						e.preventDefault();
1587
						// Load Pinterest Bookmarklet code
1588
						var s = document.createElement("script");
1589
						s.type = "text/javascript";
1590
						s.src = window.location.protocol + "//assets.pinterest.com/js/pinmarklet.js?r=" + ( Math.random() * 99999999 );
1591
						var x = document.getElementsByTagName("script")[0];
1592
						x.parentNode.insertBefore(s, x);
1593
						// Trigger Stats
1594
						var s = document.createElement("script");
1595
						s.type = "text/javascript";
1596
						s.src = this + ( this.toString().indexOf( '?' ) ? '&' : '?' ) + 'js_only=1';
1597
						var x = document.getElementsByTagName("script")[0];
1598
						x.parentNode.insertBefore(s, x);
1599
					});
1600
				});
1601
			</script>
1602
		<?php endif;
1603
	}
1604
}
1605
1606
class Share_Pocket extends Sharing_Source {
1607
	public $shortname = 'pocket';
1608
	public $icon = '\f224';
1609
1610 View Code Duplication
	public function __construct( $id, array $settings ) {
1611
		parent::__construct( $id, $settings );
1612
1613
		if ( 'official' == $this->button_style ) {
1614
			$this->smart = true;
1615
		} else {
1616
			$this->smart = false;
1617
		}
1618
	}
1619
1620
	public function get_name() {
1621
		return __( 'Pocket', 'jetpack' );
1622
	}
1623
1624 View Code Duplication
	public function process_request( $post, array $post_data ) {
1625
		// Record stats
1626
		parent::process_request( $post, $post_data );
1627
1628
		$pocket_url = esc_url_raw( 'https://getpocket.com/save/?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&title=' . rawurlencode( $this->get_share_title( $post->ID ) ) );
1629
		wp_redirect( $pocket_url );
1630
		exit;
1631
	}
1632
1633
	public function get_display( $post ) {
1634
		if ( $this->smart ) {
1635
			$post_count = 'horizontal';
1636
1637
			$button = '';
1638
			$button .= '<div class="pocket_button">';
1639
			$button .= sprintf( '<a href="https://getpocket.com/save" class="pocket-btn" data-lang="%s" data-save-url="%s" data-pocket-count="%s" >%s</a>', 'en', esc_attr( $this->get_share_url( $post->ID ) ), $post_count, esc_attr__( 'Pocket', 'jetpack' ) );
1640
			$button .= '</div>';
1641
1642
			return $button;
1643 View Code Duplication
		} else {
1644
			return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Pocket', 'share to', 'jetpack' ), __( 'Click to share on Pocket', 'jetpack' ), 'share=pocket' );
1645
		}
1646
1647
	}
1648
1649 View Code Duplication
	function display_footer() {
1650
		if ( $this->smart ) :
1651
		?>
1652
		<script>
1653
		// Don't use Pocket's default JS as it we need to force init new Pocket share buttons loaded via JS.
1654
		function jetpack_sharing_pocket_init() {
1655
			jQuery.getScript( 'https://widgets.getpocket.com/v1/j/btn.js?v=1' );
1656
		}
1657
		jQuery( document ).ready( jetpack_sharing_pocket_init );
1658
		jQuery( document.body ).on( 'post-load', jetpack_sharing_pocket_init );
1659
		</script>
1660
		<?php
1661
		else :
1662
			$this->js_dialog( $this->shortname, array( 'width' => 450, 'height' => 450 ) );
1663
		endif;
1664
1665
	}
1666
1667
}
1668
1669
class Share_Telegram extends Sharing_Source {
1670
	public $shortname = 'telegram';
1671
1672
	public function __construct( $id, array $settings ) {
1673
		parent::__construct( $id, $settings );
1674
	}
1675
1676
	public function get_name() {
1677
		return __( 'Telegram', 'jetpack' );
1678
	}
1679 View Code Duplication
	public function process_request( $post, array $post_data ) {
1680
		// Record stats
1681
		parent::process_request( $post, $post_data );
1682
		$telegram_url = esc_url_raw( 'https://telegram.me/share/url?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&text=' . rawurlencode( $this->get_share_title( $post->ID ) ) );
1683
		wp_redirect( $telegram_url );
1684
		exit;
1685
	}
1686
1687
	public function get_display( $post ) {
1688
		return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Telegram', 'share to', 'jetpack' ), __( 'Click to share on Telegram', 'jetpack' ), 'share=telegram' );
1689
	}
1690
1691
	function display_footer() {
1692
		$this->js_dialog( $this->shortname, array( 'width' => 450, 'height' => 450 ) );
1693
	}
1694
}
1695
1696
class Jetpack_Share_WhatsApp extends Sharing_Source {
1697
	public $shortname = 'jetpack-whatsapp';
1698
1699
	public function __construct( $id, array $settings ) {
1700
		parent::__construct( $id, $settings );
1701
	}
1702
1703
	public function get_name() {
1704
		return __( 'WhatsApp', 'jetpack' );
1705
	}
1706
1707
	public function get_display( $post ) {
1708
		return $this->get_link( 'https://api.whatsapp.com/send?text=' . rawurlencode( $this->get_share_title( $post->ID ) . ' ' . $this->get_share_url( $post->ID ) ), _x( 'WhatsApp', 'share to', 'jetpack' ), __( 'Click to share on WhatsApp', 'jetpack' ) );
1709
	}
1710
}
1711
1712
class Share_Skype extends Sharing_Source {
1713
	public $shortname = 'skype';
1714
	public $icon = '\f220';
1715
	private $share_type = 'default';
1716
1717 View Code Duplication
	public function __construct( $id, array $settings ) {
1718
		parent::__construct( $id, $settings );
1719
1720
		if ( isset( $settings['share_type'] ) ) {
1721
			$this->share_type = $settings['share_type'];
1722
		}
1723
1724
		if ( 'official' == $this->button_style ) {
1725
			$this->smart = true;
1726
		} else {
1727
			$this->smart = false;
1728
		}
1729
1730
	}
1731
1732
	public function get_name() {
1733
		return __( 'Skype', 'jetpack' );
1734
	}
1735
1736
	public function get_display( $post ) {
1737
		if ( $this->smart ) {
1738
			$skype_share_html = sprintf(
1739
				'<div class="skype-share" data-href="%1$s" data-lang="%2$s" data-style="small" data-source="jetpack" ></div>',
1740
				esc_attr( $this->get_share_url( $post->ID ) ),
1741
				'en-US'
1742
			);
1743
			return $skype_share_html;
1744
		}
1745
1746
		/** This filter is already documented in modules/sharedaddy/sharing-sources.php */
1747
		if ( apply_filters( 'jetpack_register_post_for_share_counts', true, $post->ID, 'skype' ) ) {
1748
			sharing_register_post_for_share_counts( $post->ID );
1749
		}
1750
		return $this->get_link(
1751
			$this->get_process_request_url( $post->ID ), _x( 'Skype', 'share to', 'jetpack' ), __( 'Click to share on Skype', 'jetpack' ), 'share=skype', 'sharing-skype-' . $post->ID );
1752
	}
1753
1754 View Code Duplication
	public function process_request( $post, array $post_data ) {
1755
		$skype_url = sprintf(
1756
			'https://web.skype.com/share?url=%1$s&lang=%2$s=&source=jetpack',
1757
			rawurlencode( $this->get_share_url( $post->ID ) ),
1758
			'en-US'
1759
		);
1760
1761
		// Record stats
1762
		parent::process_request( $post, $post_data );
1763
1764
		// Redirect to Skype
1765
		wp_redirect( $skype_url );
1766
		die();
1767
	}
1768
1769
	public function display_footer() {
1770
		if ( $this->smart ) :
1771
			?>
1772
			<script>
1773
				(function(r, d, s) {
1774
					r.loadSkypeWebSdkAsync = r.loadSkypeWebSdkAsync || function(p) {
1775
							var js, sjs = d.getElementsByTagName(s)[0];
1776
							if (d.getElementById(p.id)) { return; }
1777
							js = d.createElement(s);
1778
							js.id = p.id;
1779
							js.src = p.scriptToLoad;
1780
							js.onload = p.callback
1781
							sjs.parentNode.insertBefore(js, sjs);
1782
						};
1783
					var p = {
1784
						scriptToLoad: 'https://swx.cdn.skype.com/shared/v/latest/skypewebsdk.js',
1785
						id: 'skype_web_sdk'
1786
					};
1787
					r.loadSkypeWebSdkAsync(p);
1788
				})(window, document, 'script');
1789
			</script>
1790
			<?php
1791
		else :
1792
			$this->js_dialog( $this->shortname, array( 'width' => 305, 'height' => 665 ) );
1793
		endif;
1794
	}
1795
}
1796