PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.1
Timetics – Appointment Booking Calendar & Scheduling v1.0.1
1.0.61 1.0.60 1.0.59 1.0.58 1.0.57 1.0.56 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.2 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 All 62 releases
timetics / bootstrap.php

bootstrap.php in Timetics – Appointment Booking Calendar & Scheduling 1.0.1, at bootstrap.php

358 lines 11.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Bootstrap Class
4 *
5 * @package Timetics
6 */
7 namespace Timetics;
8
9 use ElementsKit_Lite\Widgets\Init\Enqueue_Scripts;
10
11 defined( 'ABSPATH' ) || exit;
12
13 /**
14 * Bootstrap class.
15 *
16 * @since 1.0.0
17 */
18 final class Bootstrap {
19
20 /**
21 * @var Bootstrap The Actual Timetics instance
22 * @since 1.0.0
23 */
24 private static $instance;
25
26 /**
27 * Throw Error While Trying To Clone Object
28 *
29 * @since 1.0.0
30 * @return void
31 */
32 public function __clone() {
33 _doing_it_wrong( __FUNCTION__, __( 'Cloning is forbidden.', 'timetics' ), '1.0.0' );
34 }
35
36 /**
37 * Disabling Un-serialization Of This Class
38 */
39 public function __wakeup() {
40 _doing_it_wrong( __FUNCTION__, __( 'Unserializing instances of this class is forbidden.', 'timetics' ), '1.0.0' );
41 }
42
43 /**
44 * The actual TimeTics instance
45 *
46 * @since 1.0.0
47 * @param string $file
48 * @return void
49 */
50 public static function instantiate( $file = '' ) {
51
52 // Return if already instantiated
53 if ( self::instantiated() ) {
54 return self::$instance;
55 }
56
57 self::prepare_instance( $file );
58
59 self::$instance->initialize_constants();
60 self::$instance->define_tables();
61 self::$instance->include_files();
62 self::$instance->initialize_components();
63
64 return self::$instance;
65 }
66
67 /**
68 * Return If The Main Class has Already Been Instantiated Or Not
69 *
70 * @since 1.0.0
71 * @return boolean
72 */
73 private static function instantiated() {
74 if ( ( null !== self::$instance ) && ( self::$instance instanceof Bootstrap ) ) {
75 return true;
76 }
77
78 return false;
79 }
80
81 /**
82 * Prepare Singleton Instance
83 *
84 * @since 1.0.0
85 * @param string $file
86 * @return void
87 */
88 private static function prepare_instance( $file = '' ) {
89 self::$instance = new self();
90 self::$instance->file = $file;
91 self::$instance->version = \Timetics::get_version();
92 }
93
94 /**
95 * Assets Directory URL
96 *
97 * @since 1.0.0
98 * @return void
99 */
100 public function get_assets_url() {
101 return trailingslashit( TIMETICS_PLUGIN_URL . 'assets' );
102 }
103
104 /**
105 * Assets Directory Path
106 *
107 * @since 1.0.0
108 * @return void
109 */
110 public function get_assets_dir() {
111 return trailingslashit( TIMETICS_PLUGIN_DIR . 'assets' );
112 }
113
114 /**
115 * Plugin Directory URL
116 *
117 * @return void
118 */
119 public function get_plugin_url() {
120 return trailingslashit( plugin_dir_url( TIMETICS_PLUGIN_FILE ) );
121 }
122
123 /**
124 * Plugin Directory Path
125 *
126 * @return void
127 */
128 public function get_plugin_dir() {
129 return \Timetics::get_plugin_dir();
130 }
131
132 /**
133 * Plugin Basename
134 *
135 * @return void
136 */
137 public function get_plugin_basename() {
138 return plugin_basename( TIMETICS_PLUGIN_FILE );
139 }
140
141 /**
142 * Setup Plugin Constants
143 *
144 * @since 1.0.0
145 * @return void
146 */
147 private function initialize_constants() {
148 // Plugin Version
149 self::$instance->define( 'TIMETICS_VERSION', \Timetics::get_version() );
150
151 // Plugin Main File
152 self::$instance->define( 'TIMETICS_PLUGIN_FILE', $this->file );
153
154 // Plugin File Basename
155 self::$instance->define( 'TIMETICS_PLUGIN_BASE', $this->get_plugin_basename() );
156
157 // Plugin Main Directory Path
158 self::$instance->define( 'TIMETICS_PLUGIN_DIR', $this->get_plugin_dir() );
159
160 // Plugin Main Directory URL
161 self::$instance->define( 'TIMETICS_PLUGIN_URL', $this->get_plugin_url() );
162
163 // Plugin Assets Directory URL
164 self::$instance->define( 'TIMETICS_ASSETS_URL', $this->get_assets_url() );
165
166 // Plugin Assets Directory Path
167 self::$instance->define( 'TIMETICS_ASSETS_DIR', $this->get_assets_dir() );
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
236 /**
237 * Register scripts and styles for admin
238 *
239 * @return void
240 */
241 public function admin_scripts( $handle ) {
242 if ( 'toplevel_page_timetics' !== $handle ) {
243 return;
244 }
245 global $wp_locale;
246
247 wp_enqueue_media();
248
249 wp_enqueue_style( 'timetics-admin-style', TIMETICS_ASSETS_URL . 'css/admin.css', [], TIMETICS_VERSION, 'all' );
250 wp_enqueue_style( 'timetics-vendor', TIMETICS_ASSETS_URL . 'css/vendor.css', [], TIMETICS_VERSION, 'all' );
251 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 );
252 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 );
253
254 wp_enqueue_script( 'timetics-dashboard-scripts', TIMETICS_ASSETS_URL . 'js/dashboard.js', [ 'wp-plugins', 'wp-i18n', 'wp-element', 'wp-dom', 'wp-data' ], TIMETICS_VERSION, true );
255
256 $localize_obj = array(
257 'site_url' => site_url(),
258 'admin_url' => admin_url(),
259 'nonce' => wp_create_nonce( 'wp_rest' ),
260 'timezone' => timetics_get_timezone_list(),
261 'stripe_pub_key' => timetics_get_option( 'stripe_pub_key' ),
262 'date_format' => get_option( 'date_format' ),
263 'date_format_string' => date_i18n( get_option( 'date_format' ) ),
264 'time_format' => get_option( 'time_format' ),
265 'time_format_string' => date_i18n( get_option( 'time_format' ) ),
266 'start_of_week' => $wp_locale->get_weekday( get_option( 'start_of_week' ) ),
267 'default_timezone' => timetics_get_default_timezone(),
268 'currency_list' => timetics_get_currencies(),
269 'current_user_id' => get_current_user_id(),
270 );
271 wp_localize_script( 'timetics-dashboard-scripts', 'timetics', $localize_obj );
272 }
273
274 /**
275 * Register scripts and styles for frontend
276 *
277 * @return void
278 */
279 public function frontend_scripts() {
280 wp_register_style( 'timetics-vendor', TIMETICS_ASSETS_URL . 'css/vendor.css', [], TIMETICS_VERSION, 'all' );
281 wp_register_style( 'timetics-frontend', TIMETICS_ASSETS_URL . 'css/frontend.css', [], TIMETICS_VERSION, 'all' );
282
283 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 );
284 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 );
285 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 );
286
287 // load text domain
288 wp_set_script_translations( 'timetics-frontend-scripts', 'timetics', TIMETICS_PLUGIN_DIR . 'languages/' );
289
290 global $wp_locale;
291
292 $stripe_pub_key = timetics_get_option( 'stripe_pub_key' );
293 $stripe_secret_key = timetics_get_option( 'stripe_secret_key' );
294 $stripe_configure = $stripe_pub_key && $stripe_secret_key;
295
296 $localize_obj = array(
297 'site_url' => site_url(),
298 'date_format' => get_option( 'date_format' ),
299 'time_format' => get_option( 'time_format' ),
300 'stripe_pub_key' => $stripe_pub_key,
301 'currency' => timetics_get_option( 'currency', 'USD' ),
302 'stripe_configure' => $stripe_configure,
303 'start_of_week' => $wp_locale->get_weekday( get_option( 'start_of_week' ) ),
304
305 );
306 wp_localize_script( 'timetics-frontend-scripts', 'timetics', $localize_obj );
307 }
308
309 /**
310 * What type of request
311 *
312 * @param string $type admin,frontend, ajax, cron
313 * @return boolean
314 */
315 private function is_request( $type ) {
316 switch ( $type ) {
317 case 'admin':
318 return is_admin();
319 case 'ajax':
320 return defined( 'DOING_AJAX' );
321 case 'cron':
322 return defined( 'DOING_CRON' );
323 case 'frontend':
324 return ( ! is_admin() || defined( 'DOING_AJAX' ) ) && ! defined( 'DOING_CRON' ) && ! $this->is_rest_api_request();
325 }
326 }
327
328 /**
329 * Returns if the request is non-legacy REST API request.
330 *
331 * @return boolean
332 */
333 private function is_rest_api_request() {
334 $server_request = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : false;
335
336 if ( ! $server_request ) {
337 return false;
338 }
339
340 $rest_prefix = trailingslashit( rest_get_url_prefix() );
341 $is_rest_request = ( false !== strpos( $server_request, $rest_prefix ) );
342
343 return apply_filters( 'timetics_is_rest_api_request', $is_rest_request );
344 }
345
346 }
347
348 /**
349 * Returns The Instance Of Timetics.
350 * The main function that is responsible for returning Timetics instance.
351 *
352 * @since 1.0.0
353 * @return Timetics
354 */
355 function timetics() {
356 return Bootstrap::instantiate();
357 }
358