Make WordPress Core

Ticket #39461: 39561.diff

File 39561.diff, 11.5 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

     
    51365136                // Initialize the connection with the parent frame.
    51375137                parent.send( 'ready' );
    51385138
    5139                 // Control visibility for default controls
    5140                 $.each({
    5141                         'background_image': {
    5142                                 controls: [ 'background_preset', 'background_position', 'background_size', 'background_repeat', 'background_attachment' ],
    5143                                 callback: function( to ) { return !! to; }
    5144                         },
    5145                         'show_on_front': {
    5146                                 controls: [ 'page_on_front', 'page_for_posts' ],
    5147                                 callback: function( to ) { return 'page' === to; }
    5148                         },
    5149                         'header_textcolor': {
    5150                                 controls: [ 'header_textcolor' ],
    5151                                 callback: function( to ) { return 'blank' !== to; }
    5152                         }
    5153                 }, function( settingId, o ) {
    5154                         api( settingId, function( setting ) {
    5155                                 $.each( o.controls, function( i, controlId ) {
    5156                                         api.control( controlId, function( control ) {
    5157                                                 var visibility = function( to ) {
    5158                                                         control.container.toggle( o.callback( to ) );
    5159                                                 };
    5160 
    5161                                                 visibility( setting.get() );
    5162                                                 setting.bind( visibility );
    5163                                         });
    5164                                 });
    5165                         });
    5166                 });
    5167 
    51685139                api.control( 'background_preset', function( control ) {
    5169                         var visibility, defaultValues, values, toggleVisibility, updateSettings, preset;
     5140                        var visibility, defaultValues, values, updateSettings, preset;
    51705141
    51715142                        visibility = { // position, size, repeat, attachment
    51725143                                'default': [ false, false, false, false ],
     
    51865157
    51875158                        values = { // position_x, position_y, size, repeat, attachment
    51885159                                'default': defaultValues,
    5189                                 'fill': [ 'left', 'top', 'cover', 'no-repeat', 'fixed' ],
    5190                                 'fit': [ 'left', 'top', 'contain', 'no-repeat', 'fixed' ],
    5191                                 'repeat': [ 'left', 'top', 'auto', 'repeat', 'scroll' ]
     5160                                'fill': [ defaultValues[0], defaultValues[1], 'cover', 'no-repeat', 'fixed' ],
     5161                                'fit': [ defaultValues[0], defaultValues[1], 'contain', defaultValues[3], 'fixed' ],
     5162                                'repeat': [ defaultValues[0], defaultValues[1], 'auto', 'repeat', defaultValues[4] ]
    51925163                        };
    51935164
    5194                         // @todo These should actually toggle the active state, but without the preview overriding the state in data.activeControls.
    5195                         toggleVisibility = function( preset ) {
    5196                                 _.each( [ 'background_position', 'background_size', 'background_repeat', 'background_attachment' ], function( controlId, i ) {
    5197                                         var control = api.control( controlId );
    5198                                         if ( control ) {
    5199                                                 control.container.toggle( visibility[ preset ][ i ] );
    5200                                         }
    5201                                 } );
    5202                         };
    5203 
    52045165                        updateSettings = function( preset ) {
    52055166                                _.each( [ 'background_position_x', 'background_position_y', 'background_size', 'background_repeat', 'background_attachment' ], function( settingId, i ) {
    52065167                                        var setting = api( settingId );
     
    52115172                        };
    52125173
    52135174                        preset = control.setting.get();
    5214                         toggleVisibility( preset );
    52155175
     5176                        control.toggleControls = function( preset ) {
     5177                                api( 'background_image', function( setting ) {
     5178                                        var to = setting.get();
     5179
     5180                                        _.each( [ 'background_position', 'background_size', 'background_repeat', 'background_attachment' ], function( controlId, i ) {
     5181                                                var control = api.control( controlId );
     5182
     5183                                                if ( control ) {
     5184                                                        control.container.toggle( !! to ? visibility[ preset ][ i ] : false );
     5185                                                }
     5186                                        });
     5187                                });
     5188                        };
     5189
     5190                        control.toggleControls( preset );
     5191
    52165192                        control.setting.bind( 'change', function( preset ) {
    5217                                 toggleVisibility( preset );
     5193                                control.toggleControls( preset );
     5194
    52185195                                if ( 'custom' !== preset ) {
    52195196                                        updateSettings( preset );
    52205197                                }
     
    53585335                        } );
    53595336                } );
    53605337
     5338                // Control visibility for default controls
     5339                $.each({
     5340                        'background_image': {
     5341                                controls: [ 'background_preset' ],
     5342                                callback: function( to ) { return !! to; }
     5343                        },
     5344                        'show_on_front': {
     5345                                controls: [ 'page_on_front', 'page_for_posts' ],
     5346                                callback: function( to ) { return 'page' === to; }
     5347                        },
     5348                        'header_textcolor': {
     5349                                controls: [ 'header_textcolor' ],
     5350                                callback: function( to ) { return 'blank' !== to; }
     5351                        }
     5352                }, function( settingId, o ) {
     5353                        api( settingId, function( setting ) {
     5354                                $.each( o.controls, function( i, controlId ) {
     5355                                        api.control( controlId, function( control ) {
     5356                                                var visibility = function( to ) {
     5357                                                        if ( 'background_preset' === controlId ) {
     5358                                                                control.toggleControls( control.setting.get() );
     5359                                                        }
     5360
     5361                                                        control.container.toggle( o.callback( to ) );
     5362                                                };
     5363
     5364                                                visibility( setting.get() );
     5365
     5366                                                setting.bind( visibility );
     5367                                        });
     5368                                });
     5369                        });
     5370                });
     5371
    53615372                // Update the setting validities.
    53625373                api.previewer.bind( 'selective-refresh-setting-validities', function handleSelectiveRefreshedSettingValidities( settingValidities ) {
    53635374                        api._handleSettingValidities( {
  • wp-includes/class-wp-customize-manager.php

     
    39903990
    39913991                $this->add_control( new WP_Customize_Background_Image_Control( $this ) );
    39923992
     3993                $background_preset = get_theme_support( 'custom-background', 'default-preset' );
     3994
    39933995                $this->add_setting( 'background_preset', array(
    3994                         'default'        => get_theme_support( 'custom-background', 'default-preset' ),
     3996                        'default'        => $background_preset,
    39953997                        'theme_supports' => 'custom-background',
    39963998                        'sanitize_callback' => array( $this, '_sanitize_background_setting' ),
    39973999                ) );
     
    40314033                ) ) );
    40324034
    40334035                $this->add_setting( 'background_size', array(
    4034                         'default'        => get_theme_support( 'custom-background', 'default-size' ),
     4036                        'default'        => _sanitize_custom_background_prop( 'size', $background_preset ),
    40354037                        'theme_supports' => 'custom-background',
    40364038                        'sanitize_callback' => array( $this, '_sanitize_background_setting' ),
    40374039                ) );
     
    40484050                ) );
    40494051
    40504052                $this->add_setting( 'background_repeat', array(
    4051                         'default'           => get_theme_support( 'custom-background', 'default-repeat' ),
     4053                        'default'           => _sanitize_custom_background_prop( 'repeat', $background_preset ),
    40524054                        'sanitize_callback' => array( $this, '_sanitize_background_setting' ),
    40534055                        'theme_supports'    => 'custom-background',
    40544056                ) );
     
    40604062                ) );
    40614063
    40624064                $this->add_setting( 'background_attachment', array(
    4063                         'default'           => get_theme_support( 'custom-background', 'default-attachment' ),
     4065                        'default'           => _sanitize_custom_background_prop( 'attachment', $background_preset ),
    40644066                        'sanitize_callback' => array( $this, '_sanitize_background_setting' ),
    40654067                        'theme_supports'    => 'custom-background',
    40664068                ) );
  • wp-includes/theme.php

     
    15361536        echo get_background_color();
    15371537}
    15381538
     1539function _sanitize_custom_background_prop( $prop, $preset = 'default' ) {
     1540        if ( 'size' === $prop ) {
     1541                if ( 'fill' === $preset ) {
     1542                        return 'cover';
     1543                } elseif ( 'fit' === $preset ) {
     1544                        return 'contain';
     1545                } elseif ( 'repeat' === $preset ) {
     1546                        return 'auto';
     1547                } else {
     1548                        return get_theme_support( 'custom-background', 'default-size' );
     1549                }
     1550        } elseif ( 'repeat' === $prop ) {
     1551                if ( 'fill' === $preset ) {
     1552                        return 'no-repeat';
     1553                } elseif ( 'repeat' === $preset ) {
     1554                        return 'repeat';
     1555                } else {
     1556                        return get_theme_support( 'custom-background', 'default-repeat' );
     1557                }
     1558        } elseif ( 'attachment' === $prop ) {
     1559                if ( 'fill' === $preset || 'fit' === $preset ) {
     1560                        $attachment = 'fixed';
     1561                } else {
     1562                        $attachment = get_theme_support( 'custom-background', 'default-attachment' );
     1563                }
     1564        }
     1565
     1566        return false;
     1567}
     1568
    15391569/**
    15401570 * Default custom background callback.
    15411571 *
     
    15661596        if ( $background ) {
    15671597                $image = ' background-image: url("' . esc_url_raw( $background ) . '");';
    15681598
     1599                // Background Preset.
     1600                $preset = get_theme_mod( 'background_preset', get_theme_support( 'custom-background', 'default-preset' ) );
     1601
    15691602                // Background Position.
    15701603                $position_x = get_theme_mod( 'background_position_x', get_theme_support( 'custom-background', 'default-position-x' ) );
    15711604                $position_y = get_theme_mod( 'background_position_y', get_theme_support( 'custom-background', 'default-position-y' ) );
     
    15811614                $position = " background-position: $position_x $position_y;";
    15821615
    15831616                // Background Size.
    1584                 $size = get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) );
     1617                $size = get_theme_mod( 'background_size', _sanitize_custom_background_prop( 'size', $preset ) );
    15851618
    15861619                if ( ! in_array( $size, array( 'auto', 'contain', 'cover' ), true ) ) {
    15871620                        $size = 'auto';
     
    15901623                $size = " background-size: $size;";
    15911624
    15921625                // Background Repeat.
    1593                 $repeat = get_theme_mod( 'background_repeat', get_theme_support( 'custom-background', 'default-repeat' ) );
     1626                $repeat = get_theme_mod( 'background_repeat', _sanitize_custom_background_prop( 'repeat', $preset ) );
    15941627
    15951628                if ( ! in_array( $repeat, array( 'repeat-x', 'repeat-y', 'repeat', 'no-repeat' ), true ) ) {
    15961629                        $repeat = 'repeat';
     
    15991632                $repeat = " background-repeat: $repeat;";
    16001633
    16011634                // Background Scroll.
    1602                 $attachment = get_theme_mod( 'background_attachment', get_theme_support( 'custom-background', 'default-attachment' ) );
     1635                $attachment = get_theme_mod( 'background_attachment', _sanitize_custom_background_prop( 'attachment', $preset ) );
    16031636
    16041637                if ( 'fixed' !== $attachment ) {
    16051638                        $attachment = 'scroll';