Completed
Pull Request — master (#876)
by
unknown
03:16
created

TimberPost::get_post_preview_id()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 29
rs 8.439
cc 6
eloc 17
nc 18
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
	 * @api
98
	 * @var string $id the numeric WordPress id of a post
99
	 */
100
	public $id;
101
102
	/**
103
	 * @var string 	$ID 			the numeric WordPress id of a post, capitalized to match WP usage
104
	 */
105
	public $ID;
106
107
	/**
108
	 * @var int 	$post_author 	the numeric ID of the a post's author corresponding to the wp_user dtable
109
	 */
110
	public $post_author;
111
112
	/**
113
	 * @var string 	$post_content 	the raw text of a WP post as stored in the database
114
	 */
115
	public $post_content;
116
117
	/**
118
	 * @var string 	$post_date 		the raw date string as stored in the WP database, ex: 2014-07-05 18:01:39
119
	 */
120
	public $post_date;
121
122
	/**
123
	 * @var string 	$post_exceprt 	the raw text of a manual post exceprt as stored in the database
124
	 */
125
	public $post_excerpt;
126
127
	/**
128
	 * @var int 		$post_parent 	the numeric ID of a post's parent post
129
	 */
130
	public $post_parent;
131
132
	/**
133
	 * @api
134
	 * @var string 		$post_status 	the status of a post ("draft", "publish", etc.)
135
	 */
136
	public $post_status;
137
138
	/**
139
	 * @var string 	$post_title 	the raw text of a post's title as stored in the database
140
	 */
141
	public $post_title;
142
143
	/**
144
	 * @api
145
	 * @var string 	$post_type 		the name of the post type, this is the machine name (so "my_custom_post_type" as opposed to "My Custom Post Type")
146
	 */
147
	public $post_type;
148
149
	/**
150
	 * @api
151
	 * @var string 	$slug 		the URL-safe slug, this corresponds to the poorly-named "post_name" in the WP database, ex: "hello-world"
152
	 */
153
	public $slug;
154
155
	/**
156
	 * If you send the constructor nothing it will try to figure out the current post id based on being inside The_Loop
157
	 * @example
158
	 * ```php
159
	 * $post = new TimberPost();
160
	 * $other_post = new TimberPost($random_post_id);
161
	 * ```
162
	 * @param mixed $pid
163
	 */
164
	public function __construct($pid = null) {
165
		$pid = $this->determine_id( $pid );
166
		$this->init($pid);
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...
167
	}
168
169
	/**
170
	 * tries to figure out what post you want to get if not explictly defined (or if it is, allows it to be passed through)
171
	 * @internal
172
	 * @param mixed a value to test against
173
	 * @return int the numberic id we should be using for this post object
174
	 */
175
	protected function determine_id($pid) {
176
		global $wp_query;
177
		if ( $pid === null &&
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
178
			isset($wp_query->queried_object_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...
179
			&& $wp_query->queried_object_id
180
			&& isset($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...
181
			&& 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...
182
			&& 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...
183
			) {
184
				if( isset( $_GET['preview'] ) && isset( $_GET['preview_nonce'] ) && wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . $wp_query->queried_object_id ) ) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 3 tabs, found 4
Loading history...
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
185
					$pid = $this->get_post_preview_id( $wp_query );
186
				} else if ( !$pid ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
187
					$pid = $wp_query->queried_object_id;
188
				}
189
		} else if ( $pid === null && $wp_query->is_home && isset($wp_query->queried_object_id) && $wp_query->queried_object_id )  {
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...
190
			//hack for static page as home page
191
			$pid = $wp_query->queried_object_id;
192
		} else if ( $pid === null ) {
193
			$gtid = false;
194
			$maybe_post = get_post();
195
			if ( isset($maybe_post->ID) ){
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...
196
				$gtid = true;
197
			}
198
			if ( $gtid ) {
199
				$pid = get_the_ID();
200
			}
201
			if ( !$pid ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
202
				global $wp_query;
203
				if ( isset($wp_query->query['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...
204
					$pid = $wp_query->query['p'];
205
				}
206
			}
207
		}
208
		if ( $pid === null && ($pid_from_loop = TimberPostGetter::loop_to_id()) ) {
209
			$pid = $pid_from_loop;
210
		}
211
		return $pid;
212
	}
213
214
	/**
215
	 * Outputs the title of the post if you do something like `<h1>{{post}}</h1>`
216
	 * @return string
217
	 */
218
	public function __toString() {
219
		return $this->title();
220
	}
221
222
	protected function get_post_preview_id( $query ) {
223
		$can = array(
224
	 		'edit_' . $query->queried_object->post_type . 's',
225
	 	);
226
227
	 	if ( $query->queried_object->author_id !== get_current_user_id() ) {
228
	 		$can[] = 'edit_others_' . $query->queried_object->post_type . 's';
229
	 	}
230
231
	 	$continue = true;
232
233
	 	foreach( $can as $type ) {
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...
234
	 		if( !current_user_can( $type ) ) {
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...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
235
	 			$continue = false;
236
	 			break;
237
	 		}
238
	 	}
239
		
240
		if( $continue ) {
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...
241
			$revisions = wp_get_post_revisions( $query->queried_object_id );
242
243
			if( !empty( $revisions ) ) {
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...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
244
				$last = end($revisions);
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...
245
				return $last->ID;
246
			} else {
247
				return false;
248
			}
249
		}
250
	}
251
252
	/**
253
	 * Initializes a TimberPost
254
	 * @internal
255
	 * @param int|bool $pid
256
	 */
257
	protected function init($pid = false) {
258
		if ( $pid === false ) {
0 ignored issues
show
introduced by
Found "=== false". Use Yoda Condition checks, you must
Loading history...
259
			$pid = get_the_ID();
260
		}
261
		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...
262
			$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...
263
		}
264
		$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...
265
		$this->import($post_info);
0 ignored issues
show
Bug introduced by
It seems like $post_info defined by $this->get_info($pid) on line 264 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...
266
		//cant have a function, so gots to do it this way
267
		$post_class = $this->post_class();
268
		$this->class = $post_class;
269
	}
270
271
	/**
272
	 * Get the URL that will edit the current post/object
273
	 * @internal
274
	 * @see TimberPost::edit_link
275
	 * @return bool|string
276
	 */
277
	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...
278
		if ( $this->can_edit() ) {
279
			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...
280
		}
281
	}
282
283
	/**
284
	 * updates the post_meta of the current object with the given value
285
	 * @param string $field
286
	 * @param mixed $value
287
	 */
288
	public function update( $field, $value ) {
289
		if ( isset($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...
290
			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...
291
			$this->$field = $value;
292
		}
293
	}
294
295
296
	/**
297
	 * takes a mix of integer (post ID), string (post slug),
298
	 * or object to return a WordPress post object from WP's built-in get_post() function
299
	 * @internal
300
	 * @param mixed $pid
301
	 * @return WP_Post on success
302
	 */
303
	protected function prepare_post_info( $pid = 0 ) {
304
		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...
305
			$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...
306
			$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...
307
			if ( $post ) {
308
				return $post;
309
			}
310
		}
311
		//we can skip if already is WP_Post
312
		return $pid;
313
	}
314
315
316
	/**
317
	 * helps you find the post id regardless of whether you send a string or whatever
318
	 * @param integer $pid ;
319
	 * @internal
320
	 * @return integer ID number of a post
321
	 */
322
	protected function check_post_id( $pid ) {
323
		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...
324
			$pid = get_the_ID();
325
			return $pid;
326
		}
327
		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...
328
			$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...
329
			return $pid;
330
		}
331
		if ( !$pid ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
332
			return null;
333
		}
334
		return $pid;
335
	}
336
337
338
	/**
339
	 * get_post_id_by_name($post_name)
340
	 * @internal
341
	 * @param string $post_name
342
	 * @return int
343
	 */
344
	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...
345
		global $wpdb;
346
		$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...
347
		$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...
348
		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...
349
			return null;
350
		}
351
		return $result->ID;
352
	}
353
354
	/**
355
	 * get a preview of your post, if you have an excerpt it will use that,
356
	 * otherwise it will pull from the post_content.
357
	 * If there's a <!-- more --> tag it will use that to mark where to pull through.
358
	 * @api
359
	 * @example
360
	 * ```twig
361
	 * <p>{{post.get_preview(50)}}</p>
362
	 * ```
363
	 * @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
364
	 * @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
365
	 * @param string $readmore The text you want to use on the 'readmore' link
366
	 * @param bool $strip Strip tags? yes or no. tell me!
367
	 * @param string $end The text to end the preview with (defaults to ...)
368
	 * @return string of the post preview
369
	 */
370
	function get_preview($len = 50, $force = false, $readmore = 'Read More', $strip = true, $end = '&hellip;') {
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...
371
		$text = '';
372
		$trimmed = false;
373
		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...
374
			if ( $force ) {
375
				$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...
376
				$trimmed = true;
377
			} else {
378
				$text = $this->post_excerpt;
379
			}
380
		}
381
		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...
382
			$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...
383
			$text = $pieces[0];
384
			if ( $force ) {
385
				$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...
386
				$trimmed = true;
387
			}
388
			$text = do_shortcode( $text );
389
		}
390
		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...
391
			$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...
392
			$trimmed = true;
393
		}
394
		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...
395
			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...
396
		}
397
		if ( $strip ) {
398
			$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...
399
		}
400
		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...
401
			$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...
402
			$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...
403
			if ( $last != '.' && $trimmed ) {
404
				$text .= $end;
405
			}
406
			if ( !$strip ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
407
				$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...
408
				if ( $last_p_tag !== false ) {
0 ignored issues
show
introduced by
Found "!== false". Use Yoda Condition checks, you must
Loading history...
409
					$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...
410
				}
411
				if ( $last != '.' && $trimmed ) {
412
					$text .= $end . ' ';
413
				}
414
			}
415
			$read_more_class = apply_filters('timber/post/get_preview/read_more_class', "read-more");
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...
Coding Style Comprehensibility introduced by
The string literal read-more does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
416
			if ( $readmore && isset($readmore_matches) && !empty($readmore_matches[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...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
417
				$text .= ' <a href="' . $this->get_permalink() . '" class="'.$read_more_class .'">' . 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...
418
			} elseif ( $readmore ) {
419
				$text .= ' <a href="' . $this->get_permalink() . '" class="'.$read_more_class .'">' . 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...
420
			}
421
			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...
422
				$text .= '</p>';
423
			}
424
		}
425
		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...
426
	}
427
428
	/**
429
	 * gets the post custom and attaches it to the current object
430
	 * @internal
431
	 * @param bool|int $pid a post ID number
432
	 */
433
	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...
434
		if ( !$pid ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
435
			$pid = $this->ID;
436
		}
437
		$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...
438
		$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...
439
	}
440
441
	/**
442
	 * Used internally to fetch the metadata fields (wp_postmeta table)
443
	 * and attach them to our TimberPost object
444
	 * @internal
445
	 * @param int $pid
446
	 * @return array
447
	 */
448
	protected function get_post_custom( $pid ) {
449
		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...
450
		$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...
451
		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...
452
			return array();
453
		}
454 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...
455
			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...
456
				$value = $value[0];
457
			}
458
			$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...
459
		}
460
		$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...
461
		return $customs;
462
	}
463
464
	/**
465
	 * @internal
466
	 * @see TimberPost::thumbnail
467
	 * @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...
468
	 */
469
	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...
470
		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...
471
			$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...
472
			if ( $tid ) {
473
				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...
474
			}
475
		}
476
	}
477
478
	/**
479
	 * @internal
480
	 * @see TimberPost::link
481
	 * @return string
482
	 */
483
	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...
484
		if ( isset($this->_permalink) ) {
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...
485
			return $this->_permalink;
486
		}
487
		$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...
488
		return $this->_permalink;
489
	}
490
491
	/**
492
	 * get the permalink for a post object
493
	 * In your templates you should use link:
494
	 * <a href="{{post.link}}">Read my post</a>
495
	 * @internal
496
	 * @return string
497
	 */
498
	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...
499
		return $this->get_permalink();
500
	}
501
502
	/**
503
	 * Get the next post in WordPress's ordering
504
	 * @internal
505
	 * @param bool $taxonomy
506
	 * @return TimberPost|boolean
507
	 */
508
	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...
509
		if ( !isset($this->_next) || !isset($this->_next[$taxonomy]) ) {
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...
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
510
			global $post;
511
			$this->_next = array();
512
			$old_global = $post;
513
			$post = $this;
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
514
			if ( $taxonomy ) {
515
				$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...
516
			} else {
517
				$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...
518
			}
519
520
			if ( $adjacent ) {
521
				$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...
522
			} else {
523
				$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...
524
			}
525
			$post = $old_global;
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
526
		}
527
		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...
528
	}
529
530
	/**
531
	 * Get a data array of pagination so you can navigate to the previous/next for a paginated post
532
	 * @return array
533
	 */
534
	public function get_pagination() {
535
		global $post, $page, $numpages, $multipage;
536
		$post = $this;
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
537
		$ret = array();
538
		if ( $multipage ) {
539
			for ( $i = 1; $i <= $numpages; $i++ ) {
540
				$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...
541
				$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...
542
				if ( $i == $page ) {
543
					$data['current'] = true;
544
				}
545
				$ret['pages'][] = $data;
546
			}
547
			$i = $page - 1;
548
			if ( $i ) {
549
				$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...
550
				$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...
551
			}
552
			$i = $page + 1;
553
			if ( $i <= $numpages ) {
554
				$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...
555
				$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...
556
			}
557
		}
558
		return $ret;
559
	}
560
561
	/**
562
	 * @param int $i
563
	 * @return string
564
	 */
565
	protected static function get_wp_link_page($i) {
566
		$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...
567
		$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...
568
		if ( isset($link['href']) ) {
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...
569
			return $link['href'];
570
		}
571
		return '';
572
	}
573
574
	/**
575
	 * Get the permalink for a post, but as a relative path
576
	 * For example, where {{post.link}} would return "http://example.org/2015/07/04/my-cool-post"
577
	 * this will return the relative version: "/2015/07/04/my-cool-post"
578
	 * @internal
579
	 * @return string
580
	 */
581
	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...
582
		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...
583
	}
584
585
	/**
586
	 * Get the next post in WordPress's ordering
587
	 * @internal
588
	 * @param bool $taxonomy
589
	 * @return TimberPost|boolean
590
	 */
591
	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...
592
		if ( isset($this->_prev) && isset($this->_prev[$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
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
593
			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...
594
		}
595
		global $post;
596
		$old_global = $post;
597
		$post = $this;
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
598
		$within_taxonomy = ($taxonomy) ? $taxonomy : 'category';
599
		$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...
600
		$prev_in_taxonomy = false;
601
		if ( $adjacent ) {
602
			$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...
603
		}
604
		$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...
605
		$post = $old_global;
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
606
		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...
607
	}
608
609
	/**
610
	 * Get the parent post of the post
611
	 * @internal
612
	 * @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...
613
	 */
614
	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...
615
		if ( !$this->post_parent ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
616
			return false;
617
		}
618
		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...
619
	}
620
621
	/**
622
	 * Gets a User object from the author of the post
623
	 * @internal
624
	 * @see TimberPost::author
625
	 * @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...
626
	 */
627
	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...
628
		if ( isset($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...
629
			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...
630
		}
631
	}
632
633
	/**
634
	 * @internal
635
	 * @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...
636
	 */
637
	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...
638
		$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...
639
		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...
640
	}
641
642
	/**
643
	 * Used internally by init, etc. to build TimberPost object
644
	 * @internal
645
	 * @param  int $pid
646
	 * @return null|object|WP_Post
647
	 */
648
	protected function get_info($pid) {
649
		$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...
650
		if ( !isset($post->post_status) ) {
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...
651
			return null;
652
		}
653
		$post->status = $post->post_status;
654
		$post->id = $post->ID;
655
		$post->slug = $post->post_name;
656
		$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...
657
		$post->custom = $customs;
658
		$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...
659
		return $post;
660
	}
661
662
	/**
663
	 * @internal
664
	 * @see TimberPost::date
665
	 * @param  string $date_format
666
	 * @return string
667
	 */
668
	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...
669
		$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...
670
		$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...
671
		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...
672
	}
673
674
	/**
675
	 * @internal
676
	 * @param  string $date_format
677
	 * @return string
678
	 */
679
	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...
680
		$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...
681
		$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...
682
		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...
683
	}
684
685
	/**
686
	 * @internal
687
	 * @param  string $time_format
688
	 * @return string
689
	 */
690
	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...
691
		$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...
692
		$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...
693
		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...
694
	}
695
696
	/**
697
	 * @internal
698
	 * @see TimberPost::children
699
	 * @param string 		$post_type
700
	 * @param bool|string 	$childPostClass
701
	 * @return array
702
	 */
703
	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...
704
		if ( $childPostClass === false ) {
0 ignored issues
show
introduced by
Found "=== false". Use Yoda Condition checks, you must
Loading history...
705
			$childPostClass = $this->PostClass;
706
		}
707
		if ( $post_type == 'parent' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
708
			$post_type = $this->post_type;
709
		}
710
		$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...
711
		foreach ( $children as &$child ) {
712
			$child = new $childPostClass($child->ID);
713
		}
714
		$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...
715
		return $children;
716
	}
717
718
719
	/**
720
	 * Get the comments for a post
721
	 * @internal
722
	 * @see TimberPost::comments
723
	 * @param int $ct
724
	 * @param string $order
725
	 * @param string $type
726
	 * @param string $status
727
	 * @param string $CommentClass
728
	 * @return array|mixed
729
	 */
730
731
	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...
732
733
		global $overridden_cpage, $user_ID;
734
		$overridden_cpage = false;
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
735
736
		$commenter = wp_get_current_commenter();
737
		$comment_author_email = $commenter['comment_author_email'];
738
739
		$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...
740
		if ( $ct > 0 ) {
741
			$args['number'] = $ct;
742
		}
743
		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...
744
			$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...
745
		}
746
747
		if ( $user_ID ) {
748
			$args['include_unapproved'] = array( $user_ID );
749
		} elseif ( ! empty( $comment_author_email ) ) {
750
			$args['include_unapproved'] = array( $comment_author_email );
751
		}
752
753
		$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...
754
		$timber_comments = array();
755
756
		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...
757
			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...
758
			$overridden_cpage = true;
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
759
		}
760
761
		foreach($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...
introduced by
No space after opening parenthesis is prohibited
Loading history...
introduced by
No space before closing parenthesis is prohibited
Loading history...
762
			$timber_comment = new $CommentClass($comment);
763
			$timber_comments[$timber_comment->id] = $timber_comment;
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
764
		}
765
766
		// Build a flattened (depth=1) comment tree
767
		$comments_tree = array();
768
		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...
769
			if ( ! $comment->is_child() ) {
770
				continue;
771
			}
772
773
			$tree_element = $comment;
774
			do {
775
				$tree_element = $timber_comments[$tree_element->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...
776
			} while( $tree_element->is_child() );
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
777
778
			$comments_tree[$tree_element->id][] = $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...
779
		}
780
781
		// Add child comments to the relative "super parents"
782
		foreach($comments_tree as $comment_parent => $comment_children) {
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...
introduced by
No space after opening parenthesis is prohibited
Loading history...
introduced by
No space before closing parenthesis is prohibited
Loading history...
783
			foreach($comment_children as $comment_child) {
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...
introduced by
No space after opening parenthesis is prohibited
Loading history...
introduced by
No space before closing parenthesis is prohibited
Loading history...
784
				$timber_comments[$comment_parent]->children[] = $timber_comments[$comment_child];
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
785
				unset($timber_comments[$comment_child]);
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
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
786
			}
787
		}
788
789
		$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...
790
791
		return $timber_comments;
792
	}
793
794
	/**
795
	 * Get the categories for a post
796
	 * @internal
797
	 * @see TimberPost::categories
798
	 * @return array of TimberTerms
799
	 */
800
	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...
801
		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...
802
	}
803
804
	/**
805
	 * @internal
806
	 * @see TimberPost::category
807
	 * @return mixed
808
	 */
809
	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...
810
		$cats = $this->get_categories();
811
		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...
812
			return $cats[0];
813
		}
814
	}
815
816
	/**
817
	 * @internal
818
	 * @param string|array $tax
819
	 * @param bool $merge
820
	 * @param string $TermClass
821
	 * @return array
822
	 */
823
	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...
824
825
		$TermClass = $TermClass ?: $this->TermClass;
826
827
		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...
828
			$TermClass = $merge;
829
		}
830
		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...
831
			$taxonomies = $tax;
832
		}
833
		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...
834
			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...
835
				$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...
836
			} else {
837
				$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...
838
			}
839
		}
840
841
		$term_class_objects = array();
842
843
		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...
844
			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...
845
				$taxonomy = 'post_tag';
846
			}
847
			if ( $taxonomy == 'categories' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
848
				$taxonomy = 'category';
849
			}
850
851
			$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...
852
853
			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...
854
				/* @var $terms WP_Error */
855
				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...
856
				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...
857
				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...
858
859
				return $term_class_objects;
860
			}
861
862
			// map over array of wordpress terms, and transform them into instances of the TermClass
863
			$terms = array_map(function($term) use ($TermClass, $taxonomy) {
864
				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...
865
			}, $terms);
866
867
			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...
868
				$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...
869
			} 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...
870
				$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...
871
			}
872
		}
873
		return $term_class_objects;
874
	}
875
876
	/**
877
	 * @param string|int $term_name_or_id
878
	 * @param string $taxonomy
879
	 * @return bool
880
	 */
881
	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...
882
		if ( $taxonomy == 'all' || $taxonomy == 'any' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
883
			$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...
884
			$ret = false;
885
			foreach ( $taxes as $tax ) {
886
				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...
887
					$ret = true;
888
					break;
889
				}
890
			}
891
			return $ret;
892
		}
893
		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...
894
	}
895
896
	/**
897
	 * @param string $field
898
	 * @return TimberImage
899
	 */
900
	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...
901
		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...
902
	}
903
904
	/**
905
	 * Gets an array of tags for you to use
906
	 * @internal
907
	 * @example
908
	 * ```twig
909
	 * <ul class="tags">
910
	 *     {% for tag in post.tags %}
911
	 *         <li>{{tag.name}}</li>
912
	 *     {% endfor %}
913
	 * </ul>
914
	 * ```
915
	 * @return array
916
	 */
917
	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...
918
		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...
919
	}
920
921
	/**
922
	 * Outputs the title with filters applied
923
	 * @internal
924
	 * @example
925
	 * ```twig
926
	 * <h1>{{post.get_title}}</h1>
927
	 * ```
928
	 * ```html
929
	 * <h1>Hello World!</h1>
930
	 * ```
931
	 * @return string
932
	 */
933
	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...
934
		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...
935
	}
936
937
	/**
938
	 * Displays the content of the post with filters, shortcodes and wpautop applied
939
	 * @example
940
	 * ```twig
941
	 * <div class="article-text">{{post.get_content}}</div>
942
	 * ```
943
	 * ```html
944
	 * <div class="article-text"><p>Blah blah blah</p><p>More blah blah blah.</p></div>
945
	 * ```
946
	 * @param int $len
947
	 * @param int $page
948
	 * @return string
949
	 */
950
	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...
951
		if ( $len == 0 && $page == 0 && $this->_content ) {
0 ignored issues
show
introduced by
Found "== 0". Use Yoda Condition checks, you must
Loading history...
952
			return $this->_content;
953
		}
954
		$content = $this->post_content;
955
		if ( $len ) {
956
			$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...
957
		}
958
		if ( $page ) {
959
			$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...
960
			$page--;
961
			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...
962
				$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...
963
			}
964
		}
965
		$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...
966
		if ( $len == 0 && $page == 0 ) {
0 ignored issues
show
introduced by
Found "== 0". Use Yoda Condition checks, you must
Loading history...
967
			$this->_content = $content;
968
		}
969
		return $content;
970
	}
971
972
	/**
973
	 * @return string
974
	 */
975
	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...
976
		global $page;
977
		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...
978
	}
979
	/**
980
	 *
981
	 * Here is my summary
982
	 * @example
983
	 * ```twig
984
	 * This post is from <span>{{ post.get_post_type.labels.plural }}</span>
985
	 * ```
986
	 *
987
	 * ```html
988
	 * This post is from <span>Recipes</span>
989
	 * ```
990
	 * @return mixed
991
	 */
992
	public function get_post_type() {
993
		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...
994
	}
995
996
	/**
997
	 * @return int the number of comments on a post
998
	 */
999
	public function get_comment_count() {
1000
		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...
1001
	}
1002
1003
	/**
1004
	 * @param string $field_name
1005
	 * @return mixed
1006
	 */
1007
	public function get_field( $field_name ) {
1008
		$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...
1009
		if ( $value === null ) {
1010
			$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...
1011
			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...
1012
				$value = $value[0];
1013
			}
1014
			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...
1015
				$value = null;
1016
			}
1017
		}
1018
		$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...
1019
		return $value;
1020
	}
1021
1022
	/**
1023
	 * @param string $field_name
1024
	 */
1025
	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...
1026
		$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...
1027
	}
1028
1029
	/**
1030
	 * @internal
1031
	 * @return mixed
1032
	 */
1033
	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...
1034
		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...
1035
	}
1036
1037
	/**
1038
	 * Get the CSS classes for a post. For usage you should use `{{post.class}}` instead of `{{post.post_class}}`
1039
	 * @internal
1040
	 * @param string $class additional classes you want to add
1041
	 * @see TimberPost::$class
1042
	 * @example
1043
	 * ```twig
1044
	 * <article class="{{ post.class }}">
1045
	 *    {# Some stuff here #}
1046
	 * </article>
1047
	 * ```
1048
	 *
1049
	 * ```html
1050
	 * <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">
1051
	 *    {# Some stuff here #}
1052
	 * </article>
1053
	 * ```
1054
	 * @return string a space-seperated list of classes
1055
	 */
1056
	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...
1057
		global $post;
1058
		$old_global_post = $post;
1059
		$post = $this;
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
1060
		$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...
1061
		$post = $old_global_post;
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
1062
		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...
1063
			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...
1064
		}
1065
		return $class_array;
1066
	}
1067
1068
	// Docs
1069
1070
	/**
1071
	 * @return array
1072
	 * @codeCoverageIgnore
1073
	 */
1074
	public function get_method_values() {
1075
		$ret = parent::get_method_values();
1076
		$ret['author'] = $this->author();
1077
		$ret['categories'] = $this->categories();
1078
		$ret['category'] = $this->category();
1079
		$ret['children'] = $this->children();
1080
		$ret['comments'] = $this->comments();
1081
		$ret['content'] = $this->content();
1082
		$ret['edit_link'] = $this->edit_link();
1083
		$ret['format'] = $this->format();
1084
		$ret['link'] = $this->link();
1085
		$ret['next'] = $this->next();
1086
		$ret['pagination'] = $this->pagination();
1087
		$ret['parent'] = $this->parent();
1088
		$ret['path'] = $this->path();
1089
		$ret['prev'] = $this->prev();
1090
		$ret['terms'] = $this->terms();
1091
		$ret['tags'] = $this->tags();
1092
		$ret['thumbnail'] = $this->thumbnail();
1093
		$ret['title'] = $this->title();
1094
		return $ret;
1095
	}
1096
1097
	/**
1098
	 * Return the author of a post
1099
	 * @api
1100
	 * @example
1101
	 * ```twig
1102
	 * <h1>{{post.title}}</h1>
1103
	 * <p class="byline">
1104
	 *     <a href="{{post.author.link}}">{{post.author.name}}</a>
1105
	 * </p>
1106
	 * ```
1107
	 * @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...
1108
	 */
1109
	public function author() {
1110
		return $this->get_author();
1111
	}
1112
1113
	/**
1114
	 * Get the author (WordPress user) who last modified the post
1115
	 * @example
1116
	 * ```twig
1117
	 * Last updated by {{ post.modified_author.name }}
1118
	 * ```
1119
	 * ```html
1120
	 * Last updated by Harper Lee
1121
	 * ```
1122
	 * @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...
1123
	 */
1124
	public function modified_author() {
1125
		return $this->get_modified_author();
1126
	}
1127
1128
	/**
1129
	 * Get the categoires on a particular post
1130
	 * @api
1131
	 * @return array of TimberTerms
1132
	 */
1133
	public function categories() {
1134
		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...
1135
	}
1136
1137
	/**
1138
	 * Returns a category attached to a post
1139
	 * @api
1140
	 * If mulitpuile categories are set, it will return just the first one
1141
	 * @return TimberTerm|null
1142
	 */
1143
	public function category() {
1144
		return $this->get_category();
1145
	}
1146
1147
	/**
1148
	 * Returns an array of children on the post as TimberPosts
1149
	 * (or other claass as you define).
1150
	 * @api
1151
	 * @example
1152
	 * ```twig
1153
	 * {% if post.children %}
1154
	 *     Here are the child pages:
1155
	 *     {% for child in page.children %}
1156
	 *         <a href="{{ child.link }}">{{ child.title }}</a>
1157
	 *     {% endfor %}
1158
	 * {% endif %}
1159
	 * ```
1160
	 * @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
1161
	 * @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.
1162
	 * @return array
1163
	 */
1164
	public function children( $post_type = 'any', $childPostClass = false ) {
1165
		return $this->get_children( $post_type, $childPostClass );
1166
	}
1167
1168
	/**
1169
	 * Gets the comments on a TimberPost and returns them as an array of [TimberComments](#TimberComment) (or whatever comment class you set).
1170
	 * @api
1171
	 * @param int $count Set the number of comments you want to get. `0` is analogous to "all"
1172
	 * @param string $order use ordering set in WordPress admin, or a different scheme
1173
	 * @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
1174
	 * @param string $status Could be 'pending', etc.
1175
	 * @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)
1176
	 * @example
1177
	 * ```twig
1178
	 * {# single.twig #}
1179
	 * <h4>Comments:</h4>
1180
	 * {% for comment in post.comments %}
1181
	 * 	<div class="comment-{{comment.ID}} comment-order-{{loop.index}}">
1182
	 * 		<p>{{comment.author.name}} said:</p>
1183
	 * 		<p>{{comment.content}}</p>
1184
	 * 	</div>
1185
	 * {% endfor %}
1186
	 * ```
1187
	 * @return bool|array
1188
	 */
1189
	public function comments( $count = 0, $order = 'wp', $type = 'comment', $status = 'approve', $CommentClass = 'TimberComment' ) {
1190
		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...
1191
	}
1192
1193
	/**
1194
	 * 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).
1195
	 * @api
1196
	 * @example
1197
	 * ```twig
1198
	 * <div class="article">
1199
	 *     <h2>{{post.title}}</h2>
1200
	 *     <div class="content">{{ post.content }}</div>
1201
	 * </div>
1202
	 * ```
1203
	 * @param int $page
1204
	 * @return string
1205
	 */
1206
	public function content( $page = 0 ) {
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
1207
		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...
1208
	}
1209
1210
	/**
1211
	 * @return string
1212
	 */
1213
	public function paged_content() {
1214
		return $this->get_paged_content();
1215
	}
1216
1217
	/**
1218
	 * Get the date to use in your template!
1219
	 * @api
1220
	 * @example
1221
	 * ```twig
1222
	 * Published on {{ post.date }} // Uses WP's formatting set in Admin
1223
	 * OR
1224
	 * Published on {{ post.date | date('F jS') }} // Jan 12th
1225
	 * ```
1226
	 *
1227
	 * ```html
1228
	 * Published on January 12, 2015
1229
	 * OR
1230
	 * Published on Jan 12th
1231
	 * ```
1232
	 * @param string $date_format
1233
	 * @return string
1234
	 */
1235
	public function date( $date_format = '' ) {
1236
		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...
1237
	}
1238
1239
	/**
1240
	 * Get the time to use in your template
1241
	 * @api
1242
	 * @example
1243
	 * ```twig
1244
	 * Published at {{ post.time }} // Uses WP's formatting set in Admin
1245
	 * OR
1246
	 * Published at {{ post.time | time('G:i') }} // 13:25
1247
	 * ```
1248
	 *
1249
	 * ```html
1250
	 * Published at 1:25 pm
1251
	 * OR
1252
	 * Published at 13:25
1253
	 * ```
1254
	 * @param string $time_format
1255
	 * @return string
1256
	 */
1257
	public function time( $time_format = '' ) {
1258
		$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...
1259
	 	$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...
1260
	 	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...
1261
	}
1262
1263
	/**
1264
	 * @return bool|string
1265
	 */
1266
	public function edit_link() {
1267
		return $this->get_edit_url();
1268
	}
1269
1270
	/**
1271
	 * @api
1272
	 * @return mixed
1273
	 */
1274
	public function format() {
1275
		return $this->get_format();
1276
	}
1277
1278
	/**
1279
	 * get the permalink for a post object
1280
	 * @api
1281
	 * @example
1282
	 * ```twig
1283
	 * <a href="{{post.link}}">Read my post</a>
1284
	 * ```
1285
	 * @return string ex: http://example.org/2015/07/my-awesome-post
1286
	 */
1287
	public function link() {
1288
		return $this->get_permalink();
1289
	}
1290
1291
	/**
1292
	 * @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...
1293
	 * @return mixed
1294
	 */
1295
	public function meta( $field_name = null ) {
1296
		if ( $field_name === null ) {
1297
			//on the off-chance the field is actually named meta
1298
			$field_name = 'meta';
1299
		}
1300
		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...
1301
	}
1302
1303
	/**
1304
	 * @return string
1305
	 */
1306
	public function name(){
1307
		return $this->title();
1308
	}
1309
1310
	/**
1311
	 * @param string $date_format
1312
	 * @return string
1313
	 */
1314
	public function modified_date( $date_format = '' ) {
1315
		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...
1316
	}
1317
1318
	/**
1319
	 * @param string $time_format
1320
	 * @return string
1321
	 */
1322
	public function modified_time( $time_format = '' ) {
1323
		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...
1324
	}
1325
1326
	/**
1327
	 * @api
1328
	 * @param bool $in_same_cat
1329
	 * @return mixed
1330
	 */
1331
	public function next( $in_same_cat = false ) {
1332
		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...
1333
	}
1334
1335
	/**
1336
	 * @return array
1337
	 */
1338
	public function pagination() {
1339
		return $this->get_pagination();
1340
	}
1341
1342
	/**
1343
	 * Gets the parent (if one exists) from a post as a TimberPost object (or whatever is set in TimberPost::$PostClass)
1344
	 * @api
1345
	 * @example
1346
	 * ```twig
1347
	 * Parent page: <a href="{{ post.parent.link }}">{{ post.parent.title }}</a>
1348
	 * ```
1349
	 * @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...
1350
	 */
1351
	public function parent() {
1352
		return $this->get_parent();
1353
	}
1354
1355
	/**
1356
	 * Gets the relative path of a WP Post, so while link() will return http://example.org/2015/07/my-cool-post
1357
	 * this will return just /2015/07/my-cool-post
1358
	 * @api
1359
	 * @example
1360
	 * ```twig
1361
	 * <a href="{{post.path}}">{{post.title}}</a>
1362
	 * ```
1363
	 * @return string
1364
	 */
1365
	public function path() {
1366
		return $this->get_path();
1367
	}
1368
1369
	/**
1370
	 * @deprecated 0.20.0 use link() instead
1371
	 * @return string
1372
	 */
1373
	public function permalink() {
1374
		return $this->get_permalink();
1375
	}
1376
1377
	/**
1378
	 * Get the previous post in a set
1379
	 * @api
1380
	 * @example
1381
	 * ```twig
1382
	 * <h4>Prior Entry:</h4>
1383
	 * <h3>{{post.prev.title}}</h3>
1384
	 * <p>{{post.prev.get_preview(25)}}</p>
1385
	 * ```
1386
	 * @param bool $in_same_cat
1387
	 * @return mixed
1388
	 */
1389
	public function prev( $in_same_cat = false ) {
1390
		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...
1391
	}
1392
1393
	/**
1394
	 * Get the terms associated with the post
1395
	 * This goes across all taxonomies by default
1396
	 * @api
1397
	 * @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.
1398
	 * @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)?
1399
	 * @return array
1400
	 */
1401
	public function terms( $tax = '', $merge = true ) {
1402
		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...
1403
	}
1404
1405
	/**
1406
	 * Gets the tags on a post, uses WP's post_tag taxonomy
1407
	 * @api
1408
	 * @return array
1409
	 */
1410
	public function tags() {
1411
		return $this->get_tags();
1412
	}
1413
1414
	/**
1415
	 * get the featured image as a TimberImage
1416
	 * @api
1417
	 * @example
1418
	 * ```twig
1419
	 * <img src="{{post.thumbnail.src}}" />
1420
	 * ```
1421
	 * @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...
1422
	 */
1423
	public function thumbnail() {
1424
		return $this->get_thumbnail();
1425
	}
1426
1427
	/**
1428
	 * 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.
1429
	 * @api
1430
	 * @example
1431
	 * ```twig
1432
	 * <h1>{{ post.title }}</h1>
1433
	 * ```
1434
	 * @return string
1435
	 */
1436
	public function title() {
1437
		return $this->get_title();
1438
	}
1439
1440
}
1441