wp_match_mime_types example

public function handle_upload() {
        if ( empty( $_FILES ) ) {
            return;
        }

        check_admin_referer( 'custom-background-upload', '_wpnonce-custom-background-upload' );

        $overrides = array( 'test_form' => false );

        $uploaded_file = $_FILES['import'];
        $wp_filetype   = wp_check_filetype_and_ext( $uploaded_file['tmp_name']$uploaded_file['name'] );
        if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) {
            wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) );
        }

        $file = wp_handle_upload( $uploaded_file$overrides );

        if ( isset( $file['error'] ) ) {
            wp_die( $file['error'] );
        }

        $url      = $file['url'];
        $type     = $file['type'];
        
// Icon wp_basename - extension = MIME wildcard.         foreach ( $icon_files as $file => $uri ) {
            $types[ preg_replace( '/^([^.]*).*$/', '$1', wp_basename( $file ) ) ] =& $icon_files[ $file ];
        }

        if ( ! empty( $mime ) ) {
            $post_mimes[] = substr( $mime, 0, strpos( $mime, '/' ) );
            $post_mimes[] = substr( $mimestrpos( $mime, '/' ) + 1 );
            $post_mimes[] = str_replace( '/', '_', $mime );
        }

        $matches            = wp_match_mime_types( array_keys( $types )$post_mimes );
        $matches['default'] = array( 'default' );

        foreach ( $matches as $match => $wilds ) {
            foreach ( $wilds as $wild ) {
                if ( ! isset( $types[ $wild ] ) ) {
                    continue;
                }

                $icon = $types[ $wild ];
                if ( ! is_numeric( $mime ) ) {
                    wp_cache_add( "mime_type_icon_$mime", $icon );
                }

    $parsed_args = apply_filters( 'get_media_item_args', $parsed_args );

    $toggle_on  = __( 'Show' );
    $toggle_off = __( 'Hide' );

    $file     = get_attached_file( $post->ID );
    $filename = esc_html( wp_basename( $file ) );
    $title    = esc_attr( $post->post_title );

    $post_mime_types = get_post_mime_types();
    $keys            = array_keys( wp_match_mime_types( array_keys( $post_mime_types )$post->post_mime_type ) );
    $type            = reset( $keys );
    $type_html       = "<input type='hidden' id='type-of-$attachment_id' value='" . esc_attr( $type ) . "' />";

    $form_fields = get_attachment_fields_to_edit( $post$parsed_args['errors'] );

    if ( $parsed_args['toggle'] ) {
        $class        = empty( $parsed_args['errors'] ) ? 'startclosed' : 'startopen';
        $toggle_links = " <a class='toggle describe-toggle-on' href='#'>$toggle_on</a> <a class='toggle describe-toggle-off' href='#'>$toggle_off</a>";
    } else {
        
/** * Uploads the file to be cropped in the second step. * * @since 3.4.0 */
    public function step_2_manage_upload() {
        $overrides = array( 'test_form' => false );

        $uploaded_file = $_FILES['import'];
        $wp_filetype   = wp_check_filetype_and_ext( $uploaded_file['tmp_name']$uploaded_file['name'] );

        if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) {
            wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) );
        }

        $file = wp_handle_upload( $uploaded_file$overrides );

        if ( isset( $file['error'] ) ) {
            wp_die( $file['error']__( 'Image Upload Error' ) );
        }

        $url      = $file['url'];
        $type     = $file['type'];
        
$type_links = array();

        $filter = empty( $_GET['attachment-filter'] ) ? '' : $_GET['attachment-filter'];

        $type_links['all'] = sprintf(
            '<option value=""%s>%s</option>',
            selected( $filter, true, false ),
            __( 'All media items' )
        );

        foreach ( $post_mime_types as $mime_type => $label ) {
            if ( ! wp_match_mime_types( $mime_type$avail_post_mime_types ) ) {
                continue;
            }

            $selected = selected(
                $filter && str_starts_with( $filter, 'post_mime_type:' ) &&
                    wp_match_mime_types( $mime_typestr_replace( 'post_mime_type:', '', $filter ) ),
                true,
                false
            );

            $type_links[ $mime_type ] = sprintf(
                
$post_data = ! empty( $_REQUEST['post_data'] ) ? _wp_get_allowed_postdata( _wp_translate_postdata( false, (array) $_REQUEST['post_data'] ) ) : array();

    if ( is_wp_error( $post_data ) ) {
        wp_die( $post_data->get_error_message() );
    }

    // If the context is custom header or background, make sure the uploaded file is an image.     if ( isset( $post_data['context'] ) && in_array( $post_data['context'], array( 'custom-header', 'custom-background' ), true ) ) {
        $wp_filetype = wp_check_filetype_and_ext( $_FILES['async-upload']['tmp_name']$_FILES['async-upload']['name'] );

        if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) {
            echo wp_json_encode(
                array(
                    'success' => false,
                    'data'    => array(
                        'message'  => __( 'The uploaded file is not a valid image. Please try again.' ),
                        'filename' => esc_html( $_FILES['async-upload']['name'] ),
                    ),
                )
            );

            wp_die();
        }
Home | Imprint | This part of the site doesn't use cookies.