88
99abstract class Meta {
1010
11+ /**
12+ * JCF Plugin name
13+ */
14+ const PLUGIN_JCF = 'just-custom-fields ' ;
15+
16+ /**
17+ * ACF Plugin name
18+ */
19+ const PLUGIN_ACF = 'advanced-custom-fields ' ;
20+
21+ /**
22+ * Object with meta data.
23+ *
24+ * @var \WP_Post|\WP_Term
25+ */
26+ public $ object ;
27+
1128 /**
1229 * Plugin used for extending of post custom fields.
1330 *
@@ -20,27 +37,17 @@ abstract class Meta {
2037 public $ custom_fields_plugin = 'just-custom-fields ' ;
2138
2239 /**
23- * JCF Plugin name
24- */
25- const PLUGIN_JCF = 'just-custom-fields ' ;
26-
27- /**
28- * ACF Plugin name
29- */
30- const PLUGIN_ACF = 'advanced-custom-fields ' ;
31-
32- /**
33- * Internal cache for post custom fields data
40+ * Internal cache for object meta data
3441 *
3542 * @var array
3643 */
37- protected $ _fields = [];
44+ protected static $ _meta = [];
3845
3946 /**
4047 * Meta constructor.
4148 */
4249 public function __construct () {
43- if ( class_exists ('acf ' ) ) {
50+ if ( class_exists ( 'acf ' ) ) {
4451 $ this ->custom_fields_plugin = self ::PLUGIN_ACF ;
4552 } else {
4653 $ this ->custom_fields_plugin = self ::PLUGIN_JCF ;
@@ -53,14 +60,13 @@ public function __construct() {
5360 * Do not call this method directly as it is a PHP magic method that
5461 * will be implicitly called when executing `$value = $object->property;`.
5562 *
56- * @param $name the property name.
63+ * @param string $name a property name.
5764 *
5865 * @return mixed
59- * @throws \Exception
66+ * @throws \Exception Unable to get property.
6067 */
6168 public function __get ( $ name ) {
62-
63- return $ this ->get_field ( $ name );
69+ return $ this ->get_value ( $ name );
6470 }
6571
6672 /**
@@ -76,72 +82,71 @@ public function __get( $name ) {
7682 * @return boolean Whether the named property is set (not null).
7783 */
7884 public function __isset ( $ name ) {
79-
80- return null !== $ this ->get_field ($ name );
85+ return null !== $ this ->get_value ( $ name );
8186 }
8287
88+ /**
89+ * Get object id
90+ * It can be post or term
91+ *
92+ * @return mixed
93+ */
94+ abstract public function get_object_id ();
95+
8396 /**
8497 * Getter of postmeta from just custom fields
8598 *
8699 * @param string $field_name Field name to get.
87- * @param int $object_id Post/Term ID
100+ * @param int $object_id Post/Term ID.
88101 * @param bool|string $format_value Format value or not.
89102 *
90103 * @return mixed
91104 * @throws \Exception Unsupported custom fields plugin.
92105 */
93- abstract public function get_field_jcf ( $ field_name , $ object_id , $ format_value );
106+ abstract public function get_value_jcf ( $ field_name , $ object_id , $ format_value );
94107
95108 /**
96109 * Getter of postmeta from advanced custom fields
97110 *
98111 * @param string $field_name Field name to get.
99- * @param int $object_id Post/Term ID
112+ * @param int $object_id Post/Term ID.
100113 * @param bool|string $format_value Format value or not.
101114 *
102115 * @return mixed
103116 * @throws \Exception Unsupported custom fields plugin.
104117 */
105- abstract public function get_field_acf ( $ field_name , $ object_id , $ format_value );
106-
107- /**
108- * Get id of entity
109- * It can be post or term
110- *
111- * @return mixed
112- */
113- abstract public function get_id ();
118+ abstract public function get_value_acf ( $ field_name , $ object_id , $ format_value );
114119
115120 /**
116121 * Main post meta fields getter function.
117122 *
118123 * @param string $field_name Field name to get.
119- * @param int $post_id Post ID if different from get_the_ID.
124+ * @param int $object_id Post ID if different from get_the_ID.
120125 * @param bool|string $format_value Format value or not.
121126 *
122127 * @return mixed
123128 * @throws \Exception Unsupported custom fields plugin.
124129 */
125- public function get_field ( $ field_name , $ post_id = null , $ format_value = true ) {
130+ public function get_value ( $ field_name , $ object_id = null , $ format_value = true ) {
126131
127- if ( empty ( $ post_id ) ) {
128- $ post_id = $ this ->get_id ();
132+ if ( empty ( $ object_id ) ) {
133+ $ object_id = $ this ->get_object_id ();
129134 }
130135
131- // Check cache, if not exists - get field value.
132- if ( ! isset ( $ this ->_fields [ $ post_id ][ $ field_name ] ) ) {
136+ // Check cache, if not exists - get meta value.
137+ if ( ! isset ( $ this ->_meta [ $ object_id ][ $ field_name ] ) ) {
133138 if ( self ::PLUGIN_JCF === $ this ->custom_fields_plugin ) {
134- $ value = $ this ->get_field_jcf ( $ field_name , $ post_id , $ format_value );
139+ $ value = $ this ->get_value_jcf ( $ field_name , $ object_id , $ format_value );
135140 } elseif ( self ::PLUGIN_ACF === $ this ->custom_fields_plugin ) {
136- $ value = $ this ->get_field_acf ( $ field_name , $ post_id , $ format_value );
141+ $ value = $ this ->get_value_acf ( $ field_name , $ object_id , $ format_value );
137142 } else {
138- throw new \Exception ( get_class ( $ this ) . "::get_field () : Unsupported custom fields plugin \"{$ this ->custom_fields_plugin }\"" );
143+ throw new \Exception ( get_class ( $ this ) . "::get_value () : Unsupported custom fields plugin \"{$ this ->custom_fields_plugin }\"" );
139144 }
140145
141- $ this ->_fields [ $ post_id ][ $ field_name ] = $ value ;
146+ $ this ->_meta [ $ object_id ][ $ field_name ] = $ value ;
142147 }
143148
144- return $ this ->_fields [ $ post_id ][ $ field_name ];
149+ return $ this ->_meta [ $ object_id ][ $ field_name ];
145150 }
146151
147152}
0 commit comments