| 1 |
<?php |
| 2 |
/** |
| 3 |
* Responsible for returning grids. |
| 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 grids. |
| 14 |
* |
| 15 |
* @since 3.0.0 |
| 16 |
* @package WP_Ultimate_Post_Grid |
| 17 |
* @subpackage WP_Ultimate_Post_Grid/includes/public |
| 18 |
* @author Brecht Vandersmissen <brecht@bootstrapped.ventures> |
| 19 |
*/ |
| 20 |
class WPUPG_Grid_Manager { |
| 21 |
|
| 22 |
/** |
| 23 |
* Grids that have already been requested for easy subsequent access. |
| 24 |
* |
| 25 |
* @since 3.0.0 |
| 26 |
* @access private |
| 27 |
* @var array $grids Array containing grids that have already been requested for easy access. |
| 28 |
*/ |
| 29 |
private static $grids = array(); |
| 30 |
|
| 31 |
/** |
| 32 |
* Slugs that have already been requested for easy subsequent access. |
| 33 |
* |
| 34 |
* @since 3.0.0 |
| 35 |
* @access private |
| 36 |
* @var array $slugs Array containing slugs that have already been requested for easy access. |
| 37 |
*/ |
| 38 |
private static $slugs = array(); |
| 39 |
|
| 40 |
/** |
| 41 |
* Get all grid IDs. |
| 42 |
* |
| 43 |
* @since 3.0.0 |
| 44 |
*/ |
| 45 |
public static function get_grid_ids() { |
| 46 |
$args = array( |
| 47 |
'post_type' => WPUPG_POST_TYPE, |
| 48 |
'post_status' => 'any', |
| 49 |
'nopaging' => true, |
| 50 |
'fields' => 'ids', |
| 51 |
); |
| 52 |
|
| 53 |
$query = new WP_Query( $args ); |
| 54 |
|
| 55 |
return $query->posts; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Get grids with their filters. |
| 60 |
* |
| 61 |
* @since 3.0.0 |
| 62 |
*/ |
| 63 |
public static function get_grids() { |
| 64 |
$grids = array(); |
| 65 |
|
| 66 |
$args = array( |
| 67 |
'post_type' => WPUPG_POST_TYPE, |
| 68 |
'post_status' => 'any', |
| 69 |
'orderby' => 'date', |
| 70 |
'order' => 'DESC', |
| 71 |
'posts_per_page' => -1, |
| 72 |
'offset' => 0, |
| 73 |
); |
| 74 |
|
| 75 |
$query = new WP_Query( $args ); |
| 76 |
|
| 77 |
if ( $query->have_posts() ) { |
| 78 |
$posts = $query->posts; |
| 79 |
|
| 80 |
foreach ( $posts as $post ) { |
| 81 |
$slug = $post->post_name; |
| 82 |
$grids[] = array( |
| 83 |
'id' => $post->ID, |
| 84 |
'name' => $post->post_title, |
| 85 |
'slug' => $slug ? $slug : $post->ID, |
| 86 |
); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
return $grids; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Get grid object by ID. |
| 95 |
* |
| 96 |
* @since 3.0.0 |
| 97 |
* @param mixed $slug_id_post Slug, ID or Post Object for the grid we want. |
| 98 |
*/ |
| 99 |
public static function get_grid( $slug_id_or_post ) { |
| 100 |
if ( is_object( $slug_id_or_post ) && $slug_id_or_post instanceof WP_Post ) { |
| 101 |
$post = $slug_id_or_post; |
| 102 |
$grid_id = $post->ID; |
| 103 |
} else { |
| 104 |
// Check if it's a slug. |
| 105 |
$post = false; |
| 106 |
$grid_id = self::get_grid_id_from_slug( $slug_id_or_post ); |
| 107 |
|
| 108 |
if ( ! $grid_id ) { |
| 109 |
$grid_id = intval( $slug_id_or_post ); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
// Only get new grid object if it hasn't been retrieved before. |
| 114 |
if ( ! array_key_exists( $grid_id, self::$grids ) ) { |
| 115 |
if ( ! $post ) { |
| 116 |
$post = get_post( $grid_id ); |
| 117 |
} |
| 118 |
|
| 119 |
if ( $post instanceof WP_Post && WPUPG_POST_TYPE === $post->post_type ) { |
| 120 |
$grid = new WPUPG_Grid( $post ); |
| 121 |
} else { |
| 122 |
$grid = false; |
| 123 |
} |
| 124 |
|
| 125 |
self::$grids[ $grid_id ] = $grid; |
| 126 |
} |
| 127 |
|
| 128 |
return self::$grids[ $grid_id ]; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Get grid ID by its slug. |
| 133 |
* |
| 134 |
* @since 3.0.0 |
| 135 |
* @param mixed $slug Slug of the grid we want. |
| 136 |
*/ |
| 137 |
public static function get_grid_id_from_slug( $slug ) { |
| 138 |
if ( ! array_key_exists( $slug, self::$slugs ) ) { |
| 139 |
$post = get_page_by_path( $slug, OBJECT, WPUPG_POST_TYPE ); |
| 140 |
|
| 141 |
if ( ! is_null( $post ) ) { |
| 142 |
$grid_id = $post->ID; |
| 143 |
} else { |
| 144 |
$grid_id = false; |
| 145 |
} |
| 146 |
|
| 147 |
self::$slugs[ $slug ] = $grid_id; |
| 148 |
} |
| 149 |
|
| 150 |
return self::$slugs[ $slug ]; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Invalidate cached grid. |
| 155 |
* |
| 156 |
* @since 3.0.0 |
| 157 |
* @param int $grid_id ID of the grid to invalidate. |
| 158 |
*/ |
| 159 |
public static function invalidate_grid( $grid_id ) { |
| 160 |
if ( array_key_exists( $grid_id, self::$grids ) ) { |
| 161 |
unset( self::$grids[ $grid_id ] ); |
| 162 |
} |
| 163 |
} |
| 164 |
} |