Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Registry often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Registry, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class Registry |
||
| 27 | { |
||
| 28 | |||
| 29 | const FILE_NAME_BUILD_MANIFEST = 'build-manifest.json'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var AssetCollection $assets |
||
| 33 | */ |
||
| 34 | protected $assets; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var I18nRegistry |
||
| 38 | */ |
||
| 39 | private $i18n_registry; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * This holds the jsdata data object that will be exposed on pages that enqueue the `eejs-core` script. |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | protected $jsdata = array(); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * This keeps track of all scripts with registered data. It is used to prevent duplicate data objects setup in the |
||
| 50 | * page source. |
||
| 51 | * |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | private $script_handles_with_data = array(); |
||
| 55 | |||
| 56 | |||
| 57 | /** |
||
| 58 | * Holds the manifest data obtained from registered manifest files. |
||
| 59 | * Manifests are maps of asset chunk name to actual built asset file names. |
||
| 60 | * Shape of this array is: |
||
| 61 | * array( |
||
| 62 | * 'some_namespace_slug' => array( |
||
| 63 | * 'some_chunk_name' => array( |
||
| 64 | * 'js' => 'filename.js' |
||
| 65 | * 'css' => 'filename.js' |
||
| 66 | * ), |
||
| 67 | * 'url_base' => 'https://baseurl.com/to/assets |
||
| 68 | * ) |
||
| 69 | * ) |
||
| 70 | * |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | private $manifest_data = array(); |
||
| 74 | |||
| 75 | |||
| 76 | /** |
||
| 77 | * Holds any dependency data obtained from registered dependency map json. |
||
| 78 | * Dependency map json is generated via the @wordpress/dependency-extraction-webpack-plugin via the webpack config. |
||
| 79 | * @see https://github.com/WordPress/gutenberg/tree/master/packages/dependency-extraction-webpack-plugin |
||
| 80 | * |
||
| 81 | * @var array |
||
| 82 | */ |
||
| 83 | private $dependencies_data = []; |
||
| 84 | |||
| 85 | |||
| 86 | /** |
||
| 87 | * This is a known array of possible wp css handles that correspond to what may be exposed as dependencies in our |
||
| 88 | * build process. Currently the dependency export process in webpack does not consider css imports, so we derive |
||
| 89 | * them via the js dependencies (WP uses the same handle for both js and css). This is a list of known handles that |
||
| 90 | * are used for both js and css. |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | private $wp_css_handle_dependencies = [ |
||
| 94 | 'wp-components', |
||
| 95 | 'wp-block-editor', |
||
| 96 | 'wp-block-library', |
||
| 97 | 'wp-edit-post', |
||
| 98 | 'wp-edit-widgets', |
||
| 99 | 'wp-editor', |
||
| 100 | 'wp-format-library', |
||
| 101 | 'wp-list-reusable-blocks', |
||
| 102 | 'wp-nux', |
||
| 103 | ]; |
||
| 104 | |||
| 105 | |||
| 106 | /** |
||
| 107 | * Registry constructor. |
||
| 108 | * Hooking into WP actions for script registry. |
||
| 109 | * |
||
| 110 | * @param AssetCollection $assets |
||
| 111 | * @param I18nRegistry $i18n_registry |
||
| 112 | * @throws InvalidArgumentException |
||
| 113 | * @throws InvalidDataTypeException |
||
| 114 | * @throws InvalidInterfaceException |
||
| 115 | */ |
||
| 116 | public function __construct(AssetCollection $assets, I18nRegistry $i18n_registry) |
||
| 117 | { |
||
| 118 | $this->assets = $assets; |
||
| 119 | $this->i18n_registry = $i18n_registry; |
||
| 120 | add_action('wp_enqueue_scripts', array($this, 'registerManifestFiles'), 1); |
||
| 121 | add_action('admin_enqueue_scripts', array($this, 'registerManifestFiles'), 1); |
||
| 122 | add_action('wp_enqueue_scripts', array($this, 'registerScriptsAndStyles'), 3); |
||
| 123 | add_action('admin_enqueue_scripts', array($this, 'registerScriptsAndStyles'), 3); |
||
| 124 | add_action('wp_enqueue_scripts', array($this, 'enqueueData'), 4); |
||
| 125 | add_action('admin_enqueue_scripts', array($this, 'enqueueData'), 4); |
||
| 126 | add_action('wp_print_footer_scripts', array($this, 'enqueueData'), 1); |
||
| 127 | add_action('admin_print_footer_scripts', array($this, 'enqueueData'), 1); |
||
| 128 | } |
||
| 129 | |||
| 130 | |||
| 131 | /** |
||
| 132 | * For classes that have Registry as a dependency, this provides a handy way to register script handles for i18n |
||
| 133 | * translation handling. |
||
| 134 | * |
||
| 135 | * @return I18nRegistry |
||
| 136 | */ |
||
| 137 | public function getI18nRegistry() |
||
| 138 | { |
||
| 139 | return $this->i18n_registry; |
||
| 140 | } |
||
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * Callback for the wp_enqueue_scripts actions used to register assets. |
||
| 145 | * |
||
| 146 | * @since 4.9.62.p |
||
| 147 | * @throws Exception |
||
| 148 | */ |
||
| 149 | public function registerScriptsAndStyles() |
||
| 150 | { |
||
| 151 | try { |
||
| 152 | $this->registerScripts($this->assets->getJavascriptAssets()); |
||
| 153 | $this->registerStyles($this->assets->getStylesheetAssets()); |
||
| 154 | } catch (Exception $exception) { |
||
| 155 | new ExceptionStackTraceDisplay($exception); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | |||
| 160 | /** |
||
| 161 | * Registers JS assets with WP core |
||
| 162 | * |
||
| 163 | * @param JavascriptAsset[] $scripts |
||
| 164 | * @throws AssetRegistrationException |
||
| 165 | * @throws InvalidDataTypeException |
||
| 166 | * @throws DomainException |
||
| 167 | * @since 4.9.62.p |
||
| 168 | */ |
||
| 169 | public function registerScripts(array $scripts) |
||
| 170 | { |
||
| 171 | foreach ($scripts as $script) { |
||
| 172 | // skip to next script if this has already been done |
||
| 173 | if ($script->isRegistered()) { |
||
| 174 | continue; |
||
| 175 | } |
||
| 176 | do_action( |
||
| 177 | 'AHEE__EventEspresso_core_services_assets_Registry__registerScripts__before_script', |
||
| 178 | $script |
||
| 179 | ); |
||
| 180 | $registered = wp_register_script( |
||
| 181 | $script->handle(), |
||
| 182 | $script->source(), |
||
| 183 | $script->dependencies(), |
||
| 184 | $script->version(), |
||
| 185 | $script->loadInFooter() |
||
| 186 | ); |
||
| 187 | if (! $registered && $this->debug()) { |
||
| 188 | throw new AssetRegistrationException($script->handle()); |
||
| 189 | } |
||
| 190 | $script->setRegistered($registered); |
||
| 191 | if ($script->requiresTranslation()) { |
||
| 192 | $this->registerTranslation($script->handle()); |
||
| 193 | } |
||
| 194 | do_action( |
||
| 195 | 'AHEE__EventEspresso_core_services_assets_Registry__registerScripts__after_script', |
||
| 196 | $script |
||
| 197 | ); |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | |||
| 202 | /** |
||
| 203 | * Registers CSS assets with WP core |
||
| 204 | * |
||
| 205 | * @param StylesheetAsset[] $styles |
||
| 206 | * @throws InvalidDataTypeException |
||
| 207 | * @throws DomainException |
||
| 208 | * @since 4.9.62.p |
||
| 209 | */ |
||
| 210 | public function registerStyles(array $styles) |
||
| 211 | { |
||
| 212 | foreach ($styles as $style) { |
||
| 213 | // skip to next style if this has already been done |
||
| 214 | if ($style->isRegistered()) { |
||
| 215 | continue; |
||
| 216 | } |
||
| 217 | do_action( |
||
| 218 | 'AHEE__EventEspresso_core_services_assets_Registry__registerStyles__before_style', |
||
| 219 | $style |
||
| 220 | ); |
||
| 221 | wp_register_style( |
||
| 222 | $style->handle(), |
||
| 223 | $style->source(), |
||
| 224 | $style->dependencies(), |
||
| 225 | $style->version(), |
||
| 226 | $style->media() |
||
| 227 | ); |
||
| 228 | $style->setRegistered(); |
||
| 229 | do_action( |
||
| 230 | 'AHEE__EventEspresso_core_services_assets_Registry__registerStyles__after_style', |
||
| 231 | $style |
||
| 232 | ); |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | |||
| 237 | /** |
||
| 238 | * Call back for the script print in frontend and backend. |
||
| 239 | * Used to call wp_localize_scripts so that data can be added throughout the runtime until this later hook point. |
||
| 240 | * |
||
| 241 | * @since 4.9.31.rc.015 |
||
| 242 | */ |
||
| 243 | public function enqueueData() |
||
| 244 | { |
||
| 245 | $this->removeAlreadyRegisteredDataForScriptHandles(); |
||
| 246 | wp_add_inline_script( |
||
| 247 | 'eejs-core', |
||
| 248 | 'var eejsdata=' . wp_json_encode(array('data' => $this->jsdata)), |
||
| 249 | 'before' |
||
| 250 | ); |
||
| 251 | $scripts = $this->assets->getJavascriptAssetsWithData(); |
||
| 252 | foreach ($scripts as $script) { |
||
| 253 | $this->addRegisteredScriptHandlesWithData($script->handle()); |
||
| 254 | if ($script->hasInlineDataCallback()) { |
||
| 255 | $localize = $script->inlineDataCallback(); |
||
| 256 | $localize(); |
||
| 257 | } |
||
| 258 | } |
||
| 259 | } |
||
| 260 | |||
| 261 | |||
| 262 | /** |
||
| 263 | * Used to add data to eejs.data object. |
||
| 264 | * Note: Overriding existing data is not allowed. |
||
| 265 | * Data will be accessible as a javascript object when you list `eejs-core` as a dependency for your javascript. |
||
| 266 | * If the data you add is something like this: |
||
| 267 | * $this->addData( 'my_plugin_data', array( 'foo' => 'gar' ) ); |
||
| 268 | * It will be exposed in the page source as: |
||
| 269 | * eejs.data.my_plugin_data.foo == gar |
||
| 270 | * |
||
| 271 | * @param string $key Key used to access your data |
||
| 272 | * @param string|array $value Value to attach to key |
||
| 273 | * @throws InvalidArgumentException |
||
| 274 | */ |
||
| 275 | public function addData($key, $value) |
||
| 276 | { |
||
| 277 | if ($this->verifyDataNotExisting($key)) { |
||
| 278 | $this->jsdata[ $key ] = $value; |
||
| 279 | } |
||
| 280 | } |
||
| 281 | |||
| 282 | |||
| 283 | /** |
||
| 284 | * Similar to addData except this allows for users to push values to an existing key where the values on key are |
||
| 285 | * elements in an array. |
||
| 286 | * |
||
| 287 | * When you use this method, the value you include will be merged with the array on $key. |
||
| 288 | * So if the $key was 'test' and you added a value of ['my_data'] then it would be represented in the javascript |
||
| 289 | * object like this, eejs.data.test = [ my_data, |
||
| 290 | * ] |
||
| 291 | * If there has already been a scalar value attached to the data object given key (via addData for instance), then |
||
| 292 | * this will throw an exception. |
||
| 293 | * |
||
| 294 | * Caution: Only add data using this method if you are okay with the potential for additional data added on the same |
||
| 295 | * key potentially overriding the existing data on merge (specifically with associative arrays). |
||
| 296 | * |
||
| 297 | * @param string $key Key to attach data to. |
||
| 298 | * @param string|array $value Value being registered. |
||
| 299 | * @throws InvalidArgumentException |
||
| 300 | */ |
||
| 301 | public function pushData($key, $value) |
||
| 302 | { |
||
| 303 | if (isset($this->jsdata[ $key ]) |
||
| 304 | && ! is_array($this->jsdata[ $key ]) |
||
| 305 | ) { |
||
| 306 | if (! $this->debug()) { |
||
| 307 | return; |
||
| 308 | } |
||
| 309 | throw new InvalidArgumentException( |
||
| 310 | sprintf( |
||
| 311 | __( |
||
| 312 | 'The value for %1$s is already set and it is not an array. The %2$s method can only be used to |
||
| 313 | push values to this data element when it is an array.', |
||
| 314 | 'event_espresso' |
||
| 315 | ), |
||
| 316 | $key, |
||
| 317 | __METHOD__ |
||
| 318 | ) |
||
| 319 | ); |
||
| 320 | } |
||
| 321 | if ( ! isset( $this->jsdata[ $key ] ) ) { |
||
| 322 | $this->jsdata[ $key ] = is_array($value) ? $value : [$value]; |
||
| 323 | } else { |
||
| 324 | $this->jsdata[ $key ] = array_merge( $this->jsdata[$key], (array) $value); |
||
| 325 | } |
||
| 326 | } |
||
| 327 | |||
| 328 | |||
| 329 | /** |
||
| 330 | * Used to set content used by javascript for a template. |
||
| 331 | * Note: Overrides of existing registered templates are not allowed. |
||
| 332 | * |
||
| 333 | * @param string $template_reference |
||
| 334 | * @param string $template_content |
||
| 335 | * @throws InvalidArgumentException |
||
| 336 | */ |
||
| 337 | public function addTemplate($template_reference, $template_content) |
||
| 338 | { |
||
| 339 | if (! isset($this->jsdata['templates'])) { |
||
| 340 | $this->jsdata['templates'] = array(); |
||
| 341 | } |
||
| 342 | //no overrides allowed. |
||
| 343 | View Code Duplication | if (isset($this->jsdata['templates'][ $template_reference ])) { |
|
| 344 | if (! $this->debug()) { |
||
| 345 | return; |
||
| 346 | } |
||
| 347 | throw new InvalidArgumentException( |
||
| 348 | sprintf( |
||
| 349 | __( |
||
| 350 | 'The %1$s key already exists for the templates array in the js data array. No overrides are allowed.', |
||
| 351 | 'event_espresso' |
||
| 352 | ), |
||
| 353 | $template_reference |
||
| 354 | ) |
||
| 355 | ); |
||
| 356 | } |
||
| 357 | $this->jsdata['templates'][ $template_reference ] = $template_content; |
||
| 358 | } |
||
| 359 | |||
| 360 | |||
| 361 | /** |
||
| 362 | * Retrieve the template content already registered for the given reference. |
||
| 363 | * |
||
| 364 | * @param string $template_reference |
||
| 365 | * @return string |
||
| 366 | */ |
||
| 367 | public function getTemplate($template_reference) |
||
| 368 | { |
||
| 369 | return isset($this->jsdata['templates'][ $template_reference ]) |
||
| 370 | ? $this->jsdata['templates'][ $template_reference ] |
||
| 371 | : ''; |
||
| 372 | } |
||
| 373 | |||
| 374 | |||
| 375 | /** |
||
| 376 | * Retrieve registered data. |
||
| 377 | * |
||
| 378 | * @param string $key Name of key to attach data to. |
||
| 379 | * @return mixed If there is no for the given key, then false is returned. |
||
| 380 | */ |
||
| 381 | public function getData($key) |
||
| 382 | { |
||
| 383 | return isset($this->jsdata[ $key ]) |
||
| 384 | ? $this->jsdata[ $key ] |
||
| 385 | : false; |
||
| 386 | } |
||
| 387 | |||
| 388 | |||
| 389 | /** |
||
| 390 | * Verifies whether the given data exists already on the jsdata array. |
||
| 391 | * Overriding data is not allowed. |
||
| 392 | * |
||
| 393 | * @param string $key Index for data. |
||
| 394 | * @return bool If valid then return true. |
||
| 395 | * @throws InvalidArgumentException if data already exists. |
||
| 396 | */ |
||
| 397 | protected function verifyDataNotExisting($key) |
||
| 398 | { |
||
| 399 | if (isset($this->jsdata[ $key ])) { |
||
| 400 | if (! $this->debug()) { |
||
| 401 | return false; |
||
| 402 | } |
||
| 403 | if (is_array($this->jsdata[ $key ])) { |
||
| 404 | throw new InvalidArgumentException( |
||
| 405 | sprintf( |
||
| 406 | __( |
||
| 407 | 'The value for %1$s already exists in the Registry::eejs object. |
||
| 408 | Overrides are not allowed. Since the value of this data is an array, you may want to use the |
||
| 409 | %2$s method to push your value to the array.', |
||
| 410 | 'event_espresso' |
||
| 411 | ), |
||
| 412 | $key, |
||
| 413 | 'pushData()' |
||
| 414 | ) |
||
| 415 | ); |
||
| 416 | } |
||
| 417 | throw new InvalidArgumentException( |
||
| 418 | sprintf( |
||
| 419 | __( |
||
| 420 | 'The value for %1$s already exists in the Registry::eejs object. Overrides are not |
||
| 421 | allowed. Consider attaching your value to a different key', |
||
| 422 | 'event_espresso' |
||
| 423 | ), |
||
| 424 | $key |
||
| 425 | ) |
||
| 426 | ); |
||
| 427 | } |
||
| 428 | return true; |
||
| 429 | } |
||
| 430 | |||
| 431 | |||
| 432 | /** |
||
| 433 | * Get the actual asset path for asset manifests. |
||
| 434 | * If there is no asset path found for the given $chunk_name, then the $chunk_name is returned. |
||
| 435 | * |
||
| 436 | * @param string $namespace The namespace associated with the manifest file hosting the map of chunk_name to actual |
||
| 437 | * asset file location. |
||
| 438 | * @param string $chunk_name |
||
| 439 | * @param string $asset_type |
||
| 440 | * @return string |
||
| 441 | * @since 4.9.59.p |
||
| 442 | */ |
||
| 443 | public function getAssetUrl($namespace, $chunk_name, $asset_type) |
||
| 444 | { |
||
| 445 | $url = isset( |
||
| 446 | $this->manifest_data[ $namespace ][ $chunk_name . '.' . $asset_type ], |
||
| 447 | $this->manifest_data[ $namespace ]['url_base'] |
||
| 448 | ) |
||
| 449 | ? $this->manifest_data[ $namespace ]['url_base'] |
||
| 450 | . $this->manifest_data[ $namespace ][ $chunk_name . '.' . $asset_type ] |
||
| 451 | : $chunk_name; |
||
| 452 | |||
| 453 | return apply_filters( |
||
| 454 | 'FHEE__EventEspresso_core_services_assets_Registry__getAssetUrl', |
||
| 455 | $url, |
||
| 456 | $namespace, |
||
| 457 | $chunk_name, |
||
| 458 | $asset_type |
||
| 459 | ); |
||
| 460 | } |
||
| 461 | |||
| 462 | |||
| 463 | |||
| 464 | /** |
||
| 465 | * Return the url to a js file for the given namespace and chunk name. |
||
| 466 | * |
||
| 467 | * @param string $namespace |
||
| 468 | * @param string $chunk_name |
||
| 469 | * @return string |
||
| 470 | */ |
||
| 471 | public function getJsUrl($namespace, $chunk_name) |
||
| 472 | { |
||
| 473 | return $this->getAssetUrl($namespace, $chunk_name, Asset::TYPE_JS); |
||
| 474 | } |
||
| 475 | |||
| 476 | |||
| 477 | /** |
||
| 478 | * Return the url to a css file for the given namespace and chunk name. |
||
| 479 | * |
||
| 480 | * @param string $namespace |
||
| 481 | * @param string $chunk_name |
||
| 482 | * @return string |
||
| 483 | */ |
||
| 484 | public function getCssUrl($namespace, $chunk_name) |
||
| 485 | { |
||
| 486 | return $this->getAssetUrl($namespace, $chunk_name, Asset::TYPE_CSS); |
||
| 487 | } |
||
| 488 | |||
| 489 | |||
| 490 | /** |
||
| 491 | * Return the dependencies array and version string for a given asset $chunk_name |
||
| 492 | * |
||
| 493 | * @param string $namespace |
||
| 494 | * @param string $chunk_name |
||
| 495 | * @param string $asset_type |
||
| 496 | * @return array |
||
| 497 | * @since 4.9.82.p |
||
| 498 | */ |
||
| 499 | private function getDetailsForAsset($namespace, $chunk_name, $asset_type) |
||
| 500 | { |
||
| 501 | $asset_index = $chunk_name . '.' . $asset_type; |
||
| 502 | if (! isset( $this->dependencies_data[ $namespace ][ $asset_index ])) { |
||
| 503 | $path = isset($this->manifest_data[ $namespace ]['path']) |
||
| 504 | ? $this->manifest_data[ $namespace ]['path'] |
||
| 505 | : ''; |
||
| 506 | $dependencies_index = $chunk_name . '.' . Asset::TYPE_PHP; |
||
| 507 | $file_path = isset($this->manifest_data[ $namespace ][ $dependencies_index ]) |
||
| 508 | ? $path . $this->manifest_data[ $namespace ][ $dependencies_index ] |
||
| 509 | : |
||
| 510 | ''; |
||
| 511 | $this->dependencies_data[ $namespace ][ $asset_index ] = $file_path !== '' && file_exists($file_path) |
||
| 512 | ? $this->getDetailsForAssetType($namespace, $asset_type, $file_path, $chunk_name) |
||
| 513 | : []; |
||
| 514 | } |
||
| 515 | $details = $this->dependencies_data[ $namespace ][ $asset_index ]; |
||
| 516 | return $details; |
||
| 517 | } |
||
| 518 | |||
| 519 | |||
| 520 | /** |
||
| 521 | * Return dependencies array and version string according to asset type. |
||
| 522 | * For css assets, this filters the auto generated dependencies by css type. |
||
| 523 | * |
||
| 524 | * @param string $namespace |
||
| 525 | * @param string $asset_type |
||
| 526 | * @param string $file_path |
||
| 527 | * @param string $chunk_name |
||
| 528 | * @return array |
||
| 529 | * @since 4.9.82.p |
||
| 530 | */ |
||
| 531 | private function getDetailsForAssetType($namespace, $asset_type, $file_path, $chunk_name) |
||
| 532 | { |
||
| 533 | // $asset_dependencies = json_decode(file_get_contents($file_path), true); |
||
| 534 | $asset_details = require($file_path); |
||
| 535 | $asset_details['dependencies'] = isset($asset_details['dependencies']) |
||
| 536 | ? $asset_details['dependencies'] |
||
| 537 | : []; |
||
| 538 | $asset_details['version'] = isset($asset_details['version']) |
||
| 539 | ? $asset_details['version'] |
||
| 540 | : ''; |
||
| 541 | if ($asset_type === Asset::TYPE_JS) { |
||
| 542 | $asset_details['dependencies'] = $chunk_name === 'eejs-core' |
||
| 543 | ? $asset_details['dependencies'] |
||
| 544 | : $asset_details['dependencies'] + [ CoreAssetManager::JS_HANDLE_JS_CORE ]; |
||
| 545 | return $asset_details; |
||
| 546 | } |
||
| 547 | // for css we need to make sure there is actually a css file related to this chunk. |
||
| 548 | if (isset($this->manifest_data[ $namespace ])) { |
||
| 549 | // array of css chunk files for ee. |
||
| 550 | $css_chunks = array_map( |
||
| 551 | static function ($value) { |
||
| 552 | return str_replace('.css', '', $value); |
||
| 553 | }, |
||
| 554 | array_filter( |
||
| 555 | array_keys($this->manifest_data[ $namespace ]), |
||
| 556 | static function ($value) { |
||
| 557 | return strpos($value, '.css') !== false; |
||
| 558 | } |
||
| 559 | ) |
||
| 560 | ); |
||
| 561 | // add known wp chunks with css |
||
| 562 | $css_chunks = array_merge( $css_chunks, $this->wp_css_handle_dependencies); |
||
| 563 | // flip for easier search |
||
| 564 | $css_chunks = array_flip($css_chunks); |
||
| 565 | |||
| 566 | // now let's filter the dependencies for the incoming chunk to actual chunks that have styles |
||
| 567 | $asset_details['dependencies'] = array_filter( |
||
| 568 | $asset_details['dependencies'], |
||
| 569 | static function ($chunk_name) use ($css_chunks) { |
||
| 570 | return isset($css_chunks[ $chunk_name ]); |
||
| 571 | } |
||
| 572 | ); |
||
| 573 | return $asset_details; |
||
| 574 | } |
||
| 575 | return ['dependencies' => [], 'version' => '']; |
||
| 576 | } |
||
| 577 | |||
| 578 | |||
| 579 | /** |
||
| 580 | * Get the dependencies array and version string for the given js asset chunk name |
||
| 581 | * |
||
| 582 | * @param string $namespace |
||
| 583 | * @param string $chunk_name |
||
| 584 | * @return array |
||
| 585 | * @since $VID:$ |
||
| 586 | */ |
||
| 587 | public function getJsAssetDetails($namespace, $chunk_name) |
||
| 588 | { |
||
| 589 | return $this->getDetailsForAsset($namespace, $chunk_name, Asset::TYPE_JS); |
||
| 590 | } |
||
| 591 | |||
| 592 | |||
| 593 | /** |
||
| 594 | * Get the dependencies array and version string for the given css asset chunk name |
||
| 595 | * |
||
| 596 | * @param string $namespace |
||
| 597 | * @param string $chunk_name |
||
| 598 | * @return array |
||
| 599 | * @since $VID:$ |
||
| 600 | */ |
||
| 601 | public function getCssAssetDetails($namespace, $chunk_name) |
||
| 602 | { |
||
| 603 | return $this->getDetailsForAsset($namespace, $chunk_name, Asset::TYPE_CSS); |
||
| 604 | } |
||
| 605 | |||
| 606 | |||
| 607 | /** |
||
| 608 | * @since 4.9.62.p |
||
| 609 | * @throws InvalidArgumentException |
||
| 610 | * @throws InvalidFilePathException |
||
| 611 | */ |
||
| 612 | public function registerManifestFiles() |
||
| 624 | |||
| 625 | |||
| 626 | /** |
||
| 627 | * Used to register a js/css manifest file with the registered_manifest_files property. |
||
| 628 | * |
||
| 629 | * @param string $namespace Provided to associate the manifest file with a specific namespace. |
||
| 630 | * @param string $url_base The url base for the manifest file location. |
||
| 631 | * @param string $manifest_file The absolute path to the manifest file. |
||
| 632 | * @param string $manifest_file_path The path to the folder containing the manifest file. If not provided will be |
||
| 633 | * default to `plugin_root/assets/dist`. |
||
| 634 | * @throws InvalidArgumentException |
||
| 635 | * @throws InvalidFilePathException |
||
| 636 | * @since 4.9.59.p |
||
| 637 | */ |
||
| 638 | public function registerManifestFile($namespace, $url_base, $manifest_file, $manifest_file_path = '') |
||
| 639 | { |
||
| 640 | View Code Duplication | if (isset($this->manifest_data[ $namespace ])) { |
|
| 641 | if (! $this->debug()) { |
||
| 642 | return; |
||
| 643 | } |
||
| 682 | |||
| 683 | |||
| 684 | /** |
||
| 685 | * Decodes json from the provided manifest file. |
||
| 686 | * |
||
| 687 | * @since 4.9.59.p |
||
| 688 | * @param string $manifest_file Path to manifest file. |
||
| 689 | * @return array |
||
| 690 | * @throws InvalidFilePathException |
||
| 691 | */ |
||
| 692 | private function decodeManifestFile($manifest_file) |
||
| 699 | |||
| 700 | |||
| 701 | /** |
||
| 702 | * This is used to set registered script handles that have data. |
||
| 703 | * |
||
| 704 | * @param string $script_handle |
||
| 705 | */ |
||
| 706 | private function addRegisteredScriptHandlesWithData($script_handle) |
||
| 710 | |||
| 711 | |||
| 712 | /**i |
||
| 713 | * Checks WP_Scripts for all of each script handle registered internally as having data and unsets from the |
||
| 714 | * Dependency stored in WP_Scripts if its set. |
||
| 715 | */ |
||
| 716 | private function removeAlreadyRegisteredDataForScriptHandles() |
||
| 725 | |||
| 726 | |||
| 727 | /** |
||
| 728 | * Removes any data dependency registered in WP_Scripts if its set. |
||
| 729 | * |
||
| 730 | * @param string $script_handle |
||
| 731 | */ |
||
| 732 | private function removeAlreadyRegisteredDataForScriptHandle($script_handle) |
||
| 754 | |||
| 755 | |||
| 756 | /** |
||
| 757 | * register translations for a registered script |
||
| 758 | * |
||
| 759 | * @param string $handle |
||
| 760 | */ |
||
| 761 | public function registerTranslation($handle) |
||
| 765 | |||
| 766 | |||
| 767 | /** |
||
| 768 | * @since 4.9.63.p |
||
| 769 | * @return bool |
||
| 770 | */ |
||
| 771 | private function debug() |
||
| 778 | |||
| 779 | |||
| 780 | /** |
||
| 781 | * Get the dependencies array for the given js asset chunk name |
||
| 782 | * |
||
| 783 | * @param string $namespace |
||
| 784 | * @param string $chunk_name |
||
| 785 | * @return array |
||
| 786 | * @deprecated $VID:$ |
||
| 787 | * @since 4.9.82.p |
||
| 788 | */ |
||
| 789 | public function getJsDependencies($namespace, $chunk_name) |
||
| 794 | |||
| 795 | |||
| 796 | /** |
||
| 797 | * Get the dependencies array for the given css asset chunk name |
||
| 798 | * |
||
| 799 | * @param string $namespace |
||
| 800 | * @param string $chunk_name |
||
| 801 | * @return array |
||
| 802 | * @deprecated $VID:$ |
||
| 803 | * @since 4.9.82.p |
||
| 804 | */ |
||
| 805 | public function getCssDependencies($namespace, $chunk_name) |
||
| 810 | } |
||
| 811 |