78 * |
78 * |
79 * @since 4.3.0 |
79 * @since 4.3.0 |
80 * |
80 * |
81 * @param string $cropped Cropped image URL. |
81 * @param string $cropped Cropped image URL. |
82 * @param int $parent_attachment_id Attachment ID of parent image. |
82 * @param int $parent_attachment_id Attachment ID of parent image. |
83 * @return array Attachment object. |
83 * @return array An array with attachment object data. |
84 */ |
84 */ |
85 public function create_attachment_object( $cropped, $parent_attachment_id ) { |
85 public function create_attachment_object( $cropped, $parent_attachment_id ) { |
86 $parent = get_post( $parent_attachment_id ); |
86 $parent = get_post( $parent_attachment_id ); |
87 $parent_url = wp_get_attachment_url( $parent->ID ); |
87 $parent_url = wp_get_attachment_url( $parent->ID ); |
88 $url = str_replace( wp_basename( $parent_url ), wp_basename( $cropped ), $parent_url ); |
88 $url = str_replace( wp_basename( $parent_url ), wp_basename( $cropped ), $parent_url ); |
89 |
89 |
90 $size = wp_getimagesize( $cropped ); |
90 $size = wp_getimagesize( $cropped ); |
91 $image_type = ( $size ) ? $size['mime'] : 'image/jpeg'; |
91 $image_type = ( $size ) ? $size['mime'] : 'image/jpeg'; |
92 |
92 |
93 $object = array( |
93 $attachment = array( |
94 'ID' => $parent_attachment_id, |
94 'ID' => $parent_attachment_id, |
95 'post_title' => wp_basename( $cropped ), |
95 'post_title' => wp_basename( $cropped ), |
96 'post_content' => $url, |
96 'post_content' => $url, |
97 'post_mime_type' => $image_type, |
97 'post_mime_type' => $image_type, |
98 'guid' => $url, |
98 'guid' => $url, |
99 'context' => 'site-icon', |
99 'context' => 'site-icon', |
100 ); |
100 ); |
101 |
101 |
102 return $object; |
102 return $attachment; |
103 } |
103 } |
104 |
104 |
105 /** |
105 /** |
106 * Inserts an attachment. |
106 * Inserts an attachment. |
107 * |
107 * |
108 * @since 4.3.0 |
108 * @since 4.3.0 |
109 * |
109 * |
110 * @param array $object Attachment object. |
110 * @param array $attachment An array with attachment object data. |
111 * @param string $file File path of the attached image. |
111 * @param string $file File path of the attached image. |
112 * @return int Attachment ID |
112 * @return int Attachment ID. |
113 */ |
113 */ |
114 public function insert_attachment( $object, $file ) { |
114 public function insert_attachment( $attachment, $file ) { |
115 $attachment_id = wp_insert_attachment( $object, $file ); |
115 $attachment_id = wp_insert_attachment( $attachment, $file ); |
116 $metadata = wp_generate_attachment_metadata( $attachment_id, $file ); |
116 $metadata = wp_generate_attachment_metadata( $attachment_id, $file ); |
117 |
117 |
118 /** |
118 /** |
119 * Filters the site icon attachment metadata. |
119 * Filters the site icon attachment metadata. |
120 * |
120 * |