mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-05-21 11:44:36 +00:00
Networks and Sites: Use metadata api in `*_network_options functions.
Replace logic found in `get_network_option`, `update_network_option` and `delete_network_option` to use the metadata api. Using the metadata api has a number of benefits, such as consistency, default values and useful filters. This change also improves performance by priming the caches of all network options in a single database request. Props spacedmonkey, swissspidy, sc0ttkclark, johnjamesjacoby, flixos90, jeremyfelt, pento, peterwilsoncc, mukesh27, desrosj. Fixes #37181 git-svn-id: https://develop.svn.wordpress.org/trunk@54080 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -324,15 +324,12 @@ function wp_load_alloptions( $force_cache = false ) {
|
||||
* Loads and caches certain often requested site options if is_multisite() and a persistent cache is not being used.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @global wpdb $wpdb WordPress database abstraction object.
|
||||
* @since 6.1.0 Uses update_meta_cache
|
||||
*
|
||||
* @param int $network_id Optional site ID for which to query the options. Defaults to the current site.
|
||||
*/
|
||||
function wp_load_core_site_options( $network_id = null ) {
|
||||
global $wpdb;
|
||||
|
||||
if ( ! is_multisite() || wp_using_ext_object_cache() || wp_installing() ) {
|
||||
if ( ! is_multisite() || wp_installing() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -340,20 +337,7 @@ function wp_load_core_site_options( $network_id = null ) {
|
||||
$network_id = get_current_network_id();
|
||||
}
|
||||
|
||||
$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' );
|
||||
|
||||
$core_options_in = "'" . implode( "', '", $core_options ) . "'";
|
||||
$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 ) );
|
||||
|
||||
$data = array();
|
||||
foreach ( $options as $option ) {
|
||||
$key = $option->meta_key;
|
||||
$cache_key = "{$network_id}:$key";
|
||||
$option->meta_value = maybe_unserialize( $option->meta_value );
|
||||
|
||||
$data[ $cache_key ] = $option->meta_value;
|
||||
}
|
||||
wp_cache_set_multiple( $data, 'site-options' );
|
||||
update_meta_cache( 'site', $network_id );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1362,10 +1346,10 @@ function update_site_option( $option, $value ) {
|
||||
* Retrieves a network's option value based on the option name.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @since 6.1.0 Now uses get_metadata().
|
||||
*
|
||||
* @see get_option()
|
||||
*
|
||||
* @global wpdb $wpdb WordPress database abstraction object.
|
||||
* @see get_metadata()
|
||||
*
|
||||
* @param int $network_id ID of the network. Can be null to default to the current network ID.
|
||||
* @param string $option Name of the option to retrieve. Expected to not be SQL-escaped.
|
||||
@@ -1373,8 +1357,6 @@ function update_site_option( $option, $value ) {
|
||||
* @return mixed Value set for the option.
|
||||
*/
|
||||
function get_network_option( $network_id, $option, $default = false ) {
|
||||
global $wpdb;
|
||||
|
||||
if ( $network_id && ! is_numeric( $network_id ) ) {
|
||||
return false;
|
||||
}
|
||||
@@ -1415,12 +1397,7 @@ function get_network_option( $network_id, $option, $default = false ) {
|
||||
return $pre;
|
||||
}
|
||||
|
||||
// Prevent non-existent options from triggering multiple queries.
|
||||
$notoptions_key = "$network_id:notoptions";
|
||||
$notoptions = wp_cache_get( $notoptions_key, 'site-options' );
|
||||
|
||||
if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
|
||||
|
||||
if ( ! is_multisite() ) {
|
||||
/**
|
||||
* Filters the value of a specific default network option.
|
||||
*
|
||||
@@ -1435,44 +1412,21 @@ function get_network_option( $network_id, $option, $default = false ) {
|
||||
* @param string $option Option name.
|
||||
* @param int $network_id ID of the network.
|
||||
*/
|
||||
return apply_filters( "default_site_option_{$option}", $default, $option, $network_id );
|
||||
}
|
||||
|
||||
if ( ! is_multisite() ) {
|
||||
/** This filter is documented in wp-includes/option.php */
|
||||
$default = apply_filters( 'default_site_option_' . $option, $default, $option, $network_id );
|
||||
$default = apply_filters( "default_site_option_{$option}", $default, $option, $network_id );
|
||||
$value = get_option( $option, $default );
|
||||
} else {
|
||||
$cache_key = "$network_id:$option";
|
||||
$value = wp_cache_get( $cache_key, 'site-options' );
|
||||
$meta = get_metadata_raw( 'site', $network_id, $option );
|
||||
if ( is_array( $meta ) && ! empty( $meta ) ) {
|
||||
$value = array_shift( $meta );
|
||||
} else {
|
||||
/** This filter is documented in wp-includes/option.php */
|
||||
$value = apply_filters( "default_site_option_{$option}", $default, $option, $network_id );
|
||||
|
||||
if ( ! isset( $value ) || false === $value ) {
|
||||
$row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $network_id ) );
|
||||
|
||||
// Has to be get_row() instead of get_var() because of funkiness with 0, false, null values.
|
||||
if ( is_object( $row ) ) {
|
||||
$value = $row->meta_value;
|
||||
$value = maybe_unserialize( $value );
|
||||
wp_cache_set( $cache_key, $value, 'site-options' );
|
||||
} else {
|
||||
if ( ! is_array( $notoptions ) ) {
|
||||
$notoptions = array();
|
||||
}
|
||||
|
||||
$notoptions[ $option ] = true;
|
||||
wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
|
||||
|
||||
/** This filter is documented in wp-includes/option.php */
|
||||
$value = apply_filters( 'default_site_option_' . $option, $default, $option, $network_id );
|
||||
}
|
||||
/** This action is documented in wp-includes/meta.php */
|
||||
$value = apply_filters( 'default_site_metadata', $value, $network_id, $option, true, 'site' );
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! is_array( $notoptions ) ) {
|
||||
$notoptions = array();
|
||||
wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the value of an existing network option.
|
||||
*
|
||||
@@ -1496,10 +1450,10 @@ function get_network_option( $network_id, $option, $default = false ) {
|
||||
* Existing options will not be updated.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @since 6.1.0 Now uses add_metadata().
|
||||
*
|
||||
* @see add_option()
|
||||
*
|
||||
* @global wpdb $wpdb WordPress database abstraction object.
|
||||
* @see add_metadata()
|
||||
*
|
||||
* @param int $network_id ID of the network. Can be null to default to the current network ID.
|
||||
* @param string $option Name of the option to add. Expected to not be SQL-escaped.
|
||||
@@ -1507,8 +1461,6 @@ function get_network_option( $network_id, $option, $default = false ) {
|
||||
* @return bool True if the option was added, false otherwise.
|
||||
*/
|
||||
function add_network_option( $network_id, $option, $value ) {
|
||||
global $wpdb;
|
||||
|
||||
if ( $network_id && ! is_numeric( $network_id ) ) {
|
||||
return false;
|
||||
}
|
||||
@@ -1538,48 +1490,11 @@ function add_network_option( $network_id, $option, $value ) {
|
||||
*/
|
||||
$value = apply_filters( "pre_add_site_option_{$option}", $value, $option, $network_id );
|
||||
|
||||
$notoptions_key = "$network_id:notoptions";
|
||||
|
||||
if ( ! is_multisite() ) {
|
||||
$result = add_option( $option, $value, '', 'no' );
|
||||
} else {
|
||||
$cache_key = "$network_id:$option";
|
||||
|
||||
// Make sure the option doesn't already exist.
|
||||
// We can check the 'notoptions' cache before we ask for a DB query.
|
||||
$notoptions = wp_cache_get( $notoptions_key, 'site-options' );
|
||||
|
||||
if ( ! is_array( $notoptions ) || ! isset( $notoptions[ $option ] ) ) {
|
||||
if ( false !== get_network_option( $network_id, $option, false ) ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$value = sanitize_option( $option, $value );
|
||||
|
||||
$serialized_value = maybe_serialize( $value );
|
||||
$result = $wpdb->insert(
|
||||
$wpdb->sitemeta,
|
||||
array(
|
||||
'site_id' => $network_id,
|
||||
'meta_key' => $option,
|
||||
'meta_value' => $serialized_value,
|
||||
)
|
||||
);
|
||||
|
||||
if ( ! $result ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
wp_cache_set( $cache_key, $value, 'site-options' );
|
||||
|
||||
// This option exists now.
|
||||
$notoptions = wp_cache_get( $notoptions_key, 'site-options' ); // Yes, again... we need it to be fresh.
|
||||
|
||||
if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
|
||||
unset( $notoptions[ $option ] );
|
||||
wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
|
||||
}
|
||||
$value = sanitize_option( $option, $value );
|
||||
$result = add_metadata( 'site', $network_id, wp_slash( $option ), wp_slash( $value ), true );
|
||||
}
|
||||
|
||||
if ( $result ) {
|
||||
@@ -1621,8 +1536,10 @@ function add_network_option( $network_id, $option, $value ) {
|
||||
* Removes a network option by name.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @since 6.1.0 Now uses delete_metadata().
|
||||
*
|
||||
* @see delete_option()
|
||||
* @see delete_metadata()
|
||||
*
|
||||
* @global wpdb $wpdb WordPress database abstraction object.
|
||||
*
|
||||
@@ -1631,8 +1548,6 @@ function add_network_option( $network_id, $option, $value ) {
|
||||
* @return bool True if the option was deleted, false otherwise.
|
||||
*/
|
||||
function delete_network_option( $network_id, $option ) {
|
||||
global $wpdb;
|
||||
|
||||
if ( $network_id && ! is_numeric( $network_id ) ) {
|
||||
return false;
|
||||
}
|
||||
@@ -1661,20 +1576,7 @@ function delete_network_option( $network_id, $option ) {
|
||||
if ( ! is_multisite() ) {
|
||||
$result = delete_option( $option );
|
||||
} else {
|
||||
$row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $network_id ) );
|
||||
if ( is_null( $row ) || ! $row->meta_id ) {
|
||||
return false;
|
||||
}
|
||||
$cache_key = "$network_id:$option";
|
||||
wp_cache_delete( $cache_key, 'site-options' );
|
||||
|
||||
$result = $wpdb->delete(
|
||||
$wpdb->sitemeta,
|
||||
array(
|
||||
'meta_key' => $option,
|
||||
'site_id' => $network_id,
|
||||
)
|
||||
);
|
||||
$result = delete_metadata( 'site', $network_id, wp_slash( $option ), '' );
|
||||
}
|
||||
|
||||
if ( $result ) {
|
||||
@@ -1714,10 +1616,10 @@ function delete_network_option( $network_id, $option ) {
|
||||
* Updates the value of a network option that was already added.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @since 6.1.0 Now uses update_metadata().
|
||||
*
|
||||
* @see update_option()
|
||||
*
|
||||
* @global wpdb $wpdb WordPress database abstraction object.
|
||||
* @see update_metadata()
|
||||
*
|
||||
* @param int $network_id ID of the network. Can be null to default to the current network ID.
|
||||
* @param string $option Name of the option. Expected to not be SQL-escaped.
|
||||
@@ -1725,8 +1627,6 @@ function delete_network_option( $network_id, $option ) {
|
||||
* @return bool True if the value was updated, false otherwise.
|
||||
*/
|
||||
function update_network_option( $network_id, $option, $value ) {
|
||||
global $wpdb;
|
||||
|
||||
if ( $network_id && ! is_numeric( $network_id ) ) {
|
||||
return false;
|
||||
}
|
||||
@@ -1776,33 +1676,11 @@ function update_network_option( $network_id, $option, $value ) {
|
||||
return add_network_option( $network_id, $option, $value );
|
||||
}
|
||||
|
||||
$notoptions_key = "$network_id:notoptions";
|
||||
$notoptions = wp_cache_get( $notoptions_key, 'site-options' );
|
||||
|
||||
if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
|
||||
unset( $notoptions[ $option ] );
|
||||
wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
|
||||
}
|
||||
|
||||
if ( ! is_multisite() ) {
|
||||
$result = update_option( $option, $value, 'no' );
|
||||
} else {
|
||||
$value = sanitize_option( $option, $value );
|
||||
|
||||
$serialized_value = maybe_serialize( $value );
|
||||
$result = $wpdb->update(
|
||||
$wpdb->sitemeta,
|
||||
array( 'meta_value' => $serialized_value ),
|
||||
array(
|
||||
'site_id' => $network_id,
|
||||
'meta_key' => $option,
|
||||
)
|
||||
);
|
||||
|
||||
if ( $result ) {
|
||||
$cache_key = "$network_id:$option";
|
||||
wp_cache_set( $cache_key, $value, 'site-options' );
|
||||
}
|
||||
$value = sanitize_option( $option, $value );
|
||||
$result = update_metadata( 'site', $network_id, wp_slash( $option ), wp_slash( $value ) );
|
||||
}
|
||||
|
||||
if ( $result ) {
|
||||
|
||||
Reference in New Issue
Block a user