How to trigger "wp_handle_upload_prefilter" filter when uploading an image programatically?
I am sanitizing uploaded SVG files with wp_handle_upload_prefilter
, example:
// Example code
add_filter( 'wp_handle_upload_prefilter', function($file) {
if ( $file['type'] === 'image/svg+xml' ) {
$this-sanitize( $file['tmp_name'] );
}
} );
It works fine for files uploaded through the Media Library, however, if the file is uploaded programatically, it doesn't go through the filter.
The reason I want to programatically upload an image is for testing purposes. I am uploading the file with wp_upload_bits
function:
$upload = wp_upload_bits( 'xss.svg', null, file_get_contents( $this-svg_path ) );
$uploaded_svg_contents = file_get_contents( $upload['file'] );
$this-assertFalse( $upload['error'] );
$this-assertNotContains( 'script', $uploaded_svg_contents );
How can I upload a media file programatically, that goes through the wp_handle_upload_prefilter
filter?