| [ XREF Home ] [ Index ] |
PHP Cross Reference of WordPress TrunkProvided by Yoast |
[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 58 /** 59 * Set up the hooks for the Custom Background admin page. 60 * 61 * @since 3.0.0 62 */ 63 function init() { 64 if ( ! current_user_can('edit_theme_options') ) 65 return; 66 67 $this->page = $page = add_theme_page(__('Background'), __('Background'), 'edit_theme_options', 'custom-background', array(&$this, 'admin_page')); 68 69 add_action("load-$page", array(&$this, 'admin_load')); 70 add_action("load-$page", array(&$this, 'take_action'), 49); 71 add_action("load-$page", array(&$this, 'handle_upload'), 49); 72 73 if ( $this->admin_header_callback ) 74 add_action("admin_head-$page", $this->admin_header_callback, 51); 75 } 76 77 /** 78 * Set up the enqueue for the CSS & JavaScript files. 79 * 80 * @since 3.0.0 81 */ 82 function admin_load() { 83 add_contextual_help( $this->page, '<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>' . 84 '<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>' . 85 '<p>' . __( 'You can also choose a background color. If you know the hexadecimal code for the color you want, enter it in the 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>' . 86 '<p>' . __( 'Don’t forget to click on the Save Changes button when you are finished.' ) . '</p>' . 87 '<p><strong>' . __( 'For more information:' ) . '</strong></p>' . 88 '<p>' . __( '<a href="http://codex.wordpress.org/Appearance_Background_Screen" target="_blank">Documentation on Custom Background</a>' ) . '</p>' . 89 '<p>' . __( '<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>' ) . '</p>' ); 90 wp_enqueue_script('custom-background'); 91 wp_enqueue_style('farbtastic'); 92 } 93 94 /** 95 * Execute custom background modification. 96 * 97 * @since 3.0.0 98 */ 99 function take_action() { 100 101 if ( empty($_POST) ) 102 return; 103 104 if ( isset($_POST['reset-background']) ) { 105 check_admin_referer('custom-background-reset', '_wpnonce-custom-background-reset'); 106 remove_theme_mod('background_image'); 107 remove_theme_mod('background_image_thumb'); 108 $this->updated = true; 109 return; 110 } 111 112 if ( isset($_POST['remove-background']) ) { 113 // @TODO: Uploaded files are not removed here. 114 check_admin_referer('custom-background-remove', '_wpnonce-custom-background-remove'); 115 set_theme_mod('background_image', ''); 116 set_theme_mod('background_image_thumb', ''); 117 $this->updated = true; 118 return; 119 } 120 121 if ( isset($_POST['background-repeat']) ) { 122 check_admin_referer('custom-background'); 123 if ( in_array($_POST['background-repeat'], array('repeat', 'no-repeat', 'repeat-x', 'repeat-y')) ) 124 $repeat = $_POST['background-repeat']; 125 else 126 $repeat = 'repeat'; 127 set_theme_mod('background_repeat', $repeat); 128 } 129 130 if ( isset($_POST['background-position-x']) ) { 131 check_admin_referer('custom-background'); 132 if ( in_array($_POST['background-position-x'], array('center', 'right', 'left')) ) 133 $position = $_POST['background-position-x']; 134 else 135 $position = 'left'; 136 set_theme_mod('background_position_x', $position); 137 } 138 139 if ( isset($_POST['background-attachment']) ) { 140 check_admin_referer('custom-background'); 141 if ( in_array($_POST['background-attachment'], array('fixed', 'scroll')) ) 142 $attachment = $_POST['background-attachment']; 143 else 144 $attachment = 'fixed'; 145 set_theme_mod('background_attachment', $attachment); 146 } 147 148 if ( isset($_POST['background-color']) ) { 149 check_admin_referer('custom-background'); 150 $color = preg_replace('/[^0-9a-fA-F]/', '', $_POST['background-color']); 151 if ( strlen($color) == 6 || strlen($color) == 3 ) 152 set_theme_mod('background_color', $color); 153 else 154 set_theme_mod('background_color', ''); 155 } 156 157 $this->updated = true; 158 } 159 160 /** 161 * Display the custom background page. 162 * 163 * @since 3.0.0 164 */ 165 function admin_page() { 166 ?> 167 <div class="wrap" id="custom-background"> 168 <?php screen_icon(); ?> 169 <h2><?php _e('Custom Background'); ?></h2> 170 <?php if ( !empty($this->updated) ) { ?> 171 <div id="message" class="updated"> 172 <p><?php printf( __( 'Background updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) ); ?></p> 173 </div> 174 <?php } 175 176 if ( $this->admin_image_div_callback ) { 177 call_user_func($this->admin_image_div_callback); 178 } else { 179 ?> 180 <h3><?php _e('Background Image'); ?></h3> 181 <table class="form-table"> 182 <tbody> 183 <tr valign="top"> 184 <th scope="row"><?php _e('Preview'); ?></th> 185 <td> 186 <?php 187 $background_styles = ''; 188 if ( $bgcolor = get_background_color() ) 189 $background_styles .= 'background-color: #' . $bgcolor . ';'; 190 191 if ( get_background_image() ) { 192 // background-image URL must be single quote, see below 193 $background_styles .= ' background-image: url(\'' . get_theme_mod('background_image_thumb', '') . '\');' 194 . ' background-repeat: ' . get_theme_mod('background_repeat', 'repeat') . ';' 195 . ' background-position: top ' . get_theme_mod('background_position_x', 'left'); 196 } 197 ?> 198 <div id="custom-background-image" style="<?php echo $background_styles; ?>"><?php // must be double quote, see above ?> 199 <?php if ( get_background_image() ) { ?> 200 <img class="custom-background-image" src="<?php echo get_theme_mod('background_image_thumb', ''); ?>" style="visibility:hidden;" alt="" /><br /> 201 <img class="custom-background-image" src="<?php echo get_theme_mod('background_image_thumb', ''); ?>" style="visibility:hidden;" alt="" /> 202 <?php } ?> 203 </div> 204 <?php } ?> 205 </td> 206 </tr> 207 <?php if ( get_background_image() ) : ?> 208 <tr valign="top"> 209 <th scope="row"><?php _e('Remove Image'); ?></th> 210 <td> 211 <form method="post" action=""> 212 <?php wp_nonce_field('custom-background-remove', '_wpnonce-custom-background-remove'); ?> 213 <?php submit_button( __( 'Remove Background Image' ), 'button', 'remove-background', false ); ?><br/> 214 <?php _e('This will remove the background image. You will not be able to restore any customizations.') ?> 215 </form> 216 </td> 217 </tr> 218 <?php endif; ?> 219 220 <?php if ( defined( 'BACKGROUND_IMAGE' ) ) : // Show only if a default background image exists ?> 221 <tr valign="top"> 222 <th scope="row"><?php _e('Restore Original Image'); ?></th> 223 <td> 224 <form method="post" action=""> 225 <?php wp_nonce_field('custom-background-reset', '_wpnonce-custom-background-reset'); ?> 226 <?php submit_button( __( 'Restore Original Image' ), 'button', 'reset-background', false ); ?><br/> 227 <?php _e('This will restore the original background image. You will not be able to restore any customizations.') ?> 228 </form> 229 </td> 230 </tr> 231 232 <?php endif; ?> 233 <tr valign="top"> 234 <th scope="row"><?php _e('Upload Image'); ?></th> 235 <td><form enctype="multipart/form-data" id="upload-form" method="post" action=""> 236 <label for="upload"><?php _e('Choose an image from your computer:'); ?></label><br /><input type="file" id="upload" name="import" /> 237 <input type="hidden" name="action" value="save" /> 238 <?php wp_nonce_field('custom-background-upload', '_wpnonce-custom-background-upload') ?> 239 <?php submit_button( __( 'Upload' ), 'button', 'submit', false ); ?> 240 </form> 241 </td> 242 </tr> 243 </tbody> 244 </table> 245 246 <h3><?php _e('Display Options') ?></h3> 247 <form method="post" action=""> 248 <table class="form-table"> 249 <tbody> 250 <?php if ( get_background_image() ) : ?> 251 <tr valign="top"> 252 <th scope="row"><?php _e( 'Position' ); ?></th> 253 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Position' ); ?></span></legend> 254 <label> 255 <input name="background-position-x" type="radio" value="left"<?php checked('left', get_theme_mod('background_position_x', 'left')); ?> /> 256 <?php _e('Left') ?> 257 </label> 258 <label> 259 <input name="background-position-x" type="radio" value="center"<?php checked('center', get_theme_mod('background_position_x', 'left')); ?> /> 260 <?php _e('Center') ?> 261 </label> 262 <label> 263 <input name="background-position-x" type="radio" value="right"<?php checked('right', get_theme_mod('background_position_x', 'left')); ?> /> 264 <?php _e('Right') ?> 265 </label> 266 </fieldset></td> 267 </tr> 268 269 <tr valign="top"> 270 <th scope="row"><?php _e( 'Repeat' ); ?></th> 271 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Repeat' ); ?></span></legend> 272 <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> 273 <label><input type="radio" name="background-repeat" value="repeat"<?php checked('repeat', get_theme_mod('background_repeat', 'repeat')); ?> /> <?php _e('Tile'); ?></label> 274 <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> 275 <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> 276 </fieldset></td> 277 </tr> 278 279 <tr valign="top"> 280 <th scope="row"><?php _e( 'Attachment' ); ?></th> 281 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Attachment' ); ?></span></legend> 282 <label> 283 <input name="background-attachment" type="radio" value="scroll" <?php checked('scroll', get_theme_mod('background_attachment', 'scroll')); ?> /> 284 <?php _e('Scroll') ?> 285 </label> 286 <label> 287 <input name="background-attachment" type="radio" value="fixed" <?php checked('fixed', get_theme_mod('background_attachment', 'scroll')); ?> /> 288 <?php _e('Fixed') ?> 289 </label> 290 </fieldset></td> 291 </tr> 292 <?php endif; // get_background_image() ?> 293 <tr valign="top"> 294 <th scope="row"><?php _e( 'Color' ); ?></th> 295 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Color' ); ?></span></legend> 296 <?php $show_clear = get_background_color() ? '' : ' style="display:none"'; ?> 297 <input type="text" name="background-color" id="background-color" value="#<?php echo esc_attr(get_background_color()) ?>" /> 298 <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> 299 <div id="colorPickerDiv" style="z-index: 100; background:#eee; border:1px solid #ccc; position:absolute; display:none;"></div> 300 </fieldset></td> 301 </tr> 302 </tbody> 303 </table> 304 305 <?php wp_nonce_field('custom-background'); ?> 306 <?php submit_button( null, 'primary', 'save-background-options' ); ?> 307 </form> 308 309 </div> 310 <?php 311 } 312 313 /** 314 * Handle an Image upload for the background image. 315 * 316 * @since 3.0.0 317 */ 318 function handle_upload() { 319 320 if ( empty($_FILES) ) 321 return; 322 323 check_admin_referer('custom-background-upload', '_wpnonce-custom-background-upload'); 324 $overrides = array('test_form' => false); 325 $file = wp_handle_upload($_FILES['import'], $overrides); 326 327 if ( isset($file['error']) ) 328 wp_die( $file['error'] ); 329 330 $url = $file['url']; 331 $type = $file['type']; 332 $file = $file['file']; 333 $filename = basename($file); 334 335 // Construct the object array 336 $object = array( 337 'post_title' => $filename, 338 'post_content' => $url, 339 'post_mime_type' => $type, 340 'guid' => $url, 341 'context' => 'custom-background' 342 ); 343 344 // Save the data 345 $id = wp_insert_attachment($object, $file); 346 347 // Add the meta-data 348 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) ); 349 update_post_meta( $id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) ); 350 351 set_theme_mod('background_image', esc_url($url)); 352 353 $thumbnail = wp_get_attachment_image_src( $id, 'thumbnail' ); 354 set_theme_mod('background_image_thumb', esc_url( $thumbnail[0] ) ); 355 356 do_action('wp_create_file_in_uploads', $file, $id); // For replication 357 $this->updated = true; 358 } 359 360 } 361 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Wed Jun 1 08:30:02 2011 |
Cross-referenced by PHPXref 0.7 Provided by Yoast and awesome WordPress Hosting |