Automattic /
jetpack
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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
|
|||
| 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 | |||
| 44 | public function mock_masterbar_activation() { |
||
| 45 | include dirname( __FILE__ ) . '/masterbar/masterbar.php'; |
||
| 46 | new A8C_WPCOM_Masterbar; |
||
| 47 | } |
||
| 48 | |||
| 49 | public function remove_core_menus() { |
||
| 50 | remove_menu_page( 'index.php' ); |
||
| 51 | remove_menu_page( 'jetpack' ); |
||
| 52 | remove_menu_page( 'edit.php' ); |
||
| 53 | remove_menu_page( 'edit.php?post_type=feedback' ); |
||
| 54 | remove_menu_page( 'upload.php' ); |
||
| 55 | remove_menu_page( 'edit.php?post_type=page' ); |
||
| 56 | remove_menu_page( 'edit-comments.php' ); |
||
| 57 | remove_menu_page( 'themes.php' ); |
||
| 58 | remove_menu_page( 'plugins.php' ); |
||
| 59 | remove_menu_page( 'users.php' ); |
||
| 60 | remove_menu_page( 'tools.php' ); |
||
| 61 | remove_menu_page( 'link-manager.php' ); |
||
| 62 | |||
| 63 | // Core settings pages |
||
| 64 | remove_submenu_page( 'options-general.php', 'options-general.php' ); |
||
| 65 | remove_submenu_page( 'options-general.php', 'options-writing.php' ); |
||
| 66 | remove_submenu_page( 'options-general.php', 'options-reading.php' ); |
||
| 67 | remove_submenu_page( 'options-general.php', 'options-discussion.php' ); |
||
| 68 | remove_submenu_page( 'options-general.php', 'options-media.php' ); |
||
| 69 | remove_submenu_page( 'options-general.php', 'options-permalink.php' ); |
||
| 70 | remove_submenu_page( 'options-general.php', 'privacy.php' ); |
||
| 71 | remove_submenu_page( 'options-general.php', 'sharing' ); |
||
| 72 | } |
||
| 73 | |||
| 74 | public function add_plugin_menus() { |
||
| 75 | global $menu, $submenu; |
||
| 76 | |||
| 77 | add_menu_page( __( 'Manage Plugins', 'jetpack' ), __( 'Manage Plugins', 'jetpack' ), 'activate_plugins', 'plugins.php', '', $this->installed_plugins_icon(), 1 ); |
||
| 78 | |||
| 79 | // // Count the settings page submenus, if it's zero then don't show this. |
||
| 80 | if ( empty( $submenu['options-general.php'] ) ) { |
||
| 81 | remove_menu_page( 'options-general.php' ); |
||
| 82 | } else { |
||
| 83 | // Rename and make sure the plugin settings menu is always last. |
||
| 84 | // Sneaky plugins seem to override this otherwise. |
||
| 85 | // Settings is always key 80. |
||
| 86 | $menu[80][0] = __( 'Plugin Settings', 'jetpack' ); |
||
| 87 | $menu[ max( array_keys( $menu ) ) + 1 ] = $menu[80]; |
||
| 88 | unset( $menu[80] ); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | public function enqueue() { |
||
| 93 | wp_enqueue_style( 'calypsoify_wpadminmods_css', plugin_dir_url( __FILE__ ) . 'calypsoify/style.css', false, JETPACK__VERSION ); |
||
| 94 | wp_style_add_data( 'calypsoify_wpadminmods_css', 'rtl', 'replace' ); |
||
| 95 | |||
| 96 | wp_enqueue_script( 'calypsoify_wpadminmods_js', plugin_dir_url( __FILE__ ) . 'calypsoify/mods.js', false, JETPACK__VERSION ); |
||
| 97 | } |
||
| 98 | |||
| 99 | public function enqueue_for_gutenberg() { |
||
| 100 | wp_enqueue_style( 'calypsoify_wpadminmods_css', plugin_dir_url( __FILE__ ) . 'calypsoify/style-gutenberg.css', false, JETPACK__VERSION ); |
||
| 101 | wp_style_add_data( 'calypsoify_wpadminmods_css', 'rtl', 'replace' ); |
||
| 102 | |||
| 103 | wp_enqueue_script( 'calypsoify_wpadminmods_js', plugin_dir_url( __FILE__ ) . 'calypsoify/mods-gutenberg.js', false, JETPACK__VERSION ); |
||
| 104 | wp_localize_script( |
||
| 105 | 'calypsoify_wpadminmods_js', |
||
| 106 | 'calypsoifyGutenberg', |
||
| 107 | array( |
||
| 108 | 'closeUrl' => $this->get_close_gutenberg_url(), |
||
| 109 | ) |
||
| 110 | ); |
||
| 111 | } |
||
| 112 | |||
| 113 | public function insert_sidebar_html() { ?> |
||
| 114 | <a href="<?php echo esc_url( 'https://wordpress.com/stats/day/' . Jetpack::build_raw_urls( home_url() ) ); ?>" id="calypso-sidebar-header"> |
||
| 115 | <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> |
||
| 116 | |||
| 117 | <ul> |
||
| 118 | <li id="calypso-sitename"><?php bloginfo( 'name' ); ?></li> |
||
| 119 | <li id="calypso-plugins"><?php esc_html_e( 'Plugins' ); ?></li> |
||
| 120 | </ul> |
||
| 121 | </a> |
||
| 122 | <?php |
||
| 123 | } |
||
| 124 | |||
| 125 | public function modify_masterbar() { |
||
| 126 | global $wp_admin_bar; |
||
| 127 | |||
| 128 | // Add proper links to masterbar top sections. |
||
| 129 | $my_sites_node = $wp_admin_bar->get_node( 'blog' ); |
||
| 130 | $my_sites_node->href = 'https://wordpress.com/stats/day/' . Jetpack::build_raw_urls( home_url() ); |
||
| 131 | $wp_admin_bar->add_node( $my_sites_node ); |
||
| 132 | |||
| 133 | $reader_node = $wp_admin_bar->get_node( 'newdash' ); |
||
| 134 | $reader_node->href = 'https://wordpress.com'; |
||
| 135 | $wp_admin_bar->add_node( $reader_node ); |
||
| 136 | |||
| 137 | $me_node = $wp_admin_bar->get_node( 'my-account' ); |
||
| 138 | $me_node->href = 'https://wordpress.com/me'; |
||
| 139 | $wp_admin_bar->add_node( $me_node ); |
||
| 140 | } |
||
| 141 | |||
| 142 | private function installed_plugins_icon() { |
||
| 143 | $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>'; |
||
| 144 | |||
| 145 | return 'data:image/svg+xml;base64,' . base64_encode( $svg ); |
||
| 146 | } |
||
| 147 | |||
| 148 | public function get_close_gutenberg_url() { |
||
| 149 | $screen = get_current_screen(); |
||
| 150 | |||
| 151 | // E.g. `posts`, `pages`, or `types/some_custom_post_type` |
||
| 152 | $post_type = ( 'post' === $screen->post_type || 'page' === $screen->post_type ) |
||
| 153 | ? $screen->post_type . 's' |
||
| 154 | : 'types/' . $screen->post_type; |
||
| 155 | |||
| 156 | return 'https://wordpress.com/' . $post_type . '/' . Jetpack::build_raw_urls( home_url() ); |
||
| 157 | } |
||
| 158 | |||
| 159 | public function check_param() { |
||
| 160 | if ( isset( $_GET['calypsoify'] ) ) { |
||
| 161 | if ( 1 == (int) $_GET['calypsoify'] ) { |
||
| 162 | update_user_meta( get_current_user_id(), 'calypsoify', 1 ); |
||
| 163 | } else { |
||
| 164 | update_user_meta( get_current_user_id(), 'calypsoify', 0 ); |
||
| 165 | } |
||
| 166 | |||
| 167 | $page = remove_query_arg( 'calypsoify', wp_basename( $_SERVER['REQUEST_URI'] ) ); |
||
| 168 | |||
| 169 | wp_safe_redirect( admin_url( $page ) ); |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | public function check_page() { |
||
| 174 | // If the user hits plain /wp-admin/ then disable Calypso styles. |
||
| 175 | $page = wp_basename( esc_url( $_SERVER['REQUEST_URI'] ) ); |
||
| 176 | |||
| 177 | if ( false !== strpos( 'index.php', $page ) || false !== strpos( 'wp-admin', $page ) ) { |
||
| 178 | update_user_meta( get_current_user_id(), 'calypsoify', 0 ); |
||
| 179 | wp_safe_redirect( admin_url() ); |
||
| 180 | die; |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Return whether a post type should display the Gutenberg/block editor. |
||
| 186 | * |
||
| 187 | * @since 6.7.0 |
||
| 188 | */ |
||
| 189 | public function is_post_type_gutenberg( $post_type ) { |
||
| 190 | // @TODO: Remove function check once 5.0 is the minimum supported WP version. |
||
| 191 | if ( function_exists( 'use_block_editor_for_post_type' ) ) { |
||
| 192 | return use_block_editor_for_post_type( $post_type ); |
||
| 193 | } else { |
||
| 194 | // We use the filter introduced in WordPress 5.0 to be backwards compatible. |
||
| 195 | /** This filter is already documented in core/wp-admin/includes/post.php */ |
||
| 196 | return apply_filters( 'use_block_editor_for_post_type', true, $post_type ); |
||
| 197 | } |
||
| 198 | } |
||
| 199 | |||
| 200 | public function is_page_gutenberg() { |
||
| 201 | if ( ! Jetpack_Gutenberg::is_gutenberg_available() ) { |
||
| 202 | return false; |
||
| 203 | } |
||
| 204 | |||
| 205 | $page = wp_basename( esc_url( $_SERVER['REQUEST_URI'] ) ); |
||
| 206 | |||
| 207 | if ( false !== strpos( $page, 'post-new.php' ) && empty ( $_GET['post_type'] ) ) { |
||
| 208 | return true; |
||
| 209 | } |
||
| 210 | |||
| 211 | if ( false !== strpos( $page, 'post-new.php' ) && isset( $_GET['post_type'] ) && $this->is_post_type_gutenberg( $_GET['post_type'] ) ) { |
||
| 212 | return true; |
||
| 213 | } |
||
| 214 | |||
| 215 | View Code Duplication | if ( false !== strpos( $page, 'post.php' ) ) { |
|
| 216 | $post = get_post( $_GET['post'] ); |
||
| 217 | if ( isset( $post ) && isset( $post->post_type ) && $this->is_post_type_gutenberg( $post->post_type ) ) { |
||
| 218 | return true; |
||
| 219 | } |
||
| 220 | } |
||
| 221 | |||
| 222 | View Code Duplication | if ( false !== strpos( $page, 'revision.php' ) ) { |
|
| 223 | $post = get_post( $_GET['revision'] ); |
||
| 224 | $parent = get_post( $post->post_parent ); |
||
| 225 | if ( isset( $parent ) && isset( $parent->post_type ) && $this->is_post_type_gutenberg( $parent->post_type ) ) { |
||
| 226 | return true; |
||
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | return false; |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | $Jetpack_Calypsoify = Jetpack_Calypsoify::getInstance(); |
||
| 235 |
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..