Completed
Push — fix/extend-amp-sharing-icons ( bbe20a...d27daa )
by
unknown
07:34
created

Sharing_Source::get_amp_display()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
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
	/**
248
	 * Get the AMP specific markup for a sharing button.
249
	 *
250
	 * @param \WP_Post $post The current post being viewed.
251
	 */
252
	public function get_amp_display( $post ) {
253
		// Only display markup if we're on a post.
254
		if ( empty( $post ) ) {
255
			return false;
256
		}
257
258
		return $this->build_amp_markup();
259
	}
260
261
	/**
262
	 * Generates and returns the markup for an AMP sharing button.
263
	 *
264
	 * @param array $attrs Custom attributes for rendering the social icon.
265
	 */
266
	protected function build_amp_markup( $attrs = array() ) {
267
		$attrs         = array_merge(
268
			array(
269
				'type'   => $this->get_id(),
270
				'height' => '32px',
271
				'width'  => '32px',
272
			),
273
			$attrs
274
		);
275
		$sharing_link = '<amp-social-share';
276
		foreach ( $attrs as $key => $value ) {
277
			$sharing_link .= sprintf( ' %s="%s"', sanitize_key( $key ), esc_attr( $value ) );
278
		}
279
		$sharing_link   .= '></amp-social-share>';
280
		return $sharing_link;
281
	}
282
283
	public function display_preview( $echo = true, $force_smart = false, $button_style = null ) {
284
		$text = '&nbsp;';
285
		$button_style = ( ! empty( $button_style ) ) ? $button_style : $this->button_style;
286
		if ( ! $this->smart && ! $force_smart ) {
287
			if ( $button_style != 'icon' ) {
288
				$text = $this->get_name();
289
			}
290
		}
291
292
		$klasses = array( 'share-' . $this->get_class(), 'sd-button' );
293
294
		if ( $button_style == 'icon' || $button_style == 'icon-text' ) {
295
			$klasses[] = 'share-icon';
296
		}
297
298
		if ( $button_style == 'icon' ) {
299
			$klasses[] = 'no-text';
300
		}
301
302
		if ( $button_style == 'text' ) {
303
			$klasses[] = 'no-icon';
304
		}
305
306
		$is_deprecated = $this->is_deprecated();
307
308
		$link = sprintf(
309
			'<a rel="nofollow" class="%s" href="javascript:void(0)" title="%s"><span>%s</span></a>',
310
			implode( ' ', $klasses ),
311
			esc_attr(
312
				$is_deprecated
313
					? 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() )
314
					: $this->get_name()
315
			),
316
			esc_html(
317
				$is_deprecated
318
					? sprintf( __( '%1$s has shut down', 'jetpack' ), $this->get_name() )
319
					: $text
320
			)
321
		);
322
323
		$smart = ( $this->smart || $force_smart ) ? 'on' : 'off';
324
		$return = "<div class='option option-smart-$smart'>$link</div>";
325
		if ( $echo ) {
326
			echo $return;
327
		}
328
329
		return $return;
330
	}
331
332
	public function get_total( $post = false ) {
333
		global $wpdb, $blog_id;
334
335
		$name = strtolower( $this->get_id() );
336
337
		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...
338
			// get total number of shares for service
339
			return (int) $wpdb->get_var( $wpdb->prepare( 'SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d AND share_service = %s', $blog_id, $name ) );
340
		}
341
342
		// get total shares for a post
343
		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 ) );
344
	}
345
346 View Code Duplication
	public function get_posts_total() {
347
		global $wpdb, $blog_id;
348
349
		$totals = array();
350
		$name	= strtolower( $this->get_id() );
351
352
		$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 ) );
353
354
		if ( ! empty( $my_data ) ) {
355
			foreach ( $my_data as $row ) {
356
				$totals[] = new Sharing_Post_Total( $row->id, $row->total );
357
			}
358
		}
359
360
		usort( $totals, array( 'Sharing_Post_Total', 'cmp' ) );
361
362
		return $totals;
363
	}
364
365
	public function process_request( $post, array $post_data ) {
366
		/**
367
		 * Fires when a post is shared via one of the sharing buttons.
368
		 *
369
		 * @module sharedaddy
370
		 *
371
		 * @since 1.1.0
372
		 *
373
		 * @param array $args Aray of information about the sharing service.
374
		 */
375
		do_action( 'sharing_bump_stats', array( 'service' => $this, 'post' => $post ) );
376
	}
377
378
	public function js_dialog( $name, $params = array() ) {
379
		if ( true !== $this->open_link_in_new ) {
380
			return;
381
		}
382
383
		$defaults = array(
384
			'menubar'	=> 1,
385
			'resizable' => 1,
386
			'width'		=> 600,
387
			'height'	=> 400,
388
		);
389
		$params = array_merge( $defaults, $params );
390
		$opts = array();
391
		foreach ( $params as $key => $val ) {
392
			$opts[] = "$key=$val";
393
		}
394
		$opts = implode( ',', $opts );
395
396
		// Add JS after sharing-js has been enqueued.
397
		wp_add_inline_script( 'sharing-js',
398
			"var windowOpen;
399
			jQuery( document.body ).on( 'click', 'a.share-$name', function() {
400
				// If there's another sharing window open, close it.
401
				if ( 'undefined' !== typeof windowOpen ) {
402
					windowOpen.close();
403
				}
404
				windowOpen = window.open( jQuery( this ).attr( 'href' ), 'wpcom$name', '$opts' );
405
				return false;
406
			});"
407
		);
408
	}
409
}
410
411
abstract class Deprecated_Sharing_Source extends Sharing_Source {
412
	public	  $button_style = 'text';
413
	public	  $smart = false;
414
	protected $open_link_in_new = false;
415
	protected $id;
416
	protected $deprecated = true;
417
418
	final public function __construct( $id, array $settings ) {
419
		$this->id = $id;
420
421
		if ( isset( $settings['button_style'] ) ) {
422
			$this->button_style = $settings['button_style'];
423
		}
424
	}
425
426
	final public function is_deprecated() {
427
		return true;
428
	}
429
430
	final public function get_share_url( $post_id ) {
431
		return get_permalink( $post_id );
432
	}
433
434
	/**
435
	 * No AMP display for deprecated sources.
436
	 *
437
	 * @param \WP_Post $post The current post being viewed.
438
	 */
439
	final public function get_amp_display( $post ) {
440
		return false;
441
	}
442
443
	final public function display_preview( $echo = true, $force_smart = false, $button_style = null ) {
444
		return parent::display_preview( $echo, false, $button_style );
445
	}
446
447
	final public function get_total( $post = false ) {
448
		return 0;
449
	}
450
451
	final public function get_posts_total() {
452
		return 0;
453
	}
454
455
	final public function process_request( $post, array $post_data ) {
456
		parent::process_request( $post, $post_data );
457
	}
458
459
	final public function get_display( $post ) {
460
		if ( current_user_can( 'manage_options' ) ) {
461
			return $this->display_deprecated( $post );
462
		}
463
464
		return '';
465
	}
466
467
	public function display_deprecated( $post ) {
468
		return $this->get_link(
469
			$this->get_share_url( $post->ID ),
470
			sprintf( __( '%1$s has shut down', 'jetpack' ), $this->get_name() ),
471
			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() )
472
		);
473
	}
474
}
475
476
abstract class Sharing_Advanced_Source extends Sharing_Source {
477
	public function has_advanced_options() {
478
		return true;
479
	}
480
481
	abstract public function display_options();
482
	abstract public function update_options( array $data );
483
	abstract public function get_options();
484
}
485
486
class Share_Email extends Sharing_Source {
487
	public $shortname = 'email';
488
	public $icon = '\f410';
489 View Code Duplication
	public function __construct( $id, array $settings ) {
490
		parent::__construct( $id, $settings );
491
492
		if ( 'official' == $this->button_style ) {
493
			$this->smart = true;
494
		} else {
495
			$this->smart = false;
496
		}
497
	}
498
499
	public function get_name() {
500
		return _x( 'Email', 'as sharing source', 'jetpack' );
501
	}
502
503
	// Default does nothing
504
	public function process_request( $post, array $post_data ) {
505
		$ajax = false;
506
		if ( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && strtolower( $_SERVER['HTTP_X_REQUESTED_WITH'] ) == 'xmlhttprequest' ) {
507
			$ajax = true;
508
		}
509
510
		$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...
511
512
		if ( isset( $post_data['source_email'] ) && is_email( $post_data['source_email'] ) ) {
513
			$source_email = $post_data['source_email'];
514
		}
515
516
		if ( isset( $post_data['target_email'] ) && is_email( $post_data['target_email'] ) ) {
517
			$target_email = $post_data['target_email'];
518
		}
519
520
		if ( isset( $post_data['source_name'] ) && strlen( $post_data['source_name'] ) < 200 ) {
521
			$source_name = $post_data['source_name'];
522
		} elseif ( isset( $post_data['source_name'] ) ) {
523
			$source_name = substr( $post_data['source_name'], 0, 200 );
524
		} else {
525
			$source_name = '';
526
		}
527
528
		// Test email
529
		$error = 1;	  // Failure in data
530
		if ( empty( $post_data['source_f_name'] ) && $source_email && $target_email && $source_name ) {
531
			/**
532
			 * Allow plugins to stop the email sharing button from running the shared message through Akismet.
533
			 *
534
			 * @module sharedaddy
535
			 *
536
			 * @since 1.1.0
537
			 *
538
			 * @param bool true Should we check if the message isn't spam?
539
			 * @param object $post Post information.
540
			 * @param array $post_data Information about the shared message.
541
			 */
542
			if ( apply_filters( 'sharing_email_check', true, $post, $post_data ) ) {
543
				$data = array(
544
					'post'           => $post,
545
					'source'         => $source_email,
546
					'target'         => $target_email,
547
					'name'           => $source_name,
548
					'sharing_source' => $this,
549
				);
550
				// todo: implement an error message when email doesn't get sent.
551
				/**
552
				 * Filter whether an email can be sent from the Email sharing button.
553
				 *
554
				 * @module sharedaddy
555
				 *
556
				 * @since 1.1.0
557
				 *
558
				 * @param array $data Array of information about the shared message.
559
				 */
560
				if ( ( $data = apply_filters( 'sharing_email_can_send', $data ) ) !== false ) {
561
					// Record stats
562
					parent::process_request( $data['post'], $post_data );
563
564
					/**
565
					 * Fires when an email is sent via the Email sharing button.
566
					 *
567
					 * @module sharedaddy
568
					 *
569
					 * @since 1.1.0
570
					 *
571
					 * @param array $data Array of information about the shared message.
572
					 */
573
					do_action( 'sharing_email_send_post', $data );
574
				}
575
576
				// Return a positive regardless of whether the user is subscribed or not
577
				if ( $ajax ) {
578
?>
579
<div class="response">
580
	<div class="response-title"><?php _e( 'This post has been shared!', 'jetpack' ); ?></div>
581
	<div class="response-sub"><?php printf( __( 'You have shared this post with %s', 'jetpack' ), esc_html( $target_email ) ); ?></div>
582
	<div class="response-close"><a href="#" class="sharing_cancel"><?php _e( 'Close', 'jetpack' ); ?></a></div>
583
</div>
584
<?php
585
				} else {
586
					wp_safe_redirect( get_permalink( $post->ID ) . '?shared=email' );
587
				}
588
589
				die();
590
			} else {
591
				$error = 2;	  // Email check failed
592
			}
593
		}
594
595
		if ( $ajax ) {
596
			echo $error;
597
		} else {
598
			wp_safe_redirect( get_permalink( $post->ID ) . '?shared=email&msg=fail' );
599
		}
600
601
		die();
602
	}
603
604
	public function get_display( $post ) {
605
		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' );
606
	}
607
608
	/**
609
	 * No AMP display for email.
610
	 *
611
	 * @param \WP_Post $post The current post being viewed.
612
	 */
613
	public function get_amp_display( $post ) {
614
		return false;
615
	}
616
617
	/**
618
	 * Outputs the hidden email dialog
619
	 */
620
	public function display_footer() {
621
		global $current_user;
622
623
		$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...
624
?>
625
	<div id="sharing_email" style="display: none;">
626
		<form action="<?php echo esc_url( $_SERVER['REQUEST_URI'] ); ?>" method="post">
627
			<label for="target_email"><?php _e( 'Send to Email Address', 'jetpack' ) ?></label>
628
			<input type="email" name="target_email" id="target_email" value="" />
629
630
			<?php if ( is_user_logged_in() ) : ?>
631
				<input type="hidden" name="source_name" value="<?php echo esc_attr( $current_user->display_name ); ?>" />
632
				<input type="hidden" name="source_email" value="<?php echo esc_attr( $current_user->user_email ); ?>" />
633
			<?php else : ?>
634
635
				<label for="source_name"><?php _e( 'Your Name', 'jetpack' ) ?></label>
636
				<input type="text" name="source_name" id="source_name" value="" />
637
638
				<label for="source_email"><?php _e( 'Your Email Address', 'jetpack' ) ?></label>
639
				<input type="email" name="source_email" id="source_email" value="" />
640
641
			<?php endif; ?>
642
			<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' ); ?>" />
643
			<?php
644
				/**
645
				 * Fires when the Email sharing dialog is loaded.
646
				 *
647
				 * @module sharedaddy
648
				 *
649
				 * @since 1.1.0
650
				 *
651
				 * @param string jetpack Eail sharing source.
652
				 */
653
				do_action( 'sharing_email_dialog', 'jetpack' );
654
			?>
655
656
			<img style="float: right; display: none" class="loading" src="<?php
657
			/** This filter is documented in modules/stats.php */
658
			echo apply_filters( 'jetpack_static_url', plugin_dir_url( __FILE__ ) . 'images/loading.gif' ); ?>" alt="loading" width="16" height="16" />
659
			<input type="submit" value="<?php esc_attr_e( 'Send Email', 'jetpack' ); ?>" class="sharing_send" />
660
			<a rel="nofollow" href="#cancel" class="sharing_cancel" role="button"><?php _e( 'Cancel', 'jetpack' ); ?></a>
661
662
			<div class="errors errors-1" style="display: none;">
663
				<?php _e( 'Post was not sent - check your email addresses!', 'jetpack' ); ?>
664
			</div>
665
666
			<div class="errors errors-2" style="display: none;">
667
				<?php _e( 'Email check failed, please try again', 'jetpack' ); ?>
668
			</div>
669
670
			<div class="errors errors-3" style="display: none;">
671
				<?php _e( 'Sorry, your blog cannot share posts by email.', 'jetpack' ); ?>
672
			</div>
673
		</form>
674
	</div>
675
<?php
676
	}
677
}
678
679
class Share_Twitter extends Sharing_Source {
680
	public $shortname = 'twitter';
681
	public $icon = '\f202';
682
	// 'https://dev.twitter.com/rest/reference/get/help/configuration' ( 2015/02/06 ) short_url_length is 22, short_url_length_https is 23
683
	public $short_url_length = 24;
684
685 View Code Duplication
	public function __construct( $id, array $settings ) {
686
		parent::__construct( $id, $settings );
687
688
		if ( 'official' == $this->button_style ) {
689
			$this->smart = true;
690
		} else {
691
			$this->smart = false;
692
		}
693
	}
694
695
	public function get_name() {
696
		return __( 'Twitter', 'jetpack' );
697
	}
698
699
	/**
700
	 * Determine the Twitter 'via' value for a post.
701
	 *
702
	 * @param  WP_Post|int $post Post object or post ID.
703
	 * @return string Twitter handle without the preceding @.
704
	 **/
705
	public static function sharing_twitter_via( $post ) {
706
		$post = get_post( $post );
707
		/**
708
		 * Allow third-party plugins to customize the Twitter username used as "twitter:site" Twitter Card Meta Tag.
709
		 *
710
		 * @module sharedaddy
711
		 *
712
		 * @since 3.0.0
713
		 *
714
		 * @param string $string Twitter Username.
715
		 * @param array $args Array of Open Graph Meta Tags and Twitter Cards tags.
716
		 */
717
		$twitter_site_tag_value = apply_filters(
718
			'jetpack_twitter_cards_site_tag',
719
			'',
720
			/** This action is documented in modules/sharedaddy/sharing-sources.php */
721
			array( 'twitter:creator' => apply_filters( 'jetpack_sharing_twitter_via', '', $post->ID ) )
722
		);
723
724
		/*
725
		 * Hack to remove the unwanted behavior of adding 'via @jetpack' which
726
		 * was introduced with the adding of the Twitter cards.
727
		 * This should be a temporary solution until a better method is setup.
728
		 */
729
		if ( 'jetpack' == $twitter_site_tag_value ) {
730
			$twitter_site_tag_value = '';
731
		}
732
733
		/**
734
		 * Filters the Twitter username used as "via" in the Twitter sharing button.
735
		 *
736
		 * @module sharedaddy
737
		 *
738
		 * @since 1.7.0
739
		 *
740
		 * @param string $twitter_site_tag_value Twitter Username.
741
		 * @param int $post->ID Post ID.
742
		 */
743
		$twitter_site_tag_value = apply_filters( 'jetpack_sharing_twitter_via', $twitter_site_tag_value, $post->ID );
744
745
		// Strip out anything other than a letter, number, or underscore.
746
		// This will prevent the inadvertent inclusion of an extra @, as well as normalizing the handle.
747
		return preg_replace( '/[^\da-z_]+/i', '', $twitter_site_tag_value );
748
	}
749
750
	/**
751
	 * Determine the 'related' Twitter accounts for a post.
752
	 *
753
	 * @param  WP_Post|int $post Post object or post ID.
754
	 * @return string Comma-separated list of Twitter handles.
755
	 **/
756
	public static function get_related_accounts( $post ) {
757
		$post = get_post( $post );
758
		/**
759
		 * Filter the list of related Twitter accounts added to the Twitter sharing button.
760
		 *
761
		 * @module sharedaddy
762
		 *
763
		 * @since 1.7.0
764
		 *
765
		 * @param array $args Array of Twitter usernames. Format is 'username' => 'Optional description'
766
		 * @param int $post->ID Post ID.
767
		 */
768
		$related_accounts = apply_filters( 'jetpack_sharing_twitter_related', array(), $post->ID );
769
770
		// Example related string: account1,account2:Account 2 description,account3
771
		$related = array();
772
773
		foreach ( $related_accounts as $related_account_username => $related_account_description ) {
774
			// Join the description onto the end of the username
775
			if ( $related_account_description ) {
776
				$related_account_username .= ':' . $related_account_description;
777
			}
778
779
			$related[] = $related_account_username;
780
		}
781
782
		return implode( ',', $related );
783
	}
784
785
	public function get_display( $post ) {
786
		$via = $this->sharing_twitter_via( $post );
787
788
		if ( $via ) {
789
			$via = 'data-via="' . esc_attr( $via ) . '"';
790
		} else {
791
			$via = '';
792
		}
793
794
		$related = $this->get_related_accounts( $post );
795
		if ( ! empty( $related ) && $related !== $via ) {
796
			$related = 'data-related="' . esc_attr( $related ) . '"';
797
		} else {
798
			$related = '';
799
		}
800
801
		if ( $this->smart ) {
802
			$share_url = $this->get_share_url( $post->ID );
803
			$post_title = $this->get_share_title( $post->ID );
804
			return sprintf(
805
				'<a href="https://twitter.com/share" class="twitter-share-button" data-url="%1$s" data-text="%2$s" %3$s %4$s>Tweet</a>',
806
				esc_url( $share_url ),
807
				esc_attr( $post_title ),
808
				$via,
809
				$related
810
			);
811
		} else {
812
			if (
813
				/**
814
				 * Allow plugins to disable sharing counts for specific sharing services.
815
				 *
816
				 * @module sharedaddy
817
				 *
818
				 * @since 3.0.0
819
				 *
820
				 * @param bool true Should sharing counts be enabled for this specific service. Default to true.
821
				 * @param int $post->ID Post ID.
822
				 * @param string $str Sharing service name.
823
				 */
824
				apply_filters( 'jetpack_register_post_for_share_counts', true, $post->ID, 'twitter' )
825
			) {
826
				sharing_register_post_for_share_counts( $post->ID );
827
			}
828
			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 );
829
		}
830
	}
831
832
	public function process_request( $post, array $post_data ) {
833
		$post_title = $this->get_share_title( $post->ID );
834
		$post_link = $this->get_share_url( $post->ID );
835
836
		if ( function_exists( 'mb_stripos' ) ) {
837
			$strlen = 'mb_strlen';
838
			$substr = 'mb_substr';
839
		} else {
840
			$strlen = 'strlen';
841
			$substr = 'substr';
842
		}
843
844
		$via = $this->sharing_twitter_via( $post );
845
		$related = $this->get_related_accounts( $post );
846
		if ( $via ) {
847
			$sig = " via @$via";
848
			if ( $related === $via ) {
849
				$related = false;
850
			}
851
		} else {
852
			$via = false;
853
			$sig = '';
854
		}
855
856
		$suffix_length = $this->short_url_length + $strlen( $sig );
857
		// $sig is handled by twitter in their 'via' argument.
858
		// $post_link is handled by twitter in their 'url' argument.
859
		if ( 280 < $strlen( $post_title ) + $suffix_length ) {
860
			// The -1 is for "\xE2\x80\xA6", a UTF-8 ellipsis.
861
			$text = $substr( $post_title, 0, 280 - $suffix_length - 1 ) . "\xE2\x80\xA6";
862
		} else {
863
			$text = $post_title;
864
		}
865
866
		// Record stats
867
		parent::process_request( $post, $post_data );
868
869
		$url = $post_link;
870
		$twitter_url = add_query_arg(
871
			rawurlencode_deep( array_filter( compact( 'via', 'related', 'text', 'url' ) ) ),
872
			'https://twitter.com/intent/tweet'
873
		);
874
875
		// Redirect to Twitter
876
		wp_redirect( $twitter_url );
877
		die();
878
	}
879
880
	public function has_custom_button_style() {
881
		return $this->smart;
882
	}
883
884
	public function display_footer() {
885
		if ( $this->smart ) {
886
			?>
887
			<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>
888
			<?php
889
		} else {
890
			$this->js_dialog( $this->shortname, array( 'height' => 350 ) );
891
		}
892
	}
893
}
894
895
896
class Share_Reddit extends Sharing_Source {
897
	public $shortname = 'reddit';
898
	public $icon = '\f222';
899 View Code Duplication
	public function __construct( $id, array $settings ) {
900
		parent::__construct( $id, $settings );
901
902
		if ( 'official' == $this->button_style ) {
903
			$this->smart = true;
904
		} else {
905
			$this->smart = false;
906
		}
907
	}
908
909
	public function get_name() {
910
		return __( 'Reddit', 'jetpack' );
911
	}
912
913
	public function get_display( $post ) {
914
		if ( $this->smart ) {
915
			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>';
916 View Code Duplication
		} else {
917
			return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Reddit', 'share to', 'jetpack' ), __( 'Click to share on Reddit', 'jetpack' ), 'share=reddit' );
918
		}
919
	}
920
921
	/**
922
	 * AMP display for Reddit.
923
	 *
924
	 * @param \WP_Post $post The current post being viewed.
925
	 */
926 View Code Duplication
	public function get_amp_display( $post ) {
927
		$attrs = array(
928
			'data-share-endpoint' => esc_url_raw( 'https://reddit.com/submit?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&title=' . rawurlencode( $this->get_share_title( $post->ID ) ) ),
929
		);
930
931
		return $this->build_amp_markup( $attrs );
932
	}
933
934 View Code Duplication
	public function process_request( $post, array $post_data ) {
935
		$reddit_url = $this->http() . '://reddit.com/submit?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&title=' . rawurlencode( $this->get_share_title( $post->ID ) );
936
937
		// Record stats
938
		parent::process_request( $post, $post_data );
939
940
		// Redirect to Reddit
941
		wp_redirect( $reddit_url );
942
		die();
943
	}
944
}
945
946
class Share_LinkedIn extends Sharing_Source {
947
	public $shortname = 'linkedin';
948
	public $icon = '\f207';
949 View Code Duplication
	public function __construct( $id, array $settings ) {
950
		parent::__construct( $id, $settings );
951
952
		if ( 'official' == $this->button_style ) {
953
			$this->smart = true;
954
		} else {
955
			$this->smart = false;
956
		}
957
	}
958
959
	public function get_name() {
960
		return __( 'LinkedIn', 'jetpack' );
961
	}
962
963
	public function has_custom_button_style() {
964
		return $this->smart;
965
	}
966
967
	public function get_display( $post ) {
968
		$display = '';
969
970
		if ( $this->smart ) {
971
			$share_url = $this->get_share_url( $post->ID );
972
			$display .= sprintf( '<div class="linkedin_button"><script type="in/share" data-url="%s" data-counter="right"></script></div>', esc_url( $share_url ) );
973 View Code Duplication
		} else {
974
			$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 );
975
		}
976
977
		/** This filter is already documented in modules/sharedaddy/sharing-sources.php */
978
		if ( apply_filters( 'jetpack_register_post_for_share_counts', true, $post->ID, 'linkedin' ) ) {
979
			sharing_register_post_for_share_counts( $post->ID );
980
		}
981
982
		return $display;
983
	}
984
985 View Code Duplication
	public function process_request( $post, array $post_data ) {
986
987
		$post_link = $this->get_share_url( $post->ID );
988
989
		// Using the same URL as the official button, which is *not* LinkedIn's documented sharing link
990
		// https://www.linkedin.com/cws/share?url={url}&token=&isFramed=false
991
		$linkedin_url = add_query_arg( array(
992
			'url' => rawurlencode( $post_link ),
993
		), 'https://www.linkedin.com/cws/share?token=&isFramed=false' );
994
995
		// Record stats
996
		parent::process_request( $post, $post_data );
997
998
		// Redirect to LinkedIn
999
		wp_redirect( $linkedin_url );
1000
		die();
1001
	}
1002
1003 View Code Duplication
	public function display_footer() {
1004
		if ( ! $this->smart ) {
1005
			$this->js_dialog( $this->shortname, array( 'width' => 580, 'height' => 450 ) );
1006
		} else {
1007
			?><script type="text/javascript">
1008
			jQuery( document ).ready( function() {
1009
				jQuery.getScript( 'https://platform.linkedin.com/in.js?async=true', function success() {
1010
					IN.init();
1011
				});
1012
			});
1013
			jQuery( document.body ).on( 'post-load', function() {
1014
				if ( typeof IN != 'undefined' )
1015
					IN.parse();
1016
			});
1017
			</script><?php
1018
		}
1019
	}
1020
}
1021
1022
class Share_Facebook extends Sharing_Source {
1023
	public $shortname = 'facebook';
1024
	public $icon = '\f204';
1025
	private $share_type = 'default';
1026
1027 View Code Duplication
	public function __construct( $id, array $settings ) {
1028
		parent::__construct( $id, $settings );
1029
1030
		if ( isset( $settings['share_type'] ) ) {
1031
			$this->share_type = $settings['share_type'];
1032
		}
1033
1034
		if ( 'official' == $this->button_style ) {
1035
			$this->smart = true;
1036
		} else {
1037
			$this->smart = false;
1038
		}
1039
	}
1040
1041
	public function get_name() {
1042
		return __( 'Facebook', 'jetpack' );
1043
	}
1044
1045
	public function display_header() {
1046
	}
1047
1048 View Code Duplication
	function guess_locale_from_lang( $lang ) {
1049
		if ( 'en' == $lang || 'en_US' == $lang || ! $lang ) {
1050
			return 'en_US';
1051
		}
1052
1053
		if ( ! class_exists( 'GP_Locales' ) ) {
1054
			if ( ! defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) || ! file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) {
1055
				return false;
1056
			}
1057
1058
			require JETPACK__GLOTPRESS_LOCALES_PATH;
1059
		}
1060
1061
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
1062
			// WP.com: get_locale() returns 'it'
1063
			$locale = GP_Locales::by_slug( $lang );
1064
		} else {
1065
			// Jetpack: get_locale() returns 'it_IT';
1066
			$locale = GP_Locales::by_field( 'wp_locale', $lang );
1067
		}
1068
1069
		if ( ! $locale ) {
1070
			return false;
1071
		}
1072
1073
		if ( empty( $locale->facebook_locale ) ) {
1074
			if ( empty( $locale->wp_locale ) ) {
1075
				return false;
1076
			} else {
1077
				// Facebook SDK is smart enough to fall back to en_US if a
1078
				// locale isn't supported. Since supported Facebook locales
1079
				// can fall out of sync, we'll attempt to use the known
1080
				// wp_locale value and rely on said fallback.
1081
				return $locale->wp_locale;
1082
			}
1083
		}
1084
1085
		return $locale->facebook_locale;
1086
	}
1087
1088
	public function get_display( $post ) {
1089
		if ( $this->smart ) {
1090
			$share_url = $this->get_share_url( $post->ID );
1091
			$fb_share_html = '<div class="fb-share-button" data-href="' . esc_attr( $share_url ) . '" data-layout="button_count"></div>';
1092
			/**
1093
			 * Filter the output of the Facebook Sharing button.
1094
			 *
1095
			 * @module sharedaddy
1096
			 *
1097
			 * @since 3.6.0
1098
			 *
1099
			 * @param string $fb_share_html Facebook Sharing button HTML.
1100
			 * @param string $share_url URL of the post to share.
1101
			 */
1102
			return apply_filters( 'jetpack_sharing_facebook_official_button_output', $fb_share_html, $share_url );
1103
		}
1104
1105
		/** This filter is already documented in modules/sharedaddy/sharing-sources.php */
1106
		if ( apply_filters( 'jetpack_register_post_for_share_counts', true, $post->ID, 'facebook' ) ) {
1107
			sharing_register_post_for_share_counts( $post->ID );
1108
		}
1109
		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 );
1110
	}
1111
1112
	/**
1113
	 * AMP display for Facebook.
1114
	 *
1115
	 * @param \WP_Post $post The current post being viewed.
1116
	 */
1117
	public function get_amp_display( $post ) {
1118
		$attrs = array(
1119
			/** This filter is documented in modules/sharedaddy/sharing-sources.php */
1120
			'data-param-app_id' => apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' ),
1121
		);
1122
1123
		return $this->build_amp_markup( $attrs );
1124
	}
1125
1126 View Code Duplication
	public function process_request( $post, array $post_data ) {
1127
		$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 ) );
1128
1129
		// Record stats
1130
		parent::process_request( $post, $post_data );
1131
1132
		// Redirect to Facebook
1133
		wp_redirect( $fb_url );
1134
		die();
1135
	}
1136
1137
	public function display_footer() {
1138
		$this->js_dialog( $this->shortname );
1139
		if ( $this->smart ) {
1140
			$locale = $this->guess_locale_from_lang( get_locale() );
1141
			if ( ! $locale ) {
1142
				$locale = 'en_US';
1143
			}
1144
			/**
1145
			 * Filter the App ID used in the official Facebook Share button.
1146
			 *
1147
			 * @since 3.8.0
1148
			 *
1149
			 * @param int $fb_app_id Facebook App ID. Default to 249643311490 (WordPress.com's App ID).
1150
			 */
1151
			$fb_app_id = apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' );
1152
			if ( is_numeric( $fb_app_id ) ) {
1153
				$fb_app_id = '&appId=' . $fb_app_id;
1154
			} else {
1155
				$fb_app_id = '';
1156
			}
1157
			?><div id="fb-root"></div>
1158
			<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>
1159
			<script>
1160
			jQuery( document.body ).on( 'post-load', function() {
1161
				if ( 'undefined' !== typeof FB ) {
1162
					FB.XFBML.parse();
1163
				}
1164
			} );
1165
			</script>
1166
			<?php
1167
		}
1168
	}
1169
}
1170
1171
class Share_Print extends Sharing_Source {
1172
	public $shortname = 'print';
1173
	public $icon = '\f469';
1174 View Code Duplication
	public function __construct( $id, array $settings ) {
1175
		parent::__construct( $id, $settings );
1176
1177
		if ( 'official' == $this->button_style ) {
1178
			$this->smart = true;
1179
		} else {
1180
			$this->smart = false;
1181
		}
1182
	}
1183
1184
	public function get_name() {
1185
		return __( 'Print', 'jetpack' );
1186
	}
1187
1188
	public function get_display( $post ) {
1189
		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' ) );
1190
	}
1191
1192
	/**
1193
	 * AMP display for Print.
1194
	 *
1195
	 * @param \WP_Post $post The current post being viewed.
1196
	 */
1197
	public function get_amp_display( $post ) {
1198
		$attrs = array(
1199
			'on' => 'tap:AMP.print',
1200
		);
1201
1202
		return $this->build_amp_markup( $attrs );
1203
	}
1204
}
1205
1206
class Share_PressThis extends Sharing_Source {
1207
	public $shortname = 'pressthis';
1208
	public $icon = '\f205';
1209 View Code Duplication
	public function __construct( $id, array $settings ) {
1210
		parent::__construct( $id, $settings );
1211
1212
		if ( 'official' == $this->button_style ) {
1213
			$this->smart = true;
1214
		} else {
1215
			$this->smart = false;
1216
		}
1217
	}
1218
1219
	public function get_name() {
1220
		return __( 'Press This', 'jetpack' );
1221
	}
1222
1223
	public function process_request( $post, array $post_data ) {
1224
		global $current_user;
1225
1226
		$primary_blog = (int) get_user_meta( $current_user->ID, 'primary_blog', true );
1227
		if ( $primary_blog ) {
1228
			$primary_blog_details = get_blog_details( $primary_blog );
1229
		} else {
1230
			$primary_blog_details = false;
1231
		}
1232
1233
		if ( $primary_blog_details ) {
1234
			$blogs = array( $primary_blog_details );
1235
		} elseif ( function_exists( 'get_active_blogs_for_user' ) ) {
1236
			$blogs = get_active_blogs_for_user();
1237
			if ( empty( $blogs ) ) {
1238
				$blogs = get_blogs_of_user( $current_user->ID );
1239
			}
1240
		} else {
1241
			$blogs = get_blogs_of_user( $current_user->ID );
1242
		}
1243
1244
		if ( empty( $blogs ) ) {
1245
			wp_safe_redirect( get_permalink( $post->ID ) );
1246
			die();
1247
		}
1248
1249
		$blog = current( $blogs );
1250
1251
		$args = array(
1252
			'u' => rawurlencode( $this->get_share_url( $post->ID ) ),
1253
			);
1254
1255
		$args[ 'url-scan-submit' ] = 'Scan';
1256
		$args[ '_wpnonce' ]        = wp_create_nonce( 'scan-site' );
1257
1258
		$url = $blog->siteurl . '/wp-admin/press-this.php';
1259
		$url = add_query_arg( $args, $url );
1260
1261
		// Record stats
1262
		parent::process_request( $post, $post_data );
1263
1264
		// Redirect to Press This
1265
		wp_redirect( $url );
1266
		die();
1267
	}
1268
1269
	public function get_display( $post ) {
1270
		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' );
1271
	}
1272
1273
	/**
1274
	 * No AMP display for PressThis.
1275
	 *
1276
	 * @param \WP_Post $post The current post being viewed.
1277
	 */
1278
	public function get_amp_display( $post ) {
1279
		return false;
1280
	}
1281
}
1282
1283
class Share_Custom extends Sharing_Advanced_Source {
1284
	private $name;
1285
	private $icon;
1286
	private $url;
1287
	public $smart = true;
1288
	public $shortname;
1289
1290
	public function get_class() {
1291
		return 'custom share-custom-' . sanitize_html_class( strtolower( $this->name ) );
1292
	}
1293
1294
	public function __construct( $id, array $settings ) {
1295
		parent::__construct( $id, $settings );
1296
1297
		$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...
1298
1299
		if ( isset( $settings['name'] ) ) {
1300
			$this->name = $settings['name'];
1301
			$this->shortname = preg_replace( '/[^a-z0-9]*/', '', $settings['name'] );
1302
		}
1303
1304
		if ( isset( $settings['icon'] ) ) {
1305
			$this->icon = $settings['icon'];
1306
1307
			$new_icon = esc_url_raw( wp_specialchars_decode( $this->icon, ENT_QUOTES ) );
1308
			$i = 0;
1309
			while ( $new_icon != $this->icon ) {
1310
				if ( $i > 5 ) {
1311
					$this->icon = false;
1312
					break;
1313
				} else {
1314
					$this->icon = $new_icon;
1315
					$new_icon = esc_url_raw( wp_specialchars_decode( $this->icon, ENT_QUOTES ) );
1316
				}
1317
				$i++;
1318
			}
1319
		}
1320
1321
		if ( isset( $settings['url'] ) ) {
1322
			$this->url = $settings['url'];
1323
		}
1324
	}
1325
1326
	public function get_name() {
1327
		return $this->name;
1328
	}
1329
1330
	public function get_display( $post ) {
1331
		$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 );
1332
		return str_replace( '<span>', '<span style="' . esc_attr( 'background-image:url("' . addcslashes( esc_url_raw( $this->icon ), '"' ) . '");' ) . '">', $str );
1333
	}
1334
1335
	/**
1336
	 * No AMP display for custom elements.
1337
	 *
1338
	 * @param \WP_Post $post The current post being viewed.
1339
	 */
1340
	public function get_amp_display( $post ) {
1341
		return false;
1342
	}
1343
1344
	public function process_request( $post, array $post_data ) {
1345
		$url = str_replace( '&amp;', '&', $this->url );
1346
		$url = str_replace( '%post_id%', rawurlencode( $post->ID ), $url );
1347
		$url = str_replace( '%post_url%', rawurlencode( $this->get_share_url( $post->ID ) ), $url );
1348
		$url = str_replace( '%post_full_url%', rawurlencode( get_permalink( $post->ID ) ), $url );
1349
		$url = str_replace( '%post_title%', rawurlencode( $this->get_share_title( $post->ID ) ), $url );
1350
		$url = str_replace( '%home_url%', rawurlencode( home_url() ), $url );
1351
		$url = str_replace( '%post_slug%', rawurlencode( $post->post_name ), $url );
1352
1353
		if ( strpos( $url, '%post_tags%' ) !== false ) {
1354
			$tags	= get_the_tags( $post->ID );
1355
			$tagged = '';
1356
1357
			if ( $tags ) {
1358
				$tagged_raw = array();
1359
				foreach ( $tags as $tag ) {
1360
					$tagged_raw[] = rawurlencode( $tag->name );
1361
				}
1362
1363
				$tagged = implode( ',', $tagged_raw );
1364
			}
1365
1366
			$url = str_replace( '%post_tags%', $tagged, $url );
1367
		}
1368
1369
		if ( strpos( $url, '%post_excerpt%' ) !== false ) {
1370
			$url_excerpt = $post->post_excerpt;
1371
			if ( empty( $url_excerpt ) ) {
1372
				$url_excerpt = $post->post_content;
1373
			}
1374
1375
			$url_excerpt = strip_tags( strip_shortcodes( $url_excerpt ) );
1376
			$url_excerpt = wp_html_excerpt( $url_excerpt, 100 );
1377
			$url_excerpt = rtrim( preg_replace( '/[^ .]*$/', '', $url_excerpt ) );
1378
			$url = str_replace( '%post_excerpt%', rawurlencode( $url_excerpt ), $url );
1379
		}
1380
1381
		// Record stats
1382
		parent::process_request( $post, $post_data );
1383
1384
		// Redirect
1385
		wp_redirect( $url );
1386
		die();
1387
	}
1388
1389
	public function display_options() {
1390
?>
1391
<div class="input">
1392
	<table class="form-table">
1393
		<tbody>
1394
			<tr>
1395
				<th scope="row"><?php _e( 'Label', 'jetpack' ); ?></th>
1396
				<td><input type="text" name="name" value="<?php echo esc_attr( $this->name ); ?>" /></td>
1397
			</tr>
1398
1399
			<tr>
1400
				<th scope="row"><?php _e( 'URL', 'jetpack' ); ?></th>
1401
				<td><input type="text" name="url" value="<?php echo esc_attr( $this->url ); ?>" /></td>
1402
			</tr>
1403
1404
			<tr>
1405
				<th scope="row"><?php _e( 'Icon', 'jetpack' ); ?></th>
1406
				<td><input type="text" name="icon" value="<?php echo esc_attr( $this->icon ); ?>" /></td>
1407
			</tr>
1408
1409
			<tr>
1410
				<th scope="row"></th>
1411
				<td>
1412
					<input class="button-secondary" type="submit" value="<?php esc_attr_e( 'Save', 'jetpack' ); ?>" />
1413
					<a href="#" class="remove"><small><?php _e( 'Remove Service', 'jetpack' ); ?></small></a>
1414
				</td>
1415
			</tr>
1416
		</tbody>
1417
	</table>
1418
</div>
1419
<?php
1420
	}
1421
1422
	public function update_options( array $data ) {
1423
		$name  = trim( wp_html_excerpt( wp_kses( stripslashes( $data['name'] ), array() ), 30 ) );
1424
		$url   = trim( esc_url_raw( $data['url'] ) );
1425
		$icon  = trim( esc_url_raw( $data['icon'] ) );
1426
1427
		if ( $name ) {
1428
			$this->name = $name;
1429
		}
1430
1431
		if ( $url ) {
1432
			$this->url	= $url;
1433
		}
1434
1435
		if ( $icon ) {
1436
			$this->icon = $icon;
1437
		}
1438
	}
1439
1440
	public function get_options() {
1441
		return array(
1442
			'name' => $this->name,
1443
			'icon' => $this->icon,
1444
			'url'  => $this->url,
1445
		);
1446
	}
1447
1448
	public function display_preview( $echo = true, $force_smart = false, $button_style = null ) {
1449
		$opts = $this->get_options();
1450
1451
		$text = '&nbsp;';
1452
		if ( ! $this->smart ) {
1453
			if ( $this->button_style != 'icon' ) {
1454
				$text = $this->get_name();
1455
			}
1456
		}
1457
1458
		$klasses = array( 'share-' . $this->shortname );
1459
1460
		if ( $this->button_style == 'icon' || $this->button_style == 'icon-text' ) {
1461
			$klasses[] = 'share-icon';
1462
		}
1463
1464
		if ( $this->button_style == 'icon' ) {
1465
			$text = '';
1466
			$klasses[] = 'no-text';
1467
		}
1468
1469
		if ( $this->button_style == 'text' ) {
1470
			$klasses[] = 'no-icon';
1471
		}
1472
1473
		$link = sprintf(
1474
			'<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>',
1475
			implode( ' ', $klasses ),
1476
			$this->get_name(),
1477
			addcslashes( esc_url_raw( $opts['icon'] ), '"' ),
1478
			$text
1479
		);
1480
		?>
1481
		<div class="option option-smart-off">
1482
		<?php echo $link ; ?>
1483
		</div><?php
1484
	}
1485
}
1486
1487
class Share_Tumblr extends Sharing_Source {
1488
	public $shortname = 'tumblr';
1489
	public $icon = '\f214';
1490 View Code Duplication
	public function __construct( $id, array $settings ) {
1491
		parent::__construct( $id, $settings );
1492
		if ( 'official' == $this->button_style ) {
1493
			$this->smart = true;
1494
		} else {
1495
			$this->smart = false;
1496
		}
1497
	}
1498
1499
	public function get_name() {
1500
		return __( 'Tumblr', 'jetpack' );
1501
	}
1502
1503
	public function get_display( $post ) {
1504
		if ( $this->smart ) {
1505
			$target = '';
1506
			if ( true == $this->open_link_in_new ) {
1507
				$target = '_blank';
1508
			}
1509
1510
			/**
1511
			 * If we are looking at a single post, let Tumblr figure out the post type (text, photo, link, quote, chat, or video)
1512
			 * based on the content available on the page.
1513
			 * If we are not looking at a single post, content from other posts can appear on the page and Tumblr will pick that up.
1514
			 * 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.
1515
			 */
1516
			if ( ! is_single() ) {
1517
				$posttype = 'data-posttype="link"';
1518
			} else {
1519
				$posttype = '';
1520
			}
1521
1522
			// Documentation: https://www.tumblr.com/docs/en/share_button
1523
			return sprintf(
1524
				'<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>',
1525
				$target,
1526
				'https://www.tumblr.com/share',
1527
				$this->get_share_title( $post->ID ),
1528
				$this->get_share_url( $post->ID ),
1529
				__( 'Share on Tumblr', 'jetpack' ),
1530
				$posttype
1531
			);
1532 View Code Duplication
		 } else {
1533
			return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Tumblr', 'share to', 'jetpack' ), __( 'Click to share on Tumblr', 'jetpack' ), 'share=tumblr' );
1534
		}
1535
	}
1536
1537 View Code Duplication
	public function process_request( $post, array $post_data ) {
1538
		// Record stats
1539
		parent::process_request( $post, $post_data );
1540
1541
		// Redirect to Tumblr's sharing endpoint (a la their bookmarklet)
1542
		$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=';
1543
		wp_redirect( $url );
1544
		die();
1545
	}
1546
1547 View Code Duplication
	public function display_footer() {
1548
		if ( $this->smart ) {
1549
			?><script id="tumblr-js" type="text/javascript" src="https://assets.tumblr.com/share-button.js"></script><?php
1550
		} else {
1551
			$this->js_dialog( $this->shortname, array( 'width' => 450, 'height' => 450 ) );
1552
		}
1553
	}
1554
}
1555
1556
class Share_Pinterest extends Sharing_Source {
1557
	public $shortname = 'pinterest';
1558
	public $icon = '\f209';
1559
1560 View Code Duplication
	public function __construct( $id, array $settings ) {
1561
		parent::__construct( $id, $settings );
1562
		if ( 'official' == $this->button_style ) {
1563
			$this->smart = true;
1564
		} else {
1565
			$this->smart = false;
1566
		}
1567
	}
1568
1569
	public function get_name() {
1570
		return __( 'Pinterest', 'jetpack' );
1571
	}
1572
1573
	public function get_image( $post ) {
1574
		if ( class_exists( 'Jetpack_PostImages' ) ) {
1575
			$image = Jetpack_PostImages::get_image( $post->ID, array( 'fallback_to_avatars' => true ) );
1576
			if ( ! empty( $image ) ) {
1577
				return $image['src'];
1578
			}
1579
		}
1580
1581
		/**
1582
		 * Filters the default image used by the Pinterest Pin It share button.
1583
		 *
1584
		 * @module sharedaddy
1585
		 *
1586
		 * @since 3.6.0
1587
		 *
1588
		 * @param string $url Default image URL.
1589
		 */
1590
		return apply_filters( 'jetpack_sharing_pinterest_default_image', 'https://s0.wp.com/i/blank.jpg' );
1591
	}
1592
1593
	public function get_external_url( $post ) {
1594
		$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 );
1595
1596
		/**
1597
		 * Filters the Pinterest share URL used in sharing button output.
1598
		 *
1599
		 * @module sharedaddy
1600
		 *
1601
		 * @since 3.6.0
1602
		 *
1603
		 * @param string $url Pinterest share URL.
1604
		 */
1605
		return apply_filters( 'jetpack_sharing_pinterest_share_url', $url );
1606
	}
1607
1608
	public function get_widget_type() {
1609
		/**
1610
		 * Filters the Pinterest widget type.
1611
		 *
1612
		 * @see https://business.pinterest.com/en/widget-builder
1613
		 *
1614
		 * @module sharedaddy
1615
		 *
1616
		 * @since 3.6.0
1617
		 *
1618
		 * @param string $type Pinterest widget type. Default of 'buttonPin' for single-image selection. 'buttonBookmark' for multi-image modal.
1619
		 */
1620
		return apply_filters( 'jetpack_sharing_pinterest_widget_type', 'buttonPin' );
1621
	}
1622
1623
	public function get_display( $post ) {
1624
		$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...
1625
1626
		if ( $this->smart ) {
1627
			$display = sprintf(
1628
				'<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>',
1629
				esc_url( $this->get_external_url( $post ) ),
1630
				esc_attr( $this->get_widget_type() )
1631
			);
1632 View Code Duplication
		} else {
1633
			$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 );
1634
		}
1635
1636
		/** This filter is already documented in modules/sharedaddy/sharing-sources.php */
1637
		if ( apply_filters( 'jetpack_register_post_for_share_counts', true, $post->ID, 'linkedin' ) ) {
1638
			sharing_register_post_for_share_counts( $post->ID );
1639
		}
1640
1641
		return $display;
1642
	}
1643
1644
	public function process_request( $post, array $post_data ) {
1645
		// Record stats
1646
		parent::process_request( $post, $post_data );
1647
		// If we're triggering the multi-select panel, then we don't need to redirect to Pinterest
1648
		if ( ! isset( $_GET['js_only'] ) ) {
1649
			$pinterest_url = esc_url_raw( $this->get_external_url( $post ) );
1650
			wp_redirect( $pinterest_url );
1651
		} else {
1652
			echo '// share count bumped';
1653
		}
1654
		die();
1655
	}
1656
1657
	public function display_footer() {
1658
		/**
1659
		 * Filter the Pin it button appearing when hovering over images when using the official button style.
1660
		 *
1661
		 * @module sharedaddy
1662
		 *
1663
		 * @since 3.6.0
1664
		 *
1665
		 * @param bool $jetpack_pinit_over True by default, displays the Pin it button when hovering over images.
1666
		 */
1667
		$jetpack_pinit_over = apply_filters( 'jetpack_pinit_over_button', true );
1668
		?>
1669
		<?php if ( $this->smart ) : ?>
1670
			<script type="text/javascript">
1671
				// Pinterest shared resources
1672
				var s = document.createElement("script");
1673
				s.type = "text/javascript";
1674
				s.async = true;
1675
				<?php if ( $jetpack_pinit_over ) {
1676
				echo "s.setAttribute('data-pin-hover', true);";
1677
				} ?>
1678
				s.src = window.location.protocol + "//assets.pinterest.com/js/pinit.js";
1679
				var x = document.getElementsByTagName("script")[0];
1680
				x.parentNode.insertBefore(s, x);
1681
				// if 'Pin it' button has 'counts' make container wider
1682
				jQuery(window).load( function(){ jQuery( 'li.share-pinterest a span:visible' ).closest( '.share-pinterest' ).width( '80px' ); } );
1683
			</script>
1684
		<?php elseif ( 'buttonPin' != $this->get_widget_type() ) : ?>
1685
			<script type="text/javascript">
1686
				jQuery(document).ready( function(){
1687
					jQuery('body').on('click', 'a.share-pinterest', function(e){
1688
						e.preventDefault();
1689
						// Load Pinterest Bookmarklet code
1690
						var s = document.createElement("script");
1691
						s.type = "text/javascript";
1692
						s.src = window.location.protocol + "//assets.pinterest.com/js/pinmarklet.js?r=" + ( Math.random() * 99999999 );
1693
						var x = document.getElementsByTagName("script")[0];
1694
						x.parentNode.insertBefore(s, x);
1695
						// Trigger Stats
1696
						var s = document.createElement("script");
1697
						s.type = "text/javascript";
1698
						s.src = this + ( this.toString().indexOf( '?' ) ? '&' : '?' ) + 'js_only=1';
1699
						var x = document.getElementsByTagName("script")[0];
1700
						x.parentNode.insertBefore(s, x);
1701
					});
1702
				});
1703
			</script>
1704
		<?php endif;
1705
	}
1706
}
1707
1708
class Share_Pocket extends Sharing_Source {
1709
	public $shortname = 'pocket';
1710
	public $icon = '\f224';
1711
1712 View Code Duplication
	public function __construct( $id, array $settings ) {
1713
		parent::__construct( $id, $settings );
1714
1715
		if ( 'official' == $this->button_style ) {
1716
			$this->smart = true;
1717
		} else {
1718
			$this->smart = false;
1719
		}
1720
	}
1721
1722
	public function get_name() {
1723
		return __( 'Pocket', 'jetpack' );
1724
	}
1725
1726 View Code Duplication
	public function process_request( $post, array $post_data ) {
1727
		// Record stats
1728
		parent::process_request( $post, $post_data );
1729
1730
		$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 ) ) );
1731
		wp_redirect( $pocket_url );
1732
		exit;
1733
	}
1734
1735
	public function get_display( $post ) {
1736
		if ( $this->smart ) {
1737
			$post_count = 'horizontal';
1738
1739
			$button = '';
1740
			$button .= '<div class="pocket_button">';
1741
			$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' ) );
1742
			$button .= '</div>';
1743
1744
			return $button;
1745 View Code Duplication
		} else {
1746
			return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Pocket', 'share to', 'jetpack' ), __( 'Click to share on Pocket', 'jetpack' ), 'share=pocket' );
1747
		}
1748
1749
	}
1750
1751
	/**
1752
	 * AMP display for Pocket.
1753
	 *
1754
	 * @param \WP_Post $post The current post being viewed.
1755
	 */
1756 View Code Duplication
	public function get_amp_display( $post ) {
1757
		$attrs = array(
1758
			'data-share-endpoint' => esc_url_raw( 'https://getpocket.com/save/?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&title=' . rawurlencode( $this->get_share_title( $post->ID ) ) ),
1759
		);
1760
1761
		return $this->build_amp_markup( $attrs );
1762
	}
1763
1764 View Code Duplication
	function display_footer() {
1765
		if ( $this->smart ) :
1766
		?>
1767
		<script>
1768
		// Don't use Pocket's default JS as it we need to force init new Pocket share buttons loaded via JS.
1769
		function jetpack_sharing_pocket_init() {
1770
			jQuery.getScript( 'https://widgets.getpocket.com/v1/j/btn.js?v=1' );
1771
		}
1772
		jQuery( document ).ready( jetpack_sharing_pocket_init );
1773
		jQuery( document.body ).on( 'post-load', jetpack_sharing_pocket_init );
1774
		</script>
1775
		<?php
1776
		else :
1777
			$this->js_dialog( $this->shortname, array( 'width' => 450, 'height' => 450 ) );
1778
		endif;
1779
1780
	}
1781
1782
}
1783
1784
class Share_Telegram extends Sharing_Source {
1785
	public $shortname = 'telegram';
1786
1787
	public function __construct( $id, array $settings ) {
1788
		parent::__construct( $id, $settings );
1789
	}
1790
1791
	public function get_name() {
1792
		return __( 'Telegram', 'jetpack' );
1793
	}
1794 View Code Duplication
	public function process_request( $post, array $post_data ) {
1795
		// Record stats
1796
		parent::process_request( $post, $post_data );
1797
		$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 ) ) );
1798
		wp_redirect( $telegram_url );
1799
		exit;
1800
	}
1801
1802
	public function get_display( $post ) {
1803
		return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Telegram', 'share to', 'jetpack' ), __( 'Click to share on Telegram', 'jetpack' ), 'share=telegram' );
1804
	}
1805
1806
	/**
1807
	 * AMP display for Telegram.
1808
	 *
1809
	 * @param \WP_Post $post The current post being viewed.
1810
	 */
1811 View Code Duplication
	public function get_amp_display( $post ) {
1812
		$attrs = array(
1813
			'data-share-endpoint' => esc_url_raw( 'https://telegram.me/share/url?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&text=' . rawurlencode( $this->get_share_title( $post->ID ) ) ),
1814
		);
1815
1816
		return $this->build_amp_markup( $attrs );
1817
	}
1818
1819
	function display_footer() {
1820
		$this->js_dialog( $this->shortname, array( 'width' => 450, 'height' => 450 ) );
1821
	}
1822
}
1823
1824
class Jetpack_Share_WhatsApp extends Sharing_Source {
1825
	public $shortname = 'jetpack-whatsapp';
1826
1827
	public function __construct( $id, array $settings ) {
1828
		parent::__construct( $id, $settings );
1829
	}
1830
1831
	public function get_name() {
1832
		return __( 'WhatsApp', 'jetpack' );
1833
	}
1834
1835
	public function get_display( $post ) {
1836
		return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'WhatsApp', 'share to', 'jetpack' ), __( 'Click to share on WhatsApp', 'jetpack' ), 'share=jetpack-whatsapp' );
1837
	}
1838
1839
	/**
1840
	 * AMP display for Whatsapp.
1841
	 *
1842
	 * @param \WP_Post $post The current post being viewed.
1843
	 */
1844
	public function get_amp_display( $post ) {
1845
		$attrs = array(
1846
			'type' => 'whatsapp',
1847
		);
1848
1849
		return $this->build_amp_markup( $attrs );
1850
	}
1851
1852
	public function process_request( $post, array $post_data ) {
1853
		// Record stats
1854
		parent::process_request( $post, $post_data );
1855
1856
		// Firefox for desktop doesn't handle the "api.whatsapp.com" URL properly, so use "web.whatsapp.com"
1857
		if ( Jetpack_User_Agent_Info::is_firefox_desktop() ) {
1858
			$url = 'https://web.whatsapp.com/send?text=';
1859
		} else {
1860
			$url = 'https://api.whatsapp.com/send?text=';
1861
		}
1862
1863
		$url .= rawurlencode( $this->get_share_title( $post->ID ) . ' ' . $this->get_share_url( $post->ID ) );
1864
		wp_redirect( $url );
1865
		exit;
1866
	}
1867
}
1868
1869
class Share_Skype extends Sharing_Source {
1870
	public $shortname = 'skype';
1871
	public $icon = '\f220';
1872
	private $share_type = 'default';
1873
1874 View Code Duplication
	public function __construct( $id, array $settings ) {
1875
		parent::__construct( $id, $settings );
1876
1877
		if ( isset( $settings['share_type'] ) ) {
1878
			$this->share_type = $settings['share_type'];
1879
		}
1880
1881
		if ( 'official' == $this->button_style ) {
1882
			$this->smart = true;
1883
		} else {
1884
			$this->smart = false;
1885
		}
1886
1887
	}
1888
1889
	public function get_name() {
1890
		return __( 'Skype', 'jetpack' );
1891
	}
1892
1893
	public function get_display( $post ) {
1894
		if ( $this->smart ) {
1895
			$skype_share_html = sprintf(
1896
				'<div class="skype-share" data-href="%1$s" data-lang="%2$s" data-style="small" data-source="jetpack" ></div>',
1897
				esc_attr( $this->get_share_url( $post->ID ) ),
1898
				'en-US'
1899
			);
1900
			return $skype_share_html;
1901
		}
1902
1903
		/** This filter is already documented in modules/sharedaddy/sharing-sources.php */
1904
		if ( apply_filters( 'jetpack_register_post_for_share_counts', true, $post->ID, 'skype' ) ) {
1905
			sharing_register_post_for_share_counts( $post->ID );
1906
		}
1907
		return $this->get_link(
1908
			$this->get_process_request_url( $post->ID ), _x( 'Skype', 'share to', 'jetpack' ), __( 'Click to share on Skype', 'jetpack' ), 'share=skype', 'sharing-skype-' . $post->ID );
1909
	}
1910
1911
	/**
1912
	 * AMP display for Skype.
1913
	 *
1914
	 * @param \WP_Post $post The current post being viewed.
1915
	 */
1916
	public function get_amp_display( $post ) {
1917
		$attrs = array(
1918
			'data-share-endpoint' => sprintf(
1919
				'https://web.skype.com/share?url=%1$s&lang=%2$s=&source=jetpack',
1920
				rawurlencode( $this->get_share_url( $post->ID ) ),
1921
				'en-US'
1922
			),
1923
		);
1924
1925
		return $this->build_amp_markup( $attrs );
1926
	}
1927
1928 View Code Duplication
	public function process_request( $post, array $post_data ) {
1929
		$skype_url = sprintf(
1930
			'https://web.skype.com/share?url=%1$s&lang=%2$s=&source=jetpack',
1931
			rawurlencode( $this->get_share_url( $post->ID ) ),
1932
			'en-US'
1933
		);
1934
1935
		// Record stats
1936
		parent::process_request( $post, $post_data );
1937
1938
		// Redirect to Skype
1939
		wp_redirect( $skype_url );
1940
		die();
1941
	}
1942
1943 View Code Duplication
	public function display_footer() {
1944
		if ( $this->smart ) :
1945
			?>
1946
			<script>
1947
				(function(r, d, s) {
1948
					r.loadSkypeWebSdkAsync = r.loadSkypeWebSdkAsync || function(p) {
1949
							var js, sjs = d.getElementsByTagName(s)[0];
1950
							if (d.getElementById(p.id)) { return; }
1951
							js = d.createElement(s);
1952
							js.id = p.id;
1953
							js.src = p.scriptToLoad;
1954
							js.onload = p.callback
1955
							sjs.parentNode.insertBefore(js, sjs);
1956
						};
1957
					var p = {
1958
						scriptToLoad: 'https://swx.cdn.skype.com/shared/v/latest/skypewebsdk.js',
1959
						id: 'skype_web_sdk'
1960
					};
1961
					r.loadSkypeWebSdkAsync(p);
1962
				})(window, document, 'script');
1963
			</script>
1964
			<?php
1965
		else :
1966
			$this->js_dialog( $this->shortname, array( 'width' => 305, 'height' => 665 ) );
1967
		endif;
1968
	}
1969
}
1970