| 1 |
<?php |
| 2 |
/** |
| 3 |
* Bootstrap Class |
| 4 |
* |
| 5 |
* @package Timetics |
| 6 |
*/ |
| 7 |
namespace Timetics; |
| 8 |
|
| 9 |
defined( 'ABSPATH' ) || exit; |
| 10 |
|
| 11 |
/** |
| 12 |
* Bootstrap class. |
| 13 |
* |
| 14 |
* @since 1.0.0 |
| 15 |
*/ |
| 16 |
final class Bootstrap { |
| 17 |
|
| 18 |
/** |
| 19 |
* @var Bootstrap The Actual Timetics instance |
| 20 |
* @since 1.0.0 |
| 21 |
*/ |
| 22 |
private static $instance; |
| 23 |
|
| 24 |
/** |
| 25 |
* Throw Error While Trying To Clone Object |
| 26 |
* |
| 27 |
* @since 1.0.0 |
| 28 |
* @return void |
| 29 |
*/ |
| 30 |
public function __clone() { |
| 31 |
_doing_it_wrong( __FUNCTION__, esc_html__( 'Cloning is forbidden.', 'timetics' ), '1.0.0' ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Disabling Un-serialization Of This Class |
| 36 |
*/ |
| 37 |
public function __wakeup() { |
| 38 |
_doing_it_wrong( __FUNCTION__, esc_html__( 'Unserializing instances of this class is forbidden.', 'timetics' ), '1.0.0' ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* The actual TimeTics instance |
| 43 |
* |
| 44 |
* @since 1.0.0 |
| 45 |
* @param string $file |
| 46 |
* @return void |
| 47 |
*/ |
| 48 |
public static function instantiate( $file = '' ) { |
| 49 |
|
| 50 |
// Return if already instantiated |
| 51 |
if ( self::instantiated() ) { |
| 52 |
return self::$instance; |
| 53 |
} |
| 54 |
|
| 55 |
self::prepare_instance( $file ); |
| 56 |
|
| 57 |
self::$instance->initialize_constants(); |
| 58 |
self::$instance->define_tables(); |
| 59 |
self::$instance->include_files(); |
| 60 |
self::$instance->initialize_components(); |
| 61 |
|
| 62 |
return self::$instance; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Return If The Main Class has Already Been Instantiated Or Not |
| 67 |
* |
| 68 |
* @since 1.0.0 |
| 69 |
* @return boolean |
| 70 |
*/ |
| 71 |
private static function instantiated() { |
| 72 |
if ( ( null !== self::$instance ) && ( self::$instance instanceof Bootstrap ) ) { |
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Prepare Singleton Instance |
| 81 |
* |
| 82 |
* @since 1.0.0 |
| 83 |
* @param string $file |
| 84 |
* @return void |
| 85 |
*/ |
| 86 |
private static function prepare_instance( $file = '' ) { |
| 87 |
self::$instance = new self(); |
| 88 |
self::$instance->file = $file; |
| 89 |
self::$instance->version = \Timetics::get_version(); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Assets Directory URL |
| 94 |
* |
| 95 |
* @since 1.0.0 |
| 96 |
* @return void |
| 97 |
*/ |
| 98 |
public function get_assets_url() { |
| 99 |
return trailingslashit( TIMETICS_PLUGIN_URL . 'assets' ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Assets Directory Path |
| 104 |
* |
| 105 |
* @since 1.0.0 |
| 106 |
* @return void |
| 107 |
*/ |
| 108 |
public function get_assets_dir() { |
| 109 |
return trailingslashit( TIMETICS_PLUGIN_DIR . 'assets' ); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Plugin Directory URL |
| 114 |
* |
| 115 |
* @return void |
| 116 |
*/ |
| 117 |
public function get_plugin_url() { |
| 118 |
return trailingslashit( plugin_dir_url( TIMETICS_PLUGIN_FILE ) ); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Plugin Directory Path |
| 123 |
* |
| 124 |
* @return void |
| 125 |
*/ |
| 126 |
public function get_plugin_dir() { |
| 127 |
return \Timetics::get_plugin_dir(); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Plugin Basename |
| 132 |
* |
| 133 |
* @return void |
| 134 |
*/ |
| 135 |
public function get_plugin_basename() { |
| 136 |
return plugin_basename( TIMETICS_PLUGIN_FILE ); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Setup Plugin Constants |
| 141 |
* |
| 142 |
* @since 1.0.0 |
| 143 |
* @return void |
| 144 |
*/ |
| 145 |
private function initialize_constants() { |
| 146 |
// Plugin Version |
| 147 |
self::$instance->define( 'TIMETICS_VERSION', \Timetics::get_version() ); |
| 148 |
|
| 149 |
// Plugin Main File |
| 150 |
self::$instance->define( 'TIMETICS_PLUGIN_FILE', $this->file ); |
| 151 |
|
| 152 |
// Plugin File Basename |
| 153 |
self::$instance->define( 'TIMETICS_PLUGIN_BASE', $this->get_plugin_basename() ); |
| 154 |
|
| 155 |
// Plugin Main Directory Path |
| 156 |
self::$instance->define( 'TIMETICS_PLUGIN_DIR', $this->get_plugin_dir() ); |
| 157 |
|
| 158 |
// Plugin Main Directory URL |
| 159 |
self::$instance->define( 'TIMETICS_PLUGIN_URL', $this->get_plugin_url() ); |
| 160 |
|
| 161 |
// Plugin Assets Directory URL |
| 162 |
self::$instance->define( 'TIMETICS_ASSETS_URL', $this->get_assets_url() ); |
| 163 |
|
| 164 |
// Plugin Assets Directory Path |
| 165 |
self::$instance->define( 'TIMETICS_ASSETS_DIR', $this->get_assets_dir() ); |
| 166 |
|
| 167 |
|
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Define constant if not already set. |
| 172 |
* |
| 173 |
* @since 1.0.0 |
| 174 |
* @param string $name Constant name. |
| 175 |
* @param string|bool $value Constant value. |
| 176 |
*/ |
| 177 |
private function define( $name, $value ) { |
| 178 |
if ( ! defined( $name ) ) { |
| 179 |
define( $name, $value ); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Define DB Tables Required For This Plugin |
| 185 |
* |
| 186 |
* @since 1.0.0 |
| 187 |
* @return void |
| 188 |
*/ |
| 189 |
private function define_tables() { |
| 190 |
// To Be Implemented |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Include All Required Files |
| 195 |
* |
| 196 |
* @since 1.0.0 |
| 197 |
* @return void |
| 198 |
*/ |
| 199 |
private function include_files() { |
| 200 |
// require_once TIMETICS_PLUGIN_DIR . 'includes/post-types.php'; |
| 201 |
// require_once TIMETICS_PLUGIN_DIR . 'includes/widgets/manifest.php'; |
| 202 |
// require_once TIMETICS_PLUGIN_DIR . 'includes/template-functions.php'; |
| 203 |
// require_once TIMETICS_PLUGIN_DIR . 'includes/shortcodes.php'; |
| 204 |
// require_once TIMETICS_PLUGIN_DIR . 'includes/install.php'; |
| 205 |
|
| 206 |
/** |
| 207 |
* Core helpers. |
| 208 |
*/ |
| 209 |
require_once TIMETICS_PLUGIN_DIR . 'utils/global-helper.php'; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Initialize All Components |
| 214 |
* |
| 215 |
* @since 1.0.0 |
| 216 |
* @return void |
| 217 |
*/ |
| 218 |
private function initialize_components() { |
| 219 |
|
| 220 |
// Register scripts and styles first |
| 221 |
if ( $this->is_request( 'admin' ) ) { |
| 222 |
add_action( 'admin_enqueue_scripts', [ $this, 'admin_scripts' ] ); |
| 223 |
} |
| 224 |
|
| 225 |
if ( $this->is_request( 'frontend' ) ) { |
| 226 |
add_action( 'wp_enqueue_scripts', array( $this, 'frontend_scripts' ) ); |
| 227 |
} |
| 228 |
|
| 229 |
// Register admin menu |
| 230 |
Core\Admin\Menu::instance()->init(); |
| 231 |
Core\Base::instance()->init(); |
| 232 |
Core\Services\Hooks::instance()->init(); |
| 233 |
Base\Custom_Endpoint::instance()->init(); |
| 234 |
|
| 235 |
if ( function_exists('WC') ) { |
| 236 |
Core\Integrations\Woocommerce\Hooks::instance()->init(); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Register scripts and styles for admin |
| 242 |
* |
| 243 |
* @return void |
| 244 |
*/ |
| 245 |
public function admin_scripts( $handle ) { |
| 246 |
if ( 'toplevel_page_timetics' !== $handle ) { |
| 247 |
return; |
| 248 |
} |
| 249 |
global $wp_locale; |
| 250 |
|
| 251 |
wp_enqueue_media(); |
| 252 |
wp_enqueue_style( 'wp-color-picker' ); |
| 253 |
wp_enqueue_style( 'timetics-admin-style', TIMETICS_ASSETS_URL . 'css/admin.css', [], TIMETICS_VERSION, 'all' ); |
| 254 |
wp_enqueue_style( 'timetics-vendor', TIMETICS_ASSETS_URL . 'css/vendor.css', [], TIMETICS_VERSION, 'all' ); |
| 255 |
wp_enqueue_script( 'timetics-antd-scripts', TIMETICS_ASSETS_URL . 'js/vendors/antd.js', [ 'wp-plugins', 'wp-i18n', 'wp-element', 'wp-dom', 'wp-data' ], TIMETICS_VERSION, true ); |
| 256 |
wp_enqueue_script( 'timetics-flatpickr-scripts', TIMETICS_ASSETS_URL . 'js/vendors/flatpickr.js', [ 'wp-plugins', 'wp-i18n', 'wp-element', 'wp-dom', 'wp-data' ], TIMETICS_VERSION, true ); |
| 257 |
|
| 258 |
wp_enqueue_script( 'timetics-dashboard-scripts', TIMETICS_ASSETS_URL . 'js/dashboard.js', [ 'wp-plugins', 'wp-i18n', 'wp-element', 'wp-dom', 'wp-data', 'wp-color-picker' ], TIMETICS_VERSION, true ); |
| 259 |
|
| 260 |
$localize_obj = array( |
| 261 |
'site_url' => site_url(), |
| 262 |
'admin_url' => admin_url(), |
| 263 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 264 |
'timezone' => timetics_get_timezone_list(), |
| 265 |
'stripe_pub_key' => timetics_get_option( 'stripe_pub_key' ), |
| 266 |
'date_format' => get_option( 'date_format' ), |
| 267 |
'date_format_string' => date_i18n( get_option( 'date_format' ) ), |
| 268 |
'time_format' => get_option( 'time_format' ), |
| 269 |
'time_format_string' => date_i18n( get_option( 'time_format' ) ), |
| 270 |
'start_of_week' => $wp_locale->get_weekday( get_option( 'start_of_week' ) ), |
| 271 |
'default_timezone' => timetics_get_default_timezone(), |
| 272 |
'currency_list' => timetics_get_currencies(), |
| 273 |
'current_user_id' => get_current_user_id(), |
| 274 |
'timeticsPro' => class_exists('TimeticsPro') ? true : false, |
| 275 |
); |
| 276 |
wp_localize_script( 'timetics-dashboard-scripts', 'timetics', $localize_obj ); |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* Register scripts and styles for frontend |
| 281 |
* |
| 282 |
* @return void |
| 283 |
*/ |
| 284 |
public function frontend_scripts() { |
| 285 |
wp_register_style( 'timetics-vendor', TIMETICS_ASSETS_URL . 'css/vendor.css', [], TIMETICS_VERSION, 'all' ); |
| 286 |
wp_register_style( 'timetics-frontend', TIMETICS_ASSETS_URL . 'css/frontend.css', [], TIMETICS_VERSION, 'all' ); |
| 287 |
|
| 288 |
wp_register_script( 'timetics-antd-scripts', TIMETICS_ASSETS_URL . 'js/vendors/antd.js', [ 'wp-plugins', 'wp-i18n', 'wp-element', 'wp-dom', 'wp-data' ], TIMETICS_VERSION, true ); |
| 289 |
wp_register_script( 'timetics-flatpickr-scripts', TIMETICS_ASSETS_URL . 'js/vendors/flatpickr.js', [ 'wp-plugins', 'wp-i18n', 'wp-element', 'wp-dom', 'wp-data' ], TIMETICS_VERSION, true ); |
| 290 |
wp_register_script( 'timetics-frontend-scripts', TIMETICS_ASSETS_URL . 'js/frontend.js', [ 'jquery', 'wp-plugins', 'wp-i18n', 'wp-element', 'wp-dom', 'wp-data' ], TIMETICS_VERSION, true ); |
| 291 |
|
| 292 |
// load text domain |
| 293 |
wp_set_script_translations( 'timetics-frontend-scripts', 'timetics', TIMETICS_PLUGIN_DIR . 'languages/' ); |
| 294 |
|
| 295 |
global $wp_locale; |
| 296 |
|
| 297 |
$stripe_pub_key = timetics_get_option( 'stripe_pub_key' ); |
| 298 |
$stripe_secret_key = timetics_get_option( 'stripe_secret_key' ); |
| 299 |
$stripe_configure = $stripe_pub_key && $stripe_secret_key; |
| 300 |
|
| 301 |
$localize_obj = array( |
| 302 |
'site_url' => site_url(), |
| 303 |
'date_format' => get_option( 'date_format' ), |
| 304 |
'time_format' => get_option( 'time_format' ), |
| 305 |
'stripe_pub_key' => $stripe_pub_key, |
| 306 |
'currency' => apply_filters( 'timetics_currency', timetics_get_option( 'currency', 'USD' ) ), |
| 307 |
'stripe_configure' => $stripe_configure, |
| 308 |
'start_of_week' => $wp_locale->get_weekday( get_option( 'start_of_week' ) ), |
| 309 |
|
| 310 |
); |
| 311 |
wp_localize_script( 'timetics-frontend-scripts', 'timetics', $localize_obj ); |
| 312 |
} |
| 313 |
|
| 314 |
/** |
| 315 |
* What type of request |
| 316 |
* |
| 317 |
* @param string $type admin,frontend, ajax, cron |
| 318 |
* @return boolean |
| 319 |
*/ |
| 320 |
private function is_request( $type ) { |
| 321 |
switch ( $type ) { |
| 322 |
case 'admin': |
| 323 |
return is_admin(); |
| 324 |
case 'ajax': |
| 325 |
return defined( 'DOING_AJAX' ); |
| 326 |
case 'cron': |
| 327 |
return defined( 'DOING_CRON' ); |
| 328 |
case 'frontend': |
| 329 |
return ( ! is_admin() || defined( 'DOING_AJAX' ) ) && ! defined( 'DOING_CRON' ) && ! $this->is_rest_api_request(); |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
/** |
| 334 |
* Returns if the request is non-legacy REST API request. |
| 335 |
* |
| 336 |
* @return boolean |
| 337 |
*/ |
| 338 |
private function is_rest_api_request() { |
| 339 |
$server_request = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : false; |
| 340 |
|
| 341 |
if ( ! $server_request ) { |
| 342 |
return false; |
| 343 |
} |
| 344 |
|
| 345 |
$rest_prefix = trailingslashit( rest_get_url_prefix() ); |
| 346 |
$is_rest_request = ( false !== strpos( $server_request, $rest_prefix ) ); |
| 347 |
|
| 348 |
return apply_filters( 'timetics_is_rest_api_request', $is_rest_request ); |
| 349 |
} |
| 350 |
|
| 351 |
} |
| 352 |
|
| 353 |
/** |
| 354 |
* Returns The Instance Of Timetics. |
| 355 |
* The main function that is responsible for returning Timetics instance. |
| 356 |
* |
| 357 |
* @since 1.0.0 |
| 358 |
* @return Timetics |
| 359 |
*/ |
| 360 |
function timetics() { |
| 361 |
return Bootstrap::instantiate(); |
| 362 |
} |
| 363 |
|