| 1 |
<?php |
| 2 |
/** |
| 3 |
* Booktics Main Class |
| 4 |
* |
| 5 |
* @package Booktics/Booktics |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Booktics; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
use Booktics\Container\Container; |
| 13 |
|
| 14 |
/** |
| 15 |
* Class Booktics |
| 16 |
*/ |
| 17 |
class Booktics { |
| 18 |
|
| 19 |
/** |
| 20 |
* Plugin version. |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
public $version; |
| 25 |
|
| 26 |
/** |
| 27 |
* Plugin file. |
| 28 |
* |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
public $plugin_file; |
| 32 |
|
| 33 |
/** |
| 34 |
* Plugin directory. |
| 35 |
* |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
public $plugin_directory; |
| 39 |
|
| 40 |
/** |
| 41 |
* @var string |
| 42 |
*/ |
| 43 |
public $build_url; |
| 44 |
|
| 45 |
/** |
| 46 |
* Plugin base name. |
| 47 |
* |
| 48 |
* @var string |
| 49 |
*/ |
| 50 |
public $basename; |
| 51 |
|
| 52 |
/** |
| 53 |
* Plugin text directory path. |
| 54 |
* |
| 55 |
* @var string |
| 56 |
*/ |
| 57 |
public $text_domain_directory; |
| 58 |
|
| 59 |
/** |
| 60 |
* Plugin text directory path. |
| 61 |
* |
| 62 |
* @var string |
| 63 |
*/ |
| 64 |
public $template_directory; |
| 65 |
|
| 66 |
/** |
| 67 |
* Plugin assets directory path. |
| 68 |
* |
| 69 |
* @var string |
| 70 |
*/ |
| 71 |
public $assets_url; |
| 72 |
|
| 73 |
/** |
| 74 |
* Plugin url. |
| 75 |
* |
| 76 |
* @var string |
| 77 |
*/ |
| 78 |
public $plugin_url; |
| 79 |
|
| 80 |
/** |
| 81 |
* Container that holds all the services. |
| 82 |
* |
| 83 |
* @var Container |
| 84 |
*/ |
| 85 |
public $container; |
| 86 |
|
| 87 |
/** |
| 88 |
* Booktics Constructor. |
| 89 |
*/ |
| 90 |
public function __construct() { |
| 91 |
$this->init(); |
| 92 |
add_action( 'init', array( $this, 'init_classes' ) ); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Initialize the plugin. |
| 97 |
* |
| 98 |
* @return void |
| 99 |
*/ |
| 100 |
protected function init(): void { |
| 101 |
$this->version = BOOKTICS_VERSION; |
| 102 |
$this->plugin_file = BOOKTICS_FILE; |
| 103 |
$this->plugin_directory = BOOKTICS_DIR; |
| 104 |
$this->basename = plugin_basename( $this->plugin_file ); |
| 105 |
$this->text_domain_directory = $this->plugin_directory . '/languages'; |
| 106 |
$this->template_directory = $this->plugin_directory . '/templates'; |
| 107 |
$this->plugin_url = plugins_url( '', $this->plugin_file ); |
| 108 |
$this->assets_url = $this->plugin_url . '/assets'; |
| 109 |
$this->build_url = $this->plugin_url . '/build'; |
| 110 |
register_activation_hook( |
| 111 |
$this->plugin_file, array( |
| 112 |
$this, |
| 113 |
'activate', |
| 114 |
) |
| 115 |
); |
| 116 |
register_deactivation_hook( |
| 117 |
$this->plugin_file, array( |
| 118 |
$this, |
| 119 |
'deactivate', |
| 120 |
) |
| 121 |
); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Init classes |
| 126 |
* |
| 127 |
* @return void |
| 128 |
*/ |
| 129 |
public function init_classes() { |
| 130 |
if ( ! session_id() ) { |
| 131 |
session_start(); |
| 132 |
} |
| 133 |
self::register_custom_post_type(); |
| 134 |
|
| 135 |
( new \Booktics\Base\Services\Post_Status_Service() )->register(); |
| 136 |
|
| 137 |
$service_provider = $this->get_container()->get( 'global' ); |
| 138 |
|
| 139 |
// Initilize Plugin Banner Notice. |
| 140 |
Plugin_Notice::run(); |
| 141 |
|
| 142 |
self::version_check(); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Get container |
| 147 |
* |
| 148 |
* @return Container |
| 149 |
*/ |
| 150 |
public function get_container() { |
| 151 |
return booktics_container(); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Initializes the Booktics class. |
| 156 |
* |
| 157 |
* Checks for an existing WeMeal instance |
| 158 |
* and if it doesn't find one, creates it. |
| 159 |
* |
| 160 |
* @return Booktics |
| 161 |
*/ |
| 162 |
public static function instance(): Booktics { |
| 163 |
static $instance = false; |
| 164 |
|
| 165 |
if ( ! $instance ) { |
| 166 |
$instance = new self(); |
| 167 |
} |
| 168 |
|
| 169 |
return $instance; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Trigger plugin activation class |
| 174 |
* |
| 175 |
* @return void |
| 176 |
*/ |
| 177 |
public function activate() { |
| 178 |
Activate::handle(); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Trigger plugin deactivation |
| 183 |
* |
| 184 |
* @return void |
| 185 |
*/ |
| 186 |
public function deactivate() { |
| 187 |
Deactivate::handle(); |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* returns assets url |
| 192 |
* |
| 193 |
* @return string |
| 194 |
*/ |
| 195 |
public static function assets_url(): string { |
| 196 |
return trailingslashit( plugins_url( '', BOOKTICS_FILE ) . '/assets' ); |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Check if the plugin version is up to date |
| 201 |
* |
| 202 |
* @return void |
| 203 |
*/ |
| 204 |
public static function version_check() { |
| 205 |
$latest_version = BOOKTICS_VERSION; |
| 206 |
if ( is_multisite() ) { |
| 207 |
$sites = get_sites(); |
| 208 |
|
| 209 |
foreach ( $sites as $site ) { |
| 210 |
switch_to_blog( $site->blog_id ); |
| 211 |
|
| 212 |
$installed_version = booktics_get_option( 'booktics_version' ); |
| 213 |
if ( $installed_version < $latest_version ) { |
| 214 |
self::run_migration(); |
| 215 |
booktics_update_option( 'booktics_version', booktics()->version ); |
| 216 |
} |
| 217 |
|
| 218 |
restore_current_blog(); |
| 219 |
} |
| 220 |
} else { |
| 221 |
$installed_version = booktics_get_option( 'booktics_version' ); |
| 222 |
if ( $installed_version < $latest_version ) { |
| 223 |
self::run_migration(); |
| 224 |
booktics_update_option( 'booktics_version', booktics()->version ); |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Run migration |
| 231 |
* |
| 232 |
* @return void |
| 233 |
*/ |
| 234 |
public static function run_migration() { |
| 235 |
Installer::create_tables(); |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Register post type |
| 240 |
* |
| 241 |
* @return void |
| 242 |
*/ |
| 243 |
public static function register_custom_post_type() { |
| 244 |
register_post_type( |
| 245 |
'booktics-service', array( |
| 246 |
'labels' => array( |
| 247 |
'name' => __( 'Services', 'booktics' ), |
| 248 |
'singular_name' => __( 'Service', 'booktics' ), |
| 249 |
), |
| 250 |
'public' => true, |
| 251 |
'has_archive' => true, |
| 252 |
'has_single' => true, |
| 253 |
'rewrite' => true, |
| 254 |
'supports' => array( 'title', 'editor', 'thumbnail' ), |
| 255 |
'show_in_rest' => true, |
| 256 |
'show_in_menu' => false, |
| 257 |
) |
| 258 |
); |
| 259 |
} |
| 260 |
} |
| 261 |
|