PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-admin/custom-header.php

https://github.com/shaiquddin/WordPress
PHP | 1031 lines | 712 code | 126 blank | 193 comment | 146 complexity | 194b6cfcda4dc0508505ffc21d7e1250 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1
  1. <?php
  2. /**
  3. * The custom header image script.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /**
  9. * The custom header image class.
  10. *
  11. * @since 2.1.0
  12. * @package WordPress
  13. * @subpackage Administration
  14. */
  15. class Custom_Image_Header {
  16. /**
  17. * Callback for administration header.
  18. *
  19. * @var callback
  20. * @since 2.1.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 default headers.
  34. *
  35. * @var array
  36. * @since 3.0.0
  37. * @access private
  38. */
  39. var $default_headers = array();
  40. /**
  41. * Holds custom headers uploaded by the user
  42. *
  43. * @var array
  44. * @since 3.2.0
  45. * @access private
  46. */
  47. var $uploaded_headers = array();
  48. /**
  49. * Holds the page menu hook.
  50. *
  51. * @var string
  52. * @since 3.0.0
  53. * @access private
  54. */
  55. var $page = '';
  56. /**
  57. * Constructor - Register administration header callback.
  58. *
  59. * @since 2.1.0
  60. * @param callback $admin_header_callback
  61. * @param callback $admin_image_div_callback Optional custom image div output callback.
  62. * @return Custom_Image_Header
  63. */
  64. function __construct($admin_header_callback, $admin_image_div_callback = '') {
  65. $this->admin_header_callback = $admin_header_callback;
  66. $this->admin_image_div_callback = $admin_image_div_callback;
  67. add_action( 'admin_menu', array( $this, 'init' ) );
  68. }
  69. /**
  70. * Set up the hooks for the Custom Header admin page.
  71. *
  72. * @since 2.1.0
  73. */
  74. function init() {
  75. if ( ! current_user_can('edit_theme_options') )
  76. return;
  77. $this->page = $page = add_theme_page(__('Header'), __('Header'), 'edit_theme_options', 'custom-header', array(&$this, 'admin_page'));
  78. add_action("admin_print_scripts-$page", array(&$this, 'js_includes'));
  79. add_action("admin_print_styles-$page", array(&$this, 'css_includes'));
  80. add_action("admin_head-$page", array(&$this, 'help') );
  81. add_action("admin_head-$page", array(&$this, 'take_action'), 50);
  82. add_action("admin_head-$page", array(&$this, 'js'), 50);
  83. if ( $this->admin_header_callback )
  84. add_action("admin_head-$page", $this->admin_header_callback, 51);
  85. if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] == 'custom-header' ) {
  86. add_filter( 'attachment_fields_to_edit', array( $this, 'attachment_fields_to_edit' ), 10, 2 );
  87. add_filter( 'media_upload_tabs', array( $this, 'filter_upload_tabs' ) );
  88. add_filter( 'media_upload_mime_type_links', '__return_empty_array' );
  89. }
  90. }
  91. /**
  92. * Adds contextual help.
  93. *
  94. * @since 3.0.0
  95. */
  96. function help() {
  97. get_current_screen()->add_help_tab( array(
  98. 'id' => 'overview',
  99. 'title' => __('Overview'),
  100. 'content' =>
  101. '<p>' . __( 'This screen is used to customize the header section of your theme.') . '</p>' .
  102. '<p>' . __( 'You can choose from the theme&#8217;s default header images, or use one of your own. You can also customize how your Site Title and Tagline are displayed.') . '<p>'
  103. ) );
  104. get_current_screen()->add_help_tab( array(
  105. 'id' => 'set-header-image',
  106. 'title' => __('Header Image'),
  107. 'content' =>
  108. '<p>' . __( 'You can set a custom image header for your site. Simply upload the image and crop it, and the new header will go live immediately. Alternatively, you can use an image that has already been uploaded to your Media Library by clicking the &#8220;Choose Image&#8221; button.' ) . '</p>' .
  109. '<p>' . __( 'Some themes come with additional header images bundled. If you see multiple images displayed, select the one you&#8217;d like and click the &#8220;Save Changes&#8221; button.' ) . '</p>' .
  110. '<p>' . __( 'If your theme has more than one default header image, or you have uploaded more than one custom header image, you have the option of having WordPress display a randomly different image on each page of your site. Click the &#8220;Random&#8221; radio button next to the Uploaded Images or Default Images section to enable this feature.') . '</p>' .
  111. '<p>' . __( 'If you don&#8217;t want a header image to be displayed on your site at all, click the &#8220;Remove Header Image&#8221; button at the bottom of the Header Image section of this page. If you want to re-enable the header image later, you just have to select one of the other image options and click &#8220;Save Changes&#8221;.') . '</p>'
  112. ) );
  113. get_current_screen()->add_help_tab( array(
  114. 'id' => 'set-header-text',
  115. 'title' => __('Header Text'),
  116. 'content' =>
  117. '<p>' . sprintf( __( 'For most themes, the header text is your Site Title and Tagline, as defined in the <a href="%1$s">General Settings</a> section.' ), admin_url( 'options-general.php' ) ) . '<p>' .
  118. '<p>' . __( 'In the Header Text section of this page, you can choose whether to display this text or hide it. You can also choose a color for the text by typing in a legitimate HTML hex value (eg: &#8220;#ff0000&#8221; for red) or by clicking &#8220;Select a Color&#8221; and dialing in a color using the color picker.') . '</p>' .
  119. '<p>' . __( 'Don&#8217;t forget to Save Changes when you&#8217;re done!') . '</p>'
  120. ) );
  121. get_current_screen()->set_help_sidebar(
  122. '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
  123. '<p>' . __( '<a href="http://codex.wordpress.org/Appearance_Header_Screen" target="_blank">Documentation on Custom Header</a>' ) . '</p>' .
  124. '<p>' . __( '<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>' ) . '</p>'
  125. );
  126. }
  127. /**
  128. * Get the current step.
  129. *
  130. * @since 2.6.0
  131. *
  132. * @return int Current step
  133. */
  134. function step() {
  135. if ( ! isset( $_GET['step'] ) )
  136. return 1;
  137. $step = (int) $_GET['step'];
  138. if ( $step < 1 || 3 < $step ||
  139. ( 2 == $step && ! wp_verify_nonce( $_REQUEST['_wpnonce-custom-header-upload'], 'custom-header-upload' ) ) ||
  140. ( 3 == $step && ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'custom-header-crop-image' ) )
  141. )
  142. return 1;
  143. return $step;
  144. }
  145. /**
  146. * Set up the enqueue for the JavaScript files.
  147. *
  148. * @since 2.1.0
  149. */
  150. function js_includes() {
  151. $step = $this->step();
  152. if ( ( 1 == $step || 3 == $step ) ) {
  153. add_thickbox();
  154. wp_enqueue_script( 'media-upload' );
  155. wp_enqueue_script( 'custom-header' );
  156. if ( current_theme_supports( 'custom-header', 'header-text' ) )
  157. wp_enqueue_script( 'wp-color-picker' );
  158. } elseif ( 2 == $step ) {
  159. wp_enqueue_script('imgareaselect');
  160. }
  161. }
  162. /**
  163. * Set up the enqueue for the CSS files
  164. *
  165. * @since 2.7
  166. */
  167. function css_includes() {
  168. $step = $this->step();
  169. if ( ( 1 == $step || 3 == $step ) && current_theme_supports( 'custom-header', 'header-text' ) )
  170. wp_enqueue_style( 'wp-color-picker' );
  171. elseif ( 2 == $step )
  172. wp_enqueue_style('imgareaselect');
  173. }
  174. /**
  175. * Execute custom header modification.
  176. *
  177. * @since 2.6.0
  178. */
  179. function take_action() {
  180. if ( ! current_user_can('edit_theme_options') )
  181. return;
  182. if ( empty( $_POST ) )
  183. return;
  184. $this->updated = true;
  185. if ( isset( $_POST['resetheader'] ) ) {
  186. check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
  187. $this->reset_header_image();
  188. return;
  189. }
  190. if ( isset( $_POST['resettext'] ) ) {
  191. check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
  192. remove_theme_mod('header_textcolor');
  193. return;
  194. }
  195. if ( isset( $_POST['removeheader'] ) ) {
  196. check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
  197. $this->remove_header_image();
  198. return;
  199. }
  200. if ( isset( $_POST['text-color'] ) && ! isset( $_POST['display-header-text'] ) ) {
  201. check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
  202. set_theme_mod( 'header_textcolor', 'blank' );
  203. } elseif ( isset( $_POST['text-color'] ) ) {
  204. check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
  205. $_POST['text-color'] = str_replace( '#', '', $_POST['text-color'] );
  206. $color = preg_replace('/[^0-9a-fA-F]/', '', $_POST['text-color']);
  207. if ( strlen($color) == 6 || strlen($color) == 3 )
  208. set_theme_mod('header_textcolor', $color);
  209. elseif ( ! $color )
  210. set_theme_mod( 'header_textcolor', 'blank' );
  211. }
  212. if ( isset( $_POST['default-header'] ) ) {
  213. check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
  214. $this->set_header_image( $_POST['default-header'] );
  215. return;
  216. }
  217. }
  218. /**
  219. * Process the default headers
  220. *
  221. * @since 3.0.0
  222. */
  223. function process_default_headers() {
  224. global $_wp_default_headers;
  225. if ( !empty($this->headers) )
  226. return;
  227. if ( !isset($_wp_default_headers) )
  228. return;
  229. $this->default_headers = $_wp_default_headers;
  230. $template_directory_uri = get_template_directory_uri();
  231. $stylesheet_directory_uri = get_stylesheet_directory_uri();
  232. foreach ( array_keys($this->default_headers) as $header ) {
  233. $this->default_headers[$header]['url'] = sprintf( $this->default_headers[$header]['url'], $template_directory_uri, $stylesheet_directory_uri );
  234. $this->default_headers[$header]['thumbnail_url'] = sprintf( $this->default_headers[$header]['thumbnail_url'], $template_directory_uri, $stylesheet_directory_uri );
  235. }
  236. }
  237. /**
  238. * Display UI for selecting one of several default headers.
  239. *
  240. * Show the random image option if this theme has multiple header images.
  241. * Random image option is on by default if no header has been set.
  242. *
  243. * @since 3.0.0
  244. */
  245. function show_header_selector( $type = 'default' ) {
  246. if ( 'default' == $type ) {
  247. $headers = $this->default_headers;
  248. } else {
  249. $headers = get_uploaded_header_images();
  250. $type = 'uploaded';
  251. }
  252. if ( 1 < count( $headers ) ) {
  253. echo '<div class="random-header">';
  254. echo '<label><input name="default-header" type="radio" value="random-' . $type . '-image"' . checked( is_random_header_image( $type ), true, false ) . ' />';
  255. echo __( '<strong>Random:</strong> Show a different image on each page.' );
  256. echo '</label>';
  257. echo '</div>';
  258. }
  259. echo '<div class="available-headers">';
  260. foreach ( $headers as $header_key => $header ) {
  261. $header_thumbnail = $header['thumbnail_url'];
  262. $header_url = $header['url'];
  263. $header_desc = empty( $header['description'] ) ? '' : $header['description'];
  264. echo '<div class="default-header">';
  265. echo '<label><input name="default-header" type="radio" value="' . esc_attr( $header_key ) . '" ' . checked( $header_url, get_theme_mod( 'header_image' ), false ) . ' />';
  266. $width = '';
  267. if ( !empty( $header['attachment_id'] ) )
  268. $width = ' width="230"';
  269. echo '<img src="' . set_url_scheme( $header_thumbnail ) . '" alt="' . esc_attr( $header_desc ) .'" title="' . esc_attr( $header_desc ) . '"' . $width . ' /></label>';
  270. echo '</div>';
  271. }
  272. echo '<div class="clear"></div></div>';
  273. }
  274. /**
  275. * Execute Javascript depending on step.
  276. *
  277. * @since 2.1.0
  278. */
  279. function js() {
  280. $step = $this->step();
  281. if ( ( 1 == $step || 3 == $step ) && current_theme_supports( 'custom-header', 'header-text' ) )
  282. $this->js_1();
  283. elseif ( 2 == $step )
  284. $this->js_2();
  285. }
  286. /**
  287. * Display Javascript based on Step 1 and 3.
  288. *
  289. * @since 2.6.0
  290. */
  291. function js_1() { ?>
  292. <script type="text/javascript">
  293. /* <![CDATA[ */
  294. (function($){
  295. var default_color = '#<?php echo get_theme_support( 'custom-header', 'default-text-color' ); ?>',
  296. header_text_fields;
  297. function pickColor(color) {
  298. $('#name').css('color', color);
  299. $('#desc').css('color', color);
  300. $('#text-color').val(color);
  301. }
  302. function toggle_text() {
  303. var checked = $('#display-header-text').prop('checked'),
  304. text_color;
  305. header_text_fields.toggle( checked );
  306. if ( ! checked )
  307. return;
  308. text_color = $('#text-color');
  309. if ( '' == text_color.val().replace('#', '') ) {
  310. text_color.val( default_color );
  311. pickColor( default_color );
  312. } else {
  313. pickColor( text_color.val() );
  314. }
  315. }
  316. $(document).ready(function() {
  317. var text_color = $('#text-color');
  318. header_text_fields = $('.displaying-header-text');
  319. text_color.wpColorPicker({
  320. change: function( event, ui ) {
  321. pickColor( text_color.wpColorPicker('color') );
  322. },
  323. clear: function() {
  324. pickColor( '' );
  325. }
  326. });
  327. $('#display-header-text').click( toggle_text );
  328. <?php if ( ! display_header_text() ) : ?>
  329. toggle_text();
  330. <?php endif; ?>
  331. });
  332. })(jQuery);
  333. /* ]]> */
  334. </script>
  335. <?php
  336. }
  337. /**
  338. * Display Javascript based on Step 2.
  339. *
  340. * @since 2.6.0
  341. */
  342. function js_2() { ?>
  343. <script type="text/javascript">
  344. /* <![CDATA[ */
  345. function onEndCrop( coords ) {
  346. jQuery( '#x1' ).val(coords.x);
  347. jQuery( '#y1' ).val(coords.y);
  348. jQuery( '#width' ).val(coords.w);
  349. jQuery( '#height' ).val(coords.h);
  350. }
  351. jQuery(document).ready(function() {
  352. var xinit = <?php echo absint( get_theme_support( 'custom-header', 'width' ) ); ?>;
  353. var yinit = <?php echo absint( get_theme_support( 'custom-header', 'height' ) ); ?>;
  354. var ratio = xinit / yinit;
  355. var ximg = jQuery('img#upload').width();
  356. var yimg = jQuery('img#upload').height();
  357. if ( yimg < yinit || ximg < xinit ) {
  358. if ( ximg / yimg > ratio ) {
  359. yinit = yimg;
  360. xinit = yinit * ratio;
  361. } else {
  362. xinit = ximg;
  363. yinit = xinit / ratio;
  364. }
  365. }
  366. jQuery('img#upload').imgAreaSelect({
  367. handles: true,
  368. keys: true,
  369. show: true,
  370. x1: 0,
  371. y1: 0,
  372. x2: xinit,
  373. y2: yinit,
  374. <?php
  375. if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' ) ) {
  376. ?>
  377. aspectRatio: xinit + ':' + yinit,
  378. <?php
  379. }
  380. if ( ! current_theme_supports( 'custom-header', 'flex-height' ) ) {
  381. ?>
  382. maxHeight: <?php echo get_theme_support( 'custom-header', 'height' ); ?>,
  383. <?php
  384. }
  385. if ( ! current_theme_supports( 'custom-header', 'flex-width' ) ) {
  386. ?>
  387. maxWidth: <?php echo get_theme_support( 'custom-header', 'width' ); ?>,
  388. <?php
  389. }
  390. ?>
  391. onInit: function () {
  392. jQuery('#width').val(xinit);
  393. jQuery('#height').val(yinit);
  394. },
  395. onSelectChange: function(img, c) {
  396. jQuery('#x1').val(c.x1);
  397. jQuery('#y1').val(c.y1);
  398. jQuery('#width').val(c.width);
  399. jQuery('#height').val(c.height);
  400. }
  401. });
  402. });
  403. /* ]]> */
  404. </script>
  405. <?php
  406. }
  407. /**
  408. * Display first step of custom header image page.
  409. *
  410. * @since 2.1.0
  411. */
  412. function step_1() {
  413. $this->process_default_headers();
  414. ?>
  415. <div class="wrap">
  416. <?php screen_icon(); ?>
  417. <h2><?php _e('Custom Header'); ?></h2>
  418. <?php if ( ! empty( $this->updated ) ) { ?>
  419. <div id="message" class="updated">
  420. <p><?php printf( __( 'Header updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) ); ?></p>
  421. </div>
  422. <?php } ?>
  423. <h3><?php _e( 'Header Image' ); ?></h3>
  424. <table class="form-table">
  425. <tbody>
  426. <tr valign="top">
  427. <th scope="row"><?php _e( 'Preview' ); ?></th>
  428. <td>
  429. <?php if ( $this->admin_image_div_callback ) {
  430. call_user_func( $this->admin_image_div_callback );
  431. } else {
  432. $custom_header = get_custom_header();
  433. $header_image_style = 'background-image:url(' . esc_url( get_header_image() ) . ');';
  434. if ( $custom_header->width )
  435. $header_image_style .= 'max-width:' . $custom_header->width . 'px;';
  436. if ( $custom_header->height )
  437. $header_image_style .= 'height:' . $custom_header->height . 'px;';
  438. ?>
  439. <div id="headimg" style="<?php echo $header_image_style; ?>">
  440. <?php
  441. if ( display_header_text() )
  442. $style = ' style="color:#' . get_header_textcolor() . ';"';
  443. else
  444. $style = ' style="display:none;"';
  445. ?>
  446. <h1><a id="name" class="displaying-header-text" <?php echo $style; ?> onclick="return false;" href="<?php bloginfo('url'); ?>"><?php bloginfo( 'name' ); ?></a></h1>
  447. <div id="desc" class="displaying-header-text" <?php echo $style; ?>><?php bloginfo( 'description' ); ?></div>
  448. </div>
  449. <?php } ?>
  450. </td>
  451. </tr>
  452. <?php if ( current_theme_supports( 'custom-header', 'uploads' ) ) : ?>
  453. <tr valign="top">
  454. <th scope="row"><?php _e( 'Select Image' ); ?></th>
  455. <td>
  456. <p><?php _e( 'You can select an image to be shown at the top of your site by uploading from your computer or choosing from your media library. After selecting an image you will be able to crop it.' ); ?><br />
  457. <?php
  458. if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' ) ) {
  459. printf( __( 'Images of exactly <strong>%1$d &times; %2$d pixels</strong> will be used as-is.' ) . '<br />', get_theme_support( 'custom-header', 'width' ), get_theme_support( 'custom-header', 'height' ) );
  460. } elseif ( current_theme_supports( 'custom-header', 'flex-height' ) ) {
  461. if ( ! current_theme_supports( 'custom-header', 'flex-width' ) )
  462. printf( __( 'Images should be at least <strong>%1$d pixels</strong> wide.' ) . ' ', get_theme_support( 'custom-header', 'width' ) );
  463. } elseif ( current_theme_supports( 'custom-header', 'flex-width' ) ) {
  464. if ( ! current_theme_supports( 'custom-header', 'flex-height' ) )
  465. printf( __( 'Images should be at least <strong>%1$d pixels</strong> tall.' ) . ' ', get_theme_support( 'custom-header', 'height' ) );
  466. }
  467. if ( current_theme_supports( 'custom-header', 'flex-height' ) || current_theme_supports( 'custom-header', 'flex-width' ) ) {
  468. if ( current_theme_supports( 'custom-header', 'width' ) )
  469. printf( __( 'Suggested width is <strong>%1$d pixels</strong>.' ) . ' ', get_theme_support( 'custom-header', 'width' ) );
  470. if ( current_theme_supports( 'custom-header', 'height' ) )
  471. printf( __( 'Suggested height is <strong>%1$d pixels</strong>.' ) . ' ', get_theme_support( 'custom-header', 'height' ) );
  472. }
  473. ?></p>
  474. <form enctype="multipart/form-data" id="upload-form" method="post" action="<?php echo esc_attr( add_query_arg( 'step', 2 ) ) ?>">
  475. <p>
  476. <label for="upload"><?php _e( 'Choose an image from your computer:' ); ?></label><br />
  477. <input type="file" id="upload" name="import" />
  478. <input type="hidden" name="action" value="save" />
  479. <?php wp_nonce_field( 'custom-header-upload', '_wpnonce-custom-header-upload' ); ?>
  480. <?php submit_button( __( 'Upload' ), 'small', 'submit', false ); ?>
  481. </p>
  482. <?php
  483. $image_library_url = get_upload_iframe_src( 'image', null, 'library' );
  484. $image_library_url = remove_query_arg( 'TB_iframe', $image_library_url );
  485. $image_library_url = add_query_arg( array( 'context' => 'custom-header', 'TB_iframe' => 1 ), $image_library_url );
  486. ?>
  487. <p>
  488. <label for="choose-from-library-link"><?php _e( 'Or choose an image from your media library:' ); ?></label><br />
  489. <a id="choose-from-library-link" class="button thickbox" href="<?php echo esc_url( $image_library_url ); ?>"><?php _e( 'Choose Image' ); ?></a>
  490. </p>
  491. </form>
  492. </td>
  493. </tr>
  494. <?php endif; ?>
  495. </tbody>
  496. </table>
  497. <form method="post" action="<?php echo esc_attr( add_query_arg( 'step', 1 ) ) ?>">
  498. <table class="form-table">
  499. <tbody>
  500. <?php if ( get_uploaded_header_images() ) : ?>
  501. <tr valign="top">
  502. <th scope="row"><?php _e( 'Uploaded Images' ); ?></th>
  503. <td>
  504. <p><?php _e( 'You can choose one of your previously uploaded headers, or show a random one.' ) ?></p>
  505. <?php
  506. $this->show_header_selector( 'uploaded' );
  507. ?>
  508. </td>
  509. </tr>
  510. <?php endif;
  511. if ( ! empty( $this->default_headers ) ) : ?>
  512. <tr valign="top">
  513. <th scope="row"><?php _e( 'Default Images' ); ?></th>
  514. <td>
  515. <?php if ( current_theme_supports( 'custom-header', 'uploads' ) ) : ?>
  516. <p><?php _e( 'If you don&lsquo;t want to upload your own image, you can use one of these cool headers, or show a random one.' ) ?></p>
  517. <?php else: ?>
  518. <p><?php _e( 'You can use one of these cool headers or show a random one on each page.' ) ?></p>
  519. <?php endif; ?>
  520. <?php
  521. $this->show_header_selector( 'default' );
  522. ?>
  523. </td>
  524. </tr>
  525. <?php endif;
  526. if ( get_header_image() ) : ?>
  527. <tr valign="top">
  528. <th scope="row"><?php _e( 'Remove Image' ); ?></th>
  529. <td>
  530. <p><?php _e( 'This will remove the header image. You will not be able to restore any customizations.' ) ?></p>
  531. <?php submit_button( __( 'Remove Header Image' ), 'button', 'removeheader', false ); ?>
  532. </td>
  533. </tr>
  534. <?php endif;
  535. $default_image = get_theme_support( 'custom-header', 'default-image' );
  536. if ( $default_image && get_header_image() != $default_image ) : ?>
  537. <tr valign="top">
  538. <th scope="row"><?php _e( 'Reset Image' ); ?></th>
  539. <td>
  540. <p><?php _e( 'This will restore the original header image. You will not be able to restore any customizations.' ) ?></p>
  541. <?php submit_button( __( 'Restore Original Header Image' ), 'button', 'resetheader', false ); ?>
  542. </td>
  543. </tr>
  544. <?php endif; ?>
  545. </tbody>
  546. </table>
  547. <?php if ( current_theme_supports( 'custom-header', 'header-text' ) ) : ?>
  548. <h3><?php _e( 'Header Text' ); ?></h3>
  549. <table class="form-table">
  550. <tbody>
  551. <tr valign="top">
  552. <th scope="row"><?php _e( 'Header Text' ); ?></th>
  553. <td>
  554. <p>
  555. <label><input type="checkbox" name="display-header-text" id="display-header-text"<?php checked( display_header_text() ); ?> /> <?php _e( 'Show header text with your image.' ); ?></label>
  556. </p>
  557. </td>
  558. </tr>
  559. <tr valign="top" class="displaying-header-text">
  560. <th scope="row"><?php _e( 'Text Color' ); ?></th>
  561. <td>
  562. <p>
  563. <?php
  564. $header_textcolor = display_header_text() ? get_header_textcolor() : get_theme_support( 'custom-header', 'default-text-color' );
  565. $default_color = '';
  566. if ( current_theme_supports( 'custom-header', 'default-text-color' ) )
  567. $default_color = ' data-default-color="#' . esc_attr( get_theme_support( 'custom-header', 'default-text-color' ) ) . '"';
  568. ?>
  569. <input type="text" name="text-color" id="text-color" value="#<?php echo esc_attr( $header_textcolor ); ?>"<?php echo $default_color; ?> />
  570. </p>
  571. </td>
  572. </tr>
  573. <?php if ( current_theme_supports( 'custom-header', 'default-text-color' ) && get_theme_mod( 'header_textcolor' ) ) { ?>
  574. <tr valign="top">
  575. <th scope="row"><?php _e('Reset Text Color'); ?></th>
  576. <td>
  577. <p><?php _e( 'This will restore the original header text. You will not be able to restore any customizations.' ) ?></p>
  578. <?php submit_button( __( 'Restore Original Header Text' ), 'button', 'resettext', false ); ?>
  579. </td>
  580. </tr>
  581. <?php } ?>
  582. </tbody>
  583. </table>
  584. <?php endif;
  585. do_action( 'custom_header_options' );
  586. wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' ); ?>
  587. <?php submit_button( null, 'primary', 'save-header-options' ); ?>
  588. </form>
  589. </div>
  590. <?php }
  591. /**
  592. * Display second step of custom header image page.
  593. *
  594. * @since 2.1.0
  595. */
  596. function step_2() {
  597. check_admin_referer('custom-header-upload', '_wpnonce-custom-header-upload');
  598. if ( ! current_theme_supports( 'custom-header', 'uploads' ) )
  599. wp_die( __( 'Cheatin&#8217; uh?' ) );
  600. if ( empty( $_POST ) && isset( $_GET['file'] ) ) {
  601. $attachment_id = absint( $_GET['file'] );
  602. $file = get_attached_file( $attachment_id, true );
  603. $url = wp_get_attachment_image_src( $attachment_id, 'full');
  604. $url = $url[0];
  605. } elseif ( isset( $_POST ) ) {
  606. extract($this->step_2_manage_upload());
  607. }
  608. if ( file_exists( $file ) ) {
  609. list( $width, $height, $type, $attr ) = getimagesize( $file );
  610. } else {
  611. $data = wp_get_attachment_metadata( $attachment_id );
  612. $height = $data[ 'height' ];
  613. $width = $data[ 'width' ];
  614. unset( $data );
  615. }
  616. $max_width = 0;
  617. // For flex, limit size of image displayed to 1500px unless theme says otherwise
  618. if ( current_theme_supports( 'custom-header', 'flex-width' ) )
  619. $max_width = 1500;
  620. if ( current_theme_supports( 'custom-header', 'max-width' ) )
  621. $max_width = max( $max_width, get_theme_support( 'custom-header', 'max-width' ) );
  622. $max_width = max( $max_width, get_theme_support( 'custom-header', 'width' ) );
  623. // If flexible height isn't supported and the image is the exact right size
  624. if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' )
  625. && $width == get_theme_support( 'custom-header', 'width' ) && $height == get_theme_support( 'custom-header', 'height' ) )
  626. {
  627. // Add the meta-data
  628. if ( file_exists( $file ) )
  629. wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
  630. $this->set_header_image( compact( 'url', 'attachment_id', 'width', 'height' ) );
  631. do_action('wp_create_file_in_uploads', $file, $attachment_id); // For replication
  632. return $this->finished();
  633. } elseif ( $width > $max_width ) {
  634. $oitar = $width / $max_width;
  635. $image = wp_crop_image($attachment_id, 0, 0, $width, $height, $max_width, $height / $oitar, false, str_replace(basename($file), 'midsize-'.basename($file), $file));
  636. if ( ! $image || is_wp_error( $image ) )
  637. wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
  638. $image = apply_filters('wp_create_file_in_uploads', $image, $attachment_id); // For replication
  639. $url = str_replace(basename($url), basename($image), $url);
  640. $width = $width / $oitar;
  641. $height = $height / $oitar;
  642. } else {
  643. $oitar = 1;
  644. }
  645. ?>
  646. <div class="wrap">
  647. <?php screen_icon(); ?>
  648. <h2><?php _e( 'Crop Header Image' ); ?></h2>
  649. <form method="post" action="<?php echo esc_attr(add_query_arg('step', 3)); ?>">
  650. <p class="hide-if-no-js"><?php _e('Choose the part of the image you want to use as your header.'); ?></p>
  651. <p class="hide-if-js"><strong><?php _e( 'You need Javascript to choose a part of the image.'); ?></strong></p>
  652. <div id="crop_image" style="position: relative">
  653. <img src="<?php echo esc_url( $url ); ?>" id="upload" width="<?php echo $width; ?>" height="<?php echo $height; ?>" />
  654. </div>
  655. <input type="hidden" name="x1" id="x1" value="0"/>
  656. <input type="hidden" name="y1" id="y1" value="0"/>
  657. <input type="hidden" name="width" id="width" value="<?php echo esc_attr( $width ); ?>"/>
  658. <input type="hidden" name="height" id="height" value="<?php echo esc_attr( $height ); ?>"/>
  659. <input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo esc_attr( $attachment_id ); ?>" />
  660. <input type="hidden" name="oitar" id="oitar" value="<?php echo esc_attr( $oitar ); ?>" />
  661. <?php if ( empty( $_POST ) && isset( $_GET['file'] ) ) { ?>
  662. <input type="hidden" name="create-new-attachment" value="true" />
  663. <?php } ?>
  664. <?php wp_nonce_field( 'custom-header-crop-image' ) ?>
  665. <p class="submit">
  666. <?php submit_button( __( 'Crop and Publish' ), 'primary', 'submit', false ); ?>
  667. <?php
  668. if ( isset( $oitar ) && 1 == $oitar )
  669. submit_button( __( 'Skip Cropping, Publish Image as Is' ), 'secondary', 'skip-cropping', false );
  670. ?>
  671. </p>
  672. </form>
  673. </div>
  674. <?php
  675. }
  676. /**
  677. * Upload the file to be cropped in the second step.
  678. *
  679. * @since 3.4.0
  680. */
  681. function step_2_manage_upload() {
  682. $overrides = array('test_form' => false);
  683. $file = wp_handle_upload($_FILES['import'], $overrides);
  684. if ( isset($file['error']) )
  685. wp_die( $file['error'], __( 'Image Upload Error' ) );
  686. $url = $file['url'];
  687. $type = $file['type'];
  688. $file = $file['file'];
  689. $filename = basename($file);
  690. // Construct the object array
  691. $object = array(
  692. 'post_title' => $filename,
  693. 'post_content' => $url,
  694. 'post_mime_type' => $type,
  695. 'guid' => $url,
  696. 'context' => 'custom-header'
  697. );
  698. // Save the data
  699. $attachment_id = wp_insert_attachment( $object, $file );
  700. return compact( 'attachment_id', 'file', 'filename', 'url', 'type' );
  701. }
  702. /**
  703. * Display third step of custom header image page.
  704. *
  705. * @since 2.1.0
  706. */
  707. function step_3() {
  708. check_admin_referer( 'custom-header-crop-image' );
  709. if ( ! current_theme_supports( 'custom-header', 'uploads' ) )
  710. wp_die( __( 'Cheatin&#8217; uh?' ) );
  711. if ( $_POST['oitar'] > 1 ) {
  712. $_POST['x1'] = $_POST['x1'] * $_POST['oitar'];
  713. $_POST['y1'] = $_POST['y1'] * $_POST['oitar'];
  714. $_POST['width'] = $_POST['width'] * $_POST['oitar'];
  715. $_POST['height'] = $_POST['height'] * $_POST['oitar'];
  716. }
  717. $attachment_id = absint( $_POST['attachment_id'] );
  718. $original = get_attached_file($attachment_id);
  719. $max_width = 0;
  720. // For flex, limit size of image displayed to 1500px unless theme says otherwise
  721. if ( current_theme_supports( 'custom-header', 'flex-width' ) )
  722. $max_width = 1500;
  723. if ( current_theme_supports( 'custom-header', 'max-width' ) )
  724. $max_width = max( $max_width, get_theme_support( 'custom-header', 'max-width' ) );
  725. $max_width = max( $max_width, get_theme_support( 'custom-header', 'width' ) );
  726. if ( ( current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' ) ) || $_POST['width'] > $max_width )
  727. $dst_height = absint( $_POST['height'] * ( $max_width / $_POST['width'] ) );
  728. elseif ( current_theme_supports( 'custom-header', 'flex-height' ) && current_theme_supports( 'custom-header', 'flex-width' ) )
  729. $dst_height = absint( $_POST['height'] );
  730. else
  731. $dst_height = get_theme_support( 'custom-header', 'height' );
  732. if ( ( current_theme_supports( 'custom-header', 'flex-width' ) && ! current_theme_supports( 'custom-header', 'flex-height' ) ) || $_POST['width'] > $max_width )
  733. $dst_width = absint( $_POST['width'] * ( $max_width / $_POST['width'] ) );
  734. elseif ( current_theme_supports( 'custom-header', 'flex-width' ) && current_theme_supports( 'custom-header', 'flex-height' ) )
  735. $dst_width = absint( $_POST['width'] );
  736. else
  737. $dst_width = get_theme_support( 'custom-header', 'width' );
  738. if ( empty( $_POST['skip-cropping'] ) )
  739. $cropped = wp_crop_image( $attachment_id, (int) $_POST['x1'], (int) $_POST['y1'], (int) $_POST['width'], (int) $_POST['height'], $dst_width, $dst_height );
  740. elseif ( ! empty( $_POST['create-new-attachment'] ) )
  741. $cropped = _copy_image_file( $attachment_id );
  742. else
  743. $cropped = get_attached_file( $attachment_id );
  744. if ( ! $cropped || is_wp_error( $cropped ) )
  745. wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
  746. $cropped = apply_filters('wp_create_file_in_uploads', $cropped, $attachment_id); // For replication
  747. $parent = get_post($attachment_id);
  748. $parent_url = $parent->guid;
  749. $url = str_replace( basename( $parent_url ), basename( $cropped ), $parent_url );
  750. $size = @getimagesize( $cropped );
  751. $image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
  752. // Construct the object array
  753. $object = array(
  754. 'ID' => $attachment_id,
  755. 'post_title' => basename($cropped),
  756. 'post_content' => $url,
  757. 'post_mime_type' => $image_type,
  758. 'guid' => $url,
  759. 'context' => 'custom-header'
  760. );
  761. if ( ! empty( $_POST['create-new-attachment'] ) )
  762. unset( $object['ID'] );
  763. // Update the attachment
  764. $attachment_id = wp_insert_attachment( $object, $cropped );
  765. wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $cropped ) );
  766. $width = $dst_width;
  767. $height = $dst_height;
  768. $this->set_header_image( compact( 'url', 'attachment_id', 'width', 'height' ) );
  769. // cleanup
  770. $medium = str_replace( basename( $original ), 'midsize-' . basename( $original ), $original );
  771. if ( file_exists( $medium ) )
  772. @unlink( apply_filters( 'wp_delete_file', $medium ) );
  773. if ( empty( $_POST['create-new-attachment'] ) && empty( $_POST['skip-cropping'] ) )
  774. @unlink( apply_filters( 'wp_delete_file', $original ) );
  775. return $this->finished();
  776. }
  777. /**
  778. * Display last step of custom header image page.
  779. *
  780. * @since 2.1.0
  781. */
  782. function finished() {
  783. $this->updated = true;
  784. $this->step_1();
  785. }
  786. /**
  787. * Display the page based on the current step.
  788. *
  789. * @since 2.1.0
  790. */
  791. function admin_page() {
  792. if ( ! current_user_can('edit_theme_options') )
  793. wp_die(__('You do not have permission to customize headers.'));
  794. $step = $this->step();
  795. if ( 2 == $step )
  796. $this->step_2();
  797. elseif ( 3 == $step )
  798. $this->step_3();
  799. else
  800. $this->step_1();
  801. }
  802. /**
  803. * Replace default attachment actions with "Set as header" link.
  804. *
  805. * @since 3.4.0
  806. */
  807. function attachment_fields_to_edit( $form_fields, $post ) {
  808. $form_fields = array();
  809. $href = esc_url(add_query_arg(array(
  810. 'page' => 'custom-header',
  811. 'step' => 2,
  812. '_wpnonce-custom-header-upload' => wp_create_nonce('custom-header-upload'),
  813. 'file' => $post->ID
  814. ), admin_url('themes.php')));
  815. $form_fields['buttons'] = array( 'tr' => '<tr class="submit"><td></td><td><a data-location="' . $href . '" class="wp-set-header">' . __( 'Set as header' ) . '</a></td></tr>' );
  816. $form_fields['context'] = array( 'input' => 'hidden', 'value' => 'custom-header' );
  817. return $form_fields;
  818. }
  819. /**
  820. * Leave only "Media Library" tab in the uploader window.
  821. *
  822. * @since 3.4.0
  823. */
  824. function filter_upload_tabs() {
  825. return array( 'library' => __('Media Library') );
  826. }
  827. /**
  828. * Choose a header image, selected from existing uploaded and default headers,
  829. * or provide an array of uploaded header data (either new, or from media library).
  830. *
  831. * @param mixed $choice Which header image to select. Allows for values of 'random-default-image',
  832. * for randomly cycling among the default images; 'random-uploaded-image', for randomly cycling
  833. * among the uploaded images; the key of a default image registered for that theme; and
  834. * the key of an image uploaded for that theme (the basename of the URL).
  835. * Or an array of arguments: attachment_id, url, width, height. All are required.
  836. *
  837. * @since 3.4.0
  838. */
  839. final public function set_header_image( $choice ) {
  840. if ( is_array( $choice ) || is_object( $choice ) ) {
  841. $choice = (array) $choice;
  842. if ( ! isset( $choice['attachment_id'] ) || ! isset( $choice['url'] ) )
  843. return;
  844. $choice['url'] = esc_url_raw( $choice['url'] );
  845. $header_image_data = (object) array(
  846. 'attachment_id' => $choice['attachment_id'],
  847. 'url' => $choice['url'],
  848. 'thumbnail_url' => $choice['url'],
  849. 'height' => $choice['height'],
  850. 'width' => $choice['width'],
  851. );
  852. update_post_meta( $choice['attachment_id'], '_wp_attachment_is_custom_header', get_stylesheet() );
  853. set_theme_mod( 'header_image', $choice['url'] );
  854. set_theme_mod( 'header_image_data', $header_image_data );
  855. return;
  856. }
  857. if ( in_array( $choice, array( 'remove-header', 'random-default-image', 'random-uploaded-image' ) ) ) {
  858. set_theme_mod( 'header_image', $choice );
  859. remove_theme_mod( 'header_image_data' );
  860. return;
  861. }
  862. $uploaded = get_uploaded_header_images();
  863. if ( $uploaded && isset( $uploaded[ $choice ] ) ) {
  864. $header_image_data = $uploaded[ $choice ];
  865. } else {
  866. $this->process_default_headers();
  867. if ( isset( $this->default_headers[ $choice ] ) )
  868. $header_image_data = $this->default_headers[ $choice ];
  869. else
  870. return;
  871. }
  872. set_theme_mod( 'header_image', esc_url_raw( $header_image_data['url'] ) );
  873. set_theme_mod( 'header_image_data', $header_image_data );
  874. }
  875. /**
  876. * Remove a header image.
  877. *
  878. * @since 3.4.0
  879. */
  880. final public function remove_header_image() {
  881. return $this->set_header_image( 'remove-header' );
  882. }
  883. /**
  884. * Reset a header image to the default image for the theme.
  885. *
  886. * This method does not do anything if the theme does not have a default header image.
  887. *
  888. * @since 3.4.0
  889. */
  890. final public function reset_header_image() {
  891. $this->process_default_headers();
  892. $default = get_theme_support( 'custom-header', 'default-image' );
  893. if ( ! $default )
  894. return $this->remove_header_image();
  895. $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
  896. foreach ( $this->default_headers as $header => $details ) {
  897. if ( $details['url'] == $default ) {
  898. $default_data = $details;
  899. break;
  900. }
  901. }
  902. set_theme_mod( 'header_image', $default );
  903. set_theme_mod( 'header_image_data', (object) $default_data );
  904. }
  905. }