🏡 index : ~doyle/koselig.git

author Jordan Doyle <jordan@doyle.wf> 2016-10-16 15:03:25.0 +00:00:00
committer Jordan Doyle <jordan@doyle.wf> 2016-10-16 15:03:25.0 +00:00:00
commit
e816ef8030638e30977c3c6b5b009905c245fc39 [patch]
tree
9ed70a48480dff9a36a75a993b395edbaf5e5d10
parent
979450a7feced5bddaa06c8cb9589118a6974776
download
e816ef8030638e30977c3c6b5b009905c245fc39.tar.gz

Post changes, introduced a 'Loop' class which is an alias of the current Post



Diff

 src/Facades/Loop.php                                 | 25 +++++++++++++++-
 src/Models/Post.php                                  | 23 ++++++++++++-
 src/Providers/KoseligServiceProvider.php             |  1 +-
 src/Providers/QueryServiceProvider.php               | 36 +++++++++++++++++++++-
 src/Providers/WordpressServiceProvider.php           |  5 +---
 src/Providers/WordpressTemplatingServiceProvider.php |  2 +-
 src/Support/Wordpress.php                            |  4 +-
 7 files changed, 87 insertions(+), 9 deletions(-)

diff --git a/src/Facades/Loop.php b/src/Facades/Loop.php
new file mode 100644
index 0000000..5c95126
--- /dev/null
+++ b/src/Facades/Loop.php
@@ -0,0 +1,25 @@
<?php
namespace Koselig\Facades;

use Illuminate\Support\Facades\Facade;

/**
 * Facade for {@link Koselig\Models\Post}. Provides access to the current post in "The Loop"
 *
 * @see \Koselig\Models\Post
 * @author Jordan Doyle <jordan@doyle.wf>
 */
class Loop extends Facade
{
    /**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor()
    {
        // we don't want resolveFacadeInstance to cache this value since it will always change, so
        // we have to pass an object back.
        return app('loop');
    }
}
diff --git a/src/Models/Post.php b/src/Models/Post.php
index 6512c32..88d5982 100644
--- a/src/Models/Post.php
+++ b/src/Models/Post.php
@@ -137,6 +137,16 @@ class Post extends Model
    }

    /**
     * Get the filtered title.
     *
     * @return string
     */
    public function title()
    {
        return Action::filter('the_title', $this->post_title, $this->ID);
    }

    /**
     * Get the categories of this post.
     *
     * @see get_the_category
@@ -189,7 +199,6 @@ class Post extends Model
     */
    public function excerpt()
    {
        dd($this);
        return Action::filter('get_the_excerpt', $this->post_excerpt);
    }

@@ -236,4 +245,16 @@ class Post extends Model
    {
        return new WP_Post((object) $this->toArray());
    }

    /**
     * Get the current instance of this class. Useful when you would like to get a property when behind
     * the Loop facade.
     *
     *
     * @return $this
     */
    public function get()
    {
        return $this;
    }
}
diff --git a/src/Providers/KoseligServiceProvider.php b/src/Providers/KoseligServiceProvider.php
index d3c31fc..f625a1d 100644
--- a/src/Providers/KoseligServiceProvider.php
+++ b/src/Providers/KoseligServiceProvider.php
@@ -23,6 +23,7 @@ class KoseligServiceProvider extends ServiceProvider
        // Generic service providers
        $this->app->register(WordpressServiceProvider::class);
        $this->app->register(ConfigServiceProvider::class);
        $this->app->register(QueryServiceProvider::class);

        // Blade service provider
        $this->app->register(WordpressTemplatingServiceProvider::class);
diff --git a/src/Providers/QueryServiceProvider.php b/src/Providers/QueryServiceProvider.php
new file mode 100644
index 0000000..07fea6c
--- /dev/null
+++ b/src/Providers/QueryServiceProvider.php
@@ -0,0 +1,36 @@
<?php
namespace Koselig\Providers;

use Illuminate\Support\ServiceProvider;
use Koselig\Models\Post;
use Koselig\Proxy\Query;

/**
 * Service provider that provides bindings for the several queries that Wordpress
 * has running at once.
 *
 * @author Jordan Doyle <jordan@doyle.wf>
 */
class QueryServiceProvider extends ServiceProvider
{
    private $cached = [];

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton('query', function () {
            // main page query
            return Query::instance($GLOBALS['wp_the_query']);
        });

        $this->app->bind('loop', function () {
            // current post in "The Loop"
            $post = $GLOBALS['post']->ID;
            return $this->cached[$post] ?? $this->cached[$post] = Post::find($GLOBALS['post']->ID);
        });
    }
}
diff --git a/src/Providers/WordpressServiceProvider.php b/src/Providers/WordpressServiceProvider.php
index b788c5b..6d726eb 100644
--- a/src/Providers/WordpressServiceProvider.php
+++ b/src/Providers/WordpressServiceProvider.php
@@ -4,7 +4,6 @@ namespace Koselig\Providers;
use Illuminate\Contracts\Routing\UrlGenerator;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\ServiceProvider;
use Koselig\Proxy\Query;
use Koselig\Support\Action;
use Koselig\Support\Wordpress;

@@ -38,10 +37,6 @@ class WordpressServiceProvider extends ServiceProvider

        // Set up the WordPress query.
        wp();

        $this->app->singleton('query', function () {
            return Query::instance($GLOBALS['wp_the_query']);
        });
    }

    /**
diff --git a/src/Providers/WordpressTemplatingServiceProvider.php b/src/Providers/WordpressTemplatingServiceProvider.php
index 8fe9a3e..d9731af 100644
--- a/src/Providers/WordpressTemplatingServiceProvider.php
+++ b/src/Providers/WordpressTemplatingServiceProvider.php
@@ -20,7 +20,7 @@ class WordpressTemplatingServiceProvider extends ServiceProvider
    {
        Blade::directive('loop', function ($expression) {
            return '<?php if (Koselig\Facades\Query::hasPosts()): while (Koselig\Facades\Query::hasPosts()): '
                . 'Koselig\Facades\Query::post(); ?>';
                . 'Koselig\Facades\Query::thePost(); ?>';
        });

        Blade::directive('endloop', function ($expression) {
diff --git a/src/Support/Wordpress.php b/src/Support/Wordpress.php
index 6f614e6..0d0b32a 100644
--- a/src/Support/Wordpress.php
+++ b/src/Support/Wordpress.php
@@ -13,13 +13,13 @@ use Koselig\Models\User;
class Wordpress
{
    /**
     * Get the current page id.
     * Get the page id.
     *
     * @return int
     */
    public static function id()
    {
        return get_the_ID();
        return app('query')->post->ID;
    }

    /**