Total Complexity | 64 |
Total Lines | 412 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like MonsterInsights_Admin_Assets 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 MonsterInsights_Admin_Assets, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class MonsterInsights_Admin_Assets { |
||
13 | /** |
||
14 | * MonsterInsights handles. |
||
15 | */ |
||
16 | private $own_handles = array( |
||
17 | 'monsterinsights-vue-script', |
||
18 | 'monsterinsights-vue-frontend', |
||
19 | 'monsterinsights-vue-reports', |
||
20 | 'monsterinsights-vue-widget', |
||
21 | ); |
||
22 | |||
23 | /** |
||
24 | * Store manifest.json file content. |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | private static $manifest_data; |
||
29 | |||
30 | /** |
||
31 | * Class constructor. |
||
32 | */ |
||
33 | public function __construct() { |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Update script tag. |
||
45 | * The vue code needs type=module. |
||
46 | */ |
||
47 | public function script_loader_tag( $tag, $handle, $src ) { |
||
48 | |||
49 | if ( ! in_array( $handle, $this->own_handles ) ) { |
||
50 | return $tag; |
||
51 | } |
||
52 | |||
53 | // Change the script tag by adding type="module" and return it. |
||
54 | $html = str_replace( '></script>', ' type="module"></script>', $tag ); |
||
55 | |||
56 | $domain = monsterinsights_is_pro_version() ? 'ga-premium' : 'google-analytics-for-wordpress'; |
||
57 | $html = monsterinsights_get_printable_translations( $domain ) . $html; |
||
58 | |||
59 | return $html; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Loads styles for all MonsterInsights-based Administration Screens. |
||
64 | * |
||
65 | * @return null Return early if not on the proper screen. |
||
66 | */ |
||
67 | public function admin_styles() { |
||
68 | |||
69 | $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; |
||
70 | |||
71 | // Load Common admin styles. |
||
72 | wp_register_style( 'monsterinsights-admin-common-style', plugins_url( 'assets/css/admin-common' . $suffix . '.css', MONSTERINSIGHTS_PLUGIN_FILE ), array(), monsterinsights_get_asset_version() ); |
||
73 | wp_enqueue_style( 'monsterinsights-admin-common-style' ); |
||
74 | |||
75 | // Get current screen. |
||
76 | $screen = get_current_screen(); |
||
|
|||
77 | |||
78 | // Bail if we're not on a MonsterInsights screen. |
||
79 | if ( empty( $screen->id ) || strpos( $screen->id, 'monsterinsights' ) === false ) { |
||
80 | return; |
||
81 | } |
||
82 | |||
83 | // For the settings pages, load the Vue app scripts. |
||
84 | if ( monsterinsights_is_settings_page() ) { |
||
85 | if ( ! defined( 'MONSTERINSIGHTS_LOCAL_JS_URL' ) ) { |
||
86 | +$this->enqueue_script_specific_css( 'src/modules/settings/settings.js' ); |
||
87 | } |
||
88 | |||
89 | // Don't load other scripts on the settings page. |
||
90 | return; |
||
91 | } |
||
92 | |||
93 | // For the report pages, load the Vue app scripts. |
||
94 | if ( monsterinsights_is_reports_page() ) { |
||
95 | if ( ! defined( 'MONSTERINSIGHTS_LOCAL_JS_URL' ) ) { |
||
96 | $this->enqueue_script_specific_css( 'src/modules/reports/reports.js' ); |
||
97 | } |
||
98 | |||
99 | return; |
||
100 | } |
||
101 | |||
102 | // Tooltips |
||
103 | wp_enqueue_script( 'jquery-ui-tooltip' ); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Loads scripts for all MonsterInsights-based Administration Screens. |
||
108 | * |
||
109 | * @return null Return early if not on the proper screen. |
||
110 | */ |
||
111 | public function admin_scripts() { |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * Need to identify why this function is using. |
||
297 | */ |
||
298 | public function monsterinsights_settings_ublock_error_js() { |
||
299 | echo "<script type='text/javascript'>\n"; |
||
300 | echo "jQuery( document ).ready( function( $ ) { |
||
301 | if ( window.uorigindetected == null){ |
||
302 | $('#monsterinsights-ublock-origin-error').show(); |
||
303 | $('.monsterinsights-nav-tabs').hide(); |
||
304 | $('.monsterinsights-nav-container').hide(); |
||
305 | $('#monsterinsights-addon-heading').hide(); |
||
306 | $('#monsterinsights-addons').hide(); |
||
307 | $('#monsterinsights-reports').hide(); |
||
308 | } |
||
309 | });"; |
||
310 | echo "\n</script>"; |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * Load CSS from manifest.json |
||
315 | */ |
||
316 | public static function enqueue_script_specific_css( $js_file_path ) { |
||
317 | if ( defined( 'MONSTERINSIGHTS_LOCAL_JS_URL' ) ) { |
||
318 | return; |
||
319 | } |
||
320 | |||
321 | $version_path = monsterinsights_is_pro_version() ? 'pro' : 'lite'; |
||
322 | $plugin_path = plugin_dir_path( MONSTERINSIGHTS_PLUGIN_FILE ); |
||
323 | |||
324 | if ( ! isset( self::$manifest_data[ $js_file_path ] ) ) { |
||
325 | return; |
||
326 | } |
||
327 | |||
328 | $js_imports = self::$manifest_data[ $js_file_path ]['imports']; |
||
329 | $css_file_path = $plugin_path . $version_path . '/assets/vue/'; |
||
330 | |||
331 | // Add JS own CSS file. |
||
332 | if ( isset( self::$manifest_data[ $js_file_path ]['css'] ) ) { |
||
333 | self::add_js_own_css_files( self::$manifest_data[ $js_file_path ]['css'], $version_path ); |
||
334 | } |
||
335 | |||
336 | // Loop through all imported js file of entry file. |
||
337 | foreach ( $js_imports as $js_filename ) { |
||
338 | // Check imported file available in manifest.json |
||
339 | if ( ! isset( self::$manifest_data[ $js_filename ] ) ) { |
||
340 | continue; |
||
341 | } |
||
342 | |||
343 | // Check imported js file has it's own css. |
||
344 | if ( ! isset( self::$manifest_data[ $js_filename ]['css'] ) ) { |
||
345 | continue; |
||
346 | } |
||
347 | |||
348 | $js_file_css = self::$manifest_data[ $js_filename ]['css']; |
||
349 | |||
350 | // css must be array. |
||
351 | if ( ! is_array( $js_file_css ) ) { |
||
352 | continue; |
||
353 | } |
||
354 | |||
355 | // Loop to css files of a imported js file. |
||
356 | foreach ( $js_file_css as $css_hash_name ) { |
||
357 | if ( file_exists( $css_file_path . $css_hash_name ) ) { |
||
358 | wp_enqueue_style( |
||
359 | 'monsterinsights-style-' . basename( $css_hash_name ), |
||
360 | plugins_url( $version_path . '/assets/vue/' . $css_hash_name, MONSTERINSIGHTS_PLUGIN_FILE ), |
||
361 | array(), |
||
362 | monsterinsights_get_asset_version() |
||
363 | ); |
||
364 | } |
||
365 | } |
||
366 | } |
||
367 | } |
||
368 | |||
369 | /** |
||
370 | * Add JS it's own CSS build file. |
||
371 | */ |
||
372 | private static function add_js_own_css_files( $css_files, $version_path ) { |
||
379 | ); |
||
380 | } |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * Get JS build file URL of a entry file. |
||
385 | * |
||
386 | * @return string |
||
387 | */ |
||
388 | public static function get_js_url( $path ) { |
||
389 | if ( ! $path ) { |
||
390 | return; |
||
391 | } |
||
392 | |||
393 | if ( defined( 'MONSTERINSIGHTS_LOCAL_JS_URL' ) && MONSTERINSIGHTS_LOCAL_JS_URL ) { |
||
394 | return MONSTERINSIGHTS_LOCAL_JS_URL . $path; |
||
395 | } |
||
396 | |||
397 | // If the file is not available on manifest. |
||
398 | if ( ! isset( self::$manifest_data[ $path ] ) ) { |
||
399 | return; |
||
400 | } |
||
401 | |||
402 | $js_file = self::$manifest_data[ $path ]['file']; |
||
403 | $version_path = monsterinsights_is_pro_version() ? 'pro' : 'lite'; |
||
404 | |||
405 | return plugins_url( $version_path . '/assets/vue/' . $js_file, MONSTERINSIGHTS_PLUGIN_FILE ); |
||
406 | } |
||
407 | |||
408 | /** |
||
409 | * Fetch manifest.json data and store it to array for future use. |
||
410 | * |
||
411 | * @return void |
||
412 | */ |
||
413 | private function get_manifest_data() { |
||
424 | } |
||
425 | } |
||
426 | |||
427 | new MonsterInsights_Admin_Assets(); |
||
428 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.