PluginProbe
AI / 0.2.0
AI v0.2.0
1.3.0 1.2.0 1.1.0 1.0.2 1.0.1 1.0.0 0.9.0 trunk 0.1.1 0.2.0 0.2.1 0.3.0 0.3.1 0.4.0 0.4.1 0.5.0 0.6.0 0.7.0 0.8.0
ai / includes / bootstrap.php

bootstrap.php in AI 0.2.0, at includes/bootstrap.php

257 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Bootstrap file for the AI Experiments plugin.
4 *
5 * Handles plugin initialization, version checks, and feature loading.
6 *
7 * @package WordPress\AI
8 */
9
10 declare( strict_types=1 );
11
12 namespace WordPress\AI;
13
14 use WordPress\AI\Abilities\Utilities\Posts;
15 use WordPress\AI\Settings\Settings_Page;
16 use WordPress\AI\Settings\Settings_Registration;
17 use WordPress\AI_Client\AI_Client;
18
19 // Exit if accessed directly.
20 if ( ! defined( 'ABSPATH' ) ) {
21 exit;
22 }
23
24 // Define plugin constants.
25 if ( ! defined( 'AI_EXPERIMENTS_VERSION' ) ) {
26 define( 'AI_EXPERIMENTS_VERSION', '0.2.0' );
27 }
28 if ( ! defined( 'AI_EXPERIMENTS_PLUGIN_FILE' ) ) {
29 define( 'AI_EXPERIMENTS_PLUGIN_FILE', defined( 'AI_EXPERIMENTS_DIR' ) ? AI_EXPERIMENTS_DIR . 'ai.php' : '' );
30 }
31 if ( ! defined( 'AI_EXPERIMENTS_PLUGIN_DIR' ) ) {
32 define( 'AI_EXPERIMENTS_PLUGIN_DIR', defined( 'AI_EXPERIMENTS_DIR' ) ? AI_EXPERIMENTS_DIR : '' );
33 }
34 if ( ! defined( 'AI_EXPERIMENTS_PLUGIN_URL' ) ) {
35 define( 'AI_EXPERIMENTS_PLUGIN_URL', plugin_dir_url( AI_EXPERIMENTS_PLUGIN_FILE ) );
36 }
37 if ( ! defined( 'AI_EXPERIMENTS_MIN_PHP_VERSION' ) ) {
38 define( 'AI_EXPERIMENTS_MIN_PHP_VERSION', '7.4' );
39 }
40 if ( ! defined( 'AI_EXPERIMENTS_MIN_WP_VERSION' ) ) {
41 define( 'AI_EXPERIMENTS_MIN_WP_VERSION', '6.9' );
42 }
43 if ( ! defined( 'AI_EXPERIMENTS_DEFAULT_ABILITY_CATEGORY' ) ) {
44 define( 'AI_EXPERIMENTS_DEFAULT_ABILITY_CATEGORY', 'ai-experiments' );
45 }
46
47 /**
48 * Displays an admin notice for version requirement failures.
49 *
50 * @since 0.1.0
51 *
52 * @param string $message The error message to display.
53 */
54 function version_notice( string $message ): void {
55 if ( ! is_admin() ) {
56 return;
57 }
58 ?>
59 <div class="notice notice-error">
60 <p><?php echo esc_html( $message ); ?></p>
61 </div>
62 <?php
63 }
64
65 /**
66 * Checks if the PHP version meets the minimum requirement.
67 *
68 * @since 0.1.0
69 *
70 * @return bool True if PHP version is sufficient, false otherwise.
71 */
72 function check_php_version(): bool {
73 if ( version_compare( phpversion(), AI_EXPERIMENTS_MIN_PHP_VERSION, '<' ) ) {
74 add_action(
75 'admin_notices',
76 static function () {
77 version_notice(
78 sprintf(
79 /* translators: 1: Required PHP version, 2: Current PHP version */
80 __( 'AI Experiments plugin requires PHP version %1$s or higher. You are running PHP version %2$s.', 'ai' ),
81 AI_EXPERIMENTS_MIN_PHP_VERSION,
82 PHP_VERSION
83 )
84 );
85 }
86 );
87 return false;
88 }
89 return true;
90 }
91
92 /**
93 * Checks if the WordPress version meets the minimum requirement.
94 *
95 * @since 0.1.0
96 *
97 * @return bool True if WordPress version is sufficient, false otherwise.
98 */
99 function check_wp_version(): bool {
100 if ( ! is_wp_version_compatible( AI_EXPERIMENTS_MIN_WP_VERSION ) ) {
101 add_action(
102 'admin_notices',
103 static function () {
104 global $wp_version;
105 version_notice(
106 sprintf(
107 /* translators: 1: Required WordPress version, 2: Current WordPress version */
108 __( 'AI Experiments plugin requires WordPress version %1$s or higher. You are running WordPress version %2$s.', 'ai' ),
109 AI_EXPERIMENTS_MIN_WP_VERSION,
110 $wp_version
111 )
112 );
113 }
114 );
115 return false;
116 }
117 return true;
118 }
119
120 /**
121 * Displays admin notice about missing Composer autoload files.
122 *
123 * @since 0.1.0
124 */
125 function display_composer_notice(): void {
126 ?>
127 <div class="notice notice-error">
128 <p>
129 <?php
130 printf(
131 /* translators: %s: composer install command */
132 esc_html__( 'Your installation of the AI Experiments plugin is incomplete. Please run %s.', 'ai' ),
133 '<code>composer install</code>'
134 );
135 ?>
136 </p>
137 </div>
138 <?php
139 }
140
141 /**
142 * Adds action links to the plugin list table.
143 *
144 * This adds a "Settings" link to the plugin's action links on the Plugins page.
145 *
146 * @since 0.1.1
147 *
148 * @param array<string> $links Existing action links.
149 * @return array<string> Modified action links.
150 */
151 function plugin_action_links( array $links ): array {
152 $settings_link = sprintf(
153 '<a href="%1$s">%2$s</a>',
154 admin_url( 'options-general.php?page=ai-experiments' ),
155 esc_html__( 'Settings', 'ai' )
156 );
157
158 array_unshift( $links, $settings_link );
159
160 return $links;
161 }
162
163 /**
164 * Loads the plugin after checking requirements.
165 *
166 * @since 0.1.0
167 */
168 function load(): void {
169 static $loaded = false;
170
171 // Prevent loading twice.
172 if ( $loaded ) {
173 return;
174 }
175
176 // Check version requirements.
177 if ( ! check_php_version() || ! check_wp_version() ) {
178 return;
179 }
180
181 // Load the Jetpack autoloader.
182 if ( ! file_exists( AI_EXPERIMENTS_PLUGIN_DIR . 'vendor/autoload_packages.php' ) ) {
183 add_action( 'admin_notices', __NAMESPACE__ . '\display_composer_notice' );
184 return;
185 }
186 require_once AI_EXPERIMENTS_PLUGIN_DIR . 'vendor/autoload_packages.php';
187
188 $loaded = true;
189
190 // Add plugin action links.
191 add_filter( 'plugin_action_links_' . plugin_basename( AI_EXPERIMENTS_PLUGIN_FILE ), __NAMESPACE__ . '\plugin_action_links' );
192
193 // Hook experiment initialization to init.
194 add_action( 'init', __NAMESPACE__ . '\initialize_experiments' );
195 }
196
197 /**
198 * Initializes plugin experiments.
199 *
200 * @since 0.1.0
201 */
202 function initialize_experiments(): void {
203 try {
204 // Initialize the WP AI Client.
205 AI_Client::init();
206
207 $registry = new Experiment_Registry();
208 $loader = new Experiment_Loader( $registry );
209 $loader->register_default_experiments();
210 $loader->initialize_experiments();
211
212 // Initialize settings registration.
213 $settings_registration = new Settings_Registration( $registry );
214 $settings_registration->init();
215
216 // Initialize admin settings page.
217 if ( is_admin() ) {
218 $settings_page = new Settings_Page( $registry );
219 $settings_page->init();
220 }
221
222 // Register our post-related WordPress Abilities.
223 $post_abilities = new Posts();
224 $post_abilities->register();
225
226 add_action(
227 'wp_abilities_api_categories_init',
228 static function () {
229 /**
230 * Register a generic catch-all category that all
231 * Abilities we register can use. Can re-evaluate this
232 * in the future if we need/want more specific categories.
233 */
234 wp_register_ability_category(
235 AI_EXPERIMENTS_DEFAULT_ABILITY_CATEGORY,
236 array(
237 'label' => __( 'AI Experiments', 'ai' ),
238 'description' => __( 'Various AI experiments.', 'ai' ),
239 ),
240 );
241 }
242 );
243 } catch ( \Throwable $t ) {
244 _doing_it_wrong(
245 __NAMESPACE__ . '\initialize_experiments',
246 sprintf(
247 /* translators: %s: Error message. */
248 esc_html__( 'AI Plugin initialization failed: %s', 'ai' ),
249 esc_html( $t->getMessage() )
250 ),
251 '0.1.0'
252 );
253 }
254 }
255
256 add_action( 'plugins_loaded', __NAMESPACE__ . '\load' );
257