Wordpress 3.0.4-scripts
[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 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          * Holds the page menu hook.
38          *
39          * @var string
40          * @since 3.0.0
41          * @access private
42          */
43         var $page = '';
44
45         /**
46          * PHP4 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 Custom_Background($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&#8217;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&#8217;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_SubPanel" 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 <input type="submit" class="button" name="remove-background" value="<?php esc_attr_e('Remove Background Image'); ?>" /><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 <input type="submit" class="button" name="reset-background" value="<?php esc_attr_e('Restore Original Image'); ?>" /><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 <input type="submit" class="button" value="<?php esc_attr_e('Upload'); ?>" />
240 </p>
241 </form>
242 </td>
243 </tr>
244 </tbody>
245 </table>
246
247 <h3><?php _e('Display Options') ?></h3>
248 <form method="post" action="">
249 <table class="form-table">
250 <tbody>
251 <?php if ( get_background_image() ) : ?>
252 <tr valign="top">
253 <th scope="row"><?php _e( 'Position' ); ?></th>
254 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Position' ); ?></span></legend>
255 <label>
256 <input name="background-position-x" type="radio" value="left"<?php checked('left', get_theme_mod('background_position_x', 'left')); ?> />
257 <?php _e('Left') ?>
258 </label>
259 <label>
260 <input name="background-position-x" type="radio" value="center"<?php checked('center', get_theme_mod('background_position_x', 'left')); ?> />
261 <?php _e('Center') ?>
262 </label>
263 <label>
264 <input name="background-position-x" type="radio" value="right"<?php checked('right', get_theme_mod('background_position_x', 'left')); ?> />
265 <?php _e('Right') ?>
266 </label>
267 </fieldset></td>
268 </tr>
269
270 <tr valign="top">
271 <th scope="row"><?php _e( 'Repeat' ); ?></th>
272 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Repeat' ); ?></span></legend>
273 <label><input type="radio" name="background-repeat" value="no-repeat"<?php checked('no-repeat', get_theme_mod('background_repeat', 'repeat')); ?>> <?php _e('No Repeat'); ?></option></label>
274         <label><input type="radio" name="background-repeat" value="repeat"<?php checked('repeat', get_theme_mod('background_repeat', 'repeat')); ?>> <?php _e('Tile'); ?></option></label>
275         <label><input type="radio" name="background-repeat" value="repeat-x"<?php checked('repeat-x', get_theme_mod('background_repeat', 'repeat')); ?>> <?php _e('Tile Horizontally'); ?></option></label>
276         <label><input type="radio" name="background-repeat" value="repeat-y"<?php checked('repeat-y', get_theme_mod('background_repeat', 'repeat')); ?>> <?php _e('Tile Vertically'); ?></option></label>
277 </fieldset></td>
278 </tr>
279
280 <tr valign="top">
281 <th scope="row"><?php _e( 'Attachment' ); ?></th>
282 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Attachment' ); ?></span></legend>
283 <label>
284 <input name="background-attachment" type="radio" value="scroll" <?php checked('scroll', get_theme_mod('background_attachment', 'scroll')); ?> />
285 <?php _e('Scroll') ?>
286 </label>
287 <label>
288 <input name="background-attachment" type="radio" value="fixed" <?php checked('fixed', get_theme_mod('background_attachment', 'scroll')); ?> />
289 <?php _e('Fixed') ?>
290 </label>
291 </fieldset></td>
292 </tr>
293 <?php endif; // get_background_image() ?>
294 <tr valign="top">
295 <th scope="row"><?php _e( 'Color' ); ?></th>
296 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Color' ); ?></span></legend>
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>
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 <p class="submit"><input type="submit" class="button-primary" name="save-background-options" value="<?php esc_attr_e('Save Changes'); ?>" /></p>
307 </form>
308
309 </div>
310 <?php
311         }
312
313         /**
314          * Handle a 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                 );
342
343                 // Save the data
344                 $id = wp_insert_attachment($object, $file);
345
346                 // Add the meta-data
347                 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
348
349                 set_theme_mod('background_image', esc_url($url));
350
351                 $thumbnail = wp_get_attachment_image_src( $id, 'thumbnail' );
352                 set_theme_mod('background_image_thumb', esc_url( $thumbnail[0] ) );
353
354                 do_action('wp_create_file_in_uploads', $file, $id); // For replication
355                 $this->updated = true;
356         }
357
358 }
359 ?>