init_hooks();
$this->load_dependencies();
}
/**
* Initialize hooks
*/
private function init_hooks() {
add_action('init', array($this, 'init'));
register_activation_hook(__FILE__, array($this, 'activate'));
register_deactivation_hook(__FILE__, array($this, 'deactivate'));
}
/**
* Load plugin dependencies
*/
private function load_dependencies() {
// Load required files
require_once AIAPG_PLUGIN_DIR . 'includes/class-utils.php';
require_once AIAPG_PLUGIN_DIR . 'includes/class-admin-settings.php';
require_once AIAPG_PLUGIN_DIR . 'includes/class-schedule-manager.php';
require_once AIAPG_PLUGIN_DIR . 'includes/class-post-generator.php';
require_once AIAPG_PLUGIN_DIR . 'includes/class-image-generator.php';
require_once AIAPG_PLUGIN_DIR . 'includes/class-scheduler.php';
}
/**
* Initialize plugin
*/
public function init() {
// Initialize components
$this->admin_settings = new AIAPG_Admin_Settings();
$this->schedule_manager = new AIAPG_Schedule_Manager();
$this->post_generator = new AIAPG_Post_Generator();
$this->image_generator = new AIAPG_Image_Generator();
$this->scheduler = new AIAPG_Scheduler();
// Register shortcodes
add_shortcode('aiapg_image', array($this, 'handle_image_shortcode'));
}
/**
* Handle image shortcode
*
* @param array $atts
* @return string
*/
public function handle_image_shortcode($atts) {
// This shortcode is primarily for AI processing during post generation
// If it appears in the frontend, we'll show a placeholder or nothing
$atts = shortcode_atts(array(
'prompt' => '',
), $atts);
// In frontend, return empty string as images should already be processed
if (!is_admin()) {
return '';
}
// In admin/editor, show placeholder
return sprintf(
'
AI Image Placeholder
Prompt: %s
This will be replaced with a generated image when the post is published.
',
esc_html($atts['prompt'])
);
}
/**
* Plugin activation
*/
public function activate() {
// Create database tables
$this->create_tables();
// Set default options
$this->set_default_options();
// Check for version updates
$this->check_version_updates();
// Flush rewrite rules
flush_rewrite_rules();
}
/**
* Plugin deactivation
*/
public function deactivate() {
// Clear scheduled events
wp_clear_scheduled_hook('aiapg_schedule_run');
// Flush rewrite rules
flush_rewrite_rules();
}
/**
* Check for version updates and run migrations
*/
private function check_version_updates() {
$current_version = get_option('aiapg_version', '1.0.0');
$plugin_version = AIAPG_VERSION;
if (version_compare($current_version, $plugin_version, '<')) {
// Run version-specific updates
$this->run_version_updates($current_version, $plugin_version);
// Update stored version
update_option('aiapg_version', $plugin_version);
}
}
/**
* Run version-specific updates
*/
private function run_version_updates($from_version, $to_version) {
// Update from 1.0.0 to 1.1.0
if (version_compare($from_version, '1.1.0', '<')) {
$this->update_to_1_1_0();
}
// Update from 1.1.0 to 1.1.1
if (version_compare($from_version, '1.1.1', '<')) {
$this->update_to_1_1_1();
}
}
/**
* Update to version 1.1.0
*/
private function update_to_1_1_0() {
global $wpdb;
// Migrate existing custom prompts from old format to new format
$this->migrate_custom_prompts_to_new_format();
// Update any other data structures if needed
$this->update_schedule_data_structure();
}
/**
* Migrate custom prompts from old string format to new object format
*/
private function migrate_custom_prompts_to_new_format() {
global $wpdb;
$table_name = $wpdb->prefix . 'aiapg_schedules';
// Get all schedules with custom prompts
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Migration requires direct database access without caching
$schedules = $wpdb->get_results(
$wpdb->prepare(
"SELECT id, custom_prompts FROM %s WHERE custom_prompts IS NOT NULL AND custom_prompts != ''",
$table_name
)
);
if (empty($schedules)) {
return;
}
$updated_count = 0;
foreach ($schedules as $schedule) {
$custom_prompts = maybe_unserialize($schedule->custom_prompts);
// Skip if already in new format or empty
if (empty($custom_prompts) || !is_array($custom_prompts)) {
continue;
}
// Check if already in new format (has objects with 'text' key)
$is_new_format = false;
foreach ($custom_prompts as $prompt) {
if (is_array($prompt) && isset($prompt['text'])) {
$is_new_format = true;
break;
}
}
// Skip if already in new format
if ($is_new_format) {
continue;
}
// Convert old format (array of strings) to new format (array of objects)
$new_custom_prompts = array();
foreach ($custom_prompts as $prompt_text) {
if (is_string($prompt_text) && !empty(trim($prompt_text))) {
$new_custom_prompts[] = array(
'text' => $prompt_text,
'categories' => array() // Empty categories for migrated prompts
);
}
}
// Update the schedule with new format
if (!empty($new_custom_prompts)) {
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Migration requires direct database access without caching
$result = $wpdb->update(
$table_name,
array('custom_prompts' => maybe_serialize($new_custom_prompts)),
array('id' => $schedule->id),
array('%s'),
array('%d')
);
if ($result !== false) {
$updated_count++;
}
}
}
}
/**
* Update schedule data structure for version 1.1.0
*/
private function update_schedule_data_structure() {
// Add any additional data structure updates here
// For now, the migration is handled by the custom prompts migration above
// Future: Add any other structural changes needed for 1.1.0
// For example: new database columns, new options, etc.
}
/**
* Update to version 1.1.1
*/
private function update_to_1_1_1() {
// Version 1.1.1 is a minor update with bug fixes and improvements
// No database migrations needed for this version
}
/**
* Create database tables
*/
private function create_tables() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
// Schedules table
$table_schedules = $wpdb->prefix . 'aiapg_schedules';
$sql_schedules = "CREATE TABLE $table_schedules (
id mediumint(9) NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
description text,
categories longtext,
custom_prompts longtext,
posts_per_run int(11) DEFAULT 1,
text_model varchar(100) DEFAULT 'gpt-3.5-turbo',
image_model varchar(100) DEFAULT 'dall-e-2',
fallback_image_model varchar(100) DEFAULT 'pollinations',
enable_images tinyint(1) DEFAULT 1,
image_placement varchar(50) DEFAULT 'featured',
image_size varchar(20) DEFAULT '1024x1024',
images_per_post int(11) DEFAULT 1,
interval_type varchar(20) DEFAULT 'daily',
interval_value int(11) DEFAULT 1,
custom_cron varchar(255),
is_active tinyint(1) DEFAULT 1,
last_run datetime DEFAULT NULL,
next_run datetime DEFAULT NULL,
created_at datetime DEFAULT CURRENT_TIMESTAMP,
updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) $charset_collate;";
// Logs table
$table_logs = $wpdb->prefix . 'aiapg_logs';
$sql_logs = "CREATE TABLE $table_logs (
id mediumint(9) NOT NULL AUTO_INCREMENT,
schedule_id mediumint(9),
run_time datetime DEFAULT CURRENT_TIMESTAMP,
posts_created int(11) DEFAULT 0,
text_model varchar(100),
image_model varchar(100),
status varchar(20) DEFAULT 'success',
message text,
details longtext,
PRIMARY KEY (id),
KEY schedule_id (schedule_id),
KEY run_time (run_time)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql_schedules);
dbDelta($sql_logs);
}
/**
* Set default options
*/
private function set_default_options() {
$default_options = array(
'openai_api_key' => '',
'gemini_api_key' => '',
'default_text_model' => 'gpt-3.5-turbo',
'default_image_model' => 'dall-e-2',
'default_fallback_image_model' => 'pollinations',
'setup_completed' => true,
'version' => AIAPG_VERSION
);
foreach ($default_options as $key => $value) {
if (get_option('aiapg_' . $key) === false) {
add_option('aiapg_' . $key, $value);
}
}
}
/**
* Check if setup is complete - always true since no wizard
*
* @return bool
*/
public function is_setup_complete() {
return true;
}
/**
* Get plugin URL
*
* @param string $path
* @return string
*/
public function get_url($path = '') {
return AIAPG_PLUGIN_URL . $path;
}
/**
* Get plugin path
*
* @param string $path
* @return string
*/
public function get_path($path = '') {
return AIAPG_PLUGIN_DIR . $path;
}
}
/**
* Get plugin instance
*
* @return AI_Auto_Post_Generator
*/
function aiapg() {
return AI_Auto_Post_Generator::get_instance();
}
// Initialize plugin
aiapg();