attachment.php
8.16 KB
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
<?php
/**
* Display SVG in attachment modal
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
function bodhi_svgs_response_for_svg( $response, $attachment, $meta ) {
if ( $response['mime'] == 'image/svg+xml' && empty( $response['sizes'] ) ) {
$svg_path = get_attached_file( $attachment->ID );
if ( ! file_exists( $svg_path ) ) {
// If SVG is external, use the URL instead of the path
$svg_path = $response['url'];
}
$dimensions = bodhi_svgs_get_dimensions( $svg_path );
$response['sizes'] = array(
'full' => array(
'url' => $response['url'],
'width' => $dimensions->width,
'height' => $dimensions->height,
'orientation' => $dimensions->width > $dimensions->height ? 'landscape' : 'portrait'
)
);
}
return $response;
}
add_filter( 'wp_prepare_attachment_for_js', 'bodhi_svgs_response_for_svg', 10, 3 );
function bodhi_svgs_get_dimensions( $svg ) {
$svg = simplexml_load_file( $svg );
if ( $svg === FALSE ) {
$width = '0';
$height = '0';
} else {
$attributes = $svg->attributes();
$width = (string) $attributes->width;
$height = (string) $attributes->height;
}
return (object) array( 'width' => $width, 'height' => $height );
}
/**
* Generate attachment metadata (Thanks @surml)
* Fixes Illegal String Offset Warning for Height & Width
*/
function bodhi_svgs_generate_svg_attachment_metadata( $metadata, $attachment_id ) {
$mime = get_post_mime_type( $attachment_id );
if ( $mime == 'image/svg+xml' ) {
$svg_path = get_attached_file( $attachment_id );
$upload_dir = wp_upload_dir();
// get the path relative to /uploads/ - found no better way:
$relative_path = str_replace($upload_dir['basedir'], '', $svg_path);
$filename = basename( $svg_path );
$dimensions = bodhi_svgs_get_dimensions( $svg_path );
$metadata = array(
'width' => intval($dimensions->width),
'height' => intval($dimensions->height),
'file' => $filename
);
$height = intval($dimensions->height);
$width = intval($dimensions->width);
// Might come in handy to create the sizes array too - But it's not needed for this workaround! Always links to original svg-file => Hey, it's a vector graphic! ;)
$sizes = array();
foreach ( get_intermediate_image_sizes() as $s ) {
$sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => false );
// for svg with width and height set, we need to adjust the thumbnails accordingly
if ( $width !== 0 && $height !== 0 ) {
// follow width of request size e.g. 150, 300 etc..
if ( isset( $_wp_additional_image_sizes[$s]['width'] ) ) {
$width_current_size = intval( $_wp_additional_image_sizes[$s]['width'] );
} else {
$width_current_size = get_option( "{$s}_size_w" );
}
// we have dimensions available. Use them
if ( $width > $height ) {
$ratio = round($width / $height,2);
$new_height = round($width_current_size / $ratio);
} else {
$ratio = round($height / $width,2);
$new_height = round($width_current_size * $ratio);
}
$sizes[$s]['width'] = $width_current_size;
$sizes[$s]['height'] = $new_height;
// svgs can't be cropped by WP
$sizes[$s]['crop'] = false;
} else {
// no change is needed
if ( isset( $_wp_additional_image_sizes[$s]['width'] ) ) {
$sizes[$s]['width'] = intval( $_wp_additional_image_sizes[$s]['width'] ); // For theme-added sizes
} else {
$sizes[$s]['width'] = get_option( "{$s}_size_w" ); // For default sizes set in options
}
if ( isset( $_wp_additional_image_sizes[$s]['height'] ) ) {
$sizes[$s]['height'] = intval( $_wp_additional_image_sizes[$s]['height'] ); // For theme-added sizes
} else {
$sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options
}
if ( isset( $_wp_additional_image_sizes[$s]['crop'] ) ) {
$sizes[$s]['crop'] = intval( $_wp_additional_image_sizes[$s]['crop'] ); // For theme-added sizes
} else {
$sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options
}
}
$sizes[$s]['file'] = $filename;
$sizes[$s]['mime-type'] = 'image/svg+xml';
}
$metadata['sizes'] = $sizes;
}
return $metadata;
}
add_filter( 'wp_generate_attachment_metadata', 'bodhi_svgs_generate_svg_attachment_metadata', 10, 3 );
/*
* SVG Sanitization
* Only triggers when its enabled by admin
*/
function bodhi_svgs_sanitize( $file ){
global $sanitizer;
$sanitizer->setAllowedTags( new bodhi_svg_tags() );
$sanitizer->setAllowedAttrs( new bodhi_svg_attributes() );
$dirty = file_get_contents( $file );
// try to decode if gzipped is enabled
if ( $is_zipped = bodhi_svgs_is_gzipped( $dirty ) ) {
$dirty = gzdecode( $dirty );
// return on failure, since we can't read file
if ( $dirty === false ) {
return false;
}
}
// remove remote references since they are dangerous and lead to injection
$sanitizer->removeRemoteReferences(true);
// enable minify in library if its enabled in admin panel
bodhi_svgs_minify();
$clean = $sanitizer->sanitize( $dirty );
if ( $clean === false ) {
return false;
}
// if we were gzipped, we need to re-zip
if ( $is_zipped ) {
$clean = gzencode( $clean );
}
file_put_contents( $file, $clean );
return true;
}
function bodhi_svgs_minify( ) {
global $bodhi_svgs_options;
global $sanitizer;
if ( !empty($bodhi_svgs_options['minify_svg']) && $bodhi_svgs_options['minify_svg'] === 'on' ) {
$sanitizer->minify(true);
}
}
function bodhi_svgs_is_gzipped( $contents ) {
if ( function_exists( 'mb_strpos' ) ) {
return 0 === mb_strpos( $contents, "\x1f" . "\x8b" . "\x08" );
} else {
return 0 === strpos( $contents, "\x1f" . "\x8b" . "\x08" );
}
}
function bodhi_svgs_sanitize_svg( $file ){
global $bodhi_svgs_options;
if ( !empty($bodhi_svgs_options['sanitize_svg']) && $bodhi_svgs_options['sanitize_svg'] === 'on' ) {
if ( $file['type'] === 'image/svg+xml' ) {
$sanitize_on_upload_roles_array = array();
$should_sanitize_svg = array();
$sanitize_on_upload_roles_array = (array) $bodhi_svgs_options['sanitize_on_upload_roles'];
$user = wp_get_current_user();
$current_user_roles = ( array ) $user->roles;
$should_sanitize_svg = array_intersect($sanitize_on_upload_roles_array, $current_user_roles);
if( empty($should_sanitize_svg) ) {
$file['error'] = __( "Sorry, this file couldn't be sanitized so for security reasons and wasn't uploaded.",
'safe-svg' );
}
elseif ( ! bodhi_svgs_sanitize( $file['tmp_name'] ) ) {
$file['error'] = __( "Sorry, this file couldn't be sanitized so for security reasons and wasn't uploaded",
'safe-svg' );
}
}
}
return $file;
}
// Sanitize svg if user has enabled option
add_filter( 'wp_handle_upload_prefilter', 'bodhi_svgs_sanitize_svg' );
// Fix image widget PHP warnings
function bodhi_svgs_get_attachment_metadata( $data ) {
$res = $data;
if ( !isset( $data['width'] ) || !isset( $data['height'] ) ) {
$res = false;
}
return $res;
}
// add_filter( 'wp_get_attachment_metadata' , 'bodhi_svgs_get_attachment_metadata' );
// Commented this out 20200307 because it was stripping metadata from other attachments as well. Need to make this target only SVG attachments.
// remove srcset for svg images
function bodhi_svgs_disable_srcset( $sources ) {
$first_element = reset($sources);
if ( isset($first_element) && !empty($first_element['url']) ) {
$ext = pathinfo(reset($sources)['url'], PATHINFO_EXTENSION);
if ( $ext == 'svg' ) {
// return empty array
$sources = array();
return $sources;
} else {
return $sources;
}
} else {
return $sources;
}
}
add_filter( 'wp_calculate_image_srcset', 'bodhi_svgs_disable_srcset' );
// fix for division by zero error for SVGs
// proposed by starsis
// https://github.com/WordPress/gutenberg/issues/36603
function bodhi_svgs_dimension_fallback( $image, $attachment_id, $size, $icon ) {
// only manipulate for svgs
if ( get_post_mime_type($attachment_id) == 'image/svg+xml' ) {
if ( isset($image[1]) && $image[1] === 0 ) {
$image[1] = 1;
}
if ( isset($image[2]) && $image[2] === 0 ) {
$image[2] = 1;
}
}
return $image;
}
add_filter( 'wp_get_attachment_image_src', 'bodhi_svgs_dimension_fallback', 10, 4 );