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:
| 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() { |
||
| 12 | $title = _x( 'Jetpack', 'The menu item label', 'jetpack' ); |
||
| 13 | |||
| 14 | // Add the main admin Jetpack menu |
||
| 15 | return add_menu_page( 'Jetpack', $title, 'jetpack_admin_page', 'jetpack', array( $this, 'render' ), 'div' ); |
||
| 16 | } |
||
| 17 | |||
| 18 | function add_page_actions( $hook ) { |
||
| 19 | // Add landing page specific underscore templates |
||
| 20 | /** |
||
| 21 | * Filters the js_templates callback value |
||
| 22 | * |
||
| 23 | * @since 3.6.0 |
||
| 24 | * |
||
| 25 | * @param array array( $this, 'js_templates' ) js_templates callback. |
||
| 26 | * @param string $hook Specific admin page. |
||
| 27 | */ |
||
| 28 | // @todo is that filter still relevant? |
||
|
|
|||
| 29 | // add_action( "admin_footer-$hook", apply_filters( 'jetpack_landing_page_js_templates_callback', array( $this, 'js_templates' ), $hook ) ); |
||
| 30 | |||
| 31 | /** This action is documented in class.jetpack.php */ |
||
| 32 | do_action( 'jetpack_admin_menu', $hook ); |
||
| 33 | |||
| 34 | // Place the Jetpack menu item on top and others in the order they |
||
| 35 | // appear |
||
| 36 | add_filter( 'custom_menu_order', '__return_true' ); |
||
| 37 | add_filter( 'menu_order', array( $this, 'jetpack_menu_order' ) ); |
||
| 38 | |||
| 39 | // add_action( 'jetpack_notices_update_settings', array( $this, 'show_notices_update_settings' ), 10, 1 ); |
||
| 40 | |||
| 41 | if ( ! isset( $_GET['page'] ) || 'jetpack' !== $_GET['page'] || ! empty( $_GET['configure'] ) ) { |
||
| 42 | return; // No need to handle the fallback redirection if we are not on the Jetpack page |
||
| 43 | } |
||
| 44 | |||
| 45 | // Adding a redirect meta tag for older WordPress versions |
||
| 46 | if ( $this->is_wp_version_too_old() ) { |
||
| 47 | $this->is_redirecting = true; |
||
| 48 | add_action( 'admin_head', array( $this, 'add_fallback_head_meta' ) ); |
||
| 49 | } |
||
| 50 | |||
| 51 | // Adding a redirect meta tag wrapped in noscript tags for all browsers in case they have |
||
| 52 | // JavaScript disabled |
||
| 53 | add_action( 'admin_head', array( $this, 'add_noscript_head_meta' ) ); |
||
| 54 | } |
||
| 55 | |||
| 56 | function jetpack_add_dashboard_sub_nav_item() { |
||
| 57 | global $submenu; |
||
| 58 | if ( current_user_can( 'jetpack_manage_modules' ) || ( Jetpack::is_module_active( 'protect' ) || current_user_can( 'view_stats' ) ) ) { |
||
| 59 | $permalink = Jetpack::admin_url( 'page=jetpack#/dashboard' ); |
||
| 60 | } else { |
||
| 61 | $permalink = Jetpack::admin_url( 'page=jetpack#/apps' ); |
||
| 62 | } |
||
| 63 | $submenu['jetpack'][] = array( __( 'Dashboard', 'jetpack' ), 'jetpack_admin_page', $permalink ); |
||
| 64 | } |
||
| 65 | |||
| 66 | function jetpack_add_settings_sub_nav_item() { |
||
| 67 | global $submenu; |
||
| 68 | $permalink = Jetpack::admin_url( 'page=jetpack#/settings' ); |
||
| 69 | $submenu['jetpack'][] = array( __( 'Settings', 'jetpack' ), 'jetpack_admin_page', $permalink ); |
||
| 70 | } |
||
| 71 | |||
| 72 | function add_fallback_head_meta() { |
||
| 73 | echo '<meta http-equiv="refresh" content="0; url=?page=jetpack_modules">'; |
||
| 74 | } |
||
| 75 | |||
| 76 | function add_noscript_head_meta() { |
||
| 77 | echo '<noscript>'; |
||
| 78 | $this->add_fallback_head_meta(); |
||
| 79 | echo '</noscript>'; |
||
| 80 | } |
||
| 81 | |||
| 82 | View Code Duplication | function jetpack_menu_order( $menu_order ) { |
|
| 83 | $jp_menu_order = array(); |
||
| 84 | |||
| 85 | foreach ( $menu_order as $index => $item ) { |
||
| 86 | if ( $item != 'jetpack' ) |
||
| 87 | $jp_menu_order[] = $item; |
||
| 88 | |||
| 89 | if ( $index == 0 ) |
||
| 90 | $jp_menu_order[] = 'jetpack'; |
||
| 91 | } |
||
| 92 | |||
| 93 | return $jp_menu_order; |
||
| 94 | } |
||
| 95 | |||
| 96 | // Render the configuration page for the module if it exists and an error |
||
| 97 | // screen if the module is not configurable |
||
| 98 | // @todo remove when real settings are in place |
||
| 99 | View Code Duplication | function render_nojs_configurable( $module_name ) { |
|
| 100 | $module_name = preg_replace( '/[^\da-z\-]+/', '', $_GET['configure'] ); |
||
| 101 | |||
| 102 | include_once( JETPACK__PLUGIN_DIR . '_inc/header.php' ); |
||
| 103 | echo '<div class="clouds-sm"></div>'; |
||
| 104 | echo '<div class="wrap configure-module">'; |
||
| 105 | |||
| 106 | if ( Jetpack::is_module( $module_name ) && current_user_can( 'jetpack_configure_modules' ) ) { |
||
| 107 | Jetpack::admin_screen_configure_module( $module_name ); |
||
| 108 | } else { |
||
| 109 | echo '<h2>' . esc_html__( 'Error, bad module.', 'jetpack' ) . '</h2>'; |
||
| 110 | } |
||
| 111 | |||
| 112 | echo '</div><!-- /wrap -->'; |
||
| 113 | } |
||
| 114 | |||
| 115 | function page_render() { |
||
| 116 | // Handle redirects to configuration pages |
||
| 117 | if ( ! empty( $_GET['configure'] ) ) { |
||
| 118 | return $this->render_nojs_configurable( $_GET['configure'] ); |
||
| 119 | } |
||
| 120 | ?> |
||
| 121 | <?php |
||
| 122 | /** This action is already documented in views/admin/admin-page.php */ |
||
| 123 | do_action( 'jetpack_notices' ); |
||
| 124 | ?> |
||
| 125 | <div id="jp-plugin-container"></div> |
||
| 126 | <?php } |
||
| 127 | |||
| 128 | function get_i18n_data() { |
||
| 129 | $locale_data = @file_get_contents( JETPACK__PLUGIN_DIR . '/languages/json/jetpack-' . get_locale() . '.json' ); |
||
| 130 | if ( $locale_data ) { |
||
| 131 | return $locale_data; |
||
| 132 | } else { |
||
| 133 | return '{}'; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Gets array of any Jetpack notices that have been dismissed. |
||
| 139 | * |
||
| 140 | * @since 4.0.1 |
||
| 141 | * @return mixed|void |
||
| 142 | */ |
||
| 143 | function get_dismissed_jetpack_notices() { |
||
| 155 | |||
| 156 | function page_admin_scripts() { |
||
| 157 | if ( $this->is_redirecting ) { |
||
| 158 | return; // No need for scripts on a fallback page |
||
| 159 | } |
||
| 160 | |||
| 161 | // Enqueue jp.js and localize it |
||
| 162 | wp_enqueue_script( 'react-plugin', plugins_url( '_inc/build/admin.js', JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION, true ); |
||
| 163 | wp_enqueue_style( 'dops-css', plugins_url( '_inc/build/admin.dops-style.css', JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION ); |
||
| 164 | wp_enqueue_style( 'components-css', plugins_url( '_inc/build/style.min.css', JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION ); |
||
| 165 | |||
| 228 | } |
||
| 229 | |||
| 384 |