Make WordPress Core

Ticket #12186: cleanup_background.2.patch

File cleanup_background.2.patch, 18.2 KB (added by ocean90, 15 years ago)
  • wp-admin/custom-background.php

     
    4141         * @param callback $admin_image_div_callback Optional custom image div output callback.
    4242         * @return Custom_Background
    4343         */
    44         function Custom_Background($admin_header_callback = '', $admin_image_div_callback = '') {
     44        function Custom_Background( $admin_header_callback = '', $admin_image_div_callback = '' ) {
    4545                $this->admin_header_callback = $admin_header_callback;
    4646                $this->admin_image_div_callback = $admin_image_div_callback;
    4747        }
     
    5252         * @since 3.0.0
    5353         */
    5454        function init() {
    55                 if ( ! current_user_can('switch_themes') )
     55                if ( ! current_user_can( 'edit_theme_options' ) )
    5656                        return;
    5757
    58                 $page = add_theme_page(__('Background'), __('Background'), 'switch_themes', 'custom-background', array(&$this, 'admin_page'));
     58                $page = add_theme_page( __( 'Background' ), __( 'Background' ), 'edit_theme_options', 'custom-background', array( &$this, 'admin_page' ) );
    5959
    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);
     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 );
    6363
    6464                if ( $this->admin_header_callback )
    65                         add_action("admin_head-$page", $this->admin_header_callback, 51);
     65                        add_action( "admin_head-$page", $this->admin_header_callback, 51 );
    6666        }
    6767
    6868        /**
     
    7171         * @since 3.0.0
    7272         */
    7373        function admin_load() {
    74                 wp_enqueue_script('custom-background');
    75                 wp_enqueue_style('farbtastic');
     74                wp_enqueue_script( 'custom-background' );
     75                wp_enqueue_style( 'farbtastic' );
    7676        }
    7777
    7878        /**
     
    8282         */
    8383        function take_action() {
    8484
    85                 if ( empty($_POST) )
     85                if ( empty( $_POST ) )
    8686                        return;
    8787
    88                 check_admin_referer('custom-background');
     88                check_admin_referer( 'custom-background' );
    8989
    90                 if ( isset($_POST['reset-background']) ) {
    91                         remove_theme_mods();
     90                if ( isset( $_POST['reset-background'] ) ) {
     91                        remove_theme_mod( 'background_image' );
    9292                        return;
    9393                }
    94                 if ( isset($_POST['remove-background']) ) {
     94                if ( isset( $_POST['remove-background'] ) ) {
    9595                        // @TODO: Uploaded files are not removed here.
    96                         set_theme_mod('background_image', '');
     96                        set_theme_mod( 'background_image', '' );
    9797                }
    9898
    99                 if ( isset($_POST['background-repeat']) ) {
    100                         if ( in_array($_POST['background-repeat'], array('repeat', 'no-repeat', 'repeat-x', 'repeat-y')) )
     99                if ( isset( $_POST['background-repeat'] ) ) {
     100                        if ( in_array( $_POST['background-repeat'], array( 'repeat', 'no-repeat', 'repeat-x', 'repeat-y' ) ) )
    101101                                $repeat = $_POST['background-repeat'];
    102102                        else
    103103                                $repeat = 'repeat';
    104                         set_theme_mod('background_repeat', $repeat);
     104                        set_theme_mod( 'background_repeat', $repeat );
    105105                }
    106                 if ( isset($_POST['background-position']) ) {
    107                         if ( in_array($_POST['background-position'], array('center', 'right', 'left')) )
     106                if ( isset( $_POST['background-position'] ) ) {
     107                        if ( in_array( $_POST['background-position'], array( 'center', 'right', 'left' ) ) )
    108108                                $position = $_POST['background-position'];
    109109                        else
    110110                                $position = 'left';
    111                         set_theme_mod('background_position', $position);
     111                        set_theme_mod( 'background_position', $position );
    112112                }
    113                 if ( isset($_POST['background-attachment']) ) {
    114                         if ( in_array($_POST['background-attachment'], array('fixed', 'scroll')) )
     113                if ( isset( $_POST['background-attachment'] ) ) {
     114                        if ( in_array( $_POST['background-attachment'], array( 'fixed', 'scroll' ) ) )
    115115                                $attachment = $_POST['background-attachment'];
    116116                        else
    117117                                $attachment = 'fixed';
    118                         set_theme_mod('background_attachment', $attachment);
     118                        set_theme_mod( 'background_attachment', $attachment );
    119119                }
    120                 if ( isset($_POST['background-color']) ) {
    121                         $color = preg_replace('/[^0-9a-fA-F]/', '', $_POST['background-color']);
    122                         if ( strlen($color) == 6 || strlen($color) == 3 )
    123                                 set_theme_mod('background_color', $color);
     120                if ( isset( $_POST['background-color'] ) ) {
     121                        $color = preg_replace( '/[^0-9a-fA-F]/', '', $_POST['background-color'] );
     122                        if ( strlen( $color ) == 6 || strlen( $color ) == 3 )
     123                                set_theme_mod( 'background_color', $color );
    124124                        else
    125                                 set_theme_mod('background_color', '');
     125                                set_theme_mod( 'background_color', '' );
    126126                }
    127127
    128128                $this->updated = true;
     
    137137?>
    138138<div class="wrap" id="custom-background">
    139139<?php screen_icon(); ?>
    140 <h2><?php _e('Custom Background'); ?></h2>
    141 <?php if ( !empty($this->updated) ) { ?>
     140<h2><?php _e( 'Custom Background' ); ?></h2>
     141<?php if ( !empty( $this->updated ) ) { ?>
    142142<div id="message" class="updated">
    143 <p><?php printf( __( 'Background updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) ); ?></p>
     143        <p><?php printf( __( 'Background updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) ); ?></p>
    144144</div>
    145145<?php }
    146146
    147147        if ( $this->admin_image_div_callback ) {
    148                 call_user_func($this->admin_image_div_callback);
     148                call_user_func( $this->admin_image_div_callback );
    149149        } else {
    150150?>
    151 <h3><?php _e('Background Image'); ?></h3>
     151<h3><?php _e( 'Background Image' ); ?></h3>
    152152<table class="form-table">
    153153<tbody>
    154 <tr valign="top">
    155 <th scope="row"><?php _e('Current Image'); ?></th>
    156 <td>
    157 <style type="text/css">
    158 #custom-background-image {
    159         background-color: #<?php echo get_background_color()?>;
    160         <?php if ( get_background_image() ) { ?>
    161         background: url(<?php echo get_theme_mod('background_image_thumb', ''); ?>);
    162         background-repeat: <?php echo get_theme_mod('background_repeat', 'no-repeat'); ?>;
    163         background-position: top <?php echo get_theme_mod('background_position', 'left'); ?>;
    164         background-attachment: <?php echo get_theme_mod('background_position', 'fixed'); ?>;
    165         <?php } ?>
    166 }
    167 </style>
    168 <div id="custom-background-image">
    169 <?php if ( get_background_image() ) { ?>
    170 <img class="custom-background-image" src="<?php echo get_theme_mod('background_image_thumb', ''); ?>" style="visibility:hidden;" /><br />
    171 <img class="custom-background-image" src="<?php echo get_theme_mod('background_image_thumb', ''); ?>" style="visibility:hidden;" />
    172 <?php } ?>
    173 <br class="clear" />
    174 </div>
    175 <?php } ?>
    176 </td>
    177 </tr>
    178 <?php if ( get_background_image() ) : ?>
    179 <tr valign="top">
    180 <th scope="row"><?php _e('Remove Image'); ?></th>
    181 <td><p><?php _e('This will remove the background image. You will not be able to restore any customizations.') ?></p>
    182 <form method="post" action="">
    183 <?php wp_nonce_field('custom-background'); ?>
    184 <input type="submit" class="button" name="remove-background" value="<?php esc_attr_e('Remove Background'); ?>" />
    185 </form>
    186 </td>
    187 </tr>
    188 <?php endif; ?>
     154        <tr valign="top">
     155        <th scope="row"><?php _e( 'Current Image' ); ?></th>
     156        <td>
     157                <style type="text/css">
     158                #custom-background-image {
     159                        background-color: #<?php echo get_background_color()?>;
     160                        <?php if ( get_background_image() ) { ?>
     161                        background: url(<?php echo get_theme_mod( 'background_image_thumb', '' ); ?>);
     162                        background-repeat: <?php echo get_theme_mod( 'background_repeat', 'no-repeat' ); ?>;
     163                        background-position: top <?php echo get_theme_mod( 'background_position', 'left' ); ?>;
     164                        background-attachment: <?php echo get_theme_mod( 'background_position', 'fixed' ); ?>;
     165                        <?php } ?>
     166                }
     167                </style>
     168                <div id="custom-background-image">
     169                <?php if ( get_background_image() ) { ?>
     170                <img class="custom-background-image" src="<?php echo get_theme_mod( 'background_image_thumb', '' ); ?>" style="visibility:hidden;" /><br />
     171                <img class="custom-background-image" src="<?php echo get_theme_mod( 'background_image_thumb', '' ); ?>" style="visibility:hidden;" />
     172                <?php } ?>
     173                <br class="clear" />
     174                </div>
     175                <?php } ?>
     176        </td>
     177        </tr>
    189178
    190 <?php if ( defined( 'BACKGROUND_IMAGE' ) ) : // Show only if a default background image exists ?>
    191 <tr valign="top">
    192 <th scope="row"><?php _e('Restore Original Image'); ?></th>
    193 <td><p><?php _e('This will restore the original background image. You will not be able to restore any customizations.') ?></p>
    194 <form method="post" action="">
    195 <?php wp_nonce_field('custom-background'); ?>
    196 <input type="submit" class="button" name="reset-background" value="<?php esc_attr_e('Restore Original Image'); ?>" />
    197 </form>
    198 </td>
    199 </tr>
    200 </form>
    201 <?php endif; ?>
    202 <tr valign="top">
    203 <th scope="row"><?php _e('Upload Image'); ?></th>
    204 <td><form enctype="multipart/form-data" id="uploadForm" method="POST" action="">
    205 <label for="upload"><?php _e('Choose an image from your computer:'); ?></label><br /><input type="file" id="upload" name="import" />
    206 <input type="hidden" name="action" value="save" />
    207 <?php wp_nonce_field('custom-background') ?>
    208 <p class="submit">
    209 <input type="submit" value="<?php esc_attr_e('Upload'); ?>" />
    210 </p>
    211 </form>
    212 </td>
     179        <?php if ( get_background_image() ) : ?>
     180        <tr valign="top">
     181        <th scope="row"><?php _e( 'Remove Image' ); ?></th>
     182        <td>
     183                <p><?php _e( 'This will remove the background image. You will not be able to restore any customizations.' ) ?></p>
     184                <form method="post" action="">
     185                <?php wp_nonce_field( 'custom-background' ); ?>
     186                <input type="submit" class="button" name="remove-background" value="<?php esc_attr_e( 'Remove Background' ); ?>" />
     187                </form>
     188        </td>
     189        </tr>
     190        <?php endif; ?>
     191
     192        <?php if ( defined( 'BACKGROUND_IMAGE' ) ) : // Show only if a default background image exists ?>
     193        <tr valign="top">
     194        <th scope="row"><?php _e( 'Restore Original Image' ); ?></th>
     195        <td>
     196                <p><?php _e( 'This will restore the original background image. You will not be able to restore any customizations.' ) ?></p>
     197                <form method="post" action="">
     198                <?php wp_nonce_field( 'custom-background'); ?>
     199                <input type="submit" class="button" name="reset-background" value="<?php esc_attr_e( 'Restore Original Image' ); ?>" />
     200                </form>
     201        </td>
     202        </tr>
     203        <?php endif; ?>
     204
     205        <tr valign="top">
     206        <th scope="row"><?php _e( 'Upload Image' ); ?></th>
     207        <td>
     208                <form enctype="multipart/form-data" id="uploadForm" method="post" action="">
     209                <p>
     210                        <label for="upload"><?php _e('Choose an image from your computer:'); ?></label><br/>
     211                        <input type="file" id="upload" name="import" />
     212                        <input type="hidden" name="action" value="save" />
     213                        <?php wp_nonce_field( 'custom-background' ) ?>
     214                        <input type="submit" class="button" value="<?php esc_attr_e( 'Upload' ); ?>" />
     215                </p>
     216                </form>
     217        </td>
    213218</tbody>
    214219</table>
    215220
    216 <h3><?php _e('Display Options') ?></h3>
     221<h3><?php _e( 'Display Options' ) ?></h3>
    217222<form method="post" action="">
    218223<table class="form-table">
    219224<tbody>
    220 <tr valign="top">
    221 <th scope="row"><?php _e( 'Background Color' ); ?></th>
    222 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Color' ); ?></span></legend>
    223 <input type="text" name="background-color" id="background-color" value="#<?php echo esc_attr(get_background_color()) ?>" />
    224 <input type="button" class="button" value="<?php esc_attr_e('Select a Color'); ?>" id="pickcolor" />
     225        <tr valign="top">
     226        <th scope="row"><?php _e( 'Background Color' ); ?></th>
     227        <td>
     228                <fieldset>
     229                        <legend class="screen-reader-text"><span><?php _e( 'Background Color' ); ?></span></legend>
     230                        <input type="text" name="background-color" id="background-color" value="#<?php echo esc_attr( get_background_color() ) ?>" />
     231                        <input type="button" class="button" value="<?php esc_attr_e( 'Select a Color' ); ?>" id="pickcolor" />
    225232
    226 <div id="colorPickerDiv" style="z-index: 100; background:#eee; border:1px solid #ccc; position:absolute; display:none;"></div>
    227 </fieldset></td>
    228 </tr>
     233                        <div id="colorPickerDiv" style="z-index: 100; background:#eee; border:1px solid #ccc; position:absolute; display:none;"></div>
     234                </fieldset>
     235        </td>
     236        </tr>
    229237
    230 <tr valign="top">
    231 <th scope="row"><?php _e( 'Background Position' ); ?></th>
    232 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Position' ); ?></span></legend>
    233 <label>
    234 <input name="background-position" type="radio" value="left" <?php checked('left', get_theme_mod('background_position', 'left')); ?> />
    235 <?php _e('Left') ?>
    236 </label>
    237 <label>
    238 <input name="background-position" type="radio" value="center" <?php checked('center', get_theme_mod('background_position', 'left')); ?> />
    239 <?php _e('Center') ?>
    240 </label>
    241 <label>
    242 <input name="background-position" type="radio" value="right" <?php checked('right', get_theme_mod('background_position', 'left')); ?> />
    243 <?php _e('Right') ?>
    244 </label>
    245 </fieldset></td>
    246 </tr>
     238        <tr valign="top">
     239        <th scope="row"><?php _e( 'Background Position' ); ?></th>
     240        <td>
     241                <fieldset>
     242                        <legend class="screen-reader-text"><span><?php _e( 'Background Position' ); ?></span></legend>
     243                        <label>
     244                        <input name="background-position" type="radio" value="left" <?php checked( 'left', get_theme_mod( 'background_position', 'left' ) ); ?> />
     245                        <?php _e( 'Left' ) ?>
     246                        </label>
     247                        <label>
     248                        <input name="background-position" type="radio" value="center" <?php checked( 'center', get_theme_mod( 'background_position', 'left' ) ); ?> />
     249                        <?php _e( 'Center' ) ?>
     250                        </label>
     251                        <label>
     252                        <input name="background-position" type="radio" value="right" <?php checked( 'right', get_theme_mod( 'background_position', 'left' ) ); ?> />
     253                        <?php _e( 'Right' ) ?>
     254                        </label>
     255                </fieldset>
     256        </td>
     257        </tr>
    247258
    248 <tr valign="top">
    249 <th scope="row"><?php _e( 'Repeat' ); ?></th>
    250 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Repeat' ); ?></span></legend>
    251 <label>
    252 <select name="background-repeat">
    253         <option value="no-repeat" <?php selected('no-repeat', get_theme_mod('background_repeat', 'repeat')); ?> ><?php _e('No repeat'); ?></option>
    254         <option value="repeat" <?php selected('repeat', get_theme_mod('background_repeat', 'repeat')); ?>><?php _e('Tile'); ?></option>
    255         <option value="repeat-x" <?php selected('repeat-x', get_theme_mod('background_repeat', 'repeat')); ?>><?php _e('Tile Horizontally'); ?></option>
    256         <option value="repeat-y" <?php selected('repeat-y', get_theme_mod('background_repeat', 'repeat')); ?>><?php _e('Tile Vertically'); ?></option>
    257 </select>
    258 </label>
    259 </fieldset></td>
    260 </tr>
     259        <tr valign="top">
     260        <th scope="row"><?php _e( 'Repeat' ); ?></th>
     261        <td>
     262                <fieldset>
     263                        <legend class="screen-reader-text"><span><?php _e( 'Repeat' ); ?></span></legend>
     264                        <label>
     265                        <select name="background-repeat">
     266                                <option value="no-repeat" <?php selected( 'no-repeat', get_theme_mod( 'background_repeat', 'repeat' ) ); ?> ><?php _e( 'No repeat' ); ?></option>
     267                                <option value="repeat" <?php selected( 'repeat', get_theme_mod( 'background_repeat', 'repeat' ) ); ?>><?php _e( 'Tile' ); ?></option>
     268                                <option value="repeat-x" <?php selected( 'repeat-x', get_theme_mod( 'background_repeat', 'repeat' ) ); ?>><?php _e( 'Tile Horizontally' ); ?></option>
     269                                <option value="repeat-y" <?php selected( 'repeat-y', get_theme_mod( 'background_repeat', 'repeat' ) ); ?>><?php _e( 'Tile Vertically' ); ?></option>
     270                        </select>
     271                        </label>
     272                </fieldset>
     273        </td>
     274        </tr>
    261275
    262 <tr valign="top">
    263 <th scope="row"><?php _e( 'Attachment' ); ?></th>
    264 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Attachment' ); ?></span></legend>
    265 <label>
    266 <input name="background-attachment" type="radio" value="scroll" <?php checked('scroll', get_theme_mod('background_attachment', 'fixed')); ?> />
    267 <?php _e('Scroll') ?>
    268 </label>
    269 <label>
    270 <input name="background-attachment" type="radio" value="fixed" <?php checked('fixed', get_theme_mod('background_attachment', 'fixed')); ?> />
    271 <?php _e('Fixed') ?>
    272 </label>
    273 </fieldset></td>
    274 </tr>
    275 
     276        <tr valign="top">
     277        <th scope="row"><?php _e( 'Attachment' ); ?></th>
     278        <td>
     279                <fieldset>
     280                        <legend class="screen-reader-text"><span><?php _e( 'Attachment' ); ?></span></legend>
     281                        <label>
     282                        <input name="background-attachment" type="radio" value="scroll" <?php checked( 'scroll', get_theme_mod( 'background_attachment', 'fixed' ) ); ?> />
     283                        <?php _e( 'Scroll' ) ?>
     284                        </label>
     285                        <label>
     286                        <input name="background-attachment" type="radio" value="fixed" <?php checked( 'fixed', get_theme_mod( 'background_attachment', 'fixed' ) ); ?> />
     287                        <?php _e( 'Fixed' ) ?>
     288                        </label>
     289                </fieldset>
     290        </td>
     291        </tr>
    276292</tbody>
    277293</table>
    278294
    279 <?php wp_nonce_field('custom-background'); ?>
    280 <p class="submit"><input type="submit" class="button-primary" name="save-background-options" value="<?php esc_attr_e('Save Changes'); ?>" /></p>
     295<?php wp_nonce_field( 'custom-background' ); ?>
     296<p class="submit">
     297        <input type="submit" class="button-primary" name="save-background-options" value="<?php esc_attr_e( 'Save Changes' ); ?>" />
     298</p>
    281299</form>
    282300
    283301</div>
     
    291309         */
    292310        function handle_upload() {
    293311
    294                 if ( empty($_FILES) )
     312                if ( empty( $_FILES ) )
    295313                        return;
    296314
    297                 check_admin_referer('custom-background');
    298                 $overrides = array('test_form' => false);
    299                 $file = wp_handle_upload($_FILES['import'], $overrides);
     315                check_admin_referer( 'custom-background' );
     316                $overrides = array( 'test_form' => false );
     317                $file = wp_handle_upload( $_FILES['import'], $overrides );
    300318
    301                 if ( isset($file['error']) )
    302                         wp_die( $file['error'] );
     319                if ( isset( $file['error'] ) )
     320                        wp_die( $file['error'],  __( 'Image Upload Error' ) );
    303321
    304322                $url = $file['url'];
    305323                $type = $file['type'];
    306324                $file = $file['file'];
    307                 $filename = basename($file);
     325                $filename = basename( $file );
    308326
    309327                // Construct the object array
    310328                $object = array(
    311329                        'post_title' => $filename,
    312                         'post_content' => $url,
     330                        'post_content' => __( 'This is your background image, if you delete it, you will lost your background image.' ),
    313331                        'post_mime_type' => $type,
    314332                        'guid' => $url
    315333                );
    316334
    317335                // Save the data
    318                 $id = wp_insert_attachment($object, $file);
     336                $id = wp_insert_attachment( $object, $file );
    319337
    320338                // Add the meta-data
    321339                wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
    322340
    323                 set_theme_mod('background_image', esc_url($url));
     341                set_theme_mod( 'background_image', esc_url( $url ) );
    324342
    325343                $thumbnail = wp_get_attachment_image_src( $id, 'thumbnail' );
    326                 set_theme_mod('background_image_thumb', esc_url( $thumbnail[0] ) );
     344                set_theme_mod( 'background_image_thumb', esc_url( $thumbnail[0] ) );
    327345
    328                 do_action('wp_create_file_in_uploads', $file, $id); // For replication
     346                do_action( 'wp_create_file_in_uploads', $file, $id ); // For replication
    329347                $this->updated = true;
    330348        }
    331349