| @@ -1,5 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * Registers and enqueues admin/frontend scripts and styles. | |
| 4 | + * | |
| 5 | + * @package SpringDevs\Subscription | |
| 6 | + */ | |
| 2 | 7 | |
| 3 | 8 | namespace SpringDevs\Subscription; |
| 4 | 9 | |
| 5 | 10 | /** |
| @@ -16,8 +21,14 @@ | ||
| 16 | 21 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_components' ), 6 ); |
| 17 | 22 | } else { |
| 18 | 23 | add_action( 'wp_enqueue_scripts', array( $this, 'register' ), 5 ); |
| 19 | 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 | + } | |
| 20 | 31 | } |
| 21 | 32 | |
| 22 | 33 | /** |
| 23 | 34 | * Register our app scripts and styles |
| @@ -29,8 +40,44 @@ | ||
| 29 | 40 | $this->register_styles( $this->get_styles() ); |
| 30 | 41 | } |
| 31 | 42 | |
| 32 | 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 | + /** | |
| 33 | 80 | * Register scripts |
| 34 | 81 | * |
| 35 | 82 | * @param array $scripts scripts. |
| 36 | 83 | * |
| @@ -81,8 +128,22 @@ | ||
| 81 | 128 | 'dependencies' => false, |
| 82 | 129 | 'version' => SUBSCRPT_VERSION, |
| 83 | 130 | ); |
| 84 | 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 | + | |
| 85 | 146 | $scripts = array( |
| 86 | 147 | 'sdevs_subscription_admin' => array( |
| 87 | 148 | 'src' => $plugin_js_assets_path . 'admin.js', |
| 88 | 149 | 'deps' => array( 'jquery' ), |
| @@ -88,12 +149,19 @@ | ||
| 88 | 149 | 'deps' => array( 'jquery' ), |
| 89 | 150 | 'in_footer' => true, |
| 90 | 151 | ), |
| 91 | 152 | 'subscrpt_admin_components' => array( |
| 92 | - 'src' => $plugin_js_assets_path . 'admin-components.js', | |
| 93 | - 'deps' => array( 'wp-i18n' ), | |
| 153 | + 'src' => false, | |
| 154 | + 'deps' => array_keys( $components ), | |
| 94 | 155 | 'in_footer' => true, |
| 95 | 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 | + ), | |
| 96 | 164 | 'sdevs_installer' => array( |
| 97 | 165 | 'src' => $plugin_js_assets_path . 'installer.js', |
| 98 | 166 | 'deps' => array( 'jquery' ), |
| 99 | 167 | 'in_footer' => true, |
| @@ -105,9 +173,9 @@ | ||
| 105 | 173 | 'in_footer' => true, |
| 106 | 174 | ), |
| 107 | 175 | ); |
| 108 | 176 | |
| 109 | - return $scripts; | |
| 177 | + return array_merge( $components, $scripts ); | |
| 110 | 178 | } |
| 111 | 179 | |
| 112 | 180 | /** |
| 113 | 181 | * Get registered styles |
| @@ -116,14 +184,49 @@ | ||
| 116 | 184 | */ |
| 117 | 185 | public function get_styles() { |
| 118 | 186 | $plugin_css_assets_path = SUBSCRPT_ASSETS . '/css/'; |
| 119 | 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 | + | |
| 120 | 222 | $styles = array( |
| 121 | 223 | 'subscrpt_admin_css' => array( |
| 122 | 224 | 'src' => $plugin_css_assets_path . 'admin.css', |
| 123 | 225 | ), |
| 124 | 226 | 'subscrpt_admin_components' => array( |
| 125 | - 'src' => $plugin_css_assets_path . 'admin-components.css', | |
| 227 | + 'src' => false, | |
| 228 | + 'deps' => array_keys( $component_styles ), | |
| 126 | 229 | ), |
| 127 | 230 | 'subscrpt_status_css' => array( |
| 128 | 231 | 'src' => $plugin_css_assets_path . 'status.css', |
| 129 | 232 | ), |
| @@ -131,9 +234,9 @@ | ||
| 131 | 234 | 'src' => $plugin_css_assets_path . 'installer.css', |
| 132 | 235 | ), |
| 133 | 236 | ); |
| 134 | 237 | |
| 135 | - return $styles; | |
| 238 | + return array_merge( $component_styles, $styles ); | |
| 136 | 239 | } |
| 137 | 240 | |
| 138 | 241 | /** |
| 139 | 242 | * Enqueue admin component styles on all WPSubscription admin pages. |