esc_html__('Number of items per page', 'simple-tags'),
'default' => 20,
'option' => 'st_autoterms_schedule_logs_per_page'
];
$this->logs_table = new SimpleTags_Schedule_Logs();
add_screen_option($option, $args);
}
public function save_autoterms_schedule_settings() {
if( !empty($_POST['taxopress_autoterm_schedule_test_run'])
&& !empty($_POST['_nonce'])
&& wp_verify_nonce(sanitize_text_field($_POST['_nonce']), 'taxopress_autoterm_schedule_nonce')
&& current_user_can('simple_tags')
) {
$autoterms_schedule = taxopress_get_autoterms_schedule_data();
$cron_schedule = isset($autoterms_schedule['cron_schedule']) ? $autoterms_schedule['cron_schedule'] : 'disable';
if ($cron_schedule === 'disable') {
add_action('admin_notices', function () {
echo taxopress_admin_notices_helper(
esc_html__('Schedule is disabled. Please enable a schedule frequency to run a test.', 'simple-tags'),
false
);
});
return;
}
$hook_name = 'taxopress_cron_autoterms_' . $cron_schedule;
wp_schedule_single_event(time(), $hook_name);
spawn_cron();
add_action('admin_notices', function () use ($hook_name) {
echo taxopress_admin_notices_helper(
sprintf(
esc_html__('Scheduled the cron event %s to run now. The original event will not be affected.', 'simple-tags'),
'' . esc_html($hook_name) . ''
),
true
);
});
return;
}
if( !empty($_POST['taxopress_autoterm_schedule_submit'])
&& !empty($_POST['_nonce'])
&& wp_verify_nonce(sanitize_text_field($_POST['_nonce']), 'taxopress_autoterm_schedule_nonce')
&& current_user_can('simple_tags')
) {
$auto_term_ids = !empty($_POST['taxopress_autoterm_schedule']['autoterm_id']) ? array_map('intval', (array)$_POST['taxopress_autoterm_schedule']['autoterm_id']) : [];
$cron_schedule = !empty($_POST['taxopress_autoterm_schedule']['cron_schedule']) ? taxopress_sanitize_text_field($_POST['taxopress_autoterm_schedule']['cron_schedule']) : 'disable';
$schedule_terms_batches = !empty($_POST['taxopress_autoterm_schedule']['schedule_terms_batches']) ? (int)$_POST['taxopress_autoterm_schedule']['schedule_terms_batches'] : '';
$schedule_terms_sleep = !empty($_POST['taxopress_autoterm_schedule']['schedule_terms_sleep']) ? (int)$_POST['taxopress_autoterm_schedule']['schedule_terms_sleep'] : '';
$schedule_terms_limit_days = !empty($_POST['taxopress_autoterm_schedule']['schedule_terms_limit_days']) ? taxopress_sanitize_text_field($_POST['taxopress_autoterm_schedule']['schedule_terms_limit_days']) : '';
$autoterm_schedule_exclude = !empty($_POST['taxopress_autoterm_schedule']['autoterm_schedule_exclude']) ? (int)$_POST['taxopress_autoterm_schedule']['autoterm_schedule_exclude'] : '';
$response_message = esc_html__('An error occured.', 'simple-tags');
$response_sucess = false;
if (empty($schedule_terms_batches)) {
$response_message = esc_html__('Limit per batches is required.', 'simple-tags');
} elseif (empty($schedule_terms_sleep)) {
$response_message = esc_html__('Batches wait time is required.', 'simple-tags');
} else {
$auto_term_schedule_settings = [
'autoterm_id' => $auto_term_ids,
'cron_schedule' => $cron_schedule,
'schedule_terms_batches' => $schedule_terms_batches,
'schedule_terms_sleep' => $schedule_terms_sleep,
'schedule_terms_limit_days' => $schedule_terms_limit_days,
'autoterm_schedule_exclude' => $autoterm_schedule_exclude,
];
update_option('taxopress_autoterms_schedule', $auto_term_schedule_settings);
wp_clear_scheduled_hook('taxopress_cron_autoterms_weekly');
do_action('taxopress_clear_schedule_cron_hooks');
if ($cron_schedule == 'weekly') {
wp_schedule_event(time(), 'weekly', 'taxopress_cron_autoterms_weekly');
}
do_action('taxopress_schedule_cron_events', $cron_schedule);
$autoterm_data = taxopress_get_autoterm_data();
$autoterm_data_selected = [];
if (!empty($auto_term_ids)) {
foreach ($auto_term_ids as $term_id) {
if (isset($autoterm_data[$term_id])) {
$autoterm_data_selected[$term_id] = $autoterm_data[$term_id];
}
}
}
$autoterm_data = $autoterm_data_selected;
$schedule_enabled = false;
foreach ($autoterm_data_selected as $term_data) {
if (!empty($term_data['autoterm_for_schedule'])) {
$schedule_enabled = true;
break;
}
}
if (!$schedule_enabled) {
$response_message = esc_html__('Schedule is not enabled for selected Auto Terms.', 'simple-tags');
$response_sucess = false;
} else {
$response_message = esc_html__('Settings updated successfully.', 'simple-tags');
$response_sucess = true;
}
}
add_action('admin_notices', function () use($response_message, $response_sucess) {
echo taxopress_admin_notices_helper($response_message, $response_sucess);
});
}
}
public function page_manage_autoterms_schedule()
{
if (!isset($_GET['order'])) {
$_GET['order'] = 'name-asc';
}
settings_errors(__CLASS__);
?>