2018-04-17 23:51:48 +00:00
|
|
|
<?php
|
2018-08-20 21:18:13 +00:00
|
|
|
/**
|
|
|
|
* Register javascript & css files.
|
|
|
|
*
|
|
|
|
* @package WC_Admin
|
|
|
|
*/
|
|
|
|
|
2018-04-17 23:51:48 +00:00
|
|
|
/**
|
2018-07-10 12:48:06 +00:00
|
|
|
* Registers the JS & CSS for the admin and admin embed
|
2018-04-17 23:51:48 +00:00
|
|
|
*/
|
2018-07-10 12:48:06 +00:00
|
|
|
function wc_admin_register_script() {
|
2018-06-26 14:49:42 +00:00
|
|
|
// Are we displaying the full React app or just embedding the header on a classic screen?
|
2018-07-10 12:48:06 +00:00
|
|
|
$screen_id = wc_admin_get_current_screen_id();
|
2018-06-26 14:49:42 +00:00
|
|
|
|
2018-11-28 02:58:17 +00:00
|
|
|
if ( in_array( $screen_id, wc_admin_get_embed_enabled_screen_ids(), true ) ) {
|
2018-10-30 18:57:48 +00:00
|
|
|
$entry = 'embedded';
|
2018-06-26 14:49:42 +00:00
|
|
|
} else {
|
2018-10-30 18:57:48 +00:00
|
|
|
$entry = 'app';
|
2018-06-26 14:49:42 +00:00
|
|
|
}
|
|
|
|
|
2018-11-06 21:53:22 +00:00
|
|
|
wp_register_script(
|
|
|
|
'wc-csv',
|
|
|
|
wc_admin_url( 'dist/csv-export/index.js' ),
|
|
|
|
array(),
|
|
|
|
filemtime( wc_admin_dir_path( 'dist/csv-export/index.js' ) ),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2018-08-20 21:18:13 +00:00
|
|
|
wp_register_script(
|
2018-10-30 18:57:48 +00:00
|
|
|
'wc-currency',
|
|
|
|
wc_admin_url( 'dist/currency/index.js' ),
|
2019-01-29 16:48:46 +00:00
|
|
|
array( 'wc-number' ),
|
2018-10-30 18:57:48 +00:00
|
|
|
filemtime( wc_admin_dir_path( 'dist/currency/index.js' ) ),
|
2018-08-20 21:18:13 +00:00
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2018-11-05 21:02:04 +00:00
|
|
|
wp_register_script(
|
|
|
|
'wc-navigation',
|
|
|
|
wc_admin_url( 'dist/navigation/index.js' ),
|
|
|
|
array(),
|
|
|
|
filemtime( wc_admin_dir_path( 'dist/navigation/index.js' ) ),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2019-01-29 16:48:46 +00:00
|
|
|
wp_register_script(
|
|
|
|
'wc-number',
|
|
|
|
wc_admin_url( 'dist/number/index.js' ),
|
|
|
|
array(),
|
|
|
|
filemtime( wc_admin_dir_path( 'dist/number/index.js' ) ),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2018-10-30 18:57:48 +00:00
|
|
|
wp_register_script(
|
|
|
|
'wc-date',
|
|
|
|
wc_admin_url( 'dist/date/index.js' ),
|
|
|
|
array( 'wp-date', 'wp-i18n' ),
|
|
|
|
filemtime( wc_admin_dir_path( 'dist/date/index.js' ) ),
|
|
|
|
true
|
2018-08-20 21:18:13 +00:00
|
|
|
);
|
|
|
|
|
2018-04-17 23:51:48 +00:00
|
|
|
wp_register_script(
|
2018-10-30 18:57:48 +00:00
|
|
|
'wc-components',
|
|
|
|
wc_admin_url( 'dist/components/index.js' ),
|
|
|
|
array(
|
|
|
|
'wp-components',
|
|
|
|
'wp-data',
|
|
|
|
'wp-element',
|
|
|
|
'wp-hooks',
|
|
|
|
'wp-i18n',
|
|
|
|
'wp-keycodes',
|
2018-11-06 21:53:22 +00:00
|
|
|
'wc-csv',
|
2018-10-30 18:57:48 +00:00
|
|
|
'wc-currency',
|
|
|
|
'wc-date',
|
2018-11-05 21:02:04 +00:00
|
|
|
'wc-navigation',
|
2019-01-29 16:48:46 +00:00
|
|
|
'wc-number',
|
2018-10-30 18:57:48 +00:00
|
|
|
),
|
|
|
|
filemtime( wc_admin_dir_path( 'dist/components/index.js' ) ),
|
2018-04-17 23:51:48 +00:00
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2018-10-30 18:57:48 +00:00
|
|
|
wp_register_script(
|
2018-07-10 12:48:06 +00:00
|
|
|
WC_ADMIN_APP,
|
2018-10-30 18:57:48 +00:00
|
|
|
wc_admin_url( "dist/{$entry}/index.js" ),
|
2018-11-27 00:18:36 +00:00
|
|
|
array( 'wc-components', 'wc-navigation', 'wp-date', 'wp-html-entities', 'wp-keycodes', 'wp-i18n' ),
|
2018-10-30 18:57:48 +00:00
|
|
|
filemtime( wc_admin_dir_path( "dist/{$entry}/index.js" ) ),
|
|
|
|
true
|
2018-04-17 23:51:48 +00:00
|
|
|
);
|
|
|
|
|
2018-08-20 21:18:13 +00:00
|
|
|
// Set up the text domain and translations.
|
2018-11-27 00:18:36 +00:00
|
|
|
// The old way (pre WP5 RC1).
|
2018-11-05 02:14:26 +00:00
|
|
|
if ( function_exists( 'wp_get_jed_locale_data' ) ) {
|
2019-03-13 17:14:02 +00:00
|
|
|
$locale_data = wp_get_jed_locale_data( 'woocommerce-admin' );
|
2018-11-27 00:18:36 +00:00
|
|
|
} elseif ( function_exists( 'gutenberg_get_jed_locale_data' ) ) {
|
2019-03-13 17:14:02 +00:00
|
|
|
$locale_data = gutenberg_get_jed_locale_data( 'woocommerce-admin' );
|
2018-11-27 00:18:36 +00:00
|
|
|
} else {
|
|
|
|
$locale_data = '';
|
|
|
|
}
|
|
|
|
if ( ! empty( $locale_data ) ) {
|
2019-03-13 17:14:02 +00:00
|
|
|
$content = 'wp.i18n.setLocaleData( ' . wp_json_encode( $locale_data ) . ', "woocommerce-admin" );';
|
2018-11-27 00:18:36 +00:00
|
|
|
wp_add_inline_script( 'wp-i18n', $content, 'after' );
|
|
|
|
}
|
|
|
|
// The new way (WP5 RC1 and later).
|
|
|
|
if ( function_exists( 'wp_set_script_translations' ) ) {
|
2019-03-13 17:14:02 +00:00
|
|
|
wp_set_script_translations( WC_ADMIN_APP, 'woocommerce-admin' );
|
2018-11-05 02:14:26 +00:00
|
|
|
}
|
2018-05-03 14:15:36 +00:00
|
|
|
|
2018-10-30 18:57:48 +00:00
|
|
|
wp_register_style(
|
|
|
|
'wc-components',
|
|
|
|
wc_admin_url( 'dist/components/style.css' ),
|
|
|
|
array( 'wp-edit-blocks' ),
|
|
|
|
filemtime( wc_admin_dir_path( 'dist/components/style.css' ) )
|
|
|
|
);
|
2019-03-14 10:18:54 +00:00
|
|
|
wp_style_add_data( 'wc-components', 'rtl', 'replace' );
|
2018-10-30 18:57:48 +00:00
|
|
|
|
2018-12-28 17:58:03 +00:00
|
|
|
wp_register_style(
|
|
|
|
'wc-components-ie',
|
|
|
|
wc_admin_url( 'dist/components/ie.css' ),
|
|
|
|
array( 'wp-edit-blocks' ),
|
|
|
|
filemtime( wc_admin_dir_path( 'dist/components/ie.css' ) )
|
|
|
|
);
|
2019-03-14 10:18:54 +00:00
|
|
|
wp_style_add_data( 'wc-components-ie', 'rtl', 'replace' );
|
2018-12-28 17:58:03 +00:00
|
|
|
|
2018-10-30 18:57:48 +00:00
|
|
|
wp_register_style(
|
|
|
|
WC_ADMIN_APP,
|
|
|
|
wc_admin_url( "dist/{$entry}/style.css" ),
|
|
|
|
array( 'wc-components' ),
|
|
|
|
filemtime( wc_admin_dir_path( "dist/{$entry}/style.css" ) )
|
|
|
|
);
|
2019-03-14 10:18:54 +00:00
|
|
|
wp_style_add_data( WC_ADMIN_APP, 'rtl', 'replace' );
|
2018-10-30 18:57:48 +00:00
|
|
|
}
|
|
|
|
add_action( 'admin_enqueue_scripts', 'wc_admin_register_script' );
|
|
|
|
|
2018-11-28 02:58:17 +00:00
|
|
|
/**
|
|
|
|
* Format order statuses by removing a leading 'wc-' if present.
|
|
|
|
*
|
|
|
|
* @param array $statuses Order statuses.
|
|
|
|
* @return array formatted statuses.
|
|
|
|
*/
|
2019-03-11 07:10:41 +00:00
|
|
|
function wc_admin_format_order_statuses( $statuses ) {
|
2018-11-28 02:58:17 +00:00
|
|
|
$formatted_statuses = array();
|
|
|
|
foreach ( $statuses as $key => $value ) {
|
|
|
|
$formatted_key = preg_replace( '/^wc-/', '', $key );
|
|
|
|
$formatted_statuses[ $formatted_key ] = $value;
|
|
|
|
}
|
|
|
|
return $formatted_statuses;
|
|
|
|
}
|
|
|
|
|
2018-10-30 18:57:48 +00:00
|
|
|
/**
|
|
|
|
* Output the wcSettings global before printing any script tags.
|
|
|
|
*/
|
|
|
|
function wc_admin_print_script_settings() {
|
2018-12-10 23:05:45 +00:00
|
|
|
global $wp_locale;
|
|
|
|
|
2018-12-14 15:13:34 +00:00
|
|
|
$preload_data_endpoints = array(
|
2019-03-08 15:17:24 +00:00
|
|
|
'countries' => '/wc/v4/data/countries',
|
|
|
|
'performanceIndicators' => '/wc/v4/reports/performance-indicators/allowed',
|
2018-12-14 15:13:34 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if ( function_exists( 'gutenberg_preload_api_request' ) ) {
|
|
|
|
$preload_function = 'gutenberg_preload_api_request';
|
|
|
|
} else {
|
|
|
|
$preload_function = 'rest_preload_api_request';
|
|
|
|
}
|
|
|
|
|
|
|
|
$preload_data = array_reduce(
|
|
|
|
array_values( $preload_data_endpoints ),
|
|
|
|
$preload_function
|
|
|
|
);
|
|
|
|
|
2019-01-15 02:24:06 +00:00
|
|
|
$current_user_data = array();
|
|
|
|
foreach ( wc_admin_get_user_data_fields() as $user_field ) {
|
2019-01-17 14:08:59 +00:00
|
|
|
$current_user_data[ $user_field ] = json_decode( get_user_meta( get_current_user_id(), 'wc_admin_' . $user_field, true ) );
|
2019-01-15 02:24:06 +00:00
|
|
|
}
|
|
|
|
|
Add loading indicators, error state, and EmptyContent to the revenue report. (#347, woocommerce/woocommerce-admin#348)
* Add loading indiciators for the revenue report.
* Improve accessibility, and fix up some documentation comments.
* Fix top border on mobile
* Add EmptyContent Component and revenue error/empty states. (https://github.com/woocommerce/woocommerce-admin/pull/348)
* Add EmptyContent Component and revenue error/empty states.
* Move relative image handling to ImageAsset, combine secondary and primary action rendering, add some missing isRequired proptypes, add empty error handling.
* Handle PR Feedback: Clean up button css, set a default for illustration, fix deprecation typo, some code cleanup.
2018-09-05 16:45:49 +00:00
|
|
|
/**
|
2019-02-06 06:41:53 +00:00
|
|
|
* Settings and variables can be passed here for access in the app.
|
|
|
|
*
|
|
|
|
* @todo On merge, once plugin images are added to core WooCommerce, `wcAdminAssetUrl` can be retired, and
|
Add loading indicators, error state, and EmptyContent to the revenue report. (#347, woocommerce/woocommerce-admin#348)
* Add loading indiciators for the revenue report.
* Improve accessibility, and fix up some documentation comments.
* Fix top border on mobile
* Add EmptyContent Component and revenue error/empty states. (https://github.com/woocommerce/woocommerce-admin/pull/348)
* Add EmptyContent Component and revenue error/empty states.
* Move relative image handling to ImageAsset, combine secondary and primary action rendering, add some missing isRequired proptypes, add empty error handling.
* Handle PR Feedback: Clean up button css, set a default for illustration, fix deprecation typo, some code cleanup.
2018-09-05 16:45:49 +00:00
|
|
|
* `wcAssetUrl` can be used in its place throughout the codebase.
|
|
|
|
*/
|
2018-05-10 18:35:55 +00:00
|
|
|
$settings = array(
|
2019-03-04 04:08:13 +00:00
|
|
|
'adminUrl' => admin_url(),
|
|
|
|
'wcAssetUrl' => plugins_url( 'assets/', WC_PLUGIN_FILE ),
|
|
|
|
'wcAdminAssetUrl' => plugins_url( 'images/', wc_admin_dir_path( 'wc-admin.php' ) ), // Temporary for plugin. See above.
|
|
|
|
'embedBreadcrumbs' => wc_admin_get_embed_breadcrumbs(),
|
|
|
|
'siteLocale' => esc_attr( get_bloginfo( 'language' ) ),
|
|
|
|
'currency' => wc_admin_currency_settings(),
|
2019-03-11 07:10:41 +00:00
|
|
|
'orderStatuses' => wc_admin_format_order_statuses( wc_get_order_statuses() ),
|
2019-03-04 04:08:13 +00:00
|
|
|
'stockStatuses' => wc_get_product_stock_status_options(),
|
|
|
|
'siteTitle' => get_bloginfo( 'name' ),
|
|
|
|
'dataEndpoints' => array(),
|
|
|
|
'l10n' => array(
|
2018-12-10 23:05:45 +00:00
|
|
|
'userLocale' => get_user_locale(),
|
|
|
|
'weekdaysShort' => array_values( $wp_locale->weekday_abbrev ),
|
|
|
|
),
|
2019-03-06 14:58:36 +00:00
|
|
|
'currentUserData' => $current_user_data,
|
2019-03-12 13:13:20 +00:00
|
|
|
'alertCount' => WC_Admin_Notes::get_notes_count( 'error,update', 'unactioned' ),
|
2018-05-10 18:35:55 +00:00
|
|
|
);
|
2019-01-31 01:04:11 +00:00
|
|
|
$settings = wc_admin_add_custom_settings( $settings );
|
2018-12-14 15:13:34 +00:00
|
|
|
|
|
|
|
foreach ( $preload_data_endpoints as $key => $endpoint ) {
|
|
|
|
$settings['dataEndpoints'][ $key ] = $preload_data[ $endpoint ]['body'];
|
|
|
|
}
|
2019-01-31 01:04:11 +00:00
|
|
|
|
|
|
|
$settings = apply_filters( 'wc_admin_wc_settings', $settings );
|
2018-10-30 18:57:48 +00:00
|
|
|
?>
|
|
|
|
<script type="text/javascript">
|
2018-11-28 02:58:17 +00:00
|
|
|
var wcSettings = <?php echo wp_json_encode( $settings ); ?>;
|
2018-10-30 18:57:48 +00:00
|
|
|
</script>
|
|
|
|
<?php
|
2018-04-17 23:51:48 +00:00
|
|
|
}
|
2018-10-30 18:57:48 +00:00
|
|
|
add_action( 'admin_print_footer_scripts', 'wc_admin_print_script_settings', 1 );
|
2018-04-17 23:51:48 +00:00
|
|
|
|
2019-01-31 01:04:11 +00:00
|
|
|
/**
|
|
|
|
* Add in custom settings used for WC Admin.
|
|
|
|
*
|
|
|
|
* @param array $settings Array of settings to merge into.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function wc_admin_add_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 ) {
|
|
|
|
$settings['wcAdminSettings'][ $setting['id'] ] = $setting['value'];
|
|
|
|
}
|
|
|
|
return $settings;
|
|
|
|
}
|
|
|
|
|
2018-04-17 23:51:48 +00:00
|
|
|
/**
|
|
|
|
* Load plugin text domain for translations.
|
|
|
|
*/
|
2018-07-10 12:48:06 +00:00
|
|
|
function wc_admin_load_plugin_textdomain() {
|
2019-03-13 17:14:02 +00:00
|
|
|
load_plugin_textdomain( 'woocommerce-admin', false, basename( dirname( __FILE__ ) ) . '/languages' );
|
2018-04-17 23:51:48 +00:00
|
|
|
}
|
2018-07-10 12:48:06 +00:00
|
|
|
add_action( 'plugins_loaded', 'wc_admin_load_plugin_textdomain' );
|