Skip to content

Commit

Permalink
Merge pull request #287 from corcel/dev
Browse files Browse the repository at this point in the history
Merge dev into master for v1.4.7
  • Loading branch information
jgrossi committed Jul 5, 2017
2 parents de6b5f1 + 640763f commit cc799e6
Show file tree
Hide file tree
Showing 4 changed files with 3,378 additions and 3,021 deletions.
142 changes: 88 additions & 54 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,58 @@ Corcel is a class collection created to retrieve WordPress database data using a

This way, you can use WordPress as the backend (admin panel), to insert posts, custom types, etc, and you can use whatever you want in the frontend, like Silex, Slim Framework, Laravel, Zend, or even pure PHP (why not?). So, just use Corcel to retrieve data from WordPress.

## Installation

To install Corcel, just run the following command:
# Contents

- [Installing Corcel](#install)
- [Database Setup](#database-setup)
- [Usage](#usage)
- [Posts](#posts)
- [Advanced Custom Fields (ACF) Integration](#acf)
- [Custom Post Type](#custom-post)
- [Single Table Inheritance](#single-tab)
- [Taxonomies](#taxonomies)
- [Post Format](#post-format)
- [Pages](#pages)
- [Categories & Taxonomies](#cats)
- [Attachments & Revision](#attachments)
- [Menu](#menu)
- [Users](#users)
- [Authentication](#auth)
- [Running Tests](#tests)
- [Contributing](#contrib)
- [License](#license)

# <a id="install"></a> Installing Corcel

You need to use Composer to install Corcel into your project:

```
composer require jgrossi/corcel
```

## Usage
Now you're almost ready to use Corcel classes, like `Corcel\Post`.

## WordPress Installation

You can install WordPress inside Laravel's `/public` directory, like `/public/wordpress`, for example, or even in a different domain or server. You only have to enable access to its database, because that's what Corcel will use. This is made by creating a database connection.

### I'm using Laravel
# <a id="database-setup"></a> Database Setup

If you are using Laravel you **do not need** to configure database again. It's all already set by Laravel. So you have only to change the `config/database.php` config file and set yours connections. You can use just one connection or two (one for your Laravel app and another to Corcel). Your file will look like this:
## Laravel Setup

Corcel by default will look for a `wordpress` or `corcel` database connection in your Laravel `config/database.php` file. If it does not find any of these connections it'll use the `default` connection.

For example, you can have a `default` connection for your Laravel app tables and models, and another one called `wordpress` or `corcel` for your WordPress tables. Easy like that.

```php
<?php // File: /config/database.php

'connections' => [

'mysql' => [ // this is your Laravel database connection
'mysql' => [ // for Laravel database
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'app',
'database' => 'mydatabase',
'username' => 'admin'
'password' => 'secret',
'charset' => 'utf8',
Expand All @@ -47,10 +76,10 @@ If you are using Laravel you **do not need** to configure database again. It's a
'engine' => null,
],

'wordpress' => [ // this is your Corcel database connection, where WordPress tables are
'wordpress' => [ // for WordPress database
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'corcel',
'database' => 'mydatabase',
'username' => 'admin',
'password' => 'secret',
'charset' => 'utf8',
Expand All @@ -59,33 +88,10 @@ If you are using Laravel you **do not need** to configure database again. It's a
'strict' => false,
'engine' => null,
],

],
```

Now you should create your own `Post` model class. Laravel stores model classes in `app` directory, inside `App` namespace (or the name you gave it). Your `Post` class should extends `Corcel\Post` and set the connection name you're using, in this case `wordpress`:

```php
<?php // File: app/Post.php

namespace App;

use Corcel\Post as Corcel;

class Post extends Corcel
{
protected $connection = 'wordpress';
}
```

So, now you can fetch database data:

```php
$posts = App\Post::all(); // using the 'wordpress' connection
$posts = Corcel\Post::all(); // using the 'default' Laravel connection
```

### I'm using another PHP Framework
## Other PHP Framework (not Laravel) Setup

Here you have to configure the database to fit the Corcel requirements. First, you should include the Composer `autoload` file if not already loaded:

Expand Down Expand Up @@ -115,7 +121,32 @@ You can specify all Eloquent params, but some are default (but you can override
'prefix' => 'wp_', // Specify the prefix for WordPress tables, default prefix is 'wp_'
```

### Posts
# <a id="usage"></a> Usage

Optionally you can create your own `Post` model which extends `Corcel\Post`. Then set the connection name you're using, in this case `foo-bar`:

```php
<?php // File: app/Post.php

namespace App;

use Corcel\Post as Corcel;

class Post extends Corcel
{
protected $connection = 'foo-bar';
}
```

So, now you can fetch WP database data using your own class:

```php
$posts = App\Post::all(); // using the 'foo-bar' connection
```

> Remembering that if you have a `wordpress` or `corcel` database connection you don't need to extend `Corcel\Post` class, just use `Corcel\Post` instead.
## <a id="posts"></a> Posts

> Every time you see `Post::method()`, if you're using your own Post class (where you set the connection name), like `App\Post` you should use `App\Post::method()` and not `Post::method()`. All the examples are assuming you already know this difference.
Expand Down Expand Up @@ -163,7 +194,7 @@ $post->meta->url = 'http://grossi.io';
$post->save();
```

#### Advanced Custom Fields (ACF)
## <a id="acf"></a> Advanced Custom Fields (ACF)

If you want to retrieve a custom field created by the [Advanced Custom Fields (ACF)](http://advancedcustomfields.com) plugin, you have to install the [`corcel/acf`](http://github.com/corcel/acf) plugin and call the custom field like this:

Expand All @@ -181,7 +212,7 @@ echo $post->acf->text('text_field_name');
echo $post->acf->boolean('boolean_field_name');
```

### Custom Post Type
## <a id="custom-post"></a> Custom Post Type

You can work with custom post types too. You can use the `type(string)` method or create your own class.

Expand Down Expand Up @@ -211,7 +242,7 @@ foreach ($stores as $store) {
}
```

## Single Table Inheritance
## <a id="single-tab"></a>Single Table Inheritance

If you choose to create a new class for your custom post type, you can have this class be returned for all instances of that post type.

Expand All @@ -231,7 +262,7 @@ You can also do this for inbuilt classes, such as Page or Post. Simply register

This is particular useful when you are intending to get a Collection of Posts of different types (e.g. when fetching the posts defined in a menu).

### Shortcodes
## <a id="shortcodes"></a> Shortcodes

You can add [shortcodes](https://codex.wordpress.org/Shortcode_API) by calling the `addShortcode` method on the `Post` model :

Expand All @@ -248,7 +279,7 @@ If you are using Laravel, we suggest adding your shortcodes handlers in `App\Pro

The [*thunderer/shortcode*](https://github.com/thunderer/Shortcode) library is used to parse the shortcodes. For more information, [click here](https://github.com/thunderer/Shortcode).

### Taxonomies
## <a id="taxonomies"></a>Taxonomies

You can get taxonomies for a specific post like:

Expand All @@ -264,26 +295,29 @@ Or you can search for posts using its taxonomies:
$post = Post::taxonomy('category', 'php')->first();
```

### Post Format
## <a id="post-format"></a>Post Format

You can also get the post format, like the WordPress function `get_post_format()`:

```php
echo $post->getFormat(); // should return something like 'video', etc
```

### Pages
## <a id="pages"></a>Pages

Pages are like custom post types. You can use `Post::type('page')` or the `Page` class.
Pages are like custom post types. You can use `Post::type('page')` or the `Corcel\Page` class.

```php

use Corcel\Page;

// Find a page by slug
$page = Page::slug('about')->first(); // OR
$page = Post::type('page')->slug('about')->first();
echo $page->post_title;
```

### Categories & Taxonomies
## <a id="cats"></a>Categories & Taxonomies

Get a category or taxonomy or load posts from a certain category. There are multiple ways
to achieve it.
Expand All @@ -306,7 +340,7 @@ $cat->posts->each(function($post) {
});
```

### Attachment and Revision
## <a id="attachments"></a>Attachment and Revision

Getting the attachment and/or revision from a `Post` or `Page`.

Expand All @@ -320,7 +354,7 @@ $post = Post::slug('test')->with('revision')->first();
print_r($post->revision);
```

### Menu
## <a id="menu"></a> Menu

To get a menu by its slug, use the syntax below.
The menu items will be loaded in the `nav_items` variable. The currently supported menu items are: Pages, Posts, Links, Categories, Tags.
Expand Down Expand Up @@ -361,7 +395,7 @@ foreach ($menuArray[0] as $item) {
}
```

### Users
## <a id="users"></a> Users

You can manipulate users in the same manner you work with posts:

Expand All @@ -374,9 +408,9 @@ $user = User::find(1);
echo $user->user_login;
```

### Authentication
## <a id="auth"></a>Authentication

#### Using laravel
### Using laravel

You will have to register Corcel's authentication service provider in `config/app.php` :

Expand Down Expand Up @@ -424,7 +458,7 @@ class PasswordController extends Controller
}
```

#### Using something else
### Using something else

You can use the `AuthUserProvider` class to authenticate an user :

Expand All @@ -436,7 +470,7 @@ if(!is_null($user) && $userProvider->validateCredentials($user, ['password' => '
}
```

## Running tests
# <a id="tests"></a> Running Tests

To run the phpunit tests, execute the following command :

Expand All @@ -450,7 +484,7 @@ If you have the global `phpunit` command installed you can just type:
phpunit
```

## Contributing
# <a id="contrib"></a> Contributing

All contributions are welcome to help improve Corcel.

Expand All @@ -464,8 +498,8 @@ Before you submit your pull request consider the following guidelines:

- Run the unit tests, and ensure that all tests pass.

- In GitHub, send a pull request to `corcel:dev`.
- In GitHub, send a pull request to `corcel:dev`, not `corcel:master`, please.

## Licence
## <a id="license"></a> Licence

[MIT License](http://jgrossi.mit-license.org/) © Junior Grossi
2 changes: 1 addition & 1 deletion src/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public function scopeHasMeta($query, $meta, $value = null)
return $query->whereHas('meta', function ($query) use ($meta, $value) {
$query->where('meta_key', $meta);
if (!is_null($value)) {
$query->where('meta_value', $value);
$query->{is_array($value) ? 'whereIn' : 'where'}('meta_value', $value);
}
});
}
Expand Down
11 changes: 11 additions & 0 deletions tests/HasMetaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ public function testUserHasMeta()
$post = Post::published()->hasMeta('username', 'juniorgrossi')
->first();

$posts = Post::published()->hasMeta('city', ['amsterdam', 'london'])
->get();

$this->assertTrue($post instanceof \Corcel\Post);

$this->assertNotNull($posts);
$this->assertTrue($posts instanceof Illuminate\Support\Collection);
$this->assertNotEquals(0, $posts->count());

foreach ($posts as $o_post) {
$this->assertTrue($post instanceof \Corcel\Post);
}
}
}
Loading

0 comments on commit cc799e6

Please sign in to comment.