Completed
Push — add/check_for_iframe_cookies ( 026105...318b27 )
by
unknown
08:51
created

Jetpack_Calypsoify   F

Complexity

Total Complexity 92

Size/Duplication

Total Lines 521
Duplicated Lines 7.1 %

Coupling/Cohesion

Components 3
Dependencies 4

Importance

Changes 0
Metric Value
dl 37
loc 521
rs 2
c 0
b 0
f 0
wmc 92
lcom 3
cbo 4

32 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getInstance() 0 7 2
A setup() 0 14 2
A class.jetpack-calypsoify.php ➔ get_site_suffix() 12 12 6
A setup_admin() 0 28 4
A manage_plugins_columns_header() 0 6 2
B manage_plugins_custom_column() 0 40 5
A get_dotorg_repo_plugins() 0 4 1
A bulk_actions_plugins() 0 5 1
A handle_bulk_actions_plugins() 0 17 4
A plugins_admin_notices() 0 9 3
A jetpack_toggle_autoupdate() 0 31 4
A admin_color_override() 0 3 1
A mock_masterbar_activation() 0 4 1
A remove_core_menus() 0 24 1
A add_custom_menus() 0 23 4
A enqueue() 0 13 1
A enqueue_for_gutenberg() 0 15 1
A insert_sidebar_html() 0 14 3
A modify_masterbar() 0 16 1
A installed_plugins_icon() 0 5 1
B get_calypso_origin() 12 24 6
A get_site_suffix() 12 12 6
A get_calypso_url() 0 18 6
A get_close_gutenberg_url() 0 3 1
B get_switch_to_classic_editor_url() 0 22 6
A check_param() 0 13 3
A check_page() 0 10 3
A is_post_type_gutenberg() 0 3 1
C is_page_gutenberg() 13 28 14
A attach_views_filter() 0 3 1
A filter_views() 0 7 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Jetpack_Calypsoify often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Jetpack_Calypsoify, and based on these observations, apply Extract Interface, too.

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
		add_action( 'admin_init', array( $this, 'check_param' ), 4 );
40
41
		if ( $this->is_calypsoify_enabled ) {
42
			add_action( 'admin_init', array( $this, 'setup_admin' ), 6 );
43
			add_action( 'admin_menu', array( $this, 'remove_core_menus' ), 100 );
44
			add_action( 'admin_menu', array( $this, 'add_custom_menus' ), 101 );
45
		}
46
47
		// Make this always available -- in case calypsoify gets toggled off.
48
		add_action( 'wp_ajax_jetpack_toggle_autoupdate', array( $this, 'jetpack_toggle_autoupdate' ) );
49
		add_filter( 'handle_bulk_actions-plugins', array( $this, 'handle_bulk_actions_plugins' ), 10, 3 );
50
	}
51
52
	public function setup_admin() {
53
		// Masterbar is currently required for this to work properly. Mock the instance of it
54
		if ( ! Jetpack::is_module_active( 'masterbar' ) ) {
55
			$this->mock_masterbar_activation();
56
		}
57
58
		if ( $this->is_page_gutenberg() ) {
59
			add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_for_gutenberg' ), 100 );
60
			return;
61
		}
62
63
		add_action( 'admin_init', array( $this, 'check_page' ) );
64
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ), 100 );
65
		add_action( 'in_admin_header', array( $this, 'insert_sidebar_html' ) );
66
		add_action( 'wp_before_admin_bar_render', array( $this, 'modify_masterbar' ), 100000 );
67
68
		add_filter( 'get_user_option_admin_color', array( $this, 'admin_color_override' ) );
69
70
		add_action( 'manage_plugins_columns', array( $this, 'manage_plugins_columns_header' ) );
71
		add_action( 'manage_plugins_custom_column', array( $this, 'manage_plugins_custom_column' ), 10, 2 );
72
		add_filter( 'bulk_actions-plugins', array( $this, 'bulk_actions_plugins' ) );
73
74
		add_action( 'current_screen', array( $this, 'attach_views_filter' ) );
75
76
		if ( 'plugins.php' === basename( $_SERVER['PHP_SELF'] ) ) {
77
			add_action( 'admin_notices', array( $this, 'plugins_admin_notices' ) );
78
		}
79
	}
80
81
	public function manage_plugins_columns_header( $columns ) {
82
		if ( current_user_can( 'jetpack_manage_autoupdates' ) ) {
83
			$columns['autoupdate'] = __( 'Automatic Update', 'jetpack' );
84
		}
85
		return $columns;
86
	}
87
88
	public function manage_plugins_custom_column( $column_name, $slug ) {
89
		static $repo_plugins = array();
90
91
		if ( ! current_user_can( 'jetpack_manage_autoupdates' ) ) {
92
			return;
93
		}
94
95
		if ( empty( $repo_plugins ) ) {
96
			$repo_plugins = self::get_dotorg_repo_plugins();
97
		}
98
99
		$autoupdating_plugins = Jetpack_Options::get_option( 'autoupdate_plugins', array() );
100
		// $autoupdating_plugins_translations = Jetpack_Options::get_option( 'autoupdate_plugins_translations', array() );
101
		if ( 'autoupdate' === $column_name ) {
102
			if ( ! in_array( $slug, $repo_plugins ) ) {
103
				return;
104
			}
105
			// Shamelessly swiped from https://github.com/Automattic/wp-calypso/blob/59bdfeeb97eda4266ad39410cb0a074d2c88dbc8/client/components/forms/form-toggle
106
			?>
107
108
			<span class="form-toggle__wrapper">
109
				<input
110
					id="autoupdate_plugin-toggle-<?php echo esc_attr( $slug ) ?>"
111
					name="autoupdate_plugins[<?php echo esc_attr( $slug ) ?>]"
112
					value="autoupdate"
113
					class="form-toggle autoupdate-toggle"
114
					type="checkbox"
115
					<?php checked( in_array( $slug, $autoupdating_plugins ) ); ?>
116
					readonly
117
					data-slug="<?php echo esc_attr( $slug ); ?>"
118
				/>
119
				<label class="form-toggle__label" for="autoupdate_plugin-toggle-<?php echo esc_attr( $slug ) ?>">
120
					<span class="form-toggle__switch" role="checkbox"></span>
121
					<span class="form-toggle__label-content"><?php /*  */ ?></span>
122
				</label>
123
			</span>
124
125
			<?php
126
		}
127
	}
128
129
	public static function get_dotorg_repo_plugins() {
130
		$plugins = get_site_transient( 'update_plugins' );
131
		return array_merge( array_keys( $plugins->response ), array_keys( $plugins->no_update ) );
132
	}
133
134
	public function bulk_actions_plugins( $bulk_actions ) {
135
		$bulk_actions['jetpack_enable_plugin_autoupdates'] = __( 'Enable Automatic Updates', 'jetpack' );
136
		$bulk_actions['jetpack_disable_plugin_autoupdates'] = __( 'Disable Automatic Updates', 'jetpack' );
137
		return $bulk_actions;
138
	}
139
140
	public function handle_bulk_actions_plugins( $redirect_to, $action, $slugs ) {
141
		$redirect_to = remove_query_arg( array( 'jetpack_enable_plugin_autoupdates', 'jetpack_disable_plugin_autoupdates' ), $redirect_to );
142
		if ( in_array( $action, array( 'jetpack_enable_plugin_autoupdates', 'jetpack_disable_plugin_autoupdates' ) ) ) {
143
			$list = Jetpack_Options::get_option( 'autoupdate_plugins', array() );
144
			$initial_qty = sizeof( $list );
145
146
			if ( 'jetpack_enable_plugin_autoupdates' === $action ) {
147
				$list = array_unique( array_merge( $list, $slugs ) );
148
			} elseif ( 'jetpack_disable_plugin_autoupdates' === $action ) {
149
				$list = array_diff( $list, $slugs );
150
			}
151
152
			Jetpack_Options::update_option( 'autoupdate_plugins', $list );
153
			$redirect_to = add_query_arg( $action, absint( sizeof( $list ) - $initial_qty ), $redirect_to );
154
		}
155
		return $redirect_to;
156
	}
157
158
	public function plugins_admin_notices() {
159
		if ( ! empty( $_GET['jetpack_enable_plugin_autoupdates'] ) ) {
160
			$qty = (int) $_GET['jetpack_enable_plugin_autoupdates'];
161
			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 );
162
		} elseif ( ! empty( $_GET['jetpack_disable_plugin_autoupdates'] ) ) {
163
			$qty = (int) $_GET['jetpack_disable_plugin_autoupdates'];
164
			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 );
165
		}
166
	}
167
168
	public function jetpack_toggle_autoupdate() {
169
		if ( ! current_user_can( 'jetpack_manage_autoupdates' ) ) {
170
			wp_send_json_error();
171
			return;
172
		}
173
174
		$type   = $_POST['type'];
175
		$slug   = $_POST['slug'];
176
		$active = 'false' !== $_POST['active'];
177
178
		check_ajax_referer( "jetpack_toggle_autoupdate-{$type}" );
179
180
		if ( ! in_array( $type, array( 'plugins', 'plugins_translations' ) ) ) {
181
			wp_send_json_error();
182
			return;
183
		}
184
185
		$jetpack_option_name = "autoupdate_{$type}";
186
187
		$list = Jetpack_Options::get_option( $jetpack_option_name, array() );
188
189
		if ( $active ) {
190
			$list = array_unique( array_merge( $list, (array) $slug ) );
191
		} else {
192
			$list = array_diff( $list, (array) $slug );
193
		}
194
195
		Jetpack_Options::update_option( $jetpack_option_name, $list );
196
197
		wp_send_json_success( $list );
198
	}
199
200
	public function admin_color_override( $color ) {
201
		return 'fresh';
202
	}
203
204
	public function mock_masterbar_activation() {
205
		include_once JETPACK__PLUGIN_DIR . 'modules/masterbar/masterbar.php';
206
		new A8C_WPCOM_Masterbar;
207
	}
208
209
	public function remove_core_menus() {
210
		remove_menu_page( 'edit.php?post_type=feedback' );
211
		remove_menu_page( 'index.php' );
212
		remove_menu_page( 'jetpack' );
213
		remove_menu_page( 'edit.php' );
214
		remove_menu_page( 'upload.php' );
215
		remove_menu_page( 'edit.php?post_type=page' );
216
		remove_menu_page( 'edit-comments.php' );
217
		remove_menu_page( 'themes.php' );
218
		remove_menu_page( 'plugins.php' );
219
		remove_menu_page( 'users.php' );
220
		remove_menu_page( 'tools.php' );
221
		remove_menu_page( 'link-manager.php' );
222
223
		// Core settings pages
224
		remove_submenu_page( 'options-general.php', 'options-general.php' );
225
		remove_submenu_page( 'options-general.php', 'options-writing.php' );
226
		remove_submenu_page( 'options-general.php', 'options-reading.php' );
227
		remove_submenu_page( 'options-general.php', 'options-discussion.php' );
228
		remove_submenu_page( 'options-general.php', 'options-media.php' );
229
		remove_submenu_page( 'options-general.php', 'options-permalink.php' );
230
		remove_submenu_page( 'options-general.php', 'privacy.php' );
231
		remove_submenu_page( 'options-general.php', 'sharing' );
232
	}
233
234
	public function add_custom_menus() {
235
		global $menu, $submenu;
236
237
		if ( isset( $_GET['post_type'] ) && 'feedback' === $_GET['post_type'] ) {
238
			// there is currently no gridicon for feedback, so using dashicon.
239
			add_menu_page( __( 'Feedback', 'jetpack' ), __( 'Feedback', 'jetpack' ), 'edit_pages', 'edit.php?post_type=feedback', '', 'dashicons-feedback', 1 );
240
			remove_menu_page( 'options-general.php' );
241
			remove_submenu_page( 'edit.php?post_type=feedback', 'feedback-export' );
242
		} else {
243
			add_menu_page( __( 'Manage Plugins', 'jetpack' ), __( 'Manage Plugins', 'jetpack' ), 'activate_plugins', 'plugins.php', '', $this->installed_plugins_icon(), 1 );
244
			// Count the settings page submenus, if it's zero then don't show this.
245
			if ( empty( $submenu['options-general.php'] ) ) {
246
				remove_menu_page( 'options-general.php' );
247
			} else {
248
				// Rename and make sure the plugin settings menu is always last.
249
				// Sneaky plugins seem to override this otherwise.
250
				// Settings is always key 80.
251
				$menu[80][0]                            = __( 'Plugin Settings', 'jetpack' );
252
				$menu[ max( array_keys( $menu ) ) + 1 ] = $menu[80];
253
				unset( $menu[80] );
254
			}
255
		}
256
	}
257
258
	public function enqueue() {
259
		wp_enqueue_style( 'calypsoify_wpadminmods_css', plugin_dir_url( __FILE__ ) . 'style.min.css', false, JETPACK__VERSION );
260
		wp_style_add_data( 'calypsoify_wpadminmods_css', 'rtl', 'replace' );
261
        wp_style_add_data( 'calypsoify_wpadminmods_css', 'suffix', '.min' );
262
263
		wp_enqueue_script( 'calypsoify_wpadminmods_js', plugin_dir_url( __FILE__ ) . 'mods.js', false, JETPACK__VERSION );
264
		wp_localize_script( 'calypsoify_wpadminmods_js', 'CalypsoifyOpts', array(
265
			'nonces' => array(
266
				'autoupdate_plugins' => wp_create_nonce( 'jetpack_toggle_autoupdate-plugins' ),
267
				'autoupdate_plugins_translations' => wp_create_nonce( 'jetpack_toggle_autoupdate-plugins_translations' ),
268
			)
269
		) );
270
	}
271
272
	public function enqueue_for_gutenberg() {
273
		wp_enqueue_style( 'calypsoify_wpadminmods_css', plugin_dir_url( __FILE__ ) . 'style-gutenberg.min.css', false, JETPACK__VERSION );
274
		wp_style_add_data( 'calypsoify_wpadminmods_css', 'rtl', 'replace' );
275
        wp_style_add_data( 'calypsoify_wpadminmods_css', 'suffix', '.min' );
276
277
		wp_enqueue_script( 'calypsoify_wpadminmods_js', plugin_dir_url( __FILE__ ) . 'mods-gutenberg.js', false, JETPACK__VERSION );
278
		wp_localize_script(
279
			'calypsoify_wpadminmods_js',
280
			'calypsoifyGutenberg',
281
			array(
282
				'closeUrl'   => $this->get_close_gutenberg_url(),
283
				'manageReusableBlocksUrl' => $this->get_calypso_origin() . '/types/wp_block' . $this->get_site_suffix(),
284
			)
285
		);
286
	}
287
288
	/**
289
	 * Inserts Sidebar HTML
290
	 *
291
	 * @return void
292
	 */
293
	public function insert_sidebar_html() {
294
		$heading       = ( isset( $_GET['post_type'] ) && 'feedback' === $_GET['post_type'] ) ? __( 'Feedback', 'jetpack' ) : __( 'Plugins', 'jetpack' );
295
		$stats_day_url = Redirect::get_url( 'calypso-stats-day' );
296
		?>
297
		<a href="<?php echo esc_url( $stats_day_url ); ?>" id="calypso-sidebar-header">
298
			<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>
299
300
			<ul>
301
				<li id="calypso-sitename"><?php bloginfo( 'name' ); ?></li>
302
				<li id="calypso-plugins"><?php echo esc_html( $heading ); ?></li>
303
			</ul>
304
		</a>
305
		<?php
306
	}
307
308
	public function modify_masterbar() {
309
		global $wp_admin_bar;
310
311
		// Add proper links to masterbar top sections.
312
		$my_sites_node       = (object) $wp_admin_bar->get_node( 'blog' );
313
		$my_sites_node->href = Redirect::get_url( 'calypso-stats-day' );
314
		$wp_admin_bar->add_node( $my_sites_node );
315
316
		$reader_node       = (object) $wp_admin_bar->get_node( 'newdash' );
317
		$reader_node->href = Redirect::get_url( 'calypso-read' );
318
		$wp_admin_bar->add_node( $reader_node );
319
320
		$me_node       = (object) $wp_admin_bar->get_node( 'my-account' );
321
		$me_node->href = Redirect::get_url( 'calypso-me' );
322
		$wp_admin_bar->add_node( $me_node );
323
	}
324
325
	private function installed_plugins_icon() {
326
		$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>';
327
328
		return 'data:image/svg+xml;base64,' . base64_encode( $svg );
329
	}
330
331
	/**
332
	 * Returns the Calypso domain that originated the current request.
333
	 *
334
	 * @return string
335
	 */
336
	private function get_calypso_origin() {
337
		$origin    = ! empty( $_GET['origin'] ) ? $_GET['origin'] : 'https://wordpress.com';
338
		$allowed = array(
339
			'http://calypso.localhost:3000',
340
			'http://127.0.0.1:41050', // Desktop App
341
			'https://wpcalypso.wordpress.com',
342
			'https://horizon.wordpress.com',
343
			'https://wordpress.com',
344
		);
345
		return in_array( $origin, $allowed, true ) ? $origin : 'https://wordpress.com';
346
347 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...
348
			if ( class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'build_raw_urls' ) ) {
349
				$site_suffix = Jetpack::build_raw_urls( home_url() );
350
			} elseif ( class_exists( 'WPCOM_Masterbar' ) && method_exists( 'WPCOM_Masterbar', 'get_calypso_site_slug' ) ) {
351
				$site_suffix = WPCOM_Masterbar::get_calypso_site_slug( get_current_blog_id() );
352
			}
353
354
			if ( $site_suffix ) {
355
				return "/${site_suffix}";
356
			}
357
			return '';
358
		}
359
	}
360
361
	/**
362
	 * Returns the site slug suffix to be used as part of the Calypso URLs. It already
363
	 * includes the slash separator at the beginning.
364
	 *
365
	 * @example "https://wordpress.com/block-editor" . $this->get_site_suffix()
366
	 *
367
	 * @return string
368
	 */
369 View Code Duplication
	private function get_site_suffix() {
370
		if ( class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'build_raw_urls' ) ) {
371
			$site_suffix = Jetpack::build_raw_urls( home_url() );
372
		} elseif ( class_exists( 'WPCOM_Masterbar' ) && method_exists( 'WPCOM_Masterbar', 'get_calypso_site_slug' ) ) {
373
			$site_suffix = WPCOM_Masterbar::get_calypso_site_slug( get_current_blog_id() );
374
		}
375
376
		if ( $site_suffix ) {
377
			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...
378
		}
379
		return '';
380
	}
381
382
	/**
383
	 * Returns the Calypso URL that displays either the current post type list (if no args
384
	 * are supplied) or the classic editor for the current post (if a post ID is supplied).
385
	 *
386
	 * @param int|null $post_id
387
	 * @return string
388
	 */
389
	public function get_calypso_url( $post_id = null ) {
390
		$screen = get_current_screen();
391
		$post_type = $screen->post_type;
392
		if ( is_null( $post_id ) ) {
393
			// E.g. `posts`, `pages`, or `types/some_custom_post_type`
394
			$post_type_suffix = ( 'post' === $post_type || 'page' === $post_type )
395
				? "/${post_type}s"
396
				: "/types/${post_type}";
397
			$post_suffix = '';
398
		} else {
399
			$post_type_suffix = ( 'post' === $post_type || 'page' === $post_type )
400
				? "/${post_type}"
401
				: "/edit/${post_type}";
402
			$post_suffix = "/${post_id}";
403
		}
404
405
		return $this->get_calypso_origin() . $post_type_suffix . $this->get_site_suffix() . $post_suffix;
406
	}
407
408
	/**
409
	 * Returns the URL to be used on the block editor close button for going back to the
410
	 * Calypso post list.
411
	 *
412
	 * @return string
413
	 */
414
	public function get_close_gutenberg_url() {
415
		return $this->get_calypso_url();
416
	}
417
418
	/**
419
	 * Returns the URL for switching the user's editor to the Classic editor.
420
	 *
421
	 * @return string
422
	 */
423
	public function get_switch_to_classic_editor_url() {
424
		// phpcs:ignore WordPress.Security.NonceVerification
425
		if ( isset( $_GET['editor/after-deprecation'] ) ) {
426
			$post_id    = get_the_ID();
427
			$post_type  = get_current_screen()->post_type;
428
			$path       = is_null( $post_id ) ? 'post-new.php' : 'post.php';
429
			$query_args = array(
430
				'post'       => $post_id,
431
				'action'     => ( $post_id ) ? 'edit' : null,
432
				'post_type'  => ( 'post' !== $post_type ) ? $post_type : null,
433
				'set-editor' => 'classic',
434
			);
435
436
			return add_query_arg( $query_args, admin_url( $path ) );
437
		}
438
439
		return add_query_arg(
440
			'set-editor',
441
			'classic',
442
			$this->is_calypsoify_enabled ? $this->get_calypso_url( get_the_ID() ) : false
443
		);
444
	}
445
446
	public function check_param() {
447
		if ( isset( $_GET['calypsoify'] ) ) {
448
			if ( 1 == (int) $_GET['calypsoify'] ) {
449
				update_user_meta( get_current_user_id(), 'calypsoify', 1 );
450
			} else {
451
				update_user_meta( get_current_user_id(), 'calypsoify', 0 );
452
			}
453
454
			$page = remove_query_arg( 'calypsoify', wp_basename( $_SERVER['REQUEST_URI'] ) );
455
456
			wp_safe_redirect( admin_url( $page ) );
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