PageRenderTime 48ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/public/frontend/wp-admin/custom-header.php

https://bitbucket.org/floppyxyz/musical
PHP | 1052 lines | 728 code | 131 blank | 193 comment | 156 complexity | eb5f8bbdced7ca2c23491294156df9f0 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('farbtastic');
  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('farbtastic');
  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. var farbtastic;
  295. (function($){
  296. var default_color = '#<?php echo get_theme_support( 'custom-header', 'default-text-color' ); ?>',
  297. header_text_fields;
  298. function pickColor(color) {
  299. $('#name').css('color', color);
  300. $('#desc').css('color', color);
  301. $('#text-color').val(color);
  302. farbtastic.setColor(color);
  303. }
  304. function toggle_text() {
  305. var checked = $('#display-header-text').prop('checked'),
  306. text_color;
  307. header_text_fields.toggle( checked );
  308. if ( ! checked )
  309. return;
  310. text_color = $('#text-color');
  311. if ( '' == text_color.val().replace('#', '') ) {
  312. text_color.val( default_color );
  313. pickColor( default_color );
  314. } else {
  315. pickColor( text_color.val() );
  316. }
  317. }
  318. $(document).ready(function() {
  319. header_text_fields = $('.displaying-header-text');
  320. $('#pickcolor').click(function(e) {
  321. e.preventDefault();
  322. $('#color-picker').show();
  323. });
  324. $('#display-header-text').click( toggle_text );
  325. $('#defaultcolor').click(function() {
  326. pickColor(default_color);
  327. $('#text-color').val(default_color);
  328. });
  329. $('#text-color').keyup(function() {
  330. var _hex = $('#text-color').val();
  331. var hex = _hex;
  332. if ( hex[0] != '#' )
  333. hex = '#' + hex;
  334. hex = hex.replace(/[^#a-fA-F0-9]+/, '');
  335. if ( hex != _hex )
  336. $('#text-color').val(hex);
  337. if ( hex.length == 4 || hex.length == 7 )
  338. pickColor( hex );
  339. });
  340. $(document).mousedown(function(){
  341. $('#color-picker').each( function() {
  342. var display = $(this).css('display');
  343. if (display == 'block')
  344. $(this).fadeOut(2);
  345. });
  346. });
  347. farbtastic = $.farbtastic('#color-picker', function(color) { pickColor(color); });
  348. <?php if ( display_header_text() ) { ?>
  349. pickColor('#<?php echo get_header_textcolor(); ?>');
  350. <?php } else { ?>
  351. toggle_text();
  352. <?php } ?>
  353. });
  354. })(jQuery);
  355. /* ]]> */
  356. </script>
  357. <?php
  358. }
  359. /**
  360. * Display Javascript based on Step 2.
  361. *
  362. * @since 2.6.0
  363. */
  364. function js_2() { ?>
  365. <script type="text/javascript">
  366. /* <![CDATA[ */
  367. function onEndCrop( coords ) {
  368. jQuery( '#x1' ).val(coords.x);
  369. jQuery( '#y1' ).val(coords.y);
  370. jQuery( '#width' ).val(coords.w);
  371. jQuery( '#height' ).val(coords.h);
  372. }
  373. jQuery(document).ready(function() {
  374. var xinit = <?php echo absint( get_theme_support( 'custom-header', 'width' ) ); ?>;
  375. var yinit = <?php echo absint( get_theme_support( 'custom-header', 'height' ) ); ?>;
  376. var ratio = xinit / yinit;
  377. var ximg = jQuery('img#upload').width();
  378. var yimg = jQuery('img#upload').height();
  379. if ( yimg < yinit || ximg < xinit ) {
  380. if ( ximg / yimg > ratio ) {
  381. yinit = yimg;
  382. xinit = yinit * ratio;
  383. } else {
  384. xinit = ximg;
  385. yinit = xinit / ratio;
  386. }
  387. }
  388. jQuery('img#upload').imgAreaSelect({
  389. handles: true,
  390. keys: true,
  391. show: true,
  392. x1: 0,
  393. y1: 0,
  394. x2: xinit,
  395. y2: yinit,
  396. <?php
  397. if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' ) ) {
  398. ?>
  399. aspectRatio: xinit + ':' + yinit,
  400. <?php
  401. }
  402. if ( ! current_theme_supports( 'custom-header', 'flex-height' ) ) {
  403. ?>
  404. maxHeight: <?php echo get_theme_support( 'custom-header', 'height' ); ?>,
  405. <?php
  406. }
  407. if ( ! current_theme_supports( 'custom-header', 'flex-width' ) ) {
  408. ?>
  409. maxWidth: <?php echo get_theme_support( 'custom-header', 'width' ); ?>,
  410. <?php
  411. }
  412. ?>
  413. onInit: function () {
  414. jQuery('#width').val(xinit);
  415. jQuery('#height').val(yinit);
  416. },
  417. onSelectChange: function(img, c) {
  418. jQuery('#x1').val(c.x1);
  419. jQuery('#y1').val(c.y1);
  420. jQuery('#width').val(c.width);
  421. jQuery('#height').val(c.height);
  422. }
  423. });
  424. });
  425. /* ]]> */
  426. </script>
  427. <?php
  428. }
  429. /**
  430. * Display first step of custom header image page.
  431. *
  432. * @since 2.1.0
  433. */
  434. function step_1() {
  435. $this->process_default_headers();
  436. ?>
  437. <div class="wrap">
  438. <?php screen_icon(); ?>
  439. <h2><?php _e('Custom Header'); ?></h2>
  440. <?php if ( ! empty( $this->updated ) ) { ?>
  441. <div id="message" class="updated">
  442. <p><?php printf( __( 'Header updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) ); ?></p>
  443. </div>
  444. <?php } ?>
  445. <h3><?php _e( 'Header Image' ); ?></h3>
  446. <table class="form-table">
  447. <tbody>
  448. <tr valign="top">
  449. <th scope="row"><?php _e( 'Preview' ); ?></th>
  450. <td>
  451. <?php if ( $this->admin_image_div_callback ) {
  452. call_user_func( $this->admin_image_div_callback );
  453. } else {
  454. ?>
  455. <div id="headimg" style="background-image:url(<?php esc_url ( header_image() ) ?>);max-width:<?php echo get_custom_header()->width; ?>px;height:<?php echo get_custom_header()->height; ?>px;">
  456. <?php
  457. if ( display_header_text() )
  458. $style = ' style="color:#' . get_header_textcolor() . ';"';
  459. else
  460. $style = ' style="display:none;"';
  461. ?>
  462. <h1><a id="name" class="displaying-header-text" <?php echo $style; ?> onclick="return false;" href="<?php bloginfo('url'); ?>"><?php bloginfo( 'name' ); ?></a></h1>
  463. <div id="desc" class="displaying-header-text" <?php echo $style; ?>><?php bloginfo( 'description' ); ?></div>
  464. </div>
  465. <?php } ?>
  466. </td>
  467. </tr>
  468. <?php if ( current_theme_supports( 'custom-header', 'uploads' ) ) : ?>
  469. <tr valign="top">
  470. <th scope="row"><?php _e( 'Select Image' ); ?></th>
  471. <td>
  472. <p><?php _e( 'You can upload a custom header image to be shown at the top of your site instead of the default one. On the next screen you will be able to crop the image.' ); ?><br />
  473. <?php
  474. if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' ) ) {
  475. 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' ) );
  476. } elseif ( current_theme_supports( 'custom-header', 'flex-height' ) ) {
  477. if ( ! current_theme_supports( 'custom-header', 'flex-width' ) )
  478. printf( __( 'Images should be at least <strong>%1$d pixels</strong> wide.' ) . ' ', get_theme_support( 'custom-header', 'width' ) );
  479. } elseif ( current_theme_supports( 'custom-header', 'flex-width' ) ) {
  480. if ( ! current_theme_supports( 'custom-header', 'flex-height' ) )
  481. printf( __( 'Images should be at least <strong>%1$d pixels</strong> tall.' ) . ' ', get_theme_support( 'custom-header', 'height' ) );
  482. }
  483. if ( current_theme_supports( 'custom-header', 'flex-height' ) || current_theme_supports( 'custom-header', 'flex-width' ) ) {
  484. if ( current_theme_supports( 'custom-header', 'width' ) )
  485. printf( __( 'Suggested width is <strong>%1$d pixels</strong>.' ) . ' ', get_theme_support( 'custom-header', 'width' ) );
  486. if ( current_theme_supports( 'custom-header', 'height' ) )
  487. printf( __( 'Suggested height is <strong>%1$d pixels</strong>.' ) . ' ', get_theme_support( 'custom-header', 'height' ) );
  488. }
  489. ?></p>
  490. <form enctype="multipart/form-data" id="upload-form" method="post" action="<?php echo esc_attr( add_query_arg( 'step', 2 ) ) ?>">
  491. <p>
  492. <label for="upload"><?php _e( 'Choose an image from your computer:' ); ?></label><br />
  493. <input type="file" id="upload" name="import" />
  494. <input type="hidden" name="action" value="save" />
  495. <?php wp_nonce_field( 'custom-header-upload', '_wpnonce-custom-header-upload' ); ?>
  496. <?php submit_button( __( 'Upload' ), 'button', 'submit', false ); ?>
  497. </p>
  498. <?php
  499. $image_library_url = get_upload_iframe_src( 'image', null, 'library' );
  500. $image_library_url = remove_query_arg( 'TB_iframe', $image_library_url );
  501. $image_library_url = add_query_arg( array( 'context' => 'custom-header', 'TB_iframe' => 1 ), $image_library_url );
  502. ?>
  503. <p>
  504. <label for="choose-from-library-link"><?php _e( 'Or choose an image from your media library:' ); ?></label><br />
  505. <a id="choose-from-library-link" class="button thickbox" href="<?php echo esc_url( $image_library_url ); ?>"><?php _e( 'Choose Image' ); ?></a>
  506. </p>
  507. </form>
  508. </td>
  509. </tr>
  510. <?php endif; ?>
  511. </tbody>
  512. </table>
  513. <form method="post" action="<?php echo esc_attr( add_query_arg( 'step', 1 ) ) ?>">
  514. <table class="form-table">
  515. <tbody>
  516. <?php if ( get_uploaded_header_images() ) : ?>
  517. <tr valign="top">
  518. <th scope="row"><?php _e( 'Uploaded Images' ); ?></th>
  519. <td>
  520. <p><?php _e( 'You can choose one of your previously uploaded headers, or show a random one.' ) ?></p>
  521. <?php
  522. $this->show_header_selector( 'uploaded' );
  523. ?>
  524. </td>
  525. </tr>
  526. <?php endif;
  527. if ( ! empty( $this->default_headers ) ) : ?>
  528. <tr valign="top">
  529. <th scope="row"><?php _e( 'Default Images' ); ?></th>
  530. <td>
  531. <?php if ( current_theme_supports( 'custom-header', 'uploads' ) ) : ?>
  532. <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>
  533. <?php else: ?>
  534. <p><?php _e( 'You can use one of these cool headers or show a random one on each page.' ) ?></p>
  535. <?php endif; ?>
  536. <?php
  537. $this->show_header_selector( 'default' );
  538. ?>
  539. </td>
  540. </tr>
  541. <?php endif;
  542. if ( get_header_image() ) : ?>
  543. <tr valign="top">
  544. <th scope="row"><?php _e( 'Remove Image' ); ?></th>
  545. <td>
  546. <p><?php _e( 'This will remove the header image. You will not be able to restore any customizations.' ) ?></p>
  547. <?php submit_button( __( 'Remove Header Image' ), 'button', 'removeheader', false ); ?>
  548. </td>
  549. </tr>
  550. <?php endif;
  551. $default_image = get_theme_support( 'custom-header', 'default-image' );
  552. if ( $default_image && get_header_image() != $default_image ) : ?>
  553. <tr valign="top">
  554. <th scope="row"><?php _e( 'Reset Image' ); ?></th>
  555. <td>
  556. <p><?php _e( 'This will restore the original header image. You will not be able to restore any customizations.' ) ?></p>
  557. <?php submit_button( __( 'Restore Original Header Image' ), 'button', 'resetheader', false ); ?>
  558. </td>
  559. </tr>
  560. <?php endif; ?>
  561. </tbody>
  562. </table>
  563. <?php if ( current_theme_supports( 'custom-header', 'header-text' ) ) : ?>
  564. <h3><?php _e( 'Header Text' ); ?></h3>
  565. <table class="form-table">
  566. <tbody>
  567. <tr valign="top">
  568. <th scope="row"><?php _e( 'Header Text' ); ?></th>
  569. <td>
  570. <p>
  571. <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>
  572. </p>
  573. </td>
  574. </tr>
  575. <tr valign="top" class="displaying-header-text">
  576. <th scope="row"><?php _e( 'Text Color' ); ?></th>
  577. <td>
  578. <p>
  579. <?php if ( display_header_text() ) : ?>
  580. <input type="text" name="text-color" id="text-color" value="#<?php echo esc_attr( get_header_textcolor() ); ?>" />
  581. <?php else : ?>
  582. <input type="text" name="text-color" id="text-color" value="#<?php echo esc_attr( get_theme_support( 'custom-header', 'default-text-color' ) ); ?>" />
  583. <?php endif; ?>
  584. <a href="#" class="hide-if-no-js" id="pickcolor"><?php _e( 'Select a Color' ); ?></a>
  585. </p>
  586. <div id="color-picker" style="z-index: 100; background:#eee; border:1px solid #ccc; position:absolute; display:none;"></div>
  587. </td>
  588. </tr>
  589. <?php if ( current_theme_supports( 'custom-header', 'default-text-color' ) && get_theme_mod( 'header_textcolor' ) ) { ?>
  590. <tr valign="top">
  591. <th scope="row"><?php _e('Reset Text Color'); ?></th>
  592. <td>
  593. <p><?php _e( 'This will restore the original header text. You will not be able to restore any customizations.' ) ?></p>
  594. <?php submit_button( __( 'Restore Original Header Text' ), 'button', 'resettext', false ); ?>
  595. </td>
  596. </tr>
  597. <?php } ?>
  598. </tbody>
  599. </table>
  600. <?php endif;
  601. do_action( 'custom_header_options' );
  602. wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' ); ?>
  603. <?php submit_button( null, 'primary', 'save-header-options' ); ?>
  604. </form>
  605. </div>
  606. <?php }
  607. /**
  608. * Display second step of custom header image page.
  609. *
  610. * @since 2.1.0
  611. */
  612. function step_2() {
  613. check_admin_referer('custom-header-upload', '_wpnonce-custom-header-upload');
  614. if ( ! current_theme_supports( 'custom-header', 'uploads' ) )
  615. wp_die( __( 'Cheatin&#8217; uh?' ) );
  616. if ( empty( $_POST ) && isset( $_GET['file'] ) ) {
  617. $attachment_id = absint( $_GET['file'] );
  618. $file = get_attached_file( $attachment_id, true );
  619. $url = wp_get_attachment_image_src( $attachment_id, 'full');
  620. $url = $url[0];
  621. } elseif ( isset( $_POST ) ) {
  622. extract($this->step_2_manage_upload());
  623. }
  624. if ( file_exists( $file ) ) {
  625. list( $width, $height, $type, $attr ) = getimagesize( $file );
  626. } else {
  627. $data = wp_get_attachment_metadata( $attachment_id );
  628. $height = $data[ 'height' ];
  629. $width = $data[ 'width' ];
  630. unset( $data );
  631. }
  632. $max_width = 0;
  633. // For flex, limit size of image displayed to 1500px unless theme says otherwise
  634. if ( current_theme_supports( 'custom-header', 'flex-width' ) )
  635. $max_width = 1500;
  636. if ( current_theme_supports( 'custom-header', 'max-width' ) )
  637. $max_width = max( $max_width, get_theme_support( 'custom-header', 'max-width' ) );
  638. $max_width = max( $max_width, get_theme_support( 'custom-header', 'width' ) );
  639. // If flexible height isn't supported and the image is the exact right size
  640. if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' )
  641. && $width == get_theme_support( 'custom-header', 'width' ) && $height == get_theme_support( 'custom-header', 'height' ) )
  642. {
  643. // Add the meta-data
  644. if ( file_exists( $file ) )
  645. wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
  646. $this->set_header_image( compact( 'url', 'attachment_id', 'width', 'height' ) );
  647. do_action('wp_create_file_in_uploads', $file, $attachment_id); // For replication
  648. return $this->finished();
  649. } elseif ( $width > $max_width ) {
  650. $oitar = $width / $max_width;
  651. $image = wp_crop_image($attachment_id, 0, 0, $width, $height, $max_width, $height / $oitar, false, str_replace(basename($file), 'midsize-'.basename($file), $file));
  652. if ( ! $image || is_wp_error( $image ) )
  653. wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
  654. $image = apply_filters('wp_create_file_in_uploads', $image, $attachment_id); // For replication
  655. $url = str_replace(basename($url), basename($image), $url);
  656. $width = $width / $oitar;
  657. $height = $height / $oitar;
  658. } else {
  659. $oitar = 1;
  660. }
  661. ?>
  662. <div class="wrap">
  663. <?php screen_icon(); ?>
  664. <h2><?php _e( 'Crop Header Image' ); ?></h2>
  665. <form method="post" action="<?php echo esc_attr(add_query_arg('step', 3)); ?>">
  666. <p class="hide-if-no-js"><?php _e('Choose the part of the image you want to use as your header.'); ?></p>
  667. <p class="hide-if-js"><strong><?php _e( 'You need Javascript to choose a part of the image.'); ?></strong></p>
  668. <div id="crop_image" style="position: relative">
  669. <img src="<?php echo esc_url( $url ); ?>" id="upload" width="<?php echo $width; ?>" height="<?php echo $height; ?>" />
  670. </div>
  671. <input type="hidden" name="x1" id="x1" value="0"/>
  672. <input type="hidden" name="y1" id="y1" value="0"/>
  673. <input type="hidden" name="width" id="width" value="<?php echo esc_attr( $width ); ?>"/>
  674. <input type="hidden" name="height" id="height" value="<?php echo esc_attr( $height ); ?>"/>
  675. <input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo esc_attr( $attachment_id ); ?>" />
  676. <input type="hidden" name="oitar" id="oitar" value="<?php echo esc_attr( $oitar ); ?>" />
  677. <?php if ( empty( $_POST ) && isset( $_GET['file'] ) ) { ?>
  678. <input type="hidden" name="create-new-attachment" value="true" />
  679. <?php } ?>
  680. <?php wp_nonce_field( 'custom-header-crop-image' ) ?>
  681. <p class="submit">
  682. <?php submit_button( __( 'Crop and Publish' ), 'primary', 'submit', false ); ?>
  683. <?php
  684. if ( isset( $oitar ) && 1 == $oitar )
  685. submit_button( __( 'Skip Cropping, Publish Image as Is' ), 'secondary', 'skip-cropping', false );
  686. ?>
  687. </p>
  688. </form>
  689. </div>
  690. <?php
  691. }
  692. /**
  693. * Upload the file to be cropped in the second step.
  694. *
  695. * @since 3.4.0
  696. */
  697. function step_2_manage_upload() {
  698. $overrides = array('test_form' => false);
  699. $file = wp_handle_upload($_FILES['import'], $overrides);
  700. if ( isset($file['error']) )
  701. wp_die( $file['error'], __( 'Image Upload Error' ) );
  702. $url = $file['url'];
  703. $type = $file['type'];
  704. $file = $file['file'];
  705. $filename = basename($file);
  706. // Construct the object array
  707. $object = array(
  708. 'post_title' => $filename,
  709. 'post_content' => $url,
  710. 'post_mime_type' => $type,
  711. 'guid' => $url,
  712. 'context' => 'custom-header'
  713. );
  714. // Save the data
  715. $attachment_id = wp_insert_attachment( $object, $file );
  716. return compact( 'attachment_id', 'file', 'filename', 'url', 'type' );
  717. }
  718. /**
  719. * Display third step of custom header image page.
  720. *
  721. * @since 2.1.0
  722. */
  723. function step_3() {
  724. check_admin_referer( 'custom-header-crop-image' );
  725. if ( ! current_theme_supports( 'custom-header', 'uploads' ) )
  726. wp_die( __( 'Cheatin&#8217; uh?' ) );
  727. if ( $_POST['oitar'] > 1 ) {
  728. $_POST['x1'] = $_POST['x1'] * $_POST['oitar'];
  729. $_POST['y1'] = $_POST['y1'] * $_POST['oitar'];
  730. $_POST['width'] = $_POST['width'] * $_POST['oitar'];
  731. $_POST['height'] = $_POST['height'] * $_POST['oitar'];
  732. }
  733. $attachment_id = absint( $_POST['attachment_id'] );
  734. $original = get_attached_file($attachment_id);
  735. $max_width = 0;
  736. // For flex, limit size of image displayed to 1500px unless theme says otherwise
  737. if ( current_theme_supports( 'custom-header', 'flex-width' ) )
  738. $max_width = 1500;
  739. if ( current_theme_supports( 'custom-header', 'max-width' ) )
  740. $max_width = max( $max_width, get_theme_support( 'custom-header', 'max-width' ) );
  741. $max_width = max( $max_width, get_theme_support( 'custom-header', 'width' ) );
  742. if ( ( current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' ) ) || $_POST['width'] > $max_width )
  743. $dst_height = absint( $_POST['height'] * ( $max_width / $_POST['width'] ) );
  744. elseif ( current_theme_supports( 'custom-header', 'flex-height' ) && current_theme_supports( 'custom-header', 'flex-width' ) )
  745. $dst_height = absint( $_POST['height'] );
  746. else
  747. $dst_height = get_theme_support( 'custom-header', 'height' );
  748. if ( ( current_theme_supports( 'custom-header', 'flex-width' ) && ! current_theme_supports( 'custom-header', 'flex-height' ) ) || $_POST['width'] > $max_width )
  749. $dst_width = absint( $_POST['width'] * ( $max_width / $_POST['width'] ) );
  750. elseif ( current_theme_supports( 'custom-header', 'flex-width' ) && current_theme_supports( 'custom-header', 'flex-height' ) )
  751. $dst_width = absint( $_POST['width'] );
  752. else
  753. $dst_width = get_theme_support( 'custom-header', 'width' );
  754. if ( empty( $_POST['skip-cropping'] ) )
  755. $cropped = wp_crop_image( $attachment_id, (int) $_POST['x1'], (int) $_POST['y1'], (int) $_POST['width'], (int) $_POST['height'], $dst_width, $dst_height );
  756. elseif ( ! empty( $_POST['create-new-attachment'] ) )
  757. $cropped = _copy_image_file( $attachment_id );
  758. else
  759. $cropped = get_attached_file( $attachment_id );
  760. if ( ! $cropped || is_wp_error( $cropped ) )
  761. wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
  762. $cropped = apply_filters('wp_create_file_in_uploads', $cropped, $attachment_id); // For replication
  763. $parent = get_post($attachment_id);
  764. $parent_url = $parent->guid;
  765. $url = str_replace( basename( $parent_url ), basename( $cropped ), $parent_url );
  766. $size = @getimagesize( $cropped );
  767. $image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
  768. // Construct the object array
  769. $object = array(
  770. 'ID' => $attachment_id,
  771. 'post_title' => basename($cropped),
  772. 'post_content' => $url,
  773. 'post_mime_type' => $image_type,
  774. 'guid' => $url,
  775. 'context' => 'custom-header'
  776. );
  777. if ( ! empty( $_POST['create-new-attachment'] ) )
  778. unset( $object['ID'] );
  779. // Update the attachment
  780. $attachment_id = wp_insert_attachment( $object, $cropped );
  781. wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $cropped ) );
  782. $width = $dst_width;
  783. $height = $dst_height;
  784. $this->set_header_image( compact( 'url', 'attachment_id', 'width', 'height' ) );
  785. // cleanup
  786. $medium = str_replace( basename( $original ), 'midsize-' . basename( $original ), $original );
  787. if ( file_exists( $medium ) )
  788. @unlink( apply_filters( 'wp_delete_file', $medium ) );
  789. if ( empty( $_POST['create-new-attachment'] ) && empty( $_POST['skip-cropping'] ) )
  790. @unlink( apply_filters( 'wp_delete_file', $original ) );
  791. return $this->finished();
  792. }
  793. /**
  794. * Display last step of custom header image page.
  795. *
  796. * @since 2.1.0
  797. */
  798. function finished() {
  799. $this->updated = true;
  800. $this->step_1();
  801. }
  802. /**
  803. * Display the page based on the current step.
  804. *
  805. * @since 2.1.0
  806. */
  807. function admin_page() {
  808. if ( ! current_user_can('edit_theme_options') )
  809. wp_die(__('You do not have permission to customize headers.'));
  810. $step = $this->step();
  811. if ( 2 == $step )
  812. $this->step_2();
  813. elseif ( 3 == $step )
  814. $this->step_3();
  815. else
  816. $this->step_1();
  817. }
  818. /**
  819. * Replace default attachment actions with "Set as header" link.
  820. *
  821. * @since 3.4.0
  822. */
  823. function attachment_fields_to_edit( $form_fields, $post ) {
  824. $form_fields = array();
  825. $href = esc_url(add_query_arg(array(
  826. 'page' => 'custom-header',
  827. 'step' => 2,
  828. '_wpnonce-custom-header-upload' => wp_create_nonce('custom-header-upload'),
  829. 'file' => $post->ID
  830. ), admin_url('themes.php')));
  831. $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>' );
  832. $form_fields['context'] = array( 'input' => 'hidden', 'value' => 'custom-header' );
  833. return $form_fields;
  834. }
  835. /**
  836. * Leave only "Media Library" tab in the uploader window.
  837. *
  838. * @since 3.4.0
  839. */
  840. function filter_upload_tabs() {
  841. return array( 'library' => __('Media Library') );
  842. }
  843. /**
  844. * Choose a header image, selected from existing uploaded and default headers,
  845. * or provide an array of uploaded header data (either new, or from media library).
  846. *
  847. * @param mixed $choice Which header image to select. Allows for values of 'random-default-image',
  848. * for randomly cycling among the default images; 'random-uploaded-image', for randomly cycling
  849. * among the uploaded images; the key of a default image registered for that theme; and
  850. * the key of an image uploaded for that theme (the basename of the URL).
  851. * Or an array of arguments: attachment_id, url, width, height. All are required.
  852. *
  853. * @since 3.4.0
  854. */
  855. final public function set_header_image( $choice ) {
  856. if ( is_array( $choice ) || is_object( $choice ) ) {
  857. $choice = (array) $choice;
  858. if ( ! isset( $choice['attachment_id'] ) || ! isset( $choice['url'] ) )
  859. return;
  860. $choice['url'] = esc_url_raw( $choice['url'] );
  861. $header_image_data = (object) array(
  862. 'attachment_id' => $choice['attachment_id'],
  863. 'url' => $choice['url'],
  864. 'thumbnail_url' => $choice['url'],
  865. 'height' => $choice['height'],
  866. 'width' => $choice['width'],
  867. );
  868. update_post_meta( $choice['attachment_id'], '_wp_attachment_is_custom_header', get_stylesheet() );
  869. set_theme_mod( 'header_image', $choice['url'] );
  870. set_theme_mod( 'header_image_data', $header_image_data );
  871. return;
  872. }
  873. if ( in_array( $choice, array( 'remove-header', 'random-default-image', 'random-uploaded-image' ) ) ) {
  874. set_theme_mod( 'header_image', $choice );
  875. remove_theme_mod( 'header_image_data' );
  876. return;
  877. }
  878. $uploaded = get_uploaded_header_images();
  879. if ( $uploaded && isset( $uploaded[ $choice ] ) ) {
  880. $header_image_data = $uploaded[ $choice ];
  881. } else {
  882. $this->process_default_headers();
  883. if ( isset( $this->default_headers[ $choice ] ) )
  884. $header_image_data = $this->default_headers[ $choice ];
  885. else
  886. return;
  887. }
  888. set_theme_mod( 'header_image', esc_url_raw( $header_image_data['url'] ) );
  889. set_theme_mod( 'header_image_data', $header_image_data );
  890. }
  891. /**
  892. * Remove a header image.
  893. *
  894. * @since 3.4.0
  895. */
  896. final public function remove_header_image() {
  897. return $this->set_header_image( 'remove-header' );
  898. }
  899. /**
  900. * Reset a header image to the default image for the theme.
  901. *
  902. * This method does not do anything if the theme does not have a default header image.
  903. *
  904. * @since 3.4.0
  905. */
  906. final public function reset_header_image() {
  907. $this->process_default_headers();
  908. $default = get_theme_support( 'custom-header', 'default-image' );
  909. if ( ! $default )
  910. return $this->remove_header_image();
  911. $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
  912. foreach ( $this->default_headers as $header => $details ) {
  913. if ( $details['url'] == $default ) {
  914. $default_data = $details;
  915. break;
  916. }
  917. }
  918. set_theme_mod( 'header_image', $default );
  919. set_theme_mod( 'header_image_data', (object) $default_data );
  920. }
  921. }