Skip to content

Commit

Permalink
Merge pull request #13 from iamsayan/develop
Browse files Browse the repository at this point in the history
Update to version 1.1.3
  • Loading branch information
iamsayan authored Jan 3, 2025
2 parents a93f6d8 + fee03ef commit 05f3e81
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 39 deletions.
3 changes: 2 additions & 1 deletion includes/Core/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
*/
class Admin
{
use Hooker, HelperFunctions;
use Hooker;
use HelperFunctions;

/**
* Register functions.
Expand Down
3 changes: 2 additions & 1 deletion includes/Core/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
*/
class Connection
{
use HelperFunctions, Scheduler;
use HelperFunctions;
use Scheduler;

/**
* List of Events.
Expand Down
113 changes: 86 additions & 27 deletions includes/Core/MigrateActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
*/
class MigrateActions
{
use HelperFunctions, Scheduler;
use HelperFunctions;
use Scheduler;

/**
* Register functions.
Expand All @@ -40,28 +41,39 @@ public function migrate_old_crons() {
}

$crons = _get_cron_array();
foreach ( $crons as $timestamp => $data ) {
foreach ( $data as $hook => $schedule ) {
foreach ( $schedule as $id => $info ) {
if ( in_array( $hook, $this->get_protected_hooks() ) ) {
if ( empty( $crons ) || ! is_array( $crons ) ) {
return;
}

$protected_hooks = $this->get_protected_hooks();

foreach ( $crons as $timestamp => $hooks ) {
if ( ! is_array( $hooks ) ) {
continue;
}

foreach ( $hooks as $hook => $schedules ) {
// Skip protected hooks early
if ( in_array( $hook, $protected_hooks, true ) ) {
continue;
}

foreach ( $schedules as $info ) {
if ( ! is_array( $info ) || ! isset( $info['args'] ) ) {
continue;
}

// Handle recurring events
if ( ! empty( $info['schedule'] ) && isset( $info['interval'] ) ) {
if ( ! $this->has_next_action( $hook, $info['args'] ) ) {
$this->set_recurring_action( $timestamp, $info['interval'], $hook, $info['args'] );
}
} else {
if ( empty( $this->get_next_action_by_data( $hook, $info['args'], $timestamp ) ) ) {
$this->set_single_action( $timestamp, $hook, $info['args'] );
}
$this->maybe_set_recurring_action( $timestamp, $hook, $info );
continue;
}

// remove pre scheduled crons
$this->remove_wp_cron( $timestamp, $hook, $info['args'] );
}
}
}
// Handle one-time events
$this->maybe_set_single_action( $timestamp, $hook, $info );
}
}
}
}

/**
Expand All @@ -73,22 +85,43 @@ public function regenerate_crons() {
return;
}

$statement = $wpdb->prepare( "SELECT a.action_id, a.hook, a.scheduled_date_gmt, a.args, g.slug AS `group` FROM {$wpdb->actionscheduler_actions} a LEFT JOIN {$wpdb->actionscheduler_groups} g ON a.group_id=g.group_id WHERE a.status=%s AND g.slug=%s", 'pending', 'mwpcac' );
$values = $wpdb->get_results( $statement, ARRAY_A ); // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared
$statement = $wpdb->prepare( "SELECT a.action_id, a.hook, a.scheduled_date_gmt, g.slug AS `group` FROM {$wpdb->actionscheduler_actions} a LEFT JOIN {$wpdb->actionscheduler_groups} g ON a.group_id=g.group_id WHERE a.status=%s AND g.slug=%s", 'pending', 'mwpcac' );
$values = $wpdb->get_results( $statement, ARRAY_A ); // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared

foreach ( $values as $key => $value ) {
$action = $this->get_action( $value['action_id'] );
if ( ! $action ) {
continue;
}

$values[ $key ]['args'] = $action->get_args();
$values[ $key ]['schedule'] = false;
$values[ $key ]['interval'] = 0;

$schedule = $action->get_schedule();
if ( $schedule && method_exists( $schedule, 'get_recurrence' ) ) {
$recurrence = (int) $schedule->get_recurrence();
$values[ $key ]['schedule'] = $this->get_schedule_by_interval( $recurrence );
$values[ $key ]['interval'] = $recurrence;
}
}

foreach ( $values as $value ) {
$this->generate_wp_cron( strtotime( $value['scheduled_date_gmt'] ), $value['hook'], json_decode( $value['args'], true ) );
$this->generate_wp_cron( strtotime( $value['scheduled_date_gmt'] ), $value['hook'], $value['args'], $value['schedule'], $value['interval'] );
$this->cancel_scheduled_action( $value['action_id'] );
}
}

/**
* Generate new single cron.
*
* @param int $timestamp Timestamp for when to run the event.
* @param string $hook Action hook, the execution of which will be unscheduled.
* @param array $args Arguments to pass to the hook's callback function.
* @param int $timestamp Timestamp for when to run the event.
* @param string $hook Action hook, the execution of which will be unscheduled.
* @param array $args Arguments to pass to the hook's callback function.
* @param string|false $schedule Schedule.
* @param int|null $interval Interval.
*/
private function generate_wp_cron( $timestamp, $hook, $args ) {
private function generate_wp_cron( $timestamp, $hook, $args, $schedule, $interval ) {
// get crons
$crons = _get_cron_array();
if ( ! is_array( $crons ) ) {
Expand All @@ -97,12 +130,18 @@ private function generate_wp_cron( $timestamp, $hook, $args ) {

// get keys
$key = md5( serialize( $args ) ); // PHPCS:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
$crons[ $timestamp ][ $hook ][ $key ] = [
'schedule' => false,

$cron = [
'schedule' => $schedule,
'args' => $args,
];

if ( ! empty( $interval ) ) {
$cron['interval'] = $interval;
}

$crons[ $timestamp ][ $hook ][ $key ] = $cron;

uksort( $crons, 'strnatcasecmp' );

// set cron array
Expand Down Expand Up @@ -136,4 +175,24 @@ private function remove_wp_cron( $timestamp, $hook, $args ) {
// set cron array
_set_cron_array( $crons );
}

/**
* Handle recurring action migration
*/
private function maybe_set_recurring_action( $timestamp, $hook, $info ) {
if ( ! $this->has_next_action( $hook, $info['args'] ) ) {
$this->set_recurring_action( $timestamp, $info['interval'], $hook, $info['args'] );
$this->remove_wp_cron( $timestamp, $hook, $info['args'] );
}
}

/**
* Handle single action migration
*/
private function maybe_set_single_action( $timestamp, $hook, $info ) {
if ( empty( $this->get_next_action_by_data( $hook, $info['args'], $timestamp ) ) ) {
$this->set_single_action( $timestamp, $hook, $info['args'] );
$this->remove_wp_cron( $timestamp, $hook, $info['args'] );
}
}
}
3 changes: 2 additions & 1 deletion includes/Core/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
*/
class Settings
{
use Hooker, HelperFunctions;
use Hooker;
use HelperFunctions;

/**
* Register functions.
Expand Down
2 changes: 1 addition & 1 deletion includes/Helpers/Hooker.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* Hooker class.
*/
trait Hooker
trait Hooker
{
/**
* Hooks a function on to a specific action
Expand Down
6 changes: 3 additions & 3 deletions migrate-wp-cron-to-action-scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Advanced Cron Scheduler for WordPress
* Plugin URI: https://wordpress.org/plugins/migrate-wp-cron-to-action-scheduler/
* Description: The Advanced Cron Scheduler plugin helps to easily replace or migrate Native WordPress Cron to the Action Scheduler Library.
* Version: 1.1.2
* Version: 1.1.3
* Author: Sayan Datta
* Author URI: https://www.sayandatta.co.in
* License: GPLv3
Expand Down Expand Up @@ -44,7 +44,7 @@ final class ACSWP {
*
* @var string
*/
public $version = '1.1.2';
public $version = '1.1.3';

/**
* Minimum version of WordPress required to run ACSWP.
Expand Down Expand Up @@ -180,7 +180,7 @@ private function define_constants() {
* Include the required files.
*/
private function includes() {
include dirname( __FILE__ ) . '/vendor/autoload.php';
include __DIR__ . '/vendor/autoload.php';
}

/**
Expand Down
10 changes: 6 additions & 4 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,17 @@
<!--<arg name="parallel" value="4" />-->

<!-- Configs -->
<config name="minimum_supported_wp_version" value="4.7" /> <!-- Minimum WP Version -->
<config name="testVersion" value="5.6" /> <!-- Minimum PHP Version -->
<config name="minimum_supported_wp_version" value="5.2" /> <!-- Minimum WP Version -->
<config name="testVersion" value="7.1" /> <!-- Minimum PHP Version -->
<config name="text_domain" value="migrate-wp-cron-to-action-scheduler"/>
<config name="installed_paths" value="D:\Laragon\wpcs" />

<!-- Rules -->
<rule ref="Squiz">
<exclude name="Squiz"/>
<exclude-pattern>index.php</exclude-pattern>
</rule>
<rule ref="WordPress">
<exclude name="WordPress.Files.FileName.InvalidClassFileName"/>
<exclude name="WordPress.Files.FileName.InvalidClassFileName"/>
<exclude name="WordPress.Files.FileName.NotHyphenatedLowercase"/>
<exclude name="WordPress.PHP.StrictInArray.MissingTrueStrict"/>
<exclude name="WordPress.PHP.StrictComparisons.LooseComparison"/>
Expand All @@ -45,6 +44,8 @@
<exclude name="WordPress.NamingConventions"/>
<exclude name="WordPress.DateTime.RestrictedFunctions.date_date"/>
<exclude name="WordPress.WP.I18n.MissingTranslatorsComment"/>
<exclude name="Universal.Arrays.DisallowShortArraySyntax"/>
<exclude name="WordPress.PHP.DevelopmentFunctions.error_log_error_log"/>
</rule>
<rule ref="WordPress-Core" />
<rule ref="WordPress-Extra" />
Expand Down Expand Up @@ -95,5 +96,6 @@
<exclude name="Generic.WhiteSpace.DisallowTabIndent.TabsUsed" />
<exclude name="Generic.PHP.ClosingPHPTag.NotFound" />
<exclude name="Generic.Files.LineLength.TooLong" />
<exclude name="Generic.Arrays.DisallowLongArraySyntax" />
</rule>
</ruleset>
8 changes: 7 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: infosatech
Tags: scheduler, wp cron, debug, cron manager, action scheduler
Requires at least: 5.2
Tested up to: 6.7
Stable tag: 1.1.2
Stable tag: 1.1.3
Requires PHP: 7.1
Donate link: https://www.paypal.me/iamsayan/
License: GPLv3
Expand Down Expand Up @@ -95,11 +95,17 @@ Yes, our plugins work independently of themes you are using. As long as your web

If you like Advanced Cron Scheduler, please take a moment to [give a 5-star rating](https://wordpress.org/support/plugin/migrate-wp-cron-to-action-scheduler/reviews/#new-post). It helps to keep development and support going strong. Thank you!

= 1.1.3 =
Release Date: 3rd January, 2025

* Fixed: Issue with migration of WP Cron events.

= 1.1.2 =
Release Date: 2nd January, 2025

* Added: Support for Customizing the Action Scheduler.
* Updated: Action Scheduler library to v3.9.0.
* Minimum PHP supported version is now 7.1.
* Tested up to WordPress 6.7.

= 1.1.1 =
Expand Down

0 comments on commit 05f3e81

Please sign in to comment.