From afa35f167e02080833f72d3e8663d6624d4a83b3 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Sat, 22 Oct 2016 23:09:38 +0100 Subject: [PATCH] Use Eloquent for taxonomy. Add theme supports. --- 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(-) create mode 100644 config/supports.php create mode 100644 src/Models/Term.php create mode 100644 src/Models/TermTaxonomy.php diff --git a/config/supports.php b/config/supports.php new file mode 100644 index 0000000..16e195a --- /dev/null +++ b/config/supports.php @@ -0,0 +1,13 @@ + ['comment-list', 'comment-form', 'search-form', 'gallery', 'caption'], + 'title-tag', + 'customize-selective-refresh-widgets' +]; diff --git a/src/Models/Comment.php b/src/Models/Comment.php index bdc92b5..33c1775 100644 --- a/src/Models/Comment.php +++ b/src/Models/Comment.php @@ -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 diff --git a/src/Models/Post.php b/src/Models/Post.php index 314a4ac..db77bc3 100644 --- a/src/Models/Post.php +++ b/src/Models/Post.php @@ -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; - } } diff --git a/src/Models/Term.php b/src/Models/Term.php new file mode 100644 index 0000000..8a25bf9 --- /dev/null +++ b/src/Models/Term.php @@ -0,0 +1,44 @@ + + */ +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'); + } +} diff --git a/src/Models/TermTaxonomy.php b/src/Models/TermTaxonomy.php new file mode 100644 index 0000000..7d581f6 --- /dev/null +++ b/src/Models/TermTaxonomy.php @@ -0,0 +1,33 @@ + + */ +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'); + } + } +} diff --git a/src/Models/User.php b/src/Models/User.php index 6c59513..d53fb53 100644 --- a/src/Models/User.php +++ b/src/Models/User.php @@ -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 diff --git a/src/Providers/ConfigServiceProvider.php b/src/Providers/ConfigServiceProvider.php index 9cde954..27fa111 100644 --- a/src/Providers/ConfigServiceProvider.php +++ b/src/Providers/ConfigServiceProvider.php @@ -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') ]); } } diff --git a/src/Providers/WordpressServiceProvider.php b/src/Providers/WordpressServiceProvider.php index d4bbb16..a377b2f 100644 --- a/src/Providers/WordpressServiceProvider.php +++ b/src/Providers/WordpressServiceProvider.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; + } } diff --git a/src/Providers/WordpressTemplatingServiceProvider.php b/src/Providers/WordpressTemplatingServiceProvider.php index fb7a573..2672cc1 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 ''; + . 'Koselig\Facades\Query::thePost(); $loop = app(\'loop\'); ?>'; }); Blade::directive('endloop', function ($expression) { -- libgit2 1.7.2