Skip to content

Commit ef277e7

Browse files
author
Jonny Harris
committed
Add the wp site meta sub-command, with tests
1 parent 518acae commit ef277e7

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

entity-command.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@
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 ( \WP_CLI\Utils\wp_version_compare( '5.0', '<' ) ) {
36+
WP_CLI::error( "Requires WordPress 5.0 or greater." );
37+
}
38+
if( function_exists('is_site_meta_supported') && !is_site_meta_supported() ){
39+
WP_CLI::error( sprintf( 'The %s table is not installed. Please run the network database upgrade.', $GLOBALS['wpdb']->blogmeta ) );
40+
}
41+
}
42+
) );
3043
WP_CLI::add_command( 'site option', 'Site_Option_Command', array(
3144
'before_invoke' => function() {
3245
if ( !is_multisite() ) {

features/site-meta.feature

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Feature: Manage site-wide custom fields.
2+
3+
@require-wp-5.0
4+
Scenario: Non-multisite
5+
Given a WP install
6+
7+
When I run `wp site-meta`
8+
Then STDOUT should contain:
9+
"""
10+
usage: wp site meta
11+
"""
12+
13+
When I try `wp site-meta get 1 test`
14+
Then STDOUT should be empty
15+
And STDERR should contain:
16+
"""
17+
This is not a multisite install.
18+
"""
19+
And the return code should be 1

src/Site_Meta_Command.php

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

0 commit comments

Comments
 (0)