Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 159 additions & 2 deletions inc/class-scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,27 @@ public function register_default_styles(): void {
*/
public function enqueue_default_admin_styles(): void {

wp_enqueue_style('wu-admin');
// Get current screen to conditionally load styles
$screen = get_current_screen();

if (!$screen) {
return;
}

// Only load styles on WP Ultimo admin pages
if ($this->is_wp_ultimo_admin_page($screen)) {
wp_enqueue_style('wu-admin');

// Load flag styles only when needed (e.g., on pages with country selection)
if ($this->is_wp_ultimo_flags_page($screen)) {
wp_enqueue_style('wu-flags');
}
}

// Load checkout styles only on checkout pages
if (isset($_GET['page']) && strpos($_GET['page'], 'wu-checkout') === 0) {
wp_enqueue_style('wu-checkout');
}
}

/**
Expand All @@ -346,7 +366,32 @@ public function enqueue_default_admin_styles(): void {
*/
public function enqueue_default_admin_scripts(): void {

wp_enqueue_script('wu-admin');
// Get current screen to conditionally load scripts
$screen = get_current_screen();

if (!$screen) {
return;
}

// Base admin script for all WP Ultimo admin pages
if ($this->is_wp_ultimo_admin_page($screen)) {
wp_enqueue_script('wu-admin');
}

// Only load these scripts on specific pages where they're needed
if ($this->is_wp_ultimo_list_page($screen)) {
wp_enqueue_script('wu-ajax-list-table');
}

// Load Vue apps only on pages that need them
if ($this->is_wp_ultimo_vue_page($screen)) {
wp_enqueue_script('wu-vue-apps');
}

// Load wubox only when needed
if ($this->is_wp_ultimo_modal_page($screen)) {
wp_enqueue_script('wubox');
}
}

/**
Expand Down Expand Up @@ -382,4 +427,116 @@ public function add_body_class_container_boxed($classes) {

return $classes;
}

/**
* Checks if the current screen is a WP Ultimo admin page.
*
* @since 2.0.0
*
* @param \WP_Screen $screen The current screen object.
* @return bool
*/
private function is_wp_ultimo_admin_page($screen) {
// Check if the screen ID contains 'wp-ultimo' or if it's a WP Ultimo admin page
return (
strpos($screen->id, 'wp-ultimo') !== false ||
strpos($screen->id, 'wu-') !== false ||
(isset($_GET['page']) && strpos($_GET['page'], 'wu-') === 0)
);
}

/**
* Checks if the current screen is a WP Ultimo list page that needs the list table scripts.
*
* @since 2.0.0
*
* @param \WP_Screen $screen The current screen object.
* @return bool
*/
private function is_wp_ultimo_list_page($screen) {
// Pages that use list tables
$list_pages = array(
'wu-memberships',
'wu-products',
'wu-customers',
'wu-payments',
'wu-discount-codes',
'wu-domain-mappings',
'wu-sites',
);

return $this->is_wp_ultimo_admin_page($screen) &&
isset($_GET['page']) &&
in_array($_GET['page'], $list_pages);
}

/**
* Checks if the current screen is a WP Ultimo page that needs Vue.js.
*
* @since 2.0.0
*
* @param \WP_Screen $screen The current screen object.
* @return bool
*/
private function is_wp_ultimo_vue_page($screen) {
// Pages that use Vue.js
$vue_pages = array(
'wu-settings',
'wu-add-membership',
'wu-edit-membership',
'wu-add-product',
'wu-edit-product',
'wu-add-customer',
'wu-edit-customer',
'wu-add-payment',
'wu-edit-payment',
'wu-add-discount-code',
'wu-edit-discount-code',
'wu-add-domain-mapping',
'wu-edit-domain-mapping',
'wu-add-site',
'wu-edit-site',
'wu-checkout-form',
);

return $this->is_wp_ultimo_admin_page($screen) &&
isset($_GET['page']) &&
(in_array($_GET['page'], $vue_pages) || strpos($_GET['page'], 'wu-checkout') === 0);
}

/**
* Checks if the current screen is a WP Ultimo page that needs modal functionality.
*
* @since 2.0.0
*
* @param \WP_Screen $screen The current screen object.
* @return bool
*/
private function is_wp_ultimo_modal_page($screen) {
// Most WP Ultimo admin pages need the modal functionality
return $this->is_wp_ultimo_admin_page($screen);
}

/**
* Checks if the current screen is a WP Ultimo page that needs country flags.
*
* @since 2.0.0
*
* @param \WP_Screen $screen The current screen object.
* @return bool
*/
private function is_wp_ultimo_flags_page($screen) {
// Pages that use country flags
$flags_pages = array(
'wu-settings',
'wu-add-customer',
'wu-edit-customer',
'wu-add-payment',
'wu-edit-payment',
);

return $this->is_wp_ultimo_admin_page($screen) &&
isset($_GET['page']) &&
in_array($_GET['page'], $flags_pages);
}
}
11 changes: 10 additions & 1 deletion inc/functions/assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@
function wu_get_asset($asset, $assets_dir = 'img', $base_dir = 'assets') {

if ( ! defined('SCRIPT_DEBUG') || ! SCRIPT_DEBUG) {
$asset = preg_replace('/(?<!\.min)(\.js|\.css)/', '.min$1', $asset);
// Create the minified filename
$minified_asset = preg_replace('/(?<!\.min)(\.js|\.css)/', '.min$1', $asset);

// Check if the minified file exists
$minified_path = WP_ULTIMO_PLUGIN_DIR . "$base_dir/$assets_dir/$minified_asset";

// Only use the minified version if it exists
if (file_exists($minified_path)) {
$asset = $minified_asset;
}
}

return wu_url("$base_dir/$assets_dir/$asset");
Expand Down