settings = new AI_Consigliere_Settings(); $this->openai = new AI_Consigliere_OpenAI($this->settings); add_action('admin_menu', [$this, 'add_admin_pages']); add_action('admin_init', [$this->settings, 'register_settings']); add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts']); do_action('aiconsigliere_register_custom_prompts', $this); add_action('wp_ajax_aiconsigliere_get_taxonomies', [$this, 'aiconsigliere_get_taxonomies_callback']); add_action('wp_ajax_aiconsigliere_check_taxonomy', [$this, 'aiconsigliere_check_taxonomy_callback']); add_filter('aiconsigliere_get_openai_response', [$this, 'handle_openai_query'], 10, 3); } public function add_admin_pages() { add_menu_page( __('AI Consigliere', 'ai-consigliere'), __('AI Consigliere', 'ai-consigliere'), 'manage_options', 'ai-consigliere', [$this, 'render_settings_page'], 'dashicons-rest-api', 60 ); add_submenu_page( 'ai-consigliere', __('Check Taxonomy', 'ai-consigliere'), __('Check Taxonomy', 'ai-consigliere'), 'manage_options', 'ai-consigliere-check', [$this, 'render_taxonomy_checker'] ); do_action('aiconsigliere_add_admin_pages'); } public function render_settings_page() { $this->load_page('settings-page.php', ['settings' => $this->settings]); } public function render_taxonomy_checker() { $this->load_page('taxonomy-checker.php', ['settings' => $this->settings]); } public function enqueue_scripts($hook) { if (strpos($hook, 'ai-consigliere') === false) return; $base_path = plugin_dir_path(dirname(__FILE__)); $base_url = plugin_dir_url(dirname(__FILE__)); $js_file = 'includes/js/aic.js'; $js_path = $base_path . $js_file; $js_url = $base_url . $js_file; $js_ver = file_exists($js_path) ? filemtime($js_path) : '1.0.0'; wp_enqueue_script('aiconsigliere-script', $js_url, ['jquery'], $js_ver, true); wp_localize_script('aiconsigliere-script', 'AICONSIGLIERE', [ 'ajax_url' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('aiconsigliere_nonce'), ]); $css_file = 'includes/css/aic.css'; $css_path = $base_path . $css_file; $css_url = $base_url . $css_file; $css_ver = file_exists($css_path) ? filemtime($css_path) : '1.0.0'; wp_enqueue_style('aiconsigliere-style', $css_url, [], $css_ver); } public function load_page($filename, $vars = [], $override_plugin_path = false) { extract($vars); $plugin_dir = plugin_dir_path(__FILE__) . '/../'; $body_template = $plugin_dir . 'templates/body_ai-consigliere.php'; $content_template = $plugin_dir . 'templates/include/' . $filename; if ($override_plugin_path && isset($vars['override_base_path'])) { $content_template = trailingslashit($vars['override_base_path']) . $filename; } include $body_template; } public function aiconsigliere_get_taxonomies_callback() { check_ajax_referer('aiconsigliere_nonce', 'nonce'); $post_type = sanitize_text_field(wp_unslash($_POST['post_type'] ?? '')); if (empty($post_type)) { wp_send_json_error(__('Post type is missing.', 'ai-consigliere')); } $taxonomies = get_object_taxonomies($post_type, 'objects'); if (empty($taxonomies)) { wp_send_json_error(__('No taxonomies found.', 'ai-consigliere')); } ob_start(); ?> openai->query($prompt, $args); } public function aiconsigliere_check_taxonomy_callback() { check_ajax_referer('aiconsigliere_nonce', 'nonce'); $taxonomy = sanitize_text_field(wp_unslash($_POST['taxonomy'] ?? '')); if (empty($taxonomy)) { wp_send_json_error(__('⚠️ Taxonomy is missing.', 'ai-consigliere')); } $terms = get_terms([ 'taxonomy' => $taxonomy, 'hide_empty' => false, ]); $term_names = wp_list_pluck($terms, 'name'); $term_list = implode(", ", array_slice($term_names, 0, 20)); $posts = get_posts([ 'post_type' => 'any', 'posts_per_page' => 5, 'tax_query' => [[ 'taxonomy' => $taxonomy, 'operator' => 'EXISTS', ]], ]); $examples = []; foreach ($posts as $post) { $post_terms = wp_get_post_terms($post->ID, $taxonomy, ['fields' => 'names']); $examples[] = "- {$post->post_title} ⇒ " . implode(', ', $post_terms); } $language_map = AI_Consigliere_Settings::get_language_map(); $settings = $this->settings->get_settings(); $lang_code = $settings['lang'] ?? 'en'; $lang_name = $language_map[$lang_code] ?? 'English'; // translators: %s is the name of the language (e.g., "English"). $language_instruction = sprintf(__('Please reply in %s.', 'ai-consigliere'), $lang_name); $example_text = implode("\n", $examples); $prompt = trim( 'Check the coherence of the taxonomy "' . $taxonomy . '" in relation to its usage in WordPress posts.' . "\n" . $language_instruction . "\n\n" . 'The taxonomy terms are:' . "\n" . $term_list . "\n\n" . 'Here are some example posts and the associated terms:' . "\n" . $example_text . "\n\n" . 'Please respond by pointing out whether the terms are coherent, if there are any repetitions, inconsistencies, or classification errors.' ); $response = $this->openai->query($prompt); wp_send_json_success($response); } }