Abilities
2 months ago
Cli
2 months ago
Core
2 months ago
Domain
2 months ago
Handlers
2 months ago
Infrastructure
2 months ago
Servers
2 months ago
Transport
2 months ago
Autoloader.php
2 months ago
Plugin.php
2 months ago
Plugin.php
121 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The main plugin file. |
| 4 | * |
| 5 | * If we evolve from a canonical plugin into WordPress core, this file would be left behind. |
| 6 | * |
| 7 | * @package WP\MCP |
| 8 | */ |
| 9 | |
| 10 | declare( strict_types=1 ); |
| 11 | |
| 12 | namespace WP\MCP; |
| 13 | |
| 14 | use WP\MCP\Core\McpAdapter; |
| 15 | |
| 16 | /** |
| 17 | * Class - Plugin |
| 18 | */ |
| 19 | final class Plugin { |
| 20 | /** |
| 21 | * The one true plugin. |
| 22 | * |
| 23 | * @var static |
| 24 | */ |
| 25 | private static self $instance; |
| 26 | |
| 27 | /** |
| 28 | * Gets the singleton instance of the plugin. |
| 29 | * |
| 30 | * @return self The plugin instance. |
| 31 | */ |
| 32 | public static function instance(): self { |
| 33 | if ( ! isset( self::$instance ) ) { |
| 34 | self::$instance = new self(); |
| 35 | self::$instance->setup(); |
| 36 | |
| 37 | /** |
| 38 | * Fires after the main plugin class has been initialized. |
| 39 | * |
| 40 | * @since 0.1.0 |
| 41 | * |
| 42 | * @param self $instance The main plugin class instance. |
| 43 | */ |
| 44 | do_action( 'wp_mcp_init', self::$instance ); |
| 45 | } |
| 46 | |
| 47 | return self::$instance; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Sets up the plugin. |
| 52 | */ |
| 53 | private function setup(): void { |
| 54 | // Bail if dependencies are not met. |
| 55 | if ( ! $this->has_dependencies() ) { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | McpAdapter::instance(); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Checks if all required dependencies are available. |
| 64 | * |
| 65 | * Will log an admin notice if dependencies are missing. |
| 66 | * |
| 67 | * @return bool True if all dependencies are met, false otherwise. |
| 68 | */ |
| 69 | private function has_dependencies(): bool { |
| 70 | // Check if Abilities API is available. |
| 71 | if ( ! function_exists( 'wp_register_ability' ) ) { |
| 72 | add_action( |
| 73 | 'admin_notices', |
| 74 | static function () { |
| 75 | wp_admin_notice( |
| 76 | __( 'Abilities API not available (wp_register_ability function not found)', 'mcp-adapter' ), |
| 77 | array( |
| 78 | 'type' => 'error', |
| 79 | 'dismiss' => false, |
| 80 | ), |
| 81 | ); |
| 82 | } |
| 83 | ); |
| 84 | |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Prevents the class from being cloned. |
| 93 | */ |
| 94 | public function __clone() { |
| 95 | _doing_it_wrong( |
| 96 | __FUNCTION__, |
| 97 | sprintf( |
| 98 | // translators: %s: Class name. |
| 99 | esc_html__( 'The %s class should not be cloned.', 'mcp-adapter' ), |
| 100 | esc_html( self::class ), |
| 101 | ), |
| 102 | '0.1.0' |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Prevents the class from being deserialized. |
| 108 | */ |
| 109 | public function __wakeup() { |
| 110 | _doing_it_wrong( |
| 111 | __FUNCTION__, |
| 112 | sprintf( |
| 113 | // translators: %s: Class name. |
| 114 | esc_html__( 'De-serializing instances of %s is not allowed.', 'mcp-adapter' ), |
| 115 | esc_html( self::class ), |
| 116 | ), |
| 117 | '0.1.0' |
| 118 | ); |
| 119 | } |
| 120 | } |
| 121 |