<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[57755] trunk: Media: Accessibility: Copy attachment properties on site icon crop.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/57755">57755</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/57755","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>joedolson</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2024-03-02 20:13:02 +0000 (Sat, 02 Mar 2024)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Media: Accessibility: Copy attachment properties on site icon crop.
Add parity between site icon, custom header, and default image crop behaviors. <a href="https://core.trac.wordpress.org/changeset/53027">[53027]</a> 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 <a href="https://core.trac.wordpress.org/ticket/60524">#60524</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludesajaxactionsphp">trunk/src/wp-admin/includes/ajax-actions.php</a></li>
<li><a href="#trunksrcwpadminincludesclasscustomimageheaderphp">trunk/src/wp-admin/includes/class-custom-image-header.php</a></li>
<li><a href="#trunksrcwpadminincludesclasswpsiteiconphp">trunk/src/wp-admin/includes/class-wp-site-icon.php</a></li>
<li><a href="#trunksrcwpadminincludesimagephp">trunk/src/wp-admin/includes/image.php</a></li>
<li><a href="#trunktestsphpunittestsimageheaderphp">trunk/tests/phpunit/tests/image/header.php</a></li>
<li><a href="#trunktestsphpunittestsimagesiteIconphp">trunk/tests/phpunit/tests/image/siteIcon.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsmediawpCopyParentAttachmentPropertiesphp">trunk/tests/phpunit/tests/media/wpCopyParentAttachmentProperties.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminincludesajaxactionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/ajax-actions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/ajax-actions.php 2024-03-02 14:11:53 UTC (rev 57754)
+++ trunk/src/wp-admin/includes/ajax-actions.php 2024-03-02 20:13:02 UTC (rev 57755)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4035,10 +4035,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /** This filter is documented in wp-admin/includes/class-custom-image-header.php */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication.
- $attachment = $wp_site_icon->create_attachment_object( $cropped, $attachment_id );
- unset( $attachment['ID'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication.
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Copy attachment properties.
+ $attachment = wp_copy_parent_attachment_properties( $cropped, $attachment_id, $context );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Update the attachment.
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'intermediate_image_sizes_advanced', array( $wp_site_icon, 'additional_sizes' ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $attachment_id = $wp_site_icon->insert_attachment( $attachment, $cropped );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4065,47 +4066,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /** This filter is documented in wp-admin/includes/class-custom-image-header.php */
</span><span class="cx" style="display: block; padding: 0 10px"> $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication.
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $parent_url = wp_get_attachment_url( $attachment_id );
- $parent_basename = wp_basename( $parent_url );
- $url = str_replace( $parent_basename, wp_basename( $cropped ), $parent_url );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Copy attachment properties.
+ $attachment = wp_copy_parent_attachment_properties( $cropped, $attachment_id, $context );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $size = wp_getimagesize( $cropped );
- $image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
-
- // Get the original image's post to pre-populate the cropped image.
- $original_attachment = get_post( $attachment_id );
- $sanitized_post_title = sanitize_file_name( $original_attachment->post_title );
- $use_original_title = (
- ( '' !== trim( $original_attachment->post_title ) ) &&
- /*
- * Check if the original image has a title other than the "filename" default,
- * meaning the image had a title when originally uploaded or its title was edited.
- */
- ( $parent_basename !== $sanitized_post_title ) &&
- ( pathinfo( $parent_basename, PATHINFO_FILENAME ) !== $sanitized_post_title )
- );
- $use_original_description = ( '' !== trim( $original_attachment->post_content ) );
-
- $attachment = array(
- 'post_title' => $use_original_title ? $original_attachment->post_title : wp_basename( $cropped ),
- 'post_content' => $use_original_description ? $original_attachment->post_content : $url,
- 'post_mime_type' => $image_type,
- 'guid' => $url,
- 'context' => $context,
- );
-
- // Copy the image caption attribute (post_excerpt field) from the original image.
- if ( '' !== trim( $original_attachment->post_excerpt ) ) {
- $attachment['post_excerpt'] = $original_attachment->post_excerpt;
- }
-
- // Copy the image alt text attribute from the original image.
- if ( '' !== trim( $original_attachment->_wp_attachment_image_alt ) ) {
- $attachment['meta_input'] = array(
- '_wp_attachment_image_alt' => wp_slash( $original_attachment->_wp_attachment_image_alt ),
- );
- }
-
</del><span class="cx" style="display: block; padding: 0 10px"> $attachment_id = wp_insert_attachment( $attachment, $cropped );
</span><span class="cx" style="display: block; padding: 0 10px"> $metadata = wp_generate_attachment_metadata( $attachment_id, $cropped );
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunksrcwpadminincludesclasscustomimageheaderphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/class-custom-image-header.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/class-custom-image-header.php 2024-03-02 14:11:53 UTC (rev 57754)
+++ trunk/src/wp-admin/includes/class-custom-image-header.php 2024-03-02 20:13:02 UTC (rev 57755)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1077,7 +1077,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /** This filter is documented in wp-admin/includes/class-custom-image-header.php */
</span><span class="cx" style="display: block; padding: 0 10px"> $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication.
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $attachment = $this->create_attachment_object( $cropped, $attachment_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $attachment = wp_copy_parent_attachment_properties( $cropped, $attachment_id, 'custom-header' );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $_POST['create-new-attachment'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> unset( $attachment['ID'] );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1314,6 +1314,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Creates an attachment 'object'.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 3.9.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @deprecated 6.5.0
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $cropped Cropped image URL.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param int $parent_attachment_id Attachment ID of parent image.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1320,6 +1321,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @return array An array with attachment object data.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> final public function create_attachment_object( $cropped, $parent_attachment_id ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ _deprecated_function( __METHOD__, '6.5.0', 'wp_copy_parent_attachment_properties()' );
</ins><span class="cx" style="display: block; padding: 0 10px"> $parent = get_post( $parent_attachment_id );
</span><span class="cx" style="display: block; padding: 0 10px"> $parent_url = wp_get_attachment_url( $parent->ID );
</span><span class="cx" style="display: block; padding: 0 10px"> $url = str_replace( wp_basename( $parent_url ), wp_basename( $cropped ), $parent_url );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1421,7 +1423,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /** This filter is documented in wp-admin/includes/class-custom-image-header.php */
</span><span class="cx" style="display: block; padding: 0 10px"> $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication.
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $attachment = $this->create_attachment_object( $cropped, $attachment_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $attachment = wp_copy_parent_attachment_properties( $cropped, $attachment_id, 'custom-header' );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $previous = $this->get_previous_crop( $attachment );
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunksrcwpadminincludesclasswpsiteiconphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/class-wp-site-icon.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/class-wp-site-icon.php 2024-03-02 14:11:53 UTC (rev 57754)
+++ trunk/src/wp-admin/includes/class-wp-site-icon.php 2024-03-02 20:13:02 UTC (rev 57755)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -78,6 +78,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Creates an attachment 'object'.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.3.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @deprecated 6.5.0
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $cropped Cropped image URL.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param int $parent_attachment_id Attachment ID of parent image.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -84,6 +85,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @return array An array with attachment object data.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function create_attachment_object( $cropped, $parent_attachment_id ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ _deprecated_function( __METHOD__, '6.5.0', 'wp_copy_parent_attachment_properties()' );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $parent = get_post( $parent_attachment_id );
</span><span class="cx" style="display: block; padding: 0 10px"> $parent_url = wp_get_attachment_url( $parent->ID );
</span><span class="cx" style="display: block; padding: 0 10px"> $url = str_replace( wp_basename( $parent_url ), wp_basename( $cropped ), $parent_url );
</span></span></pre></div>
<a id="trunksrcwpadminincludesimagephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/image.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/image.php 2024-03-02 14:11:53 UTC (rev 57754)
+++ trunk/src/wp-admin/includes/image.php 2024-03-02 20:13:02 UTC (rev 57755)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -483,6 +483,62 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Copy parent attachment properties to newly cropped image.
+ *
+ * @since 6.5.0
+ *
+ * @param string $cropped Path to the cropped image file.
+ * @param int $parent_attachment_id Parent file Attachment ID.
+ * @param string $context Control calling the function.
+ * @return array Properties of attachment.
+ */
+function wp_copy_parent_attachment_properties( $cropped, $parent_attachment_id, $context = '' ) {
+ $parent = get_post( $parent_attachment_id );
+ $parent_url = wp_get_attachment_url( $parent->ID );
+ $parent_basename = wp_basename( $parent_url );
+ $url = str_replace( wp_basename( $parent_url ), wp_basename( $cropped ), $parent_url );
+
+ $size = wp_getimagesize( $cropped );
+ $image_type = $size ? $size['mime'] : 'image/jpeg';
+
+ $sanitized_post_title = sanitize_file_name( $parent->post_title );
+ $use_original_title = (
+ ( '' !== trim( $parent->post_title ) ) &&
+ /*
+ * Check if the original image has a title other than the "filename" default,
+ * meaning the image had a title when originally uploaded or its title was edited.
+ */
+ ( $parent_basename !== $sanitized_post_title ) &&
+ ( pathinfo( $parent_basename, PATHINFO_FILENAME ) !== $sanitized_post_title )
+ );
+ $use_original_description = ( '' !== trim( $parent->post_content ) );
+
+ $attachment = array(
+ 'post_title' => $use_original_title ? $parent->post_title : wp_basename( $cropped ),
+ 'post_content' => $use_original_description ? $parent->post_content : $url,
+ 'post_mime_type' => $image_type,
+ 'guid' => $url,
+ 'context' => $context,
+ );
+
+ // Copy the image caption attribute (post_excerpt field) from the original image.
+ if ( '' !== trim( $parent->post_excerpt ) ) {
+ $attachment['post_excerpt'] = $parent->post_excerpt;
+ }
+
+ // Copy the image alt text attribute from the original image.
+ if ( '' !== trim( $parent->_wp_attachment_image_alt ) ) {
+ $attachment['meta_input'] = array(
+ '_wp_attachment_image_alt' => wp_slash( $parent->_wp_attachment_image_alt ),
+ );
+ }
+
+ $attachment['post_parent'] = $parent_attachment_id;
+
+ return $attachment;
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Generates attachment meta data and create image sub-sizes for images.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 2.1.0
</span></span></pre></div>
<a id="trunktestsphpunittestsimageheaderphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/image/header.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/image/header.php 2024-03-02 14:11:53 UTC (rev 57754)
+++ trunk/tests/phpunit/tests/image/header.php 2024-03-02 20:13:02 UTC (rev 57755)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -108,25 +108,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( 1200, $dimensions['dst_height'] );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_create_attachment_object() {
- $id = wp_insert_attachment(
- array(
- 'post_status' => 'publish',
- 'post_title' => 'foo.png',
- 'post_type' => 'post',
- 'guid' => 'http://localhost/foo.png',
- )
- );
-
- $cropped = 'foo-cropped.png';
-
- $object = $this->custom_image_header->create_attachment_object( $cropped, $id );
- $this->assertSame( 'foo-cropped.png', $object['post_title'] );
- $this->assertSame( 'http://localhost/' . $cropped, $object['guid'] );
- $this->assertSame( 'custom-header', $object['context'] );
- $this->assertSame( 'image/jpeg', $object['post_mime_type'] );
- }
-
</del><span class="cx" style="display: block; padding: 0 10px"> public function test_insert_cropped_attachment() {
</span><span class="cx" style="display: block; padding: 0 10px"> $id = wp_insert_attachment(
</span><span class="cx" style="display: block; padding: 0 10px"> array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -138,7 +119,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $cropped = 'foo-cropped.png';
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $object = $this->custom_image_header->create_attachment_object( $cropped, $id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $object = wp_copy_parent_attachment_properties( $cropped, $id, 'custom-header' );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $cropped_id = $this->custom_image_header->insert_attachment( $object, $cropped );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -161,7 +142,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Create inital crop object.
</span><span class="cx" style="display: block; padding: 0 10px"> $cropped_1 = 'foo-cropped-1.png';
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $object = $this->custom_image_header->create_attachment_object( $cropped_1, $id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $object = wp_copy_parent_attachment_properties( $cropped_1, $id, 'custom-header' );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Ensure no previous crop exists.
</span><span class="cx" style="display: block; padding: 0 10px"> $previous = $this->custom_image_header->get_previous_crop( $object );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -175,7 +156,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Create second crop.
</span><span class="cx" style="display: block; padding: 0 10px"> $cropped_2 = 'foo-cropped-2.png';
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $object = $this->custom_image_header->create_attachment_object( $cropped_2, $id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $object = wp_copy_parent_attachment_properties( $cropped_2, $id );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Test that a previous crop is found.
</span><span class="cx" style="display: block; padding: 0 10px"> $previous = $this->custom_image_header->get_previous_crop( $object );
</span></span></pre></div>
<a id="trunktestsphpunittestsimagesiteIconphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/image/siteIcon.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/image/siteIcon.php 2024-03-02 14:11:53 UTC (rev 57754)
+++ trunk/tests/phpunit/tests/image/siteIcon.php 2024-03-02 20:13:02 UTC (rev 57755)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -98,26 +98,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> unset( $this->wp_site_icon->site_icon_sizes[ array_search( 321, $this->wp_site_icon->site_icon_sizes, true ) ] );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_create_attachment_object() {
- $attachment_id = $this->insert_attachment();
- $parent_url = get_post( $attachment_id )->guid;
- $cropped = str_replace( wp_basename( $parent_url ), 'cropped-test-image.jpg', $parent_url );
-
- $object = $this->wp_site_icon->create_attachment_object( $cropped, $attachment_id );
-
- $this->assertSame( $object['post_title'], 'cropped-test-image.jpg' );
- $this->assertSame( $object['context'], 'site-icon' );
- $this->assertSame( $object['post_mime_type'], 'image/jpeg' );
- $this->assertSame( $object['post_content'], $cropped );
- $this->assertSame( $object['guid'], $cropped );
- }
-
</del><span class="cx" style="display: block; padding: 0 10px"> public function test_insert_cropped_attachment() {
</span><span class="cx" style="display: block; padding: 0 10px"> $attachment_id = $this->insert_attachment();
</span><span class="cx" style="display: block; padding: 0 10px"> $parent_url = get_post( $attachment_id )->guid;
</span><span class="cx" style="display: block; padding: 0 10px"> $cropped = str_replace( wp_basename( $parent_url ), 'cropped-test-image.jpg', $parent_url );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $object = $this->wp_site_icon->create_attachment_object( $cropped, $attachment_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $object = wp_copy_parent_attachment_properties( $cropped, $attachment_id, 'site-icon' );
</ins><span class="cx" style="display: block; padding: 0 10px"> $cropped_id = $this->wp_site_icon->insert_attachment( $object, $cropped );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertIsInt( $cropped_id );
</span></span></pre></div>
<a id="trunktestsphpunittestsmediawpCopyParentAttachmentPropertiesphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/media/wpCopyParentAttachmentProperties.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/media/wpCopyParentAttachmentProperties.php (rev 0)
+++ trunk/tests/phpunit/tests/media/wpCopyParentAttachmentProperties.php 2024-03-02 20:13:02 UTC (rev 57755)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,52 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * Tests for the `wp_copy_parent_attachment_properties()` function.
+ *
+ * @group media
+ * @covers ::wp_copy_parent_attachment_properties
+ */
+class Tests_Media_wpCopyParentAttachmentProperties extends WP_UnitTestCase {
+
+ public function tear_down() {
+ $this->remove_added_uploads();
+
+ parent::tear_down();
+ }
+
+ public function test_wp_copy_parent_attachment_properties() {
+ $attachment = $this->factory->attachment->create_upload_object( DIR_TESTDATA . '/images/canola.jpg' );
+ $parent_url = get_post( $attachment )->guid;
+ // Add alternative text.
+ update_post_meta( $attachment, '_wp_attachment_image_alt', 'Alt text' );
+ // Add image description.
+ wp_update_post(
+ array(
+ 'ID' => $attachment,
+ 'post_excerpt' => 'Image description',
+ )
+ );
+ $file = wp_crop_image(
+ DIR_TESTDATA . '/images/canola.jpg',
+ 0,
+ 0,
+ 100,
+ 100,
+ 100,
+ 100
+ );
+
+ $object = wp_copy_parent_attachment_properties( $file, $attachment );
+ $cropped = str_replace( wp_basename( $parent_url ), 'cropped-canola.jpg', $parent_url );
+
+ $this->assertSame( $object['post_title'], 'cropped-canola.jpg', 'Attachment title is not identical' );
+ $this->assertSame( $object['context'], '', 'Attachment context is not identical' );
+ $this->assertSame( $object['post_mime_type'], 'image/jpeg', 'Attachment mime type is not identical' );
+ $this->assertSame( $object['post_content'], $cropped, 'Attachment content is not identical' );
+ $this->assertSame( $object['guid'], $cropped, 'Attachment GUID is not identical' );
+ $this->assertSame( $object['meta_input']['_wp_attachment_image_alt'], 'Alt text', 'Attachment alt text is not identical' );
+ $this->assertSame( $object['post_excerpt'], 'Image description', 'Attachment description is not identical' );
+
+ unlink( $file );
+ }
+}
</ins></span></pre>
</div>
</div>
</body>
</html>