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