Skip to content

Commit bf540e2

Browse files
authored
Merge pull request #159 from spacedmonkey/master
Add wp site meta sub-command
2 parents d44e0bf + ccc05bf commit bf540e2

File tree

3 files changed

+158
-0
lines changed

3 files changed

+158
-0
lines changed

entity-command.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@
2727
WP_CLI::add_command( 'post term', 'Post_Term_Command' );
2828
WP_CLI::add_command( 'post-type', 'Post_Type_Command' );
2929
WP_CLI::add_command( 'site', 'Site_Command' );
30+
WP_CLI::add_command( 'site meta', 'Site_Meta_Command', array(
31+
'before_invoke' => function() {
32+
if ( !is_multisite() ) {
33+
WP_CLI::error( 'This is not a multisite installation.' );
34+
}
35+
if( function_exists('is_site_meta_supported') && !is_site_meta_supported() ){
36+
WP_CLI::error( sprintf( 'The %s table is not installed. Please run the network database upgrade.', $GLOBALS['wpdb']->blogmeta ) );
37+
}
38+
}
39+
) );
3040
WP_CLI::add_command( 'site option', 'Site_Option_Command', array(
3141
'before_invoke' => function() {
3242
if ( !is_multisite() ) {

features/site-meta.feature

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
Feature: Manage site custom fields
2+
3+
@require-wp-5.0
4+
Scenario: Site meta CRUD
5+
Given a WP multisite installation
6+
7+
When I run `wp site meta add 1 foo 'bar'`
8+
Then STDOUT should not be empty
9+
10+
When I run `wp site meta get 1 foo`
11+
Then STDOUT should be:
12+
"""
13+
bar
14+
"""
15+
16+
When I try `wp site meta get 999999 foo`
17+
Then STDERR should be:
18+
"""
19+
Error: Could not find the site with ID 999999.
20+
"""
21+
And the return code should be 1
22+
23+
When I run `wp site meta set 1 foo '[ "1", "2" ]' --format=json`
24+
Then STDOUT should not be empty
25+
26+
When I run `wp site meta get 1 foo --format=json`
27+
Then STDOUT should be:
28+
"""
29+
["1","2"]
30+
"""
31+
32+
When I run `wp site meta delete 1 foo`
33+
Then STDOUT should not be empty
34+
35+
When I try `wp site meta get 1 foo`
36+
Then the return code should be 1

src/Site_Meta_Command.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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

Comments
 (0)