init(); } /** * 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() { add_action('add_meta_boxes', [$this, 'add_custom_meta_box']); add_action('save_post', [$this, 'ar_model_viewer_meta_box_save']); add_action('woocommerce_save_product_variation', [$this, 'save_variant_field_to_variation'], 10, 2); add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts']); add_action('woocommerce_product_after_variable_attributes', [$this, 'add_variant_field_to_variation'], 10, 3); } public function enqueue_scripts() { wp_register_script('ar-model-viewer', plugins_url('../js/ar-model-viewer-admin.js', __FILE__), array('jquery'), WP_DEBUG ? time() : AR_Model_Viewer::VERSION); wp_enqueue_script('ar-model-viewer'); AR_Model_Viewer::instance()->renderer->localize(['modelId' => 'ar-model-viewer-admin-preview']); wp_enqueue_style('ar-model-viewer-admin', plugins_url('../css/ar-model-viewer-admin.css', __FILE__), [], WP_DEBUG ? time() : AR_Model_Viewer::VERSION); } function add_custom_meta_box() { $screens = ['product']; foreach ($screens as $screen) { add_meta_box( 'ar_model_viewer_box_id', // Unique ID 'AR Model Configurator', // Box title [$this, 'ar_model_viewer_content'], // Content callback, must be of type callable $screen, // Post type "advanced", "low" ); } } function ar_model_viewer_meta_box_save($post_id) { // Bail if we're doing an auto save if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return; // if our nonce isn't there, or we can't verify it, bail if (!isset($_POST['meta_box_nonce']) || !wp_verify_nonce($_POST['meta_box_nonce'], 'ar_model_viewer_file_meta_box_nonce')) return; // if our current user can't edit this post, bail // RETURNS False for some reason // if (!current_user_can('edit_post')) return; $this->is_allowed_to_add($post_id); AR_Model_Viewer::instance()->renderer->save_post_settings($post_id); } function is_allowed_to_add($post_id) { if (!isset($_POST['ar_model_viewer_file'])) { return true; } if (AR_Model_Viewer_Settings_Page::PLUGIN_PLAN == 'premium') { return true; } if (empty($_POST['ar_model_viewer_file'])) { $post_ids = AR_Model_Viewer_Settings_Page::get3DProducts(); // Check if the element exists in the array if (($key = array_search($post_id, $post_ids)) !== false) { // Remove the element from the array unset($post_ids[$key]); $this->update_products($post_ids); } } else { $post_ids = AR_Model_Viewer_Settings_Page::get3DProducts(); $total_product = sizeof($post_ids); if (in_array($post_id, $post_ids) === false) { $total_product += 1; } $max = 10; // allow only 10 products if ($total_product > $max) { $message = 'You have reached the maximum number of 3D model uploads (' . $max . '). Please upgrade to upload more models. Upgrade'; wp_die($message); } else { if (!in_array($post_id, $post_ids)) { $post_ids[] = $post_id; $this->update_products($post_ids); } } } } public function get_uploaded_count() { return sizeof(AR_Model_Viewer_Settings_Page::get3DProducts()); } public function update_products($post_ids) { update_option('ar_model_viewer_upload_products', array_unique($post_ids)); } public function ar_model_viewer_content($post) { wp_nonce_field('ar_model_viewer_file_meta_box_nonce', 'meta_box_nonce'); echo '