1 | <?php |
||
3 | class TimberPostGetter { |
||
4 | |||
5 | /** |
||
6 | * @param mixed $query |
||
7 | * @param string $PostClass |
||
8 | * @return array|bool|null |
||
9 | */ |
||
10 | static function get_post($query = false, $PostClass = 'TimberPost') { |
||
|
|||
11 | $posts = self::get_posts( $query, $PostClass ); |
||
12 | if ( $post = reset($posts ) ) { |
||
13 | return $post; |
||
14 | } |
||
15 | } |
||
16 | |||
17 | static function get_posts( $query = false, $PostClass = 'TimberPost', $return_collection = false ) { |
||
21 | |||
22 | static function query_post( $query = false, $PostClass = 'TimberPost' ) { |
||
23 | $posts = self::query_posts( $query, $PostClass ); |
||
24 | if ( method_exists($posts, 'current') && $post = $posts->current() ) { |
||
25 | return $post; |
||
26 | } |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * @param mixed $query |
||
31 | * @param string $PostClass |
||
32 | * @return array|bool|null |
||
33 | */ |
||
34 | static function query_posts($query = false, $PostClass = 'TimberPost' ) { |
||
35 | if (self::is_post_class_or_class_map($query)) { |
||
36 | $PostClass = $query; |
||
37 | $query = false; |
||
38 | } |
||
39 | |||
40 | if (is_object($query) && !is_a($query, 'WP_Query') ){ |
||
41 | // The only object other than a query is a type of post object |
||
42 | $query = array( $query ); |
||
43 | } |
||
44 | |||
45 | if ( is_array( $query ) && count( $query ) && isset( $query[0] ) && is_object( $query[0] ) ) { |
||
46 | // We have an array of post objects that already have data |
||
47 | return new TimberPostsCollection( $query, $PostClass ); |
||
48 | } else { |
||
49 | // We have a query (of sorts) to work with |
||
50 | $tqi = new TimberQueryIterator( $query, $PostClass ); |
||
51 | return $tqi; |
||
52 | } |
||
53 | } |
||
54 | |||
55 | static function get_pids($query){ |
||
56 | $posts = self::get_posts($query); |
||
57 | $pids = array(); |
||
58 | foreach($posts as $post){ |
||
59 | if (isset($post->ID)){ |
||
60 | $pids[] = $post->ID; |
||
61 | } |
||
62 | } |
||
63 | return $pids; |
||
64 | } |
||
65 | |||
66 | static function loop_to_id() { |
||
79 | |||
80 | /** |
||
81 | * @return bool |
||
82 | */ |
||
83 | static function wp_query_has_posts() { |
||
87 | |||
88 | /** |
||
89 | * @param string|array $arg |
||
90 | * @return bool |
||
91 | */ |
||
92 | static function is_post_class_or_class_map($arg){ |
||
104 | } |
||
105 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.