Skip to content

Commit 39411bb

Browse files
committed
Experimental support for GLB models.
1 parent 1c9690e commit 39411bb

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ coverage/html/
4040
# ENV files
4141
.env
4242

43+
# wp-env
44+
.wp-env.json
45+
4346
# IDE
4447
.vscode
4548

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

php/media/class-gallery.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ class Gallery extends Settings_Component {
2929
*/
3030
const GALLERY_LIBRARY_HANDLE = 'cld-gallery';
3131

32+
/**
33+
* This is a "false" mime type for GLB files.
34+
*
35+
* Canonically, the correct mime type for GLB files is `model/gltf-binary`,
36+
* however we need to fake an `image/` mime type to make the gallery
37+
* picker to list GLB files.
38+
*/
39+
const GALLERY_GLB_MIME_TYPE = 'image/glb';
40+
3241
/**
3342
* Holds the settings slug.
3443
*
@@ -462,6 +471,12 @@ public function prepare_block_render( $content, $block ) {
462471
if ( ! wp_get_attachment_url( $attachment['attachmentId'] ) ) {
463472
continue;
464473
}
474+
475+
// Initialize 3D model support.
476+
if ( self::GALLERY_GLB_MIME_TYPE === get_post_mime_type( $attachment['attachmentId'] ) ) {
477+
$attachment['mediaType'] = '3d';
478+
}
479+
465480
$transformations = $this->media->get_transformations( $attachment['attachmentId'] );
466481
if ( ! empty( $transformations ) ) {
467482
$attachment['transformation'] = array( 'transformation' => $transformations );
@@ -568,6 +583,50 @@ function_exists( 'has_block' ) && has_block( 'cloudinary/gallery' )
568583
return $can;
569584
}
570585

586+
/**
587+
* Allow GLB files to be uploaded.
588+
*
589+
* @param array $mimes The mime types.
590+
*
591+
* @return array
592+
*/
593+
public function add_glb_mime( $mimes ) {
594+
$mimes['glb'] = self::GALLERY_GLB_MIME_TYPE;
595+
596+
return $mimes;
597+
}
598+
599+
/**
600+
* Pass the GLB file type check.
601+
*
602+
* @param array $file_data The file data.
603+
* @param string $file The file.
604+
* @param string $filename The filename.
605+
*
606+
* @return array
607+
*/
608+
public function pass_glb_filetype_check( $file_data, $file, $filename ) {
609+
if ( 'glb' === pathinfo( $filename, PATHINFO_EXTENSION ) ) {
610+
$file_data['ext'] = 'glb';
611+
$file_data['type'] = self::GALLERY_GLB_MIME_TYPE;
612+
}
613+
614+
return $file_data;
615+
}
616+
617+
/**
618+
* Allow GLB files to be uploaded to Cloudinary.
619+
*
620+
* @param array $types The allowed extensions.
621+
*
622+
* @return array
623+
*/
624+
public function allow_glb_for_cloudinary( $types ) {
625+
$types[] = 'glb';
626+
627+
return $types;
628+
}
629+
571630
/**
572631
* Setup hooks for the gallery.
573632
*/
@@ -578,5 +637,23 @@ public function setup_hooks() {
578637
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
579638
add_filter( 'render_block', array( $this, 'prepare_block_render' ), 10, 2 );
580639
add_filter( 'cloudinary_admin_pages', array( $this, 'register_settings' ) );
640+
641+
/**
642+
* Filter to allow GLB files to be uploaded.
643+
*
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.
648+
*
649+
* @param bool $allow_glb_upload Whether to allow GLB files to be uploaded.
650+
*
651+
* @return bool
652+
*/
653+
if ( apply_filters( 'cloudinary_allow_glb_upload', false ) ) {
654+
add_filter( 'upload_mimes', array( $this, 'add_glb_mime' ), 20, 1 );
655+
add_filter( 'wp_check_filetype_and_ext', array( $this, 'pass_glb_filetype_check' ), 10, 3 );
656+
add_filter( 'cloudinary_allowed_extensions', array( $this, 'allow_glb_for_cloudinary' ) );
657+
}
581658
}
582659
}

0 commit comments

Comments
 (0)