2019-07-17 02:17:56 -04:00
< ? php
/**
* The custom header image script .
*
* @ package WordPress
* @ subpackage Administration
*/
/**
* The custom header image class .
*
* @ since 2.1 . 0
*/
Code Modernization: Add `AllowDynamicProperties` attribute to all (parent) classes.
Dynamic (non-explicitly declared) properties are deprecated as of PHP 8.2 and are expected to become a fatal error in PHP 9.0.
There are a number of ways to mitigate this:
* If it is an accidental typo for a declared property: fix the typo.
* For known properties: declare them on the class.
* For unknown properties: add the magic `__get()`, `__set()`, et al. methods to the class or let the class extend `stdClass` which has highly optimized versions of these magic methods built in.
* For unknown ''use'' of dynamic properties, the `#[AllowDynamicProperties]` attribute can be added to the class. The attribute will automatically be inherited by child classes.
Trac ticket #56034 is open to investigate and handle the third and fourth type of situations, however it has become clear this will need more time and will not be ready in time for WP 6.1.
To reduce “noise” in the meantime, both in the error logs of WP users moving onto PHP 8.2, in the test run logs of WP itself, in test runs of plugins and themes, as well as to prevent duplicate tickets from being opened for the same issue, this commit adds the `#[AllowDynamicProperties]` attribute to all “parent” classes in WP.
The logic used for this commit is as follows:
* If a class already has the attribute: no action needed.
* If a class does not `extend`: add the attribute.
* If a class does `extend`:
- If it extends `stdClass`: no action needed (as `stdClass` supports dynamic properties).
- If it extends a PHP native class: add the attribute.
- If it extends a class from one of WP's external dependencies: add the attribute.
* In all other cases: no action — the attribute should not be needed as child classes inherit from the parent.
Whether or not a class contains magic methods has not been taken into account, as a review of the currently existing magic methods has shown that those are generally not sturdy enough and often even set dynamic properties (which they should not). See the [https://www.youtube.com/watch?v=vDZWepDQQVE live stream from August 16, 2022] for more details.
This commit only affects classes in the `src` directory of WordPress core.
* Tests should not get this attribute, but should be fixed to not use dynamic properties instead. Patches for this are already being committed under ticket #56033.
* While a number bundled themes (2014, 2019, 2020, 2021) contain classes, they are not a part of this commit and may be updated separately.
Reference: [https://wiki.php.net/rfc/deprecate_dynamic_properties PHP RFC: Deprecate dynamic properties].
Follow-up to [53922].
Props jrf, hellofromTonya, markjaquith, peterwilsoncc, costdev, knutsp, aristath.
See #56513, #56034.
Built from https://develop.svn.wordpress.org/trunk@54133
git-svn-id: http://core.svn.wordpress.org/trunk@53692 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-12 11:47:14 -04:00
#[AllowDynamicProperties]
2019-07-17 02:17:56 -04:00
class Custom_Image_Header {
/**
* Callback for administration header .
*
* @ var callable
* @ since 2.1 . 0
*/
public $admin_header_callback ;
/**
* Callback for header div .
*
* @ var callable
* @ since 3.0 . 0
*/
public $admin_image_div_callback ;
/**
* Holds default headers .
*
* @ var array
* @ since 3.0 . 0
*/
public $default_headers = array ();
/**
* Used to trigger a success message when settings updated and set to true .
*
* @ since 3.0 . 0
* @ var bool
*/
private $updated ;
/**
2023-12-07 10:14:27 -05:00
* Constructor - Registers administration header callback .
2019-07-17 02:17:56 -04:00
*
* @ since 2.1 . 0
2023-12-07 10:14:27 -05:00
*
* @ param callable $admin_header_callback Administration header callback .
* @ param callable $admin_image_div_callback Optional . Custom image div output callback .
* Default empty string .
2019-07-17 02:17:56 -04:00
*/
public function __construct ( $admin_header_callback , $admin_image_div_callback = '' ) {
$this -> admin_header_callback = $admin_header_callback ;
$this -> admin_image_div_callback = $admin_image_div_callback ;
add_action ( 'admin_menu' , array ( $this , 'init' ) );
add_action ( 'customize_save_after' , array ( $this , 'customize_set_last_used' ) );
add_action ( 'wp_ajax_custom-header-crop' , array ( $this , 'ajax_header_crop' ) );
add_action ( 'wp_ajax_custom-header-add' , array ( $this , 'ajax_header_add' ) );
add_action ( 'wp_ajax_custom-header-remove' , array ( $this , 'ajax_header_remove' ) );
}
/**
2023-06-05 17:35:20 -04:00
* Sets up the hooks for the Custom Header admin page .
2019-07-17 02:17:56 -04:00
*
* @ since 2.1 . 0
*/
public function init () {
2023-06-05 17:05:22 -04:00
$page = add_theme_page (
_x ( 'Header' , 'custom image header' ),
_x ( 'Header' , 'custom image header' ),
'edit_theme_options' ,
'custom-header' ,
array ( $this , 'admin_page' )
);
2021-04-13 15:05:04 -04:00
2019-07-17 02:17:56 -04:00
if ( ! $page ) {
return ;
}
2019-08-22 15:08:55 -04:00
add_action ( " admin_print_scripts- { $page } " , array ( $this , 'js_includes' ) );
add_action ( " admin_print_styles- { $page } " , array ( $this , 'css_includes' ) );
add_action ( " admin_head- { $page } " , array ( $this , 'help' ) );
add_action ( " admin_head- { $page } " , array ( $this , 'take_action' ), 50 );
add_action ( " admin_head- { $page } " , array ( $this , 'js' ), 50 );
2021-04-13 15:05:04 -04:00
2019-07-17 02:17:56 -04:00
if ( $this -> admin_header_callback ) {
2019-08-22 15:08:55 -04:00
add_action ( " admin_head- { $page } " , $this -> admin_header_callback , 51 );
2019-07-17 02:17:56 -04:00
}
}
/**
* Adds contextual help .
*
* @ since 3.0 . 0
*/
public function help () {
get_current_screen () -> add_help_tab (
array (
'id' => 'overview' ,
'title' => __ ( 'Overview' ),
'content' =>
'<p>' . __ ( 'This screen is used to customize the header section of your theme.' ) . '</p>' .
'<p>' . __ ( 'You can choose from the theme’s default header images, or use one of your own. You can also customize how your Site Title and Tagline are displayed.' ) . '<p>' ,
)
);
get_current_screen () -> add_help_tab (
array (
'id' => 'set-header-image' ,
'title' => __ ( 'Header Image' ),
'content' =>
'<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 “Choose Image” button.' ) . '</p>' .
Administration: Replace contracted verb forms for better consistency.
This changeset replaces contracted verb forms like `doesn't`, `can't`, or `isn't` with non-contracted forms like `does not`, `cannot`, or `is not`, for better consistency across the WordPress administration. It also updates some corresponding unit tests strings.
Props Presskopp, socalchristina, aandrewdixon, francina, SergeyBiryukov, JeffPaul, audrasjb, hellofromTonya.
Fixes #38913.
See #39176.
Built from https://develop.svn.wordpress.org/trunk@52978
git-svn-id: http://core.svn.wordpress.org/trunk@52567 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-03-22 12:25:03 -04:00
'<p>' . __ ( 'Some themes come with additional header images bundled. If you see multiple images displayed, select the one you would like and click the “Save Changes” button.' ) . '</p>' .
2019-07-17 02:17:56 -04:00
'<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 “Random” radio button next to the Uploaded Images or Default Images section to enable this feature.' ) . '</p>' .
Administration: Replace contracted verb forms for better consistency.
This changeset replaces contracted verb forms like `doesn't`, `can't`, or `isn't` with non-contracted forms like `does not`, `cannot`, or `is not`, for better consistency across the WordPress administration. It also updates some corresponding unit tests strings.
Props Presskopp, socalchristina, aandrewdixon, francina, SergeyBiryukov, JeffPaul, audrasjb, hellofromTonya.
Fixes #38913.
See #39176.
Built from https://develop.svn.wordpress.org/trunk@52978
git-svn-id: http://core.svn.wordpress.org/trunk@52567 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-03-22 12:25:03 -04:00
'<p>' . __ ( 'If you do not want a header image to be displayed on your site at all, click the “Remove Header Image” 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 “Save Changes”.' ) . '</p>' ,
2019-07-17 02:17:56 -04:00
)
);
get_current_screen () -> add_help_tab (
array (
'id' => 'set-header-text' ,
'title' => __ ( 'Header Text' ),
'content' =>
I18N: Improve translator comments.
* Add missing translator comments.
* Fix placement of some translator comments. Translator comments should be on the line directly above the line containing the translation function call for optimal compatibility with various `.pot` file generation tools. The CS auto-fixing, which changed some inconsistent function calls to multi-line function calls, is part of the reason why this was no longer the case for a select group of translator comments.
Includes minor code layout fixes.
Polyglots, rejoice! All WordPress core files now have translator comments for all strings with placeholders!
Props jrf, subrataemfluence, GaryJ, webdados, Dency, swissspidy, alvarogois, marcomartins, mihaiiceyro, vladwtz, niq1982, flipkeijzer, michielatyoast, chandrapatel, thrijith, joshuanoyce, FesoVik, tessak22, bhaktirajdev, cleancoded, dhavalkasvala, garrett-eclipse, bibliofille, socalchristina, priyankkpatel, 5hel2l2y, adamsilverstein, JeffPaul, pierlo, SergeyBiryukov.
Fixes #44360.
Built from https://develop.svn.wordpress.org/trunk@45926
git-svn-id: http://core.svn.wordpress.org/trunk@45737 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2019-09-01 13:13:59 -04:00
'<p>' . sprintf (
2019-09-02 20:41:05 -04:00
/* translators: %s: URL to General Settings screen. */
I18N: Improve translator comments.
* Add missing translator comments.
* Fix placement of some translator comments. Translator comments should be on the line directly above the line containing the translation function call for optimal compatibility with various `.pot` file generation tools. The CS auto-fixing, which changed some inconsistent function calls to multi-line function calls, is part of the reason why this was no longer the case for a select group of translator comments.
Includes minor code layout fixes.
Polyglots, rejoice! All WordPress core files now have translator comments for all strings with placeholders!
Props jrf, subrataemfluence, GaryJ, webdados, Dency, swissspidy, alvarogois, marcomartins, mihaiiceyro, vladwtz, niq1982, flipkeijzer, michielatyoast, chandrapatel, thrijith, joshuanoyce, FesoVik, tessak22, bhaktirajdev, cleancoded, dhavalkasvala, garrett-eclipse, bibliofille, socalchristina, priyankkpatel, 5hel2l2y, adamsilverstein, JeffPaul, pierlo, SergeyBiryukov.
Fixes #44360.
Built from https://develop.svn.wordpress.org/trunk@45926
git-svn-id: http://core.svn.wordpress.org/trunk@45737 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2019-09-01 13:13:59 -04:00
__ ( 'For most themes, the header text is your Site Title and Tagline, as defined in the <a href="%s">General Settings</a> section.' ),
admin_url ( 'options-general.php' )
) .
'</p>' .
2019-07-17 02:17:56 -04:00
'<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. “#ff0000” for red, or by choosing a color using the color picker.' ) . '</p>' .
Administration: Replace contracted verb forms for better consistency.
This changeset replaces contracted verb forms like `doesn't`, `can't`, or `isn't` with non-contracted forms like `does not`, `cannot`, or `is not`, for better consistency across the WordPress administration. It also updates some corresponding unit tests strings.
Props Presskopp, socalchristina, aandrewdixon, francina, SergeyBiryukov, JeffPaul, audrasjb, hellofromTonya.
Fixes #38913.
See #39176.
Built from https://develop.svn.wordpress.org/trunk@52978
git-svn-id: http://core.svn.wordpress.org/trunk@52567 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-03-22 12:25:03 -04:00
'<p>' . __ ( 'Do not forget to click “Save Changes” when you are done!' ) . '</p>' ,
2019-07-17 02:17:56 -04:00
)
);
get_current_screen () -> set_help_sidebar (
'<p><strong>' . __ ( 'For more information:' ) . '</strong></p>' .
'<p>' . __ ( '<a href="https://codex.wordpress.org/Appearance_Header_Screen">Documentation on Custom Header</a>' ) . '</p>' .
2023-02-23 05:38:21 -05:00
'<p>' . __ ( '<a href="https://wordpress.org/support/forums/">Support forums</a>' ) . '</p>'
2019-07-17 02:17:56 -04:00
);
}
/**
2023-06-05 17:35:20 -04:00
* Gets the current step .
2019-07-17 02:17:56 -04:00
*
* @ since 2.6 . 0
*
2021-04-13 15:05:04 -04:00
* @ return int Current step .
2019-07-17 02:17:56 -04:00
*/
public function step () {
if ( ! isset ( $_GET [ 'step' ] ) ) {
return 1 ;
}
$step = ( int ) $_GET [ 'step' ];
if ( $step < 1 || 3 < $step ||
2021-04-13 15:05:04 -04:00
( 2 === $step && ! wp_verify_nonce ( $_REQUEST [ '_wpnonce-custom-header-upload' ], 'custom-header-upload' ) ) ||
( 3 === $step && ! wp_verify_nonce ( $_REQUEST [ '_wpnonce' ], 'custom-header-crop-image' ) )
2019-07-17 02:17:56 -04:00
) {
return 1 ;
}
return $step ;
}
/**
2023-06-05 17:35:20 -04:00
* Sets up the enqueue for the JavaScript files .
2019-07-17 02:17:56 -04:00
*
* @ since 2.1 . 0
*/
public function js_includes () {
$step = $this -> step ();
2021-04-13 15:05:04 -04:00
if ( ( 1 === $step || 3 === $step ) ) {
2019-07-17 02:17:56 -04:00
wp_enqueue_media ();
wp_enqueue_script ( 'custom-header' );
if ( current_theme_supports ( 'custom-header' , 'header-text' ) ) {
wp_enqueue_script ( 'wp-color-picker' );
}
2021-04-13 15:05:04 -04:00
} elseif ( 2 === $step ) {
2019-07-17 02:17:56 -04:00
wp_enqueue_script ( 'imgareaselect' );
}
}
/**
2023-06-05 17:35:20 -04:00
* Sets up the enqueue for the CSS files .
2019-07-17 02:17:56 -04:00
*
* @ since 2.7 . 0
*/
public function css_includes () {
$step = $this -> step ();
2021-04-13 15:05:04 -04:00
if ( ( 1 === $step || 3 === $step ) && current_theme_supports ( 'custom-header' , 'header-text' ) ) {
2019-07-17 02:17:56 -04:00
wp_enqueue_style ( 'wp-color-picker' );
2021-04-13 15:05:04 -04:00
} elseif ( 2 === $step ) {
2019-07-17 02:17:56 -04:00
wp_enqueue_style ( 'imgareaselect' );
}
}
/**
2023-06-05 17:35:20 -04:00
* Executes custom header modification .
2019-07-17 02:17:56 -04:00
*
* @ since 2.6 . 0
*/
public function take_action () {
if ( ! current_user_can ( 'edit_theme_options' ) ) {
return ;
}
if ( empty ( $_POST ) ) {
return ;
}
$this -> updated = true ;
if ( isset ( $_POST [ 'resetheader' ] ) ) {
check_admin_referer ( 'custom-header-options' , '_wpnonce-custom-header-options' );
2021-04-13 15:05:04 -04:00
2019-07-17 02:17:56 -04:00
$this -> reset_header_image ();
2021-04-13 15:05:04 -04:00
2019-07-17 02:17:56 -04:00
return ;
}
if ( isset ( $_POST [ 'removeheader' ] ) ) {
check_admin_referer ( 'custom-header-options' , '_wpnonce-custom-header-options' );
2021-04-13 15:05:04 -04:00
2019-07-17 02:17:56 -04:00
$this -> remove_header_image ();
2021-04-13 15:05:04 -04:00
2019-07-17 02:17:56 -04:00
return ;
}
if ( isset ( $_POST [ 'text-color' ] ) && ! isset ( $_POST [ 'display-header-text' ] ) ) {
check_admin_referer ( 'custom-header-options' , '_wpnonce-custom-header-options' );
2021-04-13 15:05:04 -04:00
2019-07-17 02:17:56 -04:00
set_theme_mod ( 'header_textcolor' , 'blank' );
} elseif ( isset ( $_POST [ 'text-color' ] ) ) {
check_admin_referer ( 'custom-header-options' , '_wpnonce-custom-header-options' );
2021-04-13 15:05:04 -04:00
2019-07-17 02:17:56 -04:00
$_POST [ 'text-color' ] = str_replace ( '#' , '' , $_POST [ 'text-color' ] );
2021-04-13 15:05:04 -04:00
$color = preg_replace ( '/[^0-9a-fA-F]/' , '' , $_POST [ 'text-color' ] );
if ( strlen ( $color ) === 6 || strlen ( $color ) === 3 ) {
2019-07-17 02:17:56 -04:00
set_theme_mod ( 'header_textcolor' , $color );
} elseif ( ! $color ) {
set_theme_mod ( 'header_textcolor' , 'blank' );
}
}
if ( isset ( $_POST [ 'default-header' ] ) ) {
check_admin_referer ( 'custom-header-options' , '_wpnonce-custom-header-options' );
2021-04-13 15:05:04 -04:00
2019-07-17 02:17:56 -04:00
$this -> set_header_image ( $_POST [ 'default-header' ] );
2021-04-13 15:05:04 -04:00
2019-07-17 02:17:56 -04:00
return ;
}
}
/**
2023-06-05 17:35:20 -04:00
* Processes the default headers .
2019-07-17 02:17:56 -04:00
*
* @ since 3.0 . 0
*
* @ global array $_wp_default_headers
*/
public function process_default_headers () {
global $_wp_default_headers ;
if ( ! isset ( $_wp_default_headers ) ) {
return ;
}
if ( ! empty ( $this -> default_headers ) ) {
return ;
}
$this -> default_headers = $_wp_default_headers ;
$template_directory_uri = get_template_directory_uri ();
$stylesheet_directory_uri = get_stylesheet_directory_uri ();
2021-04-13 15:05:04 -04:00
2019-07-17 02:17:56 -04:00
foreach ( array_keys ( $this -> default_headers ) as $header ) {
2021-04-13 15:05:04 -04:00
$this -> default_headers [ $header ][ 'url' ] = sprintf (
$this -> default_headers [ $header ][ 'url' ],
$template_directory_uri ,
$stylesheet_directory_uri
);
$this -> default_headers [ $header ][ 'thumbnail_url' ] = sprintf (
$this -> default_headers [ $header ][ 'thumbnail_url' ],
$template_directory_uri ,
$stylesheet_directory_uri
);
2019-07-17 02:17:56 -04:00
}
}
/**
2023-06-05 17:35:20 -04:00
* Displays UI for selecting one of several default headers .
2019-07-17 02:17:56 -04:00
*
2023-06-05 17:35:20 -04:00
* Shows the random image option if this theme has multiple header images .
2019-07-17 02:17:56 -04:00
* Random image option is on by default if no header has been set .
*
* @ since 3.0 . 0
*
* @ param string $type The header type . One of 'default' ( for the Uploaded Images control )
* or 'uploaded' ( for the Uploaded Images control ) .
*/
public function show_header_selector ( $type = 'default' ) {
2020-05-16 14:42:12 -04:00
if ( 'default' === $type ) {
2019-07-17 02:17:56 -04:00
$headers = $this -> default_headers ;
} else {
$headers = get_uploaded_header_images ();
$type = 'uploaded' ;
}
if ( 1 < count ( $headers ) ) {
echo '<div class="random-header">' ;
echo '<label><input name="default-header" type="radio" value="random-' . $type . '-image"' . checked ( is_random_header_image ( $type ), true , false ) . ' />' ;
_e ( '<strong>Random:</strong> Show a different image on each page.' );
echo '</label>' ;
echo '</div>' ;
}
echo '<div class="available-headers">' ;
2021-04-13 15:05:04 -04:00
2019-07-17 02:17:56 -04:00
foreach ( $headers as $header_key => $header ) {
$header_thumbnail = $header [ 'thumbnail_url' ];
$header_url = $header [ 'url' ];
$header_alt_text = empty ( $header [ 'alt_text' ] ) ? '' : $header [ 'alt_text' ];
2021-04-13 15:05:04 -04:00
2019-07-17 02:17:56 -04:00
echo '<div class="default-header">' ;
echo '<label><input name="default-header" type="radio" value="' . esc_attr ( $header_key ) . '" ' . checked ( $header_url , get_theme_mod ( 'header_image' ), false ) . ' />' ;
$width = '' ;
if ( ! empty ( $header [ 'attachment_id' ] ) ) {
$width = ' width="230"' ;
}
2023-02-06 16:36:17 -05:00
echo '<img src="' . esc_url ( set_url_scheme ( $header_thumbnail ) ) . '" alt="' . esc_attr ( $header_alt_text ) . '"' . $width . ' /></label>' ;
2019-07-17 02:17:56 -04:00
echo '</div>' ;
}
2021-04-13 15:05:04 -04:00
2019-07-17 02:17:56 -04:00
echo '<div class="clear"></div></div>' ;
}
/**
2023-06-05 17:35:20 -04:00
* Executes JavaScript depending on step .
2019-07-17 02:17:56 -04:00
*
* @ since 2.1 . 0
*/
public function js () {
$step = $this -> step ();
2021-04-13 15:05:04 -04:00
if ( ( 1 === $step || 3 === $step ) && current_theme_supports ( 'custom-header' , 'header-text' ) ) {
2019-07-17 02:17:56 -04:00
$this -> js_1 ();
2021-04-13 15:05:04 -04:00
} elseif ( 2 === $step ) {
2019-07-17 02:17:56 -04:00
$this -> js_2 ();
}
}
/**
2023-06-05 17:35:20 -04:00
* Displays JavaScript based on Step 1 and 3.
2019-07-17 02:17:56 -04:00
*
* @ since 2.6 . 0
*/
public function js_1 () {
$default_color = '' ;
if ( current_theme_supports ( 'custom-header' , 'default-text-color' ) ) {
$default_color = get_theme_support ( 'custom-header' , 'default-text-color' );
Code Modernization: Replace usage of `strpos()` with `str_contains()`.
`str_contains()` was introduced in PHP 8.0 to perform a case-sensitive check indicating if the string to search in (haystack) contains the given substring (needle).
WordPress core includes a polyfill for `str_contains()` on PHP < 8.0 as of WordPress 5.9.
This commit replaces `false !== strpos( ... )` with `str_contains()` in core files, making the code more readable and consistent, as well as better aligned with modern development practices.
Follow-up to [52039], [52040], [52326], [55703], [55710], [55987].
Props Soean, spacedmonkey, costdev, dingo_d, azaozz, mikeschroder, flixos90, peterwilsoncc, SergeyBiryukov.
Fixes #58206.
Built from https://develop.svn.wordpress.org/trunk@55988
git-svn-id: http://core.svn.wordpress.org/trunk@55500 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-06-22 10:36:26 -04:00
if ( $default_color && ! str_contains ( $default_color , '#' ) ) {
2019-07-17 02:17:56 -04:00
$default_color = '#' . $default_color ;
}
}
?>
< script type = " text/javascript " >
( function ( $ ){
2020-10-29 14:03:11 -04:00
var default_color = '<?php echo esc_js( $default_color ); ?>' ,
2019-07-17 02:17:56 -04:00
header_text_fields ;
function pickColor ( color ) {
$ ( '#name' ) . css ( 'color' , color );
$ ( '#desc' ) . css ( 'color' , color );
$ ( '#text-color' ) . val ( color );
}
function toggle_text () {
var checked = $ ( '#display-header-text' ) . prop ( 'checked' ),
text_color ;
header_text_fields . toggle ( checked );
if ( ! checked )
return ;
text_color = $ ( '#text-color' );
2020-05-16 14:42:12 -04:00
if ( '' === text_color . val () . replace ( '#' , '' ) ) {
2019-07-17 02:17:56 -04:00
text_color . val ( default_color );
pickColor ( default_color );
} else {
pickColor ( text_color . val () );
}
}
External Libraries: Further fix jQuery deprecations in WordPress core.
Follow-up to [50001], [50270], [50367], [50383], [50410], [50420], [50429], [50547].
Props chaion07, Clorith, costdev, desrosj, malthert, peterwilsoncc, presskopp, promz, sabernhardt, SergeyBiryukov, toro_unit, wpnomad.
Fixes #51519.
Built from https://develop.svn.wordpress.org/trunk@52285
git-svn-id: http://core.svn.wordpress.org/trunk@51877 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-11-30 12:18:01 -05:00
$ ( function () {
2019-07-17 02:17:56 -04:00
var text_color = $ ( '#text-color' );
header_text_fields = $ ( '.displaying-header-text' );
text_color . wpColorPicker ({
change : function ( event , ui ) {
pickColor ( text_color . wpColorPicker ( 'color' ) );
},
clear : function () {
pickColor ( '' );
}
});
$ ( '#display-header-text' ) . click ( toggle_text );
< ? php if ( ! display_header_text () ) : ?>
toggle_text ();
< ? php endif ; ?>
External Libraries: Further fix jQuery deprecations in WordPress core.
Follow-up to [50001], [50270], [50367], [50383], [50410], [50420], [50429], [50547].
Props chaion07, Clorith, costdev, desrosj, malthert, peterwilsoncc, presskopp, promz, sabernhardt, SergeyBiryukov, toro_unit, wpnomad.
Fixes #51519.
Built from https://develop.svn.wordpress.org/trunk@52285
git-svn-id: http://core.svn.wordpress.org/trunk@51877 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-11-30 12:18:01 -05:00
} );
2019-07-17 02:17:56 -04:00
})( jQuery );
</ script >
< ? php
}
/**
2023-06-05 17:35:20 -04:00
* Displays JavaScript based on Step 2.
2019-07-17 02:17:56 -04:00
*
* @ since 2.6 . 0
*/
public function js_2 () {
?>
< script type = " text/javascript " >
function onEndCrop ( coords ) {
jQuery ( '#x1' ) . val ( coords . x );
jQuery ( '#y1' ) . val ( coords . y );
jQuery ( '#width' ) . val ( coords . w );
jQuery ( '#height' ) . val ( coords . h );
}
External Libraries: Further fix jQuery deprecations in WordPress core.
Follow-up to [50001], [50270], [50367], [50383], [50410], [50420], [50429], [50547].
Props chaion07, Clorith, costdev, desrosj, malthert, peterwilsoncc, presskopp, promz, sabernhardt, SergeyBiryukov, toro_unit, wpnomad.
Fixes #51519.
Built from https://develop.svn.wordpress.org/trunk@52285
git-svn-id: http://core.svn.wordpress.org/trunk@51877 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-11-30 12:18:01 -05:00
jQuery ( function () {
2019-07-17 02:17:56 -04:00
var xinit = < ? php echo absint ( get_theme_support ( 'custom-header' , 'width' ) ); ?> ;
var yinit = < ? php echo absint ( get_theme_support ( 'custom-header' , 'height' ) ); ?> ;
var ratio = xinit / yinit ;
var ximg = jQuery ( 'img#upload' ) . width ();
var yimg = jQuery ( 'img#upload' ) . height ();
if ( yimg < yinit || ximg < xinit ) {
if ( ximg / yimg > ratio ) {
yinit = yimg ;
xinit = yinit * ratio ;
} else {
xinit = ximg ;
yinit = xinit / ratio ;
}
}
jQuery ( 'img#upload' ) . imgAreaSelect ({
handles : true ,
keys : true ,
show : true ,
x1 : 0 ,
y1 : 0 ,
x2 : xinit ,
y2 : yinit ,
< ? php
2021-04-13 15:05:04 -04:00
if ( ! current_theme_supports ( 'custom-header' , 'flex-height' )
&& ! current_theme_supports ( 'custom-header' , 'flex-width' )
) {
2019-07-17 02:17:56 -04:00
?>
aspectRatio : xinit + ':' + yinit ,
< ? php
}
if ( ! current_theme_supports ( 'custom-header' , 'flex-height' ) ) {
?>
maxHeight : < ? php echo get_theme_support ( 'custom-header' , 'height' ); ?> ,
< ? php
}
if ( ! current_theme_supports ( 'custom-header' , 'flex-width' ) ) {
?>
maxWidth : < ? php echo get_theme_support ( 'custom-header' , 'width' ); ?> ,
< ? php
}
?>
onInit : function () {
jQuery ( '#width' ) . val ( xinit );
jQuery ( '#height' ) . val ( yinit );
},
onSelectChange : function ( img , c ) {
jQuery ( '#x1' ) . val ( c . x1 );
jQuery ( '#y1' ) . val ( c . y1 );
jQuery ( '#width' ) . val ( c . width );
jQuery ( '#height' ) . val ( c . height );
}
});
External Libraries: Further fix jQuery deprecations in WordPress core.
Follow-up to [50001], [50270], [50367], [50383], [50410], [50420], [50429], [50547].
Props chaion07, Clorith, costdev, desrosj, malthert, peterwilsoncc, presskopp, promz, sabernhardt, SergeyBiryukov, toro_unit, wpnomad.
Fixes #51519.
Built from https://develop.svn.wordpress.org/trunk@52285
git-svn-id: http://core.svn.wordpress.org/trunk@51877 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-11-30 12:18:01 -05:00
} );
2019-07-17 02:17:56 -04:00
</ script >
< ? php
}
/**
2023-06-05 17:35:20 -04:00
* Displays first step of custom header image page .
2019-07-17 02:17:56 -04:00
*
* @ since 2.1 . 0
*/
public function step_1 () {
$this -> process_default_headers ();
?>
< div class = " wrap " >
< h1 >< ? php _e ( 'Custom Header' ); ?> </h1>
2023-09-13 21:13:20 -04:00
< ? php
if ( current_user_can ( 'customize' ) ) {
$message = sprintf (
2019-09-02 20:41:05 -04:00
/* translators: %s: URL to header image configuration in Customizer. */
I18N: Improve translator comments.
* Add missing translator comments.
* Fix placement of some translator comments. Translator comments should be on the line directly above the line containing the translation function call for optimal compatibility with various `.pot` file generation tools. The CS auto-fixing, which changed some inconsistent function calls to multi-line function calls, is part of the reason why this was no longer the case for a select group of translator comments.
Includes minor code layout fixes.
Polyglots, rejoice! All WordPress core files now have translator comments for all strings with placeholders!
Props jrf, subrataemfluence, GaryJ, webdados, Dency, swissspidy, alvarogois, marcomartins, mihaiiceyro, vladwtz, niq1982, flipkeijzer, michielatyoast, chandrapatel, thrijith, joshuanoyce, FesoVik, tessak22, bhaktirajdev, cleancoded, dhavalkasvala, garrett-eclipse, bibliofille, socalchristina, priyankkpatel, 5hel2l2y, adamsilverstein, JeffPaul, pierlo, SergeyBiryukov.
Fixes #44360.
Built from https://develop.svn.wordpress.org/trunk@45926
git-svn-id: http://core.svn.wordpress.org/trunk@45737 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2019-09-01 13:13:59 -04:00
__ ( 'You can now manage and live-preview Custom Header in the <a href="%s">Customizer</a>.' ),
admin_url ( 'customize.php?autofocus[control]=header_image' )
);
2023-09-13 21:13:20 -04:00
wp_admin_notice (
$message ,
array (
'type' => 'info' ,
'additional_classes' => array ( 'hide-if-no-customize' ),
)
);
}
2019-07-17 02:17:56 -04:00
Administration: Use `wp_admin_notice()` more in `/wp-admin/includes/`.
Adds further usages of `wp_admin_notice()` in `/wp-admin/includes/` on `.notice-error`, `.notice-warning`, `.error`, and `.updated`.
Ongoing task to implement new function across core.
Follow-up to [56408], [56409], [56410], [56518], [56570], [56571], [56572], [56573], [56576], [56589], [56590], [56597].
Props joedolson, mukesh27, costdev.
See #57791.
Built from https://develop.svn.wordpress.org/trunk@56599
git-svn-id: http://core.svn.wordpress.org/trunk@56111 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-17 11:23:22 -04:00
if ( ! empty ( $this -> updated ) ) {
$updated_message = sprintf (
/* translators: %s: Home URL. */
__ ( 'Header updated. <a href="%s">Visit your site</a> to see how it looks.' ),
esc_url ( home_url ( '/' ) )
);
wp_admin_notice (
$updated_message ,
array (
'id' => 'message' ,
'additional_classes' => array ( 'updated' ),
)
);
}
?>
2019-07-17 02:17:56 -04:00
< h2 >< ? php _e ( 'Header Image' ); ?> </h2>
< table class = " form-table " role = " presentation " >
< tbody >
< ? php if ( get_custom_header () || display_header_text () ) : ?>
< tr >
< th scope = " row " >< ? php _e ( 'Preview' ); ?> </th>
< td >
< ? php
if ( $this -> admin_image_div_callback ) {
call_user_func ( $this -> admin_image_div_callback );
} else {
$custom_header = get_custom_header ();
$header_image = get_header_image ();
if ( $header_image ) {
$header_image_style = 'background-image:url(' . esc_url ( $header_image ) . ');' ;
} else {
$header_image_style = '' ;
}
if ( $custom_header -> width ) {
$header_image_style .= 'max-width:' . $custom_header -> width . 'px;' ;
}
if ( $custom_header -> height ) {
$header_image_style .= 'height:' . $custom_header -> height . 'px;' ;
}
?>
< div id = " headimg " style = " <?php echo $header_image_style ; ?> " >
< ? php
if ( display_header_text () ) {
$style = ' style="color:#' . get_header_textcolor () . ';"' ;
} else {
$style = ' style="display:none;"' ;
}
?>
< h1 >< a id = " name " class = " displaying-header-text " < ? php echo $style ; ?> onclick="return false;" href="<?php bloginfo( 'url' ); ?>" tabindex="-1"><?php bloginfo( 'name' ); ?></a></h1>
< div id = " desc " class = " displaying-header-text " < ? php echo $style ; ?> ><?php bloginfo( 'description' ); ?></div>
</ div >
< ? php } ?>
</ td >
</ tr >
< ? php endif ; ?>
< ? php if ( current_user_can ( 'upload_files' ) && current_theme_supports ( 'custom-header' , 'uploads' ) ) : ?>
< tr >
< th scope = " row " >< ? php _e ( 'Select Image' ); ?> </th>
< td >
< 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 />
< ? php
2021-04-13 15:05:04 -04:00
if ( ! current_theme_supports ( 'custom-header' , 'flex-height' )
&& ! current_theme_supports ( 'custom-header' , 'flex-width' )
) {
printf (
/* translators: 1: Image width in pixels, 2: Image height in pixels. */
__ ( 'Images of exactly <strong>%1$d × %2$d pixels</strong> will be used as-is.' ) . '<br />' ,
get_theme_support ( 'custom-header' , 'width' ),
get_theme_support ( 'custom-header' , 'height' )
);
2019-07-17 02:17:56 -04:00
} elseif ( current_theme_supports ( 'custom-header' , 'flex-height' ) ) {
if ( ! current_theme_supports ( 'custom-header' , 'flex-width' ) ) {
printf (
2019-09-02 20:41:05 -04:00
/* translators: %s: Size in pixels. */
2019-07-17 02:17:56 -04:00
__ ( 'Images should be at least %s wide.' ) . ' ' ,
sprintf (
2019-09-02 20:41:05 -04:00
/* translators: %d: Custom header width. */
2019-07-17 02:17:56 -04:00
'<strong>' . __ ( '%d pixels' ) . '</strong>' ,
get_theme_support ( 'custom-header' , 'width' )
)
);
}
} elseif ( current_theme_supports ( 'custom-header' , 'flex-width' ) ) {
if ( ! current_theme_supports ( 'custom-header' , 'flex-height' ) ) {
printf (
2019-09-02 20:41:05 -04:00
/* translators: %s: Size in pixels. */
2019-07-17 02:17:56 -04:00
__ ( 'Images should be at least %s tall.' ) . ' ' ,
sprintf (
2019-09-02 20:41:05 -04:00
/* translators: %d: Custom header height. */
2019-07-17 02:17:56 -04:00
'<strong>' . __ ( '%d pixels' ) . '</strong>' ,
get_theme_support ( 'custom-header' , 'height' )
)
);
}
}
2021-04-13 15:05:04 -04:00
if ( current_theme_supports ( 'custom-header' , 'flex-height' )
|| current_theme_supports ( 'custom-header' , 'flex-width' )
) {
2019-07-17 02:17:56 -04:00
if ( current_theme_supports ( 'custom-header' , 'width' ) ) {
printf (
2019-09-02 20:41:05 -04:00
/* translators: %s: Size in pixels. */
2019-07-17 02:17:56 -04:00
__ ( 'Suggested width is %s.' ) . ' ' ,
sprintf (
2019-09-02 20:41:05 -04:00
/* translators: %d: Custom header width. */
2019-07-17 02:17:56 -04:00
'<strong>' . __ ( '%d pixels' ) . '</strong>' ,
get_theme_support ( 'custom-header' , 'width' )
)
);
}
2021-04-13 15:05:04 -04:00
2019-07-17 02:17:56 -04:00
if ( current_theme_supports ( 'custom-header' , 'height' ) ) {
printf (
2019-09-02 20:41:05 -04:00
/* translators: %s: Size in pixels. */
2019-07-17 02:17:56 -04:00
__ ( 'Suggested height is %s.' ) . ' ' ,
sprintf (
2019-09-02 20:41:05 -04:00
/* translators: %d: Custom header height. */
2019-07-17 02:17:56 -04:00
'<strong>' . __ ( '%d pixels' ) . '</strong>' ,
get_theme_support ( 'custom-header' , 'height' )
)
);
}
}
?>
</ p >
< form enctype = " multipart/form-data " id = " upload-form " class = " wp-upload-form " method = " post " action = " <?php echo esc_url( add_query_arg( 'step', 2 ) ); ?> " >
< p >
< label for = " upload " >< ? php _e ( 'Choose an image from your computer:' ); ?> </label><br />
< input type = " file " id = " upload " name = " import " />
< input type = " hidden " name = " action " value = " save " />
< ? php wp_nonce_field ( 'custom-header-upload' , '_wpnonce-custom-header-upload' ); ?>
< ? php submit_button ( __ ( 'Upload' ), '' , 'submit' , false ); ?>
</ p >
< ? php
2022-03-19 16:31:12 -04:00
$modal_update_href = add_query_arg (
array (
'page' => 'custom-header' ,
'step' => 2 ,
'_wpnonce-custom-header-upload' => wp_create_nonce ( 'custom-header-upload' ),
),
admin_url ( 'themes.php' )
);
2019-07-17 02:17:56 -04:00
?>
< p >
< label for = " choose-from-library-link " >< ? php _e ( 'Or choose an image from your media library:' ); ?> </label><br />
< button id = " choose-from-library-link " class = " button "
2022-03-19 16:31:12 -04:00
data - update - link = " <?php echo esc_url( $modal_update_href ); ?> "
2019-07-17 02:17:56 -04:00
data - choose = " <?php esc_attr_e( 'Choose a Custom Header' ); ?> "
data - update = " <?php esc_attr_e( 'Set as header' ); ?> " >< ? php _e ( 'Choose Image' ); ?> </button>
</ p >
</ form >
</ td >
</ tr >
< ? php endif ; ?>
</ tbody >
</ table >
< form method = " post " action = " <?php echo esc_url( add_query_arg( 'step', 1 ) ); ?> " >
< ? php submit_button ( null , 'screen-reader-text' , 'save-header-options' , false ); ?>
< table class = " form-table " role = " presentation " >
< tbody >
< ? php if ( get_uploaded_header_images () ) : ?>
< tr >
< th scope = " row " >< ? php _e ( 'Uploaded Images' ); ?> </th>
< td >
< p >< ? php _e ( 'You can choose one of your previously uploaded headers, or show a random one.' ); ?> </p>
< ? php
$this -> show_header_selector ( 'uploaded' );
?>
</ td >
</ tr >
< ? php
endif ;
if ( ! empty ( $this -> default_headers ) ) :
?>
< tr >
< th scope = " row " >< ? php _e ( 'Default Images' ); ?> </th>
< td >
< ? php if ( current_theme_supports ( 'custom-header' , 'uploads' ) ) : ?>
2023-06-22 01:25:29 -04:00
< p >< ? php _e ( 'If you do not want to upload your own image, you can use one of these cool headers, or show a random one.' ); ?> </p>
2019-07-17 02:17:56 -04:00
< ? php else : ?>
< p >< ? php _e ( 'You can use one of these cool headers or show a random one on each page.' ); ?> </p>
< ? php endif ; ?>
< ? php
$this -> show_header_selector ( 'default' );
?>
</ td >
</ tr >
< ? php
endif ;
if ( get_header_image () ) :
?>
< tr >
< th scope = " row " >< ? php _e ( 'Remove Image' ); ?> </th>
< td >
< p >< ? php _e ( 'This will remove the header image. You will not be able to restore any customizations.' ); ?> </p>
< ? php submit_button ( __ ( 'Remove Header Image' ), '' , 'removeheader' , false ); ?>
</ td >
</ tr >
< ? php
endif ;
2021-04-13 15:05:04 -04:00
$default_image = sprintf (
get_theme_support ( 'custom-header' , 'default-image' ),
get_template_directory_uri (),
get_stylesheet_directory_uri ()
);
if ( $default_image && get_header_image () !== $default_image ) :
2019-07-17 02:17:56 -04:00
?>
< tr >
< th scope = " row " >< ? php _e ( 'Reset Image' ); ?> </th>
< td >
< p >< ? php _e ( 'This will restore the original header image. You will not be able to restore any customizations.' ); ?> </p>
< ? php submit_button ( __ ( 'Restore Original Header Image' ), '' , 'resetheader' , false ); ?>
</ td >
</ tr >
< ? php endif ; ?>
</ tbody >
</ table >
< ? php if ( current_theme_supports ( 'custom-header' , 'header-text' ) ) : ?>
< h2 >< ? php _e ( 'Header Text' ); ?> </h2>
< table class = " form-table " role = " presentation " >
< tbody >
< tr >
< th scope = " row " >< ? php _e ( 'Header Text' ); ?> </th>
< td >
< p >
< 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>
</ p >
</ td >
</ tr >
< tr class = " displaying-header-text " >
< th scope = " row " >< ? php _e ( 'Text Color' ); ?> </th>
< td >
< p >
< ? php
$default_color = '' ;
if ( current_theme_supports ( 'custom-header' , 'default-text-color' ) ) {
$default_color = get_theme_support ( 'custom-header' , 'default-text-color' );
Code Modernization: Replace usage of `strpos()` with `str_contains()`.
`str_contains()` was introduced in PHP 8.0 to perform a case-sensitive check indicating if the string to search in (haystack) contains the given substring (needle).
WordPress core includes a polyfill for `str_contains()` on PHP < 8.0 as of WordPress 5.9.
This commit replaces `false !== strpos( ... )` with `str_contains()` in core files, making the code more readable and consistent, as well as better aligned with modern development practices.
Follow-up to [52039], [52040], [52326], [55703], [55710], [55987].
Props Soean, spacedmonkey, costdev, dingo_d, azaozz, mikeschroder, flixos90, peterwilsoncc, SergeyBiryukov.
Fixes #58206.
Built from https://develop.svn.wordpress.org/trunk@55988
git-svn-id: http://core.svn.wordpress.org/trunk@55500 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-06-22 10:36:26 -04:00
if ( $default_color && ! str_contains ( $default_color , '#' ) ) {
2019-07-17 02:17:56 -04:00
$default_color = '#' . $default_color ;
}
}
$default_color_attr = $default_color ? ' data-default-color="' . esc_attr ( $default_color ) . '"' : '' ;
$header_textcolor = display_header_text () ? get_header_textcolor () : get_theme_support ( 'custom-header' , 'default-text-color' );
Code Modernization: Replace usage of `strpos()` with `str_contains()`.
`str_contains()` was introduced in PHP 8.0 to perform a case-sensitive check indicating if the string to search in (haystack) contains the given substring (needle).
WordPress core includes a polyfill for `str_contains()` on PHP < 8.0 as of WordPress 5.9.
This commit replaces `false !== strpos( ... )` with `str_contains()` in core files, making the code more readable and consistent, as well as better aligned with modern development practices.
Follow-up to [52039], [52040], [52326], [55703], [55710], [55987].
Props Soean, spacedmonkey, costdev, dingo_d, azaozz, mikeschroder, flixos90, peterwilsoncc, SergeyBiryukov.
Fixes #58206.
Built from https://develop.svn.wordpress.org/trunk@55988
git-svn-id: http://core.svn.wordpress.org/trunk@55500 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-06-22 10:36:26 -04:00
if ( $header_textcolor && ! str_contains ( $header_textcolor , '#' ) ) {
2019-07-17 02:17:56 -04:00
$header_textcolor = '#' . $header_textcolor ;
}
echo '<input type="text" name="text-color" id="text-color" value="' . esc_attr ( $header_textcolor ) . '"' . $default_color_attr . ' />' ;
if ( $default_color ) {
2019-09-02 20:41:05 -04:00
/* translators: %s: Default text color. */
2019-07-17 02:17:56 -04:00
echo ' <span class="description hide-if-js">' . sprintf ( _x ( 'Default: %s' , 'color' ), esc_html ( $default_color ) ) . '</span>' ;
}
?>
</ p >
</ td >
</ tr >
</ tbody >
</ table >
< ? php
endif ;
/**
* Fires just before the submit button in the custom header options form .
*
* @ since 3.1 . 0
*/
do_action ( 'custom_header_options' );
wp_nonce_field ( 'custom-header-options' , '_wpnonce-custom-header-options' );
?>
< ? php submit_button ( null , 'primary' , 'save-header-options' ); ?>
</ form >
</ div >
< ? php
}
/**
2023-06-05 17:35:20 -04:00
* Displays second step of custom header image page .
2019-07-17 02:17:56 -04:00
*
* @ since 2.1 . 0
*/
public function step_2 () {
check_admin_referer ( 'custom-header-upload' , '_wpnonce-custom-header-upload' );
2021-04-13 15:05:04 -04:00
2019-07-17 02:17:56 -04:00
if ( ! current_theme_supports ( 'custom-header' , 'uploads' ) ) {
wp_die (
'<h1>' . __ ( 'Something went wrong.' ) . '</h1>' .
2022-01-15 03:44:03 -05:00
'<p>' . __ ( 'The active theme does not support uploading a custom header image.' ) . '</p>' ,
2019-07-17 02:17:56 -04:00
403
);
}
if ( empty ( $_POST ) && isset ( $_GET [ 'file' ] ) ) {
$attachment_id = absint ( $_GET [ 'file' ] );
$file = get_attached_file ( $attachment_id , true );
$url = wp_get_attachment_image_src ( $attachment_id , 'full' );
$url = $url [ 0 ];
} elseif ( isset ( $_POST ) ) {
$data = $this -> step_2_manage_upload ();
$attachment_id = $data [ 'attachment_id' ];
$file = $data [ 'file' ];
$url = $data [ 'url' ];
}
if ( file_exists ( $file ) ) {
2021-02-02 11:53:04 -05:00
list ( $width , $height , $type , $attr ) = wp_getimagesize ( $file );
2019-07-17 02:17:56 -04:00
} else {
$data = wp_get_attachment_metadata ( $attachment_id );
2021-04-13 15:05:04 -04:00
$height = isset ( $data [ 'height' ] ) ? ( int ) $data [ 'height' ] : 0 ;
$width = isset ( $data [ 'width' ] ) ? ( int ) $data [ 'width' ] : 0 ;
2019-07-17 02:17:56 -04:00
unset ( $data );
}
$max_width = 0 ;
2021-04-13 15:05:04 -04:00
2020-01-28 19:45:18 -05:00
// For flex, limit size of image displayed to 1500px unless theme says otherwise.
2019-07-17 02:17:56 -04:00
if ( current_theme_supports ( 'custom-header' , 'flex-width' ) ) {
$max_width = 1500 ;
}
if ( current_theme_supports ( 'custom-header' , 'max-width' ) ) {
$max_width = max ( $max_width , get_theme_support ( 'custom-header' , 'max-width' ) );
}
2021-04-13 15:05:04 -04:00
2019-07-17 02:17:56 -04:00
$max_width = max ( $max_width , get_theme_support ( 'custom-header' , 'width' ) );
2020-01-28 19:45:18 -05:00
// If flexible height isn't supported and the image is the exact right size.
2021-04-13 15:05:04 -04:00
if ( ! current_theme_supports ( 'custom-header' , 'flex-height' )
&& ! current_theme_supports ( 'custom-header' , 'flex-width' )
&& ( int ) get_theme_support ( 'custom-header' , 'width' ) === $width
&& ( int ) get_theme_support ( 'custom-header' , 'height' ) === $height
) {
2020-01-28 19:45:18 -05:00
// Add the metadata.
2019-07-17 02:17:56 -04:00
if ( file_exists ( $file ) ) {
wp_update_attachment_metadata ( $attachment_id , wp_generate_attachment_metadata ( $attachment_id , $file ) );
}
$this -> set_header_image ( compact ( 'url' , 'attachment_id' , 'width' , 'height' ) );
/**
Media: Consistently call the `wp_create_file_in_uploads` hook as a filter.
The filter was initially introduced for file replication purposes. Since the returned value is not always used directly, some instances were later converted to an action as part of removing unused variables, and the hook was documented as an action while still being called as a filter in other instances.
This commit aims to correct the discrepancy between the code and the documentation.
Follow-up to [4673], [4817], [4818], [6363], [6551], [13041], [25821], [33154], [33280].
Props Howdy_McGee, nicolefurlan, johnbillion, mhshujon, SergeyBiryukov.
Fixes #57775.
Built from https://develop.svn.wordpress.org/trunk@56929
git-svn-id: http://core.svn.wordpress.org/trunk@56440 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-10-13 08:20:20 -04:00
* Filters the attachment file path after the custom header or background image is set .
*
* Used for file replication .
2019-07-17 02:17:56 -04:00
*
* @ since 2.1 . 0
*
* @ param string $file Path to the file .
* @ param int $attachment_id Attachment ID .
*/
Media: Consistently call the `wp_create_file_in_uploads` hook as a filter.
The filter was initially introduced for file replication purposes. Since the returned value is not always used directly, some instances were later converted to an action as part of removing unused variables, and the hook was documented as an action while still being called as a filter in other instances.
This commit aims to correct the discrepancy between the code and the documentation.
Follow-up to [4673], [4817], [4818], [6363], [6551], [13041], [25821], [33154], [33280].
Props Howdy_McGee, nicolefurlan, johnbillion, mhshujon, SergeyBiryukov.
Fixes #57775.
Built from https://develop.svn.wordpress.org/trunk@56929
git-svn-id: http://core.svn.wordpress.org/trunk@56440 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-10-13 08:20:20 -04:00
$file = apply_filters ( 'wp_create_file_in_uploads' , $file , $attachment_id ); // For replication.
2019-07-17 02:17:56 -04:00
return $this -> finished ();
} elseif ( $width > $max_width ) {
$oitar = $width / $max_width ;
2021-04-13 15:05:04 -04:00
$image = wp_crop_image (
$attachment_id ,
0 ,
0 ,
$width ,
$height ,
$max_width ,
$height / $oitar ,
false ,
str_replace ( wp_basename ( $file ), 'midsize-' . wp_basename ( $file ), $file )
);
2019-07-17 02:17:56 -04:00
if ( ! $image || is_wp_error ( $image ) ) {
wp_die ( __ ( 'Image could not be processed. Please go back and try again.' ), __ ( 'Image Processing Error' ) );
}
/** This filter is documented in wp-admin/includes/class-custom-image-header.php */
2020-01-28 19:45:18 -05:00
$image = apply_filters ( 'wp_create_file_in_uploads' , $image , $attachment_id ); // For replication.
2019-07-17 02:17:56 -04:00
$url = str_replace ( wp_basename ( $url ), wp_basename ( $image ), $url );
$width = $width / $oitar ;
$height = $height / $oitar ;
} else {
$oitar = 1 ;
}
?>
< div class = " wrap " >
< h1 >< ? php _e ( 'Crop Header Image' ); ?> </h1>
< form method = " post " action = " <?php echo esc_url( add_query_arg( 'step', 3 ) ); ?> " >
< p class = " hide-if-no-js " >< ? php _e ( 'Choose the part of the image you want to use as your header.' ); ?> </p>
< p class = " hide-if-js " >< strong >< ? php _e ( 'You need JavaScript to choose a part of the image.' ); ?> </strong></p>
< div id = " crop_image " style = " position: relative " >
2024-01-26 09:20:12 -05:00
< img src = " <?php echo esc_url( $url ); ?> " id = " upload " width = " <?php echo esc_attr( $width ); ?> " height = " <?php echo esc_attr( $height ); ?> " alt = " " />
2019-07-17 02:17:56 -04:00
</ div >
2021-03-20 14:30:08 -04:00
< input type = " hidden " name = " x1 " id = " x1 " value = " 0 " />
< input type = " hidden " name = " y1 " id = " y1 " value = " 0 " />
< input type = " hidden " name = " width " id = " width " value = " <?php echo esc_attr( $width ); ?> " />
< input type = " hidden " name = " height " id = " height " value = " <?php echo esc_attr( $height ); ?> " />
2019-07-17 02:17:56 -04:00
< input type = " hidden " name = " attachment_id " id = " attachment_id " value = " <?php echo esc_attr( $attachment_id ); ?> " />
< input type = " hidden " name = " oitar " id = " oitar " value = " <?php echo esc_attr( $oitar ); ?> " />
< ? php if ( empty ( $_POST ) && isset ( $_GET [ 'file' ] ) ) { ?>
< input type = " hidden " name = " create-new-attachment " value = " true " />
< ? php } ?>
< ? php wp_nonce_field ( 'custom-header-crop-image' ); ?>
< p class = " submit " >
< ? php submit_button ( __ ( 'Crop and Publish' ), 'primary' , 'submit' , false ); ?>
< ? php
2021-04-13 15:05:04 -04:00
if ( isset ( $oitar ) && 1 === $oitar
&& ( current_theme_supports ( 'custom-header' , 'flex-height' )
|| current_theme_supports ( 'custom-header' , 'flex-width' ) )
) {
2019-07-17 02:17:56 -04:00
submit_button ( __ ( 'Skip Cropping, Publish Image as Is' ), '' , 'skip-cropping' , false );
}
?>
</ p >
</ form >
</ div >
< ? php
}
/**
2023-06-05 17:35:20 -04:00
* Uploads the file to be cropped in the second step .
2019-07-17 02:17:56 -04:00
*
* @ since 3.4 . 0
*/
public function step_2_manage_upload () {
$overrides = array ( 'test_form' => false );
$uploaded_file = $_FILES [ 'import' ];
$wp_filetype = wp_check_filetype_and_ext ( $uploaded_file [ 'tmp_name' ], $uploaded_file [ 'name' ] );
2021-04-13 15:05:04 -04:00
2019-07-17 02:17:56 -04:00
if ( ! wp_match_mime_types ( 'image' , $wp_filetype [ 'type' ] ) ) {
wp_die ( __ ( 'The uploaded file is not a valid image. Please try again.' ) );
}
$file = wp_handle_upload ( $uploaded_file , $overrides );
if ( isset ( $file [ 'error' ] ) ) {
wp_die ( $file [ 'error' ], __ ( 'Image Upload Error' ) );
}
$url = $file [ 'url' ];
$type = $file [ 'type' ];
$file = $file [ 'file' ];
$filename = wp_basename ( $file );
2022-04-14 11:15:12 -04:00
// Construct the attachment array.
2022-03-17 16:19:10 -04:00
$attachment = array (
2019-07-17 02:17:56 -04:00
'post_title' => $filename ,
'post_content' => $url ,
'post_mime_type' => $type ,
'guid' => $url ,
'context' => 'custom-header' ,
);
2020-01-28 19:45:18 -05:00
// Save the data.
2022-03-17 16:19:10 -04:00
$attachment_id = wp_insert_attachment ( $attachment , $file );
2021-04-13 15:05:04 -04:00
2019-07-17 02:17:56 -04:00
return compact ( 'attachment_id' , 'file' , 'filename' , 'url' , 'type' );
}
/**
2023-06-05 17:35:20 -04:00
* Displays third step of custom header image page .
2019-07-17 02:17:56 -04:00
*
* @ since 2.1 . 0
* @ since 4.4 . 0 Switched to using wp_get_attachment_url () instead of the guid
* for retrieving the header image URL .
*/
public function step_3 () {
check_admin_referer ( 'custom-header-crop-image' );
if ( ! current_theme_supports ( 'custom-header' , 'uploads' ) ) {
wp_die (
'<h1>' . __ ( 'Something went wrong.' ) . '</h1>' .
2022-01-15 03:44:03 -05:00
'<p>' . __ ( 'The active theme does not support uploading a custom header image.' ) . '</p>' ,
2019-07-17 02:17:56 -04:00
403
);
}
2021-04-13 15:05:04 -04:00
if ( ! empty ( $_POST [ 'skip-cropping' ] )
&& ! current_theme_supports ( 'custom-header' , 'flex-height' )
&& ! current_theme_supports ( 'custom-header' , 'flex-width' )
) {
2019-07-17 02:17:56 -04:00
wp_die (
'<h1>' . __ ( 'Something went wrong.' ) . '</h1>' .
2022-01-15 03:44:03 -05:00
'<p>' . __ ( 'The active theme does not support a flexible sized header image.' ) . '</p>' ,
2019-07-17 02:17:56 -04:00
403
);
}
if ( $_POST [ 'oitar' ] > 1 ) {
$_POST [ 'x1' ] = $_POST [ 'x1' ] * $_POST [ 'oitar' ];
$_POST [ 'y1' ] = $_POST [ 'y1' ] * $_POST [ 'oitar' ];
$_POST [ 'width' ] = $_POST [ 'width' ] * $_POST [ 'oitar' ];
$_POST [ 'height' ] = $_POST [ 'height' ] * $_POST [ 'oitar' ];
}
$attachment_id = absint ( $_POST [ 'attachment_id' ] );
$original = get_attached_file ( $attachment_id );
$dimensions = $this -> get_header_dimensions (
array (
'height' => $_POST [ 'height' ],
'width' => $_POST [ 'width' ],
)
);
$height = $dimensions [ 'dst_height' ];
$width = $dimensions [ 'dst_width' ];
if ( empty ( $_POST [ 'skip-cropping' ] ) ) {
2021-04-13 15:05:04 -04:00
$cropped = wp_crop_image (
$attachment_id ,
( int ) $_POST [ 'x1' ],
( int ) $_POST [ 'y1' ],
( int ) $_POST [ 'width' ],
( int ) $_POST [ 'height' ],
$width ,
$height
);
2019-07-17 02:17:56 -04:00
} elseif ( ! empty ( $_POST [ 'create-new-attachment' ] ) ) {
$cropped = _copy_image_file ( $attachment_id );
} else {
$cropped = get_attached_file ( $attachment_id );
}
if ( ! $cropped || is_wp_error ( $cropped ) ) {
wp_die ( __ ( 'Image could not be processed. Please go back and try again.' ), __ ( 'Image Processing Error' ) );
}
/** This filter is documented in wp-admin/includes/class-custom-image-header.php */
2020-01-28 19:45:18 -05:00
$cropped = apply_filters ( 'wp_create_file_in_uploads' , $cropped , $attachment_id ); // For replication.
2019-07-17 02:17:56 -04:00
Media: Accessibility: Copy attachment properties on site icon crop.
Add parity between site icon, custom header, and default image crop behaviors. [53027] fixed a bug where alt text and caption were not copied on custom headers, but did not apply that change in any other context.
Deprecate the `create_attachment_object` method in the `Wp_Site_Icon` and `Custom_Image_Header` classes and replace that functionality with the new function `wp_copy_parent_attachment_properties()` to improve consistency.
Props afercia, rcreators, jorbin, joedolson, huzaifaalmesbah, shailu25, swissspidy, mukesh27.
Fixes #60524.
Built from https://develop.svn.wordpress.org/trunk@57755
git-svn-id: http://core.svn.wordpress.org/trunk@57256 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-03-02 15:15:13 -05:00
$attachment = wp_copy_parent_attachment_properties ( $cropped , $attachment_id , 'custom-header' );
2019-07-17 02:17:56 -04:00
if ( ! empty ( $_POST [ 'create-new-attachment' ] ) ) {
2022-03-17 16:19:10 -04:00
unset ( $attachment [ 'ID' ] );
2019-07-17 02:17:56 -04:00
}
2020-01-28 19:45:18 -05:00
// Update the attachment.
2022-03-17 16:19:10 -04:00
$attachment_id = $this -> insert_attachment ( $attachment , $cropped );
2019-07-17 02:17:56 -04:00
$url = wp_get_attachment_url ( $attachment_id );
$this -> set_header_image ( compact ( 'url' , 'attachment_id' , 'width' , 'height' ) );
// Cleanup.
$medium = str_replace ( wp_basename ( $original ), 'midsize-' . wp_basename ( $original ), $original );
if ( file_exists ( $medium ) ) {
wp_delete_file ( $medium );
}
if ( empty ( $_POST [ 'create-new-attachment' ] ) && empty ( $_POST [ 'skip-cropping' ] ) ) {
wp_delete_file ( $original );
}
return $this -> finished ();
}
/**
2023-06-05 17:35:20 -04:00
* Displays last step of custom header image page .
2019-07-17 02:17:56 -04:00
*
* @ since 2.1 . 0
*/
public function finished () {
$this -> updated = true ;
$this -> step_1 ();
}
/**
2023-06-05 17:35:20 -04:00
* Displays the page based on the current step .
2019-07-17 02:17:56 -04:00
*
* @ since 2.1 . 0
*/
public function admin_page () {
if ( ! current_user_can ( 'edit_theme_options' ) ) {
wp_die ( __ ( 'Sorry, you are not allowed to customize headers.' ) );
}
2021-04-13 15:05:04 -04:00
2019-07-17 02:17:56 -04:00
$step = $this -> step ();
2021-04-13 15:05:04 -04:00
if ( 2 === $step ) {
2019-07-17 02:17:56 -04:00
$this -> step_2 ();
2021-04-13 15:05:04 -04:00
} elseif ( 3 === $step ) {
2019-07-17 02:17:56 -04:00
$this -> step_3 ();
} else {
$this -> step_1 ();
}
}
/**
* Unused since 3.5 . 0.
*
* @ since 3.4 . 0
*
* @ param array $form_fields
* @ return array $form_fields
*/
public function attachment_fields_to_edit ( $form_fields ) {
return $form_fields ;
}
/**
* Unused since 3.5 . 0.
*
* @ since 3.4 . 0
*
* @ param array $tabs
* @ return array $tabs
*/
public function filter_upload_tabs ( $tabs ) {
return $tabs ;
}
/**
2023-06-05 17:35:20 -04:00
* Chooses a header image , selected from existing uploaded and default headers ,
* or provides an array of uploaded header data ( either new , or from media library ) .
2019-07-17 02:17:56 -04:00
*
* @ since 3.4 . 0
*
* @ param mixed $choice Which header image to select . Allows for values of 'random-default-image' ,
2023-06-05 17:35:20 -04:00
* for randomly cycling among the default images ; 'random-uploaded-image' ,
* for randomly cycling among the uploaded images ; the key of a default image
* registered for that theme ; and the key of an image uploaded for that theme
* ( the attachment ID of the image ) . Or an array of arguments : attachment_id ,
* url , width , height . All are required .
2019-07-17 02:17:56 -04:00
*/
final public function set_header_image ( $choice ) {
if ( is_array ( $choice ) || is_object ( $choice ) ) {
$choice = ( array ) $choice ;
2021-04-13 15:05:04 -04:00
2019-07-17 02:17:56 -04:00
if ( ! isset ( $choice [ 'attachment_id' ] ) || ! isset ( $choice [ 'url' ] ) ) {
return ;
}
2022-06-01 14:14:10 -04:00
$choice [ 'url' ] = sanitize_url ( $choice [ 'url' ] );
2019-07-17 02:17:56 -04:00
$header_image_data = ( object ) array (
'attachment_id' => $choice [ 'attachment_id' ],
'url' => $choice [ 'url' ],
'thumbnail_url' => $choice [ 'url' ],
'height' => $choice [ 'height' ],
'width' => $choice [ 'width' ],
);
update_post_meta ( $choice [ 'attachment_id' ], '_wp_attachment_is_custom_header' , get_stylesheet () );
2021-04-13 15:05:04 -04:00
2019-07-17 02:17:56 -04:00
set_theme_mod ( 'header_image' , $choice [ 'url' ] );
set_theme_mod ( 'header_image_data' , $header_image_data );
2021-04-13 15:05:04 -04:00
2019-07-17 02:17:56 -04:00
return ;
}
2020-04-04 23:02:11 -04:00
if ( in_array ( $choice , array ( 'remove-header' , 'random-default-image' , 'random-uploaded-image' ), true ) ) {
2019-07-17 02:17:56 -04:00
set_theme_mod ( 'header_image' , $choice );
remove_theme_mod ( 'header_image_data' );
2021-04-13 15:05:04 -04:00
2019-07-17 02:17:56 -04:00
return ;
}
$uploaded = get_uploaded_header_images ();
2021-04-13 15:05:04 -04:00
2019-07-17 02:17:56 -04:00
if ( $uploaded && isset ( $uploaded [ $choice ] ) ) {
$header_image_data = $uploaded [ $choice ];
} else {
$this -> process_default_headers ();
if ( isset ( $this -> default_headers [ $choice ] ) ) {
$header_image_data = $this -> default_headers [ $choice ];
} else {
return ;
}
}
2022-06-01 14:14:10 -04:00
set_theme_mod ( 'header_image' , sanitize_url ( $header_image_data [ 'url' ] ) );
2019-07-17 02:17:56 -04:00
set_theme_mod ( 'header_image_data' , $header_image_data );
}
/**
2023-06-05 17:35:20 -04:00
* Removes a header image .
2019-07-17 02:17:56 -04:00
*
* @ since 3.4 . 0
*/
final public function remove_header_image () {
$this -> set_header_image ( 'remove-header' );
}
/**
2023-06-05 17:35:20 -04:00
* Resets a header image to the default image for the theme .
2019-07-17 02:17:56 -04:00
*
* This method does not do anything if the theme does not have a default header image .
*
* @ since 3.4 . 0
*/
final public function reset_header_image () {
$this -> process_default_headers ();
$default = get_theme_support ( 'custom-header' , 'default-image' );
if ( ! $default ) {
$this -> remove_header_image ();
return ;
}
2021-04-13 15:05:04 -04:00
2019-07-17 02:17:56 -04:00
$default = sprintf ( $default , get_template_directory_uri (), get_stylesheet_directory_uri () );
$default_data = array ();
foreach ( $this -> default_headers as $header => $details ) {
2021-04-13 15:05:04 -04:00
if ( $details [ 'url' ] === $default ) {
2019-07-17 02:17:56 -04:00
$default_data = $details ;
break ;
}
}
set_theme_mod ( 'header_image' , $default );
set_theme_mod ( 'header_image_data' , ( object ) $default_data );
}
/**
2023-06-05 17:35:20 -04:00
* Calculates width and height based on what the currently selected theme supports .
2019-07-17 02:17:56 -04:00
*
* @ since 3.9 . 0
*
* @ param array $dimensions
* @ return array dst_height and dst_width of header image .
*/
final public function get_header_dimensions ( $dimensions ) {
$max_width = 0 ;
$width = absint ( $dimensions [ 'width' ] );
$height = absint ( $dimensions [ 'height' ] );
$theme_height = get_theme_support ( 'custom-header' , 'height' );
$theme_width = get_theme_support ( 'custom-header' , 'width' );
$has_flex_width = current_theme_supports ( 'custom-header' , 'flex-width' );
$has_flex_height = current_theme_supports ( 'custom-header' , 'flex-height' );
$has_max_width = current_theme_supports ( 'custom-header' , 'max-width' );
$dst = array (
'dst_height' => null ,
'dst_width' => null ,
);
2020-01-28 19:45:18 -05:00
// For flex, limit size of image displayed to 1500px unless theme says otherwise.
2019-07-17 02:17:56 -04:00
if ( $has_flex_width ) {
$max_width = 1500 ;
}
if ( $has_max_width ) {
$max_width = max ( $max_width , get_theme_support ( 'custom-header' , 'max-width' ) );
}
$max_width = max ( $max_width , $theme_width );
if ( $has_flex_height && ( ! $has_flex_width || $width > $max_width ) ) {
$dst [ 'dst_height' ] = absint ( $height * ( $max_width / $width ) );
} elseif ( $has_flex_height && $has_flex_width ) {
$dst [ 'dst_height' ] = $height ;
} else {
$dst [ 'dst_height' ] = $theme_height ;
}
if ( $has_flex_width && ( ! $has_flex_height || $width > $max_width ) ) {
$dst [ 'dst_width' ] = absint ( $width * ( $max_width / $width ) );
} elseif ( $has_flex_width && $has_flex_height ) {
$dst [ 'dst_width' ] = $width ;
} else {
$dst [ 'dst_width' ] = $theme_width ;
}
return $dst ;
}
/**
2023-06-05 17:35:20 -04:00
* Creates an attachment 'object' .
2019-07-17 02:17:56 -04:00
*
* @ since 3.9 . 0
Media: Accessibility: Copy attachment properties on site icon crop.
Add parity between site icon, custom header, and default image crop behaviors. [53027] fixed a bug where alt text and caption were not copied on custom headers, but did not apply that change in any other context.
Deprecate the `create_attachment_object` method in the `Wp_Site_Icon` and `Custom_Image_Header` classes and replace that functionality with the new function `wp_copy_parent_attachment_properties()` to improve consistency.
Props afercia, rcreators, jorbin, joedolson, huzaifaalmesbah, shailu25, swissspidy, mukesh27.
Fixes #60524.
Built from https://develop.svn.wordpress.org/trunk@57755
git-svn-id: http://core.svn.wordpress.org/trunk@57256 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-03-02 15:15:13 -05:00
* @ deprecated 6.5 . 0
2019-07-17 02:17:56 -04:00
*
* @ param string $cropped Cropped image URL .
* @ param int $parent_attachment_id Attachment ID of parent image .
2022-03-17 16:19:10 -04:00
* @ return array An array with attachment object data .
2019-07-17 02:17:56 -04:00
*/
final public function create_attachment_object ( $cropped , $parent_attachment_id ) {
Media: Accessibility: Copy attachment properties on site icon crop.
Add parity between site icon, custom header, and default image crop behaviors. [53027] fixed a bug where alt text and caption were not copied on custom headers, but did not apply that change in any other context.
Deprecate the `create_attachment_object` method in the `Wp_Site_Icon` and `Custom_Image_Header` classes and replace that functionality with the new function `wp_copy_parent_attachment_properties()` to improve consistency.
Props afercia, rcreators, jorbin, joedolson, huzaifaalmesbah, shailu25, swissspidy, mukesh27.
Fixes #60524.
Built from https://develop.svn.wordpress.org/trunk@57755
git-svn-id: http://core.svn.wordpress.org/trunk@57256 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-03-02 15:15:13 -05:00
_deprecated_function ( __METHOD__ , '6.5.0' , 'wp_copy_parent_attachment_properties()' );
2019-07-17 02:17:56 -04:00
$parent = get_post ( $parent_attachment_id );
$parent_url = wp_get_attachment_url ( $parent -> ID );
$url = str_replace ( wp_basename ( $parent_url ), wp_basename ( $cropped ), $parent_url );
2021-02-02 11:53:04 -05:00
$size = wp_getimagesize ( $cropped );
2019-07-17 02:17:56 -04:00
$image_type = ( $size ) ? $size [ 'mime' ] : 'image/jpeg' ;
2022-03-17 16:19:10 -04:00
$attachment = array (
2019-07-17 02:17:56 -04:00
'ID' => $parent_attachment_id ,
'post_title' => wp_basename ( $cropped ),
'post_mime_type' => $image_type ,
'guid' => $url ,
'context' => 'custom-header' ,
'post_parent' => $parent_attachment_id ,
);
2022-03-17 16:19:10 -04:00
return $attachment ;
2019-07-17 02:17:56 -04:00
}
/**
2023-06-05 17:35:20 -04:00
* Inserts an attachment and its metadata .
2019-07-17 02:17:56 -04:00
*
* @ since 3.9 . 0
*
2022-03-17 16:19:10 -04:00
* @ param array $attachment An array with attachment object data .
* @ param string $cropped File path to cropped image .
2019-07-17 02:17:56 -04:00
* @ return int Attachment ID .
*/
2022-03-17 16:19:10 -04:00
final public function insert_attachment ( $attachment , $cropped ) {
$parent_id = isset ( $attachment [ 'post_parent' ] ) ? $attachment [ 'post_parent' ] : null ;
unset ( $attachment [ 'post_parent' ] );
2019-07-17 02:17:56 -04:00
2022-03-17 16:19:10 -04:00
$attachment_id = wp_insert_attachment ( $attachment , $cropped );
2019-07-17 02:17:56 -04:00
$metadata = wp_generate_attachment_metadata ( $attachment_id , $cropped );
// If this is a crop, save the original attachment ID as metadata.
if ( $parent_id ) {
$metadata [ 'attachment_parent' ] = $parent_id ;
}
/**
* Filters the header image attachment metadata .
*
* @ since 3.9 . 0
*
* @ see wp_generate_attachment_metadata ()
*
* @ param array $metadata Attachment metadata .
*/
$metadata = apply_filters ( 'wp_header_image_attachment_metadata' , $metadata );
wp_update_attachment_metadata ( $attachment_id , $metadata );
return $attachment_id ;
}
/**
* Gets attachment uploaded by Media Manager , crops it , then saves it as a
* new object . Returns JSON - encoded object details .
*
* @ since 3.9 . 0
*/
public function ajax_header_crop () {
check_ajax_referer ( 'image_editor-' . $_POST [ 'id' ], 'nonce' );
if ( ! current_user_can ( 'edit_theme_options' ) ) {
wp_send_json_error ();
}
if ( ! current_theme_supports ( 'custom-header' , 'uploads' ) ) {
wp_send_json_error ();
}
$crop_details = $_POST [ 'cropDetails' ];
$dimensions = $this -> get_header_dimensions (
array (
'height' => $crop_details [ 'height' ],
'width' => $crop_details [ 'width' ],
)
);
$attachment_id = absint ( $_POST [ 'id' ] );
$cropped = wp_crop_image (
$attachment_id ,
( int ) $crop_details [ 'x1' ],
( int ) $crop_details [ 'y1' ],
( int ) $crop_details [ 'width' ],
( int ) $crop_details [ 'height' ],
( int ) $dimensions [ 'dst_width' ],
( int ) $dimensions [ 'dst_height' ]
);
if ( ! $cropped || is_wp_error ( $cropped ) ) {
wp_send_json_error ( array ( 'message' => __ ( 'Image could not be processed. Please go back and try again.' ) ) );
}
/** This filter is documented in wp-admin/includes/class-custom-image-header.php */
2020-01-28 19:45:18 -05:00
$cropped = apply_filters ( 'wp_create_file_in_uploads' , $cropped , $attachment_id ); // For replication.
2019-07-17 02:17:56 -04:00
Media: Accessibility: Copy attachment properties on site icon crop.
Add parity between site icon, custom header, and default image crop behaviors. [53027] fixed a bug where alt text and caption were not copied on custom headers, but did not apply that change in any other context.
Deprecate the `create_attachment_object` method in the `Wp_Site_Icon` and `Custom_Image_Header` classes and replace that functionality with the new function `wp_copy_parent_attachment_properties()` to improve consistency.
Props afercia, rcreators, jorbin, joedolson, huzaifaalmesbah, shailu25, swissspidy, mukesh27.
Fixes #60524.
Built from https://develop.svn.wordpress.org/trunk@57755
git-svn-id: http://core.svn.wordpress.org/trunk@57256 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-03-02 15:15:13 -05:00
$attachment = wp_copy_parent_attachment_properties ( $cropped , $attachment_id , 'custom-header' );
2019-07-17 02:17:56 -04:00
2022-03-17 16:19:10 -04:00
$previous = $this -> get_previous_crop ( $attachment );
2019-07-17 02:17:56 -04:00
if ( $previous ) {
2022-03-17 16:19:10 -04:00
$attachment [ 'ID' ] = $previous ;
2019-07-17 02:17:56 -04:00
} else {
2022-03-17 16:19:10 -04:00
unset ( $attachment [ 'ID' ] );
2019-07-17 02:17:56 -04:00
}
2022-03-17 16:19:10 -04:00
$new_attachment_id = $this -> insert_attachment ( $attachment , $cropped );
2019-07-17 02:17:56 -04:00
2022-03-17 16:19:10 -04:00
$attachment [ 'attachment_id' ] = $new_attachment_id ;
$attachment [ 'url' ] = wp_get_attachment_url ( $new_attachment_id );
2019-07-17 02:17:56 -04:00
2022-03-17 16:19:10 -04:00
$attachment [ 'width' ] = $dimensions [ 'dst_width' ];
$attachment [ 'height' ] = $dimensions [ 'dst_height' ];
2019-07-17 02:17:56 -04:00
2022-03-17 16:19:10 -04:00
wp_send_json_success ( $attachment );
2019-07-17 02:17:56 -04:00
}
/**
* Given an attachment ID for a header image , updates its " last used "
* timestamp to now .
*
* Triggered when the user tries adds a new header image from the
* Media Manager , even if s / he doesn ' t save that change .
*
* @ since 3.9 . 0
*/
public function ajax_header_add () {
check_ajax_referer ( 'header-add' , 'nonce' );
if ( ! current_user_can ( 'edit_theme_options' ) ) {
wp_send_json_error ();
}
$attachment_id = absint ( $_POST [ 'attachment_id' ] );
if ( $attachment_id < 1 ) {
wp_send_json_error ();
}
$key = '_wp_attachment_custom_header_last_used_' . get_stylesheet ();
update_post_meta ( $attachment_id , $key , time () );
update_post_meta ( $attachment_id , '_wp_attachment_is_custom_header' , get_stylesheet () );
wp_send_json_success ();
}
/**
* Given an attachment ID for a header image , unsets it as a user - uploaded
2022-01-20 18:53:05 -05:00
* header image for the active theme .
2019-07-17 02:17:56 -04:00
*
* Triggered when the user clicks the overlay " X " button next to each image
* choice in the Customizer ' s Header tool .
*
* @ since 3.9 . 0
*/
public function ajax_header_remove () {
check_ajax_referer ( 'header-remove' , 'nonce' );
if ( ! current_user_can ( 'edit_theme_options' ) ) {
wp_send_json_error ();
}
$attachment_id = absint ( $_POST [ 'attachment_id' ] );
if ( $attachment_id < 1 ) {
wp_send_json_error ();
}
$key = '_wp_attachment_custom_header_last_used_' . get_stylesheet ();
delete_post_meta ( $attachment_id , $key );
delete_post_meta ( $attachment_id , '_wp_attachment_is_custom_header' , get_stylesheet () );
wp_send_json_success ();
}
/**
* Updates the last - used postmeta on a header image attachment after saving a new header image via the Customizer .
*
* @ since 3.9 . 0
*
* @ param WP_Customize_Manager $wp_customize Customize manager .
*/
public function customize_set_last_used ( $wp_customize ) {
$header_image_data_setting = $wp_customize -> get_setting ( 'header_image_data' );
2021-04-13 15:05:04 -04:00
2019-07-17 02:17:56 -04:00
if ( ! $header_image_data_setting ) {
return ;
}
2021-04-13 15:05:04 -04:00
2019-07-17 02:17:56 -04:00
$data = $header_image_data_setting -> post_value ();
if ( ! isset ( $data [ 'attachment_id' ] ) ) {
return ;
}
$attachment_id = $data [ 'attachment_id' ];
$key = '_wp_attachment_custom_header_last_used_' . get_stylesheet ();
update_post_meta ( $attachment_id , $key , time () );
}
/**
* Gets the details of default header images if defined .
*
* @ since 3.9 . 0
*
* @ return array Default header images .
*/
public function get_default_header_images () {
$this -> process_default_headers ();
// Get the default image if there is one.
$default = get_theme_support ( 'custom-header' , 'default-image' );
2020-01-28 19:45:18 -05:00
if ( ! $default ) { // If not, easy peasy.
return $this -> default_headers ;
2019-07-17 02:17:56 -04:00
}
2021-04-13 15:05:04 -04:00
$default = sprintf ( $default , get_template_directory_uri (), get_stylesheet_directory_uri () );
2019-07-17 02:17:56 -04:00
$already_has_default = false ;
foreach ( $this -> default_headers as $k => $h ) {
if ( $h [ 'url' ] === $default ) {
$already_has_default = true ;
break ;
}
}
if ( $already_has_default ) {
return $this -> default_headers ;
}
// If the one true image isn't included in the default set, prepend it.
$header_images = array ();
$header_images [ 'default' ] = array (
'url' => $default ,
'thumbnail_url' => $default ,
'description' => 'Default' ,
);
// The rest of the set comes after.
return array_merge ( $header_images , $this -> default_headers );
}
/**
* Gets the previously uploaded header images .
*
* @ since 3.9 . 0
*
* @ return array Uploaded header images .
*/
public function get_uploaded_header_images () {
$header_images = get_uploaded_header_images ();
$timestamp_key = '_wp_attachment_custom_header_last_used_' . get_stylesheet ();
$alt_text_key = '_wp_attachment_image_alt' ;
foreach ( $header_images as & $header_image ) {
$header_meta = get_post_meta ( $header_image [ 'attachment_id' ] );
$header_image [ 'timestamp' ] = isset ( $header_meta [ $timestamp_key ] ) ? $header_meta [ $timestamp_key ] : '' ;
$header_image [ 'alt_text' ] = isset ( $header_meta [ $alt_text_key ] ) ? $header_meta [ $alt_text_key ] : '' ;
}
return $header_images ;
}
/**
2023-06-05 17:35:20 -04:00
* Gets the ID of a previous crop from the same base image .
2019-07-17 02:17:56 -04:00
*
* @ since 4.9 . 0
*
2022-03-17 16:19:10 -04:00
* @ param array $attachment An array with a cropped attachment object data .
2019-07-17 02:17:56 -04:00
* @ return int | false An attachment ID if one exists . False if none .
*/
2022-03-17 16:19:10 -04:00
public function get_previous_crop ( $attachment ) {
2019-07-17 02:17:56 -04:00
$header_images = $this -> get_uploaded_header_images ();
// Bail early if there are no header images.
if ( empty ( $header_images ) ) {
return false ;
}
$previous = false ;
foreach ( $header_images as $image ) {
2022-03-17 16:19:10 -04:00
if ( $image [ 'attachment_parent' ] === $attachment [ 'post_parent' ] ) {
2019-07-17 02:17:56 -04:00
$previous = $image [ 'attachment_id' ];
break ;
}
}
return $previous ;
}
}