@@ -7,121 +7,121 @@ |
||
7 | 7 | */ |
8 | 8 | class ActionScheduler_WPCommentCleaner { |
9 | 9 | |
10 | - /** |
|
11 | - * Post migration hook used to cleanup the WP comment table. |
|
12 | - * |
|
13 | - * @var string |
|
14 | - */ |
|
15 | - protected static $cleanup_hook = 'action_scheduler/cleanup_wp_comment_logs'; |
|
16 | - |
|
17 | - /** |
|
18 | - * An instance of the ActionScheduler_wpCommentLogger class to interact with the comments table. |
|
19 | - * |
|
20 | - * This instance should only be used as an interface. It should not be initialized. |
|
21 | - * |
|
22 | - * @var ActionScheduler_wpCommentLogger |
|
23 | - */ |
|
24 | - protected static $wp_comment_logger = null; |
|
25 | - |
|
26 | - /** |
|
27 | - * The key used to store the cached value of whether there are logs in the WP comment table. |
|
28 | - * |
|
29 | - * @var string |
|
30 | - */ |
|
31 | - protected static $has_logs_option_key = 'as_has_wp_comment_logs'; |
|
32 | - |
|
33 | - /** |
|
34 | - * Initialize the class and attach callbacks. |
|
35 | - */ |
|
36 | - public static function init() { |
|
37 | - if ( empty( self::$wp_comment_logger ) ) { |
|
38 | - self::$wp_comment_logger = new ActionScheduler_wpCommentLogger(); |
|
39 | - } |
|
40 | - |
|
41 | - add_action( self::$cleanup_hook, array( __CLASS__, 'delete_all_action_comments' ) ); |
|
42 | - |
|
43 | - // While there are orphaned logs left in the comments table, we need to attach the callbacks which filter comment counts. |
|
44 | - add_action( 'pre_get_comments', array( self::$wp_comment_logger, 'filter_comment_queries' ), 10, 1 ); |
|
45 | - add_action( 'wp_count_comments', array( self::$wp_comment_logger, 'filter_comment_count' ), 20, 2 ); // run after WC_Comments::wp_count_comments() to make sure we exclude order notes and action logs |
|
46 | - add_action( 'comment_feed_where', array( self::$wp_comment_logger, 'filter_comment_feed' ), 10, 2 ); |
|
47 | - |
|
48 | - // Action Scheduler may be displayed as a Tools screen or WooCommerce > Status administration screen |
|
49 | - add_action( 'load-tools_page_action-scheduler', array( __CLASS__, 'register_admin_notice' ) ); |
|
50 | - add_action( 'load-woocommerce_page_wc-status', array( __CLASS__, 'register_admin_notice' ) ); |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * Determines if there are log entries in the wp comments table. |
|
55 | - * |
|
56 | - * Uses the flag set on migration completion set by @see self::maybe_schedule_cleanup(). |
|
57 | - * |
|
58 | - * @return boolean Whether there are scheduled action comments in the comments table. |
|
59 | - */ |
|
60 | - public static function has_logs() { |
|
61 | - return 'yes' === get_option( self::$has_logs_option_key ); |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * Schedules the WP Post comment table cleanup to run in 6 months if it's not already scheduled. |
|
66 | - * Attached to the migration complete hook 'action_scheduler/migration_complete'. |
|
67 | - */ |
|
68 | - public static function maybe_schedule_cleanup() { |
|
69 | - if ( (bool) get_comments( |
|
70 | - array( |
|
71 | - 'type' => ActionScheduler_wpCommentLogger::TYPE, |
|
72 | - 'number' => 1, |
|
73 | - 'fields' => 'ids', |
|
74 | - ) |
|
75 | - ) ) { |
|
76 | - update_option( self::$has_logs_option_key, 'yes' ); |
|
77 | - |
|
78 | - if ( ! as_next_scheduled_action( self::$cleanup_hook ) ) { |
|
79 | - as_schedule_single_action( gmdate( 'U' ) + ( 6 * MONTH_IN_SECONDS ), self::$cleanup_hook ); |
|
80 | - } |
|
81 | - } |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Delete all action comments from the WP Comments table. |
|
86 | - */ |
|
87 | - public static function delete_all_action_comments() { |
|
88 | - global $wpdb; |
|
89 | - $wpdb->delete( |
|
90 | - $wpdb->comments, |
|
91 | - array( |
|
92 | - 'comment_type' => ActionScheduler_wpCommentLogger::TYPE, |
|
93 | - 'comment_agent' => ActionScheduler_wpCommentLogger::AGENT, |
|
94 | - ) |
|
95 | - ); |
|
96 | - delete_option( self::$has_logs_option_key ); |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Registers admin notices about the orphaned action logs. |
|
101 | - */ |
|
102 | - public static function register_admin_notice() { |
|
103 | - add_action( 'admin_notices', array( __CLASS__, 'print_admin_notice' ) ); |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Prints details about the orphaned action logs and includes information on where to learn more. |
|
108 | - */ |
|
109 | - public static function print_admin_notice() { |
|
110 | - $next_cleanup_message = ''; |
|
111 | - $next_scheduled_cleanup_hook = as_next_scheduled_action( self::$cleanup_hook ); |
|
112 | - |
|
113 | - if ( $next_scheduled_cleanup_hook ) { |
|
114 | - /* translators: %s: date interval */ |
|
115 | - $next_cleanup_message = sprintf( __( 'This data will be deleted in %s.', 'action-scheduler' ), human_time_diff( gmdate( 'U' ), $next_scheduled_cleanup_hook ) ); |
|
116 | - } |
|
117 | - |
|
118 | - $notice = sprintf( |
|
119 | - /* translators: 1: next cleanup message 2: github issue URL */ |
|
120 | - __( 'Action Scheduler has migrated data to custom tables; however, orphaned log entries exist in the WordPress Comments table. %1$s <a href="%2$s">Learn more »</a>', 'action-scheduler' ), |
|
121 | - $next_cleanup_message, |
|
122 | - 'https://github.com/woocommerce/action-scheduler/issues/368' |
|
123 | - ); |
|
124 | - |
|
125 | - echo '<div class="notice notice-warning"><p>' . wp_kses_post( $notice ) . '</p></div>'; |
|
126 | - } |
|
10 | + /** |
|
11 | + * Post migration hook used to cleanup the WP comment table. |
|
12 | + * |
|
13 | + * @var string |
|
14 | + */ |
|
15 | + protected static $cleanup_hook = 'action_scheduler/cleanup_wp_comment_logs'; |
|
16 | + |
|
17 | + /** |
|
18 | + * An instance of the ActionScheduler_wpCommentLogger class to interact with the comments table. |
|
19 | + * |
|
20 | + * This instance should only be used as an interface. It should not be initialized. |
|
21 | + * |
|
22 | + * @var ActionScheduler_wpCommentLogger |
|
23 | + */ |
|
24 | + protected static $wp_comment_logger = null; |
|
25 | + |
|
26 | + /** |
|
27 | + * The key used to store the cached value of whether there are logs in the WP comment table. |
|
28 | + * |
|
29 | + * @var string |
|
30 | + */ |
|
31 | + protected static $has_logs_option_key = 'as_has_wp_comment_logs'; |
|
32 | + |
|
33 | + /** |
|
34 | + * Initialize the class and attach callbacks. |
|
35 | + */ |
|
36 | + public static function init() { |
|
37 | + if ( empty( self::$wp_comment_logger ) ) { |
|
38 | + self::$wp_comment_logger = new ActionScheduler_wpCommentLogger(); |
|
39 | + } |
|
40 | + |
|
41 | + add_action( self::$cleanup_hook, array( __CLASS__, 'delete_all_action_comments' ) ); |
|
42 | + |
|
43 | + // While there are orphaned logs left in the comments table, we need to attach the callbacks which filter comment counts. |
|
44 | + add_action( 'pre_get_comments', array( self::$wp_comment_logger, 'filter_comment_queries' ), 10, 1 ); |
|
45 | + add_action( 'wp_count_comments', array( self::$wp_comment_logger, 'filter_comment_count' ), 20, 2 ); // run after WC_Comments::wp_count_comments() to make sure we exclude order notes and action logs |
|
46 | + add_action( 'comment_feed_where', array( self::$wp_comment_logger, 'filter_comment_feed' ), 10, 2 ); |
|
47 | + |
|
48 | + // Action Scheduler may be displayed as a Tools screen or WooCommerce > Status administration screen |
|
49 | + add_action( 'load-tools_page_action-scheduler', array( __CLASS__, 'register_admin_notice' ) ); |
|
50 | + add_action( 'load-woocommerce_page_wc-status', array( __CLASS__, 'register_admin_notice' ) ); |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * Determines if there are log entries in the wp comments table. |
|
55 | + * |
|
56 | + * Uses the flag set on migration completion set by @see self::maybe_schedule_cleanup(). |
|
57 | + * |
|
58 | + * @return boolean Whether there are scheduled action comments in the comments table. |
|
59 | + */ |
|
60 | + public static function has_logs() { |
|
61 | + return 'yes' === get_option( self::$has_logs_option_key ); |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * Schedules the WP Post comment table cleanup to run in 6 months if it's not already scheduled. |
|
66 | + * Attached to the migration complete hook 'action_scheduler/migration_complete'. |
|
67 | + */ |
|
68 | + public static function maybe_schedule_cleanup() { |
|
69 | + if ( (bool) get_comments( |
|
70 | + array( |
|
71 | + 'type' => ActionScheduler_wpCommentLogger::TYPE, |
|
72 | + 'number' => 1, |
|
73 | + 'fields' => 'ids', |
|
74 | + ) |
|
75 | + ) ) { |
|
76 | + update_option( self::$has_logs_option_key, 'yes' ); |
|
77 | + |
|
78 | + if ( ! as_next_scheduled_action( self::$cleanup_hook ) ) { |
|
79 | + as_schedule_single_action( gmdate( 'U' ) + ( 6 * MONTH_IN_SECONDS ), self::$cleanup_hook ); |
|
80 | + } |
|
81 | + } |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * Delete all action comments from the WP Comments table. |
|
86 | + */ |
|
87 | + public static function delete_all_action_comments() { |
|
88 | + global $wpdb; |
|
89 | + $wpdb->delete( |
|
90 | + $wpdb->comments, |
|
91 | + array( |
|
92 | + 'comment_type' => ActionScheduler_wpCommentLogger::TYPE, |
|
93 | + 'comment_agent' => ActionScheduler_wpCommentLogger::AGENT, |
|
94 | + ) |
|
95 | + ); |
|
96 | + delete_option( self::$has_logs_option_key ); |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Registers admin notices about the orphaned action logs. |
|
101 | + */ |
|
102 | + public static function register_admin_notice() { |
|
103 | + add_action( 'admin_notices', array( __CLASS__, 'print_admin_notice' ) ); |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Prints details about the orphaned action logs and includes information on where to learn more. |
|
108 | + */ |
|
109 | + public static function print_admin_notice() { |
|
110 | + $next_cleanup_message = ''; |
|
111 | + $next_scheduled_cleanup_hook = as_next_scheduled_action( self::$cleanup_hook ); |
|
112 | + |
|
113 | + if ( $next_scheduled_cleanup_hook ) { |
|
114 | + /* translators: %s: date interval */ |
|
115 | + $next_cleanup_message = sprintf( __( 'This data will be deleted in %s.', 'action-scheduler' ), human_time_diff( gmdate( 'U' ), $next_scheduled_cleanup_hook ) ); |
|
116 | + } |
|
117 | + |
|
118 | + $notice = sprintf( |
|
119 | + /* translators: 1: next cleanup message 2: github issue URL */ |
|
120 | + __( 'Action Scheduler has migrated data to custom tables; however, orphaned log entries exist in the WordPress Comments table. %1$s <a href="%2$s">Learn more »</a>', 'action-scheduler' ), |
|
121 | + $next_cleanup_message, |
|
122 | + 'https://github.com/woocommerce/action-scheduler/issues/368' |
|
123 | + ); |
|
124 | + |
|
125 | + echo '<div class="notice notice-warning"><p>' . wp_kses_post( $notice ) . '</p></div>'; |
|
126 | + } |
|
127 | 127 | } |
@@ -34,20 +34,20 @@ discard block |
||
34 | 34 | * Initialize the class and attach callbacks. |
35 | 35 | */ |
36 | 36 | public static function init() { |
37 | - if ( empty( self::$wp_comment_logger ) ) { |
|
37 | + if (empty(self::$wp_comment_logger)) { |
|
38 | 38 | self::$wp_comment_logger = new ActionScheduler_wpCommentLogger(); |
39 | 39 | } |
40 | 40 | |
41 | - add_action( self::$cleanup_hook, array( __CLASS__, 'delete_all_action_comments' ) ); |
|
41 | + add_action(self::$cleanup_hook, array(__CLASS__, 'delete_all_action_comments')); |
|
42 | 42 | |
43 | 43 | // While there are orphaned logs left in the comments table, we need to attach the callbacks which filter comment counts. |
44 | - add_action( 'pre_get_comments', array( self::$wp_comment_logger, 'filter_comment_queries' ), 10, 1 ); |
|
45 | - add_action( 'wp_count_comments', array( self::$wp_comment_logger, 'filter_comment_count' ), 20, 2 ); // run after WC_Comments::wp_count_comments() to make sure we exclude order notes and action logs |
|
46 | - add_action( 'comment_feed_where', array( self::$wp_comment_logger, 'filter_comment_feed' ), 10, 2 ); |
|
44 | + add_action('pre_get_comments', array(self::$wp_comment_logger, 'filter_comment_queries'), 10, 1); |
|
45 | + add_action('wp_count_comments', array(self::$wp_comment_logger, 'filter_comment_count'), 20, 2); // run after WC_Comments::wp_count_comments() to make sure we exclude order notes and action logs |
|
46 | + add_action('comment_feed_where', array(self::$wp_comment_logger, 'filter_comment_feed'), 10, 2); |
|
47 | 47 | |
48 | 48 | // Action Scheduler may be displayed as a Tools screen or WooCommerce > Status administration screen |
49 | - add_action( 'load-tools_page_action-scheduler', array( __CLASS__, 'register_admin_notice' ) ); |
|
50 | - add_action( 'load-woocommerce_page_wc-status', array( __CLASS__, 'register_admin_notice' ) ); |
|
49 | + add_action('load-tools_page_action-scheduler', array(__CLASS__, 'register_admin_notice')); |
|
50 | + add_action('load-woocommerce_page_wc-status', array(__CLASS__, 'register_admin_notice')); |
|
51 | 51 | } |
52 | 52 | |
53 | 53 | /** |
@@ -58,7 +58,7 @@ discard block |
||
58 | 58 | * @return boolean Whether there are scheduled action comments in the comments table. |
59 | 59 | */ |
60 | 60 | public static function has_logs() { |
61 | - return 'yes' === get_option( self::$has_logs_option_key ); |
|
61 | + return 'yes' === get_option(self::$has_logs_option_key); |
|
62 | 62 | } |
63 | 63 | |
64 | 64 | /** |
@@ -66,17 +66,17 @@ discard block |
||
66 | 66 | * Attached to the migration complete hook 'action_scheduler/migration_complete'. |
67 | 67 | */ |
68 | 68 | public static function maybe_schedule_cleanup() { |
69 | - if ( (bool) get_comments( |
|
69 | + if ((bool) get_comments( |
|
70 | 70 | array( |
71 | 71 | 'type' => ActionScheduler_wpCommentLogger::TYPE, |
72 | 72 | 'number' => 1, |
73 | 73 | 'fields' => 'ids', |
74 | 74 | ) |
75 | - ) ) { |
|
76 | - update_option( self::$has_logs_option_key, 'yes' ); |
|
75 | + )) { |
|
76 | + update_option(self::$has_logs_option_key, 'yes'); |
|
77 | 77 | |
78 | - if ( ! as_next_scheduled_action( self::$cleanup_hook ) ) { |
|
79 | - as_schedule_single_action( gmdate( 'U' ) + ( 6 * MONTH_IN_SECONDS ), self::$cleanup_hook ); |
|
78 | + if ( ! as_next_scheduled_action(self::$cleanup_hook)) { |
|
79 | + as_schedule_single_action(gmdate('U') + (6 * MONTH_IN_SECONDS), self::$cleanup_hook); |
|
80 | 80 | } |
81 | 81 | } |
82 | 82 | } |
@@ -93,14 +93,14 @@ discard block |
||
93 | 93 | 'comment_agent' => ActionScheduler_wpCommentLogger::AGENT, |
94 | 94 | ) |
95 | 95 | ); |
96 | - delete_option( self::$has_logs_option_key ); |
|
96 | + delete_option(self::$has_logs_option_key); |
|
97 | 97 | } |
98 | 98 | |
99 | 99 | /** |
100 | 100 | * Registers admin notices about the orphaned action logs. |
101 | 101 | */ |
102 | 102 | public static function register_admin_notice() { |
103 | - add_action( 'admin_notices', array( __CLASS__, 'print_admin_notice' ) ); |
|
103 | + add_action('admin_notices', array(__CLASS__, 'print_admin_notice')); |
|
104 | 104 | } |
105 | 105 | |
106 | 106 | /** |
@@ -108,20 +108,20 @@ discard block |
||
108 | 108 | */ |
109 | 109 | public static function print_admin_notice() { |
110 | 110 | $next_cleanup_message = ''; |
111 | - $next_scheduled_cleanup_hook = as_next_scheduled_action( self::$cleanup_hook ); |
|
111 | + $next_scheduled_cleanup_hook = as_next_scheduled_action(self::$cleanup_hook); |
|
112 | 112 | |
113 | - if ( $next_scheduled_cleanup_hook ) { |
|
113 | + if ($next_scheduled_cleanup_hook) { |
|
114 | 114 | /* translators: %s: date interval */ |
115 | - $next_cleanup_message = sprintf( __( 'This data will be deleted in %s.', 'action-scheduler' ), human_time_diff( gmdate( 'U' ), $next_scheduled_cleanup_hook ) ); |
|
115 | + $next_cleanup_message = sprintf(__('This data will be deleted in %s.', 'action-scheduler'), human_time_diff(gmdate('U'), $next_scheduled_cleanup_hook)); |
|
116 | 116 | } |
117 | 117 | |
118 | 118 | $notice = sprintf( |
119 | 119 | /* translators: 1: next cleanup message 2: github issue URL */ |
120 | - __( 'Action Scheduler has migrated data to custom tables; however, orphaned log entries exist in the WordPress Comments table. %1$s <a href="%2$s">Learn more »</a>', 'action-scheduler' ), |
|
120 | + __('Action Scheduler has migrated data to custom tables; however, orphaned log entries exist in the WordPress Comments table. %1$s <a href="%2$s">Learn more »</a>', 'action-scheduler'), |
|
121 | 121 | $next_cleanup_message, |
122 | 122 | 'https://github.com/woocommerce/action-scheduler/issues/368' |
123 | 123 | ); |
124 | 124 | |
125 | - echo '<div class="notice notice-warning"><p>' . wp_kses_post( $notice ) . '</p></div>'; |
|
125 | + echo '<div class="notice notice-warning"><p>'.wp_kses_post($notice).'</p></div>'; |
|
126 | 126 | } |
127 | 127 | } |
@@ -5,257 +5,257 @@ |
||
5 | 5 | */ |
6 | 6 | class ActionScheduler_ActionFactory { |
7 | 7 | |
8 | - /** |
|
9 | - * Return stored actions for given params. |
|
10 | - * |
|
11 | - * @param string $status The action's status in the data store. |
|
12 | - * @param string $hook The hook to trigger when this action runs. |
|
13 | - * @param array $args Args to pass to callbacks when the hook is triggered. |
|
14 | - * @param ActionScheduler_Schedule $schedule The action's schedule. |
|
15 | - * @param string $group A group to put the action in. |
|
16 | - * |
|
17 | - * @return ActionScheduler_Action An instance of the stored action. |
|
18 | - */ |
|
19 | - public function get_stored_action( $status, $hook, array $args = array(), ActionScheduler_Schedule $schedule = null, $group = '' ) { |
|
8 | + /** |
|
9 | + * Return stored actions for given params. |
|
10 | + * |
|
11 | + * @param string $status The action's status in the data store. |
|
12 | + * @param string $hook The hook to trigger when this action runs. |
|
13 | + * @param array $args Args to pass to callbacks when the hook is triggered. |
|
14 | + * @param ActionScheduler_Schedule $schedule The action's schedule. |
|
15 | + * @param string $group A group to put the action in. |
|
16 | + * |
|
17 | + * @return ActionScheduler_Action An instance of the stored action. |
|
18 | + */ |
|
19 | + public function get_stored_action( $status, $hook, array $args = array(), ActionScheduler_Schedule $schedule = null, $group = '' ) { |
|
20 | 20 | |
21 | - switch ( $status ) { |
|
22 | - case ActionScheduler_Store::STATUS_PENDING: |
|
23 | - $action_class = 'ActionScheduler_Action'; |
|
24 | - break; |
|
25 | - case ActionScheduler_Store::STATUS_CANCELED: |
|
26 | - $action_class = 'ActionScheduler_CanceledAction'; |
|
27 | - if ( $schedule !== null && ! is_a( $schedule, 'ActionScheduler_CanceledSchedule' ) && ! is_a( $schedule, 'ActionScheduler_NullSchedule' ) ) { |
|
28 | - $schedule = new ActionScheduler_CanceledSchedule( $schedule->get_date() ); |
|
29 | - } |
|
30 | - break; |
|
31 | - default: |
|
32 | - $action_class = 'ActionScheduler_FinishedAction'; |
|
33 | - break; |
|
34 | - } |
|
21 | + switch ( $status ) { |
|
22 | + case ActionScheduler_Store::STATUS_PENDING: |
|
23 | + $action_class = 'ActionScheduler_Action'; |
|
24 | + break; |
|
25 | + case ActionScheduler_Store::STATUS_CANCELED: |
|
26 | + $action_class = 'ActionScheduler_CanceledAction'; |
|
27 | + if ( $schedule !== null && ! is_a( $schedule, 'ActionScheduler_CanceledSchedule' ) && ! is_a( $schedule, 'ActionScheduler_NullSchedule' ) ) { |
|
28 | + $schedule = new ActionScheduler_CanceledSchedule( $schedule->get_date() ); |
|
29 | + } |
|
30 | + break; |
|
31 | + default: |
|
32 | + $action_class = 'ActionScheduler_FinishedAction'; |
|
33 | + break; |
|
34 | + } |
|
35 | 35 | |
36 | - $action_class = apply_filters( 'action_scheduler_stored_action_class', $action_class, $status, $hook, $args, $schedule, $group ); |
|
36 | + $action_class = apply_filters( 'action_scheduler_stored_action_class', $action_class, $status, $hook, $args, $schedule, $group ); |
|
37 | 37 | |
38 | - $action = new $action_class( $hook, $args, $schedule, $group ); |
|
38 | + $action = new $action_class( $hook, $args, $schedule, $group ); |
|
39 | 39 | |
40 | - /** |
|
41 | - * Allow 3rd party code to change the instantiated action for a given hook, args, schedule and group. |
|
42 | - * |
|
43 | - * @param ActionScheduler_Action $action The instantiated action. |
|
44 | - * @param string $hook The instantiated action's hook. |
|
45 | - * @param array $args The instantiated action's args. |
|
46 | - * @param ActionScheduler_Schedule $schedule The instantiated action's schedule. |
|
47 | - * @param string $group The instantiated action's group. |
|
48 | - */ |
|
49 | - return apply_filters( 'action_scheduler_stored_action_instance', $action, $hook, $args, $schedule, $group ); |
|
50 | - } |
|
40 | + /** |
|
41 | + * Allow 3rd party code to change the instantiated action for a given hook, args, schedule and group. |
|
42 | + * |
|
43 | + * @param ActionScheduler_Action $action The instantiated action. |
|
44 | + * @param string $hook The instantiated action's hook. |
|
45 | + * @param array $args The instantiated action's args. |
|
46 | + * @param ActionScheduler_Schedule $schedule The instantiated action's schedule. |
|
47 | + * @param string $group The instantiated action's group. |
|
48 | + */ |
|
49 | + return apply_filters( 'action_scheduler_stored_action_instance', $action, $hook, $args, $schedule, $group ); |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * Enqueue an action to run one time, as soon as possible (rather a specific scheduled time). |
|
54 | - * |
|
55 | - * This method creates a new action with the NULLSchedule. This schedule maps to a MySQL datetime string of |
|
56 | - * 0000-00-00 00:00:00. This is done to create a psuedo "async action" type that is fully backward compatible. |
|
57 | - * Existing queries to claim actions claim by date, meaning actions scheduled for 0000-00-00 00:00:00 will |
|
58 | - * always be claimed prior to actions scheduled for a specific date. This makes sure that any async action is |
|
59 | - * given priority in queue processing. This has the added advantage of making sure async actions can be |
|
60 | - * claimed by both the existing WP Cron and WP CLI runners, as well as a new async request runner. |
|
61 | - * |
|
62 | - * @param string $hook The hook to trigger when this action runs. |
|
63 | - * @param array $args Args to pass when the hook is triggered. |
|
64 | - * @param string $group A group to put the action in. |
|
65 | - * |
|
66 | - * @return int The ID of the stored action. |
|
67 | - */ |
|
68 | - public function async( $hook, $args = array(), $group = '' ) { |
|
69 | - return $this->async_unique( $hook, $args, $group, false ); |
|
70 | - } |
|
52 | + /** |
|
53 | + * Enqueue an action to run one time, as soon as possible (rather a specific scheduled time). |
|
54 | + * |
|
55 | + * This method creates a new action with the NULLSchedule. This schedule maps to a MySQL datetime string of |
|
56 | + * 0000-00-00 00:00:00. This is done to create a psuedo "async action" type that is fully backward compatible. |
|
57 | + * Existing queries to claim actions claim by date, meaning actions scheduled for 0000-00-00 00:00:00 will |
|
58 | + * always be claimed prior to actions scheduled for a specific date. This makes sure that any async action is |
|
59 | + * given priority in queue processing. This has the added advantage of making sure async actions can be |
|
60 | + * claimed by both the existing WP Cron and WP CLI runners, as well as a new async request runner. |
|
61 | + * |
|
62 | + * @param string $hook The hook to trigger when this action runs. |
|
63 | + * @param array $args Args to pass when the hook is triggered. |
|
64 | + * @param string $group A group to put the action in. |
|
65 | + * |
|
66 | + * @return int The ID of the stored action. |
|
67 | + */ |
|
68 | + public function async( $hook, $args = array(), $group = '' ) { |
|
69 | + return $this->async_unique( $hook, $args, $group, false ); |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * Same as async, but also supports $unique param. |
|
74 | - * |
|
75 | - * @param string $hook The hook to trigger when this action runs. |
|
76 | - * @param array $args Args to pass when the hook is triggered. |
|
77 | - * @param string $group A group to put the action in. |
|
78 | - * @param bool $unique Whether to ensure the action is unique. |
|
79 | - * |
|
80 | - * @return int The ID of the stored action. |
|
81 | - */ |
|
82 | - public function async_unique( $hook, $args = array(), $group = '', $unique = true ) { |
|
83 | - $schedule = new ActionScheduler_NullSchedule(); |
|
84 | - $action = new ActionScheduler_Action( $hook, $args, $schedule, $group ); |
|
85 | - return $unique ? $this->store_unique_action( $action, $unique ) : $this->store( $action ); |
|
86 | - } |
|
72 | + /** |
|
73 | + * Same as async, but also supports $unique param. |
|
74 | + * |
|
75 | + * @param string $hook The hook to trigger when this action runs. |
|
76 | + * @param array $args Args to pass when the hook is triggered. |
|
77 | + * @param string $group A group to put the action in. |
|
78 | + * @param bool $unique Whether to ensure the action is unique. |
|
79 | + * |
|
80 | + * @return int The ID of the stored action. |
|
81 | + */ |
|
82 | + public function async_unique( $hook, $args = array(), $group = '', $unique = true ) { |
|
83 | + $schedule = new ActionScheduler_NullSchedule(); |
|
84 | + $action = new ActionScheduler_Action( $hook, $args, $schedule, $group ); |
|
85 | + return $unique ? $this->store_unique_action( $action, $unique ) : $this->store( $action ); |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * Create single action. |
|
90 | - * |
|
91 | - * @param string $hook The hook to trigger when this action runs. |
|
92 | - * @param array $args Args to pass when the hook is triggered. |
|
93 | - * @param int $when Unix timestamp when the action will run. |
|
94 | - * @param string $group A group to put the action in. |
|
95 | - * |
|
96 | - * @return int The ID of the stored action. |
|
97 | - */ |
|
98 | - public function single( $hook, $args = array(), $when = null, $group = '' ) { |
|
99 | - return $this->single_unique( $hook, $args, $when, $group, false ); |
|
100 | - } |
|
88 | + /** |
|
89 | + * Create single action. |
|
90 | + * |
|
91 | + * @param string $hook The hook to trigger when this action runs. |
|
92 | + * @param array $args Args to pass when the hook is triggered. |
|
93 | + * @param int $when Unix timestamp when the action will run. |
|
94 | + * @param string $group A group to put the action in. |
|
95 | + * |
|
96 | + * @return int The ID of the stored action. |
|
97 | + */ |
|
98 | + public function single( $hook, $args = array(), $when = null, $group = '' ) { |
|
99 | + return $this->single_unique( $hook, $args, $when, $group, false ); |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * Create single action only if there is no pending or running action with same name and params. |
|
104 | - * |
|
105 | - * @param string $hook The hook to trigger when this action runs. |
|
106 | - * @param array $args Args to pass when the hook is triggered. |
|
107 | - * @param int $when Unix timestamp when the action will run. |
|
108 | - * @param string $group A group to put the action in. |
|
109 | - * @param bool $unique Whether action scheduled should be unique. |
|
110 | - * |
|
111 | - * @return int The ID of the stored action. |
|
112 | - */ |
|
113 | - public function single_unique( $hook, $args = array(), $when = null, $group = '', $unique = true ) { |
|
114 | - $date = as_get_datetime_object( $when ); |
|
115 | - $schedule = new ActionScheduler_SimpleSchedule( $date ); |
|
116 | - $action = new ActionScheduler_Action( $hook, $args, $schedule, $group ); |
|
117 | - return $unique ? $this->store_unique_action( $action ) : $this->store( $action ); |
|
118 | - } |
|
102 | + /** |
|
103 | + * Create single action only if there is no pending or running action with same name and params. |
|
104 | + * |
|
105 | + * @param string $hook The hook to trigger when this action runs. |
|
106 | + * @param array $args Args to pass when the hook is triggered. |
|
107 | + * @param int $when Unix timestamp when the action will run. |
|
108 | + * @param string $group A group to put the action in. |
|
109 | + * @param bool $unique Whether action scheduled should be unique. |
|
110 | + * |
|
111 | + * @return int The ID of the stored action. |
|
112 | + */ |
|
113 | + public function single_unique( $hook, $args = array(), $when = null, $group = '', $unique = true ) { |
|
114 | + $date = as_get_datetime_object( $when ); |
|
115 | + $schedule = new ActionScheduler_SimpleSchedule( $date ); |
|
116 | + $action = new ActionScheduler_Action( $hook, $args, $schedule, $group ); |
|
117 | + return $unique ? $this->store_unique_action( $action ) : $this->store( $action ); |
|
118 | + } |
|
119 | 119 | |
120 | - /** |
|
121 | - * Create the first instance of an action recurring on a given interval. |
|
122 | - * |
|
123 | - * @param string $hook The hook to trigger when this action runs. |
|
124 | - * @param array $args Args to pass when the hook is triggered. |
|
125 | - * @param int $first Unix timestamp for the first run. |
|
126 | - * @param int $interval Seconds between runs. |
|
127 | - * @param string $group A group to put the action in. |
|
128 | - * |
|
129 | - * @return int The ID of the stored action. |
|
130 | - */ |
|
131 | - public function recurring( $hook, $args = array(), $first = null, $interval = null, $group = '' ) { |
|
132 | - return $this->recurring_unique( $hook, $args, $first, $interval, $group, false ); |
|
133 | - } |
|
120 | + /** |
|
121 | + * Create the first instance of an action recurring on a given interval. |
|
122 | + * |
|
123 | + * @param string $hook The hook to trigger when this action runs. |
|
124 | + * @param array $args Args to pass when the hook is triggered. |
|
125 | + * @param int $first Unix timestamp for the first run. |
|
126 | + * @param int $interval Seconds between runs. |
|
127 | + * @param string $group A group to put the action in. |
|
128 | + * |
|
129 | + * @return int The ID of the stored action. |
|
130 | + */ |
|
131 | + public function recurring( $hook, $args = array(), $first = null, $interval = null, $group = '' ) { |
|
132 | + return $this->recurring_unique( $hook, $args, $first, $interval, $group, false ); |
|
133 | + } |
|
134 | 134 | |
135 | - /** |
|
136 | - * Create the first instance of an action recurring on a given interval only if there is no pending or running action with same name and params. |
|
137 | - * |
|
138 | - * @param string $hook The hook to trigger when this action runs. |
|
139 | - * @param array $args Args to pass when the hook is triggered. |
|
140 | - * @param int $first Unix timestamp for the first run. |
|
141 | - * @param int $interval Seconds between runs. |
|
142 | - * @param string $group A group to put the action in. |
|
143 | - * @param bool $unique Whether action scheduled should be unique. |
|
144 | - * |
|
145 | - * @return int The ID of the stored action. |
|
146 | - */ |
|
147 | - public function recurring_unique( $hook, $args = array(), $first = null, $interval = null, $group = '', $unique = true ) { |
|
148 | - if ( empty( $interval ) ) { |
|
149 | - return $this->single_unique( $hook, $unique, $args, $first, $group ); |
|
150 | - } |
|
151 | - $date = as_get_datetime_object( $first ); |
|
152 | - $schedule = new ActionScheduler_IntervalSchedule( $date, $interval ); |
|
153 | - $action = new ActionScheduler_Action( $hook, $args, $schedule, $group ); |
|
154 | - return $unique ? $this->store_unique_action( $action ) : $this->store( $action ); |
|
155 | - } |
|
135 | + /** |
|
136 | + * Create the first instance of an action recurring on a given interval only if there is no pending or running action with same name and params. |
|
137 | + * |
|
138 | + * @param string $hook The hook to trigger when this action runs. |
|
139 | + * @param array $args Args to pass when the hook is triggered. |
|
140 | + * @param int $first Unix timestamp for the first run. |
|
141 | + * @param int $interval Seconds between runs. |
|
142 | + * @param string $group A group to put the action in. |
|
143 | + * @param bool $unique Whether action scheduled should be unique. |
|
144 | + * |
|
145 | + * @return int The ID of the stored action. |
|
146 | + */ |
|
147 | + public function recurring_unique( $hook, $args = array(), $first = null, $interval = null, $group = '', $unique = true ) { |
|
148 | + if ( empty( $interval ) ) { |
|
149 | + return $this->single_unique( $hook, $unique, $args, $first, $group ); |
|
150 | + } |
|
151 | + $date = as_get_datetime_object( $first ); |
|
152 | + $schedule = new ActionScheduler_IntervalSchedule( $date, $interval ); |
|
153 | + $action = new ActionScheduler_Action( $hook, $args, $schedule, $group ); |
|
154 | + return $unique ? $this->store_unique_action( $action ) : $this->store( $action ); |
|
155 | + } |
|
156 | 156 | |
157 | - /** |
|
158 | - * Create the first instance of an action recurring on a Cron schedule. |
|
159 | - * |
|
160 | - * @param string $hook The hook to trigger when this action runs. |
|
161 | - * @param array $args Args to pass when the hook is triggered. |
|
162 | - * @param int $base_timestamp The first instance of the action will be scheduled |
|
163 | - * to run at a time calculated after this timestamp matching the cron |
|
164 | - * expression. This can be used to delay the first instance of the action. |
|
165 | - * @param int $schedule A cron definition string. |
|
166 | - * @param string $group A group to put the action in. |
|
167 | - * |
|
168 | - * @return int The ID of the stored action. |
|
169 | - */ |
|
170 | - public function cron( $hook, $args = array(), $base_timestamp = null, $schedule = null, $group = '' ) { |
|
171 | - return $this->cron_unique( $hook, $args, $base_timestamp, $schedule, $group, false ); |
|
172 | - } |
|
157 | + /** |
|
158 | + * Create the first instance of an action recurring on a Cron schedule. |
|
159 | + * |
|
160 | + * @param string $hook The hook to trigger when this action runs. |
|
161 | + * @param array $args Args to pass when the hook is triggered. |
|
162 | + * @param int $base_timestamp The first instance of the action will be scheduled |
|
163 | + * to run at a time calculated after this timestamp matching the cron |
|
164 | + * expression. This can be used to delay the first instance of the action. |
|
165 | + * @param int $schedule A cron definition string. |
|
166 | + * @param string $group A group to put the action in. |
|
167 | + * |
|
168 | + * @return int The ID of the stored action. |
|
169 | + */ |
|
170 | + public function cron( $hook, $args = array(), $base_timestamp = null, $schedule = null, $group = '' ) { |
|
171 | + return $this->cron_unique( $hook, $args, $base_timestamp, $schedule, $group, false ); |
|
172 | + } |
|
173 | 173 | |
174 | - /** |
|
175 | - * Create the first instance of an action recurring on a Cron schedule only if there is no pending or running action with same name and params. |
|
176 | - * |
|
177 | - * @param string $hook The hook to trigger when this action runs. |
|
178 | - * @param array $args Args to pass when the hook is triggered. |
|
179 | - * @param int $base_timestamp The first instance of the action will be scheduled |
|
180 | - * to run at a time calculated after this timestamp matching the cron |
|
181 | - * expression. This can be used to delay the first instance of the action. |
|
182 | - * @param int $schedule A cron definition string. |
|
183 | - * @param string $group A group to put the action in. |
|
184 | - * @param bool $unique Whether action scheduled should be unique. |
|
185 | - * |
|
186 | - * @return int The ID of the stored action. |
|
187 | - **/ |
|
188 | - public function cron_unique( $hook, $args = array(), $base_timestamp = null, $schedule = null, $group = '', $unique = true ) { |
|
189 | - if ( empty( $schedule ) ) { |
|
190 | - return $this->single_unique( $hook, $unique, $args, $base_timestamp, $group ); |
|
191 | - } |
|
192 | - $date = as_get_datetime_object( $base_timestamp ); |
|
193 | - $cron = CronExpression::factory( $schedule ); |
|
194 | - $schedule = new ActionScheduler_CronSchedule( $date, $cron ); |
|
195 | - $action = new ActionScheduler_Action( $hook, $args, $schedule, $group ); |
|
196 | - return $unique ? $this->store_unique_action( $action ) : $this->store( $action ); |
|
197 | - } |
|
174 | + /** |
|
175 | + * Create the first instance of an action recurring on a Cron schedule only if there is no pending or running action with same name and params. |
|
176 | + * |
|
177 | + * @param string $hook The hook to trigger when this action runs. |
|
178 | + * @param array $args Args to pass when the hook is triggered. |
|
179 | + * @param int $base_timestamp The first instance of the action will be scheduled |
|
180 | + * to run at a time calculated after this timestamp matching the cron |
|
181 | + * expression. This can be used to delay the first instance of the action. |
|
182 | + * @param int $schedule A cron definition string. |
|
183 | + * @param string $group A group to put the action in. |
|
184 | + * @param bool $unique Whether action scheduled should be unique. |
|
185 | + * |
|
186 | + * @return int The ID of the stored action. |
|
187 | + **/ |
|
188 | + public function cron_unique( $hook, $args = array(), $base_timestamp = null, $schedule = null, $group = '', $unique = true ) { |
|
189 | + if ( empty( $schedule ) ) { |
|
190 | + return $this->single_unique( $hook, $unique, $args, $base_timestamp, $group ); |
|
191 | + } |
|
192 | + $date = as_get_datetime_object( $base_timestamp ); |
|
193 | + $cron = CronExpression::factory( $schedule ); |
|
194 | + $schedule = new ActionScheduler_CronSchedule( $date, $cron ); |
|
195 | + $action = new ActionScheduler_Action( $hook, $args, $schedule, $group ); |
|
196 | + return $unique ? $this->store_unique_action( $action ) : $this->store( $action ); |
|
197 | + } |
|
198 | 198 | |
199 | - /** |
|
200 | - * Create a successive instance of a recurring or cron action. |
|
201 | - * |
|
202 | - * Importantly, the action will be rescheduled to run based on the current date/time. |
|
203 | - * That means when the action is scheduled to run in the past, the next scheduled date |
|
204 | - * will be pushed forward. For example, if a recurring action set to run every hour |
|
205 | - * was scheduled to run 5 seconds ago, it will be next scheduled for 1 hour in the |
|
206 | - * future, which is 1 hour and 5 seconds from when it was last scheduled to run. |
|
207 | - * |
|
208 | - * Alternatively, if the action is scheduled to run in the future, and is run early, |
|
209 | - * likely via manual intervention, then its schedule will change based on the time now. |
|
210 | - * For example, if a recurring action set to run every day, and is run 12 hours early, |
|
211 | - * it will run again in 24 hours, not 36 hours. |
|
212 | - * |
|
213 | - * This slippage is less of an issue with Cron actions, as the specific run time can |
|
214 | - * be set for them to run, e.g. 1am each day. In those cases, and entire period would |
|
215 | - * need to be missed before there was any change is scheduled, e.g. in the case of an |
|
216 | - * action scheduled for 1am each day, the action would need to run an entire day late. |
|
217 | - * |
|
218 | - * @param ActionScheduler_Action $action The existing action. |
|
219 | - * |
|
220 | - * @return string The ID of the stored action |
|
221 | - * @throws InvalidArgumentException If $action is not a recurring action. |
|
222 | - */ |
|
223 | - public function repeat( $action ) { |
|
224 | - $schedule = $action->get_schedule(); |
|
225 | - $next = $schedule->get_next( as_get_datetime_object() ); |
|
199 | + /** |
|
200 | + * Create a successive instance of a recurring or cron action. |
|
201 | + * |
|
202 | + * Importantly, the action will be rescheduled to run based on the current date/time. |
|
203 | + * That means when the action is scheduled to run in the past, the next scheduled date |
|
204 | + * will be pushed forward. For example, if a recurring action set to run every hour |
|
205 | + * was scheduled to run 5 seconds ago, it will be next scheduled for 1 hour in the |
|
206 | + * future, which is 1 hour and 5 seconds from when it was last scheduled to run. |
|
207 | + * |
|
208 | + * Alternatively, if the action is scheduled to run in the future, and is run early, |
|
209 | + * likely via manual intervention, then its schedule will change based on the time now. |
|
210 | + * For example, if a recurring action set to run every day, and is run 12 hours early, |
|
211 | + * it will run again in 24 hours, not 36 hours. |
|
212 | + * |
|
213 | + * This slippage is less of an issue with Cron actions, as the specific run time can |
|
214 | + * be set for them to run, e.g. 1am each day. In those cases, and entire period would |
|
215 | + * need to be missed before there was any change is scheduled, e.g. in the case of an |
|
216 | + * action scheduled for 1am each day, the action would need to run an entire day late. |
|
217 | + * |
|
218 | + * @param ActionScheduler_Action $action The existing action. |
|
219 | + * |
|
220 | + * @return string The ID of the stored action |
|
221 | + * @throws InvalidArgumentException If $action is not a recurring action. |
|
222 | + */ |
|
223 | + public function repeat( $action ) { |
|
224 | + $schedule = $action->get_schedule(); |
|
225 | + $next = $schedule->get_next( as_get_datetime_object() ); |
|
226 | 226 | |
227 | - if ( $next === null || ! $schedule->is_recurring() ) { |
|
228 | - throw new InvalidArgumentException( __( 'Invalid action - must be a recurring action.', 'action-scheduler' ) ); |
|
229 | - } |
|
227 | + if ( $next === null || ! $schedule->is_recurring() ) { |
|
228 | + throw new InvalidArgumentException( __( 'Invalid action - must be a recurring action.', 'action-scheduler' ) ); |
|
229 | + } |
|
230 | 230 | |
231 | - $schedule_class = get_class( $schedule ); |
|
232 | - $new_schedule = new $schedule( $next, $schedule->get_recurrence(), $schedule->get_first_date() ); |
|
233 | - $new_action = new ActionScheduler_Action( $action->get_hook(), $action->get_args(), $new_schedule, $action->get_group() ); |
|
234 | - return $this->store( $new_action ); |
|
235 | - } |
|
231 | + $schedule_class = get_class( $schedule ); |
|
232 | + $new_schedule = new $schedule( $next, $schedule->get_recurrence(), $schedule->get_first_date() ); |
|
233 | + $new_action = new ActionScheduler_Action( $action->get_hook(), $action->get_args(), $new_schedule, $action->get_group() ); |
|
234 | + return $this->store( $new_action ); |
|
235 | + } |
|
236 | 236 | |
237 | - /** |
|
238 | - * Save action to database. |
|
239 | - * |
|
240 | - * @param ActionScheduler_Action $action Action object to save. |
|
241 | - * |
|
242 | - * @return int The ID of the stored action |
|
243 | - */ |
|
244 | - protected function store( ActionScheduler_Action $action ) { |
|
245 | - $store = ActionScheduler_Store::instance(); |
|
246 | - return $store->save_action( $action ); |
|
247 | - } |
|
237 | + /** |
|
238 | + * Save action to database. |
|
239 | + * |
|
240 | + * @param ActionScheduler_Action $action Action object to save. |
|
241 | + * |
|
242 | + * @return int The ID of the stored action |
|
243 | + */ |
|
244 | + protected function store( ActionScheduler_Action $action ) { |
|
245 | + $store = ActionScheduler_Store::instance(); |
|
246 | + return $store->save_action( $action ); |
|
247 | + } |
|
248 | 248 | |
249 | - /** |
|
250 | - * Store action if it's unique. |
|
251 | - * |
|
252 | - * @param ActionScheduler_Action $action Action object to store. |
|
253 | - * |
|
254 | - * @return int ID of the created action. Will be 0 if action was not created. |
|
255 | - */ |
|
256 | - protected function store_unique_action( ActionScheduler_Action $action ) { |
|
257 | - $store = ActionScheduler_Store::instance(); |
|
258 | - return method_exists( $store, 'save_unique_action' ) ? |
|
259 | - $store->save_unique_action( $action ) : $store->save_action( $action ); |
|
260 | - } |
|
249 | + /** |
|
250 | + * Store action if it's unique. |
|
251 | + * |
|
252 | + * @param ActionScheduler_Action $action Action object to store. |
|
253 | + * |
|
254 | + * @return int ID of the created action. Will be 0 if action was not created. |
|
255 | + */ |
|
256 | + protected function store_unique_action( ActionScheduler_Action $action ) { |
|
257 | + $store = ActionScheduler_Store::instance(); |
|
258 | + return method_exists( $store, 'save_unique_action' ) ? |
|
259 | + $store->save_unique_action( $action ) : $store->save_action( $action ); |
|
260 | + } |
|
261 | 261 | } |
@@ -16,16 +16,16 @@ discard block |
||
16 | 16 | * |
17 | 17 | * @return ActionScheduler_Action An instance of the stored action. |
18 | 18 | */ |
19 | - public function get_stored_action( $status, $hook, array $args = array(), ActionScheduler_Schedule $schedule = null, $group = '' ) { |
|
19 | + public function get_stored_action($status, $hook, array $args = array(), ActionScheduler_Schedule $schedule = null, $group = '') { |
|
20 | 20 | |
21 | - switch ( $status ) { |
|
21 | + switch ($status) { |
|
22 | 22 | case ActionScheduler_Store::STATUS_PENDING: |
23 | 23 | $action_class = 'ActionScheduler_Action'; |
24 | 24 | break; |
25 | 25 | case ActionScheduler_Store::STATUS_CANCELED: |
26 | 26 | $action_class = 'ActionScheduler_CanceledAction'; |
27 | - if ( $schedule !== null && ! is_a( $schedule, 'ActionScheduler_CanceledSchedule' ) && ! is_a( $schedule, 'ActionScheduler_NullSchedule' ) ) { |
|
28 | - $schedule = new ActionScheduler_CanceledSchedule( $schedule->get_date() ); |
|
27 | + if ($schedule !== null && ! is_a($schedule, 'ActionScheduler_CanceledSchedule') && ! is_a($schedule, 'ActionScheduler_NullSchedule')) { |
|
28 | + $schedule = new ActionScheduler_CanceledSchedule($schedule->get_date()); |
|
29 | 29 | } |
30 | 30 | break; |
31 | 31 | default: |
@@ -33,9 +33,9 @@ discard block |
||
33 | 33 | break; |
34 | 34 | } |
35 | 35 | |
36 | - $action_class = apply_filters( 'action_scheduler_stored_action_class', $action_class, $status, $hook, $args, $schedule, $group ); |
|
36 | + $action_class = apply_filters('action_scheduler_stored_action_class', $action_class, $status, $hook, $args, $schedule, $group); |
|
37 | 37 | |
38 | - $action = new $action_class( $hook, $args, $schedule, $group ); |
|
38 | + $action = new $action_class($hook, $args, $schedule, $group); |
|
39 | 39 | |
40 | 40 | /** |
41 | 41 | * Allow 3rd party code to change the instantiated action for a given hook, args, schedule and group. |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | * @param ActionScheduler_Schedule $schedule The instantiated action's schedule. |
47 | 47 | * @param string $group The instantiated action's group. |
48 | 48 | */ |
49 | - return apply_filters( 'action_scheduler_stored_action_instance', $action, $hook, $args, $schedule, $group ); |
|
49 | + return apply_filters('action_scheduler_stored_action_instance', $action, $hook, $args, $schedule, $group); |
|
50 | 50 | } |
51 | 51 | |
52 | 52 | /** |
@@ -65,8 +65,8 @@ discard block |
||
65 | 65 | * |
66 | 66 | * @return int The ID of the stored action. |
67 | 67 | */ |
68 | - public function async( $hook, $args = array(), $group = '' ) { |
|
69 | - return $this->async_unique( $hook, $args, $group, false ); |
|
68 | + public function async($hook, $args = array(), $group = '') { |
|
69 | + return $this->async_unique($hook, $args, $group, false); |
|
70 | 70 | } |
71 | 71 | |
72 | 72 | /** |
@@ -79,10 +79,10 @@ discard block |
||
79 | 79 | * |
80 | 80 | * @return int The ID of the stored action. |
81 | 81 | */ |
82 | - public function async_unique( $hook, $args = array(), $group = '', $unique = true ) { |
|
82 | + public function async_unique($hook, $args = array(), $group = '', $unique = true) { |
|
83 | 83 | $schedule = new ActionScheduler_NullSchedule(); |
84 | - $action = new ActionScheduler_Action( $hook, $args, $schedule, $group ); |
|
85 | - return $unique ? $this->store_unique_action( $action, $unique ) : $this->store( $action ); |
|
84 | + $action = new ActionScheduler_Action($hook, $args, $schedule, $group); |
|
85 | + return $unique ? $this->store_unique_action($action, $unique) : $this->store($action); |
|
86 | 86 | } |
87 | 87 | |
88 | 88 | /** |
@@ -95,8 +95,8 @@ discard block |
||
95 | 95 | * |
96 | 96 | * @return int The ID of the stored action. |
97 | 97 | */ |
98 | - public function single( $hook, $args = array(), $when = null, $group = '' ) { |
|
99 | - return $this->single_unique( $hook, $args, $when, $group, false ); |
|
98 | + public function single($hook, $args = array(), $when = null, $group = '') { |
|
99 | + return $this->single_unique($hook, $args, $when, $group, false); |
|
100 | 100 | } |
101 | 101 | |
102 | 102 | /** |
@@ -110,11 +110,11 @@ discard block |
||
110 | 110 | * |
111 | 111 | * @return int The ID of the stored action. |
112 | 112 | */ |
113 | - public function single_unique( $hook, $args = array(), $when = null, $group = '', $unique = true ) { |
|
114 | - $date = as_get_datetime_object( $when ); |
|
115 | - $schedule = new ActionScheduler_SimpleSchedule( $date ); |
|
116 | - $action = new ActionScheduler_Action( $hook, $args, $schedule, $group ); |
|
117 | - return $unique ? $this->store_unique_action( $action ) : $this->store( $action ); |
|
113 | + public function single_unique($hook, $args = array(), $when = null, $group = '', $unique = true) { |
|
114 | + $date = as_get_datetime_object($when); |
|
115 | + $schedule = new ActionScheduler_SimpleSchedule($date); |
|
116 | + $action = new ActionScheduler_Action($hook, $args, $schedule, $group); |
|
117 | + return $unique ? $this->store_unique_action($action) : $this->store($action); |
|
118 | 118 | } |
119 | 119 | |
120 | 120 | /** |
@@ -128,8 +128,8 @@ discard block |
||
128 | 128 | * |
129 | 129 | * @return int The ID of the stored action. |
130 | 130 | */ |
131 | - public function recurring( $hook, $args = array(), $first = null, $interval = null, $group = '' ) { |
|
132 | - return $this->recurring_unique( $hook, $args, $first, $interval, $group, false ); |
|
131 | + public function recurring($hook, $args = array(), $first = null, $interval = null, $group = '') { |
|
132 | + return $this->recurring_unique($hook, $args, $first, $interval, $group, false); |
|
133 | 133 | } |
134 | 134 | |
135 | 135 | /** |
@@ -144,14 +144,14 @@ discard block |
||
144 | 144 | * |
145 | 145 | * @return int The ID of the stored action. |
146 | 146 | */ |
147 | - public function recurring_unique( $hook, $args = array(), $first = null, $interval = null, $group = '', $unique = true ) { |
|
148 | - if ( empty( $interval ) ) { |
|
149 | - return $this->single_unique( $hook, $unique, $args, $first, $group ); |
|
147 | + public function recurring_unique($hook, $args = array(), $first = null, $interval = null, $group = '', $unique = true) { |
|
148 | + if (empty($interval)) { |
|
149 | + return $this->single_unique($hook, $unique, $args, $first, $group); |
|
150 | 150 | } |
151 | - $date = as_get_datetime_object( $first ); |
|
152 | - $schedule = new ActionScheduler_IntervalSchedule( $date, $interval ); |
|
153 | - $action = new ActionScheduler_Action( $hook, $args, $schedule, $group ); |
|
154 | - return $unique ? $this->store_unique_action( $action ) : $this->store( $action ); |
|
151 | + $date = as_get_datetime_object($first); |
|
152 | + $schedule = new ActionScheduler_IntervalSchedule($date, $interval); |
|
153 | + $action = new ActionScheduler_Action($hook, $args, $schedule, $group); |
|
154 | + return $unique ? $this->store_unique_action($action) : $this->store($action); |
|
155 | 155 | } |
156 | 156 | |
157 | 157 | /** |
@@ -167,8 +167,8 @@ discard block |
||
167 | 167 | * |
168 | 168 | * @return int The ID of the stored action. |
169 | 169 | */ |
170 | - public function cron( $hook, $args = array(), $base_timestamp = null, $schedule = null, $group = '' ) { |
|
171 | - return $this->cron_unique( $hook, $args, $base_timestamp, $schedule, $group, false ); |
|
170 | + public function cron($hook, $args = array(), $base_timestamp = null, $schedule = null, $group = '') { |
|
171 | + return $this->cron_unique($hook, $args, $base_timestamp, $schedule, $group, false); |
|
172 | 172 | } |
173 | 173 | |
174 | 174 | /** |
@@ -185,15 +185,15 @@ discard block |
||
185 | 185 | * |
186 | 186 | * @return int The ID of the stored action. |
187 | 187 | **/ |
188 | - public function cron_unique( $hook, $args = array(), $base_timestamp = null, $schedule = null, $group = '', $unique = true ) { |
|
189 | - if ( empty( $schedule ) ) { |
|
190 | - return $this->single_unique( $hook, $unique, $args, $base_timestamp, $group ); |
|
188 | + public function cron_unique($hook, $args = array(), $base_timestamp = null, $schedule = null, $group = '', $unique = true) { |
|
189 | + if (empty($schedule)) { |
|
190 | + return $this->single_unique($hook, $unique, $args, $base_timestamp, $group); |
|
191 | 191 | } |
192 | - $date = as_get_datetime_object( $base_timestamp ); |
|
193 | - $cron = CronExpression::factory( $schedule ); |
|
194 | - $schedule = new ActionScheduler_CronSchedule( $date, $cron ); |
|
195 | - $action = new ActionScheduler_Action( $hook, $args, $schedule, $group ); |
|
196 | - return $unique ? $this->store_unique_action( $action ) : $this->store( $action ); |
|
192 | + $date = as_get_datetime_object($base_timestamp); |
|
193 | + $cron = CronExpression::factory($schedule); |
|
194 | + $schedule = new ActionScheduler_CronSchedule($date, $cron); |
|
195 | + $action = new ActionScheduler_Action($hook, $args, $schedule, $group); |
|
196 | + return $unique ? $this->store_unique_action($action) : $this->store($action); |
|
197 | 197 | } |
198 | 198 | |
199 | 199 | /** |
@@ -220,18 +220,18 @@ discard block |
||
220 | 220 | * @return string The ID of the stored action |
221 | 221 | * @throws InvalidArgumentException If $action is not a recurring action. |
222 | 222 | */ |
223 | - public function repeat( $action ) { |
|
223 | + public function repeat($action) { |
|
224 | 224 | $schedule = $action->get_schedule(); |
225 | - $next = $schedule->get_next( as_get_datetime_object() ); |
|
225 | + $next = $schedule->get_next(as_get_datetime_object()); |
|
226 | 226 | |
227 | - if ( $next === null || ! $schedule->is_recurring() ) { |
|
228 | - throw new InvalidArgumentException( __( 'Invalid action - must be a recurring action.', 'action-scheduler' ) ); |
|
227 | + if ($next === null || ! $schedule->is_recurring()) { |
|
228 | + throw new InvalidArgumentException(__('Invalid action - must be a recurring action.', 'action-scheduler')); |
|
229 | 229 | } |
230 | 230 | |
231 | - $schedule_class = get_class( $schedule ); |
|
232 | - $new_schedule = new $schedule( $next, $schedule->get_recurrence(), $schedule->get_first_date() ); |
|
233 | - $new_action = new ActionScheduler_Action( $action->get_hook(), $action->get_args(), $new_schedule, $action->get_group() ); |
|
234 | - return $this->store( $new_action ); |
|
231 | + $schedule_class = get_class($schedule); |
|
232 | + $new_schedule = new $schedule($next, $schedule->get_recurrence(), $schedule->get_first_date()); |
|
233 | + $new_action = new ActionScheduler_Action($action->get_hook(), $action->get_args(), $new_schedule, $action->get_group()); |
|
234 | + return $this->store($new_action); |
|
235 | 235 | } |
236 | 236 | |
237 | 237 | /** |
@@ -241,9 +241,9 @@ discard block |
||
241 | 241 | * |
242 | 242 | * @return int The ID of the stored action |
243 | 243 | */ |
244 | - protected function store( ActionScheduler_Action $action ) { |
|
244 | + protected function store(ActionScheduler_Action $action) { |
|
245 | 245 | $store = ActionScheduler_Store::instance(); |
246 | - return $store->save_action( $action ); |
|
246 | + return $store->save_action($action); |
|
247 | 247 | } |
248 | 248 | |
249 | 249 | /** |
@@ -253,9 +253,9 @@ discard block |
||
253 | 253 | * |
254 | 254 | * @return int ID of the created action. Will be 0 if action was not created. |
255 | 255 | */ |
256 | - protected function store_unique_action( ActionScheduler_Action $action ) { |
|
256 | + protected function store_unique_action(ActionScheduler_Action $action) { |
|
257 | 257 | $store = ActionScheduler_Store::instance(); |
258 | - return method_exists( $store, 'save_unique_action' ) ? |
|
259 | - $store->save_unique_action( $action ) : $store->save_action( $action ); |
|
258 | + return method_exists($store, 'save_unique_action') ? |
|
259 | + $store->save_unique_action($action) : $store->save_action($action); |
|
260 | 260 | } |
261 | 261 | } |
@@ -9,299 +9,299 @@ |
||
9 | 9 | * @codeCoverageIgnore |
10 | 10 | */ |
11 | 11 | abstract class ActionScheduler { |
12 | - private static $plugin_file = ''; |
|
13 | - /** @var ActionScheduler_ActionFactory */ |
|
14 | - private static $factory = null; |
|
15 | - /** @var bool */ |
|
16 | - private static $data_store_initialized = false; |
|
17 | - |
|
18 | - public static function factory() { |
|
19 | - if ( ! isset( self::$factory ) ) { |
|
20 | - self::$factory = new ActionScheduler_ActionFactory(); |
|
21 | - } |
|
22 | - return self::$factory; |
|
23 | - } |
|
24 | - |
|
25 | - public static function store() { |
|
26 | - return ActionScheduler_Store::instance(); |
|
27 | - } |
|
28 | - |
|
29 | - public static function lock() { |
|
30 | - return ActionScheduler_Lock::instance(); |
|
31 | - } |
|
32 | - |
|
33 | - public static function logger() { |
|
34 | - return ActionScheduler_Logger::instance(); |
|
35 | - } |
|
36 | - |
|
37 | - public static function runner() { |
|
38 | - return ActionScheduler_QueueRunner::instance(); |
|
39 | - } |
|
40 | - |
|
41 | - public static function admin_view() { |
|
42 | - return ActionScheduler_AdminView::instance(); |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * Get the absolute system path to the plugin directory, or a file therein |
|
47 | - * |
|
48 | - * @static |
|
49 | - * @param string $path |
|
50 | - * @return string |
|
51 | - */ |
|
52 | - public static function plugin_path( $path ) { |
|
53 | - $base = dirname( self::$plugin_file ); |
|
54 | - if ( $path ) { |
|
55 | - return trailingslashit( $base ) . $path; |
|
56 | - } else { |
|
57 | - return untrailingslashit( $base ); |
|
58 | - } |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * Get the absolute URL to the plugin directory, or a file therein |
|
63 | - * |
|
64 | - * @static |
|
65 | - * @param string $path |
|
66 | - * @return string |
|
67 | - */ |
|
68 | - public static function plugin_url( $path ) { |
|
69 | - return plugins_url( $path, self::$plugin_file ); |
|
70 | - } |
|
71 | - |
|
72 | - public static function autoload( $class ) { |
|
73 | - $d = DIRECTORY_SEPARATOR; |
|
74 | - $classes_dir = self::plugin_path( 'classes' . $d ); |
|
75 | - $separator = strrpos( $class, '\\' ); |
|
76 | - if ( false !== $separator ) { |
|
77 | - if ( 0 !== strpos( $class, 'Action_Scheduler' ) ) { |
|
78 | - return; |
|
79 | - } |
|
80 | - $class = substr( $class, $separator + 1 ); |
|
81 | - } |
|
82 | - |
|
83 | - if ( 'Deprecated' === substr( $class, -10 ) ) { |
|
84 | - $dir = self::plugin_path( 'deprecated' . $d ); |
|
85 | - } elseif ( self::is_class_abstract( $class ) ) { |
|
86 | - $dir = $classes_dir . 'abstracts' . $d; |
|
87 | - } elseif ( self::is_class_migration( $class ) ) { |
|
88 | - $dir = $classes_dir . 'migration' . $d; |
|
89 | - } elseif ( 'Schedule' === substr( $class, -8 ) ) { |
|
90 | - $dir = $classes_dir . 'schedules' . $d; |
|
91 | - } elseif ( 'Action' === substr( $class, -6 ) ) { |
|
92 | - $dir = $classes_dir . 'actions' . $d; |
|
93 | - } elseif ( 'Schema' === substr( $class, -6 ) ) { |
|
94 | - $dir = $classes_dir . 'schema' . $d; |
|
95 | - } elseif ( strpos( $class, 'ActionScheduler' ) === 0 ) { |
|
96 | - $segments = explode( '_', $class ); |
|
97 | - $type = isset( $segments[1] ) ? $segments[1] : ''; |
|
98 | - |
|
99 | - switch ( $type ) { |
|
100 | - case 'WPCLI': |
|
101 | - $dir = $classes_dir . 'WP_CLI' . $d; |
|
102 | - break; |
|
103 | - case 'DBLogger': |
|
104 | - case 'DBStore': |
|
105 | - case 'HybridStore': |
|
106 | - case 'wpPostStore': |
|
107 | - case 'wpCommentLogger': |
|
108 | - $dir = $classes_dir . 'data-stores' . $d; |
|
109 | - break; |
|
110 | - default: |
|
111 | - $dir = $classes_dir; |
|
112 | - break; |
|
113 | - } |
|
114 | - } elseif ( self::is_class_cli( $class ) ) { |
|
115 | - $dir = $classes_dir . 'WP_CLI' . $d; |
|
116 | - } elseif ( strpos( $class, 'CronExpression' ) === 0 ) { |
|
117 | - $dir = self::plugin_path( 'lib' . $d . 'cron-expression' . $d ); |
|
118 | - } elseif ( strpos( $class, 'WP_Async_Request' ) === 0 ) { |
|
119 | - $dir = self::plugin_path( 'lib' . $d ); |
|
120 | - } else { |
|
121 | - return; |
|
122 | - } |
|
123 | - |
|
124 | - if ( file_exists( $dir . "{$class}.php" ) ) { |
|
125 | - include $dir . "{$class}.php"; |
|
126 | - return; |
|
127 | - } |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * Initialize the plugin |
|
132 | - * |
|
133 | - * @static |
|
134 | - * @param string $plugin_file |
|
135 | - */ |
|
136 | - public static function init( $plugin_file ) { |
|
137 | - self::$plugin_file = $plugin_file; |
|
138 | - spl_autoload_register( array( __CLASS__, 'autoload' ) ); |
|
139 | - |
|
140 | - /** |
|
141 | - * Fires in the early stages of Action Scheduler init hook. |
|
142 | - */ |
|
143 | - do_action( 'action_scheduler_pre_init' ); |
|
144 | - |
|
145 | - require_once self::plugin_path( 'functions.php' ); |
|
146 | - ActionScheduler_DataController::init(); |
|
147 | - |
|
148 | - $store = self::store(); |
|
149 | - $logger = self::logger(); |
|
150 | - $runner = self::runner(); |
|
151 | - $admin_view = self::admin_view(); |
|
152 | - |
|
153 | - // Ensure initialization on plugin activation. |
|
154 | - if ( ! did_action( 'init' ) ) { |
|
155 | - add_action( 'init', array( $admin_view, 'init' ), 0, 0 ); // run before $store::init() |
|
156 | - add_action( 'init', array( $store, 'init' ), 1, 0 ); |
|
157 | - add_action( 'init', array( $logger, 'init' ), 1, 0 ); |
|
158 | - add_action( 'init', array( $runner, 'init' ), 1, 0 ); |
|
159 | - } else { |
|
160 | - $admin_view->init(); |
|
161 | - $store->init(); |
|
162 | - $logger->init(); |
|
163 | - $runner->init(); |
|
164 | - } |
|
165 | - |
|
166 | - if ( apply_filters( 'action_scheduler_load_deprecated_functions', true ) ) { |
|
167 | - require_once self::plugin_path( 'deprecated/functions.php' ); |
|
168 | - } |
|
169 | - |
|
170 | - if ( defined( 'WP_CLI' ) && WP_CLI ) { |
|
171 | - WP_CLI::add_command( 'action-scheduler', 'ActionScheduler_WPCLI_Scheduler_command' ); |
|
172 | - if ( ! ActionScheduler_DataController::is_migration_complete() && Controller::instance()->allow_migration() ) { |
|
173 | - $command = new Migration_Command(); |
|
174 | - $command->register(); |
|
175 | - } |
|
176 | - } |
|
177 | - |
|
178 | - self::$data_store_initialized = true; |
|
179 | - |
|
180 | - /** |
|
181 | - * Handle WP comment cleanup after migration. |
|
182 | - */ |
|
183 | - if ( is_a( $logger, 'ActionScheduler_DBLogger' ) && ActionScheduler_DataController::is_migration_complete() && ActionScheduler_WPCommentCleaner::has_logs() ) { |
|
184 | - ActionScheduler_WPCommentCleaner::init(); |
|
185 | - } |
|
186 | - |
|
187 | - add_action( 'action_scheduler/migration_complete', 'ActionScheduler_WPCommentCleaner::maybe_schedule_cleanup' ); |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * Check whether the AS data store has been initialized. |
|
192 | - * |
|
193 | - * @param string $function_name The name of the function being called. Optional. Default `null`. |
|
194 | - * @return bool |
|
195 | - */ |
|
196 | - public static function is_initialized( $function_name = null ) { |
|
197 | - if ( ! self::$data_store_initialized && ! empty( $function_name ) ) { |
|
198 | - $message = sprintf( __( '%s() was called before the Action Scheduler data store was initialized', 'action-scheduler' ), esc_attr( $function_name ) ); |
|
199 | - error_log( $message, E_WARNING ); |
|
200 | - } |
|
201 | - |
|
202 | - return self::$data_store_initialized; |
|
203 | - } |
|
204 | - |
|
205 | - /** |
|
206 | - * Determine if the class is one of our abstract classes. |
|
207 | - * |
|
208 | - * @since 3.0.0 |
|
209 | - * |
|
210 | - * @param string $class The class name. |
|
211 | - * |
|
212 | - * @return bool |
|
213 | - */ |
|
214 | - protected static function is_class_abstract( $class ) { |
|
215 | - static $abstracts = array( |
|
216 | - 'ActionScheduler' => true, |
|
217 | - 'ActionScheduler_Abstract_ListTable' => true, |
|
218 | - 'ActionScheduler_Abstract_QueueRunner' => true, |
|
219 | - 'ActionScheduler_Abstract_Schedule' => true, |
|
220 | - 'ActionScheduler_Abstract_RecurringSchedule' => true, |
|
221 | - 'ActionScheduler_Lock' => true, |
|
222 | - 'ActionScheduler_Logger' => true, |
|
223 | - 'ActionScheduler_Abstract_Schema' => true, |
|
224 | - 'ActionScheduler_Store' => true, |
|
225 | - 'ActionScheduler_TimezoneHelper' => true, |
|
226 | - ); |
|
227 | - |
|
228 | - return isset( $abstracts[ $class ] ) && $abstracts[ $class ]; |
|
229 | - } |
|
230 | - |
|
231 | - /** |
|
232 | - * Determine if the class is one of our migration classes. |
|
233 | - * |
|
234 | - * @since 3.0.0 |
|
235 | - * |
|
236 | - * @param string $class The class name. |
|
237 | - * |
|
238 | - * @return bool |
|
239 | - */ |
|
240 | - protected static function is_class_migration( $class ) { |
|
241 | - static $migration_segments = array( |
|
242 | - 'ActionMigrator' => true, |
|
243 | - 'BatchFetcher' => true, |
|
244 | - 'DBStoreMigrator' => true, |
|
245 | - 'DryRun' => true, |
|
246 | - 'LogMigrator' => true, |
|
247 | - 'Config' => true, |
|
248 | - 'Controller' => true, |
|
249 | - 'Runner' => true, |
|
250 | - 'Scheduler' => true, |
|
251 | - ); |
|
252 | - |
|
253 | - $segments = explode( '_', $class ); |
|
254 | - $segment = isset( $segments[1] ) ? $segments[1] : $class; |
|
255 | - |
|
256 | - return isset( $migration_segments[ $segment ] ) && $migration_segments[ $segment ]; |
|
257 | - } |
|
258 | - |
|
259 | - /** |
|
260 | - * Determine if the class is one of our WP CLI classes. |
|
261 | - * |
|
262 | - * @since 3.0.0 |
|
263 | - * |
|
264 | - * @param string $class The class name. |
|
265 | - * |
|
266 | - * @return bool |
|
267 | - */ |
|
268 | - protected static function is_class_cli( $class ) { |
|
269 | - static $cli_segments = array( |
|
270 | - 'QueueRunner' => true, |
|
271 | - 'Command' => true, |
|
272 | - 'ProgressBar' => true, |
|
273 | - ); |
|
274 | - |
|
275 | - $segments = explode( '_', $class ); |
|
276 | - $segment = isset( $segments[1] ) ? $segments[1] : $class; |
|
277 | - |
|
278 | - return isset( $cli_segments[ $segment ] ) && $cli_segments[ $segment ]; |
|
279 | - } |
|
280 | - |
|
281 | - final public function __clone() { |
|
282 | - trigger_error( 'Singleton. No cloning allowed!', E_USER_ERROR ); |
|
283 | - } |
|
284 | - |
|
285 | - final public function __wakeup() { |
|
286 | - trigger_error( 'Singleton. No serialization allowed!', E_USER_ERROR ); |
|
287 | - } |
|
288 | - |
|
289 | - final private function __construct() {} |
|
290 | - |
|
291 | - /** Deprecated **/ |
|
292 | - |
|
293 | - public static function get_datetime_object( $when = null, $timezone = 'UTC' ) { |
|
294 | - _deprecated_function( __METHOD__, '2.0', 'wcs_add_months()' ); |
|
295 | - return as_get_datetime_object( $when, $timezone ); |
|
296 | - } |
|
297 | - |
|
298 | - /** |
|
299 | - * Issue deprecated warning if an Action Scheduler function is called in the shutdown hook. |
|
300 | - * |
|
301 | - * @param string $function_name The name of the function being called. |
|
302 | - * @deprecated 3.1.6. |
|
303 | - */ |
|
304 | - public static function check_shutdown_hook( $function_name ) { |
|
305 | - _deprecated_function( __FUNCTION__, '3.1.6' ); |
|
306 | - } |
|
12 | + private static $plugin_file = ''; |
|
13 | + /** @var ActionScheduler_ActionFactory */ |
|
14 | + private static $factory = null; |
|
15 | + /** @var bool */ |
|
16 | + private static $data_store_initialized = false; |
|
17 | + |
|
18 | + public static function factory() { |
|
19 | + if ( ! isset( self::$factory ) ) { |
|
20 | + self::$factory = new ActionScheduler_ActionFactory(); |
|
21 | + } |
|
22 | + return self::$factory; |
|
23 | + } |
|
24 | + |
|
25 | + public static function store() { |
|
26 | + return ActionScheduler_Store::instance(); |
|
27 | + } |
|
28 | + |
|
29 | + public static function lock() { |
|
30 | + return ActionScheduler_Lock::instance(); |
|
31 | + } |
|
32 | + |
|
33 | + public static function logger() { |
|
34 | + return ActionScheduler_Logger::instance(); |
|
35 | + } |
|
36 | + |
|
37 | + public static function runner() { |
|
38 | + return ActionScheduler_QueueRunner::instance(); |
|
39 | + } |
|
40 | + |
|
41 | + public static function admin_view() { |
|
42 | + return ActionScheduler_AdminView::instance(); |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * Get the absolute system path to the plugin directory, or a file therein |
|
47 | + * |
|
48 | + * @static |
|
49 | + * @param string $path |
|
50 | + * @return string |
|
51 | + */ |
|
52 | + public static function plugin_path( $path ) { |
|
53 | + $base = dirname( self::$plugin_file ); |
|
54 | + if ( $path ) { |
|
55 | + return trailingslashit( $base ) . $path; |
|
56 | + } else { |
|
57 | + return untrailingslashit( $base ); |
|
58 | + } |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * Get the absolute URL to the plugin directory, or a file therein |
|
63 | + * |
|
64 | + * @static |
|
65 | + * @param string $path |
|
66 | + * @return string |
|
67 | + */ |
|
68 | + public static function plugin_url( $path ) { |
|
69 | + return plugins_url( $path, self::$plugin_file ); |
|
70 | + } |
|
71 | + |
|
72 | + public static function autoload( $class ) { |
|
73 | + $d = DIRECTORY_SEPARATOR; |
|
74 | + $classes_dir = self::plugin_path( 'classes' . $d ); |
|
75 | + $separator = strrpos( $class, '\\' ); |
|
76 | + if ( false !== $separator ) { |
|
77 | + if ( 0 !== strpos( $class, 'Action_Scheduler' ) ) { |
|
78 | + return; |
|
79 | + } |
|
80 | + $class = substr( $class, $separator + 1 ); |
|
81 | + } |
|
82 | + |
|
83 | + if ( 'Deprecated' === substr( $class, -10 ) ) { |
|
84 | + $dir = self::plugin_path( 'deprecated' . $d ); |
|
85 | + } elseif ( self::is_class_abstract( $class ) ) { |
|
86 | + $dir = $classes_dir . 'abstracts' . $d; |
|
87 | + } elseif ( self::is_class_migration( $class ) ) { |
|
88 | + $dir = $classes_dir . 'migration' . $d; |
|
89 | + } elseif ( 'Schedule' === substr( $class, -8 ) ) { |
|
90 | + $dir = $classes_dir . 'schedules' . $d; |
|
91 | + } elseif ( 'Action' === substr( $class, -6 ) ) { |
|
92 | + $dir = $classes_dir . 'actions' . $d; |
|
93 | + } elseif ( 'Schema' === substr( $class, -6 ) ) { |
|
94 | + $dir = $classes_dir . 'schema' . $d; |
|
95 | + } elseif ( strpos( $class, 'ActionScheduler' ) === 0 ) { |
|
96 | + $segments = explode( '_', $class ); |
|
97 | + $type = isset( $segments[1] ) ? $segments[1] : ''; |
|
98 | + |
|
99 | + switch ( $type ) { |
|
100 | + case 'WPCLI': |
|
101 | + $dir = $classes_dir . 'WP_CLI' . $d; |
|
102 | + break; |
|
103 | + case 'DBLogger': |
|
104 | + case 'DBStore': |
|
105 | + case 'HybridStore': |
|
106 | + case 'wpPostStore': |
|
107 | + case 'wpCommentLogger': |
|
108 | + $dir = $classes_dir . 'data-stores' . $d; |
|
109 | + break; |
|
110 | + default: |
|
111 | + $dir = $classes_dir; |
|
112 | + break; |
|
113 | + } |
|
114 | + } elseif ( self::is_class_cli( $class ) ) { |
|
115 | + $dir = $classes_dir . 'WP_CLI' . $d; |
|
116 | + } elseif ( strpos( $class, 'CronExpression' ) === 0 ) { |
|
117 | + $dir = self::plugin_path( 'lib' . $d . 'cron-expression' . $d ); |
|
118 | + } elseif ( strpos( $class, 'WP_Async_Request' ) === 0 ) { |
|
119 | + $dir = self::plugin_path( 'lib' . $d ); |
|
120 | + } else { |
|
121 | + return; |
|
122 | + } |
|
123 | + |
|
124 | + if ( file_exists( $dir . "{$class}.php" ) ) { |
|
125 | + include $dir . "{$class}.php"; |
|
126 | + return; |
|
127 | + } |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * Initialize the plugin |
|
132 | + * |
|
133 | + * @static |
|
134 | + * @param string $plugin_file |
|
135 | + */ |
|
136 | + public static function init( $plugin_file ) { |
|
137 | + self::$plugin_file = $plugin_file; |
|
138 | + spl_autoload_register( array( __CLASS__, 'autoload' ) ); |
|
139 | + |
|
140 | + /** |
|
141 | + * Fires in the early stages of Action Scheduler init hook. |
|
142 | + */ |
|
143 | + do_action( 'action_scheduler_pre_init' ); |
|
144 | + |
|
145 | + require_once self::plugin_path( 'functions.php' ); |
|
146 | + ActionScheduler_DataController::init(); |
|
147 | + |
|
148 | + $store = self::store(); |
|
149 | + $logger = self::logger(); |
|
150 | + $runner = self::runner(); |
|
151 | + $admin_view = self::admin_view(); |
|
152 | + |
|
153 | + // Ensure initialization on plugin activation. |
|
154 | + if ( ! did_action( 'init' ) ) { |
|
155 | + add_action( 'init', array( $admin_view, 'init' ), 0, 0 ); // run before $store::init() |
|
156 | + add_action( 'init', array( $store, 'init' ), 1, 0 ); |
|
157 | + add_action( 'init', array( $logger, 'init' ), 1, 0 ); |
|
158 | + add_action( 'init', array( $runner, 'init' ), 1, 0 ); |
|
159 | + } else { |
|
160 | + $admin_view->init(); |
|
161 | + $store->init(); |
|
162 | + $logger->init(); |
|
163 | + $runner->init(); |
|
164 | + } |
|
165 | + |
|
166 | + if ( apply_filters( 'action_scheduler_load_deprecated_functions', true ) ) { |
|
167 | + require_once self::plugin_path( 'deprecated/functions.php' ); |
|
168 | + } |
|
169 | + |
|
170 | + if ( defined( 'WP_CLI' ) && WP_CLI ) { |
|
171 | + WP_CLI::add_command( 'action-scheduler', 'ActionScheduler_WPCLI_Scheduler_command' ); |
|
172 | + if ( ! ActionScheduler_DataController::is_migration_complete() && Controller::instance()->allow_migration() ) { |
|
173 | + $command = new Migration_Command(); |
|
174 | + $command->register(); |
|
175 | + } |
|
176 | + } |
|
177 | + |
|
178 | + self::$data_store_initialized = true; |
|
179 | + |
|
180 | + /** |
|
181 | + * Handle WP comment cleanup after migration. |
|
182 | + */ |
|
183 | + if ( is_a( $logger, 'ActionScheduler_DBLogger' ) && ActionScheduler_DataController::is_migration_complete() && ActionScheduler_WPCommentCleaner::has_logs() ) { |
|
184 | + ActionScheduler_WPCommentCleaner::init(); |
|
185 | + } |
|
186 | + |
|
187 | + add_action( 'action_scheduler/migration_complete', 'ActionScheduler_WPCommentCleaner::maybe_schedule_cleanup' ); |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * Check whether the AS data store has been initialized. |
|
192 | + * |
|
193 | + * @param string $function_name The name of the function being called. Optional. Default `null`. |
|
194 | + * @return bool |
|
195 | + */ |
|
196 | + public static function is_initialized( $function_name = null ) { |
|
197 | + if ( ! self::$data_store_initialized && ! empty( $function_name ) ) { |
|
198 | + $message = sprintf( __( '%s() was called before the Action Scheduler data store was initialized', 'action-scheduler' ), esc_attr( $function_name ) ); |
|
199 | + error_log( $message, E_WARNING ); |
|
200 | + } |
|
201 | + |
|
202 | + return self::$data_store_initialized; |
|
203 | + } |
|
204 | + |
|
205 | + /** |
|
206 | + * Determine if the class is one of our abstract classes. |
|
207 | + * |
|
208 | + * @since 3.0.0 |
|
209 | + * |
|
210 | + * @param string $class The class name. |
|
211 | + * |
|
212 | + * @return bool |
|
213 | + */ |
|
214 | + protected static function is_class_abstract( $class ) { |
|
215 | + static $abstracts = array( |
|
216 | + 'ActionScheduler' => true, |
|
217 | + 'ActionScheduler_Abstract_ListTable' => true, |
|
218 | + 'ActionScheduler_Abstract_QueueRunner' => true, |
|
219 | + 'ActionScheduler_Abstract_Schedule' => true, |
|
220 | + 'ActionScheduler_Abstract_RecurringSchedule' => true, |
|
221 | + 'ActionScheduler_Lock' => true, |
|
222 | + 'ActionScheduler_Logger' => true, |
|
223 | + 'ActionScheduler_Abstract_Schema' => true, |
|
224 | + 'ActionScheduler_Store' => true, |
|
225 | + 'ActionScheduler_TimezoneHelper' => true, |
|
226 | + ); |
|
227 | + |
|
228 | + return isset( $abstracts[ $class ] ) && $abstracts[ $class ]; |
|
229 | + } |
|
230 | + |
|
231 | + /** |
|
232 | + * Determine if the class is one of our migration classes. |
|
233 | + * |
|
234 | + * @since 3.0.0 |
|
235 | + * |
|
236 | + * @param string $class The class name. |
|
237 | + * |
|
238 | + * @return bool |
|
239 | + */ |
|
240 | + protected static function is_class_migration( $class ) { |
|
241 | + static $migration_segments = array( |
|
242 | + 'ActionMigrator' => true, |
|
243 | + 'BatchFetcher' => true, |
|
244 | + 'DBStoreMigrator' => true, |
|
245 | + 'DryRun' => true, |
|
246 | + 'LogMigrator' => true, |
|
247 | + 'Config' => true, |
|
248 | + 'Controller' => true, |
|
249 | + 'Runner' => true, |
|
250 | + 'Scheduler' => true, |
|
251 | + ); |
|
252 | + |
|
253 | + $segments = explode( '_', $class ); |
|
254 | + $segment = isset( $segments[1] ) ? $segments[1] : $class; |
|
255 | + |
|
256 | + return isset( $migration_segments[ $segment ] ) && $migration_segments[ $segment ]; |
|
257 | + } |
|
258 | + |
|
259 | + /** |
|
260 | + * Determine if the class is one of our WP CLI classes. |
|
261 | + * |
|
262 | + * @since 3.0.0 |
|
263 | + * |
|
264 | + * @param string $class The class name. |
|
265 | + * |
|
266 | + * @return bool |
|
267 | + */ |
|
268 | + protected static function is_class_cli( $class ) { |
|
269 | + static $cli_segments = array( |
|
270 | + 'QueueRunner' => true, |
|
271 | + 'Command' => true, |
|
272 | + 'ProgressBar' => true, |
|
273 | + ); |
|
274 | + |
|
275 | + $segments = explode( '_', $class ); |
|
276 | + $segment = isset( $segments[1] ) ? $segments[1] : $class; |
|
277 | + |
|
278 | + return isset( $cli_segments[ $segment ] ) && $cli_segments[ $segment ]; |
|
279 | + } |
|
280 | + |
|
281 | + final public function __clone() { |
|
282 | + trigger_error( 'Singleton. No cloning allowed!', E_USER_ERROR ); |
|
283 | + } |
|
284 | + |
|
285 | + final public function __wakeup() { |
|
286 | + trigger_error( 'Singleton. No serialization allowed!', E_USER_ERROR ); |
|
287 | + } |
|
288 | + |
|
289 | + final private function __construct() {} |
|
290 | + |
|
291 | + /** Deprecated **/ |
|
292 | + |
|
293 | + public static function get_datetime_object( $when = null, $timezone = 'UTC' ) { |
|
294 | + _deprecated_function( __METHOD__, '2.0', 'wcs_add_months()' ); |
|
295 | + return as_get_datetime_object( $when, $timezone ); |
|
296 | + } |
|
297 | + |
|
298 | + /** |
|
299 | + * Issue deprecated warning if an Action Scheduler function is called in the shutdown hook. |
|
300 | + * |
|
301 | + * @param string $function_name The name of the function being called. |
|
302 | + * @deprecated 3.1.6. |
|
303 | + */ |
|
304 | + public static function check_shutdown_hook( $function_name ) { |
|
305 | + _deprecated_function( __FUNCTION__, '3.1.6' ); |
|
306 | + } |
|
307 | 307 | } |
@@ -16,7 +16,7 @@ discard block |
||
16 | 16 | private static $data_store_initialized = false; |
17 | 17 | |
18 | 18 | public static function factory() { |
19 | - if ( ! isset( self::$factory ) ) { |
|
19 | + if ( ! isset(self::$factory)) { |
|
20 | 20 | self::$factory = new ActionScheduler_ActionFactory(); |
21 | 21 | } |
22 | 22 | return self::$factory; |
@@ -49,12 +49,12 @@ discard block |
||
49 | 49 | * @param string $path |
50 | 50 | * @return string |
51 | 51 | */ |
52 | - public static function plugin_path( $path ) { |
|
53 | - $base = dirname( self::$plugin_file ); |
|
54 | - if ( $path ) { |
|
55 | - return trailingslashit( $base ) . $path; |
|
52 | + public static function plugin_path($path) { |
|
53 | + $base = dirname(self::$plugin_file); |
|
54 | + if ($path) { |
|
55 | + return trailingslashit($base).$path; |
|
56 | 56 | } else { |
57 | - return untrailingslashit( $base ); |
|
57 | + return untrailingslashit($base); |
|
58 | 58 | } |
59 | 59 | } |
60 | 60 | |
@@ -65,64 +65,64 @@ discard block |
||
65 | 65 | * @param string $path |
66 | 66 | * @return string |
67 | 67 | */ |
68 | - public static function plugin_url( $path ) { |
|
69 | - return plugins_url( $path, self::$plugin_file ); |
|
68 | + public static function plugin_url($path) { |
|
69 | + return plugins_url($path, self::$plugin_file); |
|
70 | 70 | } |
71 | 71 | |
72 | - public static function autoload( $class ) { |
|
72 | + public static function autoload($class) { |
|
73 | 73 | $d = DIRECTORY_SEPARATOR; |
74 | - $classes_dir = self::plugin_path( 'classes' . $d ); |
|
75 | - $separator = strrpos( $class, '\\' ); |
|
76 | - if ( false !== $separator ) { |
|
77 | - if ( 0 !== strpos( $class, 'Action_Scheduler' ) ) { |
|
74 | + $classes_dir = self::plugin_path('classes'.$d); |
|
75 | + $separator = strrpos($class, '\\'); |
|
76 | + if (false !== $separator) { |
|
77 | + if (0 !== strpos($class, 'Action_Scheduler')) { |
|
78 | 78 | return; |
79 | 79 | } |
80 | - $class = substr( $class, $separator + 1 ); |
|
80 | + $class = substr($class, $separator + 1); |
|
81 | 81 | } |
82 | 82 | |
83 | - if ( 'Deprecated' === substr( $class, -10 ) ) { |
|
84 | - $dir = self::plugin_path( 'deprecated' . $d ); |
|
85 | - } elseif ( self::is_class_abstract( $class ) ) { |
|
86 | - $dir = $classes_dir . 'abstracts' . $d; |
|
87 | - } elseif ( self::is_class_migration( $class ) ) { |
|
88 | - $dir = $classes_dir . 'migration' . $d; |
|
89 | - } elseif ( 'Schedule' === substr( $class, -8 ) ) { |
|
90 | - $dir = $classes_dir . 'schedules' . $d; |
|
91 | - } elseif ( 'Action' === substr( $class, -6 ) ) { |
|
92 | - $dir = $classes_dir . 'actions' . $d; |
|
93 | - } elseif ( 'Schema' === substr( $class, -6 ) ) { |
|
94 | - $dir = $classes_dir . 'schema' . $d; |
|
95 | - } elseif ( strpos( $class, 'ActionScheduler' ) === 0 ) { |
|
96 | - $segments = explode( '_', $class ); |
|
97 | - $type = isset( $segments[1] ) ? $segments[1] : ''; |
|
98 | - |
|
99 | - switch ( $type ) { |
|
83 | + if ('Deprecated' === substr($class, -10)) { |
|
84 | + $dir = self::plugin_path('deprecated'.$d); |
|
85 | + } elseif (self::is_class_abstract($class)) { |
|
86 | + $dir = $classes_dir.'abstracts'.$d; |
|
87 | + } elseif (self::is_class_migration($class)) { |
|
88 | + $dir = $classes_dir.'migration'.$d; |
|
89 | + } elseif ('Schedule' === substr($class, -8)) { |
|
90 | + $dir = $classes_dir.'schedules'.$d; |
|
91 | + } elseif ('Action' === substr($class, -6)) { |
|
92 | + $dir = $classes_dir.'actions'.$d; |
|
93 | + } elseif ('Schema' === substr($class, -6)) { |
|
94 | + $dir = $classes_dir.'schema'.$d; |
|
95 | + } elseif (strpos($class, 'ActionScheduler') === 0) { |
|
96 | + $segments = explode('_', $class); |
|
97 | + $type = isset($segments[1]) ? $segments[1] : ''; |
|
98 | + |
|
99 | + switch ($type) { |
|
100 | 100 | case 'WPCLI': |
101 | - $dir = $classes_dir . 'WP_CLI' . $d; |
|
101 | + $dir = $classes_dir.'WP_CLI'.$d; |
|
102 | 102 | break; |
103 | 103 | case 'DBLogger': |
104 | 104 | case 'DBStore': |
105 | 105 | case 'HybridStore': |
106 | 106 | case 'wpPostStore': |
107 | 107 | case 'wpCommentLogger': |
108 | - $dir = $classes_dir . 'data-stores' . $d; |
|
108 | + $dir = $classes_dir.'data-stores'.$d; |
|
109 | 109 | break; |
110 | 110 | default: |
111 | 111 | $dir = $classes_dir; |
112 | 112 | break; |
113 | 113 | } |
114 | - } elseif ( self::is_class_cli( $class ) ) { |
|
115 | - $dir = $classes_dir . 'WP_CLI' . $d; |
|
116 | - } elseif ( strpos( $class, 'CronExpression' ) === 0 ) { |
|
117 | - $dir = self::plugin_path( 'lib' . $d . 'cron-expression' . $d ); |
|
118 | - } elseif ( strpos( $class, 'WP_Async_Request' ) === 0 ) { |
|
119 | - $dir = self::plugin_path( 'lib' . $d ); |
|
114 | + } elseif (self::is_class_cli($class)) { |
|
115 | + $dir = $classes_dir.'WP_CLI'.$d; |
|
116 | + } elseif (strpos($class, 'CronExpression') === 0) { |
|
117 | + $dir = self::plugin_path('lib'.$d.'cron-expression'.$d); |
|
118 | + } elseif (strpos($class, 'WP_Async_Request') === 0) { |
|
119 | + $dir = self::plugin_path('lib'.$d); |
|
120 | 120 | } else { |
121 | 121 | return; |
122 | 122 | } |
123 | 123 | |
124 | - if ( file_exists( $dir . "{$class}.php" ) ) { |
|
125 | - include $dir . "{$class}.php"; |
|
124 | + if (file_exists($dir."{$class}.php")) { |
|
125 | + include $dir."{$class}.php"; |
|
126 | 126 | return; |
127 | 127 | } |
128 | 128 | } |
@@ -133,16 +133,16 @@ discard block |
||
133 | 133 | * @static |
134 | 134 | * @param string $plugin_file |
135 | 135 | */ |
136 | - public static function init( $plugin_file ) { |
|
136 | + public static function init($plugin_file) { |
|
137 | 137 | self::$plugin_file = $plugin_file; |
138 | - spl_autoload_register( array( __CLASS__, 'autoload' ) ); |
|
138 | + spl_autoload_register(array(__CLASS__, 'autoload')); |
|
139 | 139 | |
140 | 140 | /** |
141 | 141 | * Fires in the early stages of Action Scheduler init hook. |
142 | 142 | */ |
143 | - do_action( 'action_scheduler_pre_init' ); |
|
143 | + do_action('action_scheduler_pre_init'); |
|
144 | 144 | |
145 | - require_once self::plugin_path( 'functions.php' ); |
|
145 | + require_once self::plugin_path('functions.php'); |
|
146 | 146 | ActionScheduler_DataController::init(); |
147 | 147 | |
148 | 148 | $store = self::store(); |
@@ -151,11 +151,11 @@ discard block |
||
151 | 151 | $admin_view = self::admin_view(); |
152 | 152 | |
153 | 153 | // Ensure initialization on plugin activation. |
154 | - if ( ! did_action( 'init' ) ) { |
|
155 | - add_action( 'init', array( $admin_view, 'init' ), 0, 0 ); // run before $store::init() |
|
156 | - add_action( 'init', array( $store, 'init' ), 1, 0 ); |
|
157 | - add_action( 'init', array( $logger, 'init' ), 1, 0 ); |
|
158 | - add_action( 'init', array( $runner, 'init' ), 1, 0 ); |
|
154 | + if ( ! did_action('init')) { |
|
155 | + add_action('init', array($admin_view, 'init'), 0, 0); // run before $store::init() |
|
156 | + add_action('init', array($store, 'init'), 1, 0); |
|
157 | + add_action('init', array($logger, 'init'), 1, 0); |
|
158 | + add_action('init', array($runner, 'init'), 1, 0); |
|
159 | 159 | } else { |
160 | 160 | $admin_view->init(); |
161 | 161 | $store->init(); |
@@ -163,13 +163,13 @@ discard block |
||
163 | 163 | $runner->init(); |
164 | 164 | } |
165 | 165 | |
166 | - if ( apply_filters( 'action_scheduler_load_deprecated_functions', true ) ) { |
|
167 | - require_once self::plugin_path( 'deprecated/functions.php' ); |
|
166 | + if (apply_filters('action_scheduler_load_deprecated_functions', true)) { |
|
167 | + require_once self::plugin_path('deprecated/functions.php'); |
|
168 | 168 | } |
169 | 169 | |
170 | - if ( defined( 'WP_CLI' ) && WP_CLI ) { |
|
171 | - WP_CLI::add_command( 'action-scheduler', 'ActionScheduler_WPCLI_Scheduler_command' ); |
|
172 | - if ( ! ActionScheduler_DataController::is_migration_complete() && Controller::instance()->allow_migration() ) { |
|
170 | + if (defined('WP_CLI') && WP_CLI) { |
|
171 | + WP_CLI::add_command('action-scheduler', 'ActionScheduler_WPCLI_Scheduler_command'); |
|
172 | + if ( ! ActionScheduler_DataController::is_migration_complete() && Controller::instance()->allow_migration()) { |
|
173 | 173 | $command = new Migration_Command(); |
174 | 174 | $command->register(); |
175 | 175 | } |
@@ -180,11 +180,11 @@ discard block |
||
180 | 180 | /** |
181 | 181 | * Handle WP comment cleanup after migration. |
182 | 182 | */ |
183 | - if ( is_a( $logger, 'ActionScheduler_DBLogger' ) && ActionScheduler_DataController::is_migration_complete() && ActionScheduler_WPCommentCleaner::has_logs() ) { |
|
183 | + if (is_a($logger, 'ActionScheduler_DBLogger') && ActionScheduler_DataController::is_migration_complete() && ActionScheduler_WPCommentCleaner::has_logs()) { |
|
184 | 184 | ActionScheduler_WPCommentCleaner::init(); |
185 | 185 | } |
186 | 186 | |
187 | - add_action( 'action_scheduler/migration_complete', 'ActionScheduler_WPCommentCleaner::maybe_schedule_cleanup' ); |
|
187 | + add_action('action_scheduler/migration_complete', 'ActionScheduler_WPCommentCleaner::maybe_schedule_cleanup'); |
|
188 | 188 | } |
189 | 189 | |
190 | 190 | /** |
@@ -193,10 +193,10 @@ discard block |
||
193 | 193 | * @param string $function_name The name of the function being called. Optional. Default `null`. |
194 | 194 | * @return bool |
195 | 195 | */ |
196 | - public static function is_initialized( $function_name = null ) { |
|
197 | - if ( ! self::$data_store_initialized && ! empty( $function_name ) ) { |
|
198 | - $message = sprintf( __( '%s() was called before the Action Scheduler data store was initialized', 'action-scheduler' ), esc_attr( $function_name ) ); |
|
199 | - error_log( $message, E_WARNING ); |
|
196 | + public static function is_initialized($function_name = null) { |
|
197 | + if ( ! self::$data_store_initialized && ! empty($function_name)) { |
|
198 | + $message = sprintf(__('%s() was called before the Action Scheduler data store was initialized', 'action-scheduler'), esc_attr($function_name)); |
|
199 | + error_log($message, E_WARNING); |
|
200 | 200 | } |
201 | 201 | |
202 | 202 | return self::$data_store_initialized; |
@@ -211,7 +211,7 @@ discard block |
||
211 | 211 | * |
212 | 212 | * @return bool |
213 | 213 | */ |
214 | - protected static function is_class_abstract( $class ) { |
|
214 | + protected static function is_class_abstract($class) { |
|
215 | 215 | static $abstracts = array( |
216 | 216 | 'ActionScheduler' => true, |
217 | 217 | 'ActionScheduler_Abstract_ListTable' => true, |
@@ -225,7 +225,7 @@ discard block |
||
225 | 225 | 'ActionScheduler_TimezoneHelper' => true, |
226 | 226 | ); |
227 | 227 | |
228 | - return isset( $abstracts[ $class ] ) && $abstracts[ $class ]; |
|
228 | + return isset($abstracts[$class]) && $abstracts[$class]; |
|
229 | 229 | } |
230 | 230 | |
231 | 231 | /** |
@@ -237,7 +237,7 @@ discard block |
||
237 | 237 | * |
238 | 238 | * @return bool |
239 | 239 | */ |
240 | - protected static function is_class_migration( $class ) { |
|
240 | + protected static function is_class_migration($class) { |
|
241 | 241 | static $migration_segments = array( |
242 | 242 | 'ActionMigrator' => true, |
243 | 243 | 'BatchFetcher' => true, |
@@ -250,10 +250,10 @@ discard block |
||
250 | 250 | 'Scheduler' => true, |
251 | 251 | ); |
252 | 252 | |
253 | - $segments = explode( '_', $class ); |
|
254 | - $segment = isset( $segments[1] ) ? $segments[1] : $class; |
|
253 | + $segments = explode('_', $class); |
|
254 | + $segment = isset($segments[1]) ? $segments[1] : $class; |
|
255 | 255 | |
256 | - return isset( $migration_segments[ $segment ] ) && $migration_segments[ $segment ]; |
|
256 | + return isset($migration_segments[$segment]) && $migration_segments[$segment]; |
|
257 | 257 | } |
258 | 258 | |
259 | 259 | /** |
@@ -265,34 +265,34 @@ discard block |
||
265 | 265 | * |
266 | 266 | * @return bool |
267 | 267 | */ |
268 | - protected static function is_class_cli( $class ) { |
|
268 | + protected static function is_class_cli($class) { |
|
269 | 269 | static $cli_segments = array( |
270 | 270 | 'QueueRunner' => true, |
271 | 271 | 'Command' => true, |
272 | 272 | 'ProgressBar' => true, |
273 | 273 | ); |
274 | 274 | |
275 | - $segments = explode( '_', $class ); |
|
276 | - $segment = isset( $segments[1] ) ? $segments[1] : $class; |
|
275 | + $segments = explode('_', $class); |
|
276 | + $segment = isset($segments[1]) ? $segments[1] : $class; |
|
277 | 277 | |
278 | - return isset( $cli_segments[ $segment ] ) && $cli_segments[ $segment ]; |
|
278 | + return isset($cli_segments[$segment]) && $cli_segments[$segment]; |
|
279 | 279 | } |
280 | 280 | |
281 | 281 | final public function __clone() { |
282 | - trigger_error( 'Singleton. No cloning allowed!', E_USER_ERROR ); |
|
282 | + trigger_error('Singleton. No cloning allowed!', E_USER_ERROR); |
|
283 | 283 | } |
284 | 284 | |
285 | 285 | final public function __wakeup() { |
286 | - trigger_error( 'Singleton. No serialization allowed!', E_USER_ERROR ); |
|
286 | + trigger_error('Singleton. No serialization allowed!', E_USER_ERROR); |
|
287 | 287 | } |
288 | 288 | |
289 | 289 | final private function __construct() {} |
290 | 290 | |
291 | 291 | /** Deprecated **/ |
292 | 292 | |
293 | - public static function get_datetime_object( $when = null, $timezone = 'UTC' ) { |
|
294 | - _deprecated_function( __METHOD__, '2.0', 'wcs_add_months()' ); |
|
295 | - return as_get_datetime_object( $when, $timezone ); |
|
293 | + public static function get_datetime_object($when = null, $timezone = 'UTC') { |
|
294 | + _deprecated_function(__METHOD__, '2.0', 'wcs_add_months()'); |
|
295 | + return as_get_datetime_object($when, $timezone); |
|
296 | 296 | } |
297 | 297 | |
298 | 298 | /** |
@@ -301,7 +301,7 @@ discard block |
||
301 | 301 | * @param string $function_name The name of the function being called. |
302 | 302 | * @deprecated 3.1.6. |
303 | 303 | */ |
304 | - public static function check_shutdown_hook( $function_name ) { |
|
305 | - _deprecated_function( __FUNCTION__, '3.1.6' ); |
|
304 | + public static function check_shutdown_hook($function_name) { |
|
305 | + _deprecated_function(__FUNCTION__, '3.1.6'); |
|
306 | 306 | } |
307 | 307 | } |
@@ -5,294 +5,294 @@ |
||
5 | 5 | */ |
6 | 6 | abstract class ActionScheduler_Abstract_QueueRunner extends ActionScheduler_Abstract_QueueRunner_Deprecated { |
7 | 7 | |
8 | - /** @var ActionScheduler_QueueCleaner */ |
|
9 | - protected $cleaner; |
|
10 | - |
|
11 | - /** @var ActionScheduler_FatalErrorMonitor */ |
|
12 | - protected $monitor; |
|
13 | - |
|
14 | - /** @var ActionScheduler_Store */ |
|
15 | - protected $store; |
|
16 | - |
|
17 | - /** |
|
18 | - * The created time. |
|
19 | - * |
|
20 | - * Represents when the queue runner was constructed and used when calculating how long a PHP request has been running. |
|
21 | - * For this reason it should be as close as possible to the PHP request start time. |
|
22 | - * |
|
23 | - * @var int |
|
24 | - */ |
|
25 | - private $created_time; |
|
26 | - |
|
27 | - /** |
|
28 | - * ActionScheduler_Abstract_QueueRunner constructor. |
|
29 | - * |
|
30 | - * @param ActionScheduler_Store $store |
|
31 | - * @param ActionScheduler_FatalErrorMonitor $monitor |
|
32 | - * @param ActionScheduler_QueueCleaner $cleaner |
|
33 | - */ |
|
34 | - public function __construct( ActionScheduler_Store $store = null, ActionScheduler_FatalErrorMonitor $monitor = null, ActionScheduler_QueueCleaner $cleaner = null ) { |
|
35 | - |
|
36 | - $this->created_time = microtime( true ); |
|
37 | - |
|
38 | - $this->store = $store ? $store : ActionScheduler_Store::instance(); |
|
39 | - $this->monitor = $monitor ? $monitor : new ActionScheduler_FatalErrorMonitor( $this->store ); |
|
40 | - $this->cleaner = $cleaner ? $cleaner : new ActionScheduler_QueueCleaner( $this->store ); |
|
41 | - } |
|
42 | - |
|
43 | - /** |
|
44 | - * Process an individual action. |
|
45 | - * |
|
46 | - * @param int $action_id The action ID to process. |
|
47 | - * @param string $context Optional identifer for the context in which this action is being processed, e.g. 'WP CLI' or 'WP Cron' |
|
48 | - * Generally, this should be capitalised and not localised as it's a proper noun. |
|
49 | - */ |
|
50 | - public function process_action( $action_id, $context = '' ) { |
|
51 | - try { |
|
52 | - $valid_action = false; |
|
53 | - do_action( 'action_scheduler_before_execute', $action_id, $context ); |
|
54 | - |
|
55 | - if ( ActionScheduler_Store::STATUS_PENDING !== $this->store->get_status( $action_id ) ) { |
|
56 | - do_action( 'action_scheduler_execution_ignored', $action_id, $context ); |
|
57 | - return; |
|
58 | - } |
|
59 | - |
|
60 | - $valid_action = true; |
|
61 | - do_action( 'action_scheduler_begin_execute', $action_id, $context ); |
|
62 | - |
|
63 | - $action = $this->store->fetch_action( $action_id ); |
|
64 | - $this->store->log_execution( $action_id ); |
|
65 | - $action->execute(); |
|
66 | - do_action( 'action_scheduler_after_execute', $action_id, $action, $context ); |
|
67 | - $this->store->mark_complete( $action_id ); |
|
68 | - } catch ( Exception $e ) { |
|
69 | - if ( $valid_action ) { |
|
70 | - $this->store->mark_failure( $action_id ); |
|
71 | - do_action( 'action_scheduler_failed_execution', $action_id, $e, $context ); |
|
72 | - } else { |
|
73 | - do_action( 'action_scheduler_failed_validation', $action_id, $e, $context ); |
|
74 | - } |
|
75 | - } |
|
76 | - |
|
77 | - if ( isset( $action ) && is_a( $action, 'ActionScheduler_Action' ) && $action->get_schedule()->is_recurring() ) { |
|
78 | - $this->schedule_next_instance( $action, $action_id ); |
|
79 | - } |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Schedule the next instance of the action if necessary. |
|
84 | - * |
|
85 | - * @param ActionScheduler_Action $action |
|
86 | - * @param int $action_id |
|
87 | - */ |
|
88 | - protected function schedule_next_instance( ActionScheduler_Action $action, $action_id ) { |
|
89 | - // If a recurring action has been consistently failing, we may wish to stop rescheduling it. |
|
90 | - if ( |
|
91 | - ActionScheduler_Store::STATUS_FAILED === $this->store->get_status( $action_id ) |
|
92 | - && $this->recurring_action_is_consistently_failing( $action, $action_id ) |
|
93 | - ) { |
|
94 | - ActionScheduler_Logger::instance()->log( |
|
95 | - $action_id, |
|
96 | - __( 'This action appears to be consistently failing. A new instance will not be scheduled.', 'action-scheduler' ) |
|
97 | - ); |
|
98 | - |
|
99 | - return; |
|
100 | - } |
|
101 | - |
|
102 | - try { |
|
103 | - ActionScheduler::factory()->repeat( $action ); |
|
104 | - } catch ( Exception $e ) { |
|
105 | - do_action( 'action_scheduler_failed_to_schedule_next_instance', $action_id, $e, $action ); |
|
106 | - } |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * Determine if the specified recurring action has been consistently failing. |
|
111 | - * |
|
112 | - * @param ActionScheduler_Action $action The recurring action to be rescheduled. |
|
113 | - * @param int $action_id The ID of the recurring action. |
|
114 | - * |
|
115 | - * @return bool |
|
116 | - */ |
|
117 | - private function recurring_action_is_consistently_failing( ActionScheduler_Action $action, $action_id ) { |
|
118 | - /** |
|
119 | - * Controls the failure threshold for recurring actions. |
|
120 | - * |
|
121 | - * Before rescheduling a recurring action, we look at its status. If it failed, we then check if all of the most |
|
122 | - * recent actions (upto the threshold set by this filter) sharing the same hook have also failed: if they have, |
|
123 | - * that is considered consistent failure and a new instance of the action will not be scheduled. |
|
124 | - * |
|
125 | - * @param int $failure_threshold Number of actions of the same hook to examine for failure. Defaults to 5. |
|
126 | - */ |
|
127 | - $consistent_failure_threshold = (int) apply_filters( 'action_scheduler_recurring_action_failure_threshold', 5 ); |
|
128 | - |
|
129 | - // This query should find the earliest *failing* action (for the hook we are interested in) within our threshold. |
|
130 | - $query_args = array( |
|
131 | - 'hook' => $action->get_hook(), |
|
132 | - 'status' => ActionScheduler_Store::STATUS_FAILED, |
|
133 | - 'date' => date_create( 'now', timezone_open( 'UTC' ) )->format( 'Y-m-d H:i:s' ), |
|
134 | - 'date_compare' => '<', |
|
135 | - 'per_page' => 1, |
|
136 | - 'offset' => $consistent_failure_threshold - 1, |
|
137 | - ); |
|
138 | - |
|
139 | - $first_failing_action_id = $this->store->query_actions( $query_args ); |
|
140 | - |
|
141 | - // If we didn't retrieve an action ID, then there haven't been enough failures for us to worry about. |
|
142 | - if ( empty( $first_failing_action_id ) ) { |
|
143 | - return false; |
|
144 | - } |
|
145 | - |
|
146 | - // Now let's fetch the first action (having the same hook) of *any status*ithin the same window. |
|
147 | - unset( $query_args['status'] ); |
|
148 | - $first_action_id_with_the_same_hook = $this->store->query_actions( $query_args ); |
|
149 | - |
|
150 | - // If the IDs match, then actions for this hook must be consistently failing. |
|
151 | - return $first_action_id_with_the_same_hook === $first_failing_action_id; |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * Run the queue cleaner. |
|
156 | - * |
|
157 | - * @author Jeremy Pry |
|
158 | - */ |
|
159 | - protected function run_cleanup() { |
|
160 | - $this->cleaner->clean( 10 * $this->get_time_limit() ); |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Get the number of concurrent batches a runner allows. |
|
165 | - * |
|
166 | - * @return int |
|
167 | - */ |
|
168 | - public function get_allowed_concurrent_batches() { |
|
169 | - return apply_filters( 'action_scheduler_queue_runner_concurrent_batches', 1 ); |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * Check if the number of allowed concurrent batches is met or exceeded. |
|
174 | - * |
|
175 | - * @return bool |
|
176 | - */ |
|
177 | - public function has_maximum_concurrent_batches() { |
|
178 | - return $this->store->get_claim_count() >= $this->get_allowed_concurrent_batches(); |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * Get the maximum number of seconds a batch can run for. |
|
183 | - * |
|
184 | - * @return int The number of seconds. |
|
185 | - */ |
|
186 | - protected function get_time_limit() { |
|
187 | - |
|
188 | - $time_limit = 30; |
|
189 | - |
|
190 | - // Apply deprecated filter from deprecated get_maximum_execution_time() method |
|
191 | - if ( has_filter( 'action_scheduler_maximum_execution_time' ) ) { |
|
192 | - _deprecated_function( 'action_scheduler_maximum_execution_time', '2.1.1', 'action_scheduler_queue_runner_time_limit' ); |
|
193 | - $time_limit = apply_filters( 'action_scheduler_maximum_execution_time', $time_limit ); |
|
194 | - } |
|
195 | - |
|
196 | - return absint( apply_filters( 'action_scheduler_queue_runner_time_limit', $time_limit ) ); |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * Get the number of seconds the process has been running. |
|
201 | - * |
|
202 | - * @return int The number of seconds. |
|
203 | - */ |
|
204 | - protected function get_execution_time() { |
|
205 | - $execution_time = microtime( true ) - $this->created_time; |
|
206 | - |
|
207 | - // Get the CPU time if the hosting environment uses it rather than wall-clock time to calculate a process's execution time. |
|
208 | - if ( function_exists( 'getrusage' ) && apply_filters( 'action_scheduler_use_cpu_execution_time', defined( 'PANTHEON_ENVIRONMENT' ) ) ) { |
|
209 | - $resource_usages = getrusage(); |
|
210 | - |
|
211 | - if ( isset( $resource_usages['ru_stime.tv_usec'], $resource_usages['ru_stime.tv_usec'] ) ) { |
|
212 | - $execution_time = $resource_usages['ru_stime.tv_sec'] + ( $resource_usages['ru_stime.tv_usec'] / 1000000 ); |
|
213 | - } |
|
214 | - } |
|
215 | - |
|
216 | - return $execution_time; |
|
217 | - } |
|
218 | - |
|
219 | - /** |
|
220 | - * Check if the host's max execution time is (likely) to be exceeded if processing more actions. |
|
221 | - * |
|
222 | - * @param int $processed_actions The number of actions processed so far - used to determine the likelihood of exceeding the time limit if processing another action |
|
223 | - * @return bool |
|
224 | - */ |
|
225 | - protected function time_likely_to_be_exceeded( $processed_actions ) { |
|
226 | - |
|
227 | - $execution_time = $this->get_execution_time(); |
|
228 | - $max_execution_time = $this->get_time_limit(); |
|
229 | - $time_per_action = $execution_time / $processed_actions; |
|
230 | - $estimated_time = $execution_time + ( $time_per_action * 3 ); |
|
231 | - $likely_to_be_exceeded = $estimated_time > $max_execution_time; |
|
232 | - |
|
233 | - return apply_filters( 'action_scheduler_maximum_execution_time_likely_to_be_exceeded', $likely_to_be_exceeded, $this, $processed_actions, $execution_time, $max_execution_time ); |
|
234 | - } |
|
235 | - |
|
236 | - /** |
|
237 | - * Get memory limit |
|
238 | - * |
|
239 | - * Based on WP_Background_Process::get_memory_limit() |
|
240 | - * |
|
241 | - * @return int |
|
242 | - */ |
|
243 | - protected function get_memory_limit() { |
|
244 | - if ( function_exists( 'ini_get' ) ) { |
|
245 | - $memory_limit = ini_get( 'memory_limit' ); |
|
246 | - } else { |
|
247 | - $memory_limit = '128M'; // Sensible default, and minimum required by WooCommerce |
|
248 | - } |
|
249 | - |
|
250 | - if ( ! $memory_limit || -1 === $memory_limit || '-1' === $memory_limit ) { |
|
251 | - // Unlimited, set to 32GB. |
|
252 | - $memory_limit = '32G'; |
|
253 | - } |
|
254 | - |
|
255 | - return ActionScheduler_Compatibility::convert_hr_to_bytes( $memory_limit ); |
|
256 | - } |
|
257 | - |
|
258 | - /** |
|
259 | - * Memory exceeded |
|
260 | - * |
|
261 | - * Ensures the batch process never exceeds 90% of the maximum WordPress memory. |
|
262 | - * |
|
263 | - * Based on WP_Background_Process::memory_exceeded() |
|
264 | - * |
|
265 | - * @return bool |
|
266 | - */ |
|
267 | - protected function memory_exceeded() { |
|
268 | - |
|
269 | - $memory_limit = $this->get_memory_limit() * 0.90; |
|
270 | - $current_memory = memory_get_usage( true ); |
|
271 | - $memory_exceeded = $current_memory >= $memory_limit; |
|
272 | - |
|
273 | - return apply_filters( 'action_scheduler_memory_exceeded', $memory_exceeded, $this ); |
|
274 | - } |
|
275 | - |
|
276 | - /** |
|
277 | - * See if the batch limits have been exceeded, which is when memory usage is almost at |
|
278 | - * the maximum limit, or the time to process more actions will exceed the max time limit. |
|
279 | - * |
|
280 | - * Based on WC_Background_Process::batch_limits_exceeded() |
|
281 | - * |
|
282 | - * @param int $processed_actions The number of actions processed so far - used to determine the likelihood of exceeding the time limit if processing another action |
|
283 | - * @return bool |
|
284 | - */ |
|
285 | - protected function batch_limits_exceeded( $processed_actions ) { |
|
286 | - return $this->memory_exceeded() || $this->time_likely_to_be_exceeded( $processed_actions ); |
|
287 | - } |
|
288 | - |
|
289 | - /** |
|
290 | - * Process actions in the queue. |
|
291 | - * |
|
292 | - * @author Jeremy Pry |
|
293 | - * @param string $context Optional identifer for the context in which this action is being processed, e.g. 'WP CLI' or 'WP Cron' |
|
294 | - * Generally, this should be capitalised and not localised as it's a proper noun. |
|
295 | - * @return int The number of actions processed. |
|
296 | - */ |
|
297 | - abstract public function run( $context = '' ); |
|
8 | + /** @var ActionScheduler_QueueCleaner */ |
|
9 | + protected $cleaner; |
|
10 | + |
|
11 | + /** @var ActionScheduler_FatalErrorMonitor */ |
|
12 | + protected $monitor; |
|
13 | + |
|
14 | + /** @var ActionScheduler_Store */ |
|
15 | + protected $store; |
|
16 | + |
|
17 | + /** |
|
18 | + * The created time. |
|
19 | + * |
|
20 | + * Represents when the queue runner was constructed and used when calculating how long a PHP request has been running. |
|
21 | + * For this reason it should be as close as possible to the PHP request start time. |
|
22 | + * |
|
23 | + * @var int |
|
24 | + */ |
|
25 | + private $created_time; |
|
26 | + |
|
27 | + /** |
|
28 | + * ActionScheduler_Abstract_QueueRunner constructor. |
|
29 | + * |
|
30 | + * @param ActionScheduler_Store $store |
|
31 | + * @param ActionScheduler_FatalErrorMonitor $monitor |
|
32 | + * @param ActionScheduler_QueueCleaner $cleaner |
|
33 | + */ |
|
34 | + public function __construct( ActionScheduler_Store $store = null, ActionScheduler_FatalErrorMonitor $monitor = null, ActionScheduler_QueueCleaner $cleaner = null ) { |
|
35 | + |
|
36 | + $this->created_time = microtime( true ); |
|
37 | + |
|
38 | + $this->store = $store ? $store : ActionScheduler_Store::instance(); |
|
39 | + $this->monitor = $monitor ? $monitor : new ActionScheduler_FatalErrorMonitor( $this->store ); |
|
40 | + $this->cleaner = $cleaner ? $cleaner : new ActionScheduler_QueueCleaner( $this->store ); |
|
41 | + } |
|
42 | + |
|
43 | + /** |
|
44 | + * Process an individual action. |
|
45 | + * |
|
46 | + * @param int $action_id The action ID to process. |
|
47 | + * @param string $context Optional identifer for the context in which this action is being processed, e.g. 'WP CLI' or 'WP Cron' |
|
48 | + * Generally, this should be capitalised and not localised as it's a proper noun. |
|
49 | + */ |
|
50 | + public function process_action( $action_id, $context = '' ) { |
|
51 | + try { |
|
52 | + $valid_action = false; |
|
53 | + do_action( 'action_scheduler_before_execute', $action_id, $context ); |
|
54 | + |
|
55 | + if ( ActionScheduler_Store::STATUS_PENDING !== $this->store->get_status( $action_id ) ) { |
|
56 | + do_action( 'action_scheduler_execution_ignored', $action_id, $context ); |
|
57 | + return; |
|
58 | + } |
|
59 | + |
|
60 | + $valid_action = true; |
|
61 | + do_action( 'action_scheduler_begin_execute', $action_id, $context ); |
|
62 | + |
|
63 | + $action = $this->store->fetch_action( $action_id ); |
|
64 | + $this->store->log_execution( $action_id ); |
|
65 | + $action->execute(); |
|
66 | + do_action( 'action_scheduler_after_execute', $action_id, $action, $context ); |
|
67 | + $this->store->mark_complete( $action_id ); |
|
68 | + } catch ( Exception $e ) { |
|
69 | + if ( $valid_action ) { |
|
70 | + $this->store->mark_failure( $action_id ); |
|
71 | + do_action( 'action_scheduler_failed_execution', $action_id, $e, $context ); |
|
72 | + } else { |
|
73 | + do_action( 'action_scheduler_failed_validation', $action_id, $e, $context ); |
|
74 | + } |
|
75 | + } |
|
76 | + |
|
77 | + if ( isset( $action ) && is_a( $action, 'ActionScheduler_Action' ) && $action->get_schedule()->is_recurring() ) { |
|
78 | + $this->schedule_next_instance( $action, $action_id ); |
|
79 | + } |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Schedule the next instance of the action if necessary. |
|
84 | + * |
|
85 | + * @param ActionScheduler_Action $action |
|
86 | + * @param int $action_id |
|
87 | + */ |
|
88 | + protected function schedule_next_instance( ActionScheduler_Action $action, $action_id ) { |
|
89 | + // If a recurring action has been consistently failing, we may wish to stop rescheduling it. |
|
90 | + if ( |
|
91 | + ActionScheduler_Store::STATUS_FAILED === $this->store->get_status( $action_id ) |
|
92 | + && $this->recurring_action_is_consistently_failing( $action, $action_id ) |
|
93 | + ) { |
|
94 | + ActionScheduler_Logger::instance()->log( |
|
95 | + $action_id, |
|
96 | + __( 'This action appears to be consistently failing. A new instance will not be scheduled.', 'action-scheduler' ) |
|
97 | + ); |
|
98 | + |
|
99 | + return; |
|
100 | + } |
|
101 | + |
|
102 | + try { |
|
103 | + ActionScheduler::factory()->repeat( $action ); |
|
104 | + } catch ( Exception $e ) { |
|
105 | + do_action( 'action_scheduler_failed_to_schedule_next_instance', $action_id, $e, $action ); |
|
106 | + } |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * Determine if the specified recurring action has been consistently failing. |
|
111 | + * |
|
112 | + * @param ActionScheduler_Action $action The recurring action to be rescheduled. |
|
113 | + * @param int $action_id The ID of the recurring action. |
|
114 | + * |
|
115 | + * @return bool |
|
116 | + */ |
|
117 | + private function recurring_action_is_consistently_failing( ActionScheduler_Action $action, $action_id ) { |
|
118 | + /** |
|
119 | + * Controls the failure threshold for recurring actions. |
|
120 | + * |
|
121 | + * Before rescheduling a recurring action, we look at its status. If it failed, we then check if all of the most |
|
122 | + * recent actions (upto the threshold set by this filter) sharing the same hook have also failed: if they have, |
|
123 | + * that is considered consistent failure and a new instance of the action will not be scheduled. |
|
124 | + * |
|
125 | + * @param int $failure_threshold Number of actions of the same hook to examine for failure. Defaults to 5. |
|
126 | + */ |
|
127 | + $consistent_failure_threshold = (int) apply_filters( 'action_scheduler_recurring_action_failure_threshold', 5 ); |
|
128 | + |
|
129 | + // This query should find the earliest *failing* action (for the hook we are interested in) within our threshold. |
|
130 | + $query_args = array( |
|
131 | + 'hook' => $action->get_hook(), |
|
132 | + 'status' => ActionScheduler_Store::STATUS_FAILED, |
|
133 | + 'date' => date_create( 'now', timezone_open( 'UTC' ) )->format( 'Y-m-d H:i:s' ), |
|
134 | + 'date_compare' => '<', |
|
135 | + 'per_page' => 1, |
|
136 | + 'offset' => $consistent_failure_threshold - 1, |
|
137 | + ); |
|
138 | + |
|
139 | + $first_failing_action_id = $this->store->query_actions( $query_args ); |
|
140 | + |
|
141 | + // If we didn't retrieve an action ID, then there haven't been enough failures for us to worry about. |
|
142 | + if ( empty( $first_failing_action_id ) ) { |
|
143 | + return false; |
|
144 | + } |
|
145 | + |
|
146 | + // Now let's fetch the first action (having the same hook) of *any status*ithin the same window. |
|
147 | + unset( $query_args['status'] ); |
|
148 | + $first_action_id_with_the_same_hook = $this->store->query_actions( $query_args ); |
|
149 | + |
|
150 | + // If the IDs match, then actions for this hook must be consistently failing. |
|
151 | + return $first_action_id_with_the_same_hook === $first_failing_action_id; |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * Run the queue cleaner. |
|
156 | + * |
|
157 | + * @author Jeremy Pry |
|
158 | + */ |
|
159 | + protected function run_cleanup() { |
|
160 | + $this->cleaner->clean( 10 * $this->get_time_limit() ); |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * Get the number of concurrent batches a runner allows. |
|
165 | + * |
|
166 | + * @return int |
|
167 | + */ |
|
168 | + public function get_allowed_concurrent_batches() { |
|
169 | + return apply_filters( 'action_scheduler_queue_runner_concurrent_batches', 1 ); |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * Check if the number of allowed concurrent batches is met or exceeded. |
|
174 | + * |
|
175 | + * @return bool |
|
176 | + */ |
|
177 | + public function has_maximum_concurrent_batches() { |
|
178 | + return $this->store->get_claim_count() >= $this->get_allowed_concurrent_batches(); |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * Get the maximum number of seconds a batch can run for. |
|
183 | + * |
|
184 | + * @return int The number of seconds. |
|
185 | + */ |
|
186 | + protected function get_time_limit() { |
|
187 | + |
|
188 | + $time_limit = 30; |
|
189 | + |
|
190 | + // Apply deprecated filter from deprecated get_maximum_execution_time() method |
|
191 | + if ( has_filter( 'action_scheduler_maximum_execution_time' ) ) { |
|
192 | + _deprecated_function( 'action_scheduler_maximum_execution_time', '2.1.1', 'action_scheduler_queue_runner_time_limit' ); |
|
193 | + $time_limit = apply_filters( 'action_scheduler_maximum_execution_time', $time_limit ); |
|
194 | + } |
|
195 | + |
|
196 | + return absint( apply_filters( 'action_scheduler_queue_runner_time_limit', $time_limit ) ); |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * Get the number of seconds the process has been running. |
|
201 | + * |
|
202 | + * @return int The number of seconds. |
|
203 | + */ |
|
204 | + protected function get_execution_time() { |
|
205 | + $execution_time = microtime( true ) - $this->created_time; |
|
206 | + |
|
207 | + // Get the CPU time if the hosting environment uses it rather than wall-clock time to calculate a process's execution time. |
|
208 | + if ( function_exists( 'getrusage' ) && apply_filters( 'action_scheduler_use_cpu_execution_time', defined( 'PANTHEON_ENVIRONMENT' ) ) ) { |
|
209 | + $resource_usages = getrusage(); |
|
210 | + |
|
211 | + if ( isset( $resource_usages['ru_stime.tv_usec'], $resource_usages['ru_stime.tv_usec'] ) ) { |
|
212 | + $execution_time = $resource_usages['ru_stime.tv_sec'] + ( $resource_usages['ru_stime.tv_usec'] / 1000000 ); |
|
213 | + } |
|
214 | + } |
|
215 | + |
|
216 | + return $execution_time; |
|
217 | + } |
|
218 | + |
|
219 | + /** |
|
220 | + * Check if the host's max execution time is (likely) to be exceeded if processing more actions. |
|
221 | + * |
|
222 | + * @param int $processed_actions The number of actions processed so far - used to determine the likelihood of exceeding the time limit if processing another action |
|
223 | + * @return bool |
|
224 | + */ |
|
225 | + protected function time_likely_to_be_exceeded( $processed_actions ) { |
|
226 | + |
|
227 | + $execution_time = $this->get_execution_time(); |
|
228 | + $max_execution_time = $this->get_time_limit(); |
|
229 | + $time_per_action = $execution_time / $processed_actions; |
|
230 | + $estimated_time = $execution_time + ( $time_per_action * 3 ); |
|
231 | + $likely_to_be_exceeded = $estimated_time > $max_execution_time; |
|
232 | + |
|
233 | + return apply_filters( 'action_scheduler_maximum_execution_time_likely_to_be_exceeded', $likely_to_be_exceeded, $this, $processed_actions, $execution_time, $max_execution_time ); |
|
234 | + } |
|
235 | + |
|
236 | + /** |
|
237 | + * Get memory limit |
|
238 | + * |
|
239 | + * Based on WP_Background_Process::get_memory_limit() |
|
240 | + * |
|
241 | + * @return int |
|
242 | + */ |
|
243 | + protected function get_memory_limit() { |
|
244 | + if ( function_exists( 'ini_get' ) ) { |
|
245 | + $memory_limit = ini_get( 'memory_limit' ); |
|
246 | + } else { |
|
247 | + $memory_limit = '128M'; // Sensible default, and minimum required by WooCommerce |
|
248 | + } |
|
249 | + |
|
250 | + if ( ! $memory_limit || -1 === $memory_limit || '-1' === $memory_limit ) { |
|
251 | + // Unlimited, set to 32GB. |
|
252 | + $memory_limit = '32G'; |
|
253 | + } |
|
254 | + |
|
255 | + return ActionScheduler_Compatibility::convert_hr_to_bytes( $memory_limit ); |
|
256 | + } |
|
257 | + |
|
258 | + /** |
|
259 | + * Memory exceeded |
|
260 | + * |
|
261 | + * Ensures the batch process never exceeds 90% of the maximum WordPress memory. |
|
262 | + * |
|
263 | + * Based on WP_Background_Process::memory_exceeded() |
|
264 | + * |
|
265 | + * @return bool |
|
266 | + */ |
|
267 | + protected function memory_exceeded() { |
|
268 | + |
|
269 | + $memory_limit = $this->get_memory_limit() * 0.90; |
|
270 | + $current_memory = memory_get_usage( true ); |
|
271 | + $memory_exceeded = $current_memory >= $memory_limit; |
|
272 | + |
|
273 | + return apply_filters( 'action_scheduler_memory_exceeded', $memory_exceeded, $this ); |
|
274 | + } |
|
275 | + |
|
276 | + /** |
|
277 | + * See if the batch limits have been exceeded, which is when memory usage is almost at |
|
278 | + * the maximum limit, or the time to process more actions will exceed the max time limit. |
|
279 | + * |
|
280 | + * Based on WC_Background_Process::batch_limits_exceeded() |
|
281 | + * |
|
282 | + * @param int $processed_actions The number of actions processed so far - used to determine the likelihood of exceeding the time limit if processing another action |
|
283 | + * @return bool |
|
284 | + */ |
|
285 | + protected function batch_limits_exceeded( $processed_actions ) { |
|
286 | + return $this->memory_exceeded() || $this->time_likely_to_be_exceeded( $processed_actions ); |
|
287 | + } |
|
288 | + |
|
289 | + /** |
|
290 | + * Process actions in the queue. |
|
291 | + * |
|
292 | + * @author Jeremy Pry |
|
293 | + * @param string $context Optional identifer for the context in which this action is being processed, e.g. 'WP CLI' or 'WP Cron' |
|
294 | + * Generally, this should be capitalised and not localised as it's a proper noun. |
|
295 | + * @return int The number of actions processed. |
|
296 | + */ |
|
297 | + abstract public function run( $context = '' ); |
|
298 | 298 | } |
@@ -31,13 +31,13 @@ discard block |
||
31 | 31 | * @param ActionScheduler_FatalErrorMonitor $monitor |
32 | 32 | * @param ActionScheduler_QueueCleaner $cleaner |
33 | 33 | */ |
34 | - public function __construct( ActionScheduler_Store $store = null, ActionScheduler_FatalErrorMonitor $monitor = null, ActionScheduler_QueueCleaner $cleaner = null ) { |
|
34 | + public function __construct(ActionScheduler_Store $store = null, ActionScheduler_FatalErrorMonitor $monitor = null, ActionScheduler_QueueCleaner $cleaner = null) { |
|
35 | 35 | |
36 | - $this->created_time = microtime( true ); |
|
36 | + $this->created_time = microtime(true); |
|
37 | 37 | |
38 | 38 | $this->store = $store ? $store : ActionScheduler_Store::instance(); |
39 | - $this->monitor = $monitor ? $monitor : new ActionScheduler_FatalErrorMonitor( $this->store ); |
|
40 | - $this->cleaner = $cleaner ? $cleaner : new ActionScheduler_QueueCleaner( $this->store ); |
|
39 | + $this->monitor = $monitor ? $monitor : new ActionScheduler_FatalErrorMonitor($this->store); |
|
40 | + $this->cleaner = $cleaner ? $cleaner : new ActionScheduler_QueueCleaner($this->store); |
|
41 | 41 | } |
42 | 42 | |
43 | 43 | /** |
@@ -47,35 +47,35 @@ discard block |
||
47 | 47 | * @param string $context Optional identifer for the context in which this action is being processed, e.g. 'WP CLI' or 'WP Cron' |
48 | 48 | * Generally, this should be capitalised and not localised as it's a proper noun. |
49 | 49 | */ |
50 | - public function process_action( $action_id, $context = '' ) { |
|
50 | + public function process_action($action_id, $context = '') { |
|
51 | 51 | try { |
52 | 52 | $valid_action = false; |
53 | - do_action( 'action_scheduler_before_execute', $action_id, $context ); |
|
53 | + do_action('action_scheduler_before_execute', $action_id, $context); |
|
54 | 54 | |
55 | - if ( ActionScheduler_Store::STATUS_PENDING !== $this->store->get_status( $action_id ) ) { |
|
56 | - do_action( 'action_scheduler_execution_ignored', $action_id, $context ); |
|
55 | + if (ActionScheduler_Store::STATUS_PENDING !== $this->store->get_status($action_id)) { |
|
56 | + do_action('action_scheduler_execution_ignored', $action_id, $context); |
|
57 | 57 | return; |
58 | 58 | } |
59 | 59 | |
60 | 60 | $valid_action = true; |
61 | - do_action( 'action_scheduler_begin_execute', $action_id, $context ); |
|
61 | + do_action('action_scheduler_begin_execute', $action_id, $context); |
|
62 | 62 | |
63 | - $action = $this->store->fetch_action( $action_id ); |
|
64 | - $this->store->log_execution( $action_id ); |
|
63 | + $action = $this->store->fetch_action($action_id); |
|
64 | + $this->store->log_execution($action_id); |
|
65 | 65 | $action->execute(); |
66 | - do_action( 'action_scheduler_after_execute', $action_id, $action, $context ); |
|
67 | - $this->store->mark_complete( $action_id ); |
|
68 | - } catch ( Exception $e ) { |
|
69 | - if ( $valid_action ) { |
|
70 | - $this->store->mark_failure( $action_id ); |
|
71 | - do_action( 'action_scheduler_failed_execution', $action_id, $e, $context ); |
|
66 | + do_action('action_scheduler_after_execute', $action_id, $action, $context); |
|
67 | + $this->store->mark_complete($action_id); |
|
68 | + } catch (Exception $e) { |
|
69 | + if ($valid_action) { |
|
70 | + $this->store->mark_failure($action_id); |
|
71 | + do_action('action_scheduler_failed_execution', $action_id, $e, $context); |
|
72 | 72 | } else { |
73 | - do_action( 'action_scheduler_failed_validation', $action_id, $e, $context ); |
|
73 | + do_action('action_scheduler_failed_validation', $action_id, $e, $context); |
|
74 | 74 | } |
75 | 75 | } |
76 | 76 | |
77 | - if ( isset( $action ) && is_a( $action, 'ActionScheduler_Action' ) && $action->get_schedule()->is_recurring() ) { |
|
78 | - $this->schedule_next_instance( $action, $action_id ); |
|
77 | + if (isset($action) && is_a($action, 'ActionScheduler_Action') && $action->get_schedule()->is_recurring()) { |
|
78 | + $this->schedule_next_instance($action, $action_id); |
|
79 | 79 | } |
80 | 80 | } |
81 | 81 | |
@@ -85,24 +85,24 @@ discard block |
||
85 | 85 | * @param ActionScheduler_Action $action |
86 | 86 | * @param int $action_id |
87 | 87 | */ |
88 | - protected function schedule_next_instance( ActionScheduler_Action $action, $action_id ) { |
|
88 | + protected function schedule_next_instance(ActionScheduler_Action $action, $action_id) { |
|
89 | 89 | // If a recurring action has been consistently failing, we may wish to stop rescheduling it. |
90 | 90 | if ( |
91 | - ActionScheduler_Store::STATUS_FAILED === $this->store->get_status( $action_id ) |
|
92 | - && $this->recurring_action_is_consistently_failing( $action, $action_id ) |
|
91 | + ActionScheduler_Store::STATUS_FAILED === $this->store->get_status($action_id) |
|
92 | + && $this->recurring_action_is_consistently_failing($action, $action_id) |
|
93 | 93 | ) { |
94 | 94 | ActionScheduler_Logger::instance()->log( |
95 | 95 | $action_id, |
96 | - __( 'This action appears to be consistently failing. A new instance will not be scheduled.', 'action-scheduler' ) |
|
96 | + __('This action appears to be consistently failing. A new instance will not be scheduled.', 'action-scheduler') |
|
97 | 97 | ); |
98 | 98 | |
99 | 99 | return; |
100 | 100 | } |
101 | 101 | |
102 | 102 | try { |
103 | - ActionScheduler::factory()->repeat( $action ); |
|
104 | - } catch ( Exception $e ) { |
|
105 | - do_action( 'action_scheduler_failed_to_schedule_next_instance', $action_id, $e, $action ); |
|
103 | + ActionScheduler::factory()->repeat($action); |
|
104 | + } catch (Exception $e) { |
|
105 | + do_action('action_scheduler_failed_to_schedule_next_instance', $action_id, $e, $action); |
|
106 | 106 | } |
107 | 107 | } |
108 | 108 | |
@@ -114,7 +114,7 @@ discard block |
||
114 | 114 | * |
115 | 115 | * @return bool |
116 | 116 | */ |
117 | - private function recurring_action_is_consistently_failing( ActionScheduler_Action $action, $action_id ) { |
|
117 | + private function recurring_action_is_consistently_failing(ActionScheduler_Action $action, $action_id) { |
|
118 | 118 | /** |
119 | 119 | * Controls the failure threshold for recurring actions. |
120 | 120 | * |
@@ -124,28 +124,28 @@ discard block |
||
124 | 124 | * |
125 | 125 | * @param int $failure_threshold Number of actions of the same hook to examine for failure. Defaults to 5. |
126 | 126 | */ |
127 | - $consistent_failure_threshold = (int) apply_filters( 'action_scheduler_recurring_action_failure_threshold', 5 ); |
|
127 | + $consistent_failure_threshold = (int) apply_filters('action_scheduler_recurring_action_failure_threshold', 5); |
|
128 | 128 | |
129 | 129 | // This query should find the earliest *failing* action (for the hook we are interested in) within our threshold. |
130 | 130 | $query_args = array( |
131 | 131 | 'hook' => $action->get_hook(), |
132 | 132 | 'status' => ActionScheduler_Store::STATUS_FAILED, |
133 | - 'date' => date_create( 'now', timezone_open( 'UTC' ) )->format( 'Y-m-d H:i:s' ), |
|
133 | + 'date' => date_create('now', timezone_open('UTC'))->format('Y-m-d H:i:s'), |
|
134 | 134 | 'date_compare' => '<', |
135 | 135 | 'per_page' => 1, |
136 | 136 | 'offset' => $consistent_failure_threshold - 1, |
137 | 137 | ); |
138 | 138 | |
139 | - $first_failing_action_id = $this->store->query_actions( $query_args ); |
|
139 | + $first_failing_action_id = $this->store->query_actions($query_args); |
|
140 | 140 | |
141 | 141 | // If we didn't retrieve an action ID, then there haven't been enough failures for us to worry about. |
142 | - if ( empty( $first_failing_action_id ) ) { |
|
142 | + if (empty($first_failing_action_id)) { |
|
143 | 143 | return false; |
144 | 144 | } |
145 | 145 | |
146 | 146 | // Now let's fetch the first action (having the same hook) of *any status*ithin the same window. |
147 | - unset( $query_args['status'] ); |
|
148 | - $first_action_id_with_the_same_hook = $this->store->query_actions( $query_args ); |
|
147 | + unset($query_args['status']); |
|
148 | + $first_action_id_with_the_same_hook = $this->store->query_actions($query_args); |
|
149 | 149 | |
150 | 150 | // If the IDs match, then actions for this hook must be consistently failing. |
151 | 151 | return $first_action_id_with_the_same_hook === $first_failing_action_id; |
@@ -157,7 +157,7 @@ discard block |
||
157 | 157 | * @author Jeremy Pry |
158 | 158 | */ |
159 | 159 | protected function run_cleanup() { |
160 | - $this->cleaner->clean( 10 * $this->get_time_limit() ); |
|
160 | + $this->cleaner->clean(10 * $this->get_time_limit()); |
|
161 | 161 | } |
162 | 162 | |
163 | 163 | /** |
@@ -166,7 +166,7 @@ discard block |
||
166 | 166 | * @return int |
167 | 167 | */ |
168 | 168 | public function get_allowed_concurrent_batches() { |
169 | - return apply_filters( 'action_scheduler_queue_runner_concurrent_batches', 1 ); |
|
169 | + return apply_filters('action_scheduler_queue_runner_concurrent_batches', 1); |
|
170 | 170 | } |
171 | 171 | |
172 | 172 | /** |
@@ -188,12 +188,12 @@ discard block |
||
188 | 188 | $time_limit = 30; |
189 | 189 | |
190 | 190 | // Apply deprecated filter from deprecated get_maximum_execution_time() method |
191 | - if ( has_filter( 'action_scheduler_maximum_execution_time' ) ) { |
|
192 | - _deprecated_function( 'action_scheduler_maximum_execution_time', '2.1.1', 'action_scheduler_queue_runner_time_limit' ); |
|
193 | - $time_limit = apply_filters( 'action_scheduler_maximum_execution_time', $time_limit ); |
|
191 | + if (has_filter('action_scheduler_maximum_execution_time')) { |
|
192 | + _deprecated_function('action_scheduler_maximum_execution_time', '2.1.1', 'action_scheduler_queue_runner_time_limit'); |
|
193 | + $time_limit = apply_filters('action_scheduler_maximum_execution_time', $time_limit); |
|
194 | 194 | } |
195 | 195 | |
196 | - return absint( apply_filters( 'action_scheduler_queue_runner_time_limit', $time_limit ) ); |
|
196 | + return absint(apply_filters('action_scheduler_queue_runner_time_limit', $time_limit)); |
|
197 | 197 | } |
198 | 198 | |
199 | 199 | /** |
@@ -202,14 +202,14 @@ discard block |
||
202 | 202 | * @return int The number of seconds. |
203 | 203 | */ |
204 | 204 | protected function get_execution_time() { |
205 | - $execution_time = microtime( true ) - $this->created_time; |
|
205 | + $execution_time = microtime(true) - $this->created_time; |
|
206 | 206 | |
207 | 207 | // Get the CPU time if the hosting environment uses it rather than wall-clock time to calculate a process's execution time. |
208 | - if ( function_exists( 'getrusage' ) && apply_filters( 'action_scheduler_use_cpu_execution_time', defined( 'PANTHEON_ENVIRONMENT' ) ) ) { |
|
208 | + if (function_exists('getrusage') && apply_filters('action_scheduler_use_cpu_execution_time', defined('PANTHEON_ENVIRONMENT'))) { |
|
209 | 209 | $resource_usages = getrusage(); |
210 | 210 | |
211 | - if ( isset( $resource_usages['ru_stime.tv_usec'], $resource_usages['ru_stime.tv_usec'] ) ) { |
|
212 | - $execution_time = $resource_usages['ru_stime.tv_sec'] + ( $resource_usages['ru_stime.tv_usec'] / 1000000 ); |
|
211 | + if (isset($resource_usages['ru_stime.tv_usec'], $resource_usages['ru_stime.tv_usec'])) { |
|
212 | + $execution_time = $resource_usages['ru_stime.tv_sec'] + ($resource_usages['ru_stime.tv_usec'] / 1000000); |
|
213 | 213 | } |
214 | 214 | } |
215 | 215 | |
@@ -222,15 +222,15 @@ discard block |
||
222 | 222 | * @param int $processed_actions The number of actions processed so far - used to determine the likelihood of exceeding the time limit if processing another action |
223 | 223 | * @return bool |
224 | 224 | */ |
225 | - protected function time_likely_to_be_exceeded( $processed_actions ) { |
|
225 | + protected function time_likely_to_be_exceeded($processed_actions) { |
|
226 | 226 | |
227 | 227 | $execution_time = $this->get_execution_time(); |
228 | 228 | $max_execution_time = $this->get_time_limit(); |
229 | 229 | $time_per_action = $execution_time / $processed_actions; |
230 | - $estimated_time = $execution_time + ( $time_per_action * 3 ); |
|
230 | + $estimated_time = $execution_time + ($time_per_action * 3); |
|
231 | 231 | $likely_to_be_exceeded = $estimated_time > $max_execution_time; |
232 | 232 | |
233 | - return apply_filters( 'action_scheduler_maximum_execution_time_likely_to_be_exceeded', $likely_to_be_exceeded, $this, $processed_actions, $execution_time, $max_execution_time ); |
|
233 | + return apply_filters('action_scheduler_maximum_execution_time_likely_to_be_exceeded', $likely_to_be_exceeded, $this, $processed_actions, $execution_time, $max_execution_time); |
|
234 | 234 | } |
235 | 235 | |
236 | 236 | /** |
@@ -241,18 +241,18 @@ discard block |
||
241 | 241 | * @return int |
242 | 242 | */ |
243 | 243 | protected function get_memory_limit() { |
244 | - if ( function_exists( 'ini_get' ) ) { |
|
245 | - $memory_limit = ini_get( 'memory_limit' ); |
|
244 | + if (function_exists('ini_get')) { |
|
245 | + $memory_limit = ini_get('memory_limit'); |
|
246 | 246 | } else { |
247 | 247 | $memory_limit = '128M'; // Sensible default, and minimum required by WooCommerce |
248 | 248 | } |
249 | 249 | |
250 | - if ( ! $memory_limit || -1 === $memory_limit || '-1' === $memory_limit ) { |
|
250 | + if ( ! $memory_limit || -1 === $memory_limit || '-1' === $memory_limit) { |
|
251 | 251 | // Unlimited, set to 32GB. |
252 | 252 | $memory_limit = '32G'; |
253 | 253 | } |
254 | 254 | |
255 | - return ActionScheduler_Compatibility::convert_hr_to_bytes( $memory_limit ); |
|
255 | + return ActionScheduler_Compatibility::convert_hr_to_bytes($memory_limit); |
|
256 | 256 | } |
257 | 257 | |
258 | 258 | /** |
@@ -267,10 +267,10 @@ discard block |
||
267 | 267 | protected function memory_exceeded() { |
268 | 268 | |
269 | 269 | $memory_limit = $this->get_memory_limit() * 0.90; |
270 | - $current_memory = memory_get_usage( true ); |
|
270 | + $current_memory = memory_get_usage(true); |
|
271 | 271 | $memory_exceeded = $current_memory >= $memory_limit; |
272 | 272 | |
273 | - return apply_filters( 'action_scheduler_memory_exceeded', $memory_exceeded, $this ); |
|
273 | + return apply_filters('action_scheduler_memory_exceeded', $memory_exceeded, $this); |
|
274 | 274 | } |
275 | 275 | |
276 | 276 | /** |
@@ -282,8 +282,8 @@ discard block |
||
282 | 282 | * @param int $processed_actions The number of actions processed so far - used to determine the likelihood of exceeding the time limit if processing another action |
283 | 283 | * @return bool |
284 | 284 | */ |
285 | - protected function batch_limits_exceeded( $processed_actions ) { |
|
286 | - return $this->memory_exceeded() || $this->time_likely_to_be_exceeded( $processed_actions ); |
|
285 | + protected function batch_limits_exceeded($processed_actions) { |
|
286 | + return $this->memory_exceeded() || $this->time_likely_to_be_exceeded($processed_actions); |
|
287 | 287 | } |
288 | 288 | |
289 | 289 | /** |
@@ -294,5 +294,5 @@ discard block |
||
294 | 294 | * Generally, this should be capitalised and not localised as it's a proper noun. |
295 | 295 | * @return int The number of actions processed. |
296 | 296 | */ |
297 | - abstract public function run( $context = '' ); |
|
297 | + abstract public function run($context = ''); |
|
298 | 298 | } |
@@ -11,161 +11,161 @@ |
||
11 | 11 | */ |
12 | 12 | abstract class ActionScheduler_Abstract_Schema { |
13 | 13 | |
14 | - /** |
|
15 | - * @var int Increment this value in derived class to trigger a schema update. |
|
16 | - */ |
|
17 | - protected $schema_version = 1; |
|
18 | - |
|
19 | - /** |
|
20 | - * @var string Schema version stored in database. |
|
21 | - */ |
|
22 | - protected $db_version; |
|
23 | - |
|
24 | - /** |
|
25 | - * @var array Names of tables that will be registered by this class. |
|
26 | - */ |
|
27 | - protected $tables = array(); |
|
28 | - |
|
29 | - /** |
|
30 | - * Can optionally be used by concrete classes to carry out additional initialization work |
|
31 | - * as needed. |
|
32 | - */ |
|
33 | - public function init() {} |
|
34 | - |
|
35 | - /** |
|
36 | - * Register tables with WordPress, and create them if needed. |
|
37 | - * |
|
38 | - * @param bool $force_update Optional. Default false. Use true to always run the schema update. |
|
39 | - * |
|
40 | - * @return void |
|
41 | - */ |
|
42 | - public function register_tables( $force_update = false ) { |
|
43 | - global $wpdb; |
|
44 | - |
|
45 | - // make WP aware of our tables |
|
46 | - foreach ( $this->tables as $table ) { |
|
47 | - $wpdb->tables[] = $table; |
|
48 | - $name = $this->get_full_table_name( $table ); |
|
49 | - $wpdb->$table = $name; |
|
50 | - } |
|
51 | - |
|
52 | - // create the tables |
|
53 | - if ( $this->schema_update_required() || $force_update ) { |
|
54 | - foreach ( $this->tables as $table ) { |
|
55 | - /** |
|
56 | - * Allow custom processing before updating a table schema. |
|
57 | - * |
|
58 | - * @param string $table Name of table being updated. |
|
59 | - * @param string $db_version Existing version of the table being updated. |
|
60 | - */ |
|
61 | - do_action( 'action_scheduler_before_schema_update', $table, $this->db_version ); |
|
62 | - $this->update_table( $table ); |
|
63 | - } |
|
64 | - $this->mark_schema_update_complete(); |
|
65 | - } |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * @param string $table The name of the table |
|
70 | - * |
|
71 | - * @return string The CREATE TABLE statement, suitable for passing to dbDelta |
|
72 | - */ |
|
73 | - abstract protected function get_table_definition( $table ); |
|
74 | - |
|
75 | - /** |
|
76 | - * Determine if the database schema is out of date |
|
77 | - * by comparing the integer found in $this->schema_version |
|
78 | - * with the option set in the WordPress options table |
|
79 | - * |
|
80 | - * @return bool |
|
81 | - */ |
|
82 | - private function schema_update_required() { |
|
83 | - $option_name = 'schema-' . static::class; |
|
84 | - $this->db_version = get_option( $option_name, 0 ); |
|
85 | - |
|
86 | - // Check for schema option stored by the Action Scheduler Custom Tables plugin in case site has migrated from that plugin with an older schema |
|
87 | - if ( 0 === $this->db_version ) { |
|
88 | - |
|
89 | - $plugin_option_name = 'schema-'; |
|
90 | - |
|
91 | - switch ( static::class ) { |
|
92 | - case 'ActionScheduler_StoreSchema': |
|
93 | - $plugin_option_name .= 'Action_Scheduler\Custom_Tables\DB_Store_Table_Maker'; |
|
94 | - break; |
|
95 | - case 'ActionScheduler_LoggerSchema': |
|
96 | - $plugin_option_name .= 'Action_Scheduler\Custom_Tables\DB_Logger_Table_Maker'; |
|
97 | - break; |
|
98 | - } |
|
99 | - |
|
100 | - $this->db_version = get_option( $plugin_option_name, 0 ); |
|
101 | - |
|
102 | - delete_option( $plugin_option_name ); |
|
103 | - } |
|
104 | - |
|
105 | - return version_compare( $this->db_version, $this->schema_version, '<' ); |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * Update the option in WordPress to indicate that |
|
110 | - * our schema is now up to date |
|
111 | - * |
|
112 | - * @return void |
|
113 | - */ |
|
114 | - private function mark_schema_update_complete() { |
|
115 | - $option_name = 'schema-' . static::class; |
|
116 | - |
|
117 | - // work around race conditions and ensure that our option updates |
|
118 | - $value_to_save = (string) $this->schema_version . '.0.' . time(); |
|
119 | - |
|
120 | - update_option( $option_name, $value_to_save ); |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * Update the schema for the given table |
|
125 | - * |
|
126 | - * @param string $table The name of the table to update |
|
127 | - * |
|
128 | - * @return void |
|
129 | - */ |
|
130 | - private function update_table( $table ) { |
|
131 | - require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
|
132 | - $definition = $this->get_table_definition( $table ); |
|
133 | - if ( $definition ) { |
|
134 | - $updated = dbDelta( $definition ); |
|
135 | - foreach ( $updated as $updated_table => $update_description ) { |
|
136 | - if ( strpos( $update_description, 'Created table' ) === 0 ) { |
|
137 | - do_action( 'action_scheduler/created_table', $updated_table, $table ); |
|
138 | - } |
|
139 | - } |
|
140 | - } |
|
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * @param string $table |
|
145 | - * |
|
146 | - * @return string The full name of the table, including the |
|
147 | - * table prefix for the current blog |
|
148 | - */ |
|
149 | - protected function get_full_table_name( $table ) { |
|
150 | - return $GLOBALS['wpdb']->prefix . $table; |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * Confirms that all of the tables registered by this schema class have been created. |
|
155 | - * |
|
156 | - * @return bool |
|
157 | - */ |
|
158 | - public function tables_exist() { |
|
159 | - global $wpdb; |
|
160 | - |
|
161 | - $existing_tables = $wpdb->get_col( 'SHOW TABLES' ); |
|
162 | - $expected_tables = array_map( |
|
163 | - function ( $table_name ) use ( $wpdb ) { |
|
164 | - return $wpdb->prefix . $table_name; |
|
165 | - }, |
|
166 | - $this->tables |
|
167 | - ); |
|
168 | - |
|
169 | - return count( array_intersect( $existing_tables, $expected_tables ) ) === count( $expected_tables ); |
|
170 | - } |
|
14 | + /** |
|
15 | + * @var int Increment this value in derived class to trigger a schema update. |
|
16 | + */ |
|
17 | + protected $schema_version = 1; |
|
18 | + |
|
19 | + /** |
|
20 | + * @var string Schema version stored in database. |
|
21 | + */ |
|
22 | + protected $db_version; |
|
23 | + |
|
24 | + /** |
|
25 | + * @var array Names of tables that will be registered by this class. |
|
26 | + */ |
|
27 | + protected $tables = array(); |
|
28 | + |
|
29 | + /** |
|
30 | + * Can optionally be used by concrete classes to carry out additional initialization work |
|
31 | + * as needed. |
|
32 | + */ |
|
33 | + public function init() {} |
|
34 | + |
|
35 | + /** |
|
36 | + * Register tables with WordPress, and create them if needed. |
|
37 | + * |
|
38 | + * @param bool $force_update Optional. Default false. Use true to always run the schema update. |
|
39 | + * |
|
40 | + * @return void |
|
41 | + */ |
|
42 | + public function register_tables( $force_update = false ) { |
|
43 | + global $wpdb; |
|
44 | + |
|
45 | + // make WP aware of our tables |
|
46 | + foreach ( $this->tables as $table ) { |
|
47 | + $wpdb->tables[] = $table; |
|
48 | + $name = $this->get_full_table_name( $table ); |
|
49 | + $wpdb->$table = $name; |
|
50 | + } |
|
51 | + |
|
52 | + // create the tables |
|
53 | + if ( $this->schema_update_required() || $force_update ) { |
|
54 | + foreach ( $this->tables as $table ) { |
|
55 | + /** |
|
56 | + * Allow custom processing before updating a table schema. |
|
57 | + * |
|
58 | + * @param string $table Name of table being updated. |
|
59 | + * @param string $db_version Existing version of the table being updated. |
|
60 | + */ |
|
61 | + do_action( 'action_scheduler_before_schema_update', $table, $this->db_version ); |
|
62 | + $this->update_table( $table ); |
|
63 | + } |
|
64 | + $this->mark_schema_update_complete(); |
|
65 | + } |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * @param string $table The name of the table |
|
70 | + * |
|
71 | + * @return string The CREATE TABLE statement, suitable for passing to dbDelta |
|
72 | + */ |
|
73 | + abstract protected function get_table_definition( $table ); |
|
74 | + |
|
75 | + /** |
|
76 | + * Determine if the database schema is out of date |
|
77 | + * by comparing the integer found in $this->schema_version |
|
78 | + * with the option set in the WordPress options table |
|
79 | + * |
|
80 | + * @return bool |
|
81 | + */ |
|
82 | + private function schema_update_required() { |
|
83 | + $option_name = 'schema-' . static::class; |
|
84 | + $this->db_version = get_option( $option_name, 0 ); |
|
85 | + |
|
86 | + // Check for schema option stored by the Action Scheduler Custom Tables plugin in case site has migrated from that plugin with an older schema |
|
87 | + if ( 0 === $this->db_version ) { |
|
88 | + |
|
89 | + $plugin_option_name = 'schema-'; |
|
90 | + |
|
91 | + switch ( static::class ) { |
|
92 | + case 'ActionScheduler_StoreSchema': |
|
93 | + $plugin_option_name .= 'Action_Scheduler\Custom_Tables\DB_Store_Table_Maker'; |
|
94 | + break; |
|
95 | + case 'ActionScheduler_LoggerSchema': |
|
96 | + $plugin_option_name .= 'Action_Scheduler\Custom_Tables\DB_Logger_Table_Maker'; |
|
97 | + break; |
|
98 | + } |
|
99 | + |
|
100 | + $this->db_version = get_option( $plugin_option_name, 0 ); |
|
101 | + |
|
102 | + delete_option( $plugin_option_name ); |
|
103 | + } |
|
104 | + |
|
105 | + return version_compare( $this->db_version, $this->schema_version, '<' ); |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * Update the option in WordPress to indicate that |
|
110 | + * our schema is now up to date |
|
111 | + * |
|
112 | + * @return void |
|
113 | + */ |
|
114 | + private function mark_schema_update_complete() { |
|
115 | + $option_name = 'schema-' . static::class; |
|
116 | + |
|
117 | + // work around race conditions and ensure that our option updates |
|
118 | + $value_to_save = (string) $this->schema_version . '.0.' . time(); |
|
119 | + |
|
120 | + update_option( $option_name, $value_to_save ); |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * Update the schema for the given table |
|
125 | + * |
|
126 | + * @param string $table The name of the table to update |
|
127 | + * |
|
128 | + * @return void |
|
129 | + */ |
|
130 | + private function update_table( $table ) { |
|
131 | + require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
|
132 | + $definition = $this->get_table_definition( $table ); |
|
133 | + if ( $definition ) { |
|
134 | + $updated = dbDelta( $definition ); |
|
135 | + foreach ( $updated as $updated_table => $update_description ) { |
|
136 | + if ( strpos( $update_description, 'Created table' ) === 0 ) { |
|
137 | + do_action( 'action_scheduler/created_table', $updated_table, $table ); |
|
138 | + } |
|
139 | + } |
|
140 | + } |
|
141 | + } |
|
142 | + |
|
143 | + /** |
|
144 | + * @param string $table |
|
145 | + * |
|
146 | + * @return string The full name of the table, including the |
|
147 | + * table prefix for the current blog |
|
148 | + */ |
|
149 | + protected function get_full_table_name( $table ) { |
|
150 | + return $GLOBALS['wpdb']->prefix . $table; |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * Confirms that all of the tables registered by this schema class have been created. |
|
155 | + * |
|
156 | + * @return bool |
|
157 | + */ |
|
158 | + public function tables_exist() { |
|
159 | + global $wpdb; |
|
160 | + |
|
161 | + $existing_tables = $wpdb->get_col( 'SHOW TABLES' ); |
|
162 | + $expected_tables = array_map( |
|
163 | + function ( $table_name ) use ( $wpdb ) { |
|
164 | + return $wpdb->prefix . $table_name; |
|
165 | + }, |
|
166 | + $this->tables |
|
167 | + ); |
|
168 | + |
|
169 | + return count( array_intersect( $existing_tables, $expected_tables ) ) === count( $expected_tables ); |
|
170 | + } |
|
171 | 171 | } |
@@ -39,27 +39,27 @@ discard block |
||
39 | 39 | * |
40 | 40 | * @return void |
41 | 41 | */ |
42 | - public function register_tables( $force_update = false ) { |
|
42 | + public function register_tables($force_update = false) { |
|
43 | 43 | global $wpdb; |
44 | 44 | |
45 | 45 | // make WP aware of our tables |
46 | - foreach ( $this->tables as $table ) { |
|
46 | + foreach ($this->tables as $table) { |
|
47 | 47 | $wpdb->tables[] = $table; |
48 | - $name = $this->get_full_table_name( $table ); |
|
48 | + $name = $this->get_full_table_name($table); |
|
49 | 49 | $wpdb->$table = $name; |
50 | 50 | } |
51 | 51 | |
52 | 52 | // create the tables |
53 | - if ( $this->schema_update_required() || $force_update ) { |
|
54 | - foreach ( $this->tables as $table ) { |
|
53 | + if ($this->schema_update_required() || $force_update) { |
|
54 | + foreach ($this->tables as $table) { |
|
55 | 55 | /** |
56 | 56 | * Allow custom processing before updating a table schema. |
57 | 57 | * |
58 | 58 | * @param string $table Name of table being updated. |
59 | 59 | * @param string $db_version Existing version of the table being updated. |
60 | 60 | */ |
61 | - do_action( 'action_scheduler_before_schema_update', $table, $this->db_version ); |
|
62 | - $this->update_table( $table ); |
|
61 | + do_action('action_scheduler_before_schema_update', $table, $this->db_version); |
|
62 | + $this->update_table($table); |
|
63 | 63 | } |
64 | 64 | $this->mark_schema_update_complete(); |
65 | 65 | } |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | * |
71 | 71 | * @return string The CREATE TABLE statement, suitable for passing to dbDelta |
72 | 72 | */ |
73 | - abstract protected function get_table_definition( $table ); |
|
73 | + abstract protected function get_table_definition($table); |
|
74 | 74 | |
75 | 75 | /** |
76 | 76 | * Determine if the database schema is out of date |
@@ -80,15 +80,15 @@ discard block |
||
80 | 80 | * @return bool |
81 | 81 | */ |
82 | 82 | private function schema_update_required() { |
83 | - $option_name = 'schema-' . static::class; |
|
84 | - $this->db_version = get_option( $option_name, 0 ); |
|
83 | + $option_name = 'schema-'.static::class; |
|
84 | + $this->db_version = get_option($option_name, 0); |
|
85 | 85 | |
86 | 86 | // Check for schema option stored by the Action Scheduler Custom Tables plugin in case site has migrated from that plugin with an older schema |
87 | - if ( 0 === $this->db_version ) { |
|
87 | + if (0 === $this->db_version) { |
|
88 | 88 | |
89 | 89 | $plugin_option_name = 'schema-'; |
90 | 90 | |
91 | - switch ( static::class ) { |
|
91 | + switch (static::class) { |
|
92 | 92 | case 'ActionScheduler_StoreSchema': |
93 | 93 | $plugin_option_name .= 'Action_Scheduler\Custom_Tables\DB_Store_Table_Maker'; |
94 | 94 | break; |
@@ -97,12 +97,12 @@ discard block |
||
97 | 97 | break; |
98 | 98 | } |
99 | 99 | |
100 | - $this->db_version = get_option( $plugin_option_name, 0 ); |
|
100 | + $this->db_version = get_option($plugin_option_name, 0); |
|
101 | 101 | |
102 | - delete_option( $plugin_option_name ); |
|
102 | + delete_option($plugin_option_name); |
|
103 | 103 | } |
104 | 104 | |
105 | - return version_compare( $this->db_version, $this->schema_version, '<' ); |
|
105 | + return version_compare($this->db_version, $this->schema_version, '<'); |
|
106 | 106 | } |
107 | 107 | |
108 | 108 | /** |
@@ -112,12 +112,12 @@ discard block |
||
112 | 112 | * @return void |
113 | 113 | */ |
114 | 114 | private function mark_schema_update_complete() { |
115 | - $option_name = 'schema-' . static::class; |
|
115 | + $option_name = 'schema-'.static::class; |
|
116 | 116 | |
117 | 117 | // work around race conditions and ensure that our option updates |
118 | - $value_to_save = (string) $this->schema_version . '.0.' . time(); |
|
118 | + $value_to_save = (string) $this->schema_version.'.0.'.time(); |
|
119 | 119 | |
120 | - update_option( $option_name, $value_to_save ); |
|
120 | + update_option($option_name, $value_to_save); |
|
121 | 121 | } |
122 | 122 | |
123 | 123 | /** |
@@ -127,14 +127,14 @@ discard block |
||
127 | 127 | * |
128 | 128 | * @return void |
129 | 129 | */ |
130 | - private function update_table( $table ) { |
|
131 | - require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
|
132 | - $definition = $this->get_table_definition( $table ); |
|
133 | - if ( $definition ) { |
|
134 | - $updated = dbDelta( $definition ); |
|
135 | - foreach ( $updated as $updated_table => $update_description ) { |
|
136 | - if ( strpos( $update_description, 'Created table' ) === 0 ) { |
|
137 | - do_action( 'action_scheduler/created_table', $updated_table, $table ); |
|
130 | + private function update_table($table) { |
|
131 | + require_once ABSPATH.'wp-admin/includes/upgrade.php'; |
|
132 | + $definition = $this->get_table_definition($table); |
|
133 | + if ($definition) { |
|
134 | + $updated = dbDelta($definition); |
|
135 | + foreach ($updated as $updated_table => $update_description) { |
|
136 | + if (strpos($update_description, 'Created table') === 0) { |
|
137 | + do_action('action_scheduler/created_table', $updated_table, $table); |
|
138 | 138 | } |
139 | 139 | } |
140 | 140 | } |
@@ -146,8 +146,8 @@ discard block |
||
146 | 146 | * @return string The full name of the table, including the |
147 | 147 | * table prefix for the current blog |
148 | 148 | */ |
149 | - protected function get_full_table_name( $table ) { |
|
150 | - return $GLOBALS['wpdb']->prefix . $table; |
|
149 | + protected function get_full_table_name($table) { |
|
150 | + return $GLOBALS['wpdb']->prefix.$table; |
|
151 | 151 | } |
152 | 152 | |
153 | 153 | /** |
@@ -158,14 +158,14 @@ discard block |
||
158 | 158 | public function tables_exist() { |
159 | 159 | global $wpdb; |
160 | 160 | |
161 | - $existing_tables = $wpdb->get_col( 'SHOW TABLES' ); |
|
161 | + $existing_tables = $wpdb->get_col('SHOW TABLES'); |
|
162 | 162 | $expected_tables = array_map( |
163 | - function ( $table_name ) use ( $wpdb ) { |
|
164 | - return $wpdb->prefix . $table_name; |
|
163 | + function($table_name) use ($wpdb) { |
|
164 | + return $wpdb->prefix.$table_name; |
|
165 | 165 | }, |
166 | 166 | $this->tables |
167 | 167 | ); |
168 | 168 | |
169 | - return count( array_intersect( $existing_tables, $expected_tables ) ) === count( $expected_tables ); |
|
169 | + return count(array_intersect($existing_tables, $expected_tables)) === count($expected_tables); |
|
170 | 170 | } |
171 | 171 | } |
@@ -4,149 +4,149 @@ |
||
4 | 4 | * Class ActionScheduler_TimezoneHelper |
5 | 5 | */ |
6 | 6 | abstract class ActionScheduler_TimezoneHelper { |
7 | - private static $local_timezone = null; |
|
8 | - |
|
9 | - /** |
|
10 | - * Set a DateTime's timezone to the WordPress site's timezone, or a UTC offset |
|
11 | - * if no timezone string is available. |
|
12 | - * |
|
13 | - * @since 2.1.0 |
|
14 | - * |
|
15 | - * @param DateTime $date |
|
16 | - * @return ActionScheduler_DateTime |
|
17 | - */ |
|
18 | - public static function set_local_timezone( DateTime $date ) { |
|
19 | - |
|
20 | - // Accept a DateTime for easier backward compatibility, even though we require methods on ActionScheduler_DateTime |
|
21 | - if ( ! is_a( $date, 'ActionScheduler_DateTime' ) ) { |
|
22 | - $date = as_get_datetime_object( $date->format( 'U' ) ); |
|
23 | - } |
|
24 | - |
|
25 | - if ( get_option( 'timezone_string' ) ) { |
|
26 | - $date->setTimezone( new DateTimeZone( self::get_local_timezone_string() ) ); |
|
27 | - } else { |
|
28 | - $date->setUtcOffset( self::get_local_timezone_offset() ); |
|
29 | - } |
|
30 | - |
|
31 | - return $date; |
|
32 | - } |
|
33 | - |
|
34 | - /** |
|
35 | - * Helper to retrieve the timezone string for a site until a WP core method exists |
|
36 | - * (see https://core.trac.wordpress.org/ticket/24730). |
|
37 | - * |
|
38 | - * Adapted from wc_timezone_string() and https://secure.php.net/manual/en/function.timezone-name-from-abbr.php#89155. |
|
39 | - * |
|
40 | - * If no timezone string is set, and its not possible to match the UTC offset set for the site to a timezone |
|
41 | - * string, then an empty string will be returned, and the UTC offset should be used to set a DateTime's |
|
42 | - * timezone. |
|
43 | - * |
|
44 | - * @since 2.1.0 |
|
45 | - * @return string PHP timezone string for the site or empty if no timezone string is available. |
|
46 | - */ |
|
47 | - protected static function get_local_timezone_string( $reset = false ) { |
|
48 | - // If site timezone string exists, return it. |
|
49 | - $timezone = get_option( 'timezone_string' ); |
|
50 | - if ( $timezone ) { |
|
51 | - return $timezone; |
|
52 | - } |
|
53 | - |
|
54 | - // Get UTC offset, if it isn't set then return UTC. |
|
55 | - $utc_offset = intval( get_option( 'gmt_offset', 0 ) ); |
|
56 | - if ( 0 === $utc_offset ) { |
|
57 | - return 'UTC'; |
|
58 | - } |
|
59 | - |
|
60 | - // Adjust UTC offset from hours to seconds. |
|
61 | - $utc_offset *= 3600; |
|
62 | - |
|
63 | - // Attempt to guess the timezone string from the UTC offset. |
|
64 | - $timezone = timezone_name_from_abbr( '', $utc_offset ); |
|
65 | - if ( $timezone ) { |
|
66 | - return $timezone; |
|
67 | - } |
|
68 | - |
|
69 | - // Last try, guess timezone string manually. |
|
70 | - foreach ( timezone_abbreviations_list() as $abbr ) { |
|
71 | - foreach ( $abbr as $city ) { |
|
72 | - if ( (bool) date( 'I' ) === (bool) $city['dst'] && $city['timezone_id'] && intval( $city['offset'] ) === $utc_offset ) { |
|
73 | - return $city['timezone_id']; |
|
74 | - } |
|
75 | - } |
|
76 | - } |
|
77 | - |
|
78 | - // No timezone string |
|
79 | - return ''; |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Get timezone offset in seconds. |
|
84 | - * |
|
85 | - * @since 2.1.0 |
|
86 | - * @return float |
|
87 | - */ |
|
88 | - protected static function get_local_timezone_offset() { |
|
89 | - $timezone = get_option( 'timezone_string' ); |
|
90 | - |
|
91 | - if ( $timezone ) { |
|
92 | - $timezone_object = new DateTimeZone( $timezone ); |
|
93 | - return $timezone_object->getOffset( new DateTime( 'now' ) ); |
|
94 | - } else { |
|
95 | - return floatval( get_option( 'gmt_offset', 0 ) ) * HOUR_IN_SECONDS; |
|
96 | - } |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * @deprecated 2.1.0 |
|
101 | - */ |
|
102 | - public static function get_local_timezone( $reset = false ) { |
|
103 | - _deprecated_function( __FUNCTION__, '2.1.0', 'ActionScheduler_TimezoneHelper::set_local_timezone()' ); |
|
104 | - if ( $reset ) { |
|
105 | - self::$local_timezone = null; |
|
106 | - } |
|
107 | - if ( ! isset( self::$local_timezone ) ) { |
|
108 | - $tzstring = get_option( 'timezone_string' ); |
|
109 | - |
|
110 | - if ( empty( $tzstring ) ) { |
|
111 | - $gmt_offset = get_option( 'gmt_offset' ); |
|
112 | - if ( $gmt_offset == 0 ) { |
|
113 | - $tzstring = 'UTC'; |
|
114 | - } else { |
|
115 | - $gmt_offset *= HOUR_IN_SECONDS; |
|
116 | - $tzstring = timezone_name_from_abbr( '', $gmt_offset, 1 ); |
|
117 | - |
|
118 | - // If there's no timezone string, try again with no DST. |
|
119 | - if ( false === $tzstring ) { |
|
120 | - $tzstring = timezone_name_from_abbr( '', $gmt_offset, 0 ); |
|
121 | - } |
|
122 | - |
|
123 | - // Try mapping to the first abbreviation we can find. |
|
124 | - if ( false === $tzstring ) { |
|
125 | - $is_dst = date( 'I' ); |
|
126 | - foreach ( timezone_abbreviations_list() as $abbr ) { |
|
127 | - foreach ( $abbr as $city ) { |
|
128 | - if ( $city['dst'] == $is_dst && $city['offset'] == $gmt_offset ) { |
|
129 | - // If there's no valid timezone ID, keep looking. |
|
130 | - if ( null === $city['timezone_id'] ) { |
|
131 | - continue; |
|
132 | - } |
|
133 | - |
|
134 | - $tzstring = $city['timezone_id']; |
|
135 | - break 2; |
|
136 | - } |
|
137 | - } |
|
138 | - } |
|
139 | - } |
|
140 | - |
|
141 | - // If we still have no valid string, then fall back to UTC. |
|
142 | - if ( false === $tzstring ) { |
|
143 | - $tzstring = 'UTC'; |
|
144 | - } |
|
145 | - } |
|
146 | - } |
|
147 | - |
|
148 | - self::$local_timezone = new DateTimeZone( $tzstring ); |
|
149 | - } |
|
150 | - return self::$local_timezone; |
|
151 | - } |
|
7 | + private static $local_timezone = null; |
|
8 | + |
|
9 | + /** |
|
10 | + * Set a DateTime's timezone to the WordPress site's timezone, or a UTC offset |
|
11 | + * if no timezone string is available. |
|
12 | + * |
|
13 | + * @since 2.1.0 |
|
14 | + * |
|
15 | + * @param DateTime $date |
|
16 | + * @return ActionScheduler_DateTime |
|
17 | + */ |
|
18 | + public static function set_local_timezone( DateTime $date ) { |
|
19 | + |
|
20 | + // Accept a DateTime for easier backward compatibility, even though we require methods on ActionScheduler_DateTime |
|
21 | + if ( ! is_a( $date, 'ActionScheduler_DateTime' ) ) { |
|
22 | + $date = as_get_datetime_object( $date->format( 'U' ) ); |
|
23 | + } |
|
24 | + |
|
25 | + if ( get_option( 'timezone_string' ) ) { |
|
26 | + $date->setTimezone( new DateTimeZone( self::get_local_timezone_string() ) ); |
|
27 | + } else { |
|
28 | + $date->setUtcOffset( self::get_local_timezone_offset() ); |
|
29 | + } |
|
30 | + |
|
31 | + return $date; |
|
32 | + } |
|
33 | + |
|
34 | + /** |
|
35 | + * Helper to retrieve the timezone string for a site until a WP core method exists |
|
36 | + * (see https://core.trac.wordpress.org/ticket/24730). |
|
37 | + * |
|
38 | + * Adapted from wc_timezone_string() and https://secure.php.net/manual/en/function.timezone-name-from-abbr.php#89155. |
|
39 | + * |
|
40 | + * If no timezone string is set, and its not possible to match the UTC offset set for the site to a timezone |
|
41 | + * string, then an empty string will be returned, and the UTC offset should be used to set a DateTime's |
|
42 | + * timezone. |
|
43 | + * |
|
44 | + * @since 2.1.0 |
|
45 | + * @return string PHP timezone string for the site or empty if no timezone string is available. |
|
46 | + */ |
|
47 | + protected static function get_local_timezone_string( $reset = false ) { |
|
48 | + // If site timezone string exists, return it. |
|
49 | + $timezone = get_option( 'timezone_string' ); |
|
50 | + if ( $timezone ) { |
|
51 | + return $timezone; |
|
52 | + } |
|
53 | + |
|
54 | + // Get UTC offset, if it isn't set then return UTC. |
|
55 | + $utc_offset = intval( get_option( 'gmt_offset', 0 ) ); |
|
56 | + if ( 0 === $utc_offset ) { |
|
57 | + return 'UTC'; |
|
58 | + } |
|
59 | + |
|
60 | + // Adjust UTC offset from hours to seconds. |
|
61 | + $utc_offset *= 3600; |
|
62 | + |
|
63 | + // Attempt to guess the timezone string from the UTC offset. |
|
64 | + $timezone = timezone_name_from_abbr( '', $utc_offset ); |
|
65 | + if ( $timezone ) { |
|
66 | + return $timezone; |
|
67 | + } |
|
68 | + |
|
69 | + // Last try, guess timezone string manually. |
|
70 | + foreach ( timezone_abbreviations_list() as $abbr ) { |
|
71 | + foreach ( $abbr as $city ) { |
|
72 | + if ( (bool) date( 'I' ) === (bool) $city['dst'] && $city['timezone_id'] && intval( $city['offset'] ) === $utc_offset ) { |
|
73 | + return $city['timezone_id']; |
|
74 | + } |
|
75 | + } |
|
76 | + } |
|
77 | + |
|
78 | + // No timezone string |
|
79 | + return ''; |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Get timezone offset in seconds. |
|
84 | + * |
|
85 | + * @since 2.1.0 |
|
86 | + * @return float |
|
87 | + */ |
|
88 | + protected static function get_local_timezone_offset() { |
|
89 | + $timezone = get_option( 'timezone_string' ); |
|
90 | + |
|
91 | + if ( $timezone ) { |
|
92 | + $timezone_object = new DateTimeZone( $timezone ); |
|
93 | + return $timezone_object->getOffset( new DateTime( 'now' ) ); |
|
94 | + } else { |
|
95 | + return floatval( get_option( 'gmt_offset', 0 ) ) * HOUR_IN_SECONDS; |
|
96 | + } |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * @deprecated 2.1.0 |
|
101 | + */ |
|
102 | + public static function get_local_timezone( $reset = false ) { |
|
103 | + _deprecated_function( __FUNCTION__, '2.1.0', 'ActionScheduler_TimezoneHelper::set_local_timezone()' ); |
|
104 | + if ( $reset ) { |
|
105 | + self::$local_timezone = null; |
|
106 | + } |
|
107 | + if ( ! isset( self::$local_timezone ) ) { |
|
108 | + $tzstring = get_option( 'timezone_string' ); |
|
109 | + |
|
110 | + if ( empty( $tzstring ) ) { |
|
111 | + $gmt_offset = get_option( 'gmt_offset' ); |
|
112 | + if ( $gmt_offset == 0 ) { |
|
113 | + $tzstring = 'UTC'; |
|
114 | + } else { |
|
115 | + $gmt_offset *= HOUR_IN_SECONDS; |
|
116 | + $tzstring = timezone_name_from_abbr( '', $gmt_offset, 1 ); |
|
117 | + |
|
118 | + // If there's no timezone string, try again with no DST. |
|
119 | + if ( false === $tzstring ) { |
|
120 | + $tzstring = timezone_name_from_abbr( '', $gmt_offset, 0 ); |
|
121 | + } |
|
122 | + |
|
123 | + // Try mapping to the first abbreviation we can find. |
|
124 | + if ( false === $tzstring ) { |
|
125 | + $is_dst = date( 'I' ); |
|
126 | + foreach ( timezone_abbreviations_list() as $abbr ) { |
|
127 | + foreach ( $abbr as $city ) { |
|
128 | + if ( $city['dst'] == $is_dst && $city['offset'] == $gmt_offset ) { |
|
129 | + // If there's no valid timezone ID, keep looking. |
|
130 | + if ( null === $city['timezone_id'] ) { |
|
131 | + continue; |
|
132 | + } |
|
133 | + |
|
134 | + $tzstring = $city['timezone_id']; |
|
135 | + break 2; |
|
136 | + } |
|
137 | + } |
|
138 | + } |
|
139 | + } |
|
140 | + |
|
141 | + // If we still have no valid string, then fall back to UTC. |
|
142 | + if ( false === $tzstring ) { |
|
143 | + $tzstring = 'UTC'; |
|
144 | + } |
|
145 | + } |
|
146 | + } |
|
147 | + |
|
148 | + self::$local_timezone = new DateTimeZone( $tzstring ); |
|
149 | + } |
|
150 | + return self::$local_timezone; |
|
151 | + } |
|
152 | 152 | } |
@@ -15,17 +15,17 @@ discard block |
||
15 | 15 | * @param DateTime $date |
16 | 16 | * @return ActionScheduler_DateTime |
17 | 17 | */ |
18 | - public static function set_local_timezone( DateTime $date ) { |
|
18 | + public static function set_local_timezone(DateTime $date) { |
|
19 | 19 | |
20 | 20 | // Accept a DateTime for easier backward compatibility, even though we require methods on ActionScheduler_DateTime |
21 | - if ( ! is_a( $date, 'ActionScheduler_DateTime' ) ) { |
|
22 | - $date = as_get_datetime_object( $date->format( 'U' ) ); |
|
21 | + if ( ! is_a($date, 'ActionScheduler_DateTime')) { |
|
22 | + $date = as_get_datetime_object($date->format('U')); |
|
23 | 23 | } |
24 | 24 | |
25 | - if ( get_option( 'timezone_string' ) ) { |
|
26 | - $date->setTimezone( new DateTimeZone( self::get_local_timezone_string() ) ); |
|
25 | + if (get_option('timezone_string')) { |
|
26 | + $date->setTimezone(new DateTimeZone(self::get_local_timezone_string())); |
|
27 | 27 | } else { |
28 | - $date->setUtcOffset( self::get_local_timezone_offset() ); |
|
28 | + $date->setUtcOffset(self::get_local_timezone_offset()); |
|
29 | 29 | } |
30 | 30 | |
31 | 31 | return $date; |
@@ -44,16 +44,16 @@ discard block |
||
44 | 44 | * @since 2.1.0 |
45 | 45 | * @return string PHP timezone string for the site or empty if no timezone string is available. |
46 | 46 | */ |
47 | - protected static function get_local_timezone_string( $reset = false ) { |
|
47 | + protected static function get_local_timezone_string($reset = false) { |
|
48 | 48 | // If site timezone string exists, return it. |
49 | - $timezone = get_option( 'timezone_string' ); |
|
50 | - if ( $timezone ) { |
|
49 | + $timezone = get_option('timezone_string'); |
|
50 | + if ($timezone) { |
|
51 | 51 | return $timezone; |
52 | 52 | } |
53 | 53 | |
54 | 54 | // Get UTC offset, if it isn't set then return UTC. |
55 | - $utc_offset = intval( get_option( 'gmt_offset', 0 ) ); |
|
56 | - if ( 0 === $utc_offset ) { |
|
55 | + $utc_offset = intval(get_option('gmt_offset', 0)); |
|
56 | + if (0 === $utc_offset) { |
|
57 | 57 | return 'UTC'; |
58 | 58 | } |
59 | 59 | |
@@ -61,15 +61,15 @@ discard block |
||
61 | 61 | $utc_offset *= 3600; |
62 | 62 | |
63 | 63 | // Attempt to guess the timezone string from the UTC offset. |
64 | - $timezone = timezone_name_from_abbr( '', $utc_offset ); |
|
65 | - if ( $timezone ) { |
|
64 | + $timezone = timezone_name_from_abbr('', $utc_offset); |
|
65 | + if ($timezone) { |
|
66 | 66 | return $timezone; |
67 | 67 | } |
68 | 68 | |
69 | 69 | // Last try, guess timezone string manually. |
70 | - foreach ( timezone_abbreviations_list() as $abbr ) { |
|
71 | - foreach ( $abbr as $city ) { |
|
72 | - if ( (bool) date( 'I' ) === (bool) $city['dst'] && $city['timezone_id'] && intval( $city['offset'] ) === $utc_offset ) { |
|
70 | + foreach (timezone_abbreviations_list() as $abbr) { |
|
71 | + foreach ($abbr as $city) { |
|
72 | + if ((bool) date('I') === (bool) $city['dst'] && $city['timezone_id'] && intval($city['offset']) === $utc_offset) { |
|
73 | 73 | return $city['timezone_id']; |
74 | 74 | } |
75 | 75 | } |
@@ -86,48 +86,48 @@ discard block |
||
86 | 86 | * @return float |
87 | 87 | */ |
88 | 88 | protected static function get_local_timezone_offset() { |
89 | - $timezone = get_option( 'timezone_string' ); |
|
89 | + $timezone = get_option('timezone_string'); |
|
90 | 90 | |
91 | - if ( $timezone ) { |
|
92 | - $timezone_object = new DateTimeZone( $timezone ); |
|
93 | - return $timezone_object->getOffset( new DateTime( 'now' ) ); |
|
91 | + if ($timezone) { |
|
92 | + $timezone_object = new DateTimeZone($timezone); |
|
93 | + return $timezone_object->getOffset(new DateTime('now')); |
|
94 | 94 | } else { |
95 | - return floatval( get_option( 'gmt_offset', 0 ) ) * HOUR_IN_SECONDS; |
|
95 | + return floatval(get_option('gmt_offset', 0)) * HOUR_IN_SECONDS; |
|
96 | 96 | } |
97 | 97 | } |
98 | 98 | |
99 | 99 | /** |
100 | 100 | * @deprecated 2.1.0 |
101 | 101 | */ |
102 | - public static function get_local_timezone( $reset = false ) { |
|
103 | - _deprecated_function( __FUNCTION__, '2.1.0', 'ActionScheduler_TimezoneHelper::set_local_timezone()' ); |
|
104 | - if ( $reset ) { |
|
102 | + public static function get_local_timezone($reset = false) { |
|
103 | + _deprecated_function(__FUNCTION__, '2.1.0', 'ActionScheduler_TimezoneHelper::set_local_timezone()'); |
|
104 | + if ($reset) { |
|
105 | 105 | self::$local_timezone = null; |
106 | 106 | } |
107 | - if ( ! isset( self::$local_timezone ) ) { |
|
108 | - $tzstring = get_option( 'timezone_string' ); |
|
107 | + if ( ! isset(self::$local_timezone)) { |
|
108 | + $tzstring = get_option('timezone_string'); |
|
109 | 109 | |
110 | - if ( empty( $tzstring ) ) { |
|
111 | - $gmt_offset = get_option( 'gmt_offset' ); |
|
112 | - if ( $gmt_offset == 0 ) { |
|
110 | + if (empty($tzstring)) { |
|
111 | + $gmt_offset = get_option('gmt_offset'); |
|
112 | + if ($gmt_offset == 0) { |
|
113 | 113 | $tzstring = 'UTC'; |
114 | 114 | } else { |
115 | 115 | $gmt_offset *= HOUR_IN_SECONDS; |
116 | - $tzstring = timezone_name_from_abbr( '', $gmt_offset, 1 ); |
|
116 | + $tzstring = timezone_name_from_abbr('', $gmt_offset, 1); |
|
117 | 117 | |
118 | 118 | // If there's no timezone string, try again with no DST. |
119 | - if ( false === $tzstring ) { |
|
120 | - $tzstring = timezone_name_from_abbr( '', $gmt_offset, 0 ); |
|
119 | + if (false === $tzstring) { |
|
120 | + $tzstring = timezone_name_from_abbr('', $gmt_offset, 0); |
|
121 | 121 | } |
122 | 122 | |
123 | 123 | // Try mapping to the first abbreviation we can find. |
124 | - if ( false === $tzstring ) { |
|
125 | - $is_dst = date( 'I' ); |
|
126 | - foreach ( timezone_abbreviations_list() as $abbr ) { |
|
127 | - foreach ( $abbr as $city ) { |
|
128 | - if ( $city['dst'] == $is_dst && $city['offset'] == $gmt_offset ) { |
|
124 | + if (false === $tzstring) { |
|
125 | + $is_dst = date('I'); |
|
126 | + foreach (timezone_abbreviations_list() as $abbr) { |
|
127 | + foreach ($abbr as $city) { |
|
128 | + if ($city['dst'] == $is_dst && $city['offset'] == $gmt_offset) { |
|
129 | 129 | // If there's no valid timezone ID, keep looking. |
130 | - if ( null === $city['timezone_id'] ) { |
|
130 | + if (null === $city['timezone_id']) { |
|
131 | 131 | continue; |
132 | 132 | } |
133 | 133 | |
@@ -139,13 +139,13 @@ discard block |
||
139 | 139 | } |
140 | 140 | |
141 | 141 | // If we still have no valid string, then fall back to UTC. |
142 | - if ( false === $tzstring ) { |
|
142 | + if (false === $tzstring) { |
|
143 | 143 | $tzstring = 'UTC'; |
144 | 144 | } |
145 | 145 | } |
146 | 146 | } |
147 | 147 | |
148 | - self::$local_timezone = new DateTimeZone( $tzstring ); |
|
148 | + self::$local_timezone = new DateTimeZone($tzstring); |
|
149 | 149 | } |
150 | 150 | return self::$local_timezone; |
151 | 151 | } |
@@ -5,102 +5,102 @@ |
||
5 | 5 | */ |
6 | 6 | abstract class ActionScheduler_Abstract_RecurringSchedule extends ActionScheduler_Abstract_Schedule { |
7 | 7 | |
8 | - /** |
|
9 | - * The date & time the first instance of this schedule was setup to run (which may not be this instance). |
|
10 | - * |
|
11 | - * Schedule objects are attached to an action object. Each schedule stores the run date for that |
|
12 | - * object as the start date - @see $this->start - and logic to calculate the next run date after |
|
13 | - * that - @see $this->calculate_next(). The $first_date property also keeps a record of when the very |
|
14 | - * first instance of this chain of schedules ran. |
|
15 | - * |
|
16 | - * @var DateTime |
|
17 | - */ |
|
18 | - private $first_date = null; |
|
8 | + /** |
|
9 | + * The date & time the first instance of this schedule was setup to run (which may not be this instance). |
|
10 | + * |
|
11 | + * Schedule objects are attached to an action object. Each schedule stores the run date for that |
|
12 | + * object as the start date - @see $this->start - and logic to calculate the next run date after |
|
13 | + * that - @see $this->calculate_next(). The $first_date property also keeps a record of when the very |
|
14 | + * first instance of this chain of schedules ran. |
|
15 | + * |
|
16 | + * @var DateTime |
|
17 | + */ |
|
18 | + private $first_date = null; |
|
19 | 19 | |
20 | - /** |
|
21 | - * Timestamp equivalent of @see $this->first_date |
|
22 | - * |
|
23 | - * @var int |
|
24 | - */ |
|
25 | - protected $first_timestamp = null; |
|
20 | + /** |
|
21 | + * Timestamp equivalent of @see $this->first_date |
|
22 | + * |
|
23 | + * @var int |
|
24 | + */ |
|
25 | + protected $first_timestamp = null; |
|
26 | 26 | |
27 | - /** |
|
28 | - * The recurrance between each time an action is run using this schedule. |
|
29 | - * Used to calculate the start date & time. Can be a number of seconds, in the |
|
30 | - * case of ActionScheduler_IntervalSchedule, or a cron expression, as in the |
|
31 | - * case of ActionScheduler_CronSchedule. Or something else. |
|
32 | - * |
|
33 | - * @var mixed |
|
34 | - */ |
|
35 | - protected $recurrence; |
|
27 | + /** |
|
28 | + * The recurrance between each time an action is run using this schedule. |
|
29 | + * Used to calculate the start date & time. Can be a number of seconds, in the |
|
30 | + * case of ActionScheduler_IntervalSchedule, or a cron expression, as in the |
|
31 | + * case of ActionScheduler_CronSchedule. Or something else. |
|
32 | + * |
|
33 | + * @var mixed |
|
34 | + */ |
|
35 | + protected $recurrence; |
|
36 | 36 | |
37 | - /** |
|
38 | - * @param DateTime $date The date & time to run the action. |
|
39 | - * @param mixed $recurrence The data used to determine the schedule's recurrance. |
|
40 | - * @param DateTime|null $first (Optional) The date & time the first instance of this interval schedule ran. Default null, meaning this is the first instance. |
|
41 | - */ |
|
42 | - public function __construct( DateTime $date, $recurrence, DateTime $first = null ) { |
|
43 | - parent::__construct( $date ); |
|
44 | - $this->first_date = empty( $first ) ? $date : $first; |
|
45 | - $this->recurrence = $recurrence; |
|
46 | - } |
|
37 | + /** |
|
38 | + * @param DateTime $date The date & time to run the action. |
|
39 | + * @param mixed $recurrence The data used to determine the schedule's recurrance. |
|
40 | + * @param DateTime|null $first (Optional) The date & time the first instance of this interval schedule ran. Default null, meaning this is the first instance. |
|
41 | + */ |
|
42 | + public function __construct( DateTime $date, $recurrence, DateTime $first = null ) { |
|
43 | + parent::__construct( $date ); |
|
44 | + $this->first_date = empty( $first ) ? $date : $first; |
|
45 | + $this->recurrence = $recurrence; |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * @return bool |
|
50 | - */ |
|
51 | - public function is_recurring() { |
|
52 | - return true; |
|
53 | - } |
|
48 | + /** |
|
49 | + * @return bool |
|
50 | + */ |
|
51 | + public function is_recurring() { |
|
52 | + return true; |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * Get the date & time of the first schedule in this recurring series. |
|
57 | - * |
|
58 | - * @return DateTime|null |
|
59 | - */ |
|
60 | - public function get_first_date() { |
|
61 | - return clone $this->first_date; |
|
62 | - } |
|
55 | + /** |
|
56 | + * Get the date & time of the first schedule in this recurring series. |
|
57 | + * |
|
58 | + * @return DateTime|null |
|
59 | + */ |
|
60 | + public function get_first_date() { |
|
61 | + return clone $this->first_date; |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * @return string |
|
66 | - */ |
|
67 | - public function get_recurrence() { |
|
68 | - return $this->recurrence; |
|
69 | - } |
|
64 | + /** |
|
65 | + * @return string |
|
66 | + */ |
|
67 | + public function get_recurrence() { |
|
68 | + return $this->recurrence; |
|
69 | + } |
|
70 | 70 | |
71 | - /** |
|
72 | - * For PHP 5.2 compat, since DateTime objects can't be serialized |
|
73 | - * |
|
74 | - * @return array |
|
75 | - */ |
|
76 | - public function __sleep() { |
|
77 | - $sleep_params = parent::__sleep(); |
|
78 | - $this->first_timestamp = $this->first_date->getTimestamp(); |
|
79 | - return array_merge( |
|
80 | - $sleep_params, |
|
81 | - array( |
|
82 | - 'first_timestamp', |
|
83 | - 'recurrence', |
|
84 | - ) |
|
85 | - ); |
|
86 | - } |
|
71 | + /** |
|
72 | + * For PHP 5.2 compat, since DateTime objects can't be serialized |
|
73 | + * |
|
74 | + * @return array |
|
75 | + */ |
|
76 | + public function __sleep() { |
|
77 | + $sleep_params = parent::__sleep(); |
|
78 | + $this->first_timestamp = $this->first_date->getTimestamp(); |
|
79 | + return array_merge( |
|
80 | + $sleep_params, |
|
81 | + array( |
|
82 | + 'first_timestamp', |
|
83 | + 'recurrence', |
|
84 | + ) |
|
85 | + ); |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * Unserialize recurring schedules serialized/stored prior to AS 3.0.0 |
|
90 | - * |
|
91 | - * Prior to Action Scheduler 3.0.0, schedules used different property names to refer |
|
92 | - * to equivalent data. For example, ActionScheduler_IntervalSchedule::start_timestamp |
|
93 | - * was the same as ActionScheduler_SimpleSchedule::timestamp. This was addressed in |
|
94 | - * Action Scheduler 3.0.0, where properties and property names were aligned for better |
|
95 | - * inheritance. To maintain backward compatibility with scheduled serialized and stored |
|
96 | - * prior to 3.0, we need to correctly map the old property names. |
|
97 | - */ |
|
98 | - public function __wakeup() { |
|
99 | - parent::__wakeup(); |
|
100 | - if ( $this->first_timestamp > 0 ) { |
|
101 | - $this->first_date = as_get_datetime_object( $this->first_timestamp ); |
|
102 | - } else { |
|
103 | - $this->first_date = $this->get_date(); |
|
104 | - } |
|
105 | - } |
|
88 | + /** |
|
89 | + * Unserialize recurring schedules serialized/stored prior to AS 3.0.0 |
|
90 | + * |
|
91 | + * Prior to Action Scheduler 3.0.0, schedules used different property names to refer |
|
92 | + * to equivalent data. For example, ActionScheduler_IntervalSchedule::start_timestamp |
|
93 | + * was the same as ActionScheduler_SimpleSchedule::timestamp. This was addressed in |
|
94 | + * Action Scheduler 3.0.0, where properties and property names were aligned for better |
|
95 | + * inheritance. To maintain backward compatibility with scheduled serialized and stored |
|
96 | + * prior to 3.0, we need to correctly map the old property names. |
|
97 | + */ |
|
98 | + public function __wakeup() { |
|
99 | + parent::__wakeup(); |
|
100 | + if ( $this->first_timestamp > 0 ) { |
|
101 | + $this->first_date = as_get_datetime_object( $this->first_timestamp ); |
|
102 | + } else { |
|
103 | + $this->first_date = $this->get_date(); |
|
104 | + } |
|
105 | + } |
|
106 | 106 | } |
@@ -39,9 +39,9 @@ discard block |
||
39 | 39 | * @param mixed $recurrence The data used to determine the schedule's recurrance. |
40 | 40 | * @param DateTime|null $first (Optional) The date & time the first instance of this interval schedule ran. Default null, meaning this is the first instance. |
41 | 41 | */ |
42 | - public function __construct( DateTime $date, $recurrence, DateTime $first = null ) { |
|
43 | - parent::__construct( $date ); |
|
44 | - $this->first_date = empty( $first ) ? $date : $first; |
|
42 | + public function __construct(DateTime $date, $recurrence, DateTime $first = null) { |
|
43 | + parent::__construct($date); |
|
44 | + $this->first_date = empty($first) ? $date : $first; |
|
45 | 45 | $this->recurrence = $recurrence; |
46 | 46 | } |
47 | 47 | |
@@ -97,8 +97,8 @@ discard block |
||
97 | 97 | */ |
98 | 98 | public function __wakeup() { |
99 | 99 | parent::__wakeup(); |
100 | - if ( $this->first_timestamp > 0 ) { |
|
101 | - $this->first_date = as_get_datetime_object( $this->first_timestamp ); |
|
100 | + if ($this->first_timestamp > 0) { |
|
101 | + $this->first_date = as_get_datetime_object($this->first_timestamp); |
|
102 | 102 | } else { |
103 | 103 | $this->first_date = $this->get_date(); |
104 | 104 | } |
@@ -7,56 +7,56 @@ |
||
7 | 7 | */ |
8 | 8 | abstract class ActionScheduler_Lock { |
9 | 9 | |
10 | - /** @var ActionScheduler_Lock */ |
|
11 | - private static $locker = null; |
|
12 | - |
|
13 | - /** @var int */ |
|
14 | - protected static $lock_duration = MINUTE_IN_SECONDS; |
|
15 | - |
|
16 | - /** |
|
17 | - * Check if a lock is set for a given lock type. |
|
18 | - * |
|
19 | - * @param string $lock_type A string to identify different lock types. |
|
20 | - * @return bool |
|
21 | - */ |
|
22 | - public function is_locked( $lock_type ) { |
|
23 | - return ( $this->get_expiration( $lock_type ) >= time() ); |
|
24 | - } |
|
25 | - |
|
26 | - /** |
|
27 | - * Set a lock. |
|
28 | - * |
|
29 | - * @param string $lock_type A string to identify different lock types. |
|
30 | - * @return bool |
|
31 | - */ |
|
32 | - abstract public function set( $lock_type ); |
|
33 | - |
|
34 | - /** |
|
35 | - * If a lock is set, return the timestamp it was set to expiry. |
|
36 | - * |
|
37 | - * @param string $lock_type A string to identify different lock types. |
|
38 | - * @return bool|int False if no lock is set, otherwise the timestamp for when the lock is set to expire. |
|
39 | - */ |
|
40 | - abstract public function get_expiration( $lock_type ); |
|
41 | - |
|
42 | - /** |
|
43 | - * Get the amount of time to set for a given lock. 60 seconds by default. |
|
44 | - * |
|
45 | - * @param string $lock_type A string to identify different lock types. |
|
46 | - * @return int |
|
47 | - */ |
|
48 | - protected function get_duration( $lock_type ) { |
|
49 | - return apply_filters( 'action_scheduler_lock_duration', self::$lock_duration, $lock_type ); |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * @return ActionScheduler_Lock |
|
54 | - */ |
|
55 | - public static function instance() { |
|
56 | - if ( empty( self::$locker ) ) { |
|
57 | - $class = apply_filters( 'action_scheduler_lock_class', 'ActionScheduler_OptionLock' ); |
|
58 | - self::$locker = new $class(); |
|
59 | - } |
|
60 | - return self::$locker; |
|
61 | - } |
|
10 | + /** @var ActionScheduler_Lock */ |
|
11 | + private static $locker = null; |
|
12 | + |
|
13 | + /** @var int */ |
|
14 | + protected static $lock_duration = MINUTE_IN_SECONDS; |
|
15 | + |
|
16 | + /** |
|
17 | + * Check if a lock is set for a given lock type. |
|
18 | + * |
|
19 | + * @param string $lock_type A string to identify different lock types. |
|
20 | + * @return bool |
|
21 | + */ |
|
22 | + public function is_locked( $lock_type ) { |
|
23 | + return ( $this->get_expiration( $lock_type ) >= time() ); |
|
24 | + } |
|
25 | + |
|
26 | + /** |
|
27 | + * Set a lock. |
|
28 | + * |
|
29 | + * @param string $lock_type A string to identify different lock types. |
|
30 | + * @return bool |
|
31 | + */ |
|
32 | + abstract public function set( $lock_type ); |
|
33 | + |
|
34 | + /** |
|
35 | + * If a lock is set, return the timestamp it was set to expiry. |
|
36 | + * |
|
37 | + * @param string $lock_type A string to identify different lock types. |
|
38 | + * @return bool|int False if no lock is set, otherwise the timestamp for when the lock is set to expire. |
|
39 | + */ |
|
40 | + abstract public function get_expiration( $lock_type ); |
|
41 | + |
|
42 | + /** |
|
43 | + * Get the amount of time to set for a given lock. 60 seconds by default. |
|
44 | + * |
|
45 | + * @param string $lock_type A string to identify different lock types. |
|
46 | + * @return int |
|
47 | + */ |
|
48 | + protected function get_duration( $lock_type ) { |
|
49 | + return apply_filters( 'action_scheduler_lock_duration', self::$lock_duration, $lock_type ); |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * @return ActionScheduler_Lock |
|
54 | + */ |
|
55 | + public static function instance() { |
|
56 | + if ( empty( self::$locker ) ) { |
|
57 | + $class = apply_filters( 'action_scheduler_lock_class', 'ActionScheduler_OptionLock' ); |
|
58 | + self::$locker = new $class(); |
|
59 | + } |
|
60 | + return self::$locker; |
|
61 | + } |
|
62 | 62 | } |
@@ -19,8 +19,8 @@ discard block |
||
19 | 19 | * @param string $lock_type A string to identify different lock types. |
20 | 20 | * @return bool |
21 | 21 | */ |
22 | - public function is_locked( $lock_type ) { |
|
23 | - return ( $this->get_expiration( $lock_type ) >= time() ); |
|
22 | + public function is_locked($lock_type) { |
|
23 | + return ($this->get_expiration($lock_type) >= time()); |
|
24 | 24 | } |
25 | 25 | |
26 | 26 | /** |
@@ -29,7 +29,7 @@ discard block |
||
29 | 29 | * @param string $lock_type A string to identify different lock types. |
30 | 30 | * @return bool |
31 | 31 | */ |
32 | - abstract public function set( $lock_type ); |
|
32 | + abstract public function set($lock_type); |
|
33 | 33 | |
34 | 34 | /** |
35 | 35 | * If a lock is set, return the timestamp it was set to expiry. |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | * @param string $lock_type A string to identify different lock types. |
38 | 38 | * @return bool|int False if no lock is set, otherwise the timestamp for when the lock is set to expire. |
39 | 39 | */ |
40 | - abstract public function get_expiration( $lock_type ); |
|
40 | + abstract public function get_expiration($lock_type); |
|
41 | 41 | |
42 | 42 | /** |
43 | 43 | * Get the amount of time to set for a given lock. 60 seconds by default. |
@@ -45,16 +45,16 @@ discard block |
||
45 | 45 | * @param string $lock_type A string to identify different lock types. |
46 | 46 | * @return int |
47 | 47 | */ |
48 | - protected function get_duration( $lock_type ) { |
|
49 | - return apply_filters( 'action_scheduler_lock_duration', self::$lock_duration, $lock_type ); |
|
48 | + protected function get_duration($lock_type) { |
|
49 | + return apply_filters('action_scheduler_lock_duration', self::$lock_duration, $lock_type); |
|
50 | 50 | } |
51 | 51 | |
52 | 52 | /** |
53 | 53 | * @return ActionScheduler_Lock |
54 | 54 | */ |
55 | 55 | public static function instance() { |
56 | - if ( empty( self::$locker ) ) { |
|
57 | - $class = apply_filters( 'action_scheduler_lock_class', 'ActionScheduler_OptionLock' ); |
|
56 | + if (empty(self::$locker)) { |
|
57 | + $class = apply_filters('action_scheduler_lock_class', 'ActionScheduler_OptionLock'); |
|
58 | 58 | self::$locker = new $class(); |
59 | 59 | } |
60 | 60 | return self::$locker; |
@@ -6,171 +6,171 @@ |
||
6 | 6 | * @codeCoverageIgnore |
7 | 7 | */ |
8 | 8 | abstract class ActionScheduler_Logger { |
9 | - private static $logger = null; |
|
10 | - |
|
11 | - /** |
|
12 | - * @return ActionScheduler_Logger |
|
13 | - */ |
|
14 | - public static function instance() { |
|
15 | - if ( empty( self::$logger ) ) { |
|
16 | - $class = apply_filters( 'action_scheduler_logger_class', 'ActionScheduler_wpCommentLogger' ); |
|
17 | - self::$logger = new $class(); |
|
18 | - } |
|
19 | - return self::$logger; |
|
20 | - } |
|
21 | - |
|
22 | - /** |
|
23 | - * @param string $action_id |
|
24 | - * @param string $message |
|
25 | - * @param DateTime $date |
|
26 | - * |
|
27 | - * @return string The log entry ID |
|
28 | - */ |
|
29 | - abstract public function log( $action_id, $message, DateTime $date = null ); |
|
30 | - |
|
31 | - /** |
|
32 | - * @param string $entry_id |
|
33 | - * |
|
34 | - * @return ActionScheduler_LogEntry |
|
35 | - */ |
|
36 | - abstract public function get_entry( $entry_id ); |
|
37 | - |
|
38 | - /** |
|
39 | - * @param string $action_id |
|
40 | - * |
|
41 | - * @return ActionScheduler_LogEntry[] |
|
42 | - */ |
|
43 | - abstract public function get_logs( $action_id ); |
|
44 | - |
|
45 | - /** |
|
46 | - * @codeCoverageIgnore |
|
47 | - */ |
|
48 | - public function init() { |
|
49 | - $this->hook_stored_action(); |
|
50 | - add_action( 'action_scheduler_canceled_action', array( $this, 'log_canceled_action' ), 10, 1 ); |
|
51 | - add_action( 'action_scheduler_begin_execute', array( $this, 'log_started_action' ), 10, 2 ); |
|
52 | - add_action( 'action_scheduler_after_execute', array( $this, 'log_completed_action' ), 10, 3 ); |
|
53 | - add_action( 'action_scheduler_failed_execution', array( $this, 'log_failed_action' ), 10, 3 ); |
|
54 | - add_action( 'action_scheduler_failed_action', array( $this, 'log_timed_out_action' ), 10, 2 ); |
|
55 | - add_action( 'action_scheduler_unexpected_shutdown', array( $this, 'log_unexpected_shutdown' ), 10, 2 ); |
|
56 | - add_action( 'action_scheduler_reset_action', array( $this, 'log_reset_action' ), 10, 1 ); |
|
57 | - add_action( 'action_scheduler_execution_ignored', array( $this, 'log_ignored_action' ), 10, 2 ); |
|
58 | - add_action( 'action_scheduler_failed_fetch_action', array( $this, 'log_failed_fetch_action' ), 10, 2 ); |
|
59 | - add_action( 'action_scheduler_failed_to_schedule_next_instance', array( $this, 'log_failed_schedule_next_instance' ), 10, 2 ); |
|
60 | - add_action( 'action_scheduler_bulk_cancel_actions', array( $this, 'bulk_log_cancel_actions' ), 10, 1 ); |
|
61 | - } |
|
62 | - |
|
63 | - public function hook_stored_action() { |
|
64 | - add_action( 'action_scheduler_stored_action', array( $this, 'log_stored_action' ) ); |
|
65 | - } |
|
66 | - |
|
67 | - public function unhook_stored_action() { |
|
68 | - remove_action( 'action_scheduler_stored_action', array( $this, 'log_stored_action' ) ); |
|
69 | - } |
|
70 | - |
|
71 | - public function log_stored_action( $action_id ) { |
|
72 | - $this->log( $action_id, __( 'action created', 'action-scheduler' ) ); |
|
73 | - } |
|
74 | - |
|
75 | - public function log_canceled_action( $action_id ) { |
|
76 | - $this->log( $action_id, __( 'action canceled', 'action-scheduler' ) ); |
|
77 | - } |
|
78 | - |
|
79 | - public function log_started_action( $action_id, $context = '' ) { |
|
80 | - if ( ! empty( $context ) ) { |
|
81 | - /* translators: %s: context */ |
|
82 | - $message = sprintf( __( 'action started via %s', 'action-scheduler' ), $context ); |
|
83 | - } else { |
|
84 | - $message = __( 'action started', 'action-scheduler' ); |
|
85 | - } |
|
86 | - $this->log( $action_id, $message ); |
|
87 | - } |
|
88 | - |
|
89 | - public function log_completed_action( $action_id, $action = null, $context = '' ) { |
|
90 | - if ( ! empty( $context ) ) { |
|
91 | - /* translators: %s: context */ |
|
92 | - $message = sprintf( __( 'action complete via %s', 'action-scheduler' ), $context ); |
|
93 | - } else { |
|
94 | - $message = __( 'action complete', 'action-scheduler' ); |
|
95 | - } |
|
96 | - $this->log( $action_id, $message ); |
|
97 | - } |
|
98 | - |
|
99 | - public function log_failed_action( $action_id, Exception $exception, $context = '' ) { |
|
100 | - if ( ! empty( $context ) ) { |
|
101 | - /* translators: 1: context 2: exception message */ |
|
102 | - $message = sprintf( __( 'action failed via %1$s: %2$s', 'action-scheduler' ), $context, $exception->getMessage() ); |
|
103 | - } else { |
|
104 | - /* translators: %s: exception message */ |
|
105 | - $message = sprintf( __( 'action failed: %s', 'action-scheduler' ), $exception->getMessage() ); |
|
106 | - } |
|
107 | - $this->log( $action_id, $message ); |
|
108 | - } |
|
109 | - |
|
110 | - public function log_timed_out_action( $action_id, $timeout ) { |
|
111 | - /* translators: %s: amount of time */ |
|
112 | - $this->log( $action_id, sprintf( __( 'action marked as failed after %s seconds. Unknown error occurred. Check server, PHP and database error logs to diagnose cause.', 'action-scheduler' ), $timeout ) ); |
|
113 | - } |
|
114 | - |
|
115 | - public function log_unexpected_shutdown( $action_id, $error ) { |
|
116 | - if ( ! empty( $error ) ) { |
|
117 | - /* translators: 1: error message 2: filename 3: line */ |
|
118 | - $this->log( $action_id, sprintf( __( 'unexpected shutdown: PHP Fatal error %1$s in %2$s on line %3$s', 'action-scheduler' ), $error['message'], $error['file'], $error['line'] ) ); |
|
119 | - } |
|
120 | - } |
|
121 | - |
|
122 | - public function log_reset_action( $action_id ) { |
|
123 | - $this->log( $action_id, __( 'action reset', 'action-scheduler' ) ); |
|
124 | - } |
|
125 | - |
|
126 | - public function log_ignored_action( $action_id, $context = '' ) { |
|
127 | - if ( ! empty( $context ) ) { |
|
128 | - /* translators: %s: context */ |
|
129 | - $message = sprintf( __( 'action ignored via %s', 'action-scheduler' ), $context ); |
|
130 | - } else { |
|
131 | - $message = __( 'action ignored', 'action-scheduler' ); |
|
132 | - } |
|
133 | - $this->log( $action_id, $message ); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * @param string $action_id |
|
138 | - * @param Exception|NULL $exception The exception which occured when fetching the action. NULL by default for backward compatibility. |
|
139 | - * |
|
140 | - * @return ActionScheduler_LogEntry[] |
|
141 | - */ |
|
142 | - public function log_failed_fetch_action( $action_id, Exception $exception = null ) { |
|
143 | - |
|
144 | - if ( $exception !== null ) { |
|
145 | - /* translators: %s: exception message */ |
|
146 | - $log_message = sprintf( __( 'There was a failure fetching this action: %s', 'action-scheduler' ), $exception->getMessage() ); |
|
147 | - } else { |
|
148 | - $log_message = __( 'There was a failure fetching this action', 'action-scheduler' ); |
|
149 | - } |
|
150 | - |
|
151 | - $this->log( $action_id, $log_message ); |
|
152 | - } |
|
153 | - |
|
154 | - public function log_failed_schedule_next_instance( $action_id, Exception $exception ) { |
|
155 | - /* translators: %s: exception message */ |
|
156 | - $this->log( $action_id, sprintf( __( 'There was a failure scheduling the next instance of this action: %s', 'action-scheduler' ), $exception->getMessage() ) ); |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * Bulk add cancel action log entries. |
|
161 | - * |
|
162 | - * Implemented here for backward compatibility. Should be implemented in parent loggers |
|
163 | - * for more performant bulk logging. |
|
164 | - * |
|
165 | - * @param array $action_ids List of action ID. |
|
166 | - */ |
|
167 | - public function bulk_log_cancel_actions( $action_ids ) { |
|
168 | - if ( empty( $action_ids ) ) { |
|
169 | - return; |
|
170 | - } |
|
171 | - |
|
172 | - foreach ( $action_ids as $action_id ) { |
|
173 | - $this->log_canceled_action( $action_id ); |
|
174 | - } |
|
175 | - } |
|
9 | + private static $logger = null; |
|
10 | + |
|
11 | + /** |
|
12 | + * @return ActionScheduler_Logger |
|
13 | + */ |
|
14 | + public static function instance() { |
|
15 | + if ( empty( self::$logger ) ) { |
|
16 | + $class = apply_filters( 'action_scheduler_logger_class', 'ActionScheduler_wpCommentLogger' ); |
|
17 | + self::$logger = new $class(); |
|
18 | + } |
|
19 | + return self::$logger; |
|
20 | + } |
|
21 | + |
|
22 | + /** |
|
23 | + * @param string $action_id |
|
24 | + * @param string $message |
|
25 | + * @param DateTime $date |
|
26 | + * |
|
27 | + * @return string The log entry ID |
|
28 | + */ |
|
29 | + abstract public function log( $action_id, $message, DateTime $date = null ); |
|
30 | + |
|
31 | + /** |
|
32 | + * @param string $entry_id |
|
33 | + * |
|
34 | + * @return ActionScheduler_LogEntry |
|
35 | + */ |
|
36 | + abstract public function get_entry( $entry_id ); |
|
37 | + |
|
38 | + /** |
|
39 | + * @param string $action_id |
|
40 | + * |
|
41 | + * @return ActionScheduler_LogEntry[] |
|
42 | + */ |
|
43 | + abstract public function get_logs( $action_id ); |
|
44 | + |
|
45 | + /** |
|
46 | + * @codeCoverageIgnore |
|
47 | + */ |
|
48 | + public function init() { |
|
49 | + $this->hook_stored_action(); |
|
50 | + add_action( 'action_scheduler_canceled_action', array( $this, 'log_canceled_action' ), 10, 1 ); |
|
51 | + add_action( 'action_scheduler_begin_execute', array( $this, 'log_started_action' ), 10, 2 ); |
|
52 | + add_action( 'action_scheduler_after_execute', array( $this, 'log_completed_action' ), 10, 3 ); |
|
53 | + add_action( 'action_scheduler_failed_execution', array( $this, 'log_failed_action' ), 10, 3 ); |
|
54 | + add_action( 'action_scheduler_failed_action', array( $this, 'log_timed_out_action' ), 10, 2 ); |
|
55 | + add_action( 'action_scheduler_unexpected_shutdown', array( $this, 'log_unexpected_shutdown' ), 10, 2 ); |
|
56 | + add_action( 'action_scheduler_reset_action', array( $this, 'log_reset_action' ), 10, 1 ); |
|
57 | + add_action( 'action_scheduler_execution_ignored', array( $this, 'log_ignored_action' ), 10, 2 ); |
|
58 | + add_action( 'action_scheduler_failed_fetch_action', array( $this, 'log_failed_fetch_action' ), 10, 2 ); |
|
59 | + add_action( 'action_scheduler_failed_to_schedule_next_instance', array( $this, 'log_failed_schedule_next_instance' ), 10, 2 ); |
|
60 | + add_action( 'action_scheduler_bulk_cancel_actions', array( $this, 'bulk_log_cancel_actions' ), 10, 1 ); |
|
61 | + } |
|
62 | + |
|
63 | + public function hook_stored_action() { |
|
64 | + add_action( 'action_scheduler_stored_action', array( $this, 'log_stored_action' ) ); |
|
65 | + } |
|
66 | + |
|
67 | + public function unhook_stored_action() { |
|
68 | + remove_action( 'action_scheduler_stored_action', array( $this, 'log_stored_action' ) ); |
|
69 | + } |
|
70 | + |
|
71 | + public function log_stored_action( $action_id ) { |
|
72 | + $this->log( $action_id, __( 'action created', 'action-scheduler' ) ); |
|
73 | + } |
|
74 | + |
|
75 | + public function log_canceled_action( $action_id ) { |
|
76 | + $this->log( $action_id, __( 'action canceled', 'action-scheduler' ) ); |
|
77 | + } |
|
78 | + |
|
79 | + public function log_started_action( $action_id, $context = '' ) { |
|
80 | + if ( ! empty( $context ) ) { |
|
81 | + /* translators: %s: context */ |
|
82 | + $message = sprintf( __( 'action started via %s', 'action-scheduler' ), $context ); |
|
83 | + } else { |
|
84 | + $message = __( 'action started', 'action-scheduler' ); |
|
85 | + } |
|
86 | + $this->log( $action_id, $message ); |
|
87 | + } |
|
88 | + |
|
89 | + public function log_completed_action( $action_id, $action = null, $context = '' ) { |
|
90 | + if ( ! empty( $context ) ) { |
|
91 | + /* translators: %s: context */ |
|
92 | + $message = sprintf( __( 'action complete via %s', 'action-scheduler' ), $context ); |
|
93 | + } else { |
|
94 | + $message = __( 'action complete', 'action-scheduler' ); |
|
95 | + } |
|
96 | + $this->log( $action_id, $message ); |
|
97 | + } |
|
98 | + |
|
99 | + public function log_failed_action( $action_id, Exception $exception, $context = '' ) { |
|
100 | + if ( ! empty( $context ) ) { |
|
101 | + /* translators: 1: context 2: exception message */ |
|
102 | + $message = sprintf( __( 'action failed via %1$s: %2$s', 'action-scheduler' ), $context, $exception->getMessage() ); |
|
103 | + } else { |
|
104 | + /* translators: %s: exception message */ |
|
105 | + $message = sprintf( __( 'action failed: %s', 'action-scheduler' ), $exception->getMessage() ); |
|
106 | + } |
|
107 | + $this->log( $action_id, $message ); |
|
108 | + } |
|
109 | + |
|
110 | + public function log_timed_out_action( $action_id, $timeout ) { |
|
111 | + /* translators: %s: amount of time */ |
|
112 | + $this->log( $action_id, sprintf( __( 'action marked as failed after %s seconds. Unknown error occurred. Check server, PHP and database error logs to diagnose cause.', 'action-scheduler' ), $timeout ) ); |
|
113 | + } |
|
114 | + |
|
115 | + public function log_unexpected_shutdown( $action_id, $error ) { |
|
116 | + if ( ! empty( $error ) ) { |
|
117 | + /* translators: 1: error message 2: filename 3: line */ |
|
118 | + $this->log( $action_id, sprintf( __( 'unexpected shutdown: PHP Fatal error %1$s in %2$s on line %3$s', 'action-scheduler' ), $error['message'], $error['file'], $error['line'] ) ); |
|
119 | + } |
|
120 | + } |
|
121 | + |
|
122 | + public function log_reset_action( $action_id ) { |
|
123 | + $this->log( $action_id, __( 'action reset', 'action-scheduler' ) ); |
|
124 | + } |
|
125 | + |
|
126 | + public function log_ignored_action( $action_id, $context = '' ) { |
|
127 | + if ( ! empty( $context ) ) { |
|
128 | + /* translators: %s: context */ |
|
129 | + $message = sprintf( __( 'action ignored via %s', 'action-scheduler' ), $context ); |
|
130 | + } else { |
|
131 | + $message = __( 'action ignored', 'action-scheduler' ); |
|
132 | + } |
|
133 | + $this->log( $action_id, $message ); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * @param string $action_id |
|
138 | + * @param Exception|NULL $exception The exception which occured when fetching the action. NULL by default for backward compatibility. |
|
139 | + * |
|
140 | + * @return ActionScheduler_LogEntry[] |
|
141 | + */ |
|
142 | + public function log_failed_fetch_action( $action_id, Exception $exception = null ) { |
|
143 | + |
|
144 | + if ( $exception !== null ) { |
|
145 | + /* translators: %s: exception message */ |
|
146 | + $log_message = sprintf( __( 'There was a failure fetching this action: %s', 'action-scheduler' ), $exception->getMessage() ); |
|
147 | + } else { |
|
148 | + $log_message = __( 'There was a failure fetching this action', 'action-scheduler' ); |
|
149 | + } |
|
150 | + |
|
151 | + $this->log( $action_id, $log_message ); |
|
152 | + } |
|
153 | + |
|
154 | + public function log_failed_schedule_next_instance( $action_id, Exception $exception ) { |
|
155 | + /* translators: %s: exception message */ |
|
156 | + $this->log( $action_id, sprintf( __( 'There was a failure scheduling the next instance of this action: %s', 'action-scheduler' ), $exception->getMessage() ) ); |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * Bulk add cancel action log entries. |
|
161 | + * |
|
162 | + * Implemented here for backward compatibility. Should be implemented in parent loggers |
|
163 | + * for more performant bulk logging. |
|
164 | + * |
|
165 | + * @param array $action_ids List of action ID. |
|
166 | + */ |
|
167 | + public function bulk_log_cancel_actions( $action_ids ) { |
|
168 | + if ( empty( $action_ids ) ) { |
|
169 | + return; |
|
170 | + } |
|
171 | + |
|
172 | + foreach ( $action_ids as $action_id ) { |
|
173 | + $this->log_canceled_action( $action_id ); |
|
174 | + } |
|
175 | + } |
|
176 | 176 | } |
@@ -12,8 +12,8 @@ discard block |
||
12 | 12 | * @return ActionScheduler_Logger |
13 | 13 | */ |
14 | 14 | public static function instance() { |
15 | - if ( empty( self::$logger ) ) { |
|
16 | - $class = apply_filters( 'action_scheduler_logger_class', 'ActionScheduler_wpCommentLogger' ); |
|
15 | + if (empty(self::$logger)) { |
|
16 | + $class = apply_filters('action_scheduler_logger_class', 'ActionScheduler_wpCommentLogger'); |
|
17 | 17 | self::$logger = new $class(); |
18 | 18 | } |
19 | 19 | return self::$logger; |
@@ -26,111 +26,111 @@ discard block |
||
26 | 26 | * |
27 | 27 | * @return string The log entry ID |
28 | 28 | */ |
29 | - abstract public function log( $action_id, $message, DateTime $date = null ); |
|
29 | + abstract public function log($action_id, $message, DateTime $date = null); |
|
30 | 30 | |
31 | 31 | /** |
32 | 32 | * @param string $entry_id |
33 | 33 | * |
34 | 34 | * @return ActionScheduler_LogEntry |
35 | 35 | */ |
36 | - abstract public function get_entry( $entry_id ); |
|
36 | + abstract public function get_entry($entry_id); |
|
37 | 37 | |
38 | 38 | /** |
39 | 39 | * @param string $action_id |
40 | 40 | * |
41 | 41 | * @return ActionScheduler_LogEntry[] |
42 | 42 | */ |
43 | - abstract public function get_logs( $action_id ); |
|
43 | + abstract public function get_logs($action_id); |
|
44 | 44 | |
45 | 45 | /** |
46 | 46 | * @codeCoverageIgnore |
47 | 47 | */ |
48 | 48 | public function init() { |
49 | 49 | $this->hook_stored_action(); |
50 | - add_action( 'action_scheduler_canceled_action', array( $this, 'log_canceled_action' ), 10, 1 ); |
|
51 | - add_action( 'action_scheduler_begin_execute', array( $this, 'log_started_action' ), 10, 2 ); |
|
52 | - add_action( 'action_scheduler_after_execute', array( $this, 'log_completed_action' ), 10, 3 ); |
|
53 | - add_action( 'action_scheduler_failed_execution', array( $this, 'log_failed_action' ), 10, 3 ); |
|
54 | - add_action( 'action_scheduler_failed_action', array( $this, 'log_timed_out_action' ), 10, 2 ); |
|
55 | - add_action( 'action_scheduler_unexpected_shutdown', array( $this, 'log_unexpected_shutdown' ), 10, 2 ); |
|
56 | - add_action( 'action_scheduler_reset_action', array( $this, 'log_reset_action' ), 10, 1 ); |
|
57 | - add_action( 'action_scheduler_execution_ignored', array( $this, 'log_ignored_action' ), 10, 2 ); |
|
58 | - add_action( 'action_scheduler_failed_fetch_action', array( $this, 'log_failed_fetch_action' ), 10, 2 ); |
|
59 | - add_action( 'action_scheduler_failed_to_schedule_next_instance', array( $this, 'log_failed_schedule_next_instance' ), 10, 2 ); |
|
60 | - add_action( 'action_scheduler_bulk_cancel_actions', array( $this, 'bulk_log_cancel_actions' ), 10, 1 ); |
|
50 | + add_action('action_scheduler_canceled_action', array($this, 'log_canceled_action'), 10, 1); |
|
51 | + add_action('action_scheduler_begin_execute', array($this, 'log_started_action'), 10, 2); |
|
52 | + add_action('action_scheduler_after_execute', array($this, 'log_completed_action'), 10, 3); |
|
53 | + add_action('action_scheduler_failed_execution', array($this, 'log_failed_action'), 10, 3); |
|
54 | + add_action('action_scheduler_failed_action', array($this, 'log_timed_out_action'), 10, 2); |
|
55 | + add_action('action_scheduler_unexpected_shutdown', array($this, 'log_unexpected_shutdown'), 10, 2); |
|
56 | + add_action('action_scheduler_reset_action', array($this, 'log_reset_action'), 10, 1); |
|
57 | + add_action('action_scheduler_execution_ignored', array($this, 'log_ignored_action'), 10, 2); |
|
58 | + add_action('action_scheduler_failed_fetch_action', array($this, 'log_failed_fetch_action'), 10, 2); |
|
59 | + add_action('action_scheduler_failed_to_schedule_next_instance', array($this, 'log_failed_schedule_next_instance'), 10, 2); |
|
60 | + add_action('action_scheduler_bulk_cancel_actions', array($this, 'bulk_log_cancel_actions'), 10, 1); |
|
61 | 61 | } |
62 | 62 | |
63 | 63 | public function hook_stored_action() { |
64 | - add_action( 'action_scheduler_stored_action', array( $this, 'log_stored_action' ) ); |
|
64 | + add_action('action_scheduler_stored_action', array($this, 'log_stored_action')); |
|
65 | 65 | } |
66 | 66 | |
67 | 67 | public function unhook_stored_action() { |
68 | - remove_action( 'action_scheduler_stored_action', array( $this, 'log_stored_action' ) ); |
|
68 | + remove_action('action_scheduler_stored_action', array($this, 'log_stored_action')); |
|
69 | 69 | } |
70 | 70 | |
71 | - public function log_stored_action( $action_id ) { |
|
72 | - $this->log( $action_id, __( 'action created', 'action-scheduler' ) ); |
|
71 | + public function log_stored_action($action_id) { |
|
72 | + $this->log($action_id, __('action created', 'action-scheduler')); |
|
73 | 73 | } |
74 | 74 | |
75 | - public function log_canceled_action( $action_id ) { |
|
76 | - $this->log( $action_id, __( 'action canceled', 'action-scheduler' ) ); |
|
75 | + public function log_canceled_action($action_id) { |
|
76 | + $this->log($action_id, __('action canceled', 'action-scheduler')); |
|
77 | 77 | } |
78 | 78 | |
79 | - public function log_started_action( $action_id, $context = '' ) { |
|
80 | - if ( ! empty( $context ) ) { |
|
79 | + public function log_started_action($action_id, $context = '') { |
|
80 | + if ( ! empty($context)) { |
|
81 | 81 | /* translators: %s: context */ |
82 | - $message = sprintf( __( 'action started via %s', 'action-scheduler' ), $context ); |
|
82 | + $message = sprintf(__('action started via %s', 'action-scheduler'), $context); |
|
83 | 83 | } else { |
84 | - $message = __( 'action started', 'action-scheduler' ); |
|
84 | + $message = __('action started', 'action-scheduler'); |
|
85 | 85 | } |
86 | - $this->log( $action_id, $message ); |
|
86 | + $this->log($action_id, $message); |
|
87 | 87 | } |
88 | 88 | |
89 | - public function log_completed_action( $action_id, $action = null, $context = '' ) { |
|
90 | - if ( ! empty( $context ) ) { |
|
89 | + public function log_completed_action($action_id, $action = null, $context = '') { |
|
90 | + if ( ! empty($context)) { |
|
91 | 91 | /* translators: %s: context */ |
92 | - $message = sprintf( __( 'action complete via %s', 'action-scheduler' ), $context ); |
|
92 | + $message = sprintf(__('action complete via %s', 'action-scheduler'), $context); |
|
93 | 93 | } else { |
94 | - $message = __( 'action complete', 'action-scheduler' ); |
|
94 | + $message = __('action complete', 'action-scheduler'); |
|
95 | 95 | } |
96 | - $this->log( $action_id, $message ); |
|
96 | + $this->log($action_id, $message); |
|
97 | 97 | } |
98 | 98 | |
99 | - public function log_failed_action( $action_id, Exception $exception, $context = '' ) { |
|
100 | - if ( ! empty( $context ) ) { |
|
99 | + public function log_failed_action($action_id, Exception $exception, $context = '') { |
|
100 | + if ( ! empty($context)) { |
|
101 | 101 | /* translators: 1: context 2: exception message */ |
102 | - $message = sprintf( __( 'action failed via %1$s: %2$s', 'action-scheduler' ), $context, $exception->getMessage() ); |
|
102 | + $message = sprintf(__('action failed via %1$s: %2$s', 'action-scheduler'), $context, $exception->getMessage()); |
|
103 | 103 | } else { |
104 | 104 | /* translators: %s: exception message */ |
105 | - $message = sprintf( __( 'action failed: %s', 'action-scheduler' ), $exception->getMessage() ); |
|
105 | + $message = sprintf(__('action failed: %s', 'action-scheduler'), $exception->getMessage()); |
|
106 | 106 | } |
107 | - $this->log( $action_id, $message ); |
|
107 | + $this->log($action_id, $message); |
|
108 | 108 | } |
109 | 109 | |
110 | - public function log_timed_out_action( $action_id, $timeout ) { |
|
110 | + public function log_timed_out_action($action_id, $timeout) { |
|
111 | 111 | /* translators: %s: amount of time */ |
112 | - $this->log( $action_id, sprintf( __( 'action marked as failed after %s seconds. Unknown error occurred. Check server, PHP and database error logs to diagnose cause.', 'action-scheduler' ), $timeout ) ); |
|
112 | + $this->log($action_id, sprintf(__('action marked as failed after %s seconds. Unknown error occurred. Check server, PHP and database error logs to diagnose cause.', 'action-scheduler'), $timeout)); |
|
113 | 113 | } |
114 | 114 | |
115 | - public function log_unexpected_shutdown( $action_id, $error ) { |
|
116 | - if ( ! empty( $error ) ) { |
|
115 | + public function log_unexpected_shutdown($action_id, $error) { |
|
116 | + if ( ! empty($error)) { |
|
117 | 117 | /* translators: 1: error message 2: filename 3: line */ |
118 | - $this->log( $action_id, sprintf( __( 'unexpected shutdown: PHP Fatal error %1$s in %2$s on line %3$s', 'action-scheduler' ), $error['message'], $error['file'], $error['line'] ) ); |
|
118 | + $this->log($action_id, sprintf(__('unexpected shutdown: PHP Fatal error %1$s in %2$s on line %3$s', 'action-scheduler'), $error['message'], $error['file'], $error['line'])); |
|
119 | 119 | } |
120 | 120 | } |
121 | 121 | |
122 | - public function log_reset_action( $action_id ) { |
|
123 | - $this->log( $action_id, __( 'action reset', 'action-scheduler' ) ); |
|
122 | + public function log_reset_action($action_id) { |
|
123 | + $this->log($action_id, __('action reset', 'action-scheduler')); |
|
124 | 124 | } |
125 | 125 | |
126 | - public function log_ignored_action( $action_id, $context = '' ) { |
|
127 | - if ( ! empty( $context ) ) { |
|
126 | + public function log_ignored_action($action_id, $context = '') { |
|
127 | + if ( ! empty($context)) { |
|
128 | 128 | /* translators: %s: context */ |
129 | - $message = sprintf( __( 'action ignored via %s', 'action-scheduler' ), $context ); |
|
129 | + $message = sprintf(__('action ignored via %s', 'action-scheduler'), $context); |
|
130 | 130 | } else { |
131 | - $message = __( 'action ignored', 'action-scheduler' ); |
|
131 | + $message = __('action ignored', 'action-scheduler'); |
|
132 | 132 | } |
133 | - $this->log( $action_id, $message ); |
|
133 | + $this->log($action_id, $message); |
|
134 | 134 | } |
135 | 135 | |
136 | 136 | /** |
@@ -139,21 +139,21 @@ discard block |
||
139 | 139 | * |
140 | 140 | * @return ActionScheduler_LogEntry[] |
141 | 141 | */ |
142 | - public function log_failed_fetch_action( $action_id, Exception $exception = null ) { |
|
142 | + public function log_failed_fetch_action($action_id, Exception $exception = null) { |
|
143 | 143 | |
144 | - if ( $exception !== null ) { |
|
144 | + if ($exception !== null) { |
|
145 | 145 | /* translators: %s: exception message */ |
146 | - $log_message = sprintf( __( 'There was a failure fetching this action: %s', 'action-scheduler' ), $exception->getMessage() ); |
|
146 | + $log_message = sprintf(__('There was a failure fetching this action: %s', 'action-scheduler'), $exception->getMessage()); |
|
147 | 147 | } else { |
148 | - $log_message = __( 'There was a failure fetching this action', 'action-scheduler' ); |
|
148 | + $log_message = __('There was a failure fetching this action', 'action-scheduler'); |
|
149 | 149 | } |
150 | 150 | |
151 | - $this->log( $action_id, $log_message ); |
|
151 | + $this->log($action_id, $log_message); |
|
152 | 152 | } |
153 | 153 | |
154 | - public function log_failed_schedule_next_instance( $action_id, Exception $exception ) { |
|
154 | + public function log_failed_schedule_next_instance($action_id, Exception $exception) { |
|
155 | 155 | /* translators: %s: exception message */ |
156 | - $this->log( $action_id, sprintf( __( 'There was a failure scheduling the next instance of this action: %s', 'action-scheduler' ), $exception->getMessage() ) ); |
|
156 | + $this->log($action_id, sprintf(__('There was a failure scheduling the next instance of this action: %s', 'action-scheduler'), $exception->getMessage())); |
|
157 | 157 | } |
158 | 158 | |
159 | 159 | /** |
@@ -164,13 +164,13 @@ discard block |
||
164 | 164 | * |
165 | 165 | * @param array $action_ids List of action ID. |
166 | 166 | */ |
167 | - public function bulk_log_cancel_actions( $action_ids ) { |
|
168 | - if ( empty( $action_ids ) ) { |
|
167 | + public function bulk_log_cancel_actions($action_ids) { |
|
168 | + if (empty($action_ids)) { |
|
169 | 169 | return; |
170 | 170 | } |
171 | 171 | |
172 | - foreach ( $action_ids as $action_id ) { |
|
173 | - $this->log_canceled_action( $action_id ); |
|
172 | + foreach ($action_ids as $action_id) { |
|
173 | + $this->log_canceled_action($action_id); |
|
174 | 174 | } |
175 | 175 | } |
176 | 176 | } |