PluginProbe
Yatra – Travel Booking & Tour Operator Software / 2.0.10
Yatra – Travel Booking & Tour Operator Software v2.0.10
3.0.14 3.0.14.1 3.0.14.2 3.0.12 3.0.13 3.0.11 3.0.10 3.0.9 3.0.8 3.0.7 3.0.6 3.0.5 3.0.5.1 3.0.4 3.0.3 3.0.2.9 3.0.2.7 3.0.2.8 3.0.2.6 trunk 1.0.0 2.0.0 2.0.1 2.0.10 2.0.11 All 82 releases
yatra / includes / class-yatra.php

class-yatra.php in Yatra – Travel Booking & Tour Operator Software 2.0.10, at includes/class-yatra.php

440 lines 8.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Yatra setup
4 *
5 * @package Yatra
6 * @since 1.0.0
7 */
8
9 defined('ABSPATH') || exit;
10
11 /**
12 * Main Yatra Class.
13 *
14 * @class Yatra
15 */
16 final class Yatra
17 {
18
19 /**
20 * Yatra version.
21 *
22 * @var string
23 */
24 public $version = YATRA_VERSION;
25
26
27 /**
28 * Cart instance.
29 *
30 * @var Yatra_Cart
31 */
32 public $cart = null;
33
34 /**
35 * Yatra_Error instance.
36 *
37 * @var WP_Error
38 */
39 public $yatra_error = null;
40
41 /**
42 * Yatra_Error instance.
43 *
44 * @var Yatra_Messages
45 */
46 public $yatra_messages = null;
47
48
49 /**
50 * Yatra_Core_Exporter instance.
51 *
52 * @var Yatra_Core_Exporter
53 */
54 public $exporter = null;
55
56
57 /**
58 * Yatra_Core_Importer instance.
59 *
60 * @var Yatra_Core_Importer
61 */
62 public $importer = null;
63 /**
64 * The single instance of the class.
65 *
66 * @var Yatra
67 * @since 1.0.0
68 */
69 protected static $_instance = null;
70
71
72 /**
73 * Main Yatra Instance.
74 *
75 * Ensures only one instance of Yatra is loaded or can be loaded.
76 *
77 * @return Yatra - Main instance.
78 * @see mb_aec_addons()
79 * @since 1.0.0
80 * @static
81 */
82 public static function instance()
83 {
84 if (is_null(self::$_instance)) {
85 self::$_instance = new self();
86 }
87 return self::$_instance;
88 }
89
90 /**
91 * Cloning is forbidden.
92 *
93 * @since 1.0.0
94 */
95 public function __clone()
96 {
97 _doing_it_wrong(__FUNCTION__, __('Cloning is forbidden.', 'yatra'), '1.0.0');
98 }
99
100 /**
101 * Unserializing instances of this class is forbidden.
102 *
103 * @since 1.0.0
104 */
105 public function __wakeup()
106 {
107 _doing_it_wrong(__FUNCTION__, __('Unserializing instances of this class is forbidden.', 'yatra'), '1.0.0');
108 }
109
110 /**
111 * Auto-load in-accessible properties on demand.
112 *
113 * @param mixed $key Key name.
114 * @return mixed
115 */
116 public function __get($key)
117 {
118 if (in_array($key, array(''), true)) {
119 return $this->$key();
120 }
121 }
122
123 /**
124 * Yatra Constructor.
125 */
126 public function __construct()
127 {
128
129 $this->define_constants();
130 $this->includes();
131 $this->init_hooks();
132
133
134 do_action('yatra_loaded');
135 }
136
137 /**
138 * Hook into actions and filters.
139 *
140 * @since 1.0.0
141 */
142 private function init_hooks()
143 {
144
145
146 register_activation_hook(YATRA_FILE, array('Yatra_Install', 'install'));
147
148 add_action('init', array($this, 'init'), 0);
149 add_action('init', array('Yatra_Shortcodes', 'init'));
150
151
152 }
153
154 /**
155 * Define Yatra Constants.
156 */
157 private function define_constants()
158 {
159
160 $this->define('YATRA_ABSPATH', dirname(YATRA_FILE) . '/');
161 $this->define('YATRA_BASENAME', plugin_basename(YATRA_FILE));
162 }
163
164 /**
165 * Define constant if not already set.
166 *
167 * @param string $name Constant name.
168 * @param string|bool $value Constant value.
169 */
170 private function define($name, $value)
171 {
172 if (!defined($name)) {
173 define($name, $value);
174 }
175 }
176
177 /**
178 * What type of request is this?
179 *
180 * @param string $type admin, ajax, cron or frontend.
181 * @return bool
182 */
183 private function is_request($type)
184 {
185 switch ($type) {
186 case 'admin':
187 return is_admin();
188 case 'ajax':
189 return defined('DOING_AJAX');
190 case 'cron':
191 return defined('DOING_CRON');
192 case 'frontend':
193 return (!is_admin() || defined('DOING_AJAX')) && !defined('DOING_CRON') && !defined('REST_REQUEST');
194 }
195 }
196
197 /**
198 * Include required core files used in admin and on the frontend.
199 */
200 public function includes()
201 {
202
203
204 /*
205 * Abstract Class
206 */
207
208 include_once YATRA_ABSPATH . 'includes/abstracts/abstract-yatra-form.php';
209 include_once YATRA_ABSPATH . 'includes/abstracts/abstract-yatra-payment-gateways.php';
210
211
212 /**
213 * Class autoloader.
214 */
215 include_once YATRA_ABSPATH . 'includes/class-yatra-autoloader.php';
216
217 include_once YATRA_ABSPATH . 'includes/payment-gateways/class-yatra-gateways-core.php';
218 include_once YATRA_ABSPATH . 'includes/functions.php';
219 include_once YATRA_ABSPATH . 'includes/yatra-hooks.php';
220 include_once YATRA_ABSPATH . 'includes/yatra-template-functions.php';
221 include_once YATRA_ABSPATH . 'includes/yatra-template-hooks.php';
222 include_once YATRA_ABSPATH . 'includes/class-yatra-page-templater.php';
223 include_once YATRA_ABSPATH . 'includes/class-yatra-email.php';
224 include_once YATRA_ABSPATH . 'includes/class-yatra-custom-post-type.php';
225 include_once YATRA_ABSPATH . 'includes/class-yatra-taxonomy.php';
226 include_once YATRA_ABSPATH . 'includes/class-yatra-metabox.php';
227 include_once YATRA_ABSPATH . 'includes/class-yatra-widgets.php';
228 include_once YATRA_ABSPATH . 'includes/class-yatra-shortcodes.php';
229 include_once YATRA_ABSPATH . 'includes/class-yatra-ajax.php';
230
231
232 // Compatibility
233 include_once YATRA_ABSPATH . 'includes/class-yatra-compatibility.php';
234
235
236 if ($this->is_request('admin')) {
237 Yatra_Admin::instance();
238 }
239
240 if ($this->is_request('frontend')) {
241 Yatra_Frontend::instance();
242 }
243
244 $this->yatra_error = new WP_Error;
245 $this->yatra_messages = new Yatra_Messages;
246
247
248 }
249
250
251 /**
252 * Init Yatra when WordPress Initialises.
253 */
254 public function init()
255 {
256 // Before init action.
257 do_action('before_yatra_init');
258
259 // Set up localisation.
260 $this->load_plugin_textdomain();
261
262 if ($this->is_request('admin')) {
263 $this->exporter = new Yatra_Core_Exporter();
264 $this->importer = new Yatra_Core_Importer();
265
266 }
267
268 // Classes/actions loaded for the frontend and for ajax requests.
269 //if ($this->is_request('frontend')) {
270 if (is_null($this->cart) || !$this->cart instanceof Yatra_Cart) {
271
272 $this->initialize_cart();
273 }
274
275 // Init action.
276 do_action('yatra_init');
277 }
278
279 /**
280 * Load Localisation files.
281 *
282 * Note: the first-loaded translation file overrides any following ones if the same translation is present.
283 *
284 * Locales found in:
285 * - WP_LANG_DIR/yatra/yatra-LOCALE.mo
286 * - WP_LANG_DIR/plugins/yatra-LOCALE.mo
287 */
288 public function load_plugin_textdomain()
289 {
290 $locale = is_admin() && function_exists('get_user_locale') ? get_user_locale() : get_locale();
291 $locale = apply_filters('plugin_locale', $locale, 'yatra');
292 unload_textdomain('yatra');
293 load_textdomain('yatra', WP_LANG_DIR . '/yatra/yatra-' . $locale . '.mo');
294 load_plugin_textdomain('yatra', false, plugin_basename(dirname(YATRA_FILE)) . '/i18n/languages');
295 }
296
297 /**
298 * Ensure theme and server variable compatibility and setup image sizes.
299 */
300 public function setup_environment()
301 {
302
303 $this->define('YATRA_TEMPLATE_PATH', $this->template_path());
304
305 }
306
307 /**
308 * Get the plugin url.
309 *
310 * @return string
311 */
312 public function plugin_url()
313 {
314 return untrailingslashit(plugins_url('/', YATRA_FILE));
315 }
316
317 /**
318 * Get the plugin path.
319 *
320 * @return string
321 */
322 public function plugin_path()
323 {
324 return untrailingslashit(plugin_dir_path(YATRA_FILE));
325 }
326
327 /**
328 * Get the template path.
329 *
330 * @return string
331 */
332 public function template_path()
333 {
334 return apply_filters('yatra_template_path', 'yatra/');
335 }
336
337 /**
338 * Get the template path.
339 *
340 * @return string
341 */
342 public function plugin_template_path()
343 {
344 return apply_filters('yatra_plugin_template_path', $this->plugin_path() . '/templates/');
345 }
346
347 /**
348 * Get Ajax URL.
349 *
350 * @return string
351 */
352 public function ajax_url()
353 {
354 return admin_url('admin-ajax.php', 'relative');
355 }
356
357 /**
358 * Initialize the customer and cart objects and setup customer saving on shutdown.
359 *
360 * @return void
361 * @since 2.0.0
362 */
363 public function initialize_cart()
364 {
365
366 if (is_null($this->cart) || !$this->cart instanceof Yatra_Cart) {
367 $this->cart = new Yatra_Cart();
368 }
369 }
370
371 public function get_upload_dir($create_if_not_exists = false)
372 {
373 $upload_dir = wp_upload_dir();
374
375 if ($create_if_not_exists) {
376
377 $this->create_files();
378 }
379
380 return $upload_dir['basedir'] . '/yatra/';
381
382 }
383
384 private function create_files()
385 {
386 // Bypass if filesystem is read-only and/or non-standard upload system is used.
387 if (apply_filters('yatra_install_skip_create_files', false)) {
388 return;
389 }
390
391 if (file_exists(trailingslashit($this->get_upload_dir()) . 'index.html')) {
392 return true;
393 }
394 $files = array(
395 array(
396 'base' => $this->get_upload_dir(),
397 'file' => 'index.html',
398 'content' => '',
399 ),
400 array(
401 'base' => $this->get_upload_dir(),
402 'file' => '.htaccess',
403 'content' => '<FilesMatch ".*\.(css|js)$">
404 Order Allow,Deny
405 Allow from all
406 </FilesMatch>',
407 ),
408 array(
409 'base' => $this->get_upload_dir(),
410 'file' => '.htaccess',
411 'content' => '<FilesMatch ".*\.(css|js)$">
412 Order Allow,Deny
413 Allow from all
414 </FilesMatch>',
415 )
416 );
417
418 $has_created_dir = false;
419
420 foreach ($files as $file) {
421 if (wp_mkdir_p($file['base']) && !file_exists(trailingslashit($file['base']) . $file['file'])) {
422 $file_handle = @fopen(trailingslashit($file['base']) . $file['file'], 'w'); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_system_read_fopen
423 if ($file_handle) {
424 fwrite($file_handle, $file['content']); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fwrite
425 fclose($file_handle); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fclose
426 if (!$has_created_dir) {
427 $has_created_dir = true;
428 }
429 }
430 }
431 }
432 if ($has_created_dir) {
433 return true;
434 }
435
436
437 }
438
439 }
440