-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpost-to-queue.php
More file actions
1358 lines (1210 loc) · 34.9 KB
/
post-to-queue.php
File metadata and controls
1358 lines (1210 loc) · 34.9 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
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
/**
* The Post to Queue Plugin
*
* Stack posts to queue and auto publish them in chosen interval and time frame.
*
* @package Post_to_Queue
* @subpackage Main
*/
/**
* Plugin Name: Post to Queue
* Plugin URI: http://blog.milandinic.com/wordpress/plugins/post-to-queue/
* Description: Stack posts to queue and auto publish them in chosen interval and time frame.
* Author: Milan Dinić
* Author URI: http://blog.milandinic.com/
* Version: 1.0
* Text Domain: post-to-queue
* Domain Path: /languages/
* License: GPL
*/
/* Exit if accessed directly */
if ( ! defined( 'ABSPATH' ) ) exit;
/**
* Schedule install cron event on plugin activation.
*
* Since class can't be initialized on activation,
* installation needs to occur on next page load.
* That's why we schedule single cron event that
* will be fired on next page load.
*
* @since 1.0
*/
function ptq_activation() {
wp_schedule_single_event( time(), 'ptq_single_event_reschedule' );
}
register_activation_hook( __FILE__, 'ptq_activation' );
/**
* Unschedule Post to Queue events on deactivation.
*
* @since 1.0
*/
function ptq_deactivation() {
// Since we are unscheduling, we can check any post type, not just ours
foreach ( get_post_types() as $post_type ) {
wp_clear_scheduled_hook( "ptq_event_{$post_type}" );
}
// Delete queue existence statuses
delete_transient( 'ptq_queued_existence' );
}
register_deactivation_hook( __FILE__, 'ptq_deactivation' );
/**
* Initialize a plugin.
*
* Load class when all plugins are loaded
* so that other plugins can overwrite it.
*
* @since 1.0
*/
function ptq_instantiate() {
global $post_to_queue;
$post_to_queue = new Post_to_Queue();
}
add_action( 'plugins_loaded', 'ptq_instantiate', 15 );
if ( ! class_exists( 'Post_to_Queue' ) ) :
/**
* Post to Queue main class.
*
* Queue and publish posts automatically.
*
* @since 1.0
*/
class Post_to_Queue {
/**
* Path to plugin's directory.
*
* @since 1.0
* @access public
*
* @var string
*/
public $path;
/**
* URL path to plugin's directory.
*
* @since 1.0
* @access public
*
* @var string
*/
public $url_path;
/**
* Plugin's basename.
*
* @since 1.0
* @access public
*
* @var string
*/
public $basename;
/**
* Interval between last posted and queued post.
*
* @since 1.0
* @access public
*
* @var int
*/
public $interval;
/**
* Should admin class be loaded.
*
* @since 1.0
* @access public
*
* @var bool
*/
public $show_admin;
/**
* Should reorder class be loaded.
*
* @since 1.0
* @access public
*
* @var bool
*/
public $show_reorder;
/**
* Name of queue post status.
*
* @since 1.0
* @access public
*
* @var string
*/
public $status;
/**
* Post types that allow queuing.
*
* @since 1.0
* @access public
*
* @var array
*/
public $post_types = array();
/**
* Initialize Post_to_Queue object.
*
* Set class properties and add main methods to appropriate hooks.
*
* @since 1.0
* @access public
*/
public function __construct() {
/**
* Fires before class is initialized.
*
* @since 1.0
*/
do_action( 'ptq_before_construct' );
// Set paths
$this->path = rtrim( plugin_dir_path( __FILE__ ), '/' );
$this->url_path = rtrim( plugin_dir_url( __FILE__ ), '/' );
// Set basename
$this->basename = plugin_basename( __FILE__ );
// Load translations
load_plugin_textdomain( 'post-to-queue', false, dirname( $this->basename ) . '/languages' );
// Set interval
$this->interval = $this->interval();
// Set status name
$this->status = $this->status();
// Set admin loading statuses
/**
* Filter whether admin class should be loaded.
*
* @since 1.0
*
* @param bool $show_admin Whether admin class should be loaded. Default true.
*/
$this->show_admin = apply_filters( 'ptq_show_admin', true );
/**
* Filter whether reorder class should be loaded.
*
* @since 1.0
*
* @param bool $show_admin Wwhether reorder class should be loaded. Default true.
*/
$this->show_reorder = apply_filters( 'ptq_show_reorder', true );
// Register main hooks
add_action( 'init', array( $this, 'init' ) );
add_action( 'wp_loaded', array( $this, 'wp_loaded' ), 2 );
// Add PTQ cron interval
add_filter( 'cron_schedules', array( $this, 'add_interval' ) );
/**
* Fires after class is initialized.
*
* @since 1.0
*/
do_action( 'ptq_after_construct' );
}
/**
* Register queue post status and add most of the hooks.
*
* @since 1.0
* @access public
*/
public function init() {
/**
* Fires before init method.
*
* @since 1.0
*/
do_action( 'ptq_before_init' );
// Register post status
$default_post_status_args = array(
'label' => _x( 'Queued', 'post status name', 'post-to-queue' ),
'label_count' => _nx_noop( 'Queued <span class="count">(%s)</span>', 'Queued <span class="count">(%s)</span>', 'post status count label', 'post-to-queue' ),
'protected' => true,
'exclude_from_search' => true,
'show_in_admin_status_list' => true,
'show_in_admin_all_list' => true
);
/**
* Filter parameters used when registering queue post status.
*
* @see register_post_status()
*
* @since 1.0
*
* @param array $args {
* The array of parameters used when registering queue post status.
*
* @type string $label Name of the post status used in UI.
* @type array $label_count Array of registered plural strings of post status' name.
* @type bool $protected Defaults to true.
* @type bool $exclude_from_search Whether to exclude queued posts from search results. Defaults to true.
* @type bool $show_in_admin_status_list Whether to include posts in the edit listing for their post type. Defaults to true.
* @type bool $show_in_admin_all_list Whether to show in the list of statuses with post counts. Defaults to true.
* }
*/
$post_status_args = (array) apply_filters( 'ptq_register_post_status_args', $default_post_status_args );
$post_status_args = wp_parse_args( $post_status_args, $default_post_status_args );
register_post_status(
$this->status,
$post_status_args
);
// Add queue order on post saving
add_action( 'save_post', array( $this, 'add_queue_order' ) );
// Remove queue order on post trashing
add_action( 'trashed_post', array( $this, 'delete_order' ) );
// Cleanup queue existence statuses on saving
add_action( 'save_post', array( $this, 'delete_queued_existence' ) );
// Schedule reschedule of all events when PTQ settings change
add_action( 'add_option_ptq_settings', array( $this, 'schedule_reschedule' ) );
add_action( 'update_option_ptq_settings', array( $this, 'schedule_reschedule' ) );
add_action( 'delete_option_ptq_settings', array( $this, 'schedule_reschedule' ) );
// Schedule reschedule of all events when time zone change
add_action( 'add_option_gmt_offset', array( $this, 'schedule_reschedule' ) );
add_action( 'update_option_gmt_offset', array( $this, 'schedule_reschedule' ) );
add_action( 'delete_option_gmt_offset', array( $this, 'schedule_reschedule' ) );
add_action( 'add_option_timezone_string', array( $this, 'schedule_reschedule' ) );
add_action( 'update_option_timezone_string', array( $this, 'schedule_reschedule' ) );
add_action( 'delete_option_timezone_string', array( $this, 'schedule_reschedule' ) );
// Reschedule all events when single event happens
add_action( 'ptq_single_event_reschedule', array( $this, 'reschedule' ) );
// Maybe schedule post type event when single event happens
add_action( 'ptq_single_event_maybe_schedule', array( $this, 'maybe_schedule_event' ) );
/**
* Fires after init method.
*
* @since 1.0
*/
do_action( 'ptq_after_init' );
}
/**
* Add cron event hooks and load admin classes.
*
* @since 1.0
* @access public
*/
public function wp_loaded() {
/**
* Fires before wp_loaded method.
*
* @since 1.0
*/
do_action( 'ptq_before_wp_loaded' );
// Set post types allowed for queuing
$this->post_types = $this->post_types();
// Hook event onto all post types
foreach ( $this->post_types as $post_type ) {
add_action( "ptq_event_{$post_type}", array( $this, 'event' ) );
}
// Load additional classes for admin only
if ( is_admin() ) {
// Hook Admin class
if ( isset( $this->show_admin ) && $this->show_admin ) {
$this->maybe_load_admin();
new Post_to_Queue_Admin( $this );
}
// Hook Reorder class
if ( isset( $this->show_reorder ) && $this->show_reorder ) {
$this->maybe_load_reorder();
// Iterate through each specified post type and instantiate it's organiser
foreach ( $this->post_types as $post_type ) {
// If nothing queued for post type, continue
if ( ! $this->are_queued_for_type( $post_type ) ) {
continue;
}
new Post_to_Queue_Reorder(
$this,
array( 'post_type' => $post_type )
);
}
}
}
// Register plugins action links filter
add_filter( 'plugin_action_links_' . $this->basename, array( $this, 'action_links' ) );
/**
* Fires after wp_loaded method.
*
* @since 1.0
*/
do_action( 'ptq_after_wp_loaded' );
}
/**
* Get Post to Queue setting.
*
* If no setting, return requested default value.
*
* @since 1.0
* @access public
*
* @param string $name Name of the setting.
* @param mixed $default Value returned if no setting.
* @return mixed Value of setting or default value if no setting.
*/
public function get_option( $name, $default = '' ) {
$options = get_option( 'ptq_settings' );
if ( $options && is_array( $options ) && isset( $options[$name] ) ) {
return $options[$name];
} else {
return $default;
}
}
/**
* Get interval that should be used between last posted and queued post.
*
* Use site setting or a day by default.
*
* @since 1.0
* @access public
*
* @return int $interval Queue interval in seconds. Default one day.
*/
public function interval() {
$interval = absint( $this->get_option( 'interval' ) );
$interval = $interval * MINUTE_IN_SECONDS; // minutes * seconds
/**
* Filter length of time after last posted post to when next should be posted.
*
* @since 1.0
*
* @param int $interval Length of time after last posted post to when next should be posted.
*/
$interval = $this->posabsint( apply_filters( 'ptq_interval', $interval ), DAY_IN_SECONDS ); // Verify that value is positive natural number
return $interval;
}
/**
* Get name of queue post status.
*
* @since 1.0
* @access public
*
* @return string $status The name of the queue status. Default 'queue'.
*/
public function status() {
/**
* Filter the name of the queue post status.
*
* @since 1.0
*
* @param string $status The name of the queue status. Default 'queue'.
*/
$status = sanitize_key( apply_filters( 'ptq_post_status_name', 'queue' ) );
return $status;
}
/**
* Get post types that allow queuing.
*
* Shouldn't be used before wp_loaded hook
* so that all types are registered.
*
* @since 1.0
* @access public
*
* @return array $post_types Post types allowed to be queued. Default all public ones.
*/
public function post_types() {
/**
* Filter post types that are allowed to be queued.
*
* @since 1.0
*
* @param array $post_types Post types allowed to be queued. Default all public ones.
*/
$post_types = (array) apply_filters( 'ptq_post_types', get_post_types( array( 'public' => true ) ) );
return $post_types;
}
/**
* Get hours time frame when publishing should happen.
*
* @since 1.0
* @access public
*
* @return array $hours_range Hours when publishing is allowed.
*/
public function hours() {
/**
* Filter allowed hours of time frame.
*
* @since 1.0
*
* @param array $hours {
* The array of start and end of time frame.
*
* @type int $start Beginning of time frame.
* @type int $end End of time frame.
* }
*/
if ( ( $hours = apply_filters( 'ptq_hours', $this->get_option( 'hours', array() ) ) )
&& is_array( $hours )
&& isset( $hours['start'], $hours['end'] )
&& ( $start = $hours['start'] )
&& ( $end = $hours['end'] )
&& ( in_array( $start, range( '00', '23' ) ) )
&& ( in_array( $end, range( '00', '23' ) ) )
&& ( $start != $end )
) {
// If end hour is midnight, use it as '24' for proper results
if ( '00' == $end ) {
$end = 24;
}
// Create array with available hours
if ( $start < $end ) {
$hours_range = range( $start, $end - 1 );
} else {
$hours_range = array_merge( range( '00', $end - 1 ), range( $start, '23' ) );
}
return $hours_range;
} else {
return array();
}
}
/**
* Get days when publishing should happen.
*
* @since 1.0
* @access public
*
* @return array $days Days represented by numbers when publishing is allowed.
*/
public function days() {
/**
* Filter allowed days of time frame.
*
* @since 1.0
*
* @param array $days Days represented by numbers that are allowed.
*/
$days = (array) apply_filters( 'ptq_days', $this->get_option( 'days', array() ) );
return array_unique( array_map( 'absint', $days ) );
}
/**
* Load Reorder class file if not loaded.
*
* @since 1.0
* @access public
*/
public function maybe_load_reorder() {
if ( ! class_exists( 'Post_to_Queue_Reorder' ) ) {
require_once( $this->path . '/inc/class-post-to-queue-reorder.php' );
}
}
/**
* Load Admin class file if not loaded.
*
* @since 1.0
* @access public
*/
public function maybe_load_admin() {
if ( ! class_exists( 'Post_to_Queue_Admin' ) ) {
require_once( $this->path . '/inc/class-post-to-queue-admin.php' );
}
}
/**
* Add action links to plugins page.
*
* @since 1.0
* @access public
*
* @param array $links Existing plugin's action links.
* @return array $links New plugin's action links.
*/
public function action_links( $links ) {
$links['donate'] = '<a href="http://blog.milandinic.com/donate/">' . __( 'Donate', 'post-to-queue' ) . '</a>';
$links['settings'] = '<a href="' . admin_url( 'options-writing.php' ) . '">' . _x( 'Settings', 'plugin actions link', 'post-to-queue' ) . '</a>';
return $links;
}
/**
* Add custom cron interval.
*
* Add a 'ptq' interval to the existing set
* of intervals.
*
* @since 1.0
* @access public
*
* @param array $schedules Existing cron intervals.
* @return array $schedules New cron intervals.
*/
public function add_interval( $schedules ) {
$schedules['ptq'] = array(
'interval' => $this->interval,
'display' => __( 'Post to Queue Interval', 'post-to-queue' )
);
return $schedules;
}
/**
* Cron event for post type.
*
* Call publishing method when cron is run.
*
* @since 1.0
* @access public
*/
public function event() {
/**
* Fires before cron event.
*
* @since 1.0
*/
do_action( 'ptq_before_event' );
// Get current action name
$current_hook = current_filter();
// Get current post type by subtracting 'ptq_event_'
$post_type = substr( $current_hook, 10 );
// If post type exists, publish queued post
if ( post_type_exists( $post_type ) ) {
$this->maybe_publish( $post_type );
}
/**
* Fires after cron event.
*
* @since 1.0
*/
do_action( 'ptq_after_event' );
}
/**
* Publish queued post for post type if in time frame.
*
* @since 1.0
* @access public
*
* @param string $post_type Post type that should be published.
*/
public function maybe_publish( $post_type ) {
/**
* Fires before publishing.
*
* @since 1.0
*
* @param string $post_type Post type that should be published.
*/
do_action( 'ptq_before_publish', $post_type );
// Only proceed if post type exists
if ( ! post_type_exists( $post_type ) ) {
return;
}
// If now is not it time frame, reschedule
if ( ! $this->is_in_time_frame( $post_type ) ) {
$this->schedule_post_type( $post_type );
return;
}
// Get first in order queued post
$_post = $this->get_one_queued( $post_type );
// If there are no queued posts of type, clear schedule
if ( ! $_post ) {
wp_clear_scheduled_hook( "ptq_event_{$post_type}" );
return;
}
// Clear date when post was queued so that we have real publishing date
$_post_a = get_post( $_post->ID, ARRAY_A );
$_post_a['post_date'] = '';
$_post_a['post_date_gmt'] = '';
wp_update_post( $_post_a );
// Publish post
wp_publish_post( $_post->ID );
// Remove order number for post
$this->delete_order( $_post->ID );
/**
* Fires after publishing.
*
* @since 1.0
*
* @param string $post_type Post type that should be published.
* @param WP_Post $_post WP_Post object of published post.
*/
do_action( 'ptq_after_publish', $post_type, $_post );
}
/**
* Get time of last published post of post type.
*
* @since 1.0
* @access public
*
* @param string $post_type Post type that is checked.
* @return int Unix time of last published post of post type in GMT.
*/
public function get_last_published_time( $post_type ) {
$posts = get_posts( 'numberposts=1&post_type=' . $post_type );
if ( $posts ) {
$_post = array_pop( $posts );
$time = strtotime( $_post->post_date_gmt );
} else {
$time = 0;
}
/**
* Filter time of last published post of post type.
*
* @since 1.0
*
* @param int $time Unix time of last published post of post type in GMT.
* @param string $post_type Post type that is checked.
*/
return absint( apply_filters( 'ptq_last_published_time', $time, $post_type ) );
}
/**
* Get one queued post based on args.
*
* @since 1.0
* @access public
*
* @param string $post_type Post type that is queried.
* @param array $args The array of parameters used to query first queued post in order.
* @return WP_Post $_post Post object of post that is result of query.
*/
public function get_one_queued( $post_type, $args = array() ) {
/**
* Filter parameters used to query first queued post in order.
*
* @since 1.0
*
* @param array $args The array of parameters used to query first queued post in order.
* @param string $post_type Post type that is queried.
*/
$args = (array) apply_filters( 'ptq_get_one_queued_args', $args, $post_type );
// Parse arguments
$defaults = array(
'post_type' => $post_type,
'posts_per_page' => 1,
'meta_key' => '_queue_order',
'orderby' => 'meta_value_num',
'order' => 'ASC', // Setting the order of the posts
'post_status' => $this->status, // Post status of posts to be reordered
);
$args = wp_parse_args( $args, $defaults );
// Get post
$posts = get_posts( $args );
$_post = array_pop( $posts );
return $_post;
}
/**
* Check if now is in selected time frame for post type.
*
* @since 1.0
* @access public
*
* @param string $post_type Post type that is checked.
* @return bool Whether now is in selected time frame for post type.
*/
public function is_in_time_frame( $post_type ) {
// Get time of last published post for type
if ( ( $this->get_last_published_time( $post_type ) + $this->interval ) > time() ) {
// Now is earlier than next publishing time
$status = false;
} else if ( ( $days = $this->days() ) && ! in_array( date_i18n( 'w' ), $days ) ) {
// Now isn't in selected days
$status = false;
} else if ( ( $hours = $this->hours() ) && ! in_array( date_i18n( 'H' ), $hours ) ) {
// Now isn't in selected hours of the day
$status = false;
} else {
// Everything passed, now is in time frame
$status = true;
}
/**
* Filter whether now is in selected time frame for post type.
*
* @since 1.0
*
* @param bool $status Whether now is in selected time frame for post type.
* @param string $post_type Post type that is checked.
*/
return (bool) apply_filters( 'ptq_is_in_time_frame', $status, $post_type );
}
/**
* Check are there queued posts for post type.
*
* @since 1.0
* @access public
*
* @param string $post_type Post type that is checked.
* @return bool Whether there are queued posts of post type.
*/
public function are_queued_for_type( $post_type ) {
// Get all statuses
$statues = $this->queued_existence();
if ( isset( $statues[$post_type] ) && $statues[$post_type] ) {
$status = true;
} else {
$status = false;
}
/**
* Filter whether there are queued posts of post type.
*
* @since 1.0
*
* @param bool $status Whether there are queued posts of post type.
* @param string $post_type Post type that is checked.
*/
return (bool) apply_filters( 'ptq_are_queued_for_type', $status, $post_type );
}
/**
* Get queue existence status for each post type.
*
* @since 1.0
* @access public
*
* @return array The array of statuses of existence of queued posts for each post type.
*/
public function queued_existence() {
// If not cached, get raw
if ( false === ( $statues = get_transient( 'ptq_queued_existence' ) ) ) {
$statues = array();
// Loop through all post types to see are there queued posts
foreach ( $this->post_types as $post_type ) {
if ( $this->get_one_queued( $post_type ) ) {
$statues[$post_type] = true;
} else {
$statues[$post_type] = false;
}
}
/**
* Filter time until expiration of queue existence statuses cache.
*
* @since 1.0
*
* @param int $expire Time in seconds after which queue existence statuses cache expires. Default one hour.
*/
$expire = $this->posabsint( apply_filters( 'ptq_queued_existence_expiration', HOUR_IN_SECONDS ), HOUR_IN_SECONDS );
// Save to cache for an hour by default, or to custom value that is verified positive natural number
set_transient( 'ptq_queued_existence', $statues, $expire );
}
/**
* Filter statuses of existence of queued posts for each post type.
*
* @since 1.0
*
* @param array $statues The array of statuses of existence of queued posts for each post type.
*/
return (array) apply_filters( 'ptq_queued_existence', $statues );
}
/**
* Delete queue existence statuses.
*
* @since 1.0
* @access public
*/
public function delete_queued_existence() {
delete_transient( 'ptq_queued_existence' );
}
/**
* Check does post type allows queuing.
*
* @since 1.0
* @access public
*
* @param string $post_type Post type that is checked.
* @return bool Whether post type allows queuing.
*/
public function can_type_be_queued( $post_type ) {
if ( in_array( $post_type, $this->post_types ) ) {
$status = true;
} else {
$status = false;
}
/**
* Filter whether post type allows queuing.
*
* @since 1.0
*
* @param bool $status Whether post type allows queuing.
* @param string $post_type Post type that is checked.
*/
return (bool) apply_filters( 'ptq_can_type_be_queued', $status, $post_type );
}
/**
* Check is post queued.
*
* @since 1.0
* @access public
*
* @param int|WP_Post $post Post ID or post object.
* @return bool Whether post is queued.
*/
public function is_queued( $post ) {
if ( ! $post = get_post( $post ) ) {
return false;
}
if ( $post->post_status == $this->status ) {
$status = true;
} else {
$status = false;
}
/**
* Filter whether post is queued.
*
* @since 1.0
*
* @param bool $status Whether post is queued.
* @param WP_Post $post Post object of post that is checked.
*/
return (bool) apply_filters( 'ptq_is_queued', $status, $post );
}
/**
* Schedule event for post type.
*
* Reschedule previous event and schedule new
* based on last published date for post type,
* chosen interval between posts, and time frame.
*
* This method took almost as much time to write as
* everything else in this plugin. This is nth iteration
* and previous where either to resource heavy or inefficient.
* Another problem was solving GMT and local time issue.
*
* @since 1.0
* @access public
*
* @param string $post_type Post type that is scheduled.
*/
public function schedule_post_type( $post_type ) {
/**
* Fires before scheduling.
*
* @since 1.0
*
* @param string $post_type Post type that should be scheduled.
*/
do_action( 'ptq_before_schedule_post_type', $post_type );
// If not queued for post type, unshedule event and leave
if ( ! $this->are_queued_for_type( $post_type ) ) {
wp_clear_scheduled_hook( "ptq_event_{$post_type}" );
return;
}
// Get days and hours
$days = $this->days();
$hours = $this->hours();
// Get the time when next queued post of type should be published
// It's a sum of last published post for type plus interval
$next_time = $this->get_last_published_time( $post_type ) + $this->interval;
// If there is one range, create other
if ( $days && ! $hours ) {
$hours = range( '00', '23' );
} else if ( $hours && ! $days ) {
$days = range( 0, 6 );
}
/*
This should work like this:
- check if current day and hour are available
- if available, use that time; break
- check if current day is available and there are still hours in day
- loop through rest hours and check each if available
- if available, get Unix time of that hour; break
- check if there are still days in week
- loop through rest days and check each if available
- if available, get Unix time of first available hour of that day; break
- get first available day of next week
- get Unix time of first available hour of that day; break
*/
if ( $days && $hours ) {
$next_time_tmp = false;
// Get day and hour of available time
$day_of_next_time = $this->date_i18n( 'w', $next_time );
$hour_of_next_time = $this->date_i18n( 'H', $next_time );