<?php
if (!defined('ABSPATH')) exit;

add_filter('cron_schedules', 'acwbsrh_add_custom_cron_intervals');
function acwbsrh_add_custom_cron_intervals($schedules) {
    $schedules['Weekly'] = [
        'interval' => 604800,
        'display'  => __('Every 1 week','auto-content-writer-by-srh'),
    ];
    $schedules['Bi-Weekly'] = [
        'interval' => 1209600,
        'display'  => __('Every 2 week', 'auto-content-writer-by-srh'),
    ];
    $schedules['Monthly'] = [
        'interval' => 2419200,
        'display'  => __('Every 1 month', 'auto-content-writer-by-srh'),
    ];
    
    return $schedules;
}

register_deactivation_hook(__FILE__, 'acwbsrh_unschedule_auto_blogging_cron');
function acwbsrh_unschedule_auto_blogging_cron() {
    $acwbsrh_timestamp = wp_next_scheduled('acwbsrh_auto_generate_blog');
    if ($acwbsrh_timestamp) {
        wp_unschedule_event($acwbsrh_timestamp, 'acwbsrh_auto_generate_blog');
    }
}

add_action('admin_init', 'acwbsrh_handle_pause_cron');
function acwbsrh_handle_pause_cron() {
    if (isset($_POST['acwbsrh_pause_cron']) && check_admin_referer('acwbsrh_pause_cron_action')) {
        $acwbsrh_timestamp = wp_next_scheduled('acwbsrh_auto_generate_blog');
        if ($acwbsrh_timestamp) {
            wp_unschedule_event($acwbsrh_timestamp, 'acwbsrh_auto_generate_blog');
            add_settings_error('acwbsrh_schedule_messages', 'acwbsrh_cron_paused', 'Auto blogging schedule has been paused.', 'updated');
        } else {
            add_settings_error('acwbsrh_schedule_messages', 'acwbsrh_cron_not_found', 'No scheduled auto blogging task found.', 'notice');
        }
    }
}