Skip to content

Commit 5c8ec93

Browse files
committed
cnahged receiving fields methods
1 parent 4114300 commit 5c8ec93

File tree

3 files changed

+31
-79
lines changed

3 files changed

+31
-79
lines changed

framework/Objects/Meta.php

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ abstract class Meta {
3030
const PLUGIN_ACF = 'advanced-custom-fields';
3131

3232
/**
33-
* Current page/term object. It can be WP_Post or WP_Term
33+
* Internal cache for post custom fields data
3434
*
35-
* @var object
35+
* @var array
3636
*/
37-
public $object;
37+
protected $_fields = [];
3838

3939
/**
4040
* Meta constructor.
@@ -45,8 +45,6 @@ public function __construct() {
4545
} else {
4646
$this->custom_fields_plugin = self::PLUGIN_JCF;
4747
}
48-
49-
return $this->custom_fields_plugin;
5048
}
5149

5250
/**
@@ -55,11 +53,10 @@ public function __construct() {
5553
* Do not call this method directly as it is a PHP magic method that
5654
* will be implicitly called when executing `$value = $object->property;`.
5755
*
58-
* @param string $name The property name.
56+
* @param $name the property name.
5957
*
60-
* @return mixed the property value
61-
* @throws \Exception Property is not defined.
62-
* @see __set()
58+
* @return mixed
59+
* @throws \Exception
6360
*/
6461
public function __get( $name ) {
6562

@@ -83,51 +80,37 @@ public function __isset( $name ) {
8380
return null !== $this->get_field($name);
8481
}
8582

86-
/**
87-
* Sets an object property to null.
88-
*
89-
* Do not call this method directly as it is a PHP magic method that
90-
* will be implicitly called when executing `unset($object->property)`.
91-
*
92-
* Note that if the property is not defined, this method will do nothing.
93-
* If the property is read-only, it will throw an exception.
94-
*
95-
* @param string $name The property name.
96-
*
97-
* @throws \Exception The property is read only.
98-
*/
99-
public function __unset( $name ) {
100-
$setter = 'set_' . $name;
101-
if ( method_exists( $this, $setter ) ) {
102-
$this->$setter( null );
103-
} elseif ( method_exists( $this, 'get_' . $name ) ) {
104-
throw new \Exception( 'Setting read-only property: ' . get_class( $this ) . '::' . $name );
105-
}
106-
}
107-
10883
/**
10984
* Getter of postmeta from just custom fields
11085
*
11186
* @param string $field_name Field name to get.
112-
* @param int $post_id Post ID if different from get_the_ID.
87+
* @param int $object_id Post/Term ID
11388
* @param bool|string $format_value Format value or not.
11489
*
11590
* @return mixed
11691
* @throws \Exception Unsupported custom fields plugin.
11792
*/
118-
abstract public function get_field_jcf( $field_name, $post_id, $format_value );
93+
abstract public function get_field_jcf( $field_name, $object_id, $format_value );
11994

12095
/**
12196
* Getter of postmeta from advanced custom fields
12297
*
12398
* @param string $field_name Field name to get.
124-
* @param int $post_id Post ID if different from get_the_ID.
99+
* @param int $object_id Post/Term ID
125100
* @param bool|string $format_value Format value or not.
126101
*
127102
* @return mixed
128103
* @throws \Exception Unsupported custom fields plugin.
129104
*/
130-
abstract public function get_field_acf( $field_name, $post_id, $format_value );
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();
131114

132115
/**
133116
* Main post meta fields getter function.
@@ -141,10 +124,8 @@ abstract public function get_field_acf( $field_name, $post_id, $format_value );
141124
*/
142125
public function get_field( $field_name, $post_id = null, $format_value = true ) {
143126

144-
if ( is_a( $this->object, 'WP_Post' ) ) {
145-
$post_id = $this->object->ID;
146-
} else {
147-
$post_id = $this->object->term_id;
127+
if ( empty( $post_id ) ) {
128+
$post_id = $this->get_id();
148129
}
149130

150131
// Check cache, if not exists - get field value.

framework/Objects/Postmeta.php

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,21 @@
1111
*/
1212
class Postmeta extends Meta {
1313

14-
/**
15-
* Internal cache for post custom fields data
16-
*
17-
* @var array
18-
*/
19-
protected $_fields = [];
20-
2114
/**
2215
* Postmeta constructor.
2316
*/
2417
public function __construct() {
2518
parent::__construct();
26-
27-
// set current post for new created instance.
28-
$this->set_post( null );
2919
}
3020

3121
/**
32-
* Set $post property correctly
22+
* Get post id
3323
*
34-
* @param \WP_Post|int|null $post Post object, id or null to take current object.
24+
* @return false|int
3525
*/
36-
protected function set_post( $post = null ) {
37-
if ( is_null( $post ) ) {
38-
$post = get_the_ID();
39-
}
40-
$this->object = get_post( $post );
26+
public function get_id() {
27+
28+
return get_queried_object_id();
4129
}
4230

4331
/**

framework/Objects/Termmeta.php

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,21 @@
1111
*/
1212
class Termmeta extends Meta {
1313

14-
/**
15-
* Internal cache for post custom fields data
16-
*
17-
* @var array
18-
*/
19-
protected $_fields = [];
20-
2114
/**
2215
* Postmeta constructor.
2316
*/
2417
public function __construct() {
2518
parent::__construct();
26-
27-
// set current post for new created instance.
28-
$this->set_term( null );
2919
}
3020

3121
/**
32-
* Set $term property correctly
22+
* Get post id
3323
*
34-
* @param \WP_Term|int|null $term Term object, id or null to take current object.
24+
* @return false|int
3525
*/
36-
protected function set_term( $term = null ) {
37-
if ( is_null( $term ) ) {
38-
$term = get_queried_object();
39-
}
40-
$this->object = $term;
26+
public function get_id() {
27+
28+
return get_queried_object_id();
4129
}
4230

4331
/**
@@ -51,12 +39,7 @@ protected function set_term( $term = null ) {
5139
* @throws \Exception Unsupported custom fields plugin.
5240
*/
5341
public function get_field_acf( $field_name, $term_id, $format_value ) {
54-
if ( $term_id ) {
55-
$option_name = 'category_' . $term_id . '_' . $field_name;
56-
return get_option( $option_name );
57-
} else {
58-
return false;
59-
}
42+
return get_field( $field_name, get_queried_object() );
6043
}
6144

6245
/**

0 commit comments

Comments
 (0)