Skip to content

Commit cad3602

Browse files
committed
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.
1 parent 6eaa622 commit cad3602

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

src/Network_Meta_Command.php

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

0 commit comments

Comments
 (0)