-
Notifications
You must be signed in to change notification settings - Fork 4
/
developer-share-buttons.php
713 lines (643 loc) · 19.8 KB
/
developer-share-buttons.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
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
<?php
/**
* The Developer Share Buttons WordPress plugin
*
* @package dev-share-buttons
* /
/*
Plugin Name: Developer Share Buttons
Description: Share buttons with no CSS and no JavaScript
Version: 1.3.0
Author: Grant Richmond
Author URI: https://grant.codes/
License: GPL3
License URI: http://www.gnu.org/licenses/gpl-3.0.html
Text Domain: dev-share-buttons
*/
if ( ! class_exists( 'DeveloperShareButtons' ) ) {
register_activation_hook( __FILE__, array( 'DeveloperShareButtons', 'activation_hook' ) );
require_once dirname( __FILE__ ) . '/settings-class.php';
require_once dirname( __FILE__ ) . '/developer-share-buttons-widget.php';
/**
* The main class.
*/
class DeveloperShareButtons {
/**
* Stores the setting api object
*
* @var object
*/
private $settings_api;
/**
* Human readable name of plugin
*
* @var string
*/
static $name = 'Developer Share Buttons';
/**
* The plugin slug
*
* @var string
*/
static $slug = 'dev-share-buttons';
/**
* The plugin slug using underscores
*
* @var string
*/
static $slug_ = 'dev_share_buttons';
/**
* Whether or not to use rel="me" links
*
* @var null
*/
static $relme = null;
/**
* The version of the plugin
*
* @var string
*/
public static $version = '1.2.0';
/**
* Lets get this party started
*/
function __construct() {
$this->settings_api = new WeDevs_Settings_API;
add_action( 'admin_init', array( $this, 'admin_init' ) );
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'share_api_script' ) );
add_shortcode( static::$slug_, array( $this, 'shortcode' ) );
// Gutenberg stuff.
if ( function_exists( 'register_block_type' ) ) {
add_action( 'enqueue_block_editor_assets', array( $this, 'block_editor_assets' ) );
register_block_type( static::$slug . '/share', array(
'attributes' => array(
'className' => array(
'type' => 'string',
),
),
'render_callback' => array( $this, 'block_share' ),
) );
register_block_type( static::$slug . '/profiles', array(
'attributes' => array(
'className' => array(
'type' => 'string',
),
),
'render_callback' => array( $this, 'block_profiles' ),
) );
}
}
/**
* Just saves the default option values
*
* @return void
*/
public static function activation_hook() {
$defaults = array(
'default-services' => array(
'facebook' => 'facebook',
'twitter' => 'twitter',
'google' => 'google',
),
'share-text' => 'Share On',
);
add_option( static::$slug_ . '_options', $defaults );
}
/**
* Sets up the settings sections and fields
*
* @return void
*/
function admin_init() {
// Set the settings.
$this->settings_api->set_sections( $this->get_settings_sections() );
$this->settings_api->set_fields( $this->get_settings_fields() );
// Initialize settings.
$this->settings_api->admin_init();
}
/**
* Adds the option page
*
* @return void
*/
function admin_menu() {
add_options_page( static::$name, static::$name, 'delete_posts', static::$slug, array( $this, 'plugin_page' ) );
}
/**
* Loads the default css file if it is enabled
*
* @return void
*/
function enqueue_style() {
$options = get_option( static::$slug_ . '_options' );
$load_css = isset( $options['usecss'] ) ? $options['usecss'] : false;
if ( apply_filters( static::$slug_ . '_css', $load_css ) ) {
wp_enqueue_style( static::$slug, plugin_dir_url( __FILE__ ) . 'style.css', false, static::$version );
}
}
/**
* Load the block editor stuff
*
* @return void
*/
function block_editor_assets() {
$this->enqueue_style();
wp_enqueue_script( static::$slug . '-blocks', plugin_dir_url( __FILE__ ) . 'js/blocks.js', array( 'wp-blocks', 'wp-element', 'wp-components' ) );
}
/**
* Enqueues the share api script
*
* @return void
*/
function share_api_script() {
$share_api = false;
if ( apply_filters( static::$slug_ . '_share_api', $share_api ) ) {
wp_enqueue_script( static::$slug . '-share-api', plugin_dir_url( __FILE__ ) . 'js/share-api.js', false, static::$version, true );
}
}
/**
* Sets up the settings sections (tabs)
*
* @return array The sections
*/
function get_settings_sections() {
$sections = array(
array(
'id' => static::$slug_ . '_options',
'title' => __( 'Main Options', static::$slug ),
),
array(
'id' => static::$slug_ . '_urls',
'title' => __( 'Social Links', static::$slug ),
),
);
return $sections;
}
/**
* Returns all the settings fields
*
* @return array Settings fields
*/
function get_settings_fields() {
// Default stuff.
$settings_fields = array(
static::$slug_ . '_options' => array(
array(
'name' => 'default-services',
'label' => __( 'Default Services', static::$slug ),
'desc' => __( 'The default services that are enabled when calling the function', static::$slug ),
'type' => 'multicheck',
'options' => array(),
),
array(
'name' => 'share-text',
'label' => __( 'Share Text', static::$slug ),
'desc' => __( 'The text that appears in the links', static::$slug ),
'type' => 'text',
),
array(
'name' => 'usecss',
'label' => __( 'Use default css styles' ),
'type' => 'checkbox',
'default' => false,
),
),
static::$slug_ . '_urls' => array(),
);
// Loop through all services and add them as options.
foreach ( static::get_services() as $service_id => $service ) {
// Add the profile url field.
$settings_fields[ static::$slug_ . '_urls' ][] = array(
'name' => $service_id,
'label' => __( $service['title'], static::$slug ),
'desc' => __( 'Your ' . $service['title'] . ' profile link', static::$slug ),
'type' => 'url',
'default' => '',
);
// If service has a sharing url then make it an option to share with.
if ( $service['url_structure'] ) {
$settings_fields[ static::$slug_ . '_options' ][0]['options'][ $service_id ] = $service['title'];
}
}
// Add rel="me" option.
$settings_fields[ static::$slug_ . '_urls' ][] = array(
'name' => 'relme',
'label' => __( 'Add rel="me" attributes' ),
'type' => 'checkbox',
'default' => false,
);
return $settings_fields;
}
/**
* Creates the settings page
*
* @return void
*/
function plugin_page() {
echo '<div class="wrap">';
$this->settings_api->show_navigation();
$this->settings_api->show_forms();
echo '</div>';
}
/**
* The shortcode
*
* @param array $atts Shortcode attributes.
* @return string Html for the share links
*/
public static function shortcode( $atts ) {
extract( shortcode_atts(
array(
'services' => false,
'url' => false,
'title' => false,
'text' => false,
'image' => false,
),
$atts
) );
return static::get_buttons( $services );
}
/**
* Returns all the supported services included ones added by filters
*
* @return array Supported services
*/
public static function get_services() {
// Array of services as key => array( title => human readable title, url_structure => share url structure ).
$defaults = array(
// %1$s = url to share.
// %2$s = share title.
// %3$s = share text.
// %4$s = share image.
'facebook' => array(
'id' => 'facebook',
'title' => 'Facebook',
'url_structure' => 'https://www.facebook.com/sharer.php?u=%1$s',
'url_after_title' => false,
),
'twitter' => array(
'id' => 'twitter',
'title' => 'Twitter',
'url_structure' => 'https://twitter.com/intent/tweet?url=%1$s&text=%2$s',
'url_after_title' => false,
),
'reddit' => array(
'id' => 'reddit',
'title' => 'Reddit',
'url_structure' => 'https://reddit.com/submit?url=%1$s&title=%2$s',
'url_after_title' => false,
),
'linkedin' => array(
'id' => 'linkedin',
'title' => 'LinkedIn',
'url_structure' => 'https://www.linkedin.com/shareArticle?mini=true&url=%1$s',
'url_after_title' => false,
),
'stumbleupon' => array(
'id' => 'stumbleupon',
'title' => 'StumbleUpon',
'url_structure' => 'https://www.stumbleupon.com/submit?url=%1$s&title=%2$s',
'url_after_title' => false,
),
'pinterest' => array(
'id' => 'pinterest',
'title' => 'Pinterest',
'url_structure' => 'https://www.pinterest.com/pin/find/?url=%1$s',
'url_after_title' => false,
),
'instagram' => array(
'id' => 'instagram',
'title' => 'Instagram',
'url_structure' => false,
'url_after_title' => false,
),
'github' => array(
'id' => 'github',
'title' => 'GitHub',
'url_structure' => false,
'url_after_title' => false,
),
'mastodon' => array(
'id' => 'mastodon',
'title' => 'Mastodon',
'url_structure' => false,
'url_after_title' => false,
),
'medium' => array(
'id' => 'medium',
'title' => 'Medium',
'url_structure' => false,
'url_after_title' => false,
),
'messenger' => array(
'id' => 'messenger',
'title' => 'Messenger',
'url_structure' => false,
'url_after_title' => false,
),
'skype' => array(
'id' => 'skype',
'title' => 'Skype',
'url_structure' => false,
'url_after_title' => false,
),
'snapchat' => array(
'id' => 'snapchat',
'title' => 'Snapchat',
'url_structure' => false,
'url_after_title' => false,
),
'wechat' => array(
'id' => 'wechat',
'title' => 'WeChat',
'url_structure' => false,
'url_after_title' => false,
),
'whatsapp' => array(
'id' => 'whatsapp',
'title' => 'WhatsApp',
'url_structure' => false,
'url_after_title' => false,
),
'youtube' => array(
'id' => 'youtube',
'title' => 'YouTube',
'url_structure' => false,
'url_after_title' => false,
),
);
$services = apply_filters( DeveloperShareButtons::$slug_ . '_services', $defaults );
if ( is_array( $services ) ) {
return $services;
} else {
return $defaults;
}
}
/**
* Gets the `a` tag for sharing service
*
* @param string $service Service id.
* @param string $url The url you wish to share, defaults to `get_permalink()`.
* @param string $title The title of the item to share, defaults to `get_the_title()`.
* @param string $text The text that will appear before the service title, defaults to the value set on the option page.
* @param string $image The url of an image to share (only used by some services), defaults to the post thumbnail url.
* @return string|bool Return the created `a` tag or false if not found
*/
public static function get_link_html( $service, $url = '', $title = '', $text = '', $image = '' ) {
$services = static::get_services();
if ( isset( $services[ $service ] ) ) {
$service = $services[ $service ];
if ( $service['url_structure'] ) {
$options = get_option( static::$slug_ . '_options' );
$share_text = 'Share on ';
if ( isset( $options['share-text'] ) ) {
$share_text = trim( $options['share-text'] ) . ' ';
}
$css_class = static::$slug;
// If the page is not a proper post object fall back to defaults.
if ( is_singular() && get_post() ) {
if ( ! $title && ! $url ) {
$title = get_the_title();
}
if ( ! $url ) {
$url = get_permalink();
}
if ( ! $image && $image_id = get_post_thumbnail_id() ) {
$image_object = wp_get_attachment_image_src( $image_id, 'full' );
$image = $image_object[0];
}
} else if ( is_archive() ) {
$title = get_the_archive_title();
$url = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
} else {
$title = get_bloginfo( 'title' );
$url = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}
if ( $service['url_after_title'] && $title ) {
$title .= ' ' . $url;
}
$url = rawurlencode( $url );
$title = rawurlencode( $title );
$text = rawurlencode( $text );
$image = rawurlencode( $image );
$share_text = apply_filters( static::$slug_ . '_share_text', $share_text );
$after_text = apply_filters( static::$slug_ . '_after_share_text', '', $service );
$before_text = apply_filters( static::$slug_ . '_before_share_text', '', $service );
$url = sprintf( $service['url_structure'], $url, $title, $text, $image );
$html = sprintf( '<a target="_blank" href="%1$s" class="%2$s__item %2$s__item--%3$s">%5$s<span class="%2$s__text %2$s__text--%3$s">%4$s</span>%6$s</a> ', $url, $css_class, $service['id'], $share_text . $service['title'], $before_text, $after_text );
return $html;
}
} else {
return false;
}
}
/**
* Gets the html for the share buttons
*
* @param array $services Array of service ids to include, defaults to those set on the options page.
* @param sting $url The url to pass to `get_link_html()`.
* @param string $title The title to pass to `get_link_html()`.
* @param string $text The text to pass to `get_link_html()`.
* @param string $image The image url to pass to `get_link_html()`.
* @return string Html `div` containing all the share links
*/
public static function get_buttons( $services = false, $url = '', $title = '', $text = '', $image = '' ) {
if ( ! is_array( $services ) || ! $services ) {
$options = get_option( static::$slug_ . '_options' );
if ( isset( $options['default-services'] ) ) {
$services = $options['default-services'];
} else {
return false;
}
}
$html = '<div class="' . static::$slug . '" data-share-title="' . esc_attr( $title ) . '" data-share-text="' . esc_attr( $text ) . '">';
ob_start();
do_action( 'before_' . static::$slug_ );
$html .= ob_get_clean();
foreach ( $services as $service ) {
if ( $service_html = static::get_link_html( $service, $url, $title, $text, $image ) ) {
$html .= $service_html;
}
}
ob_start();
do_action( 'after_' . static::$slug_ );
$html .= ob_get_clean();
$html .= '</div>';
return $html;
}
/**
* Wrapper to echo the output of `get_buttons()`
*
* @param array $services Array of service ids to include, defaults to those set on the options page.
* @param sting $url The url to pass to `get_link_html()`.
* @param string $title The title to pass to `get_link_html()`.
* @param string $text The text to pass to `get_link_html()`.
* @param string $image The image url to pass to `get_link_html()`.
* @return bool Success or no?
*/
public static function the_buttons( $services = false, $url = false, $title = false, $text = false, $image = false ) {
if ( $html = static::get_buttons( $services, $url, $title, $text, $image ) ) {
echo $html;
return true;
} else {
return false;
}
}
/**
* Gets an array of the saved profile urls
*
* @return array Array of urls with url and title
*/
public static function get_profile_links() {
$options = get_option( static::$slug_ . '_urls' );
$links = array();
foreach ( static::get_services() as $service_id => $service ) {
if ( isset( $options[ $service_id ] ) && $options[ $service_id ] ) {
$links[ $service_id ] = array(
'id' => $service_id,
'title' => $service['title'],
'url' => $options[ $service_id ],
);
}
}
return $links;
}
/**
* Gets all the saved profile urls in a nice html format
*
* @return bool Success or no?
*/
public static function get_the_profile_links() {
if ( $links = static::get_profile_links() ) {
$html = '<div class="' . static::$slug . '-profiles">';
foreach ( $links as $service_id => $service_link ) {
$attributes = 'class="' . static::$slug . '-link ' . static::$slug . '-link--' . $service_id .'" ';
$attributes .= 'href="' . $service_link['url'] . '" ';
if ( static::is_rel_me() ) {
$attributes .= 'rel="me" ';
}
$after_text = apply_filters( static::$slug_ . '_after_profile_text', '', $service_link );
$before_text = apply_filters( static::$slug_ . '_before_profile_text', '', $service_link );
$html .= '<a ' . $attributes . '>' . $after_text . '<span class="' . static::$slug . '-link__text ' . static::$slug . '-link--' . $service_id .'__text">' . $service_link['title'] . '</span>' . $before_text . '</a> ';
}
$html .= '</div>';
return $html;
} else {
return false;
}
}
/**
* Echos all the saved profile urls in a nice html format
*
* @return bool Success or no?
*/
public static function the_profile_links() {
$html = static::get_the_profile_links();
if ( $html ) {
echo $html;
return true;
}
return false;
}
/**
* Checks to see if the rel="me" option is enabled
*
* @return boolean
*/
public static function is_rel_me() {
if ( isset( static::$relme ) ) {
return static::$relme;
} else {
$options = get_option( static::$slug_ . '_urls' );
$relme = $options['relme'];
if ( 'off' === $relme ) {
$relme = false;
} elseif ( 'on' === $relme ) {
$relme = true;
}
static::$relme = $relme;
return $relme;
}
}
/**
* Registers the widget
*
* @return void
*/
public static function widget() {
register_widget( 'Dev_Share_Buttons_Widget' );
register_widget( 'Dev_Share_Buttons_Profiles_Widget' );
}
/**
* Basic wrapper to display the share buttons block
*
* @param array $attributes Gutenberg block attributes.
* @return string Html block
*/
function block_share( $attributes ) {
$html = get_dev_share_buttons();
if ( ! $html && is_admin() ) {
return '<p class="error">No share services found</p>';
}
if ( $html ) {
$class = 'wp-block-' . static::$slug . '-profiles';
if ( $attributes['className'] ) {
$class .= ' ' . $attributes['className'];
}
$html = '<div class="' . $class . '">' . $html . '</div>';
}
return $html;
}
/**
* Basic wrapper to display the social profiles block
*
* @param array $attributes Gutenberg block attributes.
* @return string Html block
*/
function block_profiles( $attributes ) {
$html = get_the_dev_profile_links();
if ( ! $html && is_admin() ) {
return '<p class="error">No social profiles found</p>';
}
if ( $html ) {
$class = 'wp-block-' . static::$slug . '-profiles';
if ( $attributes['className'] ) {
$class .= ' ' . $attributes['className'];
}
$html = '<div class="' . $class . '">' . $html . '</div>';
}
return $html;
}
}
// Get the show on the road.
$dev_share_buttons = new DeveloperShareButtons();
// Set up some wrapper functions for easier access.
if ( ! function_exists( 'get_dev_share_buttons' ) ) {
function get_dev_share_buttons( $services = false, $url = '', $title = '', $text = '', $image = '' ) {
return DeveloperShareButtons::get_buttons( $services, $url, $title, $text, $image );
}
}
if ( ! function_exists( 'the_dev_share_buttons' ) ) {
function the_dev_share_buttons( $services = false, $url = '', $title = '', $text = '', $image = '' ) {
DeveloperShareButtons::the_buttons( $services, $url, $title, $text, $image );
}
}
if ( ! function_exists( 'get_dev_profile_links' ) ) {
function get_dev_profile_links() {
return DeveloperShareButtons::get_profile_links();
}
}
if ( ! function_exists( 'the_dev_profile_links' ) ) {
function the_dev_profile_links() {
DeveloperShareButtons::the_profile_links();
}
}
if ( ! function_exists( 'get_the_dev_profile_links' ) ) {
function get_the_dev_profile_links() {
return DeveloperShareButtons::get_the_profile_links();
}
}
}