Skip to content

Commit

Permalink
Merge pull request #28 from corcel/develop
Browse files Browse the repository at this point in the history
Release 0.1.9
  • Loading branch information
jgrossi authored Apr 13, 2017
2 parents 19d9ce3 + e300dbf commit 4afe61b
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 31 deletions.
8 changes: 4 additions & 4 deletions src/AdvancedCustomFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Corcel\Acf;

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

/**
* Class AdvancedCustomFields.
Expand All @@ -13,14 +13,14 @@
class AdvancedCustomFields
{
/**
* @var Post
* @var mixed
*/
protected $post;

/**
* @param Post $post
* @param mixed $post
*/
public function __construct(Post $post)
public function __construct(Model $post)
{
$this->post = $post;
}
Expand Down
47 changes: 36 additions & 11 deletions src/Field/BasicField.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
namespace Corcel\Acf\Field;

use Corcel\Post;
use Corcel\Model;
use Corcel\PostMeta;
use Corcel\Term;
use Corcel\TermMeta;

/**
* Class BasicField.
Expand All @@ -13,7 +16,7 @@
abstract class BasicField
{
/**
* @var Post
* @var Model
*/
protected $post;

Expand Down Expand Up @@ -52,10 +55,16 @@ abstract class BasicField
*
* @param Post $post
*/
public function __construct(Post $post)
public function __construct(Model $post)
{
$this->post = $post;
$this->postMeta = new PostMeta();

if ($post instanceof Post) {
$this->postMeta = new PostMeta();
} elseif ($post instanceof Term) {
$this->postMeta = new TermMeta();
}

$this->postMeta->setConnection($post->getConnectionName());
}

Expand All @@ -68,9 +77,9 @@ public function __construct(Post $post)
*/
public function fetchValue($field)
{
$postMeta = $this->postMeta->where('post_id', $this->post->ID)
->where('meta_key', $field)
->first();
$postMeta = $this->postMeta->where(
$this->getKeyName(), $this->post->getKey()
)->where('meta_key', $field)->first();

if (isset($postMeta->meta_value) and ! is_null($postMeta->meta_value)) {
$value = $postMeta->meta_value;
Expand All @@ -95,7 +104,7 @@ public function fetchFieldKey($fieldName)
{
$this->name = $fieldName;

$postMeta = $this->postMeta->where('post_id', $this->post->ID)
$postMeta = $this->postMeta->where($this->getKeyName(), $this->post->getKey())
->where('meta_key', '_' . $fieldName)
->first();

Expand All @@ -115,10 +124,11 @@ public function fetchFieldKey($fieldName)
*/
public function fetchFieldType($fieldKey)
{
$post = $this->post->orWhere(function ($query) use ($fieldKey) {
$query->where('post_name', $fieldKey);
$query->where('post_type', 'acf-field');
})->first();
$post = Post::on($this->post->getConnectionName())
->orWhere(function ($query) use ($fieldKey) {
$query->where('post_name', $fieldKey);
$query->where('post_type', 'acf-field');
})->first();

if ($post) {
$fieldData = unserialize($post->post_content);
Expand All @@ -130,6 +140,21 @@ public function fetchFieldType($fieldKey)
return null;
}

/**
* Get the name of the key for the field.
*
* @return string
*/
public function getKeyName()
{
if ($this->post instanceof Post) {
return 'post_id';
} elseif ($this->post instanceof Term) {
return 'term_id';
}
}


/**
* @return mixed
*/
Expand Down
8 changes: 6 additions & 2 deletions src/Field/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ class File extends BasicField implements FieldInterface
public function process($field)
{
$value = $this->fetchValue($field);
$file = $this->post->find($value);
$this->fillFields($file);

$connection = $this->post->getConnectionName();

if ($file = Post::on($connection)->find(intval($value))) {
$this->fillFields($file);
}
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/Field/FlexibleContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Corcel\Acf\FieldFactory;
use Corcel\Acf\FieldInterface;
use Corcel\Post;
use Corcel\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;

Expand Down Expand Up @@ -72,12 +72,10 @@ protected function retrieveFieldName($metaKey, $fieldName, $id)
*
* @return mixed
*/
protected function fetchPostsMeta($fieldName, Post $post)
protected function fetchPostsMeta($fieldName, Model $post)
{
$builder = $this->postMeta->where('post_id', $post->ID);
$builder->where(function ($query) use ($fieldName) {
$query->orWhere('meta_key', 'like', "{$fieldName}_%");
});
$builder = $this->postMeta->where($this->getKeyName(), $this->post->getKey());
$builder->where('meta_key', 'like', "{$fieldName}_%");

return $builder;
}
Expand Down Expand Up @@ -114,6 +112,8 @@ protected function fetchFields($fieldName, Builder $builder)
$fields[$id]->fields->$name = $field->get();
}

ksort($fields);

return $fields;
}
}
13 changes: 7 additions & 6 deletions src/Field/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Corcel\Acf\Field;

use Corcel\Post;
use Corcel\PostMeta;
use Corcel\Acf\FieldInterface;
use Illuminate\Database\Eloquent\Collection;

Expand Down Expand Up @@ -59,14 +60,14 @@ class Image extends BasicField implements FieldInterface
public function process($field)
{
$attachmentId = $this->fetchValue($field);

$connection = $this->post->getConnectionName();

if ($attachment = Post::on($connection)->find(intval($attachmentId))) {
$this->fillFields($attachment);

$imageData = $this->fetchMetadataValue($attachment);

$this->fillMetadataFields($imageData);
}
}
Expand Down Expand Up @@ -126,9 +127,9 @@ protected function fillThumbnailFields(array $data)
*/
protected function fetchMetadataValue(Post $attachment)
{
$meta = $this->postMeta->where('post_id', $attachment->ID)
->where('meta_key', '_wp_attachment_metadata')
->first();
$meta = PostMeta::where('post_id', $attachment->ID)
->where('meta_key', '_wp_attachment_metadata')
->first();

return unserialize($meta->meta_value);
}
Expand Down
4 changes: 2 additions & 2 deletions src/FieldFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Corcel\Acf\Field\Term;
use Corcel\Acf\Field\Text;
use Corcel\Acf\Field\User;
use Corcel\Post;
use Corcel\Model;
use Illuminate\Support\Collection;

/**
Expand All @@ -36,7 +36,7 @@ private function __construct()
*
* @return FieldInterface|Collection|string
*/
public static function make($name, Post $post, $type = null)
public static function make($name, Model $post, $type = null)
{
if (null === $type) {
$fakeText = new Text($post);
Expand Down

0 comments on commit 4afe61b

Please sign in to comment.