Skip to content

Commit e300dbf

Browse files
authored
Merge pull request #27 from WP4Laravel/master
Make ACF compatible with terms
2 parents a190042 + 116941d commit e300dbf

File tree

6 files changed

+61
-31
lines changed

6 files changed

+61
-31
lines changed

src/AdvancedCustomFields.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Corcel\Acf;
44

55
use Corcel\Acf\Exception\MissingFieldNameException;
6-
use Corcel\Post;
6+
use Corcel\Model;
77

88
/**
99
* Class AdvancedCustomFields.
@@ -13,14 +13,14 @@
1313
class AdvancedCustomFields
1414
{
1515
/**
16-
* @var Post
16+
* @var mixed
1717
*/
1818
protected $post;
1919

2020
/**
21-
* @param Post $post
21+
* @param mixed $post
2222
*/
23-
public function __construct(Post $post)
23+
public function __construct(Model $post)
2424
{
2525
$this->post = $post;
2626
}

src/Field/BasicField.php

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
namespace Corcel\Acf\Field;
44

55
use Corcel\Post;
6+
use Corcel\Model;
67
use Corcel\PostMeta;
8+
use Corcel\Term;
9+
use Corcel\TermMeta;
710

811
/**
912
* Class BasicField.
@@ -13,7 +16,7 @@
1316
abstract class BasicField
1417
{
1518
/**
16-
* @var Post
19+
* @var Model
1720
*/
1821
protected $post;
1922

@@ -52,10 +55,16 @@ abstract class BasicField
5255
*
5356
* @param Post $post
5457
*/
55-
public function __construct(Post $post)
58+
public function __construct(Model $post)
5659
{
5760
$this->post = $post;
58-
$this->postMeta = new PostMeta();
61+
62+
if ($post instanceof Post) {
63+
$this->postMeta = new PostMeta();
64+
} elseif ($post instanceof Term) {
65+
$this->postMeta = new TermMeta();
66+
}
67+
5968
$this->postMeta->setConnection($post->getConnectionName());
6069
}
6170

@@ -68,9 +77,9 @@ public function __construct(Post $post)
6877
*/
6978
public function fetchValue($field)
7079
{
71-
$postMeta = $this->postMeta->where('post_id', $this->post->ID)
72-
->where('meta_key', $field)
73-
->first();
80+
$postMeta = $this->postMeta->where(
81+
$this->getKeyName(), $this->post->getKey()
82+
)->where('meta_key', $field)->first();
7483

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

98-
$postMeta = $this->postMeta->where('post_id', $this->post->ID)
107+
$postMeta = $this->postMeta->where($this->getKeyName(), $this->post->getKey())
99108
->where('meta_key', '_' . $fieldName)
100109
->first();
101110

@@ -115,10 +124,11 @@ public function fetchFieldKey($fieldName)
115124
*/
116125
public function fetchFieldType($fieldKey)
117126
{
118-
$post = $this->post->orWhere(function ($query) use ($fieldKey) {
119-
$query->where('post_name', $fieldKey);
120-
$query->where('post_type', 'acf-field');
121-
})->first();
127+
$post = Post::on($this->post->getConnectionName())
128+
->orWhere(function ($query) use ($fieldKey) {
129+
$query->where('post_name', $fieldKey);
130+
$query->where('post_type', 'acf-field');
131+
})->first();
122132

123133
if ($post) {
124134
$fieldData = unserialize($post->post_content);
@@ -130,6 +140,21 @@ public function fetchFieldType($fieldKey)
130140
return null;
131141
}
132142

143+
/**
144+
* Get the name of the key for the field.
145+
*
146+
* @return string
147+
*/
148+
public function getKeyName()
149+
{
150+
if ($this->post instanceof Post) {
151+
return 'post_id';
152+
} elseif ($this->post instanceof Term) {
153+
return 'term_id';
154+
}
155+
}
156+
157+
133158
/**
134159
* @return mixed
135160
*/

src/Field/File.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,12 @@ class File extends BasicField implements FieldInterface
4848
public function process($field)
4949
{
5050
$value = $this->fetchValue($field);
51-
$file = $this->post->find($value);
52-
$this->fillFields($file);
51+
52+
$connection = $this->post->getConnectionName();
53+
54+
if ($file = Post::on($connection)->find(intval($value))) {
55+
$this->fillFields($file);
56+
}
5357
}
5458

5559
/**

src/Field/FlexibleContent.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Corcel\Acf\FieldFactory;
66
use Corcel\Acf\FieldInterface;
7-
use Corcel\Post;
7+
use Corcel\Model;
88
use Illuminate\Database\Eloquent\Builder;
99
use Illuminate\Support\Collection;
1010

@@ -72,12 +72,10 @@ protected function retrieveFieldName($metaKey, $fieldName, $id)
7272
*
7373
* @return mixed
7474
*/
75-
protected function fetchPostsMeta($fieldName, Post $post)
75+
protected function fetchPostsMeta($fieldName, Model $post)
7676
{
77-
$builder = $this->postMeta->where('post_id', $post->ID);
78-
$builder->where(function ($query) use ($fieldName) {
79-
$query->orWhere('meta_key', 'like', "{$fieldName}_%");
80-
});
77+
$builder = $this->postMeta->where($this->getKeyName(), $this->post->getKey());
78+
$builder->where('meta_key', 'like', "{$fieldName}_%");
8179

8280
return $builder;
8381
}
@@ -114,6 +112,8 @@ protected function fetchFields($fieldName, Builder $builder)
114112
$fields[$id]->fields->$name = $field->get();
115113
}
116114

115+
ksort($fields);
116+
117117
return $fields;
118118
}
119119
}

src/Field/Image.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Corcel\Acf\Field;
44

55
use Corcel\Post;
6+
use Corcel\PostMeta;
67
use Corcel\Acf\FieldInterface;
78
use Illuminate\Database\Eloquent\Collection;
89

@@ -59,14 +60,14 @@ class Image extends BasicField implements FieldInterface
5960
public function process($field)
6061
{
6162
$attachmentId = $this->fetchValue($field);
62-
63+
6364
$connection = $this->post->getConnectionName();
64-
65+
6566
if ($attachment = Post::on($connection)->find(intval($attachmentId))) {
6667
$this->fillFields($attachment);
6768

6869
$imageData = $this->fetchMetadataValue($attachment);
69-
70+
7071
$this->fillMetadataFields($imageData);
7172
}
7273
}
@@ -126,9 +127,9 @@ protected function fillThumbnailFields(array $data)
126127
*/
127128
protected function fetchMetadataValue(Post $attachment)
128129
{
129-
$meta = $this->postMeta->where('post_id', $attachment->ID)
130-
->where('meta_key', '_wp_attachment_metadata')
131-
->first();
130+
$meta = PostMeta::where('post_id', $attachment->ID)
131+
->where('meta_key', '_wp_attachment_metadata')
132+
->first();
132133

133134
return unserialize($meta->meta_value);
134135
}

src/FieldFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Corcel\Acf\Field\Term;
1616
use Corcel\Acf\Field\Text;
1717
use Corcel\Acf\Field\User;
18-
use Corcel\Post;
18+
use Corcel\Model;
1919
use Illuminate\Support\Collection;
2020

2121
/**
@@ -36,7 +36,7 @@ private function __construct()
3636
*
3737
* @return FieldInterface|Collection|string
3838
*/
39-
public static function make($name, Post $post, $type = null)
39+
public static function make($name, Model $post, $type = null)
4040
{
4141
if (null === $type) {
4242
$fakeText = new Text($post);

0 commit comments

Comments
 (0)