Completed
Push — iterate/calypsoify ( 7a5a72...3d516f )
by George
16:45
created

manage_plugins_columns_header()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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