base.php
99 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Base storage |
| 4 | * |
| 5 | * @package Meta Box |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Class RWMB_Base_Storage |
| 10 | */ |
| 11 | class RWMB_Base_Storage implements RWMB_Storage_Interface { |
| 12 | |
| 13 | /** |
| 14 | * Object type. |
| 15 | * |
| 16 | * @var string |
| 17 | */ |
| 18 | protected $object_type; |
| 19 | |
| 20 | /** |
| 21 | * Retrieve metadata for the specified object. |
| 22 | * |
| 23 | * @param int $object_id ID of the object metadata is for. |
| 24 | * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for |
| 25 | * the specified object. |
| 26 | * @param bool|array $args Optional, default is false. |
| 27 | * If true, return only the first value of the specified meta_key. |
| 28 | * If is array, use the `single` element. |
| 29 | * This parameter has no effect if meta_key is not specified. |
| 30 | * @return mixed Single metadata value, or array of values. |
| 31 | * |
| 32 | * @see get_metadata() |
| 33 | */ |
| 34 | public function get( $object_id, $meta_key, $args = false ) { |
| 35 | if ( is_array( $args ) ) { |
| 36 | $single = ! empty( $args['single'] ); |
| 37 | } else { |
| 38 | $single = (bool) $args; |
| 39 | } |
| 40 | |
| 41 | return get_metadata( $this->object_type, $object_id, $meta_key, $single ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Add metadata |
| 46 | * |
| 47 | * @param int $object_id ID of the object metadata is for. |
| 48 | * @param string $meta_key Metadata key. |
| 49 | * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. |
| 50 | * @param bool $unique Optional, default is false. |
| 51 | * Whether the specified metadata key should be unique for the object. |
| 52 | * If true, and the object already has a value for the specified metadata key, |
| 53 | * no change will be made. |
| 54 | * @return int|false The meta ID on success, false on failure. |
| 55 | * |
| 56 | * @see add_metadata() |
| 57 | */ |
| 58 | public function add( $object_id, $meta_key, $meta_value, $unique = false ) { |
| 59 | return add_metadata( $this->object_type, $object_id, $meta_key, $meta_value, $unique ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Update metadata. |
| 64 | * |
| 65 | * @param int $object_id ID of the object metadata is for. |
| 66 | * @param string $meta_key Metadata key. |
| 67 | * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. |
| 68 | * @param mixed $prev_value Optional. If specified, only update existing metadata entries with |
| 69 | * the specified value. Otherwise, update all entries. |
| 70 | * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure. |
| 71 | * |
| 72 | * @see update_metadata() |
| 73 | */ |
| 74 | public function update( $object_id, $meta_key, $meta_value, $prev_value = '' ) { |
| 75 | return update_metadata( $this->object_type, $object_id, $meta_key, $meta_value, $prev_value ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Delete metadata. |
| 80 | * |
| 81 | * @param int $object_id ID of the object metadata is for. |
| 82 | * @param string $meta_key Metadata key. |
| 83 | * @param mixed $meta_value Optional. Metadata value. Must be serializable if non-scalar. If specified, only delete |
| 84 | * metadata entries with this value. Otherwise, delete all entries with the specified meta_key. |
| 85 | * Pass `null, `false`, or an empty string to skip this check. (For backward compatibility, |
| 86 | * it is not possible to pass an empty string to delete those entries with an empty string |
| 87 | * for a value). |
| 88 | * @param bool $delete_all Optional, default is false. If true, delete matching metadata entries for all objects, |
| 89 | * ignoring the specified object_id. Otherwise, only delete matching metadata entries for |
| 90 | * the specified object_id. |
| 91 | * @return bool True on successful delete, false on failure. |
| 92 | * |
| 93 | * @see delete_metadata() |
| 94 | */ |
| 95 | public function delete( $object_id, $meta_key, $meta_value = '', $delete_all = false ) { |
| 96 | return delete_metadata( $this->object_type, $object_id, $meta_key, $meta_value, $delete_all ); |
| 97 | } |
| 98 | } |
| 99 |