| 1 |
<?php |
| 2 |
/** |
| 3 |
* Charitable Object Fields model. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Object_Fields |
| 6 |
* @author Eric Daams |
| 7 |
* @copyright Copyright (c) 2019, Studio 164a |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.6.0 |
| 10 |
* @version 1.6.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
// Exit if accessed directly. |
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
if ( ! class_exists( 'Charitable_Object_Fields' ) ) : |
| 19 |
|
| 20 |
/** |
| 21 |
* Charitable_Object_Fields |
| 22 |
* |
| 23 |
* @since 1.6.0 |
| 24 |
*/ |
| 25 |
class Charitable_Object_Fields implements Charitable_Fields_Interface { |
| 26 |
|
| 27 |
/** |
| 28 |
* The `Charitable_Field_Registry` instance for this object. |
| 29 |
* |
| 30 |
* @since 1.6.0 |
| 31 |
* |
| 32 |
* @var Charitable_Field_Registry |
| 33 |
*/ |
| 34 |
private $registry; |
| 35 |
|
| 36 |
/** |
| 37 |
* An object. |
| 38 |
* |
| 39 |
* @since 1.6.0 |
| 40 |
* |
| 41 |
* @var mixed |
| 42 |
*/ |
| 43 |
private $object; |
| 44 |
|
| 45 |
/** |
| 46 |
* Create class object. |
| 47 |
* |
| 48 |
* @since 1.6.0 |
| 49 |
* |
| 50 |
* @param Charitable_Field_Registry $registry An instance of `Charitable_Field_Registry`. |
| 51 |
* @param mixed $object The object that will be passed to the value callback. |
| 52 |
*/ |
| 53 |
public function __construct( Charitable_Field_Registry $registry, $object ) { |
| 54 |
$this->registry = $registry; |
| 55 |
$this->object = $object; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Get the set value for a particular field. |
| 60 |
* |
| 61 |
* @since 1.6.0 |
| 62 |
* |
| 63 |
* @param string $field_key The field to get a value for. |
| 64 |
* @return mixed |
| 65 |
*/ |
| 66 |
public function get( $field_key ) { |
| 67 |
$field = $this->registry->get_field( $field_key ); |
| 68 |
|
| 69 |
if ( ! $field ) { |
| 70 |
return null; |
| 71 |
} |
| 72 |
|
| 73 |
return call_user_func( $field->value_callback, $this->object, $field_key ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Check whether a particular field is registered. |
| 78 |
* |
| 79 |
* @since 1.6.0 |
| 80 |
* |
| 81 |
* @param string $field_key The field to check for. |
| 82 |
* @return boolean |
| 83 |
*/ |
| 84 |
public function has( $field_key ) { |
| 85 |
return false !== $this->registry->get_field( $field_key ); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Check whether a particular field has a callback for getting the value. |
| 90 |
* |
| 91 |
* @since 1.6.0 |
| 92 |
* |
| 93 |
* @param string $field_key The field to check for. |
| 94 |
* @return boolean |
| 95 |
*/ |
| 96 |
public function has_value_callback( $field_key ) { |
| 97 |
$field = $this->registry->get_field( $field_key ); |
| 98 |
|
| 99 |
return $field && false !== $field->value_callback; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
endif; |
| 104 |
|