Completed
Push — update/wpcom-block-editor-shor... ( 9897fe...f43ed6 )
by Jeremy
148:26 queued 138:20
created

Jetpack_Calypsoify   A

Complexity

Total Complexity 38

Size/Duplication

Total Lines 210
Duplicated Lines 6.19 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 38
lcom 2
cbo 1
dl 13
loc 210
rs 9.36
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getInstance() 0 4 1
A get_instance() 0 7 2
A setup() 0 9 4
A enqueue_for_gutenberg() 0 15 1
A get_calypso_origin() 0 11 3
B get_calypso_url() 0 20 6
A get_close_gutenberg_url() 0 3 1
A get_switch_to_classic_editor_url() 0 7 2
A check_meta() 0 5 2
A is_post_type_gutenberg() 0 3 1
C is_page_gutenberg() 13 31 14

How to fix   Duplicated Code   

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:

1
<?php
2
/**
3
 * This is Calypso skin of the wp-admin interface that is conditionally triggered via the ?calypsoify=1 param.
4
 *
5
 * @package automattic/jetpack
6
 */
7
8
use Automattic\Jetpack\Status;
9
10
/**
11
 * Class Jetpack_Calypsoify
12
 */
13
class Jetpack_Calypsoify {
14
15
	/**
16
	 * Singleton instance of `Jetpack_Calypsoify`.
17
	 *
18
	 * @var object
19
	 */
20
	public static $instance = false;
21
22
	/**
23
	 * Is Calypsoify enabled, based on any value of `calypsoify` user meta.
24
	 *
25
	 * @var bool
26
	 */
27
	public $is_calypsoify_enabled = false;
28
29
	/**
30
	 * Jetpack_Calypsoify constructor.
31
	 */
32
	private function __construct() {
33
		add_action( 'admin_init', array( $this, 'setup' ), 4 );
34
	}
35
36
	/**
37
	 * Original singleton.
38
	 *
39
	 * @todo We need to leave this in place until wpcomsh is updated. wpcomsh can be updated once 9.3.0 is stable.
40
	 *
41
	 * Deprecated 9.3.0
42
	 *
43
	 * @return Jetpack_Calypsoify
44
	 */
45
	public static function getInstance() { //phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
46
		_deprecated_function( __METHOD__, 'Jetpack 9.3.0', 'Jetpack_Calypsoify::get_instance' );
47
		return self::get_instance();
48
	}
49
50
	/**
51
	 * Singleton.
52
	 *
53
	 * @return Jetpack_Calypsoify
54
	 */
55
	public static function get_instance() {
56
		if ( ! self::$instance ) {
57
			self::$instance = new self();
58
		}
59
60
		return self::$instance;
61
	}
62
63
	/**
64
	 * Setup function that is loaded on the `wp_loaded` hook via the constructor.
65
	 */
66
	public function setup() {
67
		$this->is_calypsoify_enabled = isset( $_GET['calypsoify'] ) && 1 === (int) $_GET['calypsoify'] && $this->is_page_gutenberg(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
68
69
		$this->check_meta();
70
71
		if ( $this->is_calypsoify_enabled ) {
72
			add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_for_gutenberg' ), 100 );
73
		}
74
	}
75
76
	/**
77
	 * Enqueues scripts, data, and styles for Gutenberg.
78
	 */
79
	public function enqueue_for_gutenberg() {
80
		wp_enqueue_style( 'calypsoify_wpadminmods_css', plugin_dir_url( __FILE__ ) . 'style-gutenberg.min.css', false, JETPACK__VERSION );
81
		wp_style_add_data( 'calypsoify_wpadminmods_css', 'rtl', 'replace' );
82
		wp_style_add_data( 'calypsoify_wpadminmods_css', 'suffix', '.min' );
83
84
		wp_enqueue_script( 'calypsoify_wpadminmods_js', plugin_dir_url( __FILE__ ) . 'mods-gutenberg.js', false, JETPACK__VERSION, false );
85
		wp_localize_script(
86
			'calypsoify_wpadminmods_js',
87
			'calypsoifyGutenberg',
88
			array(
89
				'closeUrl'                => $this->get_close_gutenberg_url(),
90
				'manageReusableBlocksUrl' => $this->get_calypso_origin() . '/types/wp_block/' . ( new Status() )->get_site_suffix(),
91
			)
92
		);
93
	}
94
95
	/**
96
	 * Returns the Calypso domain that originated the current request.
97
	 *
98
	 * @return string
99
	 */
100
	private function get_calypso_origin() {
101
		$origin  = ! empty( $_GET['origin'] ) ? $_GET['origin'] : 'https://wordpress.com'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
102
		$allowed = array(
103
			'http://calypso.localhost:3000',
104
			'http://127.0.0.1:41050', // Desktop App.
105
			'https://wpcalypso.wordpress.com',
106
			'https://horizon.wordpress.com',
107
			'https://wordpress.com',
108
		);
109
		return in_array( $origin, $allowed, true ) ? $origin : 'https://wordpress.com';
110
	}
111
112
	/**
113
	 * Returns the Calypso URL that displays either the current post type list (if no args
114
	 * are supplied) or the classic editor for the current post (if a post ID is supplied).
115
	 *
116
	 * @param int|null $post_id Post ID.
117
	 *
118
	 * @return string
119
	 */
120
	public function get_calypso_url( $post_id = null ) {
121
		$screen      = get_current_screen();
122
		$post_type   = $screen->post_type;
123
		$site_suffix = ( new Status() )->get_site_suffix();
124
125
		if ( is_null( $post_id ) ) {
126
			// E.g. posts or pages have no special suffix. CPTs are in the `types/{cpt}` format.
127
			$post_type_suffix = ( 'post' === $post_type || 'page' === $post_type )
128
				? "/${post_type}s/"
129
				: "/types/${post_type}/";
130
			$post_suffix      = '';
131
		} else {
132
			$post_type_suffix = ( 'post' === $post_type || 'page' === $post_type )
133
				? "/${post_type}/"
134
				: "/edit/${post_type}/";
135
			$post_suffix      = "/${post_id}";
136
		}
137
138
		return $this->get_calypso_origin() . $post_type_suffix . $site_suffix . $post_suffix;
139
	}
140
141
	/**
142
	 * Returns the URL to be used on the block editor close button for going back to the
143
	 * Calypso post list.
144
	 *
145
	 * @return string
146
	 */
147
	public function get_close_gutenberg_url() {
148
		return $this->get_calypso_url();
149
	}
150
151
	/**
152
	 * Returns the URL for switching the user's editor to the Calypso (WordPress.com Classic) editor.
153
	 *
154
	 * @return string
155
	 */
156
	public function get_switch_to_classic_editor_url() {
157
		return add_query_arg(
158
			'set-editor',
159
			'classic',
160
			$this->is_calypsoify_enabled ? $this->get_calypso_url( get_the_ID() ) : false
161
		);
162
	}
163
164
	/**
165
	 * Checks if the calypsoify user meta value is set, and deletes it if it is.
166
	 * This is to ensure that Calypsoify is not activated without the URL parameter.
167
	 */
168
	public function check_meta() {
169
		if ( ! empty( get_user_meta( get_current_user_id(), 'calypsoify', true ) ) ) {
170
			delete_user_meta( get_current_user_id(), 'calypsoify' );
171
		}
172
	}
173
174
	/**
175
	 * Return whether a post type should display the Gutenberg/block editor.
176
	 *
177
	 * @since 6.7.0
178
	 *
179
	 * @param string $post_type Post type.
180
	 */
181
	public function is_post_type_gutenberg( $post_type ) {
182
		return use_block_editor_for_post_type( $post_type );
183
	}
184
185
	/**
186
	 * Determines if the page is an instance of the Gutenberg block editor.
187
	 *
188
	 * @return bool
189
	 */
190
	public function is_page_gutenberg() {
191
		// phpcs:disable WordPress.Security.NonceVerification.Recommended
192
		// Disabling WordPress.Security.NonceVerification.Recommended because this function fires within admin_init and this is only changing display.
193
		$page = wp_basename( esc_url( $_SERVER['REQUEST_URI'] ) );
194
195
		if ( false !== strpos( $page, 'post-new.php' ) && empty( $_GET['post_type'] ) ) {
196
			return true;
197
		}
198
199
		if ( false !== strpos( $page, 'post-new.php' ) && isset( $_GET['post_type'] ) && $this->is_post_type_gutenberg( $_GET['post_type'] ) ) {
200
			return true;
201
		}
202
203 View Code Duplication
		if ( false !== strpos( $page, 'post.php' ) ) {
204
			$post = get_post( $_GET['post'] );
205
			if ( isset( $post ) && isset( $post->post_type ) && $this->is_post_type_gutenberg( $post->post_type ) ) {
206
				return true;
207
			}
208
		}
209
210 View Code Duplication
		if ( false !== strpos( $page, 'revision.php' ) ) {
211
			$post   = get_post( $_GET['revision'] );
212
			$parent = get_post( $post->post_parent );
213
			if ( isset( $parent ) && isset( $parent->post_type ) && $this->is_post_type_gutenberg( $parent->post_type ) ) {
214
				return true;
215
			}
216
		}
217
218
		return false;
219
		// phpcs:enable
220
	}
221
222
}
223
224
Jetpack_Calypsoify::get_instance();
225