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 | /** |
||
| 57 | * Add Jetpack Dashboard sub-link and point it to AAG if the user can view stats, manage modules or if Protect is active. |
||
| 58 | * Otherwise and only if user is allowed to see the Jetpack Admin, the Dashboard sub-link is added but pointed to Apps tab. |
||
| 59 | * |
||
| 60 | * Works in Dev Mode or when user is connected. |
||
| 61 | * |
||
| 62 | * @since 4.3 |
||
| 63 | */ |
||
| 64 | function jetpack_add_dashboard_sub_nav_item() { |
||
| 65 | if ( Jetpack::is_development_mode() || Jetpack::is_active() ) { |
||
| 66 | global $submenu; |
||
| 67 | if ( current_user_can( 'jetpack_manage_modules' ) || Jetpack::is_module_active( 'protect' ) || current_user_can( 'view_stats' ) ) { |
||
| 68 | $submenu['jetpack'][] = array( __( 'Dashboard', 'jetpack' ), 'jetpack_admin_page', Jetpack::admin_url( 'page=jetpack#/dashboard' ) ); |
||
| 69 | } elseif ( current_user_can( 'jetpack_admin_page' ) ) { |
||
| 70 | $submenu['jetpack'][] = array( __( 'Dashboard', 'jetpack' ), 'jetpack_admin_page', Jetpack::admin_url( 'page=jetpack#/apps' ) ); |
||
| 71 | } |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * If user is allowed to see the Jetpack Admin, add Settings sub-link. |
||
| 77 | * |
||
| 78 | * @since 4.3 |
||
| 79 | */ |
||
| 80 | function jetpack_add_settings_sub_nav_item() { |
||
| 81 | if ( ( Jetpack::is_development_mode() || Jetpack::is_active() ) && current_user_can( 'jetpack_admin_page' ) ) { |
||
| 82 | global $submenu; |
||
| 83 | $submenu['jetpack'][] = array( __( 'Settings', 'jetpack' ), 'jetpack_admin_page', Jetpack::admin_url( 'page=jetpack#/settings' ) ); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | function add_fallback_head_meta() { |
||
| 88 | echo '<meta http-equiv="refresh" content="0; url=?page=jetpack_modules">'; |
||
| 89 | } |
||
| 90 | |||
| 91 | function add_noscript_head_meta() { |
||
| 92 | echo '<noscript>'; |
||
| 93 | $this->add_fallback_head_meta(); |
||
| 94 | echo '</noscript>'; |
||
| 95 | } |
||
| 96 | |||
| 97 | View Code Duplication | function jetpack_menu_order( $menu_order ) { |
|
| 98 | $jp_menu_order = array(); |
||
| 99 | |||
| 100 | foreach ( $menu_order as $index => $item ) { |
||
| 101 | if ( $item != 'jetpack' ) |
||
| 102 | $jp_menu_order[] = $item; |
||
| 103 | |||
| 104 | if ( $index == 0 ) |
||
| 105 | $jp_menu_order[] = 'jetpack'; |
||
| 106 | } |
||
| 107 | |||
| 108 | return $jp_menu_order; |
||
| 109 | } |
||
| 110 | |||
| 111 | // Render the configuration page for the module if it exists and an error |
||
| 112 | // screen if the module is not configurable |
||
| 113 | // @todo remove when real settings are in place |
||
| 114 | View Code Duplication | function render_nojs_configurable( $module_name ) { |
|
| 115 | $module_name = preg_replace( '/[^\da-z\-]+/', '', $_GET['configure'] ); |
||
| 116 | |||
| 117 | include_once( JETPACK__PLUGIN_DIR . '_inc/header.php' ); |
||
| 118 | echo '<div class="clouds-sm"></div>'; |
||
| 119 | echo '<div class="wrap configure-module">'; |
||
| 120 | |||
| 121 | if ( Jetpack::is_module( $module_name ) && current_user_can( 'jetpack_configure_modules' ) ) { |
||
| 122 | Jetpack::admin_screen_configure_module( $module_name ); |
||
| 123 | } else { |
||
| 124 | echo '<h2>' . esc_html__( 'Error, bad module.', 'jetpack' ) . '</h2>'; |
||
| 125 | } |
||
| 126 | |||
| 127 | echo '</div><!-- /wrap -->'; |
||
| 128 | } |
||
| 129 | |||
| 130 | function page_render() { |
||
| 131 | // Handle redirects to configuration pages |
||
| 132 | if ( ! empty( $_GET['configure'] ) ) { |
||
| 133 | return $this->render_nojs_configurable( $_GET['configure'] ); |
||
| 134 | } |
||
| 135 | ?> |
||
| 136 | <?php |
||
| 137 | /** This action is already documented in views/admin/admin-page.php */ |
||
| 138 | do_action( 'jetpack_notices' ); |
||
| 139 | ?> |
||
| 140 | <div id="jp-plugin-container"></div> |
||
| 141 | <?php } |
||
| 142 | |||
| 143 | function get_i18n_data() { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Gets array of any Jetpack notices that have been dismissed. |
||
| 154 | * |
||
| 155 | * @since 4.0.1 |
||
| 156 | * @return mixed|void |
||
| 157 | */ |
||
| 158 | function get_dismissed_jetpack_notices() { |
||
| 170 | |||
| 171 | function page_admin_scripts() { |
||
| 172 | if ( $this->is_redirecting ) { |
||
| 173 | return; // No need for scripts on a fallback page |
||
| 174 | } |
||
| 175 | |||
| 176 | // Enqueue jp.js and localize it |
||
| 177 | wp_enqueue_script( 'react-plugin', plugins_url( '_inc/build/admin.js', JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION, true ); |
||
| 178 | wp_enqueue_style( 'dops-css', plugins_url( '_inc/build/admin.dops-style.css', JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION ); |
||
| 179 | wp_enqueue_style( 'components-css', plugins_url( '_inc/build/style.min.css', JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION ); |
||
| 243 | } |
||
| 244 | |||
| 399 |