Completed
Push — master ( fefbc6...bc6754 )
by
unknown
9s
created

ClassyPost::get_first_attached_image_id()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 2
eloc 15
c 2
b 1
f 1
nc 2
nop 0
dl 0
loc 22
rs 9.2
1
<?php
2
3
/**
4
 * Wrapper for WP_Post.
5
 */
6
class ClassyPost extends ClassyBasis {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
8
	/**
9
	 * Current post id.
10
	 *
11
	 * @var int
12
	 */
13
	public $ID;
14
15
	/**
16
	 * Stores current post object.
17
	 *
18
	 * @var WP_Post
19
	 */
20
	protected $object;
21
22
	/**
23
	 * Post title.
24
	 *
25
	 * @var string
26
	 */
27
	public $post_title;
28
29
	/**
30
	 * Post name.
31
	 *
32
	 * @var string
33
	 */
34
	public $post_name;
35
36
	/**
37
	 * Post content (raw).
38
	 *
39
	 * @var string
40
	 */
41
	public $post_content;
42
43
	/**
44
	 * Post type.
45
	 *
46
	 * @var string
47
	 */
48
	public $post_type;
49
50
	/**
51
	 * Post author id.
52
	 *
53
	 * @var int
54
	 */
55
	public $post_author;
56
57
	/**
58
	 * Post date. String as stored in the WP database, ex: 2012-04-23 08:11:23.
59
	 *
60
	 * @var string
61
	 */
62
	public $post_date;
63
64
	/**
65
	 * Post excerpt (raw).
66
	 *
67
	 * @var string
68
	 */
69
	public $post_excerpt;
70
71
	/**
72
	 * Post status. It can be draft, publish, pending, private, trash, etc.
73
	 *
74
	 * @var string
75
	 */
76
	public $post_status;
77
78
	/**
79
	 * Post permalink.
80
	 *
81
	 * @var string
82
	 */
83
	public $permalink;
84
85
	/**
86
	 * Main constructor function. If ID won't be provided we will try to find it, based on your query.
87
	 *
88
	 * @param object|int $post WP_Post or WP_Post.ID.
89
	 */
90
	public function __construct( $post = null ) {
91
		if ( is_integer( $post ) ) {
92
			$this->ID = $post;
93
			$this->init();
94
		} elseif ( is_a( $post, 'WP_Post' ) ) {
95
			$this->import( $post );
0 ignored issues
show
Bug introduced by
It seems like $post defined by parameter $post on line 90 can also be of type null; however, ClassyBasis::import() does only seem to accept object|array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
96
		}
97
	}
98
99
	/**
100
	 * Initialises Instance based on provided post id.
101
	 */
102
	protected function init() {
103
		$post = $this->get_object();
104
105
		if ( is_a( $post, 'WP_Post' ) ) {
106
			$this->import( $post );
107
		}
108
	}
109
110
	/**
111
	 * Returns post object.
112
	 *
113
	 * @return WP_Post
114
	 */
115
	public function get_object() {
116
		$object = get_post( $this->ID );
117
118
		return $object;
119
	}
120
121
	/**
122
	 * Checks if current user can edit this post.
123
	 *
124
	 * @return boolean
125
	 */
126
	public function can_edit() {
127
		if ( ! function_exists( 'current_user_can' ) ) {
128
			return false;
129
		}
130
		if ( current_user_can( 'edit_post', $this->ID ) ) {
131
			return true;
132
		}
133
134
		return false;
135
	}
136
137
	/**
138
	 * Returns post edit url.
139
	 *
140
	 * @return string
141
	 */
142
	public function get_edit_url() {
143
		if ( $this->can_edit() ) {
144
			return get_edit_post_link( $this->ID );
145
		}
146
147
		return '';
148
	}
149
150
	/**
151
	 * Returns array of attached image ids.
152
	 *
153
	 * @return false|array of ids
154
	 */
155
	public function get_attached_images() {
156
		$attrs = array(
157
			'post_parent' => $this->ID,
158
			'post_status' => null,
159
			'post_type' => 'attachment',
160
			'post_mime_type' => 'image',
161
			'order' => 'ASC',
162
			'numberposts' => -1,
163
			'orderby' => 'menu_order',
164
			'fields' => 'ids',
165
		);
166
167
		$images = get_children( $attrs );
168
169
		if ( ! count( $images ) ) {
170
			return false;
171
		}
172
173
		return $images;
174
	}
175
176
	/**
177
	 * Returns array of attached images as ClassyImage objects.
178
	 *
179
	 * @return array of ClassyImage
180
	 */
181
	public function attached_images() {
182
		$_return = array();
183
184
		$images = $this->get_attached_images();
185
186
		if ( $images ) {
187
188
			foreach ( $images as $image_id ) {
189
190
				$_return[] = new ClassyImage( $image_id );
191
192
			}
193
		}
194
195
		return $_return;
196
	}
197
198
199
	/**
200
	 * Returns first attached image id.
201
	 *
202
	 * @return int|boolean
203
	 */
204
	public function get_first_attached_image_id() {
205
		$attrs = array(
206
			'post_parent' => $this->ID,
207
			'post_status' => null,
208
			'post_type' => 'attachment',
209
			'post_mime_type' => 'image',
210
			'order' => 'ASC',
211
			'numberposts' => 1,
212
			'orderby' => 'menu_order',
213
			'fields' => 'ids',
214
		);
215
216
		$images = get_children( $attrs );
217
218
		if ( ! count( $images ) ) {
219
			return false;
220
		}
221
222
		$images = array_values( $images );
223
224
		return $images[0];
225
	}
226
227
	/**
228
	 * Returns first attached image.
229
	 *
230
	 * @return ClassyImage
231
	 */
232
	public function first_attached_image() {
233
234
		$image_id = $this->get_first_attached_image_id();
235
236
		if ( $image_id ) {
237
			return new ClassyImage( $image_id );
0 ignored issues
show
Bug introduced by
It seems like $image_id defined by $this->get_first_attached_image_id() on line 234 can also be of type boolean; however, ClassyImage::__construct() does only seem to accept integer|null, 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...
238
		}
239
240
		return new ClassyImage();
241
	}
242
243
	/**
244
	 * Returns post thumbnail.
245
	 *
246
	 * @return ClassyImage
247
	 */
248
	public function thumbnail() {
249
		if ( function_exists( 'get_post_thumbnail_id' ) ) {
250
			$image_id = get_post_thumbnail_id( $this->ID );
251
252
			if ( $image_id ) {
253
254
				return new ClassyImage( $image_id );
255
256
			}
257
		}
258
259
		return new ClassyImage();
260
	}
261
262
	/**
263
	 * Returns post title with filters applied.
264
	 *
265
	 * @return string
266
	 */
267
	public function get_title() {
268
		return apply_filters( 'the_title', $this->post_title, $this->ID );
269
	}
270
271
	/**
272
	 * Alias for get_title.
273
	 *
274
	 * @return string
275
	 */
276
	public function title() {
277
		return $this->get_title();
278
	}
279
280
	/**
281
	 * Returns the post content with filters applied.
282
	 *
283
	 * @param  integer $page Page number, in case our post has <!--nextpage--> tags.
284
	 *
285
	 * @return string        Post content
286
	 */
287
	public function get_content( $page = 0 ) {
288
		if ( 0 === absint( $page ) && $this->post_content ) {
289
			return $this->post_content;
290
		}
291
292
		$content = $this->post_content;
293
294
		if ( $page ) {
295
			$contents = explode( '<!--nextpage-->', $content );
296
297
			$page--;
298
299
			if ( count( $contents ) > $page ) {
300
				$content = $contents[ $page ];
301
			}
302
		}
303
304
		$content = apply_filters( 'the_content', ($content) );
305
306
		return $content;
307
	}
308
309
	/**
310
	 * Alias for get_content.
311
	 *
312
	 * @return string
313
	 */
314
	public function content() {
315
		return $this->get_content();
316
	}
317
318
	/**
319
	 * Returns post type object for current post.
320
	 *
321
	 * @return object
322
	 */
323
	public function get_post_type() {
324
		return get_post_type_object( $this->post_type );
325
	}
326
327
	/**
328
	 * Returns post permalink.
329
	 *
330
	 * @return string
331
	 */
332
	public function get_permalink() {
333
		if ( isset( $this->permalink ) ) {
334
			return $this->permalink;
335
		}
336
337
		$this->permalink = get_permalink( $this->ID );
338
339
		return $this->permalink;
340
	}
341
342
	/**
343
	 * Alias for get_permalink
344
	 *
345
	 * @return string
346
	 */
347
	public function permalink() {
348
		return $this->get_permalink();
349
	}
350
351
	/**
352
	 * Returns post preview of requested length.
353
	 * It will look for post_excerpt and will return it.
354
	 * If post contains <!-- more --> tag it will return content until it
355
	 *
356
	 * @param  integer $len      Number of words.
357
	 * @param  boolean $force    If is set to true it will cut your post_excerpt to desired $len length.
358
	 * @param  string  $readmore The text for 'readmore' link.
359
	 * @param  boolean $strip    Should we strip tags.
360
	 *
361
	 * @return string            Post preview.
362
	 */
363
	public function get_preview( $len = 50, $force = false, $readmore = 'Read More', $strip = true ) {
364
		$text = '';
365
		$trimmed = false;
366
367
		if ( isset( $this->post_excerpt ) && strlen( $this->post_excerpt ) ) {
368
369
			if ( $force ) {
370
				$text = ClassyHelper::trim_words( $this->post_excerpt, $len, false );
371
				$trimmed = true;
372
			} else {
373
				$text = $this->post_excerpt;
374
			}
375
		}
376
377
		if ( ! strlen( $text ) && preg_match( '/<!--\s?more(.*?)?-->/', $this->post_content, $readmore_matches ) ) {
378
379
			$pieces = explode( $readmore_matches[0], $this->post_content );
380
			$text = $pieces[0];
381
382
			if ( $force ) {
383
				$text = ClassyHelper::trim_words( $text, $len, false );
384
				$trimmed = true;
385
			}
386
387
			$text = do_shortcode( $text );
388
389
		}
390
391
		if ( ! strlen( $text ) ) {
392
393
			$text = ClassyHelper::trim_words( $this->get_content(), $len, false );
394
			$trimmed = true;
395
396
		}
397
398
		if ( ! strlen( trim( $text ) ) ) {
399
400
			return trim( $text );
401
402
		}
403
404
		if ( $strip ) {
405
406
			$text = trim( strip_tags( $text ) );
407
408
		}
409
410
		if ( strlen( $text ) ) {
411
412
			$text = trim( $text );
413
			$last = $text[ strlen( $text ) - 1 ];
414
415
			if ( '.' !== $last && $trimmed ) {
416
				$text .= ' &hellip; ';
417
			}
418
419
			if ( ! $strip ) {
420
				$last_p_tag = strrpos( $text, '</p>' );
421
				if ( false !== $last_p_tag ) {
422
					$text = substr( $text, 0, $last_p_tag );
423
				}
424
				if ( '.' !== $last && $trimmed ) {
425
					$text .= ' &hellip; ';
426
				}
427
			}
428
429
			if ( $readmore && isset( $readmore_matches ) && ! empty( $readmore_matches[1] ) ) {
430
				$text .= ' <a href="' . $this->get_permalink() . '" class="read-more">' . trim( $readmore_matches[1] ) . '</a>';
431
			} elseif ( $readmore ) {
432
				$text .= ' <a href="' . $this->get_permalink() . '" class="read-more">' . trim( $readmore ) . '</a>';
433
			}
434
435
			if ( ! $strip ) {
436
				$text .= '</p>';
437
			}
438
		}
439
440
		return trim( $text );
441
	}
442
443
	/**
444
	 * Returns comments array
445
	 *
446
	 * @param string $status Comment status.
447
	 * @param string $order  Order for comments query.
448
	 *
449
	 * @return array
450
	 */
451
	public function get_comments( $status = 'approve', $order = 'DESC' ) {
452
453
		$_return = array();
454
455
		$args = array(
456
			'post_id' => $this->ID,
457
			'status' => $status,
458
			'order' => $order,
459
		);
460
461
		$comments = get_comments( $args );
462
463
		foreach ( $comments as $comment ) {
464
465
			$_return[ $comment->comment_ID ] = new ClassyComment( $comment );
466
467
		}
468
469
		foreach ( $_return as $key => $comment ) {
470
471
			if ( $comment->has_parent() ) {
472
473
				$_return[ $comment->comment_parent ]->add_child( $comment );
474
475
				unset( $_return[ $key ] );
476
477
			}
478
		}
479
480
		return array_values( $_return );
481
	}
482
}
483