Completed
Push — add/calypsoify ( a317bb )
by
unknown
06:59
created

Jetpack_Calypsoify::enqueue_for_gutenberg()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
Plugin Name: Calypsoify
4
Plugin URI: https://wordpress.com
5
Description: WP-Admin skin and plugins only.
6
Author: Automattic
7
Author URI: https://automattic.com
8
Version: 0.2
9
*/
10
11
if( ! defined( 'CALYPSOIFY_VER' ) )
12
	define( 'CALYPSOIFY_VER', '0.2' );
13
14
class Jetpack_Calypsoify {
15
	static $instance = false;
16
17
	private function __construct() {
18
		add_action( 'wp_loaded', array( $this, 'setup' ) );
19
	}
20
21
	public static function getInstance() {
22
		if ( ! self::$instance )
23
			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...
24
		
25
		return self::$instance;
26
	}
27
28
	public function setup() {
29
		add_action( 'admin_init', array( $this, 'check_param' ) );
30
			
31
		if ( 1 == (int) get_user_meta( get_current_user_id(), 'calypsoify', true ) ) {
32
			if ( $this->is_page_gutenberg() ) {
33
				add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_for_gutenberg' ), 100 );
34
				return;
35
			}
36
			add_action( 'admin_init', array( $this, 'check_page' ) );
37
			add_action( 'admin_menu', array( $this, 'remove_core_menus' ), 100 );
38
			add_action( 'admin_menu', array( $this, 'add_plugin_menus' ), 101 );
39
			add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ), 100 );
40
			add_action( 'in_admin_header', array( $this, 'insert_sidebar_html' ) );
41
			add_action( 'wp_before_admin_bar_render', array( $this, 'modify_masterbar' ), 100000 );
42
		}	
43
	}
44
45
	public function remove_core_menus() {
46
		remove_menu_page( 'index.php' );
47
		remove_menu_page( 'jetpack' );
48
		remove_menu_page( 'edit.php' );
49
		remove_menu_page( 'edit.php?post_type=feedback' );
50
		remove_menu_page( 'upload.php' );
51
		remove_menu_page( 'edit.php?post_type=page' );
52
		remove_menu_page( 'edit-comments.php' );
53
		remove_menu_page( 'themes.php' );
54
		remove_menu_page( 'plugins.php' );
55
		remove_menu_page( 'users.php' );
56
		remove_menu_page( 'tools.php' );
57
		remove_menu_page( 'link-manager.php' );
58
59
		// Core settings pages
60
		remove_submenu_page( 'options-general.php', 'options-general.php' ); 
61
		remove_submenu_page( 'options-general.php', 'options-writing.php' ); 
62
		remove_submenu_page( 'options-general.php', 'options-reading.php' ); 
63
		remove_submenu_page( 'options-general.php', 'options-discussion.php' ); 
64
		remove_submenu_page( 'options-general.php', 'options-media.php' ); 
65
		remove_submenu_page( 'options-general.php', 'options-permalink.php' );
66
		remove_submenu_page( 'options-general.php', 'privacy.php' );
67
		remove_submenu_page( 'options-general.php', 'sharing' ); 
68
	}
69
70
	public function add_plugin_menus() {
71
		global $menu, $submenu;
72
73
		add_menu_page( __( 'Manage Plugins', 'jetpack' ), __( 'Manage Plugins', 'jetpack' ), 'activate_plugins', 'plugins.php', '', $this->installed_plugins_icon(), 1);
74
75
		// // Count the settings page submenus, if it's zero then don't show this.
76
		if ( empty( $submenu['options-general.php'] ) ) {
77
			remove_menu_page( 'options-general.php' ); 
78
		} else {
79
			// Rename and make sure the plugin settings menu is always last.
80
			// Sneaky plugins seem to override this otherwise.
81
			// Settings is always key 80.
82
			$menu[80][0] = __( 'Plugin Settings', 'jetpack' );
83
			$menu[max(array_keys($menu))+1] = $menu[80];
84
			unset($menu[80]);
85
		}
86
	}
87
88
	public function enqueue() {
89
		wp_enqueue_style( 'calypsoify_wpadminmods_css', plugin_dir_url( __FILE__ ) . 'calypsoify/style.css', false, CALYPSOIFY_VER );
90
  		wp_enqueue_script( 'calypsoify_wpadminmods_js', plugin_dir_url( __FILE__ ) . 'calypsoify/mods.js', false, CALYPSOIFY_VER );
91
	}
92
	
93
	public function enqueue_for_gutenberg() {
94
		wp_enqueue_style( 'calypsoify_wpadminmods_css', plugin_dir_url( __FILE__ ) . 'calypsoify/style-gutenberg.css', false, CALYPSOIFY_VER );
95
		wp_enqueue_script( 'calypsoify_wpadminmods_js', plugin_dir_url( __FILE__ ) . 'calypsoify/mods-gutenberg.js', false, CALYPSOIFY_VER );
96
		wp_localize_script( 'calypsoify_wpadminmods_js', 'calypsoifyGutenberg', array(
97
			'closeLabel' => __( 'Close', 'jetpack' ),
98
			'closeUrl' => $this->get_close_gutenberg_url(),
99
		) );
100
	}
101
102
  public function insert_sidebar_html() { ?>
103
		<a href="<?php echo esc_url( "https://wordpress.com/stats/day/" . str_replace( 'https://', '', get_bloginfo('url') ) ) ?>" id="calypso-sidebar-header">
104
		  <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>
105
106
		  <ul>
107
		    <li id="calypso-sitename"><?php bloginfo( 'name' ) ?></li>
108
		    <li id="calypso-plugins"><?php esc_html_e( 'Plugins' ) ?></li>
109
		  </ul>
110
		</a><?php
111
	}
112
113
	public function modify_masterbar() {
114
		global $wp_admin_bar;
115
116
		// Add proper links to masterbar top sections.
117
118
		$my_sites_node = $wp_admin_bar->get_node( 'blog' );
119
		$my_sites_node->href = "https://wordpress.com/stats/day/" . str_replace( 'https://', '', get_bloginfo( 'url' ) );
120
		$wp_admin_bar->add_node( $my_sites_node );
121
122
		$reader_node = $wp_admin_bar->get_node( 'newdash' );
123
		$reader_node->href = "https://wordpress.com";
124
		$wp_admin_bar->add_node( $reader_node );
125
126
		$me_node = $wp_admin_bar->get_node( 'my-account' );
127
		$me_node->href = "https://wordpress.com/me";
128
		$wp_admin_bar->add_node( $me_node );
129
	}
130
131
	private function installed_plugins_icon() {
132
		$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>';
133
134
		return 'data:image/svg+xml;base64,' . base64_encode( $svg );
135
	}
136
137
	public function get_close_gutenberg_url() {
138
		$screen = get_current_screen();
139
140
		// E.g. `posts`, `pages`, or `types/some_custom_post_type`
141
		$post_type = ( 'post' === $screen->post_type || 'page' === $screen->post_type )
142
			? $screen->post_type . 's'
143
			: 'types/' . $screen->post_type;
144
145
		return 'https://wordpress.com/' . $post_type . '/' . Jetpack::build_raw_urls( home_url() ) ;
146
	}
147
148
	public function check_param() {
149
		if ( isset( $_GET['calypsoify'] ) ) {
150
			if ( 1 == (int) $_GET['calypsoify'] ) {
151
				update_user_meta( get_current_user_id(), 'calypsoify', 1 );
152
			} else {
153
				update_user_meta( get_current_user_id(), 'calypsoify', 0 );
154
			}
155
156
			$page = remove_query_arg( 'calypsoify', wp_basename( $_SERVER['REQUEST_URI'] ) );
157
158
			wp_safe_redirect( admin_url( $page ) );
159
		}
160
	}
161
162
	public function check_page() {
163
		// If the user hits plain /wp-admin/ then disable Calypso styles.
164
		$page = wp_basename( esc_url( $_SERVER['REQUEST_URI'] ) );
165
166
		if ( false !== strpos( 'index.php', $page ) || false !== strpos( 'wp-admin', $page ) ) {
167
			update_user_meta( get_current_user_id(), 'calypsoify', 0 );
168
			wp_safe_redirect( admin_url() );
169
			die;
170
		}
171
	}
172
173
	public function is_page_gutenberg() {
174
		$page = wp_basename( esc_url( $_SERVER['REQUEST_URI'] ) );
175
		return function_exists( 'register_block_type' ) && (
176
				false !== strpos( $page, 'post.php' ) ||
177
				false !== strpos( $page, 'post-new.php' ) ||
178
				false !== strpos( $page, 'revision.php' )
179
			);
180
	}
181
}
182
183
$Jetpack_Calypsoify = Jetpack_Calypsoify::getInstance();
184