-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathclass-shared-counts-front.php
More file actions
745 lines (653 loc) · 31.6 KB
/
class-shared-counts-front.php
File metadata and controls
745 lines (653 loc) · 31.6 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
<?php
/**
* Front-end class.
*
* Contains functionality for the site front-end.
*
* @package SharedCounts
* @author Bill Erickson & Jared Atchison
* @since 1.0.0
* @license GPL-2.0+
* @copyright Copyright (c) 2019
*/
class Shared_Counts_Front {
/**
* Holds if a share link as been detected.
*
* @since 1.0.0
*
* @var boolean
*/
public $share_link = false;
/**
* Theme location placements.
*
* @since 1.0.0
*
* @var array
*/
public $locations;
/**
* Primary class constructor.
*
* @since 1.0.0
*/
public function __construct() {
// Load assets.
add_action( 'template_redirect', [ $this, 'theme_location' ], 99 );
add_action( 'wp_enqueue_scripts', [ $this, 'header_assets' ], 9 );
add_action( 'wp_footer', [ $this, 'load_assets' ], 1 );
add_shortcode( 'shared_counts', [ $this, 'shortcode' ] );
add_action( 'admin_bar_menu', [ $this, 'admin_bar' ], 999 );
}
/**
* Add share buttons to theme locations.
*
* @since 1.0.0
*/
public function theme_location() {
// Genesis Hooks.
if ( 'genesis' === get_template() ) {
$locations = [
'before' => [
'hook' => 'genesis_entry_header',
'filter' => false,
'priority' => 13,
'style' => false,
],
'after' => [
'hook' => 'genesis_entry_footer',
'filter' => false,
'priority' => 8,
'style' => false,
],
];
// Theme Hook Alliance.
} elseif ( current_theme_supports( 'tha_hooks', [ 'entry' ] ) ) {
$locations = [
'before' => [
'hook' => 'tha_entry_top',
'filter' => false,
'priority' => 13,
'style' => false,
],
'after' => [
'hook' => 'tha_entry_bottom',
'filter' => false,
'priority' => 8,
'style' => false,
],
];
// Fallback to 'the_content'.
} else {
$locations = [
'before' => [
'hook' => false,
'filter' => 'the_content',
'priority' => 8,
'style' => false,
],
'after' => [
'hook' => false,
'filter' => 'the_content',
'priority' => 12,
'style' => false,
],
];
}
// Filter theme locations.
$locations = apply_filters( 'shared_counts_theme_locations', $locations );
// Make locations available everywhere.
$this->locations = $locations;
// Display share buttons before content.
if ( $locations['before']['hook'] ) {
add_action( $locations['before']['hook'], [ $this, 'display_before_content' ], $locations['before']['priority'] );
} elseif ( $locations['before']['filter'] && ! is_feed() ) {
add_filter( $locations['before']['filter'], [ $this, 'display_before_content_filter' ], $locations['before']['priority'] );
}
// Display share buttons after content.
if ( $locations['after']['hook'] ) {
add_action( $locations['after']['hook'], [ $this, 'display_after_content' ], $locations['after']['priority'] );
} elseif ( $locations['after']['filter'] && ! is_feed() ) {
add_filter( $locations['after']['filter'], [ $this, 'display_after_content_filter' ], $locations['after']['priority'] );
}
}
/**
* Enqueue the assets earlier if possible.
*
* @since 1.0.0
*/
public function header_assets() {
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
// Register assets.
wp_register_style(
'shared-counts',
SHARED_COUNTS_URL . 'assets/css/shared-counts' . $suffix . '.css',
[],
SHARED_COUNTS_VERSION
);
wp_register_script(
'shared-counts',
SHARED_COUNTS_URL . 'assets/js/shared-counts' . $suffix . '.js',
[],
SHARED_COUNTS_VERSION,
true
);
$options = shared_counts()->admin->options();
if ( ! empty( $options['theme_location'] )
&& ! empty( $options['post_type'] )
&& is_singular( $options['post_type'] )
&& ! get_post_meta( get_the_ID(), 'shared_counts_exclude', true )
) {
$this->share_link = true;
$this->load_assets();
}
}
/**
* Determines if assets need to be loaded.
*
* @since 1.0.0
*/
public function load_assets() {
// Only continue if a share link was previously used in the page.
if ( ! $this->share_link ) {
return;
}
// Load CSS.
if ( apply_filters( 'shared_counts_load_css', true ) ) {
wp_enqueue_style( 'shared-counts' );
}
// Load JS.
if ( apply_filters( 'shared_counts_load_js', true ) ) {
wp_enqueue_script( 'shared-counts' );
}
// Localize JS strings.
$args = [
'social_tracking' => apply_filters( 'shared_counts_social_tracking', true ),
];
wp_localize_script( 'shared-counts', 'shared_counts', $args );
}
/**
* Display Share Counts based on plugin settings.
*
* @since 1.0.0
*
* @param string $location Theme location.
* @param bool $echo Echo or return.
* @param string $style Desired style.
* @param int $post_id Post ID.
* @param string|array $services Specific services to display.
*
* @return null|string
*/
public function display( $location = '', $echo = true, $style = false, $post_id = false, $services = '' ) {
$options = shared_counts()->admin->options();
if ( empty( $services ) ) {
$services = $options['included_services'];
} elseif ( ! is_array( $services ) ) {
$services = explode( ',', $services );
}
if ( ! $style && ! empty( $options['style'] ) ) {
$style = esc_attr( $options['style'] );
}
$included_services = array_filter( apply_filters( 'shared_counts_display_services', $services, $location ) );
$services_output = '';
foreach ( $included_services as $service ) {
$services_output .= $this->link( trim( $service ), $post_id, false, $style );
}
$classes = apply_filters( 'shared_counts_wrap_classes', [ 'shared-counts-wrap', $location, 'style-' . $style ] );
$classes = array_map( 'sanitize_html_class', array_filter( $classes ) );
$links = apply_filters( 'shared_counts_display', $services_output, $location );
$wrap_format = apply_filters( 'shared_counts_display_wrap_format', '<div class="%2$s">%1$s</div>', $location );
$output = apply_filters( 'shared_counts_display_output', sprintf( $wrap_format, $links, join( ' ', $classes ) ), $location );
if ( $echo ) {
echo $output; // phpcs:ignore
} else {
return $output;
}
}
/**
* Display Before Content.
*
* @since 1.0.0
*/
public function display_before_content() {
$options = shared_counts()->admin->options();
if (
( 'before_content' === $options['theme_location'] || 'before_after_content' === $options['theme_location'] )
&& ! empty( $options['post_type'] )
&& is_singular( $options['post_type'] )
&& ! get_post_meta( get_the_ID(), 'shared_counts_exclude', true )
) {
// Detect if we are using a hook or filter.
if ( ! empty( $this->locations['before']['hook'] ) ) {
$this->display( 'before_content', true, $this->locations['before']['style'] );
} elseif ( ! empty( $this->locations['before']['filter'] ) ) {
return $this->display( 'before_content', false, $this->locations['before']['style'] );
}
}
}
/**
* Display Before Content Filter.
*
* @since 1.0.0
*
* @param string $content Post content.
*
* @return string $content
*/
public function display_before_content_filter( $content ) {
return $this->display_before_content() . $content;
}
/**
* Display After Content.
*
* @since 1.0.0
*
* @return string
*/
public function display_after_content() {
$options = shared_counts()->admin->options();
if (
( 'after_content' === $options['theme_location'] || 'before_after_content' === $options['theme_location'] )
&& ! empty( $options['post_type'] )
&& is_singular( $options['post_type'] )
&& ! get_post_meta( get_the_ID(), 'shared_counts_exclude', true )
) {
// Detect if we are using a hook or filter.
if ( ! empty( $this->locations['after']['hook'] ) ) {
$this->display( 'after_content', true, $this->locations['after']['style'] );
} elseif ( ! empty( $this->locations['after']['filter'] ) ) {
return $this->display( 'after_content', false, $this->locations['after']['style'] );
}
}
}
/**
* Display After Content Filter.
*
* @since 1.0.0
*
* @param string $content Post content.
*
* @return string $content
*/
public function display_after_content_filter( $content ) {
return $content . $this->display_after_content();
}
/**
* Generate sharing links.
*
* @since 1.0.0
*
* @param string $types Button type.
* @param int/string $id Post or Site ID.
* @param boolean $echo Echo or return.
* @param string $style Button style.
* @param int $round How many significant digits on count.
* @param bool $show_empty Show empty counts.
*/
public function link( $types = 'facebook', $id = false, $echo = true, $style = 'generic', $round = 2, $show_empty = '' ) {
if ( ! $id ) {
$id = get_the_ID();
}
$this->share_link = true;
$types = (array) $types;
$output = '';
$options = shared_counts()->admin->options();
$attr = [ 'postid' => $id ];
if ( empty( $show_empty ) ) {
$show_empty = '1' === $options['hide_empty'] ? 'false' : 'true';
}
foreach ( $types as $type ) {
// Discontinued.
if ( in_array( $type, [ 'stumbleupon', 'google' ], true ) ) {
continue;
}
$link = [];
$link['type'] = $type;
$link['class'] = '';
$link['img'] = apply_filters( 'shared_counts_default_image', '', $id, $link );
if ( 'site' === $id ) {
$link['url'] = esc_url( home_url() );
$link['title'] = wp_strip_all_tags( get_bloginfo( 'name' ) );
} elseif ( 0 === strpos( $id, 'http' ) ) {
$link['url'] = esc_url( $id );
$link['title'] = '';
} else {
$link['url'] = esc_url( get_permalink( $id ) );
$link['title'] = wp_strip_all_tags( get_the_title( $id ) );
if ( has_post_thumbnail( $id ) ) {
$link['img'] = wp_get_attachment_image_url( get_post_thumbnail_id( $id ), 'full' );
}
$link['img'] = apply_filters( 'shared_counts_single_image', $link['img'], $id, $link );
}
$link['url'] = apply_filters( 'shared_counts_link_url', $link['url'], $link );
$link['count'] = shared_counts()->core->count( $id, $type, false, $round );
switch ( $type ) {
case 'facebook':
$link['link'] = 'https://www.facebook.com/sharer/sharer.php?u=' . $link['url'] . '&display=popup&ref=plugin&src=share_button';
$link['label'] = esc_html__( 'Facebook', 'shared-counts' );
$link['icon'] = '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="18.8125" height="32" viewBox="0 0 602 1024"><path d="M548 6.857v150.857h-89.714q-49.143 0-66.286 20.571t-17.143 61.714v108h167.429l-22.286 169.143h-145.143v433.714h-174.857v-433.714h-145.714v-169.143h145.714v-124.571q0-106.286 59.429-164.857t158.286-58.571q84 0 130.286 6.857z"></path></svg>';
$link['target'] = '_blank';
$link['rel'] = 'nofollow noopener noreferrer';
$link['attr_title'] = esc_html__( 'Share on Facebook', 'shared-counts' );
$link['social_network'] = 'Facebook';
$link['social_action'] = 'Share';
break;
case 'facebook_likes':
$link['link'] = 'https://www.facebook.com/plugins/like.php?href=' . $link['url'];
$link['label'] = esc_html__( 'Like', 'shared-counts' );
$link['icon'] = '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="18.8125" height="32" viewBox="0 0 602 1024"><path d="M548 6.857v150.857h-89.714q-49.143 0-66.286 20.571t-17.143 61.714v108h167.429l-22.286 169.143h-145.143v433.714h-174.857v-433.714h-145.714v-169.143h145.714v-124.571q0-106.286 59.429-164.857t158.286-58.571q84 0 130.286 6.857z"></path></svg>';
$link['target'] = '_blank';
$link['rel'] = 'nofollow noopener noreferrer';
$link['attr_title'] = esc_html__( 'Like on Facebook', 'shared-counts' );
$link['social_network'] = 'Facebook';
$link['social_action'] = 'Like';
break;
case 'facebook_shares':
$link['link'] = 'https://www.facebook.com/sharer/sharer.php?u=' . $link['url'] . '&display=popup&ref=plugin&src=share_button';
$link['label'] = esc_html__( 'Share', 'shared-counts' );
$link['icon'] = '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="18.8125" height="32" viewBox="0 0 602 1024"><path d="M548 6.857v150.857h-89.714q-49.143 0-66.286 20.571t-17.143 61.714v108h167.429l-22.286 169.143h-145.143v433.714h-174.857v-433.714h-145.714v-169.143h145.714v-124.571q0-106.286 59.429-164.857t158.286-58.571q84 0 130.286 6.857z"></path></svg>';
$link['target'] = '_blank';
$link['rel'] = 'nofollow noopener noreferrer';
$link['attr_title'] = esc_html__( 'Share on Facebook', 'shared-counts' );
$link['social_network'] = 'Facebook';
$link['social_action'] = 'Share';
break;
case 'twitter':
$link['link'] = 'https://twitter.com/share?url=' . $link['url'] . '&text=' . htmlspecialchars( rawurlencode( html_entity_decode( $link['title'], ENT_COMPAT, 'UTF-8' ) ), ENT_COMPAT, 'UTF-8' );
$link['label'] = esc_html__( 'Tweet', 'shared-counts' );
$link['icon'] = '<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M13.8944 10.4695L21.3345 2H19.5716L13.1085 9.35244L7.95022 2H1.99936L9.80147 13.1192L1.99936 22H3.76218L10.5832 14.2338L16.0318 22H21.9827L13.8944 10.4695ZM11.4792 13.2168L10.6875 12.1089L4.39789 3.30146H7.10594L12.1833 10.412L12.9717 11.5199L19.5708 20.7619H16.8628L11.4792 13.2168Z" /></svg>';
$link['target'] = '_blank';
$link['rel'] = 'nofollow noopener noreferrer';
$link['attr_title'] = esc_html__( 'Share on Twitter', 'shared-counts' );
$link['social_network'] = 'Twitter';
$link['social_action'] = 'Tweet';
break;
case 'pinterest':
$link['link'] = 'https://pinterest.com/pin/create/button/?url=' . $link['url'] . '&media=' . $link['img'] . '&description=' . $link['title'];
$link['label'] = esc_html__( 'Pin', 'shared-counts' );
$link['icon'] = '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="22.84375" height="32" viewBox="0 0 731 1024"><path d="M0 341.143q0-61.714 21.429-116.286t59.143-95.143 86.857-70.286 105.714-44.571 115.429-14.857q90.286 0 168 38t126.286 110.571 48.571 164q0 54.857-10.857 107.429t-34.286 101.143-57.143 85.429-82.857 58.857-108 22q-38.857 0-77.143-18.286t-54.857-50.286q-5.714 22.286-16 64.286t-13.429 54.286-11.714 40.571-14.857 40.571-18.286 35.714-26.286 44.286-35.429 49.429l-8 2.857-5.143-5.714q-8.571-89.714-8.571-107.429 0-52.571 12.286-118t38-164.286 29.714-116q-18.286-37.143-18.286-96.571 0-47.429 29.714-89.143t75.429-41.714q34.857 0 54.286 23.143t19.429 58.571q0 37.714-25.143 109.143t-25.143 106.857q0 36 25.714 59.714t62.286 23.714q31.429 0 58.286-14.286t44.857-38.857 32-54.286 21.714-63.143 11.429-63.429 3.714-56.857q0-98.857-62.571-154t-163.143-55.143q-114.286 0-190.857 74t-76.571 187.714q0 25.143 7.143 48.571t15.429 37.143 15.429 26 7.143 17.429q0 16-8.571 41.714t-21.143 25.714q-1.143 0-9.714-1.714-29.143-8.571-51.714-32t-34.857-54-18.571-61.714-6.286-60.857z"></path></svg>';
$link['target'] = '_blank';
$link['rel'] = 'nofollow noopener noreferrer';
$link['attr_title'] = esc_html__( 'Share on Pinterest', 'shared-counts' );
$link['social_network'] = 'Pinterest';
$link['social_action'] = 'Pin';
break;
case 'yummly':
$link['link'] = 'https://www.yummly.com/urb/verify?url=' . $link['url'] . '&title=' . rawurlencode( $link['title'] ) . '&yumtype=button';
$link['label'] = esc_html__( 'Yummly', 'shared-counts' );
$link['icon'] = '<svg xmlns="http://www.w3.org/2000/svg" height="32" width="32" viewBox="0 0 32 32"><path d="M27.127 21.682c-.015-.137-.132-.213-.216-.236-.21-.06-.43-.01-1.06-.29-.51-.23-2.875-1.37-6.13-1.746l2.357-13.426c.105-.602.1-1.087-.09-1.394-.277-.45-.886-.573-1.586-.514-.545.05-.98.25-1.07.312a.325.325 0 0 0-.145.288c.023.253.22.45.057 1.425-.032.21-.802 4.505-1.453 8.14-1.724 1.038-4.018 1.527-4.488.905-.228-.31-.177-.89.04-1.757.05-.193 1.06-4.03 1.347-5.135.54-2.105.13-4.035-2.05-4.23-1.88-.17-3.676.935-4.216 1.51-.39.415-.26.916.09 1.52.275.473.642.78.735.836.115.07.263.07.32.01.63-.71 1.775-1.243 2.173-.915.35.29.216.83.08 1.35 0 0-1.227 4.606-1.723 6.526-.366 1.417-.007 2.705 1.027 3.32.77.473 1.914.435 2.816.32 1.96-.24 3.11-1.066 3.296-1.208l-.363 2.02s-2.214.2-4.027 1.286c-2.383 1.428-3.345 4.673-1.82 6.347 1.526 1.674 4.193 1.04 5.277.308 1.045-.7 2.407-2.18 3.023-5.535 3.596.184 4.53 2.046 6.063 2.113 1.1.048 1.876-1.014 1.737-2.142zm-12.23 3.214c-.51.297-1.03.337-1.35-.03-.337-.388-.435-2.5 2.914-3.13.005 0-.523 2.56-1.56 3.16z"></path></svg>';
$link['target'] = '_blank';
$link['rel'] = 'nofollow noopener noreferrer';
$link['attr_title'] = esc_html__( 'Share on Yummly', 'shared-counts' );
$link['social_network'] = 'Yummly';
$link['social_action'] = 'Saved';
break;
case 'linkedin':
$link['link'] = 'https://www.linkedin.com/shareArticle?mini=true&url=' . $link['url'];
$link['label'] = esc_html__( 'LinkedIn', 'shared-counts' );
$link['icon'] = '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="27.4375" height="32" viewBox="0 0 878 1024"><path d="M199.429 357.143v566.286h-188.571v-566.286h188.571zM211.429 182.286q0.571 41.714-28.857 69.714t-77.429 28h-1.143q-46.857 0-75.429-28t-28.571-69.714q0-42.286 29.429-70t76.857-27.714 76 27.714 29.143 70zM877.714 598.857v324.571h-188v-302.857q0-60-23.143-94t-72.286-34q-36 0-60.286 19.714t-36.286 48.857q-6.286 17.143-6.286 46.286v316h-188q1.143-228 1.143-369.714t-0.571-169.143l-0.571-27.429h188v82.286h-1.143q11.429-18.286 23.429-32t32.286-29.714 49.714-24.857 65.429-8.857q97.714 0 157.143 64.857t59.429 190z"></path></svg>';
$link['target'] = '_blank';
$link['rel'] = 'nofollow noopener noreferrer';
$link['attr_title'] = esc_html__( 'Share on LinkedIn', 'shared-counts' );
$link['social_network'] = 'LinkedIn';
$link['social_action'] = 'Share';
break;
case 'included_total':
$link['link'] = '';
$link['label'] = _n( 'Share', 'Shares', $link['count'], 'shared-counts' );
$link['icon'] = '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="27.4375" height="32" viewBox="0 0 878 1024"><path d="M694.857 585.143q76 0 129.429 53.429t53.429 129.429-53.429 129.429-129.429 53.429-129.429-53.429-53.429-129.429q0-6.857 1.143-19.429l-205.714-102.857q-52.571 49.143-124.571 49.143-76 0-129.429-53.429t-53.429-129.429 53.429-129.429 129.429-53.429q72 0 124.571 49.143l205.714-102.857q-1.143-12.571-1.143-19.429 0-76 53.429-129.429t129.429-53.429 129.429 53.429 53.429 129.429-53.429 129.429-129.429 53.429q-72 0-124.571-49.143l-205.714 102.857q1.143 12.571 1.143 19.429t-1.143 19.429l205.714 102.857q52.571-49.143 124.571-49.143z"></path></svg>';
break;
case 'print':
$link['link'] = 'javascript:window.print()';
$link['label'] = esc_html__( 'Print', 'shared-counts' );
$link['icon'] = '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="29.71875" height="32" viewBox="0 0 951 1024"><path d="M219.429 877.714h512v-146.286h-512v146.286zM219.429 512h512v-219.429h-91.429q-22.857 0-38.857-16t-16-38.857v-91.429h-365.714v365.714zM877.714 548.571q0-14.857-10.857-25.714t-25.714-10.857-25.714 10.857-10.857 25.714 10.857 25.714 25.714 10.857 25.714-10.857 10.857-25.714zM950.857 548.571v237.714q0 7.429-5.429 12.857t-12.857 5.429h-128v91.429q0 22.857-16 38.857t-38.857 16h-548.571q-22.857 0-38.857-16t-16-38.857v-91.429h-128q-7.429 0-12.857-5.429t-5.429-12.857v-237.714q0-45.143 32.286-77.429t77.429-32.286h36.571v-310.857q0-22.857 16-38.857t38.857-16h384q22.857 0 50.286 11.429t43.429 27.429l86.857 86.857q16 16 27.429 43.429t11.429 50.286v146.286h36.571q45.143 0 77.429 32.286t32.286 77.429z"></path></svg>';
$link['attr_title'] = esc_html__( 'Print this Page', 'shared-counts' );
$link['social_network'] = 'Print';
$link['social_action'] = 'Printed';
break;
case 'email':
$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 );
$link['label'] = esc_html__( 'Email', 'shared-counts' );
$link['icon'] = '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 1024 1024"><path d="M1024 405.714v453.714q0 37.714-26.857 64.571t-64.571 26.857h-841.143q-37.714 0-64.571-26.857t-26.857-64.571v-453.714q25.143 28 57.714 49.714 206.857 140.571 284 197.143 32.571 24 52.857 37.429t54 27.429 62.857 14h1.143q29.143 0 62.857-14t54-27.429 52.857-37.429q97.143-70.286 284.571-197.143 32.571-22.286 57.143-49.714zM1024 237.714q0 45.143-28 86.286t-69.714 70.286q-214.857 149.143-267.429 185.714-5.714 4-24.286 17.429t-30.857 21.714-29.714 18.571-32.857 15.429-28.571 5.143h-1.143q-13.143 0-28.571-5.143t-32.857-15.429-29.714-18.571-30.857-21.714-24.286-17.429q-52-36.571-149.714-104.286t-117.143-81.429q-35.429-24-66.857-66t-31.429-78q0-44.571 23.714-74.286t67.714-29.714h841.143q37.143 0 64.286 26.857t27.143 64.571z"></path></svg>';
$link['attr_title'] = 'Share via Email';
$link['class'] .= ' no-scroll';
$link['social_network'] = 'Email';
$link['social_action'] = 'Emailed';
break;
}
$data = '';
$link = apply_filters( 'shared_counts_link', $link, $id, $style );
$link_class = ! empty( $link['class'] ) ? implode( ' ', array_map( 'sanitize_html_class' , explode( ' ', $link['class'] ) ) ) : '';
$target = ! empty( $link['target'] ) ? ' target="' . esc_attr( $link['target'] ) . '" ' : '';
$rel = ! empty( $link['rel'] ) ? ' rel="' . esc_attr( $link['rel'] ) . '" ' : '';
$attr_title = ! empty( $link['attr_title'] ) ? ' title="' . esc_attr( $link['attr_title'] ) . '" ' : '';
$show_count = true;
$elements = [];
// Add classes.
$css_classes = [
'shared-counts-button',
sanitize_html_class( $link['type'] ),
];
$css_classes = array_merge( $css_classes, explode( ' ', $link['class'] ) );
if ( empty( $link['count'] ) || ( '1' === $options['total_only'] && 'included_total' !== $type ) ) {
$css_classes[] = 'shared-counts-no-count';
}
$css_classes = array_map( 'sanitize_html_class', $css_classes );
$css_classes = implode( ' ', array_filter( $css_classes ) );
// Prevent Pinterest JS from hijacking our button.
if ( 'pinterest' === $type ) {
$attr['pin-do'] = 'none';
}
// Social interaction data attributes - used for GA social tracking.
if ( apply_filters( 'shared_counts_social_tracking', true ) ) {
if ( ! empty( $link['social_network'] ) ) {
$attr['social-network'] = $link['social_network'];
}
if ( ! empty( $link['social_action'] ) ) {
$attr['social-action'] = $link['social_action'];
}
if ( ! empty( $link['url'] ) ) {
$attr['social-target'] = $link['url'];
}
}
// Add data attribues.
$attr = apply_filters( 'shared_counts_link_data', $attr, $link, $id );
if ( ! empty( $attr ) ) {
foreach ( $attr as $key => $val ) {
$data .= ' data-' . sanitize_html_class( $key ) . '="' . esc_attr( $val ) . '"';
}
}
// Add additional attributes
$additional_attr = apply_filters( 'shared_counts_additional_attr', array(), $link, $id, $style );
if( !empty( $additional_attr ) ) {
$attr_output = join( ' ', $additional_attr );
if( !empty( $data ) )
$attr_output = ' ' . $attr_output;
$data .= $attr_output;
}
// Determine if we should show the count.
if( empty( $options['count_source'] ) || 'none' === $options['count_source'] ) {
$show_count = false;
}
if ( 'false' === $show_empty && 0 == $link['count'] ) { //phpcs:ignore
$show_count = false;
}
if ( '1' === $options['total_only'] && 'included_total' !== $type ) {
$show_count = false;
}
// Hide Total Counts button if empty and "Hide Empty Counts" setting
// is enabled.
if ( 'included_total' === $type && 0 === absint( $link['count'] ) && 'false' === $show_empty ) {
continue;
}
// Build button output.
if ( 'included_total' === $type ) {
$elements['wrap_open'] = sprintf(
'<span class="%s"%s>',
$css_classes,
$data
);
$elements['wrap_close'] = '</span>';
} else {
$elements['wrap_open'] = sprintf(
'<a href="%s"%s%s%s class="%s"%s>',
esc_url( $link['link'] ),
$attr_title,
$target,
$rel,
$css_classes,
$data
);
$elements['wrap_close'] = '</a>';
}
$elements['icon'] = '<span class="shared-counts-icon">' . $link['icon'] . '</span>';
$elements['label'] = '<span class="shared-counts-label">' . $link['label'] . '</span>';
$elements['icon_label'] = '<span class="shared-counts-icon-label">' . $elements['icon'] . $elements['label'] . '</span>';
$elements['count'] = $show_count ? '<span class="shared-counts-count">' . $link['count'] . '</span>' : '';
$elements = apply_filters( 'shared_counts_output_elements', $elements , $link, $id );
$output .= $elements['wrap_open'] . $elements['icon_label'] . $elements['count'] . $elements['wrap_close'];
}
if ( true === $echo ) {
echo $output; // phpcs:ignore
} else {
return $output;
}
}
/**
* Display share counts via shortcode.
*
* @since 1.0.0
*
* @param array $atts Shortcode atts.
*
* @return string
*/
public function shortcode( $atts = [] ) {
$atts = shortcode_atts(
[
'location' => 'shortcode',
'style' => false,
'services' => '',
],
$atts,
'shared_counts'
);
// Don't show or include the share badges in the feed, since they won't
// display well.
if ( ! is_feed() ) {
return $this->display( esc_attr( $atts['location'] ), false, esc_attr( $atts['style'] ), get_the_ID(), $atts['services'] );
}
}
/**
* Admin bar stats.
*
* @since 1.3.0
*
* @param object $wp_admin_bar WordPress admin bar object.
*/
public function admin_bar( $wp_admin_bar ) {
if ( ! is_singular() ) {
return;
}
$settings = apply_filters(
'shared_counts_admin_bar',
[
'capability' => 'manage_options',
'round' => 2,
'show' => true,
'details' => true,
'refresh' => true,
]
);
if ( ! $settings['show'] || ! current_user_can( $settings['capability'] ) ) {
return;
}
if ( $settings['refresh'] && isset( $_GET['shared_counts_refresh'] ) ) { //phpcs:ignore
shared_counts()->core->counts( get_the_ID(), true, true );
}
$options = shared_counts()->admin->options();
if (
empty( $options['post_type'] ) ||
! is_singular( $options['post_type'] ) ||
get_post_meta( get_the_ID(), 'shared_counts_exclude', true )
) {
return;
}
$icon = '<svg xmlns="http://www.w3.org/2000/svg" width="16" viewBox="0 0 20 19" style="display:inline-block;vertical-align:middle;margin:0 6px 0 0;"><path fill="#a0a5aa" fill-rule="evenodd" d="M13.2438425,10.4284937 L11.6564007,10.4284937 L11.6564007,7.36477277 C11.6564007,6.99267974 11.6643076,6.65221463 11.6805867,6.34384253 C11.5829123,6.46244718 11.4610518,6.58616811 11.3159356,6.71593556 L10.6587263,7.25826114 L9.84709835,6.2601216 L11.8345402,4.64151695 L13.2438425,4.64151695 L13.2438425,10.4284937 Z M9.43314486,8.10105184 L7.9601216,8.10105184 L7.9601216,9.52988904 L6.89547044,9.52988904 L6.89547044,8.10105184 L5.42337742,8.10105184 L5.42337742,7.0401216 L6.89547044,7.0401216 L6.89547044,5.58756346 L7.9601216,5.58756346 L7.9601216,7.0401216 L9.43314486,7.0401216 L9.43314486,8.10105184 Z M18.1666332,0.000121602787 L1.83360997,0.000121602787 C0.822447184,0.000121602787 0.000121602787,0.8229123 0.000121602787,1.83360997 L0.000121602787,12.83361 C0.000121602787,13.8443076 0.822447184,14.6666332 1.83360997,14.6666332 L9.32477277,14.6666332 L13.8666332,18.3001216 C13.99361,18.401517 14.1461681,18.45361 14.3005867,18.45361 C14.4038425,18.45361 14.5075635,18.4303542 14.6047728,18.3833774 C14.8484937,18.2661681 15.0001216,18.0247728 15.0001216,17.7545402 L15.0001216,14.6666332 L18.1666332,14.6666332 C19.1773309,14.6666332 20.0001216,13.8443076 20.0001216,12.83361 L20.0001216,1.83360997 C20.0001216,0.8229123 19.1773309,0.000121602787 18.1666332,0.000121602787 Z"/></svg>';
$total = get_post_meta( get_the_ID(), 'shared_counts_total', true );
$total = ! empty( $total ) ? absint( $total ) : 0;
$updated = get_post_meta( get_the_ID(), 'shared_counts_datetime', true );
if ( $total >= 1000 ) {
$total = shared_counts()->core->round_count( $total, $settings['round'] );
}
if ( ! empty( $updated ) ) {
$updated = ' <span style="opacity:0.4;">(' . human_time_diff( $updated, time() ) . ')</span>';
}
$menu = [
[
'id' => 'shared_counts',
'title' => $icon . $total . $updated,
'href' => $settings['refresh'] ? esc_url( add_query_arg( 'shared_counts_refresh', '1' ) ) : false,
],
];
if ( $settings['details'] ) {
$counts = json_decode( get_post_meta( get_the_ID(), 'shared_counts', true ), true );
$details = [
[
'id' => 'shared_counts_facebook_total',
'parent' => 'shared_counts',
'title' => esc_html__( 'Facebook Total:', 'shared-counts' ) . ' ' . ( ! empty( $counts['Facebook']['total_count'] ) ? number_format( absint( $counts['Facebook']['total_count'] ) ) : '0' ),
],
[
'id' => 'shared_counts_facebook_likes',
'parent' => 'shared_counts',
'title' => esc_html__( 'Facebook Likes:', 'shared-counts' ) . ' ' . ( ! empty( $counts['Facebook']['like_count'] ) ? number_format( absint( $counts['Facebook']['like_count'] ) ) : '0' ),
],
[
'id' => 'shared_counts_facebook_shares',
'parent' => 'shared_counts',
'title' => esc_html__( 'Facebook Shares:', 'shared-counts' ) . ' ' . ( ! empty( $counts['Facebook']['share_count'] ) ? number_format( absint( $counts['Facebook']['share_count'] ) ) : '0' ),
],
[
'id' => 'shared_counts_facebook_comments',
'parent' => 'shared_counts',
'title' => esc_html__( 'Facebook Comments:', 'shared-counts' ) . ' ' . ( ! empty( $counts['Facebook']['comment_count'] ) ? number_format( absint( $counts['Facebook']['comment_count'] ) ) : '0' ),
],
[
'id' => 'shared_counts_twitter',
'parent' => 'shared_counts',
'title' => esc_html__( 'Twitter:', 'shared-counts' ) . ' ' . ( ! empty( $counts['Twitter'] ) ? number_format( absint( $counts['Twitter'] ) ) : '0' ),
],
[
'id' => 'shared_counts_pinterest',
'parent' => 'shared_counts',
'title' => esc_html__( 'Pinterest:', 'shared-counts' ) . ' ' . ( ! empty( $counts['Pinterest'] ) ? number_format( absint( $counts['Pinterest'] ) ) : '0' ),
],
[
'id' => 'shared_counts_yummly',
'parent' => 'shared_counts',
'title' => esc_html__( 'Yummly:', 'shared-counts' ) . ' ' . ( ! empty( $counts['Yummly'] ) ? number_format( absint( $counts['Yummly'] ) ) : '0' ),
],
];
if ( in_array( 'email', $options['included_services'], true ) ) {
$details[] = [
'id' => 'shared_counts_email',
'parent' => 'shared_counts',
'title' => esc_html__( 'Email:', 'shared-counts' ) . ' ' . number_format( absint( get_post_meta( get_the_ID(), 'shared_counts_email', true ) ) ),
];
}
$menu = array_merge( $menu, $details );
}
$menu = apply_filters( 'shared_counts_admin_bar_items', $menu, $settings, $options );
foreach ( $menu as $args ) {
$wp_admin_bar->add_node( $args );
}
}
}