Make WordPress Core

Ticket #39461: 39461.diff

File 39461.diff, 11.9 KB (added by cdog, 8 years ago)
  • wp-admin/custom-background.php

     
    139139                        if ( in_array( $_POST['background-preset'], array( 'default', 'fill', 'fit', 'repeat', 'custom' ), true ) ) {
    140140                                $preset = $_POST['background-preset'];
    141141                        } else {
    142                                 $preset = 'default';
     142                                $preset = 'custom';
    143143                        }
    144144
    145145                        set_theme_mod( 'background_preset', $preset );
     
    261261                $background_image_thumb = get_background_image();
    262262                if ( $background_image_thumb ) {
    263263                        $background_image_thumb = esc_url( set_url_scheme( get_theme_mod( 'background_image_thumb', str_replace( '%', '%%', $background_image_thumb ) ) ) );
     264                        $background_preset = get_theme_mod( 'background_preset', get_theme_support( 'custom-background', 'default-preset' ) );
    264265                        $background_position_x = get_theme_mod( 'background_position_x', get_theme_support( 'custom-background', 'default-position-x' ) );
    265266                        $background_position_y = get_theme_mod( 'background_position_y', get_theme_support( 'custom-background', 'default-position-y' ) );
    266                         $background_size = get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) );
    267                         $background_repeat = get_theme_mod( 'background_repeat', get_theme_support( 'custom-background', 'default-repeat' ) );
    268                         $background_attachment = get_theme_mod( 'background_attachment', get_theme_support( 'custom-background', 'default-attachment' ) );
     267                        $background_size = get_theme_mod( 'background_size', _sanitize_custom_background_prop( 'size', $background_preset ) );
     268                        $background_repeat = get_theme_mod( 'background_repeat', _sanitize_custom_background_prop( 'repeat', $background_preset ) );
     269                        $background_attachment = get_theme_mod( 'background_attachment', _sanitize_custom_background_prop( 'attachment', $background_preset ) );
    269270
    270271                        // Background-image URL must be single quote, see below.
    271272                        $background_styles .= " background-image: url('$background_image_thumb');"
  • wp-admin/js/customize-controls.js

     
    51835183                // Initialize the connection with the parent frame.
    51845184                parent.send( 'ready' );
    51855185
    5186                 // Control visibility for default controls
    5187                 $.each({
    5188                         'background_image': {
    5189                                 controls: [ 'background_preset', 'background_position', 'background_size', 'background_repeat', 'background_attachment' ],
    5190                                 callback: function( to ) { return !! to; }
    5191                         },
    5192                         'show_on_front': {
    5193                                 controls: [ 'page_on_front', 'page_for_posts' ],
    5194                                 callback: function( to ) { return 'page' === to; }
    5195                         },
    5196                         'header_textcolor': {
    5197                                 controls: [ 'header_textcolor' ],
    5198                                 callback: function( to ) { return 'blank' !== to; }
    5199                         }
    5200                 }, function( settingId, o ) {
    5201                         api( settingId, function( setting ) {
    5202                                 $.each( o.controls, function( i, controlId ) {
    5203                                         api.control( controlId, function( control ) {
    5204                                                 var visibility = function( to ) {
    5205                                                         control.container.toggle( o.callback( to ) );
    5206                                                 };
    5207 
    5208                                                 visibility( setting.get() );
    5209                                                 setting.bind( visibility );
    5210                                         });
    5211                                 });
    5212                         });
    5213                 });
    5214 
    52155186                api.control( 'background_preset', function( control ) {
    5216                         var visibility, defaultValues, values, toggleVisibility, updateSettings, preset;
     5187                        var visibility, defaultValues, values, updateSettings, preset;
    52175188
    52185189                        visibility = { // position, size, repeat, attachment
    52195190                                'default': [ false, false, false, false ],
     
    52335204
    52345205                        values = { // position_x, position_y, size, repeat, attachment
    52355206                                'default': defaultValues,
    5236                                 'fill': [ 'left', 'top', 'cover', 'no-repeat', 'fixed' ],
    5237                                 'fit': [ 'left', 'top', 'contain', 'no-repeat', 'fixed' ],
    5238                                 'repeat': [ 'left', 'top', 'auto', 'repeat', 'scroll' ]
     5207                                'fill': [ defaultValues[0], defaultValues[1], 'cover', 'no-repeat', 'fixed' ],
     5208                                'fit': [ defaultValues[0], defaultValues[1], 'contain', defaultValues[3], 'fixed' ],
     5209                                'repeat': [ defaultValues[0], defaultValues[1], 'auto', 'repeat', defaultValues[4] ]
    52395210                        };
    52405211
    5241                         // @todo These should actually toggle the active state, but without the preview overriding the state in data.activeControls.
    5242                         toggleVisibility = function( preset ) {
    5243                                 _.each( [ 'background_position', 'background_size', 'background_repeat', 'background_attachment' ], function( controlId, i ) {
    5244                                         var control = api.control( controlId );
    5245                                         if ( control ) {
    5246                                                 control.container.toggle( visibility[ preset ][ i ] );
    5247                                         }
    5248                                 } );
    5249                         };
    5250 
    52515212                        updateSettings = function( preset ) {
    52525213                                _.each( [ 'background_position_x', 'background_position_y', 'background_size', 'background_repeat', 'background_attachment' ], function( settingId, i ) {
    52535214                                        var setting = api( settingId );
     
    52585219                        };
    52595220
    52605221                        preset = control.setting.get();
    5261                         toggleVisibility( preset );
    52625222
     5223                        control.toggleControls = function( preset ) {
     5224                                api( 'background_image', function( setting ) {
     5225                                        var to = setting.get();
     5226
     5227                                        _.each( [ 'background_position', 'background_size', 'background_repeat', 'background_attachment' ], function( controlId, i ) {
     5228                                                var control = api.control( controlId );
     5229
     5230                                                if ( control ) {
     5231                                                        control.container.toggle( !! to ? visibility[ preset ][ i ] : false );
     5232                                                }
     5233                                        });
     5234                                });
     5235                        };
     5236
     5237                        control.toggleControls( preset );
     5238
    52635239                        control.setting.bind( 'change', function( preset ) {
    5264                                 toggleVisibility( preset );
     5240                                control.toggleControls( preset );
     5241
    52655242                                if ( 'custom' !== preset ) {
    52665243                                        updateSettings( preset );
    52675244                                }
     
    54055382                        } );
    54065383                } );
    54075384
     5385                // Control visibility for default controls
     5386                $.each({
     5387                        'background_image': {
     5388                                controls: [ 'background_preset' ],
     5389                                callback: function( to ) { return !! to; }
     5390                        },
     5391                        'show_on_front': {
     5392                                controls: [ 'page_on_front', 'page_for_posts' ],
     5393                                callback: function( to ) { return 'page' === to; }
     5394                        },
     5395                        'header_textcolor': {
     5396                                controls: [ 'header_textcolor' ],
     5397                                callback: function( to ) { return 'blank' !== to; }
     5398                        }
     5399                }, function( settingId, o ) {
     5400                        api( settingId, function( setting ) {
     5401                                $.each( o.controls, function( i, controlId ) {
     5402                                        api.control( controlId, function( control ) {
     5403                                                var visibility = function( to ) {
     5404                                                        if ( 'background_preset' === controlId ) {
     5405                                                                control.toggleControls( control.setting.get() );
     5406                                                        }
     5407
     5408                                                        control.container.toggle( o.callback( to ) );
     5409                                                };
     5410
     5411                                                visibility( setting.get() );
     5412
     5413                                                setting.bind( visibility );
     5414                                        });
     5415                                });
     5416                        });
     5417                });
     5418
    54085419                // Update the setting validities.
    54095420                api.previewer.bind( 'selective-refresh-setting-validities', function handleSelectiveRefreshedSettingValidities( settingValidities ) {
    54105421                        api._handleSettingValidities( {
  • wp-includes/class-wp-customize-manager.php

     
    40634063
    40644064                $this->add_control( new WP_Customize_Background_Image_Control( $this ) );
    40654065
     4066                $background_preset = get_theme_support( 'custom-background', 'default-preset' );
     4067
    40664068                $this->add_setting( 'background_preset', array(
    4067                         'default'        => get_theme_support( 'custom-background', 'default-preset' ),
     4069                        'default'        => $background_preset,
    40684070                        'theme_supports' => 'custom-background',
    40694071                        'sanitize_callback' => array( $this, '_sanitize_background_setting' ),
    40704072                ) );
     
    41044106                ) ) );
    41054107
    41064108                $this->add_setting( 'background_size', array(
    4107                         'default'        => get_theme_support( 'custom-background', 'default-size' ),
     4109                        'default'        => _sanitize_custom_background_prop( 'size', $background_preset ),
    41084110                        'theme_supports' => 'custom-background',
    41094111                        'sanitize_callback' => array( $this, '_sanitize_background_setting' ),
    41104112                ) );
     
    41214123                ) );
    41224124
    41234125                $this->add_setting( 'background_repeat', array(
    4124                         'default'           => get_theme_support( 'custom-background', 'default-repeat' ),
     4126                        'default'           => _sanitize_custom_background_prop( 'repeat', $background_preset ),
    41254127                        'sanitize_callback' => array( $this, '_sanitize_background_setting' ),
    41264128                        'theme_supports'    => 'custom-background',
    41274129                ) );
     
    41334135                ) );
    41344136
    41354137                $this->add_setting( 'background_attachment', array(
    4136                         'default'           => get_theme_support( 'custom-background', 'default-attachment' ),
     4138                        'default'           => _sanitize_custom_background_prop( 'attachment', $background_preset ),
    41374139                        'sanitize_callback' => array( $this, '_sanitize_background_setting' ),
    41384140                        'theme_supports'    => 'custom-background',
    41394141                ) );
  • wp-includes/theme.php

     
    15371537}
    15381538
    15391539/**
     1540 * Sanitize background preset property value.
     1541 *
     1542 * @since 4.8.0
     1543 * @access protected
     1544 *
     1545 * @param string $prop   A background property name (expects size, repeat or attachment).
     1546 * @param string $preset Optional. A preset name (expects default, fill, fit, repeat or custom). Defaults to the default preset.
     1547 * @return string|bool The sanitized background property value or false if the specified preset doesn't exist.
     1548 */
     1549function _sanitize_custom_background_prop( $prop, $preset = 'default' ) {
     1550        if ( 'size' === $prop ) {
     1551                if ( 'fill' === $preset ) {
     1552                        return 'cover';
     1553                } elseif ( 'fit' === $preset ) {
     1554                        return 'contain';
     1555                } elseif ( 'repeat' === $preset ) {
     1556                        return 'auto';
     1557                } else {
     1558                        return get_theme_support( 'custom-background', 'default-size' );
     1559                }
     1560        } elseif ( 'repeat' === $prop ) {
     1561                if ( 'fill' === $preset ) {
     1562                        return 'no-repeat';
     1563                } elseif ( 'repeat' === $preset ) {
     1564                        return 'repeat';
     1565                } else {
     1566                        return get_theme_support( 'custom-background', 'default-repeat' );
     1567                }
     1568        } elseif ( 'attachment' === $prop ) {
     1569                if ( 'fill' === $preset || 'fit' === $preset ) {
     1570                        $attachment = 'fixed';
     1571                } else {
     1572                        $attachment = get_theme_support( 'custom-background', 'default-attachment' );
     1573                }
     1574        }
     1575
     1576        return false;
     1577}
     1578
     1579/**
    15401580 * Default custom background callback.
    15411581 *
    15421582 * @since 3.0.0
     
    15661606        if ( $background ) {
    15671607                $image = ' background-image: url("' . esc_url_raw( $background ) . '");';
    15681608
     1609                // Background Preset.
     1610                $preset = get_theme_mod( 'background_preset', get_theme_support( 'custom-background', 'default-preset' ) );
     1611
    15691612                // Background Position.
    15701613                $position_x = get_theme_mod( 'background_position_x', get_theme_support( 'custom-background', 'default-position-x' ) );
    15711614                $position_y = get_theme_mod( 'background_position_y', get_theme_support( 'custom-background', 'default-position-y' ) );
     
    15811624                $position = " background-position: $position_x $position_y;";
    15821625
    15831626                // Background Size.
    1584                 $size = get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) );
     1627                $size = get_theme_mod( 'background_size', _sanitize_custom_background_prop( 'size', $preset ) );
    15851628
    15861629                if ( ! in_array( $size, array( 'auto', 'contain', 'cover' ), true ) ) {
    15871630                        $size = 'auto';
     
    15901633                $size = " background-size: $size;";
    15911634
    15921635                // Background Repeat.
    1593                 $repeat = get_theme_mod( 'background_repeat', get_theme_support( 'custom-background', 'default-repeat' ) );
     1636                $repeat = get_theme_mod( 'background_repeat', _sanitize_custom_background_prop( 'repeat', $preset ) );
    15941637
    15951638                if ( ! in_array( $repeat, array( 'repeat-x', 'repeat-y', 'repeat', 'no-repeat' ), true ) ) {
    15961639                        $repeat = 'repeat';
     
    15991642                $repeat = " background-repeat: $repeat;";
    16001643
    16011644                // Background Scroll.
    1602                 $attachment = get_theme_mod( 'background_attachment', get_theme_support( 'custom-background', 'default-attachment' ) );
     1645                $attachment = get_theme_mod( 'background_attachment', _sanitize_custom_background_prop( 'attachment', $preset ) );
    16031646
    16041647                if ( 'fixed' !== $attachment ) {
    16051648                        $attachment = 'scroll';