-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmartcontentai.php
More file actions
124 lines (100 loc) · 4.22 KB
/
smartcontentai.php
File metadata and controls
124 lines (100 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/**
* Plugin Name: SmartContent AI
* Description: Automatic creation of articles and images using Google Gemini API, SEO optimization and Schema markup.
* Version: 2.0.0
* Author: Stelios Theodoridis
* Author URI: https://texnologia.net
* License: MIT
* License URI: https://opensource.org/licenses/MIT
* Text Domain: smartcontentai
* Domain Path: /languages
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Global constants
define( 'SMARTCONTENTAI_VERSION', '2.0.0' );
define( 'SMARTCONTENTAI_DB_VERSION', '2.0' ); // Bumped DB version
define( 'SMARTCONTENTAI_TABLE_LOGS', 'smartcontentai_logs' );
define( 'SMARTCONTENTAI_PATH', plugin_dir_path( __FILE__ ) );
define( 'SMARTCONTENTAI_URL', plugin_dir_url( __FILE__ ) );
// Load Autoloader
require_once SMARTCONTENTAI_PATH . 'includes/Autoloader.php';
SmartContentAI\Autoloader::get_instance();
// Activation Hook
register_activation_hook( __FILE__, 'smartcontentai_activate' );
function smartcontentai_activate() {
global $wpdb;
// Create old logs table for backward compatibility
$table_name = $wpdb->prefix . SMARTCONTENTAI_TABLE_LOGS;
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
created_at datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
request_payload text NOT NULL,
request_hash varchar(64) DEFAULT NULL,
status varchar(50) NOT NULL,
response_excerpt text NOT NULL,
post_id mediumint(9) DEFAULT NULL,
PRIMARY KEY (id),
KEY request_hash (request_hash),
KEY status (status),
KEY created_at (created_at)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
// Create new custom provider tables
\SmartContentAI\Core\Database::init();
add_option( 'smartcontentai_db_version', SMARTCONTENTAI_DB_VERSION );
}
// Deactivation Hook
register_deactivation_hook( __FILE__, 'smartcontentai_deactivate' );
function smartcontentai_deactivate() {
$scheduler = new SmartContentAI\Core\Scheduler();
$scheduler->unschedule_events();
}
// Initialize Plugin
function smartcontentai_init() {
// Text Domain
load_plugin_textdomain( 'smartcontentai', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
// Initialize database for custom providers FIRST, before any classes that need it
\SmartContentAI\Core\Database::init();
// Initialize database instance immediately to create tables if needed
\SmartContentAI\Core\Database::get_instance();
// Instantiate Core Classes
$logger = new SmartContentAI\Utils\Logger();
$security = new SmartContentAI\Core\Security();
$scheduler = new SmartContentAI\Core\Scheduler();
$api_client = new SmartContentAI\API\Client();
$image_generator = new SmartContentAI\Generator\Image( $api_client );
$post_generator = new SmartContentAI\Generator\Post( $api_client, $image_generator, $logger );
// Process scheduled queue items.
add_action(
'smartcontentai_process_queued_topic',
static function ( $item ) use ( $post_generator, $logger ) {
if ( ! is_array( $item ) || empty( $item['topic'] ) ) {
return;
}
$topic = (string) ( $item['topic'] ?? '' );
$keyword = (string) ( $item['keyword'] ?? '' );
$result = $post_generator->generate_and_publish( $topic, $keyword, array( 'publish_mode' => 'publish' ) );
if ( is_wp_error( $result ) ) {
$logger->log( $topic, $result->get_error_message(), 'error' );
}
}
);
// Scheduler enablement.
if ( (bool) get_option( 'smartcontentai_scheduler_enabled', false ) ) {
$frequency = get_option( 'smartcontentai_schedule_frequency', 'daily' );
$scheduler->schedule_events( $frequency );
}
// Admin
if ( is_admin() ) {
new SmartContentAI\Admin\Admin( $post_generator, $logger, $security, $scheduler );
}
// Frontend Schema
$schema = new SmartContentAI\Frontend\Schema();
add_action( 'wp_head', array( $schema, 'insert_schema_json_ld' ) );
}
add_action( 'plugins_loaded', 'smartcontentai_init' );