PageRenderTime 77ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/wordpress3.4.2/wp-admin/custom-background.php

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