diff --git a/src/AdvancedCustomFields.php b/src/AdvancedCustomFields.php index 2801104..48e0c03 100644 --- a/src/AdvancedCustomFields.php +++ b/src/AdvancedCustomFields.php @@ -3,7 +3,7 @@ namespace Corcel\Acf; use Corcel\Acf\Exception\MissingFieldNameException; -use Corcel\Post; +use Corcel\Model; /** * Class AdvancedCustomFields. @@ -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; } diff --git a/src/Field/BasicField.php b/src/Field/BasicField.php index 1888160..f5e32ea 100644 --- a/src/Field/BasicField.php +++ b/src/Field/BasicField.php @@ -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. @@ -13,7 +16,7 @@ abstract class BasicField { /** - * @var Post + * @var Model */ protected $post; @@ -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()); } @@ -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; @@ -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(); @@ -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); @@ -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 */ diff --git a/src/Field/File.php b/src/Field/File.php index 7c03259..3d4c006 100644 --- a/src/Field/File.php +++ b/src/Field/File.php @@ -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); + } } /** diff --git a/src/Field/FlexibleContent.php b/src/Field/FlexibleContent.php index b207f9c..2b13389 100644 --- a/src/Field/FlexibleContent.php +++ b/src/Field/FlexibleContent.php @@ -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; @@ -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; } @@ -114,6 +112,8 @@ protected function fetchFields($fieldName, Builder $builder) $fields[$id]->fields->$name = $field->get(); } + ksort($fields); + return $fields; } } diff --git a/src/Field/Image.php b/src/Field/Image.php index 0dc2035..87cd963 100644 --- a/src/Field/Image.php +++ b/src/Field/Image.php @@ -3,6 +3,7 @@ namespace Corcel\Acf\Field; use Corcel\Post; +use Corcel\PostMeta; use Corcel\Acf\FieldInterface; use Illuminate\Database\Eloquent\Collection; @@ -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); } } @@ -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); } diff --git a/src/FieldFactory.php b/src/FieldFactory.php index a4cbde2..13d881f 100644 --- a/src/FieldFactory.php +++ b/src/FieldFactory.php @@ -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; /** @@ -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);