Ticket #25344: 25344.10.network-options.diff
| File 25344.10.network-options.diff, 10.7 KB (added by , 9 years ago) |
|---|
-
src/wp-includes/option.php
205 205 * 206 206 * @since 3.0.0 207 207 * 208 * @ global wpdb $wpdb WordPress database abstraction object.208 * @see update_networkmeta_cache() 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 ) { 213 global $wpdb; 212 function wp_load_core_site_options( $network_id = null ) { 214 213 215 if ( ! is_multisite() || wp_using_ext_object_cache() || wp_installing() ) 214 if ( ! is_multisite() || wp_using_ext_object_cache() || wp_installing() ) { 216 215 return; 216 } 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 ); 218 $network_id = (int) $network_id; 230 219 231 wp_cache_set( $cache_key, $option->meta_value, 'site-options' ); 220 // Fallback to the current network if a network ID is not specified. 221 if ( empty( $network_id ) ) { 222 $network_id = get_current_network_id(); 232 223 } 224 225 update_networkmeta_cache( $network_id ); 233 226 } 234 227 235 228 /** … … 1080 1073 * Retrieve a network's option value based on the option name. 1081 1074 * 1082 1075 * @since 4.4.0 1076 * @since 4.8.0 Modified into wrapper for get_network_meta() 1083 1077 * 1084 1078 * @see get_option() 1079 * @see get_network_meta() 1085 1080 * 1086 1081 * @global wpdb $wpdb 1087 1082 * … … 1091 1086 * @return mixed Value set for the option. 1092 1087 */ 1093 1088 function get_network_option( $network_id, $option, $default = false ) { 1094 global $wpdb;1095 1089 1096 1090 if ( $network_id && ! is_numeric( $network_id ) ) { 1097 1091 return false; … … 1127 1121 return $pre; 1128 1122 } 1129 1123 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 } 1124 /** 1125 * Filters a specific default network option. 1126 * 1127 * The dynamic portion of the hook name, `$option`, refers to the option name. 1128 * 1129 * @since 3.4.0 1130 * @since 4.4.0 The `$option` parameter was added. 1131 * @since 4.7.0 The `$network_id` parameter was added. 1132 * 1133 * @param mixed $default The value to return if the site option does not exist 1134 * in the database. 1135 * @param string $option Option name. 1136 * @param int $network_id ID of the network. 1137 */ 1138 $default = apply_filters( 'default_site_option_' . $option, $default, $option, $network_id ); 1152 1139 1153 1140 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 1141 $value = get_option( $option, $default ); 1157 1142 } 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 } 1143 $meta = get_network_meta( $network_id, $option ); 1144 if ( is_array( $meta ) && ! empty( $meta ) ) { 1145 $value = array_shift( $meta ); 1146 } else { 1147 $value = $default; 1179 1148 } 1180 1149 } 1181 1150 … … 1202 1171 * Existing options will not be updated. 1203 1172 * 1204 1173 * @since 4.4.0 1174 * @since 4.8.0 Modified into wrapper for add_network_meta() 1205 1175 * 1206 1176 * @see add_option() 1177 * @see add_network_meta() 1207 1178 * 1208 1179 * @global wpdb $wpdb 1209 1180 * … … 1213 1184 * @return bool False if option was not added and true if option was added. 1214 1185 */ 1215 1186 function add_network_option( $network_id, $option, $value ) { 1216 global $wpdb;1217 1187 1218 1188 if ( $network_id && ! is_numeric( $network_id ) ) { 1219 1189 return false; … … 1244 1214 */ 1245 1215 $value = apply_filters( "pre_add_site_option_{$option}", $value, $option, $network_id ); 1246 1216 1247 $notoptions_key = "$network_id:notoptions";1248 1249 1217 if ( ! is_multisite() ) { 1250 1218 $result = add_option( $option, $value, '', 'no' ); 1251 1219 } 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 } 1220 $value = sanitize_option( $option, $value ); 1221 $result = add_network_meta( $network_id, $option, $value, true ); 1279 1222 } 1280 1223 1281 1224 if ( $result ) { … … 1317 1260 * Removes a network option by name. 1318 1261 * 1319 1262 * @since 4.4.0 1263 * @since 4.8.0 Modified into wrapper for delete_network_meta() 1320 1264 * 1321 1265 * @see delete_option() 1266 * @see delete_network_meta() 1322 1267 * 1323 1268 * @global wpdb $wpdb 1324 1269 * … … 1327 1272 * @return bool True, if succeed. False, if failure. 1328 1273 */ 1329 1274 function delete_network_option( $network_id, $option ) { 1330 global $wpdb;1331 1275 1332 1276 if ( $network_id && ! is_numeric( $network_id ) ) { 1333 1277 return false; … … 1357 1301 if ( ! is_multisite() ) { 1358 1302 $result = delete_option( $option ); 1359 1303 } 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 ) ); 1304 $result = delete_network_meta( $network_id, $option ); 1368 1305 } 1369 1306 1370 1307 if ( $result ) { … … 1404 1341 * Update the value of a network option that was already added. 1405 1342 * 1406 1343 * @since 4.4.0 1344 * @since 4.8.0 Modified into wrapper for update_network_meta() 1407 1345 * 1408 1346 * @see update_option() 1347 * @see update_network_meta() 1409 1348 * 1410 1349 * @global wpdb $wpdb 1411 1350 * … … 1415 1354 * @return bool False if value was not updated and true if value was updated. 1416 1355 */ 1417 1356 function update_network_option( $network_id, $option, $value ) { 1418 global $wpdb;1419 1357 1420 1358 if ( $network_id && ! is_numeric( $network_id ) ) { 1421 1359 return false; … … 1449 1387 */ 1450 1388 $value = apply_filters( "pre_update_site_option_{$option}", $value, $old_value, $option, $network_id ); 1451 1389 1452 if ( $value === $old_value ) {1453 return false;1454 }1455 1456 1390 if ( false === $old_value ) { 1457 1391 return add_network_option( $network_id, $option, $value ); 1458 1392 } 1459 1393 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 1394 if ( ! is_multisite() ) { 1468 1395 $result = update_option( $option, $value, 'no' ); 1469 1396 } 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 } 1397 $value = sanitize_option( $option, $value ); 1398 $result = update_network_meta( $network_id, $option, $value, $old_value ); 1479 1399 } 1480 1400 1481 1401 if ( $result ) { -
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 147 167 148 function test_users_can_register_signup_filter() { 168 149
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)