woocommerce/plugins/woocommerce-admin/src/Loader.php

1315 lines
43 KiB
PHP
Raw Normal View History

<?php
/**
* Register the scripts, styles, and includes needed for pieces of the WooCommerce Admin experience.
* NOTE: DO NOT edit this file in WooCommerce core, this is generated from woocommerce-admin.
*/
2019-08-12 18:11:28 +00:00
namespace Automattic\WooCommerce\Admin;
use \_WP_Dependency;
use Automattic\WooCommerce\Admin\API\Reports\Orders\DataStore as OrdersDataStore;
use Automattic\WooCommerce\Admin\API\Plugins;
use Automattic\WooCommerce\Admin\Features\Features;
use Automattic\WooCommerce\Admin\Features\Navigation\Screen;
use WC_Marketplace_Suggestions;
/**
* Loader Class.
*/
class Loader {
/**
* App entry point.
*/
const APP_ENTRY_POINT = 'wc-admin';
/**
* Class instance.
*
* @var Loader instance
*/
protected static $instance = null;
/**
* An array of classes to load from the includes folder.
*
* @var array
*/
protected static $classes = array();
/**
* WordPress capability required to use analytics features.
*
* @var string
*/
protected static $required_capability = null;
/**
* Get class instance.
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor.
* Hooks added here should be removed in `wc_admin_initialize` via the feature plugin.
*/
public function __construct() {
Features::get_instance();
add_action( 'init', array( __CLASS__, 'define_tables' ) );
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'register_scripts' ) );
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'inject_wc_settings_dependencies' ), 14 );
2019-08-12 18:11:28 +00:00
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'load_scripts' ), 15 );
// Old settings injection.
add_filter( 'woocommerce_components_settings', array( __CLASS__, 'add_component_settings' ) );
// New settings injection.
add_filter( 'woocommerce_shared_settings', array( __CLASS__, 'add_component_settings' ) );
2019-08-12 18:11:28 +00:00
add_filter( 'admin_body_class', array( __CLASS__, 'add_admin_body_classes' ) );
add_action( 'admin_menu', array( __CLASS__, 'register_page_handler' ) );
add_action( 'admin_menu', array( __CLASS__, 'register_store_details_page' ) );
2019-08-12 18:11:28 +00:00
add_filter( 'admin_title', array( __CLASS__, 'update_admin_title' ) );
add_action( 'rest_api_init', array( __CLASS__, 'register_user_data' ) );
add_action( 'in_admin_header', array( __CLASS__, 'embed_page_header' ) );
add_filter( 'woocommerce_settings_groups', array( __CLASS__, 'add_settings_group' ) );
add_filter( 'woocommerce_settings-wc_admin', array( __CLASS__, 'add_settings' ) );
add_action( 'admin_head', array( __CLASS__, 'remove_notices' ) );
Add WooCommerce Mobile App Banner Ad for Android and iOS (https://github.com/woocommerce/woocommerce-admin/pull/5037) Fixes woocommerce/woocommerce-admin#4654 The feature calls for a mobile app ad banner to be displayed to users on mobile devices. Based on the discussion in woocommerce/woocommerce-admin#4654 this implements the following: 1. [an iOS Smart App Banner](https://developer.apple.com/library/archive/documentation/AppleApplications/Reference/SafariWebContent/PromotingAppswithAppBanners/PromotingAppswithAppBanners.html). This banner is a meta tag that is generated in PHP. It will only display on iOS devices. One note about this: **This tag is not directly trackable like the implemented Android banner. If we would like to track its success then I believe [these instructions are relevant](https://stackoverflow.com/questions/12906502/is-it-possible-to-track-click-throughs-from-iphone-smart-banner/20422334woocommerce/woocommerce-admin#20422334)**. 2. A mobile app banner ad that **only displays on Android** and only displays at the `738px` breakpoint specified in the issue. To only display this banner ad on Android, we use basic checking of the user agent string. I weighed this up against other approaches and for this kind of niche use case a simple UA string check is (imho) still the best way to do this. 3. The banner ad makes use of user preferences to retain a per user setting that determines if that user has dismissed the Android banner. We don't/can't do anything like this for the iOS Smart App Banner (but in theory we shouldn't need to).
2020-08-27 01:46:53 +00:00
add_action( 'admin_head', array( __CLASS__, 'smart_app_banner' ) );
add_action( 'admin_notices', array( __CLASS__, 'inject_before_notices' ), -9999 );
2019-08-12 18:11:28 +00:00
add_action( 'admin_notices', array( __CLASS__, 'inject_after_notices' ), PHP_INT_MAX );
// Added this hook to delete the field woocommerce_onboarding_homepage_post_id when deleting the homepage.
add_action( 'trashed_post', array( __CLASS__, 'delete_homepage' ) );
// priority is 20 to run after https://github.com/woocommerce/woocommerce/blob/a55ae325306fc2179149ba9b97e66f32f84fdd9c/includes/admin/class-wc-admin-menus.php#L165.
2019-08-12 18:11:28 +00:00
add_action( 'admin_head', array( __CLASS__, 'remove_app_entry_page_menu_item' ), 20 );
2019-05-16 13:21:11 +00:00
/*
* Remove the emoji script as it always defaults to replacing emojis with Twemoji images.
* Gutenberg has also disabled emojis. More on that here -> https://github.com/WordPress/gutenberg/pull/6151
*/
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
// Combine JSON translation files (from chunks) when language packs are updated.
add_action( 'upgrader_process_complete', array( __CLASS__, 'combine_translation_chunk_files' ), 10, 2 );
// Handler for WooCommerce and WooCommerce Admin plugin activation.
add_action( 'woocommerce_activated_plugin', array( __CLASS__, 'activated_plugin' ) );
add_action( 'activated_plugin', array( __CLASS__, 'activated_plugin' ) );
}
/**
* Run when plugin is activated (can be WooCommerce or WooCommerce Admin).
*
* @param string $filename Activated plugin filename.
*/
public static function activated_plugin( $filename ) {
$plugin_domain = explode( '/', plugin_basename( __FILE__ ) )[0];
$activated_plugin_domain = explode( '/', $filename )[0];
// Ensure we're only running only on activation hook that originates from our plugin.
if ( $plugin_domain === $activated_plugin_domain ) {
self::generate_translation_strings();
}
}
/**
* 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;
}
}
/**
* Gets a build configured array of enabled WooCommerce Admin features/sections.
*
* @return array Enabled Woocommerce Admin features/sections.
*
* @deprecated since 1.9.0, use Features::get_features()
*/
public static function get_features() {
return Features::get_features();
}
/**
* Gets WordPress capability required to use analytics features.
*
* @return string
* @deprecated since 2.1.0, use value 'view_woocommerce_reports'
*/
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
* @deprecated since 2.1.0, use current_user_can( 'view_woocommerce_reports' )
*/
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.
*
* @deprecated since 1.9.0, use Features::exists( $feature )
*/
public static function is_feature_enabled( $feature ) {
return Features::exists( $feature );
}
/**
* 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 ) {
// minified files are only shipped in non-core versions of wc-admin, return false if minified files are not available.
if ( ! Features::exists( 'minified-js' ) ) {
return false;
}
// 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.
*
Use Route based code splitting to optimize bundle size (https://github.com/woocommerce/woocommerce-admin/pull/4094) * Use lazy loading to split up the size of the js downloaded * Use lazy loading to split up the size of the js downloaded * Add Moment Timezone plugin to reduce size of data file. * Lazy load header panels and use Dashicons for faster loading * Load assets from the correct publicPath * Load assets from the correct publicPath * PHP cs fixes * Fix missing quotes on string literal. * Fix PropType warning for lazy loaded component. * Separate the task list and dashboard chunks. * Lazy load dashboard sections. * Restore original icons and reduce size by importing only the icons needed * Lazy load alerts to save more Kb on initial load * Minify built JS in production mode. * Add preload tags for WC Admin assets. (https://github.com/woocommerce/woocommerce-admin/pull/4162) * Fix linting errors. * Add modified UnminifiedWebpackPlugin. * Produce minified and unminified bundles for all builds. * Remove unused variable from webpack config. * Run unminify after sourcemap generation. * Only hook after optimization if we're using a devtool. * Add minification suffix in Loader::get_url(). * Lazy load OBW on new home screen. * Move OBW style up a level to layout. * Hydrate ProfileWizard independently of withSelect and withDispatch * Fix order of composition and fallback function when using hydration. Co-authored-by: Jeff Stieler <jeff.m.stieler@gmail.com> Co-authored-by: Paul Sealock <psealock@gmail.com>
2020-04-29 18:01:27 +00:00
* @param string $file File name (without extension).
* @param string $ext File extension.
* @return string URL to asset.
*/
Use Route based code splitting to optimize bundle size (https://github.com/woocommerce/woocommerce-admin/pull/4094) * Use lazy loading to split up the size of the js downloaded * Use lazy loading to split up the size of the js downloaded * Add Moment Timezone plugin to reduce size of data file. * Lazy load header panels and use Dashicons for faster loading * Load assets from the correct publicPath * Load assets from the correct publicPath * PHP cs fixes * Fix missing quotes on string literal. * Fix PropType warning for lazy loaded component. * Separate the task list and dashboard chunks. * Lazy load dashboard sections. * Restore original icons and reduce size by importing only the icons needed * Lazy load alerts to save more Kb on initial load * Minify built JS in production mode. * Add preload tags for WC Admin assets. (https://github.com/woocommerce/woocommerce-admin/pull/4162) * Fix linting errors. * Add modified UnminifiedWebpackPlugin. * Produce minified and unminified bundles for all builds. * Remove unused variable from webpack config. * Run unminify after sourcemap generation. * Only hook after optimization if we're using a devtool. * Add minification suffix in Loader::get_url(). * Lazy load OBW on new home screen. * Move OBW style up a level to layout. * Hydrate ProfileWizard independently of withSelect and withDispatch * Fix order of composition and fallback function when using hydration. Co-authored-by: Jeff Stieler <jeff.m.stieler@gmail.com> Co-authored-by: Paul Sealock <psealock@gmail.com>
2020-04-29 18:01:27 +00:00
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' : '';
Use Route based code splitting to optimize bundle size (https://github.com/woocommerce/woocommerce-admin/pull/4094) * Use lazy loading to split up the size of the js downloaded * Use lazy loading to split up the size of the js downloaded * Add Moment Timezone plugin to reduce size of data file. * Lazy load header panels and use Dashicons for faster loading * Load assets from the correct publicPath * Load assets from the correct publicPath * PHP cs fixes * Fix missing quotes on string literal. * Fix PropType warning for lazy loaded component. * Separate the task list and dashboard chunks. * Lazy load dashboard sections. * Restore original icons and reduce size by importing only the icons needed * Lazy load alerts to save more Kb on initial load * Minify built JS in production mode. * Add preload tags for WC Admin assets. (https://github.com/woocommerce/woocommerce-admin/pull/4162) * Fix linting errors. * Add modified UnminifiedWebpackPlugin. * Produce minified and unminified bundles for all builds. * Remove unused variable from webpack config. * Run unminify after sourcemap generation. * Only hook after optimization if we're using a devtool. * Add minification suffix in Loader::get_url(). * Lazy load OBW on new home screen. * Move OBW style up a level to layout. * Hydrate ProfileWizard independently of withSelect and withDispatch * Fix order of composition and fallback function when using hydration. Co-authored-by: Jeff Stieler <jeff.m.stieler@gmail.com> Co-authored-by: Paul Sealock <psealock@gmail.com>
2020-04-29 18:01:27 +00:00
}
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.
*
Use Route based code splitting to optimize bundle size (https://github.com/woocommerce/woocommerce-admin/pull/4094) * Use lazy loading to split up the size of the js downloaded * Use lazy loading to split up the size of the js downloaded * Add Moment Timezone plugin to reduce size of data file. * Lazy load header panels and use Dashicons for faster loading * Load assets from the correct publicPath * Load assets from the correct publicPath * PHP cs fixes * Fix missing quotes on string literal. * Fix PropType warning for lazy loaded component. * Separate the task list and dashboard chunks. * Lazy load dashboard sections. * Restore original icons and reduce size by importing only the icons needed * Lazy load alerts to save more Kb on initial load * Minify built JS in production mode. * Add preload tags for WC Admin assets. (https://github.com/woocommerce/woocommerce-admin/pull/4162) * Fix linting errors. * Add modified UnminifiedWebpackPlugin. * Produce minified and unminified bundles for all builds. * Remove unused variable from webpack config. * Run unminify after sourcemap generation. * Only hook after optimization if we're using a devtool. * Add minification suffix in Loader::get_url(). * Lazy load OBW on new home screen. * Move OBW style up a level to layout. * Hydrate ProfileWizard independently of withSelect and withDispatch * Fix order of composition and fallback function when using hydration. Co-authored-by: Jeff Stieler <jeff.m.stieler@gmail.com> Co-authored-by: Paul Sealock <psealock@gmail.com>
2020-04-29 18:01:27 +00:00
* @param string $ext File extension.
* @return string The cache buster value to use for the given file.
*/
Use Route based code splitting to optimize bundle size (https://github.com/woocommerce/woocommerce-admin/pull/4094) * Use lazy loading to split up the size of the js downloaded * Use lazy loading to split up the size of the js downloaded * Add Moment Timezone plugin to reduce size of data file. * Lazy load header panels and use Dashicons for faster loading * Load assets from the correct publicPath * Load assets from the correct publicPath * PHP cs fixes * Fix missing quotes on string literal. * Fix PropType warning for lazy loaded component. * Separate the task list and dashboard chunks. * Lazy load dashboard sections. * Restore original icons and reduce size by importing only the icons needed * Lazy load alerts to save more Kb on initial load * Minify built JS in production mode. * Add preload tags for WC Admin assets. (https://github.com/woocommerce/woocommerce-admin/pull/4162) * Fix linting errors. * Add modified UnminifiedWebpackPlugin. * Produce minified and unminified bundles for all builds. * Remove unused variable from webpack config. * Run unminify after sourcemap generation. * Only hook after optimization if we're using a devtool. * Add minification suffix in Loader::get_url(). * Lazy load OBW on new home screen. * Move OBW style up a level to layout. * Hydrate ProfileWizard independently of withSelect and withDispatch * Fix order of composition and fallback function when using hydration. Co-authored-by: Jeff Stieler <jeff.m.stieler@gmail.com> Co-authored-by: Paul Sealock <psealock@gmail.com>
2020-04-29 18:01:27 +00:00
public static function get_file_version( $ext ) {
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
Use Route based code splitting to optimize bundle size (https://github.com/woocommerce/woocommerce-admin/pull/4094) * Use lazy loading to split up the size of the js downloaded * Use lazy loading to split up the size of the js downloaded * Add Moment Timezone plugin to reduce size of data file. * Lazy load header panels and use Dashicons for faster loading * Load assets from the correct publicPath * Load assets from the correct publicPath * PHP cs fixes * Fix missing quotes on string literal. * Fix PropType warning for lazy loaded component. * Separate the task list and dashboard chunks. * Lazy load dashboard sections. * Restore original icons and reduce size by importing only the icons needed * Lazy load alerts to save more Kb on initial load * Minify built JS in production mode. * Add preload tags for WC Admin assets. (https://github.com/woocommerce/woocommerce-admin/pull/4162) * Fix linting errors. * Add modified UnminifiedWebpackPlugin. * Produce minified and unminified bundles for all builds. * Remove unused variable from webpack config. * Run unminify after sourcemap generation. * Only hook after optimization if we're using a devtool. * Add minification suffix in Loader::get_url(). * Lazy load OBW on new home screen. * Move OBW style up a level to layout. * Hydrate ProfileWizard independently of withSelect and withDispatch * Fix order of composition and fallback function when using hydration. Co-authored-by: Jeff Stieler <jeff.m.stieler@gmail.com> Co-authored-by: Paul Sealock <psealock@gmail.com>
2020-04-29 18:01:27 +00:00
return filemtime( WC_ADMIN_ABSPATH . self::get_path( $ext ) );
}
return WC_ADMIN_VERSION_NUMBER;
}
/**
* Gets the path for the asset depending on file type.
*
Use Route based code splitting to optimize bundle size (https://github.com/woocommerce/woocommerce-admin/pull/4094) * Use lazy loading to split up the size of the js downloaded * Use lazy loading to split up the size of the js downloaded * Add Moment Timezone plugin to reduce size of data file. * Lazy load header panels and use Dashicons for faster loading * Load assets from the correct publicPath * Load assets from the correct publicPath * PHP cs fixes * Fix missing quotes on string literal. * Fix PropType warning for lazy loaded component. * Separate the task list and dashboard chunks. * Lazy load dashboard sections. * Restore original icons and reduce size by importing only the icons needed * Lazy load alerts to save more Kb on initial load * Minify built JS in production mode. * Add preload tags for WC Admin assets. (https://github.com/woocommerce/woocommerce-admin/pull/4162) * Fix linting errors. * Add modified UnminifiedWebpackPlugin. * Produce minified and unminified bundles for all builds. * Remove unused variable from webpack config. * Run unminify after sourcemap generation. * Only hook after optimization if we're using a devtool. * Add minification suffix in Loader::get_url(). * Lazy load OBW on new home screen. * Move OBW style up a level to layout. * Hydrate ProfileWizard independently of withSelect and withDispatch * Fix order of composition and fallback function when using hydration. Co-authored-by: Jeff Stieler <jeff.m.stieler@gmail.com> Co-authored-by: Paul Sealock <psealock@gmail.com>
2020-04-29 18:01:27 +00:00
* @param string $ext File extension.
* @return string Folder path of asset.
*/
Use Route based code splitting to optimize bundle size (https://github.com/woocommerce/woocommerce-admin/pull/4094) * Use lazy loading to split up the size of the js downloaded * Use lazy loading to split up the size of the js downloaded * Add Moment Timezone plugin to reduce size of data file. * Lazy load header panels and use Dashicons for faster loading * Load assets from the correct publicPath * Load assets from the correct publicPath * PHP cs fixes * Fix missing quotes on string literal. * Fix PropType warning for lazy loaded component. * Separate the task list and dashboard chunks. * Lazy load dashboard sections. * Restore original icons and reduce size by importing only the icons needed * Lazy load alerts to save more Kb on initial load * Minify built JS in production mode. * Add preload tags for WC Admin assets. (https://github.com/woocommerce/woocommerce-admin/pull/4162) * Fix linting errors. * Add modified UnminifiedWebpackPlugin. * Produce minified and unminified bundles for all builds. * Remove unused variable from webpack config. * Run unminify after sourcemap generation. * Only hook after optimization if we're using a devtool. * Add minification suffix in Loader::get_url(). * Lazy load OBW on new home screen. * Move OBW style up a level to layout. * Hydrate ProfileWizard independently of withSelect and withDispatch * Fix order of composition and fallback function when using hydration. Co-authored-by: Jeff Stieler <jeff.m.stieler@gmail.com> Co-authored-by: Paul Sealock <psealock@gmail.com>
2020-04-29 18:01:27 +00:00
private static function get_path( $ext ) {
return ( 'css' === $ext ) ? WC_ADMIN_DIST_CSS_FOLDER : WC_ADMIN_DIST_JS_FOLDER;
}
/**
* Connects existing WooCommerce pages.
*
* @todo The entry point for the embed needs moved to this class as well.
*/
public static function register_page_handler() {
2019-08-12 18:22:32 +00:00
require_once WC_ADMIN_ABSPATH . 'includes/connect-existing-pages.php';
}
Added skip profiler functionality (https://github.com/woocommerce/woocommerce-admin/pull/4721) * Added skip profiler functionality This commit adds the skip profiler functionality, leaving the Home screen `Store details` as incomplete. # Conflicts: # client/task-list/tasks.js * Unit tests repaiared * Added OBW page This commit makes the necessary changes to add an OBW page # Conflicts: # client/profile-wizard/index.js * Event name changed This commit renames the event name we were recording since it was wrong. * Redirection modified This commit modifies the way we do redirection # Conflicts: # client/profile-wizard/index.js * Removed `profiler` feature flag * Removed `profiler` feature flag This commit removes the `profiler` feature flag * Added error handling for `updateProfileItems` This commit adds error handling for the method `updateProfileItems` * Update client/profile-wizard/index.js Co-authored-by: Matt Sherman <matt.sherman@automattic.com> * Removed "throw" from "then" * Update client/dashboard/index.js Co-authored-by: Joshua T Flowers <joshuatf@gmail.com> * Update client/profile-wizard/index.js Co-authored-by: Joshua T Flowers <joshuatf@gmail.com> * Modified redirection URL This commit modifies the redirection URL. # Conflicts: # client/task-list/tasks.js # Conflicts: # client/profile-wizard/index.js * Removed the variable "lastStep" and the related logic. * Removed change of state for variable "skipped" * Added error trowing to onboarding actions This commit adds error trowing to onboarding actions * Removed response handling for `updateProfileItems` * Added default value for "profileItems" in Homescreen and Dashboard * Removed `step` from OnboardingProfile and test This commit removes the `step` from OnboardingProfile.php and from tests Co-authored-by: Fernando Marichal <contacto@fernandomarichal.com> Co-authored-by: Matt Sherman <matt.sherman@automattic.com> Co-authored-by: Joshua T Flowers <joshuatf@gmail.com>
2020-07-16 15:17:10 +00:00
/**
* Registers the store details (profiler) page.
Added skip profiler functionality (https://github.com/woocommerce/woocommerce-admin/pull/4721) * Added skip profiler functionality This commit adds the skip profiler functionality, leaving the Home screen `Store details` as incomplete. # Conflicts: # client/task-list/tasks.js * Unit tests repaiared * Added OBW page This commit makes the necessary changes to add an OBW page # Conflicts: # client/profile-wizard/index.js * Event name changed This commit renames the event name we were recording since it was wrong. * Redirection modified This commit modifies the way we do redirection # Conflicts: # client/profile-wizard/index.js * Removed `profiler` feature flag * Removed `profiler` feature flag This commit removes the `profiler` feature flag * Added error handling for `updateProfileItems` This commit adds error handling for the method `updateProfileItems` * Update client/profile-wizard/index.js Co-authored-by: Matt Sherman <matt.sherman@automattic.com> * Removed "throw" from "then" * Update client/dashboard/index.js Co-authored-by: Joshua T Flowers <joshuatf@gmail.com> * Update client/profile-wizard/index.js Co-authored-by: Joshua T Flowers <joshuatf@gmail.com> * Modified redirection URL This commit modifies the redirection URL. # Conflicts: # client/task-list/tasks.js # Conflicts: # client/profile-wizard/index.js * Removed the variable "lastStep" and the related logic. * Removed change of state for variable "skipped" * Added error trowing to onboarding actions This commit adds error trowing to onboarding actions * Removed response handling for `updateProfileItems` * Added default value for "profileItems" in Homescreen and Dashboard * Removed `step` from OnboardingProfile and test This commit removes the `step` from OnboardingProfile.php and from tests Co-authored-by: Fernando Marichal <contacto@fernandomarichal.com> Co-authored-by: Matt Sherman <matt.sherman@automattic.com> Co-authored-by: Joshua T Flowers <joshuatf@gmail.com>
2020-07-16 15:17:10 +00:00
*/
public static function register_store_details_page() {
Added skip profiler functionality (https://github.com/woocommerce/woocommerce-admin/pull/4721) * Added skip profiler functionality This commit adds the skip profiler functionality, leaving the Home screen `Store details` as incomplete. # Conflicts: # client/task-list/tasks.js * Unit tests repaiared * Added OBW page This commit makes the necessary changes to add an OBW page # Conflicts: # client/profile-wizard/index.js * Event name changed This commit renames the event name we were recording since it was wrong. * Redirection modified This commit modifies the way we do redirection # Conflicts: # client/profile-wizard/index.js * Removed `profiler` feature flag * Removed `profiler` feature flag This commit removes the `profiler` feature flag * Added error handling for `updateProfileItems` This commit adds error handling for the method `updateProfileItems` * Update client/profile-wizard/index.js Co-authored-by: Matt Sherman <matt.sherman@automattic.com> * Removed "throw" from "then" * Update client/dashboard/index.js Co-authored-by: Joshua T Flowers <joshuatf@gmail.com> * Update client/profile-wizard/index.js Co-authored-by: Joshua T Flowers <joshuatf@gmail.com> * Modified redirection URL This commit modifies the redirection URL. # Conflicts: # client/task-list/tasks.js # Conflicts: # client/profile-wizard/index.js * Removed the variable "lastStep" and the related logic. * Removed change of state for variable "skipped" * Added error trowing to onboarding actions This commit adds error trowing to onboarding actions * Removed response handling for `updateProfileItems` * Added default value for "profileItems" in Homescreen and Dashboard * Removed `step` from OnboardingProfile and test This commit removes the `step` from OnboardingProfile.php and from tests Co-authored-by: Fernando Marichal <contacto@fernandomarichal.com> Co-authored-by: Matt Sherman <matt.sherman@automattic.com> Co-authored-by: Joshua T Flowers <joshuatf@gmail.com>
2020-07-16 15:17:10 +00:00
wc_admin_register_page(
array(
'title' => __( 'Setup Wizard', 'woocommerce-admin' ),
'parent' => '',
'path' => '/setup-wizard',
Added skip profiler functionality (https://github.com/woocommerce/woocommerce-admin/pull/4721) * Added skip profiler functionality This commit adds the skip profiler functionality, leaving the Home screen `Store details` as incomplete. # Conflicts: # client/task-list/tasks.js * Unit tests repaiared * Added OBW page This commit makes the necessary changes to add an OBW page # Conflicts: # client/profile-wizard/index.js * Event name changed This commit renames the event name we were recording since it was wrong. * Redirection modified This commit modifies the way we do redirection # Conflicts: # client/profile-wizard/index.js * Removed `profiler` feature flag * Removed `profiler` feature flag This commit removes the `profiler` feature flag * Added error handling for `updateProfileItems` This commit adds error handling for the method `updateProfileItems` * Update client/profile-wizard/index.js Co-authored-by: Matt Sherman <matt.sherman@automattic.com> * Removed "throw" from "then" * Update client/dashboard/index.js Co-authored-by: Joshua T Flowers <joshuatf@gmail.com> * Update client/profile-wizard/index.js Co-authored-by: Joshua T Flowers <joshuatf@gmail.com> * Modified redirection URL This commit modifies the redirection URL. # Conflicts: # client/task-list/tasks.js # Conflicts: # client/profile-wizard/index.js * Removed the variable "lastStep" and the related logic. * Removed change of state for variable "skipped" * Added error trowing to onboarding actions This commit adds error trowing to onboarding actions * Removed response handling for `updateProfileItems` * Added default value for "profileItems" in Homescreen and Dashboard * Removed `step` from OnboardingProfile and test This commit removes the `step` from OnboardingProfile.php and from tests Co-authored-by: Fernando Marichal <contacto@fernandomarichal.com> Co-authored-by: Matt Sherman <matt.sherman@automattic.com> Co-authored-by: Joshua T Flowers <joshuatf@gmail.com>
2020-07-16 15:17:10 +00:00
)
);
}
/**
* 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;
}
Use Route based code splitting to optimize bundle size (https://github.com/woocommerce/woocommerce-admin/pull/4094) * Use lazy loading to split up the size of the js downloaded * Use lazy loading to split up the size of the js downloaded * Add Moment Timezone plugin to reduce size of data file. * Lazy load header panels and use Dashicons for faster loading * Load assets from the correct publicPath * Load assets from the correct publicPath * PHP cs fixes * Fix missing quotes on string literal. * Fix PropType warning for lazy loaded component. * Separate the task list and dashboard chunks. * Lazy load dashboard sections. * Restore original icons and reduce size by importing only the icons needed * Lazy load alerts to save more Kb on initial load * Minify built JS in production mode. * Add preload tags for WC Admin assets. (https://github.com/woocommerce/woocommerce-admin/pull/4162) * Fix linting errors. * Add modified UnminifiedWebpackPlugin. * Produce minified and unminified bundles for all builds. * Remove unused variable from webpack config. * Run unminify after sourcemap generation. * Only hook after optimization if we're using a devtool. * Add minification suffix in Loader::get_url(). * Lazy load OBW on new home screen. * Move OBW style up a level to layout. * Hydrate ProfileWizard independently of withSelect and withDispatch * Fix order of composition and fallback function when using hydration. Co-authored-by: Jeff Stieler <jeff.m.stieler@gmail.com> Co-authored-by: Paul Sealock <psealock@gmail.com>
2020-04-29 18:01:27 +00:00
$js_file_version = self::get_file_version( 'js' );
$css_file_version = self::get_file_version( 'css' );
$scripts = array(
'wc-customer-effort-score',
// NOTE: This should be removed when Gutenberg is updated and the notices package is removed from WooCommerce Admin.
'wc-notices',
'wc-number',
'wc-tracks',
'wc-date',
'wc-components',
WC_ADMIN_APP,
'wc-csv',
'wc-store-data',
'wc-currency',
'wc-navigation',
);
$scripts_map = array(
WC_ADMIN_APP => 'app',
'wc-csv' => 'csv-export',
'wc-store-data' => 'data',
);
$translated_scripts = array(
'wc-currency',
'wc-date',
'wc-components',
WC_ADMIN_APP,
);
foreach ( $scripts as $script ) {
$script_path_name = isset( $scripts_map[ $script ] ) ? $scripts_map[ $script ] : str_replace( 'wc-', '', $script );
$script_assets = require WC_ADMIN_ABSPATH . WC_ADMIN_DIST_JS_FOLDER . $script_path_name . '/index.min.asset.php';
wp_register_script(
$script,
self::get_url( $script_path_name . '/index', 'js' ),
$script_assets ['dependencies'],
$js_file_version,
true
);
if ( in_array( $script, $translated_scripts, true ) ) {
wp_set_script_translations( $script, 'woocommerce-admin' );
}
}
wp_register_style(
'wc-components',
Use Route based code splitting to optimize bundle size (https://github.com/woocommerce/woocommerce-admin/pull/4094) * Use lazy loading to split up the size of the js downloaded * Use lazy loading to split up the size of the js downloaded * Add Moment Timezone plugin to reduce size of data file. * Lazy load header panels and use Dashicons for faster loading * Load assets from the correct publicPath * Load assets from the correct publicPath * PHP cs fixes * Fix missing quotes on string literal. * Fix PropType warning for lazy loaded component. * Separate the task list and dashboard chunks. * Lazy load dashboard sections. * Restore original icons and reduce size by importing only the icons needed * Lazy load alerts to save more Kb on initial load * Minify built JS in production mode. * Add preload tags for WC Admin assets. (https://github.com/woocommerce/woocommerce-admin/pull/4162) * Fix linting errors. * Add modified UnminifiedWebpackPlugin. * Produce minified and unminified bundles for all builds. * Remove unused variable from webpack config. * Run unminify after sourcemap generation. * Only hook after optimization if we're using a devtool. * Add minification suffix in Loader::get_url(). * Lazy load OBW on new home screen. * Move OBW style up a level to layout. * Hydrate ProfileWizard independently of withSelect and withDispatch * Fix order of composition and fallback function when using hydration. Co-authored-by: Jeff Stieler <jeff.m.stieler@gmail.com> Co-authored-by: Paul Sealock <psealock@gmail.com>
2020-04-29 18:01:27 +00:00
self::get_url( 'components/style', 'css' ),
array(),
Use Route based code splitting to optimize bundle size (https://github.com/woocommerce/woocommerce-admin/pull/4094) * Use lazy loading to split up the size of the js downloaded * Use lazy loading to split up the size of the js downloaded * Add Moment Timezone plugin to reduce size of data file. * Lazy load header panels and use Dashicons for faster loading * Load assets from the correct publicPath * Load assets from the correct publicPath * PHP cs fixes * Fix missing quotes on string literal. * Fix PropType warning for lazy loaded component. * Separate the task list and dashboard chunks. * Lazy load dashboard sections. * Restore original icons and reduce size by importing only the icons needed * Lazy load alerts to save more Kb on initial load * Minify built JS in production mode. * Add preload tags for WC Admin assets. (https://github.com/woocommerce/woocommerce-admin/pull/4162) * Fix linting errors. * Add modified UnminifiedWebpackPlugin. * Produce minified and unminified bundles for all builds. * Remove unused variable from webpack config. * Run unminify after sourcemap generation. * Only hook after optimization if we're using a devtool. * Add minification suffix in Loader::get_url(). * Lazy load OBW on new home screen. * Move OBW style up a level to layout. * Hydrate ProfileWizard independently of withSelect and withDispatch * Fix order of composition and fallback function when using hydration. Co-authored-by: Jeff Stieler <jeff.m.stieler@gmail.com> Co-authored-by: Paul Sealock <psealock@gmail.com>
2020-04-29 18:01:27 +00:00
$css_file_version
);
wp_style_add_data( 'wc-components', 'rtl', 'replace' );
wp_register_style(
'wc-components-ie',
Use Route based code splitting to optimize bundle size (https://github.com/woocommerce/woocommerce-admin/pull/4094) * Use lazy loading to split up the size of the js downloaded * Use lazy loading to split up the size of the js downloaded * Add Moment Timezone plugin to reduce size of data file. * Lazy load header panels and use Dashicons for faster loading * Load assets from the correct publicPath * Load assets from the correct publicPath * PHP cs fixes * Fix missing quotes on string literal. * Fix PropType warning for lazy loaded component. * Separate the task list and dashboard chunks. * Lazy load dashboard sections. * Restore original icons and reduce size by importing only the icons needed * Lazy load alerts to save more Kb on initial load * Minify built JS in production mode. * Add preload tags for WC Admin assets. (https://github.com/woocommerce/woocommerce-admin/pull/4162) * Fix linting errors. * Add modified UnminifiedWebpackPlugin. * Produce minified and unminified bundles for all builds. * Remove unused variable from webpack config. * Run unminify after sourcemap generation. * Only hook after optimization if we're using a devtool. * Add minification suffix in Loader::get_url(). * Lazy load OBW on new home screen. * Move OBW style up a level to layout. * Hydrate ProfileWizard independently of withSelect and withDispatch * Fix order of composition and fallback function when using hydration. Co-authored-by: Jeff Stieler <jeff.m.stieler@gmail.com> Co-authored-by: Paul Sealock <psealock@gmail.com>
2020-04-29 18:01:27 +00:00
self::get_url( 'components/ie', 'css' ),
array(),
Use Route based code splitting to optimize bundle size (https://github.com/woocommerce/woocommerce-admin/pull/4094) * Use lazy loading to split up the size of the js downloaded * Use lazy loading to split up the size of the js downloaded * Add Moment Timezone plugin to reduce size of data file. * Lazy load header panels and use Dashicons for faster loading * Load assets from the correct publicPath * Load assets from the correct publicPath * PHP cs fixes * Fix missing quotes on string literal. * Fix PropType warning for lazy loaded component. * Separate the task list and dashboard chunks. * Lazy load dashboard sections. * Restore original icons and reduce size by importing only the icons needed * Lazy load alerts to save more Kb on initial load * Minify built JS in production mode. * Add preload tags for WC Admin assets. (https://github.com/woocommerce/woocommerce-admin/pull/4162) * Fix linting errors. * Add modified UnminifiedWebpackPlugin. * Produce minified and unminified bundles for all builds. * Remove unused variable from webpack config. * Run unminify after sourcemap generation. * Only hook after optimization if we're using a devtool. * Add minification suffix in Loader::get_url(). * Lazy load OBW on new home screen. * Move OBW style up a level to layout. * Hydrate ProfileWizard independently of withSelect and withDispatch * Fix order of composition and fallback function when using hydration. Co-authored-by: Jeff Stieler <jeff.m.stieler@gmail.com> Co-authored-by: Paul Sealock <psealock@gmail.com>
2020-04-29 18:01:27 +00:00
$css_file_version
);
wp_style_add_data( 'wc-components-ie', 'rtl', 'replace' );
wp_register_style(
'wc-customer-effort-score',
self::get_url( 'customer-effort-score/style', 'css' ),
array(),
$css_file_version
);
wp_style_add_data( 'wc-customer-effort-score', 'rtl', 'replace' );
Use Route based code splitting to optimize bundle size (https://github.com/woocommerce/woocommerce-admin/pull/4094) * Use lazy loading to split up the size of the js downloaded * Use lazy loading to split up the size of the js downloaded * Add Moment Timezone plugin to reduce size of data file. * Lazy load header panels and use Dashicons for faster loading * Load assets from the correct publicPath * Load assets from the correct publicPath * PHP cs fixes * Fix missing quotes on string literal. * Fix PropType warning for lazy loaded component. * Separate the task list and dashboard chunks. * Lazy load dashboard sections. * Restore original icons and reduce size by importing only the icons needed * Lazy load alerts to save more Kb on initial load * Minify built JS in production mode. * Add preload tags for WC Admin assets. (https://github.com/woocommerce/woocommerce-admin/pull/4162) * Fix linting errors. * Add modified UnminifiedWebpackPlugin. * Produce minified and unminified bundles for all builds. * Remove unused variable from webpack config. * Run unminify after sourcemap generation. * Only hook after optimization if we're using a devtool. * Add minification suffix in Loader::get_url(). * Lazy load OBW on new home screen. * Move OBW style up a level to layout. * Hydrate ProfileWizard independently of withSelect and withDispatch * Fix order of composition and fallback function when using hydration. Co-authored-by: Jeff Stieler <jeff.m.stieler@gmail.com> Co-authored-by: Paul Sealock <psealock@gmail.com>
2020-04-29 18:01:27 +00:00
wp_localize_script(
WC_ADMIN_APP,
'wcAdminAssets',
array(
'path' => plugins_url( self::get_path( 'js' ), WC_ADMIN_PLUGIN_FILE ),
'version' => $js_file_version,
Use Route based code splitting to optimize bundle size (https://github.com/woocommerce/woocommerce-admin/pull/4094) * Use lazy loading to split up the size of the js downloaded * Use lazy loading to split up the size of the js downloaded * Add Moment Timezone plugin to reduce size of data file. * Lazy load header panels and use Dashicons for faster loading * Load assets from the correct publicPath * Load assets from the correct publicPath * PHP cs fixes * Fix missing quotes on string literal. * Fix PropType warning for lazy loaded component. * Separate the task list and dashboard chunks. * Lazy load dashboard sections. * Restore original icons and reduce size by importing only the icons needed * Lazy load alerts to save more Kb on initial load * Minify built JS in production mode. * Add preload tags for WC Admin assets. (https://github.com/woocommerce/woocommerce-admin/pull/4162) * Fix linting errors. * Add modified UnminifiedWebpackPlugin. * Produce minified and unminified bundles for all builds. * Remove unused variable from webpack config. * Run unminify after sourcemap generation. * Only hook after optimization if we're using a devtool. * Add minification suffix in Loader::get_url(). * Lazy load OBW on new home screen. * Move OBW style up a level to layout. * Hydrate ProfileWizard independently of withSelect and withDispatch * Fix order of composition and fallback function when using hydration. Co-authored-by: Jeff Stieler <jeff.m.stieler@gmail.com> Co-authored-by: Paul Sealock <psealock@gmail.com>
2020-04-29 18:01:27 +00:00
)
);
// 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', 'wc-customer-effort-score', 'wp-components' ),
Use Route based code splitting to optimize bundle size (https://github.com/woocommerce/woocommerce-admin/pull/4094) * Use lazy loading to split up the size of the js downloaded * Use lazy loading to split up the size of the js downloaded * Add Moment Timezone plugin to reduce size of data file. * Lazy load header panels and use Dashicons for faster loading * Load assets from the correct publicPath * Load assets from the correct publicPath * PHP cs fixes * Fix missing quotes on string literal. * Fix PropType warning for lazy loaded component. * Separate the task list and dashboard chunks. * Lazy load dashboard sections. * Restore original icons and reduce size by importing only the icons needed * Lazy load alerts to save more Kb on initial load * Minify built JS in production mode. * Add preload tags for WC Admin assets. (https://github.com/woocommerce/woocommerce-admin/pull/4162) * Fix linting errors. * Add modified UnminifiedWebpackPlugin. * Produce minified and unminified bundles for all builds. * Remove unused variable from webpack config. * Run unminify after sourcemap generation. * Only hook after optimization if we're using a devtool. * Add minification suffix in Loader::get_url(). * Lazy load OBW on new home screen. * Move OBW style up a level to layout. * Hydrate ProfileWizard independently of withSelect and withDispatch * Fix order of composition and fallback function when using hydration. Co-authored-by: Jeff Stieler <jeff.m.stieler@gmail.com> Co-authored-by: Paul Sealock <psealock@gmail.com>
2020-04-29 18:01:27 +00:00
$css_file_version
);
wp_register_style(
'wc-admin-ie',
self::get_url( "ie/style{$rtl}", 'css' ),
array( WC_ADMIN_APP ),
Use Route based code splitting to optimize bundle size (https://github.com/woocommerce/woocommerce-admin/pull/4094) * Use lazy loading to split up the size of the js downloaded * Use lazy loading to split up the size of the js downloaded * Add Moment Timezone plugin to reduce size of data file. * Lazy load header panels and use Dashicons for faster loading * Load assets from the correct publicPath * Load assets from the correct publicPath * PHP cs fixes * Fix missing quotes on string literal. * Fix PropType warning for lazy loaded component. * Separate the task list and dashboard chunks. * Lazy load dashboard sections. * Restore original icons and reduce size by importing only the icons needed * Lazy load alerts to save more Kb on initial load * Minify built JS in production mode. * Add preload tags for WC Admin assets. (https://github.com/woocommerce/woocommerce-admin/pull/4162) * Fix linting errors. * Add modified UnminifiedWebpackPlugin. * Produce minified and unminified bundles for all builds. * Remove unused variable from webpack config. * Run unminify after sourcemap generation. * Only hook after optimization if we're using a devtool. * Add minification suffix in Loader::get_url(). * Lazy load OBW on new home screen. * Move OBW style up a level to layout. * Hydrate ProfileWizard independently of withSelect and withDispatch * Fix order of composition and fallback function when using hydration. Co-authored-by: Jeff Stieler <jeff.m.stieler@gmail.com> Co-authored-by: Paul Sealock <psealock@gmail.com>
2020-04-29 18:01:27 +00:00
$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 ) {
$filename = implode( '-', array( $domain, $locale, WC_ADMIN_APP ) ) . '.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 ) {
// So long as this function is called during the 'upgrader_process_complete' action,
// the filesystem object should be hooked up.
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();
if ( false === $json_i18n_filenames ) {
return $combined_translation_data;
}
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;
}
/**
* Combine translation chunks when plugin is activated.
*
* This function combines JSON translation data auto-extracted by GlotPress
* from Webpack-generated JS chunks into a single file. This is necessary
* since the JS chunks are not known to WordPress via wp_register_script()
* and wp_set_script_translations().
*/
public static function generate_translation_strings() {
$plugin_domain = explode( '/', plugin_basename( __FILE__ ) )[0];
$locale = determine_locale();
$lang_dir = WP_LANG_DIR . '/plugins/';
// Bail early if not localized.
if ( 'en_US' === $locale ) {
return;
}
if ( ! function_exists( 'get_filesystem_method' ) ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
$access_type = get_filesystem_method();
if ( 'direct' === $access_type ) {
\WP_Filesystem();
self::build_and_save_translations( $lang_dir, $plugin_domain, $locale );
} else {
// I'm reluctant to add support for other filesystems here as it would require
// user's input on activating plugin - which I don't think is common.
return;
}
}
/**
* Combine and save translations for a specific locale.
*
* Note that this assumes \WP_Filesystem is already initialized with write access.
*
* @param string $language_dir Path to language files.
* @param string $plugin_domain Text domain.
* @param string $locale Locale being retrieved.
*/
public static function build_and_save_translations( $language_dir, $plugin_domain, $locale ) {
global $wp_filesystem;
$translations_from_chunks = self::get_translation_chunk_data( $language_dir, $plugin_domain, $locale );
if ( empty( $translations_from_chunks ) ) {
return;
}
$cache_filename = self::get_combined_translation_filename( $plugin_domain, $locale );
$chunk_translations_json = wp_json_encode( $translations_from_chunks );
// Cache combined translations strings to a file.
$wp_filesystem->put_contents( $language_dir . $cache_filename, $chunk_translations_json );
}
/**
* Combine translation chunks when files are updated.
*
* 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 Language_Pack_Upgrader $instance Upgrader instance.
* @param array $hook_extra Info about the upgraded language packs.
*/
public static function combine_translation_chunk_files( $instance, $hook_extra ) {
if (
! is_a( $instance, 'Language_Pack_Upgrader' ) ||
! isset( $hook_extra['translations'] ) ||
! is_array( $hook_extra['translations'] )
) {
return;
}
// Make sure we're handing the correct domain (could be woocommerce or woocommerce-admin).
$plugin_domain = explode( '/', plugin_basename( __FILE__ ) )[0];
$locales = array();
$language_dir = WP_LANG_DIR . '/plugins/';
// Gather the locales that were updated in this operation.
foreach ( $hook_extra['translations'] as $translation ) {
if (
'plugin' === $translation['type'] &&
$plugin_domain === $translation['slug']
) {
$locales[] = $translation['language'];
}
}
// Build combined translation files for all updated locales.
foreach ( $locales as $locale ) {
// So long as this function is hooked to the 'upgrader_process_complete' action,
// WP_Filesystem should be hooked up to be able to call build_and_save_translations.
self::build_and_save_translations( $language_dir, $plugin_domain, $locale );
}
}
/**
* Load translation strings from language packs for dynamic imports.
*
* @param string $file File location for the script being translated.
* @param string $handle Script handle.
* @param string $domain Text domain.
*
* @return string New file location for the script being translated.
*/
public static function load_script_translation_file( $file, $handle, $domain ) {
// Make sure the main app script is being loaded.
if ( WC_ADMIN_APP !== $handle ) {
return $file;
}
// 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 $file;
}
$locale = determine_locale();
$cache_filename = self::get_combined_translation_filename( $domain, $locale );
return WP_LANG_DIR . '/plugins/' . $cache_filename;
}
/**
* Loads the required scripts on the correct pages.
*/
public static function load_scripts() {
if ( ! self::is_admin_or_embed_page() ) {
return;
}
// Grab translation strings from Webpack-generated chunks.
add_filter( 'load_script_translation_file', array( __CLASS__, 'load_script_translation_file' ), 10, 3 );
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' );
}
Use Route based code splitting to optimize bundle size (https://github.com/woocommerce/woocommerce-admin/pull/4094) * Use lazy loading to split up the size of the js downloaded * Use lazy loading to split up the size of the js downloaded * Add Moment Timezone plugin to reduce size of data file. * Lazy load header panels and use Dashicons for faster loading * Load assets from the correct publicPath * Load assets from the correct publicPath * PHP cs fixes * Fix missing quotes on string literal. * Fix PropType warning for lazy loaded component. * Separate the task list and dashboard chunks. * Lazy load dashboard sections. * Restore original icons and reduce size by importing only the icons needed * Lazy load alerts to save more Kb on initial load * Minify built JS in production mode. * Add preload tags for WC Admin assets. (https://github.com/woocommerce/woocommerce-admin/pull/4162) * Fix linting errors. * Add modified UnminifiedWebpackPlugin. * Produce minified and unminified bundles for all builds. * Remove unused variable from webpack config. * Run unminify after sourcemap generation. * Only hook after optimization if we're using a devtool. * Add minification suffix in Loader::get_url(). * Lazy load OBW on new home screen. * Move OBW style up a level to layout. * Hydrate ProfileWizard independently of withSelect and withDispatch * Fix order of composition and fallback function when using hydration. Co-authored-by: Jeff Stieler <jeff.m.stieler@gmail.com> Co-authored-by: Paul Sealock <psealock@gmail.com>
2020-04-29 18:01:27 +00:00
// Preload our assets.
self::output_header_preload_tags();
}
/**
* Render a preload link tag for a dependency, optionally
* checked against a provided allowlist.
Use Route based code splitting to optimize bundle size (https://github.com/woocommerce/woocommerce-admin/pull/4094) * Use lazy loading to split up the size of the js downloaded * Use lazy loading to split up the size of the js downloaded * Add Moment Timezone plugin to reduce size of data file. * Lazy load header panels and use Dashicons for faster loading * Load assets from the correct publicPath * Load assets from the correct publicPath * PHP cs fixes * Fix missing quotes on string literal. * Fix PropType warning for lazy loaded component. * Separate the task list and dashboard chunks. * Lazy load dashboard sections. * Restore original icons and reduce size by importing only the icons needed * Lazy load alerts to save more Kb on initial load * Minify built JS in production mode. * Add preload tags for WC Admin assets. (https://github.com/woocommerce/woocommerce-admin/pull/4162) * Fix linting errors. * Add modified UnminifiedWebpackPlugin. * Produce minified and unminified bundles for all builds. * Remove unused variable from webpack config. * Run unminify after sourcemap generation. * Only hook after optimization if we're using a devtool. * Add minification suffix in Loader::get_url(). * Lazy load OBW on new home screen. * Move OBW style up a level to layout. * Hydrate ProfileWizard independently of withSelect and withDispatch * Fix order of composition and fallback function when using hydration. Co-authored-by: Jeff Stieler <jeff.m.stieler@gmail.com> Co-authored-by: Paul Sealock <psealock@gmail.com>
2020-04-29 18:01:27 +00:00
*
* 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.
Use Route based code splitting to optimize bundle size (https://github.com/woocommerce/woocommerce-admin/pull/4094) * Use lazy loading to split up the size of the js downloaded * Use lazy loading to split up the size of the js downloaded * Add Moment Timezone plugin to reduce size of data file. * Lazy load header panels and use Dashicons for faster loading * Load assets from the correct publicPath * Load assets from the correct publicPath * PHP cs fixes * Fix missing quotes on string literal. * Fix PropType warning for lazy loaded component. * Separate the task list and dashboard chunks. * Lazy load dashboard sections. * Restore original icons and reduce size by importing only the icons needed * Lazy load alerts to save more Kb on initial load * Minify built JS in production mode. * Add preload tags for WC Admin assets. (https://github.com/woocommerce/woocommerce-admin/pull/4162) * Fix linting errors. * Add modified UnminifiedWebpackPlugin. * Produce minified and unminified bundles for all builds. * Remove unused variable from webpack config. * Run unminify after sourcemap generation. * Only hook after optimization if we're using a devtool. * Add minification suffix in Loader::get_url(). * Lazy load OBW on new home screen. * Move OBW style up a level to layout. * Hydrate ProfileWizard independently of withSelect and withDispatch * Fix order of composition and fallback function when using hydration. Co-authored-by: Jeff Stieler <jeff.m.stieler@gmail.com> Co-authored-by: Paul Sealock <psealock@gmail.com>
2020-04-29 18:01:27 +00:00
*/
public static function maybe_output_preload_link_tag( $dependency, $type, $allowlist = array() ) {
if ( ! empty( $allowlist ) && ! in_array( $dependency->handle, $allowlist, true ) ) {
Use Route based code splitting to optimize bundle size (https://github.com/woocommerce/woocommerce-admin/pull/4094) * Use lazy loading to split up the size of the js downloaded * Use lazy loading to split up the size of the js downloaded * Add Moment Timezone plugin to reduce size of data file. * Lazy load header panels and use Dashicons for faster loading * Load assets from the correct publicPath * Load assets from the correct publicPath * PHP cs fixes * Fix missing quotes on string literal. * Fix PropType warning for lazy loaded component. * Separate the task list and dashboard chunks. * Lazy load dashboard sections. * Restore original icons and reduce size by importing only the icons needed * Lazy load alerts to save more Kb on initial load * Minify built JS in production mode. * Add preload tags for WC Admin assets. (https://github.com/woocommerce/woocommerce-admin/pull/4162) * Fix linting errors. * Add modified UnminifiedWebpackPlugin. * Produce minified and unminified bundles for all builds. * Remove unused variable from webpack config. * Run unminify after sourcemap generation. * Only hook after optimization if we're using a devtool. * Add minification suffix in Loader::get_url(). * Lazy load OBW on new home screen. * Move OBW style up a level to layout. * Hydrate ProfileWizard independently of withSelect and withDispatch * Fix order of composition and fallback function when using hydration. Co-authored-by: Jeff Stieler <jeff.m.stieler@gmail.com> Co-authored-by: Paul Sealock <psealock@gmail.com>
2020-04-29 18:01:27 +00:00
return;
}
$source = $dependency->ver ? add_query_arg( 'ver', $dependency->ver, $dependency->src ) : $dependency->src;
echo '<link rel="preload" href="', esc_url( $source ), '" as="', esc_attr( $type ), '" />', "\n";
}
/**
* Output a preload link tag for dependencies (and their sub dependencies)
* with an optional allowlist.
Use Route based code splitting to optimize bundle size (https://github.com/woocommerce/woocommerce-admin/pull/4094) * Use lazy loading to split up the size of the js downloaded * Use lazy loading to split up the size of the js downloaded * Add Moment Timezone plugin to reduce size of data file. * Lazy load header panels and use Dashicons for faster loading * Load assets from the correct publicPath * Load assets from the correct publicPath * PHP cs fixes * Fix missing quotes on string literal. * Fix PropType warning for lazy loaded component. * Separate the task list and dashboard chunks. * Lazy load dashboard sections. * Restore original icons and reduce size by importing only the icons needed * Lazy load alerts to save more Kb on initial load * Minify built JS in production mode. * Add preload tags for WC Admin assets. (https://github.com/woocommerce/woocommerce-admin/pull/4162) * Fix linting errors. * Add modified UnminifiedWebpackPlugin. * Produce minified and unminified bundles for all builds. * Remove unused variable from webpack config. * Run unminify after sourcemap generation. * Only hook after optimization if we're using a devtool. * Add minification suffix in Loader::get_url(). * Lazy load OBW on new home screen. * Move OBW style up a level to layout. * Hydrate ProfileWizard independently of withSelect and withDispatch * Fix order of composition and fallback function when using hydration. Co-authored-by: Jeff Stieler <jeff.m.stieler@gmail.com> Co-authored-by: Paul Sealock <psealock@gmail.com>
2020-04-29 18:01:27 +00:00
*
* 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.
Use Route based code splitting to optimize bundle size (https://github.com/woocommerce/woocommerce-admin/pull/4094) * Use lazy loading to split up the size of the js downloaded * Use lazy loading to split up the size of the js downloaded * Add Moment Timezone plugin to reduce size of data file. * Lazy load header panels and use Dashicons for faster loading * Load assets from the correct publicPath * Load assets from the correct publicPath * PHP cs fixes * Fix missing quotes on string literal. * Fix PropType warning for lazy loaded component. * Separate the task list and dashboard chunks. * Lazy load dashboard sections. * Restore original icons and reduce size by importing only the icons needed * Lazy load alerts to save more Kb on initial load * Minify built JS in production mode. * Add preload tags for WC Admin assets. (https://github.com/woocommerce/woocommerce-admin/pull/4162) * Fix linting errors. * Add modified UnminifiedWebpackPlugin. * Produce minified and unminified bundles for all builds. * Remove unused variable from webpack config. * Run unminify after sourcemap generation. * Only hook after optimization if we're using a devtool. * Add minification suffix in Loader::get_url(). * Lazy load OBW on new home screen. * Move OBW style up a level to layout. * Hydrate ProfileWizard independently of withSelect and withDispatch * Fix order of composition and fallback function when using hydration. Co-authored-by: Jeff Stieler <jeff.m.stieler@gmail.com> Co-authored-by: Paul Sealock <psealock@gmail.com>
2020-04-29 18:01:27 +00:00
*/
public static function output_header_preload_tags_for_type( $type, $allowlist = array() ) {
Use Route based code splitting to optimize bundle size (https://github.com/woocommerce/woocommerce-admin/pull/4094) * Use lazy loading to split up the size of the js downloaded * Use lazy loading to split up the size of the js downloaded * Add Moment Timezone plugin to reduce size of data file. * Lazy load header panels and use Dashicons for faster loading * Load assets from the correct publicPath * Load assets from the correct publicPath * PHP cs fixes * Fix missing quotes on string literal. * Fix PropType warning for lazy loaded component. * Separate the task list and dashboard chunks. * Lazy load dashboard sections. * Restore original icons and reduce size by importing only the icons needed * Lazy load alerts to save more Kb on initial load * Minify built JS in production mode. * Add preload tags for WC Admin assets. (https://github.com/woocommerce/woocommerce-admin/pull/4162) * Fix linting errors. * Add modified UnminifiedWebpackPlugin. * Produce minified and unminified bundles for all builds. * Remove unused variable from webpack config. * Run unminify after sourcemap generation. * Only hook after optimization if we're using a devtool. * Add minification suffix in Loader::get_url(). * Lazy load OBW on new home screen. * Move OBW style up a level to layout. * Hydrate ProfileWizard independently of withSelect and withDispatch * Fix order of composition and fallback function when using hydration. Co-authored-by: Jeff Stieler <jeff.m.stieler@gmail.com> Co-authored-by: Paul Sealock <psealock@gmail.com>
2020-04-29 18:01:27 +00:00
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;
}
Use Route based code splitting to optimize bundle size (https://github.com/woocommerce/woocommerce-admin/pull/4094) * Use lazy loading to split up the size of the js downloaded * Use lazy loading to split up the size of the js downloaded * Add Moment Timezone plugin to reduce size of data file. * Lazy load header panels and use Dashicons for faster loading * Load assets from the correct publicPath * Load assets from the correct publicPath * PHP cs fixes * Fix missing quotes on string literal. * Fix PropType warning for lazy loaded component. * Separate the task list and dashboard chunks. * Lazy load dashboard sections. * Restore original icons and reduce size by importing only the icons needed * Lazy load alerts to save more Kb on initial load * Minify built JS in production mode. * Add preload tags for WC Admin assets. (https://github.com/woocommerce/woocommerce-admin/pull/4162) * Fix linting errors. * Add modified UnminifiedWebpackPlugin. * Produce minified and unminified bundles for all builds. * Remove unused variable from webpack config. * Run unminify after sourcemap generation. * Only hook after optimization if we're using a devtool. * Add minification suffix in Loader::get_url(). * Lazy load OBW on new home screen. * Move OBW style up a level to layout. * Hydrate ProfileWizard independently of withSelect and withDispatch * Fix order of composition and fallback function when using hydration. Co-authored-by: Jeff Stieler <jeff.m.stieler@gmail.com> Co-authored-by: Paul Sealock <psealock@gmail.com>
2020-04-29 18:01:27 +00:00
// 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 );
}
Use Route based code splitting to optimize bundle size (https://github.com/woocommerce/woocommerce-admin/pull/4094) * Use lazy loading to split up the size of the js downloaded * Use lazy loading to split up the size of the js downloaded * Add Moment Timezone plugin to reduce size of data file. * Lazy load header panels and use Dashicons for faster loading * Load assets from the correct publicPath * Load assets from the correct publicPath * PHP cs fixes * Fix missing quotes on string literal. * Fix PropType warning for lazy loaded component. * Separate the task list and dashboard chunks. * Lazy load dashboard sections. * Restore original icons and reduce size by importing only the icons needed * Lazy load alerts to save more Kb on initial load * Minify built JS in production mode. * Add preload tags for WC Admin assets. (https://github.com/woocommerce/woocommerce-admin/pull/4162) * Fix linting errors. * Add modified UnminifiedWebpackPlugin. * Produce minified and unminified bundles for all builds. * Remove unused variable from webpack config. * Run unminify after sourcemap generation. * Only hook after optimization if we're using a devtool. * Add minification suffix in Loader::get_url(). * Lazy load OBW on new home screen. * Move OBW style up a level to layout. * Hydrate ProfileWizard independently of withSelect and withDispatch * Fix order of composition and fallback function when using hydration. Co-authored-by: Jeff Stieler <jeff.m.stieler@gmail.com> Co-authored-by: Paul Sealock <psealock@gmail.com>
2020-04-29 18:01:27 +00:00
}
self::maybe_output_preload_link_tag( $dependency, $type, $allowlist );
Use Route based code splitting to optimize bundle size (https://github.com/woocommerce/woocommerce-admin/pull/4094) * Use lazy loading to split up the size of the js downloaded * Use lazy loading to split up the size of the js downloaded * Add Moment Timezone plugin to reduce size of data file. * Lazy load header panels and use Dashicons for faster loading * Load assets from the correct publicPath * Load assets from the correct publicPath * PHP cs fixes * Fix missing quotes on string literal. * Fix PropType warning for lazy loaded component. * Separate the task list and dashboard chunks. * Lazy load dashboard sections. * Restore original icons and reduce size by importing only the icons needed * Lazy load alerts to save more Kb on initial load * Minify built JS in production mode. * Add preload tags for WC Admin assets. (https://github.com/woocommerce/woocommerce-admin/pull/4162) * Fix linting errors. * Add modified UnminifiedWebpackPlugin. * Produce minified and unminified bundles for all builds. * Remove unused variable from webpack config. * Run unminify after sourcemap generation. * Only hook after optimization if we're using a devtool. * Add minification suffix in Loader::get_url(). * Lazy load OBW on new home screen. * Move OBW style up a level to layout. * Hydrate ProfileWizard independently of withSelect and withDispatch * Fix order of composition and fallback function when using hydration. Co-authored-by: Jeff Stieler <jeff.m.stieler@gmail.com> Co-authored-by: Paul Sealock <psealock@gmail.com>
2020-04-29 18:01:27 +00:00
}
}
/**
* 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() || ( ! self::is_admin_page() && class_exists( 'Automattic\WooCommerce\Admin\Features\Navigation\Screen' ) && Screen::is_woocommerce_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_heading( $section ) {
echo esc_html( $section );
}
/**
* Set up a div for the header embed to render into.
* The initial contents here are meant as a place loader for when the PHP page initialy loads.
*/
public static function embed_page_header() {
if ( ! self::is_admin_page() && ! self::is_embed_page() ) {
return;
}
if ( ! self::is_embed_page() ) {
return;
}
$sections = self::get_embed_breadcrumbs();
$sections = is_array( $sections ) ? $sections : array( $sections );
?>
2019-05-23 09:29:14 +00:00
<div id="woocommerce-embedded-root" class="is-embed-loading">
<div class="woocommerce-layout">
<div class="woocommerce-layout__header is-embed-loading">
<h1 class="woocommerce-layout__header-heading">
<?php self::output_heading( end( $sections ) ); ?>
</h1>
</div>
</div>
</div>
<?php
}
/**
* Adds body classes to the main wp-admin wrapper, allowing us to better target elements in specific scenarios.
*
* @param string $admin_body_class Body class to add.
*/
public static function add_admin_body_classes( $admin_body_class = '' ) {
if ( ! self::is_admin_or_embed_page() ) {
return $admin_body_class;
}
$classes = explode( ' ', trim( $admin_body_class ) );
$classes[] = 'woocommerce-page';
if ( self::is_embed_page() ) {
$classes[] = 'woocommerce-embed-page';
}
/**
* Some routes or features like onboarding hide the wp-admin navigation and masterbar.
* Setting `woocommerce_admin_is_loading` to true allows us to premeptively hide these
* elements while the JS app loads.
* This class needs to be removed by those feature components (like <ProfileWizard />).
*
* @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';
}
$admin_body_class = implode( ' ', array_unique( $classes ) );
return " $admin_body_class ";
}
Add WooCommerce Mobile App Banner Ad for Android and iOS (https://github.com/woocommerce/woocommerce-admin/pull/5037) Fixes woocommerce/woocommerce-admin#4654 The feature calls for a mobile app ad banner to be displayed to users on mobile devices. Based on the discussion in woocommerce/woocommerce-admin#4654 this implements the following: 1. [an iOS Smart App Banner](https://developer.apple.com/library/archive/documentation/AppleApplications/Reference/SafariWebContent/PromotingAppswithAppBanners/PromotingAppswithAppBanners.html). This banner is a meta tag that is generated in PHP. It will only display on iOS devices. One note about this: **This tag is not directly trackable like the implemented Android banner. If we would like to track its success then I believe [these instructions are relevant](https://stackoverflow.com/questions/12906502/is-it-possible-to-track-click-throughs-from-iphone-smart-banner/20422334woocommerce/woocommerce-admin#20422334)**. 2. A mobile app banner ad that **only displays on Android** and only displays at the `738px` breakpoint specified in the issue. To only display this banner ad on Android, we use basic checking of the user agent string. I weighed this up against other approaches and for this kind of niche use case a simple UA string check is (imho) still the best way to do this. 3. The banner ad makes use of user preferences to retain a per user setting that determines if that user has dismissed the Android banner. We don't/can't do anything like this for the iOS Smart App Banner (but in theory we shouldn't need to).
2020-08-27 01:46:53 +00:00
/**
* Adds an iOS "Smart App Banner" for display on iOS Safari.
* See https://developer.apple.com/library/archive/documentation/AppleApplications/Reference/SafariWebContent/PromotingAppswithAppBanners/PromotingAppswithAppBanners.html
*/
public static function smart_app_banner() {
if ( self::is_admin_or_embed_page() ) {
Add WooCommerce Mobile App Banner Ad for Android and iOS (https://github.com/woocommerce/woocommerce-admin/pull/5037) Fixes woocommerce/woocommerce-admin#4654 The feature calls for a mobile app ad banner to be displayed to users on mobile devices. Based on the discussion in woocommerce/woocommerce-admin#4654 this implements the following: 1. [an iOS Smart App Banner](https://developer.apple.com/library/archive/documentation/AppleApplications/Reference/SafariWebContent/PromotingAppswithAppBanners/PromotingAppswithAppBanners.html). This banner is a meta tag that is generated in PHP. It will only display on iOS devices. One note about this: **This tag is not directly trackable like the implemented Android banner. If we would like to track its success then I believe [these instructions are relevant](https://stackoverflow.com/questions/12906502/is-it-possible-to-track-click-throughs-from-iphone-smart-banner/20422334woocommerce/woocommerce-admin#20422334)**. 2. A mobile app banner ad that **only displays on Android** and only displays at the `738px` breakpoint specified in the issue. To only display this banner ad on Android, we use basic checking of the user agent string. I weighed this up against other approaches and for this kind of niche use case a simple UA string check is (imho) still the best way to do this. 3. The banner ad makes use of user preferences to retain a per user setting that determines if that user has dismissed the Android banner. We don't/can't do anything like this for the iOS Smart App Banner (but in theory we shouldn't need to).
2020-08-27 01:46:53 +00:00
echo "
<meta name='apple-itunes-app' content='app-id=1389130815'>
";
}
}
/**
* 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 '<div class="woocommerce-layout__notice-list-hide" id="wp__notice-list">';
if ( self::is_admin_page() ) {
// Capture all notices and hide them. WordPress Core looks for
// `.wp-header-end` and appends notices after it if found.
// https://github.com/WordPress/WordPress/blob/f6a37e7d39e2534d05b9e542045174498edfe536/wp-admin/js/common.js#L737 .
echo '<div class="wp-header-end" id="woocommerce-layout__notice-catcher"></div>';
}
}
/**
* Runs after admin notices and closes div.
*/
public static function inject_after_notices() {
if ( ! self::is_admin_or_embed_page() ) {
return;
}
// Close the hidden div used to prevent notices from flickering before
// they are inserted elsewhere in the page.
echo '</div>';
}
/**
* Edits Admin title based on section of wc-admin.
*
* @param string $admin_title Modifies admin title.
* @todo Can we do some URL rewriting so we can figure out which page they are on server side?
*/
public static function update_admin_title( $admin_title ) {
if (
! did_action( 'current_screen' ) ||
! self::is_admin_page()
) {
return $admin_title;
}
$sections = self::get_embed_breadcrumbs();
$pieces = array();
foreach ( $sections as $section ) {
$pieces[] = is_array( $section ) ? $section[1] : $section;
}
$pieces = array_reverse( $pieces );
$title = implode( ' &lsaquo; ', $pieces );
/* translators: %1$s: updated title, %2$s: blog info name */
return sprintf( __( '%1$s &lsaquo; %2$s', 'woocommerce-admin' ), $title, get_bloginfo( 'name' ) );
}
/**
* Set up a div for the app to render into.
*/
public static function page_wrapper() {
?>
<div class="wrap">
<div id="root"></div>
</div>
<?php
}
/**
* Hooks extra neccessary data into the component settings array already set in WooCommerce core.
*
* @param array $settings Array of component settings.
* @return array Array of component settings.
*/
public static function add_component_settings( $settings ) {
if ( ! is_admin() ) {
return $settings;
}
if ( ! function_exists( 'wc_blocks_container' ) ) {
global $wp_locale;
// inject data not available via older versions of wc_blocks/woo.
$settings['orderStatuses'] = self::get_order_statuses( wc_get_order_statuses() );
$settings['stockStatuses'] = self::get_order_statuses( wc_get_product_stock_status_options() );
$settings['currency'] = self::get_currency_settings();
$settings['locale'] = [
'siteLocale' => isset( $settings['siteLocale'] )
? $settings['siteLocale']
: get_locale(),
'userLocale' => isset( $settings['l10n']['userLocale'] )
? $settings['l10n']['userLocale']
: get_user_locale(),
'weekdaysShort' => isset( $settings['l10n']['weekdaysShort'] )
? $settings['l10n']['weekdaysShort']
: array_values( $wp_locale->weekday_abbrev ),
];
}
$preload_data_endpoints = apply_filters( 'woocommerce_component_settings_preload_endpoints', array( '/wc/v3' ) );
if ( class_exists( 'Jetpack' ) ) {
$preload_data_endpoints['jetpackStatus'] = '/jetpack/v4/connection';
}
if ( ! empty( $preload_data_endpoints ) ) {
$preload_data = array_reduce(
array_values( $preload_data_endpoints ),
'rest_preload_api_request'
);
}
$preload_options = apply_filters( 'woocommerce_admin_preload_options', array() );
if ( ! empty( $preload_options ) ) {
foreach ( $preload_options as $option ) {
$settings['preloadOptions'][ $option ] = get_option( $option );
}
}
$preload_settings = apply_filters( 'woocommerce_admin_preload_settings', array() );
if ( ! empty( $preload_settings ) ) {
$setting_options = new \WC_REST_Setting_Options_V2_Controller();
foreach ( $preload_settings as $group ) {
$group_settings = $setting_options->get_group_settings( $group );
$preload_settings = [];
foreach ( $group_settings as $option ) {
$preload_settings[ $option['id'] ] = $option['value'];
}
$settings['preloadSettings'][ $group ] = $preload_settings;
}
}
$user_controller = new \WP_REST_Users_Controller();
$request = new \WP_REST_Request();
$request->set_query_params( array( 'context' => 'edit' ) );
$user_response = $user_controller->get_current_item( $request );
Migrate user store to wp.data (https://github.com/woocommerce/woocommerce-admin/pull/4505) * Initial user preferences custom hook (wc meta). * Organize dependencies. * Specify dependencies in useSelect() call. * Remove specifying preference keys. There's currently no performance benefit to be had. * Add HoC for hydrating current user data. * Add user prefs update method. * Export user prefs hook and HoC in data package. * Handle error condition when updating user preferences. * Use closure to get user ID for prefs update. * Refactor analytics dashboard to use new user hook. * Refactor ReportTable component to use new user hook. * Move prop access to top of function component. * Add todo for potential manual resolution logic. * Refactor DashboardCharts component to use new user preferences hook. * Move code out of functional component where possible. * Fix chart interval query property. * Refactor Leaderboards component to use new user preferences hook. * Move code out of functional component where possible. * Refactor Inbox panel to use user prefs hook. @todo - updating last read isn't working. * Use user prefs hook in StatsOverview component. * Export user preferences store name. * Use new store for unread inbox indicator. * Hydrate current user data higher up in the app. * Update "last read" timestamp in inbox panel. * Fix StatsOverview test. * Remove unused dispatch method from StorePerformance component. * Remove now defunct user methods from wc-api spec. * Add tests for isRequesting. * JSON decode WC meta on updated user object. * Test user prefs retrieval and save. * Remove todo comment. * Use user prefs hook in Jetpack install CTA on homepage.
2020-06-10 16:46:46 +00:00
$current_user_data = is_wp_error( $user_response ) ? (object) array() : $user_response->get_data();
$settings['currentUserData'] = $current_user_data;
$settings['reviewsEnabled'] = get_option( 'woocommerce_enable_reviews' );
$settings['manageStock'] = get_option( 'woocommerce_manage_stock' );
$settings['commentModeration'] = get_option( 'comment_moderation' );
$settings['notifyLowStockAmount'] = get_option( 'woocommerce_notify_low_stock_amount' );
// @todo On merge, once plugin images are added to core WooCommerce, `wcAdminAssetUrl` can be retired,
// and `wcAssetUrl` can be used in its place throughout the codebase.
$settings['wcAdminAssetUrl'] = plugins_url( 'images/', dirname( __DIR__ ) . '/woocommerce-admin.php' );
$settings['wcVersion'] = WC_VERSION;
$settings['siteUrl'] = site_url();
$settings['shopUrl'] = get_permalink( wc_get_page_id( 'shop' ) );
$settings['homeUrl'] = home_url();
$settings['dateFormat'] = get_option( 'date_format' );
$settings['plugins'] = array(
'installedPlugins' => PluginsHelper::get_installed_plugin_slugs(),
Use Route based code splitting to optimize bundle size (https://github.com/woocommerce/woocommerce-admin/pull/4094) * Use lazy loading to split up the size of the js downloaded * Use lazy loading to split up the size of the js downloaded * Add Moment Timezone plugin to reduce size of data file. * Lazy load header panels and use Dashicons for faster loading * Load assets from the correct publicPath * Load assets from the correct publicPath * PHP cs fixes * Fix missing quotes on string literal. * Fix PropType warning for lazy loaded component. * Separate the task list and dashboard chunks. * Lazy load dashboard sections. * Restore original icons and reduce size by importing only the icons needed * Lazy load alerts to save more Kb on initial load * Minify built JS in production mode. * Add preload tags for WC Admin assets. (https://github.com/woocommerce/woocommerce-admin/pull/4162) * Fix linting errors. * Add modified UnminifiedWebpackPlugin. * Produce minified and unminified bundles for all builds. * Remove unused variable from webpack config. * Run unminify after sourcemap generation. * Only hook after optimization if we're using a devtool. * Add minification suffix in Loader::get_url(). * Lazy load OBW on new home screen. * Move OBW style up a level to layout. * Hydrate ProfileWizard independently of withSelect and withDispatch * Fix order of composition and fallback function when using hydration. Co-authored-by: Jeff Stieler <jeff.m.stieler@gmail.com> Co-authored-by: Paul Sealock <psealock@gmail.com>
2020-04-29 18:01:27 +00:00
'activePlugins' => Plugins::get_active_plugins(),
);
// Plugins that depend on changing the translation work on the server but not the client -
// WooCommerce Branding is an example of this - so pass through the translation of
// 'WooCommerce' to wcSettings.
$settings['woocommerceTranslation'] = __( 'WooCommerce', 'woocommerce-admin' );
// We may have synced orders with a now-unregistered status.
// E.g An extension that added statuses is now inactive or removed.
$settings['unregisteredOrderStatuses'] = self::get_unregistered_order_statuses();
Add Variations Report (https://github.com/woocommerce/woocommerce-admin/pull/5167) * Add initial Variations Report to analytics feature. * Restrict query to variations when not specifying any product IDs. * Add route to get variations without specifying a parent. * Move variations table component to variations report directory. * Add missing LIMIT clause to variations report query. * Remove broken features from Variations table. * Add Variations report controller to CSV emailer. * Add initial Variation Stats endpoint, based on Product Stats. * Hook Variations Report components up to new stats endpoint. * Hook attribute filter up to variations report queries. * Remove variation title filter usage. See: https://github.com/woocommerce/woocommerce-admin/pull/5100 * Use filtered separator in variation name formatting. * Add "single variation" to variations report, fix autocompleter API request. * Fix segmentation by variation. * Add comparison to variations report. * Always include manually specified variations in report results. * Fix variations report table comparison mode. The ReportTable component expects the `filter` query param. * Fixing styling of compare button without table search component. * Add variation filter to Orders report. * Link orders count to orders report filtered by variation. * Orders report: include variation attributes in product names. * Further style tweaks for variations report download button. * Add variations filter to order stats query. * Clean up "category includes" login in REST controllers. Prep for "category excludes" in the Variations report. * Support category exclusion in report filters. * Fix filter param used by the variation report table component. * Add category filter to variations report. * Fix initial selected ReportTable rows when using non-default compareParam. * Add a new autocompleter for variable products. * Add products filter to variations report. * Fix tests. * Handle variation IDs that are no longer found. * Add documentation. * Use getSetting() instead of directly accessing window properties in client code. * Fix ordering Variations by SKU.
2020-09-25 13:57:48 +00:00
// The separator used for attributes found in Variation titles.
$settings['variationTitleAttributesSeparator'] = apply_filters( 'woocommerce_product_variation_title_attributes_separator', ' - ', new \WC_Product() );
if ( ! empty( $preload_data_endpoints ) ) {
$settings['dataEndpoints'] = isset( $settings['dataEndpoints'] )
? $settings['dataEndpoints']
: [];
foreach ( $preload_data_endpoints as $key => $endpoint ) {
// Handle error case: rest_do_request() doesn't guarantee success.
if ( empty( $preload_data[ $endpoint ] ) ) {
$settings['dataEndpoints'][ $key ] = array();
} else {
$settings['dataEndpoints'][ $key ] = $preload_data[ $endpoint ]['body'];
}
}
}
$settings = self::get_custom_settings( $settings );
if ( self::is_embed_page() ) {
$settings['embedBreadcrumbs'] = self::get_embed_breadcrumbs();
}
$settings['allowMarketplaceSuggestions'] = WC_Marketplace_Suggestions::allow_suggestions();
$settings['connectNonce'] = wp_create_nonce( 'connect' );
return $settings;
}
/**
* Format order statuses by removing a leading 'wc-' if present.
*
* @param array $statuses Order statuses.
* @return array formatted statuses.
*/
public static function get_order_statuses( $statuses ) {
$formatted_statuses = array();
foreach ( $statuses as $key => $value ) {
$formatted_key = preg_replace( '/^wc-/', '', $key );
$formatted_statuses[ $formatted_key ] = $value;
}
return $formatted_statuses;
}
/**
* Get all order statuses present in analytics tables that aren't registered.
*
* @return array Unregistered order statuses.
*/
public static function get_unregistered_order_statuses() {
$registered_statuses = wc_get_order_statuses();
$all_synced_statuses = OrdersDataStore::get_all_statuses();
$unregistered_statuses = array_diff( $all_synced_statuses, array_keys( $registered_statuses ) );
$formatted_status_keys = self::get_order_statuses( array_fill_keys( $unregistered_statuses, '' ) );
$formatted_statuses = array_keys( $formatted_status_keys );
return array_combine( $formatted_statuses, $formatted_statuses );
}
/**
* Register the admin settings for use in the WC REST API
*
* @param array $groups Array of setting groups.
* @return array
*/
public static function add_settings_group( $groups ) {
$groups[] = array(
'id' => 'wc_admin',
'label' => __( 'WooCommerce Admin', 'woocommerce-admin' ),
'description' => __( 'Settings for WooCommerce admin reporting.', 'woocommerce-admin' ),
);
return $groups;
}
/**
* Add WC Admin specific settings
*
* @param array $settings Array of settings in wc admin group.
* @return array
*/
public static function add_settings( $settings ) {
$unregistered_statuses = self::get_unregistered_order_statuses();
$registered_statuses = self::get_order_statuses( wc_get_order_statuses() );
$all_statuses = array_merge( $unregistered_statuses, $registered_statuses );
$settings[] = array(
'id' => 'woocommerce_excluded_report_order_statuses',
'option_key' => 'woocommerce_excluded_report_order_statuses',
'label' => __( 'Excluded report order statuses', 'woocommerce-admin' ),
'description' => __( 'Statuses that should not be included when calculating report totals.', 'woocommerce-admin' ),
'default' => array( 'pending', 'cancelled', 'failed' ),
'type' => 'multiselect',
'options' => $all_statuses,
);
$settings[] = array(
'id' => 'woocommerce_actionable_order_statuses',
'option_key' => 'woocommerce_actionable_order_statuses',
'label' => __( 'Actionable order statuses', 'woocommerce-admin' ),
'description' => __( 'Statuses that require extra action on behalf of the store admin.', 'woocommerce-admin' ),
'default' => array( 'processing', 'on-hold' ),
'type' => 'multiselect',
'options' => $all_statuses,
);
2019-05-22 21:43:04 +00:00
$settings[] = array(
'id' => 'woocommerce_default_date_range',
'option_key' => 'woocommerce_default_date_range',
'label' => __( 'Default Date Range', 'woocommerce-admin' ),
'description' => __( 'Default Date Range', 'woocommerce-admin' ),
'default' => 'period=month&compare=previous_year',
'type' => 'text',
);
return $settings;
}
/**
* Gets custom settings used for WC Admin.
*
* @param array $settings Array of settings to merge into.
* @return array
*/
public static function get_custom_settings( $settings ) {
2019-08-12 18:11:28 +00:00
$wc_rest_settings_options_controller = new \WC_REST_Setting_Options_Controller();
$wc_admin_group_settings = $wc_rest_settings_options_controller->get_group_settings( 'wc_admin' );
$settings['wcAdminSettings'] = array();
foreach ( $wc_admin_group_settings as $setting ) {
if ( ! empty( $setting['id'] ) ) {
$settings['wcAdminSettings'][ $setting['id'] ] = $setting['value'];
}
}
return $settings;
}
/**
* Return an object defining the currecy options for the site's current currency
*
* @return array Settings for the current currency {
* Array of settings.
*
* @type string $code Currency code.
* @type string $precision Number of decimals.
* @type string $symbol Symbol for currency.
* }
*/
public static function get_currency_settings() {
$code = get_woocommerce_currency();
return apply_filters(
'wc_currency_settings',
array(
'code' => $code,
'precision' => wc_get_price_decimals(),
'symbol' => html_entity_decode( get_woocommerce_currency_symbol( $code ) ),
'symbolPosition' => get_option( 'woocommerce_currency_pos' ),
'decimalSeparator' => wc_get_price_decimal_separator(),
'thousandSeparator' => wc_get_price_thousand_separator(),
'priceFormat' => html_entity_decode( get_woocommerce_price_format() ),
)
);
}
/**
* Registers WooCommerce specific user data to the WordPress user API.
*/
public static function register_user_data() {
register_rest_field(
'user',
'woocommerce_meta',
array(
2019-08-12 18:11:28 +00:00
'get_callback' => array( __CLASS__, 'get_user_data_values' ),
'update_callback' => array( __CLASS__, 'update_user_data_values' ),
'schema' => null,
)
);
}
/**
* For all the registered user data fields ( Loader::get_user_data_fields ), fetch the data
* for returning via the REST API.
*
* @param WP_User $user Current user.
*/
public static function get_user_data_values( $user ) {
$values = array();
foreach ( self::get_user_data_fields() as $field ) {
$values[ $field ] = self::get_user_data_field( $user['id'], $field );
}
return $values;
}
/**
* For all the registered user data fields ( Loader::get_user_data_fields ), update the data
* for the REST API.
*
* @param array $values The new values for the meta.
* @param WP_User $user The current user.
* @param string $field_id The field id for the user meta.
*/
public static function update_user_data_values( $values, $user, $field_id ) {
if ( empty( $values ) || ! is_array( $values ) || 'woocommerce_meta' !== $field_id ) {
return;
}
$fields = self::get_user_data_fields();
$updates = array();
foreach ( $values as $field => $value ) {
if ( in_array( $field, $fields, true ) ) {
$updates[ $field ] = $value;
self::update_user_data_field( $user->ID, $field, $value );
}
}
return $updates;
}
/**
* We store some WooCommerce specific user meta attached to users endpoint,
* so that we can track certain preferences or values such as the inbox activity panel last open time.
* Additional fields can be added in the function below, and then used via wc-admin's currentUser data.
*
* @return array Fields to expose over the WP user endpoint.
*/
public static function get_user_data_fields() {
return apply_filters( 'woocommerce_admin_get_user_data_fields', array() );
}
/**
* Helper to update user data fields.
*
* @param int $user_id User ID.
* @param string $field Field name.
* @param mixed $value Field value.
*/
public static function update_user_data_field( $user_id, $field, $value ) {
update_user_meta( $user_id, 'woocommerce_admin_' . $field, $value );
}
/**
* Helper to retrive user data fields.
*
* Migrates old key prefixes as well.
*
* @param int $user_id User ID.
* @param string $field Field name.
* @return mixed The user field value.
*/
public static function get_user_data_field( $user_id, $field ) {
$meta_value = get_user_meta( $user_id, 'woocommerce_admin_' . $field, true );
// Migrate old meta values (prefix changed from `wc_admin_` to `woocommerce_admin_`).
if ( '' === $meta_value ) {
$old_meta_value = get_user_meta( $user_id, 'wc_admin_' . $field, true );
if ( '' !== $old_meta_value ) {
self::update_user_data_field( $user_id, $field, $old_meta_value );
delete_user_meta( $user_id, 'wc_admin_' . $field );
$meta_value = $old_meta_value;
}
}
return $meta_value;
}
/**
* Injects wp-shared-settings as a dependency if it's present.
*/
public static function inject_wc_settings_dependencies() {
if ( wp_script_is( 'wc-settings', 'registered' ) ) {
$handles_for_injection = [
'wc-csv',
'wc-currency',
'wc-customer-effort-score',
'wc-navigation',
// NOTE: This should be removed when Gutenberg is updated and
// the notices package is removed from WooCommerce Admin.
'wc-notices',
'wc-number',
'wc-date',
'wc-components',
'wc-tracks',
];
foreach ( $handles_for_injection as $handle ) {
$script = wp_scripts()->query( $handle, 'registered' );
if ( $script instanceof _WP_Dependency ) {
$script->deps[] = 'wc-settings';
}
}
}
}
/**
* Delete woocommerce_onboarding_homepage_post_id field when the homepage is deleted
*
* @param int $post_id The deleted post id.
*/
public static function delete_homepage( $post_id ) {
if ( 'page' !== get_post_type( $post_id ) ) {
return;
}
$homepage_id = intval( get_option( 'woocommerce_onboarding_homepage_post_id', false ) );
if ( $homepage_id === $post_id ) {
delete_option( 'woocommerce_onboarding_homepage_post_id' );
}
}
}