Abilities
7 months ago
Cli
7 months ago
Core
7 months ago
Domain
7 months ago
Handlers
7 months ago
Infrastructure
7 months ago
Servers
7 months ago
Transport
7 months ago
Autoloader.php
9 months ago
Plugin.php
9 months ago
Plugin.php
117 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 | * {@inheritDoc} |
| 29 | */ |
| 30 | public static function instance(): self { |
| 31 | if ( ! isset( self::$instance ) ) { |
| 32 | self::$instance = new self(); |
| 33 | self::$instance->setup(); |
| 34 | |
| 35 | /** |
| 36 | * Fires after the main plugin class has been initialized. |
| 37 | * |
| 38 | * @param self $instance The main plugin class instance. |
| 39 | */ |
| 40 | do_action( 'wp_mcp_init', self::$instance ); |
| 41 | } |
| 42 | |
| 43 | return self::$instance; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Setup the plugin. |
| 48 | */ |
| 49 | private function setup(): void { |
| 50 | // Bail if dependencies are not met. |
| 51 | if ( ! $this->has_dependencies() ) { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | McpAdapter::instance(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Check if all required dependencies are available. |
| 60 | * |
| 61 | * Will log an admin notice if dependencies are missing. |
| 62 | * |
| 63 | * @return bool True if all dependencies are met, false otherwise. |
| 64 | */ |
| 65 | private function has_dependencies(): bool { |
| 66 | // Check if Abilities API is available. |
| 67 | if ( ! function_exists( 'wp_register_ability' ) ) { |
| 68 | add_action( |
| 69 | 'admin_notices', |
| 70 | static function () { |
| 71 | wp_admin_notice( |
| 72 | __( 'Abilities API not available (wp_register_ability function not found)', 'mcp-adapter' ), |
| 73 | array( |
| 74 | 'type' => 'error', |
| 75 | 'dismiss' => false, |
| 76 | ), |
| 77 | ); |
| 78 | } |
| 79 | ); |
| 80 | |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Prevent the class from being cloned. |
| 89 | */ |
| 90 | public function __clone() { |
| 91 | _doing_it_wrong( |
| 92 | __FUNCTION__, |
| 93 | sprintf( |
| 94 | // translators: %s: Class name. |
| 95 | esc_html__( 'The %s class should not be cloned.', 'mcp-adapter' ), |
| 96 | esc_html( self::class ), |
| 97 | ), |
| 98 | '0.1.0' |
| 99 | ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Prevent the class from being deserialized. |
| 104 | */ |
| 105 | public function __wakeup() { |
| 106 | _doing_it_wrong( |
| 107 | __FUNCTION__, |
| 108 | sprintf( |
| 109 | // translators: %s: Class name. |
| 110 | esc_html__( 'De-serializing instances of %s is not allowed.', 'mcp-adapter' ), |
| 111 | esc_html( self::class ), |
| 112 | ), |
| 113 | '0.1.0' |
| 114 | ); |
| 115 | } |
| 116 | } |
| 117 |