| [ Index ] |
PHP Cross Reference of WordPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * The custom background script. 4 * 5 * @package WordPress 6 * @subpackage Administration 7 */ 8 9 /** 10 * The custom background class. 11 * 12 * @since 3.0.0 13 * @package WordPress 14 * @subpackage Administration 15 */ 16 class Custom_Background { 17 18 /** 19 * Callback for administration header. 20 * 21 * @var callback 22 * @since 3.0.0 23 * @access private 24 */ 25 var $admin_header_callback; 26 27 /** 28 * Callback for header div. 29 * 30 * @var callback 31 * @since 3.0.0 32 * @access private 33 */ 34 var $admin_image_div_callback; 35 36 /** 37 * Holds the page menu hook. 38 * 39 * @var string 40 * @since 3.0.0 41 * @access private 42 */ 43 var $page = ''; 44 45 /** 46 * Constructor - Register administration header callback. 47 * 48 * @since 3.0.0 49 * @param callback $admin_header_callback 50 * @param callback $admin_image_div_callback Optional custom image div output callback. 51 * @return Custom_Background 52 */ 53 function __construct($admin_header_callback = '', $admin_image_div_callback = '') { 54 $this->admin_header_callback = $admin_header_callback; 55 $this->admin_image_div_callback = $admin_image_div_callback; 56 57 add_action( 'admin_menu', array( $this, 'init' ) ); 58 add_action( 'wp_ajax_set-background-image', array( $this, 'wp_set_background_image' ) ); 59 } 60 61 /** 62 * Set up the hooks for the Custom Background admin page. 63 * 64 * @since 3.0.0 65 */ 66 function init() { 67 if ( ! current_user_can('edit_theme_options') ) 68 return; 69 70 $this->page = $page = add_theme_page(__('Background'), __('Background'), 'edit_theme_options', 'custom-background', array(&$this, 'admin_page')); 71 72 add_action("load-$page", array(&$this, 'admin_load')); 73 add_action("load-$page", array(&$this, 'take_action'), 49); 74 add_action("load-$page", array(&$this, 'handle_upload'), 49); 75 add_filter( 'attachment_fields_to_edit', array( $this, 'attachment_fields_to_edit' ), 10, 2 ); 76 add_filter( 'media_upload_tabs', array( $this, 'filter_upload_tabs' ) ); 77 78 if ( $this->admin_header_callback ) 79 add_action("admin_head-$page", $this->admin_header_callback, 51); 80 } 81 82 /** 83 * Set up the enqueue for the CSS & JavaScript files. 84 * 85 * @since 3.0.0 86 */ 87 function admin_load() { 88 get_current_screen()->add_help_tab( array( 89 'id' => 'overview', 90 'title' => __('Overview'), 91 'content' => 92 '<p>' . __( 'You can customize the look of your site without touching any of your theme’s code by using a custom background. Your background can be an image or a color.' ) . '</p>' . 93 '<p>' . __( 'To use a background image, simply upload it, then choose your display options below. You can display a single instance of your image, or tile it to fill the screen. You can have your background fixed in place, so your site content moves on top of it, or you can have it scroll with your site.' ) . '</p>' . 94 '<p>' . __( 'You can also choose a background color. If you know the hexadecimal code for the color you want, enter it in the Background Color field. If not, click on the Select a Color link, and a color picker will allow you to choose the exact shade you want.' ) . '</p>' . 95 '<p>' . __( 'Don’t forget to click on the Save Changes button when you are finished.' ) . '</p>' 96 ) ); 97 98 get_current_screen()->set_help_sidebar( 99 '<p><strong>' . __( 'For more information:' ) . '</strong></p>' . 100 '<p>' . __( '<a href="http://codex.wordpress.org/Appearance_Background_Screen" target="_blank">Documentation on Custom Background</a>' ) . '</p>' . 101 '<p>' . __( '<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>' ) . '</p>' 102 ); 103 104 add_thickbox(); 105 wp_enqueue_script('media-upload'); 106 wp_enqueue_script('custom-background'); 107 wp_enqueue_style('farbtastic'); 108 } 109 110 /** 111 * Execute custom background modification. 112 * 113 * @since 3.0.0 114 */ 115 function take_action() { 116 117 if ( empty($_POST) ) 118 return; 119 120 if ( isset($_POST['reset-background']) ) { 121 check_admin_referer('custom-background-reset', '_wpnonce-custom-background-reset'); 122 remove_theme_mod('background_image'); 123 remove_theme_mod('background_image_thumb'); 124 $this->updated = true; 125 return; 126 } 127 128 if ( isset($_POST['remove-background']) ) { 129 // @TODO: Uploaded files are not removed here. 130 check_admin_referer('custom-background-remove', '_wpnonce-custom-background-remove'); 131 set_theme_mod('background_image', ''); 132 set_theme_mod('background_image_thumb', ''); 133 $this->updated = true; 134 wp_safe_redirect( $_POST['_wp_http_referer'] ); 135 return; 136 } 137 138 if ( isset($_POST['background-repeat']) ) { 139 check_admin_referer('custom-background'); 140 if ( in_array($_POST['background-repeat'], array('repeat', 'no-repeat', 'repeat-x', 'repeat-y')) ) 141 $repeat = $_POST['background-repeat']; 142 else 143 $repeat = 'repeat'; 144 set_theme_mod('background_repeat', $repeat); 145 } 146 147 if ( isset($_POST['background-position-x']) ) { 148 check_admin_referer('custom-background'); 149 if ( in_array($_POST['background-position-x'], array('center', 'right', 'left')) ) 150 $position = $_POST['background-position-x']; 151 else 152 $position = 'left'; 153 set_theme_mod('background_position_x', $position); 154 } 155 156 if ( isset($_POST['background-attachment']) ) { 157 check_admin_referer('custom-background'); 158 if ( in_array($_POST['background-attachment'], array('fixed', 'scroll')) ) 159 $attachment = $_POST['background-attachment']; 160 else 161 $attachment = 'fixed'; 162 set_theme_mod('background_attachment', $attachment); 163 } 164 165 if ( isset($_POST['background-color']) ) { 166 check_admin_referer('custom-background'); 167 $color = preg_replace('/[^0-9a-fA-F]/', '', $_POST['background-color']); 168 if ( strlen($color) == 6 || strlen($color) == 3 ) 169 set_theme_mod('background_color', $color); 170 else 171 set_theme_mod('background_color', ''); 172 } 173 174 $this->updated = true; 175 } 176 177 /** 178 * Display the custom background page. 179 * 180 * @since 3.0.0 181 */ 182 function admin_page() { 183 ?> 184 <div class="wrap" id="custom-background"> 185 <?php screen_icon(); ?> 186 <h2><?php _e('Custom Background'); ?></h2> 187 <?php if ( !empty($this->updated) ) { ?> 188 <div id="message" class="updated"> 189 <p><?php printf( __( 'Background updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) ); ?></p> 190 </div> 191 <?php } 192 193 if ( $this->admin_image_div_callback ) { 194 call_user_func($this->admin_image_div_callback); 195 } else { 196 ?> 197 <h3><?php _e('Background Image'); ?></h3> 198 <table class="form-table"> 199 <tbody> 200 <tr valign="top"> 201 <th scope="row"><?php _e('Preview'); ?></th> 202 <td> 203 <?php 204 $background_styles = ''; 205 if ( $bgcolor = get_background_color() ) 206 $background_styles .= 'background-color: #' . $bgcolor . ';'; 207 208 if ( get_background_image() ) { 209 // background-image URL must be single quote, see below 210 $background_styles .= ' background-image: url(\'' . set_url_scheme( get_theme_mod('background_image_thumb', '') ) . '\');' 211 . ' background-repeat: ' . get_theme_mod('background_repeat', 'repeat') . ';' 212 . ' background-position: top ' . get_theme_mod('background_position_x', 'left'); 213 } 214 ?> 215 <div id="custom-background-image" style="<?php echo $background_styles; ?>"><?php // must be double quote, see above ?> 216 <?php if ( get_background_image() ) { ?> 217 <img class="custom-background-image" src="<?php echo set_url_scheme( get_theme_mod('background_image_thumb', '') ); ?>" style="visibility:hidden;" alt="" /><br /> 218 <img class="custom-background-image" src="<?php echo set_url_scheme( get_theme_mod('background_image_thumb', '') ); ?>" style="visibility:hidden;" alt="" /> 219 <?php } ?> 220 </div> 221 <?php } ?> 222 </td> 223 </tr> 224 <?php if ( get_background_image() ) : ?> 225 <tr valign="top"> 226 <th scope="row"><?php _e('Remove Image'); ?></th> 227 <td> 228 <form method="post" action=""> 229 <?php wp_nonce_field('custom-background-remove', '_wpnonce-custom-background-remove'); ?> 230 <?php submit_button( __( 'Remove Background Image' ), 'button', 'remove-background', false ); ?><br/> 231 <?php _e('This will remove the background image. You will not be able to restore any customizations.') ?> 232 </form> 233 </td> 234 </tr> 235 <?php endif; ?> 236 237 <?php if ( get_theme_support( 'custom-background', 'default-image' ) ) : ?> 238 <tr valign="top"> 239 <th scope="row"><?php _e('Restore Original Image'); ?></th> 240 <td> 241 <form method="post" action=""> 242 <?php wp_nonce_field('custom-background-reset', '_wpnonce-custom-background-reset'); ?> 243 <?php submit_button( __( 'Restore Original Image' ), 'button', 'reset-background', false ); ?><br/> 244 <?php _e('This will restore the original background image. You will not be able to restore any customizations.') ?> 245 </form> 246 </td> 247 </tr> 248 249 <?php endif; ?> 250 <tr valign="top"> 251 <th scope="row"><?php _e('Select Image'); ?></th> 252 <td><form enctype="multipart/form-data" id="upload-form" method="post" action=""> 253 <p> 254 <label for="upload"><?php _e( 'Choose an image from your computer:' ); ?></label><br /> 255 <input type="file" id="upload" name="import" /> 256 <input type="hidden" name="action" value="save" /> 257 <?php wp_nonce_field( 'custom-background-upload', '_wpnonce-custom-background-upload' ); ?> 258 <?php submit_button( __( 'Upload' ), 'button', 'submit', false ); ?> 259 </p> 260 <?php 261 $image_library_url = get_upload_iframe_src( 'image', null, 'library' ); 262 $image_library_url = remove_query_arg( 'TB_iframe', $image_library_url ); 263 $image_library_url = add_query_arg( array( 'context' => 'custom-background', 'TB_iframe' => 1 ), $image_library_url ); 264 ?> 265 <p> 266 <label for="choose-from-library-link"><?php _e( 'Or choose an image from your media library:' ); ?></label><br /> 267 <a id="choose-from-library-link" class="button thickbox" href="<?php echo esc_url( $image_library_url ); ?>"><?php _e( 'Choose Image' ); ?></a> 268 </p> 269 </form> 270 </td> 271 </tr> 272 </tbody> 273 </table> 274 275 <h3><?php _e('Display Options') ?></h3> 276 <form method="post" action=""> 277 <table class="form-table"> 278 <tbody> 279 <?php if ( get_background_image() ) : ?> 280 <tr valign="top"> 281 <th scope="row"><?php _e( 'Position' ); ?></th> 282 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Position' ); ?></span></legend> 283 <label> 284 <input name="background-position-x" type="radio" value="left"<?php checked('left', get_theme_mod('background_position_x', 'left')); ?> /> 285 <?php _e('Left') ?> 286 </label> 287 <label> 288 <input name="background-position-x" type="radio" value="center"<?php checked('center', get_theme_mod('background_position_x', 'left')); ?> /> 289 <?php _e('Center') ?> 290 </label> 291 <label> 292 <input name="background-position-x" type="radio" value="right"<?php checked('right', get_theme_mod('background_position_x', 'left')); ?> /> 293 <?php _e('Right') ?> 294 </label> 295 </fieldset></td> 296 </tr> 297 298 <tr valign="top"> 299 <th scope="row"><?php _e( 'Repeat' ); ?></th> 300 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Repeat' ); ?></span></legend> 301 <label><input type="radio" name="background-repeat" value="no-repeat"<?php checked('no-repeat', get_theme_mod('background_repeat', 'repeat')); ?> /> <?php _e('No Repeat'); ?></label> 302 <label><input type="radio" name="background-repeat" value="repeat"<?php checked('repeat', get_theme_mod('background_repeat', 'repeat')); ?> /> <?php _e('Tile'); ?></label> 303 <label><input type="radio" name="background-repeat" value="repeat-x"<?php checked('repeat-x', get_theme_mod('background_repeat', 'repeat')); ?> /> <?php _e('Tile Horizontally'); ?></label> 304 <label><input type="radio" name="background-repeat" value="repeat-y"<?php checked('repeat-y', get_theme_mod('background_repeat', 'repeat')); ?> /> <?php _e('Tile Vertically'); ?></label> 305 </fieldset></td> 306 </tr> 307 308 <tr valign="top"> 309 <th scope="row"><?php _e( 'Attachment' ); ?></th> 310 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Attachment' ); ?></span></legend> 311 <label> 312 <input name="background-attachment" type="radio" value="scroll" <?php checked('scroll', get_theme_mod('background_attachment', 'scroll')); ?> /> 313 <?php _e('Scroll') ?> 314 </label> 315 <label> 316 <input name="background-attachment" type="radio" value="fixed" <?php checked('fixed', get_theme_mod('background_attachment', 'scroll')); ?> /> 317 <?php _e('Fixed') ?> 318 </label> 319 </fieldset></td> 320 </tr> 321 <?php endif; // get_background_image() ?> 322 <tr valign="top"> 323 <th scope="row"><?php _e( 'Background Color' ); ?></th> 324 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Color' ); ?></span></legend> 325 <?php $show_clear = get_background_color() ? '' : ' style="display:none"'; ?> 326 <input type="text" name="background-color" id="background-color" value="#<?php echo esc_attr(get_background_color()) ?>" /> 327 <a class="hide-if-no-js" href="#" id="pickcolor"><?php _e('Select a Color'); ?></a> <span<?php echo $show_clear; ?> class="hide-if-no-js" id="clearcolor"> (<a href="#"><?php _e( 'Clear' ); ?></a>)</span> 328 <div id="colorPickerDiv" style="z-index: 100; background:#eee; border:1px solid #ccc; position:absolute; display:none;"></div> 329 </fieldset></td> 330 </tr> 331 </tbody> 332 </table> 333 334 <?php wp_nonce_field('custom-background'); ?> 335 <?php submit_button( null, 'primary', 'save-background-options' ); ?> 336 </form> 337 338 </div> 339 <?php 340 } 341 342 /** 343 * Handle an Image upload for the background image. 344 * 345 * @since 3.0.0 346 */ 347 function handle_upload() { 348 349 if ( empty($_FILES) ) 350 return; 351 352 check_admin_referer('custom-background-upload', '_wpnonce-custom-background-upload'); 353 $overrides = array('test_form' => false); 354 $file = wp_handle_upload($_FILES['import'], $overrides); 355 356 if ( isset($file['error']) ) 357 wp_die( $file['error'] ); 358 359 $url = $file['url']; 360 $type = $file['type']; 361 $file = $file['file']; 362 $filename = basename($file); 363 364 // Construct the object array 365 $object = array( 366 'post_title' => $filename, 367 'post_content' => $url, 368 'post_mime_type' => $type, 369 'guid' => $url, 370 'context' => 'custom-background' 371 ); 372 373 // Save the data 374 $id = wp_insert_attachment($object, $file); 375 376 // Add the meta-data 377 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) ); 378 update_post_meta( $id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) ); 379 380 set_theme_mod('background_image', esc_url($url)); 381 382 $thumbnail = wp_get_attachment_image_src( $id, 'thumbnail' ); 383 set_theme_mod('background_image_thumb', esc_url( $thumbnail[0] ) ); 384 385 do_action('wp_create_file_in_uploads', $file, $id); // For replication 386 $this->updated = true; 387 } 388 389 function attachment_fields_to_edit( $form_fields, $post ) { 390 if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] == 'custom-background' ) { 391 $form_fields = array( 'image-size' => $form_fields['image-size'] ); 392 $form_fields['buttons'] = array( 'tr' => '<tr class="submit"><td></td><td><a data-attachment-id="' . $post->ID . '" class="wp-set-background">' . __( 'Set as background' ) . '</a></td></tr>' ); 393 $form_fields['context'] = array( 'input' => 'hidden', 'value' => 'custom-background' ); 394 } 395 396 return $form_fields; 397 } 398 399 function filter_upload_tabs ( $tabs ){ 400 if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] == 'custom-background' ) 401 return array( 'library' => __('Media Library') ); 402 403 return $tabs; 404 } 405 406 public function wp_set_background_image() { 407 if ( ! current_user_can('edit_theme_options') || ! isset( $_POST['attachment_id'] ) ) exit; 408 $attachment_id = absint($_POST['attachment_id']); 409 $sizes = array_keys(apply_filters( 'image_size_names_choose', array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'), 'full' => __('Full Size')) )); 410 $size = 'thumbnail'; 411 if ( in_array( $_POST['size'], $sizes ) ) 412 $size = esc_attr( $_POST['size'] ); 413 414 update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) ); 415 $url = wp_get_attachment_image_src( $attachment_id, $size ); 416 $thumbnail = wp_get_attachment_image_src( $attachment_id, 'thumbnail' ); 417 set_theme_mod( 'background_image', esc_url( $url[0] ) ); 418 set_theme_mod( 'background_image_thumb', esc_url( $thumbnail[0] ) ); 419 exit; 420 } 421 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Fri May 25 03:56:23 2012 | Hosted by follow the white rabbit. |