88 lines
1.8 KiB
PHP
88 lines
1.8 KiB
PHP
<?php
|
|
if (!defined('ABSPATH')) {
|
|
exit; // Exit if accessed directly.
|
|
}
|
|
|
|
/**
|
|
* Main AR Model Viewer Class
|
|
*
|
|
* The main class that initiates and runs the plugin.
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
final class AR_Model_Viewer_Public
|
|
{
|
|
/**
|
|
* @var AR_Model_Viewer_Settings_Page
|
|
*/
|
|
private $settings;
|
|
/**
|
|
* Constructor
|
|
*
|
|
* @since 1.0.0
|
|
*
|
|
* @access public
|
|
*/
|
|
public function __construct($ar)
|
|
{
|
|
$this->settings = $ar->settings;
|
|
$this->init();
|
|
}
|
|
|
|
private $product;
|
|
|
|
|
|
/**
|
|
* Initialize the plugin
|
|
*
|
|
* Load the plugin only after Elementor (and other plugins) are loaded.
|
|
* Load the files required to run the plugin.
|
|
*
|
|
* Fired by `plugins_loaded` action hook.
|
|
*
|
|
* @since 1.0.0
|
|
*
|
|
* @access public
|
|
*/
|
|
public function init()
|
|
{
|
|
|
|
if (is_singular('product')) {
|
|
$this->product = wc_get_product();
|
|
wp_register_script('ar-model-viewer', plugins_url('../js/ar-model-viewer-public.js', __FILE__), array('jquery'), WP_DEBUG ? time() : AR_Model_Viewer::VERSION);
|
|
wp_enqueue_script('ar-model-viewer');
|
|
$this->set_ar_options();
|
|
|
|
add_action('wp_footer', function () {
|
|
$this->ar_model_viewer_woocommerce_single_product_image_html();
|
|
}, 10, 2);
|
|
}
|
|
}
|
|
|
|
public function ar_model_viewer_woocommerce_single_product_image_html()
|
|
{
|
|
$product = $this->product;
|
|
$render = (new AR_Model_Viewer_Renderer())->set_from_product($product->get_id());
|
|
if (empty($render->get_attribute('src'))) {
|
|
// no model found
|
|
return;
|
|
}
|
|
|
|
$this->append_modal($render);
|
|
}
|
|
|
|
public function append_modal($ar)
|
|
{
|
|
$ar->set_attributes(['id' => 'ar-model-viewer-product-page']);
|
|
$ar->set_settings(
|
|
array('modal' => true)
|
|
);
|
|
echo $ar->render_ar();
|
|
}
|
|
|
|
private function set_ar_options()
|
|
{
|
|
$options = $this->settings->getOptions();
|
|
AR_Model_Viewer::instance()->renderer->localize(['modelId' => 'ar-model-viewer-product-page', 'settings' => $options]);
|
|
}
|
|
}
|