Skip to content

Commit 5404b87

Browse files
committed
Final refactoring
1 parent 5c8ec93 commit 5404b87

File tree

5 files changed

+109
-90
lines changed

5 files changed

+109
-90
lines changed

framework/Objects/Meta.php

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,23 @@
88

99
abstract 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
}

framework/Objects/Model.php

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,6 @@
4141
*/
4242
class Model {
4343

44-
/**
45-
* Current $post object
46-
*
47-
* @var \WP_Post
48-
*/
49-
public $post;
50-
5144
/**
5245
* Internal cache of wp queries
5346
*
@@ -59,8 +52,6 @@ class Model {
5952
* Model constructor.
6053
*/
6154
public function __construct() {
62-
// set current post for new created instance.
63-
$this->set_post( null );
6455
}
6556

6657
/**
@@ -143,13 +134,13 @@ public function __isset( $name ) {
143134
* @throws \Exception The property is read only.
144135
*/
145136
public function __unset( $name ) {
146-
$setter = 'set_' . $name;
147-
if ( method_exists( $this, $setter ) ) {
148-
$this->$setter( null );
149-
} elseif ( method_exists( $this, 'get_' . $name ) ) {
150-
throw new \Exception( 'Setting read-only property: ' . get_class( $this ) . '::' . $name );
151-
}
152-
}
137+
$setter = 'set_' . $name;
138+
if ( method_exists( $this, $setter ) ) {
139+
$this->$setter( null );
140+
} elseif ( method_exists( $this, 'get_' . $name ) ) {
141+
throw new \Exception( 'Setting read-only property: ' . get_class( $this ) . '::' . $name );
142+
}
143+
}
153144

154145
/**
155146
* Run query and remember it to cache
@@ -198,16 +189,4 @@ public function reset_queries() {
198189
$this->_queries = [];
199190
}
200191

201-
/**
202-
* Set $post property correctly
203-
*
204-
* @param \WP_Post|int|null $post Post object, id or null to take current object.
205-
*/
206-
public function set_post( $post = null ) {
207-
if ( is_null( $post ) ) {
208-
$post = get_the_ID();
209-
}
210-
$this->post = get_post( $post );
211-
}
212-
213192
}

framework/Objects/Postmeta.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,45 @@
33
namespace JustCoded\WP\Framework\Objects;
44

55
/**
6-
* Get fields from posts
7-
*
86
* Class Postmeta
9-
*
10-
* @package JustCoded\WP\Framework\Objects
7+
* Get meta fields from posts
118
*/
129
class Postmeta extends Meta {
1310

11+
/**
12+
* Current Object to get meta data from.
13+
*
14+
* @var \WP_Post
15+
*/
16+
public $object;
17+
1418
/**
1519
* Postmeta constructor.
1620
*/
1721
public function __construct() {
1822
parent::__construct();
23+
$this->the_post();
24+
}
25+
26+
/**
27+
* Set $object property correctly
28+
*
29+
* @param \WP_Post|int|null $post Post object, id or null to take current object.
30+
*/
31+
public function the_post( $post = null ) {
32+
if ( is_null( $post ) ) {
33+
$post = get_the_ID();
34+
}
35+
$this->object = get_post( $post );
1936
}
2037

2138
/**
2239
* Get post id
2340
*
2441
* @return false|int
2542
*/
26-
public function get_id() {
27-
28-
return get_queried_object_id();
43+
public function get_object_id() {
44+
return $this->object->ID;
2945
}
3046

3147
/**
@@ -38,7 +54,7 @@ public function get_id() {
3854
* @return mixed
3955
* @throws \Exception Unsupported custom fields plugin.
4056
*/
41-
public function get_field_acf( $field_name, $post_id, $format_value ) {
57+
public function get_value_acf( $field_name, $post_id, $format_value ) {
4258
return get_field( $field_name, $post_id, $format_value );
4359
}
4460

@@ -52,8 +68,8 @@ public function get_field_acf( $field_name, $post_id, $format_value ) {
5268
* @return mixed
5369
* @throws \Exception Unsupported custom fields plugin.
5470
*/
55-
public function get_field_jcf( $field_name, $post_id, $format_value ) {
56-
// Fix for getter from magic property field_*.
71+
public function get_value_jcf( $field_name, $post_id, $format_value ) {
72+
// Fix for getter from magic property _*.
5773
if ( strpos( $field_name, '_' ) !== 0 ) {
5874
$field_name = "_{$field_name}";
5975
}

framework/Objects/Termmeta.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,66 @@
1111
*/
1212
class Termmeta extends Meta {
1313

14+
/**
15+
* Current Object to get meta data from.
16+
*
17+
* @var \WP_Term
18+
*/
19+
public $object;
20+
1421
/**
1522
* Postmeta constructor.
1623
*/
1724
public function __construct() {
1825
parent::__construct();
26+
$this->the_term( get_queried_object_id() );
27+
}
28+
29+
30+
/**
31+
* Set $object property correctly
32+
*
33+
* @param \WP_Term|int|null $term Term object, id or null to take current object.
34+
*/
35+
public function the_term( $term ) {
36+
$this->object = get_term( $term );
1937
}
2038

2139
/**
2240
* Get post id
2341
*
2442
* @return false|int
2543
*/
26-
public function get_id() {
27-
28-
return get_queried_object_id();
44+
public function get_object_id() {
45+
return $this->object->term_id;
2946
}
3047

3148
/**
3249
* Getter of postmeta from advanced custom fields
3350
*
3451
* @param string $field_name Field name to get.
35-
* @param int $term_id Term ID
52+
* @param int $term_id Term ID.
3653
* @param bool|string $format_value Format value or not.
3754
*
3855
* @return mixed
3956
* @throws \Exception Unsupported custom fields plugin.
4057
*/
41-
public function get_field_acf( $field_name, $term_id, $format_value ) {
42-
return get_field( $field_name, get_queried_object() );
58+
public function get_value_acf( $field_name, $term_id, $format_value ) {
59+
$term = ( $term_id === $this->get_object_id() ) ? $this->object : get_term( $term_id );
60+
return get_field( $field_name, $term );
4361
}
4462

4563
/**
46-
* Getter of postmeta from just custom fields
64+
* Getter of termmeta from just custom fields
4765
*
4866
* @param string $field_name Field name to get.
49-
* @param int $post_id Post ID if different from get_the_ID.
67+
* @param int $term_id Term ID.
5068
* @param bool|string $format_value Format value or not.
5169
*
5270
* @return mixed
5371
* @throws \Exception Unsupported custom fields plugin.
5472
*/
55-
public function get_field_jcf( $field_name, $term_id, $format_value ) {
73+
public function get_value_jcf( $field_name, $term_id, $format_value ) {
5674
// Fix for getter from magic property field_*.
5775
if ( strpos( $field_name, '_' ) !== 0 ) {
5876
$field_name = "_{$field_name}";

readme.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ To upgrade remove the old plugin folder. After than follow the installation step
9191

9292
== Changelog ==
9393

94-
= Version 2.1.0 - 15 December 2017 =
94+
= Version 2.1.0 - 5 January 2017 =
9595
* New: Updated View component and layouts wrapping rendering. Replaced layout_open()/layout_close() methods with extends() method.
96+
* New: Moved Model meta getter methods to separate Postmeta and Termmeta objects.
9697
= Version 2.0.1 - 27 November 2017 =
9798
* New: Removed theme hooks to patch WordPress-generated .htaccess (moved to starter package)
9899
= Version 2.0 - 21 November 2017 =

0 commit comments

Comments
 (0)