forked from WordPress/phpdoc-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSourceTypeTerm.php
More file actions
311 lines (281 loc) · 10.7 KB
/
SourceTypeTerm.php
File metadata and controls
311 lines (281 loc) · 10.7 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
<?php
namespace Aivec\Plugins\DocParser;
/**
* Source type additional term
*/
class SourceTypeTerm
{
/**
* Initialize
*
* @author Seiyu Inoue <s.inoue@aivec.co.jp>
* @return void
*/
public static function init() {
$source_type_tax = avcpdp_get_source_type_taxonomy_slug();
add_action("{$source_type_tax}_edit_form_fields", [get_class(), 'addReferenceSourceFilesPathEditField'], 10, 1);
add_action("edit_{$source_type_tax}", [get_class(), 'updateReferenceSourceFilesPath'], 10, 1);
add_action("{$source_type_tax}_term_edit_form_tag", [get_class(), 'addFormAttributeEncTypeMultiPart']);
add_action("{$source_type_tax}_edit_form_fields", [get_class(), 'addFieldsItemImage'], 11, 1);
add_action("edit_{$source_type_tax}", [get_class(), 'updateItemImageTermMeta'], 11, 1);
// Upload svg
add_filter('upload_mimes', [get_class(), 'addMimesTypeSvg'], 99);
add_filter('wp_check_filetype_and_ext', [get_class(), 'wpCheckFileTypeSvg'], 10, 4);
add_filter('wp_generate_attachment_metadata', [get_class(), 'generateSvgAttachmentMetaData'], 10, 3);
add_filter('wp_prepare_attachment_for_js', [get_class(), 'response4Svg'], 10, 2);
}
/**
* Adds reference source file path edit field
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param \WP_Term $term
* @return void
*/
public static function addReferenceSourceFilesPathEditField($term) {
$docsrcp = (string)get_term_meta($term->term_id, 'wp_parser_root_import_dir', true);
?>
<tr class="form-field">
<th scope="row">
<label for="doc_src_path"><?php _e('Path to Reference Source Files', 'wp-parser'); ?></label>
</th>
<td>
<input
type="text"
name="doc_src_path"
id="doc_src_path"
value="<?php echo esc_attr($docsrcp); ?>"
/>
</td>
</tr>
<?php
}
/**
* Adds/updates/deletes reference source file path
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param int $term_id
* @return void
*/
public static function updateReferenceSourceFilesPath($term_id) {
if (!isset($_POST['doc_src_path'])) {
return;
}
$docsrcp = sanitize_text_field((string)$_POST['doc_src_path']);
if (empty($docsrcp)) {
delete_term_meta($term_id, 'wp_parser_root_import_dir');
return;
}
update_term_meta($term_id, 'wp_parser_root_import_dir', $docsrcp);
}
/**
* Adds form attribute to term edit page
*
* @author Seiyu Inoue <s.inoue@aivec.co.jp>
* @return void
*/
public static function addFormAttributeEncTypeMultiPart() {
echo ' enctype="multipart/form-data"';
}
/**
* Adds item image pick field to term edit page
*
* @author Seiyu Inoue <s.inoue@aivec.co.jp>
* @param \WP_Term $term
* @return void
*/
public static function addFieldsItemImage($term) {
$max_upload_size = wp_max_upload_size() ?: 0;
$term_meta = get_term_meta($term->term_id, 'item_image', true);
?>
<tr class="form-field term-image-wrap">
<th scope="row"><label for="parent"><?php _e('Item Image', 'wp-parser'); ?></label></th>
<td>
<input type="file" name="item_image" id="item_image" multiple="false" accept="image/svg+xml"/>
<p><?php _e('Pick a item image to make an association.(svg Only)', 'wp-parser'); ?></p>
<p class="max-upload-size">
<?php printf(
__('Maximum upload file size: %s.'),
esc_html(size_format($max_upload_size))
);
?>
</p>
<?php
if ($term_meta) {
echo '<p><a href="' . $term_meta['url'] . '" target="_blank">' . $term_meta['url'] . '</a></p>';
}
?>
</td>
</tr>
<?php
}
/**
* Upload item image
*
* @author Seiyu Inoue <s.inoue@aivec.co.jp>
* @param int $term_id
* @return void
*/
public static function updateItemImageTermMeta($term_id) {
if (empty($_FILES['item_image']['name'])) {
return;
}
$file = $_FILES['item_image'];
$file_type = wp_check_filetype($file['name']);
if ($file_type['ext'] != 'svg') {
wp_die('Unsupported file type. (Supported file type: .svg)');
}
if (!function_exists('wp_handle_upload')) {
require_once(ABSPATH . 'wp-admin/includes/file.php');
}
$overrides = ['test_form' => false];
$upload = wp_handle_upload($file, $overrides);
if (isset($upload['error']) && $upload['error']) {
wp_die('Upload error : ' . $upload['error']);
}
$term_meta = get_term_meta($term_id, 'item_image', true);
if ($term_meta) {
@unlink($term_meta['file']);
}
$term_meta = [
'file' => $upload['file'],
'url' => $upload['url'],
'type' => $upload['type'],
];
update_term_meta($term_id, 'item_image', $term_meta);
}
/**
* Add mime type svg
*
* @author Seiyu Inoue <s.inoue@aivec.co.jp>
* @param array $mimes Optional. Array of allowed mime types keyed by their file extension regex.
* @return array $mimes Optional. Array of allowed mime types keyed by their file extension regex.
*/
public static function addMimesTypeSvg($mimes = []) {
$mimes['svg'] = 'image/svg+xml';
return $mimes;
}
/**
* Additional check corresponding to file extension svg
*
* @author Seiyu Inoue <s.inoue@aivec.co.jp>
* @param mixed $checked filter wp_check_filetype_and_ext return args
* @param mixed $file Full path to the file.
* @param mixed $filename The name of the file (may differ from $file due to $file being in a tmp directory).
* @param mixed $mimes Optional. Array of allowed mime types keyed by their file extension regex.
* @return mixed {
* Values for the extension, mime type, and corrected filename.
* @type string|false $ext File extension, or false if the file doesn't match a mime type.
* @type string|false $type File mime type, or false if the file doesn't match a mime type.
* @type string|false $proper_filename File name with its correct extension, or false if it cannot be determined.
* }
*/
public static function wpCheckFileTypeSvg($checked, $file, $filename, $mimes) {
if ($checked['type']) {
return $checked;
}
$check_filetype = wp_check_filetype($filename, $mimes);
$ext = $check_filetype['ext'];
$type = $check_filetype['type'];
$proper_filename = $filename;
if ($type && 0 === strpos($type, 'image/') && $ext !== 'svg') {
$ext = $type = false;
}
$checked = compact('ext', 'type', 'proper_filename');
return $checked;
}
/**
* Generate
*
* @author Seiyu Inoue <s.inoue@aivec.co.jp>
* @param mixed $metadata
* @param mixed $attachment_id
* @return mixed
*/
public static function generateSvgAttachmentMetaData($metadata, $attachment_id) {
$mime = get_post_mime_type($attachment_id);
if ($mime !== 'image/svg+xml') {
return $metadata;
}
$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 = self::getSvgDimensions($svg_path);
$metadata = [
'width' => intval($dimensions->width),
'height' => intval($dimensions->height),
'file' => $relative_path,
];
$sizes = [];
foreach (get_intermediate_image_sizes() as $s) {
$sizes[$s] = [
'width' => '',
'height' => '',
'crop' => false,
];
$sizes[$s]['width'] = isset($_wp_additional_image_sizes[$s]['width']) ?
intval($_wp_additional_image_sizes[$s]['width']) : get_option("{$s}_size_w");
$sizes[$s]['height'] = isset($_wp_additional_image_sizes[$s]['height']) ?
intval($_wp_additional_image_sizes[$s]['height']) : get_option("{$s}_size_h");
$sizes[$s]['crop'] = isset($_wp_additional_image_sizes[$s]['crop']) ?
intval($_wp_additional_image_sizes[$s]['crop']) : get_option("{$s}_crop");
$sizes[$s]['file'] = $filename;
$sizes[$s]['mime-type'] = 'image/svg+xml';
}
$metadata['sizes'] = $sizes;
return $metadata;
}
/**
* Response
*
* @author Seiyu Inoue <s.inoue@aivec.co.jp>
* @param mixed $response
* @param mixed $attachment
* @return mixed $response
*/
public static function response4Svg($response, $attachment) {
if ($response['mime'] !== 'image/svg+xml' && !empty($response['sizes'])) {
return $response;
}
$svg_path = get_attached_file($attachment->ID);
// If SVG is external, use the URL instead of the path
$svg_path = file_exists($svg_path) ?: $response['url'];
$dimensions = self::getSvgDimensions($svg_path);
$response['sizes'] = [
'full' => [
'url' => $response['url'],
'width' => $dimensions->width,
'height' => $dimensions->height,
'orientation' => $dimensions->width > $dimensions->height ? 'landscape' : 'portrait',
],
];
return $response;
}
/**
* Get size information inside svg file
*
* @author Seiyu Inoue <s.inoue@aivec.co.jp>
* @param mixed $svg_path svgPath
* @return object {
* svg file size information.
* @type string $width File width.
* @type string $height File height.
* }
*/
public static function getSvgDimensions($svg_path) {
$svg = simplexml_load_file($svg_path);
if ($svg === false) {
$width = '0';
$height = '0';
} else {
$attributes = $svg->attributes();
$width = (string)$attributes->width;
$height = (string)$attributes->height;
}
return (object)[
'width' => $width,
'height' => $height,
];
}
}