Plugin Directory

source: advanced-cron-manager/trunk/inc/AdminScreen.php

Last change on this file was 3356826, checked in by bracketspace, 3 months ago

Update to version 2.6.4 from GitHub

File size: 8.7 KB
Line 
1<?php
2/**
3 * AdminScreen class
4 * Displays admin screen
5 *
6 * @package advanced-cron-manager
7 */
8
9namespace underDEV\AdvancedCronManager;
10
11use underDEV\AdvancedCronManager\Cron\Element\Event;
12use underDEV\Utils;
13use underDEV\AdvancedCronManager\Cron;
14
15/**
16 * Admin Screen class.
17 */
18class AdminScreen {
19
20        /**
21         * View class
22         *
23         * @var instance of underDEV\AdvancedCronManage\Utils\View
24         */
25        public $view;
26
27        /**
28         * Ajax class
29         *
30         * @var instance of underDEV\AdvancedCronManage\Utils\Ajax
31         */
32        public $ajax;
33
34        /**
35         * Schedules class
36         *
37         * @var instance of underDEV\AdvancedCronManage\Cron\Schedules
38         */
39        public $schedules;
40
41        /**
42         * Events class
43         *
44         * @var instance of underDEV\AdvancedCronManage\Cron\Events
45         */
46        public $events;
47
48        /**
49         * Admin page hook
50         *
51         * @var string
52         */
53        private $page_hook;
54
55        /**
56         * Default tab names for events
57         *
58         * @var array
59         */
60        protected $default_event_details_tabs;
61
62        /**
63         * Constructor
64         *
65         * @param Utils\View     $view      View class.
66         * @param Utils\Ajax     $ajax      Ajax class.
67         * @param Cron\Schedules $schedules Schedules class.
68         * @param Cron\Events    $events    Events class.
69         */
70        public function __construct( Utils\View $view, Utils\Ajax $ajax, Cron\Schedules $schedules, Cron\Events $events ) {
71
72                $this->view      = $view;
73                $this->ajax      = $ajax;
74                $this->schedules = $schedules;
75                $this->events    = $events;
76        }
77
78        /**
79         * Get default event details tabs with translated labels
80         *
81         * @return array
82         */
83        protected function get_default_event_details_tabs() {
84                if ( null === $this->default_event_details_tabs ) {
85                        $this->default_event_details_tabs = array(
86                                'logs'           => __( 'Logs', 'advanced-cron-manager' ),
87                                'arguments'      => __( 'Arguments', 'advanced-cron-manager' ),
88                                'schedule'       => __( 'Schedule', 'advanced-cron-manager' ),
89                                'implementation' => __( 'Implementation', 'advanced-cron-manager' ),
90                                'listeners'      => __( 'Listeners', 'advanced-cron-manager' ),
91                        );
92                }
93
94                return $this->default_event_details_tabs;
95        }
96
97        /**
98         * Call method
99         *
100         * @param  string $method Called method.
101         * @param  array  $args   Arguments.
102         * @return mixed
103         */
104        public function __call( $method, $args ) {
105
106                if ( strpos( $method, 'ajax_rerender_' ) !== false ) {
107
108                        if ( ! current_user_can( 'manage_options' ) ) {
109                                $this->ajax->error( array(
110                                        __( "You're not allowed to do that.", 'advanced-cron-manager' ),
111                                ) );
112                        }
113
114                        /**
115                         * From: ajax_rerender_schedules_table
116                         * To:   load_schedules_table_part
117                         */
118                        $method_to_call = str_replace( 'ajax_rerender_', 'load_', $method . '_part' );
119
120                        ob_start();
121
122                        call_user_func( array( $this, $method_to_call ), $this );
123
124                        $this->ajax->success( ob_get_clean() );
125
126                }
127        }
128
129        /**
130         * Loads the page screen
131         *
132         * @return void
133         */
134        public function load_page_wrapper() {
135                $this->view->get_view( 'wrapper' );
136        }
137
138        /**
139         * Loads searchbox
140         * There are used $this->view instead of passed instance
141         * because we want to separate scopes
142         *
143         * @return void
144         */
145        public function load_searchbox_part() {
146                $this->view->get_view( 'parts/searchbox' );
147        }
148
149        /**
150         * Loads events table
151         * There are used $this->view instead of passed instance
152         * because we want to separate scopes
153         *
154         * @return void
155         */
156        public function load_events_table_part() {
157
158                if ( ! current_user_can( 'manage_options' ) ) {
159                        return;
160                }
161
162                $this->view->set_var( 'events', $this->events->get_events() );
163                $this->view->set_var( 'events_count', $this->events->count() );
164                $this->view->set_var( 'schedules', $this->schedules );
165
166                /**
167                 * It should be an array in format: tab_slug => Tab Name
168                 */
169                $this->view->set_var( 'details_tabs', apply_filters( 'advanced-cron-manager/screen/event/details/tabs', array() ) );
170
171                $this->view->get_view( 'parts/events/section' );
172        }
173
174        /**
175         * Loads schedules table
176         * There are used $this->view instead of passed instance
177         * because we want to separate scopes
178         *
179         * @return void
180         */
181        public function load_schedules_table_part() {
182
183                if ( ! current_user_can( 'manage_options' ) ) {
184                        return;
185                }
186
187                $this->view->set_var( 'schedules', $this->schedules->get_schedules(), true );
188
189                $this->view->get_view( 'parts/schedules/section' );
190        }
191
192        /**
193         * Loads slidebar template
194         * There are used $this->view instead of passed instance
195         * because we want to separate scopes
196         *
197         * @return void
198         */
199        public function load_slidebar_part() {
200                $this->view->get_view( 'elements/slidebar' );
201        }
202
203        /**
204         * Loads preview modal template
205         * There are used $this->view instead of passed instance
206         * because we want to separate scopes
207         *
208         * @return void
209         */
210        public function load_preview_modal_part() {
211                $this->view->get_view( 'elements/preview-modal' );
212        }
213
214        /**
215         * Adds default event details tabs
216         * It also registers the actions for the content
217         *
218         * @param array $tabs filtered tabs.
219         */
220        public function add_default_event_details_tabs( $tabs ) {
221
222                foreach ( $this->get_default_event_details_tabs() as $tab_slug => $tab_name ) {
223                        $tabs[ $tab_slug ] = $tab_name;
224                        add_action( 'advanced-cron-manager/screen/event/details/tab/' . $tab_slug, array( $this, 'load_event_tab_' . $tab_slug ), 10, 1 );
225                }
226
227                return $tabs;
228        }
229
230        /**
231         * Loads Logs tab content for event details
232         * Scope for $view is the same as in events/section view
233         *
234         * @param  object $view local View instance.
235         * @return void
236         */
237        public function load_event_tab_logs( $view ) {
238                if ( apply_filters( 'advanced-cron-manager/screen/event/details/tabs/logs/display', true ) ) {
239                        $view->get_view( 'parts/events/tabs/logs' );
240                }
241        }
242
243        /**
244         * Loads Listeners tab content for event details
245         * Scope for $view is the same as in events/section view
246         *
247         * @param  object $view local View instance.
248         * @return void
249         */
250        public function load_event_tab_listeners( $view ) {
251                if ( apply_filters( 'advanced-cron-manager/screen/event/details/tabs/listeners/display', true ) ) {
252                        $view->get_view( 'parts/events/tabs/listeners' );
253                }
254        }
255
256        /**
257         * Loads Arguments tab content for event details
258         * Scope for $view is the same as in events/section view
259         *
260         * @param  object $view local View instance.
261         * @return void
262         */
263        public function load_event_tab_arguments( $view ) {
264                if ( apply_filters( 'advanced-cron-manager/screen/event/details/tabs/arguments/display', true ) ) {
265                        $view->get_view( 'parts/events/tabs/arguments' );
266                }
267        }
268
269        /**
270         * Loads Schedule tab content for event details
271         * Scope for $view is the same as in events/section view
272         *
273         * @param  object $view local View instance.
274         * @return void
275         */
276        public function load_event_tab_schedule( $view ) {
277                if ( apply_filters( 'advanced-cron-manager/screen/event/details/tabs/schedule/display', true ) ) {
278                        $view->get_view( 'parts/events/tabs/schedule' );
279                }
280        }
281
282        /**
283         * Loads Implementation tab content for event details
284         * Scope for $view is the same as in events/section view
285         *
286         * @param  object $view local View instance.
287         * @return void
288         */
289        public function load_event_tab_implementation( $view ) {
290                if ( apply_filters( 'advanced-cron-manager/screen/event/details/tabs/implementation/display', true ) ) {
291                        $view->get_view( 'parts/events/tabs/implementation' );
292                }
293        }
294
295        /**
296         * Registers the plugin page under Tools in WP Admin
297         *
298         * @return void
299         */
300        public function register_screen() {
301
302                $this->page_hook = add_management_page(
303                        __( 'Advanced Cron Manager', 'advanced-cron-manager' ),
304                        __( 'Cron Manager', 'advanced-cron-manager' ),
305                        'manage_options',
306                        'advanced-cron-manager',
307                        array( $this, 'load_page_wrapper' )
308                );
309        }
310
311        /**
312         * Gets the page hook
313         *
314         * @return string
315         */
316        public function get_page_hook() {
317                return $this->page_hook;
318        }
319
320        /**
321         * Prepare event arguments for row
322         *
323         * @param  Event $event Event object.
324         * @return array<mixed>
325         */
326        public static function prepare_event_arguments( $event ) {
327                $parsed_args       = array();
328                $show_args_preview = false;
329
330                foreach ( $event->args as $arg ) {
331                        if ( is_array( $arg ) || is_bool( $arg ) ) {
332                                $parsed_args[] = array(
333                                        'type' => gettype( $arg ),
334                                        'msg'  => wp_json_encode( $arg ),
335                                );
336                        } elseif ( is_object( $arg ) ) {
337                                $parsed_args[] = array(
338                                        'type'      => gettype( $arg ),
339                                        'msg'       => wp_json_encode( $arg ),
340                                        'className' => get_class( $arg ),
341                                );
342                        } else {
343                                $parsed_args[] = array(
344                                        'type' => gettype( $arg ),
345                                        'msg'  => wp_filter_nohtml_kses( sanitize_text_field(
346                                                html_entity_decode( $arg, ENT_QUOTES, 'UTF-8' )
347                                        ) ),
348                                );
349                        }
350
351                        $show_args_preview = is_array( $arg ) || is_object( $arg );
352                }
353
354                $args_length = array_sum( array_map( function ( $ar ) {
355                        return strlen( $ar['msg'] );
356                }, $parsed_args ) );
357
358                return array(
359                        'args_length'       => $args_length,
360                        'parsed_args'       => $parsed_args,
361                        'show_args_preview' => $show_args_preview,
362                );
363        }
364}
Note: See TracBrowser for help on using the repository browser.