Total Complexity | 79 |
Total Lines | 596 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like uix often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use uix, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class uix { |
||
20 | |||
21 | /** |
||
22 | * The slug for this plugin |
||
23 | * |
||
24 | * @since 1.0.0 |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $plugin_slug = null; |
||
28 | |||
29 | /** |
||
30 | * List of registered pages |
||
31 | * |
||
32 | * @since 1.0.0 |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $pages = array(); |
||
36 | |||
37 | /** |
||
38 | * List of registered metaboxes |
||
39 | * |
||
40 | * @since 1.0.0 |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $metaboxes = array(); |
||
44 | |||
45 | /** |
||
46 | * Holds class instance |
||
47 | * |
||
48 | * @since 1.0.0 |
||
49 | * @var object|\uix |
||
|
|||
50 | */ |
||
51 | protected static $instance = null; |
||
52 | |||
53 | /** |
||
54 | * Holds the option screen prefix |
||
55 | * |
||
56 | * @since 1.0.0 |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $plugin_screen_hook_suffix = null; |
||
60 | |||
61 | /** |
||
62 | * Holds the current_tab being rendered |
||
63 | * |
||
64 | * @since 1.0.0 |
||
65 | * @var string |
||
66 | */ |
||
67 | public $current_tab = null; |
||
68 | |||
69 | /** |
||
70 | * Initialize the plugin by setting localization, filters, and |
||
71 | * administration functions. |
||
72 | * |
||
73 | * @since 1.0.0 |
||
74 | * @access private |
||
75 | */ |
||
76 | private function __construct( $slug ) { |
||
77 | |||
78 | |||
79 | // set slug |
||
80 | $this->plugin_slug = $slug; |
||
81 | |||
82 | // add admin page |
||
83 | add_action( 'admin_menu', array( $this, 'add_settings_pages' ), 25 ); |
||
84 | |||
85 | // add metaboxes |
||
86 | add_action( 'add_meta_boxes', array( $this, 'add_metaboxes' ), 25 ); |
||
87 | |||
88 | // save config |
||
89 | add_action( 'wp_ajax_' . $this->plugin_slug . '_save_config', array( |
||
90 | $this, |
||
91 | 'save_config', |
||
92 | ) ); |
||
93 | |||
94 | } |
||
95 | |||
96 | |||
97 | public function add_metaboxes() { |
||
98 | //add_meta_box( 'meta-box-id', __( 'My Meta Box', 'textdomain' ), array( $this, 'render_metabox' ), 'post' ); |
||
99 | } |
||
100 | |||
101 | public function render_metabox( $a ) { |
||
102 | var_dump( $a ); |
||
103 | die; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Return an instance of this class. |
||
108 | * |
||
109 | * @since 1.0.0 |
||
110 | * @return object|\uix\uix A single instance of this class. |
||
111 | */ |
||
112 | public static function get_instance( $slug ) { |
||
120 | |||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Return a setting |
||
125 | * |
||
126 | * @since 1.0.0 |
||
127 | * @return string/array the requested setting |
||
128 | */ |
||
129 | public static function get_setting( $path, $manual = false ) { |
||
130 | |||
131 | $path = explode( '.', $path ); |
||
132 | $temp = null; |
||
133 | $page_slug = array_shift( $path ); |
||
134 | |||
135 | if ( null == self::$instance || true === $manual ) { |
||
136 | if ( false === $manual ) { |
||
137 | trigger_error( 'Cannot request a value without a UIX instance. Set second argument to TRUE for manual lookup.' ); |
||
138 | |||
139 | return; |
||
140 | } |
||
141 | // attempt a manual lookup - requires the full option name |
||
142 | $option_tag = $page_slug; |
||
143 | } else { |
||
144 | if ( ! empty( self::$instance->pages[ $page_slug ]['option_name'] ) ) { |
||
145 | $option_tag = self::$instance->pages[ $page_slug ]['option_name']; |
||
146 | } else { |
||
147 | $option_tag = '_' . self::$instance->plugin_slug . '_' . $page_slug; |
||
148 | } |
||
149 | } |
||
150 | $temp = get_option( $option_tag ); |
||
151 | foreach ( $path as $index => $value ) { |
||
152 | if ( ! isset( $temp[ $value ] ) ) { |
||
153 | return null; |
||
154 | } |
||
155 | $temp = $temp[ $value ]; |
||
156 | } |
||
157 | |||
158 | return $temp; |
||
159 | |||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Register the admin pages |
||
164 | * |
||
165 | * @since 1.0.0 |
||
166 | */ |
||
167 | public function register_pages( $pages ) { |
||
168 | |||
169 | /** |
||
170 | * Filter settings pages to be created |
||
171 | * |
||
172 | * @param array $pages Page structures to be created |
||
173 | */ |
||
174 | |||
175 | $this->pages = apply_filters( $this->plugin_slug . '_set_admin_pages', $pages ); |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Register metaboxes |
||
180 | * |
||
181 | * @since 1.0.0 |
||
182 | */ |
||
183 | public function register_metaboxes( $metaboxes ) { |
||
186 | } |
||
187 | |||
188 | |||
189 | /** |
||
190 | * Add defined contextual help to admin page |
||
191 | * |
||
192 | * @since 1.0.0 |
||
193 | */ |
||
194 | public function add_help() { |
||
195 | |||
196 | |||
197 | $page = $this->get_page(); |
||
198 | |||
199 | if ( ! empty( $page['help'] ) ) { |
||
200 | |||
201 | $screen = get_current_screen(); |
||
202 | |||
203 | foreach ( (array) $page['help'] as $help_slug => $help ) { |
||
204 | |||
205 | if ( is_file( $help['content'] ) && file_exists( $help['content'] ) ) { |
||
206 | ob_start(); |
||
207 | include $help['content']; |
||
208 | $content = ob_get_clean(); |
||
209 | } else { |
||
210 | $content = $help['content']; |
||
211 | } |
||
212 | |||
213 | $screen->add_help_tab( array( |
||
214 | 'id' => $help_slug, |
||
215 | 'title' => $help['title'], |
||
216 | 'content' => $content, |
||
217 | ) ); |
||
218 | } |
||
219 | |||
220 | // Help sidebars are optional |
||
221 | if ( ! empty( $page['help_sidebar'] ) ) { |
||
222 | $screen->set_help_sidebar( $page['help_sidebar'] ); |
||
223 | } |
||
224 | } |
||
225 | |||
226 | } |
||
227 | |||
228 | |||
229 | /** |
||
230 | * Saves a config |
||
231 | * |
||
232 | * @uses "wp_ajax_uix_save_config" hook |
||
233 | * @since 0.0.1 |
||
234 | */ |
||
235 | public function save_config() { |
||
236 | |||
237 | if ( ! empty( $_POST['config'] ) ) { |
||
238 | |||
239 | $config = json_decode( stripslashes_deep( $_POST['config'] ), true ); |
||
240 | |||
241 | if ( wp_verify_nonce( $_POST['uix_setup'], $this->plugin_slug ) ) { |
||
242 | |||
243 | $page_slug = sanitize_text_field( $_POST['page_slug'] ); |
||
244 | |||
245 | if ( ! empty( $this->pages[ $page_slug ] ) ) { |
||
246 | $params = null; |
||
247 | if ( ! empty( $_POST['params'] ) ) { |
||
248 | $params = $_POST['params']; |
||
249 | } |
||
250 | /** |
||
251 | * Filter page settings pre save |
||
252 | * |
||
253 | * @param array $page_config the page config array |
||
254 | * @param array $params any defined save_params. |
||
255 | */ |
||
256 | $page = apply_filters( $this->plugin_slug . '_get_page_save', $this->pages[ $page_slug ], $params ); |
||
257 | |||
258 | /** |
||
259 | * Filter config object |
||
260 | * |
||
261 | * @param array $config the config array to save |
||
262 | * @param array $page the page config to be saved for |
||
263 | */ |
||
264 | $config = apply_filters( $this->plugin_slug . '_pre_save_config', $config, $page ); |
||
265 | |||
266 | |||
267 | $success = __( 'Settings saved.', $this->plugin_slug ); |
||
268 | if ( ! empty( $page['saved_message'] ) ) { |
||
269 | $success = $page['saved_message']; |
||
270 | } |
||
271 | $option_tag = '_' . $this->plugin_slug . '_' . $page_slug; |
||
272 | if ( ! empty( $page['option_name'] ) ) { |
||
273 | $option_tag = $page['option_name']; |
||
274 | } |
||
275 | // push backup if not autosave |
||
276 | if ( empty( $_POST['autosave'] ) ) { |
||
277 | $previous = get_option( $option_tag ); |
||
278 | if ( ! empty( $previous ) ) { |
||
279 | update_option( $option_tag . '-' . current_time( 'timestamp' ), $previous ); |
||
280 | } |
||
281 | } |
||
282 | // save object |
||
283 | update_option( $option_tag, $config ); |
||
284 | wp_send_json_success( $success ); |
||
285 | } |
||
286 | |||
287 | } else { |
||
288 | wp_send_json_error( esc_html__( 'Could not verify nonce', $this->plugin_slug ) ); |
||
289 | } |
||
290 | |||
291 | } |
||
292 | |||
293 | // nope |
||
294 | wp_send_json_error( esc_html__( 'Could not save, sorry.', $this->plugin_slug ) ); |
||
295 | } |
||
296 | |||
297 | |||
298 | /** |
||
299 | * Register and enqueue admin-specific style sheet. |
||
300 | * |
||
301 | * @since 1.0.0 |
||
302 | * @return null |
||
303 | */ |
||
304 | public function enqueue_admin_stylescripts() { |
||
305 | |||
306 | $uix = $this->get_page(); |
||
307 | if ( false === $uix ) { |
||
308 | return; |
||
309 | } |
||
310 | |||
311 | $uix['slug'] = $this->plugin_slug; |
||
312 | |||
313 | // allow for minimized scripts |
||
314 | $prefix = '.min'; |
||
315 | $uix_url = plugin_dir_url( __FILE__ ); |
||
316 | if ( defined( 'DEBUG_SCRIPTS' ) ) { |
||
317 | //$prefix = null; |
||
318 | } |
||
319 | // base styles |
||
320 | wp_enqueue_style( $this->plugin_slug . '-base-icons', $uix_url . 'assets/css/icons' . $prefix . '.css' ); |
||
321 | wp_enqueue_style( $this->plugin_slug . '-base-styles', $uix_url . 'assets/css/admin' . $prefix . '.css' ); |
||
322 | // enqueue scripts |
||
323 | wp_enqueue_script( 'handlebars', $uix_url . 'assets/js/handlebars.min-latest.js', array(), null, true ); |
||
324 | // if has modals |
||
325 | if ( ! empty( $uix['modals'] ) ) { |
||
326 | wp_enqueue_script( $this->plugin_slug . '-core-modals', $uix_url . 'assets/js/uix-modals' . $prefix . '.js', array( |
||
327 | 'jquery', |
||
328 | 'handlebars', |
||
329 | ), null, true ); |
||
330 | } |
||
331 | wp_enqueue_script( $this->plugin_slug . '-helpers', $uix_url . 'assets/js/uix-helpers' . $prefix . '.js', array( 'handlebars' ), null, true ); |
||
332 | wp_enqueue_script( $this->plugin_slug . '-core-admin', $uix_url . 'assets/js/uix-core' . $prefix . '.js', array( |
||
333 | 'jquery', |
||
334 | 'handlebars', |
||
335 | ), null, true ); |
||
336 | |||
337 | // enqueue admin runtime styles |
||
338 | $this->enqueue_set( $uix, $this->plugin_slug . '-' . $uix['page_slug'] ); |
||
339 | |||
340 | // enqueue tab specific runtime styles |
||
341 | if ( ! empty( $uix['tabs'] ) ) { |
||
342 | foreach ( $uix['tabs'] as $tab_slug => $tab ) { |
||
343 | //$this->enqueue_set( $tab, $this->plugin_slug . '-' . $uix['page_slug'] . '-' . $tab_slug ); |
||
344 | } |
||
345 | } |
||
346 | |||
347 | wp_localize_script( $this->plugin_slug . '-core-admin', 'uix', $uix ); |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * enqueue a set of styles and scripts |
||
352 | * |
||
353 | * @since 0.0.1 |
||
354 | */ |
||
355 | private function enqueue_set( $set, $prefix ) { |
||
393 | } |
||
394 | } |
||
395 | } |
||
396 | } |
||
397 | |||
398 | } |
||
399 | |||
400 | /** |
||
401 | * get the config for the current page |
||
402 | * |
||
403 | * @since 0.0.1 |
||
404 | * @return array $page array structure of current uix page |
||
405 | */ |
||
406 | private function get_page() { |
||
444 | } |
||
445 | |||
446 | /** |
||
447 | * Add options page |
||
448 | * |
||
449 | * @since 0.0.1 |
||
450 | * @uses "admin_menu" hook |
||
451 | */ |
||
452 | public function add_settings_pages() { |
||
453 | |||
454 | foreach ( (array) $this->pages as $page_slug => $page ) { |
||
455 | |||
456 | if ( empty( $page['page_title'] ) || empty( $page['menu_title'] ) ) { |
||
457 | continue; |
||
458 | } |
||
459 | |||
460 | $args = array( |
||
461 | 'capability' => 'manage_options', |
||
462 | 'icon' => null, |
||
463 | 'position' => null, |
||
464 | ); |
||
465 | $args = array_merge( $args, $page ); |
||
466 | |||
467 | if ( ! empty( $page['parent'] ) ) { |
||
468 | |||
469 | $this->plugin_screen_hook_suffix[ $page_slug ] = add_submenu_page( |
||
470 | $args['parent'], |
||
471 | $args['page_title'], |
||
472 | $args['menu_title'], |
||
473 | $args['capability'], |
||
474 | $this->plugin_slug . '-' . $page_slug, |
||
475 | array( $this, 'create_admin_page' ) |
||
476 | ); |
||
477 | |||
478 | } else { |
||
479 | |||
480 | $this->plugin_screen_hook_suffix[ $page_slug ] = add_menu_page( |
||
481 | $args['page_title'], |
||
482 | $args['menu_title'], |
||
483 | $args['capability'], |
||
484 | $this->plugin_slug . '-' . $page_slug, |
||
485 | array( $this, 'create_admin_page' ), |
||
486 | $args['icon'], |
||
487 | $args['position'] |
||
488 | ); |
||
489 | } |
||
490 | add_action( 'admin_print_styles-' . $this->plugin_screen_hook_suffix[ $page_slug ], array( |
||
491 | $this, |
||
492 | 'enqueue_admin_stylescripts', |
||
493 | ) ); |
||
494 | add_action( 'load-' . $this->plugin_screen_hook_suffix[ $page_slug ], array( |
||
495 | $this, |
||
496 | 'add_help', |
||
497 | ) ); |
||
498 | } |
||
499 | } |
||
500 | |||
501 | /** |
||
502 | * Options page callback |
||
503 | * |
||
504 | * @since 0.0.1 |
||
505 | */ |
||
506 | public function create_admin_page() { |
||
615 | |||
616 | <script type="text/html" data-template="__notice"> |
||
617 | <div class="{{#if success}}updated{{else}}error{{/if}} notice uix-notice is-dismissible"> |
||
618 | <p>{{{data}}}</p> |
||
619 | <button class="notice-dismiss" type="button"> |
||
651 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths