ID, '_ahmaipsu_enabled', true); $summary = get_post_meta($post->ID, '_ahmaipsu_content', true); $content_type = get_post_meta($post->ID, '_ahmaipsu_content_type', true); $global_enabled = get_option('ahmaipsu_settings')['ahmaipsu_global_enable'] ?? false; // For new posts/pages (no existing meta), default to global setting if ($enabled === '' && $global_enabled) { $enabled = '1'; } // Default content type from settings if this post has none yet if (empty($content_type)) { $settings = get_option('ahmaipsu_settings', array()); $content_type = $settings['ahmaipsu_summary_type'] ?? $settings['ahmaipsu_content_type'] ?? 'summary'; } // Get post type label for display $post_type_object = get_post_type_object($post->post_type); $post_type_label = $post_type_object ? $post_type_object->labels->singular_name : ucfirst($post->post_type); wp_nonce_field('ahmaipsu_meta', 'ahmaipsu_nonce'); ?>
ID)) ? (int) $post->ID : 0; wp_localize_script('ahmaipsu-post-editor', 'ahmaipsu_editor_vars', array( 'ajax_url' => admin_url('admin-ajax.php'), 'post_id' => $post_id, 'nonce' => wp_create_nonce('ahmaipsu_check_' . $post_id), 'regenerate_nonce' => wp_create_nonce('ahmaipsu_regenerate_' . $post_id), )); } function ahmaipsu_add_meta_box() { $options = get_option('ahmaipsu_settings'); $supported_post_types = isset($options['ahmaipsu_post_types']) ? $options['ahmaipsu_post_types'] : ['post']; foreach ($supported_post_types as $post_type) { add_meta_box( 'ahmaipsu_meta', __('AHM AI Post Summary', 'ahm-ai-post-summary'), 'ahmaipsu_meta_box_callback', $post_type, 'side' ); } } function ahmaipsu_save_post_meta($post_id) { if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return; if (!current_user_can('edit_post', $post_id)) return; if (!isset($_POST['ahmaipsu_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['ahmaipsu_nonce'])), 'ahmaipsu_meta')) return; $enabled = isset($_POST['ahmaipsu_enabled']) ? 1 : 0; update_post_meta($post_id, '_ahmaipsu_enabled', $enabled); // Save content type if (isset($_POST['ahmaipsu_content_type'])) { $content_type = sanitize_text_field(wp_unslash($_POST['ahmaipsu_content_type'])); if (in_array($content_type, ['summary', 'key_takeaways'])) { update_post_meta($post_id, '_ahmaipsu_content_type', $content_type); } } // Handle regeneration request - set a flag instead of deleting immediately if (isset($_POST['ahmaipsu_regenerate']) && sanitize_text_field(wp_unslash($_POST['ahmaipsu_regenerate'])) === '1') { // Set a flag to regenerate summary update_post_meta($post_id, '_ahmaipsu_regenerate_flag', '1'); } else { // Remove the flag if not checking regenerate delete_post_meta($post_id, '_ahmaipsu_regenerate_flag'); } } function ahmaipsu_register_rest_autogen() { $options = get_option('ahmaipsu_settings', array()); $types = isset($options['ahmaipsu_post_types']) ? $options['ahmaipsu_post_types'] : array('post'); foreach ($types as $type) { add_action('rest_after_insert_' . $type, 'ahmaipsu_rest_after_insert', 20, 3); } } function ahmaipsu_rest_after_insert($post, $request, $creating) { unset($creating); if (!$post instanceof WP_Post) { return; } $GLOBALS['ahmaipsu_from_rest_after_insert'] = true; $GLOBALS['ahmaipsu_rest_request'] = $request; ahmaipsu_auto_generate($post->ID); unset($GLOBALS['ahmaipsu_from_rest_after_insert'], $GLOBALS['ahmaipsu_rest_request']); } /** * @return bool|null true on, false explicit off, null unset */ function ahmaipsu_explicit_enabled_state($post_id) { $rows = get_post_meta($post_id, '_ahmaipsu_enabled', false); if (!is_array($rows) || count($rows) === 0) { return null; } $val = $rows[0]; if ($val === '' || $val === null) { return null; } if ($val === '0' || $val === 0 || $val === 'false') { return false; } if ($val === '1' || $val === 1 || $val === true || $val === 'true') { return true; } return !empty($val); } function ahmaipsu_rest_request_sent_enabled_meta() { if (empty($GLOBALS['ahmaipsu_rest_request']) || !($GLOBALS['ahmaipsu_rest_request'] instanceof WP_REST_Request)) { return false; } $meta = $GLOBALS['ahmaipsu_rest_request']->get_param('meta'); return is_array($meta) && array_key_exists('_ahmaipsu_enabled', $meta); } function ahmaipsu_post_should_autogen($post_id) { $options = get_option('ahmaipsu_settings', array()); $global_enabled = !empty($options['ahmaipsu_global_enable']); $remote_autogen = array_key_exists('ahmaipsu_remote_autogen', $options) ? !empty($options['ahmaipsu_remote_autogen']) : true; if (isset($_POST['ahmaipsu_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['ahmaipsu_nonce'])), 'ahmaipsu_meta')) { if (isset($_POST['ahmaipsu_enabled']) && sanitize_text_field(wp_unslash($_POST['ahmaipsu_enabled'])) == '1') { update_post_meta($post_id, '_ahmaipsu_enabled', '1'); return true; } update_post_meta($post_id, '_ahmaipsu_enabled', '0'); return false; } $state = ahmaipsu_explicit_enabled_state($post_id); if ($state === false) { return false; } if ($state === true) { return true; } if (!$global_enabled) { return false; } $is_xmlrpc = defined('XMLRPC_REQUEST') && XMLRPC_REQUEST; $is_rest = !empty($GLOBALS['ahmaipsu_from_rest_after_insert']); $remote_without_editor_meta = false; if ($is_xmlrpc) { $remote_without_editor_meta = true; } elseif ($is_rest && !ahmaipsu_rest_request_sent_enabled_meta()) { $remote_without_editor_meta = true; } if ($remote_without_editor_meta && !$remote_autogen) { return false; } update_post_meta($post_id, '_ahmaipsu_enabled', '1'); return true; } function ahmaipsu_auto_generate($post_id) { // Check if this is an autosave or revision if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return; if (wp_is_post_revision($post_id)) return; // Prevent running multiple times in the same request static $processed = array(); if (isset($processed[$post_id])) return; // REST applies plugin meta after wp_insert_post/save_post. Gutenberg is REST too: // skip here and generate once from rest_after_insert so we do not double-fire. $from_rest_after = !empty($GLOBALS['ahmaipsu_from_rest_after_insert']); if (!$from_rest_after && defined('REST_REQUEST') && REST_REQUEST) { return; } $processed[$post_id] = true; // Check if this post type is supported $post = get_post($post_id); if (!$post) return; $options = get_option('ahmaipsu_settings'); $supported_post_types = isset($options['ahmaipsu_post_types']) ? $options['ahmaipsu_post_types'] : ['post']; if (!in_array($post->post_type, $supported_post_types)) { return; // Post type not supported } if (!ahmaipsu_post_should_autogen($post_id)) { return; } // Check if we should regenerate or if no summary exists $existing_summary = get_post_meta($post_id, '_ahmaipsu_content', true); $should_regenerate = get_post_meta($post_id, '_ahmaipsu_regenerate_flag', true); // Get post content $post = get_post($post_id); if (!$post || empty($post->post_content)) return; $options = get_option('ahmaipsu_settings'); $char_count = $options['ahmaipsu_char_count'] ?? 200; $content_type = get_post_meta($post_id, '_ahmaipsu_content_type', true) ?: 'summary'; // Manual metabox regenerate always hits the API. // Auto save_post skips when body + model/provider/char count/content type/language match the last success. // Empty summary always generates. Failed generate does not update the fingerprint. if (!$should_regenerate) { if (!empty($existing_summary)) { $current_context = ahmaipsu_generate_context($post, $content_type); $stored_context = ahmaipsu_get_generate_fingerprint($post_id); if (empty($stored_context)) { // Existing summary from before this feature: seed, do not burn an API call. ahmaipsu_store_generate_fingerprint($post_id, $current_context); return; } if (ahmaipsu_generate_context_matches($stored_context, $current_context)) { return; } } } $summary = ahmaipsu_API_Handler::generate_summary($post->post_content, $char_count, $content_type); // Save the summary if generation was successful if (!is_wp_error($summary)) { update_post_meta($post_id, '_ahmaipsu_content', $summary); ahmaipsu_sync_destinations($post_id, $summary, (bool) $should_regenerate); ahmaipsu_store_generate_fingerprint($post_id, ahmaipsu_generate_context($post, $content_type)); // Clear the regeneration flag delete_post_meta($post_id, '_ahmaipsu_regenerate_flag'); // Add admin notice for successful generation add_action('admin_notices', function() use ($should_regenerate) { $message = $should_regenerate ? 'Post summary regenerated successfully!' : 'Post summary generated successfully!'; echo 'AHMAI Post Summary: ' . esc_html($message) . '
'; echo 'AHM AI Post Summary Error: ' . esc_html($summary->get_error_message()) . '
'; echo '