Skip to content

Commit 4114300

Browse files
committed
code refactoring
1 parent 15ef2db commit 4114300

File tree

6 files changed

+318
-294
lines changed

6 files changed

+318
-294
lines changed

framework/Objects/Meta.php

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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+
* Plugin used for extending of post custom fields.
13+
*
14+
* Available values:
15+
* 'just-custom-fields'
16+
* 'advanced-custom-fields'
17+
*
18+
* @var string
19+
*/
20+
public $custom_fields_plugin = 'just-custom-fields';
21+
22+
/**
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+
* Current page/term object. It can be WP_Post or WP_Term
34+
*
35+
* @var object
36+
*/
37+
public $object;
38+
39+
/**
40+
* Meta constructor.
41+
*/
42+
public function __construct() {
43+
if ( class_exists('acf') ) {
44+
$this->custom_fields_plugin = self::PLUGIN_ACF;
45+
} else {
46+
$this->custom_fields_plugin = self::PLUGIN_JCF;
47+
}
48+
49+
return $this->custom_fields_plugin;
50+
}
51+
52+
/**
53+
* Returns the value of an object property.
54+
*
55+
* Do not call this method directly as it is a PHP magic method that
56+
* will be implicitly called when executing `$value = $object->property;`.
57+
*
58+
* @param string $name The property name.
59+
*
60+
* @return mixed the property value
61+
* @throws \Exception Property is not defined.
62+
* @see __set()
63+
*/
64+
public function __get( $name ) {
65+
66+
return $this->get_field( $name );
67+
}
68+
69+
/**
70+
* Checks if the named property is set (not null).
71+
*
72+
* Do not call this method directly as it is a PHP magic method that
73+
* will be implicitly called when executing `isset($object->property)`.
74+
*
75+
* Note that if the property is not defined, false will be returned.
76+
*
77+
* @param string $name The property name or the event name.
78+
*
79+
* @return boolean Whether the named property is set (not null).
80+
*/
81+
public function __isset( $name ) {
82+
83+
return null !== $this->get_field($name);
84+
}
85+
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+
108+
/**
109+
* Getter of postmeta from just custom fields
110+
*
111+
* @param string $field_name Field name to get.
112+
* @param int $post_id Post ID if different from get_the_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_field_jcf( $field_name, $post_id, $format_value );
119+
120+
/**
121+
* Getter of postmeta from advanced custom fields
122+
*
123+
* @param string $field_name Field name to get.
124+
* @param int $post_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+
abstract public function get_field_acf( $field_name, $post_id, $format_value );
131+
132+
/**
133+
* Main post meta fields getter function.
134+
*
135+
* @param string $field_name Field name to get.
136+
* @param int $post_id Post ID if different from get_the_ID.
137+
* @param bool|string $format_value Format value or not.
138+
*
139+
* @return mixed
140+
* @throws \Exception Unsupported custom fields plugin.
141+
*/
142+
public function get_field( $field_name, $post_id = null, $format_value = true ) {
143+
144+
if ( is_a( $this->object, 'WP_Post' ) ) {
145+
$post_id = $this->object->ID;
146+
} else {
147+
$post_id = $this->object->term_id;
148+
}
149+
150+
// Check cache, if not exists - get field value.
151+
if ( ! isset( $this->_fields[ $post_id ][ $field_name ] ) ) {
152+
if ( self::PLUGIN_JCF === $this->custom_fields_plugin ) {
153+
$value = $this->get_field_jcf( $field_name, $post_id, $format_value );
154+
} elseif ( self::PLUGIN_ACF === $this->custom_fields_plugin ) {
155+
$value = $this->get_field_acf( $field_name, $post_id, $format_value );
156+
} else {
157+
throw new \Exception( get_class( $this ) . "::get_field() : Unsupported custom fields plugin \"{$this->custom_fields_plugin}\"" );
158+
}
159+
160+
$this->_fields[ $post_id ][ $field_name ] = $value;
161+
}
162+
163+
return $this->_fields[ $post_id ][ $field_name ];
164+
}
165+
166+
}

0 commit comments

Comments
 (0)