admin_url('admin-ajax.php'),
'test_nonce' => wp_create_nonce('ahmaipsu_test'),
'validate_nonce' => wp_create_nonce('ahmaipsu_test'),
));
// Add inline script for admin functionality
$admin_js = "
jQuery(document).ready(function($) {
// Ensure at least one post type checkbox is selected
$('.ahmaipsu-post-type-checkbox').on('change', function() {
var checkedBoxes = $('.ahmaipsu-post-type-checkbox:checked');
if (checkedBoxes.length === 0) {
$(this).prop('checked', true);
}
});
// Test summary generation
$('#generate_test_summary').on('click', function() {
var content = $('#test_content').val();
var nonce = $('#test_nonce').val();
if (!content.trim()) {
$('#test_result').html('
');
},
complete: function() {
$('#generate_test_summary').prop('disabled', false).text('Generate Test Summary');
}
});
});
});
";
wp_add_inline_script('ahmaipsu-admin-settings', $admin_js);
}
function ahmaipsu_ajax_test() {
// Verify nonce and user permissions
if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'ahmaipsu_test') || !current_user_can('manage_options')) {
wp_die(esc_html__('Security check failed', 'ahm-ai-post-summary'));
}
$content = isset($_POST['content']) ? sanitize_textarea_field(wp_unslash($_POST['content'])) : '';
if (empty($content)) {
wp_send_json_error(esc_html__('No content provided for testing.', 'ahm-ai-post-summary'));
}
$options = get_option('ahmaipsu_settings', array());
$char_count = isset($options['ahmaipsu_char_count']) ? intval($options['ahmaipsu_char_count']) : 200;
$content_type = isset($options['ahmaipsu_summary_type']) ? sanitize_text_field($options['ahmaipsu_summary_type']) : 'summary';
$summary = ahmaipsu_API_Handler::generate_summary($content, $char_count, $content_type);
if (is_wp_error($summary)) {
wp_send_json_error($summary->get_error_message());
} else {
wp_send_json_success($summary);
}
}
function ahmaipsu_ajax_validate_api_key() {
// Verify nonce and user permissions
if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'ahmaipsu_test') || !current_user_can('manage_options')) {
wp_die(esc_html__('Security check failed', 'ahm-ai-post-summary'));
}
$api_key = isset($_POST['api_key']) ? sanitize_text_field(wp_unslash($_POST['api_key'])) : '';
$api_provider = isset($_POST['api_provider']) ? sanitize_text_field(wp_unslash($_POST['api_provider'])) : 'gemini';
if (empty($api_key)) {
wp_send_json_error(esc_html__('Please enter an API key to validate.', 'ahm-ai-post-summary'));
}
// Ensure valid provider
if (!in_array($api_provider, array('gemini', 'chatgpt'))) {
$api_provider = 'gemini';
}
// Validate the API key using our API handler
$validation_result = ahmaipsu_API_Handler::validate_api_key($api_key, $api_provider);
if (is_wp_error($validation_result)) {
wp_send_json_error($validation_result->get_error_message());
}
$models = ahmaipsu_API_Handler::list_models($api_key, $api_provider);
if (is_wp_error($models)) {
$models = ahmaipsu_API_Handler::get_fallback_models($api_provider);
}
$provider_name = ($api_provider === 'gemini') ? 'Gemini' : 'ChatGPT';
/* translators: %s: API provider name (Gemini or ChatGPT) */
wp_send_json_success(array(
'message' => sprintf(esc_html__('β %s API key is valid and working correctly!', 'ahm-ai-post-summary'), $provider_name),
'models' => $models,
'default_model' => ahmaipsu_API_Handler::get_default_model($api_provider),
));
}
function ahmaipsu_donate_admin_notice() {
// Check if user has dismissed the notice for this month
$current_user = wp_get_current_user();
$dismissed_key = 'ahmaipsu_donate_notice_dismissed_' . gmdate('Y_m');
$dismissed = get_user_meta($current_user->ID, $dismissed_key, true);
if ($dismissed) {
return;
}
// Show the notice
?>
β
ID, $dismissed_key, '1');
wp_die();
}
function ahmaipsu_add_admin_menu() {
add_menu_page(
'AHM AI Post Summary Settings', // Page title
'AHM AI Summary', // Menu title (shorter for menu)
'manage_options', // Capability
'ahmaipsu', // Menu slug
'ahmaipsu_options_page', // Callback function
'dashicons-lightbulb', // Icon (lightbulb represents AI/ideas)
58 // Position (after Comments, before Appearance)
);
}
// Sanitization callback for settings
function ahmaipsu_sanitize_settings($input) {
$sanitized = array();
// Sanitize API provider
if (isset($input['ahmaipsu_api_provider'])) {
$sanitized['ahmaipsu_api_provider'] = in_array($input['ahmaipsu_api_provider'], array('gemini', 'chatgpt')) ? $input['ahmaipsu_api_provider'] : 'gemini';
}
// Sanitize API key
if (isset($input['ahmaipsu_api_key'])) {
$sanitized['ahmaipsu_api_key'] = sanitize_text_field($input['ahmaipsu_api_key']);
}
// Sanitize model ID
if (isset($input['ahmaipsu_model'])) {
$model = ahmaipsu_API_Handler::sanitize_model_id($input['ahmaipsu_model']);
$provider = isset($sanitized['ahmaipsu_api_provider']) ? $sanitized['ahmaipsu_api_provider'] : 'gemini';
$sanitized['ahmaipsu_model'] = ($model !== '') ? $model : ahmaipsu_API_Handler::get_default_model($provider);
}
// Sanitize character count
if (isset($input['ahmaipsu_char_count'])) {
$char_count = intval($input['ahmaipsu_char_count']);
$sanitized['ahmaipsu_char_count'] = ($char_count >= 50 && $char_count <= 1500) ? $char_count : 200;
}
// Sanitize global enable checkbox - only allow if API key is present
if (isset($input['ahmaipsu_global_enable'])) {
$api_key = isset($input['ahmaipsu_api_key']) ? trim(sanitize_text_field($input['ahmaipsu_api_key'])) : '';
// If no API key provided, check existing settings
if (empty($api_key)) {
$existing_options = get_option('ahmaipsu_settings', array());
$api_key = isset($existing_options['ahmaipsu_api_key']) ? trim($existing_options['ahmaipsu_api_key']) : '';
}
if (!empty($api_key)) {
$sanitized['ahmaipsu_global_enable'] = 1;
} else {
$sanitized['ahmaipsu_global_enable'] = 0;
// Add admin notice about requiring API key
add_settings_error(
'ahmaipsu_settings',
'api_key_required',
__('Global summaries cannot be enabled without a valid API key. Please enter your API key first.', 'ahm-ai-post-summary'),
'error'
);
}
} else {
$sanitized['ahmaipsu_global_enable'] = 0;
}
$sanitized['ahmaipsu_remote_autogen'] = isset($input['ahmaipsu_remote_autogen']) ? 1 : 0;
// Sanitize disclaimer text
if (isset($input['ahmaipsu_disclaimer'])) {
$sanitized['ahmaipsu_disclaimer'] = sanitize_textarea_field($input['ahmaipsu_disclaimer']);
}
// Sanitize default language
if (isset($input['ahmaipsu_default_language'])) {
$allowed_languages = ['auto', 'english', 'burmese', 'french', 'spanish', 'chinese', 'japanese', 'korean', 'thai', 'arabic', 'hindi'];
$default_language = sanitize_text_field($input['ahmaipsu_default_language']);
if (in_array($default_language, $allowed_languages)) {
$sanitized['ahmaipsu_default_language'] = $default_language;
} else {
$sanitized['ahmaipsu_default_language'] = 'auto'; // fallback to auto-detect
}
}
// Sanitize theme selection
if (isset($input['ahmaipsu_theme'])) {
$allowed_themes = ['classic', 'minimal', 'modern', 'elegant', 'card'];
$theme = sanitize_text_field($input['ahmaipsu_theme']);
if (in_array($theme, $allowed_themes)) {
$sanitized['ahmaipsu_theme'] = $theme;
} else {
$sanitized['ahmaipsu_theme'] = 'classic'; // fallback to classic
}
}
// Sanitize Summary type (keep both option keys in sync)
$existing_options = isset($existing_options) ? $existing_options : get_option('ahmaipsu_settings', array());
$allowed_summary_types = ['summary', 'key_takeaways'];
if (isset($input['ahmaipsu_summary_type'])) {
$summary_type = sanitize_text_field($input['ahmaipsu_summary_type']);
if (!in_array($summary_type, $allowed_summary_types, true)) {
$summary_type = 'summary';
}
} else {
$summary_type = $existing_options['ahmaipsu_summary_type'] ?? $existing_options['ahmaipsu_content_type'] ?? 'summary';
if (!in_array($summary_type, $allowed_summary_types, true)) {
$summary_type = 'summary';
}
}
$sanitized['ahmaipsu_summary_type'] = $summary_type;
$sanitized['ahmaipsu_content_type'] = $summary_type;
// Sanitize custom summary title
if (isset($input['ahmaipsu_custom_summary_title'])) {
$sanitized['ahmaipsu_custom_summary_title'] = sanitize_text_field($input['ahmaipsu_custom_summary_title']);
}
// Sanitize custom key takeaways title
if (isset($input['ahmaipsu_custom_key_takeaways_title'])) {
$sanitized['ahmaipsu_custom_key_takeaways_title'] = sanitize_text_field($input['ahmaipsu_custom_key_takeaways_title']);
}
// Destination sync checkboxes (unchecked = 0)
$sanitized['ahmaipsu_sync_excerpt'] = isset($input['ahmaipsu_sync_excerpt']) ? 1 : 0;
$sanitized['ahmaipsu_sync_yoast'] = isset($input['ahmaipsu_sync_yoast']) ? 1 : 0;
$sanitized['ahmaipsu_sync_rankmath'] = isset($input['ahmaipsu_sync_rankmath']) ? 1 : 0;
$sanitized['ahmaipsu_sync_on_regenerate'] = isset($input['ahmaipsu_sync_on_regenerate']) ? 1 : 0;
$sanitized['ahmaipsu_sync_overwrite'] = isset($input['ahmaipsu_sync_overwrite']) ? 1 : 0;
// Sanitize supported post types
if (isset($input['ahmaipsu_post_types']) && is_array($input['ahmaipsu_post_types'])) {
$allowed_post_types = ['post', 'page'];
$sanitized_post_types = array();
foreach ($input['ahmaipsu_post_types'] as $post_type) {
if (in_array($post_type, $allowed_post_types)) {
$sanitized_post_types[] = $post_type;
}
}
// Ensure at least one post type is selected
if (empty($sanitized_post_types)) {
$sanitized_post_types = ['post']; // Default to post if none selected
}
$sanitized['ahmaipsu_post_types'] = $sanitized_post_types;
} else {
// Default to post if not set
$sanitized['ahmaipsu_post_types'] = ['post'];
}
// Add success message if settings were saved successfully
if (!empty($sanitized)) {
add_settings_error(
'ahmaipsu_settings',
'settings_saved',
__('β Settings saved successfully! Your AI Post Summary configuration has been updated.', 'ahm-ai-post-summary'),
'success'
);
}
return $sanitized;
}
function ahmaipsu_settings_init() {
register_setting('ahmaipsu', 'ahmaipsu_settings', array(
'type' => 'array',
'sanitize_callback' => 'ahmaipsu_sanitize_settings',
'show_in_rest' => false
));
// API Settings Section
add_settings_section(
'ahmaipsu_api_section',
__('API Configuration', 'ahm-ai-post-summary'),
null,
'ahmaipsu'
);
add_settings_field(
'ahmaipsu_api_provider',
__('API Provider', 'ahm-ai-post-summary'),
'ahmaipsu_api_provider_render',
'ahmaipsu',
'ahmaipsu_api_section'
);
add_settings_field(
'ahmaipsu_api_key',
__('API Key (Gemini/ChatGPT)', 'ahm-ai-post-summary'),
'ahmaipsu_api_key_render',
'ahmaipsu',
'ahmaipsu_api_section'
);
add_settings_field(
'ahmaipsu_model',
__('Model', 'ahm-ai-post-summary'),
'ahmaipsu_model_render',
'ahmaipsu',
'ahmaipsu_api_section'
);
// Summary Settings Section
add_settings_section(
'ahmaipsu_summary_section',
__('Summary Configuration', 'ahm-ai-post-summary'),
null,
'ahmaipsu'
);
add_settings_field(
'ahmaipsu_char_count',
__('Summary Character Count', 'ahm-ai-post-summary'),
'ahmaipsu_char_count_render',
'ahmaipsu',
'ahmaipsu_summary_section'
);
add_settings_field(
'ahmaipsu_global_enable',
__('Enable Globally', 'ahm-ai-post-summary'),
'ahmaipsu_global_enable_render',
'ahmaipsu',
'ahmaipsu_summary_section'
);
add_settings_field(
'ahmaipsu_remote_autogen',
__('REST / XML-RPC', 'ahm-ai-post-summary'),
'ahmaipsu_remote_autogen_render',
'ahmaipsu',
'ahmaipsu_summary_section'
);
add_settings_field(
'ahmaipsu_post_types',
__('Supported Post Types', 'ahm-ai-post-summary'),
'ahmaipsu_post_types_render',
'ahmaipsu',
'ahmaipsu_summary_section'
);
add_settings_field(
'ahmaipsu_sync_destinations',
__('Also write to', 'ahm-ai-post-summary'),
'ahmaipsu_sync_destinations_render',
'ahmaipsu',
'ahmaipsu_summary_section'
);
add_settings_field(
'ahmaipsu_default_language',
__('Default Language', 'ahm-ai-post-summary'),
'ahmaipsu_default_language_render',
'ahmaipsu',
'ahmaipsu_summary_section'
);
// Display Settings Section
add_settings_section(
'ahmaipsu_display_section',
__('Display Configuration', 'ahm-ai-post-summary'),
null,
'ahmaipsu'
);
add_settings_field(
'ahmaipsu_disclaimer',
__('Disclaimer Text', 'ahm-ai-post-summary'),
'ahmaipsu_disclaimer_render',
'ahmaipsu',
'ahmaipsu_display_section'
);
add_settings_field(
'ahmaipsu_theme',
__('Choose Theme', 'ahm-ai-post-summary'),
'ahmaipsu_theme_render',
'ahmaipsu',
'ahmaipsu_display_section'
);
add_settings_field(
'ahmaipsu_summary_type',
__('Content Type', 'ahm-ai-post-summary'),
'ahmaipsu_summary_type_render',
'ahmaipsu',
'ahmaipsu_display_section'
);
add_settings_field(
'ahmaipsu_custom_summary_title',
__('Custom Summary Title', 'ahm-ai-post-summary'),
'ahmaipsu_custom_summary_title_render',
'ahmaipsu',
'ahmaipsu_display_section'
);
add_settings_field(
'ahmaipsu_custom_key_takeaways_title',
__('Custom Key Takeaways Title', 'ahm-ai-post-summary'),
'ahmaipsu_custom_key_takeaways_title_render',
'ahmaipsu',
'ahmaipsu_display_section'
);
}
function ahmaipsu_api_provider_render() {
$options = get_option('ahmaipsu_settings', array());
$provider = isset($options['ahmaipsu_api_provider']) ? $options['ahmaipsu_api_provider'] : 'gemini';
?>
Choose your preferred AI service. Gemini is recommended for better performance and lower costs.
';
echo '';
// Always show the validation button, but initially hide it if no API key
$button_class = empty(trim($api_key)) ? 'button button-secondary ahmaipsu-validate-button hidden' : 'button button-secondary ahmaipsu-validate-button';
echo '';
echo '';
// API key validation result container
echo '';
echo '
';
echo '
';
echo 'π Get your Gemini API key: ';
echo '1. Visit Google AI Studio β ';
echo '2. Sign in with your Google account ';
echo '3. Click "Create API Key" and select your project ';
echo '4. Copy the generated API key and paste it above ';
echo 'π‘ Gemini offers generous free tier and faster responses.';
echo '
';
echo '
';
echo '
';
echo '
';
echo 'π Get your ChatGPT API key: ';
echo '1. Visit OpenAI Platform β ';
echo '2. Sign in to your OpenAI account (create one if needed) ';
echo '3. Click "Create new secret key" and give it a name ';
echo '4. Copy the generated API key and paste it above ';
echo 'β οΈ Note: You may need to add billing information to use the API.';
echo '
When global auto-generation is on, posts published by APIs or agents (with no editor checkbox) still get a summary. Gutenberg already sends the editor flag; this does not double-generate those saves. Per-post off still wins if the agent sent it.
Choose "Detect Automatically" to let the AI analyze content and pick the best language, or select a specific language to force all summaries to use that language.
';
}
function ahmaipsu_disclaimer_render() {
$options = get_option('ahmaipsu_settings');
$disclaimer = $options['ahmaipsu_disclaimer'] ?? 'This summary was generated by AI and may contain inaccuracies or omissions. Please refer to the full article for complete information.';
echo '';
echo '
This disclaimer will appear below all AI-generated summaries on your site.
';
}
function ahmaipsu_summary_type_render() {
$options = get_option('ahmaipsu_settings');
$summary_type = $options['ahmaipsu_summary_type'] ?? $options['ahmaipsu_content_type'] ?? 'summary';
?>
' . esc_html__('If you find this plugin helpful, consider supporting its development. Your donation helps maintain and improve the plugin!', 'ahm-ai-post-summary') . '';
}
function ahmaipsu_show_donate_render() {
$options = get_option('ahmaipsu_settings');
$show_donate = isset($options['ahmaipsu_show_donate']) ? $options['ahmaipsu_show_donate'] : false;
?>
id="ahmaipsu_show_donate" />
array(
'name' => 'π― Classic',
'description' => 'Hairline box with a 2px accent underline.',
'title' => 'π Summary',
'body' => 'This is how your summary will appear with the Classic theme.',
),
'minimal' => array(
'name' => 'β¨ Minimal',
'description' => 'No box, no fill. Small tracked label.',
'title' => 'Summary',
'body' => 'This is how your summary will appear with the Minimal theme.',
),
'modern' => array(
'name' => 'π Modern',
'description' => 'Uppercase kicker and a 2px accent bar on a surface panel.',
'title' => 'Summary',
'body' => 'This is how your summary will appear with the Modern theme.',
),
'elegant' => array(
'name' => 'π Elegant',
'description' => 'Italic title and a 3px left accent bar.',
'title' => 'Summary',
'body' => 'This is how your summary will appear with the Elegant theme.',
),
'card' => array(
'name' => 'π Card',
'description' => 'Raised object with radius, soft shadow, and a hairline.',
'title' => 'π Summary',
'body' => 'This is how your summary will appear with the Card theme.',
),
);
echo '
';
echo '
π¨ Choose how you want your AI summaries to appear on your site. Click on any theme below to select it and see a live preview.
';
// Hidden input to store selected theme
echo '';
foreach ($themes as $theme_key => $theme_data) {
echo '
';
echo '
';
echo '
' . esc_html($theme_data['name']) . '
';
echo '
' . esc_html($theme_data['description']) . '
';
echo '
';
echo '
';
echo '
';
echo '
' . esc_html($theme_data['title']) . '
';
echo '
' . esc_html($theme_data['body']) . '
';
echo '
βΉοΈ ' . esc_html($disclaimer) . '
';
echo '
';
echo '
';
echo '
';
}
echo '
';
echo '
';
echo '
π‘ Pro Tip: You can further customize the appearance by adding custom CSS to your theme\'s Additional CSS section under Appearance > Customize.