api = $api; $this->settings = $settings; add_action('wp_ajax_aicp_test_woocommerce', array($this, 'ajax_test_woocommerce')); add_action('wp_ajax_aicp_send_test_product', array($this, 'ajax_send_test_product')); } /** * Check if WooCommerce integration is available * * @return bool */ public function is_available() { // Check if connected and tier supports WooCommerce $tier = $this->settings->get_subscription_tier(); $supported_tiers = array('Professional', 'Business', 'Enterprise'); return $this->settings->is_connected() && in_array($tier, $supported_tiers, true); } /** * Get WooCommerce product categories * * @return array */ public function get_product_categories() { if (!function_exists('get_terms')) { return array(); } $categories = get_terms(array( 'taxonomy' => 'product_cat', 'hide_empty' => false, 'orderby' => 'name', 'order' => 'ASC' )); if (is_wp_error($categories)) { return array(); } return array_map(function ($term) { return array( 'id' => $term->term_id, 'name' => $term->name, 'slug' => $term->slug, 'count' => $term->count ); }, $categories); } /** * Check if WooCommerce REST API is enabled * * @return bool */ public function is_rest_api_enabled() { // Check if REST API endpoint exists $rest_url = get_rest_url(null, 'wc/v3/products'); return !empty($rest_url); } /** * AJAX: Test WooCommerce connection */ public function ajax_test_woocommerce() { check_ajax_referer('aicp_woocommerce_nonce', 'nonce'); if (!current_user_can('manage_aicp_settings')) { wp_send_json_error(array('message' => __('Permission denied.', 'ai-content-plan'))); } // Check if WooCommerce is active if (!aicp_bridge()->is_woocommerce_active()) { wp_send_json_error(array('message' => __('WooCommerce is not active.', 'ai-content-plan'))); } // Check REST API if (!$this->is_rest_api_enabled()) { wp_send_json_error(array( 'message' => __('WooCommerce REST API is not available.', 'ai-content-plan') )); } wp_send_json_success(array( 'message' => __('WooCommerce is properly configured.', 'ai-content-plan'), 'categories' => $this->get_product_categories() )); } /** * AJAX: Send a test product to AICP */ public function ajax_send_test_product() { check_ajax_referer('aicp_woocommerce_nonce', 'nonce'); if (!current_user_can('manage_aicp_settings')) { wp_send_json_error(array('message' => __('Permission denied.', 'ai-content-plan'))); } // Get a random product $products = wc_get_products(array( 'limit' => 1, 'orderby' => 'rand', 'status' => 'publish' )); if (empty($products)) { wp_send_json_error(array( 'message' => __('No published products found to test with.', 'ai-content-plan') )); } $product = $products[0]; // This would typically send product data to AICP // For now, just confirm the product was found wp_send_json_success(array( 'message' => sprintf( /* translators: %s: product name */ __('Test product found: %s', 'ai-content-plan'), $product->get_name() ), 'product' => array( 'id' => $product->get_id(), 'name' => $product->get_name(), 'price' => $product->get_price() ) )); } }