+/**
+ * Removes site option by name.
+ *
+ * @see delete_option()
+ * @package WordPress
+ * @subpackage Option
+ * @since 2.8.0
+ *
+ * @uses do_action() Calls 'pre_delete_site_option_$option' hook before option is deleted.
+ * @uses do_action() Calls 'delete_site_option' and 'delete_site_option_$option'
+ * hooks on success.
+ *
+ * @param string $option Name of option to remove. Expected to not be SQL-escaped.
+ * @return bool True, if succeed. False, if failure.
+ */
+function delete_site_option( $option ) {
+ global $wpdb;
+
+ // ms_protect_special_option( $option ); @todo
+
+ do_action( 'pre_delete_site_option_' . $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, $wpdb->siteid ) );
+ if ( is_null( $row ) || !$row->meta_id )
+ return false;
+ $cache_key = "{$wpdb->siteid}:$option";
+ wp_cache_delete( $cache_key, 'site-options' );
+
+ $result = $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) );
+ }
+
+ if ( $result ) {
+ do_action( "delete_site_option_{$option}", $option );
+ do_action( "delete_site_option", $option );
+ return true;
+ }
+ return false;
+}
+
+/**
+ * Update the value of a site option that was already added.
+ *
+ * @see update_option()
+ * @since 2.8.0
+ * @package WordPress
+ * @subpackage Option
+ *
+ * @uses apply_filters() Calls 'pre_update_site_option_$option' hook to allow overwriting the
+ * option value to be stored.
+ * @uses do_action() Calls 'update_site_option_$option' and 'update_site_option' hooks on success.
+ *
+ * @param string $option Name of option. Expected to not be SQL-escaped.
+ * @param mixed $value Option value. Expected to not be SQL-escaped.
+ * @return bool False if value was not updated and true if value was updated.
+ */
+function update_site_option( $option, $value ) {
+ global $wpdb;
+
+ $oldvalue = get_site_option( $option );
+ $value = apply_filters( 'pre_update_site_option_' . $option, $value, $oldvalue );
+
+ if ( $value === $oldvalue )
+ return false;
+
+ if ( !is_multisite() ) {
+ $result = update_option( $option, $value );
+ } else {
+ $cache_key = "{$wpdb->siteid}:$option";
+
+ if ( $value && !$wpdb->get_row( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) ) )
+ return add_site_option( $option, $value );
+ $value = sanitize_option( $option, $value );
+ wp_cache_set( $cache_key, $value, 'site-options' );
+
+ $_value = $value;
+ $value = maybe_serialize( $value );
+ $result = $wpdb->update( $wpdb->sitemeta, array( 'meta_value' => $value ), array( 'site_id' => $wpdb->siteid, 'meta_key' => $option ) );
+ $value = $_value;
+ }
+
+ if ( $result ) {
+ do_action( "update_site_option_{$option}", $option, $value );
+ do_action( "update_site_option", $option, $value );
+ return true;
+ }
+ return false;
+}
+
+/**
+ * Delete a site transient
+ *
+ * @since 2.9.0
+ * @package WordPress
+ * @subpackage Transient
+ *
+ * @uses do_action() Calls 'delete_site_transient_$transient' hook before transient is deleted.
+ * @uses do_action() Calls 'deleted_site_transient' hook on success.
+ *
+ * @param string $transient Transient name. Expected to not be SQL-escaped.
+ * @return bool True if successful, false otherwise
+ */
+function delete_site_transient( $transient ) {
+ global $_wp_using_ext_object_cache;
+
+ do_action( 'delete_site_transient_' . $transient, $transient );
+ if ( $_wp_using_ext_object_cache ) {
+ $result = wp_cache_delete( $transient, 'site-transient' );
+ } else {
+ $option_timeout = '_site_transient_timeout_' . $transient;
+ $option = '_site_transient_' . $transient;
+ $result = delete_site_option( $option );
+ if ( $result )
+ delete_site_option( $option_timeout );
+ }
+ if ( $result )
+ do_action( 'deleted_site_transient', $transient );
+ return $result;
+}
+
+/**
+ * Get the value of a site transient
+ *
+ * If the transient does not exist or does not have a value, then the return value
+ * will be false.
+ *
+ * @see get_transient()
+ * @since 2.9.0
+ * @package WordPress
+ * @subpackage Transient
+ *
+ * @uses apply_filters() Calls 'pre_site_transient_$transient' hook before checking the transient.
+ * Any value other than false will "short-circuit" the retrieval of the transient
+ * and return the returned value.
+ * @uses apply_filters() Calls 'site_transient_$option' hook, after checking the transient, with
+ * the transient value.
+ *
+ * @param string $transient Transient name. Expected to not be SQL-escaped.
+ * @return mixed Value of transient
+ */
+function get_site_transient( $transient ) {
+ global $_wp_using_ext_object_cache;
+
+ $pre = apply_filters( 'pre_site_transient_' . $transient, false );
+ if ( false !== $pre )
+ return $pre;
+
+ if ( $_wp_using_ext_object_cache ) {
+ $value = wp_cache_get( $transient, 'site-transient' );
+ } else {
+ // Core transients that do not have a timeout. Listed here so querying timeouts can be avoided.
+ $no_timeout = array('update_core', 'update_plugins', 'update_themes');
+ $transient_option = '_site_transient_' . $transient;
+ if ( ! in_array( $transient, $no_timeout ) ) {
+ $transient_timeout = '_site_transient_timeout_' . $transient;
+ $timeout = get_site_option( $transient_timeout );
+ if ( false !== $timeout && $timeout < time() ) {
+ delete_site_option( $transient_option );
+ delete_site_option( $transient_timeout );
+ return false;
+ }
+ }
+
+ $value = get_site_option( $transient_option );
+ }
+
+ return apply_filters( 'site_transient_' . $transient, $value );
+}
+
+/**
+ * Set/update the value of a site transient
+ *
+ * You do not need to serialize values, if the value needs to be serialize, then
+ * it will be serialized before it is set.
+ *
+ * @see set_transient()
+ * @since 2.9.0
+ * @package WordPress
+ * @subpackage Transient
+ *
+ * @uses apply_filters() Calls 'pre_set_site_transient_$transient' hook to allow overwriting the
+ * transient value to be stored.
+ * @uses do_action() Calls 'set_site_transient_$transient' and 'setted_site_transient' hooks on success.
+ *
+ * @param string $transient Transient name. Expected to not be SQL-escaped.
+ * @param mixed $value Transient value. Expected to not be SQL-escaped.
+ * @param int $expiration Time until expiration in seconds, default 0
+ * @return bool False if value was not set and true if value was set.
+ */
+function set_site_transient( $transient, $value, $expiration = 0 ) {
+ global $_wp_using_ext_object_cache;
+
+ $value = apply_filters( 'pre_set_site_transient_' . $transient, $value );
+
+ if ( $_wp_using_ext_object_cache ) {
+ $result = wp_cache_set( $transient, $value, 'site-transient', $expiration );
+ } else {
+ $transient_timeout = '_site_transient_timeout_' . $transient;
+ $transient = '_site_transient_' . $transient;
+ if ( false === get_site_option( $transient ) ) {
+ if ( $expiration )
+ add_site_option( $transient_timeout, time() + $expiration );
+ $result = add_site_option( $transient, $value );
+ } else {
+ if ( $expiration )
+ update_site_option( $transient_timeout, time() + $expiration );
+ $result = update_site_option( $transient, $value );
+ }
+ }
+ if ( $result ) {
+ do_action( 'set_site_transient_' . $transient );
+ do_action( 'setted_site_transient', $transient );
+ }
+ return $result;
+}
+
+/**
+ * is main site
+ *
+ *
+ * @since 3.0.0
+ * @package WordPress
+ *
+ * @param int $blog_id optional blog id to test (default current blog)
+ * @return bool True if not multisite or $blog_id is main site
+ */
+function is_main_site( $blog_id = '' ) {
+ global $current_site, $current_blog;
+
+ if ( !is_multisite() )
+ return true;
+
+ if ( !$blog_id )
+ $blog_id = $current_blog->blog_id;
+
+ return $blog_id == $current_site->blog_id;
+}
+
+/**
+ * Whether global terms are enabled.
+ *
+ *
+ * @since 3.0.0
+ * @package WordPress
+ *
+ * @return bool True if multisite and global terms enabled
+ */
+function global_terms_enabled() {
+ if ( ! is_multisite() )
+ return false;
+
+ static $global_terms = null;
+ if ( is_null( $global_terms ) ) {
+ $filter = apply_filters( 'global_terms_enabled', null );
+ if ( ! is_null( $filter ) )
+ $global_terms = (bool) $filter;
+ else
+ $global_terms = (bool) get_site_option( 'global_terms_enabled', false );
+ }
+ return $global_terms;