137 | | $ext = '.jpg'; |
138 | | switch ( $metadata['image']['mime'] ) { |
139 | | case 'image/gif': |
140 | | $ext = '.gif'; |
141 | | break; |
142 | | case 'image/png': |
143 | | $ext = '.png'; |
144 | | break; |
| 137 | // check for existing cover |
| 138 | $hash = md5( $metadata['image']['data'] ); |
| 139 | $posts = get_posts( array( |
| 140 | 'fields' => 'ids', |
| 141 | 'post_type' => 'attachment', |
| 142 | 'post_mime_type' => $metadata['image']['mime'], |
| 143 | 'post_status' => 'inherit', |
| 144 | 'posts_per_page' => 1, |
| 145 | 'meta_key' => '_cover_hash', |
| 146 | 'meta_value' => $hash |
| 147 | ) ); |
| 148 | $exists = reset( $posts ); |
| 149 | |
| 150 | if ( ! empty( $exists ) ) { |
| 151 | update_post_meta( $attachment_id, '_thumbnail_id', $exists ); |
| 152 | } else { |
| 153 | $ext = '.jpg'; |
| 154 | switch ( $metadata['image']['mime'] ) { |
| 155 | case 'image/gif': |
| 156 | $ext = '.gif'; |
| 157 | break; |
| 158 | case 'image/png': |
| 159 | $ext = '.png'; |
| 160 | break; |
| 161 | } |
| 162 | $basename = str_replace( '.', '-', basename( $file ) ) . '-image' . $ext; |
| 163 | $uploaded = wp_upload_bits( $basename, '', $metadata['image']['data'] ); |
| 164 | if ( false === $uploaded['error'] ) { |
| 165 | $image_attachment = array( |
| 166 | 'post_mime_type' => $metadata['image']['mime'], |
| 167 | 'post_type' => 'attachment', |
| 168 | 'post_content' => '', |
| 169 | ); |
| 170 | /** |
| 171 | * Filter the parameters for the attachment thumbnail creation. |
| 172 | * |
| 173 | * @since 3.9.0 |
| 174 | * |
| 175 | * @param array $image_attachment An array of parameters to create the thumbnail. |
| 176 | * @param array $metadata Current attachment metadata. |
| 177 | * @param array $uploaded An array containing the thumbnail path and url. |
| 178 | */ |
| 179 | $image_attachment = apply_filters( 'attachment_thumbnail_args', $image_attachment, $metadata, $uploaded ); |
| 180 | |
| 181 | $sub_attachment_id = wp_insert_attachment( $image_attachment, $uploaded['file'] ); |
| 182 | add_post_meta( $sub_attachment_id, '_cover_hash', $hash ); |
| 183 | $attach_data = wp_generate_attachment_metadata( $sub_attachment_id, $uploaded['file'] ); |
| 184 | wp_update_attachment_metadata( $sub_attachment_id, $attach_data ); |
| 185 | update_post_meta( $attachment_id, '_thumbnail_id', $sub_attachment_id ); |
| 186 | } |
146 | | $basename = str_replace( '.', '-', basename( $file ) ) . '-image' . $ext; |
147 | | $uploaded = wp_upload_bits( $basename, '', $metadata['image']['data'] ); |
148 | | if ( false === $uploaded['error'] ) { |
149 | | $attachment = array( |
150 | | 'post_mime_type' => $metadata['image']['mime'], |
151 | | 'post_type' => 'attachment', |
152 | | 'post_content' => '', |
153 | | ); |
154 | | $sub_attachment_id = wp_insert_attachment( $attachment, $uploaded['file'] ); |
155 | | $attach_data = wp_generate_attachment_metadata( $sub_attachment_id, $uploaded['file'] ); |
156 | | wp_update_attachment_metadata( $sub_attachment_id, $attach_data ); |
157 | | update_post_meta( $attachment_id, '_thumbnail_id', $sub_attachment_id ); |
158 | | } |