Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Jetpack_React_Page 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Jetpack_React_Page, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class Jetpack_React_Page extends Jetpack_Admin_Page { |
||
6 | |||
7 | protected $dont_show_if_not_active = false; |
||
8 | |||
9 | protected $is_redirecting = false; |
||
10 | |||
11 | function get_page_hook() { |
||
17 | |||
18 | function add_page_actions( $hook ) { |
||
19 | /** This action is documented in class.jetpack.php */ |
||
20 | do_action( 'jetpack_admin_menu', $hook ); |
||
21 | |||
22 | // Place the Jetpack menu item on top and others in the order they appear |
||
23 | add_filter( 'custom_menu_order', '__return_true' ); |
||
24 | add_filter( 'menu_order', array( $this, 'jetpack_menu_order' ) ); |
||
25 | |||
26 | if ( ! isset( $_GET['page'] ) || 'jetpack' !== $_GET['page'] || ! empty( $_GET['configure'] ) ) { |
||
27 | return; // No need to handle the fallback redirection if we are not on the Jetpack page |
||
28 | } |
||
29 | |||
30 | // Adding a redirect meta tag for older WordPress versions or if the REST API is disabled |
||
31 | if ( $this->is_wp_version_too_old() || ! $this->is_rest_api_enabled() ) { |
||
32 | $this->is_redirecting = true; |
||
33 | add_action( 'admin_head', array( $this, 'add_fallback_head_meta' ) ); |
||
34 | } |
||
35 | |||
36 | // Adding a redirect meta tag wrapped in noscript tags for all browsers in case they have JavaScript disabled |
||
37 | add_action( 'admin_head', array( $this, 'add_noscript_head_meta' ) ); |
||
38 | |||
39 | // Adding a redirect tag wrapped in browser conditional comments |
||
40 | add_action( 'admin_head', array( $this, 'add_legacy_browsers_head_script' ) ); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Add Jetpack Dashboard sub-link and point it to AAG if the user can view stats, manage modules or if Protect is active. |
||
45 | * Otherwise and only if user is allowed to see the Jetpack Admin, the Dashboard sub-link is added but pointed to Apps tab. |
||
46 | * |
||
47 | * Works in Dev Mode or when user is connected. |
||
48 | * |
||
49 | * @since 4.3.0 |
||
50 | */ |
||
51 | function jetpack_add_dashboard_sub_nav_item() { |
||
52 | if ( Jetpack::is_development_mode() || Jetpack::is_active() ) { |
||
53 | global $submenu; |
||
54 | if ( current_user_can( 'jetpack_manage_modules' ) || Jetpack::is_module_active( 'protect' ) || current_user_can( 'view_stats' ) ) { |
||
55 | $submenu['jetpack'][] = array( __( 'Dashboard', 'jetpack' ), 'jetpack_admin_page', Jetpack::admin_url( 'page=jetpack#/dashboard' ) ); |
||
56 | } elseif ( current_user_can( 'jetpack_admin_page' ) ) { |
||
57 | $submenu['jetpack'][] = array( __( 'Dashboard', 'jetpack' ), 'jetpack_admin_page', Jetpack::admin_url( 'page=jetpack#/apps' ) ); |
||
58 | } |
||
59 | } |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * If user is allowed to see the Jetpack Admin, add Settings sub-link. |
||
64 | * |
||
65 | * @since 4.3.0 |
||
66 | */ |
||
67 | function jetpack_add_settings_sub_nav_item() { |
||
68 | if ( ( Jetpack::is_development_mode() || Jetpack::is_active() ) && current_user_can( 'jetpack_admin_page' ) && current_user_can( 'edit_posts' ) ) { |
||
69 | global $submenu; |
||
70 | $submenu['jetpack'][] = array( __( 'Settings', 'jetpack' ), 'jetpack_admin_page', Jetpack::admin_url( 'page=jetpack#/settings' ) ); |
||
71 | } |
||
72 | } |
||
73 | |||
74 | function add_fallback_head_meta() { |
||
75 | echo '<meta http-equiv="refresh" content="0; url=?page=jetpack_modules">'; |
||
76 | } |
||
77 | |||
78 | function add_noscript_head_meta() { |
||
79 | echo '<noscript>'; |
||
80 | $this->add_fallback_head_meta(); |
||
81 | echo '</noscript>'; |
||
82 | } |
||
83 | |||
84 | function add_legacy_browsers_head_script() { |
||
85 | echo |
||
86 | "<script type=\"text/javascript\">\n" |
||
87 | . "/*@cc_on\n" |
||
88 | . "if ( @_jscript_version <= 10) {\n" |
||
89 | . "window.location.href = '?page=jetpack_modules';\n" |
||
90 | . "}\n" |
||
91 | . "@*/\n" |
||
92 | . "</script>"; |
||
93 | } |
||
94 | |||
95 | View Code Duplication | function jetpack_menu_order( $menu_order ) { |
|
96 | $jp_menu_order = array(); |
||
97 | |||
98 | foreach ( $menu_order as $index => $item ) { |
||
99 | if ( $item != 'jetpack' ) |
||
100 | $jp_menu_order[] = $item; |
||
101 | |||
102 | if ( $index == 0 ) |
||
103 | $jp_menu_order[] = 'jetpack'; |
||
104 | } |
||
105 | |||
106 | return $jp_menu_order; |
||
107 | } |
||
108 | |||
109 | // Render the configuration page for the module if it exists and an error |
||
110 | // screen if the module is not configurable |
||
111 | // @todo remove when real settings are in place |
||
|
|||
112 | function render_nojs_configurable( $module_name ) { |
||
113 | $module_name = preg_replace( '/[^\da-z\-]+/', '', $_GET['configure'] ); |
||
114 | |||
115 | include_once( JETPACK__PLUGIN_DIR . '_inc/header.php' ); |
||
116 | echo '<div class="wrap configure-module">'; |
||
117 | |||
118 | if ( Jetpack::is_module( $module_name ) && current_user_can( 'jetpack_configure_modules' ) ) { |
||
119 | Jetpack::admin_screen_configure_module( $module_name ); |
||
120 | } else { |
||
121 | echo '<h2>' . esc_html__( 'Error, bad module.', 'jetpack' ) . '</h2>'; |
||
122 | } |
||
123 | |||
124 | echo '</div><!-- /wrap -->'; |
||
125 | } |
||
126 | |||
127 | function page_render() { |
||
128 | // Handle redirects to configuration pages |
||
129 | if ( ! empty( $_GET['configure'] ) ) { |
||
130 | return $this->render_nojs_configurable( $_GET['configure'] ); |
||
131 | } |
||
132 | |||
133 | /** This action is already documented in views/admin/admin-page.php */ |
||
134 | do_action( 'jetpack_notices' ); |
||
135 | |||
136 | // Try fetching by patch |
||
137 | $static_html = @file_get_contents( JETPACK__PLUGIN_DIR . '_inc/build/static.html' ); |
||
138 | |||
139 | if ( false === $static_html ) { |
||
140 | |||
141 | // If we still have nothing, display an error |
||
142 | esc_html_e( 'Error fetching static.html.', 'jetpack' ); |
||
143 | } else { |
||
144 | |||
145 | // We got the static.html so let's display it |
||
146 | echo $static_html; |
||
147 | } |
||
148 | } |
||
149 | |||
150 | function get_i18n_data() { |
||
151 | |||
152 | $i18n_json = JETPACK__PLUGIN_DIR . 'languages/json/jetpack-' . jetpack_get_user_locale() . '.json'; |
||
153 | |||
154 | if ( is_file( $i18n_json ) && is_readable( $i18n_json ) ) { |
||
155 | $locale_data = @file_get_contents( $i18n_json ); |
||
156 | if ( $locale_data ) { |
||
157 | return $locale_data; |
||
158 | } |
||
159 | } |
||
160 | |||
161 | // Return empty if we have nothing to return so it doesn't fail when parsed in JS |
||
162 | return '{}'; |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Gets array of any Jetpack notices that have been dismissed. |
||
167 | * |
||
168 | * @since 4.0.1 |
||
169 | * @return mixed|void |
||
170 | */ |
||
171 | function get_dismissed_jetpack_notices() { |
||
183 | |||
184 | function additional_styles() { |
||
185 | $rtl = is_rtl() ? '.rtl' : ''; |
||
186 | |||
187 | wp_enqueue_style( 'dops-css', plugins_url( "_inc/build/admin.dops-style$rtl.css", JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION ); |
||
188 | wp_enqueue_style( 'components-css', plugins_url( "_inc/build/style.min$rtl.css", JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION ); |
||
189 | } |
||
190 | |||
191 | function page_admin_scripts() { |
||
192 | if ( $this->is_redirecting ) { |
||
193 | return; // No need for scripts on a fallback page |
||
298 | |||
299 | /** |
||
300 | * Returns an array of modules and settings both as first class members of the object. |
||
301 | * |
||
302 | * @param array $modules the result of an API request to get all modules. |
||
303 | * |
||
304 | * @return array flattened settings with modules. |
||
305 | */ |
||
306 | function get_flattened_settings( $modules ) { |
||
311 | } |
||
312 | |||
437 |