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