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 a build configured array of enabled WooCommerce Admin features/sections. * * @return array 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 ) { if ( 'homescreen' === $feature && 'yes' !== get_option( 'woocommerce_homescreen_enabled', 'no' ) ) { return false; } $features = self::get_features(); return in_array( $feature, $features, true ); } /** * Determines if a minified JS file should be served. * * @param boolean $script_debug Only serve unminified files if script debug is on. * @return boolean If js asset should use minified version. */ public static function should_use_minified_js_file( $script_debug ) { // un-minified files are only shipped in non-core versions of wc-admin, return true if unminified files are not available. if ( ! self::is_feature_enabled( 'unminified-js' ) ) { return true; } // Otherwise we will serve un-minified files if SCRIPT_DEBUG is on, or if anything truthy is passed in-lieu of SCRIPT_DEBUG. return ! $script_debug; } /** * 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 ) { $script_debug = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG; $suffix = self::should_use_minified_js_file( $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() { $id = self::is_feature_enabled( 'homescreen' ) ? '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'; } /** * Registers the profiler page. */ public static function register_profiler_page() { wc_admin_register_page( array( 'title' => 'Profiler', 'parent' => '', 'path' => '/profiler', ) ); } /** * 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-tracks', self::get_url( 'tracks/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( 'wp-core-data', 'wc-components', 'wp-date', 'wc-tracks', ), $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' ); // The "app" RTL files are in a different format than the components. $rtl = is_rtl() ? '.rtl' : ''; wp_register_style( WC_ADMIN_APP, self::get_url( "app/style{$rtl}", 'css' ), array( 'wc-components' ), $css_file_version ); wp_register_style( 'wc-admin-ie', self::get_url( "ie/style{$rtl}", 'css' ), array( WC_ADMIN_APP ), $css_file_version ); wp_register_style( 'wc-material-icons', 'https://fonts.googleapis.com/icon?family=Material+Icons+Outlined', array(), $css_file_version ); } /** * Generate a filename to cache translations from JS chunks. * * @param string $domain Text domain. * @param string $locale Locale being retrieved. * @return string Filename. */ public static function get_combined_translation_filename( $domain, $locale ) { $version = self::get_file_version( 'js' ); $filename = implode( '-', array( $domain, $locale, WC_ADMIN_APP, $version ) ) . '.json'; return $filename; } /** * Find and combine translation chunk files. * * Only targets files that aren't represented by a registered script (e.g. not passed to wp_register_script()). * * @param string $lang_dir Path to language files. * @param string $domain Text domain. * @param string $locale Locale being retrieved. * @return array Combined translation chunk data. */ public static function get_translation_chunk_data( $lang_dir, $domain, $locale ) { global $wp_filesystem; // Grab all JSON files in the current language pack. $json_i18n_filenames = glob( $lang_dir . $domain . '-' . $locale . '-*.json' ); $combined_translation_data = array(); foreach ( $json_i18n_filenames as $json_filename ) { if ( ! $wp_filesystem->is_readable( $json_filename ) ) { continue; } $file_contents = $wp_filesystem->get_contents( $json_filename ); $chunk_data = \json_decode( $file_contents, true ); if ( empty( $chunk_data ) ) { continue; } if ( ! isset( $chunk_data['comment']['reference'] ) ) { continue; } $reference_file = $chunk_data['comment']['reference']; // Only combine "app" files (not scripts registered with WP). if ( false === strpos( $reference_file, 'dist/chunks/' ) && false === strpos( $reference_file, 'dist/app/index.js' ) ) { continue; } if ( empty( $combined_translation_data ) ) { // Use the first translation file as the base structure. $combined_translation_data = $chunk_data; } else { // Combine all messages from all chunk files. $combined_translation_data['locale_data']['messages'] = array_merge( $combined_translation_data['locale_data']['messages'], $chunk_data['locale_data']['messages'] ); } } // Remove inaccurate reference comment. unset( $combined_translation_data['comment'] ); return $combined_translation_data; } /** * Load translation strings from language packs for dynamic imports. * * This function combines JSON translation data auto-extracted by GlotPress * from Webpack-generated JS chunks into a single file that can be used in * subsequent requests. This is necessary since the JS chunks are not known * to WordPress via wp_register_script() and wp_set_script_translations(). * * @param string $original_translations JSON encoded translations object. * @param string $file File location for the script being translated. * @param string $handle Script handle. * @param string $domain Text domain. * * @return string JSON encoded translations object. */ public static function load_script_translations( $original_translations, $file, $handle, $domain ) { // Make sure the main app script is being loaded. if ( WC_ADMIN_APP !== $handle ) { return $original_translations; } // Make sure we're handing the correct domain (could be woocommerce or woocommerce-admin). $plugin_domain = explode( '/', plugin_basename( __FILE__ ) )[0]; if ( $plugin_domain !== $domain ) { return $original_translations; } $locale = determine_locale(); $cache_filename = self::get_combined_translation_filename( $domain, $locale ); $lang_dir = WP_LANG_DIR . '/plugins/'; // Allow us to easily interact with the filesystem. require_once ABSPATH . 'wp-admin/includes/file.php'; \WP_Filesystem(); global $wp_filesystem; // First attempt to get a previously generated combined translation file. if ( $wp_filesystem->is_file( $lang_dir . $cache_filename ) && $wp_filesystem->is_readable( $lang_dir . $cache_filename ) ) { return $wp_filesystem->get_contents( $lang_dir . $cache_filename ); } // Get all translation chunk data combined into a single object. $translations_from_chunks = self::get_translation_chunk_data( $lang_dir, $domain, $locale ); if ( empty( $translations_from_chunks ) ) { return $original_translations; } $chunk_translations_json = wp_json_encode( $translations_from_chunks ); // Cache combined translations strings to a file. $wp_filesystem->put_contents( $lang_dir . $cache_filename, $chunk_translations_json ); return $chunk_translations_json; } /** * 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; } // Grab translation strings from Webpack-generated chunks. add_filter( 'pre_load_script_translations', array( __CLASS__, 'load_script_translations' ), 10, 4 ); $features = self::get_features(); $enabled_features = array(); foreach ( $features as $key ) { $enabled_features[ $key ] = self::is_feature_enabled( $key ); } wp_add_inline_script( WC_ADMIN_APP, 'window.wcAdminFeatures = ' . wp_json_encode( $enabled_features ), 'before' ); 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 allowlist. * * 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 $allowlist Optional. List of allowed dependency handles. */ public static function maybe_output_preload_link_tag( $dependency, $type, $allowlist = array() ) { if ( ! empty( $allowlist ) && ! in_array( $dependency->handle, $allowlist, 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 allowlist. * * See: https://macarthur.me/posts/preloading-javascript-in-wordpress * * @param string $type Dependency type - 'script' or 'style'. * @param array $allowlist Optional. List of allowed dependency handles. */ public static function output_header_preload_tags_for_type( $type, $allowlist = 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->query( $dependency_handle, 'registered' ); if ( false === $dependency ) { continue; } // Preload the subdependencies first. foreach ( $dependency->deps as $sub_dependency_handle ) { $sub_dependency = $dependencies_of_type->query( $sub_dependency_handle, 'registered' ); if ( $sub_dependency ) { self::maybe_output_preload_link_tag( $sub_dependency, $type, $allowlist ); } } self::maybe_output_preload_link_tag( $dependency, $type, $allowlist ); } } /** * 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; } ?>
). * * @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 '