@@ -2,96 +2,96 @@ |
||
2 | 2 | |
3 | 3 | // Exit if accessed directly |
4 | 4 | if ( !defined( 'ABSPATH' ) ) |
5 | - exit; |
|
5 | + exit; |
|
6 | 6 | |
7 | 7 | class TimberPostsCollection extends ArrayObject { |
8 | 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() { |
|
51 | - return $this->getArrayCopy(); |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * @param array $posts |
|
56 | - * @return array |
|
57 | - */ |
|
58 | - static function maybe_set_preview( $posts ) { |
|
59 | - if ( is_array( $posts ) && isset( $_GET['preview'] ) && $_GET['preview'] |
|
60 | - && isset( $_GET['preview_id'] ) && $_GET['preview_id'] |
|
61 | - && current_user_can( 'edit_post', $_GET['preview_id'] ) ) { |
|
62 | - // No need to check the nonce, that already happened in _show_post_preview on init |
|
63 | - |
|
64 | - $preview_id = $_GET['preview_id']; |
|
65 | - foreach( $posts as &$post ) { |
|
66 | - if ( is_object( $post ) && $post->ID == $preview_id ) { |
|
67 | - // Based on _set_preview( $post ), but adds import_custom |
|
68 | - $preview = wp_get_post_autosave( $preview_id ); |
|
69 | - if ( is_object($preview) ) { |
|
70 | - |
|
71 | - $preview = sanitize_post($preview); |
|
72 | - |
|
73 | - $post->post_content = $preview->post_content; |
|
74 | - $post->post_title = $preview->post_title; |
|
75 | - $post->post_excerpt = $preview->post_excerpt; |
|
76 | - $post->import_custom( $preview_id ); |
|
77 | - |
|
78 | - add_filter( 'get_the_terms', '_wp_preview_terms_filter', 10, 3 ); |
|
79 | - } |
|
80 | - } |
|
81 | - } |
|
82 | - |
|
83 | - } |
|
84 | - |
|
85 | - return $posts; |
|
86 | - } |
|
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() { |
|
51 | + return $this->getArrayCopy(); |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * @param array $posts |
|
56 | + * @return array |
|
57 | + */ |
|
58 | + static function maybe_set_preview( $posts ) { |
|
59 | + if ( is_array( $posts ) && isset( $_GET['preview'] ) && $_GET['preview'] |
|
60 | + && isset( $_GET['preview_id'] ) && $_GET['preview_id'] |
|
61 | + && current_user_can( 'edit_post', $_GET['preview_id'] ) ) { |
|
62 | + // No need to check the nonce, that already happened in _show_post_preview on init |
|
63 | + |
|
64 | + $preview_id = $_GET['preview_id']; |
|
65 | + foreach( $posts as &$post ) { |
|
66 | + if ( is_object( $post ) && $post->ID == $preview_id ) { |
|
67 | + // Based on _set_preview( $post ), but adds import_custom |
|
68 | + $preview = wp_get_post_autosave( $preview_id ); |
|
69 | + if ( is_object($preview) ) { |
|
70 | + |
|
71 | + $preview = sanitize_post($preview); |
|
72 | + |
|
73 | + $post->post_content = $preview->post_content; |
|
74 | + $post->post_title = $preview->post_title; |
|
75 | + $post->post_excerpt = $preview->post_excerpt; |
|
76 | + $post->import_custom( $preview_id ); |
|
77 | + |
|
78 | + add_filter( 'get_the_terms', '_wp_preview_terms_filter', 10, 3 ); |
|
79 | + } |
|
80 | + } |
|
81 | + } |
|
82 | + |
|
83 | + } |
|
84 | + |
|
85 | + return $posts; |
|
86 | + } |
|
87 | 87 | |
88 | 88 | } |
89 | 89 | |
90 | 90 | class TimberPostsIterator extends ArrayIterator { |
91 | 91 | |
92 | - public function current() { |
|
93 | - global $post; |
|
94 | - $post = parent::current(); |
|
95 | - return $post; |
|
96 | - } |
|
92 | + public function current() { |
|
93 | + global $post; |
|
94 | + $post = parent::current(); |
|
95 | + return $post; |
|
96 | + } |
|
97 | 97 | } |
@@ -1,156 +1,156 @@ |
||
1 | 1 | <?php |
2 | 2 | // Exit if accessed directly |
3 | 3 | if ( !defined( 'ABSPATH' ) ) |
4 | - exit; |
|
4 | + exit; |
|
5 | 5 | |
6 | 6 | class TimberQueryIterator implements Iterator { |
7 | 7 | |
8 | - /** |
|
9 | - * |
|
10 | - * |
|
11 | - * @var WP_Query |
|
12 | - */ |
|
13 | - private $_query = null; |
|
14 | - private $_posts_class = 'TimberPost'; |
|
15 | - |
|
16 | - public function __construct( $query = false, $posts_class = 'TimberPost' ) { |
|
17 | - add_action( 'pre_get_posts', array($this, 'fix_number_posts_wp_quirk' )); |
|
18 | - if ( $posts_class ) |
|
19 | - $this->_posts_class = $posts_class; |
|
20 | - |
|
21 | - if ( is_a( $query, 'WP_Query' ) ) { |
|
22 | - // We got a full-fledged WP Query, look no further! |
|
23 | - $the_query = $query; |
|
24 | - |
|
25 | - } elseif ( false === $query ) { |
|
26 | - // If query is explicitly set to false, use the main loop |
|
27 | - global $wp_query; |
|
28 | - $the_query =& $wp_query; |
|
29 | - //if we're on a custom posts page? |
|
30 | - $the_query = self::handle_maybe_custom_posts_page($the_query); |
|
31 | - } elseif ( TimberHelper::is_array_assoc( $query ) || ( is_string( $query ) && strstr( $query, '=' ) ) ) { |
|
32 | - // We have a regularly formed WP query string or array to use |
|
33 | - $the_query = new WP_Query( $query ); |
|
34 | - |
|
35 | - } elseif ( is_numeric( $query ) || is_string( $query ) ) { |
|
36 | - // We have what could be a post name or post ID to pull out |
|
37 | - $the_query = self::get_query_from_string( $query ); |
|
38 | - |
|
39 | - } elseif ( is_array( $query ) && count( $query ) && ( is_integer( $query[0] ) || is_string( $query[0] ) ) ) { |
|
40 | - // We have a list of pids (post IDs) to extract from |
|
41 | - $the_query = self::get_query_from_array_of_ids( $query ); |
|
42 | - } elseif ( is_array($query) && empty($query)) { |
|
43 | - // it's an empty array |
|
44 | - $the_query = array(); |
|
45 | - } else { |
|
46 | - TimberHelper::error_log( 'I have failed you! in ' . basename( __FILE__ ) . '::' . __LINE__ ); |
|
47 | - TimberHelper::error_log( $query ); |
|
48 | - |
|
49 | - // We have failed hard, at least let get something. |
|
50 | - $the_query = new WP_Query(); |
|
51 | - } |
|
52 | - |
|
53 | - $this->_query = $the_query; |
|
54 | - |
|
55 | - } |
|
56 | - |
|
57 | - public function get_posts( $return_collection = false ) { |
|
58 | - if (isset($this->_query->posts)){ |
|
59 | - $posts = new TimberPostsCollection( $this->_query->posts, $this->_posts_class ); |
|
60 | - return ( $return_collection ) ? $posts : $posts->get_posts(); |
|
61 | - } |
|
62 | - } |
|
63 | - |
|
64 | - // |
|
65 | - // GET POSTS |
|
66 | - // |
|
67 | - public static function get_query_from_array_of_ids( $query = array() ) { |
|
68 | - if ( !is_array( $query ) || !count( $query ) ) |
|
69 | - return null; |
|
70 | - |
|
71 | - return new WP_Query( array( |
|
72 | - 'post_type'=> 'any', |
|
73 | - 'ignore_sticky_posts' => true, |
|
74 | - 'post__in' => $query, |
|
75 | - 'orderby' => 'post__in', |
|
76 | - 'nopaging' => true |
|
77 | - ) ); |
|
78 | - } |
|
79 | - |
|
80 | - public static function get_query_from_string( $string = '' ) { |
|
81 | - $post_type = false; |
|
82 | - |
|
83 | - if ( is_string( $string ) && strstr( $string, '#' ) ) { |
|
84 | - //we have a post_type directive here |
|
85 | - list( $post_type, $string ) = explode( '#', $string ); |
|
86 | - } |
|
87 | - |
|
88 | - $query = array( |
|
89 | - 'post_type' => ( $post_type ) ? $post_type : 'any' |
|
90 | - ); |
|
91 | - |
|
92 | - if ( is_numeric( $string ) ) { |
|
93 | - $query['p'] = $string; |
|
94 | - |
|
95 | - } else { |
|
96 | - $query['name'] = $string; |
|
97 | - } |
|
98 | - |
|
99 | - return new WP_Query( $query ); |
|
100 | - } |
|
101 | - |
|
102 | - // |
|
103 | - // Iterator Interface |
|
104 | - // |
|
105 | - |
|
106 | - public function valid() { |
|
107 | - return $this->_query->have_posts(); |
|
108 | - } |
|
109 | - |
|
110 | - public function current() { |
|
111 | - global $post; |
|
112 | - |
|
113 | - $this->_query->the_post(); |
|
114 | - |
|
115 | - // Sets up the global post, but also return the post, for use in Twig template |
|
116 | - $posts_class = $this->_posts_class; |
|
117 | - return new $posts_class( $post ); |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Don't implement next, because current already advances the loop |
|
122 | - */ |
|
123 | - final public function next() {} |
|
124 | - |
|
125 | - public function rewind() { |
|
126 | - $this->_query->rewind_posts(); |
|
127 | - } |
|
128 | - |
|
129 | - public function key() { |
|
130 | - $this->_query->current_post; |
|
131 | - } |
|
132 | - |
|
133 | - //get_posts users numberposts |
|
134 | - static function fix_number_posts_wp_quirk( $query ) { |
|
135 | - if (isset($query->query) && isset($query->query['numberposts']) |
|
136 | - && !isset($query->query['posts_per_page'])) { |
|
137 | - $query->set( 'posts_per_page', $query->query['numberposts'] ); |
|
138 | - } |
|
139 | - return $query; |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * this will test for whether a custom page to display posts is active, and if so, set the query to the default |
|
144 | - * @param WP_Query $query the original query recived from WordPress |
|
145 | - * @return WP_Query |
|
146 | - */ |
|
147 | - static function handle_maybe_custom_posts_page( $query ) { |
|
148 | - if ($custom_posts_page = get_option('page_for_posts')) { |
|
149 | - if ( isset($query->query['p']) && $query->query['p'] == $custom_posts_page ) { |
|
150 | - return new WP_Query(array('post_type' => 'post')); |
|
151 | - } |
|
152 | - } |
|
153 | - return $query; |
|
154 | - } |
|
8 | + /** |
|
9 | + * |
|
10 | + * |
|
11 | + * @var WP_Query |
|
12 | + */ |
|
13 | + private $_query = null; |
|
14 | + private $_posts_class = 'TimberPost'; |
|
15 | + |
|
16 | + public function __construct( $query = false, $posts_class = 'TimberPost' ) { |
|
17 | + add_action( 'pre_get_posts', array($this, 'fix_number_posts_wp_quirk' )); |
|
18 | + if ( $posts_class ) |
|
19 | + $this->_posts_class = $posts_class; |
|
20 | + |
|
21 | + if ( is_a( $query, 'WP_Query' ) ) { |
|
22 | + // We got a full-fledged WP Query, look no further! |
|
23 | + $the_query = $query; |
|
24 | + |
|
25 | + } elseif ( false === $query ) { |
|
26 | + // If query is explicitly set to false, use the main loop |
|
27 | + global $wp_query; |
|
28 | + $the_query =& $wp_query; |
|
29 | + //if we're on a custom posts page? |
|
30 | + $the_query = self::handle_maybe_custom_posts_page($the_query); |
|
31 | + } elseif ( TimberHelper::is_array_assoc( $query ) || ( is_string( $query ) && strstr( $query, '=' ) ) ) { |
|
32 | + // We have a regularly formed WP query string or array to use |
|
33 | + $the_query = new WP_Query( $query ); |
|
34 | + |
|
35 | + } elseif ( is_numeric( $query ) || is_string( $query ) ) { |
|
36 | + // We have what could be a post name or post ID to pull out |
|
37 | + $the_query = self::get_query_from_string( $query ); |
|
38 | + |
|
39 | + } elseif ( is_array( $query ) && count( $query ) && ( is_integer( $query[0] ) || is_string( $query[0] ) ) ) { |
|
40 | + // We have a list of pids (post IDs) to extract from |
|
41 | + $the_query = self::get_query_from_array_of_ids( $query ); |
|
42 | + } elseif ( is_array($query) && empty($query)) { |
|
43 | + // it's an empty array |
|
44 | + $the_query = array(); |
|
45 | + } else { |
|
46 | + TimberHelper::error_log( 'I have failed you! in ' . basename( __FILE__ ) . '::' . __LINE__ ); |
|
47 | + TimberHelper::error_log( $query ); |
|
48 | + |
|
49 | + // We have failed hard, at least let get something. |
|
50 | + $the_query = new WP_Query(); |
|
51 | + } |
|
52 | + |
|
53 | + $this->_query = $the_query; |
|
54 | + |
|
55 | + } |
|
56 | + |
|
57 | + public function get_posts( $return_collection = false ) { |
|
58 | + if (isset($this->_query->posts)){ |
|
59 | + $posts = new TimberPostsCollection( $this->_query->posts, $this->_posts_class ); |
|
60 | + return ( $return_collection ) ? $posts : $posts->get_posts(); |
|
61 | + } |
|
62 | + } |
|
63 | + |
|
64 | + // |
|
65 | + // GET POSTS |
|
66 | + // |
|
67 | + public static function get_query_from_array_of_ids( $query = array() ) { |
|
68 | + if ( !is_array( $query ) || !count( $query ) ) |
|
69 | + return null; |
|
70 | + |
|
71 | + return new WP_Query( array( |
|
72 | + 'post_type'=> 'any', |
|
73 | + 'ignore_sticky_posts' => true, |
|
74 | + 'post__in' => $query, |
|
75 | + 'orderby' => 'post__in', |
|
76 | + 'nopaging' => true |
|
77 | + ) ); |
|
78 | + } |
|
79 | + |
|
80 | + public static function get_query_from_string( $string = '' ) { |
|
81 | + $post_type = false; |
|
82 | + |
|
83 | + if ( is_string( $string ) && strstr( $string, '#' ) ) { |
|
84 | + //we have a post_type directive here |
|
85 | + list( $post_type, $string ) = explode( '#', $string ); |
|
86 | + } |
|
87 | + |
|
88 | + $query = array( |
|
89 | + 'post_type' => ( $post_type ) ? $post_type : 'any' |
|
90 | + ); |
|
91 | + |
|
92 | + if ( is_numeric( $string ) ) { |
|
93 | + $query['p'] = $string; |
|
94 | + |
|
95 | + } else { |
|
96 | + $query['name'] = $string; |
|
97 | + } |
|
98 | + |
|
99 | + return new WP_Query( $query ); |
|
100 | + } |
|
101 | + |
|
102 | + // |
|
103 | + // Iterator Interface |
|
104 | + // |
|
105 | + |
|
106 | + public function valid() { |
|
107 | + return $this->_query->have_posts(); |
|
108 | + } |
|
109 | + |
|
110 | + public function current() { |
|
111 | + global $post; |
|
112 | + |
|
113 | + $this->_query->the_post(); |
|
114 | + |
|
115 | + // Sets up the global post, but also return the post, for use in Twig template |
|
116 | + $posts_class = $this->_posts_class; |
|
117 | + return new $posts_class( $post ); |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Don't implement next, because current already advances the loop |
|
122 | + */ |
|
123 | + final public function next() {} |
|
124 | + |
|
125 | + public function rewind() { |
|
126 | + $this->_query->rewind_posts(); |
|
127 | + } |
|
128 | + |
|
129 | + public function key() { |
|
130 | + $this->_query->current_post; |
|
131 | + } |
|
132 | + |
|
133 | + //get_posts users numberposts |
|
134 | + static function fix_number_posts_wp_quirk( $query ) { |
|
135 | + if (isset($query->query) && isset($query->query['numberposts']) |
|
136 | + && !isset($query->query['posts_per_page'])) { |
|
137 | + $query->set( 'posts_per_page', $query->query['numberposts'] ); |
|
138 | + } |
|
139 | + return $query; |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * this will test for whether a custom page to display posts is active, and if so, set the query to the default |
|
144 | + * @param WP_Query $query the original query recived from WordPress |
|
145 | + * @return WP_Query |
|
146 | + */ |
|
147 | + static function handle_maybe_custom_posts_page( $query ) { |
|
148 | + if ($custom_posts_page = get_option('page_for_posts')) { |
|
149 | + if ( isset($query->query['p']) && $query->query['p'] == $custom_posts_page ) { |
|
150 | + return new WP_Query(array('post_type' => 'post')); |
|
151 | + } |
|
152 | + } |
|
153 | + return $query; |
|
154 | + } |
|
155 | 155 | |
156 | 156 | } |
@@ -64,7 +64,7 @@ |
||
64 | 64 | |
65 | 65 | /** @api |
66 | 66 | * @var string for people who like trackback spam |
67 | - */ |
|
67 | + */ |
|
68 | 68 | public $pingback_url; |
69 | 69 | public $siteurl; |
70 | 70 | /** |
@@ -3,180 +3,180 @@ |
||
3 | 3 | class TimberTermGetter |
4 | 4 | { |
5 | 5 | |
6 | - /** |
|
7 | - * @param string|array $args |
|
8 | - * @param array $maybe_args |
|
9 | - * @param string $TermClass |
|
10 | - * @return mixed |
|
11 | - */ |
|
12 | - public static function get_terms($args = null, $maybe_args = array(), $TermClass = 'TimberTerm'){ |
|
13 | - if (is_string($maybe_args) && !strstr($maybe_args, '=')){ |
|
14 | - //the user is sending the $TermClass in the second argument |
|
15 | - $TermClass = $maybe_args; |
|
16 | - } |
|
17 | - if (is_string($maybe_args) && strstr($maybe_args, '=')){ |
|
18 | - parse_str($maybe_args, $maybe_args); |
|
19 | - } |
|
20 | - if (is_string($args) && strstr($args, '=')){ |
|
21 | - //a string and a query string! |
|
22 | - $parsed = TimberTermGetter::get_term_query_from_query_string($args); |
|
23 | - if (is_array($maybe_args)){ |
|
24 | - $parsed->args = array_merge($parsed->args, $maybe_args); |
|
25 | - } |
|
26 | - return self::handle_term_query($parsed->taxonomies, $parsed->args, $TermClass); |
|
27 | - } else if (is_string($args)){ |
|
28 | - //its just a string with a single taxonomy |
|
29 | - $parsed = TimberTermGetter::get_term_query_from_string($args); |
|
30 | - if (is_array($maybe_args)){ |
|
31 | - $parsed->args = array_merge($parsed->args, $maybe_args); |
|
32 | - } |
|
33 | - return self::handle_term_query($parsed->taxonomies, $parsed->args, $TermClass); |
|
34 | - } else if (is_array($args) && TimberHelper::is_array_assoc($args)){ |
|
35 | - //its an associative array, like a good ole query |
|
36 | - $parsed = TimberTermGetter::get_term_query_from_assoc_array($args); |
|
37 | - return self::handle_term_query($parsed->taxonomies, $parsed->args, $TermClass); |
|
38 | - } else if (is_array($args)){ |
|
39 | - //its just an array of strings or IDs (hopefully) |
|
40 | - $parsed = TimberTermGetter::get_term_query_from_array($args); |
|
41 | - if (is_array($maybe_args)){ |
|
42 | - $parsed->args = array_merge($parsed->args, $maybe_args); |
|
43 | - } |
|
44 | - return self::handle_term_query($parsed->taxonomies, $parsed->args, $TermClass); |
|
45 | - } else if (is_null($args)) { |
|
46 | - return self::handle_term_query(get_taxonomies(), array(), $TermClass); |
|
47 | - } |
|
48 | - return null; |
|
49 | - } |
|
6 | + /** |
|
7 | + * @param string|array $args |
|
8 | + * @param array $maybe_args |
|
9 | + * @param string $TermClass |
|
10 | + * @return mixed |
|
11 | + */ |
|
12 | + public static function get_terms($args = null, $maybe_args = array(), $TermClass = 'TimberTerm'){ |
|
13 | + if (is_string($maybe_args) && !strstr($maybe_args, '=')){ |
|
14 | + //the user is sending the $TermClass in the second argument |
|
15 | + $TermClass = $maybe_args; |
|
16 | + } |
|
17 | + if (is_string($maybe_args) && strstr($maybe_args, '=')){ |
|
18 | + parse_str($maybe_args, $maybe_args); |
|
19 | + } |
|
20 | + if (is_string($args) && strstr($args, '=')){ |
|
21 | + //a string and a query string! |
|
22 | + $parsed = TimberTermGetter::get_term_query_from_query_string($args); |
|
23 | + if (is_array($maybe_args)){ |
|
24 | + $parsed->args = array_merge($parsed->args, $maybe_args); |
|
25 | + } |
|
26 | + return self::handle_term_query($parsed->taxonomies, $parsed->args, $TermClass); |
|
27 | + } else if (is_string($args)){ |
|
28 | + //its just a string with a single taxonomy |
|
29 | + $parsed = TimberTermGetter::get_term_query_from_string($args); |
|
30 | + if (is_array($maybe_args)){ |
|
31 | + $parsed->args = array_merge($parsed->args, $maybe_args); |
|
32 | + } |
|
33 | + return self::handle_term_query($parsed->taxonomies, $parsed->args, $TermClass); |
|
34 | + } else if (is_array($args) && TimberHelper::is_array_assoc($args)){ |
|
35 | + //its an associative array, like a good ole query |
|
36 | + $parsed = TimberTermGetter::get_term_query_from_assoc_array($args); |
|
37 | + return self::handle_term_query($parsed->taxonomies, $parsed->args, $TermClass); |
|
38 | + } else if (is_array($args)){ |
|
39 | + //its just an array of strings or IDs (hopefully) |
|
40 | + $parsed = TimberTermGetter::get_term_query_from_array($args); |
|
41 | + if (is_array($maybe_args)){ |
|
42 | + $parsed->args = array_merge($parsed->args, $maybe_args); |
|
43 | + } |
|
44 | + return self::handle_term_query($parsed->taxonomies, $parsed->args, $TermClass); |
|
45 | + } else if (is_null($args)) { |
|
46 | + return self::handle_term_query(get_taxonomies(), array(), $TermClass); |
|
47 | + } |
|
48 | + return null; |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * @param string|array $taxonomies |
|
53 | - * @param string|array $args |
|
54 | - * @param string $TermClass |
|
55 | - * @return mixed |
|
56 | - */ |
|
57 | - public static function handle_term_query($taxonomies, $args, $TermClass){ |
|
58 | - if (!isset($args['hide_empty'])){ |
|
59 | - $args['hide_empty'] = false; |
|
60 | - } |
|
61 | - if (isset($args['term_id']) && is_int($args['term_id'])){ |
|
62 | - $args['term_id'] = array($args['term_id']); |
|
63 | - } |
|
64 | - if (isset($args['term_id'])){ |
|
65 | - $args['include'] = $args['term_id']; |
|
66 | - } |
|
67 | - $terms = get_terms($taxonomies, $args); |
|
68 | - foreach($terms as &$term){ |
|
69 | - $term = new $TermClass($term->term_id, $term->taxonomy); |
|
70 | - } |
|
71 | - return $terms; |
|
72 | - } |
|
51 | + /** |
|
52 | + * @param string|array $taxonomies |
|
53 | + * @param string|array $args |
|
54 | + * @param string $TermClass |
|
55 | + * @return mixed |
|
56 | + */ |
|
57 | + public static function handle_term_query($taxonomies, $args, $TermClass){ |
|
58 | + if (!isset($args['hide_empty'])){ |
|
59 | + $args['hide_empty'] = false; |
|
60 | + } |
|
61 | + if (isset($args['term_id']) && is_int($args['term_id'])){ |
|
62 | + $args['term_id'] = array($args['term_id']); |
|
63 | + } |
|
64 | + if (isset($args['term_id'])){ |
|
65 | + $args['include'] = $args['term_id']; |
|
66 | + } |
|
67 | + $terms = get_terms($taxonomies, $args); |
|
68 | + foreach($terms as &$term){ |
|
69 | + $term = new $TermClass($term->term_id, $term->taxonomy); |
|
70 | + } |
|
71 | + return $terms; |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * @param string $query_string |
|
76 | - * @return stdClass |
|
77 | - */ |
|
78 | - public static function get_term_query_from_query_string($query_string) { |
|
79 | - $args = array(); |
|
80 | - parse_str($query_string, $args); |
|
81 | - $ret = self::get_term_query_from_assoc_array($args); |
|
82 | - return $ret; |
|
83 | - } |
|
74 | + /** |
|
75 | + * @param string $query_string |
|
76 | + * @return stdClass |
|
77 | + */ |
|
78 | + public static function get_term_query_from_query_string($query_string) { |
|
79 | + $args = array(); |
|
80 | + parse_str($query_string, $args); |
|
81 | + $ret = self::get_term_query_from_assoc_array($args); |
|
82 | + return $ret; |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * @param string $taxs |
|
87 | - * @return stdClass |
|
88 | - */ |
|
89 | - public static function get_term_query_from_string($taxs) { |
|
90 | - $ret = new stdClass(); |
|
91 | - $ret->args = array(); |
|
92 | - if (is_string($taxs)) { |
|
93 | - $taxs = array($taxs); |
|
94 | - } |
|
95 | - $ret->taxonomies = self::correct_taxonomy_names($taxs); |
|
96 | - return $ret; |
|
97 | - } |
|
85 | + /** |
|
86 | + * @param string $taxs |
|
87 | + * @return stdClass |
|
88 | + */ |
|
89 | + public static function get_term_query_from_string($taxs) { |
|
90 | + $ret = new stdClass(); |
|
91 | + $ret->args = array(); |
|
92 | + if (is_string($taxs)) { |
|
93 | + $taxs = array($taxs); |
|
94 | + } |
|
95 | + $ret->taxonomies = self::correct_taxonomy_names($taxs); |
|
96 | + return $ret; |
|
97 | + } |
|
98 | 98 | |
99 | - /** |
|
100 | - * @param array $args |
|
101 | - * @return stdClass |
|
102 | - */ |
|
103 | - public static function get_term_query_from_assoc_array($args) { |
|
104 | - $ret = new stdClass(); |
|
105 | - $ret->args = $args; |
|
106 | - if (isset($ret->args['tax'])) { |
|
107 | - $ret->taxonomies = $ret->args['tax']; |
|
108 | - } else if (isset($ret->args['taxonomies'])) { |
|
109 | - $ret->taxonomies = $ret->args['taxonomies']; |
|
110 | - } else if (isset($ret->args['taxs'])) { |
|
111 | - $ret->taxonomies = $ret->args['taxs']; |
|
112 | - } else if (isset($ret->args['taxonomy'])) { |
|
113 | - $ret->taxonomies = $ret->args['taxonomy']; |
|
114 | - } |
|
115 | - if (isset($ret->taxonomies)) { |
|
116 | - if (is_string($ret->taxonomies)) { |
|
117 | - $ret->taxonomies = array($ret->taxonomies); |
|
118 | - } |
|
119 | - $ret->taxonomies = self::correct_taxonomy_names($ret->taxonomies); |
|
120 | - } else { |
|
121 | - $ret->taxonomies = get_taxonomies(); |
|
122 | - } |
|
123 | - return $ret; |
|
124 | - } |
|
99 | + /** |
|
100 | + * @param array $args |
|
101 | + * @return stdClass |
|
102 | + */ |
|
103 | + public static function get_term_query_from_assoc_array($args) { |
|
104 | + $ret = new stdClass(); |
|
105 | + $ret->args = $args; |
|
106 | + if (isset($ret->args['tax'])) { |
|
107 | + $ret->taxonomies = $ret->args['tax']; |
|
108 | + } else if (isset($ret->args['taxonomies'])) { |
|
109 | + $ret->taxonomies = $ret->args['taxonomies']; |
|
110 | + } else if (isset($ret->args['taxs'])) { |
|
111 | + $ret->taxonomies = $ret->args['taxs']; |
|
112 | + } else if (isset($ret->args['taxonomy'])) { |
|
113 | + $ret->taxonomies = $ret->args['taxonomy']; |
|
114 | + } |
|
115 | + if (isset($ret->taxonomies)) { |
|
116 | + if (is_string($ret->taxonomies)) { |
|
117 | + $ret->taxonomies = array($ret->taxonomies); |
|
118 | + } |
|
119 | + $ret->taxonomies = self::correct_taxonomy_names($ret->taxonomies); |
|
120 | + } else { |
|
121 | + $ret->taxonomies = get_taxonomies(); |
|
122 | + } |
|
123 | + return $ret; |
|
124 | + } |
|
125 | 125 | |
126 | - /** |
|
127 | - * @param array $args |
|
128 | - * @return stdClass |
|
129 | - */ |
|
130 | - public static function get_term_query_from_array($args) { |
|
131 | - if (is_array($args) && !empty($args)) { |
|
132 | - //okay its an array with content |
|
133 | - if (is_int($args[0])) { |
|
134 | - return self::get_term_query_from_array_of_ids($args); |
|
135 | - } else if (is_string($args[0])) { |
|
136 | - return self::get_term_query_from_array_of_strings($args); |
|
137 | - } |
|
138 | - } |
|
139 | - return null; |
|
140 | - } |
|
126 | + /** |
|
127 | + * @param array $args |
|
128 | + * @return stdClass |
|
129 | + */ |
|
130 | + public static function get_term_query_from_array($args) { |
|
131 | + if (is_array($args) && !empty($args)) { |
|
132 | + //okay its an array with content |
|
133 | + if (is_int($args[0])) { |
|
134 | + return self::get_term_query_from_array_of_ids($args); |
|
135 | + } else if (is_string($args[0])) { |
|
136 | + return self::get_term_query_from_array_of_strings($args); |
|
137 | + } |
|
138 | + } |
|
139 | + return null; |
|
140 | + } |
|
141 | 141 | |
142 | - /** |
|
143 | - * @param integer[] $args |
|
144 | - * @return stdClass |
|
145 | - */ |
|
146 | - public static function get_term_query_from_array_of_ids($args) { |
|
147 | - $ret = new stdClass(); |
|
148 | - $ret->taxonomies = get_taxonomies(); |
|
149 | - $ret->args['include'] = $args; |
|
150 | - return $ret; |
|
151 | - } |
|
142 | + /** |
|
143 | + * @param integer[] $args |
|
144 | + * @return stdClass |
|
145 | + */ |
|
146 | + public static function get_term_query_from_array_of_ids($args) { |
|
147 | + $ret = new stdClass(); |
|
148 | + $ret->taxonomies = get_taxonomies(); |
|
149 | + $ret->args['include'] = $args; |
|
150 | + return $ret; |
|
151 | + } |
|
152 | 152 | |
153 | - /** |
|
154 | - * @param string[] $args |
|
155 | - * @return stdClass |
|
156 | - */ |
|
157 | - public static function get_term_query_from_array_of_strings($args) { |
|
158 | - $ret = new stdClass(); |
|
159 | - $ret->taxonomies = self::correct_taxonomy_names($args); |
|
160 | - $ret->args = array(); |
|
161 | - return $ret; |
|
162 | - } |
|
153 | + /** |
|
154 | + * @param string[] $args |
|
155 | + * @return stdClass |
|
156 | + */ |
|
157 | + public static function get_term_query_from_array_of_strings($args) { |
|
158 | + $ret = new stdClass(); |
|
159 | + $ret->taxonomies = self::correct_taxonomy_names($args); |
|
160 | + $ret->args = array(); |
|
161 | + return $ret; |
|
162 | + } |
|
163 | 163 | |
164 | - /** |
|
165 | - * @param string|array $taxs |
|
166 | - * @return array |
|
167 | - */ |
|
168 | - private static function correct_taxonomy_names($taxs) { |
|
169 | - if (is_string($taxs)) { |
|
170 | - $taxs = array($taxs); |
|
171 | - } |
|
172 | - foreach ($taxs as &$tax) { |
|
173 | - if ($tax == 'tags' || $tax == 'tag') { |
|
174 | - $tax = 'post_tag'; |
|
175 | - } else if ($tax == 'categories') { |
|
176 | - $tax = 'category'; |
|
177 | - } |
|
178 | - } |
|
179 | - return $taxs; |
|
180 | - } |
|
164 | + /** |
|
165 | + * @param string|array $taxs |
|
166 | + * @return array |
|
167 | + */ |
|
168 | + private static function correct_taxonomy_names($taxs) { |
|
169 | + if (is_string($taxs)) { |
|
170 | + $taxs = array($taxs); |
|
171 | + } |
|
172 | + foreach ($taxs as &$tax) { |
|
173 | + if ($tax == 'tags' || $tax == 'tag') { |
|
174 | + $tax = 'post_tag'; |
|
175 | + } else if ($tax == 'categories') { |
|
176 | + $tax = 'category'; |
|
177 | + } |
|
178 | + } |
|
179 | + return $taxs; |
|
180 | + } |
|
181 | 181 | |
182 | 182 | } |
@@ -333,7 +333,7 @@ |
||
333 | 333 | $suffix = '</p>'; |
334 | 334 | $desc = term_description( $this->ID, $this->taxonomy ); |
335 | 335 | if (substr($desc, 0, strlen($prefix)) == $prefix) { |
336 | - $desc = substr($desc, strlen($prefix)); |
|
336 | + $desc = substr($desc, strlen($prefix)); |
|
337 | 337 | } |
338 | 338 | $desc = preg_replace('/'. preg_quote('</p>', '/') . '$/', '', $desc); |
339 | 339 | return trim($desc); |
@@ -736,33 +736,33 @@ |
||
736 | 736 | $overridden_cpage = true; |
737 | 737 | } |
738 | 738 | |
739 | - foreach($comments as $key => &$comment) { |
|
740 | - $timber_comment = new $CommentClass($comment); |
|
741 | - $timber_comment->reply_link = $this->TimberComment_reply_link($comment->comment_ID, $this->ID); |
|
742 | - $timber_comments[$timber_comment->id] = $timber_comment; |
|
743 | - } |
|
739 | + foreach($comments as $key => &$comment) { |
|
740 | + $timber_comment = new $CommentClass($comment); |
|
741 | + $timber_comment->reply_link = $this->TimberComment_reply_link($comment->comment_ID, $this->ID); |
|
742 | + $timber_comments[$timber_comment->id] = $timber_comment; |
|
743 | + } |
|
744 | 744 | |
745 | 745 | // Build a flattened (depth=1) comment tree |
746 | 746 | $comments_tree = array(); |
747 | - foreach( $timber_comments as $key => $comment ) { |
|
748 | - if ( ! $comment->is_child() ) { |
|
749 | - continue; |
|
750 | - } |
|
751 | - |
|
752 | - $tree_element = $comment; |
|
753 | - do { |
|
754 | - $tree_element = $timber_comments[$tree_element->comment_parent]; |
|
755 | - } while($tree_element->is_child()); |
|
756 | - $comments_tree[$tree_element->id][] = $comment->id; |
|
757 | - } |
|
758 | - |
|
759 | - // Add child comments to the relative "super parents" |
|
760 | - foreach($comments_tree as $comment_parent => $comment_children) { |
|
761 | - foreach($comment_children as $comment_child) { |
|
762 | - $timber_comments[$comment_parent]->children[] = $timber_comments[$comment_child]; |
|
763 | - unset($timber_comments[$comment_child]); |
|
764 | - } |
|
765 | - } |
|
747 | + foreach( $timber_comments as $key => $comment ) { |
|
748 | + if ( ! $comment->is_child() ) { |
|
749 | + continue; |
|
750 | + } |
|
751 | + |
|
752 | + $tree_element = $comment; |
|
753 | + do { |
|
754 | + $tree_element = $timber_comments[$tree_element->comment_parent]; |
|
755 | + } while($tree_element->is_child()); |
|
756 | + $comments_tree[$tree_element->id][] = $comment->id; |
|
757 | + } |
|
758 | + |
|
759 | + // Add child comments to the relative "super parents" |
|
760 | + foreach($comments_tree as $comment_parent => $comment_children) { |
|
761 | + foreach($comment_children as $comment_child) { |
|
762 | + $timber_comments[$comment_parent]->children[] = $timber_comments[$comment_child]; |
|
763 | + unset($timber_comments[$comment_child]); |
|
764 | + } |
|
765 | + } |
|
766 | 766 | |
767 | 767 | $timber_comments = array_values($timber_comments); |
768 | 768 |