[], 'modal' => false); public $isPremiumPlan; private $defaultSettings = array('modal' => false); private $defaultAttributes = array('src' => '', 'ios-src' => '', 'poster' => '', 'disable-zoom' => false, 'auto-rotate' => false, 'skybox-image' => '', 'ar-placement' => 'floor', 'ar-scale' => 'auto', 'autoplay' => false); private $modelAttributes = array(); private $defaultStyles = array('margin' => '0 auto'); private $premiumHandler; private $settingsOptions; public function __construct() { $this->settingsOptions = AR_Model_Viewer_Settings_Page::getOptions(); $this->isPremiumPlan = AR_Model_Viewer_Settings_Page::is_premium_plan(); add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts']); add_action('wp_enqueue_scripts', [$this, 'enqueue_scripts']); require_once(__DIR__ . '/ar-model-premium.php'); $this->premiumHandler = new AR_Model_Viewer_Premium(); add_shortcode('ar_model_viewer', [$this, 'render_shortcode']); } public function render_shortcode($atts) { $atts = shortcode_atts($this->defaultAttributes, $atts); $this->modelAttributes = $atts; return $this->render_ar(); } public function enqueue_scripts() { add_filter('script_loader_tag', [$this, 'add_type_to_script'], 1, 3); wp_register_script('model-viewer', plugins_url('../../js/libs/model-viewer.min.js', __FILE__, [], '3.0.0')); wp_enqueue_script('model-viewer'); remove_filter('script_loader_tag', [$this, 'add_type_to_script']); wp_register_script('model-viewer-renderer', plugins_url('../../js/ar-model-viewer-renderer.js', __FILE__), array('jquery'), WP_DEBUG ? time() : AR_Model_Viewer::VERSION); wp_enqueue_script('model-viewer-renderer'); wp_enqueue_style('AR Model Configurator', plugins_url('../../css/ar-model-viewer-public.css', __FILE__), [], WP_DEBUG ? time() : AR_Model_Viewer::VERSION); } public function localize($extra = []) { global $post; if (!$post) { return; } $settings = $this->get_post_settings($post->ID); $att_poster_src = $settings['poster']; $att_bg_src = $settings['skybox-image']; $att_src = $settings['src']; $att_ios_src = $settings['ios-src']; $ar_auto_rotate = $settings['auto-rotate']; $ar_model_scaling = $settings['ar-scale']; $ar_model_placement = $settings['ar-placement']; if (empty($att_poster_src) && function_exists('wc_placeholder_img_src')) { $att_poster_src = wc_placeholder_img_src(); } $jsArr = array( "arSrc" => ["url" => $att_src, "name" => "ar_model_viewer_file"], "arIosSrc" => ["url" => $att_ios_src, "name" => "ar_model_viewer_file_ios"], "arBgImage" => ["url" => $att_bg_src, "name" => "ar_model_viewer_skybox_image"], "arPosterImage" => ["url" => $att_poster_src, "name" => "ar_model_viewer_poster_image"], "autoRotate" => ["value" => $ar_auto_rotate, "name" => "ar_model_rotate"], "arPlacement" => ["value" => $ar_model_placement, "name" => "ar_placement_model"], "arModelScaling" => ["value" => $ar_model_scaling, "name" => "ar_model_scaling_lock"], ); if ($this->isPremiumPlan) { $jsArr = $this->premiumHandler->localize_extras($jsArr, $settings); } wp_localize_script('ar-model-viewer', 'arModelViewerSettings', array_merge($jsArr, $extra)); } public function add_type_to_script($tag, $handle, $src) { if ($handle == 'model-viewer') { $tag = ''; } return $tag; } public function get_attribute($name) { return isset($this->modelAttributes[$name]) ? $this->modelAttributes[$name] : null; } public function get_post_settings($post_id) { $settings = get_post_meta($post_id, 'ar_model_viewer_settings', true); if (!is_array($settings)) { $defaults = $this->get_default_attributes(); $settings = array_merge($defaults, ['ar_variants' => [], 'wc_variants' => []]); } if (!isset($settings['ar_variants'])) { $settings['ar_variants'] = []; } if (!isset($settings['wc_variants'])) { $settings['wc_variants'] = []; } if ($this->isPremiumPlan) { $settings = $this->premiumHandler->get_post_premium_settings($settings); } $update_settings_arr = array(array("src","src-id"), array("ios-src","ios-src-id"), array("skybox-image","skybox-image-src-id"), array("poster","poster-src-id")); foreach($update_settings_arr as $value){ $this->backward_compatible_keys($settings, $value[0], $value[1]); } return $settings; } private function backward_compatible_keys(&$settings, $src_key, $id_key) { if (empty($settings[$src_key]) && isset($settings[$id_key])) { $id = $settings[$id_key]; if(!empty($id)){ $settings[$src_key] = wp_get_attachment_url($id); }; if (empty($settings[$src_key])) { $settings[$src_key] = ''; }; unset($settings[$id_key]); } } public function get_default_attributes() { return $this->defaultAttributes; } public function set_attributes($values) { $this->setAutoPlayAnimation(); if (empty($this->modelAttributes)) { $this->modelAttributes = $this->defaultAttributes; } $newArr = wp_parse_args($values, $this->modelAttributes); $this->modelAttributes = $newArr; } public function set_from_product($product_id) { $current_settings = $this->get_post_settings($product_id); $attributes = array_keys($this->get_default_attributes()); $filteredArray = array_filter($current_settings, function ($key) use ($attributes) { return in_array($key, $attributes); }, ARRAY_FILTER_USE_KEY); $this->set_attributes($filteredArray); $this->settings = array_merge($this->settings, $current_settings); return $this; } public function save_post_settings($post_id) { $current_settings = $this->get_post_settings($post_id); if (!is_array($current_settings)) { $current_settings = $this->get_default_attributes(); } if (isset($_POST['ar_model_viewer_file'])) { $current_settings['src'] = $_POST['ar_model_viewer_file']; } if (isset($_POST['ar_model_viewer_file_ios'])) { $current_settings['ios-src'] = $_POST['ar_model_viewer_file_ios']; } if (isset($_POST['ar_model_viewer_poster_image'])) { $current_settings['poster'] = $_POST['ar_model_viewer_poster_image']; } if (isset($_POST['ar_model_viewer_skybox_image'])) { $current_settings['skybox-image'] = $_POST['ar_model_viewer_skybox_image']; } if (isset($_POST['ar_model_rotate'])) { $current_settings['auto-rotate'] = 'true'; } else { $current_settings['auto-rotate'] = 'false'; } if (isset($_POST['ar_placement_model'])) { $current_settings['ar-placement'] = $_POST['ar_placement_model']; } if (isset($_POST['ar_model_scaling_lock'])) { $current_settings['ar-scale'] = 'fixed'; } else { $current_settings['ar-scale'] = 'auto'; } if ($this->isPremiumPlan) { $current_settings = $this->premiumHandler->save_post_premium_settings($post_id, $current_settings); } update_post_meta($post_id, 'ar_model_viewer_settings', $current_settings); } public function save_variation_settings($variation_id, $i) { if ($this->isPremiumPlan) { $product_id = wp_get_post_parent_id($variation_id); $current_settings = $this->get_post_settings($product_id); $current_settings = $this->premiumHandler->save_variation_premium_settings($variation_id, $i, $current_settings); update_post_meta($product_id, 'ar_model_viewer_settings', $current_settings); } } public function set_styles($values) { $newArr = wp_parse_args($values, $this->defaultStyles); $this->defaultStyles = $newArr; } public function set_settings($values) { $newArr = wp_parse_args($values, $this->settings); $this->settings = $newArr; } public function render_ar() { $desktopModalOn = $this->showDesktopModalBtn(); $desktopModalDescription = $this->getDesktopModalText(); $view_in_ar_desktop_modal = $this->append_modal('