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(-)
@@ -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');
}
}
@@ -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;
}
}
@@ -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);
@@ -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);
});
}
}
@@ -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']);
});
}
/**
@@ -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) {
@@ -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;
}
/**