Skip to content

Commit 84b79a9

Browse files
spacedmonkeyswissspidyjonnynews
authored
Implement network-specific metadata handling in commands. (#518)
Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com> Co-authored-by: Jonny Harris <jonathan.harris@news.co.uk> Co-authored-by: Pascal Birchler <pascalb@google.com>
1 parent 64a4a47 commit 84b79a9

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed

features/network-meta.feature

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,69 @@ Feature: Manage network-wide custom fields.
1616
This is not a multisite install
1717
"""
1818
And the return code should be 1
19+
20+
Scenario: Network meta is actually network options
21+
Given a WP multisite install
22+
23+
When I run `wp eval 'update_network_option( 1, "mykey", "123" );'`
24+
And I run `wp eval 'echo get_network_option( 1, "mykey" );'`
25+
Then STDOUT should be:
26+
"""
27+
123
28+
"""
29+
30+
When I run `wp network meta update 1 mykey 456`
31+
Then STDOUT should be:
32+
"""
33+
Success: Updated custom field 'mykey'.
34+
"""
35+
36+
When I run `wp network meta get 1 mykey`
37+
Then STDOUT should be:
38+
"""
39+
456
40+
"""
41+
42+
When I run `wp eval 'echo get_network_option( 1, "mykey" );'`
43+
Then STDOUT should be:
44+
"""
45+
456
46+
"""
47+
48+
@require-object-cache
49+
Scenario: Object cache correctly handles network meta updates
50+
Given a WP multisite install
51+
52+
When I run `wp eval 'update_network_option( 1, "objkey", "123" );'`
53+
54+
And I run `wp network meta get 1 objkey`
55+
Then STDOUT should be:
56+
"""
57+
123
58+
"""
59+
60+
When I run `wp eval 'update_network_option( 1, "objkey", "456" );'`
61+
62+
And I run `wp network meta get 1 objkey`
63+
Then STDOUT should be:
64+
"""
65+
456
66+
"""
67+
68+
When I run `wp network meta update 1 objkey 789`
69+
Then STDOUT should be:
70+
"""
71+
Success: Updated custom field 'objkey'.
72+
"""
73+
74+
When I run `wp network meta get 1 objkey`
75+
Then STDOUT should be:
76+
"""
77+
789
78+
"""
79+
80+
When I run `wp eval 'echo get_network_option( 1, "objkey" );'`
81+
Then STDOUT should be:
82+
"""
83+
789
84+
"""

src/Network_Meta_Command.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,82 @@
1515
*/
1616
class Network_Meta_Command extends CommandWithMeta {
1717
protected $meta_type = 'site';
18+
19+
/**
20+
* Override add_metadata() to use add_network_option().
21+
*
22+
* @param int $object_id ID of the object the metadata is for.
23+
* @param string $meta_key Metadata key to use.
24+
* @param mixed $meta_value Metadata value. Must be serializable if
25+
* non-scalar.
26+
* @param bool $unique Optional, default is false. Whether the
27+
* specified metadata key should be unique for the
28+
* object. If true, and the object already has a
29+
* value for the specified metadata key, no change
30+
* will be made.
31+
*
32+
* @return bool The meta ID on success, false on failure.
33+
*
34+
* @phpstan-ignore method.childReturnType
35+
*/
36+
protected function add_metadata( $object_id, $meta_key, $meta_value, $unique = false ) {
37+
return add_network_option( $object_id, $meta_key, $meta_value );
38+
}
39+
40+
/**
41+
* Override update_metadata() to use update_network_option().
42+
*
43+
* @param int $object_id ID of the object the metadata is for.
44+
* @param string $meta_key Metadata key to use.
45+
* @param mixed $meta_value Metadata value. Must be serializable if
46+
* non-scalar.
47+
* @param mixed $prev_value Optional. If specified, only update existing
48+
* metadata entries with the specified value.
49+
* Otherwise, update all entries.
50+
*
51+
* @return int|bool Meta ID if the key didn't exist, true on successful
52+
* update, false on failure.
53+
*/
54+
protected function update_metadata( $object_id, $meta_key, $meta_value, $prev_value = '' ) {
55+
return update_network_option( $object_id, $meta_key, $meta_value );
56+
}
57+
58+
/**
59+
* Override get_metadata() to use get_network_option().
60+
*
61+
* @param int $object_id ID of the object the metadata is for.
62+
* @param string $meta_key Optional. Metadata key. If not specified,
63+
* retrieve all metadata for the specified object.
64+
* @param bool $single Optional, default is false. If true, return only
65+
* the first value of the specified meta_key. This
66+
* parameter has no effect if meta_key is not
67+
* specified.
68+
*
69+
* @return mixed Single metadata value, or array of values.
70+
*
71+
* @phpstan-ignore method.childReturnType
72+
*/
73+
protected function get_metadata( $object_id, $meta_key = '', $single = false ) {
74+
return get_network_option( $object_id, $meta_key );
75+
}
76+
77+
/**
78+
* Override delete_metadata() to use delete_network_option().
79+
*
80+
* @param int $object_id ID of the object metadata is for
81+
* @param string $meta_key Metadata key
82+
* @param mixed $meta_value Optional. Metadata value. Must be serializable
83+
* if non-scalar. If specified, only delete
84+
* metadata entries with this value. Otherwise,
85+
* delete all entries with the specified meta_key.
86+
* Pass `null, `false`, or an empty string to skip
87+
* this check. For backward compatibility, it is
88+
* not possible to pass an empty string to delete
89+
* those entries with an empty string for a value.
90+
*
91+
* @return bool True on successful delete, false on failure.
92+
*/
93+
protected function delete_metadata( $object_id, $meta_key, $meta_value = '' ) {
94+
return delete_network_option( $object_id, $meta_key );
95+
}
1896
}

0 commit comments

Comments
 (0)