remove( $item ) { if ( $this->key_valid( $item ) ) { unset( $this->items[ $item ] ); } return $this; } /** * Clear collection * @return Current instance */ public function clear() { $this->items = array(); return $this; } /** * Checks if item exists in the collection * @param mixed $item Item(s) to check for * @return bool TRUE if item(s) in collection */ public function has( $items ) { // Attempt to locate item return false; } /** * Retrieve item(s) from collection * If no items specified, entire collection returned * @param array $args (optional) Query arguments * @return object|array Specified item(s) */ public function get( $args = null ) { $this->init(); // Parse args $args_default = array( 'orderby' => null, 'order' => 'DESC', 'include' => array(), 'exclude' => array(), ); $r = wp_parse_args( $args, $args_default ); $items = $this->items; /* Sort */ if ( ! is_null( $r['orderby'] ) ) { // Validate if ( ! is_array( $r['orderby'] ) ) { $r['orderby'] = array( 'item' => $r['orderby'] ); } // Prep $metas = ( isset( $r['orderby']['meta'] ) ) ? $this->items_meta : array(); // Sort foreach ( $r['orderby'] as $stype => $sval ) { /* Meta sorting */ if ( 'meta' === $stype ) { // Build sorting buckets $buckets = array(); foreach ( $metas as $item => $meta ) { if ( ! isset( $meta[ $sval ] ) ) { continue; } // Create bucket $idx = $meta[ $sval ]; if ( ! isset( $buckets[ $idx ] ) ) { $buckets[ $idx ] = array(); } // Add item to bucket $buckets[ $idx ][] = $item; } // Sort buckets ksort( $buckets, SORT_NUMERIC ); // Merge buckets $pool = array(); foreach ( $buckets as $bucket ) { $pool = array_merge( $pool, $bucket ); } // Fill with items $items = array_merge( array_fill_keys( $pool, null ), $items ); } } // Clear workers unset( $stype, $sval, $buckets, $pool, $item, $metas, $meta, $idx ); } return $items; } /* Metadata */ /** * Add metadata for item * @param string|int $item Item key * @param string|array $meta_key Meta key to set (or array of metadata) * @param mixed $meta_value (optional) Metadata value (if key set) * @param bool $reset (optional) Whether to remove existing metadata first (Default: FALSE) * @return object Current instance */ protected function add_meta( $item, $meta_key, $meta_value = null, $reset = false ) { // Validate if ( $this->key_valid( $item ) && ( is_array( $meta_key ) || is_string( $meta_key ) ) ) { // Prepare metadata $meta = ( is_string( $meta_key ) ) ? array( $meta_key => $meta_value ) : $meta_key; // Reset existing meta (if necessary) if ( is_array( $meta_key ) && func_num_args() > 2 ) { $reset = func_get_arg( 2 ); } if ( ! ! $reset ) { unset( $this->items_meta[ $item ] ); } // Add metadata if ( ! isset( $this->items_meta[ $item ] ) ) { $this->items_meta[ $item ] = array(); } $this->items_meta[ $item ] = array_merge( $this->items_meta[ $item ], $meta ); } return $this; } /** * Remove item metadata * @param string $item Item key * @return object Current instance */ protected function remove_meta( $item, $meta_key = null ) { if ( $this->key_valid( $item ) && isset( $this->items_meta[ $item ] ) ) { if ( is_string( $meta_key ) ) { // Remove specific meta value unset( $this->items_meta[ $item ][ $meta_key ] ); } else { // Remove all metadata unset( $this->items_meta[ $item ] ); } } return $this; } /** * Retrieve metadata * @param string $item Item key * @param string $meta_key (optional) Meta key (All metadata retrieved if no key specified) * @return mixed|null Metadata value */ protected function get_meta( $item, $meta_key = null ) { $ret = null; if ( $this->key_valid( $item ) && isset( $this->items_meta[ $item ] ) ) { if ( is_null( $meta_key ) ) { $ret = $this->items_meta[ $item ]; } elseif ( is_string( $meta_key ) && isset( $this->items_meta[ $item ][ $meta_key ] ) ) { $ret = $this->items_meta[ $item ][ $meta_key ]; } } return $ret; } /* Collection */ /** * Build entire collection of items * Prints output */ function build( $build_vars = array() ) { // Parse vars $this->parse_build_vars( $build_vars ); $this->util->do_action_ref_array( 'build_init', array( $this ) ); // Pre-build output $this->util->do_action_ref_array( 'build_pre', array( $this ) ); // Build groups $this->build_groups(); // Post-build output $this->util->do_action_ref_array( 'build_post', array( $this ) ); } /** * Parses build variables prior to use * @uses this->reset_build_vars() to reset build variables for each request * @param array $build_vars Variables to use for current request */ function parse_build_vars( $build_vars = array() ) { $this->reset_build_vars(); $this->build_vars = $this->util->apply_filters( 'parse_build_vars', wp_parse_args( $build_vars, $this->build_vars ), $this ); } /** * Reset build variables to defaults * Default Variables * > groups - array - Names of groups to build * > context - string - Context of current request * > layout - string - Name of default layout to use */ function reset_build_vars() { $this->build_vars = wp_parse_args( $this->build_vars, $this->build_vars_default ); } }