Completed
Push — update/calypsoify-back-button ( f467fe )
by Kirk
126:54 queued 119:49
created

Jetpack_Calypsoify::get_calypso_origin()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 11
rs 9.9
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
class Jetpack_Calypsoify {
8
	static $instance = false;
9
10
	private function __construct() {
11
		add_action( 'wp_loaded', array( $this, 'setup' ) );
12
	}
13
14
	public static function getInstance() {
15
		if ( ! self::$instance ) {
16
			self::$instance = new self();
0 ignored issues
show
Documentation Bug introduced by
It seems like new self() of type object<Jetpack_Calypsoify> is incompatible with the declared type boolean of property $instance.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

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