theme_name = wp_get_theme()->name; add_action('wp', [$this, 'onWoocommerceLoaded']); add_action('wp_footer', [$this, 'handleIncompatibleTheme']); add_action('bp3d_product_model_before', [$this, 'renderModel']); add_action('bp3d_product_model_after', [$this, 'renderModel']); add_action('woocommerce_product_thumbnails', [$this, 'renderGalleryThumbnail'], 40); } /** * Handle WooCommerce page load — conditionally replace the product gallery. * * Removes the default product images and substitutes the 3D model viewer * based on viewer position settings and theme compatibility. */ public function onWoocommerceLoaded(): void { $meta = Utils::getPostMeta(get_the_ID(), '_bp3d_product_'); $settings = Utils::getSettings('_bp3d_settings_'); if ( $meta('force_to_change_position', false, true) || $meta('is_custom_selector', false, true) || in_array($meta('viewer_position'), ['none', 'merge_with_first_image', 'custom_selector'], true) || !Utils::isCompatibleTheme() ) { return; } $models = $meta('bp3d_models', []); $model_src = $meta('bp3d_model_src') ?? $models['0']['model_src'] ?? ''; if ($model_src === '') { return; } if ($settings('3d_woo_switcher') !== '0' && $settings('is_not_compatible') !== '1') { remove_action('woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 10); remove_action('woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20); remove_action('woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 30); add_action('woocommerce_before_single_product_summary', [$this, 'renderProductModels'], 20); } } /** * Handle incompatible themes by rendering the 3D viewer in the footer. */ public function handleIncompatibleTheme(): void { global $product; $settings = Utils::getSettings('_bp3d_settings_'); $woo_enabled = $settings('3d_woo_switcher') !== '0'; if (!$woo_enabled || !is_object($product) || !is_single()) { return; } if (!method_exists($product, 'get_id')) { return; } if (Utils::isCompatibleTheme()) { return; } Product::instance()->get_3d_model_html(false, 'product-gallery'); } /** * Add 3D model thumbnail to WooCommerce product gallery. * * Renders the 3D model viewer inline when position is set to merge with first image. */ public function renderGalleryThumbnail(): void { $meta = Utils::getPostMeta(get_the_ID(), '_bp3d_product_'); if ($meta('viewer_position') === 'merge_with_first_image') { Product::instance()->get_3d_model_html(false, 'product-gallery-inline'); } } /** * Render the full product model gallery with 3D support. * * Replaces or augments the WooCommerce product image gallery * with 3D model viewer based on the configured viewer position. */ public function renderProductModels(): void { if (!function_exists('wc_get_gallery_image_html')) { return; } $modeview_3d = get_post_meta(get_the_ID(), '_bp3d_product_', true); $viewer_position = is_array($modeview_3d) ? ($modeview_3d['viewer_position'] ?? 'none') : 'none'; $class = Utils::getThemeClass($this->theme_name); // Handle incompatible themes with replace position if (in_array($this->theme_name, Utils::getNotCompatibleThemes(), true)) { if ($viewer_position === 'replace') { $custom_selector = Utils::getCustomSelector($this->theme_name); ?> get_image_id(); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Core WooCommerce hook, must keep its name for compatibility. $wrapper_classes = apply_filters('woocommerce_single_product_image_gallery_classes', [ 'woocommerce-product-gallery', 'woocommerce-product-gallery--' . ($post_thumbnail_id ? 'with-images' : 'without-images'), 'woocommerce-product-gallery--columns-' . absint($columns), 'images', ] ); ?>
'; } // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound, WordPress.Security.EscapeOutput.OutputNotEscaped -- Core WooCommerce hook; output escaped by WooCommerce. echo apply_filters('woocommerce_single_product_image_thumbnail_html', $html, $post_thumbnail_id); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Core WooCommerce hook, must keep its name for compatibility. do_action('woocommerce_product_thumbnails'); ?>
get_3d_model_html(false, 'product-gallery'); } }