Add AI writing assistant plugin with local backend, brave search, and image generation support

- Implement local backend AI provider with Ollama integration
- Add Brave Search API integration for real-time search suggestions
- Add image generation manager with multiple AI providers
- Create hybrid provider system with local/cloud fallback
- Add comprehensive settings UI with provider management
- Implement Gutenberg sidebar with writing assistance controls
- Add SEO schema generation for AI-generated content
- Multiple provider support: OpenRouter, local backend, Codex
This commit is contained in:
Dwindi Ramadhana
2026-05-17 10:48:05 +07:00
parent 97426d5ab1
commit d2c10756ab
61 changed files with 18725 additions and 806 deletions

View File

@@ -36,6 +36,10 @@ define( 'WP_AGENTIC_WRITER_URL', untrailingslashit( plugin_dir_url( __FILE__ ) )
// Include autoloader.
require_once WP_AGENTIC_WRITER_DIR . 'includes/class-autoloader.php';
// Include provider interface and manager.
require_once WP_AGENTIC_WRITER_DIR . 'includes/interface-ai-provider.php';
require_once WP_AGENTIC_WRITER_DIR . 'includes/class-provider-manager.php';
// Initialize the plugin.
function wp_agentic_writer_init() {
// Load plugin text domain.
@@ -51,6 +55,14 @@ function wp_agentic_writer_init() {
// Always initialize cost tracker hooks (REST API calls need this).
WP_Agentic_Writer_Cost_Tracker::get_instance();
// Schedule image cleanup cron job if not already scheduled.
if ( ! wp_next_scheduled( 'wpaw_cleanup_temp_images' ) ) {
wp_schedule_event( time(), 'daily', 'wpaw_cleanup_temp_images' );
}
// Initialize SEO Schema Agent
WP_Agentic_Writer_SEO_Schema::get_instance();
// Check if we're on the admin side.
if ( is_admin() ) {
// Initialize settings - V2 is now the default.
@@ -94,6 +106,18 @@ function wp_agentic_writer_init() {
}
add_action( 'plugins_loaded', 'wp_agentic_writer_init' );
// Hook for image cleanup cron job.
add_action( 'wpaw_cleanup_temp_images', 'wp_agentic_writer_cleanup_temp_images' );
/**
* Cleanup old temp images (7+ days).
*
* @since 0.1.0
*/
function wp_agentic_writer_cleanup_temp_images() {
WP_Agentic_Writer_Image_Manager::get_instance()->cleanup_old_temp_images();
}
// Activation hook.
register_activation_hook( __FILE__, 'wp_agentic_writer_activate' );
@@ -113,6 +137,8 @@ function wp_agentic_writer_activate() {
'search_engine' => 'auto',
'search_depth' => 'medium',
'cost_tracking_enabled' => true,
'enable_clarification_quiz' => true,
'clarity_confidence_threshold' => '0.6',
'chat_history_limit' => 20,
'preferred_languages' => array( 'auto', 'English', 'Indonesian' ),
'custom_languages' => array(),
@@ -120,8 +146,31 @@ function wp_agentic_writer_activate() {
add_option( 'wp_agentic_writer_settings', $default_options );
// Set default custom models (separate option for custom models)
$default_custom_models = array(
array(
'id' => 'black-forest-labs/flux-1.1-pro',
'name' => 'FLUX 1.1 Pro',
'type' => 'image',
),
array(
'id' => 'black-forest-labs/flux-pro',
'name' => 'FLUX Pro',
'type' => 'image',
),
array(
'id' => 'recraft-ai/recraft-v3',
'name' => 'Recraft V3',
'type' => 'image',
),
);
add_option( 'wp_agentic_writer_custom_models', $default_custom_models );
// Create cost tracking table.
wp_agentic_writer_create_cost_table();
// Create image management tables.
WP_Agentic_Writer_Image_Manager::get_instance()->create_tables();
}
/**
@@ -152,6 +201,30 @@ function wp_agentic_writer_create_cost_table() {
dbDelta( $sql );
}
// Version-based table creation (runs on plugins_loaded to ensure tables exist).
add_action( 'plugins_loaded', 'wp_agentic_writer_maybe_create_tables' );
/**
* Create database tables if they don't exist or version is outdated.
*
* @since 0.1.0
*/
function wp_agentic_writer_maybe_create_tables() {
$current_version = get_option( 'wpaw_db_version', '0' );
$required_version = '1.1.0';
if ( version_compare( $current_version, $required_version, '<' ) ) {
// Create cost tracking table.
wp_agentic_writer_create_cost_table();
// Create image management tables.
WP_Agentic_Writer_Image_Manager::get_instance()->create_tables();
// Update version.
update_option( 'wpaw_db_version', $required_version );
}
}
// Deactivation hook.
register_deactivation_hook( __FILE__, 'wp_agentic_writer_deactivate' );
@@ -161,7 +234,11 @@ register_deactivation_hook( __FILE__, 'wp_agentic_writer_deactivate' );
* @since 0.1.0
*/
function wp_agentic_writer_deactivate() {
// Cleanup if needed.
// Clear scheduled cron jobs.
$timestamp = wp_next_scheduled( 'wpaw_cleanup_temp_images' );
if ( $timestamp ) {
wp_unschedule_event( $timestamp, 'wpaw_cleanup_temp_images' );
}
}
// Uninstall hook.
@@ -176,8 +253,25 @@ function wp_agentic_writer_uninstall() {
// Delete options.
delete_option( 'wp_agentic_writer_settings' );
// Delete cost tracking table.
// Delete tables.
global $wpdb;
$table_name = $wpdb->prefix . 'wpaw_cost_tracking';
$wpdb->query( "DROP TABLE IF EXISTS $table_name" );
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}wpaw_cost_tracking" );
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}wpaw_images_variants" );
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}wpaw_images" );
// Delete temp image directory.
$upload_dir = wp_upload_dir();
$temp_dir = $upload_dir['basedir'] . '/wpaw';
if ( file_exists( $temp_dir ) ) {
// Recursively delete directory.
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator( $temp_dir, RecursiveDirectoryIterator::SKIP_DOTS ),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ( $files as $fileinfo ) {
$todo = ( $fileinfo->isDir() ? 'rmdir' : 'unlink' );
$todo( $fileinfo->getRealPath() );
}
rmdir( $temp_dir );
}
}