Ticket #25344: 25344.2.patch
File 25344.2.patch, 17.3 KB (added by , 4 years ago) |
---|
-
src/wp-includes/class-wp-network-query.php
119 119 * @type array $path__not_in Array of paths to exclude affiliated networks for. Default empty. 120 120 * @type string $search Search term(s) to retrieve matching networks for. Default empty. 121 121 * @type bool $update_network_cache Whether to prime the cache for found networks. Default true. 122 * @type bool $update_network_meta_cache Whether to update the network meta cache. Default true. 122 123 * } 123 124 */ 124 125 public function __construct( $query = '' ) { … … 140 141 'path__not_in' => '', 141 142 'search' => '', 142 143 'update_network_cache' => true, 144 'update_network_meta_cache' => true, 143 145 ); 144 146 145 147 if ( ! empty( $query ) ) { … … 248 250 } 249 251 250 252 if ( $this->query_vars['update_network_cache'] ) { 251 _prime_network_caches( $network_ids );253 _prime_network_caches( $network_ids, $this->query_vars['update_network_meta_cache'] ); 252 254 } 253 255 254 256 // Fetch full network objects from the primed cache. -
src/wp-includes/functions.php
4440 4440 // If the current network has an ID of 1, assume it is the main network. 4441 4441 $main_network_id = 1; 4442 4442 } else { 4443 $_networks = get_networks( array( 'fields' => 'ids', 'number' => 1 ) );4443 $_networks = get_networks( array( 'fields' => 'ids', 'number' => 1, 'update_network_meta_cache' => false ) ); 4444 4444 $main_network_id = array_shift( $_networks ); 4445 4445 } 4446 4446 -
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 } … … 1170 1170 1171 1171 foreach ( (array) $ids as $id ) { 1172 1172 wp_cache_delete( $id, 'networks' ); 1173 wp_cache_delete( $id, 'networks' ); 1173 1174 1174 1175 /** 1175 1176 * Fires immediately after a network has been removed from the object cache. -
src/wp-includes/ms-functions.php
2513 2513 */ 2514 2514 return apply_filters( 'subdirectory_reserved_names', $names ); 2515 2515 } 2516 2517 /** 2518 * Add metadata to a network. 2519 * 2520 * @since 4.8.0 2521 * 2522 * @param int $id Network ID. 2523 * @param string $meta_key Metadata name. 2524 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. 2525 * @param bool $unique Optional. Whether the same key should not be added. 2526 * Default false. 2527 * @return int|false Meta ID on success, false on failure. 2528 */ 2529 function add_network_meta( $id, $meta_key, $meta_value, $unique = false ) { 2530 return add_metadata( 'site', $id, $meta_key, $meta_value, $unique ); 2531 } 2532 2533 /** 2534 * Remove from a network, metadata matching key and/or value. 2535 * 2536 * You can match based on the key, or key and value. Removing based on key and 2537 * value, will keep from removing duplicate metadata with the same key. It also 2538 * allows removing all metadata matching key, if needed. 2539 * 2540 * @since 4.8.0 2541 * 2542 * @param int $id Network ID. 2543 * @param string $meta_key Metadata name. 2544 * @param mixed $meta_value Optional. Metadata value. Must be serializable if 2545 * non-scalar. Default empty. 2546 * @return bool True on success, false on failure. 2547 */ 2548 function delete_network_meta( $id, $meta_key, $meta_value = '' ) { 2549 return delete_metadata( 'site', $id, $meta_key, $meta_value ); 2550 } 2551 2552 /** 2553 * Retrieve from a network, metadata value by key. 2554 * 2555 * @since 4.8.0 2556 * 2557 * @param int $id Network ID. 2558 * @param string $meta_key Optional. The meta key to retrieve. By default, returns 2559 * data for all keys. Default empty. 2560 * @param bool $single Optional. Whether to return a single value. Default false. 2561 * @return mixed Will be an array if $single is false. Will be value of meta data 2562 * field if $single is true. 2563 */ 2564 function get_network_meta( $id, $meta_key = '', $single = false ) { 2565 return get_metadata( 'site', $id, $meta_key, $single ); 2566 } 2567 2568 /** 2569 * Update metadata for a network ID, and/or key, and/or value. 2570 * 2571 * Use the $prev_value parameter to differentiate between meta fields with the 2572 * same key and network ID. 2573 * 2574 * If the meta field for the network does not exist, it will be added. 2575 * 2576 * @since 4.8.0 2577 * 2578 * @param int $id Network ID. 2579 * @param string $meta_key Metadata key. 2580 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. 2581 * @param mixed $prev_value Optional. Previous value to check before removing. 2582 * Default empty. 2583 * @return int|bool Meta ID if the key didn't exist, true on successful update, 2584 * false on failure. 2585 */ 2586 function update_network_meta( $id, $meta_key, $meta_value, $prev_value = '' ) { 2587 return update_metadata( 'site', $id, $meta_key, $meta_value, $prev_value ); 2588 } 2589 2590 /** 2591 * Updates metadata cache for list of network IDs. 2592 * 2593 * Performs SQL query to retrieve the metadata for the network IDs and updates the 2594 * metadata cache for the posts. Therefore, the functions, which call this 2595 * function, do not need to perform SQL queries on their own. 2596 * 2597 * @since 4.8.0 2598 * 2599 * @param array $network_ids List of network IDs. 2600 * @return array|false Returns false if there is nothing to update or an array 2601 * of metadata. 2602 */ 2603 function update_networkmeta_cache( $network_ids ) { 2604 return update_meta_cache( 'site', $network_ids ); 2605 } 2606 No newline at end of file -
src/wp-includes/option.php
207 207 * 208 208 * @global wpdb $wpdb WordPress database abstraction object. 209 209 * 210 * @param int $ site_id Optional siteID for which to query the options. Defaults to the current site.210 * @param int $network_id Optional network ID for which to query the options. Defaults to the current site. 211 211 */ 212 function wp_load_core_site_options( $ site_id = null ) {212 function wp_load_core_site_options( $network_id = null ) { 213 213 global $wpdb; 214 214 215 215 if ( ! is_multisite() || wp_using_ext_object_cache() || wp_installing() ) 216 216 return; 217 217 218 if ( empty($site_id) ) 219 $site_id = $wpdb->siteid; 220 221 $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' ); 222 223 $core_options_in = "'" . implode("', '", $core_options) . "'"; 224 $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) ); 225 226 foreach ( $options as $option ) { 227 $key = $option->meta_key; 228 $cache_key = "{$site_id}:$key"; 229 $option->meta_value = maybe_unserialize( $option->meta_value ); 230 231 wp_cache_set( $cache_key, $option->meta_value, 'site-options' ); 218 if ( empty( $network_id ) ) { 219 $network_id = $wpdb->siteid; 232 220 } 221 update_networkmeta_cache( $network_id ); 233 222 } 234 223 235 224 /** … … 1091 1080 * @return mixed Value set for the option. 1092 1081 */ 1093 1082 function get_network_option( $network_id, $option, $default = false ) { 1094 global $wpdb;1095 1083 1096 1084 if ( $network_id && ! is_numeric( $network_id ) ) { 1097 1085 return false; … … 1127 1115 return $pre; 1128 1116 } 1129 1117 1130 // prevent non-existent options from triggering multiple queries 1131 $notoptions_key = "$network_id:notoptions"; 1132 $notoptions = wp_cache_get( $notoptions_key, 'site-options' ); 1133 1134 if ( isset( $notoptions[ $option ] ) ) { 1135 1136 /** 1137 * Filters a specific default network option. 1138 * 1139 * The dynamic portion of the hook name, `$option`, refers to the option name. 1140 * 1141 * @since 3.4.0 1142 * @since 4.4.0 The `$option` parameter was added. 1143 * @since 4.7.0 The `$network_id` parameter was added. 1144 * 1145 * @param mixed $default The value to return if the site option does not exist 1146 * in the database. 1147 * @param string $option Option name. 1148 * @param int $network_id ID of the network. 1149 */ 1150 return apply_filters( "default_site_option_{$option}", $default, $option, $network_id ); 1151 } 1118 /** 1119 * Filters a specific default network option. 1120 * 1121 * The dynamic portion of the hook name, `$option`, refers to the option name. 1122 * 1123 * @since 3.4.0 1124 * @since 4.4.0 The `$option` parameter was added. 1125 * @since 4.7.0 The `$network_id` parameter was added. 1126 * 1127 * @param mixed $default The value to return if the site option does not exist 1128 * in the database. 1129 * @param string $option Option name. 1130 * @param int $network_id ID of the network. 1131 */ 1132 $default = apply_filters( 'default_site_option_' . $option, $default, $option, $network_id ); 1152 1133 1153 1134 if ( ! is_multisite() ) { 1154 /** This filter is documented in wp-includes/option.php */1155 $default = apply_filters( 'default_site_option_' . $option, $default, $option, $network_id );1156 1135 $value = get_option( $option, $default ); 1157 1136 } else { 1158 $cache_key = "$network_id:$option"; 1159 $value = wp_cache_get( $cache_key, 'site-options' ); 1160 1161 if ( ! isset( $value ) || false === $value ) { 1162 $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $network_id ) ); 1163 1164 // Has to be get_row instead of get_var because of funkiness with 0, false, null values 1165 if ( is_object( $row ) ) { 1166 $value = $row->meta_value; 1167 $value = maybe_unserialize( $value ); 1168 wp_cache_set( $cache_key, $value, 'site-options' ); 1169 } else { 1170 if ( ! is_array( $notoptions ) ) { 1171 $notoptions = array(); 1172 } 1173 $notoptions[ $option ] = true; 1174 wp_cache_set( $notoptions_key, $notoptions, 'site-options' ); 1175 1176 /** This filter is documented in wp-includes/option.php */ 1177 $value = apply_filters( 'default_site_option_' . $option, $default, $option, $network_id ); 1178 } 1137 $value = get_network_meta( $network_id, $option, true ); 1138 if ( empty( $value ) ) { 1139 $value = $default; 1179 1140 } 1180 1141 } 1181 1142 … … 1213 1174 * @return bool False if option was not added and true if option was added. 1214 1175 */ 1215 1176 function add_network_option( $network_id, $option, $value ) { 1216 global $wpdb;1217 1177 1218 1178 if ( $network_id && ! is_numeric( $network_id ) ) { 1219 1179 return false; … … 1244 1204 */ 1245 1205 $value = apply_filters( "pre_add_site_option_{$option}", $value, $option, $network_id ); 1246 1206 1247 $notoptions_key = "$network_id:notoptions";1248 1249 1207 if ( ! is_multisite() ) { 1250 1208 $result = add_option( $option, $value, '', 'no' ); 1251 1209 } else { 1252 $cache_key = "$network_id:$option"; 1253 1254 // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query 1255 $notoptions = wp_cache_get( $notoptions_key, 'site-options' ); 1256 if ( ! is_array( $notoptions ) || ! isset( $notoptions[ $option ] ) ) { 1257 if ( false !== get_network_option( $network_id, $option, false ) ) { 1258 return false; 1259 } 1260 } 1261 1262 $value = sanitize_option( $option, $value ); 1263 1264 $serialized_value = maybe_serialize( $value ); 1265 $result = $wpdb->insert( $wpdb->sitemeta, array( 'site_id' => $network_id, 'meta_key' => $option, 'meta_value' => $serialized_value ) ); 1266 1267 if ( ! $result ) { 1268 return false; 1269 } 1270 1271 wp_cache_set( $cache_key, $value, 'site-options' ); 1272 1273 // This option exists now 1274 $notoptions = wp_cache_get( $notoptions_key, 'site-options' ); // yes, again... we need it to be fresh 1275 if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) { 1276 unset( $notoptions[ $option ] ); 1277 wp_cache_set( $notoptions_key, $notoptions, 'site-options' ); 1278 } 1210 $result = add_network_meta( $network_id, $option, $value ); 1279 1211 } 1280 1212 1281 1213 if ( $result ) { … … 1327 1259 * @return bool True, if succeed. False, if failure. 1328 1260 */ 1329 1261 function delete_network_option( $network_id, $option ) { 1330 global $wpdb;1331 1262 1332 1263 if ( $network_id && ! is_numeric( $network_id ) ) { 1333 1264 return false; … … 1357 1288 if ( ! is_multisite() ) { 1358 1289 $result = delete_option( $option ); 1359 1290 } else { 1360 $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $network_id ) ); 1361 if ( is_null( $row ) || ! $row->meta_id ) { 1362 return false; 1363 } 1364 $cache_key = "$network_id:$option"; 1365 wp_cache_delete( $cache_key, 'site-options' ); 1366 1367 $result = $wpdb->delete( $wpdb->sitemeta, array( 'meta_key' => $option, 'site_id' => $network_id ) ); 1291 $result = delete_network_meta( $network_id, $option ); 1368 1292 } 1369 1293 1370 1294 if ( $result ) { … … 1415 1339 * @return bool False if value was not updated and true if value was updated. 1416 1340 */ 1417 1341 function update_network_option( $network_id, $option, $value ) { 1418 global $wpdb;1419 1342 1420 1343 if ( $network_id && ! is_numeric( $network_id ) ) { 1421 1344 return false; … … 1457 1380 return add_network_option( $network_id, $option, $value ); 1458 1381 } 1459 1382 1460 $notoptions_key = "$network_id:notoptions";1461 $notoptions = wp_cache_get( $notoptions_key, 'site-options' );1462 if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {1463 unset( $notoptions[ $option ] );1464 wp_cache_set( $notoptions_key, $notoptions, 'site-options' );1465 }1466 1467 1383 if ( ! is_multisite() ) { 1468 1384 $result = update_option( $option, $value, 'no' ); 1469 1385 } else { 1470 $value = sanitize_option( $option, $value ); 1471 1472 $serialized_value = maybe_serialize( $value ); 1473 $result = $wpdb->update( $wpdb->sitemeta, array( 'meta_value' => $serialized_value ), array( 'site_id' => $network_id, 'meta_key' => $option ) ); 1474 1475 if ( $result ) { 1476 $cache_key = "$network_id:$option"; 1477 wp_cache_set( $cache_key, $value, 'site-options' ); 1478 } 1386 $value = sanitize_option( $option, $value ); 1387 $result = update_network_meta( $network_id, $option, $value ); 1479 1388 } 1480 1389 1481 1390 if ( $result ) { -
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