Completed
Push — add/check_for_iframe_cookies ( d0d281...9997ab )
by
unknown
13:41 queued 06:17
created

Jetpack_Calypsoify::check_iframe_cookie_setting()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
368
			if ( class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'build_raw_urls' ) ) {
369
				$site_suffix = Jetpack::build_raw_urls( home_url() );
370
			} elseif ( class_exists( 'WPCOM_Masterbar' ) && method_exists( 'WPCOM_Masterbar', 'get_calypso_site_slug' ) ) {
371
				$site_suffix = WPCOM_Masterbar::get_calypso_site_slug( get_current_blog_id() );
372
			}
373
374
			if ( $site_suffix ) {
375
				return "/${site_suffix}";
376
			}
377
			return '';
378
		}
379
	}
380
381
	/**
382
	 * Returns the site slug suffix to be used as part of the Calypso URLs. It already
383
	 * includes the slash separator at the beginning.
384
	 *
385
	 * @example "https://wordpress.com/block-editor" . $this->get_site_suffix()
386
	 *
387
	 * @return string
388
	 */
389 View Code Duplication
	private function get_site_suffix() {
390
		if ( class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'build_raw_urls' ) ) {
391
			$site_suffix = Jetpack::build_raw_urls( home_url() );
392
		} elseif ( class_exists( 'WPCOM_Masterbar' ) && method_exists( 'WPCOM_Masterbar', 'get_calypso_site_slug' ) ) {
393
			$site_suffix = WPCOM_Masterbar::get_calypso_site_slug( get_current_blog_id() );
394
		}
395
396
		if ( $site_suffix ) {
397
			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...
398
		}
399
		return '';
400
	}
401
402
	/**
403
	 * Returns the Calypso URL that displays either the current post type list (if no args
404
	 * are supplied) or the classic editor for the current post (if a post ID is supplied).
405
	 *
406
	 * @param int|null $post_id
407
	 * @return string
408
	 */
409
	public function get_calypso_url( $post_id = null ) {
410
		$screen = get_current_screen();
411
		$post_type = $screen->post_type;
412
		if ( is_null( $post_id ) ) {
413
			// E.g. `posts`, `pages`, or `types/some_custom_post_type`
414
			$post_type_suffix = ( 'post' === $post_type || 'page' === $post_type )
415
				? "/${post_type}s"
416
				: "/types/${post_type}";
417
			$post_suffix = '';
418
		} else {
419
			$post_type_suffix = ( 'post' === $post_type || 'page' === $post_type )
420
				? "/${post_type}"
421
				: "/edit/${post_type}";
422
			$post_suffix = "/${post_id}";
423
		}
424
425
		return $this->get_calypso_origin() . $post_type_suffix . $this->get_site_suffix() . $post_suffix;
426
	}
427
428
	/**
429
	 * Returns the URL to be used on the block editor close button for going back to the
430
	 * Calypso post list.
431
	 *
432
	 * @return string
433
	 */
434
	public function get_close_gutenberg_url() {
435
		return $this->get_calypso_url();
436
	}
437
438
	/**
439
	 * Returns the URL for switching the user's editor to the Classic editor.
440
	 *
441
	 * @return string
442
	 */
443
	public function get_switch_to_classic_editor_url() {
444
		// phpcs:ignore WordPress.Security.NonceVerification
445
		if ( isset( $_GET['editor/after-deprecation'] ) ) {
446
			$post_id    = get_the_ID();
447
			$post_type  = get_current_screen()->post_type;
448
			$path       = is_null( $post_id ) ? 'post-new.php' : 'post.php';
449
			$query_args = array(
450
				'post'       => $post_id,
451
				'action'     => ( $post_id ) ? 'edit' : null,
452
				'post_type'  => ( 'post' !== $post_type ) ? $post_type : null,
453
				'set-editor' => 'classic',
454
			);
455
456
			return add_query_arg( $query_args, admin_url( $path ) );
457
		}
458
459
		return add_query_arg(
460
			'set-editor',
461
			'classic',
462
			$this->is_calypsoify_enabled ? $this->get_calypso_url( get_the_ID() ) : false
463
		);
464
	}
465
466
	public function check_param() {
467
		if ( isset( $_GET['calypsoify'] ) ) {
468
			if ( 1 == (int) $_GET['calypsoify'] ) {
469
				update_user_meta( get_current_user_id(), 'calypsoify', 1 );
470
			} else {
471
				update_user_meta( get_current_user_id(), 'calypsoify', 0 );
472
			}
473
474
			$page = remove_query_arg( 'calypsoify', wp_basename( $_SERVER['REQUEST_URI'] ) );
475
476
			wp_safe_redirect( admin_url( $page ) );
477
		}
478
	}
479
480
	public function check_page() {
481
		// If the user hits plain /wp-admin/ then disable Calypso styles.
482
		$page = wp_basename( esc_url( $_SERVER['REQUEST_URI'] ) );
483
484
		if ( false !== strpos( 'index.php', $page ) || false !== strpos( 'wp-admin', $page ) ) {
485
			update_user_meta( get_current_user_id(), 'calypsoify', 0 );
486
			wp_safe_redirect( admin_url() );
487
			die;
488
		}
489
	}
490
491
	/**
492
	 * Return whether a post type should display the Gutenberg/block editor.
493
	 *
494
	 * @since 6.7.0
495
	 */
496
	public function is_post_type_gutenberg( $post_type ) {
497
		return use_block_editor_for_post_type( $post_type );
498
	}
499
500
	public function is_page_gutenberg() {
501
		$page = wp_basename( esc_url( $_SERVER['REQUEST_URI'] ) );
502
503
		if ( false !== strpos( $page, 'post-new.php' ) && empty ( $_GET['post_type'] ) ) {
504
			return true;
505
		}
506
507
		if ( false !== strpos( $page, 'post-new.php' ) && isset( $_GET['post_type'] ) && $this->is_post_type_gutenberg( $_GET['post_type'] ) ) {
508
			return true;
509
		}
510
511 View Code Duplication
		if ( false !== strpos( $page, 'post.php' ) ) {
512
			$post = get_post( $_GET['post'] );
513
			if ( isset( $post ) && isset( $post->post_type ) && $this->is_post_type_gutenberg( $post->post_type ) ) {
514
				return true;
515
			}
516
		}
517
518 View Code Duplication
		if ( false !== strpos( $page, 'revision.php' ) ) {
519
			$post   = get_post( $_GET['revision'] );
520
			$parent = get_post( $post->post_parent );
521
			if ( isset( $parent ) && isset( $parent->post_type ) && $this->is_post_type_gutenberg( $parent->post_type ) ) {
522
				return true;
523
			}
524
		}
525
526
		return false;
527
	}
528
529
	/**
530
	 * Attach a WP_List_Table views filter to all screens.
531
	 */
532
	public function attach_views_filter( $current_screen ) {
533
		add_filter( "views_{$current_screen->id}", array( $this, 'filter_views' ) );
534
	}
535
536
	/**
537
	 * Remove the parentheses from list table view counts when Calypsofied.
538
	 *
539
	 * @param array $views Array of views. See: WP_List_Table::get_views().
540
	 * @return array Filtered views.
541
	 */
542
	public function filter_views( $views ) {
543
		foreach ( $views as $id => $view ) {
544
			$views[ $id ] = preg_replace( '/<span class="count">\((\d+)\)<\/span>/', '<span class="count">$1</span>', $view );
545
		}
546
547
		return $views;
548
	}
549
}
550
551
$Jetpack_Calypsoify = Jetpack_Calypsoify::getInstance();
552