-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathclass-shared-counts-amp.php
108 lines (100 loc) · 2.31 KB
/
class-shared-counts-amp.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/**
* AMP class.
*
* Contains functionality for AMP compatibility
*
* @package SharedCounts
* @author Bill Erickson & Jared Atchison
* @since 1.4.0
* @license GPL-2.0+
* @copyright Copyright (c) 2017
*/
class Shared_Counts_AMP {
/**
* Primary class constructor.
*
* @since 1.4.0
*/
public function __construct() {
add_filter( 'shared_counts_load_js', array( $this, 'disable_js' ) );
add_filter( 'shared_counts_additional_attr', array( $this, 'print_action' ), 10, 4 );
add_filter( 'shared_counts_link', array( $this, 'print_link' ), 10, 3 );
add_filter( 'shared_counts_link', array( $this, 'email_action' ), 10, 3 );
}
/**
* Is AMP? conditional
*
* @since 1.4.0
*
* @return bool
*/
public function is_amp() {
return function_exists('is_amp_endpoint') ? is_amp_endpoint() : false;
}
/**
* Disable JS
*
* @since 1.4.0
*
* @return bool
*/
public function disable_js( $load_js ) {
if( $this->is_amp() )
$load_js = false;
return $load_js;
}
/**
* Print Action
*
* @since 1.4.0
*
* @param array $attr
* @param array $link
* @param int $id
* @param string $style
* @return array
*/
function print_action( $attr, $link, $id, $style ) {
if( $this->is_amp() && 'print' === $link['type'] ) {
$attr[] = 'on="tap:AMP.print()"';
}
return $attr;
}
/**
* Remove print javascript.
*
* @since 1.4.0
*
* @param array $link
* @param int $id
* @return array
*/
function print_link( $link, $id ) {
if( $this->is_amp() && 'print' === $link['type'] ) {
$link['link'] = '';
}
return $link;
}
/**
* Email action
*
* @since 1.4.0
*
* @param array $link
* @param int $id
* @param string $style
* @return array
*/
function email_action( $link, $id, $style ) {
if( $this->is_amp() && 'email' === $link['type'] ) {
$subject = esc_html__( 'Your friend has shared an article with you.', 'shared-counts' );
$subject = apply_filters( 'shared_counts_amp_email_subject', $subject, $id );
$body = html_entity_decode( get_the_title( $id ), ENT_QUOTES ) . "\r\n";
$body .= get_permalink( $id ) . "\r\n";
$body = apply_filters( 'shared_counts_amp_email_body', $body, $id );
$link['link'] = 'mailto:?subject=' . rawurlencode( $subject ) . '&body=' . rawurlencode( $body );
}
return $link;
}
}