'woocommerce-dashboard', // Expected to be overridden if dashboard is enabled.
'parent' => 'woocommerce',
'title' => null,
'path' => self::APP_ENTRY_POINT,
)
);
// Connect existing WooCommerce pages.
require_once WC_ADMIN_ABSPATH . 'includes/page-controller/connect-existing-pages.php';
}
/**
* Remove the menu item for the app entry point page.
*/
public static function remove_app_entry_page_menu_item() {
global $submenu;
// User does not have capabilites to see the submenu.
if ( ! current_user_can( 'manage_woocommerce' ) || empty( $submenu['woocommerce'] ) ) {
return;
}
$wc_admin_key = null;
foreach ( $submenu['woocommerce'] as $submenu_key => $submenu_item ) {
// Our app entry page menu item has no title.
if ( is_null( $submenu_item[0] ) && self::APP_ENTRY_POINT === $submenu_item[2] ) {
$wc_admin_key = $submenu_key;
break;
}
}
if ( ! $wc_admin_key ) {
return;
}
unset( $submenu['woocommerce'][ $wc_admin_key ] );
}
/**
* Registers all the neccessary scripts and styles to show the admin experience.
*/
public static function register_scripts() {
if ( ! function_exists( 'wp_set_script_translations' ) ) {
return;
}
wp_register_script(
'wc-csv',
self::get_url( 'csv-export/index.js' ),
array(),
self::get_file_version( 'csv-export/index.js' ),
true
);
wp_register_script(
'wc-currency',
self::get_url( 'currency/index.js' ),
array( 'wc-number' ),
self::get_file_version( 'currency/index.js' ),
true
);
wp_set_script_translations( 'wc-currency', 'woocommerce-admin' );
wp_register_script(
'wc-navigation',
self::get_url( 'navigation/index.js' ),
array(),
self::get_file_version( 'navigation/index.js' ),
true
);
wp_register_script(
'wc-number',
self::get_url( 'number/index.js' ),
array(),
self::get_file_version( 'number/index.js' ),
true
);
wp_register_script(
'wc-date',
self::get_url( 'date/index.js' ),
array( 'wp-date', 'wp-i18n' ),
self::get_file_version( 'date/index.js' ),
true
);
wp_set_script_translations( 'wc-date', 'woocommerce-admin' );
wp_register_script(
'wc-components',
self::get_url( 'components/index.js' ),
array(
'wp-components',
'wp-data',
'wp-element',
'wp-hooks',
'wp-i18n',
'wp-keycodes',
'wc-csv',
'wc-currency',
'wc-date',
'wc-navigation',
'wc-number',
),
self::get_file_version( 'components/index.js' ),
true
);
wp_set_script_translations( 'wc-components', 'woocommerce-admin' );
wp_register_style(
'wc-components',
self::get_url( 'components/style.css' ),
array( 'wp-edit-blocks' ),
self::get_file_version( 'components/style.css' )
);
wp_style_add_data( 'wc-components', 'rtl', 'replace' );
wp_register_style(
'wc-components-ie',
self::get_url( 'components/ie.css' ),
array( 'wp-edit-blocks' ),
self::get_file_version( 'components/ie.css' )
);
wp_style_add_data( 'wc-components-ie', 'rtl', 'replace' );
$entry = 'app';
if ( self::is_embed_page() ) {
$entry = 'embedded';
}
wp_register_script(
WC_ADMIN_APP,
self::get_url( "{$entry}/index.js" ),
array( 'wc-components', 'wc-navigation', 'wp-date', 'wp-html-entities', 'wp-keycodes', 'wp-i18n' ),
self::get_file_version( "{$entry}/index.js" ),
true
);
wp_set_script_translations( WC_ADMIN_APP, 'woocommerce-admin' );
wp_register_style(
WC_ADMIN_APP,
self::get_url( "{$entry}/style.css" ),
array( 'wc-components' ),
self::get_file_version( "{$entry}/style.css" )
);
wp_style_add_data( WC_ADMIN_APP, 'rtl', 'replace' );
}
/**
* Loads the required scripts on the correct pages.
*/
public static function load_scripts() {
if ( ! self::is_admin_page() && ! self::is_embed_page() ) {
return;
}
wp_enqueue_script( WC_ADMIN_APP );
wp_enqueue_style( WC_ADMIN_APP );
// Use server-side detection to prevent unneccessary stylesheet loading in other browsers.
$user_agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : ''; // WPCS: 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' );
}
}
/**
* Returns true if we are on a JS powered admin page.
*/
public static function is_admin_page() {
return wc_admin_is_registered_page();
}
/**
* Returns true if we are on a "classic" (non JS app) powered admin page.
*
* @todo See usage in `admin.php`. This needs refactored and implemented properly in core.
*/
public static function is_embed_page() {
return wc_admin_is_connected_page();
}
/**
* Returns breadcrumbs for the current page.
*/
private static function get_embed_breadcrumbs() {
return wc_admin_get_breadcrumbs();
}
/**
* Outputs breadcrumbs via PHP for the initial load of an embedded page.
*
* @param array $section Section to create breadcrumb from.
*/
private static function output_breadcrumbs( $section ) {
?>
';
echo ''; // https://github.com/WordPress/WordPress/blob/f6a37e7d39e2534d05b9e542045174498edfe536/wp-admin/js/common.js#L737.
}
/**
* Runs after admin notices and closes div.
*/
public static function inject_after_notices() {
if ( ( ! self::is_admin_page() && ! self::is_embed_page() ) ) {
return;
}
echo '';
}
/**
* 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 ( ! self::is_admin_page() && ! self::is_embed_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( ' ‹ ', $pieces );
/* translators: %1$s: updated title, %2$s: blog info name */
return sprintf( __( '%1$s ‹ %2$s', 'woocommerce-admin' ), $title, get_bloginfo( 'name' ) );
}
/**
* Set up a div for the app to render into.
*/
public static function page_wrapper() {
?>
$endpoint ) {
$settings['dataEndpoints'][ $key ] = $preload_data[ $endpoint ]['body'];
}
}
$settings = self::get_custom_settings( $settings );
if ( self::is_embed_page() ) {
$settings['embedBreadcrumbs'] = self::get_embed_breadcrumbs();
}
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;
}
/**
* 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 ) {
$statuses = self::get_order_statuses( wc_get_order_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' => $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' => $statuses,
);
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 ) {
$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'] ) && ! empty( $setting['value'] ) ) {
$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 ) ),
'position' => get_option( 'woocommerce_currency_pos' ),
'decimal_separator' => wc_get_price_decimal_separator(),
'thousand_separator' => wc_get_price_thousand_separator(),
'price_format' => 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(
'get_callback' => array( 'WC_Admin_Loader', 'get_user_data_values' ),
'update_callback' => array( 'WC_Admin_Loader', 'update_user_data_values' ),
'schema' => null,
)
);
}
/**
* For all the registered user data fields ( WC_Admin_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 ] = get_user_meta( $user['id'], 'wc_admin_' . $field, true );
}
return $values;
}
/**
* For all the registered user data fields ( WC_Admin_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;
update_user_meta( $user->ID, 'wc_admin_' . $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( 'wc_admin_get_user_data_fields', array() );
}
}
new WC_Admin_Loader();