| @@ -2,10 +2,11 @@ | ||
| 2 | 2 | /** |
| 3 | 3 | * Plugin Name: FlyWP |
| 4 | 4 | * Plugin URI: https://flywp.com |
| 5 | 5 | * Description: Helper plugin for FlyWP |
| 6 | - * Version: 0.1 | |
| 6 | + * Version: 0.3 | |
| 7 | 7 | * Author: FlyWP |
| 8 | + * Author URI: https://flywp.com/?utm_source=wporg&utm_medium=banner&utm_campaign=author-uri | |
| 8 | 9 | * License: GPL2 |
| 9 | 10 | */ |
| 10 | 11 | |
| 11 | 12 | // Exit if accessed directly. |
| @@ -14,62 +15,99 @@ | ||
| 14 | 15 | } |
| 15 | 16 | |
| 16 | 17 | require __DIR__ . '/vendor/autoload.php'; |
| 17 | 18 | |
| 19 | +use WeDevs\WpUtils\ContainerTrait; | |
| 20 | +use WeDevs\WpUtils\HookTrait; | |
| 21 | +use WeDevs\WpUtils\SingletonTrait; | |
| 22 | + | |
| 23 | +/** | |
| 24 | + * Main FlyWP Class. | |
| 25 | + * | |
| 26 | + * @var admin FlyWP\Admin | |
| 27 | + * @var rest FlyWP\Api | |
| 28 | + * @var fastcgi FlyWP\Fastcgi_Cache | |
| 29 | + * @var router FlyWP\Router | |
| 30 | + * | |
| 31 | + * @class FlyWP | |
| 32 | + */ | |
| 18 | 33 | final class FlyWP_Plugin { |
| 19 | 34 | |
| 35 | + use SingletonTrait; | |
| 36 | + use ContainerTrait; | |
| 37 | + use HookTrait; | |
| 38 | + | |
| 20 | 39 | /** |
| 21 | 40 | * Plugin version. |
| 22 | 41 | * |
| 23 | 42 | * @var string |
| 24 | 43 | */ |
| 25 | - private $version = '1.0.0'; | |
| 44 | + public $version = '0.3'; | |
| 26 | 45 | |
| 27 | 46 | /** |
| 28 | - * The single instance of the class. | |
| 47 | + * Plugin Constructor. | |
| 29 | 48 | * |
| 30 | - * @var FlyWP | |
| 49 | + * @return void | |
| 31 | 50 | */ |
| 32 | - private static $instance = null; | |
| 51 | + private function __construct() { | |
| 52 | + $this->define_constants(); | |
| 33 | 53 | |
| 54 | + $this->add_action( 'plugins_loaded', 'init_plugin' ); | |
| 55 | + register_activation_hook( __FILE__, [ $this, 'activate' ] ); | |
| 56 | + } | |
| 57 | + | |
| 34 | 58 | /** |
| 35 | - * Plugin Constructor. | |
| 59 | + * Define plugin constants. | |
| 36 | 60 | * |
| 37 | 61 | * @return void |
| 38 | 62 | */ |
| 39 | - private function __construct() { | |
| 40 | - $this->define_constants(); | |
| 63 | + private function define_constants() { | |
| 64 | + define( 'FLYWP_VERSION', $this->version ); | |
| 65 | + define( 'FLYWP_PLUGIN_FILE', __FILE__ ); | |
| 66 | + define( 'FLYWP_PLUGIN_BASENAME', plugin_basename( __FILE__ ) ); | |
| 67 | + define( 'FLYWP_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); | |
| 68 | + define( 'FLYWP_PLUGIN_URL', plugin_dir_url( __FILE__ ) ); | |
| 41 | 69 | |
| 42 | - add_action( 'plugins_loaded', [ $this, 'init_plugin' ] ); | |
| 70 | + if ( ! defined( 'FLYWP_API_KEY' ) ) { | |
| 71 | + define( 'FLYWP_API_KEY', '' ); | |
| 72 | + } | |
| 43 | 73 | } |
| 44 | 74 | |
| 45 | 75 | /** |
| 46 | - * Main FlyWP Instance. | |
| 76 | + * Plugin activation hook. | |
| 47 | 77 | * |
| 48 | - * Ensures only one instance of FlyWP is loaded or can be loaded. | |
| 49 | - * | |
| 50 | - * @return flyWP - Main instance | |
| 78 | + * @return void | |
| 51 | 79 | */ |
| 52 | - public static function instance() { | |
| 53 | - if ( null === self::$instance ) { | |
| 54 | - self::$instance = new self(); | |
| 55 | - } | |
| 80 | + public function activate() { | |
| 81 | + $router = new FlyWP\Router(); | |
| 82 | + $router->register_routes(); | |
| 56 | 83 | |
| 57 | - return self::$instance; | |
| 84 | + flush_rewrite_rules( false ); | |
| 58 | 85 | } |
| 59 | 86 | |
| 87 | + /** | |
| 88 | + * Initialize plugin. | |
| 89 | + * | |
| 90 | + * @return void | |
| 91 | + */ | |
| 60 | 92 | public function init_plugin() { |
| 61 | - if ( ! $this->has_api_key() ) { | |
| 62 | - add_action( 'admin_notices', [ $this, 'admin_notice' ] ); | |
| 93 | + if ( ! $this->has_key() ) { | |
| 94 | + $this->add_action( 'admin_notices', 'admin_notice' ); | |
| 63 | 95 | |
| 64 | 96 | return; |
| 65 | 97 | } |
| 66 | 98 | |
| 67 | 99 | if ( is_admin() ) { |
| 68 | - new FlyWP\Admin(); | |
| 100 | + $this->admin = new FlyWP\Admin(); | |
| 69 | 101 | } else { |
| 70 | - new FlyWP\Frontend(); | |
| 102 | + $this->frontend = new FlyWP\Frontend(); | |
| 71 | 103 | } |
| 104 | + | |
| 105 | + $this->router = new FlyWP\Router(); | |
| 106 | + $this->rest = new FlyWP\Api(); | |
| 107 | + $this->fastcgi = new FlyWP\Fastcgi_Cache(); | |
| 108 | + $this->opcache = new FlyWP\Opcache(); | |
| 109 | + $this->flyapi = new FlyWP\FlyApi(); | |
| 72 | 110 | } |
| 73 | 111 | |
| 74 | 112 | /** |
| 75 | 113 | * Show admin notice if API key is not set. |
| @@ -82,30 +120,13 @@ | ||
| 82 | 120 | echo '<div class="notice notice-error"><p>' . esc_html( $message ) . '</p></div>'; |
| 83 | 121 | } |
| 84 | 122 | |
| 85 | 123 | /** |
| 86 | - * Define plugin constants. | |
| 87 | - * | |
| 88 | - * @return void | |
| 89 | - */ | |
| 90 | - private function define_constants() { | |
| 91 | - define( 'FLYWP_VERSION', $this->version ); | |
| 92 | - define( 'FLYWP_PLUGIN_FILE', __FILE__ ); | |
| 93 | - define( 'FLYWP_PLUGIN_BASENAME', plugin_basename( __FILE__ ) ); | |
| 94 | - define( 'FLYWP_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); | |
| 95 | - define( 'FLYWP_PLUGIN_URL', plugin_dir_url( __FILE__ ) ); | |
| 96 | - | |
| 97 | - if ( ! defined( 'FLYWP_API_KEY' ) ) { | |
| 98 | - define( 'FLYWP_API_KEY', '' ); | |
| 99 | - } | |
| 100 | - } | |
| 101 | - | |
| 102 | - /** | |
| 103 | 124 | * Check if API key is set. |
| 104 | 125 | * |
| 105 | 126 | * @return bool |
| 106 | 127 | */ |
| 107 | - public function has_api_key() { | |
| 128 | + public function has_key() { | |
| 108 | 129 | return FLYWP_API_KEY !== ''; |
| 109 | 130 | } |
| 110 | 131 | |
| 111 | 132 | /** |
| @@ -112,9 +133,9 @@ | ||
| 112 | 133 | * Get API key. |
| 113 | 134 | * |
| 114 | 135 | * @return string |
| 115 | 136 | */ |
| 116 | - public function get_api_key() { | |
| 137 | + public function get_key() { | |
| 117 | 138 | return FLYWP_API_KEY; |
| 118 | 139 | } |
| 119 | 140 | } |
| 120 | 141 | |
| @@ -120,9 +141,9 @@ | ||
| 120 | 141 | |
| 121 | 142 | /** |
| 122 | 143 | * Returns the main instance of FlyWP to prevent the need to use globals. |
| 123 | 144 | * |
| 124 | - * @return FlyWP | |
| 145 | + * @return FlyWP_Plugin | |
| 125 | 146 | */ |
| 126 | 147 | function flywp() { |
| 127 | 148 | return FlyWP_Plugin::instance(); |
| 128 | 149 | } |