Completed
Push — add/testing-info ( be1095...03b7e9 )
by
unknown
09:20
created

Jetpack_Calypsoify   A

Complexity

Total Complexity 38

Size/Duplication

Total Lines 212
Duplicated Lines 6.13 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

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

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 17 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
		$site_suffix = ( new Status() )->get_site_suffix();
81
		wp_enqueue_style( 'calypsoify_wpadminmods_css', plugin_dir_url( __FILE__ ) . 'style-gutenberg.min.css', false, JETPACK__VERSION );
82
		wp_style_add_data( 'calypsoify_wpadminmods_css', 'rtl', 'replace' );
83
		wp_style_add_data( 'calypsoify_wpadminmods_css', 'suffix', '.min' );
84
85
		wp_enqueue_script( 'calypsoify_wpadminmods_js', plugin_dir_url( __FILE__ ) . 'mods-gutenberg.js', false, JETPACK__VERSION, false );
86
		wp_localize_script(
87
			'calypsoify_wpadminmods_js',
88
			'calypsoifyGutenberg',
89
			array(
90
				'closeUrl'                => $this->get_close_gutenberg_url(),
91
				'manageReusableBlocksUrl' => $this->get_calypso_origin() . '/types/wp_block/' . $site_suffix,
92
				'createNewPostUrl'        => $this->get_calypso_origin() . '/post/' . $site_suffix,
93
			)
94
		);
95
	}
96
97
	/**
98
	 * Returns the Calypso domain that originated the current request.
99
	 *
100
	 * @return string
101
	 */
102
	private function get_calypso_origin() {
103
		$origin  = ! empty( $_GET['origin'] ) ? $_GET['origin'] : 'https://wordpress.com'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
104
		$allowed = array(
105
			'http://calypso.localhost:3000',
106
			'http://127.0.0.1:41050', // Desktop App.
107
			'https://wpcalypso.wordpress.com',
108
			'https://horizon.wordpress.com',
109
			'https://wordpress.com',
110
		);
111
		return in_array( $origin, $allowed, true ) ? $origin : 'https://wordpress.com';
112
	}
113
114
	/**
115
	 * Returns the Calypso URL that displays either the current post type list (if no args
116
	 * are supplied) or the classic editor for the current post (if a post ID is supplied).
117
	 *
118
	 * @param int|null $post_id Post ID.
119
	 *
120
	 * @return string
121
	 */
122
	public function get_calypso_url( $post_id = null ) {
123
		$screen      = get_current_screen();
124
		$post_type   = $screen->post_type;
125
		$site_suffix = ( new Status() )->get_site_suffix();
126
127
		if ( is_null( $post_id ) ) {
128
			// E.g. posts or pages have no special suffix. CPTs are in the `types/{cpt}` format.
129
			$post_type_suffix = ( 'post' === $post_type || 'page' === $post_type )
130
				? "/${post_type}s/"
131
				: "/types/${post_type}/";
132
			$post_suffix      = '';
133
		} else {
134
			$post_type_suffix = ( 'post' === $post_type || 'page' === $post_type )
135
				? "/${post_type}/"
136
				: "/edit/${post_type}/";
137
			$post_suffix      = "/${post_id}";
138
		}
139
140
		return $this->get_calypso_origin() . $post_type_suffix . $site_suffix . $post_suffix;
141
	}
142
143
	/**
144
	 * Returns the URL to be used on the block editor close button for going back to the
145
	 * Calypso post list.
146
	 *
147
	 * @return string
148
	 */
149
	public function get_close_gutenberg_url() {
150
		return $this->get_calypso_url();
151
	}
152
153
	/**
154
	 * Returns the URL for switching the user's editor to the Calypso (WordPress.com Classic) editor.
155
	 *
156
	 * @return string
157
	 */
158
	public function get_switch_to_classic_editor_url() {
159
		return add_query_arg(
160
			'set-editor',
161
			'classic',
162
			$this->is_calypsoify_enabled ? $this->get_calypso_url( get_the_ID() ) : false
163
		);
164
	}
165
166
	/**
167
	 * Checks if the calypsoify user meta value is set, and deletes it if it is.
168
	 * This is to ensure that Calypsoify is not activated without the URL parameter.
169
	 */
170
	public function check_meta() {
171
		if ( ! empty( get_user_meta( get_current_user_id(), 'calypsoify', true ) ) ) {
172
			delete_user_meta( get_current_user_id(), 'calypsoify' );
173
		}
174
	}
175
176
	/**
177
	 * Return whether a post type should display the Gutenberg/block editor.
178
	 *
179
	 * @since 6.7.0
180
	 *
181
	 * @param string $post_type Post type.
182
	 */
183
	public function is_post_type_gutenberg( $post_type ) {
184
		return use_block_editor_for_post_type( $post_type );
185
	}
186
187
	/**
188
	 * Determines if the page is an instance of the Gutenberg block editor.
189
	 *
190
	 * @return bool
191
	 */
192
	public function is_page_gutenberg() {
193
		// phpcs:disable WordPress.Security.NonceVerification.Recommended
194
		// Disabling WordPress.Security.NonceVerification.Recommended because this function fires within admin_init and this is only changing display.
195
		$page = wp_basename( esc_url( $_SERVER['REQUEST_URI'] ) );
196
197
		if ( false !== strpos( $page, 'post-new.php' ) && empty( $_GET['post_type'] ) ) {
198
			return true;
199
		}
200
201
		if ( false !== strpos( $page, 'post-new.php' ) && isset( $_GET['post_type'] ) && $this->is_post_type_gutenberg( $_GET['post_type'] ) ) {
202
			return true;
203
		}
204
205 View Code Duplication
		if ( false !== strpos( $page, 'post.php' ) ) {
206
			$post = get_post( $_GET['post'] );
207
			if ( isset( $post ) && isset( $post->post_type ) && $this->is_post_type_gutenberg( $post->post_type ) ) {
208
				return true;
209
			}
210
		}
211
212 View Code Duplication
		if ( false !== strpos( $page, 'revision.php' ) ) {
213
			$post   = get_post( $_GET['revision'] );
214
			$parent = get_post( $post->post_parent );
215
			if ( isset( $parent ) && isset( $parent->post_type ) && $this->is_post_type_gutenberg( $parent->post_type ) ) {
216
				return true;
217
			}
218
		}
219
220
		return false;
221
		// phpcs:enable
222
	}
223
224
}
225
226
Jetpack_Calypsoify::get_instance();
227