Completed
Push — master ( 7206f2...88ad46 )
by Jared
03:56
created

TimberPost::next()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * This is the object you use to access or extend WordPress posts. Think of it as Timber's (more accessible) version of WP_Post. This is used throughout Timber to represent posts retrieved from WordPress making them available to Twig templates. See the PHP and Twig examples for an example of what it's like to work with this object in your code.
5
 * @example
6
 * ```php
7
 * <?php
8
 * // single.php, see connected twig example
9
 * $context = Timber::get_context();
10
 * $context['post'] = new TimberPost(); // It's a new TimberPost object, but an existing post from WordPress.
11
 * Timber::render('single.twig', $context);
12
 * ?>
13
 * ```
14
 * ```twig
15
 * {# single.twig #}
16
 * <article>
17
 *     <h1 class="headline">{{post.title}}</h1>
18
 *     <div class="body">
19
 *         {{post.content}}
20
 *     </div>
21
 * </article>
22
 * ```
23
 *
24
 * ```html
25
 * <article>
26
 *     <h1 class="headline">The Empire Strikes Back</h1>
27
 *     <div class="body">
28
 *         It is a dark time for the Rebellion. Although the Death Star has been destroyed, Imperial troops have driven the Rebel forces from their hidden base and pursued them across the galaxy.
29
 *     </div>
30
 * </article>
31
 * ```
32
 *
33
 * @package Timber
34
 */
35
class TimberPost extends TimberCore implements TimberCoreInterface {
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
	 * @deprecated since 0.21.7
98
	 * @var string $display_date @deprecated stores the display date (ex: "October 6, 1984"),
99
	 */
100
	public $display_date;
101
102
	/**
103
	 * @api
104
	 * @var string $id the numeric WordPress id of a post
105
	 */
106
	public $id;
107
108
	/**
109
	 * @var string 	$ID 			the numeric WordPress id of a post, capitalized to match WP usage
110
	 */
111
	public $ID;
112
113
	/**
114
	 * @var int 	$post_author 	the numeric ID of the a post's author corresponding to the wp_user dtable
115
	 */
116
	public $post_author;
117
118
	/**
119
	 * @var string 	$post_content 	the raw text of a WP post as stored in the database
120
	 */
121
	public $post_content;
122
123
	/**
124
	 * @var string 	$post_date 		the raw date string as stored in the WP database, ex: 2014-07-05 18:01:39
125
	 */
126
	public $post_date;
127
128
	/**
129
	 * @var string 	$post_exceprt 	the raw text of a manual post exceprt as stored in the database
130
	 */
131
	public $post_excerpt;
132
133
	/**
134
	 * @var int 		$post_parent 	the numeric ID of a post's parent post
135
	 */
136
	public $post_parent;
137
138
	/**
139
	 * @api
140
	 * @var string 		$post_status 	the status of a post ("draft", "publish", etc.)
141
	 */
142
	public $post_status;
143
144
	/**
145
	 * @var string 	$post_title 	the raw text of a post's title as stored in the database
146
	 */
147
	public $post_title;
148
149
	/**
150
	 * @api
151
	 * @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")
152
	 */
153
	public $post_type;
154
155
	/**
156
	 * @api
157
	 * @var string 	$slug 		the URL-safe slug, this corresponds to the poorly-named "post_name" in the WP database, ex: "hello-world"
158
	 */
159
	public $slug;
160
161
	/**
162
	 * If you send the constructor nothing it will try to figure out the current post id based on being inside The_Loop
163
	 * @example
164
	 * ```php
165
	 * $post = new TimberPost();
166
	 * $other_post = new TimberPost($random_post_id);
167
	 * ```
168
	 * @param mixed $pid
169
	 */
170
	public function __construct($pid = null) {
171
		$pid = $this->determine_id( $pid );
172
		$this->init($pid);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
173
	}
174
175
	/**
176
	 * tries to figure out what post you want to get if not explictly defined (or if it is, allows it to be passed through)
177
	 * @internal
178
	 * @param mixed a value to test against
179
	 * @return int the numberic id we should be using for this post object
180
	 */
181
	protected function determine_id($pid) {
182
		global $wp_query;
183
		if ( $pid === null &&
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
184
			isset($wp_query->queried_object_id)
185
			&& $wp_query->queried_object_id
186
			&& isset($wp_query->queried_object)
187
			&& is_object($wp_query->queried_object)
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
188
			&& get_class($wp_query->queried_object) == 'WP_Post'
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
189
			) {
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 )  {
192
			//hack for static page as home page
193
			$pid = $wp_query->queried_object_id;
194
		} else if ( $pid === null ) {
195
			$gtid = false;
196
			$maybe_post = get_post();
197
			if ( isset($maybe_post->ID) ){
198
				$gtid = true;
199
			}
200
			if ( $gtid ) {
201
				$pid = get_the_ID();
202
			}
203
			if ( !$pid ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
204
				global $wp_query;
205
				if ( isset($wp_query->query['p']) ) {
206
					$pid = $wp_query->query['p'];
207
				}
208
			}
209
		}
210
		if ( $pid === null && ($pid_from_loop = TimberPostGetter::loop_to_id()) ) {
211
			$pid = $pid_from_loop;
212
		}
213
		return $pid;
214
	}
215
216
	/**
217
	 * Outputs the title of the post if you do something like `<h1>{{post}}</h1>`
218
	 * @return string
219
	 */
220
	public function __toString() {
221
		return $this->title();
222
	}
223
224
225
	/**
226
	 * Initializes a TimberPost
227
	 * @internal
228
	 * @param int|bool $pid
229
	 */
230
	protected function init($pid = false) {
231
		if ( $pid === false ) {
0 ignored issues
show
introduced by
Found "=== false". Use Yoda Condition checks, you must
Loading history...
232
			$pid = get_the_ID();
233
		}
234
		if ( is_numeric($pid) ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
235
			$this->ID = $pid;
0 ignored issues
show
Documentation Bug introduced by
It seems like $pid can also be of type integer or double. However, the property $ID is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
236
		}
237
		$post_info = $this->get_info($pid);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
238
		$this->import($post_info);
0 ignored issues
show
Bug introduced by
It seems like $post_info defined by $this->get_info($pid) on line 237 can also be of type null; however, TimberCore::import() does only seem to accept array|object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
239
		/* deprecated, adding for support for older themes */
240
		$this->display_date = $this->date();
0 ignored issues
show
Deprecated Code introduced by
The property TimberPost::$display_date has been deprecated with message: since 0.21.7

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
241
		//cant have a function, so gots to do it this way
242
		$post_class = $this->post_class();
243
		$this->class = $post_class;
244
	}
245
246
	/**
247
	 * Get the URL that will edit the current post/object
248
	 * @internal
249
	 * @see TimberPost::edit_link
250
	 * @return bool|string
251
	 */
252
	function get_edit_url() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
253
		if ( $this->can_edit() ) {
254
			return get_edit_post_link($this->ID);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
255
		}
256
	}
257
258
	/**
259
	 * updates the post_meta of the current object with the given value
260
	 * @param string $field
261
	 * @param mixed $value
262
	 */
263
	public function update( $field, $value ) {
264
		if ( isset($this->ID) ) {
265
			update_post_meta($this->ID, $field, $value);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
266
			$this->$field = $value;
267
		}
268
	}
269
270
271
	/**
272
	 * takes a mix of integer (post ID), string (post slug),
273
	 * or object to return a WordPress post object from WP's built-in get_post() function
274
	 * @internal
275
	 * @param mixed $pid
276
	 * @return WP_Post on success
277
	 */
278
	protected function prepare_post_info( $pid = 0 ) {
279
		if ( is_string($pid) || is_numeric($pid) || (is_object($pid) && !isset($pid->post_title)) || $pid === 0 ) {
0 ignored issues
show
introduced by
Found "=== 0". Use Yoda Condition checks, you must
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
280
			$pid = self::check_post_id($pid);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
281
			$post = get_post($pid);
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
282
			if ( $post ) {
283
				return $post;
284
			} else {
285
				$post = get_page($pid);
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
286
				return $post;
287
			}
288
		}
289
		//we can skip if already is WP_Post
290
		return $pid;
291
	}
292
293
294
	/**
295
	 * helps you find the post id regardless of whether you send a string or whatever
296
	 * @param integer $pid ;
297
	 * @internal
298
	 * @return integer ID number of a post
299
	 */
300
	protected function check_post_id( $pid ) {
301
		if ( is_numeric($pid) && $pid === 0 ) {
0 ignored issues
show
introduced by
Found "=== 0". Use Yoda Condition checks, you must
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
302
			$pid = get_the_ID();
303
			return $pid;
304
		}
305
		if ( !is_numeric($pid) && is_string($pid) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
306
			$pid = self::get_post_id_by_name($pid);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
307
			return $pid;
308
		}
309
		if ( !$pid ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
310
			return null;
311
		}
312
		return $pid;
313
	}
314
315
316
	/**
317
	 * get_post_id_by_name($post_name)
318
	 * @internal
319
	 * @param string $post_name
320
	 * @return int
321
	 */
322
	static function get_post_id_by_name($post_name) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
323
		global $wpdb;
324
		$query = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_name = %s LIMIT 1", $post_name);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
325
		$result = $wpdb->get_row($query);
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
326
		if (!$result) {
0 ignored issues
show
introduced by
No space after opening parenthesis is prohibited
Loading history...
introduced by
Expected 1 space before "!"; 0 found
Loading history...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
introduced by
No space before closing parenthesis is prohibited
Loading history...
327
			return null;
328
		}
329
		return $result->ID;
330
	}
331
332
	/**
333
	 * get a preview of your post, if you have an excerpt it will use that,
334
	 * otherwise it will pull from the post_content.
335
	 * If there's a <!-- more --> tag it will use that to mark where to pull through.
336
	 * @api
337
	 * @example
338
	 * ```twig
339
	 * <p>{{post.get_preview(50)}}</p>
340
	 * ```
341
	 * @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
342
	 * @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
343
	 * @param string $readmore The text you want to use on the 'readmore' link
344
	 * @param bool $strip Strip tags? yes or no. tell me!
345
	 * @return string of the post preview
346
	 */
347
	function get_preview($len = 50, $force = false, $readmore = 'Read More', $strip = true) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
348
		$text = '';
349
		$trimmed = false;
350
		if ( isset($this->post_excerpt) && strlen($this->post_excerpt) ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
351
			if ( $force ) {
352
				$text = TimberHelper::trim_words($this->post_excerpt, $len, false);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
353
				$trimmed = true;
354
			} else {
355
				$text = $this->post_excerpt;
356
			}
357
		}
358
		if ( !strlen($text) && preg_match('/<!--\s?more(.*?)?-->/', $this->post_content, $readmore_matches) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
359
			$pieces = explode($readmore_matches[0], $this->post_content);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
360
			$text = $pieces[0];
361
			if ( $force ) {
362
				$text = TimberHelper::trim_words($text, $len, false);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
363
				$trimmed = true;
364
			}
365
			$text = do_shortcode( $text );
366
		}
367
		if ( !strlen($text) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
368
			$text = TimberHelper::trim_words($this->get_content(), $len, false);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
369
			$trimmed = true;
370
		}
371
		if ( !strlen(trim($text)) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
372
			return trim($text);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
373
		}
374
		if ( $strip ) {
375
			$text = trim(strip_tags($text));
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
376
		}
377
		if ( strlen($text) ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
378
			$text = trim($text);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
379
			$last = $text[strlen($text) - 1];
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
380
			if ( $last != '.' && $trimmed ) {
381
				$text .= ' &hellip; ';
382
			}
383
			if ( !$strip ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
384
				$last_p_tag = strrpos($text, '</p>');
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
385
				if ( $last_p_tag !== false ) {
0 ignored issues
show
introduced by
Found "!== false". Use Yoda Condition checks, you must
Loading history...
386
					$text = substr($text, 0, $last_p_tag);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
387
				}
388
				if ( $last != '.' && $trimmed ) {
389
					$text .= ' &hellip; ';
390
				}
391
			}
392
			if ( $readmore && isset($readmore_matches) && !empty($readmore_matches[1]) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
393
				$text .= ' <a href="' . $this->get_permalink() . '" class="read-more">' . trim($readmore_matches[1]) . '</a>';
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
394
			} elseif ( $readmore ) {
395
				$text .= ' <a href="' . $this->get_permalink() . '" class="read-more">' . trim($readmore) . '</a>';
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
396
			}
397
			if ( !$strip && $last_p_tag && ( strpos($text, '<p>') || strpos($text, '<p ') ) ) {
0 ignored issues
show
Bug introduced by
The variable $last_p_tag does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
398
				$text .= '</p>';
399
			}
400
		}
401
		return trim($text);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
402
	}
403
404
	/**
405
	 * gets the post custom and attaches it to the current object
406
	 * @internal
407
	 * @param bool|int $pid a post ID number
408
	 */
409
	function import_custom( $pid = false ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
410
		if ( !$pid ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
411
			$pid = $this->ID;
412
		}
413
		$customs = $this->get_post_custom($pid);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
414
		$this->import($customs);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
415
	}
416
417
	/**
418
	 * Used internally to fetch the metadata fields (wp_postmeta table)
419
	 * and attach them to our TimberPost object
420
	 * @internal
421
	 * @param int $pid
422
	 * @return array
423
	 */
424
	protected function get_post_custom( $pid ) {
425
		apply_filters('timber_post_get_meta_pre', array(), $pid, $this);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
426
		$customs = get_post_custom($pid);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
427
		if ( !is_array($customs) || empty($customs) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
428
			return array();
429
		}
430 View Code Duplication
		foreach ( $customs as $key => $value ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
431
			if ( is_array($value) && count($value) == 1 && isset($value[0]) ) {
0 ignored issues
show
introduced by
Found "== 1". Use Yoda Condition checks, you must
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
432
				$value = $value[0];
433
			}
434
			$customs[$key] = maybe_unserialize($value);
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
435
		}
436
		$customs = apply_filters('timber_post_get_meta', $customs, $pid, $this);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
437
		return $customs;
438
	}
439
440
	/**
441
	 * @internal
442
	 * @see TimberPost::thumbnail
443
	 * @return null|TimberImage
0 ignored issues
show
Documentation introduced by
Should the return type not be object|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
444
	 */
445
	function get_thumbnail() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
446
		if ( function_exists('get_post_thumbnail_id') ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
447
			$tid = get_post_thumbnail_id($this->ID);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
448
			if ( $tid ) {
449
				return new $this->ImageClass($tid);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
450
			}
451
		}
452
	}
453
454
	/**
455
	 * @internal
456
	 * @see TimberPost::link
457
	 * @return string
458
	 */
459
	function get_permalink() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
460
		if ( isset($this->_permalink) ) {
461
			return $this->_permalink;
462
		}
463
		$this->_permalink = get_permalink($this->ID);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
464
		return $this->_permalink;
465
	}
466
467
	/**
468
	 * get the permalink for a post object
469
	 * In your templates you should use link:
470
	 * <a href="{{post.link}}">Read my post</a>
471
	 * @internal
472
	 * @return string
473
	 */
474
	function get_link() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
475
		return $this->get_permalink();
476
	}
477
478
	/**
479
	 * Get the next post in WordPress's ordering
480
	 * @internal
481
	 * @param bool $taxonomy
482
	 * @return TimberPost|boolean
483
	 */
484
	function get_next( $taxonomy = false ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
485
		if ( !isset($this->_next) || !isset($this->_next[$taxonomy]) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
486
			global $post;
487
			$this->_next = array();
488
			$old_global = $post;
489
			$post = $this;
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
490
			if ( $taxonomy ) {
491
				$adjacent = get_adjacent_post(true, '', false, $taxonomy);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
492
			} else {
493
				$adjacent = get_adjacent_post(false, '', false);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
494
			}
495
496
			if ( $adjacent ) {
497
				$this->_next[$taxonomy] = new $this->PostClass($adjacent);
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
498
			} else {
499
				$this->_next[$taxonomy] = false;
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
500
			}
501
			$post = $old_global;
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
502
		}
503
		return $this->_next[$taxonomy];
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
504
	}
505
506
	/**
507
	 * Get a data array of pagination so you can navigate to the previous/next for a paginated post
508
	 * @return array
509
	 */
510
	public function get_pagination() {
511
		global $post, $page, $numpages, $multipage;
512
		$post = $this;
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
513
		$ret = array();
514
		if ( $multipage ) {
515
			for ( $i = 1; $i <= $numpages; $i++ ) {
516
				$link = self::get_wp_link_page($i);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
517
				$data = array('name' => $i, 'title' => $i, 'text' => $i, 'link' => $link);
0 ignored issues
show
introduced by
No space after opening parenthesis of array is bad style
Loading history...
introduced by
No space before closing parenthesis of array is bad style
Loading history...
518
				if ( $i == $page ) {
519
					$data['current'] = true;
520
				}
521
				$ret['pages'][] = $data;
522
			}
523
			$i = $page - 1;
524
			if ( $i ) {
525
				$link = self::get_wp_link_page($i);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
526
				$ret['prev'] = array('link' => $link);
0 ignored issues
show
introduced by
No space after opening parenthesis of array is bad style
Loading history...
introduced by
No space before closing parenthesis of array is bad style
Loading history...
527
			}
528
			$i = $page + 1;
529
			if ( $i <= $numpages ) {
530
				$link = self::get_wp_link_page($i);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
531
				$ret['next'] = array('link' => $link);
0 ignored issues
show
introduced by
No space after opening parenthesis of array is bad style
Loading history...
introduced by
No space before closing parenthesis of array is bad style
Loading history...
532
			}
533
		}
534
		return $ret;
535
	}
536
537
	/**
538
	 * @param int $i
539
	 * @return string
540
	 */
541
	protected static function get_wp_link_page($i) {
542
		$link = _wp_link_page($i);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
543
		$link = new SimpleXMLElement($link . '</a>');
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
544
		if ( isset($link['href']) ) {
545
			return $link['href'];
546
		}
547
		return '';
548
	}
549
550
	/**
551
	 * Get the permalink for a post, but as a relative path
552
	 * For example, where {{post.link}} would return "http://example.org/2015/07/04/my-cool-post"
553
	 * this will return the relative version: "/2015/07/04/my-cool-post"
554
	 * @internal
555
	 * @return string
556
	 */
557
	function get_path() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
558
		return TimberURLHelper::get_rel_url($this->get_link());
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
559
	}
560
561
	/**
562
	 * Get the next post in WordPress's ordering
563
	 * @internal
564
	 * @param bool $taxonomy
565
	 * @return TimberPost|boolean
566
	 */
567
	function get_prev( $taxonomy = false ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
568
		if ( isset($this->_prev) && isset($this->_prev[$taxonomy]) ) {
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
569
			return $this->_prev[$taxonomy];
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
570
		}
571
		global $post;
572
		$old_global = $post;
573
		$post = $this;
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
574
		$within_taxonomy = ($taxonomy) ? $taxonomy : 'category';
575
		$adjacent = get_adjacent_post(($taxonomy), '', true, $within_taxonomy);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
576
		$prev_in_taxonomy = false;
577
		if ( $adjacent ) {
578
			$prev_in_taxonomy = new $this->PostClass($adjacent);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
579
		}
580
		$this->_prev[$taxonomy] = $prev_in_taxonomy;
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
581
		$post = $old_global;
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
582
		return $this->_prev[$taxonomy];
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
583
	}
584
585
	/**
586
	 * Get the parent post of the post
587
	 * @internal
588
	 * @return bool|TimberPost
0 ignored issues
show
Documentation introduced by
Should the return type not be false|object?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
589
	 */
590
	function get_parent() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
591
		if ( !$this->post_parent ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
592
			return false;
593
		}
594
		return new $this->PostClass($this->post_parent);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
595
	}
596
597
	/**
598
	 * Gets a User object from the author of the post
599
	 * @internal
600
	 * @see TimberPost::author
601
	 * @return bool|TimberUser
0 ignored issues
show
Documentation introduced by
Should the return type not be TimberUser|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
602
	 */
603
	function get_author() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
604
		if ( isset($this->post_author) ) {
605
			return new TimberUser($this->post_author);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
606
		}
607
	}
608
609
	/**
610
	 * @internal
611
	 * @return bool|TimberUser
0 ignored issues
show
Documentation introduced by
Should the return type not be TimberUser|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
612
	 */
613
	function get_modified_author() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
614
		$user_id = get_post_meta($this->ID, '_edit_last', true);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
615
		return ($user_id ? new TimberUser($user_id) : $this->get_author());
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
616
	}
617
618
	/**
619
	 * Used internally by init, etc. to build TimberPost object
620
	 * @internal
621
	 * @param  int $pid
622
	 * @return null|object|WP_Post
623
	 */
624
	protected function get_info($pid) {
625
		$post = $this->prepare_post_info($pid);
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
626
		if ( !isset($post->post_status) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
627
			return null;
628
		}
629
		$post->status = $post->post_status;
630
		$post->id = $post->ID;
631
		$post->slug = $post->post_name;
632
		$customs = $this->get_post_custom($post->ID);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
633
		$post->custom = $customs;
634
		$post = (object) array_merge((array)$customs, (array)$post);
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
introduced by
No space before opening casting parenthesis is prohibited
Loading history...
introduced by
No space after closing casting parenthesis is prohibited
Loading history...
635
		return $post;
636
	}
637
638
	/**
639
	 * Get the human-friendly date that should actually display in a .twig template
640
	 * @deprecated since 0.20.0
641
	 * @see TimberPost::date
642
	 * @param string $use
643
	 * @return string
644
	 */
645
	function get_display_date( $use = 'post_date' ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
646
		return date(get_option('date_format'), strtotime($this->$use));
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
647
	}
648
649
	/**
650
	 * @internal
651
	 * @see TimberPost::date
652
	 * @param  string $date_format
653
	 * @return string
654
	 */
655
	function get_date( $date_format = '' ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
656
		$df = $date_format ? $date_format : get_option('date_format');
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
657
		$the_date = (string)mysql2date($df, $this->post_date);
0 ignored issues
show
introduced by
No space after closing casting parenthesis is prohibited
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
658
		return apply_filters('get_the_date', $the_date, $df);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
659
	}
660
661
	/**
662
	 * @internal
663
	 * @param  string $date_format
664
	 * @return string
665
	 */
666
	function get_modified_date( $date_format = '' ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
667
		$df = $date_format ? $date_format : get_option('date_format');
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
668
		$the_time = $this->get_modified_time($df);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
669
		return apply_filters('get_the_modified_date', $the_time, $date_format);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
670
	}
671
672
	/**
673
	 * @internal
674
	 * @param  string $time_format
675
	 * @return string
676
	 */
677
	function get_modified_time( $time_format = '' ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
678
		$tf = $time_format ? $time_format : get_option('time_format');
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
679
		$the_time = get_post_modified_time($tf, false, $this->ID, true);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
680
		return apply_filters('get_the_modified_time', $the_time, $time_format);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
681
	}
682
683
	/**
684
	 * @internal
685
	 * @see TimberPost::children
686
	 * @param string 		$post_type
687
	 * @param bool|string 	$childPostClass
688
	 * @return array
689
	 */
690
	function get_children( $post_type = 'any', $childPostClass = false ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
691
		if ( $childPostClass === false ) {
0 ignored issues
show
introduced by
Found "=== false". Use Yoda Condition checks, you must
Loading history...
692
			$childPostClass = $this->PostClass;
693
		}
694
		if ( $post_type == 'parent' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
695
			$post_type = $this->post_type;
696
		}
697
		$children = get_children('post_parent=' . $this->ID . '&post_type=' . $post_type . '&numberposts=-1&orderby=menu_order title&order=ASC');
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
introduced by
Disabling pagination is prohibited in VIP context, do not set numberposts to -1 ever.
Loading history...
698
		foreach ( $children as &$child ) {
699
			$child = new $childPostClass($child->ID);
700
		}
701
		$children = array_values($children);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
702
		return $children;
703
	}
704
705
	/**
706
	 * Get the comments for a post
707
	 * @internal
708
	 * @see TimberPost::comments
709
	 * @param int $ct
710
	 * @param string $order
711
	 * @param string $type
712
	 * @param string $status
713
	 * @param string $CommentClass
714
	 * @return array|mixed
715
	 */
716
717
	function get_comments($ct = 0, $order = 'wp', $type = 'comment', $status = 'approve', $CommentClass = 'TimberComment') {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
718
719
		global $overridden_cpage;
720
		$overridden_cpage = false;
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
721
722
		$args = array('post_id' => $this->ID, 'status' => $status, 'order' => $order);
0 ignored issues
show
introduced by
No space after opening parenthesis of array is bad style
Loading history...
introduced by
No space before closing parenthesis of array is bad style
Loading history...
723
		if ( $ct > 0 ) {
724
			$args['number'] = $ct;
725
		}
726
		if ( strtolower($order) == 'wp' || strtolower($order) == 'wordpress' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
727
			$args['order'] = get_option('comment_order');
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
728
		}
729
730
		$comments = get_comments($args);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
731
		$timber_comments = array();
732
733
		if ( '' == get_query_var('cpage') && get_option('page_comments') ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
734
			set_query_var( 'cpage', 'newest' == get_option('default_comments_page') ? get_comment_pages_count() : 1 );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
735
			$overridden_cpage = true;
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
736
		}
737
738
        foreach( $comments as $key => &$comment ) {
0 ignored issues
show
Coding Style introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
739
            $timber_comment = new $CommentClass($comment);
0 ignored issues
show
Coding Style introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
740
            $timber_comments[$timber_comment->id] = $timber_comment;
0 ignored issues
show
Coding Style introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
741
        }
0 ignored issues
show
Coding Style introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
742
743
		foreach( $timber_comments as $key => $comment ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
744
			if ( $comment->is_child() ) {
745
				unset($timber_comments[$comment->id]);
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
746
747
				if ( isset($timber_comments[$comment->comment_parent]) ) {
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
748
					$timber_comments[$comment->comment_parent]->children[] = $comment;
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
749
				}
750
			}
751
		}
752
753
		$timber_comments = array_values($timber_comments);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
754
755
		return $timber_comments;
756
	}
757
758
	/**
759
	 * Get the categories for a post
760
	 * @internal
761
	 * @see TimberPost::categories
762
	 * @return array of TimberTerms
763
	 */
764
	function get_categories() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
765
		return $this->get_terms('category');
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
766
	}
767
768
	/**
769
	 * @internal
770
	 * @see TimberPost::category
771
	 * @return mixed
772
	 */
773
	function get_category( ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
774
		$cats = $this->get_categories();
775
		if ( count($cats) && isset($cats[0]) ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
776
			return $cats[0];
777
		}
778
	}
779
780
	/**
781
	 * @internal
782
	 * @param string|array $tax
783
	 * @param bool $merge
784
	 * @param string $TermClass
785
	 * @return array
786
	 */
787
	function get_terms( $tax = '', $merge = true, $TermClass = '' ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
788
789
		$TermClass = $TermClass ?: $this->TermClass;
790
791
		if ( is_string($merge) && class_exists($merge) ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
792
			$TermClass = $merge;
793
		}
794
		if ( is_array($tax) ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
795
			$taxonomies = $tax;
796
		}
797
		if ( is_string($tax) ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
798
			if ( in_array($tax, array('all','any','')) ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
introduced by
No space after opening parenthesis of array is bad style
Loading history...
introduced by
Expected 1 space between comma and "'any'"; 0 found
Loading history...
introduced by
Expected 1 space between comma and "''"; 0 found
Loading history...
introduced by
No space before closing parenthesis of array is bad style
Loading history...
799
				$taxonomies = get_object_taxonomies($this->post_type);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
800
			} else {
801
				$taxonomies = array($tax);
0 ignored issues
show
introduced by
No space after opening parenthesis of array is bad style
Loading history...
introduced by
No space before closing parenthesis of array is bad style
Loading history...
802
			}
803
		}
804
805
		$term_class_objects = array();
806
807
		foreach ( $taxonomies as $taxonomy ) {
0 ignored issues
show
Bug introduced by
The variable $taxonomies does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
808
			if ( in_array($taxonomy, array('tag','tags')) ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
introduced by
No space after opening parenthesis of array is bad style
Loading history...
introduced by
Expected 1 space between comma and "'tags'"; 0 found
Loading history...
introduced by
No space before closing parenthesis of array is bad style
Loading history...
809
				$taxonomy = 'post_tag';
810
			}
811
			if ( $taxonomy == 'categories' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
812
				$taxonomy = 'category';
813
			}
814
815
			$terms = wp_get_post_terms($this->ID, $taxonomy);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
816
817
			if ( is_wp_error($terms) ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
818
				/* @var $terms WP_Error */
819
				TimberHelper::error_log("Error retrieving terms for taxonomy '$taxonomy' on a post in timber-post.php");
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
820
				TimberHelper::error_log('tax = ' . print_r($tax, true));
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
introduced by
The use of function print_r() is discouraged
Loading history...
821
				TimberHelper::error_log('WP_Error: ' . $terms->get_error_message());
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
822
823
				return $term_class_objects;
824
			}
825
826
			// map over array of wordpress terms, and transform them into instances of the TermClass
827
			$terms = array_map(function($term) use ($TermClass, $taxonomy) {
828
				return call_user_func(array($TermClass, 'from'), $term->term_id, $taxonomy);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
introduced by
No space after opening parenthesis of array is bad style
Loading history...
introduced by
No space before closing parenthesis of array is bad style
Loading history...
829
			}, $terms);
830
831
			if ( $merge && is_array($terms) ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
832
				$term_class_objects = array_merge($term_class_objects, $terms);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
833
			} else if ( count($terms) ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
834
				$term_class_objects[$taxonomy] = $terms;
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
835
			}
836
		}
837
838
		return $term_class_objects;
839
	}
840
841
	/**
842
	 * @param string|int $term_name_or_id
843
	 * @param string $taxonomy
844
	 * @return bool
845
	 */
846
	function has_term( $term_name_or_id, $taxonomy = 'all' ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
847
		if ( $taxonomy == 'all' || $taxonomy == 'any' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
848
			$taxes = get_object_taxonomies($this->post_type, 'names');
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
849
			$ret = false;
850
			foreach ( $taxes as $tax ) {
851
				if ( has_term($term_name_or_id, $tax, $this->ID) ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
852
					$ret = true;
853
					break;
854
				}
855
			}
856
			return $ret;
857
		}
858
		return has_term($term_name_or_id, $taxonomy, $this->ID);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
859
	}
860
861
	/**
862
	 * @param string $field
863
	 * @return TimberImage
864
	 */
865
	function get_image( $field ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
866
		return new $this->ImageClass($this->$field);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
867
	}
868
869
	/**
870
	 * Gets an array of tags for you to use
871
	 * @internal
872
	 * @example
873
	 * ```twig
874
	 * <ul class="tags">
875
	 *     {% for tag in post.tags %}
876
	 *         <li>{{tag.name}}</li>
877
	 *     {% endfor %}
878
	 * </ul>
879
	 * ```
880
	 * @return array
881
	 */
882
	function get_tags() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
883
		return $this->get_terms('post_tag');
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
884
	}
885
886
	/**
887
	 * Outputs the title with filters applied
888
	 * @internal
889
	 * @example
890
	 * ```twig
891
	 * <h1>{{post.get_title}}</h1>
892
	 * ```
893
	 * ```html
894
	 * <h1>Hello World!</h1>
895
	 * ```
896
	 * @return string
897
	 */
898
	function get_title() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
899
		return apply_filters('the_title', $this->post_title, $this->ID);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
900
	}
901
902
	/**
903
	 * Displays the content of the post with filters, shortcodes and wpautop applied
904
	 * @example
905
	 * ```twig
906
	 * <div class="article-text">{{post.get_content}}</div>
907
	 * ```
908
	 * ```html
909
	 * <div class="article-text"><p>Blah blah blah</p><p>More blah blah blah.</p></div>
910
	 * ```
911
	 * @param int $len
912
	 * @param int $page
913
	 * @return string
914
	 */
915
	function get_content( $len = 0, $page = 0 ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
introduced by
Overridding WordPress globals is prohibited
Loading history...
916
		if ( $len == 0 && $page == 0 && $this->_content ) {
0 ignored issues
show
introduced by
Found "== 0". Use Yoda Condition checks, you must
Loading history...
917
			return $this->_content;
918
		}
919
		$content = $this->post_content;
920
		if ( $len ) {
921
			$content = wp_trim_words($content, $len);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
922
		}
923
		if ( $page ) {
924
			$contents = explode('<!--nextpage-->', $content);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
925
			$page--;
926
			if ( count($contents) > $page ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
927
				$content = $contents[$page];
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
928
			}
929
		}
930
		$content = apply_filters('the_content', ($content));
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
931
		if ( $len == 0 && $page == 0 ) {
0 ignored issues
show
introduced by
Found "== 0". Use Yoda Condition checks, you must
Loading history...
932
			$this->_content = $content;
933
		}
934
		return $content;
935
	}
936
937
	/**
938
	 * @return string
939
	 */
940
	function get_paged_content() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
941
		global $page;
942
		return $this->get_content(0, $page);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
943
	}
944
	/**
945
	 *
946
	 * Here is my summary
947
	 * @example
948
	 * ```twig
949
	 * This post is from <span>{{ post.get_post_type.labels.plural }}</span>
950
	 * ```
951
	 *
952
	 * ```html
953
	 * This post is from <span>Recipes</span>
954
	 * ```
955
	 * @return mixed
956
	 */
957
	public function get_post_type() {
958
		return get_post_type_object($this->post_type);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
959
	}
960
961
	/**
962
	 * @return int
963
	 */
964
	public function get_comment_count() {
965
		if ( isset($this->ID) ) {
966
			return get_comments_number($this->ID);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
967
		} else {
968
			return 0;
969
		}
970
	}
971
972
	/**
973
	 * @param string $field_name
974
	 * @return mixed
975
	 */
976
	public function get_field( $field_name ) {
977
		$value = apply_filters('timber_post_get_meta_field_pre', null, $this->ID, $field_name, $this);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
978
		if ( $value === null ) {
979
			$value = get_post_meta($this->ID, $field_name);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
980
			if ( is_array($value) && count($value) == 1 ) {
0 ignored issues
show
introduced by
Found "== 1". Use Yoda Condition checks, you must
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
981
				$value = $value[0];
982
			}
983
			if ( is_array($value) && count($value) == 0 ) {
0 ignored issues
show
introduced by
Found "== 0". Use Yoda Condition checks, you must
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
984
				$value = null;
985
			}
986
		}
987
		$value = apply_filters('timber_post_get_meta_field', $value, $this->ID, $field_name, $this);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
988
		return $value;
989
	}
990
991
	/**
992
	 * @param string $field_name
993
	 */
994
	function import_field( $field_name ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
995
		$this->$field_name = $this->get_field($field_name);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
996
	}
997
998
	/**
999
	 * @internal
1000
	 * @return mixed
1001
	 */
1002
	function get_format() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
1003
		return get_post_format($this->ID);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
1004
	}
1005
1006
	/**
1007
	 * Get the CSS classes for a post. For usage you should use `{{post.class}}` instead of `{{post.post_class}}`
1008
	 * @internal
1009
	 * @param string $class additional classes you want to add
1010
	 * @see TimberPost::$class
1011
	 * @example
1012
	 * ```twig
1013
	 * <article class="{{ post.class }}">
1014
	 *    {# Some stuff here #}
1015
	 * </article>
1016
	 * ```
1017
	 *
1018
	 * ```html
1019
	 * <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">
1020
	 *    {# Some stuff here #}
1021
	 * </article>
1022
	 * ```
1023
	 * @return string a space-seperated list of classes
1024
	 */
1025
	public function post_class( $class='' ) {
0 ignored issues
show
introduced by
Expected 1 space between argument "$class" and equals sign; 0 found
Loading history...
introduced by
Expected 1 space between default value and equals sign for argument "$class";
Loading history...
1026
		global $post;
1027
		$old_global_post = $post;
1028
		$post = $this;
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
1029
		$class_array = get_post_class($class, $this->ID);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
1030
		$post = $old_global_post;
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
1031
		if ( is_array($class_array) ){
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
1032
			return implode(' ', $class_array);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
1033
		}
1034
		return $class_array;
1035
	}
1036
1037
	// Docs
1038
1039
	/**
1040
	 * @return array
1041
	 * @codeCoverageIgnore
1042
	 */
1043
	public function get_method_values() {
1044
		$ret = parent::get_method_values();
1045
		$ret['author'] = $this->author();
1046
		$ret['categories'] = $this->categories();
1047
		$ret['category'] = $this->category();
1048
		$ret['children'] = $this->children();
1049
		$ret['comments'] = $this->comments();
1050
		$ret['content'] = $this->content();
1051
		$ret['edit_link'] = $this->edit_link();
1052
		$ret['format'] = $this->format();
1053
		$ret['link'] = $this->link();
1054
		$ret['next'] = $this->next();
1055
		$ret['pagination'] = $this->pagination();
1056
		$ret['parent'] = $this->parent();
1057
		$ret['path'] = $this->path();
1058
		$ret['prev'] = $this->prev();
1059
		$ret['terms'] = $this->terms();
1060
		$ret['tags'] = $this->tags();
1061
		$ret['thumbnail'] = $this->thumbnail();
1062
		$ret['title'] = $this->title();
1063
		return $ret;
1064
	}
1065
1066
	/**
1067
	 * Return the author of a post
1068
	 * @api
1069
	 * @example
1070
	 * ```twig
1071
	 * <h1>{{post.title}}</h1>
1072
	 * <p class="byline">
1073
	 *     <a href="{{post.author.link}}">{{post.author.name}}</a>
1074
	 * </p>
1075
	 * ```
1076
	 * @return TimberUser|bool A TimberUser object if found, false if not
0 ignored issues
show
Documentation introduced by
Should the return type not be TimberUser|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
1077
	 */
1078
	public function author() {
1079
		return $this->get_author();
1080
	}
1081
1082
	/**
1083
	 * Get the author (WordPress user) who last modified the post
1084
	 * @example
1085
	 * ```twig
1086
	 * Last updated by {{ post.modified_author.name }}
1087
	 * ```
1088
	 * ```html
1089
	 * Last updated by Harper Lee
1090
	 * ```
1091
	 * @return TimberUser|bool A TimberUser object if found, false if not
0 ignored issues
show
Documentation introduced by
Should the return type not be TimberUser|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
1092
	 */
1093
	public function modified_author() {
1094
		return $this->get_modified_author();
1095
	}
1096
1097
	/**
1098
	 * Get the categoires on a particular post
1099
	 * @api
1100
	 * @return array of TimberTerms
1101
	 */
1102
	public function categories() {
1103
		return $this->get_terms('category');
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
1104
	}
1105
1106
	/**
1107
	 * Returns a category attached to a post
1108
	 * @api
1109
	 * If mulitpuile categories are set, it will return just the first one
1110
	 * @return TimberTerm|null
1111
	 */
1112
	public function category() {
1113
		return $this->get_category();
1114
	}
1115
1116
	/**
1117
	 * Returns an array of children on the post as TimberPosts
1118
	 * (or other claass as you define).
1119
	 * @api
1120
	 * @example
1121
	 * ```twig
1122
	 * {% if post.children %}
1123
	 *     Here are the child pages:
1124
	 *     {% for child in page.children %}
1125
	 *         <a href="{{ child.link }}">{{ child.title }}</a>
1126
	 *     {% endfor %}
1127
	 * {% endif %}
1128
	 * ```
1129
	 * @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
1130
	 * @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.
1131
	 * @return array
1132
	 */
1133
	public function children( $post_type = 'any', $childPostClass = false ) {
1134
		return $this->get_children( $post_type, $childPostClass );
1135
	}
1136
1137
	/**
1138
	 * Gets the comments on a TimberPost and returns them as an array of [TimberComments](#TimberComment) (or whatever comment class you set).
1139
	 * @api
1140
	 * @param int $count Set the number of comments you want to get. `0` is analogous to "all"
1141
	 * @param string $order use ordering set in WordPress admin, or a different scheme
1142
	 * @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
1143
	 * @param string $status Could be 'pending', etc.
1144
	 * @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)
1145
	 * @example
1146
	 * ```twig
1147
	 * {# single.twig #}
1148
	 * <h4>Comments:</h4>
1149
	 * {% for comment in post.comments %}
1150
	 * 	<div class="comment-{{comment.ID}} comment-order-{{loop.index}}">
1151
	 * 		<p>{{comment.author.name}} said:</p>
1152
	 * 		<p>{{comment.content}}</p>
1153
	 * 	</div>
1154
	 * {% endfor %}
1155
	 * ```
1156
	 * @return bool|array
1157
	 */
1158
	public function comments( $count = 0, $order = 'wp', $type = 'comment', $status = 'approve', $CommentClass = 'TimberComment' ) {
1159
		return $this->get_comments($count, $order, $type, $status, $CommentClass);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
1160
	}
1161
1162
	/**
1163
	 * 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).
1164
	 * @api
1165
	 * @example
1166
	 * ```twig
1167
	 * <div class="article">
1168
	 *     <h2>{{post.title}}</h2>
1169
	 *     <div class="content">{{ post.content }}</div>
1170
	 * </div>
1171
	 * ```
1172
	 * @param int $page
1173
	 * @return string
1174
	 */
1175
	public function content( $page = 0 ) {
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
1176
		return $this->get_content(0, $page);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
1177
	}
1178
1179
	/**
1180
	 * @return string
1181
	 */
1182
	public function paged_content() {
1183
		return $this->get_paged_content();
1184
	}
1185
1186
	/**
1187
	 * Get the date to use in your template!
1188
	 * @api
1189
	 * @example
1190
	 * ```twig
1191
	 * Published on {{ post.date }} // Uses WP's formatting set in Admin
1192
	 * OR
1193
	 * Published on {{ post.date | date('F jS') }} // Jan 12th
1194
	 * ```
1195
	 *
1196
	 * ```html
1197
	 * Published on January 12, 2015
1198
	 * OR
1199
	 * Published on Jan 12th
1200
	 * ```
1201
	 * @param string $date_format
1202
	 * @return string
1203
	 */
1204
	public function date( $date_format = '' ) {
1205
		return $this->get_date($date_format);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
1206
	}
1207
1208
	/**
1209
	 * Get the time to use in your template
1210
	 * @api
1211
	 * @example
1212
	 * ```twig
1213
	 * Published at {{ post.time }} // Uses WP's formatting set in Admin
1214
	 * OR
1215
	 * Published at {{ post.time | time('G:i') }} // 13:25
1216
	 * ```
1217
	 *
1218
	 * ```html
1219
	 * Published at 1:25 pm
1220
	 * OR
1221
	 * Published at 13:25
1222
	 * ```
1223
	 * @param string $time_format
1224
	 * @return string
1225
	 */
1226
	public function time( $time_format = '' ) {
1227
		$tf = $time_format ? $time_format : get_option('time_format');
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
1228
	 	$the_time = (string)mysql2date($tf, $this->post_date);
0 ignored issues
show
introduced by
No space after closing casting parenthesis is prohibited
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
1229
	 	return apply_filters('get_the_time', $the_time, $tf);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
1230
	}
1231
1232
	/**
1233
	 * @return bool|string
1234
	 */
1235
	public function edit_link() {
1236
		return $this->get_edit_url();
1237
	}
1238
1239
	/**
1240
	 * @api
1241
	 * @return mixed
1242
	 */
1243
	public function format() {
1244
		return $this->get_format();
1245
	}
1246
1247
	/**
1248
	 * get the permalink for a post object
1249
	 * @api
1250
	 * @example
1251
	 * ```twig
1252
	 * <a href="{{post.link}}">Read my post</a>
1253
	 * ```
1254
	 * @return string ex: http://example.org/2015/07/my-awesome-post
1255
	 */
1256
	public function link() {
1257
		return $this->get_permalink();
1258
	}
1259
1260
	/**
1261
	 * @param string $field_name
0 ignored issues
show
Documentation introduced by
Should the type for parameter $field_name not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
1262
	 * @return mixed
1263
	 */
1264
	public function meta( $field_name = null ) {
1265
		if ( $field_name === null ) {
1266
			//on the off-chance the field is actually named meta
1267
			$field_name = 'meta';
1268
		}
1269
		return $this->get_field($field_name);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
1270
	}
1271
1272
	/**
1273
	 * @return string
1274
	 */
1275
	public function name(){
1276
		return $this->title();
1277
	}
1278
1279
	/**
1280
	 * @param string $date_format
1281
	 * @return string
1282
	 */
1283
	public function modified_date( $date_format = '' ) {
1284
		return $this->get_modified_date($date_format);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
1285
	}
1286
1287
	/**
1288
	 * @param string $time_format
1289
	 * @return string
1290
	 */
1291
	public function modified_time( $time_format = '' ) {
1292
		return $this->get_modified_time($time_format);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
1293
	}
1294
1295
	/**
1296
	 * @api
1297
	 * @param bool $in_same_cat
1298
	 * @return mixed
1299
	 */
1300
	public function next( $in_same_cat = false ) {
1301
		return $this->get_next($in_same_cat);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
1302
	}
1303
1304
	/**
1305
	 * @return array
1306
	 */
1307
	public function pagination() {
1308
		return $this->get_pagination();
1309
	}
1310
1311
	/**
1312
	 * Gets the parent (if one exists) from a post as a TimberPost object (or whatever is set in TimberPost::$PostClass)
1313
	 * @api
1314
	 * @example
1315
	 * ```twig
1316
	 * Parent page: <a href="{{ post.parent.link }}">{{ post.parent.title }}</a>
1317
	 * ```
1318
	 * @return bool|TimberPost
0 ignored issues
show
Documentation introduced by
Should the return type not be false|object?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
1319
	 */
1320
	public function parent() {
1321
		return $this->get_parent();
1322
	}
1323
1324
	/**
1325
	 * Gets the relative path of a WP Post, so while link() will return http://example.org/2015/07/my-cool-post
1326
	 * this will return just /2015/07/my-cool-post
1327
	 * @api
1328
	 * @example
1329
	 * ```twig
1330
	 * <a href="{{post.path}}">{{post.title}}</a>
1331
	 * ```
1332
	 * @return string
1333
	 */
1334
	public function path() {
1335
		return $this->get_path();
1336
	}
1337
1338
	/**
1339
	 * @deprecated 0.20.0 use link() instead
1340
	 * @return string
1341
	 */
1342
	public function permalink() {
1343
		return $this->get_permalink();
1344
	}
1345
1346
	/**
1347
	 * Get the previous post in a set
1348
	 * @api
1349
	 * @example
1350
	 * ```twig
1351
	 * <h4>Prior Entry:</h4>
1352
	 * <h3>{{post.prev.title}}</h3>
1353
	 * <p>{{post.prev.get_preview(25)}}</p>
1354
	 * ```
1355
	 * @param bool $in_same_cat
1356
	 * @return mixed
1357
	 */
1358
	public function prev( $in_same_cat = false ) {
1359
		return $this->get_prev($in_same_cat);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
1360
	}
1361
1362
	/**
1363
	 * Get the terms associated with the post
1364
	 * This goes across all taxonomies by default
1365
	 * @api
1366
	 * @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.
1367
	 * @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)?
1368
	 * @return array
1369
	 */
1370
	public function terms( $tax = '', $merge = true ) {
1371
		return $this->get_terms($tax, $merge);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
1372
	}
1373
1374
	/**
1375
	 * Gets the tags on a post, uses WP's post_tag taxonomy
1376
	 * @api
1377
	 * @return array
1378
	 */
1379
	public function tags() {
1380
		return $this->get_tags();
1381
	}
1382
1383
	/**
1384
	 * get the featured image as a TimberImage
1385
	 * @api
1386
	 * @example
1387
	 * ```twig
1388
	 * <img src="{{post.thumbnail.src}}" />
1389
	 * ```
1390
	 * @return TimberImage|null of your thumbnail
0 ignored issues
show
Documentation introduced by
Should the return type not be object|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
1391
	 */
1392
	public function thumbnail() {
1393
		return $this->get_thumbnail();
1394
	}
1395
1396
	/**
1397
	 * 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.
1398
	 * @api
1399
	 * @example
1400
	 * ```twig
1401
	 * <h1>{{ post.title }}</h1>
1402
	 * ```
1403
	 * @return string
1404
	 */
1405
	public function title() {
1406
		return $this->get_title();
1407
	}
1408
1409
}
1410