From cad36028627a75252f050357c488e345728838cc Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Tue, 21 Jan 2025 20:12:41 -0500 Subject: [PATCH 1/6] Implement network-specific metadata handling in commands. Added overrides for add, update, get, and delete metadata methods to utilize network-specific options when available. This ensures compatibility and functionality for multisite network scenarios. Fallbacks to standard metadata functions are maintained for non-network environments. --- src/Network_Meta_Command.php | 86 ++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/src/Network_Meta_Command.php b/src/Network_Meta_Command.php index 7523395ed..a4a64ad18 100644 --- a/src/Network_Meta_Command.php +++ b/src/Network_Meta_Command.php @@ -15,4 +15,90 @@ */ class Network_Meta_Command extends CommandWithMeta { protected $meta_type = 'site'; + + /** + * Override add_metadata() to use add_network_option() if available. + * + * @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 int|false The meta ID on success, false on failure. + */ + protected function add_metadata( $object_id, $meta_key, $meta_value, $unique = false ) { + if ( function_exists( 'add_network_option' ) && $unique ) { + return add_network_option( $object_id, $meta_key, $meta_value ); + } + return add_metadata( $this->meta_type, $object_id, $meta_key, $meta_value, $unique ); + } + + /** + * Override update_metadata() to use update_network_option() if available. + * + * @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 = '' ) { + if ( function_exists( 'update_network_option' ) && '' === $prev_value ) { + return update_network_option( $object_id, $meta_key, $meta_value ); + } + return update_metadata( $this->meta_type, $object_id, $meta_key, $meta_value, $prev_value ); + } + + /** + * Override get_metadata() to use get_network_option() if available. + * + * @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. + */ + protected function get_metadata( $object_id, $meta_key = '', $single = false ) { + if ( function_exists( 'get_network_option' ) && '' !== $meta_key && $single ) { + return get_network_option( $object_id, $meta_key ); + } + return get_metadata( $this->meta_type, $object_id, $meta_key, $single ); + } + + /** + * Override delete_metadata() to use delete_network_option() if available. + * + * @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 = '' ) { + if ( function_exists( 'delete_network_option' ) && '' === $meta_value ) { + return delete_network_option( $object_id, $meta_key ); + } + return delete_metadata( $this->meta_type, $object_id, $meta_key, $meta_value, false ); + } } From a52134a91fe5b22e657939fa1252efb37b560e1a Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Thu, 19 Mar 2026 12:58:01 +0100 Subject: [PATCH 2/6] Remove `function_exists` checks --- src/Network_Meta_Command.php | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/src/Network_Meta_Command.php b/src/Network_Meta_Command.php index a4a64ad18..d60972ef8 100644 --- a/src/Network_Meta_Command.php +++ b/src/Network_Meta_Command.php @@ -32,10 +32,7 @@ class Network_Meta_Command extends CommandWithMeta { * @return int|false The meta ID on success, false on failure. */ protected function add_metadata( $object_id, $meta_key, $meta_value, $unique = false ) { - if ( function_exists( 'add_network_option' ) && $unique ) { - return add_network_option( $object_id, $meta_key, $meta_value ); - } - return add_metadata( $this->meta_type, $object_id, $meta_key, $meta_value, $unique ); + return add_network_option( $object_id, $meta_key, $meta_value ); } /** @@ -53,10 +50,7 @@ protected function add_metadata( $object_id, $meta_key, $meta_value, $unique = f * update, false on failure. */ protected function update_metadata( $object_id, $meta_key, $meta_value, $prev_value = '' ) { - if ( function_exists( 'update_network_option' ) && '' === $prev_value ) { - return update_network_option( $object_id, $meta_key, $meta_value ); - } - return update_metadata( $this->meta_type, $object_id, $meta_key, $meta_value, $prev_value ); + return update_network_option( $object_id, $meta_key, $meta_value ); } /** @@ -73,10 +67,7 @@ protected function update_metadata( $object_id, $meta_key, $meta_value, $prev_va * @return mixed Single metadata value, or array of values. */ protected function get_metadata( $object_id, $meta_key = '', $single = false ) { - if ( function_exists( 'get_network_option' ) && '' !== $meta_key && $single ) { - return get_network_option( $object_id, $meta_key ); - } - return get_metadata( $this->meta_type, $object_id, $meta_key, $single ); + return get_network_option( $object_id, $meta_key ); } /** @@ -96,9 +87,6 @@ protected function get_metadata( $object_id, $meta_key = '', $single = false ) { * @return bool True on successful delete, false on failure. */ protected function delete_metadata( $object_id, $meta_key, $meta_value = '' ) { - if ( function_exists( 'delete_network_option' ) && '' === $meta_value ) { - return delete_network_option( $object_id, $meta_key ); - } - return delete_metadata( $this->meta_type, $object_id, $meta_key, $meta_value, false ); + return delete_network_option( $object_id, $meta_key ); } } From f6c2a4182a9db7e6775a002b0f35bdc5f46a844f Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Sun, 22 Mar 2026 16:43:04 +0100 Subject: [PATCH 3/6] Lint fix --- src/Network_Meta_Command.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/Network_Meta_Command.php b/src/Network_Meta_Command.php index d60972ef8..da50cc8e8 100644 --- a/src/Network_Meta_Command.php +++ b/src/Network_Meta_Command.php @@ -17,7 +17,7 @@ class Network_Meta_Command extends CommandWithMeta { protected $meta_type = 'site'; /** - * Override add_metadata() to use add_network_option() if available. + * 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. @@ -29,14 +29,16 @@ class Network_Meta_Command extends CommandWithMeta { * value for the specified metadata key, no change * will be made. * - * @return int|false The meta ID on success, false on failure. + * @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() if available. + * 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. @@ -54,7 +56,7 @@ protected function update_metadata( $object_id, $meta_key, $meta_value, $prev_va } /** - * Override get_metadata() to use get_network_option() if available. + * 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, @@ -65,13 +67,15 @@ protected function update_metadata( $object_id, $meta_key, $meta_value, $prev_va * 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() if available. + * 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 From 0978ba4af2fdc86fc93629f597073906a3db0838 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Mon, 23 Mar 2026 20:16:18 +0000 Subject: [PATCH 4/6] Fix lint --- src/Network_Meta_Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Network_Meta_Command.php b/src/Network_Meta_Command.php index da50cc8e8..000411855 100644 --- a/src/Network_Meta_Command.php +++ b/src/Network_Meta_Command.php @@ -30,7 +30,7 @@ class Network_Meta_Command extends CommandWithMeta { * 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 ) { From c42149eda438645a4dc8ace05871b52f6a8c10bf Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 24 Mar 2026 18:22:40 +0100 Subject: [PATCH 5/6] Trigger CI From f41b67ae15b4194c030717058c97464d6333d5ed Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 24 Mar 2026 22:23:16 +0100 Subject: [PATCH 6/6] Add test (WIP) --- features/network-meta.feature | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/features/network-meta.feature b/features/network-meta.feature index 7fffd042e..c825bf5aa 100644 --- a/features/network-meta.feature +++ b/features/network-meta.feature @@ -16,3 +16,32 @@ Feature: Manage network-wide custom fields. This is not a multisite install """ And the return code should be 1 + + # TODO: FIXME + 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 + """ \ No newline at end of file