| 2197 | |
| 2198 | /** |
| 2199 | * If an attachment is missing its metadata, try to regenerate it |
| 2200 | * |
| 2201 | * @param post $attachment Post object. |
| 2202 | */ |
| 2203 | function maybe_regenerate_attachment_metadata( $attachment ) { |
| 2204 | if ( empty( $attachment ) || ( empty( $attachment->ID ) || ! $attachment_id = (int) $attachment->ID ) ) { |
| 2205 | return; |
| 2206 | } |
| 2207 | |
| 2208 | $file = get_attached_file( $attachment_id ); |
| 2209 | $meta = wp_get_attachment_metadata( $attachment_id ); |
| 2210 | if ( empty( $meta ) && file_exists( $file ) ) { |
| 2211 | $_meta = get_post_meta( $attachment_id ); |
| 2212 | $regeneration_lock = 'wp_regenerating_' . $attachment_id; |
| 2213 | if ( ! array_key_exists( '_wp_attachment_metadata', $_meta ) && ! get_transient( $regeneration_lock ) ) { |
| 2214 | set_transient( $regeneration_lock, $file ); |
| 2215 | wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) ); |
| 2216 | delete_transient( $regeneration_lock ); |
| 2217 | } |
| 2218 | } |
| 2219 | } |
| 2220 | No newline at end of file |