PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-admin/custom-header.php

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