Changeset 54080 for trunk/src/wp-includes/option.php
- Timestamp:
- 09/06/2022 11:26:45 AM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/option.php
r53914 r54080 325 325 * 326 326 * @since 3.0.0 327 * 328 * @global wpdb $wpdb WordPress database abstraction object. 327 * @since 6.1.0 Uses update_meta_cache 329 328 * 330 329 * @param int $network_id Optional site ID for which to query the options. Defaults to the current site. 331 330 */ 332 331 function wp_load_core_site_options( $network_id = null ) { 333 global $wpdb; 334 335 if ( ! is_multisite() || wp_using_ext_object_cache() || wp_installing() ) { 332 if ( ! is_multisite() || wp_installing() ) { 336 333 return; 337 334 } … … 341 338 } 342 339 343 $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' ); 344 345 $core_options_in = "'" . implode( "', '", $core_options ) . "'"; 346 $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", $network_id ) ); 347 348 $data = array(); 349 foreach ( $options as $option ) { 350 $key = $option->meta_key; 351 $cache_key = "{$network_id}:$key"; 352 $option->meta_value = maybe_unserialize( $option->meta_value ); 353 354 $data[ $cache_key ] = $option->meta_value; 355 } 356 wp_cache_set_multiple( $data, 'site-options' ); 340 update_meta_cache( 'site', $network_id ); 357 341 } 358 342 … … 1363 1347 * 1364 1348 * @since 4.4.0 1349 * @since 6.1.0 Now uses get_metadata(). 1365 1350 * 1366 1351 * @see get_option() 1367 * 1368 * @global wpdb $wpdb WordPress database abstraction object. 1352 * @see get_metadata() 1369 1353 * 1370 1354 * @param int $network_id ID of the network. Can be null to default to the current network ID. … … 1374 1358 */ 1375 1359 function get_network_option( $network_id, $option, $default = false ) { 1376 global $wpdb;1377 1378 1360 if ( $network_id && ! is_numeric( $network_id ) ) { 1379 1361 return false; … … 1416 1398 } 1417 1399 1418 // Prevent non-existent options from triggering multiple queries. 1419 $notoptions_key = "$network_id:notoptions"; 1420 $notoptions = wp_cache_get( $notoptions_key, 'site-options' ); 1421 1422 if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) { 1423 1400 if ( ! is_multisite() ) { 1424 1401 /** 1425 1402 * Filters the value of a specific default network option. … … 1436 1413 * @param int $network_id ID of the network. 1437 1414 */ 1438 return apply_filters( "default_site_option_{$option}", $default, $option, $network_id ); 1439 } 1440 1441 if ( ! is_multisite() ) { 1442 /** This filter is documented in wp-includes/option.php */ 1443 $default = apply_filters( 'default_site_option_' . $option, $default, $option, $network_id ); 1415 $default = apply_filters( "default_site_option_{$option}", $default, $option, $network_id ); 1444 1416 $value = get_option( $option, $default ); 1445 1417 } else { 1446 $cache_key = "$network_id:$option"; 1447 $value = wp_cache_get( $cache_key, 'site-options' ); 1448 1449 if ( ! isset( $value ) || false === $value ) { 1450 $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $network_id ) ); 1451 1452 // Has to be get_row() instead of get_var() because of funkiness with 0, false, null values. 1453 if ( is_object( $row ) ) { 1454 $value = $row->meta_value; 1455 $value = maybe_unserialize( $value ); 1456 wp_cache_set( $cache_key, $value, 'site-options' ); 1457 } else { 1458 if ( ! is_array( $notoptions ) ) { 1459 $notoptions = array(); 1460 } 1461 1462 $notoptions[ $option ] = true; 1463 wp_cache_set( $notoptions_key, $notoptions, 'site-options' ); 1464 1465 /** This filter is documented in wp-includes/option.php */ 1466 $value = apply_filters( 'default_site_option_' . $option, $default, $option, $network_id ); 1467 } 1468 } 1469 } 1470 1471 if ( ! is_array( $notoptions ) ) { 1472 $notoptions = array(); 1473 wp_cache_set( $notoptions_key, $notoptions, 'site-options' ); 1418 $meta = get_metadata_raw( 'site', $network_id, $option ); 1419 if ( is_array( $meta ) && ! empty( $meta ) ) { 1420 $value = array_shift( $meta ); 1421 } else { 1422 /** This filter is documented in wp-includes/option.php */ 1423 $value = apply_filters( "default_site_option_{$option}", $default, $option, $network_id ); 1424 1425 /** This action is documented in wp-includes/meta.php */ 1426 $value = apply_filters( 'default_site_metadata', $value, $network_id, $option, true, 'site' ); 1427 } 1474 1428 } 1475 1429 … … 1497 1451 * 1498 1452 * @since 4.4.0 1453 * @since 6.1.0 Now uses add_metadata(). 1499 1454 * 1500 1455 * @see add_option() 1501 * 1502 * @global wpdb $wpdb WordPress database abstraction object. 1456 * @see add_metadata() 1503 1457 * 1504 1458 * @param int $network_id ID of the network. Can be null to default to the current network ID. … … 1508 1462 */ 1509 1463 function add_network_option( $network_id, $option, $value ) { 1510 global $wpdb;1511 1512 1464 if ( $network_id && ! is_numeric( $network_id ) ) { 1513 1465 return false; … … 1539 1491 $value = apply_filters( "pre_add_site_option_{$option}", $value, $option, $network_id ); 1540 1492 1541 $notoptions_key = "$network_id:notoptions";1542 1543 1493 if ( ! is_multisite() ) { 1544 1494 $result = add_option( $option, $value, '', 'no' ); 1545 1495 } else { 1546 $cache_key = "$network_id:$option"; 1547 1548 // Make sure the option doesn't already exist. 1549 // We can check the 'notoptions' cache before we ask for a DB query. 1550 $notoptions = wp_cache_get( $notoptions_key, 'site-options' ); 1551 1552 if ( ! is_array( $notoptions ) || ! isset( $notoptions[ $option ] ) ) { 1553 if ( false !== get_network_option( $network_id, $option, false ) ) { 1554 return false; 1555 } 1556 } 1557 1558 $value = sanitize_option( $option, $value ); 1559 1560 $serialized_value = maybe_serialize( $value ); 1561 $result = $wpdb->insert( 1562 $wpdb->sitemeta, 1563 array( 1564 'site_id' => $network_id, 1565 'meta_key' => $option, 1566 'meta_value' => $serialized_value, 1567 ) 1568 ); 1569 1570 if ( ! $result ) { 1571 return false; 1572 } 1573 1574 wp_cache_set( $cache_key, $value, 'site-options' ); 1575 1576 // This option exists now. 1577 $notoptions = wp_cache_get( $notoptions_key, 'site-options' ); // Yes, again... we need it to be fresh. 1578 1579 if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) { 1580 unset( $notoptions[ $option ] ); 1581 wp_cache_set( $notoptions_key, $notoptions, 'site-options' ); 1582 } 1496 $value = sanitize_option( $option, $value ); 1497 $result = add_metadata( 'site', $network_id, wp_slash( $option ), wp_slash( $value ), true ); 1583 1498 } 1584 1499 … … 1622 1537 * 1623 1538 * @since 4.4.0 1539 * @since 6.1.0 Now uses delete_metadata(). 1624 1540 * 1625 1541 * @see delete_option() 1542 * @see delete_metadata() 1626 1543 * 1627 1544 * @global wpdb $wpdb WordPress database abstraction object. … … 1632 1549 */ 1633 1550 function delete_network_option( $network_id, $option ) { 1634 global $wpdb;1635 1636 1551 if ( $network_id && ! is_numeric( $network_id ) ) { 1637 1552 return false; … … 1662 1577 $result = delete_option( $option ); 1663 1578 } else { 1664 $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $network_id ) ); 1665 if ( is_null( $row ) || ! $row->meta_id ) { 1666 return false; 1667 } 1668 $cache_key = "$network_id:$option"; 1669 wp_cache_delete( $cache_key, 'site-options' ); 1670 1671 $result = $wpdb->delete( 1672 $wpdb->sitemeta, 1673 array( 1674 'meta_key' => $option, 1675 'site_id' => $network_id, 1676 ) 1677 ); 1579 $result = delete_metadata( 'site', $network_id, wp_slash( $option ), '' ); 1678 1580 } 1679 1581 … … 1715 1617 * 1716 1618 * @since 4.4.0 1619 * @since 6.1.0 Now uses update_metadata(). 1717 1620 * 1718 1621 * @see update_option() 1719 * 1720 * @global wpdb $wpdb WordPress database abstraction object. 1622 * @see update_metadata() 1721 1623 * 1722 1624 * @param int $network_id ID of the network. Can be null to default to the current network ID. … … 1726 1628 */ 1727 1629 function update_network_option( $network_id, $option, $value ) { 1728 global $wpdb;1729 1730 1630 if ( $network_id && ! is_numeric( $network_id ) ) { 1731 1631 return false; … … 1777 1677 } 1778 1678 1779 $notoptions_key = "$network_id:notoptions";1780 $notoptions = wp_cache_get( $notoptions_key, 'site-options' );1781 1782 if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {1783 unset( $notoptions[ $option ] );1784 wp_cache_set( $notoptions_key, $notoptions, 'site-options' );1785 }1786 1787 1679 if ( ! is_multisite() ) { 1788 1680 $result = update_option( $option, $value, 'no' ); 1789 1681 } else { 1790 $value = sanitize_option( $option, $value ); 1791 1792 $serialized_value = maybe_serialize( $value ); 1793 $result = $wpdb->update( 1794 $wpdb->sitemeta, 1795 array( 'meta_value' => $serialized_value ), 1796 array( 1797 'site_id' => $network_id, 1798 'meta_key' => $option, 1799 ) 1800 ); 1801 1802 if ( $result ) { 1803 $cache_key = "$network_id:$option"; 1804 wp_cache_set( $cache_key, $value, 'site-options' ); 1805 } 1682 $value = sanitize_option( $option, $value ); 1683 $result = update_metadata( 'site', $network_id, wp_slash( $option ), wp_slash( $value ) ); 1806 1684 } 1807 1685
Note: See TracChangeset
for help on using the changeset viewer.