Completed
Push — iterate/calypsoify ( 3d516f...69051d )
by George
39:12 queued 1434:48
created

Jetpack_Calypsoify::bulk_actions_plugins()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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