prefix . 'aiapg_schedules'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching return $wpdb->get_results( "SELECT * FROM " . esc_sql($table_schedules) . " ORDER BY created_at DESC" ); } /** * Get schedule by ID * * @param int $schedule_id * @return object|null */ public function get_schedule($schedule_id) { global $wpdb; $table_schedules = $wpdb->prefix . 'aiapg_schedules'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . esc_sql($table_schedules) . " WHERE id = %d", $schedule_id ) ); } /** * Create new schedule * * @param array $data * @return int|false */ public function create_schedule($data) { global $wpdb; $table_schedules = $wpdb->prefix . 'aiapg_schedules'; // Debug logging if (get_option('aiapg_enable_debug_log', false)) { } $insert_data = array( 'name' => $data['name'], 'description' => $data['description'], 'categories' => maybe_serialize($data['categories']), 'custom_prompts' => maybe_serialize($data['custom_prompts']), 'posts_per_run' => $data['posts_per_run'], 'text_model' => $data['text_model'], 'image_model' => $data['image_model'], 'fallback_image_model' => $data['fallback_image_model'] ?? get_option('aiapg_default_fallback_image_model', 'pollinations'), 'enable_images' => $data['enable_images'] ? 1 : 0, 'image_placement' => $data['image_placement'], 'image_size' => $data['image_size'], 'images_per_post' => $data['images_per_post'], 'interval_type' => $data['interval_type'], 'interval_value' => $data['interval_value'], 'custom_cron' => $data['custom_cron'], 'is_active' => $data['is_active'] ? 1 : 0, 'next_run' => $this->calculate_next_run($data) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery $result = $wpdb->insert($table_schedules, $insert_data); if ($result) { $schedule_id = $wpdb->insert_id; // Schedule the cron event if active if ($data['is_active']) { $this->schedule_cron_event($schedule_id, $data); } return $schedule_id; } return false; } /** * Update existing schedule * * @param int $schedule_id * @param array $data * @return bool */ public function update_schedule($schedule_id, $data) { global $wpdb; $table_schedules = $wpdb->prefix . 'aiapg_schedules'; $update_data = array( 'name' => $data['name'], 'description' => $data['description'], 'categories' => maybe_serialize($data['categories']), 'custom_prompts' => maybe_serialize($data['custom_prompts']), 'posts_per_run' => $data['posts_per_run'], 'text_model' => $data['text_model'], 'image_model' => $data['image_model'], 'fallback_image_model' => $data['fallback_image_model'] ?? get_option('aiapg_default_fallback_image_model', 'pollinations'), 'enable_images' => $data['enable_images'] ? 1 : 0, 'image_placement' => $data['image_placement'], 'image_size' => $data['image_size'], 'images_per_post' => $data['images_per_post'], 'interval_type' => $data['interval_type'], 'interval_value' => $data['interval_value'], 'custom_cron' => $data['custom_cron'], 'is_active' => $data['is_active'] ? 1 : 0, 'next_run' => $this->calculate_next_run($data) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $result = $wpdb->update( $table_schedules, $update_data, array('id' => $schedule_id) ); if ($result !== false) { // Clear existing cron event wp_clear_scheduled_hook('aiapg_schedule_run', array($schedule_id)); // Schedule new cron event if active if ($data['is_active']) { $this->schedule_cron_event($schedule_id, $data); } return true; } return false; } /** * Delete schedule via AJAX */ public function delete_schedule() { check_ajax_referer('aiapg_nonce', 'nonce'); if (!current_user_can('manage_options')) { wp_die(esc_html__('You do not have permission to perform this action.', 'ai-auto-post-image-generator')); } $schedule_id = intval($_POST['schedule_id'] ?? 0); if ($schedule_id <= 0) { wp_send_json_error(__('Invalid schedule ID.', 'ai-auto-post-image-generator')); } $result = $this->delete_schedule_by_id($schedule_id); if ($result) { wp_send_json_success(__('Schedule deleted successfully!', 'ai-auto-post-image-generator')); } else { wp_send_json_error(__('Error deleting schedule. Please try again.', 'ai-auto-post-image-generator')); } } /** * Delete schedule by ID * * @param int $schedule_id * @return bool */ public function delete_schedule_by_id($schedule_id) { global $wpdb; $table_schedules = $wpdb->prefix . 'aiapg_schedules'; // Clear cron event wp_clear_scheduled_hook('aiapg_schedule_run', array($schedule_id)); // Delete schedule // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $result = $wpdb->delete( $table_schedules, array('id' => $schedule_id), array('%d') ); return $result !== false; } /** * Toggle schedule status via AJAX */ public function toggle_schedule() { check_ajax_referer('aiapg_nonce', 'nonce'); if (!current_user_can('manage_options')) { wp_die(esc_html__('You do not have permission to perform this action.', 'ai-auto-post-image-generator')); } $schedule_id = intval($_POST['schedule_id'] ?? 0); $is_active = isset($_POST['is_active']) ? (bool) $_POST['is_active'] : false; if ($schedule_id <= 0) { wp_send_json_error(__('Invalid schedule ID.', 'ai-auto-post-image-generator')); } $result = $this->toggle_schedule_status($schedule_id, $is_active); if ($result) { $status_text = $is_active ? __('activated', 'ai-auto-post-image-generator') : __('deactivated', 'ai-auto-post-image-generator'); wp_send_json_success(sprintf( /* translators: %s: Status text (activated/deactivated) */ __('Schedule %s successfully!', 'ai-auto-post-image-generator'), $status_text )); } else { wp_send_json_error(__('Error updating schedule status. Please try again.', 'ai-auto-post-image-generator')); } } /** * Toggle schedule status * * @param int $schedule_id * @param bool $is_active * @return bool */ public function toggle_schedule_status($schedule_id, $is_active) { global $wpdb; $table_schedules = $wpdb->prefix . 'aiapg_schedules'; $update_data = array( 'is_active' => $is_active ? 1 : 0 ); if ($is_active) { $schedule = $this->get_schedule($schedule_id); if ($schedule) { $schedule_data = array( 'interval_type' => $schedule->interval_type, 'interval_value' => $schedule->interval_value, 'custom_cron' => $schedule->custom_cron ); $update_data['next_run'] = $this->calculate_next_run($schedule_data); } } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $result = $wpdb->update( $table_schedules, $update_data, array('id' => $schedule_id) ); if ($result !== false) { // Clear existing cron event wp_clear_scheduled_hook('aiapg_schedule_run', array($schedule_id)); // Schedule new cron event if active if ($is_active && isset($schedule)) { $this->schedule_cron_event($schedule_id, (array) $schedule); } return true; } return false; } /** * Calculate next run time * * @param array $data * @return string */ private function calculate_next_run($data) { $now = current_time('timestamp'); switch ($data['interval_type']) { case 'hourly': $next_run = $now + ($data['interval_value'] * HOUR_IN_SECONDS); break; case 'daily': $next_run = $now + ($data['interval_value'] * DAY_IN_SECONDS); break; case 'weekly': $next_run = $now + ($data['interval_value'] * WEEK_IN_SECONDS); break; case 'monthly': $next_run = $now + ($data['interval_value'] * MONTH_IN_SECONDS); break; case 'custom': // For custom cron, we'll use the custom_cron field $next_run = $now + DAY_IN_SECONDS; // Default to tomorrow break; default: $next_run = $now + DAY_IN_SECONDS; } return gmdate('Y-m-d H:i:s', $next_run); } /** * Schedule cron event * * @param int $schedule_id * @param array $data */ private function schedule_cron_event($schedule_id, $data) { $hook = 'aiapg_schedule_run'; $args = array($schedule_id); // Calculate the next run time instead of using current time $next_run = $this->calculate_next_run($data); $next_run_timestamp = strtotime($next_run); switch ($data['interval_type']) { case 'hourly': wp_schedule_event($next_run_timestamp, 'hourly', $hook, $args); break; case 'daily': wp_schedule_event($next_run_timestamp, 'daily', $hook, $args); break; case 'weekly': wp_schedule_event($next_run_timestamp, 'weekly', $hook, $args); break; case 'monthly': wp_schedule_event($next_run_timestamp, 'monthly', $hook, $args); break; case 'custom': if (!empty($data['custom_cron'])) { wp_schedule_event($next_run_timestamp, $data['custom_cron'], $hook, $args); } break; } } /** * Import schedules * * @param array $schedules * @return bool */ public function import_schedules($schedules) { if (!is_array($schedules)) { return false; } foreach ($schedules as $schedule) { // Remove ID to create new schedule unset($schedule['id']); unset($schedule['created_at']); unset($schedule['updated_at']); unset($schedule['last_run']); unset($schedule['next_run']); $this->create_schedule($schedule); } return true; } /** * Get schedules that are due to run * * @return array */ public function get_due_schedules() { global $wpdb; $table_schedules = $wpdb->prefix . 'aiapg_schedules'; $now = current_time('mysql'); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM " . esc_sql($table_schedules) . " WHERE is_active = 1 AND (next_run IS NULL OR next_run <= %s)", $now ) ); } /** * Update last run time * * @param int $schedule_id * @param string $next_run * @return bool */ public function update_last_run($schedule_id, $next_run = null) { global $wpdb; $table_schedules = $wpdb->prefix . 'aiapg_schedules'; $update_data = array( 'last_run' => current_time('mysql') ); if ($next_run) { $update_data['next_run'] = $next_run; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $result = $wpdb->update( $table_schedules, $update_data, array('id' => $schedule_id) ); return $result !== false; } }