PluginProbe
WANotifier for Forms and Actions / 3.1.1
WANotifier for Forms and Actions v3.1.1
3.1.1 3.1.0 3.0.4 2.7.10 2.7.11 2.7.12 2.7.13 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 3.0.0 3.0.1 3.0.2 3.0.3 trunk 0.1.0 0.1.1 1.0.0 1.0.1 1.0.2 All 67 releases
notifier / libraries / action-scheduler / classes / ActionScheduler_AdminView.php

ActionScheduler_AdminView.php in WANotifier for Forms and Actions 3.1.1, at libraries/action-scheduler/classes/ActionScheduler_AdminView.php

313 lines 10.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class ActionScheduler_AdminView
5 *
6 * @codeCoverageIgnore
7 */
8 class ActionScheduler_AdminView extends ActionScheduler_AdminView_Deprecated {
9
10 /**
11 * Instance.
12 *
13 * @var null|self
14 */
15 private static $admin_view = null;
16
17 /**
18 * Screen ID.
19 *
20 * @var string
21 */
22 private static $screen_id = 'tools_page_action-scheduler';
23
24 /**
25 * ActionScheduler_ListTable instance.
26 *
27 * @var ActionScheduler_ListTable
28 */
29 protected $list_table;
30
31 /**
32 * Get instance.
33 *
34 * @return ActionScheduler_AdminView
35 * @codeCoverageIgnore
36 */
37 public static function instance() {
38
39 if ( empty( self::$admin_view ) ) {
40 $class = apply_filters( 'action_scheduler_admin_view_class', 'ActionScheduler_AdminView' );
41 self::$admin_view = new $class();
42 }
43
44 return self::$admin_view;
45 }
46
47 /**
48 * Initialize.
49 *
50 * @codeCoverageIgnore
51 */
52 public function init() {
53 if ( is_admin() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) {
54
55 if ( class_exists( 'WooCommerce' ) ) {
56 add_action( 'woocommerce_admin_status_content_action-scheduler', array( $this, 'render_admin_ui' ) );
57 add_action( 'woocommerce_system_status_report', array( $this, 'system_status_report' ) );
58 add_filter( 'woocommerce_admin_status_tabs', array( $this, 'register_system_status_tab' ) );
59 }
60
61 add_action( 'admin_menu', array( $this, 'register_menu' ) );
62 add_action( 'admin_notices', array( $this, 'maybe_check_pastdue_actions' ) );
63 add_action( 'current_screen', array( $this, 'add_help_tabs' ) );
64 }
65 }
66
67 /**
68 * Print system status report.
69 */
70 public function system_status_report() {
71 $table = new ActionScheduler_wcSystemStatus( ActionScheduler::store() );
72 $table->render();
73 }
74
75 /**
76 * Registers action-scheduler into WooCommerce > System status.
77 *
78 * @param array $tabs An associative array of tab key => label.
79 * @return array $tabs An associative array of tab key => label, including Action Scheduler's tabs
80 */
81 public function register_system_status_tab( array $tabs ) {
82 $tabs['action-scheduler'] = __( 'Scheduled Actions', 'action-scheduler' );
83
84 return $tabs;
85 }
86
87 /**
88 * Include Action Scheduler's administration under the Tools menu.
89 *
90 * A menu under the Tools menu is important for backward compatibility (as that's
91 * where it started), and also provides more convenient access than the WooCommerce
92 * System Status page, and for sites where WooCommerce isn't active.
93 */
94 public function register_menu() {
95 $hook_suffix = add_submenu_page(
96 'tools.php',
97 __( 'Scheduled Actions', 'action-scheduler' ),
98 __( 'Scheduled Actions', 'action-scheduler' ),
99 'manage_options',
100 'action-scheduler',
101 array( $this, 'render_admin_ui' )
102 );
103 add_action( 'load-' . $hook_suffix, array( $this, 'process_admin_ui' ) );
104 }
105
106 /**
107 * Triggers processing of any pending actions.
108 */
109 public function process_admin_ui() {
110 $this->get_list_table();
111 }
112
113 /**
114 * Renders the Admin UI
115 */
116 public function render_admin_ui() {
117 $table = $this->get_list_table();
118 $table->display_page();
119 }
120
121 /**
122 * Get the admin UI object and process any requested actions.
123 *
124 * @return ActionScheduler_ListTable
125 */
126 protected function get_list_table() {
127 if ( null === $this->list_table ) {
128 $this->list_table = new ActionScheduler_ListTable( ActionScheduler::store(), ActionScheduler::logger(), ActionScheduler::runner() );
129 $this->list_table->process_actions();
130 }
131
132 return $this->list_table;
133 }
134
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 &raquo;</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 &raquo;</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 /**
245 * Provide more information about the screen and its data in the help tab.
246 */
247 public function add_help_tabs() {
248 $screen = get_current_screen();
249
250 if ( ! $screen || self::$screen_id !== $screen->id ) {
251 return;
252 }
253
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
268 $screen->add_help_tab(
269 array(
270 'id' => 'action_scheduler_about',
271 'title' => __( 'About', 'action-scheduler' ),
272 'content' =>
273 // translators: %s is the Action Scheduler version.
274 '<h2>' . sprintf( __( 'About Action Scheduler %s', 'action-scheduler' ), $as_version ) . '</h2>' .
275 '<p>' .
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 ) .
290 '</p>',
291 )
292 );
293
294 $screen->add_help_tab(
295 array(
296 'id' => 'action_scheduler_columns',
297 'title' => __( 'Columns', 'action-scheduler' ),
298 'content' =>
299 '<h2>' . __( 'Scheduled Action Columns', 'action-scheduler' ) . '</h2>' .
300 '<ul>' .
301 sprintf( '<li><strong>%1$s</strong>: %2$s</li>', __( 'Hook', 'action-scheduler' ), __( 'Name of the action hook that will be triggered.', 'action-scheduler' ) ) .
302 sprintf( '<li><strong>%1$s</strong>: %2$s</li>', __( 'Status', 'action-scheduler' ), __( 'Action statuses are Pending, Complete, Canceled, Failed', 'action-scheduler' ) ) .
303 sprintf( '<li><strong>%1$s</strong>: %2$s</li>', __( 'Arguments', 'action-scheduler' ), __( 'Optional data array passed to the action hook.', 'action-scheduler' ) ) .
304 sprintf( '<li><strong>%1$s</strong>: %2$s</li>', __( 'Group', 'action-scheduler' ), __( 'Optional action group.', 'action-scheduler' ) ) .
305 sprintf( '<li><strong>%1$s</strong>: %2$s</li>', __( 'Recurrence', 'action-scheduler' ), __( 'The action\'s schedule frequency.', 'action-scheduler' ) ) .
306 sprintf( '<li><strong>%1$s</strong>: %2$s</li>', __( 'Scheduled', 'action-scheduler' ), __( 'The date/time the action is/was scheduled to run.', 'action-scheduler' ) ) .
307 sprintf( '<li><strong>%1$s</strong>: %2$s</li>', __( 'Log', 'action-scheduler' ), __( 'Activity log for the action.', 'action-scheduler' ) ) .
308 '</ul>',
309 )
310 );
311 }
312 }
313