Api.php
363 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\Blocks\Assets; |
| 3 | |
| 4 | use Automattic\WooCommerce\Blocks\Domain\Package; |
| 5 | use Exception; |
| 6 | use Automattic\Jetpack\Constants; |
| 7 | /** |
| 8 | * The Api class provides an interface to various asset registration helpers. |
| 9 | * |
| 10 | * Contains asset api methods |
| 11 | * |
| 12 | * @since 2.5.0 |
| 13 | */ |
| 14 | class Api { |
| 15 | |
| 16 | /** |
| 17 | * Stores the prefixed WC version. Used because the WC Blocks version has not been updated since the monorepo merge. |
| 18 | * |
| 19 | * @var string |
| 20 | */ |
| 21 | public $wc_version; |
| 22 | |
| 23 | /** |
| 24 | * Stores inline scripts already enqueued. |
| 25 | * |
| 26 | * @var array |
| 27 | */ |
| 28 | private $inline_scripts = []; |
| 29 | |
| 30 | /** |
| 31 | * Determines if caching is enabled for script data. |
| 32 | * |
| 33 | * @var boolean |
| 34 | */ |
| 35 | private $disable_cache = false; |
| 36 | |
| 37 | /** |
| 38 | * Stores loaded script data for the current request |
| 39 | * |
| 40 | * @var array|null |
| 41 | */ |
| 42 | private $script_data = null; |
| 43 | |
| 44 | /** |
| 45 | * Tracks whether script_data was modified during the current request. |
| 46 | * |
| 47 | * @var boolean |
| 48 | */ |
| 49 | private $script_data_modified = false; |
| 50 | |
| 51 | /** |
| 52 | * Stores the hash for the script data, made up of the site url, plugin version and package path. |
| 53 | * |
| 54 | * @var string |
| 55 | */ |
| 56 | private $script_data_hash; |
| 57 | |
| 58 | /** |
| 59 | * Stores the transient key used to cache the script data. This will change if the site is accessed via HTTPS or HTTP. |
| 60 | * |
| 61 | * @var string |
| 62 | */ |
| 63 | private $script_data_transient_key = 'woocommerce_blocks_asset_api_script_data'; |
| 64 | |
| 65 | /** |
| 66 | * Reference to the Package instance |
| 67 | * |
| 68 | * @var Package |
| 69 | */ |
| 70 | private $package; |
| 71 | |
| 72 | /** |
| 73 | * Constructor for class |
| 74 | * |
| 75 | * @param Package $package An instance of Package. |
| 76 | */ |
| 77 | public function __construct( Package $package ) { |
| 78 | // Use wc- prefix here to prevent collisions when WC Core version catches up to a version previously used by the WC Blocks feature plugin. |
| 79 | $this->wc_version = 'wc-' . Constants::get_constant( 'WC_VERSION' ); |
| 80 | $this->package = $package; |
| 81 | $this->disable_cache = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) || wp_get_environment_type() !== 'production'; |
| 82 | |
| 83 | // If the site is accessed via HTTPS, change the transient key. This is to prevent the script URLs being cached |
| 84 | // with the first scheme they are accessed on after cache expiry. |
| 85 | if ( is_ssl() ) { |
| 86 | $this->script_data_transient_key .= '_ssl'; |
| 87 | } |
| 88 | if ( ! $this->disable_cache ) { |
| 89 | $this->script_data_hash = $this->get_script_data_hash(); |
| 90 | } |
| 91 | add_action( 'shutdown', array( $this, 'update_script_data_cache' ), 20 ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Get the file modified time as a cache buster if we're in dev mode. |
| 96 | * |
| 97 | * @param string $file Local path to the file (relative to the plugin |
| 98 | * directory). |
| 99 | * @return string The cache buster value to use for the given file. |
| 100 | */ |
| 101 | protected function get_file_version( $file ) { |
| 102 | if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && file_exists( $this->package->get_path() . $file ) ) { |
| 103 | return filemtime( $this->package->get_path( trim( $file, '/' ) ) ); |
| 104 | } |
| 105 | return $this->wc_version; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Retrieve the url to an asset for this plugin. |
| 110 | * |
| 111 | * @param string $relative_path An optional relative path appended to the |
| 112 | * returned url. |
| 113 | * |
| 114 | * @return string |
| 115 | */ |
| 116 | protected function get_asset_url( $relative_path = '' ) { |
| 117 | return $this->package->get_url( $relative_path ); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Get the path to a block's metadata |
| 122 | * |
| 123 | * @param string $block_name The block to get metadata for. |
| 124 | * @param string $path Optional. The path to the metadata file inside the 'assets/client/blocks' folder. |
| 125 | * |
| 126 | * @return string|boolean False if metadata file is not found for the block. |
| 127 | */ |
| 128 | public function get_block_metadata_path( $block_name, $path = '' ) { |
| 129 | $path_to_metadata_from_plugin_root = $this->package->get_path( 'assets/client/blocks/' . $path . $block_name . '/block.json' ); |
| 130 | if ( ! file_exists( $path_to_metadata_from_plugin_root ) ) { |
| 131 | return false; |
| 132 | } |
| 133 | return $path_to_metadata_from_plugin_root; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Generates a hash containing the site url, plugin version and package path. |
| 138 | * |
| 139 | * Moving the plugin, changing the version, or changing the site url will result in a new hash and the cache will be invalidated. |
| 140 | * |
| 141 | * @return string The generated hash. |
| 142 | */ |
| 143 | private function get_script_data_hash() { |
| 144 | return md5( get_option( 'siteurl', '' ) . $this->wc_version . $this->package->get_path() ); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Initialize and load cached script data from the transient cache. |
| 149 | * |
| 150 | * @return array |
| 151 | */ |
| 152 | private function get_cached_script_data() { |
| 153 | if ( $this->disable_cache ) { |
| 154 | return []; |
| 155 | } |
| 156 | |
| 157 | $transient_value = json_decode( (string) get_transient( $this->script_data_transient_key ), true ); |
| 158 | |
| 159 | if ( |
| 160 | json_last_error() !== JSON_ERROR_NONE || |
| 161 | empty( $transient_value ) || |
| 162 | empty( $transient_value['script_data'] ) || |
| 163 | empty( $transient_value['version'] ) || |
| 164 | $transient_value['version'] !== $this->wc_version || |
| 165 | empty( $transient_value['hash'] ) || |
| 166 | $transient_value['hash'] !== $this->script_data_hash |
| 167 | ) { |
| 168 | return []; |
| 169 | } |
| 170 | |
| 171 | return (array) ( $transient_value['script_data'] ?? [] ); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Store all cached script data in the transient cache. |
| 176 | */ |
| 177 | public function update_script_data_cache() { |
| 178 | if ( is_null( $this->script_data ) || $this->disable_cache ) { |
| 179 | return; |
| 180 | } |
| 181 | if ( ! $this->script_data_modified ) { |
| 182 | return; |
| 183 | } |
| 184 | set_transient( |
| 185 | $this->script_data_transient_key, |
| 186 | wp_json_encode( |
| 187 | array( |
| 188 | 'script_data' => $this->script_data, |
| 189 | 'version' => $this->wc_version, |
| 190 | 'hash' => $this->script_data_hash, |
| 191 | ) |
| 192 | ), |
| 193 | DAY_IN_SECONDS * 30 |
| 194 | ); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Use package path to find an asset data file and return the data. |
| 199 | * |
| 200 | * @param string $filename The filename of the asset. |
| 201 | * @return array The asset data. |
| 202 | */ |
| 203 | public function get_asset_data( $filename ) { |
| 204 | $asset_path = $this->package->get_path( $filename ); |
| 205 | $asset = file_exists( $asset_path ) ? require $asset_path : []; |
| 206 | return $asset; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Get src, version and dependencies given a script relative src. |
| 211 | * |
| 212 | * @param string $relative_src Relative src to the script. |
| 213 | * @param array $dependencies Optional. An array of registered script handles this script depends on. Default empty array. |
| 214 | * |
| 215 | * @return array src, version and dependencies of the script. |
| 216 | */ |
| 217 | public function get_script_data( $relative_src, $dependencies = [] ) { |
| 218 | if ( ! $relative_src ) { |
| 219 | return array( |
| 220 | 'src' => '', |
| 221 | 'version' => '1', |
| 222 | 'dependencies' => $dependencies, |
| 223 | ); |
| 224 | } |
| 225 | |
| 226 | if ( is_null( $this->script_data ) ) { |
| 227 | $this->script_data = $this->get_cached_script_data(); |
| 228 | } |
| 229 | |
| 230 | if ( empty( $this->script_data[ $relative_src ] ) ) { |
| 231 | $asset_path = $this->package->get_path( str_replace( '.js', '.asset.php', $relative_src ) ); |
| 232 | // The following require is safe because we are checking if the file exists and it is not a user input. |
| 233 | // nosemgrep audit.php.lang.security.file.inclusion-arg. |
| 234 | $asset = file_exists( $asset_path ) ? require $asset_path : []; |
| 235 | |
| 236 | $this->script_data[ $relative_src ] = array( |
| 237 | 'src' => $this->get_asset_url( $relative_src ), |
| 238 | 'version' => ! empty( $asset['version'] ) ? $asset['version'] : $this->get_file_version( $relative_src ), |
| 239 | 'dependencies' => ! empty( $asset['dependencies'] ) ? $asset['dependencies'] : [], |
| 240 | ); |
| 241 | $this->script_data_modified = true; |
| 242 | } |
| 243 | |
| 244 | // Return asset details as well as the requested dependencies array. |
| 245 | return [ |
| 246 | 'src' => $this->script_data[ $relative_src ]['src'], |
| 247 | 'version' => $this->script_data[ $relative_src ]['version'], |
| 248 | 'dependencies' => array_merge( $this->script_data[ $relative_src ]['dependencies'], $dependencies ), |
| 249 | ]; |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Registers a script according to `wp_register_script`, adding the correct prefix, and additionally loading translations. |
| 254 | * |
| 255 | * When creating script assets, the following rules should be followed: |
| 256 | * 1. All asset handles should have a `wc-` prefix. |
| 257 | * 2. If the asset handle is for a Block (in editor context) use the `-block` suffix. |
| 258 | * 3. If the asset handle is for a Block (in frontend context) use the `-block-frontend` suffix. |
| 259 | * 4. If the asset is for any other script being consumed or enqueued by the blocks plugin, use the `wc-blocks-` prefix. |
| 260 | * |
| 261 | * @since 2.5.0 |
| 262 | * @throws Exception If the registered script has a dependency on itself. |
| 263 | * |
| 264 | * @param string $handle Unique name of the script. |
| 265 | * @param string $relative_src Relative url for the script to the path from plugin root. |
| 266 | * @param array $dependencies Optional. An array of registered script handles this script depends on. Default empty array. |
| 267 | * @param bool $has_i18n Optional. Whether to add a script translation call to this file. Default: true. |
| 268 | */ |
| 269 | public function register_script( $handle, $relative_src, $dependencies = [], $has_i18n = true ) { |
| 270 | $script_data = $this->get_script_data( $relative_src, $dependencies ); |
| 271 | |
| 272 | if ( in_array( $handle, $script_data['dependencies'], true ) ) { |
| 273 | if ( wp_get_environment_type() === 'development' ) { |
| 274 | $dependencies = array_diff( $script_data['dependencies'], [ $handle ] ); |
| 275 | add_action( |
| 276 | 'admin_notices', |
| 277 | function () use ( $handle ) { |
| 278 | echo '<div class="error"><p>'; |
| 279 | /* translators: %s file handle name. */ |
| 280 | printf( esc_html__( 'Script with handle %s had a dependency on itself which has been removed. This is an indicator that your JS code has a circular dependency that can cause bugs.', 'woocommerce' ), esc_html( $handle ) ); |
| 281 | echo '</p></div>'; |
| 282 | } |
| 283 | ); |
| 284 | } else { |
| 285 | throw new Exception( sprintf( 'Script with handle %s had a dependency on itself. This is an indicator that your JS code has a circular dependency that can cause bugs.', $handle ) ); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Filters the list of script dependencies. |
| 291 | * |
| 292 | * @since 3.0.0 |
| 293 | * |
| 294 | * @param array $dependencies The list of script dependencies. |
| 295 | * @param string $handle The script's handle. |
| 296 | * @return array |
| 297 | */ |
| 298 | $script_dependencies = apply_filters( 'woocommerce_blocks_register_script_dependencies', $script_data['dependencies'], $handle ); |
| 299 | |
| 300 | wp_register_script( $handle, $script_data['src'], $script_dependencies, $script_data['version'], true ); |
| 301 | |
| 302 | if ( $has_i18n && function_exists( 'wp_set_script_translations' ) ) { |
| 303 | wp_set_script_translations( $handle, 'woocommerce', $this->package->get_path( 'languages' ) ); |
| 304 | wp_set_script_translations( $handle, 'woocommerce', $this->package->get_path( 'i18n/languages' ) ); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Registers a style according to `wp_register_style`. |
| 310 | * |
| 311 | * @since 2.5.0 |
| 312 | * @since 2.6.0 Change src to be relative source. |
| 313 | * |
| 314 | * @param string $handle Name of the stylesheet. Should be unique. |
| 315 | * @param string $relative_src Relative source of the stylesheet to the plugin path. |
| 316 | * @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array. |
| 317 | * @param string $media Optional. The media for which this stylesheet has been defined. Default 'all'. Accepts media types like |
| 318 | * 'all', 'print' and 'screen', or media queries like '(orientation: portrait)' and '(max-width: 640px)'. |
| 319 | * @param boolean $rtl Optional. Whether or not to register RTL styles. |
| 320 | */ |
| 321 | public function register_style( $handle, $relative_src, $deps = [], $media = 'all', $rtl = false ) { |
| 322 | $filename = str_replace( plugins_url( '/', dirname( __DIR__ ) ), '', $relative_src ); |
| 323 | $src = $this->get_asset_url( $relative_src ); |
| 324 | $ver = $this->get_file_version( $filename ); |
| 325 | wp_register_style( $handle, $src, $deps, $ver, $media ); |
| 326 | |
| 327 | if ( $rtl ) { |
| 328 | wp_style_add_data( $handle, 'rtl', 'replace' ); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Returns the appropriate asset path for current builds. |
| 334 | * |
| 335 | * @param string $filename Filename for asset path (without extension). |
| 336 | * @param string $type File type (.css or .js). |
| 337 | * @return string The generated path. |
| 338 | */ |
| 339 | public function get_block_asset_build_path( $filename, $type = 'js' ) { |
| 340 | return "assets/client/blocks/$filename.$type"; |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * Adds an inline script, once. |
| 345 | * |
| 346 | * @param string $handle Script handle. |
| 347 | * @param string $script Script contents. |
| 348 | */ |
| 349 | public function add_inline_script( $handle, $script ) { |
| 350 | if ( ! empty( $this->inline_scripts[ $handle ] ) && in_array( $script, $this->inline_scripts[ $handle ], true ) ) { |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | wp_add_inline_script( $handle, $script ); |
| 355 | |
| 356 | if ( isset( $this->inline_scripts[ $handle ] ) ) { |
| 357 | $this->inline_scripts[ $handle ][] = $script; |
| 358 | } else { |
| 359 | $this->inline_scripts[ $handle ] = array( $script ); |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 |