Skip to content

Commit 38d0231

Browse files
feat: add custom cron schedules workflow (#1144)
1 parent ef39900 commit 38d0231

File tree

6 files changed

+370
-2
lines changed

6 files changed

+370
-2
lines changed

css/settings.css

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2947,4 +2947,36 @@ button.feedzy-action-button {
29472947

29482948
#feedzy-validate-feed .dashicons {
29492949
font-size: 20px;
2950-
}
2950+
}
2951+
2952+
.fz-schedule-counter {
2953+
text-align: right;
2954+
margin-bottom: 10px;
2955+
color: #757575;
2956+
font-size: 13px;
2957+
}
2958+
2959+
.fz-schedules-table {
2960+
border-collapse: collapse;
2961+
}
2962+
2963+
.fz-schedules-table th {
2964+
font-weight: 600;
2965+
background: #f6f7f7;
2966+
}
2967+
2968+
.fz-schedules-table td {
2969+
padding: 10px;
2970+
}
2971+
2972+
.fz-schedule-attributes {
2973+
color: #050505;
2974+
}
2975+
2976+
.fz-delete-schedule.fz-is-destructive {
2977+
background: #dc3545;
2978+
border-color: #dc3545;
2979+
color: #ffffff;
2980+
font-size: 12px;
2981+
padding: 4px 12px;
2982+
}

includes/admin/feedzy-rss-feeds-admin.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ public function enqueue_styles_admin() {
231231
'media_iframe_button' => __( 'Set default image', 'feedzy-rss-feeds' ),
232232
'action_btn_text_1' => __( 'Choose image', 'feedzy-rss-feeds' ),
233233
'action_btn_text_2' => __( 'Replace image', 'feedzy-rss-feeds' ),
234+
'delete_btn_label' => __( 'Delete', 'feedzy-rss-feeds' ),
234235
),
235236
)
236237
);
@@ -1464,6 +1465,31 @@ function ( $item ) {
14641465
'pass' => isset( $_POST['proxy-pass'] ) ? sanitize_text_field( wp_unslash( $_POST['proxy-pass'] ) ) : '',
14651466
);
14661467
break;
1468+
case 'schedules':
1469+
if ( feedzy_is_pro() ) {
1470+
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1471+
$custom_schedules = isset( $_POST['fz-custom-schedule-interval'] ) ? (array) wp_unslash( $_POST['fz-custom-schedule-interval'] ) : array();
1472+
$settings['custom_schedules'] = array();
1473+
if ( ! empty( $custom_schedules ) ) {
1474+
$cron_timout = defined( 'WP_CRON_LOCK_TIMEOUT' ) ? WP_CRON_LOCK_TIMEOUT : 60;
1475+
1476+
foreach ( $custom_schedules as $key => $value ) {
1477+
$interval = isset( $value['interval'] ) ? absint( $value['interval'] ) : $cron_timout;
1478+
$display = isset( $value['display'] ) ? sanitize_text_field( $value['display'] ) : '';
1479+
1480+
if (
1481+
is_numeric( $interval ) && $cron_timout <= $interval &&
1482+
! empty( $display )
1483+
) {
1484+
$settings['custom_schedules'][ $key ] = array(
1485+
'interval' => $interval,
1486+
'display' => $display,
1487+
);
1488+
}
1489+
}
1490+
}
1491+
}
1492+
break;
14671493
default:
14681494
$settings = apply_filters( 'feedzy_save_tab_settings', $settings, $post_tab );
14691495
}
@@ -3133,6 +3159,35 @@ public function validate_feed() {
31333159
}
31343160
}
31353161

3162+
/**
3163+
* Append custom schedules to the existing schedules.
3164+
*
3165+
* @since 5.1.0
3166+
* @param array<string,array{interval:int,display:string}> $schedules Existing schedules.
3167+
* @return array<string,array{interval:int,display:string}> Modified schedules with custom schedules appended.
3168+
*/
3169+
public function append_custom_cron_schedules( $schedules ) {
3170+
if ( ! feedzy_is_pro() ) {
3171+
return $schedules;
3172+
}
3173+
3174+
$saved_settings = apply_filters( 'feedzy_get_settings', array() );
3175+
if ( ! empty( $saved_settings['custom_schedules'] ) && is_array( $saved_settings['custom_schedules'] ) ) {
3176+
$custom_schedules = $saved_settings['custom_schedules'];
3177+
3178+
foreach ( $custom_schedules as $key => $value ) {
3179+
if ( ! empty( $value['interval'] ) && ! empty( $value['display'] ) ) {
3180+
$schedules[ $key ] = array(
3181+
'interval' => intval( $value['interval'] ),
3182+
'display' => sanitize_text_field( $value['display'] ),
3183+
);
3184+
}
3185+
}
3186+
}
3187+
3188+
return $schedules;
3189+
}
3190+
31363191
/**
31373192
* Add slugs for internal cron schedules.
31383193
*

includes/feedzy-rss-feeds.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ private function define_admin_hooks() {
206206
self::$instance->loader->add_action( 'wp_ajax_feedzy_validate_feed', self::$instance->admin, 'validate_feed' );
207207
self::$instance->loader->add_action( 'wp_ajax_feedzy_dashboard_subscribe', self::$instance->admin, 'feedzy_dashboard_subscribe' );
208208
self::$instance->loader->add_filter( 'feedzy_internal_cron_schedule_slugs', self::$instance->admin, 'internal_cron_schedule_slugs', 10, 1 );
209+
self::$instance->loader->add_filter( 'cron_schedules', self::$instance->admin, 'append_custom_cron_schedules' );
209210

210211
// do not load this with the loader as this will need a corresponding remove_filter also.
211212
add_filter( 'update_post_metadata', array( self::$instance->admin, 'validate_category_feeds' ), 10, 5 );
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
$custom_schedules = array();
3+
$has_pro = feedzy_is_pro();
4+
$settings = apply_filters( 'feedzy_get_settings', array() );
5+
6+
if ( isset( $settings['custom_schedules'] ) && is_array( $settings['custom_schedules'] ) ) {
7+
$custom_schedules = $settings['custom_schedules'];
8+
}
9+
?>
10+
<div class="fz-form-wrap">
11+
<div class="form-block">
12+
<div class="fz-form-group">
13+
<h4 class="h4">
14+
<?php esc_html_e( 'Add Cron Schedule', 'feedzy-rss-feeds' ); ?>
15+
<?php if ( ! $has_pro ) : ?>
16+
<span class="pro-label">PRO</span>
17+
<?php endif; ?>
18+
</h4>
19+
<div class="fz-condition-control" style="padding-bottom: 0;">
20+
<div class="fz-form-row" style="margin: 0; gap: 1rem; align-items: flex-end;">
21+
<div class="fz-form-group">
22+
<label class="form-label" for="fz-schedule-interval">
23+
<?php esc_html_e( 'Interval (seconds)', 'feedzy-rss-feeds' ); ?>
24+
</label>
25+
<input
26+
type="number"
27+
class="form-control"
28+
id="fz-schedule-interval"
29+
placeholder="3600"
30+
min="<?php echo esc_attr( defined( 'WP_CRON_LOCK_TIMEOUT' ) ? WP_CRON_LOCK_TIMEOUT : 60 ); ?>"
31+
<?php disabled( ! $has_pro ); ?>
32+
/>
33+
</div>
34+
35+
<div class="fz-form-group">
36+
<label class="form-label" for="fz-schedule-display">
37+
<?php esc_html_e( 'Display Name', 'feedzy-rss-feeds' ); ?>
38+
</label>
39+
<input
40+
type="text"
41+
class="form-control"
42+
id="fz-schedule-display"
43+
placeholder="Once Hourly"
44+
<?php disabled( ! $has_pro ); ?>
45+
/>
46+
</div>
47+
48+
<div class="fz-form-group">
49+
<label class="form-label" for="fz-schedule-name">
50+
<?php esc_html_e( 'Internal Name', 'feedzy-rss-feeds' ); ?>
51+
</label>
52+
<input
53+
type="text"
54+
class="form-control"
55+
id="fz-schedule-name"
56+
placeholder="hourly"
57+
<?php disabled( ! $has_pro ); ?>
58+
/>
59+
</div>
60+
<div class="fz-form-group">
61+
<button
62+
class="btn btn-primary"
63+
id="fz-add-schedule"
64+
<?php disabled( ! $has_pro ); ?>
65+
>
66+
<?php esc_html_e( 'Add Cron Schedule', 'feedzy-rss-feeds' ); ?>
67+
</button>
68+
</div>
69+
</div>
70+
</div>
71+
</div>
72+
</div>
73+
74+
<div
75+
class="form-block"
76+
>
77+
<div
78+
class="fz-schedules-table"
79+
style="<?php echo empty( $custom_schedules ) || ! $has_pro ? 'display: none;' : ''; ?>"
80+
>
81+
<div class="fz-schedule-counter">
82+
<?php
83+
$schedule_count = count( $custom_schedules );
84+
85+
// translators: %s is the number of custom schedules.
86+
echo esc_html( sprintf( __( '%s items', 'feedzy-rss-feeds' ), $schedule_count ) );
87+
?>
88+
</div>
89+
90+
<table class="fz-schedules-table widefat striped">
91+
<thead>
92+
<tr>
93+
<th><?php esc_html_e( 'Internal Name', 'feedzy-rss-feeds' ); ?></th>
94+
<th><?php esc_html_e( 'Interval', 'feedzy-rss-feeds' ); ?></th>
95+
<th><?php esc_html_e( 'Display Name', 'feedzy-rss-feeds' ); ?></th>
96+
<th><?php esc_html_e( 'Actions', 'feedzy-rss-feeds' ); ?></th>
97+
</tr>
98+
</thead>
99+
<tbody>
100+
<?php
101+
foreach ( $custom_schedules as $slug => $schedule ) :
102+
$interval_seconds = $schedule['interval'];
103+
$interval_display = $interval_seconds . ' (' . human_time_diff( 0, $interval_seconds ) . ')';
104+
?>
105+
<tr>
106+
<td class="fz-schedule-attributes">
107+
<strong><?php echo esc_html( $slug ); ?></strong>
108+
</td>
109+
110+
<td class="fz-schedule-attributes">
111+
<?php echo esc_html( $interval_display ); ?>
112+
</td>
113+
114+
<td class="fz-schedule-attributes">
115+
<?php echo esc_html( $schedule['display'] ); ?>
116+
</td>
117+
118+
<td class="fz-schedule-attributes">
119+
<button type="button" class="btn btn-outline-primary fz-delete-schedule fz-is-destructive" data-schedule="<?php echo esc_attr( $slug ); ?>">
120+
<?php esc_html_e( 'Delete', 'feedzy-rss-feeds' ); ?>
121+
</button>
122+
</td>
123+
124+
<input
125+
type="hidden"
126+
value="<?php echo esc_attr( $schedule['interval'] ); ?>"
127+
name="fz-custom-schedule-interval[<?php echo esc_attr( $slug ); ?>][interval]"
128+
>
129+
130+
<input
131+
type="hidden"
132+
value="<?php echo esc_attr( $schedule['display'] ); ?>"
133+
name="fz-custom-schedule-interval[<?php echo esc_attr( $slug ); ?>][display]"
134+
>
135+
</tr>
136+
<?php endforeach; ?>
137+
</tbody>
138+
</table>
139+
</div>
140+
</div>
141+
</div>

includes/layouts/settings.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,17 @@ class="<?php echo 'logs' === $active_tab ? esc_attr( 'active' ) : ''; ?>"
9999
<?php esc_html_e( 'Logs', 'feedzy-rss-feeds' ); ?>
100100
</a>
101101
</li>
102+
<li>
103+
<a
104+
href="<?php echo esc_url( admin_url( 'admin.php?page=feedzy-settings&tab=schedules' ) ); ?>"
105+
class="<?php echo 'schedules' === $active_tab ? esc_attr( 'active' ) : ''; ?>"
106+
>
107+
<?php esc_html_e( 'Schedules', 'feedzy-rss-feeds' ); ?>
108+
<?php if ( ! feedzy_is_pro() ) : ?>
109+
<span class="pro-label">PRO</span>
110+
<?php endif; ?>
111+
</a>
112+
</li>
102113
<?php
103114
$_tabs = apply_filters( 'feedzy_settings_tabs', array() );
104115
if ( $_tabs ) {
@@ -506,6 +517,9 @@ class="form-control"
506517
case 'logs':
507518
$show_button = false;
508519
break;
520+
case 'schedules':
521+
load_template( FEEDZY_ABSPATH . '/includes/layouts/feedzy-schedules.php' );
522+
break;
509523
default:
510524
$fields = apply_filters( 'feedzy_display_tab_settings', array(), $active_tab );
511525
if ( $fields ) {
@@ -543,7 +557,7 @@ class="form-control"
543557
</div>
544558
</div>
545559

546-
<?php if ( 'proxy' !== $active_tab && 'headers' !== $active_tab ) : ?>
560+
<?php if ( 'proxy' !== $active_tab && 'headers' !== $active_tab && 'schedules' !== $active_tab ) : ?>
547561
<div class="cta pt-30">
548562
<a href="<?php echo esc_url( $help_btn_url ); ?>" class="btn btn-ghost" target="_blank"><?php esc_html_e( 'Need help?', 'feedzy-rss-feeds' ); ?></a>
549563
</div>

0 commit comments

Comments
 (0)