1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Clubdeuce\WPShareThis; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class WP_Share_This |
7
|
|
|
* @package Clubdeuce\WPShareThis |
8
|
|
|
* |
9
|
|
|
* @link http://www.sharethis.com/support/customization/how-to-set-custom-buttons/ |
10
|
|
|
*/ |
11
|
|
|
class WP_Share_This { |
12
|
|
|
|
13
|
|
|
const VERSION = '0.0.4'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var bool |
17
|
|
|
*/ |
18
|
|
|
private static $_facebook_og = true; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private static $_id = null; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private static $_services = array(); |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* |
32
|
|
|
*/ |
33
|
|
|
public static function initialize() { |
34
|
|
|
|
35
|
|
|
add_action( 'wp_enqueue_scripts', array( __CLASS__, '_wp_enqueue_scripts' ) ); |
36
|
|
|
add_action( 'wp_head', array( __CLASS__, '_wp_head' ) ); |
37
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* |
42
|
|
|
*/ |
43
|
|
|
public static function _wp_enqueue_scripts() { |
44
|
|
|
|
45
|
|
|
$id = self::$_id; |
46
|
|
|
|
47
|
|
|
wp_enqueue_script('sharethis', "//platform-api.sharethis.com/js/sharethis.js#property={$id}&product=unknown", null, false, true ); |
48
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* |
53
|
|
|
*/ |
54
|
|
|
public static function _wp_head() { |
55
|
|
|
|
56
|
|
|
if ( self::$_facebook_og ) { |
57
|
|
|
self::_facebook_og(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* The ShareThis account id. |
64
|
|
|
* |
65
|
|
|
* @param string $id |
66
|
|
|
*/ |
67
|
|
|
public static function register_id( $id ) { |
68
|
|
|
|
69
|
|
|
self::$_id = $id; |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $service |
75
|
|
|
* @param array $params |
76
|
|
|
*/ |
77
|
|
|
public static function register_service( $service, $params = array() ) { |
78
|
|
|
|
79
|
|
|
self::$_services[ $service ] = $params; |
80
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param array $args |
85
|
|
|
*/ |
86
|
|
|
public static function the_sharing_links( $args = array() ) { |
87
|
|
|
|
88
|
|
|
$args = wp_parse_args( $args, array( |
89
|
|
|
'post' => get_post(), |
90
|
|
|
) ); |
91
|
|
|
|
92
|
|
|
foreach ( self::$_services as $service => $params ) { |
93
|
|
|
$args = array_merge( $params, $args ); |
94
|
|
|
self::_render_sharing_link( $args, $service ); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param bool $use |
101
|
|
|
* |
102
|
|
|
* @since 0.0.2 |
103
|
|
|
*/ |
104
|
|
|
public static function use_og( $use = true ) { |
105
|
|
|
|
106
|
|
|
self::$_facebook_og = $use; |
107
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param array $params |
112
|
|
|
* @param string $service |
113
|
|
|
*/ |
114
|
|
|
private static function _render_sharing_link( $params, $service ) { |
115
|
|
|
|
116
|
|
|
$classes = apply_filters( "wpst_link_classes_{$service}", array( $service, ) ); |
117
|
|
|
$post = get_post(); |
118
|
|
|
|
119
|
|
|
if ( $params['post'] instanceof \WP_Post ) { |
120
|
|
|
$post = $params['post']; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
unset( $params['post'] ); |
124
|
|
|
|
125
|
|
|
$args = wp_parse_args( $params, array( |
126
|
|
|
'url' => get_permalink( $post ), |
127
|
|
|
'short_url' => null, |
128
|
|
|
'title' => get_the_title( $post ), |
129
|
|
|
'image' => null, |
130
|
|
|
'description' => get_the_excerpt( $post ), |
131
|
|
|
'username' => null, |
132
|
|
|
'message' => get_the_excerpt( $post ), |
133
|
|
|
'share_count' => true, |
134
|
|
|
) ); |
135
|
|
|
|
136
|
|
|
if ( has_post_thumbnail( $post ) ) { |
137
|
|
|
$args['image'] = get_the_post_thumbnail_url( $post ); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
printf( |
141
|
|
|
'<div data-network="%1$s" class="st-custom-button %2$s"%3$s%4$s%5$s%6$s%7$s%8$s%9$s>%10$s%11$s</div>', |
142
|
|
|
$service, |
143
|
|
|
implode( ' ', apply_filters( 'wpst_link_classes', $classes, $service ) ), |
144
|
|
|
esc_url( self::_item_sharing_property( 'url', $args['url'] ) ), |
145
|
|
|
esc_url( self::_item_sharing_property( 'short_url', $args['short_url'] ) ), |
146
|
|
|
esc_attr( self::_item_sharing_property( 'title', $args['title'] ) ), |
147
|
|
|
esc_url( self::_item_sharing_property( 'image', $args['image'] ) ), |
148
|
|
|
esc_attr( self::_item_sharing_property( 'description', $args['description'] ) ), |
149
|
|
|
esc_attr( self::_item_sharing_property( 'username', $args['username'] ) ), |
150
|
|
|
esc_attr( self::_item_sharing_property( 'message', $args['message'] . "\r\n\r\n" . $args['url'] ) ), |
151
|
|
|
wp_kses_post( apply_filters( 'wpst_link_text', ucfirst( $service ) ) ), |
152
|
|
|
wp_kses_post( self::_item_sharing_count( $args['share_count'] ) ) |
153
|
|
|
); |
154
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @link https://developers.facebook.com/docs/sharing/webmasters#basic |
159
|
|
|
*/ |
160
|
|
|
private static function _facebook_og() { |
161
|
|
|
|
162
|
|
|
printf( '<meta property="og:url" content="%1$s" />' . PHP_EOL, self::_og_url() ); |
163
|
|
|
printf( '<meta property="og:type" content="%1$s" />' . PHP_EOL, self::_og_type() ); |
164
|
|
|
printf( '<meta property="og:title" content="%1$s" />' . PHP_EOL, self::_og_title() ); |
165
|
|
|
|
166
|
|
|
if ( ! ( empty( $description = self::_og_description() ) ) ) { |
167
|
|
|
printf( '<meta property="og:description" content="%1$s" />' . PHP_EOL, $description ); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
if ( $image_url = self::_og_image() ) { |
171
|
|
|
printf( '<meta property="og:image" content="%1$s" />' . PHP_EOL, $image_url ); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return mixed|void |
178
|
|
|
*/ |
179
|
|
|
private static function _og_url() { |
180
|
|
|
|
181
|
|
|
$url = get_permalink(); |
182
|
|
|
|
183
|
|
|
if ( is_home() || is_front_page() ) { |
184
|
|
|
$url = home_url(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if ( is_post_type_archive() ) { |
188
|
|
|
$url = get_post_type_archive_link( get_post_type() ); |
|
|
|
|
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return apply_filters( 'wpst_og_url', $url ); |
192
|
|
|
|
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return mixed|void |
197
|
|
|
*/ |
198
|
|
|
private static function _og_type() { |
199
|
|
|
|
200
|
|
|
$type = 'website'; |
201
|
|
|
|
202
|
|
|
if ( is_single() ) { |
203
|
|
|
$type = 'article'; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return apply_filters( 'wpst_og_type', $type ); |
207
|
|
|
|
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @return mixed|void |
213
|
|
|
*/ |
214
|
|
|
private static function _og_title() { |
215
|
|
|
|
216
|
|
|
$title = get_the_title(); |
217
|
|
|
|
218
|
|
|
if ( is_home() || is_front_page() ) { |
219
|
|
|
$title = get_bloginfo( 'name' ); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
if ( is_post_type_archive() && get_post_type() ) { |
223
|
|
|
$labels = get_post_type_labels( get_post_type_object( get_post_type() ) ); |
|
|
|
|
224
|
|
|
$title = sprintf( '%1$s: %2$s', get_bloginfo( 'name' ), $labels['name'] ); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return apply_filters( 'wpst_og_title', wp_kses_post( $title ) ); |
228
|
|
|
|
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @return mixed|void |
233
|
|
|
*/ |
234
|
|
|
private static function _og_description() { |
235
|
|
|
|
236
|
|
|
$description = apply_filters( 'the_excerpt', get_the_excerpt() ); |
237
|
|
|
|
238
|
|
|
if ( is_home() || is_front_page() ) { |
239
|
|
|
$description = get_bloginfo( 'description' ); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
if ( is_post_type_archive() && get_post_type() ) { |
243
|
|
|
$labels = get_post_type_labels( get_post_type_object( get_post_type() ) ); |
|
|
|
|
244
|
|
|
$description = sprintf( 'The %1$s %2$s Archive', get_bloginfo( 'name' ), $labels['name'] ); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
return apply_filters( 'wpst_og_description', esc_html( $description ) ); |
248
|
|
|
|
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @return mixed|void |
253
|
|
|
*/ |
254
|
|
|
private static function _og_image() { |
255
|
|
|
|
256
|
|
|
$image_url = get_the_post_thumbnail_url(); |
257
|
|
|
|
258
|
|
|
if ( is_home() || is_front_page() ) { |
259
|
|
|
$image_url = ''; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
return apply_filters( 'wpst_og_image', esc_url( $image_url ) ); |
|
|
|
|
263
|
|
|
|
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @param string $property |
268
|
|
|
* @param string $value |
269
|
|
|
* |
270
|
|
|
* @return string |
271
|
|
|
*/ |
272
|
|
|
private static function _item_sharing_property( $property, $value ) { |
273
|
|
|
|
274
|
|
|
$text = ''; |
275
|
|
|
$maybe = apply_filters( "wpst_item_{$property}", $value ); |
276
|
|
|
|
277
|
|
|
if ( ! empty( $maybe ) ) { |
278
|
|
|
$text = sprintf( ' data-%1$s="%2$s" ', str_replace('_', '-', $property ), esc_attr( $maybe ) ); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
return $text; |
282
|
|
|
|
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @param bool $show |
287
|
|
|
* |
288
|
|
|
* @return string |
289
|
|
|
*/ |
290
|
|
|
private static function _item_sharing_count( $show ) { |
291
|
|
|
|
292
|
|
|
$text = ''; |
293
|
|
|
|
294
|
|
|
if ( $show ) { |
295
|
|
|
$text = '<span class="count"></span>'; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
return $text; |
299
|
|
|
|
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
} |
303
|
|
|
|