Skip to content

Commit 34b959e

Browse files
OlegApanovichcagdasdag
authored andcommitted
VC-3343 add new file formats to import
1 parent 4b23035 commit 34b959e

File tree

1 file changed

+54
-8
lines changed

1 file changed

+54
-8
lines changed

visualcomposer/Helpers/WpMedia.php

+54-8
Original file line numberDiff line numberDiff line change
@@ -270,26 +270,72 @@ public function getSizes()
270270
return $sizes;
271271
}
272272

273-
public function checkIsImage($string)
273+
/**
274+
* Check if provided url is image.
275+
*
276+
* @param string $url
277+
*
278+
* @return false|int
279+
*/
280+
public function checkIsImage($url)
274281
{
275-
if (!is_string($string)) {
282+
if (!is_string($url)) {
276283
return false;
277284
}
278285

279-
$re = '/(\.png|jpg|jpeg|gif)$/';
286+
$allowed_image_mime_types_regex = $this->get_allowed_specific_mime_types_regex('image');
287+
288+
if ($allowed_image_mime_types_regex === '') {
289+
return false;
290+
}
280291

281-
return preg_match($re, strtolower($string));
292+
return preg_match($allowed_image_mime_types_regex, strtolower($url));
282293
}
283294

284-
public function checkIsVideo($string)
295+
/**
296+
* Check if provided url is video.
297+
*
298+
* @param string $url
299+
*
300+
* @return false|int
301+
*/
302+
public function checkIsVideo($url)
285303
{
286-
if (!is_string($string)) {
304+
if (!is_string($url)) {
305+
return false;
306+
}
307+
308+
$allowed_video_mime_types_regex = $this->get_allowed_specific_mime_types_regex('video');
309+
310+
if ($allowed_video_mime_types_regex === '') {
287311
return false;
288312
}
289313

290-
$re = '/(\.mp4|avi|flv|wmv|mov)$/';
314+
return preg_match($allowed_video_mime_types_regex, strtolower($url));
315+
}
316+
317+
/**
318+
* Get allowed specific MIME types that can be loaded to the media library.
319+
*
320+
* @param string $type
321+
*
322+
* @return string
323+
*/
324+
public function get_allowed_specific_mime_types_regex($type)
325+
{
326+
// Get all allowed MIME types
327+
$all_mime_types = get_allowed_mime_types();
328+
329+
// Filter to include only image MIME types
330+
$all_mime_types = array_filter($all_mime_types, function ($mime_type) use ($type) {
331+
return strpos($mime_type, $type . '/') === 0;
332+
});
333+
334+
if (empty($all_mime_types)) {
335+
return '';
336+
}
291337

292-
return preg_match($re, strtolower($string));
338+
return '/(\.' . implode('|', array_keys($all_mime_types)) . ')$/';
293339
}
294340

295341
/**

0 commit comments

Comments
 (0)