diff --git a/CHANGELOG.md b/CHANGELOG.md index b7e9b87..1260d68 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## [0.2.9] + +### Fixed + +- **Cron scheduling initialization**: Fixed an issue where the cron scheduling was not being initialized correctly, causing the incremental synchronization to not run as expected. +- **Cron schedule interval update**: Fixed an issue where the cron schedule interval was not being updated based on the plugin settings, ensuring that the synchronization interval is respected. + ## [0.2.8] ### Fixed diff --git a/bridge-directory/README.txt b/bridge-directory/README.txt index 0c66485..18b5dd6 100644 --- a/bridge-directory/README.txt +++ b/bridge-directory/README.txt @@ -3,7 +3,7 @@ Contributors: justinh-rahb Tags: real estate, bridge, brokerage, api Requires at least: 5.0 Tested up to: 6.6.2 -Stable tag: 0.2.8 +Stable tag: 0.2.9 License: GPLv2 License URI: http://www.gnu.org/licenses/gpl-2.0.html diff --git a/bridge-directory/bridge-directory.php b/bridge-directory/bridge-directory.php index 9f8135b..7bca65d 100644 --- a/bridge-directory/bridge-directory.php +++ b/bridge-directory/bridge-directory.php @@ -3,7 +3,7 @@ * Plugin Name: Bridge API Directory * Plugin URI: https://github.com/RAHB-REALTORS-Association/Bridge-Directory-WP * Description: Displays a searchable directory of offices using the Bridge Interactive API. - * Version: 0.2.8 + * Version: 0.2.9 * Author: Cornerstone Association of REALTORS * Author URI: https://www.cornerstone.inc * License: GPL-2.0 @@ -38,25 +38,13 @@ // Initialize Data Sync $data_sync = new Data_Sync( $db_handler ); -$data_sync->schedule_incremental_sync(); // Initialize AJAX Handler $ajax_handler = new AJAX_Handler( $search_handler ); -// Activation and Deactivation Hooks -register_activation_hook( __FILE__, [ 'BridgeDirectory\DB_Handler', 'activate' ] ); -register_deactivation_hook( __FILE__, [ 'BridgeDirectory\DB_Handler', 'deactivate' ] ); - -// Cron Hook for Incremental Sync -add_action( 'bridge_directory_incremental_sync', [ $data_sync, 'incremental_sync' ] ); - -// Add custom cron schedule -add_filter( 'cron_schedules', 'bridge_directory_custom_cron_schedule' ); -function bridge_directory_custom_cron_schedule( $schedules ) { - $interval = get_option( 'bridge_directory_sync_interval', 24 ) * HOUR_IN_SECONDS; - $schedules['bridge_directory_sync_interval'] = [ - 'interval' => $interval, - 'display' => 'Bridge Directory Sync Interval', - ]; - return $schedules; -} +/** + * Register Activation and Deactivation Hooks +* These hooks delegate scheduling tasks to the Data_Sync class. +*/ +register_activation_hook( __FILE__, [ $data_sync, 'activate_plugin' ] ); +register_deactivation_hook( __FILE__, [ $data_sync, 'deactivate_plugin' ] ); diff --git a/bridge-directory/includes/Data_Sync.php b/bridge-directory/includes/Data_Sync.php index 4556b62..2c12c96 100644 --- a/bridge-directory/includes/Data_Sync.php +++ b/bridge-directory/includes/Data_Sync.php @@ -10,9 +10,95 @@ class Data_Sync { public function __construct( $db_handler ) { $this->api_client = new API_Client(); $this->db_handler = $db_handler; + + // Hook into WordPress actions and filters + add_filter( 'cron_schedules', [ $this, 'add_custom_cron_schedule' ] ); + add_action( 'bridge_directory_incremental_sync', [ $this, 'incremental_sync' ] ); + add_action( 'update_option_bridge_directory_sync_interval', [ $this, 'sync_interval_updated' ], 10, 2 ); + } + + /** + * Define Custom Cron Schedule Based on Settings + */ + public function add_custom_cron_schedule( $schedules ) { + $interval_hours = get_option( 'bridge_directory_sync_interval', 24 ); + $interval_seconds = absint( $interval_hours ) * HOUR_IN_SECONDS; + + $schedules['bridge_directory_sync_interval'] = [ + 'interval' => $interval_seconds, + 'display' => sprintf( __( 'Every %d Hours', 'bridge-directory' ), $interval_hours ), + ]; + + return $schedules; + } + + /** + * Activation Hook: Schedule the Incremental Sync Cron Event + */ + public function activate_plugin() { + // Ensure the custom cron schedule is added before scheduling + $this->add_custom_cron_schedule( wp_get_schedules() ); + + $this->schedule_incremental_sync(); + + // Optionally, perform a full sync upon activation + $this->full_sync(); + } + + /** + * Deactivation Hook: Unschedule the Incremental Sync Cron Event + */ + public function deactivate_plugin() { + $this->unschedule_incremental_sync(); + } + + /** + * Schedule the Incremental Sync Cron Event + */ + public function schedule_incremental_sync() { + if ( ! wp_next_scheduled( 'bridge_directory_incremental_sync' ) ) { + wp_schedule_event( time(), 'bridge_directory_sync_interval', 'bridge_directory_incremental_sync' ); + error_log( 'Bridge Directory: Incremental sync scheduled.' ); + } else { + error_log( 'Bridge Directory: Incremental sync already scheduled.' ); + } } + /** + * Unschedule the Incremental Sync Cron Event + */ + public function unschedule_incremental_sync() { + $timestamp = wp_next_scheduled( 'bridge_directory_incremental_sync' ); + if ( $timestamp ) { + wp_unschedule_event( $timestamp, 'bridge_directory_incremental_sync' ); + error_log( 'Bridge Directory: Incremental sync unscheduled.' ); + } + } + + /** + * Reschedule the Incremental Sync Cron Event When Interval Changes + */ + public function sync_interval_updated( $old_value, $new_value ) { + if ( $old_value !== $new_value ) { + $this->reschedule_incremental_sync(); + error_log( 'Bridge Directory: Sync interval updated from ' . $old_value . ' to ' . $new_value . ' hours.' ); + } + } + + /** + * Reschedule the Incremental Sync Cron Event + */ + public function reschedule_incremental_sync() { + $this->unschedule_incremental_sync(); + $this->schedule_incremental_sync(); + error_log( 'Bridge Directory: Incremental sync rescheduled with new interval.' ); + } + + /** + * Perform a Full Synchronization + */ public function full_sync() { + error_log( 'Bridge Directory: Starting full sync.' ); $offices = $this->api_client->fetch_all_offices(); if ( is_wp_error( $offices ) ) { // Handle error (e.g., log it) @@ -21,9 +107,14 @@ public function full_sync() { } $this->db_handler->save_offices( $offices ); update_option( 'bridge_directory_last_full_sync', gmdate( 'Y-m-d\TH:i:s\Z' ) ); + error_log( 'Bridge Directory: Full sync completed.' ); } + /** + * Perform an Incremental Synchronization + */ public function incremental_sync() { + error_log( 'Bridge Directory: Starting incremental sync.' ); $last_sync = get_option( 'bridge_directory_last_sync', '1970-01-01T00:00:00Z' ); // Ensure the timestamp is in the correct format and in UTC @@ -34,6 +125,7 @@ public function incremental_sync() { if ( ! is_wp_error( $updated_offices ) ) { $this->db_handler->update_offices( $updated_offices ); + error_log( 'Bridge Directory: Updated offices synchronized.' ); } else { // Handle error (e.g., log it) error_log( 'Bridge Directory Incremental Sync Error (Updated Offices): ' . $updated_offices->get_error_message() ); @@ -41,21 +133,13 @@ public function incremental_sync() { if ( ! is_wp_error( $inactive_offices ) ) { $this->db_handler->remove_offices( $inactive_offices ); + error_log( 'Bridge Directory: Inactive offices removed.' ); } else { // Handle error (e.g., log it) error_log( 'Bridge Directory Incremental Sync Error (Inactive Offices): ' . $inactive_offices->get_error_message() ); } update_option( 'bridge_directory_last_sync', gmdate( 'Y-m-d\TH:i:s\Z' ) ); - } - - public function schedule_incremental_sync() { - if ( ! wp_next_scheduled( 'bridge_directory_incremental_sync' ) ) { - wp_schedule_event( time(), 'bridge_directory_sync_interval', 'bridge_directory_incremental_sync' ); - } - } - - public function unschedule_incremental_sync() { - wp_clear_scheduled_hook( 'bridge_directory_incremental_sync' ); + error_log( 'Bridge Directory: Incremental sync completed.' ); } } diff --git a/bridge-directory/package.json b/bridge-directory/package.json index 2d1c77a..b57cdd8 100644 --- a/bridge-directory/package.json +++ b/bridge-directory/package.json @@ -1,6 +1,6 @@ { "name": "bridge-directory", - "version": "0.2.8", + "version": "0.2.9", "description": "A WordPress plugin that displays a searchable directory of offices using the Bridge Interactive API.", "main": "index.js", "scripts": {