| 1 |
<?php |
| 2 |
/** |
| 3 |
* Registers and enqueues admin/frontend scripts and styles. |
| 4 |
* |
| 5 |
* @package SpringDevs\Subscription |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace SpringDevs\Subscription; |
| 9 |
|
| 10 |
/** |
| 11 |
* Scripts and Styles Class |
| 12 |
*/ |
| 13 |
class Assets { |
| 14 |
|
| 15 |
/** |
| 16 |
* Assets constructor. |
| 17 |
*/ |
| 18 |
public function __construct() { |
| 19 |
if ( is_admin() ) { |
| 20 |
add_action( 'admin_enqueue_scripts', array( $this, 'register' ), 5 ); |
| 21 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_components' ), 6 ); |
| 22 |
} else { |
| 23 |
add_action( 'wp_enqueue_scripts', array( $this, 'register' ), 5 ); |
| 24 |
} |
| 25 |
|
| 26 |
// Development only: see version_asset_src(). |
| 27 |
if ( false !== strpos( SUBSCRPT_VERSION, '#WPSUBS' ) ) { |
| 28 |
add_filter( 'script_loader_src', array( $this, 'version_asset_src' ) ); |
| 29 |
add_filter( 'style_loader_src', array( $this, 'version_asset_src' ) ); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Register our app scripts and styles |
| 35 |
* |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
public function register() { |
| 39 |
$this->register_scripts( $this->get_scripts() ); |
| 40 |
$this->register_styles( $this->get_styles() ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Give this plugin's assets a version that actually changes in development. |
| 45 |
* |
| 46 |
* `SUBSCRPT_VERSION` is a build-time placeholder in a git checkout — |
| 47 |
* release.sh substitutes the real number when packaging — so every asset is |
| 48 |
* served under the same, never-changing version string and the browser |
| 49 |
* keeps its cached copy through edit after edit. That has cost real |
| 50 |
* debugging time: a fix already on disk and already being served, appearing |
| 51 |
* not to work. |
| 52 |
* |
| 53 |
* Filtering the loader rather than each registration catches the enqueues |
| 54 |
* that call wp_enqueue_script() directly with a version of their own, which |
| 55 |
* is most of the admin screens. On a release build the placeholder is gone |
| 56 |
* and this returns the src untouched. |
| 57 |
* |
| 58 |
* @param string $src Asset URL, with or without a `ver` argument. |
| 59 |
* @return string |
| 60 |
*/ |
| 61 |
public function version_asset_src( $src ) { |
| 62 |
if ( false === strpos( SUBSCRPT_VERSION, '#WPSUBS' ) ) { |
| 63 |
return $src; |
| 64 |
} |
| 65 |
|
| 66 |
if ( ! is_string( $src ) || 0 !== strpos( $src, SUBSCRPT_ASSETS ) ) { |
| 67 |
return $src; |
| 68 |
} |
| 69 |
|
| 70 |
$path = SUBSCRPT_PATH . '/assets' . strtok( substr( $src, strlen( SUBSCRPT_ASSETS ) ), '?' ); |
| 71 |
|
| 72 |
if ( ! file_exists( $path ) ) { |
| 73 |
return $src; |
| 74 |
} |
| 75 |
|
| 76 |
return add_query_arg( 'ver', (string) filemtime( $path ), $src ); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Register scripts |
| 81 |
* |
| 82 |
* @param array $scripts scripts. |
| 83 |
* |
| 84 |
* @return void |
| 85 |
*/ |
| 86 |
private function register_scripts( $scripts ) { |
| 87 |
foreach ( $scripts as $handle => $script ) { |
| 88 |
$deps = isset( $script['deps'] ) ? $script['deps'] : false; |
| 89 |
$in_footer = isset( $script['in_footer'] ) ? $script['in_footer'] : false; |
| 90 |
$version = isset( $script['version'] ) ? $script['version'] : SUBSCRPT_VERSION; |
| 91 |
|
| 92 |
wp_register_script( $handle, $script['src'], $deps, $version, $in_footer ); |
| 93 |
|
| 94 |
// Register script translations for scripts that use wp-i18n (e.g., block assets). |
| 95 |
if ( function_exists( 'wp_set_script_translations' ) && 'sdevs_subscrpt_cart_block' === $handle ) { |
| 96 |
wp_set_script_translations( $handle, 'subscription', SUBSCRPT_PATH . '/languages' ); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Register styles |
| 103 |
* |
| 104 |
* @param array $styles styles. |
| 105 |
* |
| 106 |
* @return void |
| 107 |
*/ |
| 108 |
public function register_styles( $styles ) { |
| 109 |
foreach ( $styles as $handle => $style ) { |
| 110 |
$deps = isset( $style['deps'] ) ? $style['deps'] : false; |
| 111 |
|
| 112 |
wp_register_style( $handle, $style['src'], $deps, SUBSCRPT_VERSION ); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Get all registered scripts |
| 118 |
* |
| 119 |
* @return array |
| 120 |
*/ |
| 121 |
public function get_scripts() { |
| 122 |
$plugin_js_assets_path = SUBSCRPT_ASSETS . '/js/'; |
| 123 |
|
| 124 |
$block_script_asset_path = SUBSCRPT_PATH . '/build/index.asset.php'; |
| 125 |
$block_script_asset = file_exists( $block_script_asset_path ) |
| 126 |
? require $block_script_asset_path |
| 127 |
: array( |
| 128 |
'dependencies' => false, |
| 129 |
'version' => SUBSCRPT_VERSION, |
| 130 |
); |
| 131 |
|
| 132 |
// Admin UI components, one file each for readability (assets/js/admin-components/). |
| 133 |
// Each attaches its API to `window` and auto-inits; they are registered as |
| 134 |
// individual scripts and bundled behind the `subscrpt_admin_components` handle. |
| 135 |
$components = array(); |
| 136 |
$component_files = array( 'adv-select', 'tag-select', 'editlist', 'modal', 'tabs', 'accordion', 'pagination', 'toast', 'save' ); |
| 137 |
foreach ( $component_files as $component_file ) { |
| 138 |
$handle = 'subscrpt_component_' . str_replace( '-', '_', $component_file ); |
| 139 |
$components[ $handle ] = array( |
| 140 |
'src' => $plugin_js_assets_path . 'admin-components/' . $component_file . '.js', |
| 141 |
'deps' => array(), |
| 142 |
'in_footer' => true, |
| 143 |
); |
| 144 |
} |
| 145 |
|
| 146 |
$scripts = array( |
| 147 |
'sdevs_subscription_admin' => array( |
| 148 |
'src' => $plugin_js_assets_path . 'admin.js', |
| 149 |
'deps' => array( 'jquery' ), |
| 150 |
'in_footer' => true, |
| 151 |
), |
| 152 |
'subscrpt_admin_components' => array( |
| 153 |
'src' => false, |
| 154 |
'deps' => array_keys( $components ), |
| 155 |
'in_footer' => true, |
| 156 |
), |
| 157 |
'subscrpt_plan_forms_js' => array( |
| 158 |
// Shared plan-group + selling-plan modal logic, used by both the |
| 159 |
// Plans admin screen and the product-editor Subscription tab. |
| 160 |
'src' => $plugin_js_assets_path . 'admin/plan-forms.js', |
| 161 |
'deps' => array( 'subscrpt_admin_components' ), |
| 162 |
'in_footer' => true, |
| 163 |
), |
| 164 |
'sdevs_installer' => array( |
| 165 |
'src' => $plugin_js_assets_path . 'installer.js', |
| 166 |
'deps' => array( 'jquery' ), |
| 167 |
'in_footer' => true, |
| 168 |
), |
| 169 |
'sdevs_subscrpt_cart_block' => array( |
| 170 |
'src' => SUBSCRPT_URL . '/build/index.js', |
| 171 |
'deps' => $block_script_asset['dependencies'], |
| 172 |
'version' => $block_script_asset['version'], |
| 173 |
'in_footer' => true, |
| 174 |
), |
| 175 |
); |
| 176 |
|
| 177 |
return array_merge( $components, $scripts ); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Get registered styles |
| 182 |
* |
| 183 |
* @return array |
| 184 |
*/ |
| 185 |
public function get_styles() { |
| 186 |
$plugin_css_assets_path = SUBSCRPT_ASSETS . '/css/'; |
| 187 |
|
| 188 |
// Admin UI component styles, split by section for readability |
| 189 |
// (assets/css/admin-components/). Loaded in this order behind the |
| 190 |
// `subscrpt_admin_components` handle; `tokens` must stay first. |
| 191 |
$component_styles = array(); |
| 192 |
$component_files = array( |
| 193 |
'tokens', |
| 194 |
'layout', |
| 195 |
'page-header', |
| 196 |
'forms', |
| 197 |
'buttons', |
| 198 |
'select', |
| 199 |
'table', |
| 200 |
'badges', |
| 201 |
'dropdown', |
| 202 |
'pagination', |
| 203 |
'empty', |
| 204 |
'toggle', |
| 205 |
'settings', |
| 206 |
'tag-select', |
| 207 |
'modal', |
| 208 |
'editlist', |
| 209 |
'tabs', |
| 210 |
'vnav', |
| 211 |
'accordion', |
| 212 |
'tooltip', |
| 213 |
'toast', |
| 214 |
); |
| 215 |
foreach ( $component_files as $component_file ) { |
| 216 |
$handle = 'subscrpt_style_' . str_replace( '-', '_', $component_file ); |
| 217 |
$component_styles[ $handle ] = array( |
| 218 |
'src' => $plugin_css_assets_path . 'admin-components/' . $component_file . '.css', |
| 219 |
); |
| 220 |
} |
| 221 |
|
| 222 |
$styles = array( |
| 223 |
'subscrpt_admin_css' => array( |
| 224 |
'src' => $plugin_css_assets_path . 'admin.css', |
| 225 |
), |
| 226 |
'subscrpt_admin_components' => array( |
| 227 |
'src' => false, |
| 228 |
'deps' => array_keys( $component_styles ), |
| 229 |
), |
| 230 |
'subscrpt_status_css' => array( |
| 231 |
'src' => $plugin_css_assets_path . 'status.css', |
| 232 |
), |
| 233 |
'sdevs_installer' => array( |
| 234 |
'src' => $plugin_css_assets_path . 'installer.css', |
| 235 |
), |
| 236 |
); |
| 237 |
|
| 238 |
return array_merge( $component_styles, $styles ); |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Enqueue admin component styles on all WPSubscription admin pages. |
| 243 |
* |
| 244 |
* @param string $hook The current admin page hook. |
| 245 |
*/ |
| 246 |
public function enqueue_admin_components( $hook ) { |
| 247 |
$is_main_page = 'toplevel_page_wp-subscription' === $hook; |
| 248 |
$is_subs_page = str_starts_with( $hook, 'wpsubscription_page' ) || str_starts_with( $hook, 'wp-subscription_page' ); |
| 249 |
|
| 250 |
if ( $is_main_page || $is_subs_page ) { |
| 251 |
wp_enqueue_style( 'subscrpt_admin_components' ); |
| 252 |
wp_enqueue_script( 'subscrpt_admin_components' ); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Enqueue chart.js where this method is called. |
| 258 |
* |
| 259 |
* Source: https://cdn.jsdelivr.net/npm/chart.js |
| 260 |
*/ |
| 261 |
public static function enqueue_chart_js() { |
| 262 |
wp_enqueue_script( 'subscrpt-ext-lib-chartjs', SUBSCRPT_ASSETS . '/js/chart.js', [], SUBSCRPT_VERSION, true ); |
| 263 |
} |
| 264 |
} |
| 265 |
|