Completed
Push — update/build-to-prepare-for-wp... ( a960c5...31a28f )
by
unknown
156:22 queued 147:01
created

Jetpack_Calypsoify::enqueue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
1
<?php
2
/**
3
 * This is Calypso skin of the wp-admin interface that is conditionally triggered via the ?calypsoify=1 param.
4
 *
5
 * @package Jetpack
6
 */
7
8
use Automattic\Jetpack\Dashboard_Customizations\Masterbar;
9
use Automattic\Jetpack\Redirect;
10
use Automattic\Jetpack\Status;
11
12
/**
13
 * Class Jetpack_Calypsoify
14
 */
15
class Jetpack_Calypsoify {
16
17
	/**
18
	 * Singleton instance of `Jetpack_Calypsoify`.
19
	 *
20
	 * @var object
21
	 */
22
	public static $instance = false;
23
24
	/**
25
	 * Is Calypsoify enabled, based on any value of `calypsoify` user meta.
26
	 *
27
	 * @var bool
28
	 */
29
	public $is_calypsoify_enabled = false;
30
31
	/**
32
	 * Jetpack_Calypsoify constructor.
33
	 */
34
	private function __construct() {
35
		add_action( 'wp_loaded', array( $this, 'setup' ) );
36
	}
37
38
	/**
39
	 * Original singleton.
40
	 *
41
	 * @todo We need to leave this in place until wpcomsh is updated. wpcomsh can be updated once 9.3.0 is stable.
42
	 *
43
	 * Deprecated 9.3.0
44
	 *
45
	 * @return Jetpack_Calypsoify
46
	 */
47
	public static function getInstance() { //phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
48
		_deprecated_function( __METHOD__, 'Jetpack 9.3.0', 'Jetpack_Calypsoify::get_instance' );
49
		return self::get_instance();
50
	}
51
52
	/**
53
	 * Singleton.
54
	 *
55
	 * @return Jetpack_Calypsoify
56
	 */
57
	public static function get_instance() {
58
		if ( ! self::$instance ) {
59
			self::$instance = new self();
60
		}
61
62
		return self::$instance;
63
	}
64
65
	/**
66
	 * Setup function that is loaded on the `wp_loaded` hook via the constructor.
67
	 */
68
	public function setup() {
69
		$this->is_calypsoify_enabled = 1 === (int) get_user_meta( get_current_user_id(), 'calypsoify', true );
70
		if ( isset( $_GET['calypsoify'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
71
			$this->is_calypsoify_enabled = 1 === (int) $_GET['calypsoify']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
72
		}
73
74
		add_action( 'admin_init', array( $this, 'check_param' ), 4 );
75
76
		if ( $this->is_calypsoify_enabled ) {
77
			add_action( 'admin_init', array( $this, 'setup_admin' ), 6 );
78
			add_action( 'admin_menu', array( $this, 'remove_core_menus' ), 100 );
79
			add_action( 'admin_menu', array( $this, 'add_custom_menus' ), 101 );
80
		}
81
	}
82
83
	/**
84
	 * Setup functionality within wp-admin via the `admin_init` hook.
85
	 */
86
	public function setup_admin() {
87
		// Masterbar is currently required for this to work properly. Mock the instance of it.
88
		if ( ! Jetpack::is_module_active( 'masterbar' ) ) {
89
			$this->mock_masterbar_activation();
90
		}
91
92
		if ( $this->is_page_gutenberg() ) {
93
			add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_for_gutenberg' ), 100 );
94
			return;
95
		}
96
97
		add_action( 'admin_init', array( $this, 'check_page' ) );
98
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ), 100 );
99
		add_action( 'in_admin_header', array( $this, 'insert_sidebar_html' ) );
100
		add_action( 'wp_before_admin_bar_render', array( $this, 'modify_masterbar' ), 100000 );
101
102
		add_filter( 'get_user_option_admin_color', array( $this, 'admin_color_override' ) );
103
104
		add_action( 'current_screen', array( $this, 'attach_views_filter' ) );
105
	}
106
107
	/**
108
	 * Set admin color.
109
	 *
110
	 * Used via the get_user_option_admin_color filter.
111
	 *
112
	 * @return string 'fresh'
113
	 */
114
	public function admin_color_override() {
115
		return 'fresh';
116
	}
117
118
	/**
119
	 * Mocks the Masterbar module.
120
	 *
121
	 * Calypsoify uses the Masterbar, so for sites without the Masterbar module active, this will use it for the sake of a Calyposify request.
122
	 *
123
	 * @return Masterbar
124
	 */
125
	public function mock_masterbar_activation() {
126
		include_once JETPACK__PLUGIN_DIR . 'modules/masterbar/masterbar/class-masterbar.php';
127
		return new Masterbar();
128
	}
129
130
	/**
131
	 * Removes Core's menu pages that we don't display.
132
	 */
133
	public function remove_core_menus() {
134
		remove_menu_page( 'edit.php?post_type=feedback' );
135
		remove_menu_page( 'index.php' );
136
		remove_menu_page( 'jetpack' );
137
		remove_menu_page( 'edit.php' );
138
		remove_menu_page( 'upload.php' );
139
		remove_menu_page( 'edit.php?post_type=page' );
140
		remove_menu_page( 'edit-comments.php' );
141
		remove_menu_page( 'themes.php' );
142
		remove_menu_page( 'plugins.php' );
143
		remove_menu_page( 'users.php' );
144
		remove_menu_page( 'tools.php' );
145
		remove_menu_page( 'link-manager.php' );
146
147
		// Core settings pages.
148
		remove_submenu_page( 'options-general.php', 'options-general.php' );
149
		remove_submenu_page( 'options-general.php', 'options-writing.php' );
150
		remove_submenu_page( 'options-general.php', 'options-reading.php' );
151
		remove_submenu_page( 'options-general.php', 'options-discussion.php' );
152
		remove_submenu_page( 'options-general.php', 'options-media.php' );
153
		remove_submenu_page( 'options-general.php', 'options-permalink.php' );
154
		remove_submenu_page( 'options-general.php', 'privacy.php' );
155
		remove_submenu_page( 'options-general.php', 'sharing' );
156
	}
157
158
	/**
159
	 * Adds the custom menus.
160
	 */
161
	public function add_custom_menus() {
162
		global $menu, $submenu;
163
164
		if ( isset( $_GET['post_type'] ) && 'feedback' === $_GET['post_type'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
165
			// there is currently no gridicon for feedback, so using dashicon.
166
			add_menu_page( __( 'Feedback', 'jetpack' ), __( 'Feedback', 'jetpack' ), 'edit_pages', 'edit.php?post_type=feedback', '', 'dashicons-feedback', 1 );
167
			remove_menu_page( 'options-general.php' );
168
			remove_submenu_page( 'edit.php?post_type=feedback', 'feedback-export' );
169
		} else {
170
			add_menu_page( __( 'Manage Plugins', 'jetpack' ), __( 'Manage Plugins', 'jetpack' ), 'activate_plugins', 'plugins.php', '', $this->installed_plugins_icon(), 1 );
171
			// Count the settings page submenus, if it's zero then don't show this.
172
			if ( empty( $submenu['options-general.php'] ) ) {
173
				remove_menu_page( 'options-general.php' );
174
			} else {
175
				// Rename and make sure the plugin settings menu is always last.
176
				// Sneaky plugins seem to override this otherwise.
177
				// Settings is always key 80.
178
				$menu[80][0]                            = __( 'Plugin Settings', 'jetpack' ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
179
				$menu[ max( array_keys( $menu ) ) + 1 ] = $menu[80]; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
180
				unset( $menu[80] );
181
			}
182
		}
183
	}
184
185
	/**
186
	 * Enqueues scripts, data, and styles.
187
	 */
188
	public function enqueue() {
189
		wp_enqueue_style( 'calypsoify_wpadminmods_css', plugin_dir_url( __FILE__ ) . 'style.min.css', false, JETPACK__VERSION );
190
		wp_style_add_data( 'calypsoify_wpadminmods_css', 'rtl', 'replace' );
191
		wp_style_add_data( 'calypsoify_wpadminmods_css', 'suffix', '.min' );
192
193
		wp_enqueue_script( 'calypsoify_wpadminmods_js', plugin_dir_url( __FILE__ ) . 'mods.js', false, JETPACK__VERSION, false );
194
		wp_localize_script(
195
			'calypsoify_wpadminmods_js',
196
			'CalypsoifyOpts',
197
			array(
198
				'nonces' => array(
199
					'autoupdate_plugins'              => wp_create_nonce( 'jetpack_toggle_autoupdate-plugins' ),
200
					'autoupdate_plugins_translations' => wp_create_nonce( 'jetpack_toggle_autoupdate-plugins_translations' ),
201
				),
202
			)
203
		);
204
	}
205
206
	/**
207
	 * Enqueues scripts, data, and styles for Gutenberg.
208
	 */
209
	public function enqueue_for_gutenberg() {
210
		wp_enqueue_style( 'calypsoify_wpadminmods_css', plugin_dir_url( __FILE__ ) . 'style-gutenberg.min.css', false, JETPACK__VERSION );
211
		wp_style_add_data( 'calypsoify_wpadminmods_css', 'rtl', 'replace' );
212
		wp_style_add_data( 'calypsoify_wpadminmods_css', 'suffix', '.min' );
213
214
		wp_enqueue_script( 'calypsoify_wpadminmods_js', plugin_dir_url( __FILE__ ) . 'mods-gutenberg.js', false, JETPACK__VERSION, false );
215
		wp_localize_script(
216
			'calypsoify_wpadminmods_js',
217
			'calypsoifyGutenberg',
218
			array(
219
				'closeUrl'                => $this->get_close_gutenberg_url(),
220
				'manageReusableBlocksUrl' => $this->get_calypso_origin() . '/types/wp_block/' . ( new Status() )->get_site_suffix(),
221
			)
222
		);
223
	}
224
225
	/**
226
	 * Inserts Sidebar HTML
227
	 *
228
	 * @return void
229
	 */
230
	public function insert_sidebar_html() {
231
		$heading  = ( isset( $_GET['post_type'] ) && 'feedback' === $_GET['post_type'] ) ? __( 'Feedback', 'jetpack' ) : __( 'Plugins', 'jetpack' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
232
		$home_url = Redirect::get_url( 'calypso-home' );
233
		?>
234
		<a href="<?php echo esc_url( $home_url ); ?>" id="calypso-sidebar-header">
235
			<svg class="gridicon gridicons-chevron-left" height="24" width="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><g><path d="M14 20l-8-8 8-8 1.414 1.414L8.828 12l6.586 6.586"></path></g></svg>
236
237
			<ul>
238
				<li id="calypso-sitename"><?php bloginfo( 'name' ); ?></li>
239
				<li id="calypso-plugins"><?php echo esc_html( $heading ); ?></li>
240
			</ul>
241
		</a>
242
		<?php
243
	}
244
245
	/**
246
	 * Modifies the masterbar.
247
	 */
248
	public function modify_masterbar() {
249
		global $wp_admin_bar;
250
251
		// Add proper links to masterbar top sections.
252
		$my_sites_node       = (object) $wp_admin_bar->get_node( 'blog' );
253
		$my_sites_node->href = Redirect::get_url( 'calypso-home' );
254
		$wp_admin_bar->add_node( $my_sites_node );
255
256
		$reader_node       = (object) $wp_admin_bar->get_node( 'newdash' );
257
		$reader_node->href = Redirect::get_url( 'calypso-read' );
258
		$wp_admin_bar->add_node( $reader_node );
259
260
		$me_node       = (object) $wp_admin_bar->get_node( 'my-account' );
261
		$me_node->href = Redirect::get_url( 'calypso-me' );
262
		$wp_admin_bar->add_node( $me_node );
263
	}
264
265
	/**
266
	 * Returns a SVG of the installed plugins icon.
267
	 *
268
	 * @return string SVG+XML of the installed plugins icon.
269
	 */
270
	private function installed_plugins_icon() {
271
		$svg = '<svg class="gridicon gridicons-plugins" height="24" width="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 24"><g><path d="M16 8V3c0-.552-.448-1-1-1s-1 .448-1 1v5h-4V3c0-.552-.448-1-1-1s-1 .448-1 1v5H5v4c0 2.79 1.637 5.193 4 6.317V22h6v-3.683c2.363-1.124 4-3.527 4-6.317V8h-3z" fill="black"></path></g></svg>';
272
273
		return 'data:image/svg+xml;base64,' . base64_encode( $svg ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
274
	}
275
276
	/**
277
	 * Returns the Calypso domain that originated the current request.
278
	 *
279
	 * @return string
280
	 */
281
	private function get_calypso_origin() {
282
		$origin  = ! empty( $_GET['origin'] ) ? $_GET['origin'] : 'https://wordpress.com'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
283
		$allowed = array(
284
			'http://calypso.localhost:3000',
285
			'http://127.0.0.1:41050', // Desktop App.
286
			'https://wpcalypso.wordpress.com',
287
			'https://horizon.wordpress.com',
288
			'https://wordpress.com',
289
		);
290
		return in_array( $origin, $allowed, true ) ? $origin : 'https://wordpress.com';
291
	}
292
293
	/**
294
	 * Returns the Calypso URL that displays either the current post type list (if no args
295
	 * are supplied) or the classic editor for the current post (if a post ID is supplied).
296
	 *
297
	 * @param int|null $post_id Post ID.
298
	 *
299
	 * @return string
300
	 */
301
	public function get_calypso_url( $post_id = null ) {
302
		$screen      = get_current_screen();
303
		$post_type   = $screen->post_type;
304
		$site_suffix = ( new Status() )->get_site_suffix();
305
306
		if ( is_null( $post_id ) ) {
307
			// E.g. posts or pages have no special suffix. CPTs are in the `types/{cpt}` format.
308
			$post_type_suffix = ( 'post' === $post_type || 'page' === $post_type )
309
				? "/${post_type}s/"
310
				: "/types/${post_type}/";
311
			$post_suffix      = '';
312
		} else {
313
			$post_type_suffix = ( 'post' === $post_type || 'page' === $post_type )
314
				? "/${post_type}/"
315
				: "/edit/${post_type}/";
316
			$post_suffix      = "/${post_id}";
317
		}
318
319
		return $this->get_calypso_origin() . $post_type_suffix . $site_suffix . $post_suffix;
320
	}
321
322
	/**
323
	 * Returns the URL to be used on the block editor close button for going back to the
324
	 * Calypso post list.
325
	 *
326
	 * @return string
327
	 */
328
	public function get_close_gutenberg_url() {
329
		return $this->get_calypso_url();
330
	}
331
332
	/**
333
	 * Returns the URL for switching the user's editor to the Calypso (WordPress.com Classic) editor.
334
	 *
335
	 * @return string
336
	 */
337
	public function get_switch_to_classic_editor_url() {
338
		return add_query_arg(
339
			'set-editor',
340
			'classic',
341
			$this->is_calypsoify_enabled ? $this->get_calypso_url( get_the_ID() ) : false
342
		);
343
	}
344
345
	/**
346
	 * Checks for the URL parameter if this is a Calypsoify request.
347
	 */
348
	public function check_param() {
349
		if ( isset( $_GET['calypsoify'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
350
			if ( 1 === (int) $_GET['calypsoify'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
351
				update_user_meta( get_current_user_id(), 'calypsoify', 1 );
352
			} else {
353
				update_user_meta( get_current_user_id(), 'calypsoify', 0 );
354
			}
355
		}
356
	}
357
358
	/**
359
	 * If the visitor is hitting wp-admin/ then disable this functionality.
360
	 */
361
	public function check_page() {
362
		// If the user hits plain /wp-admin/ then disable Calypso styles.
363
		$page = wp_basename( esc_url( $_SERVER['REQUEST_URI'] ) );
364
365
		if ( false !== strpos( 'index.php', $page ) || false !== strpos( 'wp-admin', $page ) ) {
366
			update_user_meta( get_current_user_id(), 'calypsoify', 0 );
367
			wp_safe_redirect( admin_url() );
368
			die;
369
		}
370
	}
371
372
	/**
373
	 * Return whether a post type should display the Gutenberg/block editor.
374
	 *
375
	 * @since 6.7.0
376
	 *
377
	 * @param string $post_type Post type.
378
	 */
379
	public function is_post_type_gutenberg( $post_type ) {
380
		return use_block_editor_for_post_type( $post_type );
381
	}
382
383
	/**
384
	 * Determines if the page is an instance of the Gutenberg block editor.
385
	 *
386
	 * @return bool
387
	 */
388
	public function is_page_gutenberg() {
389
		// phpcs:disable WordPress.Security.NonceVerification.Recommended
390
		// Disabling WordPress.Security.NonceVerification.Recommended because this function fires within admin_init and this is only changing display.
391
		$page = wp_basename( esc_url( $_SERVER['REQUEST_URI'] ) );
392
393
		if ( false !== strpos( $page, 'post-new.php' ) && empty( $_GET['post_type'] ) ) {
394
			return true;
395
		}
396
397
		if ( false !== strpos( $page, 'post-new.php' ) && isset( $_GET['post_type'] ) && $this->is_post_type_gutenberg( $_GET['post_type'] ) ) {
398
			return true;
399
		}
400
401 View Code Duplication
		if ( false !== strpos( $page, 'post.php' ) ) {
402
			$post = get_post( $_GET['post'] );
403
			if ( isset( $post ) && isset( $post->post_type ) && $this->is_post_type_gutenberg( $post->post_type ) ) {
404
				return true;
405
			}
406
		}
407
408 View Code Duplication
		if ( false !== strpos( $page, 'revision.php' ) ) {
409
			$post   = get_post( $_GET['revision'] );
410
			$parent = get_post( $post->post_parent );
411
			if ( isset( $parent ) && isset( $parent->post_type ) && $this->is_post_type_gutenberg( $parent->post_type ) ) {
412
				return true;
413
			}
414
		}
415
416
		return false;
417
		// phpcs:enable
418
	}
419
420
	/**
421
	 * Attach a WP_List_Table views filter to all screens.
422
	 *
423
	 * @param WP_Screen $current_screen Current WP_Screen instance.
424
	 */
425
	public function attach_views_filter( $current_screen ) {
426
		add_filter( "views_{$current_screen->id}", array( $this, 'filter_views' ) );
427
	}
428
429
	/**
430
	 * Remove the parentheses from list table view counts when Calypsofied.
431
	 *
432
	 * @param array $views Array of views. See: WP_List_Table::get_views().
433
	 * @return array Filtered views.
434
	 */
435
	public function filter_views( $views ) {
436
		foreach ( $views as $id => $view ) {
437
			$views[ $id ] = preg_replace( '/<span class="count">\((\d+)\)<\/span>/', '<span class="count">$1</span>', $view );
438
		}
439
440
		return $views;
441
	}
442
}
443
444
Jetpack_Calypsoify::get_instance();
445