@@ -176,7 +176,7 @@ discard block |
||
176 | 176 | * |
177 | 177 | * |
178 | 178 | * @param mixed $arg that you want to error_log |
179 | - * @return void |
|
179 | + * @return null|boolean |
|
180 | 180 | */ |
181 | 181 | public static function error_log( $arg ) { |
182 | 182 | if ( !WP_DEBUG ) { |
@@ -208,7 +208,7 @@ discard block |
||
208 | 208 | * |
209 | 209 | * @param string $text |
210 | 210 | * @param int $num_words |
211 | - * @param string|null|false $more text to appear in "Read more...". Null to use default, false to hide |
|
211 | + * @param boolean $more text to appear in "Read more...". Null to use default, false to hide |
|
212 | 212 | * @param string $allowed_tags |
213 | 213 | * @return string |
214 | 214 | */ |
@@ -48,8 +48,9 @@ |
||
48 | 48 | |
49 | 49 | // lock timeout shouldn't be higher than 5 seconds, unless |
50 | 50 | // remote calls with high timeouts are made here |
51 | - if ( $enable_transients ) |
|
52 | - self::_lock_transient( $slug, $lock_timeout ); |
|
51 | + if ( $enable_transients ) { |
|
52 | + self::_lock_transient( $slug, $lock_timeout ); |
|
53 | + } |
|
53 | 54 | |
54 | 55 | $data = $callback(); |
55 | 56 |
@@ -5,664 +5,664 @@ |
||
5 | 5 | */ |
6 | 6 | class TimberHelper { |
7 | 7 | |
8 | - /** |
|
9 | - * A utility for a one-stop shop for Transients |
|
10 | - * @api |
|
11 | - * @example |
|
12 | - * ```php |
|
13 | - * $favorites = Timber::transient('user-'.$uid.'-favorites', function() use ($uid) { |
|
14 | - * //some expensive query here that's doing something you want to store to a transient |
|
15 | - * return $favorites; |
|
16 | - * }, 600); |
|
17 | - * Timber::context['favorites'] = $favorites; |
|
18 | - * Timber::render('single.twig', $context); |
|
19 | - * ``` |
|
20 | - * |
|
21 | - * @param string $slug Unique identifier for transient |
|
22 | - * @param callable $callback Callback that generates the data that's to be cached |
|
23 | - * @param int $transient_time (optional) Expiration of transients in seconds |
|
24 | - * @param int $lock_timeout (optional) How long (in seconds) to lock the transient to prevent race conditions |
|
25 | - * @param bool $force (optional) Force callback to be executed when transient is locked |
|
26 | - * @return mixed |
|
27 | - */ |
|
28 | - public static function transient( $slug, $callback, $transient_time = 0, $lock_timeout = 5, $force = false ) { |
|
29 | - $slug = apply_filters( 'timber/transient/slug', $slug ); |
|
30 | - |
|
31 | - $enable_transients = ( $transient_time === false || ( defined( 'WP_DISABLE_TRANSIENTS' ) && WP_DISABLE_TRANSIENTS ) ) ? false : true; |
|
32 | - $data = $enable_transients ? get_transient( $slug ) : false; |
|
33 | - |
|
34 | - if ( false === $data ) { |
|
35 | - |
|
36 | - if ( $enable_transients && self::_is_transient_locked( $slug ) ) { |
|
37 | - |
|
38 | - $force = apply_filters( 'timber_force_transients', $force ); |
|
39 | - $force = apply_filters( 'timber_force_transient_' . $slug, $force ); |
|
40 | - |
|
41 | - if ( !$force ) { |
|
42 | - //the server is currently executing the process. |
|
43 | - //We're just gonna dump these users. Sorry! |
|
44 | - return false; |
|
45 | - } |
|
46 | - |
|
47 | - $enable_transients = false; |
|
48 | - } |
|
49 | - |
|
50 | - // lock timeout shouldn't be higher than 5 seconds, unless |
|
51 | - // remote calls with high timeouts are made here |
|
52 | - if ( $enable_transients ) |
|
53 | - self::_lock_transient( $slug, $lock_timeout ); |
|
54 | - |
|
55 | - $data = $callback(); |
|
56 | - |
|
57 | - if ( $enable_transients ) { |
|
58 | - set_transient( $slug, $data, $transient_time ); |
|
59 | - self::_unlock_transient( $slug ); |
|
60 | - } |
|
61 | - |
|
62 | - } |
|
63 | - |
|
64 | - return $data; |
|
65 | - |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * @internal |
|
70 | - * @param string $slug |
|
71 | - * @param integer $lock_timeout |
|
72 | - */ |
|
73 | - static function _lock_transient( $slug, $lock_timeout ) { |
|
74 | - set_transient( $slug . '_lock', true, $lock_timeout ); |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * @internal |
|
79 | - * @param string $slug |
|
80 | - */ |
|
81 | - static function _unlock_transient( $slug ) { |
|
82 | - delete_transient( $slug . '_lock', true ); |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * @internal |
|
87 | - * @param string $slug |
|
88 | - */ |
|
89 | - static function _is_transient_locked( $slug ) { |
|
90 | - return (bool)get_transient( $slug . '_lock' ); |
|
91 | - } |
|
92 | - |
|
93 | - /* These are for measuring page render time */ |
|
94 | - |
|
95 | - /** |
|
96 | - * For measuring time, this will start a timer |
|
97 | - * @api |
|
98 | - * @return float |
|
99 | - */ |
|
100 | - public static function start_timer() { |
|
101 | - $time = microtime(); |
|
102 | - $time = explode( ' ', $time ); |
|
103 | - $time = $time[1] + $time[0]; |
|
104 | - return $time; |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * For stopping time and getting the data |
|
109 | - * @example |
|
110 | - * ```php |
|
111 | - * $start = TimberHelper::start_timer(); |
|
112 | - * // do some stuff that takes awhile |
|
113 | - * echo TimberHelper::stop_timer( $start ); |
|
114 | - * ``` |
|
115 | - * @param int $start |
|
116 | - * @return string |
|
117 | - */ |
|
118 | - public static function stop_timer( $start ) { |
|
119 | - $time = microtime(); |
|
120 | - $time = explode( ' ', $time ); |
|
121 | - $time = $time[1] + $time[0]; |
|
122 | - $finish = $time; |
|
123 | - $total_time = round( ( $finish - $start ), 4 ); |
|
124 | - return $total_time . ' seconds.'; |
|
125 | - } |
|
126 | - |
|
127 | - /* Function Utilities |
|
8 | + /** |
|
9 | + * A utility for a one-stop shop for Transients |
|
10 | + * @api |
|
11 | + * @example |
|
12 | + * ```php |
|
13 | + * $favorites = Timber::transient('user-'.$uid.'-favorites', function() use ($uid) { |
|
14 | + * //some expensive query here that's doing something you want to store to a transient |
|
15 | + * return $favorites; |
|
16 | + * }, 600); |
|
17 | + * Timber::context['favorites'] = $favorites; |
|
18 | + * Timber::render('single.twig', $context); |
|
19 | + * ``` |
|
20 | + * |
|
21 | + * @param string $slug Unique identifier for transient |
|
22 | + * @param callable $callback Callback that generates the data that's to be cached |
|
23 | + * @param int $transient_time (optional) Expiration of transients in seconds |
|
24 | + * @param int $lock_timeout (optional) How long (in seconds) to lock the transient to prevent race conditions |
|
25 | + * @param bool $force (optional) Force callback to be executed when transient is locked |
|
26 | + * @return mixed |
|
27 | + */ |
|
28 | + public static function transient( $slug, $callback, $transient_time = 0, $lock_timeout = 5, $force = false ) { |
|
29 | + $slug = apply_filters( 'timber/transient/slug', $slug ); |
|
30 | + |
|
31 | + $enable_transients = ( $transient_time === false || ( defined( 'WP_DISABLE_TRANSIENTS' ) && WP_DISABLE_TRANSIENTS ) ) ? false : true; |
|
32 | + $data = $enable_transients ? get_transient( $slug ) : false; |
|
33 | + |
|
34 | + if ( false === $data ) { |
|
35 | + |
|
36 | + if ( $enable_transients && self::_is_transient_locked( $slug ) ) { |
|
37 | + |
|
38 | + $force = apply_filters( 'timber_force_transients', $force ); |
|
39 | + $force = apply_filters( 'timber_force_transient_' . $slug, $force ); |
|
40 | + |
|
41 | + if ( !$force ) { |
|
42 | + //the server is currently executing the process. |
|
43 | + //We're just gonna dump these users. Sorry! |
|
44 | + return false; |
|
45 | + } |
|
46 | + |
|
47 | + $enable_transients = false; |
|
48 | + } |
|
49 | + |
|
50 | + // lock timeout shouldn't be higher than 5 seconds, unless |
|
51 | + // remote calls with high timeouts are made here |
|
52 | + if ( $enable_transients ) |
|
53 | + self::_lock_transient( $slug, $lock_timeout ); |
|
54 | + |
|
55 | + $data = $callback(); |
|
56 | + |
|
57 | + if ( $enable_transients ) { |
|
58 | + set_transient( $slug, $data, $transient_time ); |
|
59 | + self::_unlock_transient( $slug ); |
|
60 | + } |
|
61 | + |
|
62 | + } |
|
63 | + |
|
64 | + return $data; |
|
65 | + |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * @internal |
|
70 | + * @param string $slug |
|
71 | + * @param integer $lock_timeout |
|
72 | + */ |
|
73 | + static function _lock_transient( $slug, $lock_timeout ) { |
|
74 | + set_transient( $slug . '_lock', true, $lock_timeout ); |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * @internal |
|
79 | + * @param string $slug |
|
80 | + */ |
|
81 | + static function _unlock_transient( $slug ) { |
|
82 | + delete_transient( $slug . '_lock', true ); |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * @internal |
|
87 | + * @param string $slug |
|
88 | + */ |
|
89 | + static function _is_transient_locked( $slug ) { |
|
90 | + return (bool)get_transient( $slug . '_lock' ); |
|
91 | + } |
|
92 | + |
|
93 | + /* These are for measuring page render time */ |
|
94 | + |
|
95 | + /** |
|
96 | + * For measuring time, this will start a timer |
|
97 | + * @api |
|
98 | + * @return float |
|
99 | + */ |
|
100 | + public static function start_timer() { |
|
101 | + $time = microtime(); |
|
102 | + $time = explode( ' ', $time ); |
|
103 | + $time = $time[1] + $time[0]; |
|
104 | + return $time; |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * For stopping time and getting the data |
|
109 | + * @example |
|
110 | + * ```php |
|
111 | + * $start = TimberHelper::start_timer(); |
|
112 | + * // do some stuff that takes awhile |
|
113 | + * echo TimberHelper::stop_timer( $start ); |
|
114 | + * ``` |
|
115 | + * @param int $start |
|
116 | + * @return string |
|
117 | + */ |
|
118 | + public static function stop_timer( $start ) { |
|
119 | + $time = microtime(); |
|
120 | + $time = explode( ' ', $time ); |
|
121 | + $time = $time[1] + $time[0]; |
|
122 | + $finish = $time; |
|
123 | + $total_time = round( ( $finish - $start ), 4 ); |
|
124 | + return $total_time . ' seconds.'; |
|
125 | + } |
|
126 | + |
|
127 | + /* Function Utilities |
|
128 | 128 | ======================== */ |
129 | 129 | |
130 | - /** |
|
131 | - * Calls a function with an output buffer. This is useful if you have a function that outputs text that you want to capture and use within a twig template. |
|
132 | - * @example |
|
133 | - * ```php |
|
134 | - * function the_form() { |
|
135 | - * echo '<form action="form.php"><input type="text" /><input type="submit /></form>'; |
|
136 | - * } |
|
137 | - * |
|
138 | - * $context = Timber::get_context(); |
|
139 | - * $context['post'] = new TimberPost(); |
|
140 | - * $context['my_form'] = TimberHelper::ob_function('the_form'); |
|
141 | - * Timber::render('single-form.twig', $context); |
|
142 | - * ``` |
|
143 | - * ```twig |
|
144 | - * <h1>{{ post.title }}</h1> |
|
145 | - * {{ my_form }} |
|
146 | - * ``` |
|
147 | - * ```html |
|
148 | - * <h1>Apply to my contest!</h1> |
|
149 | - * <form action="form.php"><input type="text" /><input type="submit /></form> |
|
150 | - * ``` |
|
151 | - * @api |
|
152 | - * @param callback $function |
|
153 | - * @param array $args |
|
154 | - * @return string |
|
155 | - */ |
|
156 | - public static function ob_function( $function, $args = array( null ) ) { |
|
157 | - ob_start(); |
|
158 | - call_user_func_array( $function, $args ); |
|
159 | - $data = ob_get_contents(); |
|
160 | - ob_end_clean(); |
|
161 | - return $data; |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * |
|
166 | - * |
|
167 | - * @param string $function_name |
|
168 | - * @param integer[] $defaults |
|
169 | - * @param bool $return_output_buffer |
|
170 | - * @return TimberFunctionWrapper |
|
171 | - */ |
|
172 | - public static function function_wrapper( $function_name, $defaults = array(), $return_output_buffer = false ) { |
|
173 | - return new TimberFunctionWrapper( $function_name, $defaults, $return_output_buffer ); |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * |
|
178 | - * |
|
179 | - * @param mixed $arg that you want to error_log |
|
180 | - * @return void |
|
181 | - */ |
|
182 | - public static function error_log( $arg ) { |
|
183 | - if ( !WP_DEBUG ) { |
|
184 | - return; |
|
185 | - } |
|
186 | - if ( is_object( $arg ) || is_array( $arg ) ) { |
|
187 | - $arg = print_r( $arg, true ); |
|
188 | - } |
|
189 | - return error_log( $arg ); |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * |
|
194 | - * |
|
195 | - * @param string $separator |
|
196 | - * @param string $seplocation |
|
197 | - * @return string |
|
198 | - */ |
|
199 | - public static function get_wp_title( $separator = ' ', $seplocation = 'left' ) { |
|
200 | - $separator = apply_filters( 'timber_wp_title_seperator', $separator ); |
|
201 | - return trim( wp_title( $separator, false, $seplocation ) ); |
|
202 | - } |
|
203 | - |
|
204 | - /* Text Utilities |
|
130 | + /** |
|
131 | + * Calls a function with an output buffer. This is useful if you have a function that outputs text that you want to capture and use within a twig template. |
|
132 | + * @example |
|
133 | + * ```php |
|
134 | + * function the_form() { |
|
135 | + * echo '<form action="form.php"><input type="text" /><input type="submit /></form>'; |
|
136 | + * } |
|
137 | + * |
|
138 | + * $context = Timber::get_context(); |
|
139 | + * $context['post'] = new TimberPost(); |
|
140 | + * $context['my_form'] = TimberHelper::ob_function('the_form'); |
|
141 | + * Timber::render('single-form.twig', $context); |
|
142 | + * ``` |
|
143 | + * ```twig |
|
144 | + * <h1>{{ post.title }}</h1> |
|
145 | + * {{ my_form }} |
|
146 | + * ``` |
|
147 | + * ```html |
|
148 | + * <h1>Apply to my contest!</h1> |
|
149 | + * <form action="form.php"><input type="text" /><input type="submit /></form> |
|
150 | + * ``` |
|
151 | + * @api |
|
152 | + * @param callback $function |
|
153 | + * @param array $args |
|
154 | + * @return string |
|
155 | + */ |
|
156 | + public static function ob_function( $function, $args = array( null ) ) { |
|
157 | + ob_start(); |
|
158 | + call_user_func_array( $function, $args ); |
|
159 | + $data = ob_get_contents(); |
|
160 | + ob_end_clean(); |
|
161 | + return $data; |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * |
|
166 | + * |
|
167 | + * @param string $function_name |
|
168 | + * @param integer[] $defaults |
|
169 | + * @param bool $return_output_buffer |
|
170 | + * @return TimberFunctionWrapper |
|
171 | + */ |
|
172 | + public static function function_wrapper( $function_name, $defaults = array(), $return_output_buffer = false ) { |
|
173 | + return new TimberFunctionWrapper( $function_name, $defaults, $return_output_buffer ); |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * |
|
178 | + * |
|
179 | + * @param mixed $arg that you want to error_log |
|
180 | + * @return void |
|
181 | + */ |
|
182 | + public static function error_log( $arg ) { |
|
183 | + if ( !WP_DEBUG ) { |
|
184 | + return; |
|
185 | + } |
|
186 | + if ( is_object( $arg ) || is_array( $arg ) ) { |
|
187 | + $arg = print_r( $arg, true ); |
|
188 | + } |
|
189 | + return error_log( $arg ); |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * |
|
194 | + * |
|
195 | + * @param string $separator |
|
196 | + * @param string $seplocation |
|
197 | + * @return string |
|
198 | + */ |
|
199 | + public static function get_wp_title( $separator = ' ', $seplocation = 'left' ) { |
|
200 | + $separator = apply_filters( 'timber_wp_title_seperator', $separator ); |
|
201 | + return trim( wp_title( $separator, false, $seplocation ) ); |
|
202 | + } |
|
203 | + |
|
204 | + /* Text Utilities |
|
205 | 205 | ======================== */ |
206 | 206 | |
207 | - /** |
|
208 | - * |
|
209 | - * |
|
210 | - * @param string $text |
|
211 | - * @param int $num_words |
|
212 | - * @param string|null|false $more text to appear in "Read more...". Null to use default, false to hide |
|
213 | - * @param string $allowed_tags |
|
214 | - * @return string |
|
215 | - */ |
|
216 | - public static function trim_words( $text, $num_words = 55, $more = null, $allowed_tags = 'p a span b i br blockquote' ) { |
|
217 | - if ( null === $more ) { |
|
218 | - $more = __( '…' ); |
|
219 | - } |
|
220 | - $original_text = $text; |
|
221 | - $allowed_tag_string = ''; |
|
222 | - foreach ( explode( ' ', apply_filters( 'timber/trim_words/allowed_tags', $allowed_tags ) ) as $tag ) { |
|
223 | - $allowed_tag_string .= '<' . $tag . '>'; |
|
224 | - } |
|
225 | - $text = strip_tags( $text, $allowed_tag_string ); |
|
226 | - /* translators: If your word count is based on single characters (East Asian characters), enter 'characters'. Otherwise, enter 'words'. Do not translate into your own language. */ |
|
227 | - if ( 'characters' == _x( 'words', 'word count: words or characters?' ) && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) { |
|
228 | - $text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' ); |
|
229 | - preg_match_all( '/./u', $text, $words_array ); |
|
230 | - $words_array = array_slice( $words_array[0], 0, $num_words + 1 ); |
|
231 | - $sep = ''; |
|
232 | - } else { |
|
233 | - $words_array = preg_split( "/[\n\r\t ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY ); |
|
234 | - $sep = ' '; |
|
235 | - } |
|
236 | - if ( count( $words_array ) > $num_words ) { |
|
237 | - array_pop( $words_array ); |
|
238 | - $text = implode( $sep, $words_array ); |
|
239 | - $text = $text . $more; |
|
240 | - } else { |
|
241 | - $text = implode( $sep, $words_array ); |
|
242 | - } |
|
243 | - $text = self::close_tags( $text ); |
|
244 | - return apply_filters( 'wp_trim_words', $text, $num_words, $more, $original_text ); |
|
245 | - } |
|
246 | - |
|
247 | - /** |
|
248 | - * |
|
249 | - * |
|
250 | - * @param string $html |
|
251 | - * @return string |
|
252 | - */ |
|
253 | - public static function close_tags( $html ) { |
|
254 | - //put all opened tags into an array |
|
255 | - preg_match_all( '#<([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result ); |
|
256 | - $openedtags = $result[1]; |
|
257 | - //put all closed tags into an array |
|
258 | - preg_match_all( '#</([a-z]+)>#iU', $html, $result ); |
|
259 | - $closedtags = $result[1]; |
|
260 | - $len_opened = count( $openedtags ); |
|
261 | - // all tags are closed |
|
262 | - if ( count( $closedtags ) == $len_opened ) { |
|
263 | - return $html; |
|
264 | - } |
|
265 | - $openedtags = array_reverse( $openedtags ); |
|
266 | - // close tags |
|
267 | - for ( $i = 0; $i < $len_opened; $i++ ) { |
|
268 | - if ( !in_array( $openedtags[$i], $closedtags ) ) { |
|
269 | - $html .= '</' . $openedtags[$i] . '>'; |
|
270 | - } else { |
|
271 | - unset( $closedtags[array_search( $openedtags[$i], $closedtags )] ); |
|
272 | - } |
|
273 | - } |
|
274 | - $html = str_replace(array('</br>','</hr>','</wbr>'), '', $html); |
|
275 | - $html = str_replace(array('<br>','<hr>','<wbr>'), array('<br />','<hr />','<wbr />'), $html); |
|
276 | - return $html; |
|
277 | - } |
|
278 | - |
|
279 | - /* WordPress Query Utilities |
|
207 | + /** |
|
208 | + * |
|
209 | + * |
|
210 | + * @param string $text |
|
211 | + * @param int $num_words |
|
212 | + * @param string|null|false $more text to appear in "Read more...". Null to use default, false to hide |
|
213 | + * @param string $allowed_tags |
|
214 | + * @return string |
|
215 | + */ |
|
216 | + public static function trim_words( $text, $num_words = 55, $more = null, $allowed_tags = 'p a span b i br blockquote' ) { |
|
217 | + if ( null === $more ) { |
|
218 | + $more = __( '…' ); |
|
219 | + } |
|
220 | + $original_text = $text; |
|
221 | + $allowed_tag_string = ''; |
|
222 | + foreach ( explode( ' ', apply_filters( 'timber/trim_words/allowed_tags', $allowed_tags ) ) as $tag ) { |
|
223 | + $allowed_tag_string .= '<' . $tag . '>'; |
|
224 | + } |
|
225 | + $text = strip_tags( $text, $allowed_tag_string ); |
|
226 | + /* translators: If your word count is based on single characters (East Asian characters), enter 'characters'. Otherwise, enter 'words'. Do not translate into your own language. */ |
|
227 | + if ( 'characters' == _x( 'words', 'word count: words or characters?' ) && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) { |
|
228 | + $text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' ); |
|
229 | + preg_match_all( '/./u', $text, $words_array ); |
|
230 | + $words_array = array_slice( $words_array[0], 0, $num_words + 1 ); |
|
231 | + $sep = ''; |
|
232 | + } else { |
|
233 | + $words_array = preg_split( "/[\n\r\t ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY ); |
|
234 | + $sep = ' '; |
|
235 | + } |
|
236 | + if ( count( $words_array ) > $num_words ) { |
|
237 | + array_pop( $words_array ); |
|
238 | + $text = implode( $sep, $words_array ); |
|
239 | + $text = $text . $more; |
|
240 | + } else { |
|
241 | + $text = implode( $sep, $words_array ); |
|
242 | + } |
|
243 | + $text = self::close_tags( $text ); |
|
244 | + return apply_filters( 'wp_trim_words', $text, $num_words, $more, $original_text ); |
|
245 | + } |
|
246 | + |
|
247 | + /** |
|
248 | + * |
|
249 | + * |
|
250 | + * @param string $html |
|
251 | + * @return string |
|
252 | + */ |
|
253 | + public static function close_tags( $html ) { |
|
254 | + //put all opened tags into an array |
|
255 | + preg_match_all( '#<([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result ); |
|
256 | + $openedtags = $result[1]; |
|
257 | + //put all closed tags into an array |
|
258 | + preg_match_all( '#</([a-z]+)>#iU', $html, $result ); |
|
259 | + $closedtags = $result[1]; |
|
260 | + $len_opened = count( $openedtags ); |
|
261 | + // all tags are closed |
|
262 | + if ( count( $closedtags ) == $len_opened ) { |
|
263 | + return $html; |
|
264 | + } |
|
265 | + $openedtags = array_reverse( $openedtags ); |
|
266 | + // close tags |
|
267 | + for ( $i = 0; $i < $len_opened; $i++ ) { |
|
268 | + if ( !in_array( $openedtags[$i], $closedtags ) ) { |
|
269 | + $html .= '</' . $openedtags[$i] . '>'; |
|
270 | + } else { |
|
271 | + unset( $closedtags[array_search( $openedtags[$i], $closedtags )] ); |
|
272 | + } |
|
273 | + } |
|
274 | + $html = str_replace(array('</br>','</hr>','</wbr>'), '', $html); |
|
275 | + $html = str_replace(array('<br>','<hr>','<wbr>'), array('<br />','<hr />','<wbr />'), $html); |
|
276 | + return $html; |
|
277 | + } |
|
278 | + |
|
279 | + /* WordPress Query Utilities |
|
280 | 280 | ======================== */ |
281 | 281 | |
282 | - /** |
|
283 | - * @param string $key |
|
284 | - * @param string $value |
|
285 | - * @return array|int |
|
286 | - * @deprecated 0.20.0 |
|
287 | - */ |
|
288 | - public static function get_posts_by_meta( $key, $value ) { |
|
289 | - global $wpdb; |
|
290 | - $query = $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s", $key, $value ); |
|
291 | - $results = $wpdb->get_col( $query ); |
|
292 | - $pids = array(); |
|
293 | - foreach ( $results as $result ) { |
|
294 | - if ( get_post( $result ) ) { |
|
295 | - $pids[] = $result; |
|
296 | - } |
|
297 | - } |
|
298 | - if ( count( $pids ) ) { |
|
299 | - return $pids; |
|
300 | - } |
|
301 | - return 0; |
|
302 | - } |
|
303 | - |
|
304 | - /** |
|
305 | - * |
|
306 | - * |
|
307 | - * @param string $key |
|
308 | - * @param string $value |
|
309 | - * @return int |
|
310 | - * @deprecated 0.20.0 |
|
311 | - */ |
|
312 | - public static function get_post_by_meta( $key, $value ) { |
|
313 | - global $wpdb; |
|
314 | - $query = $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s ORDER BY post_id", $key, $value ); |
|
315 | - $results = $wpdb->get_col( $query ); |
|
316 | - foreach ( $results as $result ) { |
|
317 | - if ( $result && get_post( $result ) ) { |
|
318 | - return $result; |
|
319 | - } |
|
320 | - } |
|
321 | - return 0; |
|
322 | - } |
|
323 | - |
|
324 | - /** |
|
325 | - * |
|
326 | - * @deprecated 0.21.8 |
|
327 | - * @param int $ttid |
|
328 | - * @return mixed |
|
329 | - */ |
|
330 | - public static function get_term_id_by_term_taxonomy_id( $ttid ) { |
|
331 | - global $wpdb; |
|
332 | - $query = $wpdb->prepare( "SELECT term_id FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %s", $ttid ); |
|
333 | - return $wpdb->get_var( $query ); |
|
334 | - } |
|
335 | - |
|
336 | - /* Object Utilities |
|
282 | + /** |
|
283 | + * @param string $key |
|
284 | + * @param string $value |
|
285 | + * @return array|int |
|
286 | + * @deprecated 0.20.0 |
|
287 | + */ |
|
288 | + public static function get_posts_by_meta( $key, $value ) { |
|
289 | + global $wpdb; |
|
290 | + $query = $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s", $key, $value ); |
|
291 | + $results = $wpdb->get_col( $query ); |
|
292 | + $pids = array(); |
|
293 | + foreach ( $results as $result ) { |
|
294 | + if ( get_post( $result ) ) { |
|
295 | + $pids[] = $result; |
|
296 | + } |
|
297 | + } |
|
298 | + if ( count( $pids ) ) { |
|
299 | + return $pids; |
|
300 | + } |
|
301 | + return 0; |
|
302 | + } |
|
303 | + |
|
304 | + /** |
|
305 | + * |
|
306 | + * |
|
307 | + * @param string $key |
|
308 | + * @param string $value |
|
309 | + * @return int |
|
310 | + * @deprecated 0.20.0 |
|
311 | + */ |
|
312 | + public static function get_post_by_meta( $key, $value ) { |
|
313 | + global $wpdb; |
|
314 | + $query = $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s ORDER BY post_id", $key, $value ); |
|
315 | + $results = $wpdb->get_col( $query ); |
|
316 | + foreach ( $results as $result ) { |
|
317 | + if ( $result && get_post( $result ) ) { |
|
318 | + return $result; |
|
319 | + } |
|
320 | + } |
|
321 | + return 0; |
|
322 | + } |
|
323 | + |
|
324 | + /** |
|
325 | + * |
|
326 | + * @deprecated 0.21.8 |
|
327 | + * @param int $ttid |
|
328 | + * @return mixed |
|
329 | + */ |
|
330 | + public static function get_term_id_by_term_taxonomy_id( $ttid ) { |
|
331 | + global $wpdb; |
|
332 | + $query = $wpdb->prepare( "SELECT term_id FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %s", $ttid ); |
|
333 | + return $wpdb->get_var( $query ); |
|
334 | + } |
|
335 | + |
|
336 | + /* Object Utilities |
|
337 | 337 | ======================== */ |
338 | 338 | |
339 | - /** |
|
340 | - * |
|
341 | - * |
|
342 | - * @param array $array |
|
343 | - * @param string $prop |
|
344 | - * @return void |
|
345 | - */ |
|
346 | - public static function osort( &$array, $prop ) { |
|
347 | - usort( $array, function ( $a, $b ) use ( $prop ) { |
|
348 | - return $a->$prop > $b->$prop ? 1 : -1; |
|
349 | - } ); |
|
350 | - } |
|
351 | - |
|
352 | - /** |
|
353 | - * |
|
354 | - * |
|
355 | - * @param array $arr |
|
356 | - * @return bool |
|
357 | - */ |
|
358 | - public static function is_array_assoc( $arr ) { |
|
359 | - if ( !is_array( $arr ) ) { |
|
360 | - return false; |
|
361 | - } |
|
362 | - return (bool)count( array_filter( array_keys( $arr ), 'is_string' ) ); |
|
363 | - } |
|
364 | - |
|
365 | - /** |
|
366 | - * |
|
367 | - * |
|
368 | - * @param array $array |
|
369 | - * @return stdClass |
|
370 | - */ |
|
371 | - public static function array_to_object( $array ) { |
|
372 | - $obj = new stdClass; |
|
373 | - foreach ( $array as $k => $v ) { |
|
374 | - if ( is_array( $v ) ) { |
|
375 | - $obj->{$k} = self::array_to_object( $v ); //RECURSION |
|
376 | - } else { |
|
377 | - $obj->{$k} = $v; |
|
378 | - } |
|
379 | - } |
|
380 | - return $obj; |
|
381 | - } |
|
382 | - |
|
383 | - /** |
|
384 | - * |
|
385 | - * |
|
386 | - * @param array $array |
|
387 | - * @param string $key |
|
388 | - * @param mixed $value |
|
389 | - * @return bool|int |
|
390 | - */ |
|
391 | - public static function get_object_index_by_property( $array, $key, $value ) { |
|
392 | - if ( is_array( $array ) ) { |
|
393 | - $i = 0; |
|
394 | - foreach ( $array as $arr ) { |
|
395 | - if ( is_array( $arr ) ) { |
|
396 | - if ( $arr[$key] == $value ) { |
|
397 | - return $i; |
|
398 | - } |
|
399 | - } else { |
|
400 | - if ( $arr->$key == $value ) { |
|
401 | - return $i; |
|
402 | - } |
|
403 | - } |
|
404 | - $i++; |
|
405 | - } |
|
406 | - } |
|
407 | - return false; |
|
408 | - } |
|
409 | - |
|
410 | - /** |
|
411 | - * |
|
412 | - * |
|
413 | - * @param array $array |
|
414 | - * @param string $key |
|
415 | - * @param mixed $value |
|
416 | - * @return array|null |
|
417 | - * @throws Exception |
|
418 | - */ |
|
419 | - public static function get_object_by_property( $array, $key, $value ) { |
|
420 | - if ( is_array( $array ) ) { |
|
421 | - foreach ( $array as $arr ) { |
|
422 | - if ( $arr->$key == $value ) { |
|
423 | - return $arr; |
|
424 | - } |
|
425 | - } |
|
426 | - } else { |
|
427 | - throw new InvalidArgumentException( '$array is not an array, got:' ); |
|
428 | - TimberHelper::error_log( $array ); |
|
429 | - } |
|
430 | - } |
|
431 | - |
|
432 | - /** |
|
433 | - * |
|
434 | - * |
|
435 | - * @param array $array |
|
436 | - * @param int $len |
|
437 | - * @return array |
|
438 | - */ |
|
439 | - public static function array_truncate( $array, $len ) { |
|
440 | - if ( sizeof( $array ) > $len ) { |
|
441 | - $array = array_splice( $array, 0, $len ); |
|
442 | - } |
|
443 | - return $array; |
|
444 | - } |
|
445 | - |
|
446 | - /* Bool Utilities |
|
339 | + /** |
|
340 | + * |
|
341 | + * |
|
342 | + * @param array $array |
|
343 | + * @param string $prop |
|
344 | + * @return void |
|
345 | + */ |
|
346 | + public static function osort( &$array, $prop ) { |
|
347 | + usort( $array, function ( $a, $b ) use ( $prop ) { |
|
348 | + return $a->$prop > $b->$prop ? 1 : -1; |
|
349 | + } ); |
|
350 | + } |
|
351 | + |
|
352 | + /** |
|
353 | + * |
|
354 | + * |
|
355 | + * @param array $arr |
|
356 | + * @return bool |
|
357 | + */ |
|
358 | + public static function is_array_assoc( $arr ) { |
|
359 | + if ( !is_array( $arr ) ) { |
|
360 | + return false; |
|
361 | + } |
|
362 | + return (bool)count( array_filter( array_keys( $arr ), 'is_string' ) ); |
|
363 | + } |
|
364 | + |
|
365 | + /** |
|
366 | + * |
|
367 | + * |
|
368 | + * @param array $array |
|
369 | + * @return stdClass |
|
370 | + */ |
|
371 | + public static function array_to_object( $array ) { |
|
372 | + $obj = new stdClass; |
|
373 | + foreach ( $array as $k => $v ) { |
|
374 | + if ( is_array( $v ) ) { |
|
375 | + $obj->{$k} = self::array_to_object( $v ); //RECURSION |
|
376 | + } else { |
|
377 | + $obj->{$k} = $v; |
|
378 | + } |
|
379 | + } |
|
380 | + return $obj; |
|
381 | + } |
|
382 | + |
|
383 | + /** |
|
384 | + * |
|
385 | + * |
|
386 | + * @param array $array |
|
387 | + * @param string $key |
|
388 | + * @param mixed $value |
|
389 | + * @return bool|int |
|
390 | + */ |
|
391 | + public static function get_object_index_by_property( $array, $key, $value ) { |
|
392 | + if ( is_array( $array ) ) { |
|
393 | + $i = 0; |
|
394 | + foreach ( $array as $arr ) { |
|
395 | + if ( is_array( $arr ) ) { |
|
396 | + if ( $arr[$key] == $value ) { |
|
397 | + return $i; |
|
398 | + } |
|
399 | + } else { |
|
400 | + if ( $arr->$key == $value ) { |
|
401 | + return $i; |
|
402 | + } |
|
403 | + } |
|
404 | + $i++; |
|
405 | + } |
|
406 | + } |
|
407 | + return false; |
|
408 | + } |
|
409 | + |
|
410 | + /** |
|
411 | + * |
|
412 | + * |
|
413 | + * @param array $array |
|
414 | + * @param string $key |
|
415 | + * @param mixed $value |
|
416 | + * @return array|null |
|
417 | + * @throws Exception |
|
418 | + */ |
|
419 | + public static function get_object_by_property( $array, $key, $value ) { |
|
420 | + if ( is_array( $array ) ) { |
|
421 | + foreach ( $array as $arr ) { |
|
422 | + if ( $arr->$key == $value ) { |
|
423 | + return $arr; |
|
424 | + } |
|
425 | + } |
|
426 | + } else { |
|
427 | + throw new InvalidArgumentException( '$array is not an array, got:' ); |
|
428 | + TimberHelper::error_log( $array ); |
|
429 | + } |
|
430 | + } |
|
431 | + |
|
432 | + /** |
|
433 | + * |
|
434 | + * |
|
435 | + * @param array $array |
|
436 | + * @param int $len |
|
437 | + * @return array |
|
438 | + */ |
|
439 | + public static function array_truncate( $array, $len ) { |
|
440 | + if ( sizeof( $array ) > $len ) { |
|
441 | + $array = array_splice( $array, 0, $len ); |
|
442 | + } |
|
443 | + return $array; |
|
444 | + } |
|
445 | + |
|
446 | + /* Bool Utilities |
|
447 | 447 | ======================== */ |
448 | 448 | |
449 | - /** |
|
450 | - * |
|
451 | - * |
|
452 | - * @param mixed $value |
|
453 | - * @return bool |
|
454 | - */ |
|
455 | - public static function is_true( $value ) { |
|
456 | - if ( isset( $value ) ) { |
|
457 | - if (is_string($value)) { |
|
458 | - $value = strtolower($value); |
|
459 | - } |
|
460 | - if ( ($value == 'true' || $value === 1 || $value === '1' || $value == true) && $value !== false && $value !== 'false') { |
|
461 | - return true; |
|
462 | - } |
|
463 | - } |
|
464 | - return false; |
|
465 | - } |
|
466 | - |
|
467 | - /** |
|
468 | - * |
|
469 | - * |
|
470 | - * @param int $i |
|
471 | - * @return bool |
|
472 | - */ |
|
473 | - public static function iseven( $i ) { |
|
474 | - return ( $i % 2 ) == 0; |
|
475 | - } |
|
476 | - |
|
477 | - /** |
|
478 | - * |
|
479 | - * |
|
480 | - * @param int $i |
|
481 | - * @return bool |
|
482 | - */ |
|
483 | - public static function isodd( $i ) { |
|
484 | - return ( $i % 2 ) != 0; |
|
485 | - } |
|
486 | - |
|
487 | - /* Links, Forms, Etc. Utilities |
|
449 | + /** |
|
450 | + * |
|
451 | + * |
|
452 | + * @param mixed $value |
|
453 | + * @return bool |
|
454 | + */ |
|
455 | + public static function is_true( $value ) { |
|
456 | + if ( isset( $value ) ) { |
|
457 | + if (is_string($value)) { |
|
458 | + $value = strtolower($value); |
|
459 | + } |
|
460 | + if ( ($value == 'true' || $value === 1 || $value === '1' || $value == true) && $value !== false && $value !== 'false') { |
|
461 | + return true; |
|
462 | + } |
|
463 | + } |
|
464 | + return false; |
|
465 | + } |
|
466 | + |
|
467 | + /** |
|
468 | + * |
|
469 | + * |
|
470 | + * @param int $i |
|
471 | + * @return bool |
|
472 | + */ |
|
473 | + public static function iseven( $i ) { |
|
474 | + return ( $i % 2 ) == 0; |
|
475 | + } |
|
476 | + |
|
477 | + /** |
|
478 | + * |
|
479 | + * |
|
480 | + * @param int $i |
|
481 | + * @return bool |
|
482 | + */ |
|
483 | + public static function isodd( $i ) { |
|
484 | + return ( $i % 2 ) != 0; |
|
485 | + } |
|
486 | + |
|
487 | + /* Links, Forms, Etc. Utilities |
|
488 | 488 | ======================== */ |
489 | 489 | |
490 | - /** |
|
491 | - * |
|
492 | - * Gets the comment form for use on a single article page |
|
493 | - * @deprecated 0.21.8 use `{{ function('comment_form') }}` instead |
|
494 | - * @param int $post_id which post_id should the form be tied to? |
|
495 | - * @param array $args this $args thing is a fucking mess, [fix at some point](http://codex.wordpress.org/Function_Reference/comment_form) |
|
496 | - * @return string |
|
497 | - */ |
|
498 | - public static function get_comment_form( $post_id = null, $args = array() ) { |
|
499 | - return self::ob_function( 'comment_form', array( $args, $post_id ) ); |
|
500 | - } |
|
501 | - |
|
502 | - /** |
|
503 | - * |
|
504 | - * |
|
505 | - * @param string $args |
|
506 | - * @return array |
|
507 | - */ |
|
508 | - public static function paginate_links( $args = '' ) { |
|
509 | - $defaults = array( |
|
510 | - 'base' => '%_%', // http://example.com/all_posts.php%_% : %_% is replaced by format (below) |
|
511 | - 'format' => '?page=%#%', // ?page=%#% : %#% is replaced by the page number |
|
512 | - 'total' => 1, |
|
513 | - 'current' => 0, |
|
514 | - 'show_all' => false, |
|
515 | - 'prev_next' => false, |
|
516 | - 'prev_text' => __( '« Previous' ), |
|
517 | - 'next_text' => __( 'Next »' ), |
|
518 | - 'end_size' => 1, |
|
519 | - 'mid_size' => 2, |
|
520 | - 'type' => 'array', |
|
521 | - 'add_args' => false, // array of query args to add |
|
522 | - 'add_fragment' => '' |
|
523 | - ); |
|
524 | - $args = wp_parse_args( $args, $defaults ); |
|
525 | - // Who knows what else people pass in $args |
|
526 | - $args['total'] = intval( (int)$args['total'] ); |
|
527 | - if ( $args['total'] < 2 ) { |
|
528 | - return array(); |
|
529 | - } |
|
530 | - $args['current'] = (int)$args['current']; |
|
531 | - $args['end_size'] = 0 < (int)$args['end_size'] ? (int)$args['end_size'] : 1; // Out of bounds? Make it the default. |
|
532 | - $args['mid_size'] = 0 <= (int)$args['mid_size'] ? (int)$args['mid_size'] : 2; |
|
533 | - $args['add_args'] = is_array( $args['add_args'] ) ? $args['add_args'] : false; |
|
534 | - $page_links = array(); |
|
535 | - $dots = false; |
|
536 | - for ( $n = 1; $n <= $args['total']; $n++ ) { |
|
537 | - $n_display = number_format_i18n( $n ); |
|
538 | - if ( $n == $args['current'] ) { |
|
539 | - $page_links[] = array( |
|
540 | - 'class' => 'page-number page-numbers current', |
|
541 | - 'title' => $n_display, |
|
542 | - 'text' => $n_display, |
|
543 | - 'name' => $n_display, |
|
544 | - 'current' => true |
|
545 | - ); |
|
546 | - $dots = true; |
|
547 | - } else { |
|
548 | - if ( $args['show_all'] || ( $n <= $args['end_size'] || ( $args['current'] && $n >= $args['current'] - $args['mid_size'] && $n <= $args['current'] + $args['mid_size'] ) || $n > $args['total'] - $args['end_size'] ) ) { |
|
549 | - $link = str_replace( '%_%', 1 == $n ? '' : $args['format'], $args['base'] ); |
|
550 | - $link = str_replace( '%#%', $n, $link ); |
|
551 | - $link = trailingslashit( $link ) . ltrim( $args['add_fragment'], '/' ); |
|
552 | - if ( $args['add_args'] ) { |
|
553 | - $link = rtrim( add_query_arg( $args['add_args'], $link ), '/' ); |
|
554 | - } |
|
555 | - $link = str_replace(' ', '+', $link); |
|
556 | - $link = untrailingslashit( $link ); |
|
557 | - $page_links[] = array( |
|
558 | - 'class' => 'page-number page-numbers', |
|
559 | - 'link' => esc_url( apply_filters( 'paginate_links', $link ) ), |
|
560 | - 'title' => $n_display, |
|
561 | - 'name' => $n_display, |
|
562 | - 'current' => $args['current'] == $n |
|
563 | - ); |
|
564 | - $dots = true; |
|
565 | - } elseif ( $dots && !$args['show_all'] ) { |
|
566 | - $page_links[] = array( |
|
567 | - 'class' => 'dots', |
|
568 | - 'title' => __( '…' ) |
|
569 | - ); |
|
570 | - $dots = false; |
|
571 | - } |
|
572 | - } |
|
573 | - } |
|
574 | - return $page_links; |
|
575 | - } |
|
576 | - |
|
577 | - /** |
|
578 | - * @deprecated 0.18.0 |
|
579 | - */ |
|
580 | - static function get_current_url() { |
|
581 | - return TimberURLHelper::get_current_url(); |
|
582 | - } |
|
583 | - |
|
584 | - /** |
|
585 | - * @deprecated 0.18.0 |
|
586 | - */ |
|
587 | - static function is_url( $url ) { |
|
588 | - return TimberURLHelper::is_url( $url ); |
|
589 | - } |
|
590 | - |
|
591 | - /** |
|
592 | - * @deprecated 0.18.0 |
|
593 | - */ |
|
594 | - static function get_path_base() { |
|
595 | - return TimberURLHelper::get_path_base(); |
|
596 | - } |
|
597 | - |
|
598 | - /** |
|
599 | - * @deprecated 0.18.0 |
|
600 | - */ |
|
601 | - static function get_rel_url( $url, $force = false ) { |
|
602 | - return TimberURLHelper::get_rel_url( $url, $force ); |
|
603 | - } |
|
604 | - |
|
605 | - /** |
|
606 | - * @deprecated 0.18.0 |
|
607 | - */ |
|
608 | - static function is_local( $url ) { |
|
609 | - return TimberURLHelper::is_local( $url ); |
|
610 | - } |
|
611 | - |
|
612 | - /** |
|
613 | - * @deprecated 0.18.0 |
|
614 | - */ |
|
615 | - static function get_full_path( $src ) { |
|
616 | - return TimberURLHelper::get_full_path( $src ); |
|
617 | - } |
|
618 | - |
|
619 | - /** |
|
620 | - * @deprecated 0.18.0 |
|
621 | - */ |
|
622 | - static function get_rel_path( $src ) { |
|
623 | - return TimberURLHelper::get_rel_path( $src ); |
|
624 | - } |
|
625 | - |
|
626 | - /** |
|
627 | - * @deprecated 0.18.0 |
|
628 | - */ |
|
629 | - static function remove_double_slashes( $url ) { |
|
630 | - return TimberURLHelper::remove_double_slashes( $url ); |
|
631 | - } |
|
632 | - |
|
633 | - /** |
|
634 | - * @deprecated 0.18.0 |
|
635 | - */ |
|
636 | - static function prepend_to_url( $url, $path ) { |
|
637 | - return TimberURLHelper::prepend_to_url( $url, $path ); |
|
638 | - } |
|
639 | - |
|
640 | - /** |
|
641 | - * @deprecated 0.18.0 |
|
642 | - */ |
|
643 | - static function preslashit( $path ) { |
|
644 | - return TimberURLHelper::preslashit( $path ); |
|
645 | - } |
|
646 | - |
|
647 | - /** |
|
648 | - * @deprecated 0.18.0 |
|
649 | - */ |
|
650 | - static function is_external( $url ) { |
|
651 | - return TimberURLHelper::is_external( $url ); |
|
652 | - } |
|
653 | - |
|
654 | - /** |
|
655 | - * @deprecated 0.18.0 |
|
656 | - */ |
|
657 | - static function download_url( $url, $timeout = 300 ) { |
|
658 | - return TimberURLHelper::download_url( $url, $timeout ); |
|
659 | - } |
|
660 | - |
|
661 | - /** |
|
662 | - * @deprecated 0.18.0 |
|
663 | - */ |
|
664 | - static function get_params( $i = -1 ) { |
|
665 | - return TimberURLHelper::get_params( $i ); |
|
666 | - } |
|
490 | + /** |
|
491 | + * |
|
492 | + * Gets the comment form for use on a single article page |
|
493 | + * @deprecated 0.21.8 use `{{ function('comment_form') }}` instead |
|
494 | + * @param int $post_id which post_id should the form be tied to? |
|
495 | + * @param array $args this $args thing is a fucking mess, [fix at some point](http://codex.wordpress.org/Function_Reference/comment_form) |
|
496 | + * @return string |
|
497 | + */ |
|
498 | + public static function get_comment_form( $post_id = null, $args = array() ) { |
|
499 | + return self::ob_function( 'comment_form', array( $args, $post_id ) ); |
|
500 | + } |
|
501 | + |
|
502 | + /** |
|
503 | + * |
|
504 | + * |
|
505 | + * @param string $args |
|
506 | + * @return array |
|
507 | + */ |
|
508 | + public static function paginate_links( $args = '' ) { |
|
509 | + $defaults = array( |
|
510 | + 'base' => '%_%', // http://example.com/all_posts.php%_% : %_% is replaced by format (below) |
|
511 | + 'format' => '?page=%#%', // ?page=%#% : %#% is replaced by the page number |
|
512 | + 'total' => 1, |
|
513 | + 'current' => 0, |
|
514 | + 'show_all' => false, |
|
515 | + 'prev_next' => false, |
|
516 | + 'prev_text' => __( '« Previous' ), |
|
517 | + 'next_text' => __( 'Next »' ), |
|
518 | + 'end_size' => 1, |
|
519 | + 'mid_size' => 2, |
|
520 | + 'type' => 'array', |
|
521 | + 'add_args' => false, // array of query args to add |
|
522 | + 'add_fragment' => '' |
|
523 | + ); |
|
524 | + $args = wp_parse_args( $args, $defaults ); |
|
525 | + // Who knows what else people pass in $args |
|
526 | + $args['total'] = intval( (int)$args['total'] ); |
|
527 | + if ( $args['total'] < 2 ) { |
|
528 | + return array(); |
|
529 | + } |
|
530 | + $args['current'] = (int)$args['current']; |
|
531 | + $args['end_size'] = 0 < (int)$args['end_size'] ? (int)$args['end_size'] : 1; // Out of bounds? Make it the default. |
|
532 | + $args['mid_size'] = 0 <= (int)$args['mid_size'] ? (int)$args['mid_size'] : 2; |
|
533 | + $args['add_args'] = is_array( $args['add_args'] ) ? $args['add_args'] : false; |
|
534 | + $page_links = array(); |
|
535 | + $dots = false; |
|
536 | + for ( $n = 1; $n <= $args['total']; $n++ ) { |
|
537 | + $n_display = number_format_i18n( $n ); |
|
538 | + if ( $n == $args['current'] ) { |
|
539 | + $page_links[] = array( |
|
540 | + 'class' => 'page-number page-numbers current', |
|
541 | + 'title' => $n_display, |
|
542 | + 'text' => $n_display, |
|
543 | + 'name' => $n_display, |
|
544 | + 'current' => true |
|
545 | + ); |
|
546 | + $dots = true; |
|
547 | + } else { |
|
548 | + if ( $args['show_all'] || ( $n <= $args['end_size'] || ( $args['current'] && $n >= $args['current'] - $args['mid_size'] && $n <= $args['current'] + $args['mid_size'] ) || $n > $args['total'] - $args['end_size'] ) ) { |
|
549 | + $link = str_replace( '%_%', 1 == $n ? '' : $args['format'], $args['base'] ); |
|
550 | + $link = str_replace( '%#%', $n, $link ); |
|
551 | + $link = trailingslashit( $link ) . ltrim( $args['add_fragment'], '/' ); |
|
552 | + if ( $args['add_args'] ) { |
|
553 | + $link = rtrim( add_query_arg( $args['add_args'], $link ), '/' ); |
|
554 | + } |
|
555 | + $link = str_replace(' ', '+', $link); |
|
556 | + $link = untrailingslashit( $link ); |
|
557 | + $page_links[] = array( |
|
558 | + 'class' => 'page-number page-numbers', |
|
559 | + 'link' => esc_url( apply_filters( 'paginate_links', $link ) ), |
|
560 | + 'title' => $n_display, |
|
561 | + 'name' => $n_display, |
|
562 | + 'current' => $args['current'] == $n |
|
563 | + ); |
|
564 | + $dots = true; |
|
565 | + } elseif ( $dots && !$args['show_all'] ) { |
|
566 | + $page_links[] = array( |
|
567 | + 'class' => 'dots', |
|
568 | + 'title' => __( '…' ) |
|
569 | + ); |
|
570 | + $dots = false; |
|
571 | + } |
|
572 | + } |
|
573 | + } |
|
574 | + return $page_links; |
|
575 | + } |
|
576 | + |
|
577 | + /** |
|
578 | + * @deprecated 0.18.0 |
|
579 | + */ |
|
580 | + static function get_current_url() { |
|
581 | + return TimberURLHelper::get_current_url(); |
|
582 | + } |
|
583 | + |
|
584 | + /** |
|
585 | + * @deprecated 0.18.0 |
|
586 | + */ |
|
587 | + static function is_url( $url ) { |
|
588 | + return TimberURLHelper::is_url( $url ); |
|
589 | + } |
|
590 | + |
|
591 | + /** |
|
592 | + * @deprecated 0.18.0 |
|
593 | + */ |
|
594 | + static function get_path_base() { |
|
595 | + return TimberURLHelper::get_path_base(); |
|
596 | + } |
|
597 | + |
|
598 | + /** |
|
599 | + * @deprecated 0.18.0 |
|
600 | + */ |
|
601 | + static function get_rel_url( $url, $force = false ) { |
|
602 | + return TimberURLHelper::get_rel_url( $url, $force ); |
|
603 | + } |
|
604 | + |
|
605 | + /** |
|
606 | + * @deprecated 0.18.0 |
|
607 | + */ |
|
608 | + static function is_local( $url ) { |
|
609 | + return TimberURLHelper::is_local( $url ); |
|
610 | + } |
|
611 | + |
|
612 | + /** |
|
613 | + * @deprecated 0.18.0 |
|
614 | + */ |
|
615 | + static function get_full_path( $src ) { |
|
616 | + return TimberURLHelper::get_full_path( $src ); |
|
617 | + } |
|
618 | + |
|
619 | + /** |
|
620 | + * @deprecated 0.18.0 |
|
621 | + */ |
|
622 | + static function get_rel_path( $src ) { |
|
623 | + return TimberURLHelper::get_rel_path( $src ); |
|
624 | + } |
|
625 | + |
|
626 | + /** |
|
627 | + * @deprecated 0.18.0 |
|
628 | + */ |
|
629 | + static function remove_double_slashes( $url ) { |
|
630 | + return TimberURLHelper::remove_double_slashes( $url ); |
|
631 | + } |
|
632 | + |
|
633 | + /** |
|
634 | + * @deprecated 0.18.0 |
|
635 | + */ |
|
636 | + static function prepend_to_url( $url, $path ) { |
|
637 | + return TimberURLHelper::prepend_to_url( $url, $path ); |
|
638 | + } |
|
639 | + |
|
640 | + /** |
|
641 | + * @deprecated 0.18.0 |
|
642 | + */ |
|
643 | + static function preslashit( $path ) { |
|
644 | + return TimberURLHelper::preslashit( $path ); |
|
645 | + } |
|
646 | + |
|
647 | + /** |
|
648 | + * @deprecated 0.18.0 |
|
649 | + */ |
|
650 | + static function is_external( $url ) { |
|
651 | + return TimberURLHelper::is_external( $url ); |
|
652 | + } |
|
653 | + |
|
654 | + /** |
|
655 | + * @deprecated 0.18.0 |
|
656 | + */ |
|
657 | + static function download_url( $url, $timeout = 300 ) { |
|
658 | + return TimberURLHelper::download_url( $url, $timeout ); |
|
659 | + } |
|
660 | + |
|
661 | + /** |
|
662 | + * @deprecated 0.18.0 |
|
663 | + */ |
|
664 | + static function get_params( $i = -1 ) { |
|
665 | + return TimberURLHelper::get_params( $i ); |
|
666 | + } |
|
667 | 667 | |
668 | 668 | } |
@@ -26,17 +26,17 @@ discard block |
||
26 | 26 | * @return mixed |
27 | 27 | */ |
28 | 28 | public static function transient( $slug, $callback, $transient_time = 0, $lock_timeout = 5, $force = false ) { |
29 | - $slug = apply_filters( 'timber/transient/slug', $slug ); |
|
29 | + $slug = apply_filters('timber/transient/slug', $slug); |
|
30 | 30 | |
31 | - $enable_transients = ( $transient_time === false || ( defined( 'WP_DISABLE_TRANSIENTS' ) && WP_DISABLE_TRANSIENTS ) ) ? false : true; |
|
32 | - $data = $enable_transients ? get_transient( $slug ) : false; |
|
31 | + $enable_transients = ($transient_time === false || (defined('WP_DISABLE_TRANSIENTS') && WP_DISABLE_TRANSIENTS)) ? false : true; |
|
32 | + $data = $enable_transients ? get_transient($slug) : false; |
|
33 | 33 | |
34 | 34 | if ( false === $data ) { |
35 | 35 | |
36 | - if ( $enable_transients && self::_is_transient_locked( $slug ) ) { |
|
36 | + if ( $enable_transients && self::_is_transient_locked($slug) ) { |
|
37 | 37 | |
38 | - $force = apply_filters( 'timber_force_transients', $force ); |
|
39 | - $force = apply_filters( 'timber_force_transient_' . $slug, $force ); |
|
38 | + $force = apply_filters('timber_force_transients', $force); |
|
39 | + $force = apply_filters('timber_force_transient_' . $slug, $force); |
|
40 | 40 | |
41 | 41 | if ( !$force ) { |
42 | 42 | //the server is currently executing the process. |
@@ -50,13 +50,13 @@ discard block |
||
50 | 50 | // lock timeout shouldn't be higher than 5 seconds, unless |
51 | 51 | // remote calls with high timeouts are made here |
52 | 52 | if ( $enable_transients ) |
53 | - self::_lock_transient( $slug, $lock_timeout ); |
|
53 | + self::_lock_transient($slug, $lock_timeout); |
|
54 | 54 | |
55 | 55 | $data = $callback(); |
56 | 56 | |
57 | 57 | if ( $enable_transients ) { |
58 | - set_transient( $slug, $data, $transient_time ); |
|
59 | - self::_unlock_transient( $slug ); |
|
58 | + set_transient($slug, $data, $transient_time); |
|
59 | + self::_unlock_transient($slug); |
|
60 | 60 | } |
61 | 61 | |
62 | 62 | } |
@@ -71,7 +71,7 @@ discard block |
||
71 | 71 | * @param integer $lock_timeout |
72 | 72 | */ |
73 | 73 | static function _lock_transient( $slug, $lock_timeout ) { |
74 | - set_transient( $slug . '_lock', true, $lock_timeout ); |
|
74 | + set_transient($slug . '_lock', true, $lock_timeout); |
|
75 | 75 | } |
76 | 76 | |
77 | 77 | /** |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | * @param string $slug |
80 | 80 | */ |
81 | 81 | static function _unlock_transient( $slug ) { |
82 | - delete_transient( $slug . '_lock', true ); |
|
82 | + delete_transient($slug . '_lock', true); |
|
83 | 83 | } |
84 | 84 | |
85 | 85 | /** |
@@ -87,7 +87,7 @@ discard block |
||
87 | 87 | * @param string $slug |
88 | 88 | */ |
89 | 89 | static function _is_transient_locked( $slug ) { |
90 | - return (bool)get_transient( $slug . '_lock' ); |
|
90 | + return (bool) get_transient($slug . '_lock'); |
|
91 | 91 | } |
92 | 92 | |
93 | 93 | /* These are for measuring page render time */ |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | */ |
100 | 100 | public static function start_timer() { |
101 | 101 | $time = microtime(); |
102 | - $time = explode( ' ', $time ); |
|
102 | + $time = explode(' ', $time); |
|
103 | 103 | $time = $time[1] + $time[0]; |
104 | 104 | return $time; |
105 | 105 | } |
@@ -117,10 +117,10 @@ discard block |
||
117 | 117 | */ |
118 | 118 | public static function stop_timer( $start ) { |
119 | 119 | $time = microtime(); |
120 | - $time = explode( ' ', $time ); |
|
120 | + $time = explode(' ', $time); |
|
121 | 121 | $time = $time[1] + $time[0]; |
122 | 122 | $finish = $time; |
123 | - $total_time = round( ( $finish - $start ), 4 ); |
|
123 | + $total_time = round(($finish - $start), 4); |
|
124 | 124 | return $total_time . ' seconds.'; |
125 | 125 | } |
126 | 126 | |
@@ -153,9 +153,9 @@ discard block |
||
153 | 153 | * @param array $args |
154 | 154 | * @return string |
155 | 155 | */ |
156 | - public static function ob_function( $function, $args = array( null ) ) { |
|
156 | + public static function ob_function( $function, $args = array(null) ) { |
|
157 | 157 | ob_start(); |
158 | - call_user_func_array( $function, $args ); |
|
158 | + call_user_func_array($function, $args); |
|
159 | 159 | $data = ob_get_contents(); |
160 | 160 | ob_end_clean(); |
161 | 161 | return $data; |
@@ -170,7 +170,7 @@ discard block |
||
170 | 170 | * @return TimberFunctionWrapper |
171 | 171 | */ |
172 | 172 | public static function function_wrapper( $function_name, $defaults = array(), $return_output_buffer = false ) { |
173 | - return new TimberFunctionWrapper( $function_name, $defaults, $return_output_buffer ); |
|
173 | + return new TimberFunctionWrapper($function_name, $defaults, $return_output_buffer); |
|
174 | 174 | } |
175 | 175 | |
176 | 176 | /** |
@@ -183,10 +183,10 @@ discard block |
||
183 | 183 | if ( !WP_DEBUG ) { |
184 | 184 | return; |
185 | 185 | } |
186 | - if ( is_object( $arg ) || is_array( $arg ) ) { |
|
187 | - $arg = print_r( $arg, true ); |
|
186 | + if ( is_object($arg) || is_array($arg) ) { |
|
187 | + $arg = print_r($arg, true); |
|
188 | 188 | } |
189 | - return error_log( $arg ); |
|
189 | + return error_log($arg); |
|
190 | 190 | } |
191 | 191 | |
192 | 192 | /** |
@@ -197,8 +197,8 @@ discard block |
||
197 | 197 | * @return string |
198 | 198 | */ |
199 | 199 | public static function get_wp_title( $separator = ' ', $seplocation = 'left' ) { |
200 | - $separator = apply_filters( 'timber_wp_title_seperator', $separator ); |
|
201 | - return trim( wp_title( $separator, false, $seplocation ) ); |
|
200 | + $separator = apply_filters('timber_wp_title_seperator', $separator); |
|
201 | + return trim(wp_title($separator, false, $seplocation)); |
|
202 | 202 | } |
203 | 203 | |
204 | 204 | /* Text Utilities |
@@ -215,33 +215,33 @@ discard block |
||
215 | 215 | */ |
216 | 216 | public static function trim_words( $text, $num_words = 55, $more = null, $allowed_tags = 'p a span b i br blockquote' ) { |
217 | 217 | if ( null === $more ) { |
218 | - $more = __( '…' ); |
|
218 | + $more = __('…'); |
|
219 | 219 | } |
220 | 220 | $original_text = $text; |
221 | 221 | $allowed_tag_string = ''; |
222 | - foreach ( explode( ' ', apply_filters( 'timber/trim_words/allowed_tags', $allowed_tags ) ) as $tag ) { |
|
222 | + foreach ( explode(' ', apply_filters('timber/trim_words/allowed_tags', $allowed_tags)) as $tag ) { |
|
223 | 223 | $allowed_tag_string .= '<' . $tag . '>'; |
224 | 224 | } |
225 | - $text = strip_tags( $text, $allowed_tag_string ); |
|
225 | + $text = strip_tags($text, $allowed_tag_string); |
|
226 | 226 | /* translators: If your word count is based on single characters (East Asian characters), enter 'characters'. Otherwise, enter 'words'. Do not translate into your own language. */ |
227 | - if ( 'characters' == _x( 'words', 'word count: words or characters?' ) && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) { |
|
228 | - $text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' ); |
|
229 | - preg_match_all( '/./u', $text, $words_array ); |
|
230 | - $words_array = array_slice( $words_array[0], 0, $num_words + 1 ); |
|
227 | + if ( 'characters' == _x('words', 'word count: words or characters?') && preg_match('/^utf\-?8$/i', get_option('blog_charset')) ) { |
|
228 | + $text = trim(preg_replace("/[\n\r\t ]+/", ' ', $text), ' '); |
|
229 | + preg_match_all('/./u', $text, $words_array); |
|
230 | + $words_array = array_slice($words_array[0], 0, $num_words + 1); |
|
231 | 231 | $sep = ''; |
232 | 232 | } else { |
233 | - $words_array = preg_split( "/[\n\r\t ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY ); |
|
233 | + $words_array = preg_split("/[\n\r\t ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY); |
|
234 | 234 | $sep = ' '; |
235 | 235 | } |
236 | - if ( count( $words_array ) > $num_words ) { |
|
237 | - array_pop( $words_array ); |
|
238 | - $text = implode( $sep, $words_array ); |
|
236 | + if ( count($words_array) > $num_words ) { |
|
237 | + array_pop($words_array); |
|
238 | + $text = implode($sep, $words_array); |
|
239 | 239 | $text = $text . $more; |
240 | 240 | } else { |
241 | - $text = implode( $sep, $words_array ); |
|
241 | + $text = implode($sep, $words_array); |
|
242 | 242 | } |
243 | - $text = self::close_tags( $text ); |
|
244 | - return apply_filters( 'wp_trim_words', $text, $num_words, $more, $original_text ); |
|
243 | + $text = self::close_tags($text); |
|
244 | + return apply_filters('wp_trim_words', $text, $num_words, $more, $original_text); |
|
245 | 245 | } |
246 | 246 | |
247 | 247 | /** |
@@ -252,27 +252,27 @@ discard block |
||
252 | 252 | */ |
253 | 253 | public static function close_tags( $html ) { |
254 | 254 | //put all opened tags into an array |
255 | - preg_match_all( '#<([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result ); |
|
255 | + preg_match_all('#<([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result); |
|
256 | 256 | $openedtags = $result[1]; |
257 | 257 | //put all closed tags into an array |
258 | - preg_match_all( '#</([a-z]+)>#iU', $html, $result ); |
|
258 | + preg_match_all('#</([a-z]+)>#iU', $html, $result); |
|
259 | 259 | $closedtags = $result[1]; |
260 | - $len_opened = count( $openedtags ); |
|
260 | + $len_opened = count($openedtags); |
|
261 | 261 | // all tags are closed |
262 | - if ( count( $closedtags ) == $len_opened ) { |
|
262 | + if ( count($closedtags) == $len_opened ) { |
|
263 | 263 | return $html; |
264 | 264 | } |
265 | - $openedtags = array_reverse( $openedtags ); |
|
265 | + $openedtags = array_reverse($openedtags); |
|
266 | 266 | // close tags |
267 | 267 | for ( $i = 0; $i < $len_opened; $i++ ) { |
268 | - if ( !in_array( $openedtags[$i], $closedtags ) ) { |
|
268 | + if ( !in_array($openedtags[$i], $closedtags) ) { |
|
269 | 269 | $html .= '</' . $openedtags[$i] . '>'; |
270 | 270 | } else { |
271 | - unset( $closedtags[array_search( $openedtags[$i], $closedtags )] ); |
|
271 | + unset($closedtags[array_search($openedtags[$i], $closedtags)]); |
|
272 | 272 | } |
273 | 273 | } |
274 | - $html = str_replace(array('</br>','</hr>','</wbr>'), '', $html); |
|
275 | - $html = str_replace(array('<br>','<hr>','<wbr>'), array('<br />','<hr />','<wbr />'), $html); |
|
274 | + $html = str_replace(array('</br>', '</hr>', '</wbr>'), '', $html); |
|
275 | + $html = str_replace(array('<br>', '<hr>', '<wbr>'), array('<br />', '<hr />', '<wbr />'), $html); |
|
276 | 276 | return $html; |
277 | 277 | } |
278 | 278 | |
@@ -287,15 +287,15 @@ discard block |
||
287 | 287 | */ |
288 | 288 | public static function get_posts_by_meta( $key, $value ) { |
289 | 289 | global $wpdb; |
290 | - $query = $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s", $key, $value ); |
|
291 | - $results = $wpdb->get_col( $query ); |
|
290 | + $query = $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s", $key, $value); |
|
291 | + $results = $wpdb->get_col($query); |
|
292 | 292 | $pids = array(); |
293 | 293 | foreach ( $results as $result ) { |
294 | - if ( get_post( $result ) ) { |
|
294 | + if ( get_post($result) ) { |
|
295 | 295 | $pids[] = $result; |
296 | 296 | } |
297 | 297 | } |
298 | - if ( count( $pids ) ) { |
|
298 | + if ( count($pids) ) { |
|
299 | 299 | return $pids; |
300 | 300 | } |
301 | 301 | return 0; |
@@ -311,10 +311,10 @@ discard block |
||
311 | 311 | */ |
312 | 312 | public static function get_post_by_meta( $key, $value ) { |
313 | 313 | global $wpdb; |
314 | - $query = $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s ORDER BY post_id", $key, $value ); |
|
315 | - $results = $wpdb->get_col( $query ); |
|
314 | + $query = $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s ORDER BY post_id", $key, $value); |
|
315 | + $results = $wpdb->get_col($query); |
|
316 | 316 | foreach ( $results as $result ) { |
317 | - if ( $result && get_post( $result ) ) { |
|
317 | + if ( $result && get_post($result) ) { |
|
318 | 318 | return $result; |
319 | 319 | } |
320 | 320 | } |
@@ -329,8 +329,8 @@ discard block |
||
329 | 329 | */ |
330 | 330 | public static function get_term_id_by_term_taxonomy_id( $ttid ) { |
331 | 331 | global $wpdb; |
332 | - $query = $wpdb->prepare( "SELECT term_id FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %s", $ttid ); |
|
333 | - return $wpdb->get_var( $query ); |
|
332 | + $query = $wpdb->prepare("SELECT term_id FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %s", $ttid); |
|
333 | + return $wpdb->get_var($query); |
|
334 | 334 | } |
335 | 335 | |
336 | 336 | /* Object Utilities |
@@ -344,7 +344,7 @@ discard block |
||
344 | 344 | * @return void |
345 | 345 | */ |
346 | 346 | public static function osort( &$array, $prop ) { |
347 | - usort( $array, function ( $a, $b ) use ( $prop ) { |
|
347 | + usort($array, function( $a, $b ) use ($prop) { |
|
348 | 348 | return $a->$prop > $b->$prop ? 1 : -1; |
349 | 349 | } ); |
350 | 350 | } |
@@ -356,10 +356,10 @@ discard block |
||
356 | 356 | * @return bool |
357 | 357 | */ |
358 | 358 | public static function is_array_assoc( $arr ) { |
359 | - if ( !is_array( $arr ) ) { |
|
359 | + if ( !is_array($arr) ) { |
|
360 | 360 | return false; |
361 | 361 | } |
362 | - return (bool)count( array_filter( array_keys( $arr ), 'is_string' ) ); |
|
362 | + return (bool) count(array_filter(array_keys($arr), 'is_string')); |
|
363 | 363 | } |
364 | 364 | |
365 | 365 | /** |
@@ -371,8 +371,8 @@ discard block |
||
371 | 371 | public static function array_to_object( $array ) { |
372 | 372 | $obj = new stdClass; |
373 | 373 | foreach ( $array as $k => $v ) { |
374 | - if ( is_array( $v ) ) { |
|
375 | - $obj->{$k} = self::array_to_object( $v ); //RECURSION |
|
374 | + if ( is_array($v) ) { |
|
375 | + $obj->{$k} = self::array_to_object($v); //RECURSION |
|
376 | 376 | } else { |
377 | 377 | $obj->{$k} = $v; |
378 | 378 | } |
@@ -389,10 +389,10 @@ discard block |
||
389 | 389 | * @return bool|int |
390 | 390 | */ |
391 | 391 | public static function get_object_index_by_property( $array, $key, $value ) { |
392 | - if ( is_array( $array ) ) { |
|
392 | + if ( is_array($array) ) { |
|
393 | 393 | $i = 0; |
394 | 394 | foreach ( $array as $arr ) { |
395 | - if ( is_array( $arr ) ) { |
|
395 | + if ( is_array($arr) ) { |
|
396 | 396 | if ( $arr[$key] == $value ) { |
397 | 397 | return $i; |
398 | 398 | } |
@@ -417,15 +417,15 @@ discard block |
||
417 | 417 | * @throws Exception |
418 | 418 | */ |
419 | 419 | public static function get_object_by_property( $array, $key, $value ) { |
420 | - if ( is_array( $array ) ) { |
|
420 | + if ( is_array($array) ) { |
|
421 | 421 | foreach ( $array as $arr ) { |
422 | 422 | if ( $arr->$key == $value ) { |
423 | 423 | return $arr; |
424 | 424 | } |
425 | 425 | } |
426 | 426 | } else { |
427 | - throw new InvalidArgumentException( '$array is not an array, got:' ); |
|
428 | - TimberHelper::error_log( $array ); |
|
427 | + throw new InvalidArgumentException('$array is not an array, got:'); |
|
428 | + TimberHelper::error_log($array); |
|
429 | 429 | } |
430 | 430 | } |
431 | 431 | |
@@ -437,8 +437,8 @@ discard block |
||
437 | 437 | * @return array |
438 | 438 | */ |
439 | 439 | public static function array_truncate( $array, $len ) { |
440 | - if ( sizeof( $array ) > $len ) { |
|
441 | - $array = array_splice( $array, 0, $len ); |
|
440 | + if ( sizeof($array) > $len ) { |
|
441 | + $array = array_splice($array, 0, $len); |
|
442 | 442 | } |
443 | 443 | return $array; |
444 | 444 | } |
@@ -453,11 +453,11 @@ discard block |
||
453 | 453 | * @return bool |
454 | 454 | */ |
455 | 455 | public static function is_true( $value ) { |
456 | - if ( isset( $value ) ) { |
|
457 | - if (is_string($value)) { |
|
456 | + if ( isset($value) ) { |
|
457 | + if ( is_string($value) ) { |
|
458 | 458 | $value = strtolower($value); |
459 | 459 | } |
460 | - if ( ($value == 'true' || $value === 1 || $value === '1' || $value == true) && $value !== false && $value !== 'false') { |
|
460 | + if ( ($value == 'true' || $value === 1 || $value === '1' || $value == true) && $value !== false && $value !== 'false' ) { |
|
461 | 461 | return true; |
462 | 462 | } |
463 | 463 | } |
@@ -471,7 +471,7 @@ discard block |
||
471 | 471 | * @return bool |
472 | 472 | */ |
473 | 473 | public static function iseven( $i ) { |
474 | - return ( $i % 2 ) == 0; |
|
474 | + return ($i % 2) == 0; |
|
475 | 475 | } |
476 | 476 | |
477 | 477 | /** |
@@ -481,7 +481,7 @@ discard block |
||
481 | 481 | * @return bool |
482 | 482 | */ |
483 | 483 | public static function isodd( $i ) { |
484 | - return ( $i % 2 ) != 0; |
|
484 | + return ($i % 2) != 0; |
|
485 | 485 | } |
486 | 486 | |
487 | 487 | /* Links, Forms, Etc. Utilities |
@@ -496,7 +496,7 @@ discard block |
||
496 | 496 | * @return string |
497 | 497 | */ |
498 | 498 | public static function get_comment_form( $post_id = null, $args = array() ) { |
499 | - return self::ob_function( 'comment_form', array( $args, $post_id ) ); |
|
499 | + return self::ob_function('comment_form', array($args, $post_id)); |
|
500 | 500 | } |
501 | 501 | |
502 | 502 | /** |
@@ -513,28 +513,28 @@ discard block |
||
513 | 513 | 'current' => 0, |
514 | 514 | 'show_all' => false, |
515 | 515 | 'prev_next' => false, |
516 | - 'prev_text' => __( '« Previous' ), |
|
517 | - 'next_text' => __( 'Next »' ), |
|
516 | + 'prev_text' => __('« Previous'), |
|
517 | + 'next_text' => __('Next »'), |
|
518 | 518 | 'end_size' => 1, |
519 | 519 | 'mid_size' => 2, |
520 | 520 | 'type' => 'array', |
521 | 521 | 'add_args' => false, // array of query args to add |
522 | 522 | 'add_fragment' => '' |
523 | 523 | ); |
524 | - $args = wp_parse_args( $args, $defaults ); |
|
524 | + $args = wp_parse_args($args, $defaults); |
|
525 | 525 | // Who knows what else people pass in $args |
526 | - $args['total'] = intval( (int)$args['total'] ); |
|
526 | + $args['total'] = intval((int) $args['total']); |
|
527 | 527 | if ( $args['total'] < 2 ) { |
528 | 528 | return array(); |
529 | 529 | } |
530 | - $args['current'] = (int)$args['current']; |
|
531 | - $args['end_size'] = 0 < (int)$args['end_size'] ? (int)$args['end_size'] : 1; // Out of bounds? Make it the default. |
|
532 | - $args['mid_size'] = 0 <= (int)$args['mid_size'] ? (int)$args['mid_size'] : 2; |
|
533 | - $args['add_args'] = is_array( $args['add_args'] ) ? $args['add_args'] : false; |
|
530 | + $args['current'] = (int) $args['current']; |
|
531 | + $args['end_size'] = 0 < (int) $args['end_size'] ? (int) $args['end_size'] : 1; // Out of bounds? Make it the default. |
|
532 | + $args['mid_size'] = 0 <= (int) $args['mid_size'] ? (int) $args['mid_size'] : 2; |
|
533 | + $args['add_args'] = is_array($args['add_args']) ? $args['add_args'] : false; |
|
534 | 534 | $page_links = array(); |
535 | 535 | $dots = false; |
536 | 536 | for ( $n = 1; $n <= $args['total']; $n++ ) { |
537 | - $n_display = number_format_i18n( $n ); |
|
537 | + $n_display = number_format_i18n($n); |
|
538 | 538 | if ( $n == $args['current'] ) { |
539 | 539 | $page_links[] = array( |
540 | 540 | 'class' => 'page-number page-numbers current', |
@@ -545,18 +545,18 @@ discard block |
||
545 | 545 | ); |
546 | 546 | $dots = true; |
547 | 547 | } else { |
548 | - if ( $args['show_all'] || ( $n <= $args['end_size'] || ( $args['current'] && $n >= $args['current'] - $args['mid_size'] && $n <= $args['current'] + $args['mid_size'] ) || $n > $args['total'] - $args['end_size'] ) ) { |
|
549 | - $link = str_replace( '%_%', 1 == $n ? '' : $args['format'], $args['base'] ); |
|
550 | - $link = str_replace( '%#%', $n, $link ); |
|
551 | - $link = trailingslashit( $link ) . ltrim( $args['add_fragment'], '/' ); |
|
548 | + if ( $args['show_all'] || ($n <= $args['end_size'] || ($args['current'] && $n >= $args['current'] - $args['mid_size'] && $n <= $args['current'] + $args['mid_size']) || $n > $args['total'] - $args['end_size']) ) { |
|
549 | + $link = str_replace('%_%', 1 == $n ? '' : $args['format'], $args['base']); |
|
550 | + $link = str_replace('%#%', $n, $link); |
|
551 | + $link = trailingslashit($link) . ltrim($args['add_fragment'], '/'); |
|
552 | 552 | if ( $args['add_args'] ) { |
553 | - $link = rtrim( add_query_arg( $args['add_args'], $link ), '/' ); |
|
553 | + $link = rtrim(add_query_arg($args['add_args'], $link), '/'); |
|
554 | 554 | } |
555 | 555 | $link = str_replace(' ', '+', $link); |
556 | - $link = untrailingslashit( $link ); |
|
556 | + $link = untrailingslashit($link); |
|
557 | 557 | $page_links[] = array( |
558 | 558 | 'class' => 'page-number page-numbers', |
559 | - 'link' => esc_url( apply_filters( 'paginate_links', $link ) ), |
|
559 | + 'link' => esc_url(apply_filters('paginate_links', $link)), |
|
560 | 560 | 'title' => $n_display, |
561 | 561 | 'name' => $n_display, |
562 | 562 | 'current' => $args['current'] == $n |
@@ -565,7 +565,7 @@ discard block |
||
565 | 565 | } elseif ( $dots && !$args['show_all'] ) { |
566 | 566 | $page_links[] = array( |
567 | 567 | 'class' => 'dots', |
568 | - 'title' => __( '…' ) |
|
568 | + 'title' => __('…') |
|
569 | 569 | ); |
570 | 570 | $dots = false; |
571 | 571 | } |
@@ -585,7 +585,7 @@ discard block |
||
585 | 585 | * @deprecated 0.18.0 |
586 | 586 | */ |
587 | 587 | static function is_url( $url ) { |
588 | - return TimberURLHelper::is_url( $url ); |
|
588 | + return TimberURLHelper::is_url($url); |
|
589 | 589 | } |
590 | 590 | |
591 | 591 | /** |
@@ -599,70 +599,70 @@ discard block |
||
599 | 599 | * @deprecated 0.18.0 |
600 | 600 | */ |
601 | 601 | static function get_rel_url( $url, $force = false ) { |
602 | - return TimberURLHelper::get_rel_url( $url, $force ); |
|
602 | + return TimberURLHelper::get_rel_url($url, $force); |
|
603 | 603 | } |
604 | 604 | |
605 | 605 | /** |
606 | 606 | * @deprecated 0.18.0 |
607 | 607 | */ |
608 | 608 | static function is_local( $url ) { |
609 | - return TimberURLHelper::is_local( $url ); |
|
609 | + return TimberURLHelper::is_local($url); |
|
610 | 610 | } |
611 | 611 | |
612 | 612 | /** |
613 | 613 | * @deprecated 0.18.0 |
614 | 614 | */ |
615 | 615 | static function get_full_path( $src ) { |
616 | - return TimberURLHelper::get_full_path( $src ); |
|
616 | + return TimberURLHelper::get_full_path($src); |
|
617 | 617 | } |
618 | 618 | |
619 | 619 | /** |
620 | 620 | * @deprecated 0.18.0 |
621 | 621 | */ |
622 | 622 | static function get_rel_path( $src ) { |
623 | - return TimberURLHelper::get_rel_path( $src ); |
|
623 | + return TimberURLHelper::get_rel_path($src); |
|
624 | 624 | } |
625 | 625 | |
626 | 626 | /** |
627 | 627 | * @deprecated 0.18.0 |
628 | 628 | */ |
629 | 629 | static function remove_double_slashes( $url ) { |
630 | - return TimberURLHelper::remove_double_slashes( $url ); |
|
630 | + return TimberURLHelper::remove_double_slashes($url); |
|
631 | 631 | } |
632 | 632 | |
633 | 633 | /** |
634 | 634 | * @deprecated 0.18.0 |
635 | 635 | */ |
636 | 636 | static function prepend_to_url( $url, $path ) { |
637 | - return TimberURLHelper::prepend_to_url( $url, $path ); |
|
637 | + return TimberURLHelper::prepend_to_url($url, $path); |
|
638 | 638 | } |
639 | 639 | |
640 | 640 | /** |
641 | 641 | * @deprecated 0.18.0 |
642 | 642 | */ |
643 | 643 | static function preslashit( $path ) { |
644 | - return TimberURLHelper::preslashit( $path ); |
|
644 | + return TimberURLHelper::preslashit($path); |
|
645 | 645 | } |
646 | 646 | |
647 | 647 | /** |
648 | 648 | * @deprecated 0.18.0 |
649 | 649 | */ |
650 | 650 | static function is_external( $url ) { |
651 | - return TimberURLHelper::is_external( $url ); |
|
651 | + return TimberURLHelper::is_external($url); |
|
652 | 652 | } |
653 | 653 | |
654 | 654 | /** |
655 | 655 | * @deprecated 0.18.0 |
656 | 656 | */ |
657 | 657 | static function download_url( $url, $timeout = 300 ) { |
658 | - return TimberURLHelper::download_url( $url, $timeout ); |
|
658 | + return TimberURLHelper::download_url($url, $timeout); |
|
659 | 659 | } |
660 | 660 | |
661 | 661 | /** |
662 | 662 | * @deprecated 0.18.0 |
663 | 663 | */ |
664 | 664 | static function get_params( $i = -1 ) { |
665 | - return TimberURLHelper::get_params( $i ); |
|
665 | + return TimberURLHelper::get_params($i); |
|
666 | 666 | } |
667 | 667 | |
668 | 668 | } |
@@ -85,7 +85,7 @@ discard block |
||
85 | 85 | * Generates a new image with increased size, for display on Retina screens. |
86 | 86 | * |
87 | 87 | * @param string $src |
88 | - * @param float $multiplier |
|
88 | + * @param integer $multiplier |
|
89 | 89 | * @param boolean $force |
90 | 90 | * |
91 | 91 | * @return string url to the new image |
@@ -136,7 +136,7 @@ discard block |
||
136 | 136 | * @param int $h |
137 | 137 | * @param string $color |
138 | 138 | * @param bool $force |
139 | - * @return mixed|null|string |
|
139 | + * @return string |
|
140 | 140 | */ |
141 | 141 | public static function letterbox( $src, $w, $h, $color = '#000000', $force = false ) { |
142 | 142 | $op = new TimberImageOperationLetterbox($w, $h, $color); |
@@ -325,7 +325,8 @@ |
||
325 | 325 | ); |
326 | 326 | $upload_dir = wp_upload_dir(); |
327 | 327 | $tmp = $url; |
328 | - if ( 0 === strpos($tmp, ABSPATH) ) { // we've been given a dir, not an url |
|
328 | + if ( 0 === strpos($tmp, ABSPATH) ) { |
|
329 | +// we've been given a dir, not an url |
|
329 | 330 | $result['absolute'] = true; |
330 | 331 | if ( 0 === strpos($tmp, $upload_dir['basedir']) ) { |
331 | 332 | $result['base']= self::BASE_UPLOADS; // upload based |
@@ -15,514 +15,514 @@ |
||
15 | 15 | */ |
16 | 16 | class TimberImageHelper { |
17 | 17 | |
18 | - const BASE_UPLOADS = 1; |
|
19 | - const BASE_CONTENT = 2; |
|
18 | + const BASE_UPLOADS = 1; |
|
19 | + const BASE_CONTENT = 2; |
|
20 | 20 | |
21 | - public static function init() { |
|
22 | - self::add_constants(); |
|
23 | - self::add_actions(); |
|
24 | - self::add_filters(); |
|
25 | - } |
|
21 | + public static function init() { |
|
22 | + self::add_constants(); |
|
23 | + self::add_actions(); |
|
24 | + self::add_filters(); |
|
25 | + } |
|
26 | 26 | |
27 | - /** |
|
28 | - * Generates a new image with the specified dimensions. |
|
29 | - * New dimensions are achieved by cropping to maintain ratio. |
|
30 | - * |
|
31 | - * @api |
|
32 | - * @param string $src an URL (absolute or relative) to the original image |
|
33 | - * @param int|string $w target width(int) or WordPress image size (WP-set or user-defined). |
|
34 | - * @param int $h target height (ignored if $w is WP image size). If not set, will ignore and resize based on $w only. |
|
35 | - * @param string $crop your choices are 'default', 'center', 'top', 'bottom', 'left', 'right' |
|
36 | - * @param bool $force |
|
37 | - * @example |
|
38 | - * ```twig |
|
39 | - * <img src="{{ image.src | resize(300, 200, 'top') }}" /> |
|
40 | - * ``` |
|
41 | - * ```html |
|
42 | - * <img src="http://example.org/wp-content/uploads/pic-300x200-c-top.jpg" /> |
|
43 | - * ``` |
|
44 | - * @return string (ex: ) |
|
45 | - */ |
|
46 | - public static function resize( $src, $w, $h = 0, $crop = 'default', $force = false ) { |
|
47 | - if ( !is_numeric($w) && is_string($w) ) { |
|
48 | - if ( $sizes = self::find_wp_dimensions($w) ) { |
|
49 | - $w = $sizes['w']; |
|
50 | - $h = $sizes['h']; |
|
51 | - } else { |
|
52 | - return $src; |
|
53 | - } |
|
54 | - } |
|
55 | - $op = new TimberImageOperationResize($w, $h, $crop); |
|
56 | - return self::_operate($src, $op, $force); |
|
57 | - } |
|
27 | + /** |
|
28 | + * Generates a new image with the specified dimensions. |
|
29 | + * New dimensions are achieved by cropping to maintain ratio. |
|
30 | + * |
|
31 | + * @api |
|
32 | + * @param string $src an URL (absolute or relative) to the original image |
|
33 | + * @param int|string $w target width(int) or WordPress image size (WP-set or user-defined). |
|
34 | + * @param int $h target height (ignored if $w is WP image size). If not set, will ignore and resize based on $w only. |
|
35 | + * @param string $crop your choices are 'default', 'center', 'top', 'bottom', 'left', 'right' |
|
36 | + * @param bool $force |
|
37 | + * @example |
|
38 | + * ```twig |
|
39 | + * <img src="{{ image.src | resize(300, 200, 'top') }}" /> |
|
40 | + * ``` |
|
41 | + * ```html |
|
42 | + * <img src="http://example.org/wp-content/uploads/pic-300x200-c-top.jpg" /> |
|
43 | + * ``` |
|
44 | + * @return string (ex: ) |
|
45 | + */ |
|
46 | + public static function resize( $src, $w, $h = 0, $crop = 'default', $force = false ) { |
|
47 | + if ( !is_numeric($w) && is_string($w) ) { |
|
48 | + if ( $sizes = self::find_wp_dimensions($w) ) { |
|
49 | + $w = $sizes['w']; |
|
50 | + $h = $sizes['h']; |
|
51 | + } else { |
|
52 | + return $src; |
|
53 | + } |
|
54 | + } |
|
55 | + $op = new TimberImageOperationResize($w, $h, $crop); |
|
56 | + return self::_operate($src, $op, $force); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Find the sizes of an image based on a defined image size |
|
61 | - * @param string $size the image size to search for |
|
62 | - * can be WordPress-defined ("medium") |
|
63 | - * or user-defined ("my-awesome-size") |
|
64 | - * @return false|array { |
|
65 | - * @type int w |
|
66 | - * @type int h |
|
67 | - * } |
|
68 | - */ |
|
69 | - private static function find_wp_dimensions( $size ) { |
|
70 | - global $_wp_additional_image_sizes; |
|
71 | - if ( isset($_wp_additional_image_sizes[$size]) ) { |
|
72 | - $w = $_wp_additional_image_sizes[$size]['width']; |
|
73 | - $h = $_wp_additional_image_sizes[$size]['height']; |
|
74 | - } else if ( in_array($size, array('thumbnail', 'medium', 'large')) ) { |
|
75 | - $w = get_option($size.'_size_w'); |
|
76 | - $h = get_option($size.'_size_h'); |
|
77 | - } |
|
78 | - if ( isset($w) && isset($h) && ($w || $h) ) { |
|
79 | - return array('w' => $w, 'h' => $h); |
|
80 | - } |
|
81 | - return false; |
|
82 | - } |
|
59 | + /** |
|
60 | + * Find the sizes of an image based on a defined image size |
|
61 | + * @param string $size the image size to search for |
|
62 | + * can be WordPress-defined ("medium") |
|
63 | + * or user-defined ("my-awesome-size") |
|
64 | + * @return false|array { |
|
65 | + * @type int w |
|
66 | + * @type int h |
|
67 | + * } |
|
68 | + */ |
|
69 | + private static function find_wp_dimensions( $size ) { |
|
70 | + global $_wp_additional_image_sizes; |
|
71 | + if ( isset($_wp_additional_image_sizes[$size]) ) { |
|
72 | + $w = $_wp_additional_image_sizes[$size]['width']; |
|
73 | + $h = $_wp_additional_image_sizes[$size]['height']; |
|
74 | + } else if ( in_array($size, array('thumbnail', 'medium', 'large')) ) { |
|
75 | + $w = get_option($size.'_size_w'); |
|
76 | + $h = get_option($size.'_size_h'); |
|
77 | + } |
|
78 | + if ( isset($w) && isset($h) && ($w || $h) ) { |
|
79 | + return array('w' => $w, 'h' => $h); |
|
80 | + } |
|
81 | + return false; |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * Generates a new image with increased size, for display on Retina screens. |
|
86 | - * |
|
87 | - * @param string $src |
|
88 | - * @param float $multiplier |
|
89 | - * @param boolean $force |
|
90 | - * |
|
91 | - * @return string url to the new image |
|
92 | - */ |
|
93 | - public static function retina_resize( $src, $multiplier = 2, $force = false ) { |
|
94 | - $op = new TimberImageOperationRetina($multiplier); |
|
95 | - return self::_operate($src, $op, $force); |
|
96 | - } |
|
84 | + /** |
|
85 | + * Generates a new image with increased size, for display on Retina screens. |
|
86 | + * |
|
87 | + * @param string $src |
|
88 | + * @param float $multiplier |
|
89 | + * @param boolean $force |
|
90 | + * |
|
91 | + * @return string url to the new image |
|
92 | + */ |
|
93 | + public static function retina_resize( $src, $multiplier = 2, $force = false ) { |
|
94 | + $op = new TimberImageOperationRetina($multiplier); |
|
95 | + return self::_operate($src, $op, $force); |
|
96 | + } |
|
97 | 97 | |
98 | - /** |
|
99 | - * checks to see if the given file is an aimated gif |
|
100 | - * @param string $file local filepath to a file, not a URL |
|
101 | - * @return boolean true if it's an animated gif, false if not |
|
102 | - */ |
|
103 | - public static function is_animated_gif( $file ) { |
|
104 | - if ( strpos(strtolower($file), '.gif') == -1 ) { |
|
105 | - //doesn't have .gif, bail |
|
106 | - return false; |
|
107 | - } |
|
108 | - //its a gif so test |
|
109 | - if( !($fh = @fopen($file, 'rb')) ) { |
|
110 | - return false; |
|
111 | - } |
|
112 | - $count = 0; |
|
113 | - //an animated gif contains multiple "frames", with each frame having a |
|
114 | - //header made up of: |
|
115 | - // * a static 4-byte sequence (\x00\x21\xF9\x04) |
|
116 | - // * 4 variable bytes |
|
117 | - // * a static 2-byte sequence (\x00\x2C) |
|
98 | + /** |
|
99 | + * checks to see if the given file is an aimated gif |
|
100 | + * @param string $file local filepath to a file, not a URL |
|
101 | + * @return boolean true if it's an animated gif, false if not |
|
102 | + */ |
|
103 | + public static function is_animated_gif( $file ) { |
|
104 | + if ( strpos(strtolower($file), '.gif') == -1 ) { |
|
105 | + //doesn't have .gif, bail |
|
106 | + return false; |
|
107 | + } |
|
108 | + //its a gif so test |
|
109 | + if( !($fh = @fopen($file, 'rb')) ) { |
|
110 | + return false; |
|
111 | + } |
|
112 | + $count = 0; |
|
113 | + //an animated gif contains multiple "frames", with each frame having a |
|
114 | + //header made up of: |
|
115 | + // * a static 4-byte sequence (\x00\x21\xF9\x04) |
|
116 | + // * 4 variable bytes |
|
117 | + // * a static 2-byte sequence (\x00\x2C) |
|
118 | 118 | |
119 | - // We read through the file til we reach the end of the file, or we've found |
|
120 | - // at least 2 frame headers |
|
121 | - while(!feof($fh) && $count < 2) { |
|
122 | - $chunk = fread($fh, 1024 * 100); //read 100kb at a time |
|
123 | - $count += preg_match_all('#\x00\x21\xF9\x04.{4}\x00[\x2C\x21]#s', $chunk, $matches); |
|
124 | - } |
|
119 | + // We read through the file til we reach the end of the file, or we've found |
|
120 | + // at least 2 frame headers |
|
121 | + while(!feof($fh) && $count < 2) { |
|
122 | + $chunk = fread($fh, 1024 * 100); //read 100kb at a time |
|
123 | + $count += preg_match_all('#\x00\x21\xF9\x04.{4}\x00[\x2C\x21]#s', $chunk, $matches); |
|
124 | + } |
|
125 | 125 | |
126 | - fclose($fh); |
|
127 | - return $count > 1; |
|
128 | - } |
|
126 | + fclose($fh); |
|
127 | + return $count > 1; |
|
128 | + } |
|
129 | 129 | |
130 | - /** |
|
131 | - * Generate a new image with the specified dimensions. |
|
132 | - * New dimensions are achieved by adding colored bands to maintain ratio. |
|
133 | - * |
|
134 | - * @param string $src |
|
135 | - * @param int $w |
|
136 | - * @param int $h |
|
137 | - * @param string $color |
|
138 | - * @param bool $force |
|
139 | - * @return mixed|null|string |
|
140 | - */ |
|
141 | - public static function letterbox( $src, $w, $h, $color = '#000000', $force = false ) { |
|
142 | - $op = new TimberImageOperationLetterbox($w, $h, $color); |
|
143 | - return self::_operate($src, $op, $force); |
|
144 | - } |
|
130 | + /** |
|
131 | + * Generate a new image with the specified dimensions. |
|
132 | + * New dimensions are achieved by adding colored bands to maintain ratio. |
|
133 | + * |
|
134 | + * @param string $src |
|
135 | + * @param int $w |
|
136 | + * @param int $h |
|
137 | + * @param string $color |
|
138 | + * @param bool $force |
|
139 | + * @return mixed|null|string |
|
140 | + */ |
|
141 | + public static function letterbox( $src, $w, $h, $color = '#000000', $force = false ) { |
|
142 | + $op = new TimberImageOperationLetterbox($w, $h, $color); |
|
143 | + return self::_operate($src, $op, $force); |
|
144 | + } |
|
145 | 145 | |
146 | - /** |
|
147 | - * Generates a new image by converting the source GIF or PNG into JPG |
|
148 | - * |
|
149 | - * @param string $src a url or path to the image (http://example.org/wp-content/uploads/2014/image.jpg) or (/wp-content/uploads/2014/image.jpg) |
|
150 | - * @param string $bghex |
|
151 | - * @return string |
|
152 | - */ |
|
153 | - public static function img_to_jpg( $src, $bghex = '#FFFFFF', $force = false ) { |
|
154 | - $op = new TimberImageOperationToJpg($bghex); |
|
155 | - return self::_operate($src, $op, $force); |
|
156 | - } |
|
146 | + /** |
|
147 | + * Generates a new image by converting the source GIF or PNG into JPG |
|
148 | + * |
|
149 | + * @param string $src a url or path to the image (http://example.org/wp-content/uploads/2014/image.jpg) or (/wp-content/uploads/2014/image.jpg) |
|
150 | + * @param string $bghex |
|
151 | + * @return string |
|
152 | + */ |
|
153 | + public static function img_to_jpg( $src, $bghex = '#FFFFFF', $force = false ) { |
|
154 | + $op = new TimberImageOperationToJpg($bghex); |
|
155 | + return self::_operate($src, $op, $force); |
|
156 | + } |
|
157 | 157 | |
158 | - /** |
|
159 | - * Deletes all resized versions of an image when the source is deleted |
|
160 | - */ |
|
161 | - protected static function add_actions() { |
|
162 | - add_action( 'delete_attachment', function ( $post_id ) { |
|
163 | - $post = get_post( $post_id ); |
|
164 | - $image_types = array( 'image/jpeg', 'image/png', 'image/gif', 'image/jpg' ); |
|
165 | - if ( in_array( $post->post_mime_type, $image_types ) ) { |
|
166 | - $attachment = new TimberImage( $post_id ); |
|
167 | - if ( $attachment->file_loc ) { |
|
168 | - TimberImageHelper::delete_generated_files( $attachment->file_loc ); |
|
169 | - } |
|
170 | - } |
|
171 | - } ); |
|
172 | - } |
|
158 | + /** |
|
159 | + * Deletes all resized versions of an image when the source is deleted |
|
160 | + */ |
|
161 | + protected static function add_actions() { |
|
162 | + add_action( 'delete_attachment', function ( $post_id ) { |
|
163 | + $post = get_post( $post_id ); |
|
164 | + $image_types = array( 'image/jpeg', 'image/png', 'image/gif', 'image/jpg' ); |
|
165 | + if ( in_array( $post->post_mime_type, $image_types ) ) { |
|
166 | + $attachment = new TimberImage( $post_id ); |
|
167 | + if ( $attachment->file_loc ) { |
|
168 | + TimberImageHelper::delete_generated_files( $attachment->file_loc ); |
|
169 | + } |
|
170 | + } |
|
171 | + } ); |
|
172 | + } |
|
173 | 173 | |
174 | - /** |
|
175 | - * Adds a constant defining the path to the content directory relative to the site |
|
176 | - * for example /wp-content or /content |
|
177 | - */ |
|
178 | - protected static function add_constants() { |
|
179 | - if ( !defined( 'WP_CONTENT_SUBDIR' ) ) { |
|
180 | - $wp_content_path = str_replace( home_url(), '', WP_CONTENT_URL ); |
|
181 | - define( 'WP_CONTENT_SUBDIR', $wp_content_path ); |
|
182 | - } |
|
183 | - } |
|
174 | + /** |
|
175 | + * Adds a constant defining the path to the content directory relative to the site |
|
176 | + * for example /wp-content or /content |
|
177 | + */ |
|
178 | + protected static function add_constants() { |
|
179 | + if ( !defined( 'WP_CONTENT_SUBDIR' ) ) { |
|
180 | + $wp_content_path = str_replace( home_url(), '', WP_CONTENT_URL ); |
|
181 | + define( 'WP_CONTENT_SUBDIR', $wp_content_path ); |
|
182 | + } |
|
183 | + } |
|
184 | 184 | |
185 | - /** |
|
186 | - * adds a 'relative' key to wp_upload_dir() result. |
|
187 | - * It will contain the relative url to upload dir. |
|
188 | - * @return void |
|
189 | - */ |
|
190 | - static function add_filters() { |
|
191 | - add_filter( 'upload_dir', function ( $arr ) { |
|
192 | - $arr['relative'] = str_replace( home_url(), '', $arr['baseurl'] ); |
|
193 | - return $arr; |
|
194 | - } ); |
|
195 | - } |
|
185 | + /** |
|
186 | + * adds a 'relative' key to wp_upload_dir() result. |
|
187 | + * It will contain the relative url to upload dir. |
|
188 | + * @return void |
|
189 | + */ |
|
190 | + static function add_filters() { |
|
191 | + add_filter( 'upload_dir', function ( $arr ) { |
|
192 | + $arr['relative'] = str_replace( home_url(), '', $arr['baseurl'] ); |
|
193 | + return $arr; |
|
194 | + } ); |
|
195 | + } |
|
196 | 196 | |
197 | - //-- end of public methods --// |
|
198 | - /** |
|
199 | - * Deletes the auto-generated files for resize and letterboxing created by Timber |
|
200 | - * @param string $local_file ex: /var/www/wp-content/uploads/2015/my-pic.jpg |
|
201 | - * or: http://example.org/wp-content/uploads/2015/my-pic.jpg |
|
202 | - */ |
|
203 | - static function delete_generated_files( $local_file ) { |
|
204 | - if (TimberURLHelper::is_absolute( $local_file ) ) { |
|
205 | - $local_file = TimberURLHelper::url_to_file_system( $local_file ); |
|
206 | - } |
|
207 | - $info = pathinfo( $local_file ); |
|
208 | - $dir = $info['dirname']; |
|
209 | - $ext = $info['extension']; |
|
210 | - $filename = $info['filename']; |
|
211 | - self::process_delete_generated_files( $filename, $ext, $dir, '-[0-9999999]*', '-[0-9]*x[0-9]*-c-[a-z]*.' ); |
|
212 | - self::process_delete_generated_files( $filename, $ext, $dir, '-lbox-[0-9999999]*', '-lbox-[0-9]*x[0-9]*-[a-zA-Z0-9]*.' ); |
|
213 | - } |
|
197 | + //-- end of public methods --// |
|
198 | + /** |
|
199 | + * Deletes the auto-generated files for resize and letterboxing created by Timber |
|
200 | + * @param string $local_file ex: /var/www/wp-content/uploads/2015/my-pic.jpg |
|
201 | + * or: http://example.org/wp-content/uploads/2015/my-pic.jpg |
|
202 | + */ |
|
203 | + static function delete_generated_files( $local_file ) { |
|
204 | + if (TimberURLHelper::is_absolute( $local_file ) ) { |
|
205 | + $local_file = TimberURLHelper::url_to_file_system( $local_file ); |
|
206 | + } |
|
207 | + $info = pathinfo( $local_file ); |
|
208 | + $dir = $info['dirname']; |
|
209 | + $ext = $info['extension']; |
|
210 | + $filename = $info['filename']; |
|
211 | + self::process_delete_generated_files( $filename, $ext, $dir, '-[0-9999999]*', '-[0-9]*x[0-9]*-c-[a-z]*.' ); |
|
212 | + self::process_delete_generated_files( $filename, $ext, $dir, '-lbox-[0-9999999]*', '-lbox-[0-9]*x[0-9]*-[a-zA-Z0-9]*.' ); |
|
213 | + } |
|
214 | 214 | |
215 | - /** |
|
216 | - * Deletes resized versions of the supplied file name. |
|
217 | - * So if passed a value like my-pic.jpg, this function will delete my-pic-500x200-c-left.jpg, my-pic-400x400-c-default.jpg, etc. |
|
218 | - * |
|
219 | - * keeping these here so I know what the hell we're matching |
|
220 | - * $match = preg_match("/\/srv\/www\/wordpress-develop\/src\/wp-content\/uploads\/2014\/05\/$filename-[0-9]*x[0-9]*-c-[a-z]*.jpg/", $found_file); |
|
221 | - * $match = preg_match("/\/srv\/www\/wordpress-develop\/src\/wp-content\/uploads\/2014\/05\/arch-[0-9]*x[0-9]*-c-[a-z]*.jpg/", $filename); |
|
222 | - * |
|
223 | - * @param string $filename ex: my-pic |
|
224 | - * @param string $ext ex: jpg |
|
225 | - * @param string $dir var/www/wp-content/uploads/2015/ |
|
226 | - * @param string $search_pattern pattern of files to pluck from |
|
227 | - * @param string $match_pattern pattern of files to go forth and delete |
|
228 | - */ |
|
229 | - protected static function process_delete_generated_files( $filename, $ext, $dir, $search_pattern, $match_pattern ) { |
|
230 | - $searcher = '/' . $filename . $search_pattern; |
|
231 | - foreach ( glob( $dir . $searcher ) as $found_file ) { |
|
232 | - $regexdir = str_replace( '/', '\/', $dir ); |
|
233 | - $pattern = '/' . ( $regexdir ) . '\/' . $filename . $match_pattern . $ext . '/'; |
|
234 | - $match = preg_match( $pattern, $found_file ); |
|
235 | - if ( $match ) { |
|
236 | - unlink( $found_file ); |
|
237 | - } |
|
238 | - } |
|
239 | - } |
|
215 | + /** |
|
216 | + * Deletes resized versions of the supplied file name. |
|
217 | + * So if passed a value like my-pic.jpg, this function will delete my-pic-500x200-c-left.jpg, my-pic-400x400-c-default.jpg, etc. |
|
218 | + * |
|
219 | + * keeping these here so I know what the hell we're matching |
|
220 | + * $match = preg_match("/\/srv\/www\/wordpress-develop\/src\/wp-content\/uploads\/2014\/05\/$filename-[0-9]*x[0-9]*-c-[a-z]*.jpg/", $found_file); |
|
221 | + * $match = preg_match("/\/srv\/www\/wordpress-develop\/src\/wp-content\/uploads\/2014\/05\/arch-[0-9]*x[0-9]*-c-[a-z]*.jpg/", $filename); |
|
222 | + * |
|
223 | + * @param string $filename ex: my-pic |
|
224 | + * @param string $ext ex: jpg |
|
225 | + * @param string $dir var/www/wp-content/uploads/2015/ |
|
226 | + * @param string $search_pattern pattern of files to pluck from |
|
227 | + * @param string $match_pattern pattern of files to go forth and delete |
|
228 | + */ |
|
229 | + protected static function process_delete_generated_files( $filename, $ext, $dir, $search_pattern, $match_pattern ) { |
|
230 | + $searcher = '/' . $filename . $search_pattern; |
|
231 | + foreach ( glob( $dir . $searcher ) as $found_file ) { |
|
232 | + $regexdir = str_replace( '/', '\/', $dir ); |
|
233 | + $pattern = '/' . ( $regexdir ) . '\/' . $filename . $match_pattern . $ext . '/'; |
|
234 | + $match = preg_match( $pattern, $found_file ); |
|
235 | + if ( $match ) { |
|
236 | + unlink( $found_file ); |
|
237 | + } |
|
238 | + } |
|
239 | + } |
|
240 | 240 | |
241 | 241 | |
242 | - /** |
|
243 | - * Determines the filepath corresponding to a given URL |
|
244 | - * |
|
245 | - * @param string $url |
|
246 | - * @return string |
|
247 | - */ |
|
248 | - public static function get_server_location( $url ) { |
|
249 | - // if we're already an absolute dir, just return |
|
250 | - if ( 0 === strpos( $url, ABSPATH ) ) { |
|
251 | - return $url; |
|
252 | - } |
|
253 | - // otherwise, analyze URL then build mapping path |
|
254 | - $au = self::analyze_url($url); |
|
255 | - $result = self::_get_file_path($au['base'], $au['subdir'], $au['basename']); |
|
256 | - return $result; |
|
257 | - } |
|
242 | + /** |
|
243 | + * Determines the filepath corresponding to a given URL |
|
244 | + * |
|
245 | + * @param string $url |
|
246 | + * @return string |
|
247 | + */ |
|
248 | + public static function get_server_location( $url ) { |
|
249 | + // if we're already an absolute dir, just return |
|
250 | + if ( 0 === strpos( $url, ABSPATH ) ) { |
|
251 | + return $url; |
|
252 | + } |
|
253 | + // otherwise, analyze URL then build mapping path |
|
254 | + $au = self::analyze_url($url); |
|
255 | + $result = self::_get_file_path($au['base'], $au['subdir'], $au['basename']); |
|
256 | + return $result; |
|
257 | + } |
|
258 | 258 | |
259 | - /** |
|
260 | - * Determines the filepath where a given external file will be stored. |
|
261 | - * |
|
262 | - * @param string $file |
|
263 | - * @return string |
|
264 | - */ |
|
265 | - public static function get_sideloaded_file_loc( $file ) { |
|
266 | - $upload = wp_upload_dir(); |
|
267 | - $dir = $upload['path']; |
|
268 | - $filename = $file; |
|
269 | - $file = parse_url( $file ); |
|
270 | - $path_parts = pathinfo( $file['path'] ); |
|
271 | - $basename = md5( $filename ); |
|
272 | - $ext = 'jpg'; |
|
273 | - if ( isset( $path_parts['extension'] ) ) { |
|
274 | - $ext = $path_parts['extension']; |
|
275 | - } |
|
276 | - return $dir . '/' . $basename . '.' . $ext; |
|
277 | - } |
|
259 | + /** |
|
260 | + * Determines the filepath where a given external file will be stored. |
|
261 | + * |
|
262 | + * @param string $file |
|
263 | + * @return string |
|
264 | + */ |
|
265 | + public static function get_sideloaded_file_loc( $file ) { |
|
266 | + $upload = wp_upload_dir(); |
|
267 | + $dir = $upload['path']; |
|
268 | + $filename = $file; |
|
269 | + $file = parse_url( $file ); |
|
270 | + $path_parts = pathinfo( $file['path'] ); |
|
271 | + $basename = md5( $filename ); |
|
272 | + $ext = 'jpg'; |
|
273 | + if ( isset( $path_parts['extension'] ) ) { |
|
274 | + $ext = $path_parts['extension']; |
|
275 | + } |
|
276 | + return $dir . '/' . $basename . '.' . $ext; |
|
277 | + } |
|
278 | 278 | |
279 | - /** |
|
280 | - * downloads an external image to the server and stores it on the server |
|
281 | - * |
|
282 | - * @param string $file the URL to the original file |
|
283 | - * @return string the URL to the downloaded file |
|
284 | - */ |
|
285 | - public static function sideload_image( $file ) { |
|
286 | - $loc = self::get_sideloaded_file_loc( $file ); |
|
287 | - if ( file_exists( $loc ) ) { |
|
288 | - return TimberURLHelper::preslashit( TimberURLHelper::get_rel_path( $loc ) ); |
|
289 | - } |
|
290 | - // Download file to temp location |
|
291 | - if ( !function_exists( 'download_url' ) ) { |
|
292 | - require_once ABSPATH . '/wp-admin/includes/file.php'; |
|
293 | - } |
|
294 | - $tmp = download_url( $file ); |
|
295 | - preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches ); |
|
296 | - $file_array = array(); |
|
297 | - $file_array['name'] = basename( $matches[0] ); |
|
298 | - $file_array['tmp_name'] = $tmp; |
|
299 | - // If error storing temporarily, unlink |
|
300 | - if ( is_wp_error( $tmp ) ) { |
|
301 | - @unlink( $file_array['tmp_name'] ); |
|
302 | - $file_array['tmp_name'] = ''; |
|
303 | - } |
|
304 | - // do the validation and storage stuff |
|
305 | - $locinfo = pathinfo( $loc ); |
|
306 | - $file = wp_upload_bits( $locinfo['basename'], null, file_get_contents( $file_array['tmp_name'] ) ); |
|
307 | - return $file['url']; |
|
308 | - } |
|
279 | + /** |
|
280 | + * downloads an external image to the server and stores it on the server |
|
281 | + * |
|
282 | + * @param string $file the URL to the original file |
|
283 | + * @return string the URL to the downloaded file |
|
284 | + */ |
|
285 | + public static function sideload_image( $file ) { |
|
286 | + $loc = self::get_sideloaded_file_loc( $file ); |
|
287 | + if ( file_exists( $loc ) ) { |
|
288 | + return TimberURLHelper::preslashit( TimberURLHelper::get_rel_path( $loc ) ); |
|
289 | + } |
|
290 | + // Download file to temp location |
|
291 | + if ( !function_exists( 'download_url' ) ) { |
|
292 | + require_once ABSPATH . '/wp-admin/includes/file.php'; |
|
293 | + } |
|
294 | + $tmp = download_url( $file ); |
|
295 | + preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches ); |
|
296 | + $file_array = array(); |
|
297 | + $file_array['name'] = basename( $matches[0] ); |
|
298 | + $file_array['tmp_name'] = $tmp; |
|
299 | + // If error storing temporarily, unlink |
|
300 | + if ( is_wp_error( $tmp ) ) { |
|
301 | + @unlink( $file_array['tmp_name'] ); |
|
302 | + $file_array['tmp_name'] = ''; |
|
303 | + } |
|
304 | + // do the validation and storage stuff |
|
305 | + $locinfo = pathinfo( $loc ); |
|
306 | + $file = wp_upload_bits( $locinfo['basename'], null, file_get_contents( $file_array['tmp_name'] ) ); |
|
307 | + return $file['url']; |
|
308 | + } |
|
309 | 309 | |
310 | - /** |
|
311 | - * Takes in an URL and breaks it into components, |
|
312 | - * that will then be used in the different steps of image processing. |
|
313 | - * The image is expected to be either part of a theme, plugin, or an upload. |
|
314 | - * |
|
315 | - * @param string $url an URL (absolute or relative) pointing to an image |
|
316 | - * @return array an array (see keys in code below) |
|
317 | - */ |
|
318 | - private static function analyze_url($url) { |
|
319 | - $result = array( |
|
320 | - 'url' => $url, // the initial url |
|
321 | - 'absolute' => TimberURLHelper::is_absolute($url), // is the url absolute or relative (to home_url) |
|
322 | - 'base' => 0, // is the image in uploads dir, or in content dir (theme or plugin) |
|
323 | - 'subdir' => '', // the path between base (uploads or content) and file |
|
324 | - 'filename' => '', // the filename, without extension |
|
325 | - 'extension' => '', // the file extension |
|
326 | - 'basename' => '', // full file name |
|
327 | - ); |
|
328 | - $upload_dir = wp_upload_dir(); |
|
329 | - $tmp = $url; |
|
330 | - if ( 0 === strpos($tmp, ABSPATH) ) { // we've been given a dir, not an url |
|
331 | - $result['absolute'] = true; |
|
332 | - if ( 0 === strpos($tmp, $upload_dir['basedir']) ) { |
|
333 | - $result['base']= self::BASE_UPLOADS; // upload based |
|
334 | - $tmp = str_replace($upload_dir['basedir'], '', $tmp); |
|
335 | - } |
|
336 | - if ( 0 === strpos($tmp, WP_CONTENT_DIR) ) { |
|
337 | - $result['base']= self::BASE_CONTENT; // content based |
|
338 | - $tmp = str_replace(WP_CONTENT_DIR, '', $tmp); |
|
339 | - } |
|
340 | - } else { |
|
341 | - if (!$result['absolute']) { |
|
342 | - $tmp = home_url().$tmp; |
|
343 | - } |
|
344 | - if (0 === strpos($tmp, $upload_dir['baseurl'])) { |
|
345 | - $result['base']= self::BASE_UPLOADS; // upload based |
|
346 | - $tmp = str_replace($upload_dir['baseurl'], '', $tmp); |
|
347 | - } |
|
348 | - if (0 === strpos($tmp, content_url())) { |
|
349 | - $result['base']= self::BASE_CONTENT; // content-based |
|
350 | - $tmp = str_replace(content_url(), '', $tmp); |
|
351 | - } |
|
352 | - } |
|
353 | - $parts = pathinfo($tmp); |
|
354 | - $result['subdir'] = $parts['dirname']; |
|
355 | - $result['filename'] = $parts['filename']; |
|
356 | - $result['extension'] = $parts['extension']; |
|
357 | - $result['basename'] = $parts['basename']; |
|
358 | - // todo filename |
|
359 | - return $result; |
|
360 | - } |
|
310 | + /** |
|
311 | + * Takes in an URL and breaks it into components, |
|
312 | + * that will then be used in the different steps of image processing. |
|
313 | + * The image is expected to be either part of a theme, plugin, or an upload. |
|
314 | + * |
|
315 | + * @param string $url an URL (absolute or relative) pointing to an image |
|
316 | + * @return array an array (see keys in code below) |
|
317 | + */ |
|
318 | + private static function analyze_url($url) { |
|
319 | + $result = array( |
|
320 | + 'url' => $url, // the initial url |
|
321 | + 'absolute' => TimberURLHelper::is_absolute($url), // is the url absolute or relative (to home_url) |
|
322 | + 'base' => 0, // is the image in uploads dir, or in content dir (theme or plugin) |
|
323 | + 'subdir' => '', // the path between base (uploads or content) and file |
|
324 | + 'filename' => '', // the filename, without extension |
|
325 | + 'extension' => '', // the file extension |
|
326 | + 'basename' => '', // full file name |
|
327 | + ); |
|
328 | + $upload_dir = wp_upload_dir(); |
|
329 | + $tmp = $url; |
|
330 | + if ( 0 === strpos($tmp, ABSPATH) ) { // we've been given a dir, not an url |
|
331 | + $result['absolute'] = true; |
|
332 | + if ( 0 === strpos($tmp, $upload_dir['basedir']) ) { |
|
333 | + $result['base']= self::BASE_UPLOADS; // upload based |
|
334 | + $tmp = str_replace($upload_dir['basedir'], '', $tmp); |
|
335 | + } |
|
336 | + if ( 0 === strpos($tmp, WP_CONTENT_DIR) ) { |
|
337 | + $result['base']= self::BASE_CONTENT; // content based |
|
338 | + $tmp = str_replace(WP_CONTENT_DIR, '', $tmp); |
|
339 | + } |
|
340 | + } else { |
|
341 | + if (!$result['absolute']) { |
|
342 | + $tmp = home_url().$tmp; |
|
343 | + } |
|
344 | + if (0 === strpos($tmp, $upload_dir['baseurl'])) { |
|
345 | + $result['base']= self::BASE_UPLOADS; // upload based |
|
346 | + $tmp = str_replace($upload_dir['baseurl'], '', $tmp); |
|
347 | + } |
|
348 | + if (0 === strpos($tmp, content_url())) { |
|
349 | + $result['base']= self::BASE_CONTENT; // content-based |
|
350 | + $tmp = str_replace(content_url(), '', $tmp); |
|
351 | + } |
|
352 | + } |
|
353 | + $parts = pathinfo($tmp); |
|
354 | + $result['subdir'] = $parts['dirname']; |
|
355 | + $result['filename'] = $parts['filename']; |
|
356 | + $result['extension'] = $parts['extension']; |
|
357 | + $result['basename'] = $parts['basename']; |
|
358 | + // todo filename |
|
359 | + return $result; |
|
360 | + } |
|
361 | 361 | |
362 | - /** |
|
363 | - * Builds the public URL of a file based on its different components |
|
364 | - * |
|
365 | - * @param int $base one of self::BASE_UPLOADS, self::BASE_CONTENT to indicate if file is an upload or a content (theme or plugin) |
|
366 | - * @param string $subdir subdirectory in which file is stored, relative to $base root folder |
|
367 | - * @param string $filename file name, including extension (but no path) |
|
368 | - * @param bool $absolute should the returned URL be absolute (include protocol+host), or relative |
|
369 | - * @return string the URL |
|
370 | - */ |
|
371 | - private static function _get_file_url($base, $subdir, $filename, $absolute) { |
|
372 | - $url = ''; |
|
373 | - if( self::BASE_UPLOADS == $base ) { |
|
374 | - $upload_dir = wp_upload_dir(); |
|
375 | - $url = $upload_dir['baseurl']; |
|
376 | - } |
|
377 | - if( self::BASE_CONTENT == $base ) { |
|
378 | - $url = content_url(); |
|
379 | - } |
|
380 | - if(!empty($subdir)) { |
|
381 | - $url .= $subdir; |
|
382 | - } |
|
383 | - $url .= '/'.$filename; |
|
384 | - if(!$absolute) { |
|
385 | - $url = str_replace(home_url(), '', $url); |
|
386 | - } |
|
387 | - // $url = TimberURLHelper::remove_double_slashes( $url); |
|
388 | - return $url; |
|
389 | - } |
|
362 | + /** |
|
363 | + * Builds the public URL of a file based on its different components |
|
364 | + * |
|
365 | + * @param int $base one of self::BASE_UPLOADS, self::BASE_CONTENT to indicate if file is an upload or a content (theme or plugin) |
|
366 | + * @param string $subdir subdirectory in which file is stored, relative to $base root folder |
|
367 | + * @param string $filename file name, including extension (but no path) |
|
368 | + * @param bool $absolute should the returned URL be absolute (include protocol+host), or relative |
|
369 | + * @return string the URL |
|
370 | + */ |
|
371 | + private static function _get_file_url($base, $subdir, $filename, $absolute) { |
|
372 | + $url = ''; |
|
373 | + if( self::BASE_UPLOADS == $base ) { |
|
374 | + $upload_dir = wp_upload_dir(); |
|
375 | + $url = $upload_dir['baseurl']; |
|
376 | + } |
|
377 | + if( self::BASE_CONTENT == $base ) { |
|
378 | + $url = content_url(); |
|
379 | + } |
|
380 | + if(!empty($subdir)) { |
|
381 | + $url .= $subdir; |
|
382 | + } |
|
383 | + $url .= '/'.$filename; |
|
384 | + if(!$absolute) { |
|
385 | + $url = str_replace(home_url(), '', $url); |
|
386 | + } |
|
387 | + // $url = TimberURLHelper::remove_double_slashes( $url); |
|
388 | + return $url; |
|
389 | + } |
|
390 | 390 | |
391 | - /** |
|
392 | - * Builds the absolute file system location of a file based on its different components |
|
393 | - * |
|
394 | - * @param int $base one of self::BASE_UPLOADS, self::BASE_CONTENT to indicate if file is an upload or a content (theme or plugin) |
|
395 | - * @param string $subdir subdirectory in which file is stored, relative to $base root folder |
|
396 | - * @param string $filename file name, including extension (but no path) |
|
397 | - * @return string the file location |
|
398 | - */ |
|
399 | - private static function _get_file_path($base, $subdir, $filename) { |
|
400 | - $path = ''; |
|
401 | - if(self::BASE_UPLOADS == $base) { |
|
402 | - $upload_dir = wp_upload_dir(); |
|
403 | - $path = $upload_dir['basedir']; |
|
404 | - } |
|
405 | - if(self::BASE_CONTENT == $base) { |
|
406 | - $path = WP_CONTENT_DIR; |
|
407 | - } |
|
408 | - if(!empty($subdir)) { |
|
409 | - $path .= $subdir; |
|
410 | - } |
|
411 | - $path .= '/'.$filename; |
|
412 | - return $path; |
|
413 | - } |
|
391 | + /** |
|
392 | + * Builds the absolute file system location of a file based on its different components |
|
393 | + * |
|
394 | + * @param int $base one of self::BASE_UPLOADS, self::BASE_CONTENT to indicate if file is an upload or a content (theme or plugin) |
|
395 | + * @param string $subdir subdirectory in which file is stored, relative to $base root folder |
|
396 | + * @param string $filename file name, including extension (but no path) |
|
397 | + * @return string the file location |
|
398 | + */ |
|
399 | + private static function _get_file_path($base, $subdir, $filename) { |
|
400 | + $path = ''; |
|
401 | + if(self::BASE_UPLOADS == $base) { |
|
402 | + $upload_dir = wp_upload_dir(); |
|
403 | + $path = $upload_dir['basedir']; |
|
404 | + } |
|
405 | + if(self::BASE_CONTENT == $base) { |
|
406 | + $path = WP_CONTENT_DIR; |
|
407 | + } |
|
408 | + if(!empty($subdir)) { |
|
409 | + $path .= $subdir; |
|
410 | + } |
|
411 | + $path .= '/'.$filename; |
|
412 | + return $path; |
|
413 | + } |
|
414 | 414 | |
415 | 415 | |
416 | - /** |
|
417 | - * Main method that applies operation to src image: |
|
418 | - * 1. break down supplied URL into components |
|
419 | - * 2. use components to determine result file and URL |
|
420 | - * 3. check if a result file already exists |
|
421 | - * 4. otherwise, delegate to supplied TimberImageOperation |
|
422 | - * |
|
423 | - * @param string $src an URL (absolute or relative) to an image |
|
424 | - * @param object $op object of class TimberImageOperation |
|
425 | - * @param boolean $force if true, remove any already existing result file and forces file generation |
|
426 | - * @return string URL to the new image - or the source one if error |
|
427 | - * |
|
428 | - */ |
|
429 | - private static function _operate( $src, $op, $force = false ) { |
|
430 | - if ( empty( $src ) ) { |
|
431 | - return ''; |
|
432 | - } |
|
433 | - $external = false; |
|
416 | + /** |
|
417 | + * Main method that applies operation to src image: |
|
418 | + * 1. break down supplied URL into components |
|
419 | + * 2. use components to determine result file and URL |
|
420 | + * 3. check if a result file already exists |
|
421 | + * 4. otherwise, delegate to supplied TimberImageOperation |
|
422 | + * |
|
423 | + * @param string $src an URL (absolute or relative) to an image |
|
424 | + * @param object $op object of class TimberImageOperation |
|
425 | + * @param boolean $force if true, remove any already existing result file and forces file generation |
|
426 | + * @return string URL to the new image - or the source one if error |
|
427 | + * |
|
428 | + */ |
|
429 | + private static function _operate( $src, $op, $force = false ) { |
|
430 | + if ( empty( $src ) ) { |
|
431 | + return ''; |
|
432 | + } |
|
433 | + $external = false; |
|
434 | 434 | |
435 | - // if external image, load it first |
|
436 | - if ( TimberURLHelper::is_external_content( $src ) ) { |
|
437 | - $src = self::sideload_image( $src ); |
|
438 | - $external = true; |
|
439 | - } |
|
440 | - // break down URL into components |
|
441 | - $au = self::analyze_url($src); |
|
442 | - // build URL and filenames |
|
443 | - $new_url = self::_get_file_url( |
|
444 | - $au['base'], |
|
445 | - $au['subdir'], |
|
446 | - $op->filename($au['filename'], $au['extension']), |
|
447 | - $au['absolute'] |
|
448 | - ); |
|
449 | - $new_server_path = self::_get_file_path( |
|
450 | - $au['base'], |
|
451 | - $au['subdir'], |
|
452 | - $op->filename($au['filename'], $au['extension']) |
|
453 | - ); |
|
454 | - $old_server_path = self::_get_file_path( |
|
455 | - $au['base'], |
|
456 | - $au['subdir'], |
|
457 | - $au['basename'] |
|
458 | - ); |
|
459 | - // if already exists... |
|
460 | - if ( file_exists( $new_server_path ) ) { |
|
461 | - if ( $force ) { |
|
462 | - // Force operation - warning: will regenerate the image on every pageload, use for testing purposes only! |
|
463 | - unlink( $new_server_path ); |
|
464 | - } else { |
|
465 | - // return existing file (caching) |
|
466 | - return $new_url; |
|
467 | - } |
|
468 | - } |
|
469 | - // otherwise generate result file |
|
470 | - if($op->run($old_server_path, $new_server_path)) { |
|
471 | - if( get_class( $op ) === 'TimberImageOperationResize' && $external ) { |
|
472 | - $new_url = strtolower( $new_url ); |
|
473 | - } |
|
474 | - return $new_url; |
|
475 | - } else { |
|
476 | - // in case of error, we return source file itself |
|
477 | - return $src; |
|
478 | - } |
|
479 | - } |
|
435 | + // if external image, load it first |
|
436 | + if ( TimberURLHelper::is_external_content( $src ) ) { |
|
437 | + $src = self::sideload_image( $src ); |
|
438 | + $external = true; |
|
439 | + } |
|
440 | + // break down URL into components |
|
441 | + $au = self::analyze_url($src); |
|
442 | + // build URL and filenames |
|
443 | + $new_url = self::_get_file_url( |
|
444 | + $au['base'], |
|
445 | + $au['subdir'], |
|
446 | + $op->filename($au['filename'], $au['extension']), |
|
447 | + $au['absolute'] |
|
448 | + ); |
|
449 | + $new_server_path = self::_get_file_path( |
|
450 | + $au['base'], |
|
451 | + $au['subdir'], |
|
452 | + $op->filename($au['filename'], $au['extension']) |
|
453 | + ); |
|
454 | + $old_server_path = self::_get_file_path( |
|
455 | + $au['base'], |
|
456 | + $au['subdir'], |
|
457 | + $au['basename'] |
|
458 | + ); |
|
459 | + // if already exists... |
|
460 | + if ( file_exists( $new_server_path ) ) { |
|
461 | + if ( $force ) { |
|
462 | + // Force operation - warning: will regenerate the image on every pageload, use for testing purposes only! |
|
463 | + unlink( $new_server_path ); |
|
464 | + } else { |
|
465 | + // return existing file (caching) |
|
466 | + return $new_url; |
|
467 | + } |
|
468 | + } |
|
469 | + // otherwise generate result file |
|
470 | + if($op->run($old_server_path, $new_server_path)) { |
|
471 | + if( get_class( $op ) === 'TimberImageOperationResize' && $external ) { |
|
472 | + $new_url = strtolower( $new_url ); |
|
473 | + } |
|
474 | + return $new_url; |
|
475 | + } else { |
|
476 | + // in case of error, we return source file itself |
|
477 | + return $src; |
|
478 | + } |
|
479 | + } |
|
480 | 480 | |
481 | 481 | |
482 | 482 | // -- the below methods are just used for unit testing the URL generation code |
483 | 483 | // |
484 | - static function get_letterbox_file_url($url, $w, $h, $color) { |
|
485 | - $au = self::analyze_url($url); |
|
486 | - $op = new TimberImageOperationLetterbox($w, $h, $color); |
|
487 | - $new_url = self::_get_file_url( |
|
488 | - $au['base'], |
|
489 | - $au['subdir'], |
|
490 | - $op->filename($au['filename'], $au['extension']), |
|
491 | - $au['absolute'] |
|
492 | - ); |
|
493 | - return $new_url; |
|
494 | - } |
|
495 | - public static function get_letterbox_file_path($url, $w, $h, $color ) { |
|
496 | - $au = self::analyze_url($url); |
|
497 | - $op = new TimberImageOperationLetterbox($w, $h, $color); |
|
498 | - $new_path = self::_get_file_path( |
|
499 | - $au['base'], |
|
500 | - $au['subdir'], |
|
501 | - $op->filename($au['filename'], $au['extension']) |
|
502 | - ); |
|
503 | - return $new_path; |
|
504 | - } |
|
505 | - static function get_resize_file_url($url, $w, $h, $crop) { |
|
506 | - $au = self::analyze_url($url); |
|
507 | - $op = new TimberImageOperationResize($w, $h, $crop); |
|
508 | - $new_url = self::_get_file_url( |
|
509 | - $au['base'], |
|
510 | - $au['subdir'], |
|
511 | - $op->filename($au['filename'], $au['extension']), |
|
512 | - $au['absolute'] |
|
513 | - ); |
|
514 | - return $new_url; |
|
515 | - } |
|
516 | - static function get_resize_file_path($url, $w, $h, $crop) { |
|
517 | - $au = self::analyze_url($url); |
|
518 | - $op = new TimberImageOperationResize($w, $h, $crop); |
|
519 | - $new_path = self::_get_file_path( |
|
520 | - $au['base'], |
|
521 | - $au['subdir'], |
|
522 | - $op->filename($au['filename'], $au['extension']) |
|
523 | - ); |
|
524 | - return $new_path; |
|
525 | - } |
|
484 | + static function get_letterbox_file_url($url, $w, $h, $color) { |
|
485 | + $au = self::analyze_url($url); |
|
486 | + $op = new TimberImageOperationLetterbox($w, $h, $color); |
|
487 | + $new_url = self::_get_file_url( |
|
488 | + $au['base'], |
|
489 | + $au['subdir'], |
|
490 | + $op->filename($au['filename'], $au['extension']), |
|
491 | + $au['absolute'] |
|
492 | + ); |
|
493 | + return $new_url; |
|
494 | + } |
|
495 | + public static function get_letterbox_file_path($url, $w, $h, $color ) { |
|
496 | + $au = self::analyze_url($url); |
|
497 | + $op = new TimberImageOperationLetterbox($w, $h, $color); |
|
498 | + $new_path = self::_get_file_path( |
|
499 | + $au['base'], |
|
500 | + $au['subdir'], |
|
501 | + $op->filename($au['filename'], $au['extension']) |
|
502 | + ); |
|
503 | + return $new_path; |
|
504 | + } |
|
505 | + static function get_resize_file_url($url, $w, $h, $crop) { |
|
506 | + $au = self::analyze_url($url); |
|
507 | + $op = new TimberImageOperationResize($w, $h, $crop); |
|
508 | + $new_url = self::_get_file_url( |
|
509 | + $au['base'], |
|
510 | + $au['subdir'], |
|
511 | + $op->filename($au['filename'], $au['extension']), |
|
512 | + $au['absolute'] |
|
513 | + ); |
|
514 | + return $new_url; |
|
515 | + } |
|
516 | + static function get_resize_file_path($url, $w, $h, $crop) { |
|
517 | + $au = self::analyze_url($url); |
|
518 | + $op = new TimberImageOperationResize($w, $h, $crop); |
|
519 | + $new_path = self::_get_file_path( |
|
520 | + $au['base'], |
|
521 | + $au['subdir'], |
|
522 | + $op->filename($au['filename'], $au['extension']) |
|
523 | + ); |
|
524 | + return $new_path; |
|
525 | + } |
|
526 | 526 | |
527 | 527 | |
528 | 528 | } |
@@ -72,8 +72,8 @@ discard block |
||
72 | 72 | $w = $_wp_additional_image_sizes[$size]['width']; |
73 | 73 | $h = $_wp_additional_image_sizes[$size]['height']; |
74 | 74 | } else if ( in_array($size, array('thumbnail', 'medium', 'large')) ) { |
75 | - $w = get_option($size.'_size_w'); |
|
76 | - $h = get_option($size.'_size_h'); |
|
75 | + $w = get_option($size . '_size_w'); |
|
76 | + $h = get_option($size . '_size_h'); |
|
77 | 77 | } |
78 | 78 | if ( isset($w) && isset($h) && ($w || $h) ) { |
79 | 79 | return array('w' => $w, 'h' => $h); |
@@ -106,7 +106,7 @@ discard block |
||
106 | 106 | return false; |
107 | 107 | } |
108 | 108 | //its a gif so test |
109 | - if( !($fh = @fopen($file, 'rb')) ) { |
|
109 | + if ( !($fh = @fopen($file, 'rb')) ) { |
|
110 | 110 | return false; |
111 | 111 | } |
112 | 112 | $count = 0; |
@@ -118,7 +118,7 @@ discard block |
||
118 | 118 | |
119 | 119 | // We read through the file til we reach the end of the file, or we've found |
120 | 120 | // at least 2 frame headers |
121 | - while(!feof($fh) && $count < 2) { |
|
121 | + while ( !feof($fh) && $count < 2 ) { |
|
122 | 122 | $chunk = fread($fh, 1024 * 100); //read 100kb at a time |
123 | 123 | $count += preg_match_all('#\x00\x21\xF9\x04.{4}\x00[\x2C\x21]#s', $chunk, $matches); |
124 | 124 | } |
@@ -159,13 +159,13 @@ discard block |
||
159 | 159 | * Deletes all resized versions of an image when the source is deleted |
160 | 160 | */ |
161 | 161 | protected static function add_actions() { |
162 | - add_action( 'delete_attachment', function ( $post_id ) { |
|
163 | - $post = get_post( $post_id ); |
|
164 | - $image_types = array( 'image/jpeg', 'image/png', 'image/gif', 'image/jpg' ); |
|
165 | - if ( in_array( $post->post_mime_type, $image_types ) ) { |
|
166 | - $attachment = new TimberImage( $post_id ); |
|
162 | + add_action('delete_attachment', function( $post_id ) { |
|
163 | + $post = get_post($post_id); |
|
164 | + $image_types = array('image/jpeg', 'image/png', 'image/gif', 'image/jpg'); |
|
165 | + if ( in_array($post->post_mime_type, $image_types) ) { |
|
166 | + $attachment = new TimberImage($post_id); |
|
167 | 167 | if ( $attachment->file_loc ) { |
168 | - TimberImageHelper::delete_generated_files( $attachment->file_loc ); |
|
168 | + TimberImageHelper::delete_generated_files($attachment->file_loc); |
|
169 | 169 | } |
170 | 170 | } |
171 | 171 | } ); |
@@ -176,9 +176,9 @@ discard block |
||
176 | 176 | * for example /wp-content or /content |
177 | 177 | */ |
178 | 178 | protected static function add_constants() { |
179 | - if ( !defined( 'WP_CONTENT_SUBDIR' ) ) { |
|
180 | - $wp_content_path = str_replace( home_url(), '', WP_CONTENT_URL ); |
|
181 | - define( 'WP_CONTENT_SUBDIR', $wp_content_path ); |
|
179 | + if ( !defined('WP_CONTENT_SUBDIR') ) { |
|
180 | + $wp_content_path = str_replace(home_url(), '', WP_CONTENT_URL); |
|
181 | + define('WP_CONTENT_SUBDIR', $wp_content_path); |
|
182 | 182 | } |
183 | 183 | } |
184 | 184 | |
@@ -188,8 +188,8 @@ discard block |
||
188 | 188 | * @return void |
189 | 189 | */ |
190 | 190 | static function add_filters() { |
191 | - add_filter( 'upload_dir', function ( $arr ) { |
|
192 | - $arr['relative'] = str_replace( home_url(), '', $arr['baseurl'] ); |
|
191 | + add_filter('upload_dir', function( $arr ) { |
|
192 | + $arr['relative'] = str_replace(home_url(), '', $arr['baseurl']); |
|
193 | 193 | return $arr; |
194 | 194 | } ); |
195 | 195 | } |
@@ -201,15 +201,15 @@ discard block |
||
201 | 201 | * or: http://example.org/wp-content/uploads/2015/my-pic.jpg |
202 | 202 | */ |
203 | 203 | static function delete_generated_files( $local_file ) { |
204 | - if (TimberURLHelper::is_absolute( $local_file ) ) { |
|
205 | - $local_file = TimberURLHelper::url_to_file_system( $local_file ); |
|
204 | + if ( TimberURLHelper::is_absolute($local_file) ) { |
|
205 | + $local_file = TimberURLHelper::url_to_file_system($local_file); |
|
206 | 206 | } |
207 | - $info = pathinfo( $local_file ); |
|
207 | + $info = pathinfo($local_file); |
|
208 | 208 | $dir = $info['dirname']; |
209 | 209 | $ext = $info['extension']; |
210 | 210 | $filename = $info['filename']; |
211 | - self::process_delete_generated_files( $filename, $ext, $dir, '-[0-9999999]*', '-[0-9]*x[0-9]*-c-[a-z]*.' ); |
|
212 | - self::process_delete_generated_files( $filename, $ext, $dir, '-lbox-[0-9999999]*', '-lbox-[0-9]*x[0-9]*-[a-zA-Z0-9]*.' ); |
|
211 | + self::process_delete_generated_files($filename, $ext, $dir, '-[0-9999999]*', '-[0-9]*x[0-9]*-c-[a-z]*.'); |
|
212 | + self::process_delete_generated_files($filename, $ext, $dir, '-lbox-[0-9999999]*', '-lbox-[0-9]*x[0-9]*-[a-zA-Z0-9]*.'); |
|
213 | 213 | } |
214 | 214 | |
215 | 215 | /** |
@@ -228,12 +228,12 @@ discard block |
||
228 | 228 | */ |
229 | 229 | protected static function process_delete_generated_files( $filename, $ext, $dir, $search_pattern, $match_pattern ) { |
230 | 230 | $searcher = '/' . $filename . $search_pattern; |
231 | - foreach ( glob( $dir . $searcher ) as $found_file ) { |
|
232 | - $regexdir = str_replace( '/', '\/', $dir ); |
|
233 | - $pattern = '/' . ( $regexdir ) . '\/' . $filename . $match_pattern . $ext . '/'; |
|
234 | - $match = preg_match( $pattern, $found_file ); |
|
231 | + foreach ( glob($dir . $searcher) as $found_file ) { |
|
232 | + $regexdir = str_replace('/', '\/', $dir); |
|
233 | + $pattern = '/' . ($regexdir) . '\/' . $filename . $match_pattern . $ext . '/'; |
|
234 | + $match = preg_match($pattern, $found_file); |
|
235 | 235 | if ( $match ) { |
236 | - unlink( $found_file ); |
|
236 | + unlink($found_file); |
|
237 | 237 | } |
238 | 238 | } |
239 | 239 | } |
@@ -247,7 +247,7 @@ discard block |
||
247 | 247 | */ |
248 | 248 | public static function get_server_location( $url ) { |
249 | 249 | // if we're already an absolute dir, just return |
250 | - if ( 0 === strpos( $url, ABSPATH ) ) { |
|
250 | + if ( 0 === strpos($url, ABSPATH) ) { |
|
251 | 251 | return $url; |
252 | 252 | } |
253 | 253 | // otherwise, analyze URL then build mapping path |
@@ -266,11 +266,11 @@ discard block |
||
266 | 266 | $upload = wp_upload_dir(); |
267 | 267 | $dir = $upload['path']; |
268 | 268 | $filename = $file; |
269 | - $file = parse_url( $file ); |
|
270 | - $path_parts = pathinfo( $file['path'] ); |
|
271 | - $basename = md5( $filename ); |
|
269 | + $file = parse_url($file); |
|
270 | + $path_parts = pathinfo($file['path']); |
|
271 | + $basename = md5($filename); |
|
272 | 272 | $ext = 'jpg'; |
273 | - if ( isset( $path_parts['extension'] ) ) { |
|
273 | + if ( isset($path_parts['extension']) ) { |
|
274 | 274 | $ext = $path_parts['extension']; |
275 | 275 | } |
276 | 276 | return $dir . '/' . $basename . '.' . $ext; |
@@ -283,27 +283,27 @@ discard block |
||
283 | 283 | * @return string the URL to the downloaded file |
284 | 284 | */ |
285 | 285 | public static function sideload_image( $file ) { |
286 | - $loc = self::get_sideloaded_file_loc( $file ); |
|
287 | - if ( file_exists( $loc ) ) { |
|
288 | - return TimberURLHelper::preslashit( TimberURLHelper::get_rel_path( $loc ) ); |
|
286 | + $loc = self::get_sideloaded_file_loc($file); |
|
287 | + if ( file_exists($loc) ) { |
|
288 | + return TimberURLHelper::preslashit(TimberURLHelper::get_rel_path($loc)); |
|
289 | 289 | } |
290 | 290 | // Download file to temp location |
291 | - if ( !function_exists( 'download_url' ) ) { |
|
291 | + if ( !function_exists('download_url') ) { |
|
292 | 292 | require_once ABSPATH . '/wp-admin/includes/file.php'; |
293 | 293 | } |
294 | - $tmp = download_url( $file ); |
|
295 | - preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches ); |
|
294 | + $tmp = download_url($file); |
|
295 | + preg_match('/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches); |
|
296 | 296 | $file_array = array(); |
297 | - $file_array['name'] = basename( $matches[0] ); |
|
297 | + $file_array['name'] = basename($matches[0]); |
|
298 | 298 | $file_array['tmp_name'] = $tmp; |
299 | 299 | // If error storing temporarily, unlink |
300 | - if ( is_wp_error( $tmp ) ) { |
|
301 | - @unlink( $file_array['tmp_name'] ); |
|
300 | + if ( is_wp_error($tmp) ) { |
|
301 | + @unlink($file_array['tmp_name']); |
|
302 | 302 | $file_array['tmp_name'] = ''; |
303 | 303 | } |
304 | 304 | // do the validation and storage stuff |
305 | - $locinfo = pathinfo( $loc ); |
|
306 | - $file = wp_upload_bits( $locinfo['basename'], null, file_get_contents( $file_array['tmp_name'] ) ); |
|
305 | + $locinfo = pathinfo($loc); |
|
306 | + $file = wp_upload_bits($locinfo['basename'], null, file_get_contents($file_array['tmp_name'])); |
|
307 | 307 | return $file['url']; |
308 | 308 | } |
309 | 309 | |
@@ -315,7 +315,7 @@ discard block |
||
315 | 315 | * @param string $url an URL (absolute or relative) pointing to an image |
316 | 316 | * @return array an array (see keys in code below) |
317 | 317 | */ |
318 | - private static function analyze_url($url) { |
|
318 | + private static function analyze_url( $url ) { |
|
319 | 319 | $result = array( |
320 | 320 | 'url' => $url, // the initial url |
321 | 321 | 'absolute' => TimberURLHelper::is_absolute($url), // is the url absolute or relative (to home_url) |
@@ -330,23 +330,23 @@ discard block |
||
330 | 330 | if ( 0 === strpos($tmp, ABSPATH) ) { // we've been given a dir, not an url |
331 | 331 | $result['absolute'] = true; |
332 | 332 | if ( 0 === strpos($tmp, $upload_dir['basedir']) ) { |
333 | - $result['base']= self::BASE_UPLOADS; // upload based |
|
333 | + $result['base'] = self::BASE_UPLOADS; // upload based |
|
334 | 334 | $tmp = str_replace($upload_dir['basedir'], '', $tmp); |
335 | 335 | } |
336 | 336 | if ( 0 === strpos($tmp, WP_CONTENT_DIR) ) { |
337 | - $result['base']= self::BASE_CONTENT; // content based |
|
337 | + $result['base'] = self::BASE_CONTENT; // content based |
|
338 | 338 | $tmp = str_replace(WP_CONTENT_DIR, '', $tmp); |
339 | 339 | } |
340 | 340 | } else { |
341 | - if (!$result['absolute']) { |
|
342 | - $tmp = home_url().$tmp; |
|
341 | + if ( !$result['absolute'] ) { |
|
342 | + $tmp = home_url() . $tmp; |
|
343 | 343 | } |
344 | - if (0 === strpos($tmp, $upload_dir['baseurl'])) { |
|
345 | - $result['base']= self::BASE_UPLOADS; // upload based |
|
344 | + if ( 0 === strpos($tmp, $upload_dir['baseurl']) ) { |
|
345 | + $result['base'] = self::BASE_UPLOADS; // upload based |
|
346 | 346 | $tmp = str_replace($upload_dir['baseurl'], '', $tmp); |
347 | 347 | } |
348 | - if (0 === strpos($tmp, content_url())) { |
|
349 | - $result['base']= self::BASE_CONTENT; // content-based |
|
348 | + if ( 0 === strpos($tmp, content_url()) ) { |
|
349 | + $result['base'] = self::BASE_CONTENT; // content-based |
|
350 | 350 | $tmp = str_replace(content_url(), '', $tmp); |
351 | 351 | } |
352 | 352 | } |
@@ -368,20 +368,20 @@ discard block |
||
368 | 368 | * @param bool $absolute should the returned URL be absolute (include protocol+host), or relative |
369 | 369 | * @return string the URL |
370 | 370 | */ |
371 | - private static function _get_file_url($base, $subdir, $filename, $absolute) { |
|
371 | + private static function _get_file_url( $base, $subdir, $filename, $absolute ) { |
|
372 | 372 | $url = ''; |
373 | - if( self::BASE_UPLOADS == $base ) { |
|
373 | + if ( self::BASE_UPLOADS == $base ) { |
|
374 | 374 | $upload_dir = wp_upload_dir(); |
375 | 375 | $url = $upload_dir['baseurl']; |
376 | 376 | } |
377 | - if( self::BASE_CONTENT == $base ) { |
|
377 | + if ( self::BASE_CONTENT == $base ) { |
|
378 | 378 | $url = content_url(); |
379 | 379 | } |
380 | - if(!empty($subdir)) { |
|
380 | + if ( !empty($subdir) ) { |
|
381 | 381 | $url .= $subdir; |
382 | 382 | } |
383 | - $url .= '/'.$filename; |
|
384 | - if(!$absolute) { |
|
383 | + $url .= '/' . $filename; |
|
384 | + if ( !$absolute ) { |
|
385 | 385 | $url = str_replace(home_url(), '', $url); |
386 | 386 | } |
387 | 387 | // $url = TimberURLHelper::remove_double_slashes( $url); |
@@ -396,19 +396,19 @@ discard block |
||
396 | 396 | * @param string $filename file name, including extension (but no path) |
397 | 397 | * @return string the file location |
398 | 398 | */ |
399 | - private static function _get_file_path($base, $subdir, $filename) { |
|
399 | + private static function _get_file_path( $base, $subdir, $filename ) { |
|
400 | 400 | $path = ''; |
401 | - if(self::BASE_UPLOADS == $base) { |
|
401 | + if ( self::BASE_UPLOADS == $base ) { |
|
402 | 402 | $upload_dir = wp_upload_dir(); |
403 | 403 | $path = $upload_dir['basedir']; |
404 | 404 | } |
405 | - if(self::BASE_CONTENT == $base) { |
|
405 | + if ( self::BASE_CONTENT == $base ) { |
|
406 | 406 | $path = WP_CONTENT_DIR; |
407 | 407 | } |
408 | - if(!empty($subdir)) { |
|
408 | + if ( !empty($subdir) ) { |
|
409 | 409 | $path .= $subdir; |
410 | 410 | } |
411 | - $path .= '/'.$filename; |
|
411 | + $path .= '/' . $filename; |
|
412 | 412 | return $path; |
413 | 413 | } |
414 | 414 | |
@@ -427,14 +427,14 @@ discard block |
||
427 | 427 | * |
428 | 428 | */ |
429 | 429 | private static function _operate( $src, $op, $force = false ) { |
430 | - if ( empty( $src ) ) { |
|
430 | + if ( empty($src) ) { |
|
431 | 431 | return ''; |
432 | 432 | } |
433 | 433 | $external = false; |
434 | 434 | |
435 | 435 | // if external image, load it first |
436 | - if ( TimberURLHelper::is_external_content( $src ) ) { |
|
437 | - $src = self::sideload_image( $src ); |
|
436 | + if ( TimberURLHelper::is_external_content($src) ) { |
|
437 | + $src = self::sideload_image($src); |
|
438 | 438 | $external = true; |
439 | 439 | } |
440 | 440 | // break down URL into components |
@@ -457,19 +457,19 @@ discard block |
||
457 | 457 | $au['basename'] |
458 | 458 | ); |
459 | 459 | // if already exists... |
460 | - if ( file_exists( $new_server_path ) ) { |
|
460 | + if ( file_exists($new_server_path) ) { |
|
461 | 461 | if ( $force ) { |
462 | 462 | // Force operation - warning: will regenerate the image on every pageload, use for testing purposes only! |
463 | - unlink( $new_server_path ); |
|
463 | + unlink($new_server_path); |
|
464 | 464 | } else { |
465 | 465 | // return existing file (caching) |
466 | 466 | return $new_url; |
467 | 467 | } |
468 | 468 | } |
469 | 469 | // otherwise generate result file |
470 | - if($op->run($old_server_path, $new_server_path)) { |
|
471 | - if( get_class( $op ) === 'TimberImageOperationResize' && $external ) { |
|
472 | - $new_url = strtolower( $new_url ); |
|
470 | + if ( $op->run($old_server_path, $new_server_path) ) { |
|
471 | + if ( get_class($op) === 'TimberImageOperationResize' && $external ) { |
|
472 | + $new_url = strtolower($new_url); |
|
473 | 473 | } |
474 | 474 | return $new_url; |
475 | 475 | } else { |
@@ -481,7 +481,7 @@ discard block |
||
481 | 481 | |
482 | 482 | // -- the below methods are just used for unit testing the URL generation code |
483 | 483 | // |
484 | - static function get_letterbox_file_url($url, $w, $h, $color) { |
|
484 | + static function get_letterbox_file_url( $url, $w, $h, $color ) { |
|
485 | 485 | $au = self::analyze_url($url); |
486 | 486 | $op = new TimberImageOperationLetterbox($w, $h, $color); |
487 | 487 | $new_url = self::_get_file_url( |
@@ -492,7 +492,7 @@ discard block |
||
492 | 492 | ); |
493 | 493 | return $new_url; |
494 | 494 | } |
495 | - public static function get_letterbox_file_path($url, $w, $h, $color ) { |
|
495 | + public static function get_letterbox_file_path( $url, $w, $h, $color ) { |
|
496 | 496 | $au = self::analyze_url($url); |
497 | 497 | $op = new TimberImageOperationLetterbox($w, $h, $color); |
498 | 498 | $new_path = self::_get_file_path( |
@@ -502,7 +502,7 @@ discard block |
||
502 | 502 | ); |
503 | 503 | return $new_path; |
504 | 504 | } |
505 | - static function get_resize_file_url($url, $w, $h, $crop) { |
|
505 | + static function get_resize_file_url( $url, $w, $h, $crop ) { |
|
506 | 506 | $au = self::analyze_url($url); |
507 | 507 | $op = new TimberImageOperationResize($w, $h, $crop); |
508 | 508 | $new_url = self::_get_file_url( |
@@ -513,7 +513,7 @@ discard block |
||
513 | 513 | ); |
514 | 514 | return $new_url; |
515 | 515 | } |
516 | - static function get_resize_file_path($url, $w, $h, $crop) { |
|
516 | + static function get_resize_file_path( $url, $w, $h, $crop ) { |
|
517 | 517 | $au = self::analyze_url($url); |
518 | 518 | $op = new TimberImageOperationResize($w, $h, $crop); |
519 | 519 | $new_path = self::_get_file_path( |
@@ -301,7 +301,7 @@ |
||
301 | 301 | * <img src="{{ post.thumbnail.src|resize(500) }}" alt="A sumo wrestler" /> |
302 | 302 | * {% endif %} |
303 | 303 | * ``` |
304 | - * @return float |
|
304 | + * @return integer |
|
305 | 305 | */ |
306 | 306 | public function aspect() { |
307 | 307 | $w = intval($this->width()); |
@@ -39,488 +39,488 @@ |
||
39 | 39 | */ |
40 | 40 | class TimberImage extends TimberPost implements TimberCoreInterface { |
41 | 41 | |
42 | - protected $_can_edit; |
|
43 | - protected $_dimensions; |
|
44 | - public $abs_url; |
|
45 | - /** |
|
46 | - * @var string $object_type what does this class represent in WordPress terms? |
|
47 | - */ |
|
48 | - public $object_type = 'image'; |
|
49 | - /** |
|
50 | - * @var string $representation what does this class represent in WordPress terms? |
|
51 | - */ |
|
52 | - public static $representation = 'image'; |
|
53 | - /** |
|
54 | - * @var array of supported relative file types |
|
55 | - */ |
|
56 | - private $file_types = array('jpg', 'jpeg', 'png', 'svg', 'bmp', 'ico', 'gif', 'tiff', 'pdf'); |
|
57 | - /** |
|
58 | - * @api |
|
59 | - * @var string $file_loc the location of the image file in the filesystem (ex: `/var/www/htdocs/wp-content/uploads/2015/08/my-pic.jpg`) |
|
60 | - */ |
|
61 | - public $file_loc; |
|
62 | - public $file; |
|
63 | - /** |
|
64 | - * @api |
|
65 | - * @var integer the ID of the image (which is a WP_Post) |
|
66 | - */ |
|
67 | - public $id; |
|
68 | - public $sizes = array(); |
|
69 | - /** |
|
70 | - * @api |
|
71 | - * @var string $caption the string stored in the WordPress database |
|
72 | - */ |
|
73 | - public $caption; |
|
74 | - /** |
|
75 | - * @var $_wp_attached_file the file as stored in the WordPress database |
|
76 | - */ |
|
77 | - protected $_wp_attached_file; |
|
78 | - |
|
79 | - /** |
|
80 | - * Creates a new TimberImage object |
|
81 | - * @example |
|
82 | - * ```php |
|
83 | - * // You can pass it an ID number |
|
84 | - * $myImage = new TimberImage(552); |
|
85 | - * |
|
86 | - * //Or send it a URL to an image |
|
87 | - * $myImage = new TimberImage('http://google.com/logo.jpg'); |
|
88 | - * ``` |
|
89 | - * @param int|string $iid |
|
90 | - */ |
|
91 | - public function __construct($iid) { |
|
92 | - $this->init($iid); |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * @return string the src of the file |
|
97 | - */ |
|
98 | - public function __toString() { |
|
99 | - if ( $this->get_src() ) { |
|
100 | - return $this->get_src(); |
|
101 | - } |
|
102 | - return ''; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * Get a PHP array with pathinfo() info from the file |
|
107 | - * @return array |
|
108 | - */ |
|
109 | - function get_pathinfo() { |
|
110 | - return pathinfo($this->file); |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * @internal |
|
115 | - * @param string $dim |
|
116 | - * @return array|int |
|
117 | - */ |
|
118 | - protected function get_dimensions($dim = null) { |
|
119 | - if ( isset($this->_dimensions) ) { |
|
120 | - return $this->get_dimensions_loaded($dim); |
|
121 | - } |
|
122 | - if ( file_exists($this->file_loc) && filesize($this->file_loc) ) { |
|
123 | - list($width, $height) = getimagesize($this->file_loc); |
|
124 | - $this->_dimensions = array(); |
|
125 | - $this->_dimensions[0] = $width; |
|
126 | - $this->_dimensions[1] = $height; |
|
127 | - return $this->get_dimensions_loaded($dim); |
|
128 | - } |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * @internal |
|
133 | - * @param string|null $dim |
|
134 | - * @return array|int |
|
135 | - */ |
|
136 | - protected function get_dimensions_loaded($dim) { |
|
137 | - if ( $dim === null ) { |
|
138 | - return $this->_dimensions; |
|
139 | - } |
|
140 | - if ( $dim == 'w' || $dim == 'width' ) { |
|
141 | - return $this->_dimensions[0]; |
|
142 | - } |
|
143 | - if ( $dim == 'h' || $dim == 'height' ) { |
|
144 | - return $this->_dimensions[1]; |
|
145 | - } |
|
146 | - return null; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * @internal |
|
151 | - * @param int $iid the id number of the image in the WP database |
|
152 | - */ |
|
153 | - protected function get_image_info( $iid ) { |
|
154 | - $image_info = $iid; |
|
155 | - if (is_numeric($iid)) { |
|
156 | - $image_info = wp_get_attachment_metadata($iid); |
|
157 | - if (!is_array($image_info)) { |
|
158 | - $image_info = array(); |
|
159 | - } |
|
160 | - $image_custom = get_post_custom($iid); |
|
161 | - $basic = get_post($iid); |
|
162 | - if ($basic) { |
|
163 | - if (isset($basic->post_excerpt)) { |
|
164 | - $this->caption = $basic->post_excerpt; |
|
165 | - } |
|
166 | - $image_custom = array_merge($image_custom, get_object_vars($basic)); |
|
167 | - } |
|
168 | - return array_merge($image_info, $image_custom); |
|
169 | - } |
|
170 | - if (is_array($image_info) && isset($image_info['image'])) { |
|
171 | - return $image_info['image']; |
|
172 | - } |
|
173 | - if (is_object($image_info)) { |
|
174 | - return get_object_vars($image_info); |
|
175 | - } |
|
176 | - return $iid; |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * @internal |
|
181 | - * @param string $url for evaluation |
|
182 | - * @return string with http/https corrected depending on what's appropriate for server |
|
183 | - */ |
|
184 | - protected static function _maybe_secure_url($url) { |
|
185 | - if ( is_ssl() && strpos($url, 'https') !== 0 && strpos($url, 'http') === 0 ) { |
|
186 | - $url = 'https' . substr($url, strlen('http')); |
|
187 | - } |
|
188 | - return $url; |
|
189 | - } |
|
190 | - |
|
191 | - public static function wp_upload_dir() { |
|
192 | - static $wp_upload_dir = false; |
|
193 | - |
|
194 | - if ( !$wp_upload_dir ) { |
|
195 | - $wp_upload_dir = wp_upload_dir(); |
|
196 | - } |
|
197 | - |
|
198 | - return $wp_upload_dir; |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * @internal |
|
203 | - * @param int $iid |
|
204 | - */ |
|
205 | - function init( $iid = false ) { |
|
206 | - if ( !is_numeric( $iid ) && is_string( $iid ) ) { |
|
207 | - if (strstr($iid, '://')) { |
|
208 | - $this->init_with_url($iid); |
|
209 | - return; |
|
210 | - } |
|
211 | - if ( strstr($iid, ABSPATH) ) { |
|
212 | - $this->init_with_file_path($iid); |
|
213 | - return; |
|
214 | - } |
|
215 | - |
|
216 | - $relative = false; |
|
217 | - $iid_lower = strtolower($iid); |
|
218 | - foreach( $this->file_types as $type ) { if( strstr( $iid_lower, $type ) ) { $relative = true; break; } }; |
|
219 | - if ( $relative ) { |
|
220 | - $this->init_with_relative_path( $iid ); |
|
221 | - return; |
|
222 | - } |
|
223 | - } else if ( $iid instanceof WP_Post ) { |
|
224 | - $ref = new ReflectionClass($this); |
|
225 | - $post = $ref->getParentClass()->newInstance($iid->ID); |
|
226 | - if (isset($post->_thumbnail_id) && $post->_thumbnail_id) { |
|
227 | - return $this->init((int) $post->_thumbnail_id); |
|
228 | - } |
|
229 | - return $this->init($iid->ID); |
|
230 | - } else if ( $iid instanceof TimberPost ) { |
|
231 | - /** |
|
232 | - * This will catch TimberPost and any post classes that extend TimberPost, |
|
233 | - * see http://php.net/manual/en/internals2.opcodes.instanceof.php#109108 |
|
234 | - * and https://github.com/jarednova/timber/wiki/Extending-Timber |
|
235 | - */ |
|
236 | - $iid = (int) $iid->_thumbnail_id; |
|
237 | - } |
|
238 | - |
|
239 | - $image_info = $this->get_image_info($iid); |
|
240 | - |
|
241 | - $this->import($image_info); |
|
242 | - $basedir = self::wp_upload_dir(); |
|
243 | - $basedir = $basedir['basedir']; |
|
244 | - if ( isset($this->file) ) { |
|
245 | - $this->file_loc = $basedir . DIRECTORY_SEPARATOR . $this->file; |
|
246 | - } else if ( isset($this->_wp_attached_file) ) { |
|
247 | - $this->file = reset($this->_wp_attached_file); |
|
248 | - $this->file_loc = $basedir . DIRECTORY_SEPARATOR . $this->file; |
|
249 | - } |
|
250 | - if ( isset($image_info['id']) ) { |
|
251 | - $this->ID = $image_info['id']; |
|
252 | - } else if ( is_numeric($iid) ) { |
|
253 | - $this->ID = $iid; |
|
254 | - } |
|
255 | - if ( isset($this->ID) ) { |
|
256 | - $custom = get_post_custom($this->ID); |
|
257 | - foreach ($custom as $key => $value) { |
|
258 | - $this->$key = $value[0]; |
|
259 | - } |
|
260 | - $this->id = $this->ID; |
|
261 | - } else { |
|
262 | - if ( is_array($iid) || is_object($iid) ) { |
|
263 | - TimberHelper::error_log('Not able to init in TimberImage with iid='); |
|
264 | - TimberHelper::error_log($iid); |
|
265 | - } else { |
|
266 | - TimberHelper::error_log('Not able to init in TimberImage with iid=' . $iid); |
|
267 | - } |
|
268 | - } |
|
269 | - } |
|
270 | - |
|
271 | - /** |
|
272 | - * @internal |
|
273 | - * @param string $relative_path |
|
274 | - */ |
|
275 | - protected function init_with_relative_path( $relative_path ) { |
|
276 | - $this->abs_url = home_url( $relative_path ); |
|
277 | - $file_path = TimberURLHelper::get_full_path( $relative_path ); |
|
278 | - $this->file_loc = $file_path; |
|
279 | - $this->file = $file_path; |
|
280 | - } |
|
281 | - |
|
282 | - /** |
|
283 | - * @internal |
|
284 | - * @param string $file_path |
|
285 | - */ |
|
286 | - protected function init_with_file_path( $file_path ) { |
|
287 | - $url = TimberURLHelper::file_system_to_url( $file_path ); |
|
288 | - $this->abs_url = $url; |
|
289 | - $this->file_loc = $file_path; |
|
290 | - $this->file = $file_path; |
|
291 | - } |
|
292 | - |
|
293 | - /** |
|
294 | - * @internal |
|
295 | - * @param string $url |
|
296 | - */ |
|
297 | - protected function init_with_url($url) { |
|
298 | - $this->abs_url = $url; |
|
299 | - if ( TimberURLHelper::is_local($url) ) { |
|
300 | - $this->file = ABSPATH . TimberURLHelper::get_rel_url($url); |
|
301 | - $this->file_loc = ABSPATH . TimberURLHelper::get_rel_url($url); |
|
302 | - } |
|
303 | - } |
|
304 | - |
|
305 | - /** |
|
306 | - * @api |
|
307 | - * @example |
|
308 | - * ```twig |
|
309 | - * <img src="{{ image.src }}" alt="{{ image.alt }}" /> |
|
310 | - * ``` |
|
311 | - * ```html |
|
312 | - * <img src="http://example.org/wp-content/uploads/2015/08/pic.jpg" alt="W3 Checker told me to add alt text, so I am" /> |
|
313 | - * ``` |
|
314 | - * @return string alt text stored in WordPress |
|
315 | - */ |
|
316 | - public function alt() { |
|
317 | - $alt = trim(strip_tags(get_post_meta($this->ID, '_wp_attachment_image_alt', true))); |
|
318 | - return $alt; |
|
319 | - } |
|
320 | - |
|
321 | - /** |
|
322 | - * @api |
|
323 | - * @example |
|
324 | - * ```twig |
|
325 | - * {% if post.thumbnail.aspect < 1 %} |
|
326 | - * {# handle vertical image #} |
|
327 | - * <img src="{{ post.thumbnail.src|resize(300, 500) }}" alt="A basketball player" /> |
|
328 | - * {% else %} |
|
329 | - * <img src="{{ post.thumbnail.src|resize(500) }}" alt="A sumo wrestler" /> |
|
330 | - * {% endif %} |
|
331 | - * ``` |
|
332 | - * @return float |
|
333 | - */ |
|
334 | - public function aspect() { |
|
335 | - $w = intval($this->width()); |
|
336 | - $h = intval($this->height()); |
|
337 | - return $w / $h; |
|
338 | - } |
|
339 | - |
|
340 | - /** |
|
341 | - * @api |
|
342 | - * @example |
|
343 | - * ```twig |
|
344 | - * <img src="{{ image.src }}" height="{{ image.height }}" /> |
|
345 | - * ``` |
|
346 | - * ```html |
|
347 | - * <img src="http://example.org/wp-content/uploads/2015/08/pic.jpg" height="900" /> |
|
348 | - * ``` |
|
349 | - * @return int |
|
350 | - */ |
|
351 | - public function height() { |
|
352 | - return $this->get_dimensions('height'); |
|
353 | - } |
|
354 | - |
|
355 | - /** |
|
356 | - * Returns the link to an image attachment's Permalink page (NOT the link for the image itself!!) |
|
357 | - * @api |
|
358 | - * @example |
|
359 | - * ```twig |
|
360 | - * <a href="{{ image.link }}"><img src="{{ image.src }} "/></a> |
|
361 | - * ``` |
|
362 | - * ```html |
|
363 | - * <a href="http://example.org/my-cool-picture"><img src="http://example.org/wp-content/uploads/2015/whatever.jpg"/></a> |
|
364 | - * ``` |
|
365 | - */ |
|
366 | - public function link() { |
|
367 | - if ( strlen($this->abs_url) ) { |
|
368 | - return $this->abs_url; |
|
369 | - } |
|
370 | - return get_permalink($this->ID); |
|
371 | - } |
|
372 | - |
|
373 | - /** |
|
374 | - * @api |
|
375 | - * @return bool|TimberPost |
|
376 | - */ |
|
377 | - public function parent() { |
|
378 | - if ( !$this->post_parent ) { |
|
379 | - return false; |
|
380 | - } |
|
381 | - return new $this->PostClass($this->post_parent); |
|
382 | - } |
|
383 | - |
|
384 | - /** |
|
385 | - * @api |
|
386 | - * @example |
|
387 | - * ```twig |
|
388 | - * <img src="{{ image.path }}" /> |
|
389 | - * ``` |
|
390 | - * ```html |
|
391 | - * <img src="/wp-content/uploads/2015/08/pic.jpg" /> |
|
392 | - * ``` |
|
393 | - * @return string the /relative/path/to/the/file |
|
394 | - */ |
|
395 | - public function path() { |
|
396 | - return TimberURLHelper::get_rel_path($this->src()); |
|
397 | - } |
|
398 | - |
|
399 | - /** |
|
400 | - * @param string $size a size known to WordPress (like "medium") |
|
401 | - * @api |
|
402 | - * @example |
|
403 | - * ```twig |
|
404 | - * <h1>{{post.title}}</h1> |
|
405 | - * <img src="{{post.thumbnail.src}}" /> |
|
406 | - * ``` |
|
407 | - * ```html |
|
408 | - * <img src="http://example.org/wp-content/uploads/2015/08/pic.jpg" /> |
|
409 | - * ``` |
|
410 | - * @return bool|string |
|
411 | - */ |
|
412 | - public function src($size = '') { |
|
413 | - if ( isset($this->abs_url) ) { |
|
414 | - return $this->_maybe_secure_url($this->abs_url); |
|
415 | - } |
|
416 | - |
|
417 | - if ( $size && is_string($size) && isset($this->sizes[$size]) ) { |
|
418 | - $image = image_downsize($this->ID, $size); |
|
419 | - return $this->_maybe_secure_url(reset($image)); |
|
420 | - } |
|
421 | - |
|
422 | - if ( !isset($this->file) && isset($this->_wp_attached_file) ) { |
|
423 | - $this->file = $this->_wp_attached_file; |
|
424 | - } |
|
425 | - |
|
426 | - if ( !isset($this->file) ) { |
|
427 | - return false; |
|
428 | - } |
|
429 | - |
|
430 | - $dir = self::wp_upload_dir(); |
|
431 | - $base = $dir['baseurl']; |
|
432 | - |
|
433 | - $src = trailingslashit($this->_maybe_secure_url($base)) . $this->file; |
|
434 | - $src = apply_filters('timber/image/src', $src, $this->ID); |
|
435 | - return apply_filters('timber_image_src', $src, $this->ID); |
|
436 | - } |
|
437 | - |
|
438 | - /** |
|
439 | - * @deprecated use src() instead |
|
440 | - * @return string |
|
441 | - */ |
|
442 | - function url() { |
|
443 | - return $this->get_src(); |
|
444 | - } |
|
445 | - |
|
446 | - /** |
|
447 | - * @api |
|
448 | - * @example |
|
449 | - * ```twig |
|
450 | - * <img src="{{ image.src }}" width="{{ image.width }}" /> |
|
451 | - * ``` |
|
452 | - * ```html |
|
453 | - * <img src="http://example.org/wp-content/uploads/2015/08/pic.jpg" width="1600" /> |
|
454 | - * ``` |
|
455 | - * @return int |
|
456 | - */ |
|
457 | - public function width() { |
|
458 | - return $this->get_dimensions('width'); |
|
459 | - } |
|
460 | - |
|
461 | - |
|
462 | - /** |
|
463 | - * @deprecated 0.21.9 use TimberImage::width() instead |
|
464 | - * @internal |
|
465 | - * @return int |
|
466 | - */ |
|
467 | - function get_width() { |
|
468 | - return $this->width(); |
|
469 | - } |
|
470 | - |
|
471 | - /** |
|
472 | - * @deprecated 0.21.9 use TimberImage::height() instead |
|
473 | - * @internal |
|
474 | - * @return int |
|
475 | - */ |
|
476 | - function get_height() { |
|
477 | - return $this->height(); |
|
478 | - } |
|
479 | - |
|
480 | - /** |
|
481 | - * @deprecated 0.21.9 use TimberImage::src |
|
482 | - * @internal |
|
483 | - * @param string $size |
|
484 | - * @return bool|string |
|
485 | - */ |
|
486 | - function get_src( $size = '' ) { |
|
487 | - return $this->src( $size ); |
|
488 | - } |
|
489 | - |
|
490 | - /** |
|
491 | - * @deprecated 0.21.9 use TimberImage::path() |
|
492 | - * @internal |
|
493 | - * @return string |
|
494 | - */ |
|
495 | - function get_path() { |
|
496 | - return $this->link(); |
|
497 | - } |
|
498 | - |
|
499 | - /** |
|
500 | - * @deprecated use src() instead |
|
501 | - * @return string |
|
502 | - */ |
|
503 | - function get_url() { |
|
504 | - return $this->get_src(); |
|
505 | - } |
|
506 | - |
|
507 | - /** |
|
508 | - * @internal |
|
509 | - * @deprecated 0.21.8 |
|
510 | - * @return bool|TimberPost |
|
511 | - */ |
|
512 | - function get_parent() { |
|
513 | - return $this->parent(); |
|
514 | - } |
|
515 | - |
|
516 | - /** |
|
517 | - * @internal |
|
518 | - * @deprecated 0.21.9 |
|
519 | - * @see TimberImage::alt |
|
520 | - * @return string |
|
521 | - */ |
|
522 | - function get_alt() { |
|
523 | - return $this->alt(); |
|
524 | - } |
|
42 | + protected $_can_edit; |
|
43 | + protected $_dimensions; |
|
44 | + public $abs_url; |
|
45 | + /** |
|
46 | + * @var string $object_type what does this class represent in WordPress terms? |
|
47 | + */ |
|
48 | + public $object_type = 'image'; |
|
49 | + /** |
|
50 | + * @var string $representation what does this class represent in WordPress terms? |
|
51 | + */ |
|
52 | + public static $representation = 'image'; |
|
53 | + /** |
|
54 | + * @var array of supported relative file types |
|
55 | + */ |
|
56 | + private $file_types = array('jpg', 'jpeg', 'png', 'svg', 'bmp', 'ico', 'gif', 'tiff', 'pdf'); |
|
57 | + /** |
|
58 | + * @api |
|
59 | + * @var string $file_loc the location of the image file in the filesystem (ex: `/var/www/htdocs/wp-content/uploads/2015/08/my-pic.jpg`) |
|
60 | + */ |
|
61 | + public $file_loc; |
|
62 | + public $file; |
|
63 | + /** |
|
64 | + * @api |
|
65 | + * @var integer the ID of the image (which is a WP_Post) |
|
66 | + */ |
|
67 | + public $id; |
|
68 | + public $sizes = array(); |
|
69 | + /** |
|
70 | + * @api |
|
71 | + * @var string $caption the string stored in the WordPress database |
|
72 | + */ |
|
73 | + public $caption; |
|
74 | + /** |
|
75 | + * @var $_wp_attached_file the file as stored in the WordPress database |
|
76 | + */ |
|
77 | + protected $_wp_attached_file; |
|
78 | + |
|
79 | + /** |
|
80 | + * Creates a new TimberImage object |
|
81 | + * @example |
|
82 | + * ```php |
|
83 | + * // You can pass it an ID number |
|
84 | + * $myImage = new TimberImage(552); |
|
85 | + * |
|
86 | + * //Or send it a URL to an image |
|
87 | + * $myImage = new TimberImage('http://google.com/logo.jpg'); |
|
88 | + * ``` |
|
89 | + * @param int|string $iid |
|
90 | + */ |
|
91 | + public function __construct($iid) { |
|
92 | + $this->init($iid); |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * @return string the src of the file |
|
97 | + */ |
|
98 | + public function __toString() { |
|
99 | + if ( $this->get_src() ) { |
|
100 | + return $this->get_src(); |
|
101 | + } |
|
102 | + return ''; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * Get a PHP array with pathinfo() info from the file |
|
107 | + * @return array |
|
108 | + */ |
|
109 | + function get_pathinfo() { |
|
110 | + return pathinfo($this->file); |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * @internal |
|
115 | + * @param string $dim |
|
116 | + * @return array|int |
|
117 | + */ |
|
118 | + protected function get_dimensions($dim = null) { |
|
119 | + if ( isset($this->_dimensions) ) { |
|
120 | + return $this->get_dimensions_loaded($dim); |
|
121 | + } |
|
122 | + if ( file_exists($this->file_loc) && filesize($this->file_loc) ) { |
|
123 | + list($width, $height) = getimagesize($this->file_loc); |
|
124 | + $this->_dimensions = array(); |
|
125 | + $this->_dimensions[0] = $width; |
|
126 | + $this->_dimensions[1] = $height; |
|
127 | + return $this->get_dimensions_loaded($dim); |
|
128 | + } |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * @internal |
|
133 | + * @param string|null $dim |
|
134 | + * @return array|int |
|
135 | + */ |
|
136 | + protected function get_dimensions_loaded($dim) { |
|
137 | + if ( $dim === null ) { |
|
138 | + return $this->_dimensions; |
|
139 | + } |
|
140 | + if ( $dim == 'w' || $dim == 'width' ) { |
|
141 | + return $this->_dimensions[0]; |
|
142 | + } |
|
143 | + if ( $dim == 'h' || $dim == 'height' ) { |
|
144 | + return $this->_dimensions[1]; |
|
145 | + } |
|
146 | + return null; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * @internal |
|
151 | + * @param int $iid the id number of the image in the WP database |
|
152 | + */ |
|
153 | + protected function get_image_info( $iid ) { |
|
154 | + $image_info = $iid; |
|
155 | + if (is_numeric($iid)) { |
|
156 | + $image_info = wp_get_attachment_metadata($iid); |
|
157 | + if (!is_array($image_info)) { |
|
158 | + $image_info = array(); |
|
159 | + } |
|
160 | + $image_custom = get_post_custom($iid); |
|
161 | + $basic = get_post($iid); |
|
162 | + if ($basic) { |
|
163 | + if (isset($basic->post_excerpt)) { |
|
164 | + $this->caption = $basic->post_excerpt; |
|
165 | + } |
|
166 | + $image_custom = array_merge($image_custom, get_object_vars($basic)); |
|
167 | + } |
|
168 | + return array_merge($image_info, $image_custom); |
|
169 | + } |
|
170 | + if (is_array($image_info) && isset($image_info['image'])) { |
|
171 | + return $image_info['image']; |
|
172 | + } |
|
173 | + if (is_object($image_info)) { |
|
174 | + return get_object_vars($image_info); |
|
175 | + } |
|
176 | + return $iid; |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * @internal |
|
181 | + * @param string $url for evaluation |
|
182 | + * @return string with http/https corrected depending on what's appropriate for server |
|
183 | + */ |
|
184 | + protected static function _maybe_secure_url($url) { |
|
185 | + if ( is_ssl() && strpos($url, 'https') !== 0 && strpos($url, 'http') === 0 ) { |
|
186 | + $url = 'https' . substr($url, strlen('http')); |
|
187 | + } |
|
188 | + return $url; |
|
189 | + } |
|
190 | + |
|
191 | + public static function wp_upload_dir() { |
|
192 | + static $wp_upload_dir = false; |
|
193 | + |
|
194 | + if ( !$wp_upload_dir ) { |
|
195 | + $wp_upload_dir = wp_upload_dir(); |
|
196 | + } |
|
197 | + |
|
198 | + return $wp_upload_dir; |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * @internal |
|
203 | + * @param int $iid |
|
204 | + */ |
|
205 | + function init( $iid = false ) { |
|
206 | + if ( !is_numeric( $iid ) && is_string( $iid ) ) { |
|
207 | + if (strstr($iid, '://')) { |
|
208 | + $this->init_with_url($iid); |
|
209 | + return; |
|
210 | + } |
|
211 | + if ( strstr($iid, ABSPATH) ) { |
|
212 | + $this->init_with_file_path($iid); |
|
213 | + return; |
|
214 | + } |
|
215 | + |
|
216 | + $relative = false; |
|
217 | + $iid_lower = strtolower($iid); |
|
218 | + foreach( $this->file_types as $type ) { if( strstr( $iid_lower, $type ) ) { $relative = true; break; } }; |
|
219 | + if ( $relative ) { |
|
220 | + $this->init_with_relative_path( $iid ); |
|
221 | + return; |
|
222 | + } |
|
223 | + } else if ( $iid instanceof WP_Post ) { |
|
224 | + $ref = new ReflectionClass($this); |
|
225 | + $post = $ref->getParentClass()->newInstance($iid->ID); |
|
226 | + if (isset($post->_thumbnail_id) && $post->_thumbnail_id) { |
|
227 | + return $this->init((int) $post->_thumbnail_id); |
|
228 | + } |
|
229 | + return $this->init($iid->ID); |
|
230 | + } else if ( $iid instanceof TimberPost ) { |
|
231 | + /** |
|
232 | + * This will catch TimberPost and any post classes that extend TimberPost, |
|
233 | + * see http://php.net/manual/en/internals2.opcodes.instanceof.php#109108 |
|
234 | + * and https://github.com/jarednova/timber/wiki/Extending-Timber |
|
235 | + */ |
|
236 | + $iid = (int) $iid->_thumbnail_id; |
|
237 | + } |
|
238 | + |
|
239 | + $image_info = $this->get_image_info($iid); |
|
240 | + |
|
241 | + $this->import($image_info); |
|
242 | + $basedir = self::wp_upload_dir(); |
|
243 | + $basedir = $basedir['basedir']; |
|
244 | + if ( isset($this->file) ) { |
|
245 | + $this->file_loc = $basedir . DIRECTORY_SEPARATOR . $this->file; |
|
246 | + } else if ( isset($this->_wp_attached_file) ) { |
|
247 | + $this->file = reset($this->_wp_attached_file); |
|
248 | + $this->file_loc = $basedir . DIRECTORY_SEPARATOR . $this->file; |
|
249 | + } |
|
250 | + if ( isset($image_info['id']) ) { |
|
251 | + $this->ID = $image_info['id']; |
|
252 | + } else if ( is_numeric($iid) ) { |
|
253 | + $this->ID = $iid; |
|
254 | + } |
|
255 | + if ( isset($this->ID) ) { |
|
256 | + $custom = get_post_custom($this->ID); |
|
257 | + foreach ($custom as $key => $value) { |
|
258 | + $this->$key = $value[0]; |
|
259 | + } |
|
260 | + $this->id = $this->ID; |
|
261 | + } else { |
|
262 | + if ( is_array($iid) || is_object($iid) ) { |
|
263 | + TimberHelper::error_log('Not able to init in TimberImage with iid='); |
|
264 | + TimberHelper::error_log($iid); |
|
265 | + } else { |
|
266 | + TimberHelper::error_log('Not able to init in TimberImage with iid=' . $iid); |
|
267 | + } |
|
268 | + } |
|
269 | + } |
|
270 | + |
|
271 | + /** |
|
272 | + * @internal |
|
273 | + * @param string $relative_path |
|
274 | + */ |
|
275 | + protected function init_with_relative_path( $relative_path ) { |
|
276 | + $this->abs_url = home_url( $relative_path ); |
|
277 | + $file_path = TimberURLHelper::get_full_path( $relative_path ); |
|
278 | + $this->file_loc = $file_path; |
|
279 | + $this->file = $file_path; |
|
280 | + } |
|
281 | + |
|
282 | + /** |
|
283 | + * @internal |
|
284 | + * @param string $file_path |
|
285 | + */ |
|
286 | + protected function init_with_file_path( $file_path ) { |
|
287 | + $url = TimberURLHelper::file_system_to_url( $file_path ); |
|
288 | + $this->abs_url = $url; |
|
289 | + $this->file_loc = $file_path; |
|
290 | + $this->file = $file_path; |
|
291 | + } |
|
292 | + |
|
293 | + /** |
|
294 | + * @internal |
|
295 | + * @param string $url |
|
296 | + */ |
|
297 | + protected function init_with_url($url) { |
|
298 | + $this->abs_url = $url; |
|
299 | + if ( TimberURLHelper::is_local($url) ) { |
|
300 | + $this->file = ABSPATH . TimberURLHelper::get_rel_url($url); |
|
301 | + $this->file_loc = ABSPATH . TimberURLHelper::get_rel_url($url); |
|
302 | + } |
|
303 | + } |
|
304 | + |
|
305 | + /** |
|
306 | + * @api |
|
307 | + * @example |
|
308 | + * ```twig |
|
309 | + * <img src="{{ image.src }}" alt="{{ image.alt }}" /> |
|
310 | + * ``` |
|
311 | + * ```html |
|
312 | + * <img src="http://example.org/wp-content/uploads/2015/08/pic.jpg" alt="W3 Checker told me to add alt text, so I am" /> |
|
313 | + * ``` |
|
314 | + * @return string alt text stored in WordPress |
|
315 | + */ |
|
316 | + public function alt() { |
|
317 | + $alt = trim(strip_tags(get_post_meta($this->ID, '_wp_attachment_image_alt', true))); |
|
318 | + return $alt; |
|
319 | + } |
|
320 | + |
|
321 | + /** |
|
322 | + * @api |
|
323 | + * @example |
|
324 | + * ```twig |
|
325 | + * {% if post.thumbnail.aspect < 1 %} |
|
326 | + * {# handle vertical image #} |
|
327 | + * <img src="{{ post.thumbnail.src|resize(300, 500) }}" alt="A basketball player" /> |
|
328 | + * {% else %} |
|
329 | + * <img src="{{ post.thumbnail.src|resize(500) }}" alt="A sumo wrestler" /> |
|
330 | + * {% endif %} |
|
331 | + * ``` |
|
332 | + * @return float |
|
333 | + */ |
|
334 | + public function aspect() { |
|
335 | + $w = intval($this->width()); |
|
336 | + $h = intval($this->height()); |
|
337 | + return $w / $h; |
|
338 | + } |
|
339 | + |
|
340 | + /** |
|
341 | + * @api |
|
342 | + * @example |
|
343 | + * ```twig |
|
344 | + * <img src="{{ image.src }}" height="{{ image.height }}" /> |
|
345 | + * ``` |
|
346 | + * ```html |
|
347 | + * <img src="http://example.org/wp-content/uploads/2015/08/pic.jpg" height="900" /> |
|
348 | + * ``` |
|
349 | + * @return int |
|
350 | + */ |
|
351 | + public function height() { |
|
352 | + return $this->get_dimensions('height'); |
|
353 | + } |
|
354 | + |
|
355 | + /** |
|
356 | + * Returns the link to an image attachment's Permalink page (NOT the link for the image itself!!) |
|
357 | + * @api |
|
358 | + * @example |
|
359 | + * ```twig |
|
360 | + * <a href="{{ image.link }}"><img src="{{ image.src }} "/></a> |
|
361 | + * ``` |
|
362 | + * ```html |
|
363 | + * <a href="http://example.org/my-cool-picture"><img src="http://example.org/wp-content/uploads/2015/whatever.jpg"/></a> |
|
364 | + * ``` |
|
365 | + */ |
|
366 | + public function link() { |
|
367 | + if ( strlen($this->abs_url) ) { |
|
368 | + return $this->abs_url; |
|
369 | + } |
|
370 | + return get_permalink($this->ID); |
|
371 | + } |
|
372 | + |
|
373 | + /** |
|
374 | + * @api |
|
375 | + * @return bool|TimberPost |
|
376 | + */ |
|
377 | + public function parent() { |
|
378 | + if ( !$this->post_parent ) { |
|
379 | + return false; |
|
380 | + } |
|
381 | + return new $this->PostClass($this->post_parent); |
|
382 | + } |
|
383 | + |
|
384 | + /** |
|
385 | + * @api |
|
386 | + * @example |
|
387 | + * ```twig |
|
388 | + * <img src="{{ image.path }}" /> |
|
389 | + * ``` |
|
390 | + * ```html |
|
391 | + * <img src="/wp-content/uploads/2015/08/pic.jpg" /> |
|
392 | + * ``` |
|
393 | + * @return string the /relative/path/to/the/file |
|
394 | + */ |
|
395 | + public function path() { |
|
396 | + return TimberURLHelper::get_rel_path($this->src()); |
|
397 | + } |
|
398 | + |
|
399 | + /** |
|
400 | + * @param string $size a size known to WordPress (like "medium") |
|
401 | + * @api |
|
402 | + * @example |
|
403 | + * ```twig |
|
404 | + * <h1>{{post.title}}</h1> |
|
405 | + * <img src="{{post.thumbnail.src}}" /> |
|
406 | + * ``` |
|
407 | + * ```html |
|
408 | + * <img src="http://example.org/wp-content/uploads/2015/08/pic.jpg" /> |
|
409 | + * ``` |
|
410 | + * @return bool|string |
|
411 | + */ |
|
412 | + public function src($size = '') { |
|
413 | + if ( isset($this->abs_url) ) { |
|
414 | + return $this->_maybe_secure_url($this->abs_url); |
|
415 | + } |
|
416 | + |
|
417 | + if ( $size && is_string($size) && isset($this->sizes[$size]) ) { |
|
418 | + $image = image_downsize($this->ID, $size); |
|
419 | + return $this->_maybe_secure_url(reset($image)); |
|
420 | + } |
|
421 | + |
|
422 | + if ( !isset($this->file) && isset($this->_wp_attached_file) ) { |
|
423 | + $this->file = $this->_wp_attached_file; |
|
424 | + } |
|
425 | + |
|
426 | + if ( !isset($this->file) ) { |
|
427 | + return false; |
|
428 | + } |
|
429 | + |
|
430 | + $dir = self::wp_upload_dir(); |
|
431 | + $base = $dir['baseurl']; |
|
432 | + |
|
433 | + $src = trailingslashit($this->_maybe_secure_url($base)) . $this->file; |
|
434 | + $src = apply_filters('timber/image/src', $src, $this->ID); |
|
435 | + return apply_filters('timber_image_src', $src, $this->ID); |
|
436 | + } |
|
437 | + |
|
438 | + /** |
|
439 | + * @deprecated use src() instead |
|
440 | + * @return string |
|
441 | + */ |
|
442 | + function url() { |
|
443 | + return $this->get_src(); |
|
444 | + } |
|
445 | + |
|
446 | + /** |
|
447 | + * @api |
|
448 | + * @example |
|
449 | + * ```twig |
|
450 | + * <img src="{{ image.src }}" width="{{ image.width }}" /> |
|
451 | + * ``` |
|
452 | + * ```html |
|
453 | + * <img src="http://example.org/wp-content/uploads/2015/08/pic.jpg" width="1600" /> |
|
454 | + * ``` |
|
455 | + * @return int |
|
456 | + */ |
|
457 | + public function width() { |
|
458 | + return $this->get_dimensions('width'); |
|
459 | + } |
|
460 | + |
|
461 | + |
|
462 | + /** |
|
463 | + * @deprecated 0.21.9 use TimberImage::width() instead |
|
464 | + * @internal |
|
465 | + * @return int |
|
466 | + */ |
|
467 | + function get_width() { |
|
468 | + return $this->width(); |
|
469 | + } |
|
470 | + |
|
471 | + /** |
|
472 | + * @deprecated 0.21.9 use TimberImage::height() instead |
|
473 | + * @internal |
|
474 | + * @return int |
|
475 | + */ |
|
476 | + function get_height() { |
|
477 | + return $this->height(); |
|
478 | + } |
|
479 | + |
|
480 | + /** |
|
481 | + * @deprecated 0.21.9 use TimberImage::src |
|
482 | + * @internal |
|
483 | + * @param string $size |
|
484 | + * @return bool|string |
|
485 | + */ |
|
486 | + function get_src( $size = '' ) { |
|
487 | + return $this->src( $size ); |
|
488 | + } |
|
489 | + |
|
490 | + /** |
|
491 | + * @deprecated 0.21.9 use TimberImage::path() |
|
492 | + * @internal |
|
493 | + * @return string |
|
494 | + */ |
|
495 | + function get_path() { |
|
496 | + return $this->link(); |
|
497 | + } |
|
498 | + |
|
499 | + /** |
|
500 | + * @deprecated use src() instead |
|
501 | + * @return string |
|
502 | + */ |
|
503 | + function get_url() { |
|
504 | + return $this->get_src(); |
|
505 | + } |
|
506 | + |
|
507 | + /** |
|
508 | + * @internal |
|
509 | + * @deprecated 0.21.8 |
|
510 | + * @return bool|TimberPost |
|
511 | + */ |
|
512 | + function get_parent() { |
|
513 | + return $this->parent(); |
|
514 | + } |
|
515 | + |
|
516 | + /** |
|
517 | + * @internal |
|
518 | + * @deprecated 0.21.9 |
|
519 | + * @see TimberImage::alt |
|
520 | + * @return string |
|
521 | + */ |
|
522 | + function get_alt() { |
|
523 | + return $this->alt(); |
|
524 | + } |
|
525 | 525 | |
526 | 526 | } |
@@ -88,7 +88,7 @@ discard block |
||
88 | 88 | * ``` |
89 | 89 | * @param int|string $iid |
90 | 90 | */ |
91 | - public function __construct($iid) { |
|
91 | + public function __construct( $iid ) { |
|
92 | 92 | $this->init($iid); |
93 | 93 | } |
94 | 94 | |
@@ -115,7 +115,7 @@ discard block |
||
115 | 115 | * @param string $dim |
116 | 116 | * @return array|int |
117 | 117 | */ |
118 | - protected function get_dimensions($dim = null) { |
|
118 | + protected function get_dimensions( $dim = null ) { |
|
119 | 119 | if ( isset($this->_dimensions) ) { |
120 | 120 | return $this->get_dimensions_loaded($dim); |
121 | 121 | } |
@@ -133,7 +133,7 @@ discard block |
||
133 | 133 | * @param string|null $dim |
134 | 134 | * @return array|int |
135 | 135 | */ |
136 | - protected function get_dimensions_loaded($dim) { |
|
136 | + protected function get_dimensions_loaded( $dim ) { |
|
137 | 137 | if ( $dim === null ) { |
138 | 138 | return $this->_dimensions; |
139 | 139 | } |
@@ -152,25 +152,25 @@ discard block |
||
152 | 152 | */ |
153 | 153 | protected function get_image_info( $iid ) { |
154 | 154 | $image_info = $iid; |
155 | - if (is_numeric($iid)) { |
|
155 | + if ( is_numeric($iid) ) { |
|
156 | 156 | $image_info = wp_get_attachment_metadata($iid); |
157 | - if (!is_array($image_info)) { |
|
157 | + if ( !is_array($image_info) ) { |
|
158 | 158 | $image_info = array(); |
159 | 159 | } |
160 | 160 | $image_custom = get_post_custom($iid); |
161 | 161 | $basic = get_post($iid); |
162 | - if ($basic) { |
|
163 | - if (isset($basic->post_excerpt)) { |
|
162 | + if ( $basic ) { |
|
163 | + if ( isset($basic->post_excerpt) ) { |
|
164 | 164 | $this->caption = $basic->post_excerpt; |
165 | 165 | } |
166 | 166 | $image_custom = array_merge($image_custom, get_object_vars($basic)); |
167 | 167 | } |
168 | 168 | return array_merge($image_info, $image_custom); |
169 | 169 | } |
170 | - if (is_array($image_info) && isset($image_info['image'])) { |
|
170 | + if ( is_array($image_info) && isset($image_info['image']) ) { |
|
171 | 171 | return $image_info['image']; |
172 | 172 | } |
173 | - if (is_object($image_info)) { |
|
173 | + if ( is_object($image_info) ) { |
|
174 | 174 | return get_object_vars($image_info); |
175 | 175 | } |
176 | 176 | return $iid; |
@@ -181,7 +181,7 @@ discard block |
||
181 | 181 | * @param string $url for evaluation |
182 | 182 | * @return string with http/https corrected depending on what's appropriate for server |
183 | 183 | */ |
184 | - protected static function _maybe_secure_url($url) { |
|
184 | + protected static function _maybe_secure_url( $url ) { |
|
185 | 185 | if ( is_ssl() && strpos($url, 'https') !== 0 && strpos($url, 'http') === 0 ) { |
186 | 186 | $url = 'https' . substr($url, strlen('http')); |
187 | 187 | } |
@@ -203,8 +203,8 @@ discard block |
||
203 | 203 | * @param int $iid |
204 | 204 | */ |
205 | 205 | function init( $iid = false ) { |
206 | - if ( !is_numeric( $iid ) && is_string( $iid ) ) { |
|
207 | - if (strstr($iid, '://')) { |
|
206 | + if ( !is_numeric($iid) && is_string($iid) ) { |
|
207 | + if ( strstr($iid, '://') ) { |
|
208 | 208 | $this->init_with_url($iid); |
209 | 209 | return; |
210 | 210 | } |
@@ -215,15 +215,15 @@ discard block |
||
215 | 215 | |
216 | 216 | $relative = false; |
217 | 217 | $iid_lower = strtolower($iid); |
218 | - foreach( $this->file_types as $type ) { if( strstr( $iid_lower, $type ) ) { $relative = true; break; } }; |
|
218 | + foreach ( $this->file_types as $type ) { if ( strstr($iid_lower, $type) ) { $relative = true; break; } }; |
|
219 | 219 | if ( $relative ) { |
220 | - $this->init_with_relative_path( $iid ); |
|
220 | + $this->init_with_relative_path($iid); |
|
221 | 221 | return; |
222 | 222 | } |
223 | 223 | } else if ( $iid instanceof WP_Post ) { |
224 | 224 | $ref = new ReflectionClass($this); |
225 | 225 | $post = $ref->getParentClass()->newInstance($iid->ID); |
226 | - if (isset($post->_thumbnail_id) && $post->_thumbnail_id) { |
|
226 | + if ( isset($post->_thumbnail_id) && $post->_thumbnail_id ) { |
|
227 | 227 | return $this->init((int) $post->_thumbnail_id); |
228 | 228 | } |
229 | 229 | return $this->init($iid->ID); |
@@ -254,7 +254,7 @@ discard block |
||
254 | 254 | } |
255 | 255 | if ( isset($this->ID) ) { |
256 | 256 | $custom = get_post_custom($this->ID); |
257 | - foreach ($custom as $key => $value) { |
|
257 | + foreach ( $custom as $key => $value ) { |
|
258 | 258 | $this->$key = $value[0]; |
259 | 259 | } |
260 | 260 | $this->id = $this->ID; |
@@ -273,8 +273,8 @@ discard block |
||
273 | 273 | * @param string $relative_path |
274 | 274 | */ |
275 | 275 | protected function init_with_relative_path( $relative_path ) { |
276 | - $this->abs_url = home_url( $relative_path ); |
|
277 | - $file_path = TimberURLHelper::get_full_path( $relative_path ); |
|
276 | + $this->abs_url = home_url($relative_path); |
|
277 | + $file_path = TimberURLHelper::get_full_path($relative_path); |
|
278 | 278 | $this->file_loc = $file_path; |
279 | 279 | $this->file = $file_path; |
280 | 280 | } |
@@ -284,7 +284,7 @@ discard block |
||
284 | 284 | * @param string $file_path |
285 | 285 | */ |
286 | 286 | protected function init_with_file_path( $file_path ) { |
287 | - $url = TimberURLHelper::file_system_to_url( $file_path ); |
|
287 | + $url = TimberURLHelper::file_system_to_url($file_path); |
|
288 | 288 | $this->abs_url = $url; |
289 | 289 | $this->file_loc = $file_path; |
290 | 290 | $this->file = $file_path; |
@@ -294,7 +294,7 @@ discard block |
||
294 | 294 | * @internal |
295 | 295 | * @param string $url |
296 | 296 | */ |
297 | - protected function init_with_url($url) { |
|
297 | + protected function init_with_url( $url ) { |
|
298 | 298 | $this->abs_url = $url; |
299 | 299 | if ( TimberURLHelper::is_local($url) ) { |
300 | 300 | $this->file = ABSPATH . TimberURLHelper::get_rel_url($url); |
@@ -409,7 +409,7 @@ discard block |
||
409 | 409 | * ``` |
410 | 410 | * @return bool|string |
411 | 411 | */ |
412 | - public function src($size = '') { |
|
412 | + public function src( $size = '' ) { |
|
413 | 413 | if ( isset($this->abs_url) ) { |
414 | 414 | return $this->_maybe_secure_url($this->abs_url); |
415 | 415 | } |
@@ -484,7 +484,7 @@ discard block |
||
484 | 484 | * @return bool|string |
485 | 485 | */ |
486 | 486 | function get_src( $size = '' ) { |
487 | - return $this->src( $size ); |
|
487 | + return $this->src($size); |
|
488 | 488 | } |
489 | 489 | |
490 | 490 | /** |
@@ -149,7 +149,7 @@ |
||
149 | 149 | /** |
150 | 150 | * |
151 | 151 | * @internal |
152 | - * @return bool |
|
152 | + * @return boolean|null |
|
153 | 153 | */ |
154 | 154 | function update_child_levels() { |
155 | 155 | if (is_array($this->children)) { |
@@ -2,306 +2,306 @@ |
||
2 | 2 | |
3 | 3 | class TimberMenuItem extends TimberCore implements TimberCoreInterface { |
4 | 4 | |
5 | - public $children; |
|
6 | - public $has_child_class = false; |
|
7 | - public $classes = array(); |
|
8 | - public $class = ''; |
|
9 | - public $level = 0; |
|
10 | - public $post_name; |
|
11 | - public $type; |
|
12 | - public $url; |
|
5 | + public $children; |
|
6 | + public $has_child_class = false; |
|
7 | + public $classes = array(); |
|
8 | + public $class = ''; |
|
9 | + public $level = 0; |
|
10 | + public $post_name; |
|
11 | + public $type; |
|
12 | + public $url; |
|
13 | 13 | |
14 | - public $PostClass = 'TimberPost'; |
|
14 | + public $PostClass = 'TimberPost'; |
|
15 | 15 | |
16 | - protected $_name; |
|
17 | - protected $_menu_item_object_id; |
|
18 | - protected $_menu_item_url; |
|
19 | - protected $menu_object; |
|
20 | - protected $master_object; |
|
16 | + protected $_name; |
|
17 | + protected $_menu_item_object_id; |
|
18 | + protected $_menu_item_url; |
|
19 | + protected $menu_object; |
|
20 | + protected $master_object; |
|
21 | 21 | |
22 | - /** |
|
23 | - * |
|
24 | - * |
|
25 | - * @param array|object $data |
|
26 | - */ |
|
27 | - public function __construct( $data ) { |
|
28 | - $data = (object) $data; |
|
29 | - $this->import( $data ); |
|
30 | - $this->import_classes( $data ); |
|
31 | - if ( isset( $this->name ) ) { |
|
32 | - $this->_name = $this->name; |
|
33 | - } |
|
34 | - $this->name = $this->name(); |
|
35 | - $this->add_class( 'menu-item-' . $this->ID ); |
|
36 | - $this->menu_object = $data; |
|
37 | - } |
|
22 | + /** |
|
23 | + * |
|
24 | + * |
|
25 | + * @param array|object $data |
|
26 | + */ |
|
27 | + public function __construct( $data ) { |
|
28 | + $data = (object) $data; |
|
29 | + $this->import( $data ); |
|
30 | + $this->import_classes( $data ); |
|
31 | + if ( isset( $this->name ) ) { |
|
32 | + $this->_name = $this->name; |
|
33 | + } |
|
34 | + $this->name = $this->name(); |
|
35 | + $this->add_class( 'menu-item-' . $this->ID ); |
|
36 | + $this->menu_object = $data; |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * @return string the label for the menu item |
|
41 | - */ |
|
42 | - public function __toString() { |
|
43 | - return $this->name(); |
|
44 | - } |
|
39 | + /** |
|
40 | + * @return string the label for the menu item |
|
41 | + */ |
|
42 | + public function __toString() { |
|
43 | + return $this->name(); |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * add a class the menu item should have |
|
48 | - * @param string $class_name to be added |
|
49 | - */ |
|
50 | - public function add_class( $class_name ) { |
|
51 | - $this->classes[] = $class_name; |
|
52 | - $this->class .= ' ' . $class_name; |
|
53 | - } |
|
46 | + /** |
|
47 | + * add a class the menu item should have |
|
48 | + * @param string $class_name to be added |
|
49 | + */ |
|
50 | + public function add_class( $class_name ) { |
|
51 | + $this->classes[] = $class_name; |
|
52 | + $this->class .= ' ' . $class_name; |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * The label for the menu item |
|
57 | - * @api |
|
58 | - * @return string |
|
59 | - */ |
|
60 | - public function name() { |
|
61 | - if ( $title = $this->title() ) { |
|
62 | - return $title; |
|
63 | - } |
|
64 | - if ( isset( $this->_name ) ) { |
|
65 | - return $this->_name; |
|
66 | - } |
|
67 | - return ''; |
|
68 | - } |
|
55 | + /** |
|
56 | + * The label for the menu item |
|
57 | + * @api |
|
58 | + * @return string |
|
59 | + */ |
|
60 | + public function name() { |
|
61 | + if ( $title = $this->title() ) { |
|
62 | + return $title; |
|
63 | + } |
|
64 | + if ( isset( $this->_name ) ) { |
|
65 | + return $this->_name; |
|
66 | + } |
|
67 | + return ''; |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * The slug for the menu item |
|
72 | - * @api |
|
73 | - * @example |
|
74 | - * ```twig |
|
75 | - * <ul> |
|
76 | - * {% for item in menu.items %} |
|
77 | - * <li class="{{item.slug}}"> |
|
78 | - * <a href="{{item.link}}">{{item.name}}</a> |
|
79 | - * </li> |
|
80 | - * {% endfor %} |
|
81 | - * </ul> |
|
82 | - * @return string the slug of the menu item kinda-like-this |
|
83 | - */ |
|
84 | - public function slug() { |
|
85 | - if ( !isset( $this->master_object ) ) { |
|
86 | - $this->master_object = $this->get_master_object(); |
|
87 | - } |
|
88 | - if ( isset( $this->master_object->post_name ) && $this->master_object->post_name ) { |
|
89 | - return $this->master_object->post_name; |
|
90 | - } |
|
91 | - return $this->post_name; |
|
92 | - } |
|
70 | + /** |
|
71 | + * The slug for the menu item |
|
72 | + * @api |
|
73 | + * @example |
|
74 | + * ```twig |
|
75 | + * <ul> |
|
76 | + * {% for item in menu.items %} |
|
77 | + * <li class="{{item.slug}}"> |
|
78 | + * <a href="{{item.link}}">{{item.name}}</a> |
|
79 | + * </li> |
|
80 | + * {% endfor %} |
|
81 | + * </ul> |
|
82 | + * @return string the slug of the menu item kinda-like-this |
|
83 | + */ |
|
84 | + public function slug() { |
|
85 | + if ( !isset( $this->master_object ) ) { |
|
86 | + $this->master_object = $this->get_master_object(); |
|
87 | + } |
|
88 | + if ( isset( $this->master_object->post_name ) && $this->master_object->post_name ) { |
|
89 | + return $this->master_object->post_name; |
|
90 | + } |
|
91 | + return $this->post_name; |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * @internal |
|
96 | - * @return mixed whatever object (Post, Term, etc.) the menu item represents |
|
97 | - */ |
|
98 | - protected function get_master_object() { |
|
99 | - if ( isset( $this->_menu_item_object_id ) ) { |
|
100 | - return new $this->PostClass( $this->_menu_item_object_id ); |
|
101 | - } |
|
102 | - } |
|
94 | + /** |
|
95 | + * @internal |
|
96 | + * @return mixed whatever object (Post, Term, etc.) the menu item represents |
|
97 | + */ |
|
98 | + protected function get_master_object() { |
|
99 | + if ( isset( $this->_menu_item_object_id ) ) { |
|
100 | + return new $this->PostClass( $this->_menu_item_object_id ); |
|
101 | + } |
|
102 | + } |
|
103 | 103 | |
104 | - /** |
|
105 | - * @internal |
|
106 | - * @see TimberMenuItem::link |
|
107 | - * @return string an absolute URL http://example.org/my-page |
|
108 | - */ |
|
109 | - function get_link() { |
|
110 | - if ( !isset( $this->url ) || !$this->url ) { |
|
111 | - if ( isset( $this->_menu_item_type ) && $this->_menu_item_type == 'custom' ) { |
|
112 | - $this->url = $this->_menu_item_url; |
|
113 | - } else if ( isset( $this->menu_object ) && method_exists( $this->menu_object, 'get_link' ) ) { |
|
114 | - $this->url = $this->menu_object->get_link(); |
|
115 | - } |
|
116 | - } |
|
117 | - return $this->url; |
|
118 | - } |
|
104 | + /** |
|
105 | + * @internal |
|
106 | + * @see TimberMenuItem::link |
|
107 | + * @return string an absolute URL http://example.org/my-page |
|
108 | + */ |
|
109 | + function get_link() { |
|
110 | + if ( !isset( $this->url ) || !$this->url ) { |
|
111 | + if ( isset( $this->_menu_item_type ) && $this->_menu_item_type == 'custom' ) { |
|
112 | + $this->url = $this->_menu_item_url; |
|
113 | + } else if ( isset( $this->menu_object ) && method_exists( $this->menu_object, 'get_link' ) ) { |
|
114 | + $this->url = $this->menu_object->get_link(); |
|
115 | + } |
|
116 | + } |
|
117 | + return $this->url; |
|
118 | + } |
|
119 | 119 | |
120 | - /** |
|
121 | - * @internal |
|
122 | - * @see TimberMenuItem::path() |
|
123 | - * @return string a relative url /my-page |
|
124 | - */ |
|
125 | - function get_path() { |
|
126 | - return TimberURLHelper::get_rel_url( $this->get_link() ); |
|
127 | - } |
|
120 | + /** |
|
121 | + * @internal |
|
122 | + * @see TimberMenuItem::path() |
|
123 | + * @return string a relative url /my-page |
|
124 | + */ |
|
125 | + function get_path() { |
|
126 | + return TimberURLHelper::get_rel_url( $this->get_link() ); |
|
127 | + } |
|
128 | 128 | |
129 | - /** |
|
130 | - * |
|
131 | - * |
|
132 | - * @param TimberMenuItem $item |
|
133 | - */ |
|
134 | - function add_child( $item ) { |
|
135 | - if ( !$this->has_child_class ) { |
|
136 | - $this->add_class( 'menu-item-has-children' ); |
|
137 | - $this->has_child_class = true; |
|
138 | - } |
|
139 | - if ( !isset( $this->children ) ) { |
|
140 | - $this->children = array(); |
|
141 | - } |
|
142 | - $this->children[] = $item; |
|
143 | - $item->level = $this->level + 1; |
|
144 | - if ($item->children) { |
|
145 | - $this->update_child_levels(); |
|
146 | - } |
|
147 | - } |
|
129 | + /** |
|
130 | + * |
|
131 | + * |
|
132 | + * @param TimberMenuItem $item |
|
133 | + */ |
|
134 | + function add_child( $item ) { |
|
135 | + if ( !$this->has_child_class ) { |
|
136 | + $this->add_class( 'menu-item-has-children' ); |
|
137 | + $this->has_child_class = true; |
|
138 | + } |
|
139 | + if ( !isset( $this->children ) ) { |
|
140 | + $this->children = array(); |
|
141 | + } |
|
142 | + $this->children[] = $item; |
|
143 | + $item->level = $this->level + 1; |
|
144 | + if ($item->children) { |
|
145 | + $this->update_child_levels(); |
|
146 | + } |
|
147 | + } |
|
148 | 148 | |
149 | - /** |
|
150 | - * |
|
151 | - * @internal |
|
152 | - * @return bool |
|
153 | - */ |
|
154 | - function update_child_levels() { |
|
155 | - if (is_array($this->children)) { |
|
156 | - foreach( $this->children as $child ) { |
|
157 | - $child->level = $this->level + 1; |
|
158 | - $child->update_child_levels(); |
|
159 | - } |
|
160 | - return true; |
|
161 | - } |
|
162 | - } |
|
149 | + /** |
|
150 | + * |
|
151 | + * @internal |
|
152 | + * @return bool |
|
153 | + */ |
|
154 | + function update_child_levels() { |
|
155 | + if (is_array($this->children)) { |
|
156 | + foreach( $this->children as $child ) { |
|
157 | + $child->level = $this->level + 1; |
|
158 | + $child->update_child_levels(); |
|
159 | + } |
|
160 | + return true; |
|
161 | + } |
|
162 | + } |
|
163 | 163 | |
164 | - /** |
|
165 | - * Imports the classes to be used in CSS |
|
166 | - * @internal |
|
167 | - * @param array|object $data |
|
168 | - */ |
|
169 | - function import_classes( $data ) { |
|
170 | - if ( is_array($data) ) { |
|
171 | - $data = (object) $data; |
|
172 | - } |
|
173 | - $this->classes = array_merge( $this->classes, $data->classes ); |
|
174 | - $this->classes = array_unique( $this->classes ); |
|
175 | - $this->classes = apply_filters( 'nav_menu_css_class', $this->classes, $this ); |
|
176 | - $this->class = trim( implode( ' ', $this->classes ) ); |
|
177 | - } |
|
164 | + /** |
|
165 | + * Imports the classes to be used in CSS |
|
166 | + * @internal |
|
167 | + * @param array|object $data |
|
168 | + */ |
|
169 | + function import_classes( $data ) { |
|
170 | + if ( is_array($data) ) { |
|
171 | + $data = (object) $data; |
|
172 | + } |
|
173 | + $this->classes = array_merge( $this->classes, $data->classes ); |
|
174 | + $this->classes = array_unique( $this->classes ); |
|
175 | + $this->classes = apply_filters( 'nav_menu_css_class', $this->classes, $this ); |
|
176 | + $this->class = trim( implode( ' ', $this->classes ) ); |
|
177 | + } |
|
178 | 178 | |
179 | - /** |
|
180 | - * |
|
181 | - * @internal |
|
182 | - * @return array|bool |
|
183 | - */ |
|
184 | - function get_children() { |
|
185 | - if ( isset( $this->children ) ) { |
|
186 | - return $this->children; |
|
187 | - } |
|
188 | - return false; |
|
189 | - } |
|
179 | + /** |
|
180 | + * |
|
181 | + * @internal |
|
182 | + * @return array|bool |
|
183 | + */ |
|
184 | + function get_children() { |
|
185 | + if ( isset( $this->children ) ) { |
|
186 | + return $this->children; |
|
187 | + } |
|
188 | + return false; |
|
189 | + } |
|
190 | 190 | |
191 | - /** |
|
192 | - * Checks to see if the menu item is an external link so if my site is `example.org`, `google.com/whatever` is an external link. Helpful when creating rules for the target of a link |
|
193 | - * @api |
|
194 | - * @example |
|
195 | - * ```twig |
|
196 | - * <a href="{{ item.link }}" target="{{ item.is_external ? '_blank' : '_self' }}"> |
|
197 | - * ``` |
|
198 | - * @return bool |
|
199 | - */ |
|
200 | - function is_external() { |
|
201 | - if ( $this->type != 'custom' ) { |
|
202 | - return false; |
|
203 | - } |
|
204 | - return TimberURLHelper::is_external( $this->url ); |
|
205 | - } |
|
191 | + /** |
|
192 | + * Checks to see if the menu item is an external link so if my site is `example.org`, `google.com/whatever` is an external link. Helpful when creating rules for the target of a link |
|
193 | + * @api |
|
194 | + * @example |
|
195 | + * ```twig |
|
196 | + * <a href="{{ item.link }}" target="{{ item.is_external ? '_blank' : '_self' }}"> |
|
197 | + * ``` |
|
198 | + * @return bool |
|
199 | + */ |
|
200 | + function is_external() { |
|
201 | + if ( $this->type != 'custom' ) { |
|
202 | + return false; |
|
203 | + } |
|
204 | + return TimberURLHelper::is_external( $this->url ); |
|
205 | + } |
|
206 | 206 | |
207 | - /** |
|
208 | - * @param string $key lookup key |
|
209 | - * @return mixed whatever value is storied in the database |
|
210 | - */ |
|
211 | - public function meta( $key ) { |
|
212 | - if ( is_object( $this->menu_object ) && method_exists( $this->menu_object, 'meta' ) ) { |
|
213 | - return $this->menu_object->meta( $key ); |
|
214 | - } |
|
215 | - if ( isset( $this->$key ) ) { |
|
216 | - return $this->$key; |
|
217 | - } |
|
218 | - } |
|
207 | + /** |
|
208 | + * @param string $key lookup key |
|
209 | + * @return mixed whatever value is storied in the database |
|
210 | + */ |
|
211 | + public function meta( $key ) { |
|
212 | + if ( is_object( $this->menu_object ) && method_exists( $this->menu_object, 'meta' ) ) { |
|
213 | + return $this->menu_object->meta( $key ); |
|
214 | + } |
|
215 | + if ( isset( $this->$key ) ) { |
|
216 | + return $this->$key; |
|
217 | + } |
|
218 | + } |
|
219 | 219 | |
220 | - /* Aliases */ |
|
220 | + /* Aliases */ |
|
221 | 221 | |
222 | - /** |
|
223 | - * Get the child [TimberMenuItems](#TimberMenuItem)s of a [TimberMenuItem](#TimberMenuItem) |
|
224 | - * @api |
|
225 | - * @return array|bool |
|
226 | - */ |
|
227 | - public function children() { |
|
228 | - return $this->get_children(); |
|
229 | - } |
|
222 | + /** |
|
223 | + * Get the child [TimberMenuItems](#TimberMenuItem)s of a [TimberMenuItem](#TimberMenuItem) |
|
224 | + * @api |
|
225 | + * @return array|bool |
|
226 | + */ |
|
227 | + public function children() { |
|
228 | + return $this->get_children(); |
|
229 | + } |
|
230 | 230 | |
231 | - /** |
|
232 | - * Checks to see if a link is external, helpful when creating rules for the target of a link |
|
233 | - * @see TimberMenuItem::is_external |
|
234 | - * @return bool |
|
235 | - */ |
|
236 | - public function external() { |
|
237 | - return $this->is_external(); |
|
238 | - } |
|
231 | + /** |
|
232 | + * Checks to see if a link is external, helpful when creating rules for the target of a link |
|
233 | + * @see TimberMenuItem::is_external |
|
234 | + * @return bool |
|
235 | + */ |
|
236 | + public function external() { |
|
237 | + return $this->is_external(); |
|
238 | + } |
|
239 | 239 | |
240 | - /** |
|
241 | - * Get the full link to a Menu Item |
|
242 | - * @api |
|
243 | - * @example |
|
244 | - * ```twig |
|
245 | - * {% for item in menu.items %} |
|
246 | - * <li><a href="{{ item.link }}">{{ item.title }}</a></li> |
|
247 | - * {% endfor %} |
|
248 | - * ``` |
|
249 | - * @return string a full URL like http://mysite.com/thing/ |
|
250 | - */ |
|
251 | - public function link() { |
|
252 | - return $this->get_link(); |
|
253 | - } |
|
240 | + /** |
|
241 | + * Get the full link to a Menu Item |
|
242 | + * @api |
|
243 | + * @example |
|
244 | + * ```twig |
|
245 | + * {% for item in menu.items %} |
|
246 | + * <li><a href="{{ item.link }}">{{ item.title }}</a></li> |
|
247 | + * {% endfor %} |
|
248 | + * ``` |
|
249 | + * @return string a full URL like http://mysite.com/thing/ |
|
250 | + */ |
|
251 | + public function link() { |
|
252 | + return $this->get_link(); |
|
253 | + } |
|
254 | 254 | |
255 | - /** |
|
256 | - * Return the relative path of a Menu Item's link |
|
257 | - * @example |
|
258 | - * ```twig |
|
259 | - * {% for item in menu.items %} |
|
260 | - * <li><a href="{{ item.path }}">{{ item.title }}</a></li> |
|
261 | - * {% endfor %} |
|
262 | - * ``` |
|
263 | - * @see get_path() |
|
264 | - * @return string the path of a URL like /foo |
|
265 | - */ |
|
266 | - public function path() { |
|
267 | - return $this->get_path(); |
|
268 | - } |
|
255 | + /** |
|
256 | + * Return the relative path of a Menu Item's link |
|
257 | + * @example |
|
258 | + * ```twig |
|
259 | + * {% for item in menu.items %} |
|
260 | + * <li><a href="{{ item.path }}">{{ item.title }}</a></li> |
|
261 | + * {% endfor %} |
|
262 | + * ``` |
|
263 | + * @see get_path() |
|
264 | + * @return string the path of a URL like /foo |
|
265 | + */ |
|
266 | + public function path() { |
|
267 | + return $this->get_path(); |
|
268 | + } |
|
269 | 269 | |
270 | - /** |
|
271 | - * Gets the link a menu item points at |
|
272 | - * @internal |
|
273 | - * @deprecated since 0.21.7 use link instead |
|
274 | - * @see link() |
|
275 | - * @return string a full URL like http://mysite.com/thing/ |
|
276 | - */ |
|
277 | - public function permalink() { |
|
278 | - return $this->get_link(); |
|
279 | - } |
|
270 | + /** |
|
271 | + * Gets the link a menu item points at |
|
272 | + * @internal |
|
273 | + * @deprecated since 0.21.7 use link instead |
|
274 | + * @see link() |
|
275 | + * @return string a full URL like http://mysite.com/thing/ |
|
276 | + */ |
|
277 | + public function permalink() { |
|
278 | + return $this->get_link(); |
|
279 | + } |
|
280 | 280 | |
281 | - /** |
|
282 | - * @internal |
|
283 | - * @deprecated since 0.21.7, use link instead |
|
284 | - * @see link() |
|
285 | - * @return string a full URL like http://mysite.com/thing/ |
|
286 | - */ |
|
287 | - public function get_permalink() { |
|
288 | - return $this->get_link(); |
|
289 | - } |
|
281 | + /** |
|
282 | + * @internal |
|
283 | + * @deprecated since 0.21.7, use link instead |
|
284 | + * @see link() |
|
285 | + * @return string a full URL like http://mysite.com/thing/ |
|
286 | + */ |
|
287 | + public function get_permalink() { |
|
288 | + return $this->get_link(); |
|
289 | + } |
|
290 | 290 | |
291 | - /** |
|
292 | - * Gets the public label for the menu item |
|
293 | - * @example |
|
294 | - * ```twig |
|
295 | - * {% for item in menu.items %} |
|
296 | - * <li><a href="{{ item.link }}">{{ item.title }}</a></li> |
|
297 | - * {% endfor %} |
|
298 | - * ``` |
|
299 | - * @return string the public label like Foo |
|
300 | - */ |
|
301 | - public function title() { |
|
302 | - if ( isset( $this->__title ) ) { |
|
303 | - return $this->__title; |
|
304 | - } |
|
305 | - } |
|
291 | + /** |
|
292 | + * Gets the public label for the menu item |
|
293 | + * @example |
|
294 | + * ```twig |
|
295 | + * {% for item in menu.items %} |
|
296 | + * <li><a href="{{ item.link }}">{{ item.title }}</a></li> |
|
297 | + * {% endfor %} |
|
298 | + * ``` |
|
299 | + * @return string the public label like Foo |
|
300 | + */ |
|
301 | + public function title() { |
|
302 | + if ( isset( $this->__title ) ) { |
|
303 | + return $this->__title; |
|
304 | + } |
|
305 | + } |
|
306 | 306 | |
307 | 307 | } |
@@ -26,13 +26,13 @@ discard block |
||
26 | 26 | */ |
27 | 27 | public function __construct( $data ) { |
28 | 28 | $data = (object) $data; |
29 | - $this->import( $data ); |
|
30 | - $this->import_classes( $data ); |
|
31 | - if ( isset( $this->name ) ) { |
|
29 | + $this->import($data); |
|
30 | + $this->import_classes($data); |
|
31 | + if ( isset($this->name) ) { |
|
32 | 32 | $this->_name = $this->name; |
33 | 33 | } |
34 | 34 | $this->name = $this->name(); |
35 | - $this->add_class( 'menu-item-' . $this->ID ); |
|
35 | + $this->add_class('menu-item-' . $this->ID); |
|
36 | 36 | $this->menu_object = $data; |
37 | 37 | } |
38 | 38 | |
@@ -61,7 +61,7 @@ discard block |
||
61 | 61 | if ( $title = $this->title() ) { |
62 | 62 | return $title; |
63 | 63 | } |
64 | - if ( isset( $this->_name ) ) { |
|
64 | + if ( isset($this->_name) ) { |
|
65 | 65 | return $this->_name; |
66 | 66 | } |
67 | 67 | return ''; |
@@ -82,10 +82,10 @@ discard block |
||
82 | 82 | * @return string the slug of the menu item kinda-like-this |
83 | 83 | */ |
84 | 84 | public function slug() { |
85 | - if ( !isset( $this->master_object ) ) { |
|
85 | + if ( !isset($this->master_object) ) { |
|
86 | 86 | $this->master_object = $this->get_master_object(); |
87 | 87 | } |
88 | - if ( isset( $this->master_object->post_name ) && $this->master_object->post_name ) { |
|
88 | + if ( isset($this->master_object->post_name) && $this->master_object->post_name ) { |
|
89 | 89 | return $this->master_object->post_name; |
90 | 90 | } |
91 | 91 | return $this->post_name; |
@@ -96,8 +96,8 @@ discard block |
||
96 | 96 | * @return mixed whatever object (Post, Term, etc.) the menu item represents |
97 | 97 | */ |
98 | 98 | protected function get_master_object() { |
99 | - if ( isset( $this->_menu_item_object_id ) ) { |
|
100 | - return new $this->PostClass( $this->_menu_item_object_id ); |
|
99 | + if ( isset($this->_menu_item_object_id) ) { |
|
100 | + return new $this->PostClass($this->_menu_item_object_id); |
|
101 | 101 | } |
102 | 102 | } |
103 | 103 | |
@@ -107,10 +107,10 @@ discard block |
||
107 | 107 | * @return string an absolute URL http://example.org/my-page |
108 | 108 | */ |
109 | 109 | function get_link() { |
110 | - if ( !isset( $this->url ) || !$this->url ) { |
|
111 | - if ( isset( $this->_menu_item_type ) && $this->_menu_item_type == 'custom' ) { |
|
110 | + if ( !isset($this->url) || !$this->url ) { |
|
111 | + if ( isset($this->_menu_item_type) && $this->_menu_item_type == 'custom' ) { |
|
112 | 112 | $this->url = $this->_menu_item_url; |
113 | - } else if ( isset( $this->menu_object ) && method_exists( $this->menu_object, 'get_link' ) ) { |
|
113 | + } else if ( isset($this->menu_object) && method_exists($this->menu_object, 'get_link') ) { |
|
114 | 114 | $this->url = $this->menu_object->get_link(); |
115 | 115 | } |
116 | 116 | } |
@@ -123,7 +123,7 @@ discard block |
||
123 | 123 | * @return string a relative url /my-page |
124 | 124 | */ |
125 | 125 | function get_path() { |
126 | - return TimberURLHelper::get_rel_url( $this->get_link() ); |
|
126 | + return TimberURLHelper::get_rel_url($this->get_link()); |
|
127 | 127 | } |
128 | 128 | |
129 | 129 | /** |
@@ -133,15 +133,15 @@ discard block |
||
133 | 133 | */ |
134 | 134 | function add_child( $item ) { |
135 | 135 | if ( !$this->has_child_class ) { |
136 | - $this->add_class( 'menu-item-has-children' ); |
|
136 | + $this->add_class('menu-item-has-children'); |
|
137 | 137 | $this->has_child_class = true; |
138 | 138 | } |
139 | - if ( !isset( $this->children ) ) { |
|
139 | + if ( !isset($this->children) ) { |
|
140 | 140 | $this->children = array(); |
141 | 141 | } |
142 | 142 | $this->children[] = $item; |
143 | 143 | $item->level = $this->level + 1; |
144 | - if ($item->children) { |
|
144 | + if ( $item->children ) { |
|
145 | 145 | $this->update_child_levels(); |
146 | 146 | } |
147 | 147 | } |
@@ -152,8 +152,8 @@ discard block |
||
152 | 152 | * @return bool |
153 | 153 | */ |
154 | 154 | function update_child_levels() { |
155 | - if (is_array($this->children)) { |
|
156 | - foreach( $this->children as $child ) { |
|
155 | + if ( is_array($this->children) ) { |
|
156 | + foreach ( $this->children as $child ) { |
|
157 | 157 | $child->level = $this->level + 1; |
158 | 158 | $child->update_child_levels(); |
159 | 159 | } |
@@ -170,10 +170,10 @@ discard block |
||
170 | 170 | if ( is_array($data) ) { |
171 | 171 | $data = (object) $data; |
172 | 172 | } |
173 | - $this->classes = array_merge( $this->classes, $data->classes ); |
|
174 | - $this->classes = array_unique( $this->classes ); |
|
175 | - $this->classes = apply_filters( 'nav_menu_css_class', $this->classes, $this ); |
|
176 | - $this->class = trim( implode( ' ', $this->classes ) ); |
|
173 | + $this->classes = array_merge($this->classes, $data->classes); |
|
174 | + $this->classes = array_unique($this->classes); |
|
175 | + $this->classes = apply_filters('nav_menu_css_class', $this->classes, $this); |
|
176 | + $this->class = trim(implode(' ', $this->classes)); |
|
177 | 177 | } |
178 | 178 | |
179 | 179 | /** |
@@ -182,7 +182,7 @@ discard block |
||
182 | 182 | * @return array|bool |
183 | 183 | */ |
184 | 184 | function get_children() { |
185 | - if ( isset( $this->children ) ) { |
|
185 | + if ( isset($this->children) ) { |
|
186 | 186 | return $this->children; |
187 | 187 | } |
188 | 188 | return false; |
@@ -201,7 +201,7 @@ discard block |
||
201 | 201 | if ( $this->type != 'custom' ) { |
202 | 202 | return false; |
203 | 203 | } |
204 | - return TimberURLHelper::is_external( $this->url ); |
|
204 | + return TimberURLHelper::is_external($this->url); |
|
205 | 205 | } |
206 | 206 | |
207 | 207 | /** |
@@ -209,10 +209,10 @@ discard block |
||
209 | 209 | * @return mixed whatever value is storied in the database |
210 | 210 | */ |
211 | 211 | public function meta( $key ) { |
212 | - if ( is_object( $this->menu_object ) && method_exists( $this->menu_object, 'meta' ) ) { |
|
213 | - return $this->menu_object->meta( $key ); |
|
212 | + if ( is_object($this->menu_object) && method_exists($this->menu_object, 'meta') ) { |
|
213 | + return $this->menu_object->meta($key); |
|
214 | 214 | } |
215 | - if ( isset( $this->$key ) ) { |
|
215 | + if ( isset($this->$key) ) { |
|
216 | 216 | return $this->$key; |
217 | 217 | } |
218 | 218 | } |
@@ -299,7 +299,7 @@ discard block |
||
299 | 299 | * @return string the public label like Foo |
300 | 300 | */ |
301 | 301 | public function title() { |
302 | - if ( isset( $this->__title ) ) { |
|
302 | + if ( isset($this->__title) ) { |
|
303 | 303 | return $this->__title; |
304 | 304 | } |
305 | 305 | } |
@@ -73,7 +73,7 @@ |
||
73 | 73 | public $title; |
74 | 74 | |
75 | 75 | /** |
76 | - * @param int|string $slug |
|
76 | + * @param integer $slug |
|
77 | 77 | */ |
78 | 78 | function __construct($slug = 0) { |
79 | 79 | $locations = get_nav_menu_locations(); |
@@ -101,7 +101,7 @@ discard block |
||
101 | 101 | $menu = wp_get_nav_menu_items($menu_id); |
102 | 102 | if ($menu) { |
103 | 103 | _wp_menu_item_classes_by_context($menu); |
104 | - if (is_array($menu)){ |
|
104 | + if (is_array($menu)) { |
|
105 | 105 | $menu = self::order_children($menu); |
106 | 106 | } |
107 | 107 | $this->items = $menu; |
@@ -123,7 +123,7 @@ discard block |
||
123 | 123 | $mi->__title = $mi->post_title; |
124 | 124 | } |
125 | 125 | _wp_menu_item_classes_by_context($menu); |
126 | - if (is_array($menu)){ |
|
126 | + if (is_array($menu)) { |
|
127 | 127 | $menu = self::order_children($menu); |
128 | 128 | } |
129 | 129 | $this->items = $menu; |
@@ -201,13 +201,13 @@ discard block |
||
201 | 201 | $item->__title = $item->title; |
202 | 202 | unset($item->title); |
203 | 203 | } |
204 | - if(isset($item->ID)){ |
|
205 | - if (is_object($item) && get_class($item) == 'WP_Post'){ |
|
204 | + if(isset($item->ID)) { |
|
205 | + if (is_object($item) && get_class($item) == 'WP_Post') { |
|
206 | 206 | $old_menu_item = $item; |
207 | 207 | $item = new $this->PostClass($item); |
208 | 208 | } |
209 | 209 | $menu_item = new $this->MenuItemClass($item); |
210 | - if (isset($old_menu_item)){ |
|
210 | + if (isset($old_menu_item)) { |
|
211 | 211 | $menu_item->import_classes($old_menu_item); |
212 | 212 | } |
213 | 213 | $index[$item->ID] = $menu_item; |
@@ -43,196 +43,196 @@ |
||
43 | 43 | */ |
44 | 44 | class TimberMenu extends TimberCore { |
45 | 45 | |
46 | - public $MenuItemClass = 'TimberMenuItem'; |
|
47 | - public $PostClass = 'TimberPost'; |
|
46 | + public $MenuItemClass = 'TimberMenuItem'; |
|
47 | + public $PostClass = 'TimberPost'; |
|
48 | 48 | |
49 | - /** |
|
50 | - * @api |
|
51 | - * @var TimberMenuItem[]|null $items you need to iterate through |
|
52 | - */ |
|
53 | - public $items = null; |
|
54 | - /** |
|
55 | - * @api |
|
56 | - * @var integer $id the ID# of the menu, corresponding to the wp_terms table |
|
57 | - */ |
|
58 | - public $id; |
|
59 | - public $ID; |
|
60 | - /** |
|
61 | - * @api |
|
62 | - * @var string $name of the menu (ex: `Main Navigation`) |
|
63 | - */ |
|
64 | - public $name; |
|
65 | - /** |
|
66 | - * @var integer $id the ID# of the menu, corresponding to the wp_terms table |
|
67 | - */ |
|
68 | - public $term_id; |
|
69 | - /** |
|
70 | - * @api |
|
71 | - * @var string $name of the menu (ex: `Main Navigation`) |
|
72 | - */ |
|
73 | - public $title; |
|
49 | + /** |
|
50 | + * @api |
|
51 | + * @var TimberMenuItem[]|null $items you need to iterate through |
|
52 | + */ |
|
53 | + public $items = null; |
|
54 | + /** |
|
55 | + * @api |
|
56 | + * @var integer $id the ID# of the menu, corresponding to the wp_terms table |
|
57 | + */ |
|
58 | + public $id; |
|
59 | + public $ID; |
|
60 | + /** |
|
61 | + * @api |
|
62 | + * @var string $name of the menu (ex: `Main Navigation`) |
|
63 | + */ |
|
64 | + public $name; |
|
65 | + /** |
|
66 | + * @var integer $id the ID# of the menu, corresponding to the wp_terms table |
|
67 | + */ |
|
68 | + public $term_id; |
|
69 | + /** |
|
70 | + * @api |
|
71 | + * @var string $name of the menu (ex: `Main Navigation`) |
|
72 | + */ |
|
73 | + public $title; |
|
74 | 74 | |
75 | - /** |
|
76 | - * @param int|string $slug |
|
77 | - */ |
|
78 | - function __construct($slug = 0) { |
|
79 | - $locations = get_nav_menu_locations(); |
|
80 | - if ($slug != 0 && is_numeric($slug)) { |
|
81 | - $menu_id = $slug; |
|
82 | - } else if (is_array($locations) && count($locations)) { |
|
83 | - $menu_id = $this->get_menu_id_from_locations($slug, $locations); |
|
84 | - } else if ($slug === false) { |
|
85 | - $menu_id = false; |
|
86 | - } else { |
|
87 | - $menu_id = $this->get_menu_id_from_terms($slug); |
|
88 | - } |
|
89 | - if ($menu_id) { |
|
90 | - $this->init($menu_id); |
|
91 | - } else { |
|
92 | - $this->init_as_page_menu(); |
|
93 | - } |
|
94 | - } |
|
75 | + /** |
|
76 | + * @param int|string $slug |
|
77 | + */ |
|
78 | + function __construct($slug = 0) { |
|
79 | + $locations = get_nav_menu_locations(); |
|
80 | + if ($slug != 0 && is_numeric($slug)) { |
|
81 | + $menu_id = $slug; |
|
82 | + } else if (is_array($locations) && count($locations)) { |
|
83 | + $menu_id = $this->get_menu_id_from_locations($slug, $locations); |
|
84 | + } else if ($slug === false) { |
|
85 | + $menu_id = false; |
|
86 | + } else { |
|
87 | + $menu_id = $this->get_menu_id_from_terms($slug); |
|
88 | + } |
|
89 | + if ($menu_id) { |
|
90 | + $this->init($menu_id); |
|
91 | + } else { |
|
92 | + $this->init_as_page_menu(); |
|
93 | + } |
|
94 | + } |
|
95 | 95 | |
96 | - /** |
|
97 | - * @internal |
|
98 | - * @param int $menu_id |
|
99 | - */ |
|
100 | - protected function init($menu_id) { |
|
101 | - $menu = wp_get_nav_menu_items($menu_id); |
|
102 | - if ($menu) { |
|
103 | - _wp_menu_item_classes_by_context($menu); |
|
104 | - if (is_array($menu)){ |
|
105 | - $menu = self::order_children($menu); |
|
106 | - } |
|
107 | - $this->items = $menu; |
|
108 | - $menu_info = wp_get_nav_menu_object($menu_id); |
|
109 | - $this->import($menu_info); |
|
110 | - $this->ID = $this->term_id; |
|
111 | - $this->id = $this->term_id; |
|
112 | - $this->title = $this->name; |
|
113 | - } |
|
114 | - } |
|
96 | + /** |
|
97 | + * @internal |
|
98 | + * @param int $menu_id |
|
99 | + */ |
|
100 | + protected function init($menu_id) { |
|
101 | + $menu = wp_get_nav_menu_items($menu_id); |
|
102 | + if ($menu) { |
|
103 | + _wp_menu_item_classes_by_context($menu); |
|
104 | + if (is_array($menu)){ |
|
105 | + $menu = self::order_children($menu); |
|
106 | + } |
|
107 | + $this->items = $menu; |
|
108 | + $menu_info = wp_get_nav_menu_object($menu_id); |
|
109 | + $this->import($menu_info); |
|
110 | + $this->ID = $this->term_id; |
|
111 | + $this->id = $this->term_id; |
|
112 | + $this->title = $this->name; |
|
113 | + } |
|
114 | + } |
|
115 | 115 | |
116 | - /** |
|
117 | - * @internal |
|
118 | - */ |
|
119 | - protected function init_as_page_menu() { |
|
120 | - $menu = get_pages(); |
|
121 | - if ($menu) { |
|
122 | - foreach($menu as $mi) { |
|
123 | - $mi->__title = $mi->post_title; |
|
124 | - } |
|
125 | - _wp_menu_item_classes_by_context($menu); |
|
126 | - if (is_array($menu)){ |
|
127 | - $menu = self::order_children($menu); |
|
128 | - } |
|
129 | - $this->items = $menu; |
|
130 | - } |
|
131 | - } |
|
116 | + /** |
|
117 | + * @internal |
|
118 | + */ |
|
119 | + protected function init_as_page_menu() { |
|
120 | + $menu = get_pages(); |
|
121 | + if ($menu) { |
|
122 | + foreach($menu as $mi) { |
|
123 | + $mi->__title = $mi->post_title; |
|
124 | + } |
|
125 | + _wp_menu_item_classes_by_context($menu); |
|
126 | + if (is_array($menu)){ |
|
127 | + $menu = self::order_children($menu); |
|
128 | + } |
|
129 | + $this->items = $menu; |
|
130 | + } |
|
131 | + } |
|
132 | 132 | |
133 | - /** |
|
134 | - * @internal |
|
135 | - * @param string $slug |
|
136 | - * @param array $locations |
|
137 | - * @return integer |
|
138 | - */ |
|
139 | - protected function get_menu_id_from_locations($slug, $locations) { |
|
140 | - if ($slug === 0) { |
|
141 | - $slug = $this->get_menu_id_from_terms($slug); |
|
142 | - } |
|
143 | - if (is_numeric($slug)) { |
|
144 | - $slug = array_search($slug, $locations); |
|
145 | - } |
|
146 | - if (isset($locations[$slug])) { |
|
147 | - $menu_id = $locations[$slug]; |
|
148 | - return $menu_id; |
|
149 | - } |
|
150 | - } |
|
133 | + /** |
|
134 | + * @internal |
|
135 | + * @param string $slug |
|
136 | + * @param array $locations |
|
137 | + * @return integer |
|
138 | + */ |
|
139 | + protected function get_menu_id_from_locations($slug, $locations) { |
|
140 | + if ($slug === 0) { |
|
141 | + $slug = $this->get_menu_id_from_terms($slug); |
|
142 | + } |
|
143 | + if (is_numeric($slug)) { |
|
144 | + $slug = array_search($slug, $locations); |
|
145 | + } |
|
146 | + if (isset($locations[$slug])) { |
|
147 | + $menu_id = $locations[$slug]; |
|
148 | + return $menu_id; |
|
149 | + } |
|
150 | + } |
|
151 | 151 | |
152 | - /** |
|
153 | - * @internal |
|
154 | - * @param int $slug |
|
155 | - * @return int |
|
156 | - */ |
|
157 | - protected function get_menu_id_from_terms($slug = 0) { |
|
158 | - if (!is_numeric($slug) && is_string($slug)) { |
|
159 | - //we have a string so lets search for that |
|
160 | - $menu_id = get_term_by('slug', $slug, 'nav_menu'); |
|
161 | - if ($menu_id) { |
|
162 | - return $menu_id; |
|
163 | - } |
|
164 | - $menu_id = get_term_by('name', $slug, 'nav_menu'); |
|
165 | - if ($menu_id) { |
|
166 | - return $menu_id; |
|
167 | - } |
|
168 | - } |
|
169 | - $menus = get_terms('nav_menu', array('hide_empty' => true)); |
|
170 | - if (is_array($menus) && count($menus)) { |
|
171 | - if (isset($menus[0]->term_id)) { |
|
172 | - return $menus[0]->term_id; |
|
173 | - } |
|
174 | - } |
|
175 | - return 0; |
|
176 | - } |
|
152 | + /** |
|
153 | + * @internal |
|
154 | + * @param int $slug |
|
155 | + * @return int |
|
156 | + */ |
|
157 | + protected function get_menu_id_from_terms($slug = 0) { |
|
158 | + if (!is_numeric($slug) && is_string($slug)) { |
|
159 | + //we have a string so lets search for that |
|
160 | + $menu_id = get_term_by('slug', $slug, 'nav_menu'); |
|
161 | + if ($menu_id) { |
|
162 | + return $menu_id; |
|
163 | + } |
|
164 | + $menu_id = get_term_by('name', $slug, 'nav_menu'); |
|
165 | + if ($menu_id) { |
|
166 | + return $menu_id; |
|
167 | + } |
|
168 | + } |
|
169 | + $menus = get_terms('nav_menu', array('hide_empty' => true)); |
|
170 | + if (is_array($menus) && count($menus)) { |
|
171 | + if (isset($menus[0]->term_id)) { |
|
172 | + return $menus[0]->term_id; |
|
173 | + } |
|
174 | + } |
|
175 | + return 0; |
|
176 | + } |
|
177 | 177 | |
178 | - /** |
|
179 | - * @param array $menu_items |
|
180 | - * @param int $parent_id |
|
181 | - * @return TimberMenuItem|null |
|
182 | - */ |
|
183 | - function find_parent_item_in_menu($menu_items, $parent_id) { |
|
184 | - foreach ($menu_items as &$item) { |
|
185 | - if ($item->ID == $parent_id) { |
|
186 | - return $item; |
|
187 | - } |
|
188 | - } |
|
189 | - } |
|
178 | + /** |
|
179 | + * @param array $menu_items |
|
180 | + * @param int $parent_id |
|
181 | + * @return TimberMenuItem|null |
|
182 | + */ |
|
183 | + function find_parent_item_in_menu($menu_items, $parent_id) { |
|
184 | + foreach ($menu_items as &$item) { |
|
185 | + if ($item->ID == $parent_id) { |
|
186 | + return $item; |
|
187 | + } |
|
188 | + } |
|
189 | + } |
|
190 | 190 | |
191 | - /** |
|
192 | - * @internal |
|
193 | - * @param array $items |
|
194 | - * @return array |
|
195 | - */ |
|
196 | - protected function order_children($items) { |
|
197 | - $index = array(); |
|
198 | - $menu = array(); |
|
199 | - foreach ($items as $item) { |
|
200 | - if (isset($item->title)) { |
|
201 | - //items from wp can come with a $title property which conflicts with methods |
|
202 | - $item->__title = $item->title; |
|
203 | - unset($item->title); |
|
204 | - } |
|
205 | - if(isset($item->ID)){ |
|
206 | - if (is_object($item) && get_class($item) == 'WP_Post'){ |
|
207 | - $old_menu_item = $item; |
|
208 | - $item = new $this->PostClass($item); |
|
209 | - } |
|
210 | - $menu_item = new $this->MenuItemClass($item); |
|
211 | - if (isset($old_menu_item)){ |
|
212 | - $menu_item->import_classes($old_menu_item); |
|
213 | - } |
|
214 | - $index[$item->ID] = $menu_item; |
|
215 | - } |
|
216 | - } |
|
217 | - foreach ($index as $item) { |
|
218 | - if (isset($item->menu_item_parent) && $item->menu_item_parent && isset($index[$item->menu_item_parent])) { |
|
219 | - $index[$item->menu_item_parent]->add_child($item); |
|
220 | - } else { |
|
221 | - $menu[] = $item; |
|
222 | - } |
|
223 | - } |
|
224 | - return $menu; |
|
225 | - } |
|
191 | + /** |
|
192 | + * @internal |
|
193 | + * @param array $items |
|
194 | + * @return array |
|
195 | + */ |
|
196 | + protected function order_children($items) { |
|
197 | + $index = array(); |
|
198 | + $menu = array(); |
|
199 | + foreach ($items as $item) { |
|
200 | + if (isset($item->title)) { |
|
201 | + //items from wp can come with a $title property which conflicts with methods |
|
202 | + $item->__title = $item->title; |
|
203 | + unset($item->title); |
|
204 | + } |
|
205 | + if(isset($item->ID)){ |
|
206 | + if (is_object($item) && get_class($item) == 'WP_Post'){ |
|
207 | + $old_menu_item = $item; |
|
208 | + $item = new $this->PostClass($item); |
|
209 | + } |
|
210 | + $menu_item = new $this->MenuItemClass($item); |
|
211 | + if (isset($old_menu_item)){ |
|
212 | + $menu_item->import_classes($old_menu_item); |
|
213 | + } |
|
214 | + $index[$item->ID] = $menu_item; |
|
215 | + } |
|
216 | + } |
|
217 | + foreach ($index as $item) { |
|
218 | + if (isset($item->menu_item_parent) && $item->menu_item_parent && isset($index[$item->menu_item_parent])) { |
|
219 | + $index[$item->menu_item_parent]->add_child($item); |
|
220 | + } else { |
|
221 | + $menu[] = $item; |
|
222 | + } |
|
223 | + } |
|
224 | + return $menu; |
|
225 | + } |
|
226 | 226 | |
227 | - /** |
|
228 | - * @return array |
|
229 | - */ |
|
230 | - function get_items() { |
|
231 | - if (is_array($this->items)) { |
|
232 | - return $this->items; |
|
233 | - } |
|
234 | - return array(); |
|
235 | - } |
|
227 | + /** |
|
228 | + * @return array |
|
229 | + */ |
|
230 | + function get_items() { |
|
231 | + if (is_array($this->items)) { |
|
232 | + return $this->items; |
|
233 | + } |
|
234 | + return array(); |
|
235 | + } |
|
236 | 236 | } |
237 | 237 | |
238 | 238 |
@@ -75,18 +75,18 @@ discard block |
||
75 | 75 | /** |
76 | 76 | * @param int|string $slug |
77 | 77 | */ |
78 | - function __construct($slug = 0) { |
|
78 | + function __construct( $slug = 0 ) { |
|
79 | 79 | $locations = get_nav_menu_locations(); |
80 | - if ($slug != 0 && is_numeric($slug)) { |
|
80 | + if ( $slug != 0 && is_numeric($slug) ) { |
|
81 | 81 | $menu_id = $slug; |
82 | - } else if (is_array($locations) && count($locations)) { |
|
82 | + } else if ( is_array($locations) && count($locations) ) { |
|
83 | 83 | $menu_id = $this->get_menu_id_from_locations($slug, $locations); |
84 | - } else if ($slug === false) { |
|
84 | + } else if ( $slug === false ) { |
|
85 | 85 | $menu_id = false; |
86 | 86 | } else { |
87 | 87 | $menu_id = $this->get_menu_id_from_terms($slug); |
88 | 88 | } |
89 | - if ($menu_id) { |
|
89 | + if ( $menu_id ) { |
|
90 | 90 | $this->init($menu_id); |
91 | 91 | } else { |
92 | 92 | $this->init_as_page_menu(); |
@@ -97,11 +97,11 @@ discard block |
||
97 | 97 | * @internal |
98 | 98 | * @param int $menu_id |
99 | 99 | */ |
100 | - protected function init($menu_id) { |
|
100 | + protected function init( $menu_id ) { |
|
101 | 101 | $menu = wp_get_nav_menu_items($menu_id); |
102 | - if ($menu) { |
|
102 | + if ( $menu ) { |
|
103 | 103 | _wp_menu_item_classes_by_context($menu); |
104 | - if (is_array($menu)){ |
|
104 | + if ( is_array($menu) ) { |
|
105 | 105 | $menu = self::order_children($menu); |
106 | 106 | } |
107 | 107 | $this->items = $menu; |
@@ -118,12 +118,12 @@ discard block |
||
118 | 118 | */ |
119 | 119 | protected function init_as_page_menu() { |
120 | 120 | $menu = get_pages(); |
121 | - if ($menu) { |
|
122 | - foreach($menu as $mi) { |
|
121 | + if ( $menu ) { |
|
122 | + foreach ( $menu as $mi ) { |
|
123 | 123 | $mi->__title = $mi->post_title; |
124 | 124 | } |
125 | 125 | _wp_menu_item_classes_by_context($menu); |
126 | - if (is_array($menu)){ |
|
126 | + if ( is_array($menu) ) { |
|
127 | 127 | $menu = self::order_children($menu); |
128 | 128 | } |
129 | 129 | $this->items = $menu; |
@@ -136,14 +136,14 @@ discard block |
||
136 | 136 | * @param array $locations |
137 | 137 | * @return integer |
138 | 138 | */ |
139 | - protected function get_menu_id_from_locations($slug, $locations) { |
|
140 | - if ($slug === 0) { |
|
139 | + protected function get_menu_id_from_locations( $slug, $locations ) { |
|
140 | + if ( $slug === 0 ) { |
|
141 | 141 | $slug = $this->get_menu_id_from_terms($slug); |
142 | 142 | } |
143 | - if (is_numeric($slug)) { |
|
143 | + if ( is_numeric($slug) ) { |
|
144 | 144 | $slug = array_search($slug, $locations); |
145 | 145 | } |
146 | - if (isset($locations[$slug])) { |
|
146 | + if ( isset($locations[$slug]) ) { |
|
147 | 147 | $menu_id = $locations[$slug]; |
148 | 148 | return $menu_id; |
149 | 149 | } |
@@ -154,21 +154,21 @@ discard block |
||
154 | 154 | * @param int $slug |
155 | 155 | * @return int |
156 | 156 | */ |
157 | - protected function get_menu_id_from_terms($slug = 0) { |
|
158 | - if (!is_numeric($slug) && is_string($slug)) { |
|
157 | + protected function get_menu_id_from_terms( $slug = 0 ) { |
|
158 | + if ( !is_numeric($slug) && is_string($slug) ) { |
|
159 | 159 | //we have a string so lets search for that |
160 | 160 | $menu_id = get_term_by('slug', $slug, 'nav_menu'); |
161 | - if ($menu_id) { |
|
161 | + if ( $menu_id ) { |
|
162 | 162 | return $menu_id; |
163 | 163 | } |
164 | 164 | $menu_id = get_term_by('name', $slug, 'nav_menu'); |
165 | - if ($menu_id) { |
|
165 | + if ( $menu_id ) { |
|
166 | 166 | return $menu_id; |
167 | 167 | } |
168 | 168 | } |
169 | 169 | $menus = get_terms('nav_menu', array('hide_empty' => true)); |
170 | - if (is_array($menus) && count($menus)) { |
|
171 | - if (isset($menus[0]->term_id)) { |
|
170 | + if ( is_array($menus) && count($menus) ) { |
|
171 | + if ( isset($menus[0]->term_id) ) { |
|
172 | 172 | return $menus[0]->term_id; |
173 | 173 | } |
174 | 174 | } |
@@ -180,9 +180,9 @@ discard block |
||
180 | 180 | * @param int $parent_id |
181 | 181 | * @return TimberMenuItem|null |
182 | 182 | */ |
183 | - function find_parent_item_in_menu($menu_items, $parent_id) { |
|
184 | - foreach ($menu_items as &$item) { |
|
185 | - if ($item->ID == $parent_id) { |
|
183 | + function find_parent_item_in_menu( $menu_items, $parent_id ) { |
|
184 | + foreach ( $menu_items as &$item ) { |
|
185 | + if ( $item->ID == $parent_id ) { |
|
186 | 186 | return $item; |
187 | 187 | } |
188 | 188 | } |
@@ -193,29 +193,29 @@ discard block |
||
193 | 193 | * @param array $items |
194 | 194 | * @return array |
195 | 195 | */ |
196 | - protected function order_children($items) { |
|
196 | + protected function order_children( $items ) { |
|
197 | 197 | $index = array(); |
198 | 198 | $menu = array(); |
199 | - foreach ($items as $item) { |
|
200 | - if (isset($item->title)) { |
|
199 | + foreach ( $items as $item ) { |
|
200 | + if ( isset($item->title) ) { |
|
201 | 201 | //items from wp can come with a $title property which conflicts with methods |
202 | 202 | $item->__title = $item->title; |
203 | 203 | unset($item->title); |
204 | 204 | } |
205 | - if(isset($item->ID)){ |
|
206 | - if (is_object($item) && get_class($item) == 'WP_Post'){ |
|
205 | + if ( isset($item->ID) ) { |
|
206 | + if ( is_object($item) && get_class($item) == 'WP_Post' ) { |
|
207 | 207 | $old_menu_item = $item; |
208 | 208 | $item = new $this->PostClass($item); |
209 | 209 | } |
210 | 210 | $menu_item = new $this->MenuItemClass($item); |
211 | - if (isset($old_menu_item)){ |
|
211 | + if ( isset($old_menu_item) ) { |
|
212 | 212 | $menu_item->import_classes($old_menu_item); |
213 | 213 | } |
214 | 214 | $index[$item->ID] = $menu_item; |
215 | 215 | } |
216 | 216 | } |
217 | - foreach ($index as $item) { |
|
218 | - if (isset($item->menu_item_parent) && $item->menu_item_parent && isset($index[$item->menu_item_parent])) { |
|
217 | + foreach ( $index as $item ) { |
|
218 | + if ( isset($item->menu_item_parent) && $item->menu_item_parent && isset($index[$item->menu_item_parent]) ) { |
|
219 | 219 | $index[$item->menu_item_parent]->add_child($item); |
220 | 220 | } else { |
221 | 221 | $menu[] = $item; |
@@ -228,7 +228,7 @@ discard block |
||
228 | 228 | * @return array |
229 | 229 | */ |
230 | 230 | function get_items() { |
231 | - if (is_array($this->items)) { |
|
231 | + if ( is_array($this->items) ) { |
|
232 | 232 | return $this->items; |
233 | 233 | } |
234 | 234 | return array(); |
@@ -225,7 +225,7 @@ discard block |
||
225 | 225 | /** |
226 | 226 | * Initializes a TimberPost |
227 | 227 | * @internal |
228 | - * @param int|bool $pid |
|
228 | + * @param integer $pid |
|
229 | 229 | */ |
230 | 230 | protected function init($pid = false) { |
231 | 231 | if ( $pid === false ) { |
@@ -272,7 +272,7 @@ discard block |
||
272 | 272 | * takes a mix of integer (post ID), string (post slug), |
273 | 273 | * or object to return a WordPress post object from WP's built-in get_post() function |
274 | 274 | * @internal |
275 | - * @param mixed $pid |
|
275 | + * @param integer $pid |
|
276 | 276 | * @return WP_Post on success |
277 | 277 | */ |
278 | 278 | protected function prepare_post_info( $pid = 0 ) { |
@@ -598,7 +598,7 @@ discard block |
||
598 | 598 | * Gets a User object from the author of the post |
599 | 599 | * @internal |
600 | 600 | * @see TimberPost::author |
601 | - * @return bool|TimberUser |
|
601 | + * @return TimberUser|null |
|
602 | 602 | */ |
603 | 603 | function get_author() { |
604 | 604 | if ( isset($this->post_author) ) { |
@@ -608,7 +608,7 @@ discard block |
||
608 | 608 | |
609 | 609 | /** |
610 | 610 | * @internal |
611 | - * @return bool|TimberUser |
|
611 | + * @return TimberUser|null |
|
612 | 612 | */ |
613 | 613 | function get_modified_author() { |
614 | 614 | $user_id = get_post_meta($this->ID, '_edit_last', true); |
@@ -1073,7 +1073,7 @@ discard block |
||
1073 | 1073 | * <a href="{{post.author.link}}">{{post.author.name}}</a> |
1074 | 1074 | * </p> |
1075 | 1075 | * ``` |
1076 | - * @return TimberUser|bool A TimberUser object if found, false if not |
|
1076 | + * @return TimberUser|null A TimberUser object if found, false if not |
|
1077 | 1077 | */ |
1078 | 1078 | public function author() { |
1079 | 1079 | return $this->get_author(); |
@@ -1088,7 +1088,7 @@ discard block |
||
1088 | 1088 | * ```html |
1089 | 1089 | * Last updated by Harper Lee |
1090 | 1090 | * ``` |
1091 | - * @return TimberUser|bool A TimberUser object if found, false if not |
|
1091 | + * @return TimberUser|null A TimberUser object if found, false if not |
|
1092 | 1092 | */ |
1093 | 1093 | public function modified_author() { |
1094 | 1094 | return $this->get_modified_author(); |
@@ -188,13 +188,13 @@ discard block |
||
188 | 188 | && get_class($wp_query->queried_object) == 'WP_Post' |
189 | 189 | ) { |
190 | 190 | $pid = $wp_query->queried_object_id; |
191 | - } else if ( $pid === null && $wp_query->is_home && isset($wp_query->queried_object_id) && $wp_query->queried_object_id ) { |
|
191 | + } else if ( $pid === null && $wp_query->is_home && isset($wp_query->queried_object_id) && $wp_query->queried_object_id ) { |
|
192 | 192 | //hack for static page as home page |
193 | 193 | $pid = $wp_query->queried_object_id; |
194 | 194 | } else if ( $pid === null ) { |
195 | 195 | $gtid = false; |
196 | 196 | $maybe_post = get_post(); |
197 | - if ( isset($maybe_post->ID) ){ |
|
197 | + if ( isset($maybe_post->ID) ) { |
|
198 | 198 | $gtid = true; |
199 | 199 | } |
200 | 200 | if ( $gtid ) { |
@@ -1028,7 +1028,7 @@ discard block |
||
1028 | 1028 | $post = $this; |
1029 | 1029 | $class_array = get_post_class($class, $this->ID); |
1030 | 1030 | $post = $old_global_post; |
1031 | - if ( is_array($class_array) ){ |
|
1031 | + if ( is_array($class_array) ) { |
|
1032 | 1032 | return implode(' ', $class_array); |
1033 | 1033 | } |
1034 | 1034 | return $class_array; |
@@ -1248,7 +1248,7 @@ discard block |
||
1248 | 1248 | /** |
1249 | 1249 | * @return string |
1250 | 1250 | */ |
1251 | - public function name(){ |
|
1251 | + public function name() { |
|
1252 | 1252 | return $this->title(); |
1253 | 1253 | } |
1254 | 1254 |
@@ -34,1409 +34,1409 @@ |
||
34 | 34 | */ |
35 | 35 | class TimberPost extends TimberCore implements TimberCoreInterface { |
36 | 36 | |
37 | - /** |
|
38 | - * @var string $ImageClass the name of the class to handle images by default |
|
39 | - */ |
|
40 | - public $ImageClass = 'TimberImage'; |
|
41 | - |
|
42 | - /** |
|
43 | - * @var string $PostClass the name of the class to handle posts by default |
|
44 | - */ |
|
45 | - public $PostClass = 'TimberPost'; |
|
46 | - |
|
47 | - /** |
|
48 | - * @var string $TermClass the name of the class to handle terms by default |
|
49 | - */ |
|
50 | - public $TermClass = 'TimberTerm'; |
|
51 | - |
|
52 | - /** |
|
53 | - * @var string $object_type what does this class represent in WordPress terms? |
|
54 | - */ |
|
55 | - public $object_type = 'post'; |
|
56 | - |
|
57 | - /** |
|
58 | - * @var string $representation what does this class represent in WordPress terms? |
|
59 | - */ |
|
60 | - public static $representation = 'post'; |
|
61 | - |
|
62 | - /** |
|
63 | - * @internal |
|
64 | - * @var string $_content stores the processed content internally |
|
65 | - */ |
|
66 | - protected $_content; |
|
67 | - |
|
68 | - /** |
|
69 | - * @internal |
|
70 | - * @var array $_get_terms stores the results of a get_terms method call |
|
71 | - * @deprecated |
|
72 | - */ |
|
73 | - protected $_get_terms; |
|
74 | - |
|
75 | - /** |
|
76 | - * @var string $_permalink the returned permalink from WP's get_permalink function |
|
77 | - */ |
|
78 | - protected $_permalink; |
|
79 | - |
|
80 | - /** |
|
81 | - * @var array $_next stores the results of the next TimberPost in a set inside an array (in order to manage by-taxonomy) |
|
82 | - */ |
|
83 | - protected $_next = array(); |
|
84 | - |
|
85 | - /** |
|
86 | - * @var array $_prev stores the results of the previous TimberPost in a set inside an array (in order to manage by-taxonomy) |
|
87 | - */ |
|
88 | - protected $_prev = array(); |
|
89 | - |
|
90 | - /** |
|
91 | - * @api |
|
92 | - * @var string $class stores the CSS classes for the post (ex: "post post-type-book post-123") |
|
93 | - */ |
|
94 | - public $class; |
|
95 | - |
|
96 | - /** |
|
97 | - * @api |
|
98 | - * @var string $id the numeric WordPress id of a post |
|
99 | - */ |
|
100 | - public $id; |
|
101 | - |
|
102 | - /** |
|
103 | - * @var string $ID the numeric WordPress id of a post, capitalized to match WP usage |
|
104 | - */ |
|
105 | - public $ID; |
|
106 | - |
|
107 | - /** |
|
108 | - * @var int $post_author the numeric ID of the a post's author corresponding to the wp_user dtable |
|
109 | - */ |
|
110 | - public $post_author; |
|
111 | - |
|
112 | - /** |
|
113 | - * @var string $post_content the raw text of a WP post as stored in the database |
|
114 | - */ |
|
115 | - public $post_content; |
|
116 | - |
|
117 | - /** |
|
118 | - * @var string $post_date the raw date string as stored in the WP database, ex: 2014-07-05 18:01:39 |
|
119 | - */ |
|
120 | - public $post_date; |
|
121 | - |
|
122 | - /** |
|
123 | - * @var string $post_exceprt the raw text of a manual post exceprt as stored in the database |
|
124 | - */ |
|
125 | - public $post_excerpt; |
|
126 | - |
|
127 | - /** |
|
128 | - * @var int $post_parent the numeric ID of a post's parent post |
|
129 | - */ |
|
130 | - public $post_parent; |
|
131 | - |
|
132 | - /** |
|
133 | - * @api |
|
134 | - * @var string $post_status the status of a post ("draft", "publish", etc.) |
|
135 | - */ |
|
136 | - public $post_status; |
|
137 | - |
|
138 | - /** |
|
139 | - * @var string $post_title the raw text of a post's title as stored in the database |
|
140 | - */ |
|
141 | - public $post_title; |
|
142 | - |
|
143 | - /** |
|
144 | - * @api |
|
145 | - * @var string $post_type the name of the post type, this is the machine name (so "my_custom_post_type" as opposed to "My Custom Post Type") |
|
146 | - */ |
|
147 | - public $post_type; |
|
148 | - |
|
149 | - /** |
|
150 | - * @api |
|
151 | - * @var string $slug the URL-safe slug, this corresponds to the poorly-named "post_name" in the WP database, ex: "hello-world" |
|
152 | - */ |
|
153 | - public $slug; |
|
154 | - |
|
155 | - /** |
|
156 | - * If you send the constructor nothing it will try to figure out the current post id based on being inside The_Loop |
|
157 | - * @example |
|
158 | - * ```php |
|
159 | - * $post = new TimberPost(); |
|
160 | - * $other_post = new TimberPost($random_post_id); |
|
161 | - * ``` |
|
162 | - * @param mixed $pid |
|
163 | - */ |
|
164 | - public function __construct($pid = null) { |
|
165 | - $pid = $this->determine_id( $pid ); |
|
166 | - $this->init($pid); |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * tries to figure out what post you want to get if not explictly defined (or if it is, allows it to be passed through) |
|
171 | - * @internal |
|
172 | - * @param mixed a value to test against |
|
173 | - * @return int the numberic id we should be using for this post object |
|
174 | - */ |
|
175 | - protected function determine_id($pid) { |
|
176 | - global $wp_query; |
|
177 | - if ( $pid === null && |
|
178 | - isset($wp_query->queried_object_id) |
|
179 | - && $wp_query->queried_object_id |
|
180 | - && isset($wp_query->queried_object) |
|
181 | - && is_object($wp_query->queried_object) |
|
182 | - && get_class($wp_query->queried_object) == 'WP_Post' |
|
183 | - ) { |
|
184 | - if( isset( $_GET['preview'] ) && isset( $_GET['preview_nonce'] ) && wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . $wp_query->queried_object_id ) ) { |
|
185 | - $pid = $this->get_post_preview_id( $wp_query ); |
|
186 | - } else if ( !$pid ) { |
|
187 | - $pid = $wp_query->queried_object_id; |
|
188 | - } |
|
189 | - } else if ( $pid === null && $wp_query->is_home && isset($wp_query->queried_object_id) && $wp_query->queried_object_id ) { |
|
190 | - //hack for static page as home page |
|
191 | - $pid = $wp_query->queried_object_id; |
|
192 | - } else if ( $pid === null ) { |
|
193 | - $gtid = false; |
|
194 | - $maybe_post = get_post(); |
|
195 | - if ( isset($maybe_post->ID) ){ |
|
196 | - $gtid = true; |
|
197 | - } |
|
198 | - if ( $gtid ) { |
|
199 | - $pid = get_the_ID(); |
|
200 | - } |
|
201 | - if ( !$pid ) { |
|
202 | - global $wp_query; |
|
203 | - if ( isset($wp_query->query['p']) ) { |
|
204 | - $pid = $wp_query->query['p']; |
|
205 | - } |
|
206 | - } |
|
207 | - } |
|
208 | - if ( $pid === null && ($pid_from_loop = TimberPostGetter::loop_to_id()) ) { |
|
209 | - $pid = $pid_from_loop; |
|
210 | - } |
|
211 | - return $pid; |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * Outputs the title of the post if you do something like `<h1>{{post}}</h1>` |
|
216 | - * @return string |
|
217 | - */ |
|
218 | - public function __toString() { |
|
219 | - return $this->title(); |
|
220 | - } |
|
221 | - |
|
222 | - protected function get_post_preview_id( $query ) { |
|
223 | - $can = array( |
|
224 | - 'edit_' . $query->queried_object->post_type . 's', |
|
225 | - ); |
|
226 | - |
|
227 | - if ( $query->queried_object->author_id !== get_current_user_id() ) { |
|
228 | - $can[] = 'edit_others_' . $query->queried_object->post_type . 's'; |
|
229 | - } |
|
230 | - |
|
231 | - $can_preview = array(); |
|
232 | - |
|
233 | - foreach( $can as $type ) { |
|
234 | - if( current_user_can( $type ) ) { |
|
235 | - $can_preview[] = true; |
|
236 | - } |
|
237 | - } |
|
238 | - |
|
239 | - if ( count( $can_preview ) !== count( $can ) ) { |
|
240 | - return; |
|
241 | - } |
|
242 | - |
|
243 | - $revisions = wp_get_post_revisions( $query->queried_object_id ); |
|
244 | - |
|
245 | - if( !empty( $revisions ) ) { |
|
246 | - $last = end($revisions); |
|
247 | - return $last->ID; |
|
248 | - } |
|
249 | - |
|
250 | - return false; |
|
251 | - } |
|
252 | - |
|
253 | - /** |
|
254 | - * Initializes a TimberPost |
|
255 | - * @internal |
|
256 | - * @param int|bool $pid |
|
257 | - */ |
|
258 | - protected function init($pid = false) { |
|
259 | - if ( $pid === false ) { |
|
260 | - $pid = get_the_ID(); |
|
261 | - } |
|
262 | - if ( is_numeric($pid) ) { |
|
263 | - $this->ID = $pid; |
|
264 | - } |
|
265 | - $post_info = $this->get_info($pid); |
|
266 | - $this->import($post_info); |
|
267 | - //cant have a function, so gots to do it this way |
|
268 | - $post_class = $this->post_class(); |
|
269 | - $this->class = $post_class; |
|
270 | - } |
|
271 | - |
|
272 | - /** |
|
273 | - * Get the URL that will edit the current post/object |
|
274 | - * @internal |
|
275 | - * @see TimberPost::edit_link |
|
276 | - * @return bool|string |
|
277 | - */ |
|
278 | - function get_edit_url() { |
|
279 | - if ( $this->can_edit() ) { |
|
280 | - return get_edit_post_link($this->ID); |
|
281 | - } |
|
282 | - } |
|
283 | - |
|
284 | - /** |
|
285 | - * updates the post_meta of the current object with the given value |
|
286 | - * @param string $field |
|
287 | - * @param mixed $value |
|
288 | - */ |
|
289 | - public function update( $field, $value ) { |
|
290 | - if ( isset($this->ID) ) { |
|
291 | - update_post_meta($this->ID, $field, $value); |
|
292 | - $this->$field = $value; |
|
293 | - } |
|
294 | - } |
|
295 | - |
|
296 | - |
|
297 | - /** |
|
298 | - * takes a mix of integer (post ID), string (post slug), |
|
299 | - * or object to return a WordPress post object from WP's built-in get_post() function |
|
300 | - * @internal |
|
301 | - * @param mixed $pid |
|
302 | - * @return WP_Post on success |
|
303 | - */ |
|
304 | - protected function prepare_post_info( $pid = 0 ) { |
|
305 | - if ( is_string($pid) || is_numeric($pid) || (is_object($pid) && !isset($pid->post_title)) || $pid === 0 ) { |
|
306 | - $pid = self::check_post_id($pid); |
|
307 | - $post = get_post($pid); |
|
308 | - if ( $post ) { |
|
309 | - return $post; |
|
310 | - } |
|
311 | - } |
|
312 | - //we can skip if already is WP_Post |
|
313 | - return $pid; |
|
314 | - } |
|
315 | - |
|
316 | - |
|
317 | - /** |
|
318 | - * helps you find the post id regardless of whether you send a string or whatever |
|
319 | - * @param integer $pid ; |
|
320 | - * @internal |
|
321 | - * @return integer ID number of a post |
|
322 | - */ |
|
323 | - protected function check_post_id( $pid ) { |
|
324 | - if ( is_numeric($pid) && $pid === 0 ) { |
|
325 | - $pid = get_the_ID(); |
|
326 | - return $pid; |
|
327 | - } |
|
328 | - if ( !is_numeric($pid) && is_string($pid) ) { |
|
329 | - $pid = self::get_post_id_by_name($pid); |
|
330 | - return $pid; |
|
331 | - } |
|
332 | - if ( !$pid ) { |
|
333 | - return null; |
|
334 | - } |
|
335 | - return $pid; |
|
336 | - } |
|
337 | - |
|
338 | - |
|
339 | - /** |
|
340 | - * get_post_id_by_name($post_name) |
|
341 | - * @internal |
|
342 | - * @param string $post_name |
|
343 | - * @return int |
|
344 | - */ |
|
345 | - static function get_post_id_by_name($post_name) { |
|
346 | - global $wpdb; |
|
347 | - $query = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_name = %s LIMIT 1", $post_name); |
|
348 | - $result = $wpdb->get_row($query); |
|
349 | - if (!$result) { |
|
350 | - return null; |
|
351 | - } |
|
352 | - return $result->ID; |
|
353 | - } |
|
354 | - |
|
355 | - /** |
|
356 | - * get a preview of your post, if you have an excerpt it will use that, |
|
357 | - * otherwise it will pull from the post_content. |
|
358 | - * If there's a <!-- more --> tag it will use that to mark where to pull through. |
|
359 | - * @api |
|
360 | - * @example |
|
361 | - * ```twig |
|
362 | - * <p>{{post.get_preview(50)}}</p> |
|
363 | - * ``` |
|
364 | - * @param int $len The number of words that WP should use to make the tease. (Isn't this better than [this mess](http://wordpress.org/support/topic/changing-the-default-length-of-the_excerpt-1?replies=14)?). If you've set a post_excerpt on a post, we'll use that for the preview text; otherwise the first X words of the post_content |
|
365 | - * @param bool $force What happens if your custom post excerpt is longer then the length requested? By default (`$force = false`) it will use the full `post_excerpt`. However, you can set this to true to *force* your excerpt to be of the desired length |
|
366 | - * @param string $readmore The text you want to use on the 'readmore' link |
|
367 | - * @param bool|string $strip true for default, false for none, string for list of custom attributes |
|
368 | - * @param string $end The text to end the preview with (defaults to ...) |
|
369 | - * @return string of the post preview |
|
370 | - */ |
|
371 | - function get_preview($len = 50, $force = false, $readmore = 'Read More', $strip = true, $end = '…') { |
|
372 | - $text = ''; |
|
373 | - $trimmed = false; |
|
374 | - if ( isset($this->post_excerpt) && strlen($this->post_excerpt) ) { |
|
375 | - if ( $force ) { |
|
376 | - $text = TimberHelper::trim_words($this->post_excerpt, $len, false); |
|
377 | - $trimmed = true; |
|
378 | - } else { |
|
379 | - $text = $this->post_excerpt; |
|
380 | - } |
|
381 | - } |
|
382 | - if ( !strlen($text) && preg_match('/<!--\s?more(.*?)?-->/', $this->post_content, $readmore_matches) ) { |
|
383 | - $pieces = explode($readmore_matches[0], $this->post_content); |
|
384 | - $text = $pieces[0]; |
|
385 | - if ( $force ) { |
|
386 | - $text = TimberHelper::trim_words($text, $len, false); |
|
387 | - $trimmed = true; |
|
388 | - } |
|
389 | - $text = do_shortcode( $text ); |
|
390 | - } |
|
391 | - if ( !strlen($text) ) { |
|
392 | - $text = TimberHelper::trim_words($this->get_content(), $len, false); |
|
393 | - $trimmed = true; |
|
394 | - } |
|
395 | - if ( !strlen(trim($text)) ) { |
|
396 | - return trim($text); |
|
397 | - } |
|
398 | - if ( $strip ) { |
|
399 | - $allowable_tags = (is_string($strip)) ? $strip : null; |
|
400 | - $text = trim(strip_tags($text, $allowable_tags)); |
|
401 | - } |
|
402 | - if ( strlen($text) ) { |
|
403 | - $text = trim($text); |
|
404 | - $last = $text[strlen($text) - 1]; |
|
405 | - if ( $last != '.' && $trimmed ) { |
|
406 | - $text .= $end; |
|
407 | - } |
|
408 | - if ( !$strip ) { |
|
409 | - $last_p_tag = strrpos($text, '</p>'); |
|
410 | - if ( $last_p_tag !== false ) { |
|
411 | - $text = substr($text, 0, $last_p_tag); |
|
412 | - } |
|
413 | - if ( $last != '.' && $trimmed ) { |
|
414 | - $text .= $end . ' '; |
|
415 | - } |
|
416 | - } |
|
417 | - $read_more_class = apply_filters('timber/post/get_preview/read_more_class', "read-more"); |
|
418 | - if ( $readmore && isset($readmore_matches) && !empty($readmore_matches[1]) ) { |
|
419 | - $text .= ' <a href="' . $this->get_permalink() . '" class="'.$read_more_class .'">' . trim($readmore_matches[1]) . '</a>'; |
|
420 | - } elseif ( $readmore ) { |
|
421 | - $text .= ' <a href="' . $this->get_permalink() . '" class="'.$read_more_class .'">' . trim($readmore) . '</a>'; |
|
422 | - } |
|
423 | - if ( !$strip && $last_p_tag && ( strpos($text, '<p>') || strpos($text, '<p ') ) ) { |
|
424 | - $text .= '</p>'; |
|
425 | - } |
|
426 | - } |
|
427 | - return trim($text); |
|
428 | - } |
|
429 | - |
|
430 | - /** |
|
431 | - * gets the post custom and attaches it to the current object |
|
432 | - * @internal |
|
433 | - * @param bool|int $pid a post ID number |
|
434 | - */ |
|
435 | - function import_custom( $pid = false ) { |
|
436 | - if ( !$pid ) { |
|
437 | - $pid = $this->ID; |
|
438 | - } |
|
439 | - $customs = $this->get_post_custom($pid); |
|
440 | - $this->import($customs); |
|
441 | - } |
|
442 | - |
|
443 | - /** |
|
444 | - * Used internally to fetch the metadata fields (wp_postmeta table) |
|
445 | - * and attach them to our TimberPost object |
|
446 | - * @internal |
|
447 | - * @param int $pid |
|
448 | - * @return array |
|
449 | - */ |
|
450 | - protected function get_post_custom( $pid ) { |
|
451 | - apply_filters('timber_post_get_meta_pre', array(), $pid, $this); |
|
452 | - $customs = get_post_custom($pid); |
|
453 | - if ( !is_array($customs) || empty($customs) ) { |
|
454 | - return array(); |
|
455 | - } |
|
456 | - foreach ( $customs as $key => $value ) { |
|
457 | - if ( is_array($value) && count($value) == 1 && isset($value[0]) ) { |
|
458 | - $value = $value[0]; |
|
459 | - } |
|
460 | - $customs[$key] = maybe_unserialize($value); |
|
461 | - } |
|
462 | - $customs = apply_filters('timber_post_get_meta', $customs, $pid, $this); |
|
463 | - return $customs; |
|
464 | - } |
|
465 | - |
|
466 | - /** |
|
467 | - * @internal |
|
468 | - * @see TimberPost::thumbnail |
|
469 | - * @return null|TimberImage |
|
470 | - */ |
|
471 | - function get_thumbnail() { |
|
472 | - if ( function_exists('get_post_thumbnail_id') ) { |
|
473 | - $tid = get_post_thumbnail_id($this->ID); |
|
474 | - if ( $tid ) { |
|
475 | - return new $this->ImageClass($tid); |
|
476 | - } |
|
477 | - } |
|
478 | - } |
|
479 | - |
|
480 | - /** |
|
481 | - * @internal |
|
482 | - * @see TimberPost::link |
|
483 | - * @return string |
|
484 | - */ |
|
485 | - function get_permalink() { |
|
486 | - if ( isset($this->_permalink) ) { |
|
487 | - return $this->_permalink; |
|
488 | - } |
|
489 | - $this->_permalink = get_permalink($this->ID); |
|
490 | - return $this->_permalink; |
|
491 | - } |
|
492 | - |
|
493 | - /** |
|
494 | - * get the permalink for a post object |
|
495 | - * In your templates you should use link: |
|
496 | - * <a href="{{post.link}}">Read my post</a> |
|
497 | - * @internal |
|
498 | - * @return string |
|
499 | - */ |
|
500 | - function get_link() { |
|
501 | - return $this->get_permalink(); |
|
502 | - } |
|
503 | - |
|
504 | - /** |
|
505 | - * Get the next post in WordPress's ordering |
|
506 | - * @internal |
|
507 | - * @param bool $taxonomy |
|
508 | - * @return TimberPost|boolean |
|
509 | - */ |
|
510 | - function get_next( $taxonomy = false ) { |
|
511 | - if ( !isset($this->_next) || !isset($this->_next[$taxonomy]) ) { |
|
512 | - global $post; |
|
513 | - $this->_next = array(); |
|
514 | - $old_global = $post; |
|
515 | - $post = $this; |
|
516 | - if ( $taxonomy ) { |
|
517 | - $adjacent = get_adjacent_post(true, '', false, $taxonomy); |
|
518 | - } else { |
|
519 | - $adjacent = get_adjacent_post(false, '', false); |
|
520 | - } |
|
521 | - |
|
522 | - if ( $adjacent ) { |
|
523 | - $this->_next[$taxonomy] = new $this->PostClass($adjacent); |
|
524 | - } else { |
|
525 | - $this->_next[$taxonomy] = false; |
|
526 | - } |
|
527 | - $post = $old_global; |
|
528 | - } |
|
529 | - return $this->_next[$taxonomy]; |
|
530 | - } |
|
531 | - |
|
532 | - /** |
|
533 | - * Get a data array of pagination so you can navigate to the previous/next for a paginated post |
|
534 | - * @return array |
|
535 | - */ |
|
536 | - public function get_pagination() { |
|
537 | - global $post, $page, $numpages, $multipage; |
|
538 | - $post = $this; |
|
539 | - $ret = array(); |
|
540 | - if ( $multipage ) { |
|
541 | - for ( $i = 1; $i <= $numpages; $i++ ) { |
|
542 | - $link = self::get_wp_link_page($i); |
|
543 | - $data = array('name' => $i, 'title' => $i, 'text' => $i, 'link' => $link); |
|
544 | - if ( $i == $page ) { |
|
545 | - $data['current'] = true; |
|
546 | - } |
|
547 | - $ret['pages'][] = $data; |
|
548 | - } |
|
549 | - $i = $page - 1; |
|
550 | - if ( $i ) { |
|
551 | - $link = self::get_wp_link_page($i); |
|
552 | - $ret['prev'] = array('link' => $link); |
|
553 | - } |
|
554 | - $i = $page + 1; |
|
555 | - if ( $i <= $numpages ) { |
|
556 | - $link = self::get_wp_link_page($i); |
|
557 | - $ret['next'] = array('link' => $link); |
|
558 | - } |
|
559 | - } |
|
560 | - return $ret; |
|
561 | - } |
|
562 | - |
|
563 | - /** |
|
564 | - * @param int $i |
|
565 | - * @return string |
|
566 | - */ |
|
567 | - protected static function get_wp_link_page($i) { |
|
568 | - $link = _wp_link_page($i); |
|
569 | - $link = new SimpleXMLElement($link . '</a>'); |
|
570 | - if ( isset($link['href']) ) { |
|
571 | - return $link['href']; |
|
572 | - } |
|
573 | - return ''; |
|
574 | - } |
|
575 | - |
|
576 | - /** |
|
577 | - * Get the permalink for a post, but as a relative path |
|
578 | - * For example, where {{post.link}} would return "http://example.org/2015/07/04/my-cool-post" |
|
579 | - * this will return the relative version: "/2015/07/04/my-cool-post" |
|
580 | - * @internal |
|
581 | - * @return string |
|
582 | - */ |
|
583 | - function get_path() { |
|
584 | - return TimberURLHelper::get_rel_url($this->get_link()); |
|
585 | - } |
|
586 | - |
|
587 | - /** |
|
588 | - * Get the next post in WordPress's ordering |
|
589 | - * @internal |
|
590 | - * @param bool $taxonomy |
|
591 | - * @return TimberPost|boolean |
|
592 | - */ |
|
593 | - function get_prev( $taxonomy = false ) { |
|
594 | - if ( isset($this->_prev) && isset($this->_prev[$taxonomy]) ) { |
|
595 | - return $this->_prev[$taxonomy]; |
|
596 | - } |
|
597 | - global $post; |
|
598 | - $old_global = $post; |
|
599 | - $post = $this; |
|
600 | - $within_taxonomy = ($taxonomy) ? $taxonomy : 'category'; |
|
601 | - $adjacent = get_adjacent_post(($taxonomy), '', true, $within_taxonomy); |
|
602 | - $prev_in_taxonomy = false; |
|
603 | - if ( $adjacent ) { |
|
604 | - $prev_in_taxonomy = new $this->PostClass($adjacent); |
|
605 | - } |
|
606 | - $this->_prev[$taxonomy] = $prev_in_taxonomy; |
|
607 | - $post = $old_global; |
|
608 | - return $this->_prev[$taxonomy]; |
|
609 | - } |
|
610 | - |
|
611 | - /** |
|
612 | - * Get the parent post of the post |
|
613 | - * @internal |
|
614 | - * @return bool|TimberPost |
|
615 | - */ |
|
616 | - function get_parent() { |
|
617 | - if ( !$this->post_parent ) { |
|
618 | - return false; |
|
619 | - } |
|
620 | - return new $this->PostClass($this->post_parent); |
|
621 | - } |
|
622 | - |
|
623 | - /** |
|
624 | - * Gets a User object from the author of the post |
|
625 | - * @internal |
|
626 | - * @see TimberPost::author |
|
627 | - * @return bool|TimberUser |
|
628 | - */ |
|
629 | - function get_author() { |
|
630 | - if ( isset($this->post_author) ) { |
|
631 | - return new TimberUser($this->post_author); |
|
632 | - } |
|
633 | - } |
|
634 | - |
|
635 | - /** |
|
636 | - * @internal |
|
637 | - * @return bool|TimberUser |
|
638 | - */ |
|
639 | - function get_modified_author() { |
|
640 | - $user_id = get_post_meta($this->ID, '_edit_last', true); |
|
641 | - return ($user_id ? new TimberUser($user_id) : $this->get_author()); |
|
642 | - } |
|
643 | - |
|
644 | - /** |
|
645 | - * Used internally by init, etc. to build TimberPost object |
|
646 | - * @internal |
|
647 | - * @param int $pid |
|
648 | - * @return null|object|WP_Post |
|
649 | - */ |
|
650 | - protected function get_info($pid) { |
|
651 | - $post = $this->prepare_post_info($pid); |
|
652 | - if ( !isset($post->post_status) ) { |
|
653 | - return null; |
|
654 | - } |
|
655 | - $post->status = $post->post_status; |
|
656 | - $post->id = $post->ID; |
|
657 | - $post->slug = $post->post_name; |
|
658 | - $customs = $this->get_post_custom($post->ID); |
|
659 | - $post->custom = $customs; |
|
660 | - $post = (object) array_merge((array)$customs, (array)$post); |
|
661 | - return $post; |
|
662 | - } |
|
663 | - |
|
664 | - /** |
|
665 | - * @internal |
|
666 | - * @see TimberPost::date |
|
667 | - * @param string $date_format |
|
668 | - * @return string |
|
669 | - */ |
|
670 | - function get_date( $date_format = '' ) { |
|
671 | - $df = $date_format ? $date_format : get_option('date_format'); |
|
672 | - $the_date = (string)mysql2date($df, $this->post_date); |
|
673 | - return apply_filters('get_the_date', $the_date, $df); |
|
674 | - } |
|
675 | - |
|
676 | - /** |
|
677 | - * @internal |
|
678 | - * @param string $date_format |
|
679 | - * @return string |
|
680 | - */ |
|
681 | - function get_modified_date( $date_format = '' ) { |
|
682 | - $df = $date_format ? $date_format : get_option('date_format'); |
|
683 | - $the_time = $this->get_modified_time($df); |
|
684 | - return apply_filters('get_the_modified_date', $the_time, $date_format); |
|
685 | - } |
|
686 | - |
|
687 | - /** |
|
688 | - * @internal |
|
689 | - * @param string $time_format |
|
690 | - * @return string |
|
691 | - */ |
|
692 | - function get_modified_time( $time_format = '' ) { |
|
693 | - $tf = $time_format ? $time_format : get_option('time_format'); |
|
694 | - $the_time = get_post_modified_time($tf, false, $this->ID, true); |
|
695 | - return apply_filters('get_the_modified_time', $the_time, $time_format); |
|
696 | - } |
|
697 | - |
|
698 | - /** |
|
699 | - * @internal |
|
700 | - * @see TimberPost::children |
|
701 | - * @param string $post_type |
|
702 | - * @param bool|string $childPostClass |
|
703 | - * @return array |
|
704 | - */ |
|
705 | - function get_children( $post_type = 'any', $childPostClass = false ) { |
|
706 | - if ( $childPostClass === false ) { |
|
707 | - $childPostClass = $this->PostClass; |
|
708 | - } |
|
709 | - if ( $post_type == 'parent' ) { |
|
710 | - $post_type = $this->post_type; |
|
711 | - } |
|
712 | - $children = get_children('post_parent=' . $this->ID . '&post_type=' . $post_type . '&numberposts=-1&orderby=menu_order title&order=ASC&post_status=publish'); |
|
713 | - foreach ( $children as &$child ) { |
|
714 | - $child = new $childPostClass($child->ID); |
|
715 | - } |
|
716 | - $children = array_values($children); |
|
717 | - return $children; |
|
718 | - } |
|
719 | - |
|
720 | - |
|
721 | - /** |
|
722 | - * Get the comments for a post |
|
723 | - * @internal |
|
724 | - * @see TimberPost::comments |
|
725 | - * @param int $ct |
|
726 | - * @param string $order |
|
727 | - * @param string $type |
|
728 | - * @param string $status |
|
729 | - * @param string $CommentClass |
|
730 | - * @return array|mixed |
|
731 | - */ |
|
732 | - |
|
733 | - function get_comments($ct = 0, $order = 'wp', $type = 'comment', $status = 'approve', $CommentClass = 'TimberComment') { |
|
734 | - |
|
735 | - global $overridden_cpage, $user_ID; |
|
736 | - $overridden_cpage = false; |
|
737 | - |
|
738 | - $commenter = wp_get_current_commenter(); |
|
739 | - $comment_author_email = $commenter['comment_author_email']; |
|
740 | - |
|
741 | - $args = array('post_id' => $this->ID, 'status' => $status, 'order' => $order); |
|
742 | - if ( $ct > 0 ) { |
|
743 | - $args['number'] = $ct; |
|
744 | - } |
|
745 | - if ( strtolower($order) == 'wp' || strtolower($order) == 'wordpress' ) { |
|
746 | - $args['order'] = get_option('comment_order'); |
|
747 | - } |
|
748 | - |
|
749 | - if ( $user_ID ) { |
|
750 | - $args['include_unapproved'] = array( $user_ID ); |
|
751 | - } elseif ( ! empty( $comment_author_email ) ) { |
|
752 | - $args['include_unapproved'] = array( $comment_author_email ); |
|
753 | - } |
|
754 | - |
|
755 | - $comments = get_comments($args); |
|
756 | - $timber_comments = array(); |
|
757 | - |
|
758 | - if ( '' == get_query_var('cpage') && get_option('page_comments') ) { |
|
759 | - set_query_var( 'cpage', 'newest' == get_option('default_comments_page') ? get_comment_pages_count() : 1 ); |
|
760 | - $overridden_cpage = true; |
|
761 | - } |
|
762 | - |
|
763 | - foreach($comments as $key => &$comment) { |
|
764 | - $timber_comment = new $CommentClass($comment); |
|
765 | - $timber_comments[$timber_comment->id] = $timber_comment; |
|
766 | - } |
|
767 | - |
|
768 | - // Build a flattened (depth=1) comment tree |
|
769 | - $comments_tree = array(); |
|
770 | - foreach( $timber_comments as $key => $comment ) { |
|
771 | - if ( ! $comment->is_child() ) { |
|
772 | - continue; |
|
773 | - } |
|
774 | - |
|
775 | - $tree_element = $comment; |
|
776 | - do { |
|
777 | - $tree_element = $timber_comments[$tree_element->comment_parent]; |
|
778 | - } while( $tree_element->is_child() ); |
|
779 | - |
|
780 | - $comments_tree[$tree_element->id][] = $comment->id; |
|
781 | - } |
|
782 | - |
|
783 | - // Add child comments to the relative "super parents" |
|
784 | - foreach($comments_tree as $comment_parent => $comment_children) { |
|
785 | - foreach($comment_children as $comment_child) { |
|
786 | - $timber_comments[$comment_parent]->children[] = $timber_comments[$comment_child]; |
|
787 | - unset($timber_comments[$comment_child]); |
|
788 | - } |
|
789 | - } |
|
790 | - |
|
791 | - $timber_comments = array_values($timber_comments); |
|
792 | - |
|
793 | - return $timber_comments; |
|
794 | - } |
|
795 | - |
|
796 | - /** |
|
797 | - * Get the categories for a post |
|
798 | - * @internal |
|
799 | - * @see TimberPost::categories |
|
800 | - * @return array of TimberTerms |
|
801 | - */ |
|
802 | - function get_categories() { |
|
803 | - return $this->get_terms('category'); |
|
804 | - } |
|
805 | - |
|
806 | - /** |
|
807 | - * @internal |
|
808 | - * @see TimberPost::category |
|
809 | - * @return mixed |
|
810 | - */ |
|
811 | - function get_category( ) { |
|
812 | - $cats = $this->get_categories(); |
|
813 | - if ( count($cats) && isset($cats[0]) ) { |
|
814 | - return $cats[0]; |
|
815 | - } |
|
816 | - } |
|
817 | - |
|
818 | - /** |
|
819 | - * @internal |
|
820 | - * @param string|array $tax |
|
821 | - * @param bool $merge |
|
822 | - * @param string $TermClass |
|
823 | - * @return array |
|
824 | - */ |
|
825 | - function get_terms( $tax = '', $merge = true, $TermClass = '' ) { |
|
826 | - |
|
827 | - $TermClass = $TermClass ?: $this->TermClass; |
|
828 | - |
|
829 | - if ( is_string($merge) && class_exists($merge) ) { |
|
830 | - $TermClass = $merge; |
|
831 | - } |
|
832 | - if ( is_array($tax) ) { |
|
833 | - $taxonomies = $tax; |
|
834 | - } |
|
835 | - if ( is_string($tax) ) { |
|
836 | - if ( in_array($tax, array('all','any','')) ) { |
|
837 | - $taxonomies = get_object_taxonomies($this->post_type); |
|
838 | - } else { |
|
839 | - $taxonomies = array($tax); |
|
840 | - } |
|
841 | - } |
|
842 | - |
|
843 | - $term_class_objects = array(); |
|
844 | - |
|
845 | - foreach ( $taxonomies as $taxonomy ) { |
|
846 | - if ( in_array($taxonomy, array('tag','tags')) ) { |
|
847 | - $taxonomy = 'post_tag'; |
|
848 | - } |
|
849 | - if ( $taxonomy == 'categories' ) { |
|
850 | - $taxonomy = 'category'; |
|
851 | - } |
|
852 | - |
|
853 | - $terms = wp_get_post_terms($this->ID, $taxonomy); |
|
854 | - |
|
855 | - if ( is_wp_error($terms) ) { |
|
856 | - /* @var $terms WP_Error */ |
|
857 | - TimberHelper::error_log("Error retrieving terms for taxonomy '$taxonomy' on a post in timber-post.php"); |
|
858 | - TimberHelper::error_log('tax = ' . print_r($tax, true)); |
|
859 | - TimberHelper::error_log('WP_Error: ' . $terms->get_error_message()); |
|
860 | - |
|
861 | - return $term_class_objects; |
|
862 | - } |
|
863 | - |
|
864 | - // map over array of wordpress terms, and transform them into instances of the TermClass |
|
865 | - $terms = array_map(function($term) use ($TermClass, $taxonomy) { |
|
866 | - return call_user_func(array($TermClass, 'from'), $term->term_id, $taxonomy); |
|
867 | - }, $terms); |
|
868 | - |
|
869 | - if ( $merge && is_array($terms) ) { |
|
870 | - $term_class_objects = array_merge($term_class_objects, $terms); |
|
871 | - } else if ( count($terms) ) { |
|
872 | - $term_class_objects[$taxonomy] = $terms; |
|
873 | - } |
|
874 | - } |
|
875 | - return $term_class_objects; |
|
876 | - } |
|
877 | - |
|
878 | - /** |
|
879 | - * @param string|int $term_name_or_id |
|
880 | - * @param string $taxonomy |
|
881 | - * @return bool |
|
882 | - */ |
|
883 | - function has_term( $term_name_or_id, $taxonomy = 'all' ) { |
|
884 | - if ( $taxonomy == 'all' || $taxonomy == 'any' ) { |
|
885 | - $taxes = get_object_taxonomies($this->post_type, 'names'); |
|
886 | - $ret = false; |
|
887 | - foreach ( $taxes as $tax ) { |
|
888 | - if ( has_term($term_name_or_id, $tax, $this->ID) ) { |
|
889 | - $ret = true; |
|
890 | - break; |
|
891 | - } |
|
892 | - } |
|
893 | - return $ret; |
|
894 | - } |
|
895 | - return has_term($term_name_or_id, $taxonomy, $this->ID); |
|
896 | - } |
|
897 | - |
|
898 | - /** |
|
899 | - * @param string $field |
|
900 | - * @return TimberImage |
|
901 | - */ |
|
902 | - function get_image( $field ) { |
|
903 | - return new $this->ImageClass($this->$field); |
|
904 | - } |
|
905 | - |
|
906 | - /** |
|
907 | - * Gets an array of tags for you to use |
|
908 | - * @internal |
|
909 | - * @example |
|
910 | - * ```twig |
|
911 | - * <ul class="tags"> |
|
912 | - * {% for tag in post.tags %} |
|
913 | - * <li>{{tag.name}}</li> |
|
914 | - * {% endfor %} |
|
915 | - * </ul> |
|
916 | - * ``` |
|
917 | - * @return array |
|
918 | - */ |
|
919 | - function get_tags() { |
|
920 | - return $this->get_terms('post_tag'); |
|
921 | - } |
|
922 | - |
|
923 | - /** |
|
924 | - * Outputs the title with filters applied |
|
925 | - * @internal |
|
926 | - * @example |
|
927 | - * ```twig |
|
928 | - * <h1>{{post.get_title}}</h1> |
|
929 | - * ``` |
|
930 | - * ```html |
|
931 | - * <h1>Hello World!</h1> |
|
932 | - * ``` |
|
933 | - * @return string |
|
934 | - */ |
|
935 | - function get_title() { |
|
936 | - return apply_filters('the_title', $this->post_title, $this->ID); |
|
937 | - } |
|
938 | - |
|
939 | - /** |
|
940 | - * Displays the content of the post with filters, shortcodes and wpautop applied |
|
941 | - * @example |
|
942 | - * ```twig |
|
943 | - * <div class="article-text">{{post.get_content}}</div> |
|
944 | - * ``` |
|
945 | - * ```html |
|
946 | - * <div class="article-text"><p>Blah blah blah</p><p>More blah blah blah.</p></div> |
|
947 | - * ``` |
|
948 | - * @param int $len |
|
949 | - * @param int $page |
|
950 | - * @return string |
|
951 | - */ |
|
952 | - function get_content( $len = 0, $page = 0 ) { |
|
953 | - if ( $len == 0 && $page == 0 && $this->_content ) { |
|
954 | - return $this->_content; |
|
955 | - } |
|
956 | - $content = $this->post_content; |
|
957 | - if ( $len ) { |
|
958 | - $content = wp_trim_words($content, $len); |
|
959 | - } |
|
960 | - if ( $page ) { |
|
961 | - $contents = explode('<!--nextpage-->', $content); |
|
962 | - $page--; |
|
963 | - if ( count($contents) > $page ) { |
|
964 | - $content = $contents[$page]; |
|
965 | - } |
|
966 | - } |
|
967 | - $content = apply_filters('the_content', ($content)); |
|
968 | - if ( $len == 0 && $page == 0 ) { |
|
969 | - $this->_content = $content; |
|
970 | - } |
|
971 | - return $content; |
|
972 | - } |
|
973 | - |
|
974 | - /** |
|
975 | - * @return string |
|
976 | - */ |
|
977 | - function get_paged_content() { |
|
978 | - global $page; |
|
979 | - return $this->get_content(0, $page); |
|
980 | - } |
|
981 | - /** |
|
982 | - * |
|
983 | - * Here is my summary |
|
984 | - * @example |
|
985 | - * ```twig |
|
986 | - * This post is from <span>{{ post.get_post_type.labels.plural }}</span> |
|
987 | - * ``` |
|
988 | - * |
|
989 | - * ```html |
|
990 | - * This post is from <span>Recipes</span> |
|
991 | - * ``` |
|
992 | - * @return mixed |
|
993 | - */ |
|
994 | - public function get_post_type() { |
|
995 | - return get_post_type_object($this->post_type); |
|
996 | - } |
|
997 | - |
|
998 | - /** |
|
999 | - * @return int the number of comments on a post |
|
1000 | - */ |
|
1001 | - public function get_comment_count() { |
|
1002 | - return get_comments_number($this->ID); |
|
1003 | - } |
|
1004 | - |
|
1005 | - /** |
|
1006 | - * @param string $field_name |
|
1007 | - * @return mixed |
|
1008 | - */ |
|
1009 | - public function get_field( $field_name ) { |
|
1010 | - $value = apply_filters('timber_post_get_meta_field_pre', null, $this->ID, $field_name, $this); |
|
1011 | - if ( $value === null ) { |
|
1012 | - $value = get_post_meta($this->ID, $field_name); |
|
1013 | - if ( is_array($value) && count($value) == 1 ) { |
|
1014 | - $value = $value[0]; |
|
1015 | - } |
|
1016 | - if ( is_array($value) && count($value) == 0 ) { |
|
1017 | - $value = null; |
|
1018 | - } |
|
1019 | - } |
|
1020 | - $value = apply_filters('timber_post_get_meta_field', $value, $this->ID, $field_name, $this); |
|
1021 | - return $value; |
|
1022 | - } |
|
1023 | - |
|
1024 | - /** |
|
1025 | - * @param string $field_name |
|
1026 | - */ |
|
1027 | - function import_field( $field_name ) { |
|
1028 | - $this->$field_name = $this->get_field($field_name); |
|
1029 | - } |
|
1030 | - |
|
1031 | - /** |
|
1032 | - * @internal |
|
1033 | - * @return mixed |
|
1034 | - */ |
|
1035 | - function get_format() { |
|
1036 | - return get_post_format($this->ID); |
|
1037 | - } |
|
1038 | - |
|
1039 | - /** |
|
1040 | - * Get the CSS classes for a post. For usage you should use `{{post.class}}` instead of `{{post.post_class}}` |
|
1041 | - * @internal |
|
1042 | - * @param string $class additional classes you want to add |
|
1043 | - * @see TimberPost::$class |
|
1044 | - * @example |
|
1045 | - * ```twig |
|
1046 | - * <article class="{{ post.class }}"> |
|
1047 | - * {# Some stuff here #} |
|
1048 | - * </article> |
|
1049 | - * ``` |
|
1050 | - * |
|
1051 | - * ```html |
|
1052 | - * <article class="post-2612 post type-post status-publish format-standard has-post-thumbnail hentry category-data tag-charleston-church-shooting tag-dylann-roof tag-gun-violence tag-hate-crimes tag-national-incident-based-reporting-system"> |
|
1053 | - * {# Some stuff here #} |
|
1054 | - * </article> |
|
1055 | - * ``` |
|
1056 | - * @return string a space-seperated list of classes |
|
1057 | - */ |
|
1058 | - public function post_class( $class='' ) { |
|
1059 | - global $post; |
|
1060 | - $old_global_post = $post; |
|
1061 | - $post = $this; |
|
1062 | - $class_array = get_post_class($class, $this->ID); |
|
1063 | - $post = $old_global_post; |
|
1064 | - if ( is_array($class_array) ){ |
|
1065 | - return implode(' ', $class_array); |
|
1066 | - } |
|
1067 | - return $class_array; |
|
1068 | - } |
|
1069 | - |
|
1070 | - // Docs |
|
1071 | - |
|
1072 | - /** |
|
1073 | - * @return array |
|
1074 | - * @codeCoverageIgnore |
|
1075 | - */ |
|
1076 | - public function get_method_values() { |
|
1077 | - $ret = parent::get_method_values(); |
|
1078 | - $ret['author'] = $this->author(); |
|
1079 | - $ret['categories'] = $this->categories(); |
|
1080 | - $ret['category'] = $this->category(); |
|
1081 | - $ret['children'] = $this->children(); |
|
1082 | - $ret['comments'] = $this->comments(); |
|
1083 | - $ret['content'] = $this->content(); |
|
1084 | - $ret['edit_link'] = $this->edit_link(); |
|
1085 | - $ret['format'] = $this->format(); |
|
1086 | - $ret['link'] = $this->link(); |
|
1087 | - $ret['next'] = $this->next(); |
|
1088 | - $ret['pagination'] = $this->pagination(); |
|
1089 | - $ret['parent'] = $this->parent(); |
|
1090 | - $ret['path'] = $this->path(); |
|
1091 | - $ret['prev'] = $this->prev(); |
|
1092 | - $ret['terms'] = $this->terms(); |
|
1093 | - $ret['tags'] = $this->tags(); |
|
1094 | - $ret['thumbnail'] = $this->thumbnail(); |
|
1095 | - $ret['title'] = $this->title(); |
|
1096 | - return $ret; |
|
1097 | - } |
|
1098 | - |
|
1099 | - /** |
|
1100 | - * Return the author of a post |
|
1101 | - * @api |
|
1102 | - * @example |
|
1103 | - * ```twig |
|
1104 | - * <h1>{{post.title}}</h1> |
|
1105 | - * <p class="byline"> |
|
1106 | - * <a href="{{post.author.link}}">{{post.author.name}}</a> |
|
1107 | - * </p> |
|
1108 | - * ``` |
|
1109 | - * @return TimberUser|bool A TimberUser object if found, false if not |
|
1110 | - */ |
|
1111 | - public function author() { |
|
1112 | - return $this->get_author(); |
|
1113 | - } |
|
1114 | - |
|
1115 | - /** |
|
1116 | - * Get the author (WordPress user) who last modified the post |
|
1117 | - * @example |
|
1118 | - * ```twig |
|
1119 | - * Last updated by {{ post.modified_author.name }} |
|
1120 | - * ``` |
|
1121 | - * ```html |
|
1122 | - * Last updated by Harper Lee |
|
1123 | - * ``` |
|
1124 | - * @return TimberUser|bool A TimberUser object if found, false if not |
|
1125 | - */ |
|
1126 | - public function modified_author() { |
|
1127 | - return $this->get_modified_author(); |
|
1128 | - } |
|
1129 | - |
|
1130 | - /** |
|
1131 | - * Get the categoires on a particular post |
|
1132 | - * @api |
|
1133 | - * @return array of TimberTerms |
|
1134 | - */ |
|
1135 | - public function categories() { |
|
1136 | - return $this->get_terms('category'); |
|
1137 | - } |
|
1138 | - |
|
1139 | - /** |
|
1140 | - * Returns a category attached to a post |
|
1141 | - * @api |
|
1142 | - * If mulitpuile categories are set, it will return just the first one |
|
1143 | - * @return TimberTerm|null |
|
1144 | - */ |
|
1145 | - public function category() { |
|
1146 | - return $this->get_category(); |
|
1147 | - } |
|
1148 | - |
|
1149 | - /** |
|
1150 | - * Returns an array of children on the post as TimberPosts |
|
1151 | - * (or other claass as you define). |
|
1152 | - * @api |
|
1153 | - * @example |
|
1154 | - * ```twig |
|
1155 | - * {% if post.children %} |
|
1156 | - * Here are the child pages: |
|
1157 | - * {% for child in page.children %} |
|
1158 | - * <a href="{{ child.link }}">{{ child.title }}</a> |
|
1159 | - * {% endfor %} |
|
1160 | - * {% endif %} |
|
1161 | - * ``` |
|
1162 | - * @param string $post_type _optional_ use to find children of a particular post type (attachment vs. page for example). You might want to restrict to certain types of children in case other stuff gets all mucked in there. You can use 'parent' to use the parent's post type |
|
1163 | - * @param string|bool $childPostClass _optional_ a custom post class (ex: 'MyTimberPost') to return the objects as. By default (false) it will use TimberPost::$post_class value. |
|
1164 | - * @return array |
|
1165 | - */ |
|
1166 | - public function children( $post_type = 'any', $childPostClass = false ) { |
|
1167 | - return $this->get_children( $post_type, $childPostClass ); |
|
1168 | - } |
|
1169 | - |
|
1170 | - /** |
|
1171 | - * Gets the comments on a TimberPost and returns them as an array of [TimberComments](#TimberComment) (or whatever comment class you set). |
|
1172 | - * @api |
|
1173 | - * @param int $count Set the number of comments you want to get. `0` is analogous to "all" |
|
1174 | - * @param string $order use ordering set in WordPress admin, or a different scheme |
|
1175 | - * @param string $type For when other plugins use the comments table for their own special purposes, might be set to 'liveblog' or other depending on what's stored in yr comments table |
|
1176 | - * @param string $status Could be 'pending', etc. |
|
1177 | - * @param string $CommentClass What class to use when returning Comment objects. As you become a Timber pro, you might find yourself extending TimberComment for your site or app (obviously, totally optional) |
|
1178 | - * @example |
|
1179 | - * ```twig |
|
1180 | - * {# single.twig #} |
|
1181 | - * <h4>Comments:</h4> |
|
1182 | - * {% for comment in post.comments %} |
|
1183 | - * <div class="comment-{{comment.ID}} comment-order-{{loop.index}}"> |
|
1184 | - * <p>{{comment.author.name}} said:</p> |
|
1185 | - * <p>{{comment.content}}</p> |
|
1186 | - * </div> |
|
1187 | - * {% endfor %} |
|
1188 | - * ``` |
|
1189 | - * @return bool|array |
|
1190 | - */ |
|
1191 | - public function comments( $count = 0, $order = 'wp', $type = 'comment', $status = 'approve', $CommentClass = 'TimberComment' ) { |
|
1192 | - return $this->get_comments($count, $order, $type, $status, $CommentClass); |
|
1193 | - } |
|
1194 | - |
|
1195 | - /** |
|
1196 | - * Gets the actual content of a WP Post, as opposed to post_content this will run the hooks/filters attached to the_content. \This guy will return your posts content with WordPress filters run on it (like for shortcodes and wpautop). |
|
1197 | - * @api |
|
1198 | - * @example |
|
1199 | - * ```twig |
|
1200 | - * <div class="article"> |
|
1201 | - * <h2>{{post.title}}</h2> |
|
1202 | - * <div class="content">{{ post.content }}</div> |
|
1203 | - * </div> |
|
1204 | - * ``` |
|
1205 | - * @param int $page |
|
1206 | - * @return string |
|
1207 | - */ |
|
1208 | - public function content( $page = 0 ) { |
|
1209 | - return $this->get_content(0, $page); |
|
1210 | - } |
|
1211 | - |
|
1212 | - /** |
|
1213 | - * @return string |
|
1214 | - */ |
|
1215 | - public function paged_content() { |
|
1216 | - return $this->get_paged_content(); |
|
1217 | - } |
|
1218 | - |
|
1219 | - /** |
|
1220 | - * Get the date to use in your template! |
|
1221 | - * @api |
|
1222 | - * @example |
|
1223 | - * ```twig |
|
1224 | - * Published on {{ post.date }} // Uses WP's formatting set in Admin |
|
1225 | - * OR |
|
1226 | - * Published on {{ post.date | date('F jS') }} // Jan 12th |
|
1227 | - * ``` |
|
1228 | - * |
|
1229 | - * ```html |
|
1230 | - * Published on January 12, 2015 |
|
1231 | - * OR |
|
1232 | - * Published on Jan 12th |
|
1233 | - * ``` |
|
1234 | - * @param string $date_format |
|
1235 | - * @return string |
|
1236 | - */ |
|
1237 | - public function date( $date_format = '' ) { |
|
1238 | - return $this->get_date($date_format); |
|
1239 | - } |
|
1240 | - |
|
1241 | - /** |
|
1242 | - * Get the time to use in your template |
|
1243 | - * @api |
|
1244 | - * @example |
|
1245 | - * ```twig |
|
1246 | - * Published at {{ post.time }} // Uses WP's formatting set in Admin |
|
1247 | - * OR |
|
1248 | - * Published at {{ post.time | time('G:i') }} // 13:25 |
|
1249 | - * ``` |
|
1250 | - * |
|
1251 | - * ```html |
|
1252 | - * Published at 1:25 pm |
|
1253 | - * OR |
|
1254 | - * Published at 13:25 |
|
1255 | - * ``` |
|
1256 | - * @param string $time_format |
|
1257 | - * @return string |
|
1258 | - */ |
|
1259 | - public function time( $time_format = '' ) { |
|
1260 | - $tf = $time_format ? $time_format : get_option('time_format'); |
|
1261 | - $the_time = (string)mysql2date($tf, $this->post_date); |
|
1262 | - return apply_filters('get_the_time', $the_time, $tf); |
|
1263 | - } |
|
1264 | - |
|
1265 | - /** |
|
1266 | - * @return bool|string |
|
1267 | - */ |
|
1268 | - public function edit_link() { |
|
1269 | - return $this->get_edit_url(); |
|
1270 | - } |
|
1271 | - |
|
1272 | - /** |
|
1273 | - * @api |
|
1274 | - * @return mixed |
|
1275 | - */ |
|
1276 | - public function format() { |
|
1277 | - return $this->get_format(); |
|
1278 | - } |
|
1279 | - |
|
1280 | - /** |
|
1281 | - * get the permalink for a post object |
|
1282 | - * @api |
|
1283 | - * @example |
|
1284 | - * ```twig |
|
1285 | - * <a href="{{post.link}}">Read my post</a> |
|
1286 | - * ``` |
|
1287 | - * @return string ex: http://example.org/2015/07/my-awesome-post |
|
1288 | - */ |
|
1289 | - public function link() { |
|
1290 | - return $this->get_permalink(); |
|
1291 | - } |
|
1292 | - |
|
1293 | - /** |
|
1294 | - * @param string $field_name |
|
1295 | - * @return mixed |
|
1296 | - */ |
|
1297 | - public function meta( $field_name = null ) { |
|
1298 | - if ( $field_name === null ) { |
|
1299 | - //on the off-chance the field is actually named meta |
|
1300 | - $field_name = 'meta'; |
|
1301 | - } |
|
1302 | - return $this->get_field($field_name); |
|
1303 | - } |
|
1304 | - |
|
1305 | - /** |
|
1306 | - * @return string |
|
1307 | - */ |
|
1308 | - public function name(){ |
|
1309 | - return $this->title(); |
|
1310 | - } |
|
1311 | - |
|
1312 | - /** |
|
1313 | - * @param string $date_format |
|
1314 | - * @return string |
|
1315 | - */ |
|
1316 | - public function modified_date( $date_format = '' ) { |
|
1317 | - return $this->get_modified_date($date_format); |
|
1318 | - } |
|
1319 | - |
|
1320 | - /** |
|
1321 | - * @param string $time_format |
|
1322 | - * @return string |
|
1323 | - */ |
|
1324 | - public function modified_time( $time_format = '' ) { |
|
1325 | - return $this->get_modified_time($time_format); |
|
1326 | - } |
|
1327 | - |
|
1328 | - /** |
|
1329 | - * @api |
|
1330 | - * @param bool $in_same_cat |
|
1331 | - * @return mixed |
|
1332 | - */ |
|
1333 | - public function next( $in_same_cat = false ) { |
|
1334 | - return $this->get_next($in_same_cat); |
|
1335 | - } |
|
1336 | - |
|
1337 | - /** |
|
1338 | - * @return array |
|
1339 | - */ |
|
1340 | - public function pagination() { |
|
1341 | - return $this->get_pagination(); |
|
1342 | - } |
|
1343 | - |
|
1344 | - /** |
|
1345 | - * Gets the parent (if one exists) from a post as a TimberPost object (or whatever is set in TimberPost::$PostClass) |
|
1346 | - * @api |
|
1347 | - * @example |
|
1348 | - * ```twig |
|
1349 | - * Parent page: <a href="{{ post.parent.link }}">{{ post.parent.title }}</a> |
|
1350 | - * ``` |
|
1351 | - * @return bool|TimberPost |
|
1352 | - */ |
|
1353 | - public function parent() { |
|
1354 | - return $this->get_parent(); |
|
1355 | - } |
|
1356 | - |
|
1357 | - /** |
|
1358 | - * Gets the relative path of a WP Post, so while link() will return http://example.org/2015/07/my-cool-post |
|
1359 | - * this will return just /2015/07/my-cool-post |
|
1360 | - * @api |
|
1361 | - * @example |
|
1362 | - * ```twig |
|
1363 | - * <a href="{{post.path}}">{{post.title}}</a> |
|
1364 | - * ``` |
|
1365 | - * @return string |
|
1366 | - */ |
|
1367 | - public function path() { |
|
1368 | - return $this->get_path(); |
|
1369 | - } |
|
1370 | - |
|
1371 | - /** |
|
1372 | - * @deprecated 0.20.0 use link() instead |
|
1373 | - * @return string |
|
1374 | - */ |
|
1375 | - public function permalink() { |
|
1376 | - return $this->get_permalink(); |
|
1377 | - } |
|
1378 | - |
|
1379 | - /** |
|
1380 | - * Get the previous post in a set |
|
1381 | - * @api |
|
1382 | - * @example |
|
1383 | - * ```twig |
|
1384 | - * <h4>Prior Entry:</h4> |
|
1385 | - * <h3>{{post.prev.title}}</h3> |
|
1386 | - * <p>{{post.prev.get_preview(25)}}</p> |
|
1387 | - * ``` |
|
1388 | - * @param bool $in_same_cat |
|
1389 | - * @return mixed |
|
1390 | - */ |
|
1391 | - public function prev( $in_same_cat = false ) { |
|
1392 | - return $this->get_prev($in_same_cat); |
|
1393 | - } |
|
1394 | - |
|
1395 | - /** |
|
1396 | - * Get the terms associated with the post |
|
1397 | - * This goes across all taxonomies by default |
|
1398 | - * @api |
|
1399 | - * @param string|array $tax What taxonom(y|ies) to pull from. Defaults to all registered taxonomies for the post type. You can use custom ones, or built-in WordPress taxonomies (category, tag). Timber plays nice and figures out that tag/tags/post_tag are all the same (and categories/category), for custom taxonomies you're on your own. |
|
1400 | - * @param bool $merge Should the resulting array be one big one (true)? Or should it be an array of sub-arrays for each taxonomy (false)? |
|
1401 | - * @return array |
|
1402 | - */ |
|
1403 | - public function terms( $tax = '', $merge = true ) { |
|
1404 | - return $this->get_terms($tax, $merge); |
|
1405 | - } |
|
1406 | - |
|
1407 | - /** |
|
1408 | - * Gets the tags on a post, uses WP's post_tag taxonomy |
|
1409 | - * @api |
|
1410 | - * @return array |
|
1411 | - */ |
|
1412 | - public function tags() { |
|
1413 | - return $this->get_tags(); |
|
1414 | - } |
|
1415 | - |
|
1416 | - /** |
|
1417 | - * get the featured image as a TimberImage |
|
1418 | - * @api |
|
1419 | - * @example |
|
1420 | - * ```twig |
|
1421 | - * <img src="{{post.thumbnail.src}}" /> |
|
1422 | - * ``` |
|
1423 | - * @return TimberImage|null of your thumbnail |
|
1424 | - */ |
|
1425 | - public function thumbnail() { |
|
1426 | - return $this->get_thumbnail(); |
|
1427 | - } |
|
1428 | - |
|
1429 | - /** |
|
1430 | - * Returns the processed title to be used in templates. This returns the title of the post after WP's filters have run. This is analogous to `the_title()` in standard WP template tags. |
|
1431 | - * @api |
|
1432 | - * @example |
|
1433 | - * ```twig |
|
1434 | - * <h1>{{ post.title }}</h1> |
|
1435 | - * ``` |
|
1436 | - * @return string |
|
1437 | - */ |
|
1438 | - public function title() { |
|
1439 | - return $this->get_title(); |
|
1440 | - } |
|
37 | + /** |
|
38 | + * @var string $ImageClass the name of the class to handle images by default |
|
39 | + */ |
|
40 | + public $ImageClass = 'TimberImage'; |
|
41 | + |
|
42 | + /** |
|
43 | + * @var string $PostClass the name of the class to handle posts by default |
|
44 | + */ |
|
45 | + public $PostClass = 'TimberPost'; |
|
46 | + |
|
47 | + /** |
|
48 | + * @var string $TermClass the name of the class to handle terms by default |
|
49 | + */ |
|
50 | + public $TermClass = 'TimberTerm'; |
|
51 | + |
|
52 | + /** |
|
53 | + * @var string $object_type what does this class represent in WordPress terms? |
|
54 | + */ |
|
55 | + public $object_type = 'post'; |
|
56 | + |
|
57 | + /** |
|
58 | + * @var string $representation what does this class represent in WordPress terms? |
|
59 | + */ |
|
60 | + public static $representation = 'post'; |
|
61 | + |
|
62 | + /** |
|
63 | + * @internal |
|
64 | + * @var string $_content stores the processed content internally |
|
65 | + */ |
|
66 | + protected $_content; |
|
67 | + |
|
68 | + /** |
|
69 | + * @internal |
|
70 | + * @var array $_get_terms stores the results of a get_terms method call |
|
71 | + * @deprecated |
|
72 | + */ |
|
73 | + protected $_get_terms; |
|
74 | + |
|
75 | + /** |
|
76 | + * @var string $_permalink the returned permalink from WP's get_permalink function |
|
77 | + */ |
|
78 | + protected $_permalink; |
|
79 | + |
|
80 | + /** |
|
81 | + * @var array $_next stores the results of the next TimberPost in a set inside an array (in order to manage by-taxonomy) |
|
82 | + */ |
|
83 | + protected $_next = array(); |
|
84 | + |
|
85 | + /** |
|
86 | + * @var array $_prev stores the results of the previous TimberPost in a set inside an array (in order to manage by-taxonomy) |
|
87 | + */ |
|
88 | + protected $_prev = array(); |
|
89 | + |
|
90 | + /** |
|
91 | + * @api |
|
92 | + * @var string $class stores the CSS classes for the post (ex: "post post-type-book post-123") |
|
93 | + */ |
|
94 | + public $class; |
|
95 | + |
|
96 | + /** |
|
97 | + * @api |
|
98 | + * @var string $id the numeric WordPress id of a post |
|
99 | + */ |
|
100 | + public $id; |
|
101 | + |
|
102 | + /** |
|
103 | + * @var string $ID the numeric WordPress id of a post, capitalized to match WP usage |
|
104 | + */ |
|
105 | + public $ID; |
|
106 | + |
|
107 | + /** |
|
108 | + * @var int $post_author the numeric ID of the a post's author corresponding to the wp_user dtable |
|
109 | + */ |
|
110 | + public $post_author; |
|
111 | + |
|
112 | + /** |
|
113 | + * @var string $post_content the raw text of a WP post as stored in the database |
|
114 | + */ |
|
115 | + public $post_content; |
|
116 | + |
|
117 | + /** |
|
118 | + * @var string $post_date the raw date string as stored in the WP database, ex: 2014-07-05 18:01:39 |
|
119 | + */ |
|
120 | + public $post_date; |
|
121 | + |
|
122 | + /** |
|
123 | + * @var string $post_exceprt the raw text of a manual post exceprt as stored in the database |
|
124 | + */ |
|
125 | + public $post_excerpt; |
|
126 | + |
|
127 | + /** |
|
128 | + * @var int $post_parent the numeric ID of a post's parent post |
|
129 | + */ |
|
130 | + public $post_parent; |
|
131 | + |
|
132 | + /** |
|
133 | + * @api |
|
134 | + * @var string $post_status the status of a post ("draft", "publish", etc.) |
|
135 | + */ |
|
136 | + public $post_status; |
|
137 | + |
|
138 | + /** |
|
139 | + * @var string $post_title the raw text of a post's title as stored in the database |
|
140 | + */ |
|
141 | + public $post_title; |
|
142 | + |
|
143 | + /** |
|
144 | + * @api |
|
145 | + * @var string $post_type the name of the post type, this is the machine name (so "my_custom_post_type" as opposed to "My Custom Post Type") |
|
146 | + */ |
|
147 | + public $post_type; |
|
148 | + |
|
149 | + /** |
|
150 | + * @api |
|
151 | + * @var string $slug the URL-safe slug, this corresponds to the poorly-named "post_name" in the WP database, ex: "hello-world" |
|
152 | + */ |
|
153 | + public $slug; |
|
154 | + |
|
155 | + /** |
|
156 | + * If you send the constructor nothing it will try to figure out the current post id based on being inside The_Loop |
|
157 | + * @example |
|
158 | + * ```php |
|
159 | + * $post = new TimberPost(); |
|
160 | + * $other_post = new TimberPost($random_post_id); |
|
161 | + * ``` |
|
162 | + * @param mixed $pid |
|
163 | + */ |
|
164 | + public function __construct($pid = null) { |
|
165 | + $pid = $this->determine_id( $pid ); |
|
166 | + $this->init($pid); |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * tries to figure out what post you want to get if not explictly defined (or if it is, allows it to be passed through) |
|
171 | + * @internal |
|
172 | + * @param mixed a value to test against |
|
173 | + * @return int the numberic id we should be using for this post object |
|
174 | + */ |
|
175 | + protected function determine_id($pid) { |
|
176 | + global $wp_query; |
|
177 | + if ( $pid === null && |
|
178 | + isset($wp_query->queried_object_id) |
|
179 | + && $wp_query->queried_object_id |
|
180 | + && isset($wp_query->queried_object) |
|
181 | + && is_object($wp_query->queried_object) |
|
182 | + && get_class($wp_query->queried_object) == 'WP_Post' |
|
183 | + ) { |
|
184 | + if( isset( $_GET['preview'] ) && isset( $_GET['preview_nonce'] ) && wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . $wp_query->queried_object_id ) ) { |
|
185 | + $pid = $this->get_post_preview_id( $wp_query ); |
|
186 | + } else if ( !$pid ) { |
|
187 | + $pid = $wp_query->queried_object_id; |
|
188 | + } |
|
189 | + } else if ( $pid === null && $wp_query->is_home && isset($wp_query->queried_object_id) && $wp_query->queried_object_id ) { |
|
190 | + //hack for static page as home page |
|
191 | + $pid = $wp_query->queried_object_id; |
|
192 | + } else if ( $pid === null ) { |
|
193 | + $gtid = false; |
|
194 | + $maybe_post = get_post(); |
|
195 | + if ( isset($maybe_post->ID) ){ |
|
196 | + $gtid = true; |
|
197 | + } |
|
198 | + if ( $gtid ) { |
|
199 | + $pid = get_the_ID(); |
|
200 | + } |
|
201 | + if ( !$pid ) { |
|
202 | + global $wp_query; |
|
203 | + if ( isset($wp_query->query['p']) ) { |
|
204 | + $pid = $wp_query->query['p']; |
|
205 | + } |
|
206 | + } |
|
207 | + } |
|
208 | + if ( $pid === null && ($pid_from_loop = TimberPostGetter::loop_to_id()) ) { |
|
209 | + $pid = $pid_from_loop; |
|
210 | + } |
|
211 | + return $pid; |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * Outputs the title of the post if you do something like `<h1>{{post}}</h1>` |
|
216 | + * @return string |
|
217 | + */ |
|
218 | + public function __toString() { |
|
219 | + return $this->title(); |
|
220 | + } |
|
221 | + |
|
222 | + protected function get_post_preview_id( $query ) { |
|
223 | + $can = array( |
|
224 | + 'edit_' . $query->queried_object->post_type . 's', |
|
225 | + ); |
|
226 | + |
|
227 | + if ( $query->queried_object->author_id !== get_current_user_id() ) { |
|
228 | + $can[] = 'edit_others_' . $query->queried_object->post_type . 's'; |
|
229 | + } |
|
230 | + |
|
231 | + $can_preview = array(); |
|
232 | + |
|
233 | + foreach( $can as $type ) { |
|
234 | + if( current_user_can( $type ) ) { |
|
235 | + $can_preview[] = true; |
|
236 | + } |
|
237 | + } |
|
238 | + |
|
239 | + if ( count( $can_preview ) !== count( $can ) ) { |
|
240 | + return; |
|
241 | + } |
|
242 | + |
|
243 | + $revisions = wp_get_post_revisions( $query->queried_object_id ); |
|
244 | + |
|
245 | + if( !empty( $revisions ) ) { |
|
246 | + $last = end($revisions); |
|
247 | + return $last->ID; |
|
248 | + } |
|
249 | + |
|
250 | + return false; |
|
251 | + } |
|
252 | + |
|
253 | + /** |
|
254 | + * Initializes a TimberPost |
|
255 | + * @internal |
|
256 | + * @param int|bool $pid |
|
257 | + */ |
|
258 | + protected function init($pid = false) { |
|
259 | + if ( $pid === false ) { |
|
260 | + $pid = get_the_ID(); |
|
261 | + } |
|
262 | + if ( is_numeric($pid) ) { |
|
263 | + $this->ID = $pid; |
|
264 | + } |
|
265 | + $post_info = $this->get_info($pid); |
|
266 | + $this->import($post_info); |
|
267 | + //cant have a function, so gots to do it this way |
|
268 | + $post_class = $this->post_class(); |
|
269 | + $this->class = $post_class; |
|
270 | + } |
|
271 | + |
|
272 | + /** |
|
273 | + * Get the URL that will edit the current post/object |
|
274 | + * @internal |
|
275 | + * @see TimberPost::edit_link |
|
276 | + * @return bool|string |
|
277 | + */ |
|
278 | + function get_edit_url() { |
|
279 | + if ( $this->can_edit() ) { |
|
280 | + return get_edit_post_link($this->ID); |
|
281 | + } |
|
282 | + } |
|
283 | + |
|
284 | + /** |
|
285 | + * updates the post_meta of the current object with the given value |
|
286 | + * @param string $field |
|
287 | + * @param mixed $value |
|
288 | + */ |
|
289 | + public function update( $field, $value ) { |
|
290 | + if ( isset($this->ID) ) { |
|
291 | + update_post_meta($this->ID, $field, $value); |
|
292 | + $this->$field = $value; |
|
293 | + } |
|
294 | + } |
|
295 | + |
|
296 | + |
|
297 | + /** |
|
298 | + * takes a mix of integer (post ID), string (post slug), |
|
299 | + * or object to return a WordPress post object from WP's built-in get_post() function |
|
300 | + * @internal |
|
301 | + * @param mixed $pid |
|
302 | + * @return WP_Post on success |
|
303 | + */ |
|
304 | + protected function prepare_post_info( $pid = 0 ) { |
|
305 | + if ( is_string($pid) || is_numeric($pid) || (is_object($pid) && !isset($pid->post_title)) || $pid === 0 ) { |
|
306 | + $pid = self::check_post_id($pid); |
|
307 | + $post = get_post($pid); |
|
308 | + if ( $post ) { |
|
309 | + return $post; |
|
310 | + } |
|
311 | + } |
|
312 | + //we can skip if already is WP_Post |
|
313 | + return $pid; |
|
314 | + } |
|
315 | + |
|
316 | + |
|
317 | + /** |
|
318 | + * helps you find the post id regardless of whether you send a string or whatever |
|
319 | + * @param integer $pid ; |
|
320 | + * @internal |
|
321 | + * @return integer ID number of a post |
|
322 | + */ |
|
323 | + protected function check_post_id( $pid ) { |
|
324 | + if ( is_numeric($pid) && $pid === 0 ) { |
|
325 | + $pid = get_the_ID(); |
|
326 | + return $pid; |
|
327 | + } |
|
328 | + if ( !is_numeric($pid) && is_string($pid) ) { |
|
329 | + $pid = self::get_post_id_by_name($pid); |
|
330 | + return $pid; |
|
331 | + } |
|
332 | + if ( !$pid ) { |
|
333 | + return null; |
|
334 | + } |
|
335 | + return $pid; |
|
336 | + } |
|
337 | + |
|
338 | + |
|
339 | + /** |
|
340 | + * get_post_id_by_name($post_name) |
|
341 | + * @internal |
|
342 | + * @param string $post_name |
|
343 | + * @return int |
|
344 | + */ |
|
345 | + static function get_post_id_by_name($post_name) { |
|
346 | + global $wpdb; |
|
347 | + $query = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_name = %s LIMIT 1", $post_name); |
|
348 | + $result = $wpdb->get_row($query); |
|
349 | + if (!$result) { |
|
350 | + return null; |
|
351 | + } |
|
352 | + return $result->ID; |
|
353 | + } |
|
354 | + |
|
355 | + /** |
|
356 | + * get a preview of your post, if you have an excerpt it will use that, |
|
357 | + * otherwise it will pull from the post_content. |
|
358 | + * If there's a <!-- more --> tag it will use that to mark where to pull through. |
|
359 | + * @api |
|
360 | + * @example |
|
361 | + * ```twig |
|
362 | + * <p>{{post.get_preview(50)}}</p> |
|
363 | + * ``` |
|
364 | + * @param int $len The number of words that WP should use to make the tease. (Isn't this better than [this mess](http://wordpress.org/support/topic/changing-the-default-length-of-the_excerpt-1?replies=14)?). If you've set a post_excerpt on a post, we'll use that for the preview text; otherwise the first X words of the post_content |
|
365 | + * @param bool $force What happens if your custom post excerpt is longer then the length requested? By default (`$force = false`) it will use the full `post_excerpt`. However, you can set this to true to *force* your excerpt to be of the desired length |
|
366 | + * @param string $readmore The text you want to use on the 'readmore' link |
|
367 | + * @param bool|string $strip true for default, false for none, string for list of custom attributes |
|
368 | + * @param string $end The text to end the preview with (defaults to ...) |
|
369 | + * @return string of the post preview |
|
370 | + */ |
|
371 | + function get_preview($len = 50, $force = false, $readmore = 'Read More', $strip = true, $end = '…') { |
|
372 | + $text = ''; |
|
373 | + $trimmed = false; |
|
374 | + if ( isset($this->post_excerpt) && strlen($this->post_excerpt) ) { |
|
375 | + if ( $force ) { |
|
376 | + $text = TimberHelper::trim_words($this->post_excerpt, $len, false); |
|
377 | + $trimmed = true; |
|
378 | + } else { |
|
379 | + $text = $this->post_excerpt; |
|
380 | + } |
|
381 | + } |
|
382 | + if ( !strlen($text) && preg_match('/<!--\s?more(.*?)?-->/', $this->post_content, $readmore_matches) ) { |
|
383 | + $pieces = explode($readmore_matches[0], $this->post_content); |
|
384 | + $text = $pieces[0]; |
|
385 | + if ( $force ) { |
|
386 | + $text = TimberHelper::trim_words($text, $len, false); |
|
387 | + $trimmed = true; |
|
388 | + } |
|
389 | + $text = do_shortcode( $text ); |
|
390 | + } |
|
391 | + if ( !strlen($text) ) { |
|
392 | + $text = TimberHelper::trim_words($this->get_content(), $len, false); |
|
393 | + $trimmed = true; |
|
394 | + } |
|
395 | + if ( !strlen(trim($text)) ) { |
|
396 | + return trim($text); |
|
397 | + } |
|
398 | + if ( $strip ) { |
|
399 | + $allowable_tags = (is_string($strip)) ? $strip : null; |
|
400 | + $text = trim(strip_tags($text, $allowable_tags)); |
|
401 | + } |
|
402 | + if ( strlen($text) ) { |
|
403 | + $text = trim($text); |
|
404 | + $last = $text[strlen($text) - 1]; |
|
405 | + if ( $last != '.' && $trimmed ) { |
|
406 | + $text .= $end; |
|
407 | + } |
|
408 | + if ( !$strip ) { |
|
409 | + $last_p_tag = strrpos($text, '</p>'); |
|
410 | + if ( $last_p_tag !== false ) { |
|
411 | + $text = substr($text, 0, $last_p_tag); |
|
412 | + } |
|
413 | + if ( $last != '.' && $trimmed ) { |
|
414 | + $text .= $end . ' '; |
|
415 | + } |
|
416 | + } |
|
417 | + $read_more_class = apply_filters('timber/post/get_preview/read_more_class', "read-more"); |
|
418 | + if ( $readmore && isset($readmore_matches) && !empty($readmore_matches[1]) ) { |
|
419 | + $text .= ' <a href="' . $this->get_permalink() . '" class="'.$read_more_class .'">' . trim($readmore_matches[1]) . '</a>'; |
|
420 | + } elseif ( $readmore ) { |
|
421 | + $text .= ' <a href="' . $this->get_permalink() . '" class="'.$read_more_class .'">' . trim($readmore) . '</a>'; |
|
422 | + } |
|
423 | + if ( !$strip && $last_p_tag && ( strpos($text, '<p>') || strpos($text, '<p ') ) ) { |
|
424 | + $text .= '</p>'; |
|
425 | + } |
|
426 | + } |
|
427 | + return trim($text); |
|
428 | + } |
|
429 | + |
|
430 | + /** |
|
431 | + * gets the post custom and attaches it to the current object |
|
432 | + * @internal |
|
433 | + * @param bool|int $pid a post ID number |
|
434 | + */ |
|
435 | + function import_custom( $pid = false ) { |
|
436 | + if ( !$pid ) { |
|
437 | + $pid = $this->ID; |
|
438 | + } |
|
439 | + $customs = $this->get_post_custom($pid); |
|
440 | + $this->import($customs); |
|
441 | + } |
|
442 | + |
|
443 | + /** |
|
444 | + * Used internally to fetch the metadata fields (wp_postmeta table) |
|
445 | + * and attach them to our TimberPost object |
|
446 | + * @internal |
|
447 | + * @param int $pid |
|
448 | + * @return array |
|
449 | + */ |
|
450 | + protected function get_post_custom( $pid ) { |
|
451 | + apply_filters('timber_post_get_meta_pre', array(), $pid, $this); |
|
452 | + $customs = get_post_custom($pid); |
|
453 | + if ( !is_array($customs) || empty($customs) ) { |
|
454 | + return array(); |
|
455 | + } |
|
456 | + foreach ( $customs as $key => $value ) { |
|
457 | + if ( is_array($value) && count($value) == 1 && isset($value[0]) ) { |
|
458 | + $value = $value[0]; |
|
459 | + } |
|
460 | + $customs[$key] = maybe_unserialize($value); |
|
461 | + } |
|
462 | + $customs = apply_filters('timber_post_get_meta', $customs, $pid, $this); |
|
463 | + return $customs; |
|
464 | + } |
|
465 | + |
|
466 | + /** |
|
467 | + * @internal |
|
468 | + * @see TimberPost::thumbnail |
|
469 | + * @return null|TimberImage |
|
470 | + */ |
|
471 | + function get_thumbnail() { |
|
472 | + if ( function_exists('get_post_thumbnail_id') ) { |
|
473 | + $tid = get_post_thumbnail_id($this->ID); |
|
474 | + if ( $tid ) { |
|
475 | + return new $this->ImageClass($tid); |
|
476 | + } |
|
477 | + } |
|
478 | + } |
|
479 | + |
|
480 | + /** |
|
481 | + * @internal |
|
482 | + * @see TimberPost::link |
|
483 | + * @return string |
|
484 | + */ |
|
485 | + function get_permalink() { |
|
486 | + if ( isset($this->_permalink) ) { |
|
487 | + return $this->_permalink; |
|
488 | + } |
|
489 | + $this->_permalink = get_permalink($this->ID); |
|
490 | + return $this->_permalink; |
|
491 | + } |
|
492 | + |
|
493 | + /** |
|
494 | + * get the permalink for a post object |
|
495 | + * In your templates you should use link: |
|
496 | + * <a href="{{post.link}}">Read my post</a> |
|
497 | + * @internal |
|
498 | + * @return string |
|
499 | + */ |
|
500 | + function get_link() { |
|
501 | + return $this->get_permalink(); |
|
502 | + } |
|
503 | + |
|
504 | + /** |
|
505 | + * Get the next post in WordPress's ordering |
|
506 | + * @internal |
|
507 | + * @param bool $taxonomy |
|
508 | + * @return TimberPost|boolean |
|
509 | + */ |
|
510 | + function get_next( $taxonomy = false ) { |
|
511 | + if ( !isset($this->_next) || !isset($this->_next[$taxonomy]) ) { |
|
512 | + global $post; |
|
513 | + $this->_next = array(); |
|
514 | + $old_global = $post; |
|
515 | + $post = $this; |
|
516 | + if ( $taxonomy ) { |
|
517 | + $adjacent = get_adjacent_post(true, '', false, $taxonomy); |
|
518 | + } else { |
|
519 | + $adjacent = get_adjacent_post(false, '', false); |
|
520 | + } |
|
521 | + |
|
522 | + if ( $adjacent ) { |
|
523 | + $this->_next[$taxonomy] = new $this->PostClass($adjacent); |
|
524 | + } else { |
|
525 | + $this->_next[$taxonomy] = false; |
|
526 | + } |
|
527 | + $post = $old_global; |
|
528 | + } |
|
529 | + return $this->_next[$taxonomy]; |
|
530 | + } |
|
531 | + |
|
532 | + /** |
|
533 | + * Get a data array of pagination so you can navigate to the previous/next for a paginated post |
|
534 | + * @return array |
|
535 | + */ |
|
536 | + public function get_pagination() { |
|
537 | + global $post, $page, $numpages, $multipage; |
|
538 | + $post = $this; |
|
539 | + $ret = array(); |
|
540 | + if ( $multipage ) { |
|
541 | + for ( $i = 1; $i <= $numpages; $i++ ) { |
|
542 | + $link = self::get_wp_link_page($i); |
|
543 | + $data = array('name' => $i, 'title' => $i, 'text' => $i, 'link' => $link); |
|
544 | + if ( $i == $page ) { |
|
545 | + $data['current'] = true; |
|
546 | + } |
|
547 | + $ret['pages'][] = $data; |
|
548 | + } |
|
549 | + $i = $page - 1; |
|
550 | + if ( $i ) { |
|
551 | + $link = self::get_wp_link_page($i); |
|
552 | + $ret['prev'] = array('link' => $link); |
|
553 | + } |
|
554 | + $i = $page + 1; |
|
555 | + if ( $i <= $numpages ) { |
|
556 | + $link = self::get_wp_link_page($i); |
|
557 | + $ret['next'] = array('link' => $link); |
|
558 | + } |
|
559 | + } |
|
560 | + return $ret; |
|
561 | + } |
|
562 | + |
|
563 | + /** |
|
564 | + * @param int $i |
|
565 | + * @return string |
|
566 | + */ |
|
567 | + protected static function get_wp_link_page($i) { |
|
568 | + $link = _wp_link_page($i); |
|
569 | + $link = new SimpleXMLElement($link . '</a>'); |
|
570 | + if ( isset($link['href']) ) { |
|
571 | + return $link['href']; |
|
572 | + } |
|
573 | + return ''; |
|
574 | + } |
|
575 | + |
|
576 | + /** |
|
577 | + * Get the permalink for a post, but as a relative path |
|
578 | + * For example, where {{post.link}} would return "http://example.org/2015/07/04/my-cool-post" |
|
579 | + * this will return the relative version: "/2015/07/04/my-cool-post" |
|
580 | + * @internal |
|
581 | + * @return string |
|
582 | + */ |
|
583 | + function get_path() { |
|
584 | + return TimberURLHelper::get_rel_url($this->get_link()); |
|
585 | + } |
|
586 | + |
|
587 | + /** |
|
588 | + * Get the next post in WordPress's ordering |
|
589 | + * @internal |
|
590 | + * @param bool $taxonomy |
|
591 | + * @return TimberPost|boolean |
|
592 | + */ |
|
593 | + function get_prev( $taxonomy = false ) { |
|
594 | + if ( isset($this->_prev) && isset($this->_prev[$taxonomy]) ) { |
|
595 | + return $this->_prev[$taxonomy]; |
|
596 | + } |
|
597 | + global $post; |
|
598 | + $old_global = $post; |
|
599 | + $post = $this; |
|
600 | + $within_taxonomy = ($taxonomy) ? $taxonomy : 'category'; |
|
601 | + $adjacent = get_adjacent_post(($taxonomy), '', true, $within_taxonomy); |
|
602 | + $prev_in_taxonomy = false; |
|
603 | + if ( $adjacent ) { |
|
604 | + $prev_in_taxonomy = new $this->PostClass($adjacent); |
|
605 | + } |
|
606 | + $this->_prev[$taxonomy] = $prev_in_taxonomy; |
|
607 | + $post = $old_global; |
|
608 | + return $this->_prev[$taxonomy]; |
|
609 | + } |
|
610 | + |
|
611 | + /** |
|
612 | + * Get the parent post of the post |
|
613 | + * @internal |
|
614 | + * @return bool|TimberPost |
|
615 | + */ |
|
616 | + function get_parent() { |
|
617 | + if ( !$this->post_parent ) { |
|
618 | + return false; |
|
619 | + } |
|
620 | + return new $this->PostClass($this->post_parent); |
|
621 | + } |
|
622 | + |
|
623 | + /** |
|
624 | + * Gets a User object from the author of the post |
|
625 | + * @internal |
|
626 | + * @see TimberPost::author |
|
627 | + * @return bool|TimberUser |
|
628 | + */ |
|
629 | + function get_author() { |
|
630 | + if ( isset($this->post_author) ) { |
|
631 | + return new TimberUser($this->post_author); |
|
632 | + } |
|
633 | + } |
|
634 | + |
|
635 | + /** |
|
636 | + * @internal |
|
637 | + * @return bool|TimberUser |
|
638 | + */ |
|
639 | + function get_modified_author() { |
|
640 | + $user_id = get_post_meta($this->ID, '_edit_last', true); |
|
641 | + return ($user_id ? new TimberUser($user_id) : $this->get_author()); |
|
642 | + } |
|
643 | + |
|
644 | + /** |
|
645 | + * Used internally by init, etc. to build TimberPost object |
|
646 | + * @internal |
|
647 | + * @param int $pid |
|
648 | + * @return null|object|WP_Post |
|
649 | + */ |
|
650 | + protected function get_info($pid) { |
|
651 | + $post = $this->prepare_post_info($pid); |
|
652 | + if ( !isset($post->post_status) ) { |
|
653 | + return null; |
|
654 | + } |
|
655 | + $post->status = $post->post_status; |
|
656 | + $post->id = $post->ID; |
|
657 | + $post->slug = $post->post_name; |
|
658 | + $customs = $this->get_post_custom($post->ID); |
|
659 | + $post->custom = $customs; |
|
660 | + $post = (object) array_merge((array)$customs, (array)$post); |
|
661 | + return $post; |
|
662 | + } |
|
663 | + |
|
664 | + /** |
|
665 | + * @internal |
|
666 | + * @see TimberPost::date |
|
667 | + * @param string $date_format |
|
668 | + * @return string |
|
669 | + */ |
|
670 | + function get_date( $date_format = '' ) { |
|
671 | + $df = $date_format ? $date_format : get_option('date_format'); |
|
672 | + $the_date = (string)mysql2date($df, $this->post_date); |
|
673 | + return apply_filters('get_the_date', $the_date, $df); |
|
674 | + } |
|
675 | + |
|
676 | + /** |
|
677 | + * @internal |
|
678 | + * @param string $date_format |
|
679 | + * @return string |
|
680 | + */ |
|
681 | + function get_modified_date( $date_format = '' ) { |
|
682 | + $df = $date_format ? $date_format : get_option('date_format'); |
|
683 | + $the_time = $this->get_modified_time($df); |
|
684 | + return apply_filters('get_the_modified_date', $the_time, $date_format); |
|
685 | + } |
|
686 | + |
|
687 | + /** |
|
688 | + * @internal |
|
689 | + * @param string $time_format |
|
690 | + * @return string |
|
691 | + */ |
|
692 | + function get_modified_time( $time_format = '' ) { |
|
693 | + $tf = $time_format ? $time_format : get_option('time_format'); |
|
694 | + $the_time = get_post_modified_time($tf, false, $this->ID, true); |
|
695 | + return apply_filters('get_the_modified_time', $the_time, $time_format); |
|
696 | + } |
|
697 | + |
|
698 | + /** |
|
699 | + * @internal |
|
700 | + * @see TimberPost::children |
|
701 | + * @param string $post_type |
|
702 | + * @param bool|string $childPostClass |
|
703 | + * @return array |
|
704 | + */ |
|
705 | + function get_children( $post_type = 'any', $childPostClass = false ) { |
|
706 | + if ( $childPostClass === false ) { |
|
707 | + $childPostClass = $this->PostClass; |
|
708 | + } |
|
709 | + if ( $post_type == 'parent' ) { |
|
710 | + $post_type = $this->post_type; |
|
711 | + } |
|
712 | + $children = get_children('post_parent=' . $this->ID . '&post_type=' . $post_type . '&numberposts=-1&orderby=menu_order title&order=ASC&post_status=publish'); |
|
713 | + foreach ( $children as &$child ) { |
|
714 | + $child = new $childPostClass($child->ID); |
|
715 | + } |
|
716 | + $children = array_values($children); |
|
717 | + return $children; |
|
718 | + } |
|
719 | + |
|
720 | + |
|
721 | + /** |
|
722 | + * Get the comments for a post |
|
723 | + * @internal |
|
724 | + * @see TimberPost::comments |
|
725 | + * @param int $ct |
|
726 | + * @param string $order |
|
727 | + * @param string $type |
|
728 | + * @param string $status |
|
729 | + * @param string $CommentClass |
|
730 | + * @return array|mixed |
|
731 | + */ |
|
732 | + |
|
733 | + function get_comments($ct = 0, $order = 'wp', $type = 'comment', $status = 'approve', $CommentClass = 'TimberComment') { |
|
734 | + |
|
735 | + global $overridden_cpage, $user_ID; |
|
736 | + $overridden_cpage = false; |
|
737 | + |
|
738 | + $commenter = wp_get_current_commenter(); |
|
739 | + $comment_author_email = $commenter['comment_author_email']; |
|
740 | + |
|
741 | + $args = array('post_id' => $this->ID, 'status' => $status, 'order' => $order); |
|
742 | + if ( $ct > 0 ) { |
|
743 | + $args['number'] = $ct; |
|
744 | + } |
|
745 | + if ( strtolower($order) == 'wp' || strtolower($order) == 'wordpress' ) { |
|
746 | + $args['order'] = get_option('comment_order'); |
|
747 | + } |
|
748 | + |
|
749 | + if ( $user_ID ) { |
|
750 | + $args['include_unapproved'] = array( $user_ID ); |
|
751 | + } elseif ( ! empty( $comment_author_email ) ) { |
|
752 | + $args['include_unapproved'] = array( $comment_author_email ); |
|
753 | + } |
|
754 | + |
|
755 | + $comments = get_comments($args); |
|
756 | + $timber_comments = array(); |
|
757 | + |
|
758 | + if ( '' == get_query_var('cpage') && get_option('page_comments') ) { |
|
759 | + set_query_var( 'cpage', 'newest' == get_option('default_comments_page') ? get_comment_pages_count() : 1 ); |
|
760 | + $overridden_cpage = true; |
|
761 | + } |
|
762 | + |
|
763 | + foreach($comments as $key => &$comment) { |
|
764 | + $timber_comment = new $CommentClass($comment); |
|
765 | + $timber_comments[$timber_comment->id] = $timber_comment; |
|
766 | + } |
|
767 | + |
|
768 | + // Build a flattened (depth=1) comment tree |
|
769 | + $comments_tree = array(); |
|
770 | + foreach( $timber_comments as $key => $comment ) { |
|
771 | + if ( ! $comment->is_child() ) { |
|
772 | + continue; |
|
773 | + } |
|
774 | + |
|
775 | + $tree_element = $comment; |
|
776 | + do { |
|
777 | + $tree_element = $timber_comments[$tree_element->comment_parent]; |
|
778 | + } while( $tree_element->is_child() ); |
|
779 | + |
|
780 | + $comments_tree[$tree_element->id][] = $comment->id; |
|
781 | + } |
|
782 | + |
|
783 | + // Add child comments to the relative "super parents" |
|
784 | + foreach($comments_tree as $comment_parent => $comment_children) { |
|
785 | + foreach($comment_children as $comment_child) { |
|
786 | + $timber_comments[$comment_parent]->children[] = $timber_comments[$comment_child]; |
|
787 | + unset($timber_comments[$comment_child]); |
|
788 | + } |
|
789 | + } |
|
790 | + |
|
791 | + $timber_comments = array_values($timber_comments); |
|
792 | + |
|
793 | + return $timber_comments; |
|
794 | + } |
|
795 | + |
|
796 | + /** |
|
797 | + * Get the categories for a post |
|
798 | + * @internal |
|
799 | + * @see TimberPost::categories |
|
800 | + * @return array of TimberTerms |
|
801 | + */ |
|
802 | + function get_categories() { |
|
803 | + return $this->get_terms('category'); |
|
804 | + } |
|
805 | + |
|
806 | + /** |
|
807 | + * @internal |
|
808 | + * @see TimberPost::category |
|
809 | + * @return mixed |
|
810 | + */ |
|
811 | + function get_category( ) { |
|
812 | + $cats = $this->get_categories(); |
|
813 | + if ( count($cats) && isset($cats[0]) ) { |
|
814 | + return $cats[0]; |
|
815 | + } |
|
816 | + } |
|
817 | + |
|
818 | + /** |
|
819 | + * @internal |
|
820 | + * @param string|array $tax |
|
821 | + * @param bool $merge |
|
822 | + * @param string $TermClass |
|
823 | + * @return array |
|
824 | + */ |
|
825 | + function get_terms( $tax = '', $merge = true, $TermClass = '' ) { |
|
826 | + |
|
827 | + $TermClass = $TermClass ?: $this->TermClass; |
|
828 | + |
|
829 | + if ( is_string($merge) && class_exists($merge) ) { |
|
830 | + $TermClass = $merge; |
|
831 | + } |
|
832 | + if ( is_array($tax) ) { |
|
833 | + $taxonomies = $tax; |
|
834 | + } |
|
835 | + if ( is_string($tax) ) { |
|
836 | + if ( in_array($tax, array('all','any','')) ) { |
|
837 | + $taxonomies = get_object_taxonomies($this->post_type); |
|
838 | + } else { |
|
839 | + $taxonomies = array($tax); |
|
840 | + } |
|
841 | + } |
|
842 | + |
|
843 | + $term_class_objects = array(); |
|
844 | + |
|
845 | + foreach ( $taxonomies as $taxonomy ) { |
|
846 | + if ( in_array($taxonomy, array('tag','tags')) ) { |
|
847 | + $taxonomy = 'post_tag'; |
|
848 | + } |
|
849 | + if ( $taxonomy == 'categories' ) { |
|
850 | + $taxonomy = 'category'; |
|
851 | + } |
|
852 | + |
|
853 | + $terms = wp_get_post_terms($this->ID, $taxonomy); |
|
854 | + |
|
855 | + if ( is_wp_error($terms) ) { |
|
856 | + /* @var $terms WP_Error */ |
|
857 | + TimberHelper::error_log("Error retrieving terms for taxonomy '$taxonomy' on a post in timber-post.php"); |
|
858 | + TimberHelper::error_log('tax = ' . print_r($tax, true)); |
|
859 | + TimberHelper::error_log('WP_Error: ' . $terms->get_error_message()); |
|
860 | + |
|
861 | + return $term_class_objects; |
|
862 | + } |
|
863 | + |
|
864 | + // map over array of wordpress terms, and transform them into instances of the TermClass |
|
865 | + $terms = array_map(function($term) use ($TermClass, $taxonomy) { |
|
866 | + return call_user_func(array($TermClass, 'from'), $term->term_id, $taxonomy); |
|
867 | + }, $terms); |
|
868 | + |
|
869 | + if ( $merge && is_array($terms) ) { |
|
870 | + $term_class_objects = array_merge($term_class_objects, $terms); |
|
871 | + } else if ( count($terms) ) { |
|
872 | + $term_class_objects[$taxonomy] = $terms; |
|
873 | + } |
|
874 | + } |
|
875 | + return $term_class_objects; |
|
876 | + } |
|
877 | + |
|
878 | + /** |
|
879 | + * @param string|int $term_name_or_id |
|
880 | + * @param string $taxonomy |
|
881 | + * @return bool |
|
882 | + */ |
|
883 | + function has_term( $term_name_or_id, $taxonomy = 'all' ) { |
|
884 | + if ( $taxonomy == 'all' || $taxonomy == 'any' ) { |
|
885 | + $taxes = get_object_taxonomies($this->post_type, 'names'); |
|
886 | + $ret = false; |
|
887 | + foreach ( $taxes as $tax ) { |
|
888 | + if ( has_term($term_name_or_id, $tax, $this->ID) ) { |
|
889 | + $ret = true; |
|
890 | + break; |
|
891 | + } |
|
892 | + } |
|
893 | + return $ret; |
|
894 | + } |
|
895 | + return has_term($term_name_or_id, $taxonomy, $this->ID); |
|
896 | + } |
|
897 | + |
|
898 | + /** |
|
899 | + * @param string $field |
|
900 | + * @return TimberImage |
|
901 | + */ |
|
902 | + function get_image( $field ) { |
|
903 | + return new $this->ImageClass($this->$field); |
|
904 | + } |
|
905 | + |
|
906 | + /** |
|
907 | + * Gets an array of tags for you to use |
|
908 | + * @internal |
|
909 | + * @example |
|
910 | + * ```twig |
|
911 | + * <ul class="tags"> |
|
912 | + * {% for tag in post.tags %} |
|
913 | + * <li>{{tag.name}}</li> |
|
914 | + * {% endfor %} |
|
915 | + * </ul> |
|
916 | + * ``` |
|
917 | + * @return array |
|
918 | + */ |
|
919 | + function get_tags() { |
|
920 | + return $this->get_terms('post_tag'); |
|
921 | + } |
|
922 | + |
|
923 | + /** |
|
924 | + * Outputs the title with filters applied |
|
925 | + * @internal |
|
926 | + * @example |
|
927 | + * ```twig |
|
928 | + * <h1>{{post.get_title}}</h1> |
|
929 | + * ``` |
|
930 | + * ```html |
|
931 | + * <h1>Hello World!</h1> |
|
932 | + * ``` |
|
933 | + * @return string |
|
934 | + */ |
|
935 | + function get_title() { |
|
936 | + return apply_filters('the_title', $this->post_title, $this->ID); |
|
937 | + } |
|
938 | + |
|
939 | + /** |
|
940 | + * Displays the content of the post with filters, shortcodes and wpautop applied |
|
941 | + * @example |
|
942 | + * ```twig |
|
943 | + * <div class="article-text">{{post.get_content}}</div> |
|
944 | + * ``` |
|
945 | + * ```html |
|
946 | + * <div class="article-text"><p>Blah blah blah</p><p>More blah blah blah.</p></div> |
|
947 | + * ``` |
|
948 | + * @param int $len |
|
949 | + * @param int $page |
|
950 | + * @return string |
|
951 | + */ |
|
952 | + function get_content( $len = 0, $page = 0 ) { |
|
953 | + if ( $len == 0 && $page == 0 && $this->_content ) { |
|
954 | + return $this->_content; |
|
955 | + } |
|
956 | + $content = $this->post_content; |
|
957 | + if ( $len ) { |
|
958 | + $content = wp_trim_words($content, $len); |
|
959 | + } |
|
960 | + if ( $page ) { |
|
961 | + $contents = explode('<!--nextpage-->', $content); |
|
962 | + $page--; |
|
963 | + if ( count($contents) > $page ) { |
|
964 | + $content = $contents[$page]; |
|
965 | + } |
|
966 | + } |
|
967 | + $content = apply_filters('the_content', ($content)); |
|
968 | + if ( $len == 0 && $page == 0 ) { |
|
969 | + $this->_content = $content; |
|
970 | + } |
|
971 | + return $content; |
|
972 | + } |
|
973 | + |
|
974 | + /** |
|
975 | + * @return string |
|
976 | + */ |
|
977 | + function get_paged_content() { |
|
978 | + global $page; |
|
979 | + return $this->get_content(0, $page); |
|
980 | + } |
|
981 | + /** |
|
982 | + * |
|
983 | + * Here is my summary |
|
984 | + * @example |
|
985 | + * ```twig |
|
986 | + * This post is from <span>{{ post.get_post_type.labels.plural }}</span> |
|
987 | + * ``` |
|
988 | + * |
|
989 | + * ```html |
|
990 | + * This post is from <span>Recipes</span> |
|
991 | + * ``` |
|
992 | + * @return mixed |
|
993 | + */ |
|
994 | + public function get_post_type() { |
|
995 | + return get_post_type_object($this->post_type); |
|
996 | + } |
|
997 | + |
|
998 | + /** |
|
999 | + * @return int the number of comments on a post |
|
1000 | + */ |
|
1001 | + public function get_comment_count() { |
|
1002 | + return get_comments_number($this->ID); |
|
1003 | + } |
|
1004 | + |
|
1005 | + /** |
|
1006 | + * @param string $field_name |
|
1007 | + * @return mixed |
|
1008 | + */ |
|
1009 | + public function get_field( $field_name ) { |
|
1010 | + $value = apply_filters('timber_post_get_meta_field_pre', null, $this->ID, $field_name, $this); |
|
1011 | + if ( $value === null ) { |
|
1012 | + $value = get_post_meta($this->ID, $field_name); |
|
1013 | + if ( is_array($value) && count($value) == 1 ) { |
|
1014 | + $value = $value[0]; |
|
1015 | + } |
|
1016 | + if ( is_array($value) && count($value) == 0 ) { |
|
1017 | + $value = null; |
|
1018 | + } |
|
1019 | + } |
|
1020 | + $value = apply_filters('timber_post_get_meta_field', $value, $this->ID, $field_name, $this); |
|
1021 | + return $value; |
|
1022 | + } |
|
1023 | + |
|
1024 | + /** |
|
1025 | + * @param string $field_name |
|
1026 | + */ |
|
1027 | + function import_field( $field_name ) { |
|
1028 | + $this->$field_name = $this->get_field($field_name); |
|
1029 | + } |
|
1030 | + |
|
1031 | + /** |
|
1032 | + * @internal |
|
1033 | + * @return mixed |
|
1034 | + */ |
|
1035 | + function get_format() { |
|
1036 | + return get_post_format($this->ID); |
|
1037 | + } |
|
1038 | + |
|
1039 | + /** |
|
1040 | + * Get the CSS classes for a post. For usage you should use `{{post.class}}` instead of `{{post.post_class}}` |
|
1041 | + * @internal |
|
1042 | + * @param string $class additional classes you want to add |
|
1043 | + * @see TimberPost::$class |
|
1044 | + * @example |
|
1045 | + * ```twig |
|
1046 | + * <article class="{{ post.class }}"> |
|
1047 | + * {# Some stuff here #} |
|
1048 | + * </article> |
|
1049 | + * ``` |
|
1050 | + * |
|
1051 | + * ```html |
|
1052 | + * <article class="post-2612 post type-post status-publish format-standard has-post-thumbnail hentry category-data tag-charleston-church-shooting tag-dylann-roof tag-gun-violence tag-hate-crimes tag-national-incident-based-reporting-system"> |
|
1053 | + * {# Some stuff here #} |
|
1054 | + * </article> |
|
1055 | + * ``` |
|
1056 | + * @return string a space-seperated list of classes |
|
1057 | + */ |
|
1058 | + public function post_class( $class='' ) { |
|
1059 | + global $post; |
|
1060 | + $old_global_post = $post; |
|
1061 | + $post = $this; |
|
1062 | + $class_array = get_post_class($class, $this->ID); |
|
1063 | + $post = $old_global_post; |
|
1064 | + if ( is_array($class_array) ){ |
|
1065 | + return implode(' ', $class_array); |
|
1066 | + } |
|
1067 | + return $class_array; |
|
1068 | + } |
|
1069 | + |
|
1070 | + // Docs |
|
1071 | + |
|
1072 | + /** |
|
1073 | + * @return array |
|
1074 | + * @codeCoverageIgnore |
|
1075 | + */ |
|
1076 | + public function get_method_values() { |
|
1077 | + $ret = parent::get_method_values(); |
|
1078 | + $ret['author'] = $this->author(); |
|
1079 | + $ret['categories'] = $this->categories(); |
|
1080 | + $ret['category'] = $this->category(); |
|
1081 | + $ret['children'] = $this->children(); |
|
1082 | + $ret['comments'] = $this->comments(); |
|
1083 | + $ret['content'] = $this->content(); |
|
1084 | + $ret['edit_link'] = $this->edit_link(); |
|
1085 | + $ret['format'] = $this->format(); |
|
1086 | + $ret['link'] = $this->link(); |
|
1087 | + $ret['next'] = $this->next(); |
|
1088 | + $ret['pagination'] = $this->pagination(); |
|
1089 | + $ret['parent'] = $this->parent(); |
|
1090 | + $ret['path'] = $this->path(); |
|
1091 | + $ret['prev'] = $this->prev(); |
|
1092 | + $ret['terms'] = $this->terms(); |
|
1093 | + $ret['tags'] = $this->tags(); |
|
1094 | + $ret['thumbnail'] = $this->thumbnail(); |
|
1095 | + $ret['title'] = $this->title(); |
|
1096 | + return $ret; |
|
1097 | + } |
|
1098 | + |
|
1099 | + /** |
|
1100 | + * Return the author of a post |
|
1101 | + * @api |
|
1102 | + * @example |
|
1103 | + * ```twig |
|
1104 | + * <h1>{{post.title}}</h1> |
|
1105 | + * <p class="byline"> |
|
1106 | + * <a href="{{post.author.link}}">{{post.author.name}}</a> |
|
1107 | + * </p> |
|
1108 | + * ``` |
|
1109 | + * @return TimberUser|bool A TimberUser object if found, false if not |
|
1110 | + */ |
|
1111 | + public function author() { |
|
1112 | + return $this->get_author(); |
|
1113 | + } |
|
1114 | + |
|
1115 | + /** |
|
1116 | + * Get the author (WordPress user) who last modified the post |
|
1117 | + * @example |
|
1118 | + * ```twig |
|
1119 | + * Last updated by {{ post.modified_author.name }} |
|
1120 | + * ``` |
|
1121 | + * ```html |
|
1122 | + * Last updated by Harper Lee |
|
1123 | + * ``` |
|
1124 | + * @return TimberUser|bool A TimberUser object if found, false if not |
|
1125 | + */ |
|
1126 | + public function modified_author() { |
|
1127 | + return $this->get_modified_author(); |
|
1128 | + } |
|
1129 | + |
|
1130 | + /** |
|
1131 | + * Get the categoires on a particular post |
|
1132 | + * @api |
|
1133 | + * @return array of TimberTerms |
|
1134 | + */ |
|
1135 | + public function categories() { |
|
1136 | + return $this->get_terms('category'); |
|
1137 | + } |
|
1138 | + |
|
1139 | + /** |
|
1140 | + * Returns a category attached to a post |
|
1141 | + * @api |
|
1142 | + * If mulitpuile categories are set, it will return just the first one |
|
1143 | + * @return TimberTerm|null |
|
1144 | + */ |
|
1145 | + public function category() { |
|
1146 | + return $this->get_category(); |
|
1147 | + } |
|
1148 | + |
|
1149 | + /** |
|
1150 | + * Returns an array of children on the post as TimberPosts |
|
1151 | + * (or other claass as you define). |
|
1152 | + * @api |
|
1153 | + * @example |
|
1154 | + * ```twig |
|
1155 | + * {% if post.children %} |
|
1156 | + * Here are the child pages: |
|
1157 | + * {% for child in page.children %} |
|
1158 | + * <a href="{{ child.link }}">{{ child.title }}</a> |
|
1159 | + * {% endfor %} |
|
1160 | + * {% endif %} |
|
1161 | + * ``` |
|
1162 | + * @param string $post_type _optional_ use to find children of a particular post type (attachment vs. page for example). You might want to restrict to certain types of children in case other stuff gets all mucked in there. You can use 'parent' to use the parent's post type |
|
1163 | + * @param string|bool $childPostClass _optional_ a custom post class (ex: 'MyTimberPost') to return the objects as. By default (false) it will use TimberPost::$post_class value. |
|
1164 | + * @return array |
|
1165 | + */ |
|
1166 | + public function children( $post_type = 'any', $childPostClass = false ) { |
|
1167 | + return $this->get_children( $post_type, $childPostClass ); |
|
1168 | + } |
|
1169 | + |
|
1170 | + /** |
|
1171 | + * Gets the comments on a TimberPost and returns them as an array of [TimberComments](#TimberComment) (or whatever comment class you set). |
|
1172 | + * @api |
|
1173 | + * @param int $count Set the number of comments you want to get. `0` is analogous to "all" |
|
1174 | + * @param string $order use ordering set in WordPress admin, or a different scheme |
|
1175 | + * @param string $type For when other plugins use the comments table for their own special purposes, might be set to 'liveblog' or other depending on what's stored in yr comments table |
|
1176 | + * @param string $status Could be 'pending', etc. |
|
1177 | + * @param string $CommentClass What class to use when returning Comment objects. As you become a Timber pro, you might find yourself extending TimberComment for your site or app (obviously, totally optional) |
|
1178 | + * @example |
|
1179 | + * ```twig |
|
1180 | + * {# single.twig #} |
|
1181 | + * <h4>Comments:</h4> |
|
1182 | + * {% for comment in post.comments %} |
|
1183 | + * <div class="comment-{{comment.ID}} comment-order-{{loop.index}}"> |
|
1184 | + * <p>{{comment.author.name}} said:</p> |
|
1185 | + * <p>{{comment.content}}</p> |
|
1186 | + * </div> |
|
1187 | + * {% endfor %} |
|
1188 | + * ``` |
|
1189 | + * @return bool|array |
|
1190 | + */ |
|
1191 | + public function comments( $count = 0, $order = 'wp', $type = 'comment', $status = 'approve', $CommentClass = 'TimberComment' ) { |
|
1192 | + return $this->get_comments($count, $order, $type, $status, $CommentClass); |
|
1193 | + } |
|
1194 | + |
|
1195 | + /** |
|
1196 | + * Gets the actual content of a WP Post, as opposed to post_content this will run the hooks/filters attached to the_content. \This guy will return your posts content with WordPress filters run on it (like for shortcodes and wpautop). |
|
1197 | + * @api |
|
1198 | + * @example |
|
1199 | + * ```twig |
|
1200 | + * <div class="article"> |
|
1201 | + * <h2>{{post.title}}</h2> |
|
1202 | + * <div class="content">{{ post.content }}</div> |
|
1203 | + * </div> |
|
1204 | + * ``` |
|
1205 | + * @param int $page |
|
1206 | + * @return string |
|
1207 | + */ |
|
1208 | + public function content( $page = 0 ) { |
|
1209 | + return $this->get_content(0, $page); |
|
1210 | + } |
|
1211 | + |
|
1212 | + /** |
|
1213 | + * @return string |
|
1214 | + */ |
|
1215 | + public function paged_content() { |
|
1216 | + return $this->get_paged_content(); |
|
1217 | + } |
|
1218 | + |
|
1219 | + /** |
|
1220 | + * Get the date to use in your template! |
|
1221 | + * @api |
|
1222 | + * @example |
|
1223 | + * ```twig |
|
1224 | + * Published on {{ post.date }} // Uses WP's formatting set in Admin |
|
1225 | + * OR |
|
1226 | + * Published on {{ post.date | date('F jS') }} // Jan 12th |
|
1227 | + * ``` |
|
1228 | + * |
|
1229 | + * ```html |
|
1230 | + * Published on January 12, 2015 |
|
1231 | + * OR |
|
1232 | + * Published on Jan 12th |
|
1233 | + * ``` |
|
1234 | + * @param string $date_format |
|
1235 | + * @return string |
|
1236 | + */ |
|
1237 | + public function date( $date_format = '' ) { |
|
1238 | + return $this->get_date($date_format); |
|
1239 | + } |
|
1240 | + |
|
1241 | + /** |
|
1242 | + * Get the time to use in your template |
|
1243 | + * @api |
|
1244 | + * @example |
|
1245 | + * ```twig |
|
1246 | + * Published at {{ post.time }} // Uses WP's formatting set in Admin |
|
1247 | + * OR |
|
1248 | + * Published at {{ post.time | time('G:i') }} // 13:25 |
|
1249 | + * ``` |
|
1250 | + * |
|
1251 | + * ```html |
|
1252 | + * Published at 1:25 pm |
|
1253 | + * OR |
|
1254 | + * Published at 13:25 |
|
1255 | + * ``` |
|
1256 | + * @param string $time_format |
|
1257 | + * @return string |
|
1258 | + */ |
|
1259 | + public function time( $time_format = '' ) { |
|
1260 | + $tf = $time_format ? $time_format : get_option('time_format'); |
|
1261 | + $the_time = (string)mysql2date($tf, $this->post_date); |
|
1262 | + return apply_filters('get_the_time', $the_time, $tf); |
|
1263 | + } |
|
1264 | + |
|
1265 | + /** |
|
1266 | + * @return bool|string |
|
1267 | + */ |
|
1268 | + public function edit_link() { |
|
1269 | + return $this->get_edit_url(); |
|
1270 | + } |
|
1271 | + |
|
1272 | + /** |
|
1273 | + * @api |
|
1274 | + * @return mixed |
|
1275 | + */ |
|
1276 | + public function format() { |
|
1277 | + return $this->get_format(); |
|
1278 | + } |
|
1279 | + |
|
1280 | + /** |
|
1281 | + * get the permalink for a post object |
|
1282 | + * @api |
|
1283 | + * @example |
|
1284 | + * ```twig |
|
1285 | + * <a href="{{post.link}}">Read my post</a> |
|
1286 | + * ``` |
|
1287 | + * @return string ex: http://example.org/2015/07/my-awesome-post |
|
1288 | + */ |
|
1289 | + public function link() { |
|
1290 | + return $this->get_permalink(); |
|
1291 | + } |
|
1292 | + |
|
1293 | + /** |
|
1294 | + * @param string $field_name |
|
1295 | + * @return mixed |
|
1296 | + */ |
|
1297 | + public function meta( $field_name = null ) { |
|
1298 | + if ( $field_name === null ) { |
|
1299 | + //on the off-chance the field is actually named meta |
|
1300 | + $field_name = 'meta'; |
|
1301 | + } |
|
1302 | + return $this->get_field($field_name); |
|
1303 | + } |
|
1304 | + |
|
1305 | + /** |
|
1306 | + * @return string |
|
1307 | + */ |
|
1308 | + public function name(){ |
|
1309 | + return $this->title(); |
|
1310 | + } |
|
1311 | + |
|
1312 | + /** |
|
1313 | + * @param string $date_format |
|
1314 | + * @return string |
|
1315 | + */ |
|
1316 | + public function modified_date( $date_format = '' ) { |
|
1317 | + return $this->get_modified_date($date_format); |
|
1318 | + } |
|
1319 | + |
|
1320 | + /** |
|
1321 | + * @param string $time_format |
|
1322 | + * @return string |
|
1323 | + */ |
|
1324 | + public function modified_time( $time_format = '' ) { |
|
1325 | + return $this->get_modified_time($time_format); |
|
1326 | + } |
|
1327 | + |
|
1328 | + /** |
|
1329 | + * @api |
|
1330 | + * @param bool $in_same_cat |
|
1331 | + * @return mixed |
|
1332 | + */ |
|
1333 | + public function next( $in_same_cat = false ) { |
|
1334 | + return $this->get_next($in_same_cat); |
|
1335 | + } |
|
1336 | + |
|
1337 | + /** |
|
1338 | + * @return array |
|
1339 | + */ |
|
1340 | + public function pagination() { |
|
1341 | + return $this->get_pagination(); |
|
1342 | + } |
|
1343 | + |
|
1344 | + /** |
|
1345 | + * Gets the parent (if one exists) from a post as a TimberPost object (or whatever is set in TimberPost::$PostClass) |
|
1346 | + * @api |
|
1347 | + * @example |
|
1348 | + * ```twig |
|
1349 | + * Parent page: <a href="{{ post.parent.link }}">{{ post.parent.title }}</a> |
|
1350 | + * ``` |
|
1351 | + * @return bool|TimberPost |
|
1352 | + */ |
|
1353 | + public function parent() { |
|
1354 | + return $this->get_parent(); |
|
1355 | + } |
|
1356 | + |
|
1357 | + /** |
|
1358 | + * Gets the relative path of a WP Post, so while link() will return http://example.org/2015/07/my-cool-post |
|
1359 | + * this will return just /2015/07/my-cool-post |
|
1360 | + * @api |
|
1361 | + * @example |
|
1362 | + * ```twig |
|
1363 | + * <a href="{{post.path}}">{{post.title}}</a> |
|
1364 | + * ``` |
|
1365 | + * @return string |
|
1366 | + */ |
|
1367 | + public function path() { |
|
1368 | + return $this->get_path(); |
|
1369 | + } |
|
1370 | + |
|
1371 | + /** |
|
1372 | + * @deprecated 0.20.0 use link() instead |
|
1373 | + * @return string |
|
1374 | + */ |
|
1375 | + public function permalink() { |
|
1376 | + return $this->get_permalink(); |
|
1377 | + } |
|
1378 | + |
|
1379 | + /** |
|
1380 | + * Get the previous post in a set |
|
1381 | + * @api |
|
1382 | + * @example |
|
1383 | + * ```twig |
|
1384 | + * <h4>Prior Entry:</h4> |
|
1385 | + * <h3>{{post.prev.title}}</h3> |
|
1386 | + * <p>{{post.prev.get_preview(25)}}</p> |
|
1387 | + * ``` |
|
1388 | + * @param bool $in_same_cat |
|
1389 | + * @return mixed |
|
1390 | + */ |
|
1391 | + public function prev( $in_same_cat = false ) { |
|
1392 | + return $this->get_prev($in_same_cat); |
|
1393 | + } |
|
1394 | + |
|
1395 | + /** |
|
1396 | + * Get the terms associated with the post |
|
1397 | + * This goes across all taxonomies by default |
|
1398 | + * @api |
|
1399 | + * @param string|array $tax What taxonom(y|ies) to pull from. Defaults to all registered taxonomies for the post type. You can use custom ones, or built-in WordPress taxonomies (category, tag). Timber plays nice and figures out that tag/tags/post_tag are all the same (and categories/category), for custom taxonomies you're on your own. |
|
1400 | + * @param bool $merge Should the resulting array be one big one (true)? Or should it be an array of sub-arrays for each taxonomy (false)? |
|
1401 | + * @return array |
|
1402 | + */ |
|
1403 | + public function terms( $tax = '', $merge = true ) { |
|
1404 | + return $this->get_terms($tax, $merge); |
|
1405 | + } |
|
1406 | + |
|
1407 | + /** |
|
1408 | + * Gets the tags on a post, uses WP's post_tag taxonomy |
|
1409 | + * @api |
|
1410 | + * @return array |
|
1411 | + */ |
|
1412 | + public function tags() { |
|
1413 | + return $this->get_tags(); |
|
1414 | + } |
|
1415 | + |
|
1416 | + /** |
|
1417 | + * get the featured image as a TimberImage |
|
1418 | + * @api |
|
1419 | + * @example |
|
1420 | + * ```twig |
|
1421 | + * <img src="{{post.thumbnail.src}}" /> |
|
1422 | + * ``` |
|
1423 | + * @return TimberImage|null of your thumbnail |
|
1424 | + */ |
|
1425 | + public function thumbnail() { |
|
1426 | + return $this->get_thumbnail(); |
|
1427 | + } |
|
1428 | + |
|
1429 | + /** |
|
1430 | + * Returns the processed title to be used in templates. This returns the title of the post after WP's filters have run. This is analogous to `the_title()` in standard WP template tags. |
|
1431 | + * @api |
|
1432 | + * @example |
|
1433 | + * ```twig |
|
1434 | + * <h1>{{ post.title }}</h1> |
|
1435 | + * ``` |
|
1436 | + * @return string |
|
1437 | + */ |
|
1438 | + public function title() { |
|
1439 | + return $this->get_title(); |
|
1440 | + } |
|
1441 | 1441 | |
1442 | 1442 | } |
@@ -161,8 +161,8 @@ discard block |
||
161 | 161 | * ``` |
162 | 162 | * @param mixed $pid |
163 | 163 | */ |
164 | - public function __construct($pid = null) { |
|
165 | - $pid = $this->determine_id( $pid ); |
|
164 | + public function __construct( $pid = null ) { |
|
165 | + $pid = $this->determine_id($pid); |
|
166 | 166 | $this->init($pid); |
167 | 167 | } |
168 | 168 | |
@@ -172,7 +172,7 @@ discard block |
||
172 | 172 | * @param mixed a value to test against |
173 | 173 | * @return int the numberic id we should be using for this post object |
174 | 174 | */ |
175 | - protected function determine_id($pid) { |
|
175 | + protected function determine_id( $pid ) { |
|
176 | 176 | global $wp_query; |
177 | 177 | if ( $pid === null && |
178 | 178 | isset($wp_query->queried_object_id) |
@@ -181,18 +181,18 @@ discard block |
||
181 | 181 | && is_object($wp_query->queried_object) |
182 | 182 | && get_class($wp_query->queried_object) == 'WP_Post' |
183 | 183 | ) { |
184 | - if( isset( $_GET['preview'] ) && isset( $_GET['preview_nonce'] ) && wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . $wp_query->queried_object_id ) ) { |
|
185 | - $pid = $this->get_post_preview_id( $wp_query ); |
|
184 | + if ( isset($_GET['preview']) && isset($_GET['preview_nonce']) && wp_verify_nonce($_GET['preview_nonce'], 'post_preview_' . $wp_query->queried_object_id) ) { |
|
185 | + $pid = $this->get_post_preview_id($wp_query); |
|
186 | 186 | } else if ( !$pid ) { |
187 | 187 | $pid = $wp_query->queried_object_id; |
188 | 188 | } |
189 | - } else if ( $pid === null && $wp_query->is_home && isset($wp_query->queried_object_id) && $wp_query->queried_object_id ) { |
|
189 | + } else if ( $pid === null && $wp_query->is_home && isset($wp_query->queried_object_id) && $wp_query->queried_object_id ) { |
|
190 | 190 | //hack for static page as home page |
191 | 191 | $pid = $wp_query->queried_object_id; |
192 | 192 | } else if ( $pid === null ) { |
193 | 193 | $gtid = false; |
194 | 194 | $maybe_post = get_post(); |
195 | - if ( isset($maybe_post->ID) ){ |
|
195 | + if ( isset($maybe_post->ID) ) { |
|
196 | 196 | $gtid = true; |
197 | 197 | } |
198 | 198 | if ( $gtid ) { |
@@ -230,19 +230,19 @@ discard block |
||
230 | 230 | |
231 | 231 | $can_preview = array(); |
232 | 232 | |
233 | - foreach( $can as $type ) { |
|
234 | - if( current_user_can( $type ) ) { |
|
233 | + foreach ( $can as $type ) { |
|
234 | + if ( current_user_can($type) ) { |
|
235 | 235 | $can_preview[] = true; |
236 | 236 | } |
237 | 237 | } |
238 | 238 | |
239 | - if ( count( $can_preview ) !== count( $can ) ) { |
|
239 | + if ( count($can_preview) !== count($can) ) { |
|
240 | 240 | return; |
241 | 241 | } |
242 | 242 | |
243 | - $revisions = wp_get_post_revisions( $query->queried_object_id ); |
|
243 | + $revisions = wp_get_post_revisions($query->queried_object_id); |
|
244 | 244 | |
245 | - if( !empty( $revisions ) ) { |
|
245 | + if ( !empty($revisions) ) { |
|
246 | 246 | $last = end($revisions); |
247 | 247 | return $last->ID; |
248 | 248 | } |
@@ -255,7 +255,7 @@ discard block |
||
255 | 255 | * @internal |
256 | 256 | * @param int|bool $pid |
257 | 257 | */ |
258 | - protected function init($pid = false) { |
|
258 | + protected function init( $pid = false ) { |
|
259 | 259 | if ( $pid === false ) { |
260 | 260 | $pid = get_the_ID(); |
261 | 261 | } |
@@ -342,11 +342,11 @@ discard block |
||
342 | 342 | * @param string $post_name |
343 | 343 | * @return int |
344 | 344 | */ |
345 | - static function get_post_id_by_name($post_name) { |
|
345 | + static function get_post_id_by_name( $post_name ) { |
|
346 | 346 | global $wpdb; |
347 | 347 | $query = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_name = %s LIMIT 1", $post_name); |
348 | 348 | $result = $wpdb->get_row($query); |
349 | - if (!$result) { |
|
349 | + if ( !$result ) { |
|
350 | 350 | return null; |
351 | 351 | } |
352 | 352 | return $result->ID; |
@@ -368,7 +368,7 @@ discard block |
||
368 | 368 | * @param string $end The text to end the preview with (defaults to ...) |
369 | 369 | * @return string of the post preview |
370 | 370 | */ |
371 | - function get_preview($len = 50, $force = false, $readmore = 'Read More', $strip = true, $end = '…') { |
|
371 | + function get_preview( $len = 50, $force = false, $readmore = 'Read More', $strip = true, $end = '…' ) { |
|
372 | 372 | $text = ''; |
373 | 373 | $trimmed = false; |
374 | 374 | if ( isset($this->post_excerpt) && strlen($this->post_excerpt) ) { |
@@ -386,7 +386,7 @@ discard block |
||
386 | 386 | $text = TimberHelper::trim_words($text, $len, false); |
387 | 387 | $trimmed = true; |
388 | 388 | } |
389 | - $text = do_shortcode( $text ); |
|
389 | + $text = do_shortcode($text); |
|
390 | 390 | } |
391 | 391 | if ( !strlen($text) ) { |
392 | 392 | $text = TimberHelper::trim_words($this->get_content(), $len, false); |
@@ -416,11 +416,11 @@ discard block |
||
416 | 416 | } |
417 | 417 | $read_more_class = apply_filters('timber/post/get_preview/read_more_class', "read-more"); |
418 | 418 | if ( $readmore && isset($readmore_matches) && !empty($readmore_matches[1]) ) { |
419 | - $text .= ' <a href="' . $this->get_permalink() . '" class="'.$read_more_class .'">' . trim($readmore_matches[1]) . '</a>'; |
|
419 | + $text .= ' <a href="' . $this->get_permalink() . '" class="' . $read_more_class . '">' . trim($readmore_matches[1]) . '</a>'; |
|
420 | 420 | } elseif ( $readmore ) { |
421 | - $text .= ' <a href="' . $this->get_permalink() . '" class="'.$read_more_class .'">' . trim($readmore) . '</a>'; |
|
421 | + $text .= ' <a href="' . $this->get_permalink() . '" class="' . $read_more_class . '">' . trim($readmore) . '</a>'; |
|
422 | 422 | } |
423 | - if ( !$strip && $last_p_tag && ( strpos($text, '<p>') || strpos($text, '<p ') ) ) { |
|
423 | + if ( !$strip && $last_p_tag && (strpos($text, '<p>') || strpos($text, '<p ')) ) { |
|
424 | 424 | $text .= '</p>'; |
425 | 425 | } |
426 | 426 | } |
@@ -564,7 +564,7 @@ discard block |
||
564 | 564 | * @param int $i |
565 | 565 | * @return string |
566 | 566 | */ |
567 | - protected static function get_wp_link_page($i) { |
|
567 | + protected static function get_wp_link_page( $i ) { |
|
568 | 568 | $link = _wp_link_page($i); |
569 | 569 | $link = new SimpleXMLElement($link . '</a>'); |
570 | 570 | if ( isset($link['href']) ) { |
@@ -647,7 +647,7 @@ discard block |
||
647 | 647 | * @param int $pid |
648 | 648 | * @return null|object|WP_Post |
649 | 649 | */ |
650 | - protected function get_info($pid) { |
|
650 | + protected function get_info( $pid ) { |
|
651 | 651 | $post = $this->prepare_post_info($pid); |
652 | 652 | if ( !isset($post->post_status) ) { |
653 | 653 | return null; |
@@ -657,7 +657,7 @@ discard block |
||
657 | 657 | $post->slug = $post->post_name; |
658 | 658 | $customs = $this->get_post_custom($post->ID); |
659 | 659 | $post->custom = $customs; |
660 | - $post = (object) array_merge((array)$customs, (array)$post); |
|
660 | + $post = (object) array_merge((array) $customs, (array) $post); |
|
661 | 661 | return $post; |
662 | 662 | } |
663 | 663 | |
@@ -669,7 +669,7 @@ discard block |
||
669 | 669 | */ |
670 | 670 | function get_date( $date_format = '' ) { |
671 | 671 | $df = $date_format ? $date_format : get_option('date_format'); |
672 | - $the_date = (string)mysql2date($df, $this->post_date); |
|
672 | + $the_date = (string) mysql2date($df, $this->post_date); |
|
673 | 673 | return apply_filters('get_the_date', $the_date, $df); |
674 | 674 | } |
675 | 675 | |
@@ -730,7 +730,7 @@ discard block |
||
730 | 730 | * @return array|mixed |
731 | 731 | */ |
732 | 732 | |
733 | - function get_comments($ct = 0, $order = 'wp', $type = 'comment', $status = 'approve', $CommentClass = 'TimberComment') { |
|
733 | + function get_comments( $ct = 0, $order = 'wp', $type = 'comment', $status = 'approve', $CommentClass = 'TimberComment' ) { |
|
734 | 734 | |
735 | 735 | global $overridden_cpage, $user_ID; |
736 | 736 | $overridden_cpage = false; |
@@ -747,42 +747,42 @@ discard block |
||
747 | 747 | } |
748 | 748 | |
749 | 749 | if ( $user_ID ) { |
750 | - $args['include_unapproved'] = array( $user_ID ); |
|
751 | - } elseif ( ! empty( $comment_author_email ) ) { |
|
752 | - $args['include_unapproved'] = array( $comment_author_email ); |
|
750 | + $args['include_unapproved'] = array($user_ID); |
|
751 | + } elseif ( !empty($comment_author_email) ) { |
|
752 | + $args['include_unapproved'] = array($comment_author_email); |
|
753 | 753 | } |
754 | 754 | |
755 | 755 | $comments = get_comments($args); |
756 | 756 | $timber_comments = array(); |
757 | 757 | |
758 | 758 | if ( '' == get_query_var('cpage') && get_option('page_comments') ) { |
759 | - set_query_var( 'cpage', 'newest' == get_option('default_comments_page') ? get_comment_pages_count() : 1 ); |
|
759 | + set_query_var('cpage', 'newest' == get_option('default_comments_page') ? get_comment_pages_count() : 1); |
|
760 | 760 | $overridden_cpage = true; |
761 | 761 | } |
762 | 762 | |
763 | - foreach($comments as $key => &$comment) { |
|
763 | + foreach ( $comments as $key => &$comment ) { |
|
764 | 764 | $timber_comment = new $CommentClass($comment); |
765 | 765 | $timber_comments[$timber_comment->id] = $timber_comment; |
766 | 766 | } |
767 | 767 | |
768 | 768 | // Build a flattened (depth=1) comment tree |
769 | 769 | $comments_tree = array(); |
770 | - foreach( $timber_comments as $key => $comment ) { |
|
771 | - if ( ! $comment->is_child() ) { |
|
770 | + foreach ( $timber_comments as $key => $comment ) { |
|
771 | + if ( !$comment->is_child() ) { |
|
772 | 772 | continue; |
773 | 773 | } |
774 | 774 | |
775 | 775 | $tree_element = $comment; |
776 | 776 | do { |
777 | 777 | $tree_element = $timber_comments[$tree_element->comment_parent]; |
778 | - } while( $tree_element->is_child() ); |
|
778 | + } while ( $tree_element->is_child() ); |
|
779 | 779 | |
780 | 780 | $comments_tree[$tree_element->id][] = $comment->id; |
781 | 781 | } |
782 | 782 | |
783 | 783 | // Add child comments to the relative "super parents" |
784 | - foreach($comments_tree as $comment_parent => $comment_children) { |
|
785 | - foreach($comment_children as $comment_child) { |
|
784 | + foreach ( $comments_tree as $comment_parent => $comment_children ) { |
|
785 | + foreach ( $comment_children as $comment_child ) { |
|
786 | 786 | $timber_comments[$comment_parent]->children[] = $timber_comments[$comment_child]; |
787 | 787 | unset($timber_comments[$comment_child]); |
788 | 788 | } |
@@ -833,7 +833,7 @@ discard block |
||
833 | 833 | $taxonomies = $tax; |
834 | 834 | } |
835 | 835 | if ( is_string($tax) ) { |
836 | - if ( in_array($tax, array('all','any','')) ) { |
|
836 | + if ( in_array($tax, array('all', 'any', '')) ) { |
|
837 | 837 | $taxonomies = get_object_taxonomies($this->post_type); |
838 | 838 | } else { |
839 | 839 | $taxonomies = array($tax); |
@@ -843,7 +843,7 @@ discard block |
||
843 | 843 | $term_class_objects = array(); |
844 | 844 | |
845 | 845 | foreach ( $taxonomies as $taxonomy ) { |
846 | - if ( in_array($taxonomy, array('tag','tags')) ) { |
|
846 | + if ( in_array($taxonomy, array('tag', 'tags')) ) { |
|
847 | 847 | $taxonomy = 'post_tag'; |
848 | 848 | } |
849 | 849 | if ( $taxonomy == 'categories' ) { |
@@ -862,7 +862,7 @@ discard block |
||
862 | 862 | } |
863 | 863 | |
864 | 864 | // map over array of wordpress terms, and transform them into instances of the TermClass |
865 | - $terms = array_map(function($term) use ($TermClass, $taxonomy) { |
|
865 | + $terms = array_map(function( $term ) use ($TermClass, $taxonomy) { |
|
866 | 866 | return call_user_func(array($TermClass, 'from'), $term->term_id, $taxonomy); |
867 | 867 | }, $terms); |
868 | 868 | |
@@ -1055,13 +1055,13 @@ discard block |
||
1055 | 1055 | * ``` |
1056 | 1056 | * @return string a space-seperated list of classes |
1057 | 1057 | */ |
1058 | - public function post_class( $class='' ) { |
|
1058 | + public function post_class( $class = '' ) { |
|
1059 | 1059 | global $post; |
1060 | 1060 | $old_global_post = $post; |
1061 | 1061 | $post = $this; |
1062 | 1062 | $class_array = get_post_class($class, $this->ID); |
1063 | 1063 | $post = $old_global_post; |
1064 | - if ( is_array($class_array) ){ |
|
1064 | + if ( is_array($class_array) ) { |
|
1065 | 1065 | return implode(' ', $class_array); |
1066 | 1066 | } |
1067 | 1067 | return $class_array; |
@@ -1164,7 +1164,7 @@ discard block |
||
1164 | 1164 | * @return array |
1165 | 1165 | */ |
1166 | 1166 | public function children( $post_type = 'any', $childPostClass = false ) { |
1167 | - return $this->get_children( $post_type, $childPostClass ); |
|
1167 | + return $this->get_children($post_type, $childPostClass); |
|
1168 | 1168 | } |
1169 | 1169 | |
1170 | 1170 | /** |
@@ -1258,7 +1258,7 @@ discard block |
||
1258 | 1258 | */ |
1259 | 1259 | public function time( $time_format = '' ) { |
1260 | 1260 | $tf = $time_format ? $time_format : get_option('time_format'); |
1261 | - $the_time = (string)mysql2date($tf, $this->post_date); |
|
1261 | + $the_time = (string) mysql2date($tf, $this->post_date); |
|
1262 | 1262 | return apply_filters('get_the_time', $the_time, $tf); |
1263 | 1263 | } |
1264 | 1264 | |
@@ -1305,7 +1305,7 @@ discard block |
||
1305 | 1305 | /** |
1306 | 1306 | * @return string |
1307 | 1307 | */ |
1308 | - public function name(){ |
|
1308 | + public function name() { |
|
1309 | 1309 | return $this->title(); |
1310 | 1310 | } |
1311 | 1311 |
@@ -4,6 +4,7 @@ discard block |
||
4 | 4 | |
5 | 5 | /** |
6 | 6 | * @deprecated since 0.21.1 use Upstatement/routes instead |
7 | + * @param Timber $timber |
|
7 | 8 | */ |
8 | 9 | public static function init( $timber ) { |
9 | 10 | // Install ourselves in Timber |
@@ -24,7 +25,7 @@ discard block |
||
24 | 25 | * @param mixed $query |
25 | 26 | * @param int $status_code |
26 | 27 | * @param bool $tparams |
27 | - * @return bool |
|
28 | + * @return boolean|null |
|
28 | 29 | * @deprecated since 0.21.1 use Upstatement/routes instead |
29 | 30 | */ |
30 | 31 | public static function load_view($template, $query = false, $status_code = 200, $tparams = false) { |
@@ -2,32 +2,32 @@ |
||
2 | 2 | |
3 | 3 | class TimberRoutes { |
4 | 4 | |
5 | - /** |
|
6 | - * @deprecated since 0.21.1 use Upstatement/routes instead |
|
7 | - */ |
|
8 | - public static function init( $timber ) { |
|
9 | - // Install ourselves in Timber |
|
10 | - $timber->routes = new TimberRoutes(); |
|
11 | - } |
|
5 | + /** |
|
6 | + * @deprecated since 0.21.1 use Upstatement/routes instead |
|
7 | + */ |
|
8 | + public static function init( $timber ) { |
|
9 | + // Install ourselves in Timber |
|
10 | + $timber->routes = new TimberRoutes(); |
|
11 | + } |
|
12 | 12 | |
13 | - /** |
|
14 | - * @param string $route |
|
15 | - * @param callable $callback |
|
16 | - * @deprecated since 0.21.1 use Upstatement/routes instead |
|
17 | - */ |
|
18 | - public static function add_route($route, $callback, $args = array()) { |
|
19 | - Routes::map($route, $callback, $args); |
|
20 | - } |
|
13 | + /** |
|
14 | + * @param string $route |
|
15 | + * @param callable $callback |
|
16 | + * @deprecated since 0.21.1 use Upstatement/routes instead |
|
17 | + */ |
|
18 | + public static function add_route($route, $callback, $args = array()) { |
|
19 | + Routes::map($route, $callback, $args); |
|
20 | + } |
|
21 | 21 | |
22 | - /** |
|
23 | - * @param array $template |
|
24 | - * @param mixed $query |
|
25 | - * @param int $status_code |
|
26 | - * @param bool $tparams |
|
27 | - * @return bool |
|
28 | - * @deprecated since 0.21.1 use Upstatement/routes instead |
|
29 | - */ |
|
30 | - public static function load_view($template, $query = false, $status_code = 200, $tparams = false) { |
|
31 | - Routes::load($template, $tparams, $query, $status_code); |
|
32 | - } |
|
22 | + /** |
|
23 | + * @param array $template |
|
24 | + * @param mixed $query |
|
25 | + * @param int $status_code |
|
26 | + * @param bool $tparams |
|
27 | + * @return bool |
|
28 | + * @deprecated since 0.21.1 use Upstatement/routes instead |
|
29 | + */ |
|
30 | + public static function load_view($template, $query = false, $status_code = 200, $tparams = false) { |
|
31 | + Routes::load($template, $tparams, $query, $status_code); |
|
32 | + } |
|
33 | 33 | } |
@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | * @param callable $callback |
16 | 16 | * @deprecated since 0.21.1 use Upstatement/routes instead |
17 | 17 | */ |
18 | - public static function add_route($route, $callback, $args = array()) { |
|
18 | + public static function add_route( $route, $callback, $args = array() ) { |
|
19 | 19 | Routes::map($route, $callback, $args); |
20 | 20 | } |
21 | 21 | |
@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | * @return bool |
28 | 28 | * @deprecated since 0.21.1 use Upstatement/routes instead |
29 | 29 | */ |
30 | - public static function load_view($template, $query = false, $status_code = 200, $tparams = false) { |
|
30 | + public static function load_view( $template, $query = false, $status_code = 200, $tparams = false ) { |
|
31 | 31 | Routes::load($template, $tparams, $query, $status_code); |
32 | 32 | } |
33 | 33 | } |
@@ -136,6 +136,9 @@ discard block |
||
136 | 136 | return $path; |
137 | 137 | } |
138 | 138 | |
139 | + /** |
|
140 | + * @param string $fs |
|
141 | + */ |
|
139 | 142 | public static function file_system_to_url( $fs ) { |
140 | 143 | $relative_path = self::get_rel_path( $fs ); |
141 | 144 | $home = home_url( '/'.$relative_path ); |
@@ -210,6 +213,7 @@ discard block |
||
210 | 213 | /** |
211 | 214 | * This will evaluate wheter a URL is at an aboslute location (like http://example.org/whatever) |
212 | 215 | * |
216 | + * @param string $path |
|
213 | 217 | * @return boolean true if $path is an absolute url, false if relative. |
214 | 218 | */ |
215 | 219 | public static function is_absolute( $path ) { |
@@ -230,6 +234,9 @@ discard block |
||
230 | 234 | return $is_external; |
231 | 235 | } |
232 | 236 | |
237 | + /** |
|
238 | + * @param string $url |
|
239 | + */ |
|
233 | 240 | private static function is_internal_content($url) { |
234 | 241 | // using content_url() instead of site_url or home_url is IMPORTANT |
235 | 242 | // otherwise you run into errors with sites that: |
@@ -267,8 +267,9 @@ |
||
267 | 267 | * @return string |
268 | 268 | */ |
269 | 269 | public static function remove_trailing_slash( $link ) { |
270 | - if ( $link != "/" ) |
|
271 | - $link = untrailingslashit( $link ); |
|
270 | + if ( $link != "/" ) { |
|
271 | + $link = untrailingslashit( $link ); |
|
272 | + } |
|
272 | 273 | return $link; |
273 | 274 | } |
274 | 275 |
@@ -2,333 +2,333 @@ |
||
2 | 2 | |
3 | 3 | class TimberURLHelper { |
4 | 4 | |
5 | - /** |
|
6 | - * |
|
7 | - * |
|
8 | - * @return string |
|
9 | - */ |
|
10 | - public static function get_current_url() { |
|
11 | - $pageURL = "http://"; |
|
12 | - if ( isset( $_SERVER['HTTPS'] ) && $_SERVER["HTTPS"] == "on" ) { |
|
13 | - $pageURL = "https://";; |
|
14 | - } |
|
15 | - if ( isset($_SERVER["SERVER_PORT"]) && $_SERVER["SERVER_PORT"] && $_SERVER["SERVER_PORT"] != "80" ) { |
|
16 | - $pageURL .= self::get_host() . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"]; |
|
17 | - } else { |
|
18 | - $pageURL .= self::get_host() . $_SERVER["REQUEST_URI"]; |
|
19 | - } |
|
20 | - return $pageURL; |
|
21 | - } |
|
5 | + /** |
|
6 | + * |
|
7 | + * |
|
8 | + * @return string |
|
9 | + */ |
|
10 | + public static function get_current_url() { |
|
11 | + $pageURL = "http://"; |
|
12 | + if ( isset( $_SERVER['HTTPS'] ) && $_SERVER["HTTPS"] == "on" ) { |
|
13 | + $pageURL = "https://";; |
|
14 | + } |
|
15 | + if ( isset($_SERVER["SERVER_PORT"]) && $_SERVER["SERVER_PORT"] && $_SERVER["SERVER_PORT"] != "80" ) { |
|
16 | + $pageURL .= self::get_host() . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"]; |
|
17 | + } else { |
|
18 | + $pageURL .= self::get_host() . $_SERVER["REQUEST_URI"]; |
|
19 | + } |
|
20 | + return $pageURL; |
|
21 | + } |
|
22 | 22 | |
23 | - /** |
|
24 | - * |
|
25 | - * |
|
26 | - * @param string $url |
|
27 | - * @return bool |
|
28 | - */ |
|
29 | - public static function is_url( $url ) { |
|
30 | - if ( !is_string( $url ) ) { |
|
31 | - return false; |
|
32 | - } |
|
33 | - $url = strtolower( $url ); |
|
34 | - if ( strstr( $url, '://' ) ) { |
|
35 | - return true; |
|
36 | - } |
|
37 | - return false; |
|
38 | - } |
|
23 | + /** |
|
24 | + * |
|
25 | + * |
|
26 | + * @param string $url |
|
27 | + * @return bool |
|
28 | + */ |
|
29 | + public static function is_url( $url ) { |
|
30 | + if ( !is_string( $url ) ) { |
|
31 | + return false; |
|
32 | + } |
|
33 | + $url = strtolower( $url ); |
|
34 | + if ( strstr( $url, '://' ) ) { |
|
35 | + return true; |
|
36 | + } |
|
37 | + return false; |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * |
|
42 | - * |
|
43 | - * @return string |
|
44 | - */ |
|
45 | - public static function get_path_base() { |
|
46 | - $struc = get_option( 'permalink_structure' ); |
|
47 | - $struc = explode( '/', $struc ); |
|
48 | - $p = '/'; |
|
49 | - foreach ( $struc as $s ) { |
|
50 | - if ( !strstr( $s, '%' ) && strlen( $s ) ) { |
|
51 | - $p .= $s . '/'; |
|
52 | - } |
|
53 | - } |
|
54 | - return $p; |
|
55 | - } |
|
40 | + /** |
|
41 | + * |
|
42 | + * |
|
43 | + * @return string |
|
44 | + */ |
|
45 | + public static function get_path_base() { |
|
46 | + $struc = get_option( 'permalink_structure' ); |
|
47 | + $struc = explode( '/', $struc ); |
|
48 | + $p = '/'; |
|
49 | + foreach ( $struc as $s ) { |
|
50 | + if ( !strstr( $s, '%' ) && strlen( $s ) ) { |
|
51 | + $p .= $s . '/'; |
|
52 | + } |
|
53 | + } |
|
54 | + return $p; |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * |
|
59 | - * |
|
60 | - * @param string $url |
|
61 | - * @param bool $force |
|
62 | - * @return string |
|
63 | - */ |
|
64 | - public static function get_rel_url( $url, $force = false ) { |
|
65 | - $url_info = parse_url( $url ); |
|
66 | - if ( isset( $url_info['host'] ) && $url_info['host'] != self::get_host() && !$force ) { |
|
67 | - return $url; |
|
68 | - } |
|
69 | - $link = ''; |
|
70 | - if ( isset( $url_info['path'] ) ) { |
|
71 | - $link = $url_info['path']; |
|
72 | - } |
|
73 | - if ( isset( $url_info['query'] ) && strlen( $url_info['query'] ) ) { |
|
74 | - $link .= '?' . $url_info['query']; |
|
75 | - } |
|
76 | - if ( isset( $url_info['fragment'] ) && strlen( $url_info['fragment'] ) ) { |
|
77 | - $link .= '#' . $url_info['fragment']; |
|
78 | - } |
|
79 | - $link = TimberURLHelper::remove_double_slashes( $link ); |
|
80 | - return $link; |
|
81 | - } |
|
57 | + /** |
|
58 | + * |
|
59 | + * |
|
60 | + * @param string $url |
|
61 | + * @param bool $force |
|
62 | + * @return string |
|
63 | + */ |
|
64 | + public static function get_rel_url( $url, $force = false ) { |
|
65 | + $url_info = parse_url( $url ); |
|
66 | + if ( isset( $url_info['host'] ) && $url_info['host'] != self::get_host() && !$force ) { |
|
67 | + return $url; |
|
68 | + } |
|
69 | + $link = ''; |
|
70 | + if ( isset( $url_info['path'] ) ) { |
|
71 | + $link = $url_info['path']; |
|
72 | + } |
|
73 | + if ( isset( $url_info['query'] ) && strlen( $url_info['query'] ) ) { |
|
74 | + $link .= '?' . $url_info['query']; |
|
75 | + } |
|
76 | + if ( isset( $url_info['fragment'] ) && strlen( $url_info['fragment'] ) ) { |
|
77 | + $link .= '#' . $url_info['fragment']; |
|
78 | + } |
|
79 | + $link = TimberURLHelper::remove_double_slashes( $link ); |
|
80 | + return $link; |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * Some setups like HTTP_HOST, some like SERVER_NAME, it's complicated |
|
85 | - * @link http://stackoverflow.com/questions/2297403/http-host-vs-server-name |
|
86 | - * @return string the HTTP_HOST or SERVER_NAME |
|
87 | - */ |
|
88 | - public static function get_host() { |
|
89 | - if (isset($_SERVER['HTTP_HOST'])) { |
|
90 | - return $_SERVER['HTTP_HOST']; |
|
91 | - } |
|
92 | - if (isset($_SERVER['SERVER_NAME'])) { |
|
93 | - return $_SERVER['SERVER_NAME']; |
|
94 | - } |
|
95 | - return ''; |
|
96 | - } |
|
83 | + /** |
|
84 | + * Some setups like HTTP_HOST, some like SERVER_NAME, it's complicated |
|
85 | + * @link http://stackoverflow.com/questions/2297403/http-host-vs-server-name |
|
86 | + * @return string the HTTP_HOST or SERVER_NAME |
|
87 | + */ |
|
88 | + public static function get_host() { |
|
89 | + if (isset($_SERVER['HTTP_HOST'])) { |
|
90 | + return $_SERVER['HTTP_HOST']; |
|
91 | + } |
|
92 | + if (isset($_SERVER['SERVER_NAME'])) { |
|
93 | + return $_SERVER['SERVER_NAME']; |
|
94 | + } |
|
95 | + return ''; |
|
96 | + } |
|
97 | 97 | |
98 | - /** |
|
99 | - * |
|
100 | - * |
|
101 | - * @param string $url |
|
102 | - * @return bool |
|
103 | - */ |
|
104 | - public static function is_local( $url ) { |
|
105 | - if ( strstr( $url, self::get_host() ) ) { |
|
106 | - return true; |
|
107 | - } |
|
108 | - return false; |
|
109 | - } |
|
98 | + /** |
|
99 | + * |
|
100 | + * |
|
101 | + * @param string $url |
|
102 | + * @return bool |
|
103 | + */ |
|
104 | + public static function is_local( $url ) { |
|
105 | + if ( strstr( $url, self::get_host() ) ) { |
|
106 | + return true; |
|
107 | + } |
|
108 | + return false; |
|
109 | + } |
|
110 | 110 | |
111 | - /** |
|
112 | - * |
|
113 | - * |
|
114 | - * @param string $src |
|
115 | - * @return string |
|
116 | - */ |
|
117 | - public static function get_full_path( $src ) { |
|
118 | - $root = ABSPATH; |
|
119 | - $old_root_path = $root . $src; |
|
120 | - $old_root_path = str_replace( '//', '/', $old_root_path ); |
|
121 | - return $old_root_path; |
|
122 | - } |
|
111 | + /** |
|
112 | + * |
|
113 | + * |
|
114 | + * @param string $src |
|
115 | + * @return string |
|
116 | + */ |
|
117 | + public static function get_full_path( $src ) { |
|
118 | + $root = ABSPATH; |
|
119 | + $old_root_path = $root . $src; |
|
120 | + $old_root_path = str_replace( '//', '/', $old_root_path ); |
|
121 | + return $old_root_path; |
|
122 | + } |
|
123 | 123 | |
124 | - /** |
|
125 | - * Takes a url and figures out its place based in the file system based on path |
|
126 | - * NOTE: Not fool-proof, makes a lot of assumptions about the file path |
|
127 | - * matching the URL path |
|
128 | - * |
|
129 | - * @param string $url |
|
130 | - * @return string |
|
131 | - */ |
|
132 | - public static function url_to_file_system( $url ) { |
|
133 | - $url_parts = parse_url( $url ); |
|
134 | - $path = ABSPATH . $url_parts['path']; |
|
135 | - $path = str_replace( '//', '/', $path ); |
|
136 | - return $path; |
|
137 | - } |
|
124 | + /** |
|
125 | + * Takes a url and figures out its place based in the file system based on path |
|
126 | + * NOTE: Not fool-proof, makes a lot of assumptions about the file path |
|
127 | + * matching the URL path |
|
128 | + * |
|
129 | + * @param string $url |
|
130 | + * @return string |
|
131 | + */ |
|
132 | + public static function url_to_file_system( $url ) { |
|
133 | + $url_parts = parse_url( $url ); |
|
134 | + $path = ABSPATH . $url_parts['path']; |
|
135 | + $path = str_replace( '//', '/', $path ); |
|
136 | + return $path; |
|
137 | + } |
|
138 | 138 | |
139 | - public static function file_system_to_url( $fs ) { |
|
140 | - $relative_path = self::get_rel_path( $fs ); |
|
141 | - $home = home_url( '/'.$relative_path ); |
|
142 | - return $home; |
|
143 | - } |
|
139 | + public static function file_system_to_url( $fs ) { |
|
140 | + $relative_path = self::get_rel_path( $fs ); |
|
141 | + $home = home_url( '/'.$relative_path ); |
|
142 | + return $home; |
|
143 | + } |
|
144 | 144 | |
145 | - /** |
|
146 | - * |
|
147 | - * |
|
148 | - * @param string $src |
|
149 | - * @return string |
|
150 | - */ |
|
151 | - public static function get_rel_path( $src ) { |
|
152 | - if ( strstr( $src, ABSPATH ) ) { |
|
153 | - return str_replace( ABSPATH, '', $src ); |
|
154 | - } |
|
155 | - //its outside the wordpress directory, alternate setups: |
|
156 | - $src = str_replace( WP_CONTENT_DIR, '', $src ); |
|
157 | - return WP_CONTENT_SUBDIR . $src; |
|
158 | - } |
|
145 | + /** |
|
146 | + * |
|
147 | + * |
|
148 | + * @param string $src |
|
149 | + * @return string |
|
150 | + */ |
|
151 | + public static function get_rel_path( $src ) { |
|
152 | + if ( strstr( $src, ABSPATH ) ) { |
|
153 | + return str_replace( ABSPATH, '', $src ); |
|
154 | + } |
|
155 | + //its outside the wordpress directory, alternate setups: |
|
156 | + $src = str_replace( WP_CONTENT_DIR, '', $src ); |
|
157 | + return WP_CONTENT_SUBDIR . $src; |
|
158 | + } |
|
159 | 159 | |
160 | - /** |
|
161 | - * |
|
162 | - * |
|
163 | - * @param string $url |
|
164 | - * @return string |
|
165 | - */ |
|
166 | - public static function remove_double_slashes( $url ) { |
|
167 | - $url = str_replace( '//', '/', $url ); |
|
168 | - if ( strstr( $url, 'http:' ) && !strstr( $url, 'http://' ) ) { |
|
169 | - $url = str_replace( 'http:/', 'http://', $url ); |
|
170 | - } |
|
171 | - return $url; |
|
172 | - } |
|
160 | + /** |
|
161 | + * |
|
162 | + * |
|
163 | + * @param string $url |
|
164 | + * @return string |
|
165 | + */ |
|
166 | + public static function remove_double_slashes( $url ) { |
|
167 | + $url = str_replace( '//', '/', $url ); |
|
168 | + if ( strstr( $url, 'http:' ) && !strstr( $url, 'http://' ) ) { |
|
169 | + $url = str_replace( 'http:/', 'http://', $url ); |
|
170 | + } |
|
171 | + return $url; |
|
172 | + } |
|
173 | 173 | |
174 | - /** |
|
175 | - * |
|
176 | - * |
|
177 | - * @param string $url |
|
178 | - * @param string $path |
|
179 | - * @return string |
|
180 | - */ |
|
181 | - public static function prepend_to_url( $url, $path ) { |
|
182 | - if ( strstr( strtolower( $url ), 'http' ) ) { |
|
183 | - $url_parts = parse_url( $url ); |
|
184 | - $url = $url_parts['scheme'] . '://' . $url_parts['host'] . $path . $url_parts['path']; |
|
185 | - if ( isset( $url_parts['query'] ) ) { |
|
186 | - $url .= $url_parts['query']; |
|
187 | - } |
|
188 | - if ( isset( $url_parts['fragment'] ) ) { |
|
189 | - $url .= $url_parts['fragment']; |
|
190 | - } |
|
191 | - } else { |
|
192 | - $url = $url . $path; |
|
193 | - } |
|
194 | - return self::remove_double_slashes( $url ); |
|
195 | - } |
|
174 | + /** |
|
175 | + * |
|
176 | + * |
|
177 | + * @param string $url |
|
178 | + * @param string $path |
|
179 | + * @return string |
|
180 | + */ |
|
181 | + public static function prepend_to_url( $url, $path ) { |
|
182 | + if ( strstr( strtolower( $url ), 'http' ) ) { |
|
183 | + $url_parts = parse_url( $url ); |
|
184 | + $url = $url_parts['scheme'] . '://' . $url_parts['host'] . $path . $url_parts['path']; |
|
185 | + if ( isset( $url_parts['query'] ) ) { |
|
186 | + $url .= $url_parts['query']; |
|
187 | + } |
|
188 | + if ( isset( $url_parts['fragment'] ) ) { |
|
189 | + $url .= $url_parts['fragment']; |
|
190 | + } |
|
191 | + } else { |
|
192 | + $url = $url . $path; |
|
193 | + } |
|
194 | + return self::remove_double_slashes( $url ); |
|
195 | + } |
|
196 | 196 | |
197 | - /** |
|
198 | - * |
|
199 | - * |
|
200 | - * @param string $path |
|
201 | - * @return string |
|
202 | - */ |
|
203 | - public static function preslashit( $path ) { |
|
204 | - if ( strpos( $path, '/' ) != 0 ) { |
|
205 | - $path = '/' . $path; |
|
206 | - } |
|
207 | - return $path; |
|
208 | - } |
|
197 | + /** |
|
198 | + * |
|
199 | + * |
|
200 | + * @param string $path |
|
201 | + * @return string |
|
202 | + */ |
|
203 | + public static function preslashit( $path ) { |
|
204 | + if ( strpos( $path, '/' ) != 0 ) { |
|
205 | + $path = '/' . $path; |
|
206 | + } |
|
207 | + return $path; |
|
208 | + } |
|
209 | 209 | |
210 | - /** |
|
211 | - * This will evaluate wheter a URL is at an aboslute location (like http://example.org/whatever) |
|
212 | - * |
|
213 | - * @return boolean true if $path is an absolute url, false if relative. |
|
214 | - */ |
|
215 | - public static function is_absolute( $path ) { |
|
216 | - return (boolean) ( strstr( $path, 'http' ) ); |
|
217 | - } |
|
210 | + /** |
|
211 | + * This will evaluate wheter a URL is at an aboslute location (like http://example.org/whatever) |
|
212 | + * |
|
213 | + * @return boolean true if $path is an absolute url, false if relative. |
|
214 | + */ |
|
215 | + public static function is_absolute( $path ) { |
|
216 | + return (boolean) ( strstr( $path, 'http' ) ); |
|
217 | + } |
|
218 | 218 | |
219 | - /** |
|
220 | - * This function is slightly different from the one below in the case of: |
|
221 | - * an image hosted on the same domain BUT on a different site than the |
|
222 | - * Wordpress install will be reported as external content. |
|
223 | - * |
|
224 | - * @param string $url a URL to evaluate against |
|
225 | - * @return boolean if $url points to an external location returns true |
|
226 | - */ |
|
227 | - public static function is_external_content( $url ) { |
|
228 | - $is_external = TimberURLHelper::is_absolute( $url ) && ! TimberURLHelper::is_internal_content( $url ); |
|
219 | + /** |
|
220 | + * This function is slightly different from the one below in the case of: |
|
221 | + * an image hosted on the same domain BUT on a different site than the |
|
222 | + * Wordpress install will be reported as external content. |
|
223 | + * |
|
224 | + * @param string $url a URL to evaluate against |
|
225 | + * @return boolean if $url points to an external location returns true |
|
226 | + */ |
|
227 | + public static function is_external_content( $url ) { |
|
228 | + $is_external = TimberURLHelper::is_absolute( $url ) && ! TimberURLHelper::is_internal_content( $url ); |
|
229 | 229 | |
230 | - return $is_external; |
|
231 | - } |
|
230 | + return $is_external; |
|
231 | + } |
|
232 | 232 | |
233 | - private static function is_internal_content($url) { |
|
234 | - // using content_url() instead of site_url or home_url is IMPORTANT |
|
235 | - // otherwise you run into errors with sites that: |
|
236 | - // 1. use WPML plugin |
|
237 | - // 2. or redefine content directory |
|
238 | - $is_content_url = strstr( $url, content_url() ); |
|
233 | + private static function is_internal_content($url) { |
|
234 | + // using content_url() instead of site_url or home_url is IMPORTANT |
|
235 | + // otherwise you run into errors with sites that: |
|
236 | + // 1. use WPML plugin |
|
237 | + // 2. or redefine content directory |
|
238 | + $is_content_url = strstr( $url, content_url() ); |
|
239 | 239 | |
240 | - // this case covers when the upload directory has been redefined |
|
241 | - $upload_dir = wp_upload_dir(); |
|
242 | - $is_upload_url = strstr( $url, $upload_dir['baseurl'] ); |
|
240 | + // this case covers when the upload directory has been redefined |
|
241 | + $upload_dir = wp_upload_dir(); |
|
242 | + $is_upload_url = strstr( $url, $upload_dir['baseurl'] ); |
|
243 | 243 | |
244 | - return $is_content_url || $is_upload_url; |
|
245 | - } |
|
244 | + return $is_content_url || $is_upload_url; |
|
245 | + } |
|
246 | 246 | |
247 | - /** |
|
248 | - * |
|
249 | - * |
|
250 | - * @param string $url |
|
251 | - * @return bool true if $path is an external url, false if relative or local. |
|
252 | - * true if it's a subdomain (http://cdn.example.org = true) |
|
253 | - */ |
|
254 | - public static function is_external( $url ) { |
|
255 | - $has_http = strstr( strtolower( $url ), 'http' ); |
|
256 | - $on_domain = strstr( $url, self::get_host() ); |
|
257 | - if ( $has_http && !$on_domain ) { |
|
258 | - return true; |
|
259 | - } |
|
260 | - return false; |
|
261 | - } |
|
247 | + /** |
|
248 | + * |
|
249 | + * |
|
250 | + * @param string $url |
|
251 | + * @return bool true if $path is an external url, false if relative or local. |
|
252 | + * true if it's a subdomain (http://cdn.example.org = true) |
|
253 | + */ |
|
254 | + public static function is_external( $url ) { |
|
255 | + $has_http = strstr( strtolower( $url ), 'http' ); |
|
256 | + $on_domain = strstr( $url, self::get_host() ); |
|
257 | + if ( $has_http && !$on_domain ) { |
|
258 | + return true; |
|
259 | + } |
|
260 | + return false; |
|
261 | + } |
|
262 | 262 | |
263 | - /** |
|
264 | - * Pass links through untrailingslashit unless they are a single / |
|
265 | - * |
|
266 | - * @param string $link |
|
267 | - * @return string |
|
268 | - */ |
|
269 | - public static function remove_trailing_slash( $link ) { |
|
270 | - if ( $link != "/" ) |
|
271 | - $link = untrailingslashit( $link ); |
|
272 | - return $link; |
|
273 | - } |
|
263 | + /** |
|
264 | + * Pass links through untrailingslashit unless they are a single / |
|
265 | + * |
|
266 | + * @param string $link |
|
267 | + * @return string |
|
268 | + */ |
|
269 | + public static function remove_trailing_slash( $link ) { |
|
270 | + if ( $link != "/" ) |
|
271 | + $link = untrailingslashit( $link ); |
|
272 | + return $link; |
|
273 | + } |
|
274 | 274 | |
275 | - /** |
|
276 | - * Download an external file via a URL |
|
277 | - * |
|
278 | - * @param string $url |
|
279 | - * @param int $timeout |
|
280 | - * @return string|WP_Error the location of the temporay file name or an error |
|
281 | - * @deprecated since 0.20.0 |
|
282 | - */ |
|
283 | - static function download_url( $url, $timeout = 300 ) { |
|
284 | - if ( !$url ) { |
|
285 | - return new WP_Error( 'http_no_url', __( 'Invalid URL Provided.' ) ); |
|
286 | - } |
|
275 | + /** |
|
276 | + * Download an external file via a URL |
|
277 | + * |
|
278 | + * @param string $url |
|
279 | + * @param int $timeout |
|
280 | + * @return string|WP_Error the location of the temporay file name or an error |
|
281 | + * @deprecated since 0.20.0 |
|
282 | + */ |
|
283 | + static function download_url( $url, $timeout = 300 ) { |
|
284 | + if ( !$url ) { |
|
285 | + return new WP_Error( 'http_no_url', __( 'Invalid URL Provided.' ) ); |
|
286 | + } |
|
287 | 287 | |
288 | - $tmpfname = wp_tempnam( $url ); |
|
289 | - if ( !$tmpfname ) { |
|
290 | - return new WP_Error( 'http_no_file', __( 'Could not create Temporary file.' ) ); |
|
291 | - } |
|
288 | + $tmpfname = wp_tempnam( $url ); |
|
289 | + if ( !$tmpfname ) { |
|
290 | + return new WP_Error( 'http_no_file', __( 'Could not create Temporary file.' ) ); |
|
291 | + } |
|
292 | 292 | |
293 | - $response = wp_remote_get( $url, array( 'timeout' => $timeout, 'stream' => true, 'filename' => $tmpfname ) ); |
|
293 | + $response = wp_remote_get( $url, array( 'timeout' => $timeout, 'stream' => true, 'filename' => $tmpfname ) ); |
|
294 | 294 | |
295 | - if ( is_wp_error( $response ) ) { |
|
296 | - unlink( $tmpfname ); |
|
297 | - return $response; |
|
298 | - } |
|
299 | - if ( 200 != wp_remote_retrieve_response_code( $response ) ) { |
|
300 | - unlink( $tmpfname ); |
|
301 | - return new WP_Error( 'http_404', trim( wp_remote_retrieve_response_message( $response ) ) ); |
|
302 | - } |
|
303 | - return $tmpfname; |
|
304 | - } |
|
295 | + if ( is_wp_error( $response ) ) { |
|
296 | + unlink( $tmpfname ); |
|
297 | + return $response; |
|
298 | + } |
|
299 | + if ( 200 != wp_remote_retrieve_response_code( $response ) ) { |
|
300 | + unlink( $tmpfname ); |
|
301 | + return new WP_Error( 'http_404', trim( wp_remote_retrieve_response_message( $response ) ) ); |
|
302 | + } |
|
303 | + return $tmpfname; |
|
304 | + } |
|
305 | 305 | |
306 | - /** |
|
307 | - * Returns the url parameters, for example for url http://example.org/blog/post/news/2014/whatever |
|
308 | - * this will return array('blog', 'post', 'news', '2014', 'whatever'); |
|
309 | - * OR if sent an integer like: TimberUrlHelper::get_params(2); this will return 'news'; |
|
310 | - * |
|
311 | - * @param int $i the position of the parameter to grab. |
|
312 | - * @return array|string |
|
313 | - */ |
|
314 | - public static function get_params( $i = false ) { |
|
315 | - $args = explode( '/', trim( strtolower( $_SERVER['REQUEST_URI'] ) ) ); |
|
316 | - $newargs = array(); |
|
317 | - foreach ( $args as $arg ) { |
|
318 | - if ( strlen( $arg ) ) { |
|
319 | - $newargs[] = $arg; |
|
320 | - } |
|
321 | - } |
|
322 | - if ( $i === false ) { |
|
323 | - return $newargs; |
|
324 | - } |
|
325 | - if ( $i < 0 ) { |
|
326 | - //count from end |
|
327 | - $i = count( $newargs ) + $i; |
|
328 | - } |
|
329 | - if ( isset( $newargs[$i] ) ) { |
|
330 | - return $newargs[$i]; |
|
331 | - } |
|
332 | - } |
|
306 | + /** |
|
307 | + * Returns the url parameters, for example for url http://example.org/blog/post/news/2014/whatever |
|
308 | + * this will return array('blog', 'post', 'news', '2014', 'whatever'); |
|
309 | + * OR if sent an integer like: TimberUrlHelper::get_params(2); this will return 'news'; |
|
310 | + * |
|
311 | + * @param int $i the position of the parameter to grab. |
|
312 | + * @return array|string |
|
313 | + */ |
|
314 | + public static function get_params( $i = false ) { |
|
315 | + $args = explode( '/', trim( strtolower( $_SERVER['REQUEST_URI'] ) ) ); |
|
316 | + $newargs = array(); |
|
317 | + foreach ( $args as $arg ) { |
|
318 | + if ( strlen( $arg ) ) { |
|
319 | + $newargs[] = $arg; |
|
320 | + } |
|
321 | + } |
|
322 | + if ( $i === false ) { |
|
323 | + return $newargs; |
|
324 | + } |
|
325 | + if ( $i < 0 ) { |
|
326 | + //count from end |
|
327 | + $i = count( $newargs ) + $i; |
|
328 | + } |
|
329 | + if ( isset( $newargs[$i] ) ) { |
|
330 | + return $newargs[$i]; |
|
331 | + } |
|
332 | + } |
|
333 | 333 | |
334 | 334 | } |
@@ -9,8 +9,8 @@ discard block |
||
9 | 9 | */ |
10 | 10 | public static function get_current_url() { |
11 | 11 | $pageURL = "http://"; |
12 | - if ( isset( $_SERVER['HTTPS'] ) && $_SERVER["HTTPS"] == "on" ) { |
|
13 | - $pageURL = "https://";; |
|
12 | + if ( isset($_SERVER['HTTPS']) && $_SERVER["HTTPS"] == "on" ) { |
|
13 | + $pageURL = "https://"; ; |
|
14 | 14 | } |
15 | 15 | if ( isset($_SERVER["SERVER_PORT"]) && $_SERVER["SERVER_PORT"] && $_SERVER["SERVER_PORT"] != "80" ) { |
16 | 16 | $pageURL .= self::get_host() . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"]; |
@@ -27,11 +27,11 @@ discard block |
||
27 | 27 | * @return bool |
28 | 28 | */ |
29 | 29 | public static function is_url( $url ) { |
30 | - if ( !is_string( $url ) ) { |
|
30 | + if ( !is_string($url) ) { |
|
31 | 31 | return false; |
32 | 32 | } |
33 | - $url = strtolower( $url ); |
|
34 | - if ( strstr( $url, '://' ) ) { |
|
33 | + $url = strtolower($url); |
|
34 | + if ( strstr($url, '://') ) { |
|
35 | 35 | return true; |
36 | 36 | } |
37 | 37 | return false; |
@@ -43,11 +43,11 @@ discard block |
||
43 | 43 | * @return string |
44 | 44 | */ |
45 | 45 | public static function get_path_base() { |
46 | - $struc = get_option( 'permalink_structure' ); |
|
47 | - $struc = explode( '/', $struc ); |
|
46 | + $struc = get_option('permalink_structure'); |
|
47 | + $struc = explode('/', $struc); |
|
48 | 48 | $p = '/'; |
49 | 49 | foreach ( $struc as $s ) { |
50 | - if ( !strstr( $s, '%' ) && strlen( $s ) ) { |
|
50 | + if ( !strstr($s, '%') && strlen($s) ) { |
|
51 | 51 | $p .= $s . '/'; |
52 | 52 | } |
53 | 53 | } |
@@ -62,21 +62,21 @@ discard block |
||
62 | 62 | * @return string |
63 | 63 | */ |
64 | 64 | public static function get_rel_url( $url, $force = false ) { |
65 | - $url_info = parse_url( $url ); |
|
66 | - if ( isset( $url_info['host'] ) && $url_info['host'] != self::get_host() && !$force ) { |
|
65 | + $url_info = parse_url($url); |
|
66 | + if ( isset($url_info['host']) && $url_info['host'] != self::get_host() && !$force ) { |
|
67 | 67 | return $url; |
68 | 68 | } |
69 | 69 | $link = ''; |
70 | - if ( isset( $url_info['path'] ) ) { |
|
70 | + if ( isset($url_info['path']) ) { |
|
71 | 71 | $link = $url_info['path']; |
72 | 72 | } |
73 | - if ( isset( $url_info['query'] ) && strlen( $url_info['query'] ) ) { |
|
73 | + if ( isset($url_info['query']) && strlen($url_info['query']) ) { |
|
74 | 74 | $link .= '?' . $url_info['query']; |
75 | 75 | } |
76 | - if ( isset( $url_info['fragment'] ) && strlen( $url_info['fragment'] ) ) { |
|
76 | + if ( isset($url_info['fragment']) && strlen($url_info['fragment']) ) { |
|
77 | 77 | $link .= '#' . $url_info['fragment']; |
78 | 78 | } |
79 | - $link = TimberURLHelper::remove_double_slashes( $link ); |
|
79 | + $link = TimberURLHelper::remove_double_slashes($link); |
|
80 | 80 | return $link; |
81 | 81 | } |
82 | 82 | |
@@ -86,10 +86,10 @@ discard block |
||
86 | 86 | * @return string the HTTP_HOST or SERVER_NAME |
87 | 87 | */ |
88 | 88 | public static function get_host() { |
89 | - if (isset($_SERVER['HTTP_HOST'])) { |
|
89 | + if ( isset($_SERVER['HTTP_HOST']) ) { |
|
90 | 90 | return $_SERVER['HTTP_HOST']; |
91 | 91 | } |
92 | - if (isset($_SERVER['SERVER_NAME'])) { |
|
92 | + if ( isset($_SERVER['SERVER_NAME']) ) { |
|
93 | 93 | return $_SERVER['SERVER_NAME']; |
94 | 94 | } |
95 | 95 | return ''; |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | * @return bool |
103 | 103 | */ |
104 | 104 | public static function is_local( $url ) { |
105 | - if ( strstr( $url, self::get_host() ) ) { |
|
105 | + if ( strstr($url, self::get_host()) ) { |
|
106 | 106 | return true; |
107 | 107 | } |
108 | 108 | return false; |
@@ -117,7 +117,7 @@ discard block |
||
117 | 117 | public static function get_full_path( $src ) { |
118 | 118 | $root = ABSPATH; |
119 | 119 | $old_root_path = $root . $src; |
120 | - $old_root_path = str_replace( '//', '/', $old_root_path ); |
|
120 | + $old_root_path = str_replace('//', '/', $old_root_path); |
|
121 | 121 | return $old_root_path; |
122 | 122 | } |
123 | 123 | |
@@ -130,15 +130,15 @@ discard block |
||
130 | 130 | * @return string |
131 | 131 | */ |
132 | 132 | public static function url_to_file_system( $url ) { |
133 | - $url_parts = parse_url( $url ); |
|
133 | + $url_parts = parse_url($url); |
|
134 | 134 | $path = ABSPATH . $url_parts['path']; |
135 | - $path = str_replace( '//', '/', $path ); |
|
135 | + $path = str_replace('//', '/', $path); |
|
136 | 136 | return $path; |
137 | 137 | } |
138 | 138 | |
139 | 139 | public static function file_system_to_url( $fs ) { |
140 | - $relative_path = self::get_rel_path( $fs ); |
|
141 | - $home = home_url( '/'.$relative_path ); |
|
140 | + $relative_path = self::get_rel_path($fs); |
|
141 | + $home = home_url('/' . $relative_path); |
|
142 | 142 | return $home; |
143 | 143 | } |
144 | 144 | |
@@ -149,11 +149,11 @@ discard block |
||
149 | 149 | * @return string |
150 | 150 | */ |
151 | 151 | public static function get_rel_path( $src ) { |
152 | - if ( strstr( $src, ABSPATH ) ) { |
|
153 | - return str_replace( ABSPATH, '', $src ); |
|
152 | + if ( strstr($src, ABSPATH) ) { |
|
153 | + return str_replace(ABSPATH, '', $src); |
|
154 | 154 | } |
155 | 155 | //its outside the wordpress directory, alternate setups: |
156 | - $src = str_replace( WP_CONTENT_DIR, '', $src ); |
|
156 | + $src = str_replace(WP_CONTENT_DIR, '', $src); |
|
157 | 157 | return WP_CONTENT_SUBDIR . $src; |
158 | 158 | } |
159 | 159 | |
@@ -164,9 +164,9 @@ discard block |
||
164 | 164 | * @return string |
165 | 165 | */ |
166 | 166 | public static function remove_double_slashes( $url ) { |
167 | - $url = str_replace( '//', '/', $url ); |
|
168 | - if ( strstr( $url, 'http:' ) && !strstr( $url, 'http://' ) ) { |
|
169 | - $url = str_replace( 'http:/', 'http://', $url ); |
|
167 | + $url = str_replace('//', '/', $url); |
|
168 | + if ( strstr($url, 'http:') && !strstr($url, 'http://') ) { |
|
169 | + $url = str_replace('http:/', 'http://', $url); |
|
170 | 170 | } |
171 | 171 | return $url; |
172 | 172 | } |
@@ -179,19 +179,19 @@ discard block |
||
179 | 179 | * @return string |
180 | 180 | */ |
181 | 181 | public static function prepend_to_url( $url, $path ) { |
182 | - if ( strstr( strtolower( $url ), 'http' ) ) { |
|
183 | - $url_parts = parse_url( $url ); |
|
182 | + if ( strstr(strtolower($url), 'http') ) { |
|
183 | + $url_parts = parse_url($url); |
|
184 | 184 | $url = $url_parts['scheme'] . '://' . $url_parts['host'] . $path . $url_parts['path']; |
185 | - if ( isset( $url_parts['query'] ) ) { |
|
185 | + if ( isset($url_parts['query']) ) { |
|
186 | 186 | $url .= $url_parts['query']; |
187 | 187 | } |
188 | - if ( isset( $url_parts['fragment'] ) ) { |
|
188 | + if ( isset($url_parts['fragment']) ) { |
|
189 | 189 | $url .= $url_parts['fragment']; |
190 | 190 | } |
191 | 191 | } else { |
192 | 192 | $url = $url . $path; |
193 | 193 | } |
194 | - return self::remove_double_slashes( $url ); |
|
194 | + return self::remove_double_slashes($url); |
|
195 | 195 | } |
196 | 196 | |
197 | 197 | /** |
@@ -201,7 +201,7 @@ discard block |
||
201 | 201 | * @return string |
202 | 202 | */ |
203 | 203 | public static function preslashit( $path ) { |
204 | - if ( strpos( $path, '/' ) != 0 ) { |
|
204 | + if ( strpos($path, '/') != 0 ) { |
|
205 | 205 | $path = '/' . $path; |
206 | 206 | } |
207 | 207 | return $path; |
@@ -213,7 +213,7 @@ discard block |
||
213 | 213 | * @return boolean true if $path is an absolute url, false if relative. |
214 | 214 | */ |
215 | 215 | public static function is_absolute( $path ) { |
216 | - return (boolean) ( strstr( $path, 'http' ) ); |
|
216 | + return (boolean) (strstr($path, 'http')); |
|
217 | 217 | } |
218 | 218 | |
219 | 219 | /** |
@@ -225,21 +225,21 @@ discard block |
||
225 | 225 | * @return boolean if $url points to an external location returns true |
226 | 226 | */ |
227 | 227 | public static function is_external_content( $url ) { |
228 | - $is_external = TimberURLHelper::is_absolute( $url ) && ! TimberURLHelper::is_internal_content( $url ); |
|
228 | + $is_external = TimberURLHelper::is_absolute($url) && !TimberURLHelper::is_internal_content($url); |
|
229 | 229 | |
230 | 230 | return $is_external; |
231 | 231 | } |
232 | 232 | |
233 | - private static function is_internal_content($url) { |
|
233 | + private static function is_internal_content( $url ) { |
|
234 | 234 | // using content_url() instead of site_url or home_url is IMPORTANT |
235 | 235 | // otherwise you run into errors with sites that: |
236 | 236 | // 1. use WPML plugin |
237 | 237 | // 2. or redefine content directory |
238 | - $is_content_url = strstr( $url, content_url() ); |
|
238 | + $is_content_url = strstr($url, content_url()); |
|
239 | 239 | |
240 | 240 | // this case covers when the upload directory has been redefined |
241 | 241 | $upload_dir = wp_upload_dir(); |
242 | - $is_upload_url = strstr( $url, $upload_dir['baseurl'] ); |
|
242 | + $is_upload_url = strstr($url, $upload_dir['baseurl']); |
|
243 | 243 | |
244 | 244 | return $is_content_url || $is_upload_url; |
245 | 245 | } |
@@ -252,8 +252,8 @@ discard block |
||
252 | 252 | * true if it's a subdomain (http://cdn.example.org = true) |
253 | 253 | */ |
254 | 254 | public static function is_external( $url ) { |
255 | - $has_http = strstr( strtolower( $url ), 'http' ); |
|
256 | - $on_domain = strstr( $url, self::get_host() ); |
|
255 | + $has_http = strstr(strtolower($url), 'http'); |
|
256 | + $on_domain = strstr($url, self::get_host()); |
|
257 | 257 | if ( $has_http && !$on_domain ) { |
258 | 258 | return true; |
259 | 259 | } |
@@ -268,7 +268,7 @@ discard block |
||
268 | 268 | */ |
269 | 269 | public static function remove_trailing_slash( $link ) { |
270 | 270 | if ( $link != "/" ) |
271 | - $link = untrailingslashit( $link ); |
|
271 | + $link = untrailingslashit($link); |
|
272 | 272 | return $link; |
273 | 273 | } |
274 | 274 | |
@@ -282,23 +282,23 @@ discard block |
||
282 | 282 | */ |
283 | 283 | static function download_url( $url, $timeout = 300 ) { |
284 | 284 | if ( !$url ) { |
285 | - return new WP_Error( 'http_no_url', __( 'Invalid URL Provided.' ) ); |
|
285 | + return new WP_Error('http_no_url', __('Invalid URL Provided.')); |
|
286 | 286 | } |
287 | 287 | |
288 | - $tmpfname = wp_tempnam( $url ); |
|
288 | + $tmpfname = wp_tempnam($url); |
|
289 | 289 | if ( !$tmpfname ) { |
290 | - return new WP_Error( 'http_no_file', __( 'Could not create Temporary file.' ) ); |
|
290 | + return new WP_Error('http_no_file', __('Could not create Temporary file.')); |
|
291 | 291 | } |
292 | 292 | |
293 | - $response = wp_remote_get( $url, array( 'timeout' => $timeout, 'stream' => true, 'filename' => $tmpfname ) ); |
|
293 | + $response = wp_remote_get($url, array('timeout' => $timeout, 'stream' => true, 'filename' => $tmpfname)); |
|
294 | 294 | |
295 | - if ( is_wp_error( $response ) ) { |
|
296 | - unlink( $tmpfname ); |
|
295 | + if ( is_wp_error($response) ) { |
|
296 | + unlink($tmpfname); |
|
297 | 297 | return $response; |
298 | 298 | } |
299 | - if ( 200 != wp_remote_retrieve_response_code( $response ) ) { |
|
300 | - unlink( $tmpfname ); |
|
301 | - return new WP_Error( 'http_404', trim( wp_remote_retrieve_response_message( $response ) ) ); |
|
299 | + if ( 200 != wp_remote_retrieve_response_code($response) ) { |
|
300 | + unlink($tmpfname); |
|
301 | + return new WP_Error('http_404', trim(wp_remote_retrieve_response_message($response))); |
|
302 | 302 | } |
303 | 303 | return $tmpfname; |
304 | 304 | } |
@@ -312,10 +312,10 @@ discard block |
||
312 | 312 | * @return array|string |
313 | 313 | */ |
314 | 314 | public static function get_params( $i = false ) { |
315 | - $args = explode( '/', trim( strtolower( $_SERVER['REQUEST_URI'] ) ) ); |
|
315 | + $args = explode('/', trim(strtolower($_SERVER['REQUEST_URI']))); |
|
316 | 316 | $newargs = array(); |
317 | 317 | foreach ( $args as $arg ) { |
318 | - if ( strlen( $arg ) ) { |
|
318 | + if ( strlen($arg) ) { |
|
319 | 319 | $newargs[] = $arg; |
320 | 320 | } |
321 | 321 | } |
@@ -324,9 +324,9 @@ discard block |
||
324 | 324 | } |
325 | 325 | if ( $i < 0 ) { |
326 | 326 | //count from end |
327 | - $i = count( $newargs ) + $i; |
|
327 | + $i = count($newargs) + $i; |
|
328 | 328 | } |
329 | - if ( isset( $newargs[$i] ) ) { |
|
329 | + if ( isset($newargs[$i]) ) { |
|
330 | 330 | return $newargs[$i]; |
331 | 331 | } |
332 | 332 | } |
@@ -44,7 +44,7 @@ |
||
44 | 44 | * (ex: /src/var/www/wp-content/uploads/[email protected]) |
45 | 45 | * @return bool true if everything went fine, false otherwise |
46 | 46 | */ |
47 | - function run($load_filename, $save_filename){ |
|
47 | + function run($load_filename, $save_filename) { |
|
48 | 48 | $image = wp_get_image_editor( $load_filename ); |
49 | 49 | if ( !is_wp_error( $image ) ) { |
50 | 50 | $current_size = $image->get_size(); |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | /** |
3 | - * Contains the class for running image retina-izing operations |
|
4 | - */ |
|
3 | + * Contains the class for running image retina-izing operations |
|
4 | + */ |
|
5 | 5 | |
6 | 6 | /** |
7 | 7 | * Increases image size by a given factor |
@@ -56,11 +56,11 @@ discard block |
||
56 | 56 | $image->crop( 0, 0, $src_w, $src_h, $w, $h ); |
57 | 57 | $result = $image->save( $save_filename ); |
58 | 58 | if ( is_wp_error( $result ) ) { |
59 | - // @codeCoverageIgnoreStart |
|
60 | - TimberHelper::error_log( 'Error resizing image' ); |
|
61 | - TimberHelper::error_log( $result ); |
|
62 | - return false; |
|
63 | - // @codeCoverageIgnoreEnd |
|
59 | + // @codeCoverageIgnoreStart |
|
60 | + TimberHelper::error_log( 'Error resizing image' ); |
|
61 | + TimberHelper::error_log( $result ); |
|
62 | + return false; |
|
63 | + // @codeCoverageIgnoreEnd |
|
64 | 64 | } else { |
65 | 65 | return true; |
66 | 66 | } |
@@ -17,7 +17,7 @@ discard block |
||
17 | 17 | * Construct our operation |
18 | 18 | * @param float $factor to multiply original dimensions by |
19 | 19 | */ |
20 | - function __construct($factor) { |
|
20 | + function __construct( $factor ) { |
|
21 | 21 | $this->factor = $factor; |
22 | 22 | } |
23 | 23 | |
@@ -28,7 +28,7 @@ discard block |
||
28 | 28 | * @param string $src_extension the extension (ex: .jpg) |
29 | 29 | * @return string the final filename to be used (ex: [email protected]) |
30 | 30 | */ |
31 | - function filename($src_filename, $src_extension) { |
|
31 | + function filename( $src_filename, $src_extension ) { |
|
32 | 32 | $newbase = $src_filename . '@' . $this->factor . 'x'; // add @2x, @3x, @1.5x, etc. |
33 | 33 | $new_name = $newbase . '.' . $src_extension; |
34 | 34 | return $new_name; |
@@ -44,30 +44,30 @@ discard block |
||
44 | 44 | * (ex: /src/var/www/wp-content/uploads/[email protected]) |
45 | 45 | * @return bool true if everything went fine, false otherwise |
46 | 46 | */ |
47 | - function run($load_filename, $save_filename){ |
|
48 | - $image = wp_get_image_editor( $load_filename ); |
|
49 | - if ( !is_wp_error( $image ) ) { |
|
47 | + function run( $load_filename, $save_filename ) { |
|
48 | + $image = wp_get_image_editor($load_filename); |
|
49 | + if ( !is_wp_error($image) ) { |
|
50 | 50 | $current_size = $image->get_size(); |
51 | 51 | $src_w = $current_size['width']; |
52 | 52 | $src_h = $current_size['height']; |
53 | 53 | // Get ratios |
54 | 54 | $w = $src_w * $this->factor; |
55 | 55 | $h = $src_h * $this->factor; |
56 | - $image->crop( 0, 0, $src_w, $src_h, $w, $h ); |
|
57 | - $result = $image->save( $save_filename ); |
|
58 | - if ( is_wp_error( $result ) ) { |
|
56 | + $image->crop(0, 0, $src_w, $src_h, $w, $h); |
|
57 | + $result = $image->save($save_filename); |
|
58 | + if ( is_wp_error($result) ) { |
|
59 | 59 | // @codeCoverageIgnoreStart |
60 | - TimberHelper::error_log( 'Error resizing image' ); |
|
61 | - TimberHelper::error_log( $result ); |
|
60 | + TimberHelper::error_log('Error resizing image'); |
|
61 | + TimberHelper::error_log($result); |
|
62 | 62 | return false; |
63 | 63 | // @codeCoverageIgnoreEnd |
64 | 64 | } else { |
65 | 65 | return true; |
66 | 66 | } |
67 | - } else if ( isset( $image->error_data['error_loading_image'] ) ) { |
|
68 | - TimberHelper::error_log( 'Error loading ' . $image->error_data['error_loading_image'] ); |
|
67 | + } else if ( isset($image->error_data['error_loading_image']) ) { |
|
68 | + TimberHelper::error_log('Error loading ' . $image->error_data['error_loading_image']); |
|
69 | 69 | } else { |
70 | - TimberHelper::error_log( $image ); |
|
70 | + TimberHelper::error_log($image); |
|
71 | 71 | } |
72 | 72 | return false; |
73 | 73 | } |