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