Use Eloquent for taxonomy. Add theme supports.
Diff
config/supports.php | 13 +++-
src/Models/Comment.php | 12 +++-
src/Models/Post.php | 84 ++++++++++++---------
src/Models/Term.php | 44 +++++++++++-
src/Models/TermTaxonomy.php | 33 ++++++++-
src/Models/User.php | 16 ++++-
src/Providers/ConfigServiceProvider.php | 1 +-
src/Providers/WordpressServiceProvider.php | 67 ++++++++++++-----
src/Providers/WordpressTemplatingServiceProvider.php | 2 +-
9 files changed, 217 insertions(+), 55 deletions(-)
@@ -0,0 +1,13 @@
<?php
return [
'post-formats',
'post-thumbnails',
'custom-background',
'custom-header',
'custom-logo',
'automatic-feed-links',
'html5' => ['comment-list', 'comment-form', 'search-form', 'gallery', 'caption'],
'title-tag',
'customize-selective-refresh-widgets'
];
@@ -3,6 +3,7 @@ namespace Koselig\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Koselig\Support\Action;
use Koselig\Support\Wordpress;
/**
@@ -18,6 +19,7 @@ use Koselig\Support\Wordpress;
* @property Carbon $comment_date_gmt Date this comment was posted
* @property string $comment_content Content of this comment
* @property integer $comment_karma Karma of this comment
* @property string $content Content of the comment filtered through "comment_text"
* @property boolean $comment_approved Whether or not this comment has been approved
* @property string $comment_agent
* @property string $comment_type
@@ -62,6 +64,16 @@ class Comment extends Model
}
/**
* Comment filtered through the "comment_text" filters.
*
* @return string
*/
public function getContentAttribute()
{
return Action::filter('comment_text', $this->comment_content);
}
/**
* Get the user that posted this comment.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
@@ -2,8 +2,10 @@
namespace Koselig\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Koselig\Exceptions\UnsatisfiedDependencyException;
use Koselig\Support\Action;
@@ -20,6 +22,7 @@ class Post extends Model
protected $table = DB_PREFIX . 'posts';
protected $primaryKey = 'ID';
protected $dates = ['post_date', 'post_date_gmt', 'post_modified', 'post_modified_gmt'];
protected $prefix = DB_PREFIX;
public $timestamps = false;
/**
@@ -34,7 +37,8 @@ class Post extends Model
// Set the current table to the site's own table if we're in a multisite
if (Wordpress::multisite() && (Wordpress::getSiteId() !== 0 && Wordpress::getSiteId() !== 1)) {
$this->setTable(DB_PREFIX . Wordpress::getSiteId() . '_posts');
$this->prefix = DB_PREFIX . Wordpress::getSiteId() . '_';
$this->setTable($this->prefix . 'posts');
}
}
@@ -181,20 +185,41 @@ class Post extends Model
*
* @return string
*/
public function title()
public function getTitleAttribute()
{
return Action::filter('the_title', $this->post_title, $this->ID);
}
/**
* Get the excerpt of this post.
*
* @return string
*/
public function getExcerptAttribute()
{
return Action::filter('get_the_excerpt', $this->post_excerpt);
}
/**
* Get the filtered content of this post.
*
* @return string
*/
public function getContentAttribute()
{
return str_replace(']]>', ']]>', Action::filter('the_content', $this->post_content));
}
/**
* Get the categories of this post.
*
* @see get_the_category
* @return array
* @return Term[]|Collection
*/
public function category()
public function getCategoriesAttribute()
{
return get_the_category($this->ID);
return $this->terms()->whereHas('taxonomy', function ($query) {
$query->where('taxonomy', 'category');
})->get();
}
/**
@@ -203,7 +228,7 @@ class Post extends Model
* @see get_permalink
* @return false|string
*/
public function link()
public function getLinkAttribute()
{
return get_permalink($this->toWordpressPost());
}
@@ -211,47 +236,46 @@ class Post extends Model
/**
* Get the tags of this post.
*
* @see get_the_tags
* @return array|false|\WP_Error
* @return Term[]|Collection
*/
public function tags()
public function getTagsAttribute()
{
return get_the_tags($this->ID);
return $this->terms()->whereHas('taxonomy', function ($query) {
$query->where('taxonomy', 'post_tag');
})->get();
}
/**
* Get the thumbnail of this post
*
* @see get_the_post_thumbnail
* @param string $size
* @param string $attr
* @return string
*/
public function thumbnail($size = 'post-thumbnail', $attr = '')
public function getThumbnailAttribute()
{
return get_the_post_thumbnail($this->toWordpressPost(), $size, $attr);
return $this->thumbnail();
}
/**
* Get the excerpt of this post.
* Get the thumbnail of this post
*
* @see get_the_post_thumbnail
* @param string $size
* @return string
*/
public function excerpt()
public function thumbnail($size = 'post-thumbnail')
{
return Action::filter('get_the_excerpt', $this->post_excerpt);
return get_the_post_thumbnail_url($this->toWordpressPost(), $size);
}
/**
* Get the all the terms of this post.
*
* @see get_the_terms
* @param $taxonomy
* @return array|false|\WP_Error
* @return BelongsToMany
*/
public function terms($taxonomy)
public function terms()
{
return get_the_terms($this->toWordpressPost(), $taxonomy);
return $this->belongsToMany(Term::class, $this->prefix . 'term_relationships', 'object_id', 'term_taxonomy_id');
}
/**
@@ -270,7 +294,7 @@ class Post extends Model
* @see get_post_class
* @return string
*/
public function classes()
public function getClassesAttribute()
{
return implode(' ', get_post_class('', $this->toWordpressPost()));
}
@@ -285,16 +309,4 @@ 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;
}
}
@@ -0,0 +1,44 @@
<?php
namespace Koselig\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Koselig\Support\Wordpress;
/**
* Table containing all terms used by Wordpress.
*
* @author Jordan Doyle <jordan@doyle.wf>
*/
class Term extends Model
{
protected $primaryKey = 'term_id';
protected $table = DB_PREFIX . 'terms';
public $timestamps = false;
/**
* Create a new Eloquent model instance.
*
* @param array $attributes
* @return void
*/
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
// Set the current table to the site's own table if we're in a multisite
if (Wordpress::multisite() && (Wordpress::getSiteId() !== 0 && Wordpress::getSiteId() !== 1)) {
$this->setTable(DB_PREFIX . Wordpress::getSiteId() . '_terms');
}
}
/**
* Get the taxonomy for this term.
*
* @return HasMany
*/
public function taxonomy()
{
return $this->hasMany(TermTaxonomy::class, 'term_id');
}
}
@@ -0,0 +1,33 @@
<?php
namespace Koselig\Models;
use Illuminate\Database\Eloquent\Model;
use Koselig\Support\Wordpress;
/**
* Taxonomy for the terms in the CMS.
*
* @author Jordan Doyle <jordan@doyle.wf>
*/
class TermTaxonomy extends Model
{
protected $primaryKey = 'term_taxonomy_id';
protected $table = DB_PREFIX . 'term_taxonomy';
public $timestamps = false;
/**
* Create a new Eloquent model instance.
*
* @param array $attributes
* @return void
*/
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
// Set the current table to the site's own table if we're in a multisite
if (Wordpress::multisite() && (Wordpress::getSiteId() !== 0 && Wordpress::getSiteId() !== 1)) {
$this->setTable(DB_PREFIX . Wordpress::getSiteId() . '_term_taxonomy');
}
}
}
@@ -22,6 +22,22 @@ class User extends Model implements AuthenticatableContract
public $timestamps = false;
/**
* Create a new Eloquent model instance.
*
* @param array $attributes
* @return void
*/
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
// Set the current table to the site's own table if we're in a multisite
if (Wordpress::multisite() && (Wordpress::getSiteId() !== 0 && Wordpress::getSiteId() !== 1)) {
$this->setTable(DB_PREFIX . Wordpress::getSiteId() . '_users');
}
}
/**
* Get all the posts that belong to this user.
*
* @return HasMany
@@ -16,6 +16,7 @@ class ConfigServiceProvider extends ServiceProvider
realpath(__DIR__ . '/../../config/templates.php') => config_path('templates.php'),
realpath(__DIR__ . '/../../config/wordpress.php') => config_path('wordpress.php'),
realpath(__DIR__ . '/../../config/posttypes.php') => config_path('posttypes.php'),
realpath(__DIR__ . '/../../config/supports.php') => config_path('supports.php')
]);
}
}
@@ -69,6 +69,11 @@ class WordpressServiceProvider extends ServiceProvider
$_SERVER['HTTP_HOST'] = parse_url(config('app.url'))['host'];
}
$GLOBALS['wp_filter']['after_setup_theme'][10][] = [
'function' => [$this, 'addThemeSupport'],
'accepted_args' => 0
];
require ABSPATH . 'wp-settings.php';
}
@@ -128,7 +133,7 @@ class WordpressServiceProvider extends ServiceProvider
*/
private function setMultisiteConstants()
{
$multisite = $this->app->make('config')->get('wordpress.wp_allow_multisite');
$multisite = config('wordpress.wp_allow_multisite');
if ($multisite) {
define('WP_ALLOW_MULTISITE', $multisite);
@@ -159,28 +164,13 @@ class WordpressServiceProvider extends ServiceProvider
return array_merge($pageTemplates, config('templates'));
});
// hacky fix to get network admin working, wordpress is basing the network admin path off of
// the default site's main link, which obviously doesn't work when the site and wordpress are in
// separate directories.
Action::hook('network_site_url', function ($url, $path, $scheme) {
if ($scheme == 'relative') {
$url = Wordpress::site()->path;
} else {
$url = set_url_scheme('http://' . Wordpress::site()->domain . Wordpress::site()->path, $scheme);
}
if ($path && is_string($path)) {
$url .= str_replace('public/', '', WP_PATH) . ltrim($path, '/');
}
return $url;
}, 10, 3);
Action::hook('network_site_url', [$this, 'rewriteNetworkUrl'], 10, 3);
$this->registerPostTypes();
}
/**
* Register all the user's custom post types with Wordpress.
* Register all the site's custom post types with Wordpress.
*
* @return void
*/
@@ -190,4 +180,45 @@ class WordpressServiceProvider extends ServiceProvider
register_post_type($key, $value);
}
}
/**
* Register all of the site's theme support.
*
* @return void
*/
public function addThemeSupport()
{
foreach (config('supports') as $key => $value) {
if (is_string($key)) {
add_theme_support($key, $value);
} else {
add_theme_support($value);
}
}
}
/**
* Hacky fix to get network admin working, Wordpress is basing the network admin path off of
* the default site's main link, which obviously doesn't work when the site and Wordpress are in
* separate directories.
*
* @param $url
* @param $path
* @param $scheme
* @return string
*/
public function rewriteNetworkUrl($url, $path, $scheme)
{
if ($scheme == 'relative') {
$url = Wordpress::site()->path;
} else {
$url = set_url_scheme('http://' . Wordpress::site()->domain . Wordpress::site()->path, $scheme);
}
if ($path && is_string($path)) {
$url .= str_replace('public/', '', WP_PATH) . ltrim($path, '/');
}
return $url;
}
}
@@ -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::thePost(); ?>';
. 'Koselig\Facades\Query::thePost(); $loop = app(\'loop\'); ?>';
});
Blade::directive('endloop', function ($expression) {