Ticket #12186: custom-background.diff

File custom-background.diff, 11.6 KB (added by ptahdunbar, 3 years ago)

Prettified the custom background page.

  • wp-admin/custom-background.php

     
    99/** 
    1010 * The custom background image class. 
    1111 * 
    12  * @since unknown 
     12 * @since 3.0.0 
    1313 * @package WordPress 
    1414 * @subpackage Administration 
    1515 */ 
     
    1919         * Callback for administration header. 
    2020         * 
    2121         * @var callback 
    22          * @since unknown 
     22         * @since 3.0.0 
    2323         * @access private 
    2424         */ 
    2525        var $admin_header_callback; 
     
    2828         * Callback for header div. 
    2929         * 
    3030         * @var callback 
    31          * @since unknown 
     31         * @since 3.0.0 
    3232         * @access private 
    3333         */ 
    3434        var $admin_image_div_callback; 
     
    3636        /** 
    3737         * PHP4 Constructor - Register administration header callback. 
    3838         * 
    39          * @since unknown 
     39         * @since 3.0.0 
    4040         * @param callback $admin_header_callback 
    4141         * @param callback $admin_image_div_callback Optional custom image div output callback. 
    4242         * @return Custom_Background 
     
    4949        /** 
    5050         * Setup the hooks for the Custom Background admin page. 
    5151         * 
    52          * @since unknown 
     52         * @since 3.0.0 
    5353         */ 
    5454        function init() { 
    5555                if ( ! current_user_can('switch_themes') ) 
    5656                        return; 
    5757 
    58                 $page = add_theme_page(__('Custom Background'), __('Custom Background'), 'switch_themes', 'custom-background', array(&$this, 'admin_page')); 
     58                $page = add_theme_page( __( 'Custom Background' ), __( 'Custom Background' ), 'switch_themes', 'custom-background', array(&$this, 'admin_page') ); 
    5959 
    60                 add_action("admin_head-$page", array(&$this, 'take_action'), 50); 
     60                add_action( "admin_head-$page", array(&$this, 'take_action'), 50 ); 
    6161                if ( $this->admin_header_callback ) 
    62                         add_action("admin_head-$page", $this->admin_header_callback, 51); 
     62                        add_action( "admin_head-$page", $this->admin_header_callback, 51 ); 
    6363        } 
    6464 
    6565        /** 
    6666         * Get the current step. 
    6767         * 
    68          * @since unknown 
    69          * 
     68         * @since 3.0.0 
    7069         * @return int Current step 
    7170         */ 
    7271        function step() { 
     
    8382        /** 
    8483         * Execute custom background modification. 
    8584         * 
    86          * @since unknown 
     85         * @since 3.0.0 
    8786         */ 
    8887        function take_action() { 
    8988                if ( ! current_user_can('switch_themes') ) 
    9089                        return; 
    9190 
    9291                if ( isset($_POST['reset-background']) ) { 
    93                         check_admin_referer('custom-background'); 
     92                        check_admin_referer( 'custom-background' ); 
    9493                        remove_theme_mods(); 
    9594                } 
    9695                if ( isset($_POST['repeat-background']) ) { 
    97                         check_admin_referer('custom-background'); 
    98                         $repeat = $_POST['repeat-background'] ? true: false; 
    99                         set_theme_mod('background_repeat', $repeat); 
     96                        check_admin_referer( 'custom-background' ); 
     97                        $repeat = $_POST['repeat-background'] ? true : false; 
     98                        set_theme_mod( 'background_repeat', $repeat ); 
     99                         
    100100                } elseif ( isset($_POST['save-background-options']) ) { 
    101                         set_theme_mod('background_repeat', false); 
     101                        set_theme_mod( 'background_repeat', false ); 
    102102                } 
    103103                if ( isset($_POST['remove-background']) ) { 
    104                         check_admin_referer('custom-background'); 
    105                         set_theme_mod('background_image', ''); 
     104                        check_admin_referer( 'custom-background' ); 
     105                        set_theme_mod( 'background_image', '' ); 
    106106                } 
    107107        } 
    108108 
    109109        /** 
    110110         * Display first step of custom background image page. 
    111111         * 
    112          * @since unknown 
     112         * @since 3.0.0 
    113113         */ 
    114114        function step_1() { 
    115                 if ( isset($_GET['updated']) && $_GET['updated'] ) { ?> 
    116 <div id="message" class="updated"> 
    117 <p><?php printf(__('Background updated. <a href="%s">Visit your site</a> to see how it looks.'), home_url()); ?></p> 
    118 </div> 
    119                 <?php } ?> 
     115                echo '<div class="wrap" style="margin-bottom: 50px;">'; 
     116                        screen_icon(); 
     117                        echo '<h2>'. __( 'Custom Background' ) .'</h2>'; 
     118                        $this->feedback(); 
    120119 
    121 <div class="wrap"> 
    122 <?php screen_icon(); ?> 
    123 <h2><?php _e('Custom Background'); ?></h2> 
    124 <?php if ( get_background_image() ) { ?> 
    125 <p><?php _e('This is your current background image.'); ?></p> 
    126 <?php 
    127 } else { ?> 
    128 <p><?php _e('There is currently no background image.'); ?></p> <?php 
    129 } 
     120                        if ( get_background_image() ) 
     121                                echo '<h3>'. __( 'Current background image:' ) .'</h3>'; 
     122                         
     123                        if ( is_callable($this->admin_image_div_callback) ) 
     124                                call_user_func( $this->admin_image_div_callback ); 
     125                        else 
     126                                $this->background_image(); 
    130127 
    131 if ( $this->admin_image_div_callback ) { 
    132   call_user_func($this->admin_image_div_callback); 
    133 } else { 
    134 ?> 
    135 <div id="background-image"> 
    136 <img src="<?php background_image(); ?>" /> 
    137 </div> 
    138 <?php } ?> 
    139 </div> 
    140 <div class="wrap"> 
    141 <h2><?php _e('Upload New Background Image'); ?></h2><p><?php _e('Here you can upload a new background image.'); ?></p> 
     128                        if ( get_background_image() ) : 
     129                                $this->form_update_background(); 
     130                                $this->form_remove_background(); 
     131                        endif; 
    142132 
    143 <form enctype="multipart/form-data" id="uploadForm" method="POST" action="<?php echo esc_attr(add_query_arg('step', 2)) ?>" style="margin: auto; width: 50%;"> 
    144 <label for="upload"><?php _e('Choose an image from your computer:'); ?></label><br /><input type="file" id="upload" name="import" /> 
    145 <input type="hidden" name="action" value="save" /> 
    146 <?php wp_nonce_field('custom-background') ?> 
    147 <p class="submit"> 
    148 <input type="submit" value="<?php esc_attr_e('Upload'); ?>" /> 
    149 </p> 
    150 </form> 
    151  
    152 </div> 
    153  
    154 <div class="wrap"> 
    155  
    156 <h2><?php _e('Change Display Options') ?></h2> 
    157 <form method="post" action="<?php echo esc_attr(add_query_arg('step', 1)) ?>"> 
    158 <label for="repeat-background"> 
    159 <p><input name="repeat-background" type="checkbox" id="repeat-background" value="1" <?php checked(true, get_theme_mod('background_repeat')); ?> /> 
    160 <?php _e('Tile the background.') ?></label></p> 
    161 <?php wp_nonce_field('custom-background'); ?> 
    162 <input type="submit" class="button" name="save-background-options" value="<?php esc_attr_e('Save Changes'); ?>" /> 
    163 </form> 
    164 </div> 
    165  
    166 <?php if ( get_theme_mod('background_image') ) : ?> 
    167 <div class="wrap"> 
    168 <h2><?php _e('Reset Background Image'); ?></h2> 
    169 <p><?php _e('This will restore the original background image. You will not be able to retrieve any customizations.') ?></p> 
    170 <form method="post" action="<?php echo esc_attr(add_query_arg('step', 1)) ?>"> 
    171 <?php wp_nonce_field('custom-background'); ?> 
    172 <input type="submit" class="button" name="reset-background" value="<?php esc_attr_e('Restore Original Background'); ?>" /> 
    173 </form> 
    174 </div> 
    175 <?php endif; 
    176  
    177 if ( get_background_image() ) : 
    178 ?> 
    179 <div class="wrap"> 
    180 <h2><?php _e('Remove Background Image'); ?></h2> 
    181 <p><?php _e('This will remove background image. You will not be able to retrieve any customizations.') ?></p> 
    182 <form method="post" action="<?php echo esc_attr(add_query_arg('step', 1)) ?>"> 
    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 </div> 
    187 <?php endif; 
    188  
     133                        $this->form_upload_background(); 
     134                echo '</div>'; 
    189135        } 
    190136 
    191137        /** 
    192138         * Display second step of custom background image page. 
    193139         * 
    194          * @since unknown 
     140         * @since 3.0.0 
    195141         */ 
    196142        function step_2() { 
    197                 check_admin_referer('custom-background'); 
    198                 $overrides = array('test_form' => false); 
    199                 $file = wp_handle_upload($_FILES['import'], $overrides); 
     143                check_admin_referer( 'custom-background' ); 
     144                $overrides = array( 'test_form' => false ); 
     145                $file = wp_handle_upload( $_FILES['import'], $overrides ); 
    200146 
    201147                if ( isset($file['error']) ) 
    202                         die( $file['error'] ); 
     148                        wp_die( $file['error'] ); 
    203149 
    204150                $url = $file['url']; 
    205151                $type = $file['type']; 
    206152                $file = $file['file']; 
    207                 $filename = basename($file); 
     153                $filename = basename( $file ); 
    208154 
    209155                // Construct the object array 
    210156                $object = array( 
    211                 'post_title' => $filename, 
    212                 'post_content' => $url, 
    213                 'post_mime_type' => $type, 
    214                 'guid' => $url); 
     157                        'post_title' => $filename, 
     158                        'post_content' => $url, 
     159                        'post_mime_type' => $type, 
     160                        'guid' => $url, 
     161                ); 
    215162 
    216163                // Save the data 
    217                 $id = wp_insert_attachment($object, $file); 
     164                $id = wp_insert_attachment( $object, $file ); 
    218165 
    219166                // Add the meta-data 
    220167                wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) ); 
    221168 
    222                 set_theme_mod('background_image', esc_url($url)); 
    223                 do_action('wp_create_file_in_uploads', $file, $id); // For replication 
     169                set_theme_mod( 'background_image', esc_url($url) ); 
     170                do_action( 'wp_create_file_in_uploads', $file, $id ); // For replication 
     171                 
    224172                return $this->finished(); 
    225173        } 
    226174 
    227175        /** 
    228176         * Display last step of custom header image page. 
    229177         * 
    230          * @since unknown 
     178         * @since 3.0.0 
    231179         */ 
    232180        function finished() { 
    233181                $_GET['updated'] = 1; 
    234           $this->step_1(); 
     182                $this->step_1(); 
    235183        } 
    236184 
    237185        /** 
    238186         * Display the page based on the current step. 
    239187         * 
    240          * @since unknown 
     188         * @since 3.0.0 
    241189         */ 
    242190        function admin_page() { 
    243191                if ( ! current_user_can('switch_themes') ) 
    244                         wp_die(__('You do not have permission to customize the background.')); 
     192                        wp_die( __('You do not have permission to customize the background.') ); 
    245193                $step = $this->step(); 
    246194                if ( 1 == $step ) 
    247195                        $this->step_1(); 
    248196                elseif ( 2 == $step ) 
    249197                        $this->step_2(); 
    250198        } 
    251  
     199         
     200        /** 
     201         * Gives feedback to the user when they perform an action. 
     202         * 
     203         * @since 3.0.0 
     204         **/ 
     205        function feedback() {  
     206                if ( isset($_GET['updated']) && $_GET['updated'] ) : ?> 
     207                        <div id="message" class="updated"> 
     208                        <?php show_message( sprintf(__('Background image <strong>uploaded</strong>. <a href="%s">Visit your site</a> to see how it looks.'), home_url()) ); ?> 
     209                        </div> 
     210                <?php 
     211                endif; 
     212        } 
     213         
     214        /** 
     215         * Displays the custom background image. 
     216         * 
     217         * @since 3.0.0 
     218         **/ 
     219        function background_image() { 
     220                echo '<div id="background-image">'; 
     221                        echo '<img src="'. get_background_image() .'" width="100%" height="100%" />'; 
     222                echo '</div>'; 
     223        } 
     224         
     225        /** 
     226         * Displays the upload background image form. 
     227         * 
     228         * @since 3.0.0 
     229         **/ 
     230        function form_upload_background() { ?> 
     231                <h3><?php _e( 'Upload New Background Image' ); ?></h3> 
     232                <form enctype="multipart/form-data" id="uploadForm" method="post" action="<?php echo esc_attr(add_query_arg('step', 2)) ?>"> 
     233                        <?php wp_nonce_field( 'custom-background' ); ?> 
     234                        <input type="hidden" name="action" value="save" /> 
     235                         
     236                        <p><label for="upload"><?php _e( 'Choose an image from your computer:' ); ?></label></p> 
     237                        <input type="file" id="upload" name="import" /><input type="submit" class="button" value="<?php esc_attr_e( 'Upload' ); ?>" /> 
     238                         
     239                        <p> 
     240                                <label for="repeat-background"> 
     241                                        <input name="repeat-background" type="checkbox" id="repeat-background" value="1" <?php checked(true, get_theme_mod('background_repeat')); ?> /> 
     242                                        <?php _e( 'Tile the background image.' ) ?> 
     243                                </label> 
     244                        </p> 
     245                </form> 
     246                <?php 
     247        } 
     248         
     249        /** 
     250         * Displays the update background image form. 
     251         * 
     252         * @since 3.0.0 
     253         **/ 
     254        function form_update_background() { ?> 
     255                <h3><?php _e( 'Change Display Options' ) ?></h3> 
     256                <form method="post" action="<?php echo esc_attr(add_query_arg('step', 1)) ?>"> 
     257                        <?php wp_nonce_field( 'custom-background' ); ?> 
     258                         
     259                        <label for="repeat-background"> 
     260                        <p><input name="repeat-background" type="checkbox" id="repeat-background" value="1" <?php checked(true, get_theme_mod('background_repeat')); ?> /> 
     261                        <?php _e( 'Tile the background image.' ) ?> 
     262                        </p></label> 
     263                                                 
     264                        <input type="submit" class="button" name="save-background-options" value="<?php esc_attr_e( 'Save Changes' ); ?>" /> 
     265                </form> 
     266                <?php 
     267        } 
     268         
     269        /** 
     270         * Displays the remove background image form. 
     271         * 
     272         * @since 3.0.0 
     273         **/ 
     274        function form_remove_background() { ?> 
     275                <h3><?php _e( 'Remove Background Image' ); ?></h3> 
     276                <p><?php _e( 'This will remove background image. You <strong>will not</strong> be able to retrieve any customizations.' ); ?></p> 
     277                <form method="post" action="<?php echo esc_attr(add_query_arg('step', 1)) ?>"> 
     278                        <?php wp_nonce_field( 'custom-background' ); ?> 
     279                        <input type="submit" class="button" name="remove-background" value="<?php esc_attr_e( 'Remove Background' ); ?>" /> 
     280                </form> 
     281                <?php 
     282        } 
    252283} 
    253284?>