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 | * |
||
46 | * Works in Dev Mode or when user is connected. |
||
47 | * |
||
48 | * @since 4.3.0 |
||
49 | */ |
||
50 | function jetpack_add_dashboard_sub_nav_item() { |
||
51 | View Code Duplication | if ( Jetpack::is_development_mode() || Jetpack::is_active() ) { |
|
52 | global $submenu; |
||
53 | if ( current_user_can( 'jetpack_admin_page' ) ) { |
||
54 | $submenu['jetpack'][] = array( __( 'Dashboard', 'jetpack' ), 'jetpack_admin_page', 'admin.php?page=jetpack#/dashboard' ); |
||
55 | } |
||
56 | } |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * If user is allowed to see the Jetpack Admin, add Settings sub-link. |
||
61 | * |
||
62 | * @since 4.3.0 |
||
63 | */ |
||
64 | function jetpack_add_settings_sub_nav_item() { |
||
65 | View Code Duplication | if ( ( Jetpack::is_development_mode() || Jetpack::is_active() ) && current_user_can( 'jetpack_admin_page' ) && current_user_can( 'edit_posts' ) ) { |
|
66 | global $submenu; |
||
67 | $submenu['jetpack'][] = array( __( 'Settings', 'jetpack' ), 'jetpack_admin_page', 'admin.php?page=jetpack#/settings' ); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | function add_fallback_head_meta() { |
||
74 | |||
75 | function add_noscript_head_meta() { |
||
80 | |||
81 | function add_legacy_browsers_head_script() { |
||
82 | echo |
||
83 | "<script type=\"text/javascript\">\n" |
||
84 | . "/*@cc_on\n" |
||
85 | . "if ( @_jscript_version <= 10) {\n" |
||
86 | . "window.location.href = '?page=jetpack_modules';\n" |
||
87 | . "}\n" |
||
88 | . "@*/\n" |
||
89 | . "</script>"; |
||
90 | } |
||
91 | |||
92 | View Code Duplication | function jetpack_menu_order( $menu_order ) { |
|
93 | $jp_menu_order = array(); |
||
94 | |||
95 | foreach ( $menu_order as $index => $item ) { |
||
96 | if ( $item != 'jetpack' ) |
||
97 | $jp_menu_order[] = $item; |
||
98 | |||
99 | if ( $index == 0 ) |
||
100 | $jp_menu_order[] = 'jetpack'; |
||
101 | } |
||
102 | |||
103 | return $jp_menu_order; |
||
104 | } |
||
105 | |||
106 | // Render the configuration page for the module if it exists and an error |
||
107 | // screen if the module is not configurable |
||
108 | // @todo remove when real settings are in place |
||
|
|||
109 | function render_nojs_configurable( $module_name ) { |
||
110 | $module_name = preg_replace( '/[^\da-z\-]+/', '', $_GET['configure'] ); |
||
111 | |||
112 | include_once( JETPACK__PLUGIN_DIR . '_inc/header.php' ); |
||
113 | echo '<div class="wrap configure-module">'; |
||
114 | |||
115 | if ( Jetpack::is_module( $module_name ) && current_user_can( 'jetpack_configure_modules' ) ) { |
||
116 | Jetpack::admin_screen_configure_module( $module_name ); |
||
117 | } else { |
||
118 | echo '<h2>' . esc_html__( 'Error, bad module.', 'jetpack' ) . '</h2>'; |
||
119 | } |
||
120 | |||
121 | echo '</div><!-- /wrap -->'; |
||
122 | } |
||
123 | |||
124 | function page_render() { |
||
125 | // Handle redirects to configuration pages |
||
126 | if ( ! empty( $_GET['configure'] ) ) { |
||
127 | return $this->render_nojs_configurable( $_GET['configure'] ); |
||
128 | } |
||
129 | |||
130 | /** This action is already documented in views/admin/admin-page.php */ |
||
131 | do_action( 'jetpack_notices' ); |
||
132 | |||
133 | // Try fetching by patch |
||
134 | $static_html = @file_get_contents( JETPACK__PLUGIN_DIR . '_inc/build/static.html' ); |
||
135 | |||
136 | View Code Duplication | if ( false === $static_html ) { |
|
137 | |||
138 | // If we still have nothing, display an error |
||
139 | echo '<p>'; |
||
140 | esc_html_e( 'Error fetching static.html. Try running: ', 'jetpack' ); |
||
141 | echo '<code>yarn distclean && yarn build</code>'; |
||
142 | echo '</p>'; |
||
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() { |
||
190 | |||
191 | function page_admin_scripts() { |
||
192 | if ( $this->is_redirecting ) { |
||
193 | return; // No need for scripts on a fallback page |
||
194 | } |
||
195 | |||
196 | $is_dev_mode = Jetpack::is_development_mode(); |
||
197 | |||
198 | // Enqueue jp.js and localize it |
||
199 | wp_enqueue_script( 'react-plugin', plugins_url( '_inc/build/admin.js', JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION, true ); |
||
200 | |||
201 | if ( ! $is_dev_mode && Jetpack::is_active() ) { |
||
202 | // Required for Analytics |
||
203 | wp_enqueue_script( 'jp-tracks', '//stats.wp.com/w.js', array(), gmdate( 'YW' ), true ); |
||
204 | } |
||
205 | |||
206 | // Collecting roles that can view site stats. |
||
207 | $stats_roles = array(); |
||
208 | $enabled_roles = function_exists( 'stats_get_option' ) ? stats_get_option( 'roles' ) : array( 'administrator' ); |
||
209 | |||
210 | if ( ! function_exists( 'get_editable_roles' ) ) { |
||
211 | require_once ABSPATH . 'wp-admin/includes/user.php'; |
||
212 | } |
||
213 | foreach ( get_editable_roles() as $slug => $role ) { |
||
214 | $stats_roles[ $slug ] = array( |
||
215 | 'name' => translate_user_role( $role['name'] ), |
||
216 | 'canView' => is_array( $enabled_roles ) ? in_array( $slug, $enabled_roles, true ) : false, |
||
217 | ); |
||
218 | } |
||
219 | |||
220 | // Load API endpoint base classes and endpoints for getting the module list fed into the JS Admin Page |
||
221 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/core-api/class.jetpack-core-api-xmlrpc-consumer-endpoint.php'; |
||
222 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php'; |
||
223 | $moduleListEndpoint = new Jetpack_Core_API_Module_List_Endpoint(); |
||
224 | $modules = $moduleListEndpoint->get_modules(); |
||
225 | |||
226 | // Preparing translated fields for JSON encoding by transforming all HTML entities to |
||
227 | // respective characters. |
||
228 | foreach( $modules as $slug => $data ) { |
||
229 | $modules[ $slug ]['name'] = html_entity_decode( $data['name'] ); |
||
230 | $modules[ $slug ]['description'] = html_entity_decode( $data['description'] ); |
||
231 | $modules[ $slug ]['short_description'] = html_entity_decode( $data['short_description'] ); |
||
232 | $modules[ $slug ]['long_description'] = html_entity_decode( $data['long_description'] ); |
||
233 | } |
||
234 | |||
235 | // Get last post, to build the link to Customizer in the Related Posts module. |
||
236 | $last_post = get_posts( array( 'posts_per_page' => 1 ) ); |
||
237 | $last_post = isset( $last_post[0] ) && $last_post[0] instanceof WP_Post |
||
238 | ? get_permalink( $last_post[0]->ID ) |
||
239 | : get_home_url(); |
||
240 | |||
241 | // Get information about current theme. |
||
242 | $current_theme = wp_get_theme(); |
||
243 | |||
244 | // Get all themes that Infinite Scroll provides support for natively. |
||
245 | $inf_scr_support_themes = array(); |
||
246 | foreach ( Jetpack::glob_php( JETPACK__PLUGIN_DIR . 'modules/infinite-scroll/themes' ) as $path ) { |
||
247 | if ( is_readable( $path ) ) { |
||
248 | $inf_scr_support_themes[] = basename( $path, '.php' ); |
||
249 | } |
||
250 | } |
||
251 | |||
252 | // Add objects to be passed to the initial state of the app |
||
253 | wp_localize_script( 'react-plugin', 'Initial_State', array( |
||
254 | 'WP_API_root' => esc_url_raw( rest_url() ), |
||
255 | 'WP_API_nonce' => wp_create_nonce( 'wp_rest' ), |
||
256 | 'pluginBaseUrl' => plugins_url( '', JETPACK__PLUGIN_FILE ), |
||
257 | 'connectionStatus' => array( |
||
258 | 'isActive' => Jetpack::is_active(), |
||
259 | 'isStaging' => Jetpack::is_staging_site(), |
||
260 | 'devMode' => array( |
||
261 | 'isActive' => $is_dev_mode, |
||
262 | 'constant' => defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG, |
||
263 | 'url' => site_url() && false === strpos( site_url(), '.' ), |
||
264 | 'filter' => apply_filters( 'jetpack_development_mode', false ), |
||
265 | ), |
||
266 | 'isPublic' => '1' == get_option( 'blog_public' ), |
||
267 | 'isInIdentityCrisis' => Jetpack::validate_sync_error_idc_option(), |
||
268 | ), |
||
269 | 'connectUrl' => Jetpack::init()->build_connect_url( true, false, false ), |
||
270 | 'dismissedNotices' => $this->get_dismissed_jetpack_notices(), |
||
271 | 'isDevVersion' => Jetpack::is_development_version(), |
||
272 | 'currentVersion' => JETPACK__VERSION, |
||
273 | 'getModules' => $modules, |
||
274 | 'showJumpstart' => jetpack_show_jumpstart(), |
||
275 | 'rawUrl' => Jetpack::build_raw_urls( get_home_url() ), |
||
276 | 'adminUrl' => esc_url( admin_url() ), |
||
277 | 'stats' => array( |
||
278 | // data is populated asynchronously on page load |
||
279 | 'data' => array( |
||
280 | 'general' => false, |
||
281 | 'day' => false, |
||
282 | 'week' => false, |
||
283 | 'month' => false, |
||
284 | ), |
||
285 | 'roles' => $stats_roles, |
||
286 | ), |
||
287 | 'settings' => $this->get_flattened_settings( $modules ), |
||
288 | 'userData' => array( |
||
289 | // 'othersLinked' => Jetpack::get_other_linked_admins(), |
||
290 | 'currentUser' => jetpack_current_user_data(), |
||
291 | ), |
||
292 | 'siteData' => array( |
||
293 | 'icon' => has_site_icon() |
||
294 | ? apply_filters( 'jetpack_photon_url', get_site_icon_url(), array( 'w' => 64 ) ) |
||
295 | : '', |
||
296 | 'siteVisibleToSearchEngines' => '1' == get_option( 'blog_public' ), |
||
297 | /** |
||
298 | * Whether promotions are visible or not. |
||
299 | * |
||
300 | * @since 4.8.0 |
||
301 | * |
||
302 | * @param bool $are_promotions_active Status of promotions visibility. True by default. |
||
303 | */ |
||
304 | 'showPromotions' => apply_filters( 'jetpack_show_promotions', true ), |
||
305 | 'isAtomicSite' => jetpack_is_atomic_site(), |
||
306 | ), |
||
307 | 'themeData' => array( |
||
308 | 'name' => $current_theme->get( 'Name' ), |
||
309 | 'hasUpdate' => (bool) get_theme_update_available( $current_theme ), |
||
310 | 'support' => array( |
||
311 | 'infinite-scroll' => current_theme_supports( 'infinite-scroll' ) || in_array( $current_theme->get_stylesheet(), $inf_scr_support_themes ), |
||
312 | ), |
||
313 | ), |
||
314 | 'locale' => $this->get_i18n_data(), |
||
315 | 'localeSlug' => join( '-', explode( '_', jetpack_get_user_locale() ) ), |
||
316 | 'jetpackStateNotices' => array( |
||
317 | 'messageCode' => Jetpack::state( 'message' ), |
||
318 | 'errorCode' => Jetpack::state( 'error' ), |
||
319 | 'errorDescription' => Jetpack::state( 'error_description' ), |
||
320 | ), |
||
321 | 'tracksUserData' => Jetpack_Tracks_Client::get_connected_user_tracks_identity(), |
||
322 | 'currentIp' => function_exists( 'jetpack_protect_get_ip' ) ? jetpack_protect_get_ip() : false, |
||
323 | 'lastPostUrl' => esc_url( $last_post ), |
||
324 | ) ); |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * Returns an array of modules and settings both as first class members of the object. |
||
329 | * |
||
330 | * @param array $modules the result of an API request to get all modules. |
||
331 | * |
||
332 | * @return array flattened settings with modules. |
||
333 | */ |
||
334 | function get_flattened_settings( $modules ) { |
||
339 | } |
||
340 | |||
435 |