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 Admin_Menu 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 Admin_Menu, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Admin_Menu { |
||
| 17 | /** |
||
| 18 | * Holds class instances. |
||
| 19 | * |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | protected static $instances; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Whether the current request is a REST API request. |
||
| 26 | * |
||
| 27 | * @var bool |
||
| 28 | */ |
||
| 29 | protected $is_api_request = false; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Domain of the current site. |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $domain; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Admin_Menu constructor. |
||
| 40 | */ |
||
| 41 | protected function __construct() { |
||
| 42 | add_action( 'admin_menu', array( $this, 'reregister_menu_items' ), 99999 ); |
||
| 43 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
||
| 44 | add_action( 'wp_enqueue_scripts', array( $this, 'dequeue_scripts' ), 20 ); |
||
| 45 | add_action( 'admin_enqueue_scripts', array( $this, 'dequeue_scripts' ), 20 ); |
||
| 46 | add_filter( 'rest_request_before_callbacks', array( $this, 'rest_api_init' ), 11 ); |
||
| 47 | |||
| 48 | $this->domain = ( new Status() )->get_site_suffix(); |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Returns class instance. |
||
| 53 | * |
||
| 54 | * @return Admin_Menu |
||
| 55 | */ |
||
| 56 | public static function get_instance() { |
||
| 57 | $class = get_called_class(); |
||
| 58 | |||
| 59 | if ( empty( static::$instances[ $class ] ) ) { |
||
| 60 | static::$instances[ $class ] = new $class(); |
||
| 61 | } |
||
| 62 | |||
| 63 | return static::$instances[ $class ]; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Sets up class properties for REST API requests. |
||
| 68 | * |
||
| 69 | * @param WP_REST_Response $response Response from the endpoint. |
||
| 70 | */ |
||
| 71 | public function rest_api_init( $response ) { |
||
| 72 | $this->is_api_request = true; |
||
| 73 | |||
| 74 | return $response; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Create the desired menu output. |
||
| 79 | */ |
||
| 80 | public function reregister_menu_items() { |
||
| 81 | // Constant is not defined until parse_request. |
||
| 82 | if ( ! $this->is_api_request ) { |
||
| 83 | $this->is_api_request = defined( 'REST_REQUEST' ) && REST_REQUEST; |
||
| 84 | } |
||
| 85 | |||
| 86 | /* |
||
| 87 | * Whether links should point to Calypso or wp-admin. |
||
| 88 | * |
||
| 89 | * Options: |
||
| 90 | * false - Calypso (Default). |
||
| 91 | * true - wp-admin. |
||
| 92 | */ |
||
| 93 | $wp_admin = $this->should_link_to_wp_admin(); |
||
| 94 | |||
| 95 | // Remove separators. |
||
| 96 | remove_menu_page( 'separator1' ); |
||
| 97 | |||
| 98 | $this->add_stats_menu(); |
||
| 99 | $this->add_upgrades_menu(); |
||
| 100 | $this->add_posts_menu( $wp_admin ); |
||
| 101 | $this->add_media_menu( $wp_admin ); |
||
| 102 | $this->add_page_menu( $wp_admin ); |
||
| 103 | $this->add_testimonials_menu( $wp_admin ); |
||
| 104 | $this->add_portfolio_menu( $wp_admin ); |
||
| 105 | $this->add_comments_menu( $wp_admin ); |
||
| 106 | |||
| 107 | // Whether Themes/Customize links should point to Calypso (false) or wp-admin (true). |
||
| 108 | $wp_admin_themes = $wp_admin; |
||
| 109 | $wp_admin_customize = $wp_admin; |
||
| 110 | $this->add_appearance_menu( $wp_admin_themes, $wp_admin_customize ); |
||
| 111 | $this->add_plugins_menu( $wp_admin ); |
||
| 112 | $this->add_users_menu( $wp_admin ); |
||
| 113 | |||
| 114 | // Whether Import/Export links should point to Calypso (false) or wp-admin (true). |
||
| 115 | $wp_admin_import = $wp_admin; |
||
| 116 | $wp_admin_export = $wp_admin; |
||
| 117 | $this->add_tools_menu( $wp_admin_import, $wp_admin_export ); |
||
| 118 | |||
| 119 | $this->add_options_menu( $wp_admin ); |
||
| 120 | $this->add_jetpack_menu(); |
||
| 121 | |||
| 122 | ksort( $GLOBALS['menu'] ); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Adds My Home menu. |
||
| 127 | * |
||
| 128 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 129 | */ |
||
| 130 | public function add_my_home_menu( $wp_admin = false ) { |
||
| 131 | global $submenu; |
||
| 132 | |||
| 133 | $menu_slug = $wp_admin ? 'index.php' : 'https://wordpress.com/home/' . $this->domain; |
||
| 134 | $cap = $wp_admin ? 'read' : 'manage_options'; // Calypso's My Home is only available for admins. |
||
| 135 | |||
| 136 | remove_menu_page( 'index.php' ); |
||
| 137 | remove_submenu_page( 'index.php', 'index.php' ); |
||
| 138 | |||
| 139 | add_menu_page( __( 'My Home', 'jetpack' ), __( 'My Home', 'jetpack' ), $cap, $menu_slug, null, 'dashicons-admin-home', 2 ); |
||
| 140 | |||
| 141 | // Only add submenu when there are other submenu items. |
||
| 142 | View Code Duplication | if ( ! empty( $submenu['index.php'] ) ) { |
|
| 143 | add_submenu_page( $menu_slug, __( 'My Home', 'jetpack' ), __( 'My Home', 'jetpack' ), $cap, $menu_slug, null, 1 ); |
||
| 144 | } |
||
| 145 | |||
| 146 | $this->migrate_submenus( 'index.php', $menu_slug ); |
||
| 147 | add_filter( |
||
| 148 | 'parent_file', |
||
| 149 | function ( $parent_file ) use ( $menu_slug ) { |
||
| 150 | return 'index.php' === $parent_file ? $menu_slug : $parent_file; |
||
| 151 | } |
||
| 152 | ); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Adds Stats menu. |
||
| 157 | */ |
||
| 158 | public function add_stats_menu() { |
||
| 159 | add_menu_page( __( 'Stats', 'jetpack' ), __( 'Stats', 'jetpack' ), 'view_stats', 'https://wordpress.com/stats/day/' . $this->domain, null, 'dashicons-chart-bar', 3 ); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Adds Upgrades menu. |
||
| 164 | */ |
||
| 165 | View Code Duplication | public function add_upgrades_menu() { |
|
| 166 | remove_menu_page( 'paid-upgrades.php' ); |
||
| 167 | |||
| 168 | $menu_slug = 'https://wordpress.com/plans/' . $this->domain; |
||
| 169 | |||
| 170 | add_menu_page( __( 'Upgrades', 'jetpack' ), __( 'Upgrades', 'jetpack' ), 'manage_options', $menu_slug, null, 'dashicons-cart', 4 ); |
||
| 171 | add_submenu_page( $menu_slug, __( 'Plans', 'jetpack' ), __( 'Plans', 'jetpack' ), 'manage_options', $menu_slug, null, 5 ); |
||
| 172 | add_submenu_page( $menu_slug, __( 'Purchases', 'jetpack' ), __( 'Purchases', 'jetpack' ), 'manage_options', 'https://wordpress.com/purchases/subscriptions/' . $this->domain, null, 15 ); |
||
| 173 | |||
| 174 | $this->migrate_submenus( 'paid-upgrades.php', $menu_slug ); |
||
| 175 | add_filter( |
||
| 176 | 'parent_file', |
||
| 177 | function ( $parent_file ) use ( $menu_slug ) { |
||
| 178 | return 'paid-upgrades.php' === $parent_file ? $menu_slug : $parent_file; |
||
| 179 | } |
||
| 180 | ); |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Adds Posts menu. |
||
| 185 | * |
||
| 186 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 187 | */ |
||
| 188 | View Code Duplication | public function add_posts_menu( $wp_admin = false ) { |
|
| 189 | if ( $wp_admin ) { |
||
| 190 | return; |
||
| 191 | } |
||
| 192 | |||
| 193 | $ptype_obj = get_post_type_object( 'post' ); |
||
| 194 | $menu_slug = 'https://wordpress.com/posts/' . $this->domain; |
||
| 195 | |||
| 196 | remove_menu_page( 'edit.php' ); |
||
| 197 | remove_submenu_page( 'edit.php', 'edit.php' ); |
||
| 198 | remove_submenu_page( 'edit.php', 'post-new.php' ); |
||
| 199 | |||
| 200 | add_menu_page( esc_attr( $ptype_obj->labels->menu_name ), $ptype_obj->labels->menu_name, $ptype_obj->cap->edit_posts, $menu_slug, null, 'dashicons-admin-post', $ptype_obj->menu_position ); |
||
| 201 | add_submenu_page( $menu_slug, $ptype_obj->labels->all_items, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, $menu_slug, null, 5 ); |
||
| 202 | add_submenu_page( $menu_slug, $ptype_obj->labels->add_new, $ptype_obj->labels->add_new, $ptype_obj->cap->create_posts, 'https://wordpress.com/post/' . $this->domain, null, 10 ); |
||
| 203 | |||
| 204 | $this->migrate_submenus( 'edit.php', $menu_slug ); |
||
| 205 | add_filter( |
||
| 206 | 'parent_file', |
||
| 207 | function ( $parent_file ) use ( $menu_slug ) { |
||
| 208 | return 'edit.php' === $parent_file ? $menu_slug : $parent_file; |
||
| 209 | } |
||
| 210 | ); |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Adds Media menu. |
||
| 215 | * |
||
| 216 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 217 | */ |
||
| 218 | public function add_media_menu( $wp_admin = false ) { |
||
| 219 | remove_submenu_page( 'upload.php', 'upload.php' ); |
||
| 220 | remove_submenu_page( 'upload.php', 'media-new.php' ); |
||
| 221 | |||
| 222 | if ( ! $wp_admin ) { |
||
| 223 | $menu_slug = 'https://wordpress.com/media/' . $this->domain; |
||
| 224 | |||
| 225 | remove_menu_page( 'upload.php' ); |
||
| 226 | add_menu_page( __( 'Media', 'jetpack' ), __( 'Media', 'jetpack' ), 'upload_files', $menu_slug, null, 'dashicons-admin-media', 10 ); |
||
| 227 | $this->migrate_submenus( 'upload.php', $menu_slug ); |
||
| 228 | |||
| 229 | add_filter( |
||
| 230 | 'parent_file', |
||
| 231 | function ( $parent_file ) use ( $menu_slug ) { |
||
| 232 | return 'upload.php' === $parent_file ? $menu_slug : $parent_file; |
||
| 233 | } |
||
| 234 | ); |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Adds Page menu. |
||
| 240 | * |
||
| 241 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 242 | */ |
||
| 243 | View Code Duplication | public function add_page_menu( $wp_admin = false ) { |
|
| 244 | if ( $wp_admin ) { |
||
| 245 | return; |
||
| 246 | } |
||
| 247 | |||
| 248 | $ptype_obj = get_post_type_object( 'page' ); |
||
| 249 | $menu_slug = 'https://wordpress.com/pages/' . $this->domain; |
||
| 250 | |||
| 251 | remove_menu_page( 'edit.php?post_type=page' ); |
||
| 252 | remove_submenu_page( 'edit.php?post_type=page', 'edit.php?post_type=page' ); |
||
| 253 | remove_submenu_page( 'edit.php?post_type=page', 'post-new.php?post_type=page' ); |
||
| 254 | |||
| 255 | add_menu_page( esc_attr( $ptype_obj->labels->menu_name ), $ptype_obj->labels->menu_name, $ptype_obj->cap->edit_posts, $menu_slug, null, 'dashicons-admin-page', $ptype_obj->menu_position ); |
||
| 256 | add_submenu_page( $menu_slug, $ptype_obj->labels->all_items, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, $menu_slug, null, 5 ); |
||
| 257 | add_submenu_page( $menu_slug, $ptype_obj->labels->add_new, $ptype_obj->labels->add_new, $ptype_obj->cap->create_posts, 'https://wordpress.com/page/' . $this->domain, null, 10 ); |
||
| 258 | |||
| 259 | $this->migrate_submenus( 'edit.php?post_type=page', $menu_slug ); |
||
| 260 | add_filter( |
||
| 261 | 'parent_file', |
||
| 262 | function ( $parent_file ) use ( $menu_slug ) { |
||
| 263 | return 'edit.php?post_type=page' === $parent_file ? $menu_slug : $parent_file; |
||
| 264 | } |
||
| 265 | ); |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Adds Testimonials menu. |
||
| 270 | * |
||
| 271 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 272 | */ |
||
| 273 | public function add_testimonials_menu( $wp_admin = false ) { |
||
| 274 | $this->add_custom_post_type_menu( 'jetpack-testimonial', $wp_admin ); |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Adds Portfolio menu. |
||
| 279 | * |
||
| 280 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 281 | */ |
||
| 282 | public function add_portfolio_menu( $wp_admin = false ) { |
||
| 283 | $this->add_custom_post_type_menu( 'jetpack-portfolio', $wp_admin ); |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Adds a custom post type menu. |
||
| 288 | * |
||
| 289 | * @param string $post_type Custom post type. |
||
| 290 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 291 | */ |
||
| 292 | public function add_custom_post_type_menu( $post_type, $wp_admin = false ) { |
||
| 293 | if ( $wp_admin ) { |
||
| 294 | return; |
||
| 295 | } |
||
| 296 | |||
| 297 | $ptype_obj = get_post_type_object( $post_type ); |
||
| 298 | if ( empty( $ptype_obj ) ) { |
||
| 299 | return; |
||
| 300 | } |
||
| 301 | |||
| 302 | $cpt_slug = 'edit.php?post_type=' . $post_type; |
||
| 303 | $menu_slug = 'https://wordpress.com/types/' . $post_type . '/' . $this->domain; |
||
| 304 | |||
| 305 | remove_menu_page( $cpt_slug ); |
||
| 306 | remove_submenu_page( $cpt_slug, $cpt_slug ); |
||
| 307 | remove_submenu_page( $cpt_slug, 'post-new.php?post_type=' . $post_type ); |
||
| 308 | |||
| 309 | // Menu icon. |
||
| 310 | $menu_icon = 'dashicons-admin-post'; |
||
| 311 | if ( is_string( $ptype_obj->menu_icon ) ) { |
||
| 312 | // Special handling for data:image/svg+xml and Dashicons. |
||
| 313 | if ( 0 === strpos( $ptype_obj->menu_icon, 'data:image/svg+xml;base64,' ) || 0 === strpos( $ptype_obj->menu_icon, 'dashicons-' ) ) { |
||
| 314 | $menu_icon = $ptype_obj->menu_icon; |
||
| 315 | } else { |
||
| 316 | $menu_icon = esc_url( $ptype_obj->menu_icon ); |
||
| 317 | } |
||
| 318 | } |
||
| 319 | |||
| 320 | /* |
||
| 321 | * Menu position. |
||
| 322 | * |
||
| 323 | * If $ptype_menu_position is already populated or will be populated |
||
| 324 | * by a hard-coded value below, increment the position. |
||
| 325 | */ |
||
| 326 | $ptype_menu_position = is_int( $ptype_obj->menu_position ) ? $ptype_obj->menu_position : ++$GLOBALS['_wp_last_object_menu']; |
||
| 327 | $core_menu_positions = array( 59, 60, 65, 70, 75, 80, 85, 99 ); |
||
| 328 | while ( isset( $GLOBALS['menu'][ $ptype_menu_position ] ) || in_array( $ptype_menu_position, $core_menu_positions, true ) ) { |
||
| 329 | $ptype_menu_position++; |
||
| 330 | } |
||
| 331 | |||
| 332 | add_menu_page( esc_attr( $ptype_obj->labels->menu_name ), $ptype_obj->labels->menu_name, $ptype_obj->cap->edit_posts, $menu_slug, null, $menu_icon, $ptype_menu_position ); |
||
| 333 | add_submenu_page( $menu_slug, $ptype_obj->labels->all_items, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, $menu_slug, null, 5 ); |
||
| 334 | add_submenu_page( $menu_slug, $ptype_obj->labels->add_new, $ptype_obj->labels->add_new, $ptype_obj->cap->create_posts, 'https://wordpress.com/edit/' . $post_type . '/' . $this->domain, null, 10 ); |
||
| 335 | $this->migrate_submenus( $cpt_slug, $menu_slug ); |
||
| 336 | |||
| 337 | add_filter( |
||
| 338 | 'parent_file', |
||
| 339 | function ( $parent_file ) use ( $cpt_slug, $menu_slug ) { |
||
| 340 | return $cpt_slug === $parent_file ? $menu_slug : $parent_file; |
||
| 341 | } |
||
| 342 | ); |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Adds Comments menu. |
||
| 347 | * |
||
| 348 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 349 | */ |
||
| 350 | public function add_comments_menu( $wp_admin = false ) { |
||
| 351 | if ( $wp_admin || ! current_user_can( 'edit_posts' ) ) { |
||
| 352 | return; |
||
| 353 | } |
||
| 354 | |||
| 355 | $awaiting_mod = wp_count_comments(); |
||
| 356 | $awaiting_mod = $awaiting_mod->moderated; |
||
| 357 | $awaiting_mod_i18n = number_format_i18n( $awaiting_mod ); |
||
| 358 | /* translators: %s: Number of comments. */ |
||
| 359 | $awaiting_mod_text = sprintf( _n( '%s Comment in moderation', '%s Comments in moderation', $awaiting_mod, 'jetpack' ), $awaiting_mod_i18n ); |
||
| 360 | |||
| 361 | /* translators: %s: Number of comments. */ |
||
| 362 | $menu_title = sprintf( __( 'Comments %s', 'jetpack' ), '<span class="awaiting-mod count-' . absint( $awaiting_mod ) . '"><span class="pending-count" aria-hidden="true">' . $awaiting_mod_i18n . '</span><span class="comments-in-moderation-text screen-reader-text">' . $awaiting_mod_text . '</span></span>' ); |
||
| 363 | $menu_slug = 'https://wordpress.com/comments/all/' . $this->domain; |
||
| 364 | |||
| 365 | remove_menu_page( 'edit-comments.php' ); |
||
| 366 | remove_submenu_page( 'edit-comments.php', 'edit-comments.php' ); |
||
| 367 | |||
| 368 | add_menu_page( esc_attr__( 'Comments', 'jetpack' ), $menu_title, 'edit_posts', $menu_slug, null, 'dashicons-admin-comments', 25 ); |
||
| 369 | |||
| 370 | $this->migrate_submenus( 'edit-comments.php', $menu_slug ); |
||
| 371 | add_filter( |
||
| 372 | 'parent_file', |
||
| 373 | function ( $parent_file ) use ( $menu_slug ) { |
||
| 374 | return 'edit-comments.php' === $parent_file ? $menu_slug : $parent_file; |
||
| 375 | } |
||
| 376 | ); |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Adds Appearance menu. |
||
| 381 | * |
||
| 382 | * @param bool $wp_admin_themes Optional. Whether Themes link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 383 | * @param bool $wp_admin_customize Optional. Whether Customize link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 384 | */ |
||
| 385 | public function add_appearance_menu( $wp_admin_themes = false, $wp_admin_customize = false ) { |
||
| 386 | $user_can_customize = current_user_can( 'customize' ); |
||
| 387 | $appearance_cap = current_user_can( 'switch_themes' ) ? 'switch_themes' : 'edit_theme_options'; |
||
| 388 | $themes_slug = $wp_admin_themes ? 'themes.php' : 'https://wordpress.com/themes/' . $this->domain; |
||
| 389 | $customize_slug = $wp_admin_customize ? 'customize.php' : 'https://wordpress.com/customize/' . $this->domain; // phpcs:ignore |
||
| 390 | remove_menu_page( 'themes.php' ); |
||
| 391 | remove_submenu_page( 'themes.php', 'themes.php' ); |
||
| 392 | remove_submenu_page( 'themes.php', 'theme-editor.php' ); |
||
| 393 | remove_submenu_page( 'themes.php', add_query_arg( 'return', rawurlencode( remove_query_arg( wp_removable_query_args(), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ), 'customize.php' ) ); |
||
| 394 | remove_submenu_page( 'themes.php', 'custom-header' ); |
||
| 395 | remove_submenu_page( 'themes.php', 'custom-background' ); |
||
| 396 | |||
| 397 | add_menu_page( esc_attr__( 'Appearance', 'jetpack' ), __( 'Appearance', 'jetpack' ), $appearance_cap, $themes_slug, null, 'dashicons-admin-appearance', 60 ); |
||
| 398 | add_submenu_page( $themes_slug, esc_attr__( 'Themes', 'jetpack' ), __( 'Themes', 'jetpack' ), 'switch_themes', $themes_slug, null, 0 ); |
||
| 399 | add_submenu_page( $themes_slug, esc_attr__( 'Customize', 'jetpack' ), __( 'Customize', 'jetpack' ), 'customize', $customize_slug, null, 1 ); |
||
| 400 | |||
| 401 | // Maintain id as JS selector. |
||
| 402 | $GLOBALS['menu'][60][5] = 'menu-appearance'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
||
| 403 | |||
| 404 | if ( current_theme_supports( 'custom-header' ) && $user_can_customize ) { |
||
| 405 | $customize_header_url = add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $customize_slug ); |
||
| 406 | remove_submenu_page( 'themes.php', esc_url( $customize_header_url ) ); |
||
| 407 | |||
| 408 | // TODO: Remove WPCom_Theme_Customizer::modify_header_menu_links() and WPcom_Custom_Header::modify_admin_menu_links(). |
||
| 409 | $customize_header_url = admin_url( 'themes.php?page=custom-header' ); |
||
| 410 | remove_submenu_page( 'themes.php', esc_url( $customize_header_url ) ); |
||
| 411 | |||
| 412 | $customize_header_url = add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $customize_slug ); |
||
| 413 | add_submenu_page( $themes_slug, __( 'Header', 'jetpack' ), __( 'Header', 'jetpack' ), 'customize', esc_url( $customize_header_url ), null, 15 ); |
||
| 414 | } |
||
| 415 | |||
| 416 | if ( current_theme_supports( 'custom-background' ) && $user_can_customize ) { |
||
| 417 | $customize_background_url = add_query_arg( array( 'autofocus' => array( 'control' => 'background_image' ) ), $customize_slug ); |
||
| 418 | remove_submenu_page( 'themes.php', esc_url( $customize_background_url ) ); |
||
| 419 | |||
| 420 | // TODO: Remove Colors_Manager::modify_header_menu_links() and Colors_Manager_Common::modify_header_menu_links(). |
||
| 421 | $customize_background_url = add_query_arg( array( 'autofocus' => array( 'section' => 'colors_manager_tool' ) ), admin_url( 'customize.php' ) ); |
||
| 422 | remove_submenu_page( 'themes.php', esc_url( $customize_background_url ) ); |
||
| 423 | |||
| 424 | $customize_background_url = add_query_arg( array( 'autofocus' => array( 'section' => 'colors_manager_tool' ) ), $customize_slug ); |
||
| 425 | add_submenu_page( $themes_slug, esc_attr__( 'Background', 'jetpack' ), __( 'Background', 'jetpack' ), 'customize', esc_url( $customize_background_url ), null, 20 ); |
||
| 426 | } |
||
| 427 | |||
| 428 | View Code Duplication | if ( current_theme_supports( 'widgets' ) ) { |
|
| 429 | remove_submenu_page( 'themes.php', 'widgets.php' ); |
||
| 430 | remove_submenu_page( 'themes.php', 'gutenberg-widgets' ); |
||
| 431 | |||
| 432 | $customize_widgets_url = $wp_admin_customize ? 'widgets.php' : add_query_arg( array( 'autofocus' => array( 'panel' => 'widgets' ) ), $customize_slug ); |
||
| 433 | add_submenu_page( $themes_slug, esc_attr__( 'Widgets', 'jetpack' ), __( 'Widgets', 'jetpack' ), 'customize', esc_url( $customize_widgets_url ), null, 20 ); |
||
| 434 | } |
||
| 435 | |||
| 436 | View Code Duplication | if ( current_theme_supports( 'menus' ) || current_theme_supports( 'widgets' ) ) { |
|
| 437 | remove_submenu_page( 'themes.php', 'nav-menus.php' ); |
||
| 438 | |||
| 439 | $customize_menus_url = $wp_admin_customize ? 'nav-menus.php' : add_query_arg( array( 'autofocus' => array( 'panel' => 'nav_menus' ) ), $customize_slug ); |
||
| 440 | add_submenu_page( $themes_slug, esc_attr__( 'Menus', 'jetpack' ), __( 'Menus', 'jetpack' ), 'customize', esc_url( $customize_menus_url ), null, 20 ); |
||
| 441 | } |
||
| 442 | |||
| 443 | // Register menu for the Custom CSS Jetpack module, but don't add it as a menu item. |
||
| 444 | $GLOBALS['_registered_pages']['admin_page_editcss'] = true; // phpcs:ignore |
||
| 445 | |||
| 446 | $this->migrate_submenus( 'themes.php', $themes_slug ); |
||
| 447 | add_filter( |
||
| 448 | 'parent_file', |
||
| 449 | function ( $parent_file ) use ( $themes_slug ) { |
||
| 450 | return 'themes.php' === $parent_file ? $themes_slug : $parent_file; |
||
| 451 | } |
||
| 452 | ); |
||
| 453 | } |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Adds Plugins menu. |
||
| 457 | * |
||
| 458 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 459 | */ |
||
| 460 | public function add_plugins_menu( $wp_admin = false ) { |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Adds Users menu. |
||
| 492 | * |
||
| 493 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 494 | */ |
||
| 495 | public function add_users_menu( $wp_admin = false ) { |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Adds Tools menu. |
||
| 526 | * |
||
| 527 | * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 528 | * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 529 | */ |
||
| 530 | public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) { |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Adds Settings menu. |
||
| 560 | * |
||
| 561 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 562 | */ |
||
| 563 | View Code Duplication | public function add_options_menu( $wp_admin = false ) { |
|
| 586 | |||
| 587 | /** |
||
| 588 | * Migrates submenu items from wp-admin menu slugs to Calypso menu slugs. |
||
| 589 | * |
||
| 590 | * @param string $old_slug WP-Admin menu slug. |
||
| 591 | * @param string $new_slug Calypso menu slug. (Calypso URL). |
||
| 592 | */ |
||
| 593 | public function migrate_submenus( $old_slug, $new_slug ) { |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Adds Jetpack menu. |
||
| 610 | */ |
||
| 611 | public function add_jetpack_menu() { |
||
| 612 | global $menu; |
||
| 613 | |||
| 614 | $position = 50; |
||
| 615 | while ( isset( $menu[ $position ] ) ) { |
||
| 616 | $position++; |
||
| 617 | } |
||
| 618 | |||
| 619 | // TODO: Replace with proper SVG data url. |
||
| 620 | $jetpack_icon = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 32 32' %3E%3Cpath fill='%23a0a5aa' d='M16,0C7.2,0,0,7.2,0,16s7.2,16,16,16s16-7.2,16-16S24.8,0,16,0z'%3E%3C/path%3E%3Cpolygon fill='%23fff' points='15,19 7,19 15,3 '%3E%3C/polygon%3E%3Cpolygon fill='%23fff' points='17,29 17,13 25,13 '%3E%3C/polygon%3E%3C/svg%3E"; |
||
| 621 | $jetpack_slug = 'https://wordpress.com/activity-log/' . $this->domain; |
||
| 622 | |||
| 648 | |||
| 649 | /** |
||
| 650 | * Adds a menu separator. |
||
| 651 | * |
||
| 652 | * @param int $position The position in the menu order this item should appear. |
||
| 653 | * @param string $cap Optional. The capability required for this menu to be displayed to the user. |
||
| 654 | * Default: 'read'. |
||
| 655 | */ |
||
| 656 | public function add_admin_menu_separator( $position, $cap = 'read' ) { |
||
| 669 | |||
| 670 | /** |
||
| 671 | * Enqueues scripts and styles. |
||
| 672 | */ |
||
| 673 | public function enqueue_scripts() { |
||
| 695 | |||
| 696 | /** |
||
| 697 | * Dequeues unnecessary scripts. |
||
| 698 | */ |
||
| 699 | public function dequeue_scripts() { |
||
| 702 | |||
| 703 | /** |
||
| 704 | * Whether to use wp-admin pages rather than Calypso. |
||
| 705 | * |
||
| 706 | * @return bool |
||
| 707 | */ |
||
| 708 | public function should_link_to_wp_admin() { |
||
| 711 | } |
||
| 712 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.