Ticket #25344: 25344.10.network-meta.diff
| File 25344.10.network-meta.diff, 29.3 KB (added by , 10 years ago) |
|---|
-
src/wp-includes/class-wp-network-query.php
42 42 ); 43 43 44 44 /** 45 * Metadata query container. 46 * 47 * @since 4.8.0 48 * @access public 49 * @var object WP_Meta_Query 50 */ 51 public $meta_query = false; 52 53 /** 54 * Metadata query clauses. 55 * 56 * @since 4.8.0 57 * @access protected 58 * @var array 59 */ 60 protected $meta_query_clauses = array(); 61 62 /** 45 63 * Query vars set by the user. 46 64 * 47 65 * @since 4.6.0 … … 92 110 * Sets up the network query, based on the query vars passed. 93 111 * 94 112 * @since 4.6.0 113 * @since 4.8.0 Introduced the `update_network_meta_cache`, `meta_query`, 114 * `meta_key`, `meta_value`, `meta_type` and `meta_compare` 115 * parameters. 95 116 * @access public 96 117 * 97 118 * @param string|array $query { 98 119 * Optional. Array or query string of network query parameters. Default empty. 99 120 * 100 * @type array $network__in Array of network IDs to include. Default empty. 101 * @type array $network__not_in Array of network IDs to exclude. Default empty. 102 * @type bool $count Whether to return a network count (true) or array of network objects. 103 * Default false. 104 * @type string $fields Network fields to return. Accepts 'ids' (returns an array of network IDs) 105 * or empty (returns an array of complete network objects). Default empty. 106 * @type int $number Maximum number of networks to retrieve. Default empty (no limit). 107 * @type int $offset Number of networks to offset the query. Used to build LIMIT clause. 108 * Default 0. 109 * @type bool $no_found_rows Whether to disable the `SQL_CALC_FOUND_ROWS` query. Default true. 110 * @type string|array $orderby Network status or array of statuses. Accepts 'id', 'domain', 'path', 111 * 'domain_length', 'path_length' and 'network__in'. Also accepts false, 112 * an empty array, or 'none' to disable `ORDER BY` clause. Default 'id'. 113 * @type string $order How to order retrieved networks. Accepts 'ASC', 'DESC'. Default 'ASC'. 114 * @type string $domain Limit results to those affiliated with a given domain. Default empty. 115 * @type array $domain__in Array of domains to include affiliated networks for. Default empty. 116 * @type array $domain__not_in Array of domains to exclude affiliated networks for. Default empty. 117 * @type string $path Limit results to those affiliated with a given path. Default empty. 118 * @type array $path__in Array of paths to include affiliated networks for. Default empty. 119 * @type array $path__not_in Array of paths to exclude affiliated networks for. Default empty. 120 * @type string $search Search term(s) to retrieve matching networks for. Default empty. 121 * @type bool $update_network_cache Whether to prime the cache for found networks. Default true. 121 * @type array $network__in Array of network IDs to include. Default empty. 122 * @type array $network__not_in Array of network IDs to exclude. Default empty. 123 * @type bool $count Whether to return a network count (true) or array of network objects. 124 * Default false. 125 * @type string $fields Network fields to return. Accepts 'ids' (returns an array of network IDs) 126 * or empty (returns an array of complete network objects). Default empty. 127 * @type int $number Maximum number of networks to retrieve. Default empty (no limit). 128 * @type int $offset Number of networks to offset the query. Used to build LIMIT clause. 129 * Default 0. 130 * @type bool $no_found_rows Whether to disable the `SQL_CALC_FOUND_ROWS` query. Default true. 131 * @type string|array $orderby Network status or array of statuses. Accepts 'id', 'domain', 'path', 132 * 'domain_length', 'path_length' and 'network__in'. Also accepts false, 133 * an empty array, or 'none' to disable `ORDER BY` clause. Default 'id'. 134 * @type string $order How to order retrieved networks. Accepts 'ASC', 'DESC'. Default 'ASC'. 135 * @type string $domain Limit results to those affiliated with a given domain. Default empty. 136 * @type array $domain__in Array of domains to include affiliated networks for. Default empty. 137 * @type array $domain__not_in Array of domains to exclude affiliated networks for. Default empty. 138 * @type string $path Limit results to those affiliated with a given path. Default empty. 139 * @type array $path__in Array of paths to include affiliated networks for. Default empty. 140 * @type array $path__not_in Array of paths to exclude affiliated networks for. Default empty. 141 * @type string $search Search term(s) to retrieve matching networks for. Default empty. 142 * @type bool $update_network_cache Whether to prime the cache for found networks. Default true. 143 * @type bool $update_network_meta_cache Whether to prime meta caches for found networks. Default true. 144 * @type array $meta_query Optional. Meta query clauses to limit retrieved networks by. 145 * See `WP_Meta_Query`. Default empty. 146 * @type string $meta_key Limit networks to those matching a specific metadata key. 147 * Can be used in conjunction with `$meta_value`. Default empty. 148 * @type string $meta_value Limit networks to those matching a specific metadata value. 149 * Usually used in conjunction with `$meta_key`. Default empty. 150 * @type string $meta_type Type of object metadata is for (e.g., comment, post, or user). 151 * Default empty. 152 * @type string $meta_compare Comparison operator to test the 'meta_value'. Default empty. 122 153 * } 123 154 */ 124 155 public function __construct( $query = '' ) { 125 156 $this->query_var_defaults = array( 126 'network__in' => '', 127 'network__not_in' => '', 128 'count' => false, 129 'fields' => '', 130 'number' => '', 131 'offset' => '', 132 'no_found_rows' => true, 133 'orderby' => 'id', 134 'order' => 'ASC', 135 'domain' => '', 136 'domain__in' => '', 137 'domain__not_in' => '', 138 'path' => '', 139 'path__in' => '', 140 'path__not_in' => '', 141 'search' => '', 142 'update_network_cache' => true, 157 'network__in' => '', 158 'network__not_in' => '', 159 'count' => false, 160 'fields' => '', 161 'number' => '', 162 'offset' => '', 163 'no_found_rows' => true, 164 'orderby' => 'id', 165 'order' => 'ASC', 166 'domain' => '', 167 'domain__in' => '', 168 'domain__not_in' => '', 169 'path' => '', 170 'path__in' => '', 171 'path__not_in' => '', 172 'search' => '', 173 'update_network_cache' => true, 174 'update_network_meta_cache' => true, 175 'meta_query' => '', 176 'meta_key' => '', 177 'meta_value' => '', 178 'meta_type' => '', 179 'meta_compare' => '', 143 180 ); 144 181 145 182 if ( ! empty( $query ) ) { … … 193 230 * @since 4.6.0 194 231 * @access public 195 232 * 233 * @global wpdb $wpdb WordPress database abstraction object. 234 * 196 235 * @return int|array The list of networks. 197 236 */ 198 237 public function get_networks() { 238 global $wpdb; 239 199 240 $this->parse_query(); 200 241 242 // Set up meta_query so it's available to 'pre_get_networks'. 243 if ( class_exists( 'WP_Meta_Query' ) ) { 244 $this->meta_query = new WP_Meta_Query(); 245 $this->meta_query->parse_query_vars( $this->query_vars ); 246 } 247 201 248 /** 202 249 * Fires before networks are retrieved. 203 250 * … … 207 254 */ 208 255 do_action_ref_array( 'pre_get_networks', array( &$this ) ); 209 256 257 // Reparse query vars, in case they were modified in a 'pre_get_comments' callback. 258 if ( $this->meta_query ) { 259 $this->meta_query->parse_query_vars( $this->query_vars ); 260 if ( ! empty( $this->meta_query->queries ) ) { 261 $this->meta_query_clauses = $this->meta_query->get_sql( 'site', $wpdb->site, 'id', $this ); 262 } 263 } 264 210 265 // $args can include anything. Only use the args defined in the query_var_defaults to compute the key. 211 266 $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) ); 212 267 $last_changed = wp_cache_get_last_changed( 'networks' ); … … 248 303 } 249 304 250 305 if ( $this->query_vars['update_network_cache'] ) { 251 _prime_network_caches( $network_ids ); 306 if ( $this->meta_query ) { 307 _prime_network_caches( $network_ids, $this->query_vars['update_network_meta_cache'] ); 308 } else { 309 _prime_network_caches( $network_ids, false ); 310 } 252 311 } 253 312 254 313 // Fetch full network objects from the primed cache. … … 394 453 395 454 $join = ''; 396 455 456 if ( ! empty( $this->meta_query_clauses ) ) { 457 $join .= $this->meta_query_clauses['join']; 458 459 // Strip leading 'AND'. 460 $this->sql_clauses['where']['meta_query'] = preg_replace( '/^\s*AND\s*/', '', $this->meta_query_clauses['where'] ); 461 462 if ( ! $this->query_vars['count'] ) { 463 $groupby = "$wpdb->site.id"; 464 } 465 } 466 397 467 $where = implode( ' AND ', $this->sql_clauses['where'] ); 398 468 399 469 $pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' ); … … 533 603 $parsed = "$wpdb->site.$orderby"; 534 604 } 535 605 606 if ( ! $parsed && $this->meta_query ) { 607 switch ( $orderby ) { 608 case $this->query_vars['meta_key']: 609 case 'meta_value': 610 $parsed = "$wpdb->sitemeta.meta_value"; 611 break; 612 case 'meta_value_num': 613 $parsed = "$wpdb->sitemeta.meta_value+0"; 614 break; 615 default: 616 $meta_query_clauses = $this->meta_query->get_clauses(); 617 if ( $meta_query_clauses && isset( $meta_query_clauses[ $orderby ] ) ) { 618 $meta_clause = $meta_query_clauses[ $orderby ]; 619 $parsed = sprintf( "CAST(%s.meta_value AS %s)", esc_sql( $meta_clause['alias'] ), esc_sql( $meta_clause['cast'] ) ); 620 } 621 } 622 } 623 536 624 return $parsed; 537 625 } 538 626 -
src/wp-includes/load.php
515 515 } 516 516 517 517 if ( function_exists( 'wp_cache_add_global_groups' ) ) { 518 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'site-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites' ) );518 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'site-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site_meta' ) ); 519 519 wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) ); 520 520 } 521 521 } -
src/wp-includes/ms-blogs.php
842 842 if ( is_array( $global_groups ) ) { 843 843 wp_cache_add_global_groups( $global_groups ); 844 844 } else { 845 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details' ) );845 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details', 'site_meta' ) ); 846 846 } 847 847 wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) ); 848 848 } … … 916 916 if ( is_array( $global_groups ) ) { 917 917 wp_cache_add_global_groups( $global_groups ); 918 918 } else { 919 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details' ) );919 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details', 'site_meta' ) ); 920 920 } 921 921 wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) ); 922 922 } … … 1153 1153 } 1154 1154 1155 1155 /** 1156 * Add metadata to a network. 1157 * 1158 * @since 4.8.0 1159 * 1160 * @param int $id Network ID. 1161 * @param string $meta_key Metadata name. 1162 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. 1163 * @param bool $unique Optional. Whether the same key should not be added. 1164 * Default false. 1165 * @return int|false Meta ID on success, false on failure. 1166 */ 1167 function add_network_meta( $id, $meta_key, $meta_value, $unique = false ) { 1168 $added = add_metadata( 'site', $id, $meta_key, $meta_value, $unique ); 1169 1170 if ( $added ) { 1171 wp_cache_set( 'last_changed', microtime(), 'networks' ); 1172 } 1173 1174 return $added; 1175 } 1176 1177 /** 1178 * Remove from a network, metadata matching key and/or value. 1179 * 1180 * You can match based on the key, or key and value. Removing based on key and 1181 * value, will keep from removing duplicate metadata with the same key. It also 1182 * allows removing all metadata matching key, if needed. 1183 * 1184 * @since 4.8.0 1185 * 1186 * @param int $id Network ID. 1187 * @param string $meta_key Metadata name. 1188 * @param mixed $meta_value Optional. Metadata value. Must be serializable if 1189 * non-scalar. Default empty. 1190 * @return bool True on success, false on failure. 1191 */ 1192 function delete_network_meta( $id, $meta_key, $meta_value = '' ) { 1193 $deleted = delete_metadata( 'site', $id, $meta_key, $meta_value ); 1194 1195 if ( $deleted ) { 1196 wp_cache_set( 'last_changed', microtime(), 'networks' ); 1197 } 1198 1199 return $deleted; 1200 } 1201 1202 /** 1203 * Retrieve from a network, metadata value by key. 1204 * 1205 * @since 4.8.0 1206 * 1207 * @param int $id Network ID. 1208 * @param string $meta_key Optional. The meta key to retrieve. By default, returns 1209 * data for all keys. Default empty. 1210 * @param bool $single Optional. Whether to return a single value. Default false. 1211 * @return mixed Will be an array if $single is false. Will be value of meta data 1212 * field if $single is true. 1213 */ 1214 function get_network_meta( $id, $meta_key = '', $single = false ) { 1215 return get_metadata( 'site', $id, $meta_key, $single ); 1216 } 1217 1218 /** 1219 * Update metadata for a network ID, and/or key, and/or value. 1220 * 1221 * Use the $prev_value parameter to differentiate between meta fields with the 1222 * same key and network ID. 1223 * 1224 * If the meta field for the network does not exist, it will be added. 1225 * 1226 * @since 4.8.0 1227 * 1228 * @param int $id Network ID. 1229 * @param string $meta_key Metadata key. 1230 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. 1231 * @param mixed $prev_value Optional. Previous value to check before removing. 1232 * Default empty. 1233 * @return int|bool Meta ID if the key didn't exist, true on successful update, 1234 * false on failure. 1235 */ 1236 function update_network_meta( $id, $meta_key, $meta_value, $prev_value = '' ) { 1237 $updated = update_metadata( 'site', $id, $meta_key, $meta_value, $prev_value ); 1238 1239 if ( $updated ) { 1240 wp_cache_set( 'last_changed', microtime(), 'networks' ); 1241 } 1242 1243 return $updated; 1244 } 1245 1246 /** 1247 * Delete everything from network meta matching meta key. 1248 * 1249 * @since 4.8.0 1250 * 1251 * @param string $network_meta_key Key to search for when deleting. 1252 * 1253 * @return bool Whether the network meta key was deleted from the database. 1254 */ 1255 function delete_network_meta_by_key( $network_meta_key ) { 1256 $deleted = delete_metadata( 'site', null, $network_meta_key, '', true ); 1257 1258 if ( $deleted ) { 1259 wp_cache_set( 'last_changed', microtime(), 'networks' ); 1260 } 1261 1262 return $deleted; 1263 } 1264 1265 /** 1156 1266 * Removes a network from the object cache. 1157 1267 * 1158 1268 * @since 4.6.0 … … 1170 1280 1171 1281 foreach ( (array) $ids as $id ) { 1172 1282 wp_cache_delete( $id, 'networks' ); 1283 wp_cache_delete( $id, 'site_meta' ); 1173 1284 1174 1285 /** 1175 1286 * Fires immediately after a network has been removed from the object cache. … … 1192 1303 * cache using the network group with the key using the ID of the networks. 1193 1304 * 1194 1305 * @since 4.6.0 1306 * @since 4.8.0 Introduced the `$update_meta_cache` parameter. 1195 1307 * 1196 * @param array $networks Array of network row objects. 1308 * @param array $networks Array of network row objects. 1309 * @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true. 1197 1310 */ 1198 function update_network_cache( $networks ) {1311 function update_network_cache( $networks, $update_meta_cache = true ) { 1199 1312 foreach ( (array) $networks as $network ) { 1200 1313 wp_cache_add( $network->id, $network, 'networks' ); 1201 1314 } 1315 1316 if ( $update_meta_cache ) { 1317 update_networkmeta_cache( wp_list_pluck( $networks, 'id' ) ); 1318 } 1319 } 1320 1321 /** 1322 * Updates metadata cache for list of network IDs. 1323 * 1324 * Performs SQL query to retrieve the metadata for the network IDs and updates the 1325 * metadata cache for the posts. Therefore, the functions, which call this 1326 * function, do not need to perform SQL queries on their own. 1327 * 1328 * @since 4.8.0 1329 * 1330 * @param array $network_ids List of network IDs. 1331 * @return array|false Returns false if there is nothing to update or an array 1332 * of metadata. 1333 */ 1334 function update_networkmeta_cache( $network_ids) { 1335 return update_meta_cache( 'site', $network_ids ); 1202 1336 } 1203 1337 1204 1338 /** 1205 1339 * Adds any networks from the given IDs to the cache that do not already exist in cache. 1206 1340 * 1207 1341 * @since 4.6.0 1342 * @since 4.8.0 Introduced the `$update_meta_cache` parameter. 1208 1343 * @access private 1209 1344 * 1210 1345 * @see update_network_cache() 1211 1346 * @global wpdb $wpdb WordPress database abstraction object. 1212 1347 * 1213 * @param array $network_ids Array of network IDs. 1348 * @param array $network_ids Array of network IDs. 1349 * @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true. 1214 1350 */ 1215 function _prime_network_caches( $network_ids ) {1351 function _prime_network_caches( $network_ids, $update_meta_cache = true ) { 1216 1352 global $wpdb; 1217 1353 1218 1354 $non_cached_ids = _get_non_cached_ids( $network_ids, 'networks' ); 1219 if ( ! empty( $non_cached_ids ) ) {1220 $fresh_networks = $wpdb->get_results( sprintf( "SELECT $wpdb->site.* FROM $wpdb->siteWHERE id IN (%s)", join( ",", array_map( 'intval', $non_cached_ids ) ) ) );1355 if ( ! empty( $non_cached_ids ) ) { 1356 $fresh_networks = $wpdb->get_results( sprintf( "SELECT * FROM {$wpdb->site} WHERE id IN (%s)", join( ",", array_map( 'intval', $non_cached_ids ) ) ) ); 1221 1357 1222 update_network_cache( $fresh_networks );1358 update_network_cache( $fresh_networks, $update_meta_cache ); 1223 1359 } 1224 1360 } 1225 1361 -
tests/phpunit/includes/testcase.php
301 301 $wp_object_cache->__remoteset(); 302 302 } 303 303 wp_cache_flush(); 304 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details' ) );304 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details', 'site_meta' ) ); 305 305 wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) ); 306 306 } 307 307 -
tests/phpunit/tests/cache.php
22 22 global $wp_object_cache; 23 23 $cache_class = get_class( $wp_object_cache ); 24 24 $cache = new $cache_class(); 25 $cache->add_global_groups( array( 'global-cache-test', 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details' ) );25 $cache->add_global_groups( array( 'global-cache-test', 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details', 'site_meta' ) ); 26 26 return $cache; 27 27 } 28 28 -
tests/phpunit/tests/multisite/networkMeta.php
1 <?php 2 3 if ( is_multisite() ) : 4 /** 5 * @group ms-network 6 * @group multisite 7 * @group meta 8 * @ticket 25344 9 */ 10 class Tests_Multisite_Network_Meta extends WP_UnitTestCase { 11 protected static $network_id; 12 protected static $network_id2; 13 14 public static function wpSetUpBeforeClass( $factory ) { 15 self::$network_id = $factory->network->create( array( 'domain' => 'wordpress.org', 'path' => '/' ) ); 16 self::$network_id2 = $factory->network->create( array( 'domain' => 'wordpress.org', 'path' => '/foo/' ) ); 17 } 18 19 public static function wpTearDownAfterClass() { 20 global $wpdb; 21 22 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", self::$network_id ) ); 23 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", self::$network_id ) ); 24 25 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", self::$network_id2 ) ); 26 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", self::$network_id2 ) ); 27 } 28 29 public function test_add() { 30 $this->assertNotEmpty( add_network_meta( self::$network_id, 'foo', 'bar' ) ); 31 } 32 33 public function test_add_unique() { 34 $this->assertNotEmpty( add_network_meta( self::$network_id, 'foo', 'bar' ) ); 35 $this->assertFalse( add_network_meta( self::$network_id, 'foo', 'bar', true ) ); 36 } 37 38 public function test_delete() { 39 add_network_meta( self::$network_id, 'foo', 'bar' ); 40 41 $this->assertTrue( delete_network_meta( self::$network_id, 'foo' ) ); 42 } 43 44 public function test_delete_with_invalid_meta_key_should_return_false() { 45 $this->assertFalse( delete_network_meta( self::$network_id, 'foo' ) ); 46 } 47 48 public function test_delete_should_respect_meta_value() { 49 add_network_meta( self::$network_id, 'foo', 'bar' ); 50 add_network_meta( self::$network_id, 'foo', 'baz' ); 51 52 $this->assertTrue( delete_network_meta( self::$network_id, 'foo', 'bar' ) ); 53 54 $metas = get_network_meta( self::$network_id, 'foo', false ); 55 $this->assertSame( array( 'baz' ), $metas ); 56 } 57 58 public function test_get_with_no_key_should_fetch_all_keys() { 59 add_network_meta( self::$network_id, 'foo', 'bar' ); 60 add_network_meta( self::$network_id, 'foo1', 'baz' ); 61 62 $found = get_network_meta( self::$network_id ); 63 64 $expected = array( 65 'foo' => array( 'bar' ), 66 'foo1' => array( 'baz' ), 67 ); 68 69 // Ignore default network options. 70 $found = array_intersect_key( $found, $expected ); 71 72 $this->assertEqualSets( $expected, $found ); 73 } 74 75 public function test_get_with_key_should_fetch_all_for_key() { 76 add_network_meta( self::$network_id, 'foo', 'bar' ); 77 add_network_meta( self::$network_id, 'foo', 'baz' ); 78 add_network_meta( self::$network_id, 'foo1', 'baz' ); 79 80 $found = get_network_meta( self::$network_id, 'foo' ); 81 $expected = array( 'bar', 'baz' ); 82 83 $this->assertEqualSets( $expected, $found ); 84 } 85 86 public function test_get_should_respect_single_true() { 87 add_network_meta( self::$network_id, 'foo', 'bar' ); 88 add_network_meta( self::$network_id, 'foo', 'baz' ); 89 90 $found = get_network_meta( self::$network_id, 'foo', true ); 91 $this->assertEquals( 'bar', $found ); 92 } 93 94 public function test_update_should_pass_to_add_when_no_value_exists_for_key() { 95 $actual = update_network_meta( self::$network_id, 'foo', 'bar' ); 96 $this->assertInternalType( 'int', $actual ); 97 $this->assertNotEmpty( $actual ); 98 99 $meta = get_network_meta( self::$network_id, 'foo', true ); 100 $this->assertSame( 'bar', $meta ); 101 } 102 103 public function test_update_should_return_true_when_updating_existing_value_for_key() { 104 add_network_meta( self::$network_id, 'foo', 'bar' ); 105 106 $actual = update_network_meta( self::$network_id, 'foo', 'baz' ); 107 $this->assertTrue( $actual ); 108 109 $meta = get_network_meta( self::$network_id, 'foo', true ); 110 $this->assertSame( 'baz', $meta ); 111 } 112 113 public function test_delete_by_key() { 114 add_network_meta( self::$network_id, 'unique_delete_by_key', 'value', true ); 115 add_network_meta( self::$network_id2, 'unique_delete_by_key', 'value', true ); 116 117 $this->assertEquals( 'value', get_network_meta( self::$network_id, 'unique_delete_by_key', true ) ); 118 $this->assertEquals( 'value', get_network_meta( self::$network_id2, 'unique_delete_by_key', true ) ); 119 120 $this->assertTrue( delete_network_meta_by_key( 'unique_delete_by_key' ) ); 121 122 $this->assertEquals( '', get_network_meta( self::$network_id, 'unique_delete_by_key', true ) ); 123 $this->assertEquals( '', get_network_meta( self::$network_id2, 'unique_delete_by_key', true ) ); 124 } 125 126 public function test_adding_network_meta_should_bust_get_networks_cache() { 127 add_network_meta( self::$network_id, 'foo', 'bar' ); 128 129 // Prime cache. 130 $found = get_networks( array( 131 'fields' => 'ids', 132 'meta_query' => array( 133 array( 134 'key' => 'foo', 135 'value' => 'bar', 136 ), 137 ), 138 ) ); 139 140 $this->assertEqualSets( array( self::$network_id ), $found ); 141 142 add_network_meta( self::$network_id2, 'foo', 'bar' ); 143 144 $found = get_networks( array( 145 'fields' => 'ids', 146 'meta_query' => array( 147 array( 148 'key' => 'foo', 149 'value' => 'bar', 150 ), 151 ), 152 ) ); 153 154 $this->assertEqualSets( array( self::$network_id, self::$network_id2 ), $found ); 155 } 156 157 public function test_updating_network_meta_should_bust_get_networks_cache() { 158 add_network_meta( self::$network_id, 'foo', 'bar' ); 159 add_network_meta( self::$network_id2, 'foo', 'baz' ); 160 161 // Prime cache. 162 $found = get_networks( array( 163 'fields' => 'ids', 164 'meta_query' => array( 165 array( 166 'key' => 'foo', 167 'value' => 'bar', 168 ), 169 ), 170 ) ); 171 172 $this->assertEqualSets( array( self::$network_id ), $found ); 173 174 update_network_meta( self::$network_id2, 'foo', 'bar' ); 175 176 $found = get_networks( array( 177 'fields' => 'ids', 178 'meta_query' => array( 179 array( 180 'key' => 'foo', 181 'value' => 'bar', 182 ), 183 ), 184 ) ); 185 186 $this->assertEqualSets( array( self::$network_id, self::$network_id2 ), $found ); 187 } 188 189 public function test_deleting_network_meta_should_bust_get_networks_cache() { 190 add_network_meta( self::$network_id, 'foo', 'bar' ); 191 add_network_meta( self::$network_id2, 'foo', 'bar' ); 192 193 // Prime cache. 194 $found = get_networks( array( 195 'fields' => 'ids', 196 'meta_query' => array( 197 array( 198 'key' => 'foo', 199 'value' => 'bar', 200 ), 201 ), 202 ) ); 203 204 $this->assertEqualSets( array( self::$network_id, self::$network_id2 ), $found ); 205 206 delete_network_meta( self::$network_id2, 'foo', 'bar' ); 207 208 $found = get_networks( array( 209 'fields' => 'ids', 210 'meta_query' => array( 211 array( 212 'key' => 'foo', 213 'value' => 'bar', 214 ), 215 ), 216 ) ); 217 218 $this->assertEqualSets( array( self::$network_id ), $found ); 219 } 220 } 221 222 endif;
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)