Completed
Push — renovate/debug-4.x ( e10916...91e0c8 )
by
unknown
86:28 queued 77:02
created

Jetpack_Calypsoify::enqueue_for_gutenberg()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
nc 1
nop 0
1
<?php
2
/**
3
 * This is Calypso skin of the wp-admin interface that is conditionally triggered via the ?calypsoify=1 param.
4
 * Ported from an internal Automattic plugin.
5
 */
6
7
use Automattic\Jetpack\Redirect;
8
use Automattic\Jetpack\Status;
9
10
class Jetpack_Calypsoify {
11
12
	/**
13
	 * Singleton instance of `Jetpack_Calypsoify`.
14
	 *
15
	 * @var object
16
	 */
17
	public static $instance = false;
18
19
	/**
20
	 * Is Calypsoify enabled, based on any value of `calypsoify` user meta.
21
	 *
22
	 * @var bool
23
	 */
24
	public $is_calypsoify_enabled = false;
25
26
	private function __construct() {
27
		add_action( 'wp_loaded', array( $this, 'setup' ) );
28
	}
29
30
	public static function getInstance() {
31
		if ( ! self::$instance ) {
32
			self::$instance = new self();
33
		}
34
35
		return self::$instance;
36
	}
37
38
	public function setup() {
39
		$this->is_calypsoify_enabled = 1 == (int) get_user_meta( get_current_user_id(), 'calypsoify', true );
40
		if ( isset( $_GET['calypsoify'] ) ) {
41
			$this->is_calypsoify_enabled = 1 === (int) $_GET['calypsoify'];
42
		}
43
44
		add_action( 'admin_init', array( $this, 'check_param' ), 4 );
45
46
		if ( $this->is_calypsoify_enabled ) {
47
			add_action( 'admin_init', array( $this, 'setup_admin' ), 6 );
48
			add_action( 'admin_menu', array( $this, 'remove_core_menus' ), 100 );
49
			add_action( 'admin_menu', array( $this, 'add_custom_menus' ), 101 );
50
		}
51
	}
52
53
	public function setup_admin() {
54
		// Masterbar is currently required for this to work properly. Mock the instance of it
55
		if ( ! Jetpack::is_module_active( 'masterbar' ) ) {
56
			$this->mock_masterbar_activation();
57
		}
58
59
		if ( $this->is_page_gutenberg() ) {
60
			add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_for_gutenberg' ), 100 );
61
			return;
62
		}
63
64
		add_action( 'admin_init', array( $this, 'check_page' ) );
65
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ), 100 );
66
		add_action( 'in_admin_header', array( $this, 'insert_sidebar_html' ) );
67
		add_action( 'wp_before_admin_bar_render', array( $this, 'modify_masterbar' ), 100000 );
68
69
		add_filter( 'get_user_option_admin_color', array( $this, 'admin_color_override' ) );
70
71
		add_action( 'current_screen', array( $this, 'attach_views_filter' ) );
72
	}
73
74
	public function admin_color_override( $color ) {
75
		return 'fresh';
76
	}
77
78
	public function mock_masterbar_activation() {
79
		include_once JETPACK__PLUGIN_DIR . 'modules/masterbar/masterbar.php';
80
		new A8C_WPCOM_Masterbar;
81
	}
82
83
	public function remove_core_menus() {
84
		remove_menu_page( 'edit.php?post_type=feedback' );
85
		remove_menu_page( 'index.php' );
86
		remove_menu_page( 'jetpack' );
87
		remove_menu_page( 'edit.php' );
88
		remove_menu_page( 'upload.php' );
89
		remove_menu_page( 'edit.php?post_type=page' );
90
		remove_menu_page( 'edit-comments.php' );
91
		remove_menu_page( 'themes.php' );
92
		remove_menu_page( 'plugins.php' );
93
		remove_menu_page( 'users.php' );
94
		remove_menu_page( 'tools.php' );
95
		remove_menu_page( 'link-manager.php' );
96
97
		// Core settings pages
98
		remove_submenu_page( 'options-general.php', 'options-general.php' );
99
		remove_submenu_page( 'options-general.php', 'options-writing.php' );
100
		remove_submenu_page( 'options-general.php', 'options-reading.php' );
101
		remove_submenu_page( 'options-general.php', 'options-discussion.php' );
102
		remove_submenu_page( 'options-general.php', 'options-media.php' );
103
		remove_submenu_page( 'options-general.php', 'options-permalink.php' );
104
		remove_submenu_page( 'options-general.php', 'privacy.php' );
105
		remove_submenu_page( 'options-general.php', 'sharing' );
106
	}
107
108
	public function add_custom_menus() {
109
		global $menu, $submenu;
110
111
		if ( isset( $_GET['post_type'] ) && 'feedback' === $_GET['post_type'] ) {
112
			// there is currently no gridicon for feedback, so using dashicon.
113
			add_menu_page( __( 'Feedback', 'jetpack' ), __( 'Feedback', 'jetpack' ), 'edit_pages', 'edit.php?post_type=feedback', '', 'dashicons-feedback', 1 );
114
			remove_menu_page( 'options-general.php' );
115
			remove_submenu_page( 'edit.php?post_type=feedback', 'feedback-export' );
116
		} else {
117
			add_menu_page( __( 'Manage Plugins', 'jetpack' ), __( 'Manage Plugins', 'jetpack' ), 'activate_plugins', 'plugins.php', '', $this->installed_plugins_icon(), 1 );
118
			// Count the settings page submenus, if it's zero then don't show this.
119
			if ( empty( $submenu['options-general.php'] ) ) {
120
				remove_menu_page( 'options-general.php' );
121
			} else {
122
				// Rename and make sure the plugin settings menu is always last.
123
				// Sneaky plugins seem to override this otherwise.
124
				// Settings is always key 80.
125
				$menu[80][0]                            = __( 'Plugin Settings', 'jetpack' );
126
				$menu[ max( array_keys( $menu ) ) + 1 ] = $menu[80];
127
				unset( $menu[80] );
128
			}
129
		}
130
	}
131
132
	public function enqueue() {
133
		wp_enqueue_style( 'calypsoify_wpadminmods_css', plugin_dir_url( __FILE__ ) . 'style.min.css', false, JETPACK__VERSION );
134
		wp_style_add_data( 'calypsoify_wpadminmods_css', 'rtl', 'replace' );
135
        wp_style_add_data( 'calypsoify_wpadminmods_css', 'suffix', '.min' );
136
137
		wp_enqueue_script( 'calypsoify_wpadminmods_js', plugin_dir_url( __FILE__ ) . 'mods.js', false, JETPACK__VERSION );
138
		wp_localize_script( 'calypsoify_wpadminmods_js', 'CalypsoifyOpts', array(
139
			'nonces' => array(
140
				'autoupdate_plugins' => wp_create_nonce( 'jetpack_toggle_autoupdate-plugins' ),
141
				'autoupdate_plugins_translations' => wp_create_nonce( 'jetpack_toggle_autoupdate-plugins_translations' ),
142
			)
143
		) );
144
	}
145
146
	public function enqueue_for_gutenberg() {
147
		wp_enqueue_style( 'calypsoify_wpadminmods_css', plugin_dir_url( __FILE__ ) . 'style-gutenberg.min.css', false, JETPACK__VERSION );
148
		wp_style_add_data( 'calypsoify_wpadminmods_css', 'rtl', 'replace' );
149
        wp_style_add_data( 'calypsoify_wpadminmods_css', 'suffix', '.min' );
150
151
		wp_enqueue_script( 'calypsoify_wpadminmods_js', plugin_dir_url( __FILE__ ) . 'mods-gutenberg.js', false, JETPACK__VERSION );
152
		wp_localize_script(
153
			'calypsoify_wpadminmods_js',
154
			'calypsoifyGutenberg',
155
			array(
156
				'closeUrl'                => $this->get_close_gutenberg_url(),
157
				'manageReusableBlocksUrl' => $this->get_calypso_origin() . '/types/wp_block/' . ( new Status() )->get_site_suffix(),
158
			)
159
		);
160
	}
161
162
	/**
163
	 * Inserts Sidebar HTML
164
	 *
165
	 * @return void
166
	 */
167
	public function insert_sidebar_html() {
168
		$heading       = ( isset( $_GET['post_type'] ) && 'feedback' === $_GET['post_type'] ) ? __( 'Feedback', 'jetpack' ) : __( 'Plugins', 'jetpack' );
169
		$home_url = Redirect::get_url( 'calypso-home' );
170
		?>
171
		<a href="<?php echo esc_url( $home_url ); ?>" id="calypso-sidebar-header">
172
			<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>
173
174
			<ul>
175
				<li id="calypso-sitename"><?php bloginfo( 'name' ); ?></li>
176
				<li id="calypso-plugins"><?php echo esc_html( $heading ); ?></li>
177
			</ul>
178
		</a>
179
		<?php
180
	}
181
182
	public function modify_masterbar() {
183
		global $wp_admin_bar;
184
185
		// Add proper links to masterbar top sections.
186
		$my_sites_node       = (object) $wp_admin_bar->get_node( 'blog' );
187
		$my_sites_node->href = Redirect::get_url( 'calypso-home' );
188
		$wp_admin_bar->add_node( $my_sites_node );
189
190
		$reader_node       = (object) $wp_admin_bar->get_node( 'newdash' );
191
		$reader_node->href = Redirect::get_url( 'calypso-read' );
192
		$wp_admin_bar->add_node( $reader_node );
193
194
		$me_node       = (object) $wp_admin_bar->get_node( 'my-account' );
195
		$me_node->href = Redirect::get_url( 'calypso-me' );
196
		$wp_admin_bar->add_node( $me_node );
197
	}
198
199
	private function installed_plugins_icon() {
200
		$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>';
201
202
		return 'data:image/svg+xml;base64,' . base64_encode( $svg );
203
	}
204
205
	/**
206
	 * Returns the Calypso domain that originated the current request.
207
	 *
208
	 * @return string
209
	 */
210
	private function get_calypso_origin() {
211
		$origin    = ! empty( $_GET['origin'] ) ? $_GET['origin'] : 'https://wordpress.com';
212
		$allowed = array(
213
			'http://calypso.localhost:3000',
214
			'http://127.0.0.1:41050', // Desktop App
215
			'https://wpcalypso.wordpress.com',
216
			'https://horizon.wordpress.com',
217
			'https://wordpress.com',
218
		);
219
		return in_array( $origin, $allowed, true ) ? $origin : 'https://wordpress.com';
220
	}
221
222
	/**
223
	 * Returns the Calypso URL that displays either the current post type list (if no args
224
	 * are supplied) or the classic editor for the current post (if a post ID is supplied).
225
	 *
226
	 * @param int|null $post_id
227
	 * @return string
228
	 */
229
	public function get_calypso_url( $post_id = null ) {
230
		$screen      = get_current_screen();
231
		$post_type   = $screen->post_type;
232
		$site_suffix = ( new Status() )->get_site_suffix();
233
234
		if ( is_null( $post_id ) ) {
235
			// E.g. `posts`, `pages`, or `types/some_custom_post_type`
236
			$post_type_suffix = ( 'post' === $post_type || 'page' === $post_type )
237
				? "/${post_type}s/"
238
				: "/types/${post_type}/";
239
			$post_suffix = '';
240
		} else {
241
			$post_type_suffix = ( 'post' === $post_type || 'page' === $post_type )
242
				? "/${post_type}/"
243
				: "/edit/${post_type}/";
244
			$post_suffix = "/${post_id}";
245
		}
246
247
		return $this->get_calypso_origin() . $post_type_suffix . $site_suffix . $post_suffix;
248
	}
249
250
	/**
251
	 * Returns the URL to be used on the block editor close button for going back to the
252
	 * Calypso post list.
253
	 *
254
	 * @return string
255
	 */
256
	public function get_close_gutenberg_url() {
257
		return $this->get_calypso_url();
258
	}
259
260
	/**
261
	 * Returns the URL for switching the user's editor to the Calypso (WordPress.com Classic) editor.
262
	 *
263
	 * @return string
264
	 */
265
	public function get_switch_to_classic_editor_url() {
266
		return add_query_arg(
267
			'set-editor',
268
			'classic',
269
			$this->is_calypsoify_enabled ? $this->get_calypso_url( get_the_ID() ) : false
270
		);
271
	}
272
273
	public function check_param() {
274
		if ( isset( $_GET['calypsoify'] ) ) {
275
			if ( 1 == (int) $_GET['calypsoify'] ) {
276
				update_user_meta( get_current_user_id(), 'calypsoify', 1 );
277
			} else {
278
				update_user_meta( get_current_user_id(), 'calypsoify', 0 );
279
			}
280
		}
281
	}
282
283
	public function check_page() {
284
		// If the user hits plain /wp-admin/ then disable Calypso styles.
285
		$page = wp_basename( esc_url( $_SERVER['REQUEST_URI'] ) );
286
287
		if ( false !== strpos( 'index.php', $page ) || false !== strpos( 'wp-admin', $page ) ) {
288
			update_user_meta( get_current_user_id(), 'calypsoify', 0 );
289
			wp_safe_redirect( admin_url() );
290
			die;
291
		}
292
	}
293
294
	/**
295
	 * Return whether a post type should display the Gutenberg/block editor.
296
	 *
297
	 * @since 6.7.0
298
	 */
299
	public function is_post_type_gutenberg( $post_type ) {
300
		return use_block_editor_for_post_type( $post_type );
301
	}
302
303
	public function is_page_gutenberg() {
304
		$page = wp_basename( esc_url( $_SERVER['REQUEST_URI'] ) );
305
306
		if ( false !== strpos( $page, 'post-new.php' ) && empty ( $_GET['post_type'] ) ) {
307
			return true;
308
		}
309
310
		if ( false !== strpos( $page, 'post-new.php' ) && isset( $_GET['post_type'] ) && $this->is_post_type_gutenberg( $_GET['post_type'] ) ) {
311
			return true;
312
		}
313
314 View Code Duplication
		if ( false !== strpos( $page, 'post.php' ) ) {
315
			$post = get_post( $_GET['post'] );
316
			if ( isset( $post ) && isset( $post->post_type ) && $this->is_post_type_gutenberg( $post->post_type ) ) {
317
				return true;
318
			}
319
		}
320
321 View Code Duplication
		if ( false !== strpos( $page, 'revision.php' ) ) {
322
			$post   = get_post( $_GET['revision'] );
323
			$parent = get_post( $post->post_parent );
324
			if ( isset( $parent ) && isset( $parent->post_type ) && $this->is_post_type_gutenberg( $parent->post_type ) ) {
325
				return true;
326
			}
327
		}
328
329
		return false;
330
	}
331
332
	/**
333
	 * Attach a WP_List_Table views filter to all screens.
334
	 */
335
	public function attach_views_filter( $current_screen ) {
336
		add_filter( "views_{$current_screen->id}", array( $this, 'filter_views' ) );
337
	}
338
339
	/**
340
	 * Remove the parentheses from list table view counts when Calypsofied.
341
	 *
342
	 * @param array $views Array of views. See: WP_List_Table::get_views().
343
	 * @return array Filtered views.
344
	 */
345
	public function filter_views( $views ) {
346
		foreach ( $views as $id => $view ) {
347
			$views[ $id ] = preg_replace( '/<span class="count">\((\d+)\)<\/span>/', '<span class="count">$1</span>', $view );
348
		}
349
350
		return $views;
351
	}
352
}
353
354
$Jetpack_Calypsoify = Jetpack_Calypsoify::getInstance();
355