version = DP_AICOSO_VERSION; $this->plugin_name = 'aicoso-dynamic-pricing-for-woocommerce'; $this->load_dependencies(); $this->set_locale(); $this->define_admin_hooks(); $this->define_public_hooks(); $this->init_woocommerce_integration(); } /** * Load the required dependencies for this plugin. */ private function load_dependencies() { // The class responsible for orchestrating the actions and filters of the core plugin. require_once DP_AICOSO_PLUGIN_DIR . 'includes/class-dp-aicoso-loader.php'; // The class responsible for defining internationalization functionality. require_once DP_AICOSO_PLUGIN_DIR . 'includes/class-dp-aicoso-i18n.php'; // The class responsible for defining all actions that occur in the admin area. require_once DP_AICOSO_PLUGIN_DIR . 'admin/class-dp-aicoso-admin.php'; // The class responsible for defining all actions that occur in the public-facing side. require_once DP_AICOSO_PLUGIN_DIR . 'public/class-dp-aicoso-public.php'; // Pricing rules classes. require_once DP_AICOSO_PLUGIN_DIR . 'includes/pricing-rules/class-dp-aicoso-abstract-pricing-rule.php'; require_once DP_AICOSO_PLUGIN_DIR . 'includes/pricing-rules/class-dp-aicoso-product-rule.php'; require_once DP_AICOSO_PLUGIN_DIR . 'includes/pricing-rules/class-dp-aicoso-category-rule.php'; require_once DP_AICOSO_PLUGIN_DIR . 'includes/pricing-rules/class-dp-aicoso-role-rule.php'; require_once DP_AICOSO_PLUGIN_DIR . 'includes/pricing-rules/class-dp-aicoso-time-rule.php'; // Calculator and cache classes. require_once DP_AICOSO_PLUGIN_DIR . 'includes/class-dp-aicoso-cache-manager.php'; require_once DP_AICOSO_PLUGIN_DIR . 'includes/calculators/class-dp-aicoso-price-calculator.php'; // WooCommerce integration. require_once DP_AICOSO_PLUGIN_DIR . 'includes/class-dp-aicoso-woocommerce-integration.php'; $this->loader = new DP_AICOSO_Loader(); } /** * Define the locale for this plugin for internationalization. */ private function set_locale() { $plugin_i18n = new DP_AICOSO_I18n(); $this->loader->add_action( 'plugins_loaded', $plugin_i18n, 'load_plugin_textdomain' ); } /** * Register all of the hooks related to the admin area functionality. */ private function define_admin_hooks() { $plugin_admin = new DP_AICOSO_Admin( $this->get_plugin_name(), $this->get_version() ); $this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' ); $this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' ); $this->loader->add_action( 'admin_menu', $plugin_admin, 'add_admin_menu' ); $this->loader->add_action( 'admin_init', $this, 'register_settings' ); // Handle exports early before any output. $this->loader->add_action( 'admin_init', $plugin_admin, 'handle_export_early', 1 ); // AJAX handlers. $this->loader->add_action( 'wp_ajax_dp_aicoso_save_rule', $plugin_admin, 'ajax_save_rule' ); $this->loader->add_action( 'wp_ajax_dp_aicoso_delete_rule', $plugin_admin, 'ajax_delete_rule' ); $this->loader->add_action( 'wp_ajax_dp_aicoso_duplicate_rule', $plugin_admin, 'ajax_duplicate_rule' ); $this->loader->add_action( 'wp_ajax_dp_aicoso_toggle_status', $plugin_admin, 'ajax_toggle_status' ); $this->loader->add_action( 'wp_ajax_dp_aicoso_search_products', $plugin_admin, 'ajax_search_products' ); $this->loader->add_action( 'wp_ajax_dp_aicoso_clear_cache', $plugin_admin, 'ajax_clear_cache' ); } /** * Register plugin settings. */ public function register_settings() { register_setting( 'dp_aicoso_settings_group', 'dp_aicoso_settings', array( 'type' => 'array', 'sanitize_callback' => array( $this, 'sanitize_settings' ), ) ); } /** * Sanitize plugin settings. * * @param array $input The settings input. * @return array Sanitized settings. */ public function sanitize_settings( $input ) { $sanitized = array(); if ( isset( $input['enable_cache'] ) ) { $sanitized['enable_cache'] = ( '1' === $input['enable_cache'] ) ? '1' : '0'; } if ( isset( $input['cache_expiration'] ) ) { $sanitized['cache_expiration'] = absint( $input['cache_expiration'] ); } if ( isset( $input['enable_debug'] ) ) { $sanitized['enable_debug'] = ( '1' === $input['enable_debug'] ) ? '1' : '0'; } if ( isset( $input['bulk_pricing_table'] ) ) { $sanitized['bulk_pricing_table'] = ( '1' === $input['bulk_pricing_table'] ) ? '1' : '0'; } if ( isset( $input['show_original_price'] ) ) { $sanitized['show_original_price'] = ( '1' === $input['show_original_price'] ) ? '1' : '0'; } if ( isset( $input['use_database_cache'] ) ) { $sanitized['use_database_cache'] = ( '1' === $input['use_database_cache'] ) ? '1' : '0'; } return $sanitized; } /** * Register all of the hooks related to the public-facing functionality. */ private function define_public_hooks() { $plugin_public = new DP_AICOSO_Public( $this->get_plugin_name(), $this->get_version() ); $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' ); $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' ); // Note: Price filters are handled by WooCommerce Integration class to avoid duplication. // The public class methods for pricing are kept for backward compatibility but not hooked here. } /** * Initialize WooCommerce integration. */ private function init_woocommerce_integration() { // Initialize WooCommerce integration on init action with proper checks. $this->loader->add_action( 'init', $this, 'load_woocommerce_integration', 20 ); } /** * Load WooCommerce integration. */ public function load_woocommerce_integration() { // Prevent multiple initializations. static $initialized = false; if ( $initialized ) { return; } if ( class_exists( 'WooCommerce' ) ) { // Check if required database tables exist before initializing. if ( $this->check_database_tables() ) { // Use the fixed WooCommerce Integration class with advanced conditions support. new DP_AICOSO_WooCommerce_Integration(); $initialized = true; } } } /** * Check if required database tables exist. * * @return bool True if tables exist, false otherwise. */ private function check_database_tables() { global $wpdb; $table_name = $wpdb->prefix . 'dp_aicoso_rules'; // Check if the rules table exists. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Table existence check, initialization operation $table_exists = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table_name ) ) === $table_name; return $table_exists; } /** * Run the loader to execute all of the hooks with WordPress. */ public function run() { $this->loader->run(); } /** * The name of the plugin used to uniquely identify it. * * @return string The name of the plugin. */ public function get_plugin_name() { return $this->plugin_name; } /** * The reference to the class that orchestrates the hooks with the plugin. * * @return DP_AICOSO_Loader Orchestrates the hooks of the plugin. */ public function get_loader() { return $this->loader; } /** * Retrieve the version number of the plugin. * * @return string The version number of the plugin. */ public function get_version() { return $this->version; } }