206 lines
5.7 KiB
PHP
206 lines
5.7 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_Admin
|
|
{
|
|
|
|
/**
|
|
* Constructor
|
|
*
|
|
* @since 1.0.0
|
|
*
|
|
* @access public
|
|
*/
|
|
public function __construct()
|
|
{
|
|
|
|
$this->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. <a href="https://bitbute.tech" target="_blank">Upgrade</a>';
|
|
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 '<div id="ar-model-viewer-admin-preview-box">';
|
|
echo '<h4>Preview</h4>';
|
|
$render = AR_Model_Viewer::instance()->renderer->set_from_product($post->ID);
|
|
if (empty($render->get_attribute('poster'))) {
|
|
$render->set_attributes(['poster' => wc_placeholder_img_src('')]);
|
|
}
|
|
$render->set_attributes(['id' => 'ar-model-viewer-admin-preview']);
|
|
echo $render->render_ar();
|
|
echo '</div>';
|
|
|
|
echo '<div id="ar-model-viewer-post-metabox">';
|
|
echo "</div>";
|
|
}
|
|
|
|
function add_variant_field_to_variation($loop, $variation_data, $variation)
|
|
{
|
|
|
|
$renderer = AR_Model_Viewer::instance()->renderer;
|
|
if (!$renderer->isPremiumPlan) {
|
|
return;
|
|
}
|
|
$current_settings = $renderer->get_post_settings(wp_get_post_parent_id($variation->ID));
|
|
|
|
$variants = isset($current_settings['ar_variants']) ? $current_settings['ar_variants'] : [];
|
|
if (!is_array($variants)) {
|
|
$variant_options = [];
|
|
} else {
|
|
$variant_options = [];
|
|
$variant_options = ['' => 'None'];
|
|
foreach ($variants as $key => $variant) {
|
|
$variant_options[$key] = $variant['name'];
|
|
}
|
|
}
|
|
|
|
$wc_variants = $current_settings['wc_variants'];
|
|
if (!empty($wc_variants)) {
|
|
$current_value = $wc_variants[$variation->ID];
|
|
} else {
|
|
$current_value = '';
|
|
}
|
|
woocommerce_wp_select(
|
|
array(
|
|
'id' => '_ar_variant_id[' . $loop . ']',
|
|
'label' => __('AR Variant', 'woocommerce'),
|
|
'options' => $variant_options,
|
|
'description' => __('Choose the AR Variant to display with this variation', 'woocommerce'),
|
|
'value' => $current_value
|
|
)
|
|
);
|
|
}
|
|
|
|
// Save the new field value when a variation is saved
|
|
function save_variant_field_to_variation($variation_id, $i)
|
|
{
|
|
AR_Model_Viewer::instance()->renderer->save_variation_settings($variation_id, $i);
|
|
}
|
|
}
|