Completed
Push — renovate/octokit-rest-18.x ( 2bdfe3...d8558e )
by
unknown
137:32 queued 127:02
created

Base_Admin_Menu::set_menu_item()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
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
		// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
137
		unset( $menu[ $menu_position ] );
138
		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...
139
			$menu_position = $position;
140
		}
141
		$this->set_menu_item( $menu_item, $menu_position );
142
143
		// Only add submenu when there are other submenu items.
144
		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...
145
			add_submenu_page( $slug, $menu_item[3], $menu_item[0], $menu_item[1], $url, null, 0 );
146
		}
147
148
		return true;
149
	}
150
151
	/**
152
	 * Updates the submenus of the given menu slug.
153
	 *
154
	 * @param string $slug Menu slug.
155
	 * @param array  $submenus_to_update Array of new submenu slugs.
156
	 */
157
	public function update_submenus( $slug, $submenus_to_update ) {
158
		global $submenu;
159
160
		if ( ! isset( $submenu[ $slug ] ) ) {
161
			return;
162
		}
163
164
		foreach ( $submenu[ $slug ] as $i => $submenu_item ) {
165
			if ( array_key_exists( $submenu_item[2], $submenus_to_update ) ) {
166
				$submenu_item[2] = $submenus_to_update[ $submenu_item[2] ];
167
				// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
168
				$submenu[ $slug ][ $i ] = $submenu_item;
169
			}
170
		}
171
	}
172
173
	/**
174
	 * Adds a menu separator.
175
	 *
176
	 * @param int    $position The position in the menu order this item should appear.
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...
177
	 * @param string $cap Optional. The capability required for this menu to be displayed to the user.
178
	 *                         Default: 'read'.
179
	 */
180
	public function add_admin_menu_separator( $position = null, $cap = 'read' ) {
181
		$menu_item = array(
182
			'',                                  // Menu title (ignored).
183
			$cap,                                // Required capability.
184
			wp_unique_id( 'separator-custom-' ), // URL or file (ignored, but must be unique).
185
			'',                                  // Page title (ignored).
186
			'wp-menu-separator',                 // CSS class. Identifies this item as a separator.
187
		);
188
189
		$this->set_menu_item( $menu_item, $position );
190
	}
191
192
	/**
193
	 * Enqueues scripts and styles.
194
	 */
195
	public function enqueue_scripts() {
196
		$is_wpcom = defined( 'IS_WPCOM' ) && IS_WPCOM;
197
198
		if ( $this->is_rtl() ) {
199
			if ( $is_wpcom ) {
200
				$css_path = 'rtl/admin-menu-rtl.css';
201
			} else {
202
				$css_path = 'admin-menu-rtl.css';
203
			}
204
		} else {
205
			$css_path = 'admin-menu.css';
206
		}
207
208
		wp_enqueue_style(
209
			'jetpack-admin-menu',
210
			plugins_url( $css_path, __FILE__ ),
211
			array(),
212
			JETPACK__VERSION
213
		);
214
215
		wp_enqueue_script(
216
			'jetpack-admin-menu',
217
			plugins_url( 'admin-menu.js', __FILE__ ),
218
			array(),
219
			JETPACK__VERSION,
220
			true
221
		);
222
	}
223
224
	/**
225
	 * Adds the given menu item in the specified position.
226
	 *
227
	 * @param array $item The menu item to add.
228
	 * @param int   $position The position in the menu order this item should appear.
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...
229
	 */
230
	public function set_menu_item( $item, $position = null ) {
231
		global $menu;
232
233
		// Handle position (avoids overwriting menu items already populated in the given position).
234
		// Inspired by https://core.trac.wordpress.org/browser/trunk/src/wp-admin/menu.php?rev=49837#L160.
235
		if ( null === $position ) {
236
			$menu[] = $item; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
237
		} elseif ( isset( $menu[ "$position" ] ) ) {
238
			$position            = $position + substr( base_convert( md5( $item[2] . $item[0] ), 16, 10 ), -5 ) * 0.00001;
239
			$menu[ "$position" ] = $item; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
240
		} else {
241
			$menu[ $position ] = $item; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
242
		}
243
	}
244
245
	/**
246
	 * Determines whether the current locale is right-to-left (RTL).
247
	 */
248
	public function is_rtl() {
249
		return is_rtl();
250
	}
251
252
	/**
253
	 * Whether to use wp-admin pages rather than Calypso.
254
	 *
255
	 * Options:
256
	 * false - Calypso (Default).
257
	 * true  - wp-admin.
258
	 *
259
	 * @return bool
260
	 */
261
	abstract public function should_link_to_wp_admin();
262
263
	/**
264
	 * Create the desired menu output.
265
	 */
266
	abstract public function reregister_menu_items();
267
}
268