-
Notifications
You must be signed in to change notification settings - Fork 1
/
schedulify.php
298 lines (256 loc) · 10 KB
/
schedulify.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
<?php
/*
Plugin Name: Schedulify
Description: WordPress plugin that automatically publishes all the scheduled posts missed by WordPress cron. Sends email notifications to administrators.
Author: WP Corner
Author URI: https://wpcorner.co
Version: 1.0.5
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Text Domain: schedulify
Domain Path: /languages
*/
//bail if not WordPress path
if ( false === defined( 'ABSPATH' ) ) {
return;
}
//plugin basename for further reference
$nv_wpms_base_name = plugin_basename( __FILE__ );
// Hook into WordPress
add_action( 'init', 'nv_wpmsp_init', 0 );
// Plugin Actions
add_filter( 'plugin_action_links_' . $nv_wpms_base_name, 'nv_wpmsp_plugin_activation_link', 10, 1 );
add_filter( 'plugin_row_meta', 'nv_wpmsp_plugin_row_meta', 10, 2 );
// Add activation link under the Posts menu
add_action( 'admin_menu', 'nv_wpmsp_add_activation_link_to_menu' );
// Add settings link
add_action( 'admin_menu', 'nv_wpmsp_add_settings_link_to_menu' );
// Add Cron Event Stats link
add_action( 'admin_menu', 'nv_wpmsp_add_cron_event_stats_link_to_menu' );
// Register settings
add_action( 'admin_init', 'nv_wpmsp_register_settings' );
/**
* Check timestamp from transient and publish all missed posts
*/
function nv_wpmsp_init() {
$last_scheduled_missed_time = get_transient( 'wp_scheduled_missed_time' );
$time = current_time( 'timestamp', 0 );
if ( false !== $last_scheduled_missed_time && absint( $last_scheduled_missed_time ) > ( $time - nv_wpmsp_get_interval() ) ) {
return;
}
set_transient( 'wp_scheduled_missed_time', $time, nv_wpmsp_get_interval() );
global $wpdb;
$sql_query = "SELECT ID FROM {$wpdb->posts} WHERE ( ( post_date > 0 && post_date <= %s ) ) AND post_status = 'future' LIMIT 0,%d";
$sql = $wpdb->prepare( $sql_query, current_time( 'mysql', 0 ), nv_wpmsp_get_post_limit() );
$scheduled_post_ids = $wpdb->get_col( $sql );
if ( ! count( $scheduled_post_ids ) ) {
return;
}
foreach ( $scheduled_post_ids as $scheduled_post_id ) {
if ( ! $scheduled_post_id ) {
continue;
}
wp_publish_post( $scheduled_post_id );
// Send Email Notification
if ( nv_wpmsp_get_email_notifications() ) {
nv_wpmsp_send_email_notification( $scheduled_post_id );
}
}
}
/**
* Send email notification to administrators
*
* @param int $post_id
*/
function nv_wpmsp_send_email_notification( $post_id ) {
$admin_email = get_option( 'admin_email' );
$subject = sprintf( esc_html__( 'Scheduled Post Published: #%d', 'schedulify' ), $post_id );
$message = sprintf( esc_html__( 'The scheduled post #%d has been published.', 'schedulify' ), $post_id );
wp_mail( $admin_email, $subject, $message );
}
/**
* Add plugin activation link
*
* @param $links
*
* @return array
*/
function nv_wpmsp_plugin_activation_link( $links ) {
$links[] = '<a href="edit.php?post_status=future&post_type=post">' . esc_html__( 'Scheduled Posts', 'schedulify' ) . '</a>';
return $links;
}
/**
* Add link in plugin row meta
*
* @param $links
*
* @return array
*/
function nv_wpmsp_plugin_row_meta( $links, $file ) {
if ( false === is_admin() ) {
return;
}
if ( false === current_user_can( 'administrator' ) ) {
return;
}
if ( $file == plugin_basename( __FILE__ ) ) {
$links[] = '<a href="https://github.com/lumumbapl/Schedulify/wiki/Documentation">' . esc_html__( 'Documentation', 'schedulify' ) . '</a>';
}
return $links;
}
/**
* Add settings link under the Schedulify menu
*/
function nv_wpmsp_add_settings_link_to_menu() {
add_menu_page(
esc_html__( 'Schedulify', 'schedulify' ),
esc_html__( 'Schedulify', 'schedulify' ),
'manage_options',
'nv_wpmsp_settings_page',
'nv_wpmsp_render_settings_page',
'dashicons-calendar'
);
// Move Scheduled Posts submenu
remove_submenu_page( 'edit.php?post_status=future&post_type=post', 'edit.php?post_status=future&post_type=post' );
add_submenu_page(
'nv_wpmsp_settings_page',
esc_html__( 'Scheduled Posts', 'schedulify' ),
esc_html__( 'Scheduled Posts', 'schedulify' ),
'read',
'edit.php?post_status=future&post_type=post'
);
}
/**
* Add Cron Event Stats link under the Schedulify menu
*/
function nv_wpmsp_add_cron_event_stats_link_to_menu() {
add_submenu_page(
'nv_wpmsp_settings_page',
esc_html__( 'Cron Event Stats', 'schedulify' ),
esc_html__( 'Cron Event Stats', 'schedulify' ),
'read',
'nv_wpmsp_cron_event_stats_page',
'nv_wpmsp_render_cron_event_stats_page'
);
}
/**
* Register plugin settings
*/
function nv_wpmsp_register_settings() {
register_setting( 'nv_wpmsp_settings_group', 'nv_wpmsp_email_notifications', 'intval' );
register_setting( 'nv_wpmsp_settings_group', 'nv_wpmsp_admin_email', 'sanitize_email' );
register_setting( 'nv_wpmsp_settings_group', 'nv_wpmsp_custom_interval', 'intval' );
register_setting( 'nv_wpmsp_settings_group', 'nv_wpmsp_allowed_roles', 'nv_wpmsp_sanitize_roles' );
}
/**
* Render settings page
*/
function nv_wpmsp_render_settings_page() {
// Check if the settings have been saved
if ( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] ) {
?>
<div id="message" class="updated notice is-dismissible">
<p><strong><?php esc_html_e( 'Settings saved.', 'schedulify' ); ?></strong></p>
<button type="button" class="notice-dismiss">
<span class="screen-reader-text"><?php esc_html_e( 'Dismiss this notice.', 'schedulify' ); ?></span>
</button>
</div>
<?php
}
?>
<div class="wrap">
<h1><?php esc_html_e( 'Schedulify Settings', 'schedulify' ); ?></h1>
<form method="post" action="options.php">
<?php settings_fields( 'nv_wpmsp_settings_group' ); ?>
<?php do_settings_sections( 'nv_wpmsp_settings_group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row"><?php esc_html_e( 'Email Notifications', 'schedulify' ); ?></th>
<td>
<label>
<input type="checkbox" name="nv_wpmsp_email_notifications" value="1" <?php checked( get_option( 'nv_wpmsp_email_notifications', 1 ), 1 ); ?> />
<?php esc_html_e( 'Receive email notifications', 'schedulify' ); ?>
</label>
<?php if ( get_option( 'nv_wpmsp_email_notifications', 1 ) ) : ?>
<br>
<label for="nv_wpmsp_admin_email">
<?php esc_html_e( 'Email Address:', 'schedulify' ); ?>
</label>
<input type="email" name="nv_wpmsp_admin_email" value="<?php echo esc_attr( get_option( 'nv_wpmsp_admin_email' ) ); ?>" />
<?php endif; ?>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php esc_html_e( 'Setting Custom Interval', 'schedulify' ); ?></th>
<td>
<label for="nv_wpmsp_custom_interval">
<?php esc_html_e( 'Choose Interval:', 'schedulify' ); ?>
</label>
<select name="nv_wpmsp_custom_interval" id="nv_wpmsp_custom_interval">
<?php
$selected_interval = get_option( 'nv_wpmsp_custom_interval', 15 );
$intervals = array( 5, 10, 15, 30, 60 );
foreach ( $intervals as $interval ) {
echo '<option value="' . esc_attr( $interval ) . '" ' . selected( $selected_interval, $interval, false ) . '>' . esc_html( $interval ) . ' ' . esc_html__( 'minutes', 'schedulify' ) . '</option>';
}
?>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php esc_html_e( 'User Roles Access', 'schedulify' ); ?></th>
<td>
<?php
$allowed_roles = get_option( 'nv_wpmsp_allowed_roles', array() );
$all_roles = wp_roles()->get_names();
foreach ( $all_roles as $role => $label ) :
?>
<label>
<input type="checkbox" name="nv_wpmsp_allowed_roles[]" value="<?php echo esc_attr( $role ); ?>" <?php checked( in_array( $role, $allowed_roles ), true ); ?> />
<?php echo esc_html( $label ); ?>
</label>
<br>
<?php endforeach; ?>
</td>
</tr>
</table>
<?php submit_button( esc_html__( 'Save Settings', 'schedulify' ) ); ?>
</form>
</div>
<?php
}
/**
* Render Cron Event Stats page
*/
function nv_wpmsp_render_cron_event_stats_page() {
?>
<div class="wrap">
<h1><?php esc_html_e( 'Cron Event Stats', 'schedulify' ); ?></h1>
<!-- Add your Cron Event Stats content here -->
</div>
<?php
}
/**
* Get the configured post limit
*
* @return int
*/
function nv_wpmsp_get_post_limit() {
return apply_filters( 'nv_wpmsp_post_limit', get_option( 'nv_wpmsp_post_limit', 20 ) );
}
/**
* Get the configured interval
*
* @return int
*/
function nv_wpmsp_get_interval() {
return apply_filters( 'nv_wpmsp_interval', get_option( 'nv_wpmsp_custom_interval', 15 ) * MINUTE_IN_SECONDS );
}
/**
* Get the configured email notifications status
*
* @return bool
*/
function nv_wpmsp_get_email_notifications() {
return apply_filters( 'nv_wpmsp_email_notifications', get_option( 'nv_wpmsp_email_notifications', true ) );
}