| 1 |
<?php |
| 2 |
|
| 3 |
namespace Buddypress\CLI\Command; |
| 4 |
|
| 5 |
use WP_CLI\CommandWithMeta; |
| 6 |
|
| 7 |
/** |
| 8 |
* Adds, updates, deletes, and lists group custom fields. |
| 9 |
* |
| 10 |
* ## EXAMPLES |
| 11 |
* |
| 12 |
* # Set group meta |
| 13 |
* $ wp bp group meta set 123 description "Mary is a Group user." |
| 14 |
* Success: Updated custom field 'description'. |
| 15 |
* |
| 16 |
* # Get group meta |
| 17 |
* $ wp bp group meta get 123 description |
| 18 |
* Mary is a Group user. |
| 19 |
* |
| 20 |
* # Update group meta |
| 21 |
* $ wp bp group meta update 123 description "Mary is an awesome Group user." |
| 22 |
* Success: Updated custom field 'description'. |
| 23 |
* |
| 24 |
* # List group meta. |
| 25 |
* $ wp bp group meta list 123 |
| 26 |
* |
| 27 |
* # Delete group meta |
| 28 |
* $ wp bp group meta delete 123 description |
| 29 |
* Success: Deleted custom field. |
| 30 |
* |
| 31 |
* @since 2.0.0 |
| 32 |
*/ |
| 33 |
class Group_Meta extends CommandWithMeta { |
| 34 |
protected $meta_type = 'group'; |
| 35 |
|
| 36 |
/** |
| 37 |
* Wrapper method for add_metadata that can be overridden in sub classes. |
| 38 |
* |
| 39 |
* @param int $object_id ID of the object the metadata is for. |
| 40 |
* @param string $meta_key Metadata key to use. |
| 41 |
* @param mixed $meta_value Metadata value. Must be serializable if |
| 42 |
* non-scalar. |
| 43 |
* @param bool $unique Optional, default is false. Whether the |
| 44 |
* specified metadata key should be unique for the |
| 45 |
* object. If true, and the object already has a |
| 46 |
* value for the specified metadata key, no change |
| 47 |
* will be made. |
| 48 |
* |
| 49 |
* @return int|false The meta ID on success, false on failure. |
| 50 |
*/ |
| 51 |
protected function add_metadata( $object_id, $meta_key, $meta_value, $unique = false ) { |
| 52 |
return groups_add_groupmeta( $object_id, $meta_key, $meta_value ); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Wrapper method for update_metadata that can be overridden in sub classes. |
| 57 |
* |
| 58 |
* @param int $object_id ID of the object the metadata is for. |
| 59 |
* @param string $meta_key Metadata key to use. |
| 60 |
* @param mixed $meta_value Metadata value. Must be serializable if |
| 61 |
* non-scalar. |
| 62 |
* @param mixed $prev_value Optional. If specified, only update existing |
| 63 |
* metadata entries with the specified value. |
| 64 |
* Otherwise, update all entries. |
| 65 |
* |
| 66 |
* @return int|bool Meta ID if the key didn't exist, true on successful |
| 67 |
* update, false on failure. |
| 68 |
*/ |
| 69 |
protected function update_metadata( $object_id, $meta_key, $meta_value, $prev_value = '' ) { |
| 70 |
return groups_update_groupmeta( $object_id, $meta_key, $meta_value, $prev_value ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Wrapper method for get_metadata that can be overridden in sub classes. |
| 75 |
* |
| 76 |
* @param int $object_id ID of the object the metadata is for. |
| 77 |
* @param string $meta_key Optional. Metadata key. If not specified, |
| 78 |
* retrieve all metadata for the specified object. |
| 79 |
* @param bool $single Optional, default is false. If true, return only |
| 80 |
* the first value of the specified meta_key. This |
| 81 |
* parameter has no effect if meta_key is not |
| 82 |
* specified. |
| 83 |
* |
| 84 |
* @return mixed Single metadata value, or array of values. |
| 85 |
*/ |
| 86 |
protected function get_metadata( $object_id, $meta_key = '', $single = true ) { |
| 87 |
return groups_get_groupmeta( $object_id, $meta_key, $single ); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Wrapper method for delete_metadata that can be overridden in sub classes. |
| 92 |
* |
| 93 |
* @param int $object_id ID of the object metadata is for |
| 94 |
* @param string $meta_key Metadata key |
| 95 |
* @param mixed $meta_value Optional. Metadata value. Must be serializable |
| 96 |
* if non-scalar. If specified, only delete |
| 97 |
* metadata entries with this value. Otherwise, |
| 98 |
* delete all entries with the specified meta_key. |
| 99 |
* Pass `null, `false`, or an empty string to skip |
| 100 |
* this check. For backward compatibility, it is |
| 101 |
* not possible to pass an empty string to delete |
| 102 |
* those entries with an empty string for a value. |
| 103 |
* |
| 104 |
* @return bool True on successful delete, false on failure. |
| 105 |
*/ |
| 106 |
protected function delete_metadata( $object_id, $meta_key, $meta_value = '' ) { |
| 107 |
return groups_delete_groupmeta( $object_id, $meta_key, $meta_value ); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Check that the group ID exists. |
| 112 |
* |
| 113 |
* @param int $object_id Object ID. |
| 114 |
* @return int |
| 115 |
*/ |
| 116 |
protected function check_object_id( $object_id ) { |
| 117 |
$fetcher = new Group_Fetcher(); |
| 118 |
$group = $fetcher->get_check( $object_id ); |
| 119 |
|
| 120 |
return $group->id; |
| 121 |
} |
| 122 |
} |
| 123 |
|