1 | <?php |
||
7 | class TimberPostsCollection extends ArrayObject { |
||
8 | |||
9 | public function __construct( $posts = array(), $post_class = 'TimberPost' ) { |
||
10 | $returned_posts = array(); |
||
11 | if ( is_null( $posts ) ){ |
||
12 | $posts = array(); |
||
13 | } |
||
14 | foreach ( $posts as $post_object ) { |
||
15 | $post_class_use = $post_class; |
||
16 | |||
17 | if ( is_array( $post_class ) ) { |
||
18 | $post_type = get_post_type( $post_object ); |
||
19 | $post_class_use = 'TimberPost'; |
||
20 | |||
21 | if ( isset( $post_class[$post_type] ) ) { |
||
22 | $post_class_use = $post_class[$post_type]; |
||
23 | |||
24 | } else { |
||
25 | if ( is_array( $post_class ) ) { |
||
26 | TimberHelper::error_log( $post_type . ' of ' . $post_object->ID . ' not found in ' . print_r( $post_class, true ) ); |
||
27 | } else { |
||
28 | TimberHelper::error_log( $post_type . ' not found in ' . $post_class ); |
||
29 | } |
||
30 | } |
||
31 | } |
||
32 | |||
33 | // Don't create yet another object if $post_object is already of the right type |
||
34 | if ( is_a( $post_object, $post_class_use ) ) { |
||
35 | $post = $post_object; |
||
36 | } else { |
||
37 | $post = new $post_class_use( $post_object ); |
||
38 | } |
||
39 | |||
40 | if ( isset( $post->ID ) ) { |
||
41 | $returned_posts[] = $post; |
||
42 | } |
||
43 | } |
||
44 | |||
45 | $returned_posts = self::maybe_set_preview($returned_posts); |
||
46 | |||
47 | parent::__construct( $returned_posts, $flags = 0, 'TimberPostsIterator' ); |
||
48 | } |
||
49 | |||
50 | public function get_posts() { |
||
53 | |||
54 | /** |
||
55 | * @param array $posts |
||
56 | * @return array |
||
57 | */ |
||
58 | static function maybe_set_preview( $posts ) { |
||
87 | |||
88 | } |
||
89 | |||
98 |
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.