https://github.com/WordPress/gutenberg/pull/6151 */ remove_action( 'admin_print_scripts', 'print_emoji_detection_script' ); } /** * Add custom tables to $wpdb object. */ public static function define_tables() { global $wpdb; // List of tables without prefixes. $tables = array( 'wc_category_lookup' => 'wc_category_lookup', ); foreach ( $tables as $name => $table ) { $wpdb->$name = $wpdb->prefix . $table; $wpdb->tables[] = $table; } } /** * Returns true if WooCommerce Admin is currently running in a development environment. */ public static function is_dev() { if ( self::is_feature_enabled( 'devdocs' ) && defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) { return true; } return false; } /** * Gets an array of enabled WooCommerce Admin features/sections. * * @return bool Enabled Woocommerce Admin features/sections. */ public static function get_features() { return apply_filters( 'woocommerce_admin_features', array() ); } /** * Gets WordPress capability required to use analytics features. * * @return string */ public static function get_analytics_capability() { if ( null === static::$required_capability ) { /** * Filters the required capability to use the analytics features. * * @param string $capability WordPress capability. */ static::$required_capability = apply_filters( 'woocommerce_analytics_menu_capability', 'view_woocommerce_reports' ); } return static::$required_capability; } /** * Helper function indicating whether the current user has the required analytics capability. * * @return bool */ public static function user_can_analytics() { return current_user_can( static::get_analytics_capability() ); } /** * Returns if a specific wc-admin feature is enabled. * * @param string $feature Feature slug. * @return bool Returns true if the feature is enabled. */ public static function is_feature_enabled( $feature ) { $features = self::get_features(); return in_array( $feature, $features, true ); } /** * Returns if the onboarding feature of WooCommerce Admin should be enabled. * * While we preform an a/b test of onboarding, the feature will be enabled within the plugin build, but only if the user recieved the test/opted in. * * @return bool Returns true if the onboarding is enabled. */ public static function is_onboarding_enabled() { if ( ! self::is_feature_enabled( 'onboarding' ) ) { return false; } $onboarding_opt_in = 'yes' === get_option( Onboarding::OPT_IN_OPTION, 'no' ); $legacy_onboarding_opt_in = 'yes' === get_option( 'wc_onboarding_opt_in', 'no' ); if ( $onboarding_opt_in || $legacy_onboarding_opt_in ) { return true; } return false; } /** * Gets the URL to an asset file. * * @param string $file File name (without extension). * @param string $ext File extension. * @return string URL to asset. */ public static function get_url( $file, $ext ) { $suffix = ''; // Potentially enqueue minified JavaScript. if ( 'js' === $ext ) { $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; } return plugins_url( self::get_path( $ext ) . $file . $suffix . '.' . $ext, WC_ADMIN_PLUGIN_FILE ); } /** * Gets the file modified time as a cache buster if we're in dev mode, or the plugin version otherwise. * * @param string $ext File extension. * @return string The cache buster value to use for the given file. */ public static function get_file_version( $ext ) { if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) { return filemtime( WC_ADMIN_ABSPATH . self::get_path( $ext ) ); } return WC_ADMIN_VERSION_NUMBER; } /** * Gets the path for the asset depending on file type. * * @param string $ext File extension. * @return string Folder path of asset. */ private static function get_path( $ext ) { return ( 'css' === $ext ) ? WC_ADMIN_DIST_CSS_FOLDER : WC_ADMIN_DIST_JS_FOLDER; } /** * Class loader for enabled WooCommerce Admin features/sections. */ public static function load_features() { $features = self::get_features(); foreach ( $features as $feature ) { $feature = str_replace( '-', '', ucwords( strtolower( $feature ), '-' ) ); $feature = 'Automattic\\WooCommerce\\Admin\\Features\\' . $feature; if ( class_exists( $feature ) ) { new $feature(); } } } /** * Registers a basic page handler for the app entry point. * * @todo The entry point for the embed needs moved to this class as well. */ public static function register_page_handler() { $features = wc_admin_get_feature_config(); $id = $features['homepage'] ? 'woocommerce-home' : 'woocommerce-dashboard'; wc_admin_register_page( array( 'id' => $id, // Expected to be overridden if dashboard is enabled. 'parent' => 'woocommerce', 'title' => null, 'path' => self::APP_ENTRY_POINT, 'capability' => static::get_analytics_capability(), ) ); // Connect existing WooCommerce pages. require_once WC_ADMIN_ABSPATH . 'includes/connect-existing-pages.php'; } /** * Remove the menu item for the app entry point page. */ public static 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 ] ); } /** * Registers all the neccessary scripts and styles to show the admin experience. */ public static function register_scripts() { if ( ! function_exists( 'wp_set_script_translations' ) ) { return; } $js_file_version = self::get_file_version( 'js' ); $css_file_version = self::get_file_version( 'css' ); wp_register_script( 'wc-csv', self::get_url( 'csv-export/index', 'js' ), array( 'moment' ), $js_file_version, true ); wp_register_script( 'wc-currency', self::get_url( 'currency/index', 'js' ), array( 'wc-number' ), $js_file_version, true ); wp_set_script_translations( 'wc-currency', 'woocommerce-admin' ); wp_register_script( 'wc-navigation', self::get_url( 'navigation/index', 'js' ), array(), $js_file_version, true ); wp_register_script( 'wc-number', self::get_url( 'number/index', 'js' ), array(), $js_file_version, true ); wp_register_script( 'wc-date', self::get_url( 'date/index', 'js' ), array( 'moment', 'wp-date', 'wp-i18n' ), $js_file_version, true ); wp_register_script( 'wc-store-data', self::get_url( 'data/index', 'js' ), array(), $js_file_version, true ); wp_set_script_translations( 'wc-date', 'woocommerce-admin' ); wp_register_script( 'wc-components', self::get_url( 'components/index', 'js' ), array( 'moment', 'wp-api-fetch', 'wp-data', 'wp-data-controls', 'wp-element', 'wp-hooks', 'wp-html-entities', 'wp-i18n', 'wp-keycodes', 'wc-csv', 'wc-currency', 'wc-date', 'wc-navigation', 'wc-number', 'wc-store-data', ), $js_file_version, true ); wp_set_script_translations( 'wc-components', 'woocommerce-admin' ); wp_register_style( 'wc-components', self::get_url( 'components/style', 'css' ), array(), $css_file_version ); wp_style_add_data( 'wc-components', 'rtl', 'replace' ); wp_register_style( 'wc-components-ie', self::get_url( 'components/ie', 'css' ), array(), $css_file_version ); wp_style_add_data( 'wc-components-ie', 'rtl', 'replace' ); wp_register_script( WC_ADMIN_APP, self::get_url( 'app/index', 'js' ), array( 'wc-components', 'wc-navigation', 'wp-date', 'wp-html-entities', 'wp-keycodes', 'wp-i18n', 'moment' ), $js_file_version, true ); wp_localize_script( WC_ADMIN_APP, 'wcAdminAssets', array( 'path' => plugins_url( self::get_path( 'js' ), WC_ADMIN_PLUGIN_FILE ), ) ); wp_set_script_translations( WC_ADMIN_APP, 'woocommerce-admin' ); wp_register_style( WC_ADMIN_APP, self::get_url( 'app/style', 'css' ), array( 'wc-components' ), $css_file_version ); wp_style_add_data( WC_ADMIN_APP, 'rtl', 'replace' ); wp_register_style( 'wc-admin-ie', self::get_url( 'ie/style', 'css' ), array( WC_ADMIN_APP ), $css_file_version ); wp_style_add_data( 'wc-admin-ie', 'rtl', 'replace' ); wp_register_style( 'wc-material-icons', 'https://fonts.googleapis.com/icon?family=Material+Icons+Outlined', array(), $css_file_version ); } /** * Loads the required scripts on the correct pages. */ public static function load_scripts() { if ( ! self::is_admin_or_embed_page() ) { return; } if ( ! static::user_can_analytics() ) { return; } wp_enqueue_script( WC_ADMIN_APP ); wp_enqueue_style( WC_ADMIN_APP ); wp_enqueue_style( 'wc-material-icons' ); // Use server-side detection to prevent unneccessary stylesheet loading in other browsers. $user_agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : ''; // phpcs:ignore sanitization ok. preg_match( '/MSIE (.*?);/', $user_agent, $matches ); if ( count( $matches ) < 2 ) { preg_match( '/Trident\/\d{1,2}.\d{1,2}; rv:([0-9]*)/', $user_agent, $matches ); } if ( count( $matches ) > 1 ) { wp_enqueue_style( 'wc-components-ie' ); wp_enqueue_style( 'wc-admin-ie' ); } // Preload our assets. self::output_header_preload_tags(); } /** * Render a preload link tag for a dependency, optionally * checked against a provided whitelist. * * See: https://macarthur.me/posts/preloading-javascript-in-wordpress * * @param WP_Dependency $dependency The WP_Dependency being preloaded. * @param string $type Dependency type - 'script' or 'style'. * @param array $whitelist Optional. List of allowed dependency handles. */ public static function maybe_output_preload_link_tag( $dependency, $type, $whitelist = array() ) { if ( ! empty( $whitelist ) && ! in_array( $dependency->handle, $whitelist, true ) ) { return; } $source = $dependency->ver ? add_query_arg( 'ver', $dependency->ver, $dependency->src ) : $dependency->src; echo '', "\n"; } /** * Output a preload link tag for dependencies (and their sub dependencies) * with an optional whitelist. * * See: https://macarthur.me/posts/preloading-javascript-in-wordpress * * @param string $type Dependency type - 'script' or 'style'. * @param array $whitelist Optional. List of allowed dependency handles. */ public static function output_header_preload_tags_for_type( $type, $whitelist = array() ) { if ( 'script' === $type ) { $dependencies_of_type = wp_scripts(); } elseif ( 'style' === $type ) { $dependencies_of_type = wp_styles(); } else { return; } foreach ( $dependencies_of_type->queue as $dependency_handle ) { $dependency = $dependencies_of_type->registered[ $dependency_handle ]; // Preload the subdependencies first. foreach ( $dependency->deps as $sub_dependency_handle ) { $sub_dependency = $dependencies_of_type->registered[ $sub_dependency_handle ]; self::maybe_output_preload_link_tag( $sub_dependency, $type, $whitelist ); } self::maybe_output_preload_link_tag( $dependency, $type, $whitelist ); } } /** * Output preload link tags for all enqueued stylesheets and scripts. * * See: https://macarthur.me/posts/preloading-javascript-in-wordpress */ public static function output_header_preload_tags() { $wc_admin_scripts = array( WC_ADMIN_APP, 'wc-components', ); $wc_admin_styles = array( WC_ADMIN_APP, 'wc-components', 'wc-components-ie', 'wc-admin-ie', 'wc-material-icons', ); // Preload styles. self::output_header_preload_tags_for_type( 'style', $wc_admin_styles ); // Preload scripts. self::output_header_preload_tags_for_type( 'script', $wc_admin_scripts ); } /** * 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() { return wc_admin_is_registered_page(); } /** * 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(); } /** * Returns breadcrumbs for the current page. */ private static function get_embed_breadcrumbs() { return wc_admin_get_breadcrumbs(); } /** * Outputs breadcrumbs via PHP for the initial load of an embedded page. * * @param array $section Section to create breadcrumb from. */ private static function output_breadcrumbs( $section ) { if ( ! static::user_can_analytics() ) { return; } ?> is_woocommerce_page() ) { self::embed_navigation_menu(); } if ( ! self::is_admin_page() && ! self::is_embed_page() ) { return; } if ( ! static::user_can_analytics() ) { return; } if ( ! self::is_embed_page() ) { return; } $sections = self::get_embed_breadcrumbs(); $sections = is_array( $sections ) ? $sections : array( $sections ); ?>
). * * @param bool $is_loading If WooCommerce Admin is loading a fullscreen view. */ $is_loading = apply_filters( 'woocommerce_admin_is_loading', false ); if ( self::is_admin_page() && $is_loading ) { $classes[] = 'woocommerce-admin-is-loading'; } $features = self::get_features(); foreach ( $features as $feature_key ) { $classes[] = sanitize_html_class( 'woocommerce-feature-enabled-' . $feature_key ); } $admin_body_class = implode( ' ', array_unique( $classes ) ); return " $admin_body_class "; } /** * Removes notices that should not be displayed on WC Admin pages. */ public static function remove_notices() { if ( ! self::is_admin_or_embed_page() ) { return; } // Hello Dolly. if ( function_exists( 'hello_dolly' ) ) { remove_action( 'admin_notices', 'hello_dolly' ); } } /** * Runs before admin notices action and hides them. */ public static function inject_before_notices() { if ( ! self::is_admin_or_embed_page() ) { return; } // Wrap the notices in a hidden div to prevent flickering before // they are moved elsewhere in the page by WordPress Core. echo '