forked from DLXPlugins/highlight-and-share
-
Notifications
You must be signed in to change notification settings - Fork 0
/
highlight-and-share.php
executable file
·1048 lines (918 loc) · 39.1 KB
/
highlight-and-share.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
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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
Plugin Name: Highlight and Share
Plugin URI: http://wordpress.org/extend/plugins/highlight-and-share/
Description: Highlight text and share via Twitter or Facebook
Author: ronalfy
Version: 2.1.2
Requires at least: 4.4
Author URI: http://www.ronalfy.com
Contributors: ronalfy
Text Domain: highlight-and-share
Domain Path: /languages
Recommended pre-requisite plugin - https://wordpress.org/plugins/better-font-awesome/
WordPress SEO - Twitter/Facebook OpenGraph data - https://wordpress.org/plugins/wordpress-seo/
*/
class Highlight_And_Share {
private static $instance = null;
private $options = false;
private $errors = false;
/**
* Return an instance of the class
*
* Return an instance of the Highlight and Share Class.
*
* @since 1.0.0
* @access public static
*
* @return Highlight_And_Share class instance.
*/
public static function get_instance() {
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
} //end get_instance
/**
* Class constructor.
*
* Initialize plugin and load text domain for internationalization
*
* @since 1.0.0
* @access private
*
*/
private function __construct() {
add_action( 'init', array( $this, 'init' ), 9 );
add_action( 'wp', array( $this, 'wp_loaded' ), 15 );
// Get errors for email
$this->errors[ 'could_not_send' ] = esc_html__( 'Could not send the e-mail', 'highlight-and-share' );
$this->errors[ 'invalid_email' ] = esc_html__( 'Not a valid e-mail address', 'highlight-and-share' );
$this->errors[ 'email_sent' ] = esc_html__( 'Your email has been sent', 'highlight-and-share' );
$this->errors[ 'nonce_invalid' ] = esc_html__( 'Could not process your request', 'highlight-and-share' );
$this->errors[ 'name' ] = esc_html__( 'You must supply a name', 'highlight-and-share' );
//* Localization Code */
load_plugin_textdomain( 'highlight-and-share', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
add_action( 'template_redirect', array( $this, 'maybe_show_email' ) );
// Ajax for form submissions
add_action( 'wp_ajax_has_form_submission', array( $this, 'ajax_send_has_email' ) );
add_action( 'wp_ajax_nopriv_has_form_submission', array( $this, 'ajax_send_has_email' ) );
} //end constructor
public function ajax_send_has_email() {
check_ajax_referer( 'has_email_form_submission', '_ajax' );
$email = trim( urldecode( $_POST[ 'email' ] ) );
$name = trim( urldecode( $_POST[ 'name' ] ) );
$url = urldecode( $_POST[ 'url' ] );
$title = urldecode( $_POST[ 'title' ] );
$return = array();
if ( empty( $name ) ) {
$return[ 'error' ] = $this->get_error( 'name' );
die( json_encode( $return ) );
}
if ( ! is_email( $email ) ) {
$return[ 'error' ] = $this->get_error( 'invalid_email' );
die( json_encode( $return ) );
}
$message = sprintf( '%s wants to share a link with you.', esc_html( $name ) ) . "\n\n";
$message .= sprintf( '%s', esc_html( $title ) ) . "\n\n";
$message .= sprintf( '%s', esc_html( $url ) ) . "\n\n";
error_log( $message );
wp_mail( $email, __( 'Someone wants to share a link with you', 'highlight-and-share' ), $message );
die( json_encode( array() ) );
}
/**
* Show the e-mail template
*
* Show the e-mail template
*
* @since 6.0.0
* @access public
*
*/
public function maybe_show_email() {
if ( isset( $_GET[ 'action' ] ) && 'has_email' == $_GET[ 'action' ] && isset( $_GET[ 'nonce' ] ) ) {
if ( ! wp_verify_nonce( $_GET[ 'nonce' ], 'has_email_option' ) ) {
return;
}
?>
<html>
<head>
<link rel='stylesheet' href='<?php echo esc_url( get_stylesheet_uri() ); ?>' type='text/css' media='all' />
<script src="<?php echo esc_url( includes_url( '/js/jquery/jquery.js' ) ); ?>"></script>
<style>
body {
text-align: center;
}
</style>
<script>
jQuery( document ).ready( function( $ ) {
$( '#has_form' ).submit( function( e ) {
e.preventDefault();
ajax_object = {
action : 'has_form_submission',
_ajax : $( '#_has_email_submission_nonce' ).val(),
title : encodeURIComponent( $( '#title' ).val() ),
url : encodeURIComponent( $( '#url' ).val() ),
name : encodeURIComponent( $( '#your_name' ).val() ),
email : encodeURIComponent( $( '#your_email' ).val() )
};
$.post( '<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>', ajax_object, function( response ) {
if ( response.error ) {
alert( response.error );
return;
}
$( '#has_form' ).hide();
$( 'body' ).append( '<p>E-mail sent</p>' );
}, 'json' );
} );
} );
</script>
</head>
<body>
<main>
<article>
<form method="POST" action="" id="has_form">
<?php
wp_nonce_field( 'has_email_form_submission', '_has_email_submission_nonce', false, true );
?>
<div id="form-wrapper">
<div>
<p><input type="text" name="your_name" id="your_name" placeholder="<?php echo esc_attr( __( 'Your Name', 'highlight-and-share' ) ) ?>" /></p>
</div>
<div>
<p><input type="email" name="your_email" id="your_email" placeholder="<?php echo esc_attr( __( 'E-mail Address', 'highlight-and-share' ) ) ?>" /></p>
</div>
<input type="hidden" id="title" value="<?php echo esc_attr( urldecode( $_GET[ 'title' ] ) ); ?>" />
<input type="hidden" id="url" value="<?php echo esc_attr( urldecode( $_GET[ 'url' ] ) ); ?>" />
<div>
<p><input type="submit" name="submit" id="submit" value="<?php echo esc_attr( __( 'Submit', 'highlight-and-share' ) ); ?>" />
</p>
</div>
</div><!-- #form-wrapper -->
</form>
</article>
</main>
</body>
</html>
<?php
exit();
}
}
/**
* Get an error message
*
* Get an error message based on a passed key
*
* @since 6.0.0
* @access public
*
* @param string key to retrieve
* @return string key value
*/
public function get_error( $key ) {
if ( isset( $this->errors[ $key ] ) ) {
return $this->errors[ $key ];
} else {
return '';
}
}
/**
* Main plugin initialization
*
* Initialize admin menus, options,and scripts
*
* @since 1.0.0
* @access public
*
* @see __construct
*
*/
public function init() {
$settings = $this->get_plugin_options();
//Admin Settings
add_action( 'admin_menu', array( $this, 'add_admin_menu' ) );
add_action( 'admin_init', array( $this, 'init_admin_settings' ) );
//Plugin settings
add_filter( 'plugin_action_links_' . plugin_basename(__FILE__) , array( $this, 'add_settings_link' ) );
} //end init
public function wp_loaded() {
//Disable if on a feed
if ( is_feed() ) return;
$settings = $this->get_plugin_options();
//Skip loading if both twitter/facebook are turned off
$show_facebook = (bool)apply_filters( 'has_show_facebook', $settings[ 'show_facebook' ] );
$show_twitter = (bool)apply_filters( 'has_show_twitter', $settings[ 'show_twitter' ] );
$show_linkedin = (bool)apply_filters( 'has_show_linkedin', $settings[ 'show_linkedin' ] );
$show_pinterest = (bool)apply_filters( 'has_show_pinterest', $settings[ 'show_pinterest' ] );
$show_email = (bool)apply_filters( 'has_show_email', $settings[ 'show_email' ] );
if ( ! $show_facebook && ! $show_twitter && ! $show_linkedin && ! $show_pinterest && ! $show_email ) return;
//Disable if mobile
if ( wp_is_mobile() ) {
if ( (bool)apply_filters( 'has_enable_mobile', true ) ) {
add_action( 'wp_enqueue_scripts', array( $this, 'add_scripts_mobile' ) );
} else {
return;
}
}
//Load scripts
add_action( 'wp_enqueue_scripts', array( $this, 'add_scripts' ) );
//Load content area
if ( apply_filters( 'has_enable_content', (bool)$settings[ 'enable_content' ] ) ) {
add_filter( 'the_content', array( $this, 'content_area' ) );
}
if ( apply_filters( 'has_enable_excerpt', (bool)$settings[ 'enable_excerpt' ] ) ) {
add_filter( 'the_excerpt', array( $this, 'excerpt_area' ) );
}
}
/**
* Add a class and data attribute around the main content.
*
* Add a class and data attribute around the main content.
*
* @since 1.0.0
* @access public
*
* @see init
*
* @param string $content Main post content
* @return string $content Modified
*/
public function content_area( $content ) {
global $post;
if ( !in_the_loop( ) ) return $content;
if ( !is_object( $post ) ) return $content;
$post_id = $post->ID;
$url = $this->get_content_url( $post_id );
$title = get_the_title( $post_id );
$content = sprintf( '<div class="has-content-area" data-url="%s" data-title="%s">%s</div>', esc_url( $url ), esc_attr( $title ), $content );
return $content;
}
/**
* Add a class and data attribute around the main excerpts.
*
* Add a class and data attribute around the main excerpts.
*
* @since 1.0.0
* @access public
*
* @see init
*
* @param string $content Main excerpt content
* @return string $content Modified
*/
public function excerpt_area( $content ) {
global $post;
if ( !in_the_loop( ) ) return $content;
if ( !is_object( $post ) ) return $content;
$post_id = $post->ID;
$url = $this->get_content_url( $post_id );
$title = get_the_title( $post_id );
$content = sprintf( '<div class="has-excerpt-area" data-url="%s" data-title="%s">%s</div>', esc_url( $url ), esc_attr( $title ), $content );
return $content;
}
/**
* Retrieve a post's URL
*
* Retrieve a post's URL (may be shortened)
*
* @since 1.1
* @access private
*
*
* @param int $post_id Post ID to retrieve the URL for
* @return string $url URL to the post
*/
private function get_content_url( $post_id ) {
$settings = $this->get_plugin_options();
$enable_shortlinks = isset( $settings[ 'shortlinks' ] ) ? (bool)$settings[ 'shortlinks' ] : false;
$url = get_permalink( $post_id );
if ( $enable_shortlinks ) {
$url = wp_get_shortlink( $post_id );
}
return apply_filters( 'has_content_url', $url, $post_id );
}
/**
* Add a settings link to the plugin's options.
*
* Add a settings link on the WordPress plugin's page.
*
* @since 1.0.0
* @access public
*
* @see init
*
* @param array $links Array of plugin options
* @return array $links Array of plugin options
*/
public function add_settings_link( $links ) {
$settings_link = sprintf( '<a href="%s">%s</a>', esc_url( admin_url( 'options-general.php?page=highlight-and-share' ) ), _x( 'Settings', 'Plugin settings link on the plugins page', 'highlight-and-share' ) );
array_unshift($links, $settings_link);
return $links;
}
/**
* Load plugin scripts and styles
*
* Enqueue scripts/styles and provide localization
*
* @since 1.0.0
* @access public
*
* @see init
*
*/
public function add_scripts() {
$deps = array( 'jquery' );
if ( wp_is_mobile() && apply_filters( 'has_enable_mobile', true ) ) {
$deps[] = 'jquery.mobile';
}
$main_script_uri = $this->get_plugin_url( 'js/highlight-and-share.js' );
wp_enqueue_script( 'highlight-and-share', $main_script_uri, $deps, '20170112', true );
/**Build JSON Object**/
$settings = $this->get_plugin_options();
$json_arr = array();
//Facebook
$json_arr[ 'show_facebook' ] = (bool)apply_filters( 'has_show_facebook', $settings[ 'show_facebook' ] );
//Twitter
$json_arr[ 'show_twitter' ] = (bool)apply_filters( 'has_show_twitter', $settings[ 'show_twitter' ] );
//Linked In
$json_arr[ 'show_linkedin' ] = (bool)apply_filters( 'has_show_linkedin', $settings[ 'show_linkedin' ] );
//Pinterest
$json_arr[ 'show_pinterest' ] = (bool)apply_filters( 'has_show_pinterest', $settings[ 'show_pinterest' ] );
//Email
$json_arr[ 'show_email' ] = (bool)apply_filters( 'has_show_email', $settings[ 'show_email' ] );
//Twitter Username
$json_arr[ 'twitter_username' ] = trim( sanitize_text_field( apply_filters( 'has_twitter_username', $settings[ 'twitter' ] ) ) );
//Override the filter if no username is present for twitter
if ( empty( $json_arr[ 'twitter_username' ] ) ) {
$json_arr[ 'twitter_username' ] = '';
}
//Add mobile
if ( wp_is_mobile() ) {
$json_arr[ 'mobile' ] = true;
} else {
$json_arr[ 'mobile' ] = false;
}
//Content areas
$classes = apply_filters( 'has_js_classes', $settings[ 'js_content' ] ); //Pass comma separated values (e.g., entry-content,type-post,type-page)
$classes = explode( ',', $classes );
if ( apply_filters( 'has_enable_content', (bool)$settings[ 'enable_content' ] ) ) {
$classes[] = 'has-content-area';
}
if ( apply_filters( 'has_enable_excerpt', (bool)$settings[ 'enable_excerpt' ] ) ) {
$classes[] = 'has-excerpt-area';
}
foreach( $classes as $index => &$string ) {
$string = trim( $string ); //Trim
if ( empty( $string ) ) {
unset( $classes[ $index ] );
continue;
}
$string = trim( esc_js( '.' . $string ) ); //Get in class format (.%s) and trim just in case
}
$ids = (array)apply_filters( 'has_js_ids', array() ); //Pass array of jQuery ID elements (without the #)
foreach( $ids as $index => &$string ) {
$string = trim( $string );
if ( empty( $string ) ) continue;
$string = trim( esc_js( '#' . $string ) ); //Get in ID format (#%s) and trim just in case
}
$elements = (array)apply_filters( 'has_js_elements', array() ); //Pass array of jQuery HTML elements (e.g., blockquote, article)
foreach( $elements as $index => &$string ) {
$string = trim( $string );
if ( empty( $string ) ) continue;
$string = trim( esc_js( $string ) );
}
$content = array_merge( $classes, $ids, $elements );
$json_arr[ 'content' ] = apply_filters( 'has_js_selectors', implode(',', $content ), $content, $classes, $ids, $elements );
//Text to display
$json_arr[ 'tweet_text' ] = apply_filters( 'has_twitter_text', _x( 'Tweet', 'Twitter share text', 'highlight-and-share' ) );
$json_arr[ 'facebook_text' ] = apply_filters( 'has_facebook_text', _x( 'Share', 'Facebook share text', 'highlight-and-share' ) );
$json_arr[ 'linkedin_text' ] = apply_filters( 'has_linkedin_text', _x( 'LinkedIn', 'LinkedIn share text', 'highlight-and-share' ) );
$json_arr[ 'pinterest_text' ] = apply_filters( 'has_pinterest_text', _x( 'Pinterest', 'Pinterest share text', 'highlight-and-share' ) );
$json_arr[ 'email_text' ] = apply_filters( 'has_email_text', _x( 'E-mail', 'E-mail share text', 'highlight-and-share' ) );
//Icons
$json_arr[ 'icons' ] = apply_filters( 'has_icons', $settings[ 'icons' ] );
// Facebook API Key
$json_arr[ 'facebook_app_id' ] = isset( $settings[ 'facebook_app_id' ] ) ? absint( $settings[ 'facebook_app_id' ] ) : 0;
// URL for e-mail
$url_email = add_query_arg( array(
'action' => 'has_email',
'nonce' => wp_create_nonce( 'has_email_option' ),
'title' => '%title%',
'url' => '%url%'
),
home_url()
);
$json_arr[ 'email_url' ] = esc_url_raw( $url_email );
//Localize
wp_localize_script( 'highlight-and-share', 'highlight_and_share', $json_arr );
//Add CSS
if ( apply_filters( 'has_load_css', true ) ) {
wp_enqueue_style( 'highlight-and-share', $this->get_plugin_url( 'css/highlight-and-share.css' ), array(), '20160521', 'all' );
}
}
/**
* Load mobile scripts
*
* Enqueue scripts/styles to enable mobile events
*
* @since 1.0.0
* @access public
*
* @see init
*
*/
public function add_scripts_mobile() {
$main_script_uri = $this->get_plugin_url( 'js/jquery.mobile.custom.min.js' );
if ( defined( 'SCRIPT_DEBUG' ) ) {
if ( SCRIPT_DEBUG == true ) {
$main_script_uri = $this->get_plugin_url( 'js/jquery.mobile.custom.js' );
}
}
wp_enqueue_script( 'jquery.mobile', $main_script_uri, array( 'jquery' ), '1.4.5', true );
}
/**
* Initialize options page
*
* Create plugin options page and callback
*
* @since 1.0.0
* @access public
*
* @see init
*
*/
public function add_admin_menu() {
add_options_page( _x( 'Highlight and Share', 'Plugin Name - Settings Page Title', 'highlight-and-share' ), _x( 'Highlight and Share', 'Plugin Name - Menu Item', 'highlight-and-share' ), 'manage_options', 'highlight-and-share', array( $this, 'options_page' ) );
}
/**
* Output options page HTML.
*
* Output option page HTML and fields/sections.
*
* @since 1.0.0
* @access public
*
* @see add_admin_menu
*
*/
public function options_page() {
?>
<div class="wrap">
<h2><?php echo esc_html( _x( 'Highlight and Share', 'Plugin Name - Settings Page Title', 'highlight-and-share' ) ); ?></h2>
<form action="<?php echo esc_url( admin_url( 'options.php' ) ); ?>" method="POST">
<?php settings_fields( 'highlight-and-share' ); ?>
<?php do_settings_sections( 'highlight-and-share' ); ?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
/**
* Initialize options
*
* Initialize page settings, fields, and sections and their callbacks
*
* @since 1.0.0
* @access public
*
* @see init
*
*/
public function init_admin_settings() {
register_setting( 'highlight-and-share', 'highlight-and-share', array( $this, 'sanitization' ) );
add_settings_section( 'has-display', _x( 'Display', 'plugin settings heading' , 'highlight-and-share' ), array( $this, 'settings_section' ), 'highlight-and-share' );
add_settings_section( 'has-config', _x( 'Content', 'plugin settings heading' , 'highlight-and-share' ), array( $this, 'settings_section' ), 'highlight-and-share' );
add_settings_section( 'has-twitter', _x( 'Twitter Settings', 'plugin settings heading' , 'highlight-and-share' ), array( $this, 'settings_section' ), 'highlight-and-share' );
add_settings_section( 'has-facebook', _x( 'Facebook Settings', 'plugin settings heading' , 'highlight-and-share' ), array( $this, 'settings_section' ), 'highlight-and-share' );
add_settings_section( 'has-linkedin', _x( 'LinkedIn Settings', 'plugin settings heading' , 'highlight-and-share' ), array( $this, 'settings_section' ), 'highlight-and-share' );
add_settings_section( 'has-pinterest', _x( 'Pinterest Settings', 'plugin settings heading' , 'highlight-and-share' ), array( $this, 'settings_section' ), 'highlight-and-share' );
add_settings_section( 'has-email', _x( 'E-mail Settings', 'plugin settings heading' , 'highlight-and-share' ), array( $this, 'settings_section' ), 'highlight-and-share' );
add_settings_section( 'has-shortlink', _x( 'Post URL Settings', 'plugin settings heading' , 'highlight-and-share' ), array( $this, 'settings_section' ), 'highlight-and-share' );
add_settings_section( 'has-advanced', _x( 'Advanced', 'plugin settings heading' , 'highlight-and-share' ), array( $this, 'settings_section' ), 'highlight-and-share' );
add_settings_field( 'hightlight-and-share-display-enable', __( 'Show Icons Only', 'highlight-and-share' ), array( $this, 'add_settings_field_display_enable' ), 'highlight-and-share', 'has-display', array( 'desc' => __( 'Show icons only. Recommended if you choose more than two options to show.', 'highlight-and-share' ) ) );
add_settings_field( 'hightlight-and-share-content-enable', __( 'Add to Post Content', 'highlight-and-share' ), array( $this, 'add_settings_field_content_enable' ), 'highlight-and-share', 'has-config', array( 'desc' => __( 'Would you like to add sharing to the main content areas?', 'highlight-and-share' ) ) );
add_settings_field( 'hightlight-and-share-excerpt-enable', __( 'Add to Excerpt Content', 'highlight-and-share' ), array( $this, 'add_settings_field_excerpt_enable' ), 'highlight-and-share', 'has-config', array( 'desc' => __( 'Would you like to add sharing to the excerpts?', 'highlight-and-share' ) ) );
add_settings_field( 'hightlight-and-share-twitter-enable', __( 'Show Twitter Option', 'highlight-and-share' ), array( $this, 'add_settings_field_twitter_enable' ), 'highlight-and-share', 'has-twitter', array( 'desc' => __( 'Would you like to enable sharing via Twitter?', 'highlight-and-share' ) ) );
add_settings_field( 'hightlight-and-share-linkedin-enable', __( 'Show LinkedIn Option', 'highlight-and-share' ), array( $this, 'add_settings_field_linkedin_enable' ), 'highlight-and-share', 'has-linkedin', array( 'desc' => __( 'Would you like to enable sharing via LinkedIn?', 'highlight-and-share' ) ) );
add_settings_field( 'hightlight-and-share-pinterest-enable', __( 'Show Pinterest Option', 'highlight-and-share' ), array( $this, 'add_settings_field_pinterest_enable' ), 'highlight-and-share', 'has-pinterest', array( 'desc' => __( 'Would you like to enable sharing via Pinterest?', 'highlight-and-share' ) ) );
add_settings_field( 'hightlight-and-share-email-enable', __( 'Show E-mail Option', 'highlight-and-share' ), array( $this, 'add_settings_field_email_enable' ), 'highlight-and-share', 'has-email', array( 'desc' => __( 'Would you like to enable sharing via E-mail?', 'highlight-and-share' ) ) );
add_settings_field( 'hightlight-and-share-twitter-handle', __( 'Twitter Username', 'highlight-and-share' ), array( $this, 'add_settings_field_twitter' ), 'highlight-and-share', 'has-twitter', array( 'label_for' => 'hightlight-and-share-twitter-handle', 'desc' => __( 'Enter Your Twitter Username', 'highlight-and-share' ) ) );
add_settings_field( 'hightlight-and-share-facebook-enable', __( 'Show Facebook Option', 'highlight-and-share' ), array( $this, 'add_settings_field_facebook_enable' ), 'highlight-and-share', 'has-facebook', array( 'desc' => __( 'Would you like to enable sharing via Facebook?', 'highlight-and-share' ) ) );
add_settings_field( 'hightlight-and-share-facebook-api', __( 'Facebook App ID', 'highlight-and-share' ), array( $this, 'add_settings_field_facebook_api' ), 'highlight-and-share', 'has-facebook', array( 'label_for' => 'hightlight-and-share-facebook-api', 'desc' => __( 'A Facebook App ID allows you to highlight text and share it.', 'highlight-and-share' ) ) );
add_settings_field( 'hightlight-and-share-shortlink-enable', __( 'Shortlinks', 'highlight-and-share' ), array( $this, 'add_settings_field_shortlink_enable' ), 'highlight-and-share', 'has-shortlink', array( 'desc' => __( 'Please decide if you would like to use the default post URL or a shortened version.', 'highlight-and-share' ) ) );
add_settings_field( 'hightlight-and-share-js-content', _x( 'jQuery classes', 'Label - Where in the HTML document to search for text to capture', 'highlight-and-share' ), array( $this, 'add_settings_field_js_content' ), 'highlight-and-share', 'has-advanced', array( 'label_for' => 'hightlight-and-share-js-content', 'desc' => __( 'Enter jQuery classes to search for in the HTML. You must comma-separate classes (e.g., entry-content,post,page).', 'highlight-and-share' ) ) );
}
/**
* Output settings HTML
*
* Output any HTML required to go into a settings section
*
* @since 1.0.0
* @access public
*
* @see init_admin_settings
*
*/
public function settings_section() {
}
/**
* Sanitize options before they are saved.
*
* Sanitize and prepare error messages when saving options.
*
* @since 1.0.0
* @access public
*
* @see init_admin_settings
*
* @param array $input {
@type string $js_content Content to be parsed via Javascript. Default 'entry-content'.
@type string $twitter Twitter username. Default ''.
@type bool $show_twitter Whether to show twitter share option. Default true.
@type bool $show_facebook Whether to show facebook share option. Default true
}
* @return array Sanitized array of options
*/
public function sanitization( $input = array() ) {
$output = get_option( 'highlight-and-share' );
//Check if settings are being initialized for the first time
if ( false === $output ) {
//No settings have been saved yet and we're being supplied with defaults
foreach( $input as $key => &$value ) {
if ( is_bool( $value ) ) continue;
$value = sanitize_text_field( $value );
}
return $input;
}
//Settings are being saved. Update.
foreach( $input as $key => $value ) {
if ( 'twitter' == $key ) {
$twitter_username = trim( $value );
if ( empty( $twitter_username ) ) {
$output[ $key ] ='';
} elseif ( !preg_match( '/^[a-zA-Z0-9_]{1,15}$/', $twitter_username ) ) {
add_settings_error( 'highlight-and-share', 'invalid_twitter', _x( 'You have entered an incorrect Twitter username', 'Twitter username error', 'highlight-and-share' ) );
} else {
$output[ $key ] = sanitize_text_field( $value );
}
} elseif( 'facebook_app_id' == $key ) {
$app_id = absint( $value );
if ( empty( $app_id ) || 0 === $app_id ) {
$output[ $key ] = '';
} else {
$output[ $key ] = $app_id;
}
} elseif( 'js_content' == $key ) {
$js_content = trim( $value );
if( empty( $js_content ) || preg_match( '/[-_0-9a-zA-Z]+(,[-_0-9a-zA-Z]+)*$/', $js_content ) ) {
$output[ $key ] = sanitize_text_field( $js_content );
} else {
add_settings_error( 'highlight-and-share', 'invalid_twitter', _x( 'You must enter valid comma-separated values for the content.', 'Invalid comma-separated values', 'highlight-and-share' ) );
}
} elseif( ( 'show_twitter' || 'show_facebook' || 'enable_content' || 'enable_excerpt' || 'shortlinks' || 'icons' ) == $key ) {
if ( $input[ $key ] == 'on' ) {
$output[ $key ] = true;
} else {
$output[ $key ] = false;
}
}
}
return $output;
}
/**
* Add JavaScript content setting.
*
* Output text input HTML for JavaScript content classes.
*
* @since 1.0.0
* @access public
*
* @see init_admin_settings
*
* @param array $args {
@type string $label_for Settings label and ID.
@type string $desc Description for the setting.
}
*/
public function add_settings_field_js_content( $args = array() ) {
$settings = $this->get_plugin_options();
$js_content = isset( $settings[ 'js_content' ] ) ? $settings[ 'js_content' ] : '';
printf( '<p>%s</p>', esc_html( $args[ 'desc' ] ) );
printf( '<input id="%s" type="text" name="highlight-and-share[js_content]" value="%s" />', esc_attr( $args[ 'label_for' ] ), esc_attr( $js_content ) );
}
/**
* Add Twitter username setting.
*
* Output text input HTML for the Twitter username.
*
* @since 1.0.0
* @access public
*
* @see init_admin_settings
*
* @param array $args {
@type string $label_for Settings label and ID.
@type string $desc Description for the setting.
}
*/
public function add_settings_field_twitter( $args = array() ) {
$settings = $this->get_plugin_options();
$js_content = isset( $settings[ 'twitter' ] ) ? $settings[ 'twitter' ] : '';
printf( '<p>%s</p>', esc_html( $args[ 'desc' ] ) );
printf( '<input id="%s" type="text" name="highlight-and-share[twitter]" value="%s" />', esc_attr( $args[ 'label_for' ] ), esc_attr( $js_content ) );
}
/**
* Option for displaying icons only
*
* Output icons only
*
* @since 2.0.0
* @access public
*
* @see init_admin_settings
*
* @param array $args {
@type string $label_for Settings label and ID.
@type string $desc Description for the setting.
}
*/
public function add_settings_field_display_enable( $args = array() ) {
$settings = $this->get_plugin_options();
$enable_icons = isset( $settings[ 'icons' ] ) ? (bool)$settings[ 'icons' ] : true;
echo '<input name="highlight-and-share[icons]" value="on" type="hidden" />';
printf( '<input id="has-show-icons" type="checkbox" name="highlight-and-share[icons]" value="off" %s /> <label for="has-show-icons">%s</label>', checked( false, $enable_icons, false ), __( 'Enable Icons Only?', 'highlight-and-share' ) );
$bfa = sprintf( '<a href="%s">Requires %s or equivalent.</a>', 'https://wordpress.org/plugins/better-font-awesome/', __( 'Better Font Awesome', 'highlight-and-share' ) );
printf( '<div><em>%s<br />%s</em></div>', $bfa, esc_html( $args[ 'desc' ] ) );
}
/**
* Add Twitter option for sharing.
*
* Output checkbox for displaying Twitter sharing.
*
* @since 1.0.0
* @access public
*
* @see init_admin_settings
*
* @param array $args {
@type string $label_for Settings label and ID.
@type string $desc Description for the setting.
}
*/
public function add_settings_field_twitter_enable( $args = array() ) {
$settings = $this->get_plugin_options();
$enable_twitter = isset( $settings[ 'show_twitter' ] ) ? (bool)$settings[ 'show_twitter' ] : true;
echo '<input name="highlight-and-share[show_twitter]" value="off" type="hidden" />';
printf( '<input id="has-show-twitter" type="checkbox" name="highlight-and-share[show_twitter]" value="on" %s /> <label for="has-show-twitter">%s</label>', checked( true, $enable_twitter, false ), __( 'Enable Twitter Sharing?', 'highlight-and-share' ) );
}
/**
* Add LinkedIn Option for Sharing
*
* Output checkbox for displaying LinkedIn sharing.
*
* @since 2.0.0
* @access public
*
* @see init_admin_settings
*
* @param array $args {
@type string $label_for Settings label and ID.
@type string $desc Description for the setting.
}
*/
public function add_settings_field_linkedin_enable( $args = array() ) {
$settings = $this->get_plugin_options();
$linkedin = isset( $settings[ 'show_linkedin' ] ) ? (bool)$settings[ 'show_linkedin' ] : true;
echo '<input name="highlight-and-share[show_linkedin]" value="off" type="hidden" />';
printf( '<input id="has-show-linkedin" type="checkbox" name="highlight-and-share[show_linkedin]" value="on" %s /> <label for="has-show-linkedin">%s</label>', checked( true, $linkedin, false ), __( 'Enable LinkedIn Sharing?', 'highlight-and-share' ) );
}
/**
* Add Pinterest Option for Sharing
*
* Output checkbox for displaying Pinterest sharing.
*
* @since 2.0.0
* @access public
*
* @see init_admin_settings
*
* @param array $args {
@type string $label_for Settings label and ID.
@type string $desc Description for the setting.
}
*/
public function add_settings_field_pinterest_enable( $args = array() ) {
$settings = $this->get_plugin_options();
$pinterest = isset( $settings[ 'show_pinterest' ] ) ? (bool)$settings[ 'show_pinterest' ] : true;
echo '<input name="highlight-and-share[show_pinterest]" value="off" type="hidden" />';
printf( '<input id="has-show-pinterest" type="checkbox" name="highlight-and-share[show_pinterest]" value="on" %s /> <label for="has-show-pinterest">%s</label>', checked( true, $pinterest, false ), __( 'Enable Pinterest Sharing?', 'highlight-and-share' ) );
}
/**
* Add E-mail Option for Sharing
*
* Output checkbox for displaying E-mail sharing.
*
* @since 2.0.0
* @access public
*
* @see init_admin_settings
*
* @param array $args {
@type string $label_for Settings label and ID.
@type string $desc Description for the setting.
}
*/
public function add_settings_field_email_enable( $args = array() ) {
$settings = $this->get_plugin_options();
$email = isset( $settings[ 'show_email' ] ) ? (bool)$settings[ 'show_email' ] : true;
echo '<input name="highlight-and-share[show_email]" value="off" type="hidden" />';
printf( '<input id="has-show-email" type="checkbox" name="highlight-and-share[show_email]" value="on" %s /> <label for="has-show-email">%s</label>', checked( true, $email, false ), __( 'Enable E-mail Sharing?', 'highlight-and-share' ) );
}
/**
* Add Facebook option for sharing.
*
* Output checkbox for displaying Facebook sharing.
*
* @since 1.0.0
* @access public
*
* @see init_admin_settings
*
* @param array $args {
@type string $label_for Settings label and ID.
@type string $desc Description for the setting.
}
*/
public function add_settings_field_facebook_enable( $args = array() ) {
$settings = $this->get_plugin_options();
$enable_facebook = isset( $settings[ 'show_facebook' ] ) ? (bool)$settings[ 'show_facebook' ] : true;
echo '<input name="highlight-and-share[show_facebook]" value="off" type="hidden" />';
printf( '<input id="has-show-facebook" type="checkbox" name="highlight-and-share[show_facebook]" value="on" %s /> <label for="has-show-facebook">%s</label>', checked( true, $enable_facebook, false ), __( 'Enable Facebook Sharing?', 'highlight-and-share' ) );
}
/**
* Add Facebook option for an Application ID.
*
* Add Facebook option for an Application ID.
*
* @since 2.1.0
* @access public
*
* @see init_admin_settings
*
* @param array $args {
@type string $label_for Settings label and ID.
@type string $desc Description for the setting.
}
*/
public function add_settings_field_facebook_api( $args = array() ) {
$settings = $this->get_plugin_options();
$app_id = isset( $settings[ 'facebook_app_id' ] ) ? absint( $settings[ 'facebook_app_id' ] ) : '';
if ( 0 === $app_id ) {
$app_id = '';
}
printf( '<p>%s</p>', esc_html( $args[ 'desc' ] ) );
printf( '<p><a href="%s">%s</a></p>', 'https://developers.facebook.com/apps', __( 'Requires a Facebook developer application.', 'highlight-and-share' ) );
printf( '<input id="%s" type="text" name="highlight-and-share[facebook_app_id]" value="%s" />', esc_attr( $args[ 'label_for' ] ), esc_attr( $app_id ) );
}
/**
* Add Content option for sharing.
*
* Output checkbox for sharing on main post content
*
* @since 1.0.0
* @access public
*
* @see init_admin_settings
*
* @param array $args {
@type string $label_for Settings label and ID.
@type string $desc Description for the setting.
}
*/
public function add_settings_field_content_enable( $args = array() ) {
$settings = $this->get_plugin_options();
$enable_content = isset( $settings[ 'enable_content' ] ) ? (bool)$settings[ 'enable_content' ] : true;
echo '<input name="highlight-and-share[enable_content]" value="off" type="hidden" />';
printf( '<input id="has-enable-content" type="checkbox" name="highlight-and-share[enable_content]" value="on" %s /> <label for="has-enable-content">%s</label>', checked( true, $enable_content, false ), __( 'Enable Content?', 'highlight-and-share' ) );
}
/**
* Add Content option for sharing.
*
* Output checkbox for sharing on main post content
*
* @since 1.0.0
* @access public
*
* @see init_admin_settings
*
* @param array $args {
@type string $label_for Settings label and ID.
@type string $desc Description for the setting.
}
*/
public function add_settings_field_excerpt_enable( $args = array() ) {
$settings = $this->get_plugin_options();
$enable_excerpt = isset( $settings[ 'enable_excerpt' ] ) ? (bool)$settings[ 'enable_excerpt' ] : true;
echo '<input name="highlight-and-share[enable_excerpt]" value="off" type="hidden" />';
printf( '<input id="has-enable-excerpt" type="checkbox" name="highlight-and-share[enable_excerpt]" value="on" %s /> <label for="has-enable-excerpt">%s</label>', checked( true, $enable_excerpt, false ), __( 'Enable Excerpt?', 'highlight-and-share' ) );
}
/**
* Add Shortlink Option for Sharing.
*
* Output checkbox for shortlinks.
*
* @since 1.0.0
* @access public
*
* @see init_admin_settings
*
* @param array $args {
@type string $label_for Settings label and ID.
@type string $desc Description for the setting.
}
*/
public function add_settings_field_shortlink_enable( $args = array() ) {
$settings = $this->get_plugin_options();
$enable_shortlinks = isset( $settings[ 'shortlinks' ] ) ? (bool)$settings[ 'shortlinks' ] : false;
echo '<input name="highlight-and-share[shortlinks]" value="off" type="hidden" />';
printf( '<input id="has-shortlinks" type="checkbox" name="highlight-and-share[shortlinks]" value="on" %s /> <label for="has-shortlinks">%s</label>', checked( true, $enable_shortlinks, false ), __( 'Enable Shortlinks?', 'highlight-and-share' ) );
}
/**
* Initialize and return plugin options.
*
* Return an array of plugin options.
*
* @since 1.0.0
* @access public
*
* @see init
*
* @return array Plugin options
*/
public function get_plugin_options() {
if ( false === $this->options ) {
$settings = get_option( 'highlight-and-share' );
} else {
$settings = $this->options;
}
$defaults = array(
'js_content' => '',
'twitter' => '',
'show_twitter' => true,
'show_facebook' => true,
'show_linkedin' => false,
'show_pinterest' => false,
'show_email' => false,
'enable_content' => true,
'enable_excerpt' => true,
'shortlinks' => false,
'icons' => true,
);
if ( false === $settings || !is_array( $settings ) ) {
update_option( 'highlight-and-share', $defaults );
return $defaults;
} else {
$settings = wp_parse_args( $settings, $defaults );