prefix . "ai_moderation_history"; // Correct WordPress table prefix
$charset_collate = $wpdb->get_charset_collate(); // Get charset
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; // Ensure dbDelta() is available
$sql = "CREATE TABLE IF NOT EXISTS {$table_name} (
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
comment_id BIGINT UNSIGNED NOT NULL,
action VARCHAR(50) NOT NULL,
ai_confidence DECIMAL(5,2) DEFAULT NULL,
moderated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (comment_id) REFERENCES {$wpdb->comments}(comment_ID) ON DELETE CASCADE
) {$charset_collate};";
dbDelta($sql);
}
register_activation_hook(__FILE__, 'aicomo_create_moderation_table');
// 1?? Retrieve API key from WordPress options (or default to empty string).
$aicomo_api_key = get_option('aicomo_api_key', '');
// 2?? Define constants only if they're not defined yet
if (!defined('AICOMO_API_KEY')) {
define('AICOMO_API_KEY', $aicomo_api_key);
}
if (!defined('AICOMO_PLUGIN_VERSION')) {
define('AICOMO_PLUGIN_VERSION', '1.0');
}
if (!defined('AICOMO_PLUGIN_DIR')) {
define('AICOMO_PLUGIN_DIR', plugin_dir_path(__FILE__));
}
if (!defined('AICOMO_MODERATOR_PATH')) {
define('AICOMO_MODERATOR_PATH', plugin_dir_path(__DIR__));
}
if (!defined('AICOMO_API_ENDPOINT')) {
define('AICOMO_API_ENDPOINT', 'https://api.openai.com/v1/moderations');
}
// 3?? Include Required Files
require_once AICOMO_PLUGIN_DIR . 'includes/config.php';
require_once AICOMO_PLUGIN_DIR . 'includes/moderation.php';
require_once AICOMO_PLUGIN_DIR . 'includes/admin-page.php';
require_once AICOMO_PLUGIN_DIR . 'includes/ajax.php';
require_once AICOMO_PLUGIN_DIR . 'includes/admin-settings.php';
require_once AICOMO_PLUGIN_DIR . 'includes/chatgpt.php';
/**
* Enqueue admin scripts & styles for AI Comment Moderator plugin.
*
* @param string $hook The current admin page hook.
*/
function aicomo_admin_enqueue_scripts($hook) {
// Load scripts/styles only on the correct admin page
if ($hook !== 'toplevel_page_ai-comment-auto-moderator') {
return;
}
// Plugin directory URL for assets
$plugin_url = plugin_dir_url(__FILE__);
// Register & enqueue main JS
wp_enqueue_script(
'ai-moderator-js',
$plugin_url . 'assets/js/ai-moderator.js',
array('jquery'), // Dependencies
AICOMO_PLUGIN_VERSION, // Use new plugin version constant
true // Load in footer
);
// Register & enqueue secondary JS
wp_enqueue_script(
'ai-main-mod-js',
$plugin_url . 'assets/js/ai-main-mod.js',
array('jquery', 'ai-moderator-js'), // Load after `ai-moderator.js`
AICOMO_PLUGIN_VERSION,
true
);
// Localize script with AJAX variables
wp_localize_script('ai-moderator-js', 'aiModeratorAjax', array(
'ajaxurl' => admin_url('admin-ajax.php'),
// Updated nonce action name to match your new prefix
'security' => wp_create_nonce('aicomo_nonce_action'),
));
// Register & enqueue CSS
wp_enqueue_style(
'ai-moderator-css',
$plugin_url . 'assets/css/ai-moderator.css',
array(), // No dependencies
AICOMO_PLUGIN_VERSION
);
// Optionally add `defer` to these scripts for better performance
add_filter('script_loader_tag', function ($tag, $handle) {
if (in_array($handle, ['ai-moderator-js', 'ai-main-mod-js'])) {
return str_replace(' src=', ' defer src=', $tag);
}
return $tag;
}, 10, 2);
}
add_action('admin_enqueue_scripts', 'aicomo_admin_enqueue_scripts');
/**
* Add defer attribute to AI Moderator JS for better performance (optional extra).
*
* @param string $tag The script tag.
* @param string $handle The script handle.
* @return string Modified script tag with defer attribute.
*/
function aicomo_add_defer_to_script($tag, $handle) {
if ('ai-moderator-js' === $handle) {
return str_replace('src=', 'defer src=', $tag);
}
return $tag;
}
add_filter('script_loader_tag', 'aicomo_add_defer_to_script', 10, 2);
/**
* Add plugin action links (Settings & Premium) in the Plugins page.
*
* @param array $links Existing plugin action links.
* @return array Modified links with Settings and Premium options.
*/
function aicomo_plugin_links($links) {
$settings_link = '⚙ Settings';
$premium_link = '🚀 Go Premium';
array_unshift($links, $settings_link, $premium_link);
return $links;
}
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'aicomo_plugin_links');