rumur/wp-eloquent-models

WordPress Models 的 Eloquent 包装器

v1.0.0 2020-06-15 19:17 UTC

This package is auto-updated.

Last update: 2024-09-11 02:53:44 UTC


README

创建此包是为了提高生产力,并充分发挥 Laravel 的 Eloquent ORM 的全部功能。

该包支持 Themosis 框架Laravel,后者与 WordPress 数据库有连接。

包安装

composer require rumur/wp-eloquent-models

模型集

附件

<?php
use Rumur\WordPress\Eloquent\Model\Attachment;

// Getting an attachment 
$attachment = Attachment::find(2020);

// Available relationships
$attachment->meta;
$attachment->post;
$attachment->author;

// As a WordPress Entity
$attachment->toWordPressEntity(); // <- ?\WP_Post

评论

<?php
use Rumur\WordPress\Eloquent\Model\Comment;

// Getting a comment
$comment = Comment::find(2020);

// Available relationships
$comment->meta;
$comment->post;

// As a WordPress Entity
$comment->toWordPressEntity(); // <- ?\WP_Comment

文章

<?php
use Rumur\WordPress\Eloquent\Model\Post;

// Getting a post
$post = Post::find(2020);

// Available relationships
$post->meta;
$post->terms;
$post->author;
$post->comments;
$post->attachments;

// As a WordPress Entity
$post->toWordPressEntity(); // <- ?\WP_Post

// Taxonomy Scope
$posts = Post::limit(15)->taxonomy('post_tag')->get();

// Status Scope
$published = Post::limit(15)->status('publish')->get();

// Post Type Scope
$orders = Post::with(['author'])->limit(15)->type('order')->get();

术语

<?php
use Rumur\WordPress\Eloquent\Model\Term;

// Getting a term with a specific id
$term = Term::with(['posts'])->find(2020);

// Available relationships
$term->meta;
$term->posts;

// As a WordPress Entity
$term->toWordPressEntity(); // <- ?\WP_Term

// Taxonomy Scope
$tags = Term::limit(15)->taxonomy('post_tag')->get();

用户

<?php
use Rumur\WordPress\Eloquent\Model\User;

// Getting a user with a specific id
$user = User::find(2020);

// Available relationships
$user->meta;
$user->posts;
$user->comments;

// As a WordPress Entity
$user->toWordPressEntity(); // <- ?\WP_User

元数据

模型 AttachmentPostUserCommentTerm 都使用了 HasMeta 特性。因此,它们的元数据可以通过 getMetadeleteMetasetMeta 方法轻松检索、删除和设置。

<?php
use Rumur\WordPress\Eloquent\Model\{Attachment, Comment, Post, Term, User};

$post = Post::find(2020);

$post->setMeta('progress_status', 88);
$featured_img_id = $post->getMeta('_thumbnail_id');

// The same approach can be applied for all other models.

$user = User::find(2020);

$networks = $user->getMeta('networks');

$user->setMeta('networks',  [
	'twitter' => 'https://twitter.com/username',
	'facebook' => 'https://facebook.com/username', 
	'instagram' => 'https://instagram.com/username',
]);

$attachment = Attachment::find(2020);
$meta = $attachment->getMeta('any_attachment_meta_key');

$comment = Comment::find(2020);
$meta = $comment->getMeta('any_comment_meta_key');

$term = Term::find(2020);
$meta = $term->getMeta('any_term_meta_key');

// Delete meta.
Post::find(2020)->deleteMeta('any_meta_key');
Term::find(2020)->deleteMeta('any_meta_key');
User::find(2020)->deleteMeta('any_meta_key');
Comment::find(2020)->deleteMeta('any_meta_key');
Attachment::find(2020)->deleteMeta('any_meta_key');

创建自己的模型

如果您想创建自己的模型,例如 Product,它可能从文章模型扩展而来,那么您只需要将 Rumur\WordPress\Eloquent\Scope\HasPostTypeScope 特性应用于模型以添加全局范围和特定的 post_type

如果您的 post_type 与模型类名不同,您可以通过添加特定的来明确指定将要使用的 post_type

<?php
namespace App\Model;

use Rumur\WordPress\Eloquent\Model\Post;
use Rumur\WordPress\Eloquent\Scope\HasPostTypeScope;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;

class Product extends Post
{
    // Adds a global `post_tpe` scope
    use HasPostTypeScope;

    /**
     * The `post_type` for a model.
     *
     * @var string
     */
    protected static $postType = 'cpt_product';

    public function stores(): HasManyThrough
    {
        return $this->terms()->where('taxonomy', 'store');
    }
}

不带显式指向 post_type 的示例

<?php
namespace App\Model;

use Rumur\WordPress\Eloquent\Model\Post;
use Rumur\WordPress\Eloquent\Scope\HasPostTypeScope;

class Page extends Post
{
    // Adds a global `post_tpe` scope
    use HasPostTypeScope;
}

许可证

此包受 MIT 许可证的许可 - 有关详细信息,请参阅 LICENSE.md 文件。