306 lines
9.6 KiB
PHP
306 lines
9.6 KiB
PHP
<?php
|
|
/**
|
|
* Plugin Name: Dutchie Menus
|
|
* Plugin URI: https://github.com/yourusername/dutchie-menus
|
|
* Description: Display cannabis product menus from your Dutchie scraper with Elementor integration
|
|
* Version: 1.0.0
|
|
* Author: Your Name
|
|
* Author URI: https://yoursite.com
|
|
* License: GPL v2 or later
|
|
* Text Domain: dutchie-menus
|
|
* Requires PHP: 7.4
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit; // Exit if accessed directly
|
|
}
|
|
|
|
define('DUTCHIE_MENUS_VERSION', '1.0.0');
|
|
define('DUTCHIE_MENUS_PLUGIN_DIR', plugin_dir_path(__FILE__));
|
|
define('DUTCHIE_MENUS_PLUGIN_URL', plugin_dir_url(__FILE__));
|
|
|
|
/**
|
|
* Main Plugin Class
|
|
*/
|
|
class Dutchie_Menus_Plugin {
|
|
|
|
private static $instance = null;
|
|
|
|
public static function instance() {
|
|
if (is_null(self::$instance)) {
|
|
self::$instance = new self();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
public function __construct() {
|
|
add_action('plugins_loaded', [$this, 'init']);
|
|
add_action('elementor/widgets/register', [$this, 'register_elementor_widgets']);
|
|
add_action('admin_menu', [$this, 'add_admin_menu']);
|
|
add_action('admin_init', [$this, 'register_settings']);
|
|
add_action('wp_enqueue_scripts', [$this, 'enqueue_scripts']);
|
|
}
|
|
|
|
public function init() {
|
|
// Initialize plugin
|
|
load_plugin_textdomain('dutchie-menus', false, dirname(plugin_basename(__FILE__)) . '/languages');
|
|
|
|
// Register shortcodes
|
|
add_shortcode('dutchie_products', [$this, 'products_shortcode']);
|
|
add_shortcode('dutchie_product', [$this, 'single_product_shortcode']);
|
|
}
|
|
|
|
/**
|
|
* Register Elementor Widgets
|
|
*/
|
|
public function register_elementor_widgets($widgets_manager) {
|
|
require_once DUTCHIE_MENUS_PLUGIN_DIR . 'widgets/product-grid.php';
|
|
require_once DUTCHIE_MENUS_PLUGIN_DIR . 'widgets/single-product.php';
|
|
|
|
$widgets_manager->register(new \Dutchie_Menus_Product_Grid_Widget());
|
|
$widgets_manager->register(new \Dutchie_Menus_Single_Product_Widget());
|
|
}
|
|
|
|
/**
|
|
* Enqueue Scripts and Styles
|
|
*/
|
|
public function enqueue_scripts() {
|
|
wp_enqueue_style(
|
|
'dutchie-menus-styles',
|
|
DUTCHIE_MENUS_PLUGIN_URL . 'assets/css/dutchie-menus.css',
|
|
[],
|
|
DUTCHIE_MENUS_VERSION
|
|
);
|
|
|
|
wp_enqueue_script(
|
|
'dutchie-menus-script',
|
|
DUTCHIE_MENUS_PLUGIN_URL . 'assets/js/dutchie-menus.js',
|
|
['jquery'],
|
|
DUTCHIE_MENUS_VERSION,
|
|
true
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Add Admin Menu
|
|
*/
|
|
public function add_admin_menu() {
|
|
add_menu_page(
|
|
'Dutchie Menus',
|
|
'Dutchie Menus',
|
|
'manage_options',
|
|
'dutchie-menus',
|
|
[$this, 'admin_page'],
|
|
'dashicons-products',
|
|
30
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Register Plugin Settings
|
|
*/
|
|
public function register_settings() {
|
|
register_setting('dutchie_menus_settings', 'dutchie_api_url');
|
|
register_setting('dutchie_menus_settings', 'dutchie_api_token');
|
|
register_setting('dutchie_menus_settings', 'dutchie_default_store_id');
|
|
}
|
|
|
|
/**
|
|
* Admin Page
|
|
*/
|
|
public function admin_page() {
|
|
?>
|
|
<div class="wrap">
|
|
<h1>Dutchie Menus Settings</h1>
|
|
<form method="post" action="options.php">
|
|
<?php settings_fields('dutchie_menus_settings'); ?>
|
|
<?php do_settings_sections('dutchie_menus_settings'); ?>
|
|
|
|
<table class="form-table">
|
|
<tr>
|
|
<th scope="row"><label for="dutchie_api_url">API URL</label></th>
|
|
<td>
|
|
<input type="url" id="dutchie_api_url" name="dutchie_api_url"
|
|
value="<?php echo esc_attr(get_option('dutchie_api_url', 'http://localhost:3010')); ?>"
|
|
class="regular-text" />
|
|
<p class="description">Your Dutchie Menus API endpoint (e.g., http://localhost:3010)</p>
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<th scope="row"><label for="dutchie_api_token">API Token</label></th>
|
|
<td>
|
|
<input type="password" id="dutchie_api_token" name="dutchie_api_token"
|
|
value="<?php echo esc_attr(get_option('dutchie_api_token')); ?>"
|
|
class="regular-text" />
|
|
<p class="description">Your authentication token from the admin dashboard</p>
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<th scope="row"><label for="dutchie_default_store_id">Default Store ID</label></th>
|
|
<td>
|
|
<input type="number" id="dutchie_default_store_id" name="dutchie_default_store_id"
|
|
value="<?php echo esc_attr(get_option('dutchie_default_store_id', '1')); ?>"
|
|
class="small-text" />
|
|
<p class="description">Default store ID to use</p>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
|
|
<?php submit_button(); ?>
|
|
</form>
|
|
|
|
<hr />
|
|
|
|
<h2>Test Connection</h2>
|
|
<button type="button" id="test-api-connection" class="button">Test API Connection</button>
|
|
<div id="api-test-result" style="margin-top: 10px;"></div>
|
|
|
|
<script>
|
|
jQuery(document).ready(function($) {
|
|
$('#test-api-connection').on('click', function() {
|
|
var apiUrl = $('#dutchie_api_url').val();
|
|
var apiToken = $('#dutchie_api_token').val();
|
|
|
|
$('#api-test-result').html('<p>Testing connection...</p>');
|
|
|
|
$.ajax({
|
|
url: apiUrl + '/api/auth/me',
|
|
method: 'GET',
|
|
headers: {
|
|
'Authorization': 'Bearer ' + apiToken
|
|
},
|
|
success: function(response) {
|
|
$('#api-test-result').html(
|
|
'<div class="notice notice-success"><p><strong>Success!</strong> Connected as: ' +
|
|
response.user.email + '</p></div>'
|
|
);
|
|
},
|
|
error: function(xhr) {
|
|
$('#api-test-result').html(
|
|
'<div class="notice notice-error"><p><strong>Error:</strong> ' +
|
|
(xhr.responseJSON?.error || 'Connection failed') + '</p></div>'
|
|
);
|
|
}
|
|
});
|
|
});
|
|
});
|
|
</script>
|
|
</div>
|
|
<?php
|
|
}
|
|
|
|
/**
|
|
* Products Shortcode
|
|
*/
|
|
public function products_shortcode($atts) {
|
|
$atts = shortcode_atts([
|
|
'store_id' => get_option('dutchie_default_store_id', 1),
|
|
'category_id' => '',
|
|
'limit' => 12,
|
|
'columns' => 3,
|
|
'in_stock' => 'true'
|
|
], $atts);
|
|
|
|
$products = $this->fetch_products($atts);
|
|
|
|
if (!$products) {
|
|
return '<p>No products found.</p>';
|
|
}
|
|
|
|
ob_start();
|
|
include DUTCHIE_MENUS_PLUGIN_DIR . 'templates/product-grid.php';
|
|
return ob_get_clean();
|
|
}
|
|
|
|
/**
|
|
* Single Product Shortcode
|
|
*/
|
|
public function single_product_shortcode($atts) {
|
|
$atts = shortcode_atts([
|
|
'id' => 0
|
|
], $atts);
|
|
|
|
if (!$atts['id']) {
|
|
return '<p>Product ID required.</p>';
|
|
}
|
|
|
|
$product = $this->fetch_product($atts['id']);
|
|
|
|
if (!$product) {
|
|
return '<p>Product not found.</p>';
|
|
}
|
|
|
|
ob_start();
|
|
include DUTCHIE_MENUS_PLUGIN_DIR . 'templates/single-product.php';
|
|
return ob_get_clean();
|
|
}
|
|
|
|
/**
|
|
* Fetch Products from API
|
|
*/
|
|
public function fetch_products($args = []) {
|
|
$api_url = get_option('dutchie_api_url', 'http://localhost:3010');
|
|
$api_token = get_option('dutchie_api_token');
|
|
|
|
if (!$api_token) {
|
|
return false;
|
|
}
|
|
|
|
$query_args = http_build_query($args);
|
|
$url = $api_url . '/api/products?' . $query_args;
|
|
|
|
$response = wp_remote_get($url, [
|
|
'headers' => [
|
|
'Authorization' => 'Bearer ' . $api_token
|
|
],
|
|
'timeout' => 30
|
|
]);
|
|
|
|
if (is_wp_error($response)) {
|
|
return false;
|
|
}
|
|
|
|
$body = wp_remote_retrieve_body($response);
|
|
$data = json_decode($body, true);
|
|
|
|
return $data['products'] ?? false;
|
|
}
|
|
|
|
/**
|
|
* Fetch Single Product from API
|
|
*/
|
|
public function fetch_product($id) {
|
|
$api_url = get_option('dutchie_api_url', 'http://localhost:3010');
|
|
$api_token = get_option('dutchie_api_token');
|
|
|
|
if (!$api_token) {
|
|
return false;
|
|
}
|
|
|
|
$url = $api_url . '/api/products/' . intval($id);
|
|
|
|
$response = wp_remote_get($url, [
|
|
'headers' => [
|
|
'Authorization' => 'Bearer ' . $api_token
|
|
],
|
|
'timeout' => 30
|
|
]);
|
|
|
|
if (is_wp_error($response)) {
|
|
return false;
|
|
}
|
|
|
|
$body = wp_remote_retrieve_body($response);
|
|
$data = json_decode($body, true);
|
|
|
|
return $data['product'] ?? false;
|
|
}
|
|
}
|
|
|
|
// Initialize Plugin
|
|
function dutchie_menus() {
|
|
return Dutchie_Menus_Plugin::instance();
|
|
}
|
|
|
|
dutchie_menus();
|