WordPress.org

Make WordPress Core

Changeset 13788


Ignore:
Timestamp:
03/21/2010 06:06:18 AM (12 years ago)
Author:
dd32
Message:

Cleanup of Custom Background. Move JS to external file, Add @since, Remove redundant steps functionality, Preview of Image alignment. See #12186

Location:
trunk
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/custom-background.php

    r13765 r13788  
    11<?php
    22/**
    3  * The custom background image script.
     3 * The custom background script.
    44 *
    55 * @package WordPress
     
    88
    99/**
    10  * The custom background image class.
     10 * The custom background class.
    1111 *
    12  * @since unknown
     12 * @since 3.0
    1313 * @package WordPress
    1414 * @subpackage Administration
     
    2929     *
    3030     * @var callback
    31      * @since unknown
     31     * @since 3.0
    3232     * @access private
    3333     */
     
    3737     * PHP4 Constructor - Register administration header callback.
    3838     *
    39      * @since unknown
     39     * @since 3.0
    4040     * @param callback $admin_header_callback
    4141     * @param callback $admin_image_div_callback Optional custom image div output callback.
     
    5050     * Set up the hooks for the Custom Background admin page.
    5151     *
    52      * @since unknown
     52     * @since 3.0
    5353     */
    5454    function init() {
     
    5858        $page = add_theme_page(__('Background'), __('Background'), 'switch_themes', 'custom-background', array(&$this, 'admin_page'));
    5959
    60         add_action("admin_print_scripts-$page", array(&$this, 'js_includes'));
    61         add_action("admin_print_styles-$page", array(&$this, 'css_includes'));
    62         add_action("admin_head-$page", array(&$this, 'js'), 50);
    63         add_action("admin_head-$page", array(&$this, 'take_action'), 49);
     60        add_action("load-$page", array(&$this, 'admin_load'));
     61        add_action("load-$page", array(&$this, 'take_action'), 49);
     62        add_action("load-$page", array(&$this, 'handle_upload'), 49);
     63
    6464        if ( $this->admin_header_callback )
    6565            add_action("admin_head-$page", $this->admin_header_callback, 51);
     
    6767
    6868    /**
    69      * Get the current step.
    70      *
    71      * @since unknown
    72      *
    73      * @return int Current step
    74      */
    75     function step() {
    76         if ( ! isset( $_GET['step'] ) )
    77             return 1;
    78 
    79         $step = (int) $_GET['step'];
    80         if ( $step < 1 || 3 < $step )
    81             $step = 1;
    82 
    83         return $step;
    84     }
    85 
    86     /**
    87      * Set up the enqueue for the JavaScript files.
    88      *
    89      * @since unknown
    90      */
    91     function js_includes() {
    92         wp_enqueue_script('farbtastic');
    93     }
    94 
    95     /**
    96      * Set up the enqueue for the CSS files
    97      *
    98      * @since unknown
    99      */
    100     function css_includes() {
     69     * Set up the enqueue for the CSS & JavaScript files.
     70     *
     71     * @since 3.0
     72     */
     73    function admin_load() {
     74        wp_enqueue_script('custom-background');
    10175        wp_enqueue_style('farbtastic');
    10276    }
     
    10579     * Execute custom background modification.
    10680     *
    107      * @since unknown
     81     * @since 3.0
    10882     */
    10983    function take_action() {
    110         if ( ! current_user_can('switch_themes') )
    111             return;
    11284
    11385        if ( empty($_POST) )
     
    11688        check_admin_referer('custom-background');
    11789
    118         if ( isset($_POST['reset-background']) )
     90        // @TODO: No UI entry point for this:
     91        if ( isset($_POST['reset-background']) ) {
    11992            remove_theme_mods();
     93            return;
     94        }
     95        if ( isset($_POST['remove-background']) ) {
     96            // @TODO: Uploaded files are not removed here.
     97            set_theme_mod('background_image', '');
     98        }
     99
    120100        if ( isset($_POST['background-repeat']) ) {
    121101            if ( in_array($_POST['background-repeat'], array('repeat', 'no-repeat')) )
     
    139119            set_theme_mod('background_attachment', $attachment);
    140120        }
    141         if ( isset($_POST['remove-background']) )
    142             set_theme_mod('background_image', '');
    143         if ( isset( $_POST['background-color'] ) ) {
     121        if ( isset($_POST['background-color']) ) {
    144122            $color = preg_replace('/[^0-9a-fA-F]/', '', $_POST['background-color']);
    145123            if ( strlen($color) == 6 || strlen($color) == 3 )
     
    153131
    154132    /**
    155      * Execute Javascript depending on step.
    156      *
    157      * @since unknown
    158      */
    159     function js() {
    160         $this->js_1();
    161     }
    162 
    163     /**
    164      * Display Javascript based on Step 1.
    165      *
    166      * @since unknown
    167      */
    168     function js_1() { ?>
    169 <script type="text/javascript">
    170     var buttons = ['#pickcolor'],
    171         farbtastic;
    172 
    173     function pickColor(color) {
    174         jQuery('#background-color').val(color);
    175         farbtastic.setColor(color);
    176         jQuery('#custom-background-image').css('background-color', color);
    177     }
    178 
    179     jQuery(document).ready(function() {
    180         jQuery('#pickcolor').click(function() {
    181             jQuery('#colorPickerDiv').show();
    182         });
    183         jQuery('#background-color').keyup(function() {
    184             var _hex = jQuery('#background-color').val();
    185             var hex = _hex;
    186             if ( hex[0] != '#' )
    187                 hex = '#' + hex;
    188             hex = hex.replace(/[^#a-fA-F0-9]+/, '');
    189             if ( hex != _hex )
    190                 jQuery('#background-color').val(hex);
    191             if ( hex.length == 4 || hex.length == 7 )
    192                 pickColor( hex );
    193         });
    194 
    195         farbtastic = jQuery.farbtastic('#colorPickerDiv', function(color) { pickColor(color); });
    196         pickColor('#<?php background_color(); ?>');
    197     });
    198 
    199     jQuery(document).mousedown(function(){
    200         hide_picker(); // Make the picker disappear if you click outside its div element
    201     });
    202 
    203     function hide_picker(what) {
    204         var update = false;
    205         jQuery('#colorPickerDiv').each(function(){
    206             var id = jQuery(this).attr('id');
    207             if (id == what) {
    208                 return;
    209             }
    210             var display = jQuery(this).css('display');
    211             if (display == 'block') {
    212                 jQuery(this).fadeOut(2);
    213             }
    214         });
    215     }
    216 
    217 </script>
    218 <?php
    219     }
    220 
    221     /**
    222      * Display first step of custom background image page.
    223      *
    224      * @since unknown
    225      */
    226     function step_1() {
     133     * Display the custom background page.
     134     *
     135     * @since 3.0
     136     */
     137    function admin_page() {
    227138?>
    228139<div class="wrap" id="custom-background">
     
    240151    } else {
    241152        if ( $bgcolor = get_background_color() )
    242             $bgcolor = ' style="background-color: #' . $bgcolor . ';"';
    243         else
    244             $bgcolor = '';
     153            $bgcolor = 'background-color: #' . $bgcolor . ';';
     154
     155        if ( $align = get_theme_mod('background_position', 'left') )
     156            $align = "text-align: $align;";     
    245157?>
    246 <div id="custom-background-image"<?php echo $bgcolor; ?>>
     158<div id="custom-background-image"  style="<?php echo $bgcolor, $align ?>">
    247159<?php if ( get_background_image() ) { ?>
    248160<img class="custom-background-image" src="<?php background_image(); ?>" />
    249161<?php } ?>
     162<br class="clear" />
    250163</div>
    251164<?php } ?>
    252165<h3><?php _e('Change Display Options') ?></h3>
    253 <form method="post" action="<?php echo esc_attr(add_query_arg('step', 1)) ?>">
     166<form method="post" action="">
    254167<table>
    255168<thead>
     
    315228
    316229<h3><?php _e('Upload New Background Image'); ?></h3>
    317 <form enctype="multipart/form-data" id="uploadForm" method="POST" action="<?php echo esc_attr(add_query_arg('step', 2)) ?>">
     230<form enctype="multipart/form-data" id="uploadForm" method="POST" action="">
    318231<label for="upload"><?php _e('Choose an image from your computer:'); ?></label><br /><input type="file" id="upload" name="import" />
    319232<input type="hidden" name="action" value="save" />
     
    327240<h3><?php _e('Remove Background Image'); ?></h3>
    328241<p><?php _e('This will remove the background image. You will not be able to retrieve any customizations.') ?></p>
    329 <form method="post" action="<?php echo esc_attr(add_query_arg('step', 1)) ?>">
     242<form method="post" action="">
    330243<?php wp_nonce_field('custom-background'); ?>
    331244<input type="submit" class="button" name="remove-background" value="<?php esc_attr_e('Remove Background'); ?>" />
    332245</form>
    333 
    334246<?php endif; ?>
     247
    335248</div>
    336249<?php
     
    338251
    339252    /**
    340      * Display second step of custom background image page.
    341      *
    342      * @since unknown
    343      */
    344     function step_2() {
     253     * Handle a Image upload for the background image.
     254     *
     255     * @since 3.0
     256     */
     257    function handle_upload() {
     258
     259        if ( empty($_FILES) )
     260            return;
     261
    345262        check_admin_referer('custom-background');
    346263        $overrides = array('test_form' => false);
     
    372289        do_action('wp_create_file_in_uploads', $file, $id); // For replication
    373290        $this->updated = true;
    374         return $this->finished();
    375     }
    376 
    377     /**
    378      * Display last step of custom header image page.
    379      *
    380      * @since unknown
    381      */
    382     function finished() {
    383         $this->step_1();
    384     }
    385 
    386     /**
    387      * Display the page based on the current step.
    388      *
    389      * @since unknown
    390      */
    391     function admin_page() {
    392         if ( ! current_user_can('switch_themes') )
    393             wp_die(__('You do not have permission to customize the background.'));
    394         $step = $this->step();
    395         if ( 1 == $step )
    396             $this->step_1();
    397         elseif ( 2 == $step )
    398             $this->step_2();
    399291    }
    400292
  • trunk/wp-includes/script-loader.php

    r13766 r13788  
    407407            'warnDelete' => __( "You are about to permanently delete this menu. \n 'Cancel' to stop, 'OK' to delete." ),
    408408        ) );
     409
     410        $scripts->add( 'custom-background', "/wp-admin/js/custom-background$suffix.js", array('farbtastic'), '20100321' );
     411        $scripts->add_data( 'custom-background', 'group', 1 );
     412        // See wp_just_in_time_script_localization() for translation data for this object
     413
    409414    }
    410415}
     
    510515
    511516/**
    512  * Load localized script just in time for MCE.
     517 * Load localized data on print rather than initialization.
    513518 *
    514519 * These localizations require information that may not be loaded even by init.
     
    527532        'l10n_print_after' => 'try{convertEntities(autosaveL10n);}catch(e){};'
    528533    ) );
     534
     535    wp_localize_script( 'custom-background', 'customBackgroundL10n', array(
     536        'backgroundcolor' => '#' . get_background_color(),
     537    ) );
    529538}
    530539
Note: See TracChangeset for help on using the changeset viewer.