定制 drewjbartlett/wordpress-eloquent 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

drewjbartlett/wordpress-eloquent

Composer 安装命令:

composer require drewjbartlett/wordpress-eloquent

包简介

A Laravel wrapper for wordpress which turns all Wordpress models into Laravel Eloquent Models.

README 文档

README

A library that converts converts wordpress tables into Laravel Eloquent Models. This is helpful for dropping into any wordpress project where maybe you'd rather use the awesome features of Laravel's Eloquent Models. Or maybe you're writing an API with something like Slim or better yet Lumen don't want to increase your load time by loading the entire WP core. This is a great boiler plate based off Eloquent by Laravel to get you going.

** This is documentation for additional functionality on top of Eloquent. For documentation on all of Eloquent's features you visit the documentation.

Overview

Installation

composer require drewjbartlett/wordpress-eloquent

Setup

require_once('vendor/autoload.php');

\WPEloquent\Core\Laravel::connect([
    'global' => true,

    'config' => [

        'database' => [
            'user'     => 'user',
            'password' => 'password',
            'name'     => 'database',
            'host'     => '127.0.0.1',
            'port'     => '3306'
        ],

        // your wpdb prefix
        'prefix' => 'wp_',
    ],

    // enable events
    'events' => false,

    // enable query log
    'log'    => true
]);

If you wanted to enable this on your entire WP install you could create a file with the above code to drop in the mu-plugins folder.

Posts

use \WPEloquent\Model\Post;

// getting a post
$post = Post::find(1);

// available relationships
$post->author;
$post->comments;
$post->terms;
$post->tags;
$post->categories;
$post->meta;

Statuses

By default, the Post returns posts with all statuses. You can however override this with the local scope published to return only published posts.

Post::published()->get();

Or if you need a specific status you can override with defined status via local scope.

Post::status('draft')->get();

Post Types

By default, the Post returns posts with all post types. You can however override this by defining a post type via local scope.

Post::type('page')->get();

Comments

use \WPEloquent\Model\Comment;

// getting a comment
$comment = Comment::find(12345);

// available relationships
$comment->post;
$comment->author;
$comment->meta

Terms

In this version Term is still accesible as a model but is only leveraged through posts.

$post->terms()->where('taxonomy', 'country');

Users

use \WPEloquent\Model\User;

// getting a comment
$user = User::find(123);

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

Meta

The models Post, User, Comment, Term, all implement the HasMeta. Therefore they meta can easily be retrieved by the getMeta and set by the setMeta helper functions:

$post = Post::find(1);
$post->setMeta('featured_image', 'my-image.jpg');
$post->setMeta('breakfast', ['waffles' => 'blueberry', 'pancakes' => 'banana']);

// or all in one call
$featured_image = Post::find(1)->getMeta('featured_image');
Post::find(1)->setMeta('featured_image', 'image.jpg');

// same applies for all other models

$user = User::find(1)
$facebook = $user->getMeta('facebook');
$user->setMeta('social', ['facebook' => 'facebook.com/me', 'instagram' => 'instagram.com/me']);

$comment = Comment::find(1);
$meta = $comment->getMeta('some_comment_meta');

$term = Term::find(123);
$meta = $term->getMeta('some_term_meta');

// delete meta
$post = Post::find(123)->deleteMeta('some_term_meta');

Options

In wordpress you can use get_option. Alternatively, if you don't want to load the wordpress core you can use helper function getValue.

use \WPEloquent\Model\Post;

$siteurl = Option::getValue('siteurl');

Or of course, the long form:

use \WPEloquent\Model\Options;

$siteurl = Option::where('option_name', 'siteurl')->value('option_value');

Links

use \WPEloquent\Model\Link;

$siteurl = Link::find(1);

Extending your own models

If you want to add your own functionality to a model, for instance a User you can do so like this:

namespace App\Model;

class User extends \WPEloquent\Model\User {

    public function orders() {
        return $this->hasMany('\App\Model\User\Orders');
    }

    public function current() {
        // some functionality to get current user
    }

    public function favorites() {
        return $this->hasMany('Favorites');
    }

}

Another example would be for custom taxonomies on a post, say country

namespace App\Model;

class Post extends \WPEloquent\Model\Post {

    public function countries() {
        return $this->terms()->where('taxonomy', 'country');
    }

}

Post::with(['categories', 'countries'])->find(1);

Query Logs

Sometimes it's helpful to see the query logs for debugging. You can enable the logs by passing log is set to true (see setup) on the Laravel::connect method. Logs are retrieved by running.

use \WPEloquent\Core\Laravel;

print_r(Laravel::queryLog());

drewjbartlett/wordpress-eloquent 适用场景与选型建议

drewjbartlett/wordpress-eloquent 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.51k 次下载、GitHub Stars 达 150, 最近一次更新时间为 2017 年 01 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「wordpress」 「laravel」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 drewjbartlett/wordpress-eloquent 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 drewjbartlett/wordpress-eloquent 我们能提供哪些服务?
定制开发 / 二次开发

基于 drewjbartlett/wordpress-eloquent 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.51k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 151
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 150
  • Watchers: 10
  • Forks: 47
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-01-12