Completed
Push — renovate/wordpress-monorepo ( 8cdadc...6203b6 )
by
unknown
552:54 queued 542:45
created

Base_Admin_Menu::update_menu()   D

Complexity

Conditions 12
Paths 291

Size

Total Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
nc 291
nop 6
dl 0
loc 55
rs 4.9958
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Base Admin Menu file.
4
 *
5
 * @package automattic/jetpack
6
 */
7
8
namespace Automattic\Jetpack\Dashboard_Customizations;
9
10
use Automattic\Jetpack\Status;
11
12
/**
13
 * Class Base_Admin_Menu
14
 */
15
abstract class Base_Admin_Menu {
16
	/**
17
	 * Holds class instances.
18
	 *
19
	 * @var array
20
	 */
21
	protected static $instances;
22
23
	/**
24
	 * Whether the current request is a REST API request.
25
	 *
26
	 * @var bool
27
	 */
28
	protected $is_api_request = false;
29
30
	/**
31
	 * Domain of the current site.
32
	 *
33
	 * @var string
34
	 */
35
	protected $domain;
36
37
	/**
38
	 * Base_Admin_Menu constructor.
39
	 */
40
	protected function __construct() {
41
		add_action( 'admin_menu', array( $this, 'set_is_api_request' ), 99998 );
42
		add_action( 'admin_menu', array( $this, 'reregister_menu_items' ), 99999 );
43
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
44
		add_filter( 'rest_request_before_callbacks', array( $this, 'rest_api_init' ), 11 );
45
46
		$this->domain = ( new Status() )->get_site_suffix();
47
	}
48
49
	/**
50
	 * Determine if the current request is from API
51
	 */
52
	public function set_is_api_request() {
53
		// Constant is not defined until parse_request.
54
		if ( ! $this->is_api_request ) {
55
			$this->is_api_request = defined( 'REST_REQUEST' ) && REST_REQUEST;
56
		}
57
	}
58
59
	/**
60
	 * Returns class instance.
61
	 *
62
	 * @return Admin_Menu
63
	 */
64
	public static function get_instance() {
65
		$class = get_called_class();
66
67
		if ( empty( static::$instances[ $class ] ) ) {
68
			static::$instances[ $class ] = new $class();
69
		}
70
71
		return static::$instances[ $class ];
72
	}
73
74
	/**
75
	 * Sets up class properties for REST API requests.
76
	 *
77
	 * @param \WP_REST_Response $response Response from the endpoint.
78
	 */
79
	public function rest_api_init( $response ) {
80
		$this->is_api_request = true;
81
82
		return $response;
83
	}
84
85
	/**
86
	 * Updates the menu data of the given menu slug.
87
	 *
88
	 * @param string $slug Slug of the menu to update.
89
	 * @param string $url New menu URL.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $url not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
90
	 * @param string $title New menu title.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $title not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
91
	 * @param string $cap New menu capability.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $cap not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
92
	 * @param string $icon New menu icon.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $icon not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
93
	 * @param int    $position New menu position.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $position not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
94
	 * @return bool Whether the menu has been updated.
95
	 */
96
	public function update_menu( $slug, $url = null, $title = null, $cap = null, $icon = null, $position = null ) {
97
		global $menu, $submenu;
98
99
		$menu_item     = null;
100
		$menu_position = null;
101
102
		foreach ( $menu as $i => $item ) {
103
			if ( $slug === $item[2] ) {
104
				$menu_item     = $item;
105
				$menu_position = $i;
106
				break;
107
			}
108
		}
109
110
		if ( ! $menu_item ) {
111
			return false;
112
		}
113
114
		if ( $title ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $title of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
115
			$menu_item[0] = $title;
116
			$menu_item[3] = esc_attr( $title );
117
		}
118
119
		if ( $cap ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $cap of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
120
			$menu_item[1] = $cap;
121
		}
122
123
		// Change parent slug only if there are no submenus (the slug of the 1st submenu will be used if there are submenus).
124
		if ( $url ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $url of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
125
			remove_submenu_page( $slug, $slug );
126
			if ( empty( $submenu[ $slug ] ) ) {
127
				$menu_item[2] = $url;
128
			}
129
		}
130
131
		if ( $icon ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $icon of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
132
			$menu_item[4] = 'menu-top';
133
			$menu_item[6] = $icon;
134
		}
135
136
		if ( $position ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $position of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
137
			// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
138
			unset( $menu[ $menu_position ] );
139
			$menu_position = $position;
140
		}
141
		// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
142
		$menu[ $menu_position ] = $menu_item;
143
144
		// Only add submenu when there are other submenu items.
145
		if ( $url && ! empty( $submenu[ $slug ] ) ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $url of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
146
			add_submenu_page( $slug, $menu_item[3], $menu_item[0], $menu_item[1], $url, null, 0 );
147
		}
148
149
		return true;
150
	}
151
152
	/**
153
	 * Updates the submenus of the given menu slug.
154
	 *
155
	 * @param string $slug Menu slug.
156
	 * @param array  $submenus_to_update Array of new submenu slugs.
157
	 */
158
	public function update_submenus( $slug, $submenus_to_update ) {
159
		global $submenu;
160
161
		if ( ! isset( $submenu[ $slug ] ) ) {
162
			return;
163
		}
164
165
		foreach ( $submenu[ $slug ] as $i => $submenu_item ) {
166
			if ( array_key_exists( $submenu_item[2], $submenus_to_update ) ) {
167
				$submenu_item[2] = $submenus_to_update[ $submenu_item[2] ];
168
				// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
169
				$submenu[ $slug ][ $i ] = $submenu_item;
170
			}
171
		}
172
	}
173
174
	/**
175
	 * Adds a menu separator.
176
	 *
177
	 * @param int    $position The position in the menu order this item should appear.
178
	 * @param string $cap Optional. The capability required for this menu to be displayed to the user.
179
	 *                         Default: 'read'.
180
	 */
181
	public function add_admin_menu_separator( $position, $cap = 'read' ) {
182
		global $menu;
183
184
		// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
185
		$menu[ $position ] = array(
186
			'',                                  // Menu title (ignored).
187
			$cap,                                // Required capability.
188
			wp_unique_id( 'separator-custom-' ), // URL or file (ignored, but must be unique).
189
			'',                                  // Page title (ignored).
190
			'wp-menu-separator',                 // CSS class. Identifies this item as a separator.
191
		);
192
		ksort( $menu );
193
	}
194
195
	/**
196
	 * Enqueues scripts and styles.
197
	 */
198
	public function enqueue_scripts() {
199
		$rtl = is_rtl() ? '-rtl' : '';
200 View Code Duplication
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
201
			$style_dependencies = array( 'wpcom-admin-bar', 'wpcom-masterbar-css' );
202
		} else {
203
			$style_dependencies = array( 'a8c-wpcom-masterbar' . $rtl, 'a8c-wpcom-masterbar-overrides' . $rtl );
204
		}
205
		wp_enqueue_style(
206
			'jetpack-admin-menu',
207
			plugins_url( 'admin-menu.css', __FILE__ ),
208
			$style_dependencies,
209
			JETPACK__VERSION
210
		);
211
		wp_enqueue_script(
212
			'jetpack-admin-menu',
213
			plugins_url( 'admin-menu.js', __FILE__ ),
214
			array(),
215
			JETPACK__VERSION,
216
			true
217
		);
218
	}
219
220
	/**
221
	 * Remove submenu items from given menu slug.
222
	 *
223
	 * @param string $slug Menu slug.
224
	 */
225
	public function remove_submenus( $slug ) {
226
		global $submenu;
227
228
		if ( isset( $submenu[ $slug ] ) ) {
229
			// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
230
			$submenu[ $slug ] = array();
231
		}
232
	}
233
234
	/**
235
	 * Whether to use wp-admin pages rather than Calypso.
236
	 *
237
	 * Options:
238
	 * false - Calypso (Default).
239
	 * true  - wp-admin.
240
	 *
241
	 * @return bool
242
	 */
243
	abstract public function should_link_to_wp_admin();
244
245
	/**
246
	 * Create the desired menu output.
247
	 */
248
	abstract public function reregister_menu_items();
249
}
250