Completed
Push — add/sync-partial-sync-checksum... ( 93f4e2...c9ed3f )
by
unknown
253:26 queued 243:12
created

Jetpack_Calypsoify::enqueue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 13
rs 9.8333
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\Redirect;
8
9
class Jetpack_Calypsoify {
10
11
	/**
12
	 * Singleton instance of `Jetpack_Calypsoify`.
13
	 *
14
	 * @var object
15
	 */
16
	public static $instance = false;
17
18
	/**
19
	 * Is Calypsoify enabled, based on any value of `calypsoify` user meta.
20
	 *
21
	 * @var bool
22
	 */
23
	public $is_calypsoify_enabled = false;
24
25
	private function __construct() {
26
		add_action( 'wp_loaded', array( $this, 'setup' ) );
27
	}
28
29
	public static function getInstance() {
30
		if ( ! self::$instance ) {
31
			self::$instance = new self();
32
		}
33
34
		return self::$instance;
35
	}
36
37
	public function setup() {
38
		$this->is_calypsoify_enabled = 1 == (int) get_user_meta( get_current_user_id(), 'calypsoify', true );
39
		if ( isset( $_GET['calypsoify'] ) ) {
40
			$this->is_calypsoify_enabled = 1 === (int) $_GET['calypsoify'];
41
		}
42
		
43
		add_action( 'admin_init', array( $this, 'check_param' ), 4 );
44
45
		if ( $this->is_calypsoify_enabled ) {
46
			add_action( 'admin_init', array( $this, 'setup_admin' ), 6 );
47
			add_action( 'admin_menu', array( $this, 'remove_core_menus' ), 100 );
48
			add_action( 'admin_menu', array( $this, 'add_custom_menus' ), 101 );
49
		}
50
51
		// Make this always available -- in case calypsoify gets toggled off.
52
		add_action( 'wp_ajax_jetpack_toggle_autoupdate', array( $this, 'jetpack_toggle_autoupdate' ) );
53
		add_filter( 'handle_bulk_actions-plugins', array( $this, 'handle_bulk_actions_plugins' ), 10, 3 );
54
	}
55
56
	public function setup_admin() {
57
		global $wp_version;
58
		// Masterbar is currently required for this to work properly. Mock the instance of it
59
		if ( ! Jetpack::is_module_active( 'masterbar' ) ) {
60
			$this->mock_masterbar_activation();
61
		}
62
63
		if ( $this->is_page_gutenberg() ) {
64
			add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_for_gutenberg' ), 100 );
65
			return;
66
		}
67
68
		add_action( 'admin_init', array( $this, 'check_page' ) );
69
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ), 100 );
70
		add_action( 'in_admin_header', array( $this, 'insert_sidebar_html' ) );
71
		add_action( 'wp_before_admin_bar_render', array( $this, 'modify_masterbar' ), 100000 );
72
73
		add_filter( 'get_user_option_admin_color', array( $this, 'admin_color_override' ) );
74
75
		// The following three add the autoupdates UI, which we aren't adding for WP 5.5.
76
		if ( version_compare( '5.5-alpha', $wp_version, '>=' ) ) {
77
			add_action( 'manage_plugins_columns', array( $this, 'manage_plugins_columns_header' ) );
78
			add_action( 'manage_plugins_custom_column', array( $this, 'manage_plugins_custom_column' ), 10, 2 );
79
			add_filter( 'bulk_actions-plugins', array( $this, 'bulk_actions_plugins' ) );
80
		}
81
82
		add_action( 'current_screen', array( $this, 'attach_views_filter' ) );
83
84
		if ( 'plugins.php' === basename( $_SERVER['PHP_SELF'] ) ) {
85
			add_action( 'admin_notices', array( $this, 'plugins_admin_notices' ) );
86
		}
87
	}
88
89
	public function manage_plugins_columns_header( $columns ) {
90
		if ( current_user_can( 'jetpack_manage_autoupdates' ) ) {
91
			$columns['autoupdate'] = __( 'Automatic Update', 'jetpack' );
92
		}
93
		return $columns;
94
	}
95
96
	public function manage_plugins_custom_column( $column_name, $slug ) {
97
		static $repo_plugins = array();
98
99
		if ( ! current_user_can( 'jetpack_manage_autoupdates' ) ) {
100
			return;
101
		}
102
103
		if ( empty( $repo_plugins ) ) {
104
			$repo_plugins = self::get_dotorg_repo_plugins();
105
		}
106
107
		$autoupdating_plugins = (array) get_site_option( 'auto_update_plugins', array() );
108
		// $autoupdating_plugins_translations = Jetpack_Options::get_option( 'autoupdate_plugins_translations', array() );
109
		if ( 'autoupdate' === $column_name ) {
110
			if ( ! in_array( $slug, $repo_plugins ) ) {
111
				return;
112
			}
113
			// Shamelessly swiped from https://github.com/Automattic/wp-calypso/blob/59bdfeeb97eda4266ad39410cb0a074d2c88dbc8/client/components/forms/form-toggle
114
			?>
115
116
			<span class="form-toggle__wrapper">
117
				<input
118
					id="autoupdate_plugin-toggle-<?php echo esc_attr( $slug ) ?>"
119
					name="autoupdate_plugins[<?php echo esc_attr( $slug ) ?>]"
120
					value="autoupdate"
121
					class="form-toggle autoupdate-toggle"
122
					type="checkbox"
123
					<?php checked( in_array( $slug, $autoupdating_plugins ) ); ?>
124
					readonly
125
					data-slug="<?php echo esc_attr( $slug ); ?>"
126
				/>
127
				<label class="form-toggle__label" for="autoupdate_plugin-toggle-<?php echo esc_attr( $slug ) ?>">
128
					<span class="form-toggle__switch" role="checkbox"></span>
129
					<span class="form-toggle__label-content"><?php /*  */ ?></span>
130
				</label>
131
			</span>
132
133
			<?php
134
		}
135
	}
136
137
	public static function get_dotorg_repo_plugins() {
138
		$plugins = get_site_transient( 'update_plugins' );
139
		return array_merge( array_keys( $plugins->response ), array_keys( $plugins->no_update ) );
140
	}
141
142
	/**
143
	 * Remove when WP 5.5 is the min ver.
144
	 *
145
	 * @param array $bulk_actions Bulk actions array.
146
	 */
147
	public function bulk_actions_plugins( $bulk_actions ) {
148
		$bulk_actions['jetpack_enable_plugin_autoupdates'] = __( 'Enable Automatic Updates', 'jetpack' );
149
		$bulk_actions['jetpack_disable_plugin_autoupdates'] = __( 'Disable Automatic Updates', 'jetpack' );
150
		return $bulk_actions;
151
	}
152
153
	public function handle_bulk_actions_plugins( $redirect_to, $action, $slugs ) {
154
		$redirect_to = remove_query_arg( array( 'jetpack_enable_plugin_autoupdates', 'jetpack_disable_plugin_autoupdates' ), $redirect_to );
155
		if ( in_array( $action, array( 'jetpack_enable_plugin_autoupdates', 'jetpack_disable_plugin_autoupdates' ) ) ) {
156
			$list        = (array) get_site_option( 'auto_update_plugins', array() );
157
			$initial_qty = sizeof( $list );
158
159
			if ( 'jetpack_enable_plugin_autoupdates' === $action ) {
160
				$list = array_unique( array_merge( $list, $slugs ) );
161
			} elseif ( 'jetpack_disable_plugin_autoupdates' === $action ) {
162
				$list = array_diff( $list, $slugs );
163
			}
164
165
			update_site_option( 'auto_update_plugins', $list );
166
			$redirect_to = add_query_arg( $action, absint( sizeof( $list ) - $initial_qty ), $redirect_to );
167
		}
168
		return $redirect_to;
169
	}
170
171
	/**
172
	 * Remove when WP 5.5 is the min ver.
173
	 */
174
	public function plugins_admin_notices() {
175
		if ( ! empty( $_GET['jetpack_enable_plugin_autoupdates'] ) ) {
176
			$qty = (int) $_GET['jetpack_enable_plugin_autoupdates'];
177
			printf( '<div id="message" class="updated fade"><p>' . _n( 'Enabled automatic updates on %d plugin.', 'Enabled automatic updates on %d plugins.', $qty, 'jetpack' ) . '</p></div>', $qty );
178
		} elseif ( ! empty( $_GET['jetpack_disable_plugin_autoupdates'] ) ) {
179
			$qty = (int) $_GET['jetpack_disable_plugin_autoupdates'];
180
			printf( '<div id="message" class="updated fade"><p>' . _n( 'Disabled automatic updates on %d plugin.', 'Disabled automatic updates on %d plugins.', $qty, 'jetpack' ) . '</p></div>', $qty );
181
		}
182
	}
183
184
	/**
185
	 * Remove when WP 5.5 is the min ver.
186
	 */
187
	public function jetpack_toggle_autoupdate() {
188
		if ( ! current_user_can( 'jetpack_manage_autoupdates' ) ) {
189
			wp_send_json_error();
190
			return;
191
		}
192
193
		$type   = $_POST['type'];
194
		$slug   = $_POST['slug'];
195
		$active = 'false' !== $_POST['active'];
196
197
		check_ajax_referer( "jetpack_toggle_autoupdate-{$type}" );
198
199
		if ( ! in_array( $type, array( 'plugins', 'plugins_translations' ) ) ) {
200
			wp_send_json_error();
201
			return;
202
		}
203
204
		$jetpack_option_name = "autoupdate_{$type}";
205
206
		$list = Jetpack_Options::get_option( $jetpack_option_name, array() );
207
208
		if ( $active ) {
209
			$list = array_unique( array_merge( $list, (array) $slug ) );
210
		} else {
211
			$list = array_diff( $list, (array) $slug );
212
		}
213
214
		Jetpack_Options::update_option( $jetpack_option_name, $list );
215
216
		wp_send_json_success( $list );
217
	}
218
219
	public function admin_color_override( $color ) {
220
		return 'fresh';
221
	}
222
223
	public function mock_masterbar_activation() {
224
		include_once JETPACK__PLUGIN_DIR . 'modules/masterbar/masterbar.php';
225
		new A8C_WPCOM_Masterbar;
226
	}
227
228
	public function remove_core_menus() {
229
		remove_menu_page( 'edit.php?post_type=feedback' );
230
		remove_menu_page( 'index.php' );
231
		remove_menu_page( 'jetpack' );
232
		remove_menu_page( 'edit.php' );
233
		remove_menu_page( 'upload.php' );
234
		remove_menu_page( 'edit.php?post_type=page' );
235
		remove_menu_page( 'edit-comments.php' );
236
		remove_menu_page( 'themes.php' );
237
		remove_menu_page( 'plugins.php' );
238
		remove_menu_page( 'users.php' );
239
		remove_menu_page( 'tools.php' );
240
		remove_menu_page( 'link-manager.php' );
241
242
		// Core settings pages
243
		remove_submenu_page( 'options-general.php', 'options-general.php' );
244
		remove_submenu_page( 'options-general.php', 'options-writing.php' );
245
		remove_submenu_page( 'options-general.php', 'options-reading.php' );
246
		remove_submenu_page( 'options-general.php', 'options-discussion.php' );
247
		remove_submenu_page( 'options-general.php', 'options-media.php' );
248
		remove_submenu_page( 'options-general.php', 'options-permalink.php' );
249
		remove_submenu_page( 'options-general.php', 'privacy.php' );
250
		remove_submenu_page( 'options-general.php', 'sharing' );
251
	}
252
253
	public function add_custom_menus() {
254
		global $menu, $submenu;
255
256
		if ( isset( $_GET['post_type'] ) && 'feedback' === $_GET['post_type'] ) {
257
			// there is currently no gridicon for feedback, so using dashicon.
258
			add_menu_page( __( 'Feedback', 'jetpack' ), __( 'Feedback', 'jetpack' ), 'edit_pages', 'edit.php?post_type=feedback', '', 'dashicons-feedback', 1 );
259
			remove_menu_page( 'options-general.php' );
260
			remove_submenu_page( 'edit.php?post_type=feedback', 'feedback-export' );
261
		} else {
262
			add_menu_page( __( 'Manage Plugins', 'jetpack' ), __( 'Manage Plugins', 'jetpack' ), 'activate_plugins', 'plugins.php', '', $this->installed_plugins_icon(), 1 );
263
			// Count the settings page submenus, if it's zero then don't show this.
264
			if ( empty( $submenu['options-general.php'] ) ) {
265
				remove_menu_page( 'options-general.php' );
266
			} else {
267
				// Rename and make sure the plugin settings menu is always last.
268
				// Sneaky plugins seem to override this otherwise.
269
				// Settings is always key 80.
270
				$menu[80][0]                            = __( 'Plugin Settings', 'jetpack' );
271
				$menu[ max( array_keys( $menu ) ) + 1 ] = $menu[80];
272
				unset( $menu[80] );
273
			}
274
		}
275
	}
276
277
	public function enqueue() {
278
		wp_enqueue_style( 'calypsoify_wpadminmods_css', plugin_dir_url( __FILE__ ) . 'style.min.css', false, JETPACK__VERSION );
279
		wp_style_add_data( 'calypsoify_wpadminmods_css', 'rtl', 'replace' );
280
        wp_style_add_data( 'calypsoify_wpadminmods_css', 'suffix', '.min' );
281
282
		wp_enqueue_script( 'calypsoify_wpadminmods_js', plugin_dir_url( __FILE__ ) . 'mods.js', false, JETPACK__VERSION );
283
		wp_localize_script( 'calypsoify_wpadminmods_js', 'CalypsoifyOpts', array(
284
			'nonces' => array(
285
				'autoupdate_plugins' => wp_create_nonce( 'jetpack_toggle_autoupdate-plugins' ),
286
				'autoupdate_plugins_translations' => wp_create_nonce( 'jetpack_toggle_autoupdate-plugins_translations' ),
287
			)
288
		) );
289
	}
290
291
	public function enqueue_for_gutenberg() {
292
		wp_enqueue_style( 'calypsoify_wpadminmods_css', plugin_dir_url( __FILE__ ) . 'style-gutenberg.min.css', false, JETPACK__VERSION );
293
		wp_style_add_data( 'calypsoify_wpadminmods_css', 'rtl', 'replace' );
294
        wp_style_add_data( 'calypsoify_wpadminmods_css', 'suffix', '.min' );
295
296
		wp_enqueue_script( 'calypsoify_wpadminmods_js', plugin_dir_url( __FILE__ ) . 'mods-gutenberg.js', false, JETPACK__VERSION );
297
		wp_localize_script(
298
			'calypsoify_wpadminmods_js',
299
			'calypsoifyGutenberg',
300
			array(
301
				'closeUrl'   => $this->get_close_gutenberg_url(),
302
				'manageReusableBlocksUrl' => $this->get_calypso_origin() . '/types/wp_block' . $this->get_site_suffix(),
303
			)
304
		);
305
	}
306
307
	/**
308
	 * Inserts Sidebar HTML
309
	 *
310
	 * @return void
311
	 */
312
	public function insert_sidebar_html() {
313
		$heading       = ( isset( $_GET['post_type'] ) && 'feedback' === $_GET['post_type'] ) ? __( 'Feedback', 'jetpack' ) : __( 'Plugins', 'jetpack' );
314
		$home_url = Redirect::get_url( 'calypso-home' );
315
		?>
316
		<a href="<?php echo esc_url( $home_url ); ?>" id="calypso-sidebar-header">
317
			<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>
318
319
			<ul>
320
				<li id="calypso-sitename"><?php bloginfo( 'name' ); ?></li>
321
				<li id="calypso-plugins"><?php echo esc_html( $heading ); ?></li>
322
			</ul>
323
		</a>
324
		<?php
325
	}
326
327
	public function modify_masterbar() {
328
		global $wp_admin_bar;
329
330
		// Add proper links to masterbar top sections.
331
		$my_sites_node       = (object) $wp_admin_bar->get_node( 'blog' );
332
		$my_sites_node->href = Redirect::get_url( 'calypso-home' );
333
		$wp_admin_bar->add_node( $my_sites_node );
334
335
		$reader_node       = (object) $wp_admin_bar->get_node( 'newdash' );
336
		$reader_node->href = Redirect::get_url( 'calypso-read' );
337
		$wp_admin_bar->add_node( $reader_node );
338
339
		$me_node       = (object) $wp_admin_bar->get_node( 'my-account' );
340
		$me_node->href = Redirect::get_url( 'calypso-me' );
341
		$wp_admin_bar->add_node( $me_node );
342
	}
343
344
	private function installed_plugins_icon() {
345
		$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>';
346
347
		return 'data:image/svg+xml;base64,' . base64_encode( $svg );
348
	}
349
350
	/**
351
	 * Returns the Calypso domain that originated the current request.
352
	 *
353
	 * @return string
354
	 */
355
	private function get_calypso_origin() {
356
		$origin    = ! empty( $_GET['origin'] ) ? $_GET['origin'] : 'https://wordpress.com';
357
		$allowed = array(
358
			'http://calypso.localhost:3000',
359
			'http://127.0.0.1:41050', // Desktop App
360
			'https://wpcalypso.wordpress.com',
361
			'https://horizon.wordpress.com',
362
			'https://wordpress.com',
363
		);
364
		return in_array( $origin, $allowed, true ) ? $origin : 'https://wordpress.com';
365
366
		function get_site_suffix() {
0 ignored issues
show
Unused Code introduced by
function get_site_suffix... } return ''; } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
367
			if ( class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'build_raw_urls' ) ) {
368
				$site_suffix = Jetpack::build_raw_urls( home_url() );
369
			} elseif ( class_exists( 'WPCOM_Masterbar' ) && method_exists( 'WPCOM_Masterbar', 'get_calypso_site_slug' ) ) {
370
				$site_suffix = WPCOM_Masterbar::get_calypso_site_slug( get_current_blog_id() );
371
			}
372
373
			if ( $site_suffix ) {
374
				return "/${site_suffix}";
375
			}
376
			return '';
377
		}
378
	}
379
380
	/**
381
	 * Returns the site slug suffix to be used as part of the Calypso URLs. It already
382
	 * includes the slash separator at the beginning.
383
	 *
384
	 * @example "https://wordpress.com/block-editor" . $this->get_site_suffix()
385
	 *
386
	 * @return string
387
	 */
388
	private function get_site_suffix() {
389
		if ( class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'build_raw_urls' ) ) {
390
			$site_suffix = Jetpack::build_raw_urls( home_url() );
391
		} elseif ( class_exists( 'WPCOM_Masterbar' ) && method_exists( 'WPCOM_Masterbar', 'get_calypso_site_slug' ) ) {
392
			$site_suffix = WPCOM_Masterbar::get_calypso_site_slug( get_current_blog_id() );
393
		}
394
395
		if ( $site_suffix ) {
396
			return "/${site_suffix}";
0 ignored issues
show
Bug introduced by
The variable $site_suffix does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
397
		}
398
		return '';
399
	}
400
401
	/**
402
	 * Returns the Calypso URL that displays either the current post type list (if no args
403
	 * are supplied) or the classic editor for the current post (if a post ID is supplied).
404
	 *
405
	 * @param int|null $post_id
406
	 * @return string
407
	 */
408
	public function get_calypso_url( $post_id = null ) {
409
		$screen = get_current_screen();
410
		$post_type = $screen->post_type;
411
		if ( is_null( $post_id ) ) {
412
			// E.g. `posts`, `pages`, or `types/some_custom_post_type`
413
			$post_type_suffix = ( 'post' === $post_type || 'page' === $post_type )
414
				? "/${post_type}s"
415
				: "/types/${post_type}";
416
			$post_suffix = '';
417
		} else {
418
			$post_type_suffix = ( 'post' === $post_type || 'page' === $post_type )
419
				? "/${post_type}"
420
				: "/edit/${post_type}";
421
			$post_suffix = "/${post_id}";
422
		}
423
424
		return $this->get_calypso_origin() . $post_type_suffix . $this->get_site_suffix() . $post_suffix;
425
	}
426
427
	/**
428
	 * Returns the URL to be used on the block editor close button for going back to the
429
	 * Calypso post list.
430
	 *
431
	 * @return string
432
	 */
433
	public function get_close_gutenberg_url() {
434
		return $this->get_calypso_url();
435
	}
436
437
	/**
438
	 * Returns the URL for switching the user's editor to the Calypso (WordPress.com Classic) editor.
439
	 *
440
	 * @return string
441
	 */
442
	public function get_switch_to_classic_editor_url() {
443
		return add_query_arg(
444
			'set-editor',
445
			'classic',
446
			$this->is_calypsoify_enabled ? $this->get_calypso_url( get_the_ID() ) : false
447
		);
448
	}
449
450
	public function check_param() {
451
		if ( isset( $_GET['calypsoify'] ) ) {
452
			if ( 1 == (int) $_GET['calypsoify'] ) {
453
				update_user_meta( get_current_user_id(), 'calypsoify', 1 );
454
			} else {
455
				update_user_meta( get_current_user_id(), 'calypsoify', 0 );
456
			}
457
		}
458
	}
459
460
	public function check_page() {
461
		// If the user hits plain /wp-admin/ then disable Calypso styles.
462
		$page = wp_basename( esc_url( $_SERVER['REQUEST_URI'] ) );
463
464
		if ( false !== strpos( 'index.php', $page ) || false !== strpos( 'wp-admin', $page ) ) {
465
			update_user_meta( get_current_user_id(), 'calypsoify', 0 );
466
			wp_safe_redirect( admin_url() );
467
			die;
468
		}
469
	}
470
471
	/**
472
	 * Return whether a post type should display the Gutenberg/block editor.
473
	 *
474
	 * @since 6.7.0
475
	 */
476
	public function is_post_type_gutenberg( $post_type ) {
477
		return use_block_editor_for_post_type( $post_type );
478
	}
479
480
	public function is_page_gutenberg() {
481
		$page = wp_basename( esc_url( $_SERVER['REQUEST_URI'] ) );
482
483
		if ( false !== strpos( $page, 'post-new.php' ) && empty ( $_GET['post_type'] ) ) {
484
			return true;
485
		}
486
487
		if ( false !== strpos( $page, 'post-new.php' ) && isset( $_GET['post_type'] ) && $this->is_post_type_gutenberg( $_GET['post_type'] ) ) {
488
			return true;
489
		}
490
491 View Code Duplication
		if ( false !== strpos( $page, 'post.php' ) ) {
492
			$post = get_post( $_GET['post'] );
493
			if ( isset( $post ) && isset( $post->post_type ) && $this->is_post_type_gutenberg( $post->post_type ) ) {
494
				return true;
495
			}
496
		}
497
498 View Code Duplication
		if ( false !== strpos( $page, 'revision.php' ) ) {
499
			$post   = get_post( $_GET['revision'] );
500
			$parent = get_post( $post->post_parent );
501
			if ( isset( $parent ) && isset( $parent->post_type ) && $this->is_post_type_gutenberg( $parent->post_type ) ) {
502
				return true;
503
			}
504
		}
505
506
		return false;
507
	}
508
509
	/**
510
	 * Attach a WP_List_Table views filter to all screens.
511
	 */
512
	public function attach_views_filter( $current_screen ) {
513
		add_filter( "views_{$current_screen->id}", array( $this, 'filter_views' ) );
514
	}
515
516
	/**
517
	 * Remove the parentheses from list table view counts when Calypsofied.
518
	 *
519
	 * @param array $views Array of views. See: WP_List_Table::get_views().
520
	 * @return array Filtered views.
521
	 */
522
	public function filter_views( $views ) {
523
		foreach ( $views as $id => $view ) {
524
			$views[ $id ] = preg_replace( '/<span class="count">\((\d+)\)<\/span>/', '<span class="count">$1</span>', $view );
525
		}
526
527
		return $views;
528
	}
529
}
530
531
$Jetpack_Calypsoify = Jetpack_Calypsoify::getInstance();
532