Make WordPress Core

Ticket #33814: 33814.patch

File 33814.patch, 14.0 KB (added by spacedmonkey, 9 years ago)
  • wp-includes/option.php

    diff --git a/wp-includes/option.php b/wp-includes/option.php
    index 5749f8b..3a0dc0d 100644
    a b function delete_all_user_settings() { 
    980980 *
    981981 * @global wpdb $wpdb
    982982 *
    983  * @param string $option    Name of option to retrieve. Expected to not be SQL-escaped.
    984  * @param mixed  $default   Optional value to return if option doesn't exist. Default false.
    985  * @param bool   $use_cache Whether to use cache. Multisite only. Default true.
     983 * @param string     $option      Name of option to retrieve. Expected to not be SQL-escaped.
     984 * @param mixed      $default     Optional value to return if option doesn't exist. Default false.
     985 * @param bool       $use_cache   Whether to use cache. Multisite only. Default true.
     986 * @param bool|int   $network_id  The ID of the network for the option to retrieve. Multisite only. Default false.
    986987 * @return mixed Value set for the option.
    987988 */
    988 function get_site_option( $option, $default = false, $use_cache = true ) {
     989function get_site_option( $option, $default = false, $use_cache = true, $network_id = false ) {
    989990        global $wpdb;
    990991
     992        if( ! $network_id  ){
     993                $network_id = $wpdb->siteid;
     994        }
     995
    991996        /**
    992997         * Filter an existing site option before it is retrieved.
    993998         *
    function get_site_option( $option, $default = false, $use_cache = true ) { 
    9991004         * @since 2.9.0 As 'pre_site_option_' . $key
    10001005         * @since 3.0.0
    10011006         * @since 4.4.0 The `$option` parameter was added
     1007         * @since 4.4.0 The `$network_id` parameter was added
    10021008         *
    10031009         * @param mixed  $pre_option The default value to return if the option does not exist.
    10041010         * @param string $option     Option name.
    10051011         */
    1006         $pre = apply_filters( 'pre_site_option_' . $option, false, $option );
     1012        $pre = apply_filters( 'pre_site_option_' . $option, false, $option, $network_id );
    10071013
    10081014        if ( false !== $pre )
    10091015                return $pre;
    10101016
    10111017        // prevent non-existent options from triggering multiple queries
    1012         $notoptions_key = "{$wpdb->siteid}:notoptions";
     1018        $notoptions_key = "{$network_id}:notoptions";
    10131019        $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
    10141020
    10151021        if ( isset( $notoptions[$option] ) ) {
    function get_site_option( $option, $default = false, $use_cache = true ) { 
    10211027                 *
    10221028                 * @since 3.4.0
    10231029                 * @since 4.4.0 The `$option` parameter was added.
     1030                 * @since 4.4.0 The `$network_id` parameter was added
    10241031                 *
    10251032                 * @param mixed  $default The value to return if the site option does not exist
    10261033                 *                        in the database.
    10271034                 * @param string $option  Option name.
    10281035                 */
    1029                 return apply_filters( 'default_site_option_' . $option, $default, $option );
     1036                return apply_filters( 'default_site_option_' . $option, $default, $option, $network_id );
    10301037        }
    10311038
    10321039        if ( ! is_multisite() ) {
    10331040
    10341041                /** This filter is documented in wp-includes/option.php */
    1035                 $default = apply_filters( 'default_site_option_' . $option, $default, $option );
     1042                $default = apply_filters( 'default_site_option_' . $option, $default, $option, $network_id );
    10361043                $value = get_option($option, $default);
    10371044        } else {
    1038                 $cache_key = "{$wpdb->siteid}:$option";
     1045                $cache_key = "{$network_id}:$option";
    10391046                if ( $use_cache )
    10401047                        $value = wp_cache_get($cache_key, 'site-options');
    10411048
    10421049                if ( !isset($value) || (false === $value) ) {
    1043                         $row = $wpdb->get_row( $wpdb->prepare("SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) );
     1050                        $row = $wpdb->get_row( $wpdb->prepare("SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $network_id ) );
    10441051
    10451052                        // Has to be get_row instead of get_var because of funkiness with 0, false, null values
    10461053                        if ( is_object( $row ) ) {
    function get_site_option( $option, $default = false, $use_cache = true ) { 
    10551062                                wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
    10561063
    10571064                                /** This filter is documented in wp-includes/option.php */
    1058                                 $value = apply_filters( 'default_site_option_' . $option, $default, $option );
     1065                                $value = apply_filters( 'default_site_option_' . $option, $default, $option, $network_id );
    10591066                        }
    10601067                }
    10611068        }
    function get_site_option( $option, $default = false, $use_cache = true ) { 
    10681075         * @since 2.9.0 As 'site_option_' . $key
    10691076         * @since 3.0.0
    10701077         * @since 4.4.0 The `$option` parameter was added
     1078         * @since 4.4.0 The `$network_id` parameter was added
    10711079         *
    10721080         * @param mixed  $value  Value of site option.
    10731081         * @param string $option Option name.
    10741082         */
    1075         return apply_filters( 'site_option_' . $option, $value, $option );
     1083        return apply_filters( 'site_option_' . $option, $value, $option, $network_id );
    10761084}
    10771085
    10781086/**
    function get_site_option( $option, $default = false, $use_cache = true ) { 
    10861094 *
    10871095 * @global wpdb $wpdb
    10881096 *
    1089  * @param string $option Name of option to add. Expected to not be SQL-escaped.
    1090  * @param mixed  $value  Optional. Option value, can be anything. Expected to not be SQL-escaped.
     1097 * @param string     $option Name of option to add. Expected to not be SQL-escaped.
     1098 * @param mixed      $value  Optional. Option value, can be anything. Expected to not be SQL-escaped.
     1099 * @param bool|int   $network_id  The ID of the network for the option to retrieve. Multisite only. Default false.
    10911100 * @return bool False if option was not added and true if option was added.
    10921101 */
    1093 function add_site_option( $option, $value ) {
     1102function add_site_option( $option, $value, $network_id = false ) {
    10941103        global $wpdb;
    10951104
    10961105        wp_protect_special_option( $option );
    10971106
     1107        if( ! $network_id  ){
     1108                $network_id = $wpdb->siteid;
     1109        }
     1110
    10981111        /**
    10991112         * Filter the value of a specific site option before it is added.
    11001113         *
    function add_site_option( $option, $value ) { 
    11031116         * @since 2.9.0 As 'pre_add_site_option_' . $key
    11041117         * @since 3.0.0
    11051118         * @since 4.4.0 The `$option` parameter was added
     1119         * @since 4.4.0 The `$network_id` parameter was added
    11061120         *
    11071121         * @param mixed  $value  Value of site option.
    11081122         * @param string $option Option name.
    11091123         */
    1110         $value = apply_filters( 'pre_add_site_option_' . $option, $value, $option );
     1124        $value = apply_filters( 'pre_add_site_option_' . $option, $value, $option, $network_id );
    11111125
    1112         $notoptions_key = "{$wpdb->siteid}:notoptions";
     1126        $notoptions_key = "{$network_id}:notoptions";
    11131127
    11141128        if ( !is_multisite() ) {
    11151129                $result = add_option( $option, $value );
    11161130        } else {
    1117                 $cache_key = "{$wpdb->siteid}:$option";
     1131                $cache_key = "{$network_id}:$option";
    11181132
    11191133                // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
    11201134                $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
    11211135                if ( ! is_array( $notoptions ) || ! isset( $notoptions[$option] ) )
    1122                         if ( false !== get_site_option( $option ) )
     1136                        if ( false !== get_site_option( $option, false, true, $network_id ) )
    11231137                                return false;
    11241138
    11251139                $value = sanitize_option( $option, $value );
    11261140
    11271141                $serialized_value = maybe_serialize( $value );
    1128                 $result = $wpdb->insert( $wpdb->sitemeta, array('site_id' => $wpdb->siteid, 'meta_key' => $option, 'meta_value' => $serialized_value ) );
     1142                $result = $wpdb->insert( $wpdb->sitemeta, array('site_id' => $network_id, 'meta_key' => $option, 'meta_value' => $serialized_value ) );
    11291143
    11301144                if ( ! $result )
    11311145                        return false;
    function add_site_option( $option, $value ) { 
    11491163                 *
    11501164                 * @since 2.9.0 As "add_site_option_{$key}"
    11511165                 * @since 3.0.0
     1166                 * @since 4.4.0 The `$network_id` parameter was added
    11521167                 *
    11531168                 * @param string $option Name of site option.
    11541169                 * @param mixed  $value  Value of site option.
     1170                 *
    11551171                 */
    1156                 do_action( "add_site_option_{$option}", $option, $value );
     1172                do_action( "add_site_option_{$option}", $option, $value, $network_id );
    11571173
    11581174                /**
    11591175                 * Fires after a site option has been successfully added.
    11601176                 *
    11611177                 * @since 3.0.0
     1178                 * @since 4.4.0 The `$network_id` parameter was added
    11621179                 *
    11631180                 * @param string $option Name of site option.
    11641181                 * @param mixed  $value  Value of site option.
    11651182                 */
    1166                 do_action( "add_site_option", $option, $value );
     1183                do_action( "add_site_option", $option, $value, $network_id );
    11671184
    11681185                return true;
    11691186        }
    function add_site_option( $option, $value ) { 
    11801197 * @global wpdb $wpdb
    11811198 *
    11821199 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
     1200 * @param bool|int   $network_id  The ID of the network for the option to retrieve. Multisite only. Default false.
    11831201 * @return bool True, if succeed. False, if failure.
    11841202 */
    1185 function delete_site_option( $option ) {
     1203function delete_site_option( $option, $network_id = false ) {
    11861204        global $wpdb;
    11871205
    11881206        // ms_protect_special_option( $option ); @todo
    11891207
     1208        if( ! $network_id  ){
     1209                $network_id = $wpdb->siteid;
     1210        }
     1211
    11901212        /**
    11911213         * Fires immediately before a specific site option is deleted.
    11921214         *
    function delete_site_option( $option ) { 
    11941216         *
    11951217         * @since 3.0.0
    11961218         * @since 4.4.0 The `$option` parameter was added
     1219         * @since 4.4.0 The `$network_id` parameter was added
    11971220         *
    11981221         * @param string $option Option name.
    11991222         */
    1200         do_action( 'pre_delete_site_option_' . $option, $option );
     1223        do_action( 'pre_delete_site_option_' . $option, $option, $network_id );
    12011224
    12021225        if ( !is_multisite() ) {
    12031226                $result = delete_option( $option );
    12041227        } else {
    1205                 $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) );
     1228                $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $network_id ) );
    12061229                if ( is_null( $row ) || !$row->meta_id )
    12071230                        return false;
    1208                 $cache_key = "{$wpdb->siteid}:$option";
     1231                $cache_key = "{$network_id}:$option";
    12091232                wp_cache_delete( $cache_key, 'site-options' );
    12101233
    1211                 $result = $wpdb->delete( $wpdb->sitemeta, array( 'meta_key' => $option, 'site_id' => $wpdb->siteid ) );
     1234                $result = $wpdb->delete( $wpdb->sitemeta, array( 'meta_key' => $option, 'site_id' => $network_id ) );
    12121235        }
    12131236
    12141237        if ( $result ) {
    function delete_site_option( $option ) { 
    12201243                 *
    12211244                 * @since 2.9.0 As "delete_site_option_{$key}"
    12221245                 * @since 3.0.0
     1246                 * @since 4.4.0 The `$network_id` parameter was added
    12231247                 *
    12241248                 * @param string $option Name of the site option.
    12251249                 */
    1226                 do_action( "delete_site_option_{$option}", $option );
     1250                do_action( "delete_site_option_{$option}", $option, $network_id );
    12271251
    12281252                /**
    12291253                 * Fires after a site option has been deleted.
    12301254                 *
    12311255                 * @since 3.0.0
     1256                 * @since 4.4.0 The `$network_id` parameter was added
    12321257                 *
    12331258                 * @param string $option Name of the site option.
    12341259                 */
    1235                 do_action( "delete_site_option", $option );
     1260                do_action( "delete_site_option", $option, $network_id );
    12361261
    12371262                return true;
    12381263        }
    function delete_site_option( $option ) { 
    12501275 *
    12511276 * @param string $option Name of option. Expected to not be SQL-escaped.
    12521277 * @param mixed  $value  Option value. Expected to not be SQL-escaped.
     1278 * @param bool|int   $network_id  The ID of the network for the option to retrieve. Multisite only. Default false.
    12531279 * @return bool False if value was not updated and true if value was updated.
    12541280 */
    1255 function update_site_option( $option, $value ) {
     1281function update_site_option( $option, $value, $network_id = false ) {
    12561282        global $wpdb;
    12571283
    12581284        wp_protect_special_option( $option );
    12591285
    1260         $old_value = get_site_option( $option );
     1286        if( ! $network_id  ){
     1287                $network_id = $wpdb->siteid;
     1288        }
     1289
     1290        $old_value = get_site_option( $option, false, true, $network_id );
    12611291
    12621292        /**
    12631293         * Filter a specific site option before its value is updated.
    function update_site_option( $option, $value ) { 
    12671297         * @since 2.9.0 As 'pre_update_site_option_' . $key
    12681298         * @since 3.0.0
    12691299         * @since 4.4.0 The `$option` parameter was added
     1300         * @since 4.4.0 The `$network_id` parameter was added
    12701301         *
    12711302         * @param mixed  $value     New value of site option.
    12721303         * @param mixed  $old_value Old value of site option.
    12731304         * @param string $option    Option name.
    12741305         */
    1275         $value = apply_filters( 'pre_update_site_option_' . $option, $value, $old_value, $option );
     1306        $value = apply_filters( 'pre_update_site_option_' . $option, $value, $old_value, $option, $network_id );
    12761307
    12771308        if ( $value === $old_value )
    12781309                return false;
    12791310
    12801311        if ( false === $old_value )
    1281                 return add_site_option( $option, $value );
     1312                return add_site_option( $option, $value, $network_id );
    12821313
    1283         $notoptions_key = "{$wpdb->siteid}:notoptions";
     1314        $notoptions_key = "{$network_id}:notoptions";
    12841315        $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
    12851316        if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
    12861317                unset( $notoptions[$option] );
    function update_site_option( $option, $value ) { 
    12931324                $value = sanitize_option( $option, $value );
    12941325
    12951326                $serialized_value = maybe_serialize( $value );
    1296                 $result = $wpdb->update( $wpdb->sitemeta, array( 'meta_value' => $serialized_value ), array( 'site_id' => $wpdb->siteid, 'meta_key' => $option ) );
     1327                $result = $wpdb->update( $wpdb->sitemeta, array( 'meta_value' => $serialized_value ), array( 'site_id' => $network_id, 'meta_key' => $option ) );
    12971328
    12981329                if ( $result ) {
    1299                         $cache_key = "{$wpdb->siteid}:$option";
     1330                        $cache_key = "{$network_id}:$option";
    13001331                        wp_cache_set( $cache_key, $value, 'site-options' );
    13011332                }
    13021333        }
    function update_site_option( $option, $value ) { 
    13101341                 *
    13111342                 * @since 2.9.0 As "update_site_option_{$key}"
    13121343                 * @since 3.0.0
     1344                 * @since 4.4.0 The `$network_id` parameter was added
    13131345                 *
    13141346                 * @param string $option    Name of site option.
    13151347                 * @param mixed  $value     Current value of site option.
    13161348                 * @param mixed  $old_value Old value of site option.
    13171349                 */
    1318                 do_action( "update_site_option_{$option}", $option, $value, $old_value );
     1350                do_action( "update_site_option_{$option}", $option, $value, $old_value, $network_id );
    13191351
    13201352                /**
    13211353                 * Fires after the value of a site option has been successfully updated.
    13221354                 *
    13231355                 * @since 3.0.0
     1356                 * @since 4.4.0 The `$network_id` parameter was added
    13241357                 *
    13251358                 * @param string $option    Name of site option.
    13261359                 * @param mixed  $value     Current value of site option.
    13271360                 * @param mixed  $old_value Old value of site option.
    13281361                 */
    1329                 do_action( "update_site_option", $option, $value, $old_value );
     1362                do_action( "update_site_option", $option, $value, $old_value, $network_id );
    13301363
    13311364                return true;
    13321365        }