Files
cannaiq/wordpress-plugin/dutchie-analytics/includes/Admin_Settings.php
2025-11-28 19:45:44 -07:00

114 lines
4.6 KiB
PHP

<?php
namespace DutchieAnalytics;
class Admin_Settings {
public function __construct() {
add_action('admin_menu', [$this, 'add_settings_page']);
add_action('admin_init', [$this, 'register_settings']);
}
public function add_settings_page() {
add_options_page(
__('Dutchie Analytics Settings', 'dutchie-analytics'),
__('Dutchie Analytics', 'dutchie-analytics'),
'manage_options',
'dutchie-analytics',
[$this, 'render_settings_page']
);
}
public function register_settings() {
register_setting('dutchie_analytics_settings', 'dutchie_analytics_api_url');
register_setting('dutchie_analytics_settings', 'dutchie_analytics_api_token');
add_settings_section(
'dutchie_analytics_main',
__('API Configuration', 'dutchie-analytics'),
[$this, 'render_section_description'],
'dutchie-analytics'
);
add_settings_field(
'dutchie_analytics_api_url',
__('API URL', 'dutchie-analytics'),
[$this, 'render_api_url_field'],
'dutchie-analytics',
'dutchie_analytics_main'
);
add_settings_field(
'dutchie_analytics_api_token',
__('API Token', 'dutchie-analytics'),
[$this, 'render_api_token_field'],
'dutchie-analytics',
'dutchie_analytics_main'
);
}
public function render_settings_page() {
?>
<div class="wrap">
<h1><?php echo esc_html(get_admin_page_title()); ?></h1>
<form action="options.php" method="post">
<?php
settings_fields('dutchie_analytics_settings');
do_settings_sections('dutchie-analytics');
submit_button(__('Save Settings', 'dutchie-analytics'));
?>
</form>
<hr>
<h2><?php _e('Test Connection', 'dutchie-analytics'); ?></h2>
<?php
$api = new API_Client();
$stores = $api->get_stores();
if ($stores && isset($stores['stores'])) {
echo '<p style="color: green;"><strong>✓ Connection successful!</strong></p>';
echo '<p>' . sprintf(__('Found %d stores', 'dutchie-analytics'), count($stores['stores'])) . '</p>';
} else {
echo '<p style="color: red;"><strong>✗ Connection failed</strong></p>';
echo '<p>' . __('Please check your API URL and Token', 'dutchie-analytics') . '</p>';
}
?>
<hr>
<h2><?php _e('Documentation', 'dutchie-analytics'); ?></h2>
<h3><?php _e('Shortcodes', 'dutchie-analytics'); ?></h3>
<ul>
<li><code>[dutchie_products store_id="1" limit="12"]</code> - Display products grid</li>
<li><code>[dutchie_carousel store_id="1" limit="20"]</code> - Display products carousel</li>
<li><code>[dutchie_stores]</code> - Display all stores</li>
<li><code>[dutchie_brands store_id="1"]</code> - Display store brands</li>
<li><code>[dutchie_specials store_id="1"]</code> - Display today's specials</li>
</ul>
<h3><?php _e('Field Selection', 'dutchie-analytics'); ?></h3>
<p>You can select which fields to display using the <code>fields</code> parameter:</p>
<code>[dutchie_products store_id="1" fields="id,name,price,brand,in_stock"]</code>
<h3><?php _e('Elementor Widgets', 'dutchie-analytics'); ?></h3>
<p>Find all widgets in the <strong>Dutchie Analytics</strong> category in Elementor.</p>
</div>
<?php
}
public function render_section_description() {
echo '<p>' . __('Configure your Dutchie Analytics API connection', 'dutchie-analytics') . '</p>';
}
public function render_api_url_field() {
$value = get_option('dutchie_analytics_api_url', 'http://localhost:3010/api');
echo '<input type="text" name="dutchie_analytics_api_url" value="' . esc_attr($value) . '" class="regular-text">';
echo '<p class="description">' . __('Example: https://api.dutchieanalytics.com/api', 'dutchie-analytics') . '</p>';
}
public function render_api_token_field() {
$value = get_option('dutchie_analytics_api_token', '');
echo '<input type="password" name="dutchie_analytics_api_token" value="' . esc_attr($value) . '" class="regular-text">';
echo '<p class="description">' . __('Your JWT authentication token', 'dutchie-analytics') . '</p>';
}
}