home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress / wp-includes / class.wp-dependencies.php < prev    next >
Encoding:
PHP Script  |  2017-07-26  |  10.9 KB  |  397 lines

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