Completed
Push — add/calypsoify-site-importer ( 2f6031 )
by
unknown
07:17
created

Jetpack_Calypsoify::is_active()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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