Ticket #25344: 25344.10.network-meta-without-query.diff
| File 25344.10.network-meta-without-query.diff, 22.3 KB (added by , 10 years ago) |
|---|
-
src/wp-includes/class-wp-network-query.php
92 92 * Sets up the network query, based on the query vars passed. 93 93 * 94 94 * @since 4.6.0 95 * @since 4.8.0 Introduced the `update_network_meta_cache` parameter. 95 96 * @access public 96 97 * 97 98 * @param string|array $query { 98 99 * Optional. Array or query string of network query parameters. Default empty. 99 100 * 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. 101 * @type array $network__in Array of network IDs to include. Default empty. 102 * @type array $network__not_in Array of network IDs to exclude. Default empty. 103 * @type bool $count Whether to return a network count (true) or array of network objects. 104 * Default false. 105 * @type string $fields Network fields to return. Accepts 'ids' (returns an array of network IDs) 106 * or empty (returns an array of complete network objects). Default empty. 107 * @type int $number Maximum number of networks to retrieve. Default empty (no limit). 108 * @type int $offset Number of networks to offset the query. Used to build LIMIT clause. 109 * Default 0. 110 * @type bool $no_found_rows Whether to disable the `SQL_CALC_FOUND_ROWS` query. Default true. 111 * @type string|array $orderby Network status or array of statuses. Accepts 'id', 'domain', 'path', 112 * 'domain_length', 'path_length' and 'network__in'. Also accepts false, 113 * an empty array, or 'none' to disable `ORDER BY` clause. Default 'id'. 114 * @type string $order How to order retrieved networks. Accepts 'ASC', 'DESC'. Default 'ASC'. 115 * @type string $domain Limit results to those affiliated with a given domain. Default empty. 116 * @type array $domain__in Array of domains to include affiliated networks for. Default empty. 117 * @type array $domain__not_in Array of domains to exclude affiliated networks for. Default empty. 118 * @type string $path Limit results to those affiliated with a given path. Default empty. 119 * @type array $path__in Array of paths to include affiliated networks for. Default empty. 120 * @type array $path__not_in Array of paths to exclude affiliated networks for. Default empty. 121 * @type string $search Search term(s) to retrieve matching networks for. Default empty. 122 * @type bool $update_network_cache Whether to prime the cache for found networks. Default true. 123 * @type bool $update_network_meta_cache Whether to prime meta caches for found networks. Default true. 122 124 * } 123 125 */ 124 126 public function __construct( $query = '' ) { 125 127 $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, 128 'network__in' => '', 129 'network__not_in' => '', 130 'count' => false, 131 'fields' => '', 132 'number' => '', 133 'offset' => '', 134 'no_found_rows' => true, 135 'orderby' => 'id', 136 'order' => 'ASC', 137 'domain' => '', 138 'domain__in' => '', 139 'domain__not_in' => '', 140 'path' => '', 141 'path__in' => '', 142 'path__not_in' => '', 143 'search' => '', 144 'update_network_cache' => true, 145 'update_network_meta_cache' => true, 143 146 ); 144 147 145 148 if ( ! empty( $query ) ) { … … 248 251 } 249 252 250 253 if ( $this->query_vars['update_network_cache'] ) { 251 _prime_network_caches( $network_ids );254 _prime_network_caches( $network_ids, $this->query_vars['update_network_meta_cache'] ); 252 255 } 253 256 254 257 // Fetch full network objects from the primed cache. -
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 return add_metadata( 'site', $id, $meta_key, $meta_value, $unique ); 1169 } 1170 1171 /** 1172 * Remove from a network, metadata matching key and/or value. 1173 * 1174 * You can match based on the key, or key and value. Removing based on key and 1175 * value, will keep from removing duplicate metadata with the same key. It also 1176 * allows removing all metadata matching key, if needed. 1177 * 1178 * @since 4.8.0 1179 * 1180 * @param int $id Network ID. 1181 * @param string $meta_key Metadata name. 1182 * @param mixed $meta_value Optional. Metadata value. Must be serializable if 1183 * non-scalar. Default empty. 1184 * @return bool True on success, false on failure. 1185 */ 1186 function delete_network_meta( $id, $meta_key, $meta_value = '' ) { 1187 return delete_metadata( 'site', $id, $meta_key, $meta_value ); 1188 } 1189 1190 /** 1191 * Retrieve from a network, metadata value by key. 1192 * 1193 * @since 4.8.0 1194 * 1195 * @param int $id Network ID. 1196 * @param string $meta_key Optional. The meta key to retrieve. By default, returns 1197 * data for all keys. Default empty. 1198 * @param bool $single Optional. Whether to return a single value. Default false. 1199 * @return mixed Will be an array if $single is false. Will be value of meta data 1200 * field if $single is true. 1201 */ 1202 function get_network_meta( $id, $meta_key = '', $single = false ) { 1203 return get_metadata( 'site', $id, $meta_key, $single ); 1204 } 1205 1206 /** 1207 * Update metadata for a network ID, and/or key, and/or value. 1208 * 1209 * Use the $prev_value parameter to differentiate between meta fields with the 1210 * same key and network ID. 1211 * 1212 * If the meta field for the network does not exist, it will be added. 1213 * 1214 * @since 4.8.0 1215 * 1216 * @param int $id Network ID. 1217 * @param string $meta_key Metadata key. 1218 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. 1219 * @param mixed $prev_value Optional. Previous value to check before removing. 1220 * Default empty. 1221 * @return int|bool Meta ID if the key didn't exist, true on successful update, 1222 * false on failure. 1223 */ 1224 function update_network_meta( $id, $meta_key, $meta_value, $prev_value = '' ) { 1225 return update_metadata( 'site', $id, $meta_key, $meta_value, $prev_value ); 1226 } 1227 1228 /** 1229 * Delete everything from network meta matching meta key. 1230 * 1231 * @since 4.8.0 1232 * 1233 * @param string $network_meta_key Key to search for when deleting. 1234 * 1235 * @return bool Whether the network meta key was deleted from the database. 1236 */ 1237 function delete_network_meta_by_key( $network_meta_key ) { 1238 return delete_metadata( 'site', null, $network_meta_key, '', true ); 1239 } 1240 1241 /** 1156 1242 * Removes a network from the object cache. 1157 1243 * 1158 1244 * @since 4.6.0 … … 1170 1256 1171 1257 foreach ( (array) $ids as $id ) { 1172 1258 wp_cache_delete( $id, 'networks' ); 1259 wp_cache_delete( $id, 'site_meta' ); 1173 1260 1174 1261 /** 1175 1262 * Fires immediately after a network has been removed from the object cache. … … 1192 1279 * cache using the network group with the key using the ID of the networks. 1193 1280 * 1194 1281 * @since 4.6.0 1282 * @since 4.8.0 Introduced the `$update_meta_cache` parameter. 1195 1283 * 1196 * @param array $networks Array of network row objects. 1284 * @param array $networks Array of network row objects. 1285 * @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true. 1197 1286 */ 1198 function update_network_cache( $networks ) {1287 function update_network_cache( $networks, $update_meta_cache = true ) { 1199 1288 foreach ( (array) $networks as $network ) { 1200 1289 wp_cache_add( $network->id, $network, 'networks' ); 1201 1290 } 1291 1292 if ( $update_meta_cache ) { 1293 update_networkmeta_cache( wp_list_pluck( $networks, 'id' ) ); 1294 } 1202 1295 } 1203 1296 1204 1297 /** 1298 * Updates metadata cache for list of network IDs. 1299 * 1300 * Performs SQL query to retrieve the metadata for the network IDs and updates the 1301 * metadata cache for the posts. Therefore, the functions, which call this 1302 * function, do not need to perform SQL queries on their own. 1303 * 1304 * @since 4.8.0 1305 * 1306 * @param array $network_ids List of network IDs. 1307 * @return array|false Returns false if there is nothing to update or an array 1308 * of metadata. 1309 */ 1310 function update_networkmeta_cache( $network_ids) { 1311 return update_meta_cache( 'site', $network_ids ); 1312 } 1313 1314 /** 1205 1315 * Adds any networks from the given IDs to the cache that do not already exist in cache. 1206 1316 * 1207 1317 * @since 4.6.0 1318 * @since 4.8.0 Introduced the `$update_meta_cache` parameter. 1208 1319 * @access private 1209 1320 * 1210 1321 * @see update_network_cache() 1211 1322 * @global wpdb $wpdb WordPress database abstraction object. 1212 1323 * 1213 * @param array $network_ids Array of network IDs. 1324 * @param array $network_ids Array of network IDs. 1325 * @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true. 1214 1326 */ 1215 function _prime_network_caches( $network_ids ) {1327 function _prime_network_caches( $network_ids, $update_meta_cache = true ) { 1216 1328 global $wpdb; 1217 1329 1218 1330 $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 ) ) ) );1331 if ( ! empty( $non_cached_ids ) ) { 1332 $fresh_networks = $wpdb->get_results( sprintf( "SELECT * FROM {$wpdb->site} WHERE id IN (%s)", join( ",", array_map( 'intval', $non_cached_ids ) ) ) ); 1221 1333 1222 update_network_cache( $fresh_networks );1334 update_network_cache( $fresh_networks, $update_meta_cache ); 1223 1335 } 1224 1336 } 1225 1337 -
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 127 endif;
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)