$style ){ $new = ''; foreach( $style as $property => $value ){ if( $value == '' ){ $new .= $property; }else { $new .= " $property: $value;"; } } $output .= "$selector { $new }"; } return $output; } } // 3D Viewer Block class tdvb3DViewerBlock{ protected static $_instance = null; function __construct(){ add_action( 'enqueue_block_assets', [$this, 'enqueueBlockAssets'] ); add_action( 'init', [$this, 'register'] ); add_filter( 'upload_mimes', [$this, 'uploadMimes'] ); if ( version_compare( $GLOBALS['wp_version'], '5.1' ) >= 0 ) { add_filter( 'wp_check_filetype_and_ext', [$this, 'wpCheckFiletypeAndExt'], 10, 5 ); } else { add_filter( 'wp_check_filetype_and_ext', [$this, 'wpCheckFiletypeAndExt'], 10, 4 ); } } public static function instance(){ if( self::$_instance === null ){ self::$_instance = new self(); } return self::$_instance; } function enqueueBlockAssets(){ echo wp_get_script_tag( [ 'src' => TDVB_ASSETS_DIR . 'js/model-viewer.min.js', 'type' => 'module', ] ); } function register() { wp_register_script( 'tdvb_editor_script', plugins_url( 'dist/editor.js', __FILE__ ), [ 'wp-blob', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-data', 'wp-element', 'wp-html-entities', 'wp-i18n', 'wp-rich-text' ], TDVB_PLUGIN_VERSION, false ); // Backend Script wp_register_style( 'tdvb_editor_style', plugins_url( 'dist/editor.css', __FILE__ ), [ 'wp-edit-blocks' ], TDVB_PLUGIN_VERSION ); // Backend Style wp_register_script( 'tdvb_script', plugins_url( 'dist/script.js', __FILE__ ), [ 'jquery' ], TDVB_PLUGIN_VERSION, true ); // Frontend Script wp_register_style( 'tdvb_style', plugins_url( 'dist/style.css', __FILE__ ), [ 'wp-editor' ], TDVB_PLUGIN_VERSION ); // Both Style register_block_type( 'tdvb/td-viewer', [ 'editor_script' => 'tdvb_editor_script', 'editor_style' => 'tdvb_editor_style', 'script' => 'tdvb_script', 'style' => 'tdvb_style', 'render_callback' => [$this, 'render'] ] ); // Register Block wp_set_script_translations( 'tdvb_editor_script', '3d-viewer-block', plugin_dir_path( __FILE__ ) . 'languages' ); // Translate } function render( $attributes ){ extract( $attributes ); $align = $align ?? ''; $cId = $cId ?? ''; $alignment = $alignment ?? 'center'; $sourceType = $sourceType ?? 'upload'; $model = $model ?? [ 'id' => null, 'url' => '', 'title' => '' ]; $modelLink = $modelLink ?? 'https://modelviewer.dev/shared-assets/models/shishkebab.glb'; $width = $width ?? '100%'; $height = $height ?? '350px'; $isTouchMove = $isTouchMove ?? true; $isZoom = $isZoom ?? true; $loadingType = $loadingType ?? 'auto'; $tDViewerStyle = new TDVBStyleGenerator(); // Generate Styles $tDViewerStyle::addStyle( "#tdvb3DViewerBlock-$cId .tdvb3DViewerBlock", [ 'text-align' => $alignment ] ); $tDViewerStyle::addStyle( "#tdvb3DViewerBlock-$cId .tdvb3DViewerBlock .tdvbModel", [ 'width' => '0px' === $width || '0%' === $width || '0em' === $width ? 'auto' : $width, 'height' => '0px' === $height || '0%' === $height || '0em' === $height ? '350px' : $height, ] ); $jsonData = wp_json_encode( [ 'isTouchMove' => $isTouchMove, 'isZoom' => $isZoom ] ); ob_start(); ?>