Ticket #12186: custom-background.diff
File custom-background.diff, 11.6 KB (added by , 15 years ago) |
---|
-
wp-admin/custom-background.php
9 9 /** 10 10 * The custom background image class. 11 11 * 12 * @since unknown12 * @since 3.0.0 13 13 * @package WordPress 14 14 * @subpackage Administration 15 15 */ … … 19 19 * Callback for administration header. 20 20 * 21 21 * @var callback 22 * @since unknown22 * @since 3.0.0 23 23 * @access private 24 24 */ 25 25 var $admin_header_callback; … … 28 28 * Callback for header div. 29 29 * 30 30 * @var callback 31 * @since unknown31 * @since 3.0.0 32 32 * @access private 33 33 */ 34 34 var $admin_image_div_callback; … … 36 36 /** 37 37 * PHP4 Constructor - Register administration header callback. 38 38 * 39 * @since unknown39 * @since 3.0.0 40 40 * @param callback $admin_header_callback 41 41 * @param callback $admin_image_div_callback Optional custom image div output callback. 42 42 * @return Custom_Background … … 49 49 /** 50 50 * Setup the hooks for the Custom Background admin page. 51 51 * 52 * @since unknown52 * @since 3.0.0 53 53 */ 54 54 function init() { 55 55 if ( ! current_user_can('switch_themes') ) 56 56 return; 57 57 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') ); 59 59 60 add_action( "admin_head-$page", array(&$this, 'take_action'), 50);60 add_action( "admin_head-$page", array(&$this, 'take_action'), 50 ); 61 61 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 ); 63 63 } 64 64 65 65 /** 66 66 * Get the current step. 67 67 * 68 * @since unknown 69 * 68 * @since 3.0.0 70 69 * @return int Current step 71 70 */ 72 71 function step() { … … 83 82 /** 84 83 * Execute custom background modification. 85 84 * 86 * @since unknown85 * @since 3.0.0 87 86 */ 88 87 function take_action() { 89 88 if ( ! current_user_can('switch_themes') ) 90 89 return; 91 90 92 91 if ( isset($_POST['reset-background']) ) { 93 check_admin_referer( 'custom-background');92 check_admin_referer( 'custom-background' ); 94 93 remove_theme_mods(); 95 94 } 96 95 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 100 100 } elseif ( isset($_POST['save-background-options']) ) { 101 set_theme_mod( 'background_repeat', false);101 set_theme_mod( 'background_repeat', false ); 102 102 } 103 103 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', '' ); 106 106 } 107 107 } 108 108 109 109 /** 110 110 * Display first step of custom background image page. 111 111 * 112 * @since unknown112 * @since 3.0.0 113 113 */ 114 114 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(); 120 119 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(); 130 127 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; 142 132 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>'; 189 135 } 190 136 191 137 /** 192 138 * Display second step of custom background image page. 193 139 * 194 * @since unknown140 * @since 3.0.0 195 141 */ 196 142 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 ); 200 146 201 147 if ( isset($file['error']) ) 202 die( $file['error'] );148 wp_die( $file['error'] ); 203 149 204 150 $url = $file['url']; 205 151 $type = $file['type']; 206 152 $file = $file['file']; 207 $filename = basename( $file);153 $filename = basename( $file ); 208 154 209 155 // Construct the object array 210 156 $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 ); 215 162 216 163 // Save the data 217 $id = wp_insert_attachment( $object, $file);164 $id = wp_insert_attachment( $object, $file ); 218 165 219 166 // Add the meta-data 220 167 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) ); 221 168 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 224 172 return $this->finished(); 225 173 } 226 174 227 175 /** 228 176 * Display last step of custom header image page. 229 177 * 230 * @since unknown178 * @since 3.0.0 231 179 */ 232 180 function finished() { 233 181 $_GET['updated'] = 1; 234 182 $this->step_1(); 235 183 } 236 184 237 185 /** 238 186 * Display the page based on the current step. 239 187 * 240 * @since unknown188 * @since 3.0.0 241 189 */ 242 190 function admin_page() { 243 191 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.') ); 245 193 $step = $this->step(); 246 194 if ( 1 == $step ) 247 195 $this->step_1(); 248 196 elseif ( 2 == $step ) 249 197 $this->step_2(); 250 198 } 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 } 252 283 } 253 284 ?>