The connected page data or false if not identified. */ return apply_filters( 'woocommerce_navigation_is_connected_page', $is_connected_page, $current_page ); } /** * Returns true if we are on a page registed with this controller. * * @return boolean */ public function is_registered_page() { $current_page = $this->get_current_page(); if ( false === $current_page ) { $is_registered_page = false; } else { $is_registered_page = isset( $current_page['js_page'] ) && $current_page['js_page']; } /** * Whether or not the current page was registered with this controller. * * Used to determine if this is a JS-powered WooCommerce Admin page. * * @param boolean $is_registered_page True if the current page was registered with this controller. * @param array|boolean $current_page The registered page data or false if not identified. */ return apply_filters( 'woocommerce_navigation_is_registered_page', $is_registered_page, $current_page ); } /** * Adds a JS powered page to wc-admin. * * @param array $options { * Array describing the page. * * @type string id Id to reference the page. * @type string title Page title. Used in menus and breadcrumbs. * @type string|null parent Parent ID. Null for new top level page. * @type string path Path for this page, full path in app context; ex /analytics/report * @type string capability Capability needed to access the page. * @type string icon Icon. Dashicons helper class, base64-encoded SVG, or 'none'. * @type int position Menu item position. * @type int order Navigation item order. * } */ public function register_page( $options ) { $defaults = array( 'id' => null, 'parent' => null, 'title' => '', 'page_title' => '', 'capability' => 'view_woocommerce_reports', 'path' => '', 'icon' => '', 'position' => null, 'js_page' => true, ); $options = wp_parse_args( $options, $defaults ); if ( 0 !== strpos( $options['path'], self::PAGE_ROOT ) ) { $options['path'] = self::PAGE_ROOT . '&path=' . $options['path']; } if ( null !== $options['position'] ) { $options['position'] = intval( round( $options['position'] ) ); } if ( empty( $options['page_title'] ) ) { $options['page_title'] = $options['title']; } if ( is_null( $options['parent'] ) ) { add_menu_page( $options['page_title'], $options['title'], $options['capability'], $options['path'], array( __CLASS__, 'page_wrapper' ), $options['icon'], $options['position'] ); } else { $parent_path = $this->get_path_from_id( $options['parent'] ); // @todo check for null path. add_submenu_page( $parent_path, $options['page_title'], $options['title'], $options['capability'], $options['path'], array( __CLASS__, 'page_wrapper' ) ); } $this->connect_page( $options ); } /** * Get registered pages. * * @return array */ public function get_pages() { return $this->pages; } /** * Set up a div for the app to render into. */ public static function page_wrapper() { Loader::page_wrapper(); } /** * Connects existing WooCommerce pages. * * @todo The entry point for the embed needs moved to this class as well. */ public function register_page_handler() { require_once WC_ADMIN_ABSPATH . 'includes/react-admin/connect-existing-pages.php'; } /** * Registers the store details (profiler) page. */ public function register_store_details_page() { wc_admin_register_page( array( 'title' => __( 'Setup Wizard', 'woocommerce' ), 'parent' => '', 'path' => '/setup-wizard', ) ); } /** * Remove the menu item for the app entry point page. */ public function remove_app_entry_page_menu_item() { global $submenu; // User does not have capabilites to see the submenu. if ( ! current_user_can( 'manage_woocommerce' ) || empty( $submenu['woocommerce'] ) ) { return; } $wc_admin_key = null; foreach ( $submenu['woocommerce'] as $submenu_key => $submenu_item ) { // Our app entry page menu item has no title. if ( is_null( $submenu_item[0] ) && self::APP_ENTRY_POINT === $submenu_item[2] ) { $wc_admin_key = $submenu_key; break; } } if ( ! $wc_admin_key ) { return; } unset( $submenu['woocommerce'][ $wc_admin_key ] ); } /** * Returns true if we are on a JS powered admin page or * a "classic" (non JS app) powered admin page (an embedded page). */ public static function is_admin_or_embed_page() { return self::is_admin_page() || self::is_embed_page(); } /** * Returns true if we are on a JS powered admin page. */ public static function is_admin_page() { // phpcs:disable WordPress.Security.NonceVerification return isset( $_GET['page'] ) && 'wc-admin' === $_GET['page']; // phpcs:enable WordPress.Security.NonceVerification } /** * Returns true if we are on a "classic" (non JS app) powered admin page. * * TODO: See usage in `admin.php`. This needs refactored and implemented properly in core. */ public static function is_embed_page() { return wc_admin_is_connected_page() || ( ! self::is_admin_page() && class_exists( 'Automattic\WooCommerce\Admin\Features\Navigation\Screen' ) && Screen::is_woocommerce_page() ); } }