-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-media-metadata-fix.php
340 lines (272 loc) · 11.7 KB
/
wp-media-metadata-fix.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<?php
/**
* Plugin Name: WP Media Metadata Fix
* Plugin URI: https://wordpress.org/plugins/wp-media-metadata-fix/
* Description: Fixes the metadata of the images in the Media library.
* Version: 1.0
* Author: Malik Naik
* Author URI: http://maliknaik.me/
* License: GNU General Public License v3
* License URI: https://www.gnu.org/licenses/gpl-3.0.en.html
* Text Domain: wp-media-metadata-fix
* Domain Path: /languages
*/
// Make sure we don't expose any info if called directly
defined('ABSPATH') || exit;
class WPMediaMetadataFix {
/**
* Instantiate the object.
*/
public function __construct() {
add_action( 'plugins_loaded', [ $this, 'initialize' ] );
add_action( 'wp_loaded', [ $this, 'initialize' ] );
}
/**
* Initialize the plugin.
*/
public function initialize() {
add_filter( 'manage_upload_columns', [ $this, 'addMetadataColumn' ] );
add_action( 'manage_media_custom_column', [ $this, 'renderMetadata' ], 10, 2 );
add_action( 'wp_loaded', [ $this, 'fixMetadata' ] );
add_action( 'add_meta_boxes', [ $this, 'addMetadataMetaBox' ] );
}
/**
* Adds a metadata column to the media library listing.
*
* @param array $columns
*
* @return array
*/
public function addMetadataColumn( $columns ) {
$columns['wpmmf_metadata'] = 'Metadata';
return $columns;
}
/**
* Renders the metadata.
*
* @param string $column_name
* @param int $post_id
*
* @return void
*/
public function renderMetadata( $column_name, $post_id ) {
// Set the error handler to catch warnings.
set_error_handler(function( $severity, $message, $file, $line ) {
throw new \ErrorException( $message, $severity, $severity, $file, $line );
});
try{
$output = '';
if ( 'wpmmf_metadata' !== $column_name || !wp_attachment_is_image( $post_id ) ) {
if ( false !== ($btn = $this->getMissingFileBtn( $post_id ) ) ) {
// Render fix image button.
_e( $btn, 'wp-media-metadata-fix' );
}
// Restore the error handler.
restore_error_handler();
return;
}
// Get the image url from post id.
$image_url = wp_get_attachment_image_src( $post_id, 'original' );
if ( false === $image_url ) {
$this->renderFixButton( $post_id, $output, '<b>_wp_attached_file</b> meta key missing in the database' );
restore_error_handler();
// Render fix image meta key button.
_e( $output, 'wp-media-metadata-fix' );
return;
}
// Get the image url.
$image_url = $image_url[0];
if (!file_exists( get_attached_file( $post_id ) )) {
$output .= 'Original File missing.';
restore_error_handler();
// Render the missing file message.
_e( $output, 'wp-media-metadata-fix' );
return;
}
// Get the image metadata into the $output.
$this->getImageMetadata( $post_id, $output );
// Restore the error handler.
restore_error_handler();
// Render the image metadata.
_e( $output, 'wp-media-metadata-fix' );
return;
} catch( \Exception $e ) {
// Render the exception.
_e( $e->getMessage(), 'wp-media-metadata-fix' );
}
}
/**
* Fixes the metadata of the attachment.
*
* @return void
*/
public function fixMetadata() {
$attachment_id = NULL;
if ( isset( $_GET['wpmmf_attachment_id'] ) ) {
// Get the attachement id from the query string.
$filtered_attachment_id = filter_input(INPUT_GET, 'wpmmf_attachment_id', FILTER_VALIDATE_INT); // $_GET['wpmmf_attachment_id'];
if ( $filtered_attachment_id !== false ) {
$attachment_id = $filtered_attachment_id;
}
}
if ( isset( $_GET['wpmmf_nonce'] ) ) {
// Get the nonce from the query string.
$wpmmf_nonce = $_GET['wpmmf_nonce'];
}
if ( !empty( $attachment_id ) && !empty( $wpmmf_nonce ) ) {
if ( wp_verify_nonce( $wpmmf_nonce, 'wpmmf_attachment_id' ) ) {
// Get the post meta with the meta key `_wp_attached_file`.
$attached_file = get_post_meta( $attachment_id, '_wp_attached_file' );
if ( array_key_exists( 'wpmmf_file', $_GET ) && count( $attached_file ) < 1 || $attached_file[0] === NULL ) {
// Get the file path from the attachment/post id.
$meta_value = $this->getFilePathFromPostID( $attachment_id );
// Insert a new post meta with meta key `_wp_attached_file`.
add_post_meta( $attachment_id, '_wp_attached_file', $meta_value, true );
}
if ( !function_exists( 'wp_generate_attachment_metadata' ) || !function_exists( 'wp_update_attachment_metadata' ) ) {
include( ABSPATH . 'wp-admin/includes/image.php' );
}
// Generate the attachment metadata.
$metadata = wp_generate_attachment_metadata( $attachment_id, get_attached_file( $attachment_id ) );
// Update the attachment metadata.
wp_update_attachment_metadata( $attachment_id, $metadata );
// Log the fixed attachment id.
error_log( "Fixed metadata for the attachment id $attachment_id" );
}
}
// Remove some query string from the URI.
$_SERVER['REQUEST_URI'] = remove_query_arg( array( 'wpmmf_attachment_id', 'wpmmf_nonce', 'wpmmf_file' ), $_SERVER['REQUEST_URI'] );
}
/**
* Adds the meta box to the attachment page to the right.
*
* @return void
*/
public function addMetadataMetaBox() {
add_meta_box( 'wpmmf_metabox', __( 'Metadata' ), [ $this, 'renderMetabox' ], 'attachment', 'side' );
}
/**
* Renders the metadata in the meta box.
*
* @param object $post
*
* @return void
*/
public function renderMetabox( $post ) {
$this->renderMetadata( 'wpmmf_metadata', $post->ID );
}
/**
* Returns the relative path from the post id of the attachment.
*
* @param int $post_id
*
* @return string
*/
public function getFilePathFromPostID( $post_id ) {
// Get the image url.
$image_url = get_the_guid( $post_id );
// Parse the url.
$parsed_url = parse_url( $image_url );
// Get the subdirectory of the image.
$subdir = ltrim(wp_get_upload_dir()['subdir'], '/');
// Get the filename of the image.
$file_name = wp_basename($image_url);
// Join the subdirectory and the filename.
$file_path = path_join( $subdir, $file_name );
return $file_path;
}
/**
* Returns the fix image button.
*
* @param int $post_id
*
* @return bool|string
*/
public function getMissingFileBtn( $post_id ) {
$output = '';
$attached_file = get_post_meta( $post_id, '_wp_attached_file' );
if ( count( $attached_file ) < 1 || $attached_file[0] === NULL ) {
// Get the mime type of the post.
$mime_type = get_post_mime_type( $post_id );
if ( false !== strpos( $mime_type, 'image' ) ) {
// Get allowed mime types.
$allowed_mime_types = get_allowed_mime_types();
if ( in_array( $mime_type, $allowed_mime_types ) ) {
$this->renderFixButton( $post_id, $output, 'Image meta key missing in the database.', 'Fix Image', true );
return $output;
}
}
} else {
return false;
}
}
/**
* Initializes the html string for the image metadata.
*
* @param int $post_id
* @param string &$output
*
* @return void
*/
public function getImageMetadata( $post_id, &$output ) {
// Get the image meta data.
$image_metadata = get_post_meta( $post_id );
if ( is_array( $image_metadata ) ) {
// Check if the meta key '_wp_attached_file' exists and then get the file.
if ( array_key_exists( '_wp_attached_file', $image_metadata ) &&
count( $image_metadata['_wp_attached_file']) > 0 ) {
$output .= sprintf( "<b>File:</b> %s<br/>", $image_metadata['_wp_attached_file'][0] );
} else {
$output .= sprintf("File name missing<br/>");
}
// Check if the meta key '_wp_attachment_metadata' exists then print the meta data of the image.
if ( array_key_exists( '_wp_attachment_metadata', $image_metadata ) &&
count( $image_metadata['_wp_attachment_metadata'] ) > 0 &&
is_serialized( $image_metadata['_wp_attachment_metadata'][0] ) ) {
// Get the unserialized metadata.
$unserialized_metadata = maybe_unserialize( $image_metadata['_wp_attachment_metadata'][0] );
if ( is_array( $unserialized_metadata ) ) {
// Get width of the image.
if ( array_key_exists( 'width', $unserialized_metadata ) ) {
$output .= sprintf("<b>Width</b>: %s px<br/>", $unserialized_metadata['width']);
} else {
$output .= sprintf("<b>Width</b>: Missing</br>");
}
// Get height of the image.
if ( array_key_exists( 'height', $unserialized_metadata ) ) {
$output .= sprintf("<b>Height</b>: %s px<br/>", $unserialized_metadata['height']);
} else {
$output .= sprintf("<b>Height</b>: Missing</br>");
}
}
} else {
$this->renderFixButton( $post_id, $output, 'Image metadata is missing in the database', 'Fix metadata' );
}
}
}
/**
* Renders the button to fix the image metadata.
*
* @param int $post_id
* @param string &$output
* @param string $msg
* @param string $button_string
* @param bool $insert_file
*
* @return bool|string
*/
public function renderFixButton( $post_id, &$output, $msg, $button_string, $insert_file = false ) {
$output .= sprintf("<p>%s</p>", $msg);
$paged = isset( $_GET['paged'] ) ? '&paged=' . sanitize_text_field( $_GET['paged'] ) : "";
$page = isset( $_GET['page'] ) ? '&page=' . sanitize_text_field( $_GET['page'] ) : "";
$post = isset( $_GET['post'] ) ? '&post=' . sanitize_text_field( $_GET['post'] ) : "";
$action = isset( $_GET['action'] ) ? '&action=' . sanitize_text_field( $_GET['action'] ) : "";
$item = isset( $_GET['item'] ) ? '&item=' . sanitize_text_field( $_GET['item'] ) : "";
$wpmmf_id = sprintf( "&wpmmf_attachment_id=%s", $post_id );
$insert_file_qs = $insert_file ? "&wpmmf_file=1" : "";
$url_path = sprintf( "?%s%s%s%s%s%s%s", $page, $item, $paged, $post, $action, $wpmmf_id, $insert_file_qs );
$nonce_url = wp_nonce_url( $url_path, 'wpmmf_attachment_id', 'wpmmf_nonce' );
$output .= sprintf("<a href='%s' class='button button-primary button-small'>%s</a>", esc_url( $nonce_url ), __( $button_string, 'wp-media-metadata-fix' ) );
}
}
$wpmmf = new WPMediaMetadataFix();