3
3
namespace Corcel \Acf \Field ;
4
4
5
5
use Corcel \Post ;
6
+ use Corcel \Model ;
6
7
use Corcel \PostMeta ;
8
+ use Corcel \Term ;
9
+ use Corcel \TermMeta ;
7
10
8
11
/**
9
12
* Class BasicField.
13
16
abstract class BasicField
14
17
{
15
18
/**
16
- * @var Post
19
+ * @var Model
17
20
*/
18
21
protected $ post ;
19
22
@@ -52,10 +55,16 @@ abstract class BasicField
52
55
*
53
56
* @param Post $post
54
57
*/
55
- public function __construct (Post $ post )
58
+ public function __construct (Model $ post )
56
59
{
57
60
$ 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
+
59
68
$ this ->postMeta ->setConnection ($ post ->getConnectionName ());
60
69
}
61
70
@@ -68,9 +77,9 @@ public function __construct(Post $post)
68
77
*/
69
78
public function fetchValue ($ field )
70
79
{
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 ();
74
83
75
84
if (isset ($ postMeta ->meta_value ) and ! is_null ($ postMeta ->meta_value )) {
76
85
$ value = $ postMeta ->meta_value ;
@@ -95,7 +104,7 @@ public function fetchFieldKey($fieldName)
95
104
{
96
105
$ this ->name = $ fieldName ;
97
106
98
- $ postMeta = $ this ->postMeta ->where (' post_id ' , $ this ->post ->ID )
107
+ $ postMeta = $ this ->postMeta ->where ($ this -> getKeyName () , $ this ->post ->getKey () )
99
108
->where ('meta_key ' , '_ ' . $ fieldName )
100
109
->first ();
101
110
@@ -115,10 +124,11 @@ public function fetchFieldKey($fieldName)
115
124
*/
116
125
public function fetchFieldType ($ fieldKey )
117
126
{
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 ();
122
132
123
133
if ($ post ) {
124
134
$ fieldData = unserialize ($ post ->post_content );
@@ -130,6 +140,21 @@ public function fetchFieldType($fieldKey)
130
140
return null ;
131
141
}
132
142
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
+
133
158
/**
134
159
* @return mixed
135
160
*/
0 commit comments