PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/custom-background.php

https://bitbucket.org/abnopanda/wordpress
PHP | 425 lines | 294 code | 49 blank | 82 comment | 34 complexity | 0604898fe4a7ebd9a651774f9bf258da MD5 | raw file
  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. add_action( 'admin_menu', array( $this, 'init' ) );
  52. add_action( 'wp_ajax_set-background-image', array( $this, 'wp_set_background_image' ) );
  53. }
  54. /**
  55. * Set up the hooks for the Custom Background admin page.
  56. *
  57. * @since 3.0.0
  58. */
  59. function init() {
  60. if ( ! current_user_can('edit_theme_options') )
  61. return;
  62. $this->page = $page = add_theme_page(__('Background'), __('Background'), 'edit_theme_options', 'custom-background', array(&$this, 'admin_page'));
  63. add_action("load-$page", array(&$this, 'admin_load'));
  64. add_action("load-$page", array(&$this, 'take_action'), 49);
  65. add_action("load-$page", array(&$this, 'handle_upload'), 49);
  66. if ( $this->admin_header_callback )
  67. add_action("admin_head-$page", $this->admin_header_callback, 51);
  68. }
  69. /**
  70. * Set up the enqueue for the CSS & JavaScript files.
  71. *
  72. * @since 3.0.0
  73. */
  74. function admin_load() {
  75. get_current_screen()->add_help_tab( array(
  76. 'id' => 'overview',
  77. 'title' => __('Overview'),
  78. 'content' =>
  79. '<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>' .
  80. '<p>' . __( 'To use a background image, simply upload it or choose an image that has already been uploaded to your Media Library by clicking the &#8220;Choose Image&#8221; button. 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>' .
  81. '<p>' . __( 'You can also choose a background color by clicking the Select Color button and either typing in a legitimate HTML hex value, e.g. &#8220;#ff0000&#8221; for red, or by choosing a color using the color picker.' ) . '</p>' .
  82. '<p>' . __( 'Don&#8217;t forget to click on the Save Changes button when you are finished.' ) . '</p>'
  83. ) );
  84. get_current_screen()->set_help_sidebar(
  85. '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
  86. '<p>' . __( '<a href="http://codex.wordpress.org/Appearance_Background_Screen" target="_blank">Documentation on Custom Background</a>' ) . '</p>' .
  87. '<p>' . __( '<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>' ) . '</p>'
  88. );
  89. wp_enqueue_media();
  90. wp_enqueue_script('custom-background');
  91. wp_enqueue_style('wp-color-picker');
  92. }
  93. /**
  94. * Execute custom background modification.
  95. *
  96. * @since 3.0.0
  97. */
  98. function take_action() {
  99. if ( empty($_POST) )
  100. return;
  101. if ( isset($_POST['reset-background']) ) {
  102. check_admin_referer('custom-background-reset', '_wpnonce-custom-background-reset');
  103. remove_theme_mod('background_image');
  104. remove_theme_mod('background_image_thumb');
  105. $this->updated = true;
  106. return;
  107. }
  108. if ( isset($_POST['remove-background']) ) {
  109. // @TODO: Uploaded files are not removed here.
  110. check_admin_referer('custom-background-remove', '_wpnonce-custom-background-remove');
  111. set_theme_mod('background_image', '');
  112. set_theme_mod('background_image_thumb', '');
  113. $this->updated = true;
  114. wp_safe_redirect( $_POST['_wp_http_referer'] );
  115. return;
  116. }
  117. if ( isset($_POST['background-repeat']) ) {
  118. check_admin_referer('custom-background');
  119. if ( in_array($_POST['background-repeat'], array('repeat', 'no-repeat', 'repeat-x', 'repeat-y')) )
  120. $repeat = $_POST['background-repeat'];
  121. else
  122. $repeat = 'repeat';
  123. set_theme_mod('background_repeat', $repeat);
  124. }
  125. if ( isset($_POST['background-position-x']) ) {
  126. check_admin_referer('custom-background');
  127. if ( in_array($_POST['background-position-x'], array('center', 'right', 'left')) )
  128. $position = $_POST['background-position-x'];
  129. else
  130. $position = 'left';
  131. set_theme_mod('background_position_x', $position);
  132. }
  133. if ( isset($_POST['background-attachment']) ) {
  134. check_admin_referer('custom-background');
  135. if ( in_array($_POST['background-attachment'], array('fixed', 'scroll')) )
  136. $attachment = $_POST['background-attachment'];
  137. else
  138. $attachment = 'fixed';
  139. set_theme_mod('background_attachment', $attachment);
  140. }
  141. if ( isset($_POST['background-color']) ) {
  142. check_admin_referer('custom-background');
  143. $color = preg_replace('/[^0-9a-fA-F]/', '', $_POST['background-color']);
  144. if ( strlen($color) == 6 || strlen($color) == 3 )
  145. set_theme_mod('background_color', $color);
  146. else
  147. set_theme_mod('background_color', '');
  148. }
  149. $this->updated = true;
  150. }
  151. /**
  152. * Display the custom background page.
  153. *
  154. * @since 3.0.0
  155. */
  156. function admin_page() {
  157. ?>
  158. <div class="wrap" id="custom-background">
  159. <?php screen_icon(); ?>
  160. <h2><?php _e('Custom Background'); ?></h2>
  161. <?php if ( !empty($this->updated) ) { ?>
  162. <div id="message" class="updated">
  163. <p><?php printf( __( 'Background updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) ); ?></p>
  164. </div>
  165. <?php }
  166. if ( $this->admin_image_div_callback ) {
  167. call_user_func($this->admin_image_div_callback);
  168. } else {
  169. ?>
  170. <h3><?php _e('Background Image'); ?></h3>
  171. <table class="form-table">
  172. <tbody>
  173. <tr valign="top">
  174. <th scope="row"><?php _e('Preview'); ?></th>
  175. <td>
  176. <?php
  177. $background_styles = '';
  178. if ( $bgcolor = get_background_color() )
  179. $background_styles .= 'background-color: #' . $bgcolor . ';';
  180. if ( get_background_image() ) {
  181. // background-image URL must be single quote, see below
  182. $background_styles .= ' background-image: url(\'' . set_url_scheme( get_theme_mod( 'background_image_thumb', get_background_image() ) ) . '\');'
  183. . ' background-repeat: ' . get_theme_mod('background_repeat', 'repeat') . ';'
  184. . ' background-position: top ' . get_theme_mod('background_position_x', 'left');
  185. }
  186. ?>
  187. <div id="custom-background-image" style="<?php echo $background_styles; ?>"><?php // must be double quote, see above ?>
  188. <?php if ( get_background_image() ) { ?>
  189. <img class="custom-background-image" src="<?php echo set_url_scheme( get_theme_mod( 'background_image_thumb', get_background_image() ) ); ?>" style="visibility:hidden;" alt="" /><br />
  190. <img class="custom-background-image" src="<?php echo set_url_scheme( get_theme_mod( 'background_image_thumb', get_background_image() ) ); ?>" style="visibility:hidden;" alt="" />
  191. <?php } ?>
  192. </div>
  193. <?php } ?>
  194. </td>
  195. </tr>
  196. <?php if ( get_background_image() ) : ?>
  197. <tr valign="top">
  198. <th scope="row"><?php _e('Remove Image'); ?></th>
  199. <td>
  200. <form method="post" action="">
  201. <?php wp_nonce_field('custom-background-remove', '_wpnonce-custom-background-remove'); ?>
  202. <?php submit_button( __( 'Remove Background Image' ), 'button', 'remove-background', false ); ?><br/>
  203. <?php _e('This will remove the background image. You will not be able to restore any customizations.') ?>
  204. </form>
  205. </td>
  206. </tr>
  207. <?php endif; ?>
  208. <?php $default_image = get_theme_support( 'custom-background', 'default-image' ); ?>
  209. <?php if ( $default_image && get_background_image() != $default_image ) : ?>
  210. <tr valign="top">
  211. <th scope="row"><?php _e('Restore Original Image'); ?></th>
  212. <td>
  213. <form method="post" action="">
  214. <?php wp_nonce_field('custom-background-reset', '_wpnonce-custom-background-reset'); ?>
  215. <?php submit_button( __( 'Restore Original Image' ), 'button', 'reset-background', false ); ?><br/>
  216. <?php _e('This will restore the original background image. You will not be able to restore any customizations.') ?>
  217. </form>
  218. </td>
  219. </tr>
  220. <?php endif; ?>
  221. <tr valign="top">
  222. <th scope="row"><?php _e('Select Image'); ?></th>
  223. <td><form enctype="multipart/form-data" id="upload-form" class="wp-upload-form" method="post" action="">
  224. <p>
  225. <label for="upload"><?php _e( 'Choose an image from your computer:' ); ?></label><br />
  226. <input type="file" id="upload" name="import" />
  227. <input type="hidden" name="action" value="save" />
  228. <?php wp_nonce_field( 'custom-background-upload', '_wpnonce-custom-background-upload' ); ?>
  229. <?php submit_button( __( 'Upload' ), 'button', 'submit', false ); ?>
  230. </p>
  231. <p>
  232. <label for="choose-from-library-link"><?php _e( 'Or choose an image from your media library:' ); ?></label><br />
  233. <a id="choose-from-library-link" class="button"
  234. data-choose="<?php esc_attr_e( 'Choose a Background Image' ); ?>"
  235. data-update="<?php esc_attr_e( 'Set as background' ); ?>"><?php _e( 'Choose Image' ); ?></a>
  236. </p>
  237. </form>
  238. </td>
  239. </tr>
  240. </tbody>
  241. </table>
  242. <h3><?php _e('Display Options') ?></h3>
  243. <form method="post" action="">
  244. <table class="form-table">
  245. <tbody>
  246. <?php if ( get_background_image() ) : ?>
  247. <tr valign="top">
  248. <th scope="row"><?php _e( 'Position' ); ?></th>
  249. <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Position' ); ?></span></legend>
  250. <label>
  251. <input name="background-position-x" type="radio" value="left"<?php checked('left', get_theme_mod('background_position_x', 'left')); ?> />
  252. <?php _e('Left') ?>
  253. </label>
  254. <label>
  255. <input name="background-position-x" type="radio" value="center"<?php checked('center', get_theme_mod('background_position_x', 'left')); ?> />
  256. <?php _e('Center') ?>
  257. </label>
  258. <label>
  259. <input name="background-position-x" type="radio" value="right"<?php checked('right', get_theme_mod('background_position_x', 'left')); ?> />
  260. <?php _e('Right') ?>
  261. </label>
  262. </fieldset></td>
  263. </tr>
  264. <tr valign="top">
  265. <th scope="row"><?php _e( 'Repeat' ); ?></th>
  266. <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Repeat' ); ?></span></legend>
  267. <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>
  268. <label><input type="radio" name="background-repeat" value="repeat"<?php checked('repeat', get_theme_mod('background_repeat', 'repeat')); ?> /> <?php _e('Tile'); ?></label>
  269. <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>
  270. <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>
  271. </fieldset></td>
  272. </tr>
  273. <tr valign="top">
  274. <th scope="row"><?php _e( 'Attachment' ); ?></th>
  275. <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Attachment' ); ?></span></legend>
  276. <label>
  277. <input name="background-attachment" type="radio" value="scroll" <?php checked('scroll', get_theme_mod('background_attachment', 'scroll')); ?> />
  278. <?php _e('Scroll') ?>
  279. </label>
  280. <label>
  281. <input name="background-attachment" type="radio" value="fixed" <?php checked('fixed', get_theme_mod('background_attachment', 'scroll')); ?> />
  282. <?php _e('Fixed') ?>
  283. </label>
  284. </fieldset></td>
  285. </tr>
  286. <?php endif; // get_background_image() ?>
  287. <tr valign="top">
  288. <th scope="row"><?php _e( 'Background Color' ); ?></th>
  289. <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Color' ); ?></span></legend>
  290. <?php
  291. $default_color = '';
  292. if ( current_theme_supports( 'custom-background', 'default-color' ) )
  293. $default_color = ' data-default-color="#' . esc_attr( get_theme_support( 'custom-background', 'default-color' ) ) . '"';
  294. ?>
  295. <input type="text" name="background-color" id="background-color" value="#<?php echo esc_attr( get_background_color() ); ?>"<?php echo $default_color ?> />
  296. </fieldset></td>
  297. </tr>
  298. </tbody>
  299. </table>
  300. <?php wp_nonce_field('custom-background'); ?>
  301. <?php submit_button( null, 'primary', 'save-background-options' ); ?>
  302. </form>
  303. </div>
  304. <?php
  305. }
  306. /**
  307. * Handle an Image upload for the background image.
  308. *
  309. * @since 3.0.0
  310. */
  311. function handle_upload() {
  312. if ( empty($_FILES) )
  313. return;
  314. check_admin_referer('custom-background-upload', '_wpnonce-custom-background-upload');
  315. $overrides = array('test_form' => false);
  316. $uploaded_file = $_FILES['import'];
  317. $wp_filetype = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'], false );
  318. if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) )
  319. wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) );
  320. $file = wp_handle_upload($uploaded_file, $overrides);
  321. if ( isset($file['error']) )
  322. wp_die( $file['error'] );
  323. $url = $file['url'];
  324. $type = $file['type'];
  325. $file = $file['file'];
  326. $filename = basename($file);
  327. // Construct the object array
  328. $object = array(
  329. 'post_title' => $filename,
  330. 'post_content' => $url,
  331. 'post_mime_type' => $type,
  332. 'guid' => $url,
  333. 'context' => 'custom-background'
  334. );
  335. // Save the data
  336. $id = wp_insert_attachment($object, $file);
  337. // Add the meta-data
  338. wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
  339. update_post_meta( $id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) );
  340. set_theme_mod('background_image', esc_url_raw($url));
  341. $thumbnail = wp_get_attachment_image_src( $id, 'thumbnail' );
  342. set_theme_mod('background_image_thumb', esc_url_raw( $thumbnail[0] ) );
  343. do_action('wp_create_file_in_uploads', $file, $id); // For replication
  344. $this->updated = true;
  345. }
  346. /**
  347. * Unused since 3.5.0.
  348. *
  349. * @since 3.4.0
  350. */
  351. function attachment_fields_to_edit( $form_fields ) {
  352. return $form_fields;
  353. }
  354. /**
  355. * Unused since 3.5.0.
  356. *
  357. * @since 3.4.0
  358. */
  359. function filter_upload_tabs( $tabs ) {
  360. return $tabs;
  361. }
  362. public function wp_set_background_image() {
  363. if ( ! current_user_can('edit_theme_options') || ! isset( $_POST['attachment_id'] ) ) exit;
  364. $attachment_id = absint($_POST['attachment_id']);
  365. $sizes = array_keys(apply_filters( 'image_size_names_choose', array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'), 'full' => __('Full Size')) ));
  366. $size = 'thumbnail';
  367. if ( in_array( $_POST['size'], $sizes ) )
  368. $size = esc_attr( $_POST['size'] );
  369. update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) );
  370. $url = wp_get_attachment_image_src( $attachment_id, $size );
  371. $thumbnail = wp_get_attachment_image_src( $attachment_id, 'thumbnail' );
  372. set_theme_mod( 'background_image', esc_url_raw( $url[0] ) );
  373. set_theme_mod( 'background_image_thumb', esc_url_raw( $thumbnail[0] ) );
  374. exit;
  375. }
  376. }