Skip to content
66 changes: 66 additions & 0 deletions features/network-meta.feature
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,69 @@ Feature: Manage network-wide custom fields.
This is not a multisite install
"""
And the return code should be 1

Scenario: Network meta is actually network options
Given a WP multisite install

When I run `wp eval 'update_network_option( 1, "mykey", "123" );'`
And I run `wp eval 'echo get_network_option( 1, "mykey" );'`
Then STDOUT should be:
"""
123
"""

When I run `wp network meta update 1 mykey 456`
Then STDOUT should be:
"""
Success: Updated custom field 'mykey'.
"""

When I run `wp network meta get 1 mykey`
Then STDOUT should be:
"""
456
"""

When I run `wp eval 'echo get_network_option( 1, "mykey" );'`
Then STDOUT should be:
"""
456
"""

@require-object-cache
Scenario: Object cache correctly handles network meta updates
Given a WP multisite install

When I run `wp eval 'update_network_option( 1, "objkey", "123" );'`

And I run `wp network meta get 1 objkey`
Then STDOUT should be:
"""
123
"""

When I run `wp eval 'update_network_option( 1, "objkey", "456" );'`

And I run `wp network meta get 1 objkey`
Then STDOUT should be:
"""
456
"""

When I run `wp network meta update 1 objkey 789`
Then STDOUT should be:
"""
Success: Updated custom field 'objkey'.
"""

When I run `wp network meta get 1 objkey`
Then STDOUT should be:
"""
789
"""

When I run `wp eval 'echo get_network_option( 1, "objkey" );'`
Then STDOUT should be:
"""
789
"""
78 changes: 78 additions & 0 deletions src/Network_Meta_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,82 @@
*/
class Network_Meta_Command extends CommandWithMeta {
protected $meta_type = 'site';

/**
* Override add_metadata() to use add_network_option().
*
* @param int $object_id ID of the object the metadata is for.
* @param string $meta_key Metadata key to use.
* @param mixed $meta_value Metadata value. Must be serializable if
* non-scalar.
* @param bool $unique Optional, default is false. Whether the
* specified metadata key should be unique for the
* object. If true, and the object already has a
* value for the specified metadata key, no change
* will be made.
*
* @return bool The meta ID on success, false on failure.
*
* @phpstan-ignore method.childReturnType
*/
protected function add_metadata( $object_id, $meta_key, $meta_value, $unique = false ) {
return add_network_option( $object_id, $meta_key, $meta_value );
}

/**
* Override update_metadata() to use update_network_option().
*
* @param int $object_id ID of the object the metadata is for.
* @param string $meta_key Metadata key to use.
* @param mixed $meta_value Metadata value. Must be serializable if
* non-scalar.
* @param mixed $prev_value Optional. If specified, only update existing
* metadata entries with the specified value.
* Otherwise, update all entries.
*
* @return int|bool Meta ID if the key didn't exist, true on successful
* update, false on failure.
*/
protected function update_metadata( $object_id, $meta_key, $meta_value, $prev_value = '' ) {
return update_network_option( $object_id, $meta_key, $meta_value );
}

/**
* Override get_metadata() to use get_network_option().
*
* @param int $object_id ID of the object the metadata is for.
* @param string $meta_key Optional. Metadata key. If not specified,
* retrieve all metadata for the specified object.
* @param bool $single Optional, default is false. If true, return only
* the first value of the specified meta_key. This
* parameter has no effect if meta_key is not
* specified.
*
* @return mixed Single metadata value, or array of values.
*
* @phpstan-ignore method.childReturnType
*/
protected function get_metadata( $object_id, $meta_key = '', $single = false ) {
return get_network_option( $object_id, $meta_key );
}

/**
* Override delete_metadata() to use delete_network_option().
*
* @param int $object_id ID of the object metadata is for
* @param string $meta_key Metadata key
* @param mixed $meta_value Optional. Metadata value. Must be serializable
* if non-scalar. If specified, only delete
* metadata entries with this value. Otherwise,
* delete all entries with the specified meta_key.
* Pass `null, `false`, or an empty string to skip
* this check. For backward compatibility, it is
* not possible to pass an empty string to delete
* those entries with an empty string for a value.
*
* @return bool True on successful delete, false on failure.
*/
protected function delete_metadata( $object_id, $meta_key, $meta_value = '' ) {
return delete_network_option( $object_id, $meta_key );
}
}