🏡 index : ~doyle/koselig.git

author Jordan Doyle <jordan@doyle.wf> 2016-10-25 10:55:53.0 +00:00:00
committer Jordan Doyle <jordan@doyle.wf> 2016-10-25 10:55:53.0 +00:00:00
commit
50f11a924724c35c4050a1194dbc72d5e56b38ea [patch]
tree
2866a98951517921e3c0f724c63a5b30338860b2
parent
7cbe5083197cb4b59d34055b805e072dcdfa707c
download
50f11a924724c35c4050a1194dbc72d5e56b38ea.tar.gz

Add navigation method to Post model



Diff

 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);
        });
    }
}