Ticket #37181: 37181.2.diff
File 37181.2.diff, 14.0 KB (added by , 7 years ago) |
---|
-
src/wp-includes/class-wp-network-query.php
111 111 * @type array $path__not_in Array of paths to exclude affiliated networks for. Default empty. 112 112 * @type string $search Search term(s) to retrieve matching networks for. Default empty. 113 113 * @type bool $update_network_cache Whether to prime the cache for found networks. Default true. 114 * @type bool $update_network_meta_cache Whether to prime the metadata (option) cache for found networks. 115 * Default true. 114 116 * } 115 117 */ 116 118 public function __construct( $query = '' ) { … … 132 134 'path__not_in' => '', 133 135 'search' => '', 134 136 'update_network_cache' => true, 137 'update_network_meta_cache' => true, 135 138 ); 136 139 137 140 if ( ! empty( $query ) ) { … … 242 245 } 243 246 244 247 if ( $this->query_vars['update_network_cache'] ) { 245 _prime_network_caches( $network_ids );248 _prime_network_caches( $network_ids, $this->query_vars['update_network_meta_cache'] ); 246 249 } 247 250 248 251 // Fetch full network objects from the primed cache. -
src/wp-includes/load.php
542 542 543 543 require( ABSPATH . WPINC . '/kses.php' ); 544 544 require( ABSPATH . WPINC . '/pluggable.php' ); 545 require( ABSPATH . WPINC . '/formatting.php' );546 545 547 546 $link = wp_guess_url() . '/wp-admin/install.php'; 548 547 -
src/wp-includes/ms-blogs.php
1192 1192 * cache using the network group with the key using the ID of the networks. 1193 1193 * 1194 1194 * @since 4.6.0 1195 * @since 4.9.0 Introduced the `$update_meta_cache` parameter. 1195 1196 * 1196 1197 * @param array $networks Array of network row objects. 1198 * @param bool $update_meta_cache Whether to update sitemeta cache. Default true. 1197 1199 */ 1198 function update_network_cache( $networks ) { 1200 function update_network_cache( $networks, $update_meta_cache = true ) { 1201 $network_ids = array(); 1199 1202 foreach ( (array) $networks as $network ) { 1203 $network_ids[] = $network->id; 1200 1204 wp_cache_add( $network->id, $network, 'networks' ); 1201 1205 } 1206 1207 if ( $update_meta_cache ) { 1208 update_meta_cache( 'site', $network_ids ); 1209 } 1202 1210 } 1203 1211 1204 1212 /** 1205 1213 * Adds any networks from the given IDs to the cache that do not already exist in cache. 1206 1214 * 1207 1215 * @since 4.6.0 1216 * @since 4.9.0 Introduced the `$update_meta_cache` parameter. 1208 1217 * @access private 1209 1218 * 1210 1219 * @see update_network_cache() 1211 1220 * @global wpdb $wpdb WordPress database abstraction object. 1212 1221 * 1213 1222 * @param array $network_ids Array of network IDs. 1223 * @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true. 1214 1224 */ 1215 function _prime_network_caches( $network_ids ) {1225 function _prime_network_caches( $network_ids, $update_meta_cache = true ) { 1216 1226 global $wpdb; 1217 1227 1218 1228 $non_cached_ids = _get_non_cached_ids( $network_ids, 'networks' ); 1219 1229 if ( !empty( $non_cached_ids ) ) { 1220 1230 $fresh_networks = $wpdb->get_results( sprintf( "SELECT $wpdb->site.* FROM $wpdb->site WHERE id IN (%s)", join( ",", array_map( 'intval', $non_cached_ids ) ) ) ); 1221 1231 1222 update_network_cache( $fresh_networks );1232 update_network_cache( $fresh_networks, $update_meta_cache ); 1223 1233 } 1224 1234 } 1225 1235 -
src/wp-includes/option.php
214 214 * 215 215 * @global wpdb $wpdb WordPress database abstraction object. 216 216 * 217 * @param int $ site_id Optional site ID for which to query the options. Defaults to the current site.217 * @param int $network_id Optional site ID for which to query the options. Defaults to the current site. 218 218 */ 219 function wp_load_core_site_options( $ site_id = null ) {219 function wp_load_core_site_options( $network_id = null ) { 220 220 global $wpdb; 221 221 222 222 if ( ! is_multisite() || wp_using_ext_object_cache() || wp_installing() ) 223 223 return; 224 224 225 if ( empty($site_id) ) 226 $site_id = $wpdb->siteid; 225 $network_id = (int) $network_id; 227 226 228 $core_options = array('site_name', 'siteurl', 'active_sitewide_plugins', '_site_transient_timeout_theme_roots', '_site_transient_theme_roots', 'site_admins', 'can_compress_scripts', 'global_terms_enabled', 'ms_files_rewriting' ); 227 // Fallback to the current network if a network ID is not specified. 228 if ( ! $network_id ) { 229 $network_id = get_current_network_id(); 230 } 229 231 230 $core_options_in = "'" . implode("', '", $core_options) . "'"; 231 $options = $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value FROM $wpdb->sitemeta WHERE meta_key IN ($core_options_in) AND site_id = %d", $site_id) ); 232 233 foreach ( $options as $option ) { 234 $key = $option->meta_key; 235 $cache_key = "{$site_id}:$key"; 236 $option->meta_value = maybe_unserialize( $option->meta_value ); 237 238 wp_cache_set( $cache_key, $option->meta_value, 'site-options' ); 239 } 232 update_meta_cache( 'site', $network_id ); 240 233 } 241 234 242 235 /** … … 1140 1133 return $pre; 1141 1134 } 1142 1135 1143 // prevent non-existent options from triggering multiple queries1144 $notoptions_key = "$network_id:notoptions";1145 $notoptions = wp_cache_get( $notoptions_key, 'site-options' );1146 1147 if ( isset( $notoptions[ $option ] ) ) {1148 1149 /**1150 * Filters a specific default network option.1151 *1152 * The dynamic portion of the hook name, `$option`, refers to the option name.1153 *1154 * @since 3.4.01155 * @since 4.4.0 The `$option` parameter was added.1156 * @since 4.7.0 The `$network_id` parameter was added.1157 *1158 * @param mixed $default The value to return if the site option does not exist1159 * in the database.1160 * @param string $option Option name.1161 * @param int $network_id ID of the network.1162 */1163 return apply_filters( "default_site_option_{$option}", $default, $option, $network_id );1164 }1165 1136 1166 1137 if ( ! is_multisite() ) { 1167 1138 /** This filter is documented in wp-includes/option.php */ 1168 1139 $default = apply_filters( 'default_site_option_' . $option, $default, $option, $network_id ); 1169 1140 $value = get_option( $option, $default ); 1170 1141 } else { 1171 $cache_key = "$network_id:$option"; 1172 $value = wp_cache_get( $cache_key, 'site-options' ); 1173 1174 if ( ! isset( $value ) || false === $value ) { 1175 $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $network_id ) ); 1176 1177 // Has to be get_row instead of get_var because of funkiness with 0, false, null values 1178 if ( is_object( $row ) ) { 1179 $value = $row->meta_value; 1180 $value = maybe_unserialize( $value ); 1181 wp_cache_set( $cache_key, $value, 'site-options' ); 1182 } else { 1183 if ( ! is_array( $notoptions ) ) { 1184 $notoptions = array(); 1185 } 1186 $notoptions[ $option ] = true; 1187 wp_cache_set( $notoptions_key, $notoptions, 'site-options' ); 1188 1189 /** This filter is documented in wp-includes/option.php */ 1190 $value = apply_filters( 'default_site_option_' . $option, $default, $option, $network_id ); 1191 } 1192 } 1142 $meta = get_metadata( 'site', $network_id, $option ); 1143 if ( is_array( $meta ) && ! empty( $meta ) ) { 1144 $value = array_shift( $meta ); 1145 $value = maybe_unserialize( $value ); 1146 } else { 1147 $value = apply_filters( 'default_site_option_' . $option, $default, $option ); 1148 } 1193 1149 } 1194 1150 1195 1151 /** … … 1257 1213 */ 1258 1214 $value = apply_filters( "pre_add_site_option_{$option}", $value, $option, $network_id ); 1259 1215 1260 $notoptions_key = "$network_id:notoptions";1261 1262 1216 if ( ! is_multisite() ) { 1263 1217 $result = add_option( $option, $value, '', 'no' ); 1264 1218 } else { 1265 $cache_key = "$network_id:$option"; 1266 1267 // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query 1268 $notoptions = wp_cache_get( $notoptions_key, 'site-options' ); 1269 if ( ! is_array( $notoptions ) || ! isset( $notoptions[ $option ] ) ) { 1270 if ( false !== get_network_option( $network_id, $option, false ) ) { 1271 return false; 1272 } 1273 } 1274 1275 $value = sanitize_option( $option, $value ); 1276 1277 $serialized_value = maybe_serialize( $value ); 1278 $result = $wpdb->insert( $wpdb->sitemeta, array( 'site_id' => $network_id, 'meta_key' => $option, 'meta_value' => $serialized_value ) ); 1279 1280 if ( ! $result ) { 1281 return false; 1282 } 1283 1284 wp_cache_set( $cache_key, $value, 'site-options' ); 1285 1286 // This option exists now 1287 $notoptions = wp_cache_get( $notoptions_key, 'site-options' ); // yes, again... we need it to be fresh 1288 if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) { 1289 unset( $notoptions[ $option ] ); 1290 wp_cache_set( $notoptions_key, $notoptions, 'site-options' ); 1291 } 1219 $value = sanitize_option( $option, $value ); 1220 $value = maybe_serialize( $value ); 1221 $result = add_metadata( 'site', $network_id, $option, $value, true ); 1292 1222 } 1293 1223 1294 1224 if ( $result ) { … … 1370 1300 if ( ! is_multisite() ) { 1371 1301 $result = delete_option( $option ); 1372 1302 } else { 1373 $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $network_id ) ); 1374 if ( is_null( $row ) || ! $row->meta_id ) { 1375 return false; 1376 } 1377 $cache_key = "$network_id:$option"; 1378 wp_cache_delete( $cache_key, 'site-options' ); 1379 1380 $result = $wpdb->delete( $wpdb->sitemeta, array( 'meta_key' => $option, 'site_id' => $network_id ) ); 1303 $result = delete_metadata( 'site', $network_id, $option, '' ); 1381 1304 } 1382 1305 1383 1306 if ( $result ) { … … 1470 1393 return add_network_option( $network_id, $option, $value ); 1471 1394 } 1472 1395 1473 $notoptions_key = "$network_id:notoptions";1474 $notoptions = wp_cache_get( $notoptions_key, 'site-options' );1475 if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {1476 unset( $notoptions[ $option ] );1477 wp_cache_set( $notoptions_key, $notoptions, 'site-options' );1478 }1479 1480 1396 if ( ! is_multisite() ) { 1481 1397 $result = update_option( $option, $value, 'no' ); 1482 1398 } else { 1483 $value = sanitize_option( $option, $value ); 1484 1485 $serialized_value = maybe_serialize( $value ); 1486 $result = $wpdb->update( $wpdb->sitemeta, array( 'meta_value' => $serialized_value ), array( 'site_id' => $network_id, 'meta_key' => $option ) ); 1487 1488 if ( $result ) { 1489 $cache_key = "$network_id:$option"; 1490 wp_cache_set( $cache_key, $value, 'site-options' ); 1491 } 1399 $value = sanitize_option( $option, $value ); 1400 $value = maybe_serialize( $value ); 1401 $result = update_metadata( 'site', $network_id, $option, $value ); 1492 1402 } 1493 1403 1494 1404 if ( $result ) { -
src/wp-settings.php
95 95 // Load early WordPress files. 96 96 require( ABSPATH . WPINC . '/compat.php' ); 97 97 require( ABSPATH . WPINC . '/class-wp-list-util.php' ); 98 require( ABSPATH . WPINC . '/formatting.php' ); 99 require( ABSPATH . WPINC . '/meta.php' ); 98 100 require( ABSPATH . WPINC . '/functions.php' ); 99 101 require( ABSPATH . WPINC . '/class-wp-matchesmapregex.php' ); 100 102 require( ABSPATH . WPINC . '/class-wp.php' ); … … 142 144 // Load most of WordPress. 143 145 require( ABSPATH . WPINC . '/class-wp-walker.php' ); 144 146 require( ABSPATH . WPINC . '/class-wp-ajax-response.php' ); 145 require( ABSPATH . WPINC . '/formatting.php' ); 147 146 148 require( ABSPATH . WPINC . '/capabilities.php' ); 147 149 require( ABSPATH . WPINC . '/class-wp-roles.php' ); 148 150 require( ABSPATH . WPINC . '/class-wp-role.php' ); … … 157 159 require( ABSPATH . WPINC . '/class-wp-user-query.php' ); 158 160 require( ABSPATH . WPINC . '/class-wp-session-tokens.php' ); 159 161 require( ABSPATH . WPINC . '/class-wp-user-meta-session-tokens.php' ); 160 require( ABSPATH . WPINC . '/meta.php' );161 162 require( ABSPATH . WPINC . '/class-wp-meta-query.php' ); 162 163 require( ABSPATH . WPINC . '/class-wp-metadata-lazyloader.php' ); 163 164 require( ABSPATH . WPINC . '/general-template.php' ); -
tests/phpunit/tests/option/multisite.php
144 144 //$this->assertFalse( get_option( $key2 ) ); // check get_option() 145 145 } 146 146 147 /**148 * @group multisite149 */150 function test_site_notoptions() {151 global $wpdb;152 $notoptions_key = "{$wpdb->siteid}:notoptions";153 154 $_notoptions = wp_cache_get( 'notoptions', 'site-options' );155 $this->assertEmpty( $_notoptions );156 $_notoptions1 = wp_cache_get( $notoptions_key, 'site-options' );157 $this->assertEmpty( $_notoptions1 );158 159 get_site_option( 'burrito' );160 161 $notoptions = wp_cache_get( 'notoptions', 'site-options' );162 $this->assertEmpty( $notoptions );163 $notoptions1 = wp_cache_get( $notoptions_key, 'site-options' );164 $this->assertNotEmpty( $notoptions1 );165 }166 167 147 function test_users_can_register_signup_filter() { 168 148 169 149 $registration = get_site_option('registration'); -
tests/phpunit/tests/option/networkOption.php
78 78 $this->assertFalse( isset( $options[ $key ] ) ); 79 79 } 80 80 81 /** 82 * @ticket 37181 83 * @group ms-required 84 */ 85 public function test_meta_api_use_in_network_option(){ 86 $network_id = self::factory()->network->create(); 87 $option = 'test_option_name'; 88 add_metadata( 'site', $network_id, $option, 'monday', true ); 89 add_metadata( 'site', $network_id, $option, 'tuesday', true ); 90 add_metadata( 'site', $network_id, $option, 'wednesday', true ); 91 $this->assertEquals( 'monday', get_network_option( $network_id, $option, true ) ); 92 } 93 81 94 /** 82 95 * @dataProvider data_network_id_parameter 83 96 *