cleanup_old_temp_images(); } // Activation hook. register_activation_hook( __FILE__, 'wp_agentic_writer_activate' ); /** * Plugin activation. * * @since 0.1.0 */ function wp_agentic_writer_activate() { // Set default options. $default_options = array( 'openrouter_api_key' => '', 'planning_model' => 'google/gemini-2.0-flash-exp', 'execution_model' => 'anthropic/claude-sonnet-4-20250514', 'image_model' => 'openai/gpt-4o', 'web_search_enabled' => false, '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(), ); 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(); } /** * Create cost tracking table. * * @since 0.1.0 */ function wp_agentic_writer_create_cost_table() { global $wpdb; $table_name = $wpdb->prefix . 'wpaw_cost_tracking'; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE $table_name ( id bigint(20) NOT NULL AUTO_INCREMENT, post_id bigint(20) NOT NULL, model varchar(255) NOT NULL, action varchar(50) NOT NULL, input_tokens int(11) NOT NULL, output_tokens int(11) NOT NULL, cost decimal(10,6) NOT NULL, created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL, PRIMARY KEY (id), KEY post_id (post_id) ) $charset_collate;"; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; 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' ); /** * Plugin deactivation. * * @since 0.1.0 */ function wp_agentic_writer_deactivate() { // Clear scheduled cron jobs. $timestamp = wp_next_scheduled( 'wpaw_cleanup_temp_images' ); if ( $timestamp ) { wp_unschedule_event( $timestamp, 'wpaw_cleanup_temp_images' ); } } // Uninstall hook. register_uninstall_hook( __FILE__, 'wp_agentic_writer_uninstall' ); /** * Plugin uninstall. * * @since 0.1.0 */ function wp_agentic_writer_uninstall() { // Delete options. delete_option( 'wp_agentic_writer_settings' ); // Delete tables. global $wpdb; $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 ); } }