| 1 |
<?php |
| 2 |
/** |
| 3 |
* The main plugin file. |
| 4 |
* |
| 5 |
* @package WordPress\AI |
| 6 |
* |
| 7 |
* @since 0.8.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
declare( strict_types=1 ); |
| 11 |
|
| 12 |
namespace WordPress\AI; |
| 13 |
|
| 14 |
use WordPress\AI\Admin\Activation; |
| 15 |
use WordPress\AI\Admin\Dashboard\Dashboard_Widgets; |
| 16 |
use WordPress\AI\Admin\Deactivation; |
| 17 |
use WordPress\AI\Admin\Site_Health; |
| 18 |
use WordPress\AI\Admin\Upgrades; |
| 19 |
use WordPress\AI\Experiments\Experiments; |
| 20 |
use WordPress\AI\Features\Loader; |
| 21 |
use WordPress\AI\Features\Registry; |
| 22 |
use WordPress\AI\Settings\Settings_Page; |
| 23 |
use WordPress\AI\Settings\Settings_Registration; |
| 24 |
|
| 25 |
// Exit if accessed directly. |
| 26 |
defined( 'ABSPATH' ) || exit; |
| 27 |
|
| 28 |
/** |
| 29 |
* Class - Main |
| 30 |
* |
| 31 |
* @internal This class should not be used outside the plugin and there is no guarantee of backwards compatibility. |
| 32 |
* |
| 33 |
* @since 0.8.0 |
| 34 |
*/ |
| 35 |
final class Main { |
| 36 |
/** |
| 37 |
* Instance of the class. |
| 38 |
* @since 0.8.0 |
| 39 |
* |
| 40 |
* |
| 41 |
* @var ?static |
| 42 |
*/ |
| 43 |
private static $instance; |
| 44 |
|
| 45 |
/** |
| 46 |
* Gets the (singleton) instance of the Main class. |
| 47 |
* |
| 48 |
* @since 0.8.0 |
| 49 |
*/ |
| 50 |
public static function get_instance(): self { |
| 51 |
if ( ! isset( self::$instance ) ) { |
| 52 |
self::$instance = new self(); |
| 53 |
self::$instance->setup(); |
| 54 |
} |
| 55 |
|
| 56 |
return self::$instance; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Setup the plugin. |
| 61 |
*/ |
| 62 |
private function setup(): void { |
| 63 |
// Load the plugin classes. |
| 64 |
add_action( 'plugins_loaded', array( $this, 'load' ) ); |
| 65 |
|
| 66 |
// Register activation and deactivation hooks. |
| 67 |
register_activation_hook( WPAI_PLUGIN_FILE, array( Activation::class, 'activation_callback' ) ); |
| 68 |
register_deactivation_hook( WPAI_PLUGIN_FILE, array( Deactivation::class, 'deactivation_callback' ) ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Load the plugin classes. |
| 73 |
* |
| 74 |
* @since 0.8.0 |
| 75 |
* |
| 76 |
* @internal Used in the plugins_loaded action. |
| 77 |
*/ |
| 78 |
public function load(): void { |
| 79 |
// Check plugin requirements before continuing. |
| 80 |
if ( ! ( new Requirements() )->are_requirements_met() ) { |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
// Include globals |
| 85 |
require_once WPAI_PLUGIN_DIR . 'includes/helpers.php'; |
| 86 |
|
| 87 |
// Handle any pending upgrades. |
| 88 |
( new Upgrades() )->init(); |
| 89 |
|
| 90 |
// Handle deprecated code. |
| 91 |
( new Deprecated() )->init(); |
| 92 |
|
| 93 |
// Add plugin action links to plugins screen. |
| 94 |
add_filter( 'plugin_action_links_' . plugin_basename( WPAI_PLUGIN_FILE ), array( $this, 'plugin_action_links' ) ); |
| 95 |
|
| 96 |
// Defer feature initialization to the 'init' action. |
| 97 |
add_action( 'init', array( $this, 'initialize_features' ), 15 ); |
| 98 |
|
| 99 |
// Register provider data globally so it is available to any plugin script. |
| 100 |
add_action( 'init', array( $this, 'register_provider_data' ), 20 ); |
| 101 |
|
| 102 |
// Register the default ability category. |
| 103 |
add_action( 'wp_abilities_api_categories_init', array( $this, 'register_ability_category' ) ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Initializes plugin features. |
| 108 |
* |
| 109 |
* @since 0.8.0 |
| 110 |
*/ |
| 111 |
public function initialize_features(): void { |
| 112 |
try { |
| 113 |
// Experiments are hooked into our Loader, so we need to register them first. |
| 114 |
( new Experiments() )->init(); |
| 115 |
|
| 116 |
// The one true registry of all features. |
| 117 |
$registry = new Registry(); |
| 118 |
|
| 119 |
// Initializes all the features. |
| 120 |
( new Loader( $registry ) )->init(); |
| 121 |
|
| 122 |
// Initialize settings registration. |
| 123 |
( new Settings_Registration( $registry ) )->init(); |
| 124 |
|
| 125 |
// Register admin settings page menu item and dashboard widgets. |
| 126 |
if ( is_admin() ) { |
| 127 |
Settings_Page::init( $registry ); |
| 128 |
|
| 129 |
( new Dashboard_Widgets( $registry ) )->init(); |
| 130 |
} |
| 131 |
|
| 132 |
// Register Site Health integration. The `debug_information` and |
| 133 |
// `site_status_tests` filters are only ever consumed from admin |
| 134 |
// screens, the Site Health REST endpoints (which power the async |
| 135 |
// status checks in wp-admin/site-health.php), and the weekly |
| 136 |
// WP-Cron health-check email — never on the public front end. |
| 137 |
if ( is_admin() || wp_doing_cron() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { |
| 138 |
( new Site_Health() )->init(); |
| 139 |
} |
| 140 |
} catch ( \Throwable $e ) { |
| 141 |
_doing_it_wrong( |
| 142 |
__METHOD__, |
| 143 |
sprintf( |
| 144 |
/* translators: %s: Error message. */ |
| 145 |
esc_html__( 'AI Plugin initialization failed: %s', 'ai' ), |
| 146 |
esc_html( $e->getMessage() ) |
| 147 |
), |
| 148 |
'0.8.0' |
| 149 |
); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Adds action links to the plugin list table. |
| 155 |
* |
| 156 |
* This adds "Settings" and "Connectors" links to |
| 157 |
* the plugin's action links on the Plugins page. |
| 158 |
* |
| 159 |
* @since 0.8.0 |
| 160 |
* |
| 161 |
* @param array<string> $links Existing action links. |
| 162 |
* @return array<string> Modified action links. |
| 163 |
*/ |
| 164 |
public function plugin_action_links( array $links ): array { |
| 165 |
$settings_link = sprintf( |
| 166 |
'<a href="%1$s">%2$s</a>', |
| 167 |
admin_url( 'options-general.php?page=ai-wp-admin' ), |
| 168 |
esc_html__( 'Settings', 'ai' ) |
| 169 |
); |
| 170 |
|
| 171 |
$connectors_link = sprintf( |
| 172 |
'<a href="%1$s">%2$s</a>', |
| 173 |
admin_url( 'options-connectors.php' ), |
| 174 |
esc_html__( 'Connectors', 'ai' ) |
| 175 |
); |
| 176 |
|
| 177 |
array_unshift( $links, $connectors_link, $settings_link ); |
| 178 |
|
| 179 |
return $links; |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Registers provider availability data for frontend scripts. |
| 184 |
* |
| 185 |
* @since 1.0.0 |
| 186 |
*/ |
| 187 |
public function register_provider_data(): void { |
| 188 |
Asset_Loader::add_global_data( 'ProviderData', get_provider_availability_data() ); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Register a generic catch-all category that all Abilities we register can use. |
| 193 |
* |
| 194 |
* This can be re-evaluated in the future if we need/want more specific categories. |
| 195 |
* |
| 196 |
* @internal Used in the wp_abilities_api_categories_init action. |
| 197 |
* |
| 198 |
* @since 0.8.0 |
| 199 |
*/ |
| 200 |
public function register_ability_category(): void { |
| 201 |
wp_register_ability_category( |
| 202 |
WPAI_DEFAULT_ABILITY_CATEGORY, |
| 203 |
array( |
| 204 |
'label' => __( 'AI', 'ai' ), |
| 205 |
'description' => __( 'Various AI features and experiments.', 'ai' ), |
| 206 |
), |
| 207 |
); |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Prevent the class from being cloned. |
| 212 |
* |
| 213 |
* @since 0.8.0 |
| 214 |
*/ |
| 215 |
public function __clone() { |
| 216 |
_doing_it_wrong( |
| 217 |
__FUNCTION__, |
| 218 |
sprintf( |
| 219 |
// translators: %s: Class name. |
| 220 |
esc_html__( 'The %s class should not be cloned.', 'ai' ), |
| 221 |
esc_html( self::class ), |
| 222 |
), |
| 223 |
'0.8.0' |
| 224 |
); |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Prevent the class from being deserialized. |
| 229 |
* |
| 230 |
* @since 0.8.0 |
| 231 |
*/ |
| 232 |
public function __wakeup() { |
| 233 |
_doing_it_wrong( |
| 234 |
__FUNCTION__, |
| 235 |
sprintf( |
| 236 |
// translators: %s: Class name. |
| 237 |
esc_html__( 'De-serializing instances of %s is not allowed.', 'ai' ), |
| 238 |
esc_html( self::class ), |
| 239 |
), |
| 240 |
'0.8.0' |
| 241 |
); |
| 242 |
} |
| 243 |
} |
| 244 |
|