| 1 |
<?php |
| 2 |
/** |
| 3 |
* Responsible for returning items. |
| 4 |
* |
| 5 |
* @link https://bootstrapped.ventures |
| 6 |
* @since 3.0.0 |
| 7 |
* |
| 8 |
* @package WP_Ultimate_Post_Grid |
| 9 |
* @subpackage WP_Ultimate_Post_Grid/includes/public |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* Responsible for returning items. |
| 14 |
* |
| 15 |
* @since 3.0.0 |
| 16 |
* @package WP_Ultimate_Post_Grid |
| 17 |
* @subpackage WP_Ultimate_Post_Grid/includes/public |
| 18 |
* @author Brecht Vandersmissen <[email protected]> |
| 19 |
*/ |
| 20 |
class WPUPG_Item_Manager { |
| 21 |
|
| 22 |
/** |
| 23 |
* Items that have already been requested for easy subsequent access. |
| 24 |
* |
| 25 |
* @since 3.0.0 |
| 26 |
* @access private |
| 27 |
* @var array $items Array containing items that have already been requested for easy access. |
| 28 |
*/ |
| 29 |
private static $items = array( |
| 30 |
'posts' => array(), |
| 31 |
'terms' => array(), |
| 32 |
); |
| 33 |
|
| 34 |
/** |
| 35 |
* Get item object by ID. |
| 36 |
* |
| 37 |
* @since 3.0.0 |
| 38 |
* @param mixed $slug_id_post Slug, ID or Post Object for the item we want. |
| 39 |
* @param mixed $grid_type Type of grid we want to get the item for. |
| 40 |
*/ |
| 41 |
public static function get_item( $id, $grid_type = 'posts' ) { |
| 42 |
$id = intval( $id ); |
| 43 |
|
| 44 |
// Only get new item object if it hasn't been retrieved before. |
| 45 |
if ( ! array_key_exists( $id, self::$items[ $grid_type ] ) ) { |
| 46 |
if ( 'posts' === $grid_type ) { |
| 47 |
self::$items[ 'posts' ][ $id ] = new WPUPG_Item_Post( $id ); |
| 48 |
} else { |
| 49 |
self::$items[ 'terms' ][ $id ] = class_exists( 'WPUPGP_Item_Term' ) ? new WPUPGP_Item_Term( $id ) : new WPUPG_Item( array() ); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
return self::$items[ $grid_type ][ $id ]; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Invalidate cached item. |
| 58 |
* |
| 59 |
* @since 3.0.0 |
| 60 |
* @param int $id ID of the item to invalidate. |
| 61 |
* @param mixed $grid_type Type of grid we want to invalidate the item for. |
| 62 |
*/ |
| 63 |
public static function invalidate_item( $id, $grid_type = 'posts' ) { |
| 64 |
$id = intval( $id ); |
| 65 |
|
| 66 |
if ( array_key_exists( $id, self::$items[ $grid_type ] ) ) { |
| 67 |
unset( self::$items[ $grid_type ][ $id ] ); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|