PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.3
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.3
6.0.3 6.0.2 6.0.1 6.0.0 5.8.6 5.8.5 5.8.4 5.8.3 5.8.2 5.8.1 5.8.0 5.7.9.2 5.7.9.1 5.7.8 5.7.9 5.7.6 5.7.7 5.7.5 5.7.4 5.7.3 5.7.2 5.7.1 trunk 5.6.0 5.6.1 All 34 releases
← All changes | libraries/ct/includes/class-ct-view.php +544 -0 5.7.3 → 6.0.3 View file →
@@ -1,0 +1,544 @@
1 +<?php
2 +/**
3 + * View class
4 + *
5 + * @author GamiPress <[email protected]>, Ruben Garcia <[email protected]>
6 + *
7 + * @since 1.0.0
8 + */
9 +// Exit if accessed directly
10 +defined( 'ABSPATH' ) || exit;
11 +
12 +if ( ! class_exists( 'CT_View' ) ) :
13 +
14 + class CT_View {
15 +
16 + /**
17 + * @var string View name
18 + */
19 + protected $name = '';
20 +
21 + /**
22 + * @var array View args
23 + */
24 + protected $args = array();
25 +
26 + /**
27 + * CT_View constructor.
28 + *
29 + * @since 1.0.0
30 + *
31 + * @param string $name
32 + * @param array $args
33 + */
34 + public function __construct( $name, $args ) {
35 +
36 + $this->name = $name;
37 +
38 + $this->args = wp_parse_args( $args, array(
39 + 'menu_title' => ucfirst( $this->name ),
40 + 'page_title' => ucfirst( $this->name ),
41 + 'menu_slug' => $this->name,
42 + 'parent_slug' => '',
43 + 'show_in_menu' => true,
44 + 'menu_icon' => '',
45 + 'menu_position' => null,
46 + 'capability' => 'manage_options',
47 + 'priority' => null, // Used for the hook 'admin_menu'
48 + 'admin_bar' => false,
49 + 'admin_bar_parent' => false,
50 + 'admin_bar_priority' => null, // Used for the hook 'admin_bar_menu'
51 + ) );
52 +
53 + $this->add_hooks();
54 +
55 + }
56 +
57 + /**
58 + * View hooks (called on constructor).
59 + *
60 + * @since 1.0.0
61 + */
62 + public function add_hooks() {
63 +
64 + add_action( 'admin_init', array( $this, 'admin_init' ) );
65 + add_action( 'admin_init', array( $this, 'override_page_title' ) );
66 +
67 + if( $this->args['priority'] === null ) {
68 + $this->args['priority'] = empty( $this->args['parent_slug'] ) ? 20 : 21;
69 + }
70 +
71 + // Note: sub-menus need to be registered after parent
72 + add_action( 'admin_menu', array( $this, 'admin_menu' ), $this->args['priority'] );
73 +
74 + add_filter( 'parent_file', array( $this, 'parent_file' ), 10 );
75 +
76 + add_filter( 'submenu_file', array( $this, 'submenu_file' ), 10, 2 );
77 +
78 + add_action( 'adminmenu', array( $this, 'restore_plugin_page' ), 10, 2 );
79 +
80 + if( $this->args['admin_bar'] ) {
81 + add_action( 'admin_bar_menu', array( $this, 'admin_bar_menu' ), ( $this->args['admin_bar_priority'] === null ? 999 : $this->args['admin_bar_priority'] ) );
82 + }
83 +
84 + add_filter( 'screen_options_show_screen', array( $this, 'show_screen_options' ), 10, 2 );
85 +
86 + add_filter( 'screen_settings', array( $this, 'maybe_screen_settings' ), 10, 2 );
87 +
88 + add_filter( 'admin_init', array( $this, 'maybe_set_screen_settings' ), 11 );
89 + add_filter( 'ct-set-screen-option', array( $this, 'set_screen_settings' ), 10, 3 );
90 +
91 + }
92 +
93 + public function show_screen_options( $show_screen, $screen ) {
94 +
95 + if( ! $this->is_current_view() ) {
96 + return $show_screen;
97 + }
98 +
99 + $screen_slug = explode( '_page_', $screen->id );
100 +
101 + if( isset( $screen_slug[1] ) && $screen_slug[1] === $this->args['menu_slug'] ) {
102 + return true;
103 + }
104 +
105 + return $show_screen;
106 +
107 + }
108 +
109 + /**
110 + * Check if current screen is own.
111 + *
112 + * @since 1.0.0
113 + *
114 + * @param string $screen_settings Screen settings.
115 + * @param WP_Screen $screen WP_Screen object.
116 + *
117 + * @return string $screen_settings
118 + */
119 + public function maybe_screen_settings( $screen_settings, $screen ) {
120 +
121 + if( ! $this->is_current_view() ) {
122 + return $screen_settings;
123 + }
124 +
125 + $screen_slug = explode( '_page_', $screen->id );
126 +
127 + // Check if current screen matches this menu slug
128 + if( isset( $screen_slug[1] ) && $screen_slug[1] === $this->args['menu_slug'] ) {
129 +
130 + global $ct_registered_tables, $ct_table;
131 +
132 + if( ! isset( $ct_registered_tables[$this->name] ) ) {
133 + return $screen_settings;
134 + }
135 +
136 + // Set up global vars
137 + $ct_table = $ct_registered_tables[$this->name];
138 +
139 + ob_start();
140 + $this->screen_settings( $screen_settings, $screen );
141 + $screen_settings .= ob_get_clean();
142 +
143 + }
144 +
145 + return $screen_settings;
146 +
147 + }
148 +
149 + /**
150 + * Screen settings text displayed in the Screen Options tab.
151 + *
152 + * @since 1.0.0
153 + *
154 + * @param string $screen_settings Screen settings.
155 + * @param WP_Screen $screen WP_Screen object.
156 + */
157 + public function screen_settings( $screen_settings, $screen ) {
158 + // Override
159 + }
160 +
161 + /**
162 + * Saves view options.
163 + *
164 + * Function based on set_screen_options()
165 + *
166 + * @since 1.0.0
167 + *
168 + * @see set_screen_options()
169 + */
170 + function maybe_set_screen_settings() {
171 +
172 + if( ! $this->is_current_view() ) {
173 + return;
174 + }
175 +
176 + if ( isset( $_POST['wp_screen_options'] ) && is_array( $_POST['wp_screen_options'] ) ) {
177 + check_admin_referer( 'screen-options-nonce', 'screenoptionnonce' );
178 +
179 + if ( ! $user = wp_get_current_user() ) {
180 + return;
181 + }
182 +
183 + $option = sanitize_key( $_POST['wp_screen_options']['option'] );
184 + $value = sanitize_text_field( $_POST['wp_screen_options']['value'] );
185 +
186 + if ( empty( $option ) ) {
187 + return;
188 + }
189 +
190 + $option = str_replace('-', '_', $option);
191 +
192 + /**
193 + * Filters a screen option value before it is set.
194 + *
195 + * The filter can also be used to modify non-standard [items]_per_page
196 + * settings. See the parent function for a full list of standard options.
197 + *
198 + * Returning false to the filter will skip saving the current option.
199 + *
200 + * @since 1.0.0
201 + *
202 + * @see set_screen_options()
203 + *
204 + * @param bool|int $value Screen option value. Default false to skip.
205 + * @param string $option The option name.
206 + * @param int $value The number of rows to use.
207 + */
208 + $value = apply_filters( 'ct-set-screen-option', false, $option, $value );
209 +
210 + if ( false === $value )
211 + return;
212 +
213 + update_user_meta( $user->ID, $option, $value );
214 +
215 + $url = remove_query_arg( array( 'pagenum', 'apage', 'paged' ), wp_get_referer() );
216 + if ( isset( $_POST['mode'] ) ) {
217 + $url = add_query_arg( array( 'mode' => $_POST['mode'] ), $url );
218 + }
219 +
220 + wp_safe_redirect( $url );
221 + exit;
222 + }
223 + }
224 +
225 + /**
226 + * Screen option value before it is set.
227 + *
228 + * The filter can also be used to modify non-standard [items]_per_page
229 + * settings. See the parent function for a full list of standard options.
230 + *
231 + * Returning false to the filter will skip saving the current option.
232 + *
233 + * @since 1.0.0
234 + *
235 + * @see set_screen_options()
236 + *
237 + * @param bool|int $value_to_set Screen option value to set. Default false to skip.
238 + * @param string $option The option name.
239 + * @param int $value The option value.
240 + *
241 + * @return bool|mixed False to skip or any other value to set as option value
242 + */
243 + public function set_screen_settings( $value_to_set, $option, $value ) {
244 +
245 + // Override
246 +
247 + return $value_to_set;
248 +
249 + }
250 +
251 + /**
252 + * Create a new menu
253 + *
254 + * @since 1.0.0
255 + */
256 + public function admin_menu() {
257 +
258 + // Override capability by the table capability
259 + $ct_table = ct_get_table_object( $this->name );
260 +
261 + if( ! empty( $ct_table->capability ) ) {
262 + $this->args['capability'] = $ct_table->capability;
263 + }
264 +
265 + $page_title = $this->get_page_title();
266 + $menu_title = $this->get_menu_title();
267 +
268 + if( ! $this->args['show_in_menu'] ) {
269 +
270 + // Add in a hidden menu
271 + add_submenu_page( '', $page_title, $menu_title, $this->args['capability'], $this->args['menu_slug'], array( $this, 'render' ) );
272 +
273 + } else {
274 +
275 + if( empty( $this->args['parent_slug'] ) ) {
276 + // View menu
277 + add_menu_page( $page_title, $menu_title, $this->args['capability'], $this->args['menu_slug'], array( $this, 'render' ), $this->args['menu_icon'], $this->args['menu_position'] );
278 + } else {
279 + // View sub menu
280 + add_submenu_page( $this->args['parent_slug'], $page_title, $menu_title, $this->args['capability'], $this->args['menu_slug'], array( $this, 'render' ) );
281 + }
282 +
283 + }
284 +
285 + }
286 +
287 + /**
288 + * Parent file fix when a view is registered in a submenu
289 + *
290 + * @since 1.0.0
291 + */
292 + public function parent_file( $parent_file ) {
293 +
294 + global $ct_table, $plugin_page;
295 +
296 + if( ! $this->is_current_view() ) {
297 + return $parent_file;
298 + }
299 +
300 + $list_view_args = $ct_table->views->list->args;
301 +
302 + // If not empty parent slug, override actual parent slug
303 + if( ! empty( $this->args['parent_slug'] ) ) {
304 + $parent_file = $this->args['parent_slug'];
305 +
306 + if( $this->args['menu_slug'] !== $list_view_args['menu_slug'] ) {
307 + // Hack required to make parent file work because get overwritten on get_admin_page_parent() function
308 + $plugin_page = null;
309 + }
310 + }
311 +
312 + // If we are on an add or edit view and list is displayed on menu, apply the list parent slug
313 + if( $this->args['menu_slug'] !== $list_view_args['menu_slug'] ) {
314 +
315 + if( $list_view_args['show_in_menu'] && ! empty( $list_view_args['parent_slug'] ) ) {
316 + $parent_file = $list_view_args['parent_slug'];
317 +
318 + // Hack required to make parent file work because get overwritten on get_admin_page_parent() function
319 + $plugin_page = null;
320 + }
321 +
322 + }
323 +
324 + return $parent_file;
325 +
326 + }
327 +
328 + /**
329 + * Submenu file fix when a view is registered in a submenu
330 + *
331 + * @since 1.0.0
332 + */
333 + public function submenu_file( $submenu_file, $parent_file ) {
334 +
335 + global $ct_table;
336 +
337 + if( ! $this->is_current_view() ) {
338 + return $submenu_file;
339 + }
340 +
341 + $list_view_args = $ct_table->views->list->args;
342 +
343 + // If we are on an add or edit view and list is displayed on menu, then highlight list view
344 + if( $this->args['menu_slug'] !== $list_view_args['menu_slug'] ) {
345 + if( $list_view_args['show_in_menu'] && ! empty( $list_view_args['parent_slug'] ) ) {
346 + $submenu_file = $list_view_args['menu_slug'];
347 + }
348 + }
349 +
350 + return $submenu_file;
351 +
352 + }
353 +
354 + /**
355 + * Restore global $plugin_page since it was required to get overwritten on parent_file()
356 + *
357 + * @since 1.0.0
358 + */
359 + public function restore_plugin_page() {
360 +
361 + global $ct_table, $plugin_page;
362 +
363 + if( ! $this->is_current_view() ) {
364 + return;
365 + }
366 +
367 + $list_view_args = $ct_table->views->list->args;
368 +
369 + // If we are on an add or edit view and list is displayed on menu, restore plugin page too
370 + if( $this->args['menu_slug'] !== $list_view_args['menu_slug'] ) {
371 +
372 + // If not empty parent slug then restore plugin page
373 + if( ! empty( $this->args['parent_slug'] ) ) {
374 + $plugin_page = $this->args['menu_slug'];
375 + }
376 +
377 + if( $list_view_args['show_in_menu'] && ! empty( $list_view_args['parent_slug'] ) ) {
378 + $plugin_page = $this->args['menu_slug'];
379 + }
380 + }
381 +
382 + }
383 +
384 + public function admin_bar_menu( $wp_admin_bar ) {
385 +
386 + $wp_admin_bar->add_node( array(
387 + 'id' => $this->args['menu_slug'],
388 + 'title' => $this->get_page_title(),
389 + 'parent' => $this->args['admin_bar_parent'],
390 + 'href' => admin_url( 'admin.php?page=' . $this->args['menu_slug'] )
391 + ) );
392 +
393 + }
394 +
395 + public function is_current_view() {
396 +
397 + global $ct_registered_tables, $pagenow;
398 +
399 + if( $pagenow !== 'admin.php' ) {
400 + return false;
401 + }
402 +
403 + if( ! isset( $_GET['page'] ) ) {
404 + return false;
405 + }
406 +
407 + if( empty( $_GET['page'] ) || $_GET['page'] !== $this->args['menu_slug'] ) {
408 + return false;
409 + }
410 +
411 + if( ! isset( $ct_registered_tables[$this->name] ) ) {
412 + return false;
413 + }
414 +
415 + return true;
416 + }
417 +
418 + public function get_page_title() {
419 +
420 + $prefix = substr( $this->args['menu_slug'], 0, 4 );
421 +
422 + switch( $prefix ) {
423 + case 'add_':
424 + $title = ct_get_table_label( $this->name, 'add_page_title' );
425 + break;
426 + case 'edit':
427 + $title = ct_get_table_label( $this->name, 'edit_page_title' );
428 + break;
429 + default:
430 + $title = ct_get_table_label( $this->name, 'list_page_title' );
431 + break;
432 + }
433 +
434 + if( ! empty( $title ) ) {
435 + return $title;
436 + }
437 +
438 + return $this->args['page_title'];
439 +
440 + }
441 +
442 + public function get_menu_title() {
443 +
444 + $prefix = substr( $this->args['menu_slug'], 0, 4 );
445 +
446 + switch( $prefix ) {
447 + case 'add_':
448 + $title = ct_get_table_label( $this->name, 'add_menu_title' );
449 + break;
450 + case 'edit':
451 + $title = ct_get_table_label( $this->name, 'edit_menu_title' );
452 + break;
453 + default:
454 + $title = ct_get_table_label( $this->name, 'list_menu_title' );
455 + break;
456 + }
457 +
458 + if( ! empty( $title ) ) {
459 + return $title;
460 + }
461 +
462 + return $this->args['menu_title'];
463 + }
464 +
465 + public function get_slug() {
466 + return $this->args['menu_slug'];
467 + }
468 +
469 + public function get_link() {
470 + return admin_url( "admin.php?page=" . $this->args['menu_slug'] );
471 + }
472 +
473 + /**
474 + * View admin init.
475 + *
476 + * This function is called on admin_init hook.
477 + * Includes some checks to determine if the init() function should be called.
478 + *
479 + * @since 1.0.0
480 + */
481 + public function admin_init() {
482 +
483 + global $ct_registered_tables, $ct_table;
484 +
485 + if( ! $this->is_current_view() ) {
486 + return;
487 + }
488 +
489 + // Setup the global CT_Table object for this screen
490 + $ct_table = $ct_registered_tables[$this->name];
491 +
492 + // Run the init function
493 + $this->init();
494 +
495 + }
496 +
497 + /**
498 + * View init.
499 + *
500 + * Run redirects here to avoid "headers already sent" error.
501 + *
502 + * @since 1.0.0
503 + */
504 + public function init() {
505 +
506 + do_action( "ct_init_{$this->name}_view", $this );
507 +
508 + }
509 +
510 + /**
511 + * View content.
512 + *
513 + * @since 1.0.0
514 + */
515 + public function render() {
516 +
517 + do_action( "ct_render_{$this->name}_view", $this );
518 +
519 + }
520 +
521 + /**
522 + * Set the $title global to avoid warnings
523 + *
524 + * @since 1.0.0
525 + *
526 + * @return string
527 + */
528 + public function override_page_title() {
529 +
530 + global $title;
531 +
532 + if ( $this->is_current_view() ) {
533 +
534 + if ( is_null( $title ) || empty( $title ) ) {
535 + $title = $this->get_page_title();
536 +
537 + }
538 +
539 + }
540 + }
541 +
542 + }
543 +
544 +endif;