oont-contents/plugins/ar-model-viewer/admin/settings-page.php
2025-02-08 15:10:23 +01:00

239 lines
8.5 KiB
PHP

<?php
class AR_Model_Viewer_Settings_Page
{
private $plugin_name;
// PLUGIN_PLAN values: "pro"/"premium"
const PLUGIN_PLAN = "pro";
const AR_MODEL_VIEWER_SETTINGS = '_ar_model_viewer_settings';
const PRODUCT_BTN_TEXT_KEY = 'product_btn_text';
const REMOVE_BRAND_KEY = 'remove_brand';
const REMOVE_DESKTOP_MODAL = "remove_desktop_modal";
const DESKTOP_MODAL_DESCRIPTION = 'desktop_modal_description';
const AUTO_PLAY_ANIMATION = 'auto_play_animation';
const INSTALLED_3D_PRODUCTS = 'ar_model_viewer_upload_products';
public function __construct($plugin_name)
{
$this->plugin_name = $plugin_name;
add_action('admin_menu', array($this, 'addPluginAdminMenu'), 9);
add_action('admin_init', [$this, 'register_settings_fields']);
add_filter('plugin_action_links_ar-model-viewer/ar-model-viewer.php', array($this, 'add_plugin_action_link'));
}
public static function is_premium_plan()
{
return self::PLUGIN_PLAN === 'premium';
}
public function addPluginAdminMenu()
{
add_submenu_page('options-general.php', 'AR Model Configurator Settings', 'AR Model Configurator', 'administrator', $this->plugin_name . '-settings', array($this, 'display_settings'));
}
public function add_plugin_action_link($links)
{
$url = esc_url(add_query_arg(
'page',
$this->plugin_name . '-settings',
get_admin_url() . 'admin.php'
));
// Create the link.
$settings_link = "<a href='$url'>" . __('Settings') . '</a>';
// Adds the link to the end of the array.
array_push(
$links,
$settings_link
);
return $links;
}
public function display_settings()
{
echo '<h1>AR Model Viewer</h1><br />';
echo '<form method="POST" action="options.php">';
settings_fields($this->plugin_name);
do_settings_sections($this->plugin_name);
submit_button();
echo '</form>';
$this->showInstalledProducts();
echo '<hr />';
$this->getFeedback();
}
public function register_settings_fields()
{
add_settings_section(
'ar_model_viewer_product_section',
__('', 'my-textdomain'),
function () {
$this->getSettingsHeader();
},
$this->plugin_name
);
add_settings_field(
'ar_model_viewer_option_' . self::PRODUCT_BTN_TEXT_KEY,
'Product Button Text',
function () {
echo '<input type="text" name="' . self::AR_MODEL_VIEWER_SETTINGS . '[' . self::PRODUCT_BTN_TEXT_KEY . ']" value="' . $this->getProductButtonText() . '" />';
},
$this->plugin_name,
'ar_model_viewer_product_section'
);
add_settings_field(
'ar_model_viewer_option_' . self::AUTO_PLAY_ANIMATION,
'Autoplay Animation',
function () {
$auto_play_animation = $this->autoPlayAnimation() ? 'checked' : '';
echo '<input type="checkbox" name="' . self::AR_MODEL_VIEWER_SETTINGS . '[' . self::AUTO_PLAY_ANIMATION . ']" ' . $auto_play_animation . ' />';
},
$this->plugin_name,
'ar_model_viewer_product_section'
);
if (self::is_premium_plan()) {
add_settings_field(
'ar_model_viewer_option_' . self::REMOVE_BRAND_KEY,
'Remove Branding',
function () {
$remove_brand_key_checked = $this->removeBrandKey() ? 'checked' : '';
echo '<input type="checkbox" name="' . self::AR_MODEL_VIEWER_SETTINGS . '[' . self::REMOVE_BRAND_KEY . ']" ' . $remove_brand_key_checked . ' />';
},
$this->plugin_name,
'ar_model_viewer_product_section'
);
add_settings_field(
'ar_model_viewer_option_' . self::REMOVE_DESKTOP_MODAL,
'Remove Desktop Modal',
function () {
$remove_desktop_modal_checked = $this->removeDesktopModal() ? 'checked' : '';
echo '<input type="checkbox" name="' . self::AR_MODEL_VIEWER_SETTINGS . '[' . self::REMOVE_DESKTOP_MODAL . ']" ' . $remove_desktop_modal_checked . ' />';
},
$this->plugin_name,
'ar_model_viewer_product_section'
);
add_settings_field(
'ar_model_viewer_option_' . self::DESKTOP_MODAL_DESCRIPTION,
'Desktop Modal Description',
function () {
$description = $this->getDesktopModalDescription();
echo '<textarea id="my_textarea_field" name="' . self::AR_MODEL_VIEWER_SETTINGS . '[' . self::DESKTOP_MODAL_DESCRIPTION . ']" rows="5" cols="50">' . esc_textarea($description) . '</textarea>';
},
$this->plugin_name,
'ar_model_viewer_product_section'
);
}
register_setting($this->plugin_name, self::AR_MODEL_VIEWER_SETTINGS);
}
public function getProductButtonText()
{
$options = self::getOptions();
$options = $this->getOptions();
return $options[self::PRODUCT_BTN_TEXT_KEY];
}
public function autoPlayAnimation()
{
$options = self::getOptions();
$value = $options[self::AUTO_PLAY_ANIMATION];
return !empty($value);
}
public function getDesktopModalDescription()
{
$options = self::getOptions();
return $options[self::DESKTOP_MODAL_DESCRIPTION];
}
public function removeBrandKey()
{
$options = self::getOptions();
$value = $options[self::REMOVE_BRAND_KEY];
return !empty($value);
}
public function removeDesktopModal()
{
$options = self::getOptions();
$value = $options[self::REMOVE_DESKTOP_MODAL];
return !empty($value);
}
public static function getOptions()
{
$defaultOptions = array(
self::PRODUCT_BTN_TEXT_KEY => "View In AR",
self::AUTO_PLAY_ANIMATION => '',
self::REMOVE_BRAND_KEY => '',
self::REMOVE_DESKTOP_MODAL => '',
self::DESKTOP_MODAL_DESCRIPTION => 'View this page on your mobile device to experience AR'
);
$options = get_option(self::AR_MODEL_VIEWER_SETTINGS);
if (empty($options)) {
$options = [];
}
$rs = array_merge($defaultOptions, $options);
return $rs;
}
public static function get3DProducts()
{
$post_ids = get_option(self::INSTALLED_3D_PRODUCTS, []);
if (is_array($post_ids)) {
return $post_ids;
}
return [];
}
private function showInstalledProducts()
{
if (self::PLUGIN_PLAN != 'pro') {
return;
}
$html = "<h2>List of Products with 3D Models</h2>";
$products = self::get3DProducts();
if (empty($products)) {
$html .= '<small>No Products Found</small>';
} else {
$links = array_map(fn ($item) => '<a href="' . get_edit_post_link($item) . '">' . get_the_title($item) . '</a>', $products);
$html .= implode(' | ', $links);
}
echo $html;
echo '<br /><br /><div><a href="https:://bitbute.tech" target="_blank" style="color: #93003c; text-shadow: 1px 1px 1px #eee; font-weight: 700;">Upgrade for more features</a></div>';
}
private function getFeedback()
{
$feedbackHTML = '<div>
<p>We are always looking for ways to improve & would love to have a any valuable feedback from our customers. Please contact us to submit any query/feedback.</p>
<a href="https://bitbute.tech/contact/" target="_blank">Contact Us</a>
</div>';
echo $feedbackHTML;
}
private function getSettingsHeader()
{
$headerHTML = '<div class="ar-model-settings-header">
<div class="ar-logo-container">
<a href="https://bitbute.tech/" target="_blank">
<img class="ar-logo-bitbute-image" src="' . plugins_url('../assets/bitbute-logo.png', __FILE__) . '" alt="Bitbute logo">
BitBute
</a>
</div>
<h2>Product Page Settings</h2>
</div>';
echo $headerHTML;
}
}