Completed
Push — update/admin-menu-sync-wpcom ( b0ac91...43248e )
by
unknown
124:17 queued 113:08
created

Base_Admin_Menu::enqueue_scripts()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 18
nop 0
dl 0
loc 36
rs 8.7217
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
		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
		$is_wpcom = defined( 'IS_WPCOM' ) && IS_WPCOM;
200
201
		if ( $is_wpcom ) {
202
			$style_dependencies = array( 'wpcom-admin-bar', 'wpcom-masterbar-css' );
203
		} elseif ( $this->is_rtl() ) {
204
			$style_dependencies = array( 'a8c-wpcom-masterbar-rtl', 'a8c-wpcom-masterbar-overrides-rtl' );
205
		} else {
206
			$style_dependencies = array( 'a8c-wpcom-masterbar', 'a8c-wpcom-masterbar-overrides' );
207
		}
208
209
		if ( $this->is_rtl() ) {
210
			if ( $is_wpcom ) {
211
				$css_path = 'rtl/admin-menu-rtl.css';
212
			} else {
213
				$css_path = 'admin-menu-rtl.css';
214
			}
215
		} else {
216
			$css_path = 'admin-menu.css';
217
		}
218
219
		wp_enqueue_style(
220
			'jetpack-admin-menu',
221
			plugins_url( $css_path, __FILE__ ),
222
			$style_dependencies,
223
			JETPACK__VERSION
224
		);
225
226
		wp_enqueue_script(
227
			'jetpack-admin-menu',
228
			plugins_url( 'admin-menu.js', __FILE__ ),
229
			array(),
230
			JETPACK__VERSION,
231
			true
232
		);
233
	}
234
235
	/**
236
	 * Remove submenu items from given menu slug.
237
	 *
238
	 * @param string $slug Menu slug.
239
	 */
240
	public function remove_submenus( $slug ) {
241
		global $submenu;
242
243
		if ( isset( $submenu[ $slug ] ) ) {
244
			// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
245
			$submenu[ $slug ] = array();
246
		}
247
	}
248
249
	/**
250
	 * Determines whether the current locale is right-to-left (RTL).
251
	 */
252
	public function is_rtl() {
253
		return is_rtl();
254
	}
255
256
	/**
257
	 * Whether to use wp-admin pages rather than Calypso.
258
	 *
259
	 * Options:
260
	 * false - Calypso (Default).
261
	 * true  - wp-admin.
262
	 *
263
	 * @return bool
264
	 */
265
	abstract public function should_link_to_wp_admin();
266
267
	/**
268
	 * Create the desired menu output.
269
	 */
270
	abstract public function reregister_menu_items();
271
}
272