User: ---
File: /Users/matkirsch/Local Sites/divi5/app/public/wp-content/plugins/ai-excerpt-generator/ai-excerpt-generator.php

<?php
/*
Plugin Name: AI Excerpt Generator
Description: Automatically generates post excerpts using Gemini API based on post category
Version: 1.2
*/

// Add settings page
function aieg_add_settings_page() {
    add_options_page('AI Excerpt Generator Settings', 'AI Excerpt Generator', 'manage_options', 'ai-excerpt-generator', 'aieg_render_settings_page');
}
add_action('admin_menu', 'aieg_add_settings_page');

// Register settings
function aieg_register_settings() {
    register_setting('aieg_settings', 'aieg_api_key');
    register_setting('aieg_settings', 'aieg_prompt_reviews');
    register_setting('aieg_settings', 'aieg_prompt_interviews');
    register_setting('aieg_settings', 'aieg_prompt_premieres');
    register_setting('aieg_settings', 'aieg_prompt_default');
}
add_action('admin_init', 'aieg_register_settings');

// Render settings page
function aieg_render_settings_page() {
    ?>
    <div class="wrap">
        <h2>AI Excerpt Generator Settings</h2>
        <form method="post" action="options.php">
            <?php settings_fields('aieg_settings'); ?>
            <table class="form-table">
                <!-- ... (settings fields remain the same) ... -->
            </table>
            <?php submit_button(); ?>
        </form>
    </div>
    <?php
}

// Get prompt based on category
function aieg_get_prompt($content, $post_id) {
    $categories = get_the_category($post_id);
    $category_names = $categories ? array_column($categories, 'slug') : [];
    
    // Added null coalescing operator for fallback
    if (in_array('reviews', $category_names)) {
        $prompt = get_option('aieg_prompt_reviews');
    } elseif (in_array('interview', $category_names)) {
        $prompt = get_option('aieg_prompt_interviews');
    } elseif (in_array('premieres', $category_names)) {
        $prompt = get_option('aieg_prompt_premieres');
    } else {
        $prompt = get_option('aieg_prompt_default');
    }
    
    return $prompt ? $prompt . ' ' . wp_strip_all_tags($content) : '';
}

// Generate excerpt using Gemini API
function aieg_generate_excerpt($content, $post_id) {
    $api_key = get_option('aieg_api_key');
    if (empty($api_key)) return false;

    $url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent';
    
    $body = json_encode([
        'contents' => [[
            'parts' => [[
                'text' => aieg_get_prompt($content, $post_id)
            ]]
        ]]
    ]);

    $response = wp_remote_post($url . '?key=' . $api_key, [
        'headers' => ['Content-Type' => 'application/json'],
        'body' => $body,
        'timeout' => 15
    ]);

    if (is_wp_error($response)) {
        error_log('AI Excerpt Generator API Error: ' . $response->get_error_message());
        return false;
    }

    $body = json_decode(wp_remote_retrieve_body($response), true);
    
    // Improved error handling
    if (!isset($body['candidates'][0]['content']['parts'][0]['text'])) {
        error_log('AI Excerpt Generator Response Error: ' . print_r($body, true));
        return false;
    }

    return $body['candidates'][0]['content']['parts'][0]['text'];
}

// AJAX handler
function aieg_ajax_generate_excerpt() {
    check_ajax_referer('aieg_generate_excerpt', 'nonce');
    
    $post_id = intval($_POST['post_id']);
    $post = get_post($post_id);
    
    if (!$post) {
        wp_send_json_error(['message' => 'Invalid post ID']);
        return;
    }

    $excerpt = aieg_generate_excerpt($post->post_content, $post_id);
    
    if ($excerpt) {
        update_post_meta($post_id, '_ai_generated_excerpt', $excerpt);
        wp_update_post(['ID' => $post_id, 'post_excerpt' => $excerpt]);
        wp_send_json_success(['excerpt' => $excerpt]);
    } else {
        wp_send_json_error(['message' => 'Failed to generate excerpt. Check error logs.']);
    }
}
add_action('wp_ajax_aieg_generate_excerpt', 'aieg_ajax_generate_excerpt');

// Enqueue admin scripts
function aieg_enqueue_admin_scripts($hook) {
    if (!in_array($hook, ['post.php', 'post-new.php'])) return;
    
    wp_enqueue_script(
        'aieg-admin', 
        plugins_url('js/admin.js', __FILE__), 
        ['jquery', 'wp-data', 'wp-editor'], // Added required WordPress dependencies
        '1.1',
        true
    );
    
    wp_localize_script('aieg-admin', 'aieg', [
        'ajax_url' => admin_url('admin-ajax.php'),
        'nonce' => wp_create_nonce('aieg_generate_excerpt')
    ]);
}
add_action('admin_enqueue_scripts', 'aieg_enqueue_admin_scripts');

// Save excerpt on draft save
function aieg_save_post($post_id) {
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
    if (!current_user_can('edit_post', $post_id)) return;
    if (!in_array(get_post_status($post_id), ['draft', 'auto-draft'])) return;
    
    $post = get_post($post_id);
    $excerpt = aieg_generate_excerpt($post->post_content, $post_id);
    
    if ($excerpt) {
        update_post_meta($post_id, '_ai_generated_excerpt', $excerpt);
        wp_update_post(['ID' => $post_id, 'post_excerpt' => $excerpt]);
    }
}
add_action('save_post', 'aieg_save_post');

// Metabox functions
function aieg_add_metabox() {
    add_meta_box('ai_excerpt_metabox', 'AI Generated Excerpt', 'aieg_render_metabox', 'post', 'normal', 'high');
}
add_action('add_meta_boxes', 'aieg_add_metabox');

function aieg_render_metabox($post) {
    $excerpt = get_post_meta($post->ID, '_ai_generated_excerpt', true);
    ?>
    <div>
        <p>Current AI Generated Excerpt:</p>
        <div id="ai-excerpt-content" style="white-space: pre-wrap; border: 1px solid #ddd; padding: 10px; margin: 10px 0; min-height: 100px;">
            <?php echo esc_html($excerpt); ?>
        </div>
        <button type="button" class="button button-primary" id="generate-excerpt">Generate/Refresh Excerpt</button>
        <span id="excerpt-status" style="margin-left: 10px;"></span>
    </div>
    <?php
}
Assistant: [system: file uploaded]
User: ---
File: /Users/matkirsch/Local Sites/divi5/app/public/wp-content/plugins/ai-excerpt-generator/js/admin.js

jQuery(document).ready(function($) {
    $('#generate-excerpt').on('click', function(e) {
        e.preventDefault();
        const button = $(this);
        const status = $('#excerpt-status');
        const postId = jQuery('#post_ID').val();

        button.prop('disabled', true);
        status.text('Generating...').removeClass('error').addClass('updating-message');

        $.ajax({
            url: aieg.ajax_url,
            type: 'POST',
            dataType: 'text',
            data: {
                action: 'aieg_generate_excerpt',
                post_id: postId,
                nonce: aieg.nonce
            },
            success: function(response) {
                if (response.success) {
                    $('#ai-excerpt-content').text(response.data.excerpt);
                    status.text('Generated successfully!').removeClass('updating-message');
                } else {
                    status.text('Error: ' + response.data.message).addClass('error');
                }
            },
            error: function(xhr) {
                let errorMessage = 'Request failed';
                try {
                    const response = JSON.parse(xhr.responseText);
                    errorMessage = response.data.message || errorMessage;
                } catch(e) {}
                status.text('Error: ' + errorMessage).addClass('error');
            },
            complete: function() {
                button.prop('disabled', false);
                setTimeout(() => status.text('').removeClass('error updating-message'), 3000);
            }
        });
    });
});
Assistant: [system: file uploaded]
User: You are an expert in Wordpress development. 

I am creating a plugin to automatically create an AI generated excerpt for my posts. These are my files. At the moment it gets stuck on "Generating...." after pressing the button. Check the code for possible errors and give the the full corrected code.
Assistant: