1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Timber; |
4
|
|
|
|
5
|
|
|
use Timber\Helper; |
6
|
|
|
use Timber\Post; |
7
|
|
|
use Timber\PostGetter; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* A PostQuery allows a user to query for a Collection of WordPress Posts. |
11
|
|
|
* PostCollections are used directly in Twig templates to iterate through and retrieve |
12
|
|
|
* meta information about the collection of posts |
13
|
|
|
* @api |
14
|
|
|
* @package Timber |
15
|
|
|
*/ |
16
|
|
|
class PostQuery extends PostCollection { |
17
|
|
|
/** |
18
|
|
|
* Found posts. |
19
|
|
|
* |
20
|
|
|
* The total amount of posts found for this query. Will be `0` if you used `no_found_rows` as a |
21
|
|
|
* query parameter. Will be `null` if you passed in an existing collection of posts. |
22
|
|
|
* |
23
|
|
|
* @api |
24
|
|
|
* @since 1.11.1 |
25
|
|
|
* @var int The amount of posts found in the query. |
26
|
|
|
*/ |
27
|
|
|
public $found_posts = null; |
28
|
|
|
|
29
|
|
|
protected $userQuery; |
30
|
|
|
protected $queryIterator; |
31
|
|
|
protected $pagination = null; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param mixed $query |
35
|
|
|
* @param string $post_class |
36
|
|
|
*/ |
37
|
|
|
public function __construct( $query = false, $post_class = '\Timber\Post' ) { |
38
|
|
|
$this->userQuery = $query; |
39
|
|
|
$this->queryIterator = PostGetter::query_posts($query, $post_class); |
40
|
|
|
|
41
|
|
|
if ( $this->queryIterator instanceof QueryIterator ) { |
42
|
|
|
$this->found_posts = $this->queryIterator->found_posts(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$posts = $this->queryIterator->get_posts(); |
46
|
|
|
|
47
|
|
|
parent::__construct($posts, $post_class); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return mixed the query the user orignally passed |
52
|
|
|
* to the pagination object |
53
|
|
|
*/ |
54
|
|
|
protected function get_query() { |
55
|
|
|
return $this->userQuery; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Set pagination for the collection. Optionally could be used to get pagination with custom preferences. |
60
|
|
|
* |
61
|
|
|
* @param array $prefs |
62
|
|
|
* @return Timber\Pagination object |
|
|
|
|
63
|
|
|
*/ |
64
|
|
|
public function pagination( $prefs = array() ) { |
65
|
|
|
if ( !$this->pagination && is_a($this->queryIterator, 'Timber\QueryIterator') ) { |
66
|
|
|
$this->pagination = $this->queryIterator->get_pagination($prefs, $this->get_query()); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
return $this->pagination; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|