Completed
Push — try/e2e-check-if-chrome-instal... ( 24dbcf...296824 )
by Yaroslav
23:03 queued 13:54
created

class.jetpack-calypsoify.php ➔ get_site_suffix()   A

Complexity

Conditions 6

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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