Make WordPress Core

Ticket #35379: 35379.3.diff

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

Updated patch -merged version ($context and $context_id illustrated)

  • 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')
     3645 * @param int $context_id Unique identifier of the $context, e.g. for 'network' , its 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 mode
     3656                global $current_site;           
     3657                if ( ( isset( $current_site->id ) ) && ( empty( $context_id ) ) ) {
     3658                        //If $context_id is not set, use the current network ID
     3659                        $network_id = $current_site->id;
     3660                } else {
     3661                        //If context_id is set, make sure its a network ID
     3662                        $context_id = (int) $context_id;
     3663                        if ( $context_id > 0 ) {
     3664                                //Use the validated context as network ID
     3665                                $network_id = $context_id;
     3666                        }                       
     3667                }               
     3668        }
     3669       
    36513670        switch ( $option ) {
    36523671                case 'admin_email' :
    36533672                case 'new_admin_email' :
     
    37833802                                $allowed[] = WPLANG;
    37843803                        }
    37853804                        if ( ! in_array( $value, $allowed ) && ! empty( $value ) ) {
    3786                                 $value = get_option( $option );
     3805                                if ( 'network' === $context ) {
     3806                                        $value = get_network_option( $network_id, $option, false );
     3807                                } else {
     3808                                        $value = get_option( $option );
     3809                                }                               
    37873810                        }
    37883811                        break;
    37893812
     
    38633886        }
    38643887
    38653888        if ( ! empty( $error ) ) {
    3866                 $value = get_option( $option );
     3889                if ( 'network' === $context ) {
     3890                        $value = get_network_option( $network_id, $option, false );
     3891                } else {
     3892                        $value = get_option( $option );
     3893                }               
    38673894                if ( function_exists( 'add_settings_error' ) ) {
    38683895                        add_settings_error( $option, "invalid_{$option}", $error );
    38693896                }
     
    38703897        }
    38713898
    38723899        /**
     3900         * Filter an option value following sanitization based on its context.
     3901         *
     3902         * The dynamic part $context of the filter determines what kind of option is being sanitized.
     3903         * It can be set to either 'site' or 'network'.
     3904         *
     3905         * The $option part of the filter is the option name being sanitized.
     3906         *
     3907         * @since 4.6.0
     3908         *
     3909         * @param string $value          The sanitized option value.
     3910         * @param string $option         The option name.
     3911         * @param string $original_value The original value passed to the function.
     3912         */
     3913        $value = apply_filters( "sanitize_{$context}_context_option_{$option}", $value, $option, $original_value );
     3914
     3915        /**
    38733916         * Filter an option value following sanitization.
    38743917         *
     3918         * This filter is here for backwards compatibility. You should use the
     3919         * `sanitize_{$context}_context_option_{$option}` filter instead.
     3920         *
    38753921         * @since 2.3.0
    38763922         * @since 4.3.0 Added the `$original_value` parameter.
    38773923         *
  • src/wp-includes/option.php

     
    12351235                        }
    12361236                }
    12371237
    1238                 $value = sanitize_option( $option, $value );
     1238                $value = sanitize_option( $option, $value, 'network', $network_id );
    12391239
    12401240                $serialized_value = maybe_serialize( $value );
    12411241                $result = $wpdb->insert( $wpdb->sitemeta, array( 'site_id'    => $network_id, 'meta_key'   => $option, 'meta_value' => $serialized_value ) );
     
    14331433        if ( ! is_multisite() ) {
    14341434                $result = update_option( $option, $value );
    14351435        } else {
    1436                 $value = sanitize_option( $option, $value );
     1436                $value = sanitize_option( $option, $value, 'network', $network_id );
    14371437
    14381438                $serialized_value = maybe_serialize( $value );
    14391439                $result = $wpdb->update( $wpdb->sitemeta, array( 'meta_value' => $serialized_value ), array( 'site_id' => $network_id, 'meta_key' => $option ) );