Skip to content

Commit 4a29497

Browse files
authored
Merge pull request #27 from justcoded/develop
Moved post meta getters from Model class to separate Postmeta / Termmeta classes
2 parents 3e25c1f + 035e9f4 commit 4a29497

File tree

6 files changed

+322
-109
lines changed

6 files changed

+322
-109
lines changed

framework/Objects/Meta.php

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
3+
namespace JustCoded\WP\Framework\Objects;
4+
5+
/**
6+
* Meta is the base class for acquiring data from custom fields.
7+
*/
8+
9+
abstract class Meta {
10+
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+
28+
/**
29+
* Plugin used for extending of post custom fields.
30+
*
31+
* Available values:
32+
* 'just-custom-fields'
33+
* 'advanced-custom-fields'
34+
*
35+
* @var string
36+
*/
37+
public $custom_fields_plugin = 'just-custom-fields';
38+
39+
/**
40+
* Internal cache for object meta data
41+
*
42+
* @var array
43+
*/
44+
protected static $_meta = [];
45+
46+
/**
47+
* Meta constructor.
48+
*/
49+
public function __construct() {
50+
if ( class_exists( 'acf' ) ) {
51+
$this->custom_fields_plugin = self::PLUGIN_ACF;
52+
} else {
53+
$this->custom_fields_plugin = self::PLUGIN_JCF;
54+
}
55+
}
56+
57+
/**
58+
* Returns the value of an object property.
59+
*
60+
* Do not call this method directly as it is a PHP magic method that
61+
* will be implicitly called when executing `$value = $object->property;`.
62+
*
63+
* @param string $name a property name.
64+
*
65+
* @return mixed
66+
* @throws \Exception Unable to get property.
67+
*/
68+
public function __get( $name ) {
69+
return $this->get_value( $name );
70+
}
71+
72+
/**
73+
* Checks if the named property is set (not null).
74+
*
75+
* Do not call this method directly as it is a PHP magic method that
76+
* will be implicitly called when executing `isset($object->property)`.
77+
*
78+
* Note that if the property is not defined, false will be returned.
79+
*
80+
* @param string $name The property name or the event name.
81+
*
82+
* @return boolean Whether the named property is set (not null).
83+
*/
84+
public function __isset( $name ) {
85+
return null !== $this->get_value( $name );
86+
}
87+
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+
96+
/**
97+
* Getter of postmeta from just custom fields
98+
*
99+
* @param string $field_name Field name to get.
100+
* @param int $object_id Post/Term ID.
101+
* @param bool|string $format_value Format value or not.
102+
*
103+
* @return mixed
104+
* @throws \Exception Unsupported custom fields plugin.
105+
*/
106+
abstract public function get_value_jcf( $field_name, $object_id, $format_value );
107+
108+
/**
109+
* Getter of postmeta from advanced custom fields
110+
*
111+
* @param string $field_name Field name to get.
112+
* @param int $object_id Post/Term ID.
113+
* @param bool|string $format_value Format value or not.
114+
*
115+
* @return mixed
116+
* @throws \Exception Unsupported custom fields plugin.
117+
*/
118+
abstract public function get_value_acf( $field_name, $object_id, $format_value );
119+
120+
/**
121+
* Main post meta fields getter function.
122+
*
123+
* @param string $field_name Field name to get.
124+
* @param int $object_id Post ID if different from get_the_ID.
125+
* @param bool|string $format_value Format value or not.
126+
*
127+
* @return mixed
128+
* @throws \Exception Unsupported custom fields plugin.
129+
*/
130+
public function get_value( $field_name, $object_id = null, $format_value = true ) {
131+
132+
if ( empty( $object_id ) ) {
133+
$object_id = $this->get_object_id();
134+
}
135+
136+
// Check cache, if not exists - get meta value.
137+
if ( ! isset( $this->_meta[ $object_id ][ $field_name ] ) ) {
138+
if ( self::PLUGIN_JCF === $this->custom_fields_plugin ) {
139+
$value = $this->get_value_jcf( $field_name, $object_id, $format_value );
140+
} elseif ( self::PLUGIN_ACF === $this->custom_fields_plugin ) {
141+
$value = $this->get_value_acf( $field_name, $object_id, $format_value );
142+
} else {
143+
throw new \Exception( get_class( $this ) . "::get_value() : Unsupported custom fields plugin \"{$this->custom_fields_plugin}\"" );
144+
}
145+
146+
$this->_meta[ $object_id ][ $field_name ] = $value;
147+
}
148+
149+
return $this->_meta[ $object_id ][ $field_name ];
150+
}
151+
152+
}

framework/Objects/Model.php

Lines changed: 0 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,6 @@
4040
* to modify the property value will cause an exception.
4141
*/
4242
class Model {
43-
/**
44-
* Plugin used for extending of post custom fields.
45-
*
46-
* Available values:
47-
* 'just-custom-fields'
48-
* 'advanced-custom-fields'
49-
*
50-
* @var string
51-
*/
52-
public $custom_fields_plugin = 'just-custom-fields';
53-
54-
/**
55-
* Current $post object
56-
*
57-
* @var \WP_Post
58-
*/
59-
public $post;
6043

6144
/**
6245
* Internal cache of wp queries
@@ -65,19 +48,10 @@ class Model {
6548
*/
6649
private $_queries = [];
6750

68-
/**
69-
* Internal cache for post custom fields data
70-
*
71-
* @var array
72-
*/
73-
protected $_fields = [];
74-
7551
/**
7652
* Model constructor.
7753
*/
7854
public function __construct() {
79-
// set current post for new created instance.
80-
$this->set_post( null );
8155
}
8256

8357
/**
@@ -93,12 +67,6 @@ public function __construct() {
9367
* @see __set()
9468
*/
9569
public function __get( $name ) {
96-
// getter for magic property $this->field_*.
97-
if ( strpos( $name, 'field_' ) === 0 ) {
98-
$field_name = preg_replace( '/^field_/', '', $name );
99-
return $this->get_field( $field_name );
100-
}
101-
10270
// trying to find get_{property} method.
10371
$getter = 'get_' . $name;
10472
if ( method_exists( $this, $getter ) ) {
@@ -221,80 +189,4 @@ public function reset_queries() {
221189
$this->_queries = [];
222190
}
223191

224-
/**
225-
* Set $post property correctly
226-
*
227-
* @param \WP_Post|int|null $post Post object, id or null to take current object.
228-
*/
229-
public function set_post( $post = null ) {
230-
if ( is_null( $post ) ) {
231-
$post = get_the_ID();
232-
}
233-
$this->post = get_post( $post );
234-
}
235-
236-
/**
237-
* Main post meta fields getter function.
238-
*
239-
* @param string $field_name Field name to get.
240-
* @param int $post_id Post ID if different from get_the_ID.
241-
* @param bool|string $format_value Format value or not.
242-
*
243-
* @return mixed
244-
* @throws \Exception Unsupported custom fields plugin.
245-
*/
246-
public function get_field( $field_name, $post_id = null, $format_value = true ) {
247-
248-
// Use $this->post in case ID is empty.
249-
if ( empty( $post_id ) && ! empty( $this->post ) ) {
250-
$post_id = $this->post->ID;
251-
}
252-
253-
// Check cache, if not exists - get field value.
254-
if ( ! isset( $this->_fields[ $post_id ][ $field_name ] ) ) {
255-
if ( 'just-custom-fields' === $this->custom_fields_plugin ) {
256-
$value = $this->get_field_jcf( $field_name, $post_id, $format_value );
257-
} elseif ( 'advanced-custom-fields' === $this->custom_fields_plugin ) {
258-
$value = $this->get_field_acf( $field_name, $post_id, $format_value );
259-
} else {
260-
throw new \Exception( get_class( $this ) . "::get_field() : Unsupported custom fields plugin \"{$this->custom_fields_plugin}\"" );
261-
}
262-
263-
$this->_fields[ $post_id ][ $field_name ] = $value;
264-
}
265-
266-
return $this->_fields[ $post_id ][ $field_name ];
267-
}
268-
269-
/**
270-
* Getter of postmeta from just custom fields
271-
*
272-
* @param string $field_name Field name to get.
273-
* @param int $post_id Post ID if different from get_the_ID.
274-
* @param bool|string $format_value Format value or not.
275-
*
276-
* @return mixed
277-
* @throws \Exception Unsupported custom fields plugin.
278-
*/
279-
public function get_field_jcf( $field_name, $post_id, $format_value ) {
280-
// Fix for getter from magic property field_*.
281-
if ( strpos( $field_name, '_' ) !== 0 ) {
282-
$field_name = "_{$field_name}";
283-
}
284-
return get_post_meta( $post_id, $field_name, $format_value );
285-
}
286-
287-
/**
288-
* Getter of postmeta from advanced custom fields
289-
*
290-
* @param string $field_name Field name to get.
291-
* @param int $post_id Post ID if different from get_the_ID.
292-
* @param bool|string $format_value Format value or not.
293-
*
294-
* @return mixed
295-
* @throws \Exception Unsupported custom fields plugin.
296-
*/
297-
public function get_field_acf( $field_name, $post_id, $format_value ) {
298-
return get_field( $field_name, $post_id, $format_value );
299-
}
300192
}

framework/Objects/Postmeta.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace JustCoded\WP\Framework\Objects;
4+
5+
/**
6+
* Class Postmeta
7+
* Get meta fields from posts
8+
*/
9+
class Postmeta extends Meta {
10+
11+
/**
12+
* Current Object to get meta data from.
13+
*
14+
* @var \WP_Post
15+
*/
16+
public $object;
17+
18+
/**
19+
* Postmeta constructor.
20+
*/
21+
public function __construct() {
22+
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 );
36+
}
37+
38+
/**
39+
* Get post id
40+
*
41+
* @return false|int
42+
*/
43+
public function get_object_id() {
44+
return $this->object->ID;
45+
}
46+
47+
/**
48+
* Getter of postmeta from advanced custom fields
49+
*
50+
* @param string $field_name Field name to get.
51+
* @param int $post_id Post ID if different from get_the_ID.
52+
* @param bool|string $format_value Format value or not.
53+
*
54+
* @return mixed
55+
* @throws \Exception Unsupported custom fields plugin.
56+
*/
57+
public function get_value_acf( $field_name, $post_id, $format_value ) {
58+
return get_field( $field_name, $post_id, $format_value );
59+
}
60+
61+
/**
62+
* Getter of postmeta from just custom fields
63+
*
64+
* @param string $field_name Field name to get.
65+
* @param int $post_id Post ID if different from get_the_ID.
66+
* @param bool|string $format_value Format value or not.
67+
*
68+
* @return mixed
69+
* @throws \Exception Unsupported custom fields plugin.
70+
*/
71+
public function get_value_jcf( $field_name, $post_id, $format_value ) {
72+
// Fix for getter from magic property _*.
73+
if ( strpos( $field_name, '_' ) !== 0 ) {
74+
$field_name = "_{$field_name}";
75+
}
76+
return get_post_meta( $post_id, $field_name, $format_value );
77+
}
78+
79+
}

framework/Objects/Taxonomy.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ abstract class Taxonomy {
7575
*/
7676
protected $rewrite_singular = false;
7777

78+
/**
79+
* Allow hierarchical urls, defaults false
80+
*
81+
* @var bool
82+
*/
83+
protected $rewrite_hierarchical = false;
84+
7885
/**
7986
* Show in admin menu
8087
* affect: show_in_menu, show_ui
@@ -194,6 +201,7 @@ public function register() {
194201
'rewrite' => array(
195202
'slug' => $this::$SLUG,
196203
'with_front' => ! $this->rewrite_singular,
204+
'hierarchical' => $this->rewrite_hierarchical,
197205
),
198206
);
199207

0 commit comments

Comments
 (0)