[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> class.wp-dependencies.php (source)

   1  <?php
   2  /**
   3   * Dependencies API: WP_Dependencies base class
   4   *
   5   * @since 2.6.0
   6   *
   7   * @package WordPress
   8   * @subpackage Dependencies
   9   */
  10  
  11  /**
  12   * Core base class extended to register items.
  13   *
  14   * @since 2.6.0
  15   *
  16   * @see _WP_Dependency
  17   */
  18  class WP_Dependencies {
  19      /**
  20       * An array of all registered dependencies keyed by handle.
  21       *
  22       * @since 2.6.8
  23       *
  24       * @var _WP_Dependency[]
  25       */
  26      public $registered = array();
  27  
  28      /**
  29       * An array of handles of queued dependencies.
  30       *
  31       * @since 2.6.8
  32       *
  33       * @var string[]
  34       */
  35      public $queue = array();
  36  
  37      /**
  38       * An array of handles of dependencies to queue.
  39       *
  40       * @since 2.6.0
  41       *
  42       * @var string[]
  43       */
  44      public $to_do = array();
  45  
  46      /**
  47       * An array of handles of dependencies already queued.
  48       *
  49       * @since 2.6.0
  50       *
  51       * @var string[]
  52       */
  53      public $done = array();
  54  
  55      /**
  56       * An array of additional arguments passed when a handle is registered.
  57       *
  58       * Arguments are appended to the item query string.
  59       *
  60       * @since 2.6.0
  61       *
  62       * @var array
  63       */
  64      public $args = array();
  65  
  66      /**
  67       * An array of dependency groups to enqueue.
  68       *
  69       * Each entry is keyed by handle and represents the integer group level or boolean
  70       * false if the handle has no group.
  71       *
  72       * @since 2.8.0
  73       *
  74       * @var (int|false)[]
  75       */
  76      public $groups = array();
  77  
  78      /**
  79       * A handle group to enqueue.
  80       *
  81       * @since 2.8.0
  82       *
  83       * @deprecated 4.5.0
  84       * @var int
  85       */
  86      public $group = 0;
  87  
  88      /**
  89       * Cached lookup array of flattened queued items and dependencies.
  90       *
  91       * @since 5.4.0
  92       *
  93       * @var array
  94       */
  95      private $all_queued_deps;
  96  
  97      /**
  98       * Processes the items and dependencies.
  99       *
 100       * Processes the items passed to it or the queue, and their dependencies.
 101       *
 102       * @since 2.6.0
 103       * @since 2.8.0 Added the `$group` parameter.
 104       *
 105       * @param string|string[]|false $handles Optional. Items to be processed: queue (false),
 106       *                                       single item (string), or multiple items (array of strings).
 107       *                                       Default false.
 108       * @param int|false             $group   Optional. Group level: level (int), no group (false).
 109       * @return string[] Array of handles of items that have been processed.
 110       */
 111  	public function do_items( $handles = false, $group = false ) {
 112          /*
 113           * If nothing is passed, print the queue. If a string is passed,
 114           * print that item. If an array is passed, print those items.
 115           */
 116          $handles = false === $handles ? $this->queue : (array) $handles;
 117          $this->all_deps( $handles );
 118  
 119          foreach ( $this->to_do as $key => $handle ) {
 120              if ( ! in_array( $handle, $this->done, true ) && isset( $this->registered[ $handle ] ) ) {
 121                  /*
 122                   * Attempt to process the item. If successful,
 123                   * add the handle to the done array.
 124                   *
 125                   * Unset the item from the to_do array.
 126                   */
 127                  if ( $this->do_item( $handle, $group ) ) {
 128                      $this->done[] = $handle;
 129                  }
 130  
 131                  unset( $this->to_do[ $key ] );
 132              }
 133          }
 134  
 135          return $this->done;
 136      }
 137  
 138      /**
 139       * Processes a dependency.
 140       *
 141       * @since 2.6.0
 142       * @since 5.5.0 Added the `$group` parameter.
 143       *
 144       * @param string    $handle Name of the item. Should be unique.
 145       * @param int|false $group  Optional. Group level: level (int), no group (false).
 146       *                          Default false.
 147       * @return bool True on success, false if not set.
 148       */
 149  	public function do_item( $handle, $group = false ) {
 150          return isset( $this->registered[ $handle ] );
 151      }
 152  
 153      /**
 154       * Determines dependencies.
 155       *
 156       * Recursively builds an array of items to process taking
 157       * dependencies into account. Does NOT catch infinite loops.
 158       *
 159       * @since 2.1.0
 160       * @since 2.6.0 Moved from `WP_Scripts`.
 161       * @since 2.8.0 Added the `$group` parameter.
 162       *
 163       * @param string|string[] $handles   Item handle (string) or item handles (array of strings).
 164       * @param bool            $recursion Optional. Internal flag that function is calling itself.
 165       *                                   Default false.
 166       * @param int|false       $group     Optional. Group level: level (int), no group (false).
 167       *                                   Default false.
 168       * @return bool True on success, false on failure.
 169       */
 170  	public function all_deps( $handles, $recursion = false, $group = false ) {
 171          $handles = (array) $handles;
 172          if ( ! $handles ) {
 173              return false;
 174          }
 175  
 176          foreach ( $handles as $handle ) {
 177              $handle_parts = explode( '?', $handle );
 178              $handle       = $handle_parts[0];
 179              $queued       = in_array( $handle, $this->to_do, true );
 180  
 181              if ( in_array( $handle, $this->done, true ) ) { // Already done.
 182                  continue;
 183              }
 184  
 185              $moved     = $this->set_group( $handle, $recursion, $group );
 186              $new_group = $this->groups[ $handle ];
 187  
 188              if ( $queued && ! $moved ) { // Already queued and in the right group.
 189                  continue;
 190              }
 191  
 192              $keep_going = true;
 193              if ( ! isset( $this->registered[ $handle ] ) ) {
 194                  $keep_going = false; // Item doesn't exist.
 195              } elseif ( $this->registered[ $handle ]->deps && array_diff( $this->registered[ $handle ]->deps, array_keys( $this->registered ) ) ) {
 196                  $keep_going = false; // Item requires dependencies that don't exist.
 197              } elseif ( $this->registered[ $handle ]->deps && ! $this->all_deps( $this->registered[ $handle ]->deps, true, $new_group ) ) {
 198                  $keep_going = false; // Item requires dependencies that don't exist.
 199              }
 200  
 201              if ( ! $keep_going ) { // Either item or its dependencies don't exist.
 202                  if ( $recursion ) {
 203                      return false; // Abort this branch.
 204                  } else {
 205                      continue; // We're at the top level. Move on to the next one.
 206                  }
 207              }
 208  
 209              if ( $queued ) { // Already grabbed it and its dependencies.
 210                  continue;
 211              }
 212  
 213              if ( isset( $handle_parts[1] ) ) {
 214                  $this->args[ $handle ] = $handle_parts[1];
 215              }
 216  
 217              $this->to_do[] = $handle;
 218          }
 219  
 220          return true;
 221      }
 222  
 223      /**
 224       * Register an item.
 225       *
 226       * Registers the item if no item of that name already exists.
 227       *
 228       * @since 2.1.0
 229       * @since 2.6.0 Moved from `WP_Scripts`.
 230       *
 231       * @param string           $handle Name of the item. Should be unique.
 232       * @param string|bool      $src    Full URL of the item, or path of the item relative
 233       *                                 to the WordPress root directory. If source is set to false,
 234       *                                 item is an alias of other items it depends on.
 235       * @param string[]         $deps   Optional. An array of registered item handles this item depends on.
 236       *                                 Default empty array.
 237       * @param string|bool|null $ver    Optional. String specifying item version number, if it has one,
 238       *                                 which is added to the URL as a query string for cache busting purposes.
 239       *                                 If version is set to false, a version number is automatically added
 240       *                                 equal to current installed WordPress version.
 241       *                                 If set to null, no version is added.
 242       * @param mixed            $args   Optional. Custom property of the item. NOT the class property $args.
 243       *                                 Examples: $media, $in_footer.
 244       * @return bool Whether the item has been registered. True on success, false on failure.
 245       */
 246  	public function add( $handle, $src, $deps = array(), $ver = false, $args = null ) {
 247          if ( isset( $this->registered[ $handle ] ) ) {
 248              return false;
 249          }
 250          $this->registered[ $handle ] = new _WP_Dependency( $handle, $src, $deps, $ver, $args );
 251          return true;
 252      }
 253  
 254      /**
 255       * Add extra item data.
 256       *
 257       * Adds data to a registered item.
 258       *
 259       * @since 2.6.0
 260       *
 261       * @param string $handle Name of the item. Should be unique.
 262       * @param string $key    The data key.
 263       * @param mixed  $value  The data value.
 264       * @return bool True on success, false on failure.
 265       */
 266  	public function add_data( $handle, $key, $value ) {
 267          if ( ! isset( $this->registered[ $handle ] ) ) {
 268              return false;
 269          }
 270  
 271          return $this->registered[ $handle ]->add_data( $key, $value );
 272      }
 273  
 274      /**
 275       * Get extra item data.
 276       *
 277       * Gets data associated with a registered item.
 278       *
 279       * @since 3.3.0
 280       *
 281       * @param string $handle Name of the item. Should be unique.
 282       * @param string $key    The data key.
 283       * @return mixed Extra item data (string), false otherwise.
 284       */
 285  	public function get_data( $handle, $key ) {
 286          if ( ! isset( $this->registered[ $handle ] ) ) {
 287              return false;
 288          }
 289  
 290          if ( ! isset( $this->registered[ $handle ]->extra[ $key ] ) ) {
 291              return false;
 292          }
 293  
 294          return $this->registered[ $handle ]->extra[ $key ];
 295      }
 296  
 297      /**
 298       * Un-register an item or items.
 299       *
 300       * @since 2.1.0
 301       * @since 2.6.0 Moved from `WP_Scripts`.
 302       *
 303       * @param string|string[] $handles Item handle (string) or item handles (array of strings).
 304       */
 305  	public function remove( $handles ) {
 306          foreach ( (array) $handles as $handle ) {
 307              unset( $this->registered[ $handle ] );
 308          }
 309      }
 310  
 311      /**
 312       * Queue an item or items.
 313       *
 314       * Decodes handles and arguments, then queues handles and stores
 315       * arguments in the class property $args. For example in extending
 316       * classes, $args is appended to the item url as a query string.
 317       * Note $args is NOT the $args property of items in the $registered array.
 318       *
 319       * @since 2.1.0
 320       * @since 2.6.0 Moved from `WP_Scripts`.
 321       *
 322       * @param string|string[] $handles Item handle (string) or item handles (array of strings).
 323       */
 324  	public function enqueue( $handles ) {
 325          foreach ( (array) $handles as $handle ) {
 326              $handle = explode( '?', $handle );
 327  
 328              if ( ! in_array( $handle[0], $this->queue, true ) && isset( $this->registered[ $handle[0] ] ) ) {
 329                  $this->queue[] = $handle[0];
 330  
 331                  // Reset all dependencies so they must be recalculated in recurse_deps().
 332                  $this->all_queued_deps = null;
 333  
 334                  if ( isset( $handle[1] ) ) {
 335                      $this->args[ $handle[0] ] = $handle[1];
 336                  }
 337              }
 338          }
 339      }
 340  
 341      /**
 342       * Dequeue an item or items.
 343       *
 344       * Decodes handles and arguments, then dequeues handles
 345       * and removes arguments from the class property $args.
 346       *
 347       * @since 2.1.0
 348       * @since 2.6.0 Moved from `WP_Scripts`.
 349       *
 350       * @param string|string[] $handles Item handle (string) or item handles (array of strings).
 351       */
 352  	public function dequeue( $handles ) {
 353          foreach ( (array) $handles as $handle ) {
 354              $handle = explode( '?', $handle );
 355              $key    = array_search( $handle[0], $this->queue, true );
 356  
 357              if ( false !== $key ) {
 358                  // Reset all dependencies so they must be recalculated in recurse_deps().
 359                  $this->all_queued_deps = null;
 360  
 361                  unset( $this->queue[ $key ] );
 362                  unset( $this->args[ $handle[0] ] );
 363              }
 364          }
 365      }
 366  
 367      /**
 368       * Recursively search the passed dependency tree for a handle.
 369       *
 370       * @since 4.0.0
 371       *
 372       * @param string[] $queue  An array of queued _WP_Dependency handles.
 373       * @param string   $handle Name of the item. Should be unique.
 374       * @return bool Whether the handle is found after recursively searching the dependency tree.
 375       */
 376  	protected function recurse_deps( $queue, $handle ) {
 377          if ( isset( $this->all_queued_deps ) ) {
 378              return isset( $this->all_queued_deps[ $handle ] );
 379          }
 380  
 381          $all_deps = array_fill_keys( $queue, true );
 382          $queues   = array();
 383          $done     = array();
 384  
 385          while ( $queue ) {
 386              foreach ( $queue as $queued ) {
 387                  if ( ! isset( $done[ $queued ] ) && isset( $this->registered[ $queued ] ) ) {
 388                      $deps = $this->registered[ $queued ]->deps;
 389                      if ( $deps ) {
 390                          $all_deps += array_fill_keys( $deps, true );
 391                          array_push( $queues, $deps );
 392                      }
 393                      $done[ $queued ] = true;
 394                  }
 395              }
 396              $queue = array_pop( $queues );
 397          }
 398  
 399          $this->all_queued_deps = $all_deps;
 400  
 401          return isset( $this->all_queued_deps[ $handle ] );
 402      }
 403  
 404      /**
 405       * Query list for an item.
 406       *
 407       * @since 2.1.0
 408       * @since 2.6.0 Moved from `WP_Scripts`.
 409       *
 410       * @param string $handle Name of the item. Should be unique.
 411       * @param string $list   Optional. Property name of list array. Default 'registered'.
 412       * @return bool|_WP_Dependency Found, or object Item data.
 413       */
 414  	public function query( $handle, $list = 'registered' ) {
 415          switch ( $list ) {
 416              case 'registered':
 417              case 'scripts': // Back compat.
 418                  if ( isset( $this->registered[ $handle ] ) ) {
 419                      return $this->registered[ $handle ];
 420                  }
 421                  return false;
 422  
 423              case 'enqueued':
 424              case 'queue':
 425                  if ( in_array( $handle, $this->queue, true ) ) {
 426                      return true;
 427                  }
 428                  return $this->recurse_deps( $this->queue, $handle );
 429  
 430              case 'to_do':
 431              case 'to_print': // Back compat.
 432                  return in_array( $handle, $this->to_do, true );
 433  
 434              case 'done':
 435              case 'printed': // Back compat.
 436                  return in_array( $handle, $this->done, true );
 437          }
 438          return false;
 439      }
 440  
 441      /**
 442       * Set item group, unless already in a lower group.
 443       *
 444       * @since 2.8.0
 445       *
 446       * @param string    $handle    Name of the item. Should be unique.
 447       * @param bool      $recursion Internal flag that calling function was called recursively.
 448       * @param int|false $group     Group level: level (int), no group (false).
 449       * @return bool Not already in the group or a lower group.
 450       */
 451  	public function set_group( $handle, $recursion, $group ) {
 452          $group = (int) $group;
 453  
 454          if ( isset( $this->groups[ $handle ] ) && $this->groups[ $handle ] <= $group ) {
 455              return false;
 456          }
 457  
 458          $this->groups[ $handle ] = $group;
 459  
 460          return true;
 461      }
 462  
 463  }


Generated: Sun Sep 26 01:00:08 2021 Cross-referenced by PHPXref 0.7.1