[ Index ]

PHP Cross Reference of WordPress 3.0 beta 1

[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/wp-admin/ -> custom-background.php (source)

   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 unknown
  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       * PHP4 Constructor - Register administration header callback.
  38       *
  39       * @since 3.0.0
  40       * @param callback $admin_header_callback
  41       * @param callback $admin_image_div_callback Optional custom image div output callback.
  42       * @return Custom_Background
  43       */
  44  	function Custom_Background($admin_header_callback = '', $admin_image_div_callback = '') {
  45          $this->admin_header_callback = $admin_header_callback;
  46          $this->admin_image_div_callback = $admin_image_div_callback;
  47      }
  48  
  49      /**
  50       * Set up the hooks for the Custom Background admin page.
  51       *
  52       * @since 3.0.0
  53       */
  54  	function init() {
  55          if ( ! current_user_can('switch_themes') )
  56              return;
  57  
  58          $page = add_theme_page(__('Background'), __('Background'), 'switch_themes', 'custom-background', array(&$this, 'admin_page'));
  59  
  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);
  63  
  64          if ( $this->admin_header_callback )
  65              add_action("admin_head-$page", $this->admin_header_callback, 51);
  66      }
  67  
  68      /**
  69       * Set up the enqueue for the CSS & JavaScript files.
  70       *
  71       * @since 3.0.0
  72       */
  73  	function admin_load() {
  74          wp_enqueue_script('custom-background');
  75          wp_enqueue_style('farbtastic');
  76      }
  77  
  78      /**
  79       * Execute custom background modification.
  80       *
  81       * @since 3.0.0
  82       */
  83  	function take_action() {
  84  
  85          if ( empty($_POST) )
  86              return;
  87  
  88          check_admin_referer('custom-background');
  89  
  90          // @TODO: No UI entry point for this:
  91          if ( isset($_POST['reset-background']) ) {
  92              remove_theme_mods();
  93              return;
  94          }
  95          if ( isset($_POST['remove-background']) ) {
  96              // @TODO: Uploaded files are not removed here.
  97              set_theme_mod('background_image', '');
  98          }
  99  
 100          if ( isset($_POST['background-repeat']) ) {
 101              if ( in_array($_POST['background-repeat'], array('repeat', 'no-repeat', 'repeat-x', 'repeat-y')) )
 102                  $repeat = $_POST['background-repeat'];
 103              else
 104                  $repeat = 'repeat';
 105              set_theme_mod('background_repeat', $repeat);
 106          }
 107          if ( isset($_POST['background-position']) ) {
 108              if ( in_array($_POST['background-position'], array('center', 'right', 'left')) )
 109                  $position = $_POST['background-position'];
 110              else
 111                  $position = 'left';
 112              set_theme_mod('background_position', $position);
 113          }
 114          if ( isset($_POST['background-attachment']) ) {
 115              if ( in_array($_POST['background-attachment'], array('fixed', 'scroll')) )
 116                  $attachment = $_POST['background-attachment'];
 117              else
 118                  $attachment = 'fixed';
 119              set_theme_mod('background_attachment', $attachment);
 120          }
 121          if ( isset($_POST['background-color']) ) {
 122              $color = preg_replace('/[^0-9a-fA-F]/', '', $_POST['background-color']);
 123              if ( strlen($color) == 6 || strlen($color) == 3 )
 124                  set_theme_mod('background_color', $color);
 125              else
 126                  set_theme_mod('background_color', '');
 127          }
 128  
 129          $this->updated = true;
 130      }
 131  
 132      /**
 133       * Display the custom background page.
 134       *
 135       * @since 3.0.0
 136       */
 137  	function admin_page() {
 138  ?>
 139  <div class="wrap" id="custom-background">
 140  <?php screen_icon(); ?>
 141  <h2><?php _e('Custom Background'); ?></h2>
 142  <?php if ( !empty($this->updated) ) { ?>
 143  <div id="message" class="updated">
 144  <p><?php printf(__('Background updated. <a href="%s">Visit your site</a> to see how it looks.'), home_url()); ?></p>
 145  </div>
 146  <?php } ?>
 147  <p><?php _e('This is your current background.'); ?></p>
 148  <?php
 149      if ( $this->admin_image_div_callback ) {
 150          call_user_func($this->admin_image_div_callback);
 151      } else {
 152  ?>
 153  
 154  <style type="text/css"> 
 155  #custom-background-image {
 156      background-color: #<?php echo get_background_color()?>;
 157      <?php if ( get_background_image() ) { ?>
 158      background: url(<?php echo get_theme_mod('background_image_thumb', ''); ?>);
 159      background-repeat: <?php echo get_theme_mod('background_repeat', 'no-repeat'); ?>;
 160      background-position: top <?php echo get_theme_mod('background_position', 'left'); ?>;
 161      background-attachment: <?php echo get_theme_mod('background_position', 'fixed'); ?>;
 162      <?php } ?>
 163  }
 164  </style> 
 165  <div id="custom-background-image">
 166  <?php if ( get_background_image() ) { ?>
 167  <img class="custom-background-image" src="<?php echo get_theme_mod('background_image_thumb', ''); ?>" style="visibility:hidden;" /><br />
 168  <img class="custom-background-image" src="<?php echo get_theme_mod('background_image_thumb', ''); ?>" style="visibility:hidden;" />
 169  <?php } ?>
 170  <br class="clear" />
 171  </div>
 172  <?php } ?>
 173  <h3><?php _e('Change Display Options') ?></h3>
 174  <form method="post" action="">
 175  <table>
 176  <thead>
 177  <tr>
 178  <th scope="col"><?php _e( 'Position' ); ?></th>
 179  <th scope="col"><?php _e( 'Repeat' ); ?></th>
 180  <th scope="col"><?php _e( 'Attachment' ); ?></th>
 181  <th scope="col"><?php _e( 'Color' ); ?></th>
 182  </tr>
 183  
 184  <tbody>
 185  <tr>
 186  <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Position' ); ?></span></legend>
 187  <label>
 188  <input name="background-position" type="radio" value="left" <?php checked('left', get_theme_mod('background_position', 'left')); ?> />
 189  <?php _e('Left') ?>
 190  </label>
 191  <label>
 192  <input name="background-position" type="radio" value="center" <?php checked('center', get_theme_mod('background_position', 'left')); ?> />
 193  <?php _e('Center') ?>
 194  </label>
 195  <label>
 196  <input name="background-position" type="radio" value="right" <?php checked('right', get_theme_mod('background_position', 'left')); ?> />
 197  <?php _e('Right') ?>
 198  </label>
 199  </fieldset></td>
 200  
 201  <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Repeat' ); ?></span></legend>
 202  <label>
 203  <select name="background-repeat">
 204      <option value="no-repeat" <?php selected('no-repeat', get_theme_mod('background_repeat', 'repeat')); ?> ><?php _e('No repeat'); ?></option>
 205      <option value="repeat" <?php selected('repeat', get_theme_mod('background_repeat', 'repeat')); ?>><?php _e('Tile'); ?></option>
 206      <option value="repeat-x" <?php selected('repeat-x', get_theme_mod('background_repeat', 'repeat')); ?>><?php _e('Tile Horizontally'); ?></option>
 207      <option value="repeat-y" <?php selected('repeat-y', get_theme_mod('background_repeat', 'repeat')); ?>><?php _e('Tile Vertically'); ?></option>
 208  </select>
 209  </label>
 210  </fieldset></td>
 211  
 212  <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Attachment' ); ?></span></legend>
 213  <label>
 214  <input name="background-attachment" type="radio" value="scroll" <?php checked('scroll', get_theme_mod('background_attachment', 'fixed')); ?> />
 215  <?php _e('Scroll') ?>
 216  </label>
 217  <label>
 218  <input name="background-attachment" type="radio" value="fixed" <?php checked('fixed', get_theme_mod('background_attachment', 'fixed')); ?> />
 219  <?php _e('Fixed') ?>
 220  </label>
 221  </fieldset></td>
 222  
 223  <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Color' ); ?></span></legend>
 224  <input type="text" name="background-color" id="background-color" value="#<?php echo esc_attr(get_background_color()) ?>" />
 225  <input type="button" class="button" value="<?php esc_attr_e('Select a Color'); ?>" id="pickcolor" />
 226  
 227  <div id="colorPickerDiv" style="z-index: 100; background:#eee; border:1px solid #ccc; position:absolute; display:none;"></div>
 228  </fieldset></td>
 229  </tr>
 230  </tbody>
 231  </table>
 232  
 233  <?php wp_nonce_field('custom-background'); ?>
 234  <p class="submit"><input type="submit" class="button" name="save-background-options" value="<?php esc_attr_e('Save Changes'); ?>" /></p>
 235  </form>
 236  
 237  <h3><?php _e('Upload New Background Image'); ?></h3>
 238  <form enctype="multipart/form-data" id="uploadForm" method="POST" action="">
 239  <label for="upload"><?php _e('Choose an image from your computer:'); ?></label><br /><input type="file" id="upload" name="import" />
 240  <input type="hidden" name="action" value="save" />
 241  <?php wp_nonce_field('custom-background') ?>
 242  <p class="submit">
 243  <input type="submit" value="<?php esc_attr_e('Upload'); ?>" />
 244  </p>
 245  </form>
 246  
 247  <?php if ( get_background_image() ) : ?>
 248  <h3><?php _e('Remove Background Image'); ?></h3>
 249  <p><?php _e('This will remove the background image. You will not be able to retrieve any customizations.') ?></p>
 250  <form method="post" action="">
 251  <?php wp_nonce_field('custom-background'); ?>
 252  <input type="submit" class="button" name="remove-background" value="<?php esc_attr_e('Remove Background'); ?>" />
 253  </form>
 254  <?php endif; ?>
 255  
 256  </div>
 257  <?php
 258      }
 259  
 260      /**
 261       * Handle a Image upload for the background image.
 262       *
 263       * @since 3.0.0
 264       */
 265  	function handle_upload() {
 266  
 267          if ( empty($_FILES) )
 268              return;
 269  
 270          check_admin_referer('custom-background');
 271          $overrides = array('test_form' => false);
 272          $file = wp_handle_upload($_FILES['import'], $overrides);
 273  
 274          if ( isset($file['error']) )
 275              wp_die( $file['error'] );
 276  
 277          $url = $file['url'];
 278          $type = $file['type'];
 279          $file = $file['file'];
 280          $filename = basename($file);
 281  
 282          // Construct the object array
 283          $object = array(
 284              'post_title' => $filename,
 285              'post_content' => $url,
 286              'post_mime_type' => $type,
 287              'guid' => $url
 288          );
 289  
 290          // Save the data
 291          $id = wp_insert_attachment($object, $file);
 292  
 293          // Add the meta-data
 294          wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
 295  
 296          set_theme_mod('background_image', esc_url($url));
 297  
 298          $thumbnail = wp_get_attachment_image_src( $id, 'thumbnail' );
 299          set_theme_mod('background_image_thumb', esc_url( $thumbnail[0] ) );
 300  
 301          do_action('wp_create_file_in_uploads', $file, $id); // For replication
 302          $this->updated = true;
 303      }
 304  
 305  }
 306  ?>


Generated: Mon Apr 5 14:26:09 2010 Cross-referenced by PHPXref 0.7