-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
demo-quotes-plugin.php
889 lines (751 loc) · 25.5 KB
/
demo-quotes-plugin.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
<?php
/**
* Demo Quotes Plugin.
*
* @package WordPress\Plugins\Demo_Quotes_Plugin
* @author Juliette Reinders Folmer <[email protected]>
* @link https://github.com/jrfnl/wp-plugin-best-practices-demo
* @version 1.0.1
*
* @copyright 2013-2015 Juliette Reinders Folmer
* @license http://creativecommons.org/licenses/GPL/2.0/ GNU General Public License, version 3 or higher
*
* @wordpress-plugin
* Plugin Name: Demo Quotes Plugin
* Plugin URI: https://github.com/jrfnl/wp-plugin-best-practices-demo
* Description: Demo plugin for WordPress Plugins Best Practices Tutorial
* Version: 1.1
* Author: Juliette Reinders Folmer
* Author URI: http://adviesenzo.nl/
* Text Domain: demo-quotes-plugin
* Domain Path: /languages
* License: GPL v3
*
* Copyright (C) 2013, Juliette Reinders Folmer - [email protected]
*
* GNU General Public License, Free Software Foundation <http://creativecommons.org/licenses/GPL/3.0/>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
if ( ! class_exists( 'Demo_Quotes_Plugin' ) ) {
/**
* Demo Quotes Plugin.
*/
class Demo_Quotes_Plugin {
/* *** DEFINE CLASS CONSTANTS *** */
/**
* Plugin version number.
*
* @const string
*/
const VERSION = '1.0.1';
/**
* Version in which the front-end styles where last changed.
*
* @const string
*/
const STYLES_VERSION = '1.0';
/**
* Version in which the front-end scripts where last changed.
*
* @const string
*/
const SCRIPTS_VERSION = '1.0';
/**
* Version in which the admin styles where last changed.
*
* @const string
*/
const ADMIN_STYLES_VERSION = '1.0';
/**
* Version in which the admin scripts where last changed.
*
* @const string
*/
const ADMIN_SCRIPTS_VERSION = '1.0';
/**
* Name of our shortcode.
*
* @const string
*/
const SHORTCODE = 'demo_quote';
/**
* Name of our update check transient.
*
* @const string
*/
const UPDATE_TRANSIENT = 'demo_quote_update';
/* *** DEFINE STATIC CLASS PROPERTIES *** */
/**
* These static properties will be initialized - *before* class instantiation -
* by the static init() function.
*/
/**
* Plugin Basename = 'dir/file.php'.
*
* @var string
*/
public static $basename;
/**
* Plugin name = dirname of the plugin.
*
* @var string
*/
public static $name;
/**
* Suffix to use if scripts/styles are in debug mode.
*
* @var string
*/
public static $suffix;
/* *** DEFINE CLASS PROPERTIES *** */
/* *** Properties Holding Various Parts of the Class' State. *** */
/**
* Settings page class.
*
* @var object
*/
public $settings_page;
/* *** PLUGIN INITIALIZATION METHODS *** */
/**
* Object constructor for plugin.
*
* @uses Demo_Quotes_Plugin::VERSION
* @uses Demo_Quotes_Plugin::SHORTCODE
*
* @return Demo_Quotes_Plugin
*/
public function __construct() {
spl_autoload_register( array( $this, 'auto_load' ) );
/* Check if we have any upgrade actions to do. */
if ( ! isset( Demo_Quotes_Plugin_Option::$current['version'] ) || version_compare( self::VERSION, Demo_Quotes_Plugin_Option::$current['version'], '>' ) ) {
add_action( 'init', array( $this, 'upgrade' ), 1 );
}
// Make sure that the upgrade actions are run on (re-)activation as well.
// @todo check if this will really work.
add_action( 'demo_quotes_plugin_activate', array( $this, 'upgrade' ) );
/* Register the plugin initialization actions. */
add_action( 'init', array( $this, 'init' ), 8 );
add_action( 'admin_menu', array( $this, 'setup_options_page' ) );
add_action( 'admin_init', array( $this, 'admin_init' ) );
/* Register the widget. */
add_action( 'widgets_init', array( $this, 'widgets_init' ) );
/* Register the shortcode. */
add_shortcode( self::SHORTCODE, array( $this, 'do_shortcode' ) );
}
/**
* Set the static path and directory variables for this class
* Is called from the global space *before* instantiating the class to make
* sure the correct values are available to the object
*
* @return void
*/
public static function init_statics() {
self::$basename = plugin_basename( __FILE__ );
self::$name = trim( dirname( self::$basename ) );
self::$suffix = ( ( defined( 'SCRIPT_DEBUG' ) && true === SCRIPT_DEBUG ) ? '' : '.min' );
}
/**
* Auto load our class files.
*
* @param string $class Class name.
*
* @return void
*/
public function auto_load( $class ) {
static $classes = null;
if ( null === $classes ) {
$classes = array(
'demo_quotes_plugin_cpt' => 'class-demo-quotes-plugin-cpt.php',
'demo_quotes_plugin_option' => 'class-demo-quotes-plugin-option.php',
'demo_quotes_plugin_settings_page' => 'class-demo-quotes-plugin-settings-page.php',
'demo_quotes_plugin_widget' => 'class-demo-quotes-plugin-widget.php',
'demo_quotes_plugin_people_widget' => 'class-demo-quotes-plugin-people-widget.php',
);
}
$cn = strtolower( $class );
if ( isset( $classes[ $cn ] ) ) {
include_once plugin_dir_path( __FILE__ ) . $classes[ $cn ];
}
}
/* *** ADMINISTRATIVE METHODS *** */
/**
* Add the actions for the front-end functionality.
* Add actions which are needed for both front-end and back-end functionality.
*
* @return void
*/
public function init() {
/* Allow filtering of our plugin name. */
self::filter_statics();
/*
* Load plugin text strings.
* @see http://geertdedeckere.be/article/loading-wordpress-language-files-the-right-way
*
* If you'll be hosting your plugin at wordpress.org and using the translations as
* provided via GlotPress (translate.wordpress.org), you can simplify this to the
* below and you can remove the local `load_textdomain()` function as well:
*
* `load_plugin_textdomain( 'demo-quotes-plugin' );`
*
* The net effect of this will be that WP will ignore translations included with the
* plugin and will look in the `wp-content/languages/plugins/` folder for translations
* instead.
*/
$this->load_textdomain( 'demo-quotes-plugin' );
/* Register the Quotes Custom Post Type and add any related action and filters. */
Demo_Quotes_Plugin_Cpt::init();
/* Register our ajax actions for the widget. */
add_action( 'wp_ajax_demo_quotes_widget_next', array( $this, 'demo_quotes_widget_next' ) );
add_action( 'wp_ajax_nopriv_demo_quotes_widget_next', array( $this, 'demo_quotes_widget_next' ) );
/* Add js and css files. */
add_action( 'wp_enqueue_scripts', array( $this, 'wp_enqueue_scripts' ) );
}
/**
* Allow filtering of the plugin name.
* Mainly useful for non-standard directory setups.
*
* @return void
*/
public static function filter_statics() {
self::$name = apply_filters( 'demo_quotes_plugin_name', self::$name );
}
/**
* Load the plugin text strings.
*
* Compatible with use of the plugin in the must-use plugins directory.
*
* @param string $domain Text domain to load.
*/
protected function load_textdomain( $domain ) {
$lang_path = dirname( plugin_basename( __FILE__ ) ) . '/languages';
if ( false === strpos( __FILE__, basename( WPMU_PLUGIN_DIR ) ) ) {
load_plugin_textdomain( $domain, false, $lang_path );
} else {
load_muplugin_textdomain( $domain, $lang_path );
}
}
/**
* Add back-end functionality.
*
* @return void
*/
public function admin_init() {
/* Add actions and filters for our custom post type. */
Demo_Quotes_Plugin_Cpt::admin_init();
/* Add js and css files. */
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
}
/**
* Register the options page for all users that have the required capability.
*
* @return void
*/
public function setup_options_page() {
/* Don't do anything if user does not have the required capability. */
if ( false === is_admin() || false === current_user_can( Demo_Quotes_Plugin_Option::REQUIRED_CAP ) ) {
return;
}
$this->settings_page = new Demo_Quotes_Plugin_Settings_Page();
}
/**
* Register the Widgets.
*
* @see register_widget()
*
* @return void
*/
public function widgets_init() {
register_widget( 'Demo_Quotes_Plugin_Widget' );
register_widget( 'Demo_Quotes_Plugin_People_Widget' );
}
/**
* Register, but don't yet enqueue necessary javascript and css files for the front-end.
*
* @uses Demo_Quotes_Plugin::STYLES_VERSION
* @uses Demo_Quotes_Plugin::SCRIPTS_VERSION
*
* @return void
*/
public function wp_enqueue_scripts() {
wp_register_style(
self::$name . '-css', // ID.
plugins_url( 'css/style' . self::$suffix . '.css', __FILE__ ), // URL.
array(), // Not used.
self::STYLES_VERSION, // Version.
'all' // Media.
);
wp_register_script(
self::$name . '-js', // ID.
plugins_url( 'js/interaction' . self::$suffix . '.js', __FILE__ ), // URL.
array( 'jquery', 'wp-ajax-response' ), // Dependants.
self::SCRIPTS_VERSION, // Version.
true // Load in footer ?
);
}
/**
* Conditionally add necessary javascript and css files for the back-end on the appropriate screens.
*
* @uses Demo_Quotes_Plugin::$name
* @uses Demo_Quotes_Plugin::$suffix
* @uses Demo_Quotes_Plugin::$settings_page
* @uses Demo_Quotes_Plugin_Cpt::$post_type_name
* @uses Demo_Quotes_Plugin::ADMIN_STYLES_VERSION
* @uses Demo_Quotes_Plugin::ADMIN_SCRIPTS_VERSION
*
* @return void
*/
public function admin_enqueue_scripts() {
$screen = get_current_screen();
if ( property_exists( $screen, 'post_type' ) && Demo_Quotes_Plugin_Cpt::$post_type_name === $screen->post_type ) {
wp_enqueue_style(
self::$name . '-admin-css', // ID.
plugins_url( 'css/admin-style' . self::$suffix . '.css', __FILE__ ), // URL.
array(), // Not used.
self::ADMIN_STYLES_VERSION, // Version.
'all' // Media.
);
}
/* Admin js for settings page only. */
if ( property_exists( $screen, 'base' ) && ( isset( $this->settings_page ) && $screen->base === $this->settings_page->hook ) ) {
wp_enqueue_script(
self::$name . '-admin-js', // ID.
plugins_url( 'js/admin-interaction' . self::$suffix . '.js', __FILE__ ), // URL.
array( 'jquery', 'jquery-ui-accordion' ), // Dependants.
self::ADMIN_SCRIPTS_VERSION, // Version.
true // Load in footer ?
);
}
}
/**
* Function containing the helptext strings.
*
* Of course in a real plugin, we'd have proper helpful texts here.
*
* @static
*
* @param object $screen Screen object for the screen the user is on.
* @param array $tab Help tab being requested.
*/
public static function get_helptext( $screen, $tab ) {
switch ( $tab['id'] ) {
case self::$name . '-main':
echo '
<p>', esc_html__( 'Here comes a helpful help text ;-)', 'demo-quotes-plugin' ), '</p>
<p>', esc_html__( 'And some more help.', 'demo-quotes-plugin' ), '</p>';
break;
case self::$name . '-add':
echo '
<p>', esc_html__( 'Some specific information about editing a quote', 'demo-quotes-plugin' ), '</p>
<p>', esc_html__( 'And some more help.', 'demo-quotes-plugin' ), '</p>';
break;
case self::$name . '-advanced':
echo '
<p>', esc_html__( 'Some information about advanced features if we create any.', 'demo-quotes-plugin' ), '</p>';
break;
case self::$name . '-extras':
echo '
<p>', esc_html__( 'And here we may say something on extra\'s we add to the post type', 'demo-quotes-plugin' ), '</p>';
break;
case self::$name . '-settings':
echo '
<p>', esc_html__( 'Some information on the effect of the settings', 'demo-quotes-plugin' ), '</p>';
break;
default:
// Nothing here.
break;
}
}
/**
* Generate the links for the help sidebar.
* Of course in a real plugin, we'd have proper links here.
*
* @static
*
* @return string
*/
public static function get_help_sidebar() {
return '
<p><strong>' . esc_html__( 'For more information:', 'default' ) . '</strong></p>
<p>
<a href="https://wordpress.org/plugins/" target="_blank">' . esc_html__( 'Official plugin page (if there would be one)', 'demo-quotes-plugin' ) . '</a> |
<a href="#" target="_blank">' . esc_html__( 'FAQ', 'demo-quotes-plugin' ) . '</a> |
<a href="#" target="_blank">' . esc_html__( 'Changelog', 'demo-quotes-plugin' ) . '</a> |
<a href="https://github.com/jrfnl/wp-plugin-best-practices-demo/issues" target="_blank">' . esc_html__( 'Report issues', 'demo-quotes-plugin' ) . '</a>
</p>
<p><a href="https://github.com/jrfnl/wp-plugin-best-practices-demo" target="_blank">' . esc_html__( 'Github repository', 'demo-quotes-plugin' ) . '</a></p>
<p>' .
wp_kses_post(
sprintf(
/* translators: 1: link tag; 2: link closing tag. */
__( 'Created by %1$sAdvies en zo%2$s', 'demo-quotes-plugin' ),
'<a href="http://adviesenzo.nl/" target="_blank">',
'</a>'
)
) .
'</p>
';
}
/* *** PLUGIN ACTIVATION, UPGRADING AND DEACTIVATION *** */
/**
* Plugin Activation routine.
*
* @return void
*/
public static function activate() {
/* Security check. */
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
/* Register the Quotes Custom Post Type so WP knows how to adjust the rewrite rules. */
Demo_Quotes_Plugin_Cpt::register_post_type();
Demo_Quotes_Plugin_Cpt::register_taxonomy();
/* Make sure our post type and taxonomy slugs will be recognized. */
flush_rewrite_rules();
/* Execute any extra actions registered. */
do_action( 'demo_quotes_plugin_activate' );
}
/**
* Plugin deactivation routine.
*
* @return void
*/
public static function deactivate() {
/* Security check. */
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
$plugin = ( isset( $_REQUEST['plugin'] ) ? sanitize_text_field( $_REQUEST['plugin'] ) : '' );
check_admin_referer( 'deactivate-plugin_' . $plugin );
/* Make sure our post type and taxonomy slugs will be removed. */
flush_rewrite_rules();
/* Execute any extra actions registered. */
do_action( 'demo_quotes_plugin_deactivate' );
}
/**
* Function used when activating and/or upgrading the plugin.
*
* Upgrades for any version of this plugin lower than x.x.
* N.B.: Version nr has to be hard coded to be future-proof, i.e. facilitate
* upgrade routines for various versions.
*
* - Initial activate: Save version number to option.
* - v0.2 ensure post format is always set to 'quote'.
* - v0.3 auto-set the post title and slug for our post type posts.
*
* @todo - figure out if any special actions need to be run if multisite
* Probably not as this is run on init, so as soon as a page of another site in a multisite
* install is requested, the upgrade will run.
*
* @uses Demo_Quotes_Plugin::VERSION
*
* @return void
*/
public function upgrade() {
/**
* Check to make sure that the upgrade is not already being run in a parallel process.
* This is especially important when running intensive "only-once" database queries
* which take longer then a few microseconds in an upgrade process.
* Both the standard WP AJAX call might kick in while the upgrade is being run just as
* another user might be visiting the site and quite often the changes can start conflicting
* if run twice in parallel.
* Using this transient will prevent all that.
*/
if ( get_transient( self::UPDATE_TRANSIENT ) !== false ) {
return;
}
set_transient( self::UPDATE_TRANSIENT, true, HOUR_IN_SECONDS );
/**
* Now we don't want the user to surf away from a page which is running the update and break
* the update in the process, so let's make sure that doesn't happen.
*/
$ignore = ignore_user_abort( true );
/* Get our currently saved option to find out from which version we'll need to upgrade. */
$options = Demo_Quotes_Plugin_Option::$current;
/**
* Cpt post format upgrade for version 0.2.
*
* Ensure all posts of our custom post type have the 'quote' post format.
*/
if ( ! isset( $options['version'] ) || version_compare( $options['version'], '0.2', '<' ) ) {
/* Get all posts of our custom post type which currently do not have the 'quote' post format. */
$args = array(
'post_type' => Demo_Quotes_Plugin_Cpt::$post_type_name,
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-quote' ),
'operator' => 'NOT IN',
),
),
'nopaging' => true,
);
$query = new WP_Query( $args );
/* Set the post format. */
while ( $query->have_posts() ) {
$query->next_post();
set_post_format( $query->post->ID, 'quote' );
}
wp_reset_postdata(); // Always restore original Post Data.
unset( $args, $query );
}
/**
* Cpt slug and title upgrade for version 0.3.
*
* Ensure all posts of our custom post type posts have a title and a textual slug.
*/
if ( ! isset( $options['version'] ) || version_compare( $options['version'], '0.3', '<' ) ) {
/*
* Get all posts of our custom post type except for those with post status auto-draft,
* inherit (=revision) or trash.
* Alternative way of getting the results for demonstration purposes.
*/
$sql = $GLOBALS['wpdb']->prepare(
'SELECT *
FROM `' . $GLOBALS['wpdb']->posts . '`
WHERE `post_type` = %s
AND `post_status` NOT IN ( "auto-draft", "inherit", "trash" )
',
Demo_Quotes_Plugin_Cpt::$post_type_name
);
$result = $GLOBALS['wpdb']->get_results( $sql );
/* Update the post title and post slug. */
if ( is_array( $result ) && array() !== $result ) {
foreach ( $result as $row ) {
Demo_Quotes_Plugin_Cpt::update_post_title_and_name( $row->ID, $row );
}
unset( $row );
}
unset( $sql, $result );
}
/**
* Custom taxonomies upgrade for version 0.5.
*
* Ensure the rewrite rules are refreshed.
*/
if ( ! isset( $options['version'] ) || version_compare( $options['version'], '0.5', '<' ) ) {
/* Register the Quotes Custom Post Type so WP knows how to adjust the rewrite rules. */
Demo_Quotes_Plugin_Cpt::register_post_type();
Demo_Quotes_Plugin_Cpt::register_taxonomy();
flush_rewrite_rules();
}
/* Always update the version number. */
$options['version'] = self::VERSION;
/*
* Update the settings and refresh our property.
*
* We'll be saving our options during the upgrade routine *before* the setting
* is registered (and therefore the validation is registered), so make sure that the
* options are validated anyway.
*/
update_option( Demo_Quotes_Plugin_Option::NAME, $options );
delete_transient( self::UPDATE_TRANSIENT );
ignore_user_abort( $ignore );
}
/* *** HELPER METHODS *** */
/* *** FRONT-END: DISPLAY METHODS *** */
/**
* Retrieve the next quote to display and send it back to the AJAX request.
*
* @return void
*/
public function demo_quotes_widget_next() {
/* Security check. */
check_ajax_referer(
'demo-quotes-widget-next-nonce', // Name of our nonce.
'dqpwNonce', // $REQUEST variable to look at.
true // Die if check fails.
);
$not = null;
if ( isset( $_POST['currentQuote'] ) ) {
$not = intval( $_POST['currentQuote'] );
}
$quote = self::get_random_quote( $not, false, 'array' );
$response = new WP_Ajax_Response();
$response->add(
array(
'what' => 'quote',
'action' => 'next_quote',
'data' => '',
'supplemental' => array(
'quoteid' => $quote['id'],
'quote' => '<div class="dqpw-quote-wrapper">' . $quote['html'] . '</div>',
),
)
);
$response->send();
exit;
}
/**
* Return random quote via shortcode.
*
* @uses Demo_Quotes_Plugin::get_random_quote()
*
* @param array $args Shortcode arguments received.
*
* @return mixed
*/
public function do_shortcode( $args ) {
/*
// Filter received arguments and combine them with our defaults.
$args = shortcode_atts(
$this->shortcode_defaults, // the defaults
$args, // the received shortcode arguments
self::SHORTCODE // Shortcode name to be used by shortcode_args_{$shortcode} filter (WP 3.6+)
);
*/
return self::get_random_quote();
}
/**
* Get a quote at random.
*
* @param int|null $not Optional. Post id to exclude, defaults to none.
* @param bool $echo Optional. Whether to echo the result, defaults to false.
* @param string $return_type Optional. What to return:
* 'string' = html string
* 'array' = array consisting of:
* 'html' => html string,
* 'id' => post id
* 'post' => post object
* Defaults to 'string'.
*
* @return mixed False if no quotes found, null if echo = true, string/array if echo = false
*/
public static function get_random_quote( $not = null, $echo = false, $return_type = 'string' ) {
// WP_Query arguments.
$args = array(
'post_type' => Demo_Quotes_Plugin_Cpt::$post_type_name,
'post_status' => 'publish',
'posts_per_page' => '1',
'orderby' => 'rand',
);
if ( isset( $not ) && false !== filter_var( $not, FILTER_VALIDATE_INT ) ) {
$args['post__not_in'] = (array) $not;
}
/*
// @todo
if ( ID ) {
// add id to query
} elseif ( person ||tag || most recent) {
if ( person ) {
// add to query
}
if ( tag ) {
// add to query
}
if ( most recent ) {
// add most recent to query
}
} else {
// add random to query
}
*/
// The Query.
$query = new WP_Query( $args );
$html = '';
if ( 1 === $query->post_count ) {
$html .= '
<div class="dqp-quote dqp-quote-' . esc_attr( $query->post->ID ) . '">
<p>' . wp_kses_post( $query->post->post_content ) . '</p>
</div>';
$html .= self::get_quoted_by( $query->post->ID, false );
if ( true === $echo ) {
echo $html; // WPCS: XSS ok.
wp_reset_postdata();
} else {
$return = null;
if ( 'array' === $return_type ) {
$return = array(
'html' => $html,
'id' => $query->post->ID,
'object' => $query->post,
);
} else {
$return = $html;
}
wp_reset_postdata();
return $return;
}
}
return false;
}
/**
* Generate link to person quoted.
*
* @param int $post_id Current post ID.
* @param bool $echo Whether to echo out the result. Defaults to false.
*
* @return string
*/
public static function get_quoted_by( $post_id, $echo = false ) {
$html = '';
$terms = wp_get_post_terms( $post_id, Demo_Quotes_Plugin_Cpt::$taxonomy_name );
if ( is_array( $terms ) && array() !== $terms ) {
$html .= '
<div class="dqp-quote-by"><p>';
foreach ( $terms as $term ) {
/* translators: %s: Quotee (author of the quote). */
$title_attr = sprintf( __( 'View more quotes by %s', 'demo-quotes-plugin' ), $term->name );
$html .= '
<a href="' . esc_url( get_term_link( $term ) ) . '" title="' . esc_attr( $title_attr ) . '">' . esc_html( $term->name ) . '</a>';
}
$html .= '
</p></div>';
}
if ( true === $echo ) {
echo $html; // WPCS: XSS ok.
return '';
} else {
return $html;
}
}
} /* End of class. */
/*
* Instantiate our class.
*
* wp_installing() function was introduced in WP 4.4.
*/
if ( ( function_exists( 'wp_installing' ) && wp_installing() === false ) || ( ! function_exists( 'wp_installing' ) && ( ! defined( 'WP_INSTALLING' ) || WP_INSTALLING === false ) ) ) {
add_action( 'plugins_loaded', 'demo_quotes_plugin_init' );
}
if ( ! function_exists( 'demo_quotes_plugin_init' ) ) {
/**
* Initialize the class.
*
* @return void
*/
function demo_quotes_plugin_init() {
/* Initialize the static variables. */
Demo_Quotes_Plugin::init_statics();
$GLOBALS['demo_quotes_plugin'] = new Demo_Quotes_Plugin();
}
}
if ( ! function_exists( 'dqp_get_demo_quote' ) ) {
/**
* Template tag to display a quote.
*
* @param array $args Arguments to retrieve the quote.
* @param bool $echo Whether or not to echo out the result.
*
* @return string|bool False if no quote found, else a properly escaped html string.
*/
function dqp_get_demo_quote( $args, $echo = false ) {
return Demo_Quotes_Plugin::get_random_quote( $args, $echo );
}
}
/* Set up the (de-)activation actions. */
register_activation_hook( __FILE__, array( 'Demo_Quotes_Plugin', 'activate' ) );
register_deactivation_hook( __FILE__, array( 'Demo_Quotes_Plugin', 'deactivate' ) );
} /* End of class-exists wrapper. */