|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace JustCoded\WP\Framework\Objects; |
| 4 | + |
| 5 | +/** |
| 6 | + * Meta_Fields is the base class for acquiring data from custom fields. |
| 7 | + */ |
| 8 | + |
| 9 | +class Meta_Fields { |
| 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 | + * Current $post object |
| 24 | + * |
| 25 | + * @var \WP_Post |
| 26 | + */ |
| 27 | + public $post; |
| 28 | + |
| 29 | + /** |
| 30 | + * Current $term object |
| 31 | + * |
| 32 | + * @var \WP_Term |
| 33 | + */ |
| 34 | + public $term; |
| 35 | + |
| 36 | + /** |
| 37 | + * Returns the value of an object property. |
| 38 | + * |
| 39 | + * Do not call this method directly as it is a PHP magic method that |
| 40 | + * will be implicitly called when executing `$value = $object->property;`. |
| 41 | + * |
| 42 | + * @param string $name The property name. |
| 43 | + * |
| 44 | + * @return mixed the property value |
| 45 | + * @throws \Exception Property is not defined. |
| 46 | + * @see __set() |
| 47 | + */ |
| 48 | + public function __get( $name ) { |
| 49 | + if ( $this->post ) { |
| 50 | + // getter for magic property $this->field_*. |
| 51 | + return $this->get_field( $name ); |
| 52 | + } else { |
| 53 | + return $this->get_term_field( $name ); |
| 54 | + } |
| 55 | + |
| 56 | + // trying to find get_{property} method. |
| 57 | + $getter = 'get_' . $name; |
| 58 | + if ( method_exists( $this, $getter ) ) { |
| 59 | + return $this->$getter(); |
| 60 | + } else { |
| 61 | + throw new \Exception( 'Getting unknown property: ' . get_class( $this ) . '::' . $name ); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Sets value of an object property. |
| 67 | + * |
| 68 | + * Do not call this method directly as it is a PHP magic method that |
| 69 | + * will be implicitly called when executing `$object->property = $value;`. |
| 70 | + * |
| 71 | + * @param string $name The property name or the event name. |
| 72 | + * @param mixed $value The property value. |
| 73 | + * |
| 74 | + * @throws \Exception Property is not defined or read-only. |
| 75 | + * @see __get() |
| 76 | + */ |
| 77 | + public function __set( $name, $value ) { |
| 78 | + $setter = 'set_' . $name; |
| 79 | + if ( method_exists( $this, $setter ) ) { |
| 80 | + $this->$setter( $value ); |
| 81 | + } elseif ( method_exists( $this, 'get_' . $name ) ) { |
| 82 | + throw new \Exception( 'Setting read-only property: ' . get_class( $this ) . '::' . $name ); |
| 83 | + } else { |
| 84 | + throw new \Exception( 'Setting unknown property: ' . get_class( $this ) . '::' . $name ); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Checks if the named property is set (not null). |
| 90 | + * |
| 91 | + * Do not call this method directly as it is a PHP magic method that |
| 92 | + * will be implicitly called when executing `isset($object->property)`. |
| 93 | + * |
| 94 | + * Note that if the property is not defined, false will be returned. |
| 95 | + * |
| 96 | + * @param string $name The property name or the event name. |
| 97 | + * |
| 98 | + * @return boolean Whether the named property is set (not null). |
| 99 | + */ |
| 100 | + public function __isset( $name ) { |
| 101 | + $getter = 'get_' . $name; |
| 102 | + if ( method_exists( $this, $getter ) ) { |
| 103 | + return $this->$getter() !== null; |
| 104 | + } else { |
| 105 | + return false; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Sets an object property to null. |
| 111 | + * |
| 112 | + * Do not call this method directly as it is a PHP magic method that |
| 113 | + * will be implicitly called when executing `unset($object->property)`. |
| 114 | + * |
| 115 | + * Note that if the property is not defined, this method will do nothing. |
| 116 | + * If the property is read-only, it will throw an exception. |
| 117 | + * |
| 118 | + * @param string $name The property name. |
| 119 | + * |
| 120 | + * @throws \Exception The property is read only. |
| 121 | + */ |
| 122 | + public function __unset( $name ) { |
| 123 | + $setter = 'set_' . $name; |
| 124 | + if ( method_exists( $this, $setter ) ) { |
| 125 | + $this->$setter( null ); |
| 126 | + } elseif ( method_exists( $this, 'get_' . $name ) ) { |
| 127 | + throw new \Exception( 'Setting read-only property: ' . get_class( $this ) . '::' . $name ); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Getter of postmeta from just custom fields |
| 133 | + * |
| 134 | + * @param string $field_name Field name to get. |
| 135 | + * @param int $post_id Post ID if different from get_the_ID. |
| 136 | + * @param bool|string $format_value Format value or not. |
| 137 | + * |
| 138 | + * @return mixed |
| 139 | + * @throws \Exception Unsupported custom fields plugin. |
| 140 | + */ |
| 141 | + protected function get_field_jcf( $field_name, $post_id, $format_value ) { |
| 142 | + // Fix for getter from magic property field_*. |
| 143 | + if ( strpos( $field_name, '_' ) !== 0 ) { |
| 144 | + $field_name = "_{$field_name}"; |
| 145 | + } |
| 146 | + return get_post_meta( $post_id, $field_name, $format_value ); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Getter of postmeta from advanced custom fields |
| 151 | + * |
| 152 | + * @param string $field_name Field name to get. |
| 153 | + * @param int $post_id Post ID if different from get_the_ID. |
| 154 | + * @param bool|string $format_value Format value or not. |
| 155 | + * |
| 156 | + * @return mixed |
| 157 | + * @throws \Exception Unsupported custom fields plugin. |
| 158 | + */ |
| 159 | + protected function get_field_acf( $field_name, $post_id, $format_value ) { |
| 160 | + return get_field( $field_name, $post_id, $format_value ); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Main post meta fields getter function. |
| 165 | + * |
| 166 | + * @param string $field_name Field name to get. |
| 167 | + * @param int $post_id Post ID if different from get_the_ID. |
| 168 | + * @param bool|string $format_value Format value or not. |
| 169 | + * |
| 170 | + * @return mixed |
| 171 | + * @throws \Exception Unsupported custom fields plugin. |
| 172 | + */ |
| 173 | + public function get_field( $field_name, $post_id = null, $format_value = true ) { |
| 174 | + |
| 175 | + // Use $this->post in case ID is empty. |
| 176 | + if ( empty( $post_id ) && ! empty( $this->post ) ) { |
| 177 | + $post_id = $this->post->ID; |
| 178 | + } |
| 179 | + |
| 180 | + // Check cache, if not exists - get field value. |
| 181 | + if ( ! isset( $this->_fields[ $post_id ][ $field_name ] ) ) { |
| 182 | + if ( 'just-custom-fields' === $this->custom_fields_plugin ) { |
| 183 | + $value = $this->get_field_jcf( $field_name, $post_id, $format_value ); |
| 184 | + } elseif ( 'advanced-custom-fields' === $this->custom_fields_plugin ) { |
| 185 | + $value = $this->get_field_acf( $field_name, $post_id, $format_value ); |
| 186 | + } else { |
| 187 | + throw new \Exception( get_class( $this ) . "::get_field() : Unsupported custom fields plugin \"{$this->custom_fields_plugin}\"" ); |
| 188 | + } |
| 189 | + |
| 190 | + $this->_fields[ $post_id ][ $field_name ] = $value; |
| 191 | + } |
| 192 | + |
| 193 | + return $this->_fields[ $post_id ][ $field_name ]; |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Main term meta fields getter function. |
| 198 | + * |
| 199 | + * @param string $field_name Field name to get. |
| 200 | + * @param int $term_id Term ID |
| 201 | + * @param bool|string $format_value Format value or not. |
| 202 | + * |
| 203 | + * @return mixed |
| 204 | + * @throws \Exception Unsupported custom fields plugin. |
| 205 | + */ |
| 206 | + protected function get_term_field( $field_name, $term_id = null, $format_value = true ) { |
| 207 | + |
| 208 | + // Use $this->term in case term_id is empty. |
| 209 | + if ( empty( $term_id ) && ! empty( $this->term ) ) { |
| 210 | + $term_id = $this->term->term_id; |
| 211 | + } |
| 212 | + |
| 213 | + // Check cache, if not exists - get field value. |
| 214 | + if ( ! isset( $this->_fields[ $term_id ][ $field_name ] ) ) { |
| 215 | + if ( 'just-custom-fields' === $this->custom_fields_plugin ) { |
| 216 | + $value = $this->get_term_field_jcf( $field_name, $term_id, $format_value ); |
| 217 | + } elseif ( 'advanced-custom-fields' === $this->custom_fields_plugin ) { |
| 218 | + $value = $this->get_term_field_acf( $field_name, $term_id ); |
| 219 | + } else { |
| 220 | + throw new \Exception( get_class( $this ) . "::get_field() : Unsupported custom fields plugin \"{$this->custom_fields_plugin}\"" ); |
| 221 | + } |
| 222 | + |
| 223 | + $this->_fields[ $term_id ][ $field_name ] = $value; |
| 224 | + } |
| 225 | + |
| 226 | + return $this->_fields[ $term_id ][ $field_name ]; |
| 227 | + } |
| 228 | + |
| 229 | + /** |
| 230 | + * Getter of termmeta from just custom fields |
| 231 | + * |
| 232 | + * @param string $field_name Field name to get. |
| 233 | + * @param int $term_id Term ID |
| 234 | + * @param bool|string $format_value Format value or not. |
| 235 | + * |
| 236 | + * @return mixed |
| 237 | + * @throws \Exception Unsupported custom fields plugin. |
| 238 | + */ |
| 239 | + protected function get_term_field_jcf( $field_name, $term_id, $format_value ) { |
| 240 | + // Fix for getter from magic property field_*. |
| 241 | + if ( strpos( $field_name, '_' ) !== 0 ) { |
| 242 | + $field_name = "_{$field_name}"; |
| 243 | + } |
| 244 | + return get_term_meta( $term_id, $field_name, $format_value ); |
| 245 | + } |
| 246 | + |
| 247 | + /** |
| 248 | + * Getter of term field data from wp options for advanced custom fields |
| 249 | + * |
| 250 | + * @param string $field_name Field name to get. |
| 251 | + * @param int $post_id Term ID |
| 252 | + * @param bool|string $format_value Format value or not. |
| 253 | + * |
| 254 | + * @return mixed |
| 255 | + * @throws \Exception Unsupported custom fields plugin. |
| 256 | + */ |
| 257 | + protected function get_term_field_acf( $field_name, $term_id ) { |
| 258 | + if ( $term_id ) { |
| 259 | + $option_name = 'category_' . $term_id . '_' . $field_name; |
| 260 | + return get_option( $option_name ); |
| 261 | + } else { |
| 262 | + return false; |
| 263 | + } |
| 264 | + } |
| 265 | + |
| 266 | +} |
0 commit comments