| 1 |
<?php |
| 2 |
namespace Depicter\WordPress; |
| 3 |
|
| 4 |
use WPEmerge\ServiceProviders\ServiceProviderInterface; |
| 5 |
|
| 6 |
class SVGServiceProvider implements ServiceProviderInterface |
| 7 |
{ |
| 8 |
|
| 9 |
/** |
| 10 |
* {@inheritDoc} |
| 11 |
*/ |
| 12 |
public function register( $container ) { |
| 13 |
// Nothing to register. |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* {@inheritDoc} |
| 18 |
*/ |
| 19 |
public function bootstrap( $container ) { |
| 20 |
// Enable SVG support |
| 21 |
add_filter( 'wp_check_filetype_and_ext', [ $this, 'checkFileType' ], 10, 4 ); |
| 22 |
add_filter( 'upload_mimes', [ $this, 'addExtraMimeType' ] ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Allow SVG |
| 27 |
* |
| 28 |
* @param $data |
| 29 |
* @param $file |
| 30 |
* @param $filename |
| 31 |
* @param $mimes |
| 32 |
* |
| 33 |
* @return array |
| 34 |
*/ |
| 35 |
public function checkFileType( $data, $file, $filename, $mimes ) { |
| 36 |
$fileType = wp_check_filetype( $filename, $mimes ); |
| 37 |
|
| 38 |
return [ |
| 39 |
'ext' => $fileType['ext'], |
| 40 |
'type' => $fileType['type'], |
| 41 |
'proper_filename' => $data['proper_filename'] |
| 42 |
]; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Add SVG mime type |
| 47 |
* |
| 48 |
* @param $mimes |
| 49 |
* |
| 50 |
* @return mixed |
| 51 |
*/ |
| 52 |
public function addExtraMimeType( $mimes ){ |
| 53 |
if ( \Depicter::options()->get('allow_unfiltered_data_upload' ) === 'on' ) { |
| 54 |
$mimes['svg'] = 'image/svg+xml'; |
| 55 |
$mimes['json'] = 'application/json'; |
| 56 |
} |
| 57 |
|
| 58 |
return $mimes; |
| 59 |
} |
| 60 |
|
| 61 |
} |
| 62 |
|