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 |
||
| 27 | class Registry |
||
| 28 | { |
||
| 29 | |||
| 30 | const FILE_NAME_BUILD_MANIFEST = 'build-manifest.json'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var AssetCollection $assets |
||
| 34 | */ |
||
| 35 | protected $assets; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var I18nRegistry |
||
| 39 | */ |
||
| 40 | private $i18n_registry; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * This holds the jsdata data object that will be exposed on pages that enqueue the `eejs-core` script. |
||
| 44 | * |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | protected $jsdata = array(); |
||
| 48 | |||
| 49 | /** |
||
| 50 | * This keeps track of all scripts with registered data. It is used to prevent duplicate data objects setup in the |
||
| 51 | * page source. |
||
| 52 | * |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | private $script_handles_with_data = array(); |
||
| 56 | |||
| 57 | |||
| 58 | /** |
||
| 59 | * Holds the manifest data obtained from registered manifest files. |
||
| 60 | * Manifests are maps of asset chunk name to actual built asset file names. |
||
| 61 | * Shape of this array is: |
||
| 62 | * array( |
||
| 63 | * 'some_namespace_slug' => array( |
||
| 64 | * 'some_chunk_name' => array( |
||
| 65 | * 'js' => 'filename.js' |
||
| 66 | * 'css' => 'filename.js' |
||
| 67 | * ), |
||
| 68 | * 'url_base' => 'https://baseurl.com/to/assets |
||
| 69 | * ) |
||
| 70 | * ) |
||
| 71 | * |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | private $manifest_data = array(); |
||
| 75 | |||
| 76 | |||
| 77 | /** |
||
| 78 | * Holds any dependency data obtained from registered dependency map json. |
||
| 79 | * Dependency map json is generated via the @wordpress/dependency-extraction-webpack-plugin via the webpack config. |
||
| 80 | * @see https://github.com/WordPress/gutenberg/tree/master/packages/dependency-extraction-webpack-plugin |
||
| 81 | * |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | private $dependencies_data = []; |
||
| 85 | |||
| 86 | |||
| 87 | /** |
||
| 88 | * This is a known array of possible wp css handles that correspond to what may be exposed as dependencies in our |
||
| 89 | * build process. Currently the dependency export process in webpack does not consider css imports, so we derive |
||
| 90 | * them via the js dependencies (WP uses the same handle for both js and css). This is a list of known handles that |
||
| 91 | * are used for both js and css. |
||
| 92 | * @var array |
||
| 93 | */ |
||
| 94 | private $wp_css_handle_dependencies = [ |
||
| 95 | 'wp-components', |
||
| 96 | 'wp-block-editor', |
||
| 97 | 'wp-block-library', |
||
| 98 | 'wp-edit-post', |
||
| 99 | 'wp-edit-widgets', |
||
| 100 | 'wp-editor', |
||
| 101 | 'wp-format-library', |
||
| 102 | 'wp-list-reusable-blocks', |
||
| 103 | 'wp-nux', |
||
| 104 | ]; |
||
| 105 | |||
| 106 | |||
| 107 | /** |
||
| 108 | * Registry constructor. |
||
| 109 | * Hooking into WP actions for script registry. |
||
| 110 | * |
||
| 111 | * @param AssetCollection $assets |
||
| 112 | * @param I18nRegistry $i18n_registry |
||
| 113 | * @throws InvalidArgumentException |
||
| 114 | * @throws InvalidDataTypeException |
||
| 115 | * @throws InvalidInterfaceException |
||
| 116 | */ |
||
| 117 | public function __construct(AssetCollection $assets, I18nRegistry $i18n_registry) |
||
| 118 | { |
||
| 119 | $this->assets = $assets; |
||
| 120 | $this->i18n_registry = $i18n_registry; |
||
| 121 | add_action('wp_enqueue_scripts', array($this, 'registerManifestFiles'), 1); |
||
| 122 | add_action('admin_enqueue_scripts', array($this, 'registerManifestFiles'), 1); |
||
| 123 | add_action('wp_enqueue_scripts', array($this, 'registerScriptsAndStyles'), 3); |
||
| 124 | add_action('admin_enqueue_scripts', array($this, 'registerScriptsAndStyles'), 3); |
||
| 125 | add_action('wp_enqueue_scripts', array($this, 'enqueueData'), 4); |
||
| 126 | add_action('admin_enqueue_scripts', array($this, 'enqueueData'), 4); |
||
| 127 | add_action('wp_print_footer_scripts', array($this, 'enqueueData'), 1); |
||
| 128 | add_action('admin_print_footer_scripts', array($this, 'enqueueData'), 1); |
||
| 129 | } |
||
| 130 | |||
| 131 | |||
| 132 | /** |
||
| 133 | * For classes that have Registry as a dependency, this provides a handy way to register script handles for i18n |
||
| 134 | * translation handling. |
||
| 135 | * |
||
| 136 | * @return I18nRegistry |
||
| 137 | */ |
||
| 138 | public function getI18nRegistry() |
||
| 139 | { |
||
| 140 | return $this->i18n_registry; |
||
| 141 | } |
||
| 142 | |||
| 143 | |||
| 144 | /** |
||
| 145 | * Callback for the wp_enqueue_scripts actions used to register assets. |
||
| 146 | * |
||
| 147 | * @since 4.9.62.p |
||
| 148 | * @throws Exception |
||
| 149 | */ |
||
| 150 | public function registerScriptsAndStyles() |
||
| 151 | { |
||
| 152 | try { |
||
| 153 | $this->registerScripts($this->assets->getJavascriptAssets()); |
||
| 154 | $this->registerStyles($this->assets->getStylesheetAssets()); |
||
| 155 | } catch (Exception $exception) { |
||
| 156 | new ExceptionStackTraceDisplay($exception); |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | |||
| 161 | /** |
||
| 162 | * Registers JS assets with WP core |
||
| 163 | * |
||
| 164 | * @since 4.9.62.p |
||
| 165 | * @param JavascriptAsset[] $scripts |
||
| 166 | * @throws AssetRegistrationException |
||
| 167 | * @throws InvalidDataTypeException |
||
| 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 | * @since 4.9.62.p |
||
| 206 | * @param StylesheetAsset[] $styles |
||
| 207 | * @throws InvalidDataTypeException |
||
| 208 | */ |
||
| 209 | public function registerStyles(array $styles) |
||
| 210 | { |
||
| 211 | foreach ($styles as $style) { |
||
| 212 | // skip to next style if this has already been done |
||
| 213 | if ($style->isRegistered()) { |
||
| 214 | continue; |
||
| 215 | } |
||
| 216 | do_action( |
||
| 217 | 'AHEE__EventEspresso_core_services_assets_Registry__registerStyles__before_style', |
||
| 218 | $style |
||
| 219 | ); |
||
| 220 | wp_register_style( |
||
| 221 | $style->handle(), |
||
| 222 | $style->source(), |
||
| 223 | $style->dependencies(), |
||
| 224 | $style->version(), |
||
| 225 | $style->media() |
||
| 226 | ); |
||
| 227 | $style->setRegistered(); |
||
| 228 | do_action( |
||
| 229 | 'AHEE__EventEspresso_core_services_assets_Registry__registerStyles__after_style', |
||
| 230 | $style |
||
| 231 | ); |
||
| 232 | } |
||
| 233 | } |
||
| 234 | |||
| 235 | |||
| 236 | /** |
||
| 237 | * Call back for the script print in frontend and backend. |
||
| 238 | * Used to call wp_localize_scripts so that data can be added throughout the runtime until this later hook point. |
||
| 239 | * |
||
| 240 | * @since 4.9.31.rc.015 |
||
| 241 | */ |
||
| 242 | public function enqueueData() |
||
| 243 | { |
||
| 244 | $this->removeAlreadyRegisteredDataForScriptHandles(); |
||
| 245 | wp_add_inline_script( |
||
| 246 | 'eejs-core', |
||
| 247 | 'var eejsdata=' . wp_json_encode(array('data' => $this->jsdata)), |
||
| 248 | 'before' |
||
| 249 | ); |
||
| 250 | $scripts = $this->assets->getJavascriptAssetsWithData(); |
||
| 251 | foreach ($scripts as $script) { |
||
| 252 | $this->addRegisteredScriptHandlesWithData($script->handle()); |
||
| 253 | if ($script->hasInlineDataCallback()) { |
||
| 254 | $localize = $script->inlineDataCallback(); |
||
| 255 | $localize(); |
||
| 256 | } |
||
| 257 | } |
||
| 258 | } |
||
| 259 | |||
| 260 | |||
| 261 | /** |
||
| 262 | * Used to add data to eejs.data object. |
||
| 263 | * Note: Overriding existing data is not allowed. |
||
| 264 | * Data will be accessible as a javascript object when you list `eejs-core` as a dependency for your javascript. |
||
| 265 | * If the data you add is something like this: |
||
| 266 | * $this->addData( 'my_plugin_data', array( 'foo' => 'gar' ) ); |
||
| 267 | * It will be exposed in the page source as: |
||
| 268 | * eejs.data.my_plugin_data.foo == gar |
||
| 269 | * |
||
| 270 | * @param string $key Key used to access your data |
||
| 271 | * @param string|array $value Value to attach to key |
||
| 272 | * @throws InvalidArgumentException |
||
| 273 | */ |
||
| 274 | public function addData($key, $value) |
||
| 275 | { |
||
| 276 | if ($this->verifyDataNotExisting($key)) { |
||
| 277 | $this->jsdata[ $key ] = $value; |
||
| 278 | } |
||
| 279 | } |
||
| 280 | |||
| 281 | |||
| 282 | /** |
||
| 283 | * Similar to addData except this allows for users to push values to an existing key where the values on key are |
||
| 284 | * elements in an array. |
||
| 285 | * |
||
| 286 | * When you use this method, the value you include will be merged with the array on $key. |
||
| 287 | * So if the $key was 'test' and you added a value of ['my_data'] then it would be represented in the javascript |
||
| 288 | * object like this, eejs.data.test = [ my_data, |
||
| 289 | * ] |
||
| 290 | * If there has already been a scalar value attached to the data object given key (via addData for instance), then |
||
| 291 | * this will throw an exception. |
||
| 292 | * |
||
| 293 | * Caution: Only add data using this method if you are okay with the potential for additional data added on the same |
||
| 294 | * key potentially overriding the existing data on merge (specifically with associative arrays). |
||
| 295 | * |
||
| 296 | * @param string $key Key to attach data to. |
||
| 297 | * @param string|array $value Value being registered. |
||
| 298 | * @throws InvalidArgumentException |
||
| 299 | */ |
||
| 300 | public function pushData($key, $value) |
||
| 301 | { |
||
| 302 | if (isset($this->jsdata[ $key ]) |
||
| 303 | && ! is_array($this->jsdata[ $key ]) |
||
| 304 | ) { |
||
| 305 | if (! $this->debug()) { |
||
| 306 | return; |
||
| 307 | } |
||
| 308 | throw new InvalidArgumentException( |
||
| 309 | sprintf( |
||
| 310 | __( |
||
| 311 | 'The value for %1$s is already set and it is not an array. The %2$s method can only be used to |
||
| 312 | push values to this data element when it is an array.', |
||
| 313 | 'event_espresso' |
||
| 314 | ), |
||
| 315 | $key, |
||
| 316 | __METHOD__ |
||
| 317 | ) |
||
| 318 | ); |
||
| 319 | } |
||
| 320 | if ( ! isset( $this->jsdata[ $key ] ) ) { |
||
| 321 | $this->jsdata[ $key ] = is_array($value) ? $value : [$value]; |
||
| 322 | } else { |
||
| 323 | $this->jsdata[ $key ] = array_merge( $this->jsdata[$key], (array) $value); |
||
| 324 | } |
||
| 325 | } |
||
| 326 | |||
| 327 | |||
| 328 | /** |
||
| 329 | * Used to set content used by javascript for a template. |
||
| 330 | * Note: Overrides of existing registered templates are not allowed. |
||
| 331 | * |
||
| 332 | * @param string $template_reference |
||
| 333 | * @param string $template_content |
||
| 334 | * @throws InvalidArgumentException |
||
| 335 | */ |
||
| 336 | public function addTemplate($template_reference, $template_content) |
||
| 337 | { |
||
| 338 | if (! isset($this->jsdata['templates'])) { |
||
| 339 | $this->jsdata['templates'] = array(); |
||
| 340 | } |
||
| 341 | //no overrides allowed. |
||
| 342 | View Code Duplication | if (isset($this->jsdata['templates'][ $template_reference ])) { |
|
| 343 | if (! $this->debug()) { |
||
| 344 | return; |
||
| 345 | } |
||
| 346 | throw new InvalidArgumentException( |
||
| 347 | sprintf( |
||
| 348 | __( |
||
| 349 | 'The %1$s key already exists for the templates array in the js data array. No overrides are allowed.', |
||
| 350 | 'event_espresso' |
||
| 351 | ), |
||
| 352 | $template_reference |
||
| 353 | ) |
||
| 354 | ); |
||
| 355 | } |
||
| 356 | $this->jsdata['templates'][ $template_reference ] = $template_content; |
||
| 357 | } |
||
| 358 | |||
| 359 | |||
| 360 | /** |
||
| 361 | * Retrieve the template content already registered for the given reference. |
||
| 362 | * |
||
| 363 | * @param string $template_reference |
||
| 364 | * @return string |
||
| 365 | */ |
||
| 366 | public function getTemplate($template_reference) |
||
| 367 | { |
||
| 368 | return isset($this->jsdata['templates'][ $template_reference ]) |
||
| 369 | ? $this->jsdata['templates'][ $template_reference ] |
||
| 370 | : ''; |
||
| 371 | } |
||
| 372 | |||
| 373 | |||
| 374 | /** |
||
| 375 | * Retrieve registered data. |
||
| 376 | * |
||
| 377 | * @param string $key Name of key to attach data to. |
||
| 378 | * @return mixed If there is no for the given key, then false is returned. |
||
| 379 | */ |
||
| 380 | public function getData($key) |
||
| 381 | { |
||
| 382 | return isset($this->jsdata[ $key ]) |
||
| 383 | ? $this->jsdata[ $key ] |
||
| 384 | : false; |
||
| 385 | } |
||
| 386 | |||
| 387 | |||
| 388 | /** |
||
| 389 | * Verifies whether the given data exists already on the jsdata array. |
||
| 390 | * Overriding data is not allowed. |
||
| 391 | * |
||
| 392 | * @param string $key Index for data. |
||
| 393 | * @return bool If valid then return true. |
||
| 394 | * @throws InvalidArgumentException if data already exists. |
||
| 395 | */ |
||
| 396 | protected function verifyDataNotExisting($key) |
||
| 397 | { |
||
| 398 | if (isset($this->jsdata[ $key ])) { |
||
| 399 | if (! $this->debug()) { |
||
| 400 | return false; |
||
| 401 | } |
||
| 402 | if (is_array($this->jsdata[ $key ])) { |
||
| 403 | throw new InvalidArgumentException( |
||
| 404 | sprintf( |
||
| 405 | __( |
||
| 406 | 'The value for %1$s already exists in the Registry::eejs object. |
||
| 407 | Overrides are not allowed. Since the value of this data is an array, you may want to use the |
||
| 408 | %2$s method to push your value to the array.', |
||
| 409 | 'event_espresso' |
||
| 410 | ), |
||
| 411 | $key, |
||
| 412 | 'pushData()' |
||
| 413 | ) |
||
| 414 | ); |
||
| 415 | } |
||
| 416 | throw new InvalidArgumentException( |
||
| 417 | sprintf( |
||
| 418 | __( |
||
| 419 | 'The value for %1$s already exists in the Registry::eejs object. Overrides are not |
||
| 420 | allowed. Consider attaching your value to a different key', |
||
| 421 | 'event_espresso' |
||
| 422 | ), |
||
| 423 | $key |
||
| 424 | ) |
||
| 425 | ); |
||
| 426 | } |
||
| 427 | return true; |
||
| 428 | } |
||
| 429 | |||
| 430 | |||
| 431 | /** |
||
| 432 | * Get the actual asset path for asset manifests. |
||
| 433 | * If there is no asset path found for the given $chunk_name, then the $chunk_name is returned. |
||
| 434 | * |
||
| 435 | * @param string $namespace The namespace associated with the manifest file hosting the map of chunk_name to actual |
||
| 436 | * asset file location. |
||
| 437 | * @param string $chunk_name |
||
| 438 | * @param string $asset_type |
||
| 439 | * @return string |
||
| 440 | * @since 4.9.59.p |
||
| 441 | */ |
||
| 442 | public function getAssetUrl($namespace, $chunk_name, $asset_type) |
||
| 443 | { |
||
| 444 | $url = isset( |
||
| 445 | $this->manifest_data[ $namespace ][ $chunk_name . '.' . $asset_type ], |
||
| 446 | $this->manifest_data[ $namespace ]['url_base'] |
||
| 447 | ) |
||
| 448 | ? $this->manifest_data[ $namespace ]['url_base'] |
||
| 449 | . $this->manifest_data[ $namespace ][ $chunk_name . '.' . $asset_type ] |
||
| 450 | : $chunk_name; |
||
| 451 | |||
| 452 | return apply_filters( |
||
| 453 | 'FHEE__EventEspresso_core_services_assets_Registry__getAssetUrl', |
||
| 454 | $url, |
||
| 455 | $namespace, |
||
| 456 | $chunk_name, |
||
| 457 | $asset_type |
||
| 458 | ); |
||
| 459 | } |
||
| 460 | |||
| 461 | |||
| 462 | |||
| 463 | /** |
||
| 464 | * Return the url to a js file for the given namespace and chunk name. |
||
| 465 | * |
||
| 466 | * @param string $namespace |
||
| 467 | * @param string $chunk_name |
||
| 468 | * @return string |
||
| 469 | */ |
||
| 470 | public function getJsUrl($namespace, $chunk_name) |
||
| 471 | { |
||
| 472 | return $this->getAssetUrl($namespace, $chunk_name, Asset::TYPE_JS); |
||
| 473 | } |
||
| 474 | |||
| 475 | |||
| 476 | /** |
||
| 477 | * Return the url to a css file for the given namespace and chunk name. |
||
| 478 | * |
||
| 479 | * @param string $namespace |
||
| 480 | * @param string $chunk_name |
||
| 481 | * @return string |
||
| 482 | */ |
||
| 483 | public function getCssUrl($namespace, $chunk_name) |
||
| 484 | { |
||
| 485 | return $this->getAssetUrl($namespace, $chunk_name, Asset::TYPE_CSS); |
||
| 486 | } |
||
| 487 | |||
| 488 | |||
| 489 | /** |
||
| 490 | * Return the dependencies for a given asset $chunk_name |
||
| 491 | * |
||
| 492 | * @param string $namespace |
||
| 493 | * @param string $chunk_name |
||
| 494 | * @param string $asset_type |
||
| 495 | * @return array |
||
| 496 | * @since 4.9.82.p |
||
| 497 | */ |
||
| 498 | private function getDependenciesForAsset($namespace, $chunk_name, $asset_type) |
||
| 499 | { |
||
| 500 | $asset_index = $chunk_name . '.' . $asset_type; |
||
| 501 | if (! isset( $this->dependencies_data[ $namespace ][ $asset_index ])) { |
||
| 502 | $path = isset($this->manifest_data[ $namespace ]['path']) |
||
| 503 | ? $this->manifest_data[ $namespace ]['path'] |
||
| 504 | : ''; |
||
| 505 | $dependencies_index = $chunk_name . '.' . Asset::TYPE_JSON; |
||
| 506 | $file_path = isset($this->manifest_data[ $namespace ][ $dependencies_index ]) |
||
| 507 | ? $path . $this->manifest_data[ $namespace ][ $dependencies_index ] |
||
| 508 | : |
||
| 509 | ''; |
||
| 510 | $this->dependencies_data[ $namespace ][ $asset_index ] = $file_path !== '' && file_exists($file_path) |
||
| 511 | ? $this->getDependenciesForAssetType($namespace, $asset_type, $file_path, $chunk_name) |
||
| 512 | : []; |
||
| 513 | } |
||
| 514 | return $this->dependencies_data[ $namespace ][ $asset_index ]; |
||
| 515 | } |
||
| 516 | |||
| 517 | |||
| 518 | /** |
||
| 519 | * Return dependencies according to asset type. |
||
| 520 | * |
||
| 521 | * For css assets, this filters the auto generated dependencies by css type. |
||
| 522 | * |
||
| 523 | * @param string $namespace |
||
| 524 | * @param string $asset_type |
||
| 525 | * @param string $file_path |
||
| 526 | * @param string $chunk_name |
||
| 527 | * @return array |
||
| 528 | * @since 4.9.82.p |
||
| 529 | */ |
||
| 530 | private function getDependenciesForAssetType($namespace, $asset_type, $file_path, $chunk_name) |
||
| 531 | { |
||
| 532 | $asset_dependencies = json_decode(file_get_contents($file_path), true); |
||
| 533 | if ($asset_type === Asset::TYPE_JS) { |
||
| 534 | return $chunk_name === 'eejs-core' ? $asset_dependencies : array_merge( |
||
| 535 | $asset_dependencies, |
||
| 536 | [ CoreAssetManager::JS_HANDLE_JS_CORE ] |
||
| 537 | ); |
||
| 538 | } |
||
| 539 | // for css we need to make sure there is actually a css file related to this chunk. |
||
| 540 | if (isset($this->manifest_data[ $namespace ])) { |
||
| 541 | // array of css chunk files for ee. |
||
| 542 | $css_chunks = array_map( |
||
| 543 | function ($value) { |
||
| 544 | return str_replace('.css', '', $value); |
||
| 545 | }, |
||
| 546 | array_filter( |
||
| 547 | array_keys($this->manifest_data[ $namespace ]), |
||
| 548 | function ($value) { |
||
| 549 | return strpos($value, '.css') !== false; |
||
| 550 | } |
||
| 551 | ) |
||
| 552 | ); |
||
| 553 | // add known wp chunks with css |
||
| 554 | $css_chunks = array_merge( $css_chunks, $this->wp_css_handle_dependencies); |
||
| 555 | // flip for easier search |
||
| 556 | $css_chunks = array_flip($css_chunks); |
||
| 557 | // now let's filter the dependencies for the incoming chunk to actual chunks that have styles |
||
| 558 | return array_filter( |
||
| 559 | $asset_dependencies, |
||
| 560 | function ($chunk_name) use ($css_chunks) { |
||
| 561 | return isset($css_chunks[ $chunk_name ]); |
||
| 562 | } |
||
| 563 | ); |
||
| 564 | } |
||
| 565 | return []; |
||
| 566 | } |
||
| 567 | |||
| 568 | |||
| 569 | /** |
||
| 570 | * Get the dependencies array for the given js asset chunk name |
||
| 571 | * |
||
| 572 | * @param string $namespace |
||
| 573 | * @param string $chunk_name |
||
| 574 | * @return array |
||
| 575 | * @since 4.9.82.p |
||
| 576 | */ |
||
| 577 | public function getJsDependencies($namespace, $chunk_name) |
||
| 581 | |||
| 582 | |||
| 583 | /** |
||
| 584 | * Get the dependencies array for the given css asset chunk name |
||
| 585 | * |
||
| 586 | * @param string $namespace |
||
| 587 | * @param string $chunk_name |
||
| 588 | * @return array |
||
| 589 | * @since 4.9.82.p |
||
| 590 | */ |
||
| 591 | public function getCssDependencies($namespace, $chunk_name) |
||
| 595 | |||
| 596 | |||
| 597 | /** |
||
| 598 | * @since 4.9.62.p |
||
| 599 | * @throws InvalidArgumentException |
||
| 600 | * @throws InvalidFilePathException |
||
| 601 | */ |
||
| 602 | public function registerManifestFiles() |
||
| 614 | |||
| 615 | |||
| 616 | /** |
||
| 617 | * Used to register a js/css manifest file with the registered_manifest_files property. |
||
| 618 | * |
||
| 619 | * @param string $namespace Provided to associate the manifest file with a specific namespace. |
||
| 620 | * @param string $url_base The url base for the manifest file location. |
||
| 621 | * @param string $manifest_file The absolute path to the manifest file. |
||
| 622 | * @param string $manifest_file_path The path to the folder containing the manifest file. If not provided will be |
||
| 623 | * default to `plugin_root/assets/dist`. |
||
| 624 | * @throws InvalidArgumentException |
||
| 625 | * @throws InvalidFilePathException |
||
| 626 | * @since 4.9.59.p |
||
| 627 | */ |
||
| 628 | public function registerManifestFile($namespace, $url_base, $manifest_file, $manifest_file_path = '') |
||
| 629 | { |
||
| 630 | View Code Duplication | if (isset($this->manifest_data[ $namespace ])) { |
|
| 631 | if (! $this->debug()) { |
||
| 672 | |||
| 673 | |||
| 674 | /** |
||
| 675 | * Decodes json from the provided manifest file. |
||
| 676 | * |
||
| 677 | * @since 4.9.59.p |
||
| 678 | * @param string $manifest_file Path to manifest file. |
||
| 679 | * @return array |
||
| 680 | * @throws InvalidFilePathException |
||
| 681 | */ |
||
| 682 | private function decodeManifestFile($manifest_file) |
||
| 689 | |||
| 690 | |||
| 691 | /** |
||
| 692 | * This is used to set registered script handles that have data. |
||
| 693 | * |
||
| 694 | * @param string $script_handle |
||
| 695 | */ |
||
| 696 | private function addRegisteredScriptHandlesWithData($script_handle) |
||
| 700 | |||
| 701 | |||
| 702 | /**i |
||
| 703 | * Checks WP_Scripts for all of each script handle registered internally as having data and unsets from the |
||
| 704 | * Dependency stored in WP_Scripts if its set. |
||
| 705 | */ |
||
| 706 | private function removeAlreadyRegisteredDataForScriptHandles() |
||
| 715 | |||
| 716 | |||
| 717 | /** |
||
| 718 | * Removes any data dependency registered in WP_Scripts if its set. |
||
| 719 | * |
||
| 720 | * @param string $script_handle |
||
| 721 | */ |
||
| 722 | private function removeAlreadyRegisteredDataForScriptHandle($script_handle) |
||
| 744 | |||
| 745 | |||
| 746 | /** |
||
| 747 | * register translations for a registered script |
||
| 748 | * |
||
| 749 | * @param string $handle |
||
| 750 | */ |
||
| 751 | public function registerTranslation($handle) |
||
| 755 | |||
| 756 | |||
| 757 | /** |
||
| 758 | * @since 4.9.63.p |
||
| 759 | * @return bool |
||
| 760 | */ |
||
| 761 | private function debug() |
||
| 768 | } |
||
| 769 |