From 50f11a924724c35c4050a1194dbc72d5e56b38ea Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Tue, 25 Oct 2016 11:55:53 +0100 Subject: [PATCH] Add navigation method to Post model --- src/Models/Post.php | 51 +++++++++++++++++++++++++-------------------------- src/Providers/QueryServiceProvider.php | 2 +- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/src/Models/Post.php b/src/Models/Post.php index ac61d6e..4c72208 100644 --- a/src/Models/Post.php +++ b/src/Models/Post.php @@ -44,16 +44,17 @@ class Post extends Model } /** - * Get all the posts within a certain post type. - * - * @param Builder $query query to add the scope to - * @param string $name name of the post type + * The "booting" method of the model. * - * @return Builder + * @return void */ - public function scopePostType($query, $name) + protected static function boot() { - return $query->where('post_type', $name); + parent::boot(); + + static::addGlobalScope('published', function (Builder $builder) { + $builder->where('post_status', 'publish'); + }); } /** @@ -207,12 +208,24 @@ class Post extends Model */ public function getCategoriesAttribute() { - return $this->terms()->whereHas('taxonomy', function ($query) { + return $this->terms()->whereHas('taxonomy', function (Builder $query) { $query->where('taxonomy', 'category'); })->get(); } /** + * Retrieves the navigation to next/previous post, when applicable. + * + * @see get_the_post_navigation() + * @param array $args + * @return string + */ + public static function navigation($args = []) + { + return get_the_post_navigation($args); + } + + /** * Get the permalink for this post. * * @see get_permalink @@ -239,7 +252,7 @@ class Post extends Model /** * Get the thumbnail of this post. * - * @see get_the_post_thumbnail + * @see Post::thumbnail() * * @return string */ @@ -251,7 +264,7 @@ class Post extends Model /** * Get the thumbnail of this post. * - * @see get_the_post_thumbnail + * @see wp_get_attachment_image_url * * @param string $size * @@ -259,7 +272,7 @@ class Post extends Model */ public function thumbnail($size = 'post-thumbnail') { - return get_the_post_thumbnail_url($this->toWordpressPost(), $size); + return wp_get_attachment_image_url($this->getMeta('_thumbnail_id'), $size); } /** @@ -297,7 +310,7 @@ class Post extends Model /** * Get the {@link WP_Post} instance for this Post. * - * @deprecated + * @deprecated Use the methods already provided by this model. * * @return WP_Post */ @@ -305,18 +318,4 @@ class Post extends Model { return new WP_Post((object) $this->toArray()); } - - /** - * The "booting" method of the model. - * - * @return void - */ - protected static function boot() - { - parent::boot(); - - static::addGlobalScope('published', function (Builder $builder) { - $builder->where('post_status', 'publish'); - }); - } } diff --git a/src/Providers/QueryServiceProvider.php b/src/Providers/QueryServiceProvider.php index c0721c2..d042834 100644 --- a/src/Providers/QueryServiceProvider.php +++ b/src/Providers/QueryServiceProvider.php @@ -31,7 +31,7 @@ class QueryServiceProvider extends ServiceProvider // current post in "The Loop" $post = $GLOBALS['post']->ID; - return $this->cached[$post] ?? $this->cached[$post] = Post::find($GLOBALS['post']->ID); + return $this->cached[$post] ?? $this->cached[$post] = Post::find($post); }); } } -- libgit2 1.7.2