← All changes
|
libraries/action-scheduler/classes/ActionScheduler_AdminView.php
+166
-8
1.0.2
→
3.1.1
View file →
| @@ -1,20 +1,37 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | /** |
| 4 | 4 | * Class ActionScheduler_AdminView |
| 5 | + * | |
| 5 | 6 | * @codeCoverageIgnore |
| 6 | 7 | */ |
| 7 | 8 | class ActionScheduler_AdminView extends ActionScheduler_AdminView_Deprecated { |
| 8 | 9 | |
| 9 | - private static $admin_view = NULL; | |
| 10 | + /** | |
| 11 | + * Instance. | |
| 12 | + * | |
| 13 | + * @var null|self | |
| 14 | + */ | |
| 15 | + private static $admin_view = null; | |
| 10 | 16 | |
| 17 | + /** | |
| 18 | + * Screen ID. | |
| 19 | + * | |
| 20 | + * @var string | |
| 21 | + */ | |
| 11 | 22 | private static $screen_id = 'tools_page_action-scheduler'; |
| 12 | 23 | |
| 13 | - /** @var ActionScheduler_ListTable */ | |
| 24 | + /** | |
| 25 | + * ActionScheduler_ListTable instance. | |
| 26 | + * | |
| 27 | + * @var ActionScheduler_ListTable | |
| 28 | + */ | |
| 14 | 29 | protected $list_table; |
| 15 | 30 | |
| 16 | 31 | /** |
| 32 | + * Get instance. | |
| 33 | + * | |
| 17 | 34 | * @return ActionScheduler_AdminView |
| 18 | 35 | * @codeCoverageIgnore |
| 19 | 36 | */ |
| 20 | 37 | public static function instance() { |
| @@ -19,9 +36,9 @@ | ||
| 19 | 36 | */ |
| 20 | 37 | public static function instance() { |
| 21 | 38 | |
| 22 | 39 | if ( empty( self::$admin_view ) ) { |
| 23 | - $class = apply_filters('action_scheduler_admin_view_class', 'ActionScheduler_AdminView'); | |
| 40 | + $class = apply_filters( 'action_scheduler_admin_view_class', 'ActionScheduler_AdminView' ); | |
| 24 | 41 | self::$admin_view = new $class(); |
| 25 | 42 | } |
| 26 | 43 | |
| 27 | 44 | return self::$admin_view; |
| @@ -27,12 +44,14 @@ | ||
| 27 | 44 | return self::$admin_view; |
| 28 | 45 | } |
| 29 | 46 | |
| 30 | 47 | /** |
| 48 | + * Initialize. | |
| 49 | + * | |
| 31 | 50 | * @codeCoverageIgnore |
| 32 | 51 | */ |
| 33 | 52 | public function init() { |
| 34 | - if ( is_admin() && ( ! defined( 'DOING_AJAX' ) || false == DOING_AJAX ) ) { | |
| 53 | + if ( is_admin() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) { | |
| 35 | 54 | |
| 36 | 55 | if ( class_exists( 'WooCommerce' ) ) { |
| 37 | 56 | add_action( 'woocommerce_admin_status_content_action-scheduler', array( $this, 'render_admin_ui' ) ); |
| 38 | 57 | add_action( 'woocommerce_system_status_report', array( $this, 'system_status_report' ) ); |
| @@ -39,13 +58,16 @@ | ||
| 39 | 58 | add_filter( 'woocommerce_admin_status_tabs', array( $this, 'register_system_status_tab' ) ); |
| 40 | 59 | } |
| 41 | 60 | |
| 42 | 61 | add_action( 'admin_menu', array( $this, 'register_menu' ) ); |
| 43 | - | |
| 62 | + add_action( 'admin_notices', array( $this, 'maybe_check_pastdue_actions' ) ); | |
| 44 | 63 | add_action( 'current_screen', array( $this, 'add_help_tabs' ) ); |
| 45 | 64 | } |
| 46 | 65 | } |
| 47 | 66 | |
| 67 | + /** | |
| 68 | + * Print system status report. | |
| 69 | + */ | |
| 48 | 70 | public function system_status_report() { |
| 49 | 71 | $table = new ActionScheduler_wcSystemStatus( ActionScheduler::store() ); |
| 50 | 72 | $table->render(); |
| 51 | 73 | } |
| @@ -77,9 +99,9 @@ | ||
| 77 | 99 | 'manage_options', |
| 78 | 100 | 'action-scheduler', |
| 79 | 101 | array( $this, 'render_admin_ui' ) |
| 80 | 102 | ); |
| 81 | - add_action( 'load-' . $hook_suffix , array( $this, 'process_admin_ui' ) ); | |
| 103 | + add_action( 'load-' . $hook_suffix, array( $this, 'process_admin_ui' ) ); | |
| 82 | 104 | } |
| 83 | 105 | |
| 84 | 106 | /** |
| 85 | 107 | * Triggers processing of any pending actions. |
| @@ -110,26 +132,162 @@ | ||
| 110 | 132 | return $this->list_table; |
| 111 | 133 | } |
| 112 | 134 | |
| 113 | 135 | /** |
| 136 | + * Action: admin_notices | |
| 137 | + * | |
| 138 | + * Maybe check past-due actions, and print notice. | |
| 139 | + * | |
| 140 | + * @uses $this->check_pastdue_actions() | |
| 141 | + */ | |
| 142 | + public function maybe_check_pastdue_actions() { | |
| 143 | + | |
| 144 | + // Filter to prevent checking actions (ex: inappropriate user). | |
| 145 | + if ( ! apply_filters( 'action_scheduler_check_pastdue_actions', current_user_can( 'manage_options' ) ) ) { | |
| 146 | + return; | |
| 147 | + } | |
| 148 | + | |
| 149 | + // Get last check transient. | |
| 150 | + $last_check = get_transient( 'action_scheduler_last_pastdue_actions_check' ); | |
| 151 | + | |
| 152 | + // If transient exists, we're within interval, so bail. | |
| 153 | + if ( ! empty( $last_check ) ) { | |
| 154 | + return; | |
| 155 | + } | |
| 156 | + | |
| 157 | + // Perform the check. | |
| 158 | + $this->check_pastdue_actions(); | |
| 159 | + } | |
| 160 | + | |
| 161 | + /** | |
| 162 | + * Check past-due actions, and print notice. | |
| 163 | + */ | |
| 164 | + protected function check_pastdue_actions() { | |
| 165 | + | |
| 166 | + // Set thresholds. | |
| 167 | + $threshold_seconds = (int) apply_filters( 'action_scheduler_pastdue_actions_seconds', DAY_IN_SECONDS ); | |
| 168 | + $threshold_min = (int) apply_filters( 'action_scheduler_pastdue_actions_min', 1 ); | |
| 169 | + | |
| 170 | + // Set fallback value for past-due actions count. | |
| 171 | + $num_pastdue_actions = 0; | |
| 172 | + | |
| 173 | + // Allow third-parties to preempt the default check logic. | |
| 174 | + $check = apply_filters( 'action_scheduler_pastdue_actions_check_pre', null ); | |
| 175 | + | |
| 176 | + // If no third-party preempted and there are no past-due actions, return early. | |
| 177 | + if ( ! is_null( $check ) ) { | |
| 178 | + return; | |
| 179 | + } | |
| 180 | + | |
| 181 | + // Scheduled actions query arguments. | |
| 182 | + $query_args = array( | |
| 183 | + 'date' => as_get_datetime_object( time() - $threshold_seconds ), | |
| 184 | + 'status' => ActionScheduler_Store::STATUS_PENDING, | |
| 185 | + 'per_page' => $threshold_min, | |
| 186 | + ); | |
| 187 | + | |
| 188 | + // If no third-party preempted, run default check. | |
| 189 | + if ( is_null( $check ) ) { | |
| 190 | + $store = ActionScheduler_Store::instance(); | |
| 191 | + $num_pastdue_actions = (int) $store->query_actions( $query_args, 'count' ); | |
| 192 | + | |
| 193 | + // Check if past-due actions count is greater than or equal to threshold. | |
| 194 | + $check = ( $num_pastdue_actions >= $threshold_min ); | |
| 195 | + $check = (bool) apply_filters( 'action_scheduler_pastdue_actions_check', $check, $num_pastdue_actions, $threshold_seconds, $threshold_min ); | |
| 196 | + } | |
| 197 | + | |
| 198 | + // If check failed, set transient and abort. | |
| 199 | + if ( ! boolval( $check ) ) { | |
| 200 | + $interval = apply_filters( 'action_scheduler_pastdue_actions_check_interval', round( $threshold_seconds / 4 ), $threshold_seconds ); | |
| 201 | + set_transient( 'action_scheduler_last_pastdue_actions_check', time(), $interval ); | |
| 202 | + | |
| 203 | + return; | |
| 204 | + } | |
| 205 | + | |
| 206 | + $actions_url = add_query_arg( | |
| 207 | + array( | |
| 208 | + 'page' => 'action-scheduler', | |
| 209 | + 'status' => 'past-due', | |
| 210 | + 'order' => 'asc', | |
| 211 | + ), | |
| 212 | + admin_url( 'tools.php' ) | |
| 213 | + ); | |
| 214 | + | |
| 215 | + // Print notice. | |
| 216 | + $message = wp_kses( | |
| 217 | + // translators: 1) is the number of affected actions, 2) is a link to an admin screen. | |
| 218 | + _n( | |
| 219 | + '<strong>Action Scheduler:</strong> %1$d <a href="%2$s">past-due action</a> found; something may be wrong. <a href="https://actionscheduler.org/faq/#my-site-has-past-due-actions-what-can-i-do" target="_blank">Read documentation »</a>', | |
| 220 | + '<strong>Action Scheduler:</strong> %1$d <a href="%2$s">past-due actions</a> found; something may be wrong. <a href="https://actionscheduler.org/faq/#my-site-has-past-due-actions-what-can-i-do" target="_blank">Read documentation »</a>', | |
| 221 | + $num_pastdue_actions, | |
| 222 | + 'action-scheduler' | |
| 223 | + ), | |
| 224 | + array( | |
| 225 | + 'strong' => array(), | |
| 226 | + 'a' => array( | |
| 227 | + 'href' => true, | |
| 228 | + 'target' => true, | |
| 229 | + ), | |
| 230 | + ) | |
| 231 | + ); | |
| 232 | + wp_admin_notice( | |
| 233 | + $message, | |
| 234 | + array( | |
| 235 | + 'type' => 'warning', | |
| 236 | + 'paragraph_wrap' => true, | |
| 237 | + ) | |
| 238 | + ); | |
| 239 | + | |
| 240 | + // Facilitate third-parties to evaluate and print notices. | |
| 241 | + do_action( 'action_scheduler_pastdue_actions_extra_notices', $query_args ); | |
| 242 | + } | |
| 243 | + | |
| 244 | + /** | |
| 114 | 245 | * Provide more information about the screen and its data in the help tab. |
| 115 | 246 | */ |
| 116 | 247 | public function add_help_tabs() { |
| 117 | 248 | $screen = get_current_screen(); |
| 118 | 249 | |
| 119 | - if ( ! $screen || self::$screen_id != $screen->id ) { | |
| 250 | + if ( ! $screen || self::$screen_id !== $screen->id ) { | |
| 120 | 251 | return; |
| 121 | 252 | } |
| 122 | 253 | |
| 123 | - $as_version = ActionScheduler_Versions::instance()->latest_version(); | |
| 254 | + $as_version = ActionScheduler_Versions::instance()->latest_version(); | |
| 255 | + $as_source = ActionScheduler_SystemInformation::active_source(); | |
| 256 | + $as_source_path = ActionScheduler_SystemInformation::active_source_path(); | |
| 257 | + $as_source_markup = sprintf( '<code>%s</code>', esc_html( $as_source_path ) ); | |
| 258 | + | |
| 259 | + if ( ! empty( $as_source ) ) { | |
| 260 | + $as_source_markup = sprintf( | |
| 261 | + '%s: <abbr title="%s">%s</abbr>', | |
| 262 | + ucfirst( $as_source['type'] ), | |
| 263 | + esc_attr( $as_source_path ), | |
| 264 | + esc_html( $as_source['name'] ) | |
| 265 | + ); | |
| 266 | + } | |
| 267 | + | |
| 124 | 268 | $screen->add_help_tab( |
| 125 | 269 | array( |
| 126 | 270 | 'id' => 'action_scheduler_about', |
| 127 | 271 | 'title' => __( 'About', 'action-scheduler' ), |
| 128 | 272 | 'content' => |
| 273 | + // translators: %s is the Action Scheduler version. | |
| 129 | 274 | '<h2>' . sprintf( __( 'About Action Scheduler %s', 'action-scheduler' ), $as_version ) . '</h2>' . |
| 130 | 275 | '<p>' . |
| 131 | 276 | __( 'Action Scheduler is a scalable, traceable job queue for background processing large sets of actions. Action Scheduler works by triggering an action hook to run at some time in the future. Scheduled actions can also be scheduled to run on a recurring schedule.', 'action-scheduler' ) . |
| 277 | + '</p>' . | |
| 278 | + '<h3>' . esc_html__( 'Source', 'action-scheduler' ) . '</h3>' . | |
| 279 | + '<p>' . | |
| 280 | + esc_html__( 'Action Scheduler is currently being loaded from the following location. This can be useful when debugging, or if requested by the support team.', 'action-scheduler' ) . | |
| 281 | + '</p>' . | |
| 282 | + '<p>' . $as_source_markup . '</p>' . | |
| 283 | + '<h3>' . esc_html__( 'WP CLI', 'action-scheduler' ) . '</h3>' . | |
| 284 | + '<p>' . | |
| 285 | + sprintf( | |
| 286 | + /* translators: %1$s is WP CLI command (not translatable) */ | |
| 287 | + esc_html__( 'WP CLI commands are available: execute %1$s for a list of available commands.', 'action-scheduler' ), | |
| 288 | + '<code>wp help action-scheduler</code>' | |
| 289 | + ) . | |
| 132 | 290 | '</p>', |
| 133 | 291 | ) |
| 134 | 292 | ); |
| 135 | 293 | |