PluginProbe
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO / 1.0.0
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO v1.0.0
2.7.0 2.6.0 2.5.0 2.4.0 2.3.0 2.2.0 2.1.1 2.1.0 2.0.2 2.0.1 2.0.0 1.32.0 1.31.0 1.30.0 1.29.0 1.28.0 1.27.0 1.26.0 1.25.0 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.10.0 All 48 releases
thinkrank / includes / core / class-activator.php

class-activator.php in ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO 1.0.0, at includes/core/class-activator.php

168 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Activator Class
4 *
5 * Handles plugin activation tasks
6 *
7 * @package ThinkRank\Core
8 * @since 1.0.0
9 */
10
11 declare(strict_types=1);
12
13 namespace ThinkRank\Core;
14
15 use ThinkRank\Database\Database_Schema;
16
17 // Prevent direct access
18 if (!defined('ABSPATH')) {
19 exit;
20 }
21
22 /**
23 * Activator Class
24 *
25 * Single Responsibility: Handle plugin activation tasks only
26 *
27 * @since 1.0.0
28 */
29 class Activator {
30
31 /**
32 * Plugin activation tasks
33 *
34 * @return void
35 * @throws \Exception If activation fails
36 */
37 public function activate(): void {
38 $this->check_requirements();
39 $this->create_database_tables();
40 $this->set_default_options();
41 $this->schedule_cron_jobs();
42 $this->set_activation_flag();
43 }
44
45 /**
46 * Check system requirements
47 *
48 * @return void
49 * @throws \Exception If requirements not met
50 */
51 private function check_requirements(): void {
52 // PHP version check
53 if (version_compare(PHP_VERSION, '8.0', '<')) {
54 throw new \Exception('ThinkRank requires PHP 8.0 or higher');
55 }
56
57 // WordPress version check
58 if (version_compare(get_bloginfo('version'), '6.0', '<')) {
59 throw new \Exception('ThinkRank requires WordPress 6.0 or higher');
60 }
61
62 // Required PHP extensions
63 $required_extensions = ['curl', 'json', 'mbstring'];
64 foreach ($required_extensions as $extension) {
65 if (!extension_loaded($extension)) {
66 throw new \Exception(sprintf("Required PHP extension '%s' is not loaded", esc_html($extension)));
67 }
68 }
69
70 // Check if we can write to WordPress root directory (for robots.txt, llms.txt, sitemaps)
71 if (!wp_is_writable(ABSPATH)) {
72 throw new \Exception('WordPress root directory is not writable');
73 }
74 }
75
76 /**
77 * Create database tables
78 *
79 * Uses the consolidated Database_Schema class to create all 11 ThinkRank tables:
80 * - SEO Tables (7): Settings, Analysis, Keywords, Schema, Social, Performance, Local
81 * - AI/Core Tables (4): AI Cache, AI Usage, Content Briefs, SEO Scores
82 *
83 * @return void
84 * @throws \Exception If table creation fails
85 */
86 private function create_database_tables(): void {
87 try {
88 // Use the Database_Schema class to create all 11 ThinkRank tables
89 $schema = new Database_Schema();
90 $results = $schema->create_tables();
91
92 if (!$results['success']) {
93 $error_message = 'Failed to create database tables: ' . implode(', ', $results['errors']);
94 throw new \Exception($error_message);
95 }
96
97 // Add performance indexes for Phase 2 optimization
98 $index_results = $schema->add_performance_indexes();
99 if (!$index_results) {
100 // Performance indexes could not be created - activation continues
101 }
102
103 // Database tables created successfully
104
105 } catch (\Exception $e) {
106 throw new \Exception('Database table creation failed: ' . esc_html($e->getMessage()));
107 }
108 }
109
110
111
112 /**
113 * Set default plugin options
114 *
115 * @return void
116 */
117 private function set_default_options(): void {
118 $default_options = [
119 'thinkrank_version' => THINKRANK_VERSION,
120
121 'thinkrank_ai_provider' => 'openai',
122 'thinkrank_cache_duration' => 3600, // 1 hour
123 'thinkrank_max_requests_per_minute' => 10,
124 'thinkrank_enable_logging' => true,
125 'thinkrank_auto_optimize' => false,
126 'thinkrank_seo_score_threshold' => 70,
127 ];
128
129 foreach ($default_options as $option_name => $option_value) {
130 if (get_option($option_name) === false) {
131 add_option($option_name, $option_value);
132 }
133 }
134 }
135
136
137 /**
138 * Schedule cron jobs
139 *
140 * @return void
141 */
142 private function schedule_cron_jobs(): void {
143
144 // Schedule cache cleanup
145 if (!wp_next_scheduled('thinkrank_cache_cleanup')) {
146 wp_schedule_event(time(), 'daily', 'thinkrank_cache_cleanup');
147 }
148
149 // Schedule usage analytics
150 if (!wp_next_scheduled('thinkrank_usage_analytics')) {
151 wp_schedule_event(time(), 'weekly', 'thinkrank_usage_analytics');
152 }
153 }
154
155 /**
156 * Set activation flag for first-time setup
157 *
158 * @return void
159 */
160 private function set_activation_flag(): void {
161 update_option('thinkrank_activated', true);
162 update_option('thinkrank_activation_time', time());
163
164 // Set flag for showing welcome screen
165 update_option('thinkrank_show_welcome', true);
166 }
167 }
168