Make WordPress Core

Ticket #35379: 35379.4.diff

File 35379.4.diff, 4.2 KB (added by codex-m, 9 years ago)

Refreshed version of patch simplified

  • src/wp-includes/formatting.php

     
    36353635 * of functions depending on the $option.
    36363636 *
    36373637 * @since 2.0.5
     3638 * @since 4.6.0 Added a $context and $context_id parameter to distinguish between site and network options.
    36383639 *
    36393640 * @global wpdb $wpdb WordPress database abstraction object.
    36403641 *
    3641  * @param string $option The name of the option.
    3642  * @param string $value  The unsanitised value.
     3642 * @param string $option  The name of the option.
     3643 * @param string $value   The unsanitised value.
     3644 * @param string $context The context of the option (either 'site' or 'network'). If it passes 'network' as context in a single-site mode, 'site' context is used.
     3645 * @param int $context_id Unique identifier of the $context, example for 'network' , it is the network id.
    36433646 * @return string Sanitized value.
    36443647 */
    3645 function sanitize_option( $option, $value ) {
     3648function sanitize_option( $option, $value, $context = 'site', $context_id = '' ) {
    36463649        global $wpdb;
    36473650
    36483651        $original_value = $value;
    3649         $error = '';
    3650 
     3652        $error = '';   
     3653       
     3654        if ( 'network' === $context ) {
     3655                //'network' context passed
     3656                if ( ! is_multisite() ) {
     3657                        //Not in multisite, fallback to 'site' context
     3658                        $context = 'site';
     3659                } elseif ( ! $context_id ) {
     3660                        //context_id not set, use the current network ID
     3661                        global $current_site;   
     3662                        $context_id = $current_site->id;
     3663                }
     3664        }       
     3665       
    36513666        switch ( $option ) {
    36523667                case 'admin_email' :
    36533668                case 'new_admin_email' :
     
    37833798                                $allowed[] = WPLANG;
    37843799                        }
    37853800                        if ( ! in_array( $value, $allowed ) && ! empty( $value ) ) {
    3786                                 $value = get_option( $option );
     3801                                if ( 'network' === $context ) {
     3802                                        $value = get_network_option( $context_id, $option, false );
     3803                                } else {
     3804                                        $value = get_option( $option );
     3805                                }                               
    37873806                        }
    37883807                        break;
    37893808
     
    38633882        }
    38643883
    38653884        if ( ! empty( $error ) ) {
    3866                 $value = get_option( $option );
     3885                if ( 'network' === $context ) {
     3886                        $value = get_network_option( $context_id, $option, false );
     3887                } else {
     3888                        $value = get_option( $option );
     3889                }               
    38673890                if ( function_exists( 'add_settings_error' ) ) {
    38683891                        add_settings_error( $option, "invalid_{$option}", $error );
    38693892                }
     
    38703893        }
    38713894
    38723895        /**
     3896         * Filter an option value following sanitization based on its context.
     3897         *
     3898         * The dynamic part $context of the filter determines what kind of option is being sanitized.
     3899         * It can be set to either 'site' or 'network'.
     3900         *
     3901         * The $option part of the filter is the option name being sanitized.
     3902         *
     3903         * @since 4.6.0
     3904         *
     3905         * @param string $value          The sanitized option value.
     3906         * @param string $option         The option name.
     3907         * @param string $original_value The original value passed to the function.
     3908         */
     3909        $value = apply_filters( "sanitize_{$context}_context_option_{$option}", $value, $option, $original_value );
     3910
     3911        /**
    38733912         * Filter an option value following sanitization.
    38743913         *
     3914         * This filter is here for backwards compatibility. You should use the
     3915         * `sanitize_{$context}_context_option_{$option}` filter instead.
     3916         *
    38753917         * @since 2.3.0
    38763918         * @since 4.3.0 Added the `$original_value` parameter.
    38773919         *
  • src/wp-includes/option.php

     
    12391239                        }
    12401240                }
    12411241
    1242                 $value = sanitize_option( $option, $value );
     1242                $value = sanitize_option( $option, $value, 'network', $network_id );
    12431243
    12441244                $serialized_value = maybe_serialize( $value );
    12451245                $result = $wpdb->insert( $wpdb->sitemeta, array( 'site_id'    => $network_id, 'meta_key'   => $option, 'meta_value' => $serialized_value ) );
     
    14371437        if ( ! is_multisite() ) {
    14381438                $result = update_option( $option, $value, 'no' );
    14391439        } else {
    1440                 $value = sanitize_option( $option, $value );
     1440                $value = sanitize_option( $option, $value, 'network', $network_id );
    14411441
    14421442                $serialized_value = maybe_serialize( $value );
    14431443                $result = $wpdb->update( $wpdb->sitemeta, array( 'meta_value' => $serialized_value ), array( 'site_id' => $network_id, 'meta_key' => $option ) );