Files
IQ-Dynamic-Google-Pricing/includes/class-informatiq-sp-scheduler.php

171 lines
4.1 KiB
PHP
Raw Permalink Normal View History

<?php
/**
* WP Cron scheduler for automated daily price updates.
*
* @package InformatiqSmartPricing
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Scheduler class.
*/
class Informatiq_SP_Scheduler {
/**
* Price updater instance.
*
* @var Informatiq_SP_Price_Updater
*/
private $price_updater;
/**
* Cron hook name.
*
* @var string
*/
const CRON_HOOK = 'informatiq_sp_daily_price_update';
/**
* Constructor.
*
* @param Informatiq_SP_Price_Updater $price_updater Price updater instance.
*/
public function __construct( $price_updater ) {
$this->price_updater = $price_updater;
// Register cron hook.
add_action( self::CRON_HOOK, array( $this, 'run_daily_update' ) );
// Add custom cron schedule if needed.
add_filter( 'cron_schedules', array( $this, 'add_custom_cron_schedule' ) );
}
/**
* Run daily price update.
*/
public function run_daily_update() {
// Check if we should run the update.
if ( ! $this->should_run_update() ) {
return;
}
// Set start time.
update_option( 'informatiq_sp_last_run_start', current_time( 'mysql' ) );
// Process all products.
$results = $this->price_updater->process_all_products();
// Set completion time and results.
update_option( 'informatiq_sp_last_run_end', current_time( 'mysql' ) );
update_option( 'informatiq_sp_last_run_results', $results );
}
/**
* Check if update should run.
*
* @return bool True if update should run.
*/
private function should_run_update() {
// Check if plugin is properly configured.
$merchant_id = get_option( 'informatiq_sp_merchant_id' );
$service_account = get_option( 'informatiq_sp_service_account' );
if ( empty( $merchant_id ) || empty( $service_account ) ) {
return false;
}
// Check if auto-update is enabled.
$auto_update_enabled = get_option( 'informatiq_sp_auto_update_enabled', '1' );
if ( $auto_update_enabled !== '1' ) {
return false;
}
return true;
}
/**
* Add custom cron schedule.
*
* @param array $schedules Existing schedules.
* @return array Modified schedules.
*/
public function add_custom_cron_schedule( $schedules ) {
// Add twice daily schedule.
$schedules['twice_daily'] = array(
'interval' => 12 * HOUR_IN_SECONDS,
'display' => __( 'Twice Daily', 'informatiq-smart-pricing' ),
);
// Add custom hourly schedule.
$schedules['every_6_hours'] = array(
'interval' => 6 * HOUR_IN_SECONDS,
'display' => __( 'Every 6 Hours', 'informatiq-smart-pricing' ),
);
return $schedules;
}
/**
* Get next scheduled run time.
*
* @return int|false Timestamp of next run or false if not scheduled.
*/
public static function get_next_run_time() {
return wp_next_scheduled( self::CRON_HOOK );
}
/**
* Get last run information.
*
* @return array Last run information.
*/
public static function get_last_run_info() {
$last_run_start = get_option( 'informatiq_sp_last_run_start' );
$last_run_end = get_option( 'informatiq_sp_last_run_end' );
$last_run_results = get_option( 'informatiq_sp_last_run_results', array() );
return array(
'start' => $last_run_start,
'end' => $last_run_end,
'results' => $last_run_results,
);
}
/**
* Manually trigger update (for admin interface).
*
* @return array Results of the update.
*/
public function trigger_manual_update() {
update_option( 'informatiq_sp_last_run_start', current_time( 'mysql' ) );
$results = $this->price_updater->process_all_products();
update_option( 'informatiq_sp_last_run_end', current_time( 'mysql' ) );
update_option( 'informatiq_sp_last_run_results', $results );
return $results;
}
/**
* Reschedule cron event with new frequency.
*
* @param string $frequency Frequency (daily, twice_daily, every_6_hours).
* @return bool True if rescheduled successfully.
*/
public static function reschedule( $frequency = 'daily' ) {
// Clear existing schedule.
$timestamp = wp_next_scheduled( self::CRON_HOOK );
if ( $timestamp ) {
wp_unschedule_event( $timestamp, self::CRON_HOOK );
}
// Schedule new event.
return wp_schedule_event( time(), $frequency, self::CRON_HOOK );
}
}