1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
<?php
namespace Tev\Post\Model;
use WP_Post;
use Carbon\Carbon;
use Tev\Contracts\WordpressWrapperInterface,
Tev\Author\Factory as AuthorFactory,
Tev\Taxonomy\Factory as TaxonomyFactory,
Tev\Field\Factory as FieldFactory,
Tev\Post\Factory as PostFactory,
Tev\Taxonomy\Model\Taxonomy;
/**
* Abstract post entity class.
*
* Provides a nicely object-oriented interface to a Wordpress post.
*/
abstract class AbstractPost implements WordpressWrapperInterface
{
/**
* Underlying `WP_Post` object.
*
* @var \WP_Post
*/
private $base;
/**
* Author entity factory.
*
* @var \Tev\Author\Factory
*/
private $authorFactory;
/**
* Taxonomy entity factory.
*
* @var \Tev\Taxonomy\Factory
*/
private $taxonomyFactory;
/**
* Field entity factory.
*
* @var \Tev\Field\Factory
*/
private $fieldFactory;
/**
* Post entity factory.
*
* @var \Tev\Post\Factory
*/
private $postFactory;
/**
* Post author.
*
* @var \Tev\Author\Model\Author
*/
private $author;
/**
* Featured image post.
*
* Will be false if no featured image exists for the post
*
* @var \Tev\Post\Model\Attachment|false
*/
private $featuredImage;
/**
* Post type object.
*
* Contains information about the post type of this post.
*
* @var \stdClass
*/
private $postTypeObj;
/**
* Parent post.
*
* @var \Tev\Post\Model\AbstractPost|false
*/
private $parent;
/**
* Constructor.
*
* Inject dependencies.
*
* @param \WP_Post $base Base Wordpress post
* @param \Tev\Author\Factory $authorFactory Author factory
* @param \Tev\Taxonomy\Factory $taxonomyFactory Taxonomy entity factory
* @param \Tev\Field\Factory $fieldFactory Field entity factory
* @param \Tev\Post\Factory $postFactory Post entity factory
* @return void
*/
public function __construct(WP_Post $base,
AuthorFactory $authorFactory,
TaxonomyFactory $taxonomyFactory,
FieldFactory $fieldFactory,
PostFactory $postFactory)
{
$this->base = $base;
$this->authorFactory = $authorFactory;
$this->taxonomyFactory = $taxonomyFactory;
$this->fieldFactory = $fieldFactory;
$this->postFactory = $postFactory;
$this->author = null;
$this->featuredImage = null;
$this->postTypeObj = null;
$this->parent = null;
}
/**
* Get the post ID.
*
* @return int
*/
public function getId()
{
return $this->base->ID;
}
/**
* Get the post type.
*
* @return string
*/
public function getType()
{
return $this->base->post_type;
}
/**
* Get the singular name for the post type of this post.
*
* @return string
*/
public function getTypeName()
{
return $this->getTypeLabels()->singular_name;
}
/**
* Get the post type labels for this post.
*
* See:
*
* http://codex.wordpress.org/Function_Reference/get_post_type_object
*
* for the list of labels returned.
*
* @return \stdClass
*/
public function getTypeLabels()
{
return $this->getPostTypeObject()->labels;
}
/**
* Get the post slug.
*
* @return string
*/
public function getName()
{
return $this->base->post_name;
}
/**
* Get the post status. One of:
*
* - publish
* - pending
* - draft
* - auto-draft
* - future
* - private
* - inherit
* - trash
*
* @return string
*/
public function getStatus()
{
return $this->base->post_status;
}
/**
* Get the post title.
*
* @return string
*/
public function getTitle()
{
return $this->base->post_title;
}
/**
* Get the post content, formatted as HTML.
*
* @return string
*/
public function getContent()
{
$content = $this->getRawContent();
$content = apply_filters('the_content', $content);
$content = str_replace( ']]>', ']]>', $content);
return $content;
}
/**
* Get the raw post content.
*
* @return string
*/
public function getRawContent()
{
return $this->base->post_content;
}
/**
* Check if this post has a manually set excerpt.
*
* @return boolean True if has excerpt, false if not
*/
public function hasExcerpt()
{
return (boolean) strlen($this->base->post_excerpt);
}
/**
* Get the post excerpt.
*
* Adapted from wp_trim_excerpt() in wp-includes/formatting.php.
*
* @param string|null $content Optional. Base content to use for excerpt
* if post excerpt isn't defined. Defaults to
* post content
* @return string
*/
public function getExcerpt($content = null)
{
$raw = $exc = $this->base->post_excerpt;
if (!strlen($exc)) {
$exc = $content === null ? $this->getRawContent() : $content;
$exc = strip_shortcodes($exc);
$exc = apply_filters('the_content', $exc);
$exc = str_replace(']]>', ']]>', $exc);
$exc = wp_trim_words(
$exc,
apply_filters('excerpt_length', 55),
apply_filters('excerpt_more', ' ' . '[…]')
);
}
return apply_filters('wp_trim_excerpt', $exc, $raw);
}
/**
* Get post URL.
*
* @param array $query Query string args. Key value pairs
* @return string
*/
public function getUrl($query = array())
{
return add_query_arg($query, get_permalink($this->getId()));
}
/**
* Get this Post's featured image.
*
* @return \Tev\Post\Model\Attachment|false Featured image object, or false if not
* set
*/
public function getFeaturedImage()
{
if ($this->featuredImage === null) {
if ($imgId = get_post_thumbnail_id((int) $this->getId())) {
$this->featuredImage = $this->postFactory->create(get_post($imgId));
} else {
$this->featuredImage = false;
}
}
return $this->featuredImage;
}
/**
* Get Post featured image URL.
*
* Convenient alias for:
*
* ```
* $pos->getFeaturedImage()->getImageUrl($size);
* ```
*
* @param mixed $size Arguments accepted by second param of wp_get_attachment_image_src()
* @return string|null Image URL, or null if no image set
*/
public function getFeaturedImageUrl($size = 'thumbnail')
{
if ($img = $this->getFeaturedImage()) {
return $img->getImageUrl($size);
}
return null;
}
/**
* Get the post author ID.
*
* @return string
*/
public function getAuthorId()
{
return $this->base->post_author;
}
/**
* Get post author.
*
* @return \Tev\Author\Model\Author
*/
public function getAuthor()
{
if ($this->author === null) {
$this->author = $this->authorFactory->create($this->getAuthorId());
}
return $this->author;
}
/**
* Get the parent post ID.
*
* @return int|null Parent post ID or null if no parent
*/
public function getParentPostId()
{
return $this->base->post_parent ?: null;
}
/**
* Get the parent post, if set.
*
* @return \Tev\Post\Model\AbstractPost|null
*/
public function getParent()
{
if ($this->parent === null) {
if (($parentId = $this->getParentPostId()) !== null) {
$this->parent = $this->postFactory->create(get_post($parentId));
} else {
$this->parent = false;
}
}
return $this->parent ?: null;
}
/**
* Get the post published date.
*
* @return \Carbon\Carbon
*/
public function getPublishedDate()
{
return Carbon::createFromFormat('Y-m-d H:i:s', $this->base->post_date);
}
/**
* Get the post published date in GMT.
*
* @return \Carbon\Carbon
*/
public function getPublishedDateGmt()
{
return Carbon::createFromFormat(
'Y-m-d H:i:s',
$this->base->post_date_gmt,
'UTC'
);
}
/**
* Get the post modified date.
*
* @return \Carbon\Carbon
*/
public function getModifiedDate()
{
return Carbon::createFromFormat('Y-m-d H:i:s', $this->base->post_modified);
}
/**
* Get the post modified date in GMT.
*
* @return \Carbon\Carbon
*/
public function getModifiedDateGmt()
{
return Carbon::createFromFormat(
'Y-m-d H:i:s',
$this->base->post_modified_gmt,
'UTC'
);
}
/**
* Get all categories for this post.
*
* @return Tev\Term\Model\Term[] Array of categories
*/
public function getCategories()
{
return $this->getTermsFor('category');
}
/**
* Get all tags for this post.
*
* @return Tev\Term\Model\Term[] Array of tags
*/
public function getTags()
{
return $this->getTermsFor('post_tag');
}
/**
* Get all terms for this post in the given taxonomy.
*
* @param mixed|\Tev\Taxonomy\Model\Taxonomy $taxonomy Taxonomy name or object
* @return \Tev\Term\Model\Term[] Array of terms
*/
public function getTermsFor($taxonomy)
{
if (!($taxonomy instanceof Taxonomy)) {
$taxonomy = $this->taxonomyFactory->create($taxonomy);
}
return $taxonomy->getTermsForPost($this);
}
/**
* Get a meta value on this post.
*
* See: http://codex.wordpress.org/Function_Reference/get_post_meta for
* how `$single` works.
*
* @param string $key Meta item key
* @param boolean $single Optional. Defaults to true
* @return mixed Meta data
*/
public function meta($key, $single = true)
{
return get_post_meta($this->getId(), $key, $single);
}
/**
* Get a custom field on this post.
*
* @param string $name Field name (or key)
* @return \Tev\Field\Model\AbstractField Field object
*/
public function field($name)
{
return $this->fieldFactory->create($name, $this);
}
/**
* Get the underlying `WP_Post` object.
*
* @return \WP_Post
*/
public function getBaseObject()
{
return $this->base;
}
/**
* Get the post type object for this post.
*
* Contains information about the post type of this post.
*
* @return \stdClass
*/
protected function getPostTypeObject()
{
if ($this->postTypeObj === null) {
$this->postTypeObj = get_post_type_object($this->getType());
}
return $this->postTypeObj;
}
}