PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-admin/custom-background.php

https://bitbucket.org/aqge/deptandashboard
PHP | 370 lines | 255 code | 43 blank | 72 comment | 27 complexity | 4a22847f29cd27926c4c845e1ad9b317 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * The custom background script.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /**
  9. * The custom background class.
  10. *
  11. * @since 3.0.0
  12. * @package WordPress
  13. * @subpackage Administration
  14. */
  15. class Custom_Background {
  16. /**
  17. * Callback for administration header.
  18. *
  19. * @var callback
  20. * @since 3.0.0
  21. * @access private
  22. */
  23. var $admin_header_callback;
  24. /**
  25. * Callback for header div.
  26. *
  27. * @var callback
  28. * @since 3.0.0
  29. * @access private
  30. */
  31. var $admin_image_div_callback;
  32. /**
  33. * Holds the page menu hook.
  34. *
  35. * @var string
  36. * @since 3.0.0
  37. * @access private
  38. */
  39. var $page = '';
  40. /**
  41. * Constructor - Register administration header callback.
  42. *
  43. * @since 3.0.0
  44. * @param callback $admin_header_callback
  45. * @param callback $admin_image_div_callback Optional custom image div output callback.
  46. * @return Custom_Background
  47. */
  48. function __construct($admin_header_callback = '', $admin_image_div_callback = '') {
  49. $this->admin_header_callback = $admin_header_callback;
  50. $this->admin_image_div_callback = $admin_image_div_callback;
  51. }
  52. /**
  53. * Set up the hooks for the Custom Background admin page.
  54. *
  55. * @since 3.0.0
  56. */
  57. function init() {
  58. if ( ! current_user_can('edit_theme_options') )
  59. return;
  60. $this->page = $page = add_theme_page(__('Background'), __('Background'), 'edit_theme_options', 'custom-background', array(&$this, 'admin_page'));
  61. add_action("load-$page", array(&$this, 'admin_load'));
  62. add_action("load-$page", array(&$this, 'take_action'), 49);
  63. add_action("load-$page", array(&$this, 'handle_upload'), 49);
  64. if ( $this->admin_header_callback )
  65. add_action("admin_head-$page", $this->admin_header_callback, 51);
  66. }
  67. /**
  68. * Set up the enqueue for the CSS & JavaScript files.
  69. *
  70. * @since 3.0.0
  71. */
  72. function admin_load() {
  73. get_current_screen()->add_help_tab( array(
  74. 'id' => 'overview',
  75. 'title' => __('Overview'),
  76. 'content' =>
  77. '<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>' .
  78. '<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>' .
  79. '<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>' .
  80. '<p>' . __( 'Don&#8217;t forget to click on the Save Changes button when you are finished.' ) . '</p>'
  81. ) );
  82. get_current_screen()->set_help_sidebar(
  83. '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
  84. '<p>' . __( '<a href="http://codex.wordpress.org/Appearance_Background_Screen" target="_blank">Documentation on Custom Background</a>' ) . '</p>' .
  85. '<p>' . __( '<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>' ) . '</p>'
  86. );
  87. wp_enqueue_script('custom-background');
  88. wp_enqueue_style('farbtastic');
  89. }
  90. /**
  91. * Execute custom background modification.
  92. *
  93. * @since 3.0.0
  94. */
  95. function take_action() {
  96. if ( empty($_POST) )
  97. return;
  98. if ( isset($_POST['reset-background']) ) {
  99. check_admin_referer('custom-background-reset', '_wpnonce-custom-background-reset');
  100. remove_theme_mod('background_image');
  101. remove_theme_mod('background_image_thumb');
  102. $this->updated = true;
  103. return;
  104. }
  105. if ( isset($_POST['remove-background']) ) {
  106. // @TODO: Uploaded files are not removed here.
  107. check_admin_referer('custom-background-remove', '_wpnonce-custom-background-remove');
  108. set_theme_mod('background_image', '');
  109. set_theme_mod('background_image_thumb', '');
  110. $this->updated = true;
  111. return;
  112. }
  113. if ( isset($_POST['background-repeat']) ) {
  114. check_admin_referer('custom-background');
  115. if ( in_array($_POST['background-repeat'], array('repeat', 'no-repeat', 'repeat-x', 'repeat-y')) )
  116. $repeat = $_POST['background-repeat'];
  117. else
  118. $repeat = 'repeat';
  119. set_theme_mod('background_repeat', $repeat);
  120. }
  121. if ( isset($_POST['background-position-x']) ) {
  122. check_admin_referer('custom-background');
  123. if ( in_array($_POST['background-position-x'], array('center', 'right', 'left')) )
  124. $position = $_POST['background-position-x'];
  125. else
  126. $position = 'left';
  127. set_theme_mod('background_position_x', $position);
  128. }
  129. if ( isset($_POST['background-attachment']) ) {
  130. check_admin_referer('custom-background');
  131. if ( in_array($_POST['background-attachment'], array('fixed', 'scroll')) )
  132. $attachment = $_POST['background-attachment'];
  133. else
  134. $attachment = 'fixed';
  135. set_theme_mod('background_attachment', $attachment);
  136. }
  137. if ( isset($_POST['background-color']) ) {
  138. check_admin_referer('custom-background');
  139. $color = preg_replace('/[^0-9a-fA-F]/', '', $_POST['background-color']);
  140. if ( strlen($color) == 6 || strlen($color) == 3 )
  141. set_theme_mod('background_color', $color);
  142. else
  143. set_theme_mod('background_color', '');
  144. }
  145. $this->updated = true;
  146. }
  147. /**
  148. * Display the custom background page.
  149. *
  150. * @since 3.0.0
  151. */
  152. function admin_page() {
  153. ?>
  154. <div class="wrap" id="custom-background">
  155. <?php screen_icon(); ?>
  156. <h2><?php _e('Custom Background'); ?></h2>
  157. <?php if ( !empty($this->updated) ) { ?>
  158. <div id="message" class="updated">
  159. <p><?php printf( __( 'Background updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) ); ?></p>
  160. </div>
  161. <?php }
  162. if ( $this->admin_image_div_callback ) {
  163. call_user_func($this->admin_image_div_callback);
  164. } else {
  165. ?>
  166. <h3><?php _e('Background Image'); ?></h3>
  167. <table class="form-table">
  168. <tbody>
  169. <tr valign="top">
  170. <th scope="row"><?php _e('Preview'); ?></th>
  171. <td>
  172. <?php
  173. $background_styles = '';
  174. if ( $bgcolor = get_background_color() )
  175. $background_styles .= 'background-color: #' . $bgcolor . ';';
  176. if ( get_background_image() ) {
  177. // background-image URL must be single quote, see below
  178. $background_styles .= ' background-image: url(\'' . get_theme_mod('background_image_thumb', '') . '\');'
  179. . ' background-repeat: ' . get_theme_mod('background_repeat', 'repeat') . ';'
  180. . ' background-position: top ' . get_theme_mod('background_position_x', 'left');
  181. }
  182. ?>
  183. <div id="custom-background-image" style="<?php echo $background_styles; ?>"><?php // must be double quote, see above ?>
  184. <?php if ( get_background_image() ) { ?>
  185. <img class="custom-background-image" src="<?php echo get_theme_mod('background_image_thumb', ''); ?>" style="visibility:hidden;" alt="" /><br />
  186. <img class="custom-background-image" src="<?php echo get_theme_mod('background_image_thumb', ''); ?>" style="visibility:hidden;" alt="" />
  187. <?php } ?>
  188. </div>
  189. <?php } ?>
  190. </td>
  191. </tr>
  192. <?php if ( get_background_image() ) : ?>
  193. <tr valign="top">
  194. <th scope="row"><?php _e('Remove Image'); ?></th>
  195. <td>
  196. <form method="post" action="">
  197. <?php wp_nonce_field('custom-background-remove', '_wpnonce-custom-background-remove'); ?>
  198. <?php submit_button( __( 'Remove Background Image' ), 'button', 'remove-background', false ); ?><br/>
  199. <?php _e('This will remove the background image. You will not be able to restore any customizations.') ?>
  200. </form>
  201. </td>
  202. </tr>
  203. <?php endif; ?>
  204. <?php if ( defined( 'BACKGROUND_IMAGE' ) ) : // Show only if a default background image exists ?>
  205. <tr valign="top">
  206. <th scope="row"><?php _e('Restore Original Image'); ?></th>
  207. <td>
  208. <form method="post" action="">
  209. <?php wp_nonce_field('custom-background-reset', '_wpnonce-custom-background-reset'); ?>
  210. <?php submit_button( __( 'Restore Original Image' ), 'button', 'reset-background', false ); ?><br/>
  211. <?php _e('This will restore the original background image. You will not be able to restore any customizations.') ?>
  212. </form>
  213. </td>
  214. </tr>
  215. <?php endif; ?>
  216. <tr valign="top">
  217. <th scope="row"><?php _e('Upload Image'); ?></th>
  218. <td><form enctype="multipart/form-data" id="upload-form" method="post" action="">
  219. <label for="upload"><?php _e('Choose an image from your computer:'); ?></label><br /><input type="file" id="upload" name="import" />
  220. <input type="hidden" name="action" value="save" />
  221. <?php wp_nonce_field('custom-background-upload', '_wpnonce-custom-background-upload') ?>
  222. <?php submit_button( __( 'Upload' ), 'button', 'submit', false ); ?>
  223. </form>
  224. </td>
  225. </tr>
  226. </tbody>
  227. </table>
  228. <h3><?php _e('Display Options') ?></h3>
  229. <form method="post" action="">
  230. <table class="form-table">
  231. <tbody>
  232. <?php if ( get_background_image() ) : ?>
  233. <tr valign="top">
  234. <th scope="row"><?php _e( 'Position' ); ?></th>
  235. <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Position' ); ?></span></legend>
  236. <label>
  237. <input name="background-position-x" type="radio" value="left"<?php checked('left', get_theme_mod('background_position_x', 'left')); ?> />
  238. <?php _e('Left') ?>
  239. </label>
  240. <label>
  241. <input name="background-position-x" type="radio" value="center"<?php checked('center', get_theme_mod('background_position_x', 'left')); ?> />
  242. <?php _e('Center') ?>
  243. </label>
  244. <label>
  245. <input name="background-position-x" type="radio" value="right"<?php checked('right', get_theme_mod('background_position_x', 'left')); ?> />
  246. <?php _e('Right') ?>
  247. </label>
  248. </fieldset></td>
  249. </tr>
  250. <tr valign="top">
  251. <th scope="row"><?php _e( 'Repeat' ); ?></th>
  252. <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Repeat' ); ?></span></legend>
  253. <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>
  254. <label><input type="radio" name="background-repeat" value="repeat"<?php checked('repeat', get_theme_mod('background_repeat', 'repeat')); ?> /> <?php _e('Tile'); ?></label>
  255. <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>
  256. <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>
  257. </fieldset></td>
  258. </tr>
  259. <tr valign="top">
  260. <th scope="row"><?php _e( 'Attachment' ); ?></th>
  261. <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Attachment' ); ?></span></legend>
  262. <label>
  263. <input name="background-attachment" type="radio" value="scroll" <?php checked('scroll', get_theme_mod('background_attachment', 'scroll')); ?> />
  264. <?php _e('Scroll') ?>
  265. </label>
  266. <label>
  267. <input name="background-attachment" type="radio" value="fixed" <?php checked('fixed', get_theme_mod('background_attachment', 'scroll')); ?> />
  268. <?php _e('Fixed') ?>
  269. </label>
  270. </fieldset></td>
  271. </tr>
  272. <?php endif; // get_background_image() ?>
  273. <tr valign="top">
  274. <th scope="row"><?php _e( 'Background Color' ); ?></th>
  275. <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Color' ); ?></span></legend>
  276. <?php $show_clear = get_background_color() ? '' : ' style="display:none"'; ?>
  277. <input type="text" name="background-color" id="background-color" value="#<?php echo esc_attr(get_background_color()) ?>" />
  278. <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>
  279. <div id="colorPickerDiv" style="z-index: 100; background:#eee; border:1px solid #ccc; position:absolute; display:none;"></div>
  280. </fieldset></td>
  281. </tr>
  282. </tbody>
  283. </table>
  284. <?php wp_nonce_field('custom-background'); ?>
  285. <?php submit_button( null, 'primary', 'save-background-options' ); ?>
  286. </form>
  287. </div>
  288. <?php
  289. }
  290. /**
  291. * Handle an Image upload for the background image.
  292. *
  293. * @since 3.0.0
  294. */
  295. function handle_upload() {
  296. if ( empty($_FILES) )
  297. return;
  298. check_admin_referer('custom-background-upload', '_wpnonce-custom-background-upload');
  299. $overrides = array('test_form' => false);
  300. $file = wp_handle_upload($_FILES['import'], $overrides);
  301. if ( isset($file['error']) )
  302. wp_die( $file['error'] );
  303. $url = $file['url'];
  304. $type = $file['type'];
  305. $file = $file['file'];
  306. $filename = basename($file);
  307. // Construct the object array
  308. $object = array(
  309. 'post_title' => $filename,
  310. 'post_content' => $url,
  311. 'post_mime_type' => $type,
  312. 'guid' => $url,
  313. 'context' => 'custom-background'
  314. );
  315. // Save the data
  316. $id = wp_insert_attachment($object, $file);
  317. // Add the meta-data
  318. wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
  319. update_post_meta( $id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) );
  320. set_theme_mod('background_image', esc_url($url));
  321. $thumbnail = wp_get_attachment_image_src( $id, 'thumbnail' );
  322. set_theme_mod('background_image_thumb', esc_url( $thumbnail[0] ) );
  323. do_action('wp_create_file_in_uploads', $file, $id); // For replication
  324. $this->updated = true;
  325. }
  326. }
  327. ?>