Skip to content

Commit

Permalink
Merge branch 'release/0.1.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
jgrossi committed Nov 2, 2016
2 parents bbffed5 + 273a5c9 commit 4feb056
Show file tree
Hide file tree
Showing 25 changed files with 149 additions and 81 deletions.
41 changes: 38 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
# Corcel ACF Plugin

[![Travis](https://travis-ci.org/jgrossi/corcel.svg?branch=master)](https://travis-ci.org/corcel/acf?branch=master)
[![Packagist](https://img.shields.io/packagist/v/corcel/acf.svg)](https://github.com/corcel/acf/releases)
[![Packagist](https://img.shields.io/packagist/dt/corcel/acf.svg)](https://packagist.org/packages/corcel/acf)

> Fetch all Advanced Custom Fields (ACF) fields inside Corcel easily.
This Corcel plugin allows you to fetch WordPress custom fields created by the [ACF](http://advancedcustomfields.com) plugin using the same syntax of Eloquent, from the [Laravel Framework](http://laravel.com). You can use Eloquent models and Collections to improve your development, using the WordPress backend with any PHP application.

For more information about how Corcel works please visit [the repository](http://github.com/jgrossi/corcel).

* [Installation](#installation)
* [Usage](#usage)
- [The Idea](#the-idea)
- [What is Missing](#what-is-missing)
- [Fields](#fields)
* [Contributing](#contributing)
- [Running Tests](#running-tests)
* [Licence](#licence)

# Installation

To install the ACF plugin for Corcel is easy:
Expand All @@ -21,6 +38,20 @@ $post = Post::find(1);
echo $post->acf->url; // returns the url custom field created using ACF
```

## Performance

When using something like `$post->acf->url` the plugin has to make some extra SQL queries to get the field type according ACF approach. So we created another way to get that without making those extra queries. You have only the inform the plugin what is the post type, as a function:

```php
// Extra queries way
echo $post->acf->author_username; // it's a User field

// Without extra queries
echo $post->acf->user('author_username');
```

> PS: The method names should be written in `camelCase()` format. So, for example, for the field type `date_picker` you should write `$post->acf->datePicker('fieldName')`. The plugin does the conversion from `camelCase` to `snake_case` for you.

## The Idea

Using the default `$post->meta->field_name` in Corcel returns the value of the `meta_value` column in the `wp_postmeta` table. It can be a string, an integer or even a serialized array. The problem is, when you're using ACF this value can be more than that. If you have an integer, for example, it can be a `post id`, an `user id` or even an array of `posts ids`.
Expand All @@ -31,9 +62,13 @@ First ACF fetches the `meta_value` in `wp_postmeta` table, where the `meta_key`

This plugin works with a basic logic inside `Corcel\Acf\Field\BasicField` abstract class, that has a lot of useful functions to return the `field key`, the `value`, etc. The `Corcel\Acf\FieldFactory` is responsible to return the correct field instance according the field type, so, if the field type is `post_object` it return an instance of `Corcel\Acf\Field\PostObject`, and it will returns in the `get()` method an instance of `Corcel\Post`.

## What's Missing
## What is Missing

First we should create the fields classes and the test cases. After we have to setup how Corcel is going to work with the `corcel/acf` plugin, returning the custom field value in the format `$post->meta->field` or maybe `$post->acf->field` having different behavior. This should be done yet!
First we should create the fields classes and the test cases. After we have to setup how Corcel is going to work with the `corcel/acf` plugin, returning the custom field value in the format `$post->meta->field` or maybe `$post->acf->field` having different behavior (done!).

- Create more unit tests for `Repeater` field;
- Implement the `Flexible Content` field with unit tests;
- Improve performance. Currently the plugin makes one SQL query for each field. This goal is to improve that using `whereIn()` clauses.

Some fields are still missing (check table below and contribute).

Expand Down Expand Up @@ -91,6 +126,6 @@ You should import the `database.sql` file to a database inside your local enviro
> If you want to access the WordPress Admin Panel just use username as `admin` and password `123456`.
## Licence
# Licence
[MIT License](http://jgrossi.mit-license.org/) © Junior Grossi
26 changes: 24 additions & 2 deletions src/AdvancedCustomFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace Corcel\Acf;

use Corcel\Acf\Exception\MissingFieldNameException;
use Corcel\Post;

/**
* Class AdvancedCustomFields
* Class AdvancedCustomFields.
*
* @package Corcel\Acf
* @author Junior Grossi <[email protected]>
*/
class AdvancedCustomFields
Expand Down Expand Up @@ -37,10 +37,32 @@ public function get($fieldName)

/**
* @param string $name
*
* @return mixed
*/
public function __get($name)
{
return $this->get($name);
}

/**
* Make possible to call $post->acf->fieldType('fieldName').
*
* @param string$name
* @param array $arguments
*
* @return mixed
*
* @throws MissingFieldNameException
*/
public function __call($name, $arguments)
{
if (!isset($arguments[0])) {
throw new MissingFieldNameException('The field name is missing');
}

$field = FieldFactory::make($arguments[0], $this->post, snake_case($name));

return $field->get();
}
}
12 changes: 12 additions & 0 deletions src/Exception/MissingFieldNameException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Corcel\Acf\Exception;

/**
* Class MissingFieldNameException.
*
* @author Junior Grossi <[email protected]>
*/
class MissingFieldNameException extends \Exception
{
}
14 changes: 8 additions & 6 deletions src/Field/BasicField.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
use Corcel\PostMeta;

/**
* Class BasicField
* Class BasicField.
*
* @package Corcel\Acf\Field
* @author Junior Grossi <[email protected]>
*/
abstract class BasicField
Expand Down Expand Up @@ -44,18 +43,19 @@ abstract class BasicField
protected $value;

/**
* Constructor method
* Constructor method.
*/
public function __construct()
{
$this->postMeta = new PostMeta();
}

/**
* Get the value of a field according it's post ID
* Get the value of a field according it's post ID.
*
* @param string $field
* @param Post $post
* @param Post $post
*
* @return array|string
*/
public function fetchValue($field, Post $post)
Expand All @@ -80,7 +80,8 @@ public function fetchValue($field, Post $post)

/**
* @param string $fieldName
* @param Post $post
* @param Post $post
*
* @return string
*/
public function fetchFieldKey($fieldName, Post $post)
Expand All @@ -103,6 +104,7 @@ public function fetchFieldKey($fieldName, Post $post)

/**
* @param string $fieldKey
*
* @return string
*/
public function fetchFieldType($fieldKey)
Expand Down
5 changes: 2 additions & 3 deletions src/Field/Boolean.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
use Corcel\Acf\FieldInterface;

/**
* Class Boolean
* Class Boolean.
*
* @package Corcel\Acf\Field
* @author Junior Grossi <[email protected]>
*/
class Boolean extends Text implements FieldInterface
Expand All @@ -17,6 +16,6 @@ class Boolean extends Text implements FieldInterface
*/
public function get()
{
return (bool)parent::get();
return (bool) parent::get();
}
}
6 changes: 3 additions & 3 deletions src/Field/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
use Corcel\Post;

/**
* Class DateTime
* Class DateTime.
*
* @package Corcel\Acf\Field
* @author Junior Grossi <[email protected]>
*/
class DateTime extends BasicField implements FieldInterface
Expand All @@ -21,7 +20,7 @@ class DateTime extends BasicField implements FieldInterface

/**
* @param string $fieldName
* @param Post $post
* @param Post $post
*/
public function process($fieldName, Post $post)
{
Expand All @@ -40,6 +39,7 @@ public function get()

/**
* @param string $dateString
*
* @return string
*/
protected function getDateFormatFromString($dateString)
Expand Down
5 changes: 2 additions & 3 deletions src/Field/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
use Corcel\Post;

/**
* Class File
* Class File.
*
* @package Corcel\Acf\Field
* @author Junior Grossi <[email protected]>
*/
class File extends BasicField implements FieldInterface
Expand Down Expand Up @@ -45,7 +44,7 @@ class File extends BasicField implements FieldInterface

/**
* @param string $field
* @param Post $post
* @param Post $post
*/
public function process($field, Post $post)
{
Expand Down
3 changes: 1 addition & 2 deletions src/Field/Gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
use Illuminate\Support\Collection;

/**
* Class Gallery
* Class Gallery.
*
* @package Corcel\Acf\Field
* @author Junior Grossi <[email protected]>
*/
class Gallery extends Image implements FieldInterface
Expand Down
9 changes: 6 additions & 3 deletions src/Field/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
use Illuminate\Database\Eloquent\Collection;

/**
* Class Image
* Class Image.
*
* @package Corcel\Acf\Field
* @author Junior Grossi
*/
class Image extends BasicField implements FieldInterface
Expand Down Expand Up @@ -56,7 +55,7 @@ class Image extends BasicField implements FieldInterface

/**
* @param string $field
* @param Post $post
* @param Post $post
*/
public function process($field, Post $post)
{
Expand Down Expand Up @@ -89,6 +88,7 @@ protected function fillFields(Post $attachment)

/**
* @param string $size
*
* @return Image
*/
public function size($size)
Expand All @@ -102,6 +102,7 @@ public function size($size)

/**
* @param array $data
*
* @return Image
*/
protected function fillThumbnailFields(array $data)
Expand All @@ -117,6 +118,7 @@ protected function fillThumbnailFields(array $data)

/**
* @param Post $attachment
*
* @return array
*/
protected function fetchMetadataValue(Post $attachment)
Expand All @@ -130,6 +132,7 @@ protected function fetchMetadataValue(Post $attachment)

/**
* @param Collection $attachments
*
* @return Collection
*/
protected function fetchMultipleMetadataValues(Collection $attachments)
Expand Down
3 changes: 1 addition & 2 deletions src/Field/PageLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
use Corcel\Acf\FieldInterface;

/**
* Class PageLink
* Class PageLink.
*
* @package Corcel\Acf\Field
* @author Junior Grossi <[email protected]>
*/
class PageLink extends PostObject implements FieldInterface
Expand Down
5 changes: 2 additions & 3 deletions src/Field/PostObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
use Corcel\Post;

/**
* Class PostObject
* Class PostObject.
*
* @package Corcel\Acf\Field
* @author Junior Grossi <[email protected]>
*/
class PostObject extends BasicField implements FieldInterface
Expand All @@ -20,7 +19,7 @@ class PostObject extends BasicField implements FieldInterface

/**
* @param string $fieldName
* @param Post $post
* @param Post $post
*/
public function process($fieldName, Post $post)
{
Expand Down
Loading

0 comments on commit 4feb056

Please sign in to comment.