Skip to content

Commit

Permalink
Fix: cron
Browse files Browse the repository at this point in the history
  • Loading branch information
justinh-rahb committed Oct 3, 2024
1 parent 481d0a2 commit 1fa87ed
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 31 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion bridge-directory/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 7 additions & 19 deletions bridge-directory/bridge-directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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' ] );
104 changes: 94 additions & 10 deletions bridge-directory/includes/Data_Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -34,28 +125,21 @@ 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() );
}

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.' );
}
}
2 changes: 1 addition & 1 deletion bridge-directory/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit 1fa87ed

Please sign in to comment.