Skip to content

Commit 7512b9b

Browse files
committed
Render GLB preview in the media library.
1 parent 39411bb commit 7512b9b

File tree

1 file changed

+61
-5
lines changed

1 file changed

+61
-5
lines changed

php/media/class-gallery.php

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,61 @@ public function allow_glb_for_cloudinary( $types ) {
627627
return $types;
628628
}
629629

630+
/**
631+
* Allow GLB files to be delivered from Cloudinary.
632+
*
633+
* @param bool $is Whether the file is deliverable.
634+
* @param int $attachment_id The attachment ID.
635+
*
636+
* @return bool
637+
*/
638+
public function allow_glb_delivery_from_cloudinary( $is, $attachment_id ) {
639+
if ( self::GALLERY_GLB_MIME_TYPE === get_post_mime_type( $attachment_id ) ) {
640+
$is = true;
641+
}
642+
643+
return $is;
644+
}
645+
646+
/**
647+
* Replace image urls from .glb to .png.
648+
*
649+
* This allows the GLB files to be served as PNG files in the image context.
650+
*
651+
* @param string $url The URL.
652+
* @param int $attachment_id The attachment ID.
653+
*
654+
* @return string
655+
*/
656+
public function admin_replace_glb_url( $url, $attachment_id ) {
657+
if ( ! is_admin() ) {
658+
return $url;
659+
}
660+
661+
if ( self::GALLERY_GLB_MIME_TYPE === get_post_mime_type( $attachment_id ) ) {
662+
$url = str_replace( '.glb', '.png', $url );
663+
}
664+
return $url;
665+
}
666+
667+
/**
668+
* We pretend that GLB files are images with dimensions of 512x512.
669+
*
670+
* This allows WordPress to correctly render the GLB files as images in the media library.
671+
*
672+
* @param array $metadata The attachment metadata.
673+
* @param int $attachment_id The attachment ID.
674+
*
675+
* @return array
676+
*/
677+
public function inject_glb_metadata( $metadata, $attachment_id ) {
678+
if ( self::GALLERY_GLB_MIME_TYPE === get_post_mime_type( $attachment_id ) ) {
679+
$metadata['width'] = 512;
680+
$metadata['height'] = 512;
681+
}
682+
return $metadata;
683+
}
684+
630685
/**
631686
* Setup hooks for the gallery.
632687
*/
@@ -639,12 +694,10 @@ public function setup_hooks() {
639694
add_filter( 'cloudinary_admin_pages', array( $this, 'register_settings' ) );
640695

641696
/**
642-
* Filter to allow GLB files to be uploaded.
697+
* Filter to allow GLB 3D model files to be uploaded.
643698
*
644-
* WARNING: This is an experimental hook. When enabled, the GLB files
645-
* will become uploadable in the media library, but these files
646-
* will render no preview. Also, the only place where GLB files
647-
* can be used is in the Cloudinary Gallery block.
699+
* WARNING: This is an experimental hook. The only place where GLB files can be used
700+
* is in the Cloudinary Gallery block.
648701
*
649702
* @param bool $allow_glb_upload Whether to allow GLB files to be uploaded.
650703
*
@@ -654,6 +707,9 @@ public function setup_hooks() {
654707
add_filter( 'upload_mimes', array( $this, 'add_glb_mime' ), 20, 1 );
655708
add_filter( 'wp_check_filetype_and_ext', array( $this, 'pass_glb_filetype_check' ), 10, 3 );
656709
add_filter( 'cloudinary_allowed_extensions', array( $this, 'allow_glb_for_cloudinary' ) );
710+
add_filter( 'cloudinary_is_deliverable', array( $this, 'allow_glb_delivery_from_cloudinary' ), 10, 2 );
711+
add_filter( 'wp_get_attachment_url', array( $this, 'admin_replace_glb_url' ), 11, 2 );
712+
add_filter( 'wp_generate_attachment_metadata', array( $this, 'inject_glb_metadata' ), 10, 2 );
657713
}
658714
}
659715
}

0 commit comments

Comments
 (0)