@@ -406,6 +406,21 @@ public function feedzy_import_feed_options() {
406
406
$ default_thumbnail_id = ! empty ( $ this ->free_settings ['general ' ]['default-thumbnail-id ' ] ) ? (int ) $ this ->free_settings ['general ' ]['default-thumbnail-id ' ] : 0 ;
407
407
}
408
408
}
409
+ $ import_schedule = array (
410
+ 'fz_execution_offset ' => ! empty ( $ this ->free_settings ['general ' ]['fz_execution_offset ' ] ) ? $ this ->free_settings ['general ' ]['fz_execution_offset ' ] : '' ,
411
+ 'fz_cron_execution ' => ! empty ( $ this ->free_settings ['general ' ]['fz_cron_execution ' ] ) ? $ this ->free_settings ['general ' ]['fz_cron_execution ' ] : '' ,
412
+ 'fz_cron_schedule ' => ! empty ( $ this ->free_settings ['general ' ]['fz_cron_schedule ' ] ) ? $ this ->free_settings ['general ' ]['fz_cron_schedule ' ] : '' ,
413
+ );
414
+
415
+ $ fz_cron_execution = get_post_meta ( $ post ->ID , 'fz_cron_execution ' , true );
416
+ $ fz_cron_schedule = get_post_meta ( $ post ->ID , 'fz_cron_schedule ' , true );
417
+ $ fz_execution_offset = get_post_meta ( $ post ->ID , 'fz_execution_offset ' , true );
418
+ if ( ! empty ( $ fz_cron_schedule ) && ! empty ( $ fz_cron_execution ) ) {
419
+ $ import_schedule ['fz_cron_schedule ' ] = $ fz_cron_schedule ;
420
+ $ import_schedule ['fz_execution_offset ' ] = $ fz_execution_offset ;
421
+ $ import_schedule ['fz_cron_execution ' ] = $ fz_cron_execution ;
422
+ }
423
+
409
424
$ post_status = $ post ->post_status ;
410
425
$ nonce = wp_create_nonce ( FEEDZY_BASEFILE );
411
426
$ invalid_source_msg = apply_filters ( 'feedzy_get_source_validity_error ' , '' , $ post );
@@ -473,6 +488,20 @@ public function save_feedzy_import_feed_meta( $post_id, $post ) {
473
488
}
474
489
}
475
490
}
491
+
492
+ $ global_cron_execution = ! empty ( $ this ->free_settings ['general ' ]['fz_cron_execution ' ] ) ? $ this ->free_settings ['general ' ]['fz_cron_execution ' ] : '' ;
493
+ $ global_cron_schedule = ! empty ( $ this ->free_settings ['general ' ]['fz_cron_schedule ' ] ) ? $ this ->free_settings ['general ' ]['fz_cron_schedule ' ] : '' ;
494
+ if (
495
+ (
496
+ empty ( $ data_meta ['fz_cron_execution ' ] ) || $ global_cron_schedule === $ data_meta ['fz_cron_execution ' ]
497
+ )
498
+ &&
499
+ empty ( $ data_meta ['fz_cron_schedule ' ] ) || $ global_cron_schedule === $ data_meta ['fz_cron_schedule ' ]
500
+ ) {
501
+ // Remove scheduled cron settings if they are equal to the global settings.
502
+ unset( $ data_meta ['fz_cron_execution ' ], $ data_meta ['fz_cron_schedule ' ], $ data_meta ['fz_execution_offset ' ] );
503
+ }
504
+
476
505
$ custom_fields_keys = array ();
477
506
if ( isset ( $ _POST ['custom_vars_key ' ] ) && is_array ( $ _POST ['custom_vars_key ' ] ) ) {
478
507
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
@@ -543,7 +572,8 @@ public function save_feedzy_import_feed_meta( $post_id, $post ) {
543
572
wp_update_post ( $ activate );
544
573
add_action ( 'save_post_feedzy_imports ' , array ( $ this , 'save_feedzy_import_feed_meta ' ), 1 , 2 );
545
574
}
546
-
575
+ // Clear the import job cron schedule if it exists.
576
+ Feedzy_Rss_Feeds_Util_Scheduler::clear_scheduled_hook ( 'feedzy_cron ' , array ( 100 , $ post_id ) );
547
577
do_action ( 'feedzy_save_fields ' , $ post_id , $ post );
548
578
}
549
579
@@ -724,20 +754,33 @@ public function manage_feedzy_import_columns( $column, $post_id ) {
724
754
725
755
break ;
726
756
case 'feedzy-next_run ' :
727
- $ next = wp_next_scheduled ( 'feedzy_cron ' );
757
+ $ next = Feedzy_Rss_Feeds_Util_Scheduler::is_scheduled ( 'feedzy_cron ' , array ( 100 , $ post_id ) );
758
+ if ( ! $ next ) {
759
+ $ next = Feedzy_Rss_Feeds_Util_Scheduler::is_scheduled ( 'feedzy_cron ' );
760
+ }
728
761
if ( $ next ) {
729
762
$ now = new DateTime ();
730
763
$ then = new DateTime ();
731
764
$ then = $ then ->setTimestamp ( $ next );
732
765
$ in = $ now ->diff ( $ then );
733
- echo wp_kses_post (
734
- sprintf (
735
- // translators: %1$d: number of hours, %2$d: number of minutes
736
- __ ( 'In %1$d hours %2$d minutes ' , 'feedzy-rss-feeds ' ),
737
- $ in ->format ( '%h ' ),
738
- $ in ->format ( '%i ' )
739
- )
740
- );
766
+
767
+ $ time_string = array ();
768
+ // Add days if they exist.
769
+ if ( $ in ->d > 0 ) {
770
+ // translators: %1$s days.
771
+ $ time_string [] = sprintf ( __ ( '%1$d days ' , 'feedzy-rss-feeds ' ), $ in ->d );
772
+ }
773
+ // Add hours if they exist.
774
+ if ( $ in ->h > 0 ) {
775
+ // translators: %1$s hours.
776
+ $ time_string [] = sprintf ( __ ( '%1$d hours ' , 'feedzy-rss-feeds ' ), $ in ->h );
777
+ }
778
+ // Add minutes if they exist.
779
+ if ( $ in ->i > 0 ) {
780
+ // translators: %1$s minutes.
781
+ $ time_string [] = sprintf ( __ ( '%1$d minutes ' , 'feedzy-rss-feeds ' ), $ in ->i );
782
+ }
783
+ echo wp_kses_post ( join ( ' ' , $ time_string ) );
741
784
}
742
785
break ;
743
786
default :
@@ -1223,7 +1266,7 @@ function ( $errors, $feed, $url ) {
1223
1266
* @since 1.2.0
1224
1267
* @access public
1225
1268
*/
1226
- public function run_cron ( $ max = 100 ) {
1269
+ public function run_cron ( $ max = 100 , $ job_id = 0 ) {
1227
1270
if ( empty ( $ max ) ) {
1228
1271
$ max = 10 ;
1229
1272
}
@@ -1234,9 +1277,26 @@ public function run_cron( $max = 100 ) {
1234
1277
'post_type ' => 'feedzy_imports ' ,
1235
1278
'post_status ' => 'publish ' ,
1236
1279
'numberposts ' => 99 ,
1280
+ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
1281
+ 'meta_query ' => array (
1282
+ 'relation ' => 'AND ' ,
1283
+ array (
1284
+ 'key ' => 'fz_cron_execution ' ,
1285
+ 'compare ' => 'NOT EXISTS ' ,
1286
+ ),
1287
+ array (
1288
+ 'key ' => 'fz_cron_schedule ' ,
1289
+ 'compare ' => 'NOT EXISTS ' ,
1290
+ ),
1291
+ ),
1237
1292
)
1238
1293
);
1239
1294
1295
+ if ( $ job_id ) {
1296
+ $ args ['post__in ' ] = array ( $ job_id );
1297
+ unset( $ args ['meta_query ' ], $ args ['numberposts ' ] );
1298
+ }
1299
+
1240
1300
$ feedzy_imports = get_posts ( $ args );
1241
1301
foreach ( $ feedzy_imports as $ job ) {
1242
1302
try {
@@ -2266,11 +2326,46 @@ public function add_cron() {
2266
2326
$ offset = sanitize_text_field ( wp_unslash ( $ _POST ['fz_execution_offset ' ] ) );
2267
2327
$ time = $ this ->get_cron_execution ( $ execution , $ offset );
2268
2328
$ schedule = sanitize_text_field ( wp_unslash ( $ _POST ['fz_cron_schedule ' ] ) );
2269
- wp_clear_scheduled_hook ( 'feedzy_cron ' );
2329
+ Feedzy_Rss_Feeds_Util_Scheduler:: clear_scheduled_hook ( 'feedzy_cron ' );
2270
2330
}
2271
2331
}
2272
- if ( false === wp_next_scheduled ( 'feedzy_cron ' ) ) {
2273
- wp_schedule_event ( $ time , $ schedule , 'feedzy_cron ' );
2332
+ if ( false === Feedzy_Rss_Feeds_Util_Scheduler::is_scheduled ( 'feedzy_cron ' ) ) {
2333
+ Feedzy_Rss_Feeds_Util_Scheduler::schedule_event ( $ time , $ schedule , 'feedzy_cron ' );
2334
+ }
2335
+
2336
+ // Register import jobs based cron jobs.
2337
+ $ import_job_crons = get_posts (
2338
+ array (
2339
+ 'post_type ' => 'feedzy_imports ' ,
2340
+ 'post_status ' => 'publish ' ,
2341
+ 'numberposts ' => 99 ,
2342
+ 'fields ' => 'ids ' ,
2343
+ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
2344
+ 'meta_query ' => array (
2345
+ 'relation ' => 'AND ' ,
2346
+ array (
2347
+ 'key ' => 'fz_cron_execution ' ,
2348
+ 'compare ' => 'EXISTS ' ,
2349
+ ),
2350
+ array (
2351
+ 'key ' => 'fz_cron_schedule ' ,
2352
+ 'compare ' => 'EXISTS ' ,
2353
+ ),
2354
+ ),
2355
+ )
2356
+ );
2357
+
2358
+ if ( ! empty ( $ import_job_crons ) ) {
2359
+ foreach ( $ import_job_crons as $ job_id ) {
2360
+ $ fz_cron_execution = get_post_meta ( $ job_id , 'fz_cron_execution ' , true );
2361
+ $ fz_cron_schedule = get_post_meta ( $ job_id , 'fz_cron_schedule ' , true );
2362
+ $ fz_execution_offset = get_post_meta ( $ job_id , 'fz_execution_offset ' , true );
2363
+ $ time = $ this ->get_cron_execution ( $ fz_cron_execution , $ fz_execution_offset );
2364
+
2365
+ if ( false === Feedzy_Rss_Feeds_Util_Scheduler::is_scheduled ( 'feedzy_cron ' , array ( 100 , $ job_id ) ) ) {
2366
+ Feedzy_Rss_Feeds_Util_Scheduler::schedule_event ( $ time , $ fz_cron_schedule , 'feedzy_cron ' , array ( 100 , $ job_id ) );
2367
+ }
2368
+ }
2274
2369
}
2275
2370
}
2276
2371
@@ -2285,7 +2380,7 @@ public function get_cron_execution( $execution, $offset = 0 ) {
2285
2380
if ( empty ( $ offset ) && ! empty ( $ this ->free_settings ['general ' ]['fz_execution_offset ' ] ) ) {
2286
2381
$ offset = $ this ->free_settings ['general ' ]['fz_execution_offset ' ];
2287
2382
}
2288
- $ execution = strtotime ( $ execution ) ? strtotime ( $ execution ) + ( HOUR_IN_SECONDS * $ offset ) : time () + ( HOUR_IN_SECONDS * $ offset );
2383
+ $ execution = strtotime ( $ execution ) ? strtotime ( $ execution ) + ( HOUR_IN_SECONDS * ( int ) $ offset ) : time () + ( HOUR_IN_SECONDS * ( int ) $ offset );
2289
2384
return $ execution ;
2290
2385
}
2291
2386
@@ -2306,7 +2401,7 @@ public function admin_notices() {
2306
2401
echo wp_kses_post ( '<div class="notice notice-error feedzy-error-critical is-dismissible"><p> ' . __ ( 'WP Cron is disabled. Your feeds would not get updated. Please contact your hosting provider or system administrator ' , 'feedzy-rss-feeds ' ) . '</p></div> ' );
2307
2402
}
2308
2403
2309
- if ( false === wp_next_scheduled ( 'feedzy_cron ' ) ) {
2404
+ if ( false === Feedzy_Rss_Feeds_Util_Scheduler:: is_scheduled ( 'feedzy_cron ' ) ) {
2310
2405
echo wp_kses_post ( '<div class="notice notice-error"><p> ' . __ ( 'Unable to register cron job. Your feeds might not get updated ' , 'feedzy-rss-feeds ' ) . '</p></div> ' );
2311
2406
}
2312
2407
}
0 commit comments