All Projects → drewjbartlett → wordpress-eloquent

drewjbartlett / wordpress-eloquent

Licence: MIT license
A library that converts converts wordpress tables into Laravel Eloquent Models.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to wordpress-eloquent

You Don T Know Oop
Знаете ли вы ооп?
Stars: ✭ 170 (+31.78%)
Mutual labels:  oop
Bliss
Blissful JavaScript
Stars: ✭ 2,352 (+1723.26%)
Mutual labels:  oop
Jcabi Github
Object Oriented Wrapper of Github API
Stars: ✭ 252 (+95.35%)
Mutual labels:  oop
Unifiedtransform
A school management Software
Stars: ✭ 2,248 (+1642.64%)
Mutual labels:  oop
Pencil.js
✏️ Nice modular interactive 2D drawing library
Stars: ✭ 204 (+58.14%)
Mutual labels:  oop
Aquila
🎨 An Advanced WordPress theme
Stars: ✭ 204 (+58.14%)
Mutual labels:  oop
Lw oopc
modified from http://sourceforge.net/projects/lwoopc/
Stars: ✭ 159 (+23.26%)
Mutual labels:  oop
sihae
A PHP 7.4+ blog engine built with Slim Framework and Doctrine ORM
Stars: ✭ 18 (-86.05%)
Mutual labels:  slim-framework
Testdeck
Object oriented testing
Stars: ✭ 206 (+59.69%)
Mutual labels:  oop
Oop
OOP in Elixir!
Stars: ✭ 233 (+80.62%)
Mutual labels:  oop
Object Oriented Programming Using Python
Python is a multi-paradigm programming language. Meaning, it supports different programming approach. One of the popular approach to solve a programming problem is by creating objects. This is known as Object-Oriented Programming (OOP).
Stars: ✭ 183 (+41.86%)
Mutual labels:  oop
Interview Questions
List of all the Interview questions practiced from online resources and books
Stars: ✭ 187 (+44.96%)
Mutual labels:  oop
Logtalk3
Logtalk - declarative object-oriented logic programming language
Stars: ✭ 221 (+71.32%)
Mutual labels:  oop
Software Engineer Interview Questions
A lot of questions and links to prepare yourself for an interview.
Stars: ✭ 176 (+36.43%)
Mutual labels:  oop
awesome-software-architecture
A curated list of awesome articles, videos, and other resources to learn and practice software architecture, patterns, and principles.
Stars: ✭ 1,594 (+1135.66%)
Mutual labels:  oop
Ploop
Prototype Lua object-oriented program system, with many modern features like attribute, overload, etc. For Lua 5.1 or above, include luajit
Stars: ✭ 163 (+26.36%)
Mutual labels:  oop
Learning Oop In Php
A collection of resources to learn object-oriented programming and related concepts for PHP developers.
Stars: ✭ 2,359 (+1728.68%)
Mutual labels:  oop
Design-Patterns
Project for learning and discuss about design patterns
Stars: ✭ 16 (-87.6%)
Mutual labels:  oop
slim3-mvc
Slim 3 PHP micro framework MVC application boilerplate
Stars: ✭ 24 (-81.4%)
Mutual labels:  slim-framework
Crystal Patterns
📖 Examples of GOF patterns written in Crystal
Stars: ✭ 228 (+76.74%)
Mutual labels:  oop

Wordpress Laravel Eloquent Models

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());
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].