PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.6.2
Jetpack – WP Security, Backup, Speed, & Growth v7.6.2
16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 All 503 releases
jetpack / modules / calypsoify / class.jetpack-calypsoify.php

class.jetpack-calypsoify.php in Jetpack – WP Security, Backup, Speed, & Growth 7.6.2, at modules/calypsoify/class.jetpack-calypsoify.php

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