| 1 |
<?php |
| 2 |
/** |
| 3 |
* Yatra setup |
| 4 |
* |
| 5 |
* @package Yatra |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
use Yatra\Core\Admin\Notices; |
| 10 |
use Yatra\Core\Admin\Tracking; |
| 11 |
use Yatra\Core\Helper; |
| 12 |
use Yatra\Core\Initialize; |
| 13 |
use Yatra\Core\Session; |
| 14 |
use Yatra\Core\Tour\TourFactory; |
| 15 |
|
| 16 |
defined('ABSPATH') || exit; |
| 17 |
|
| 18 |
/** |
| 19 |
* Main Yatra Class. |
| 20 |
* |
| 21 |
* @class Yatra |
| 22 |
*/ |
| 23 |
final class Yatra |
| 24 |
{ |
| 25 |
|
| 26 |
/** |
| 27 |
* Yatra version. |
| 28 |
* |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
public $version = YATRA_VERSION; |
| 32 |
|
| 33 |
|
| 34 |
/** |
| 35 |
* EDD loader file. |
| 36 |
* |
| 37 |
* @since 2.1.12 |
| 38 |
* @var string |
| 39 |
*/ |
| 40 |
private $file = ''; |
| 41 |
/** |
| 42 |
* Cart instance. |
| 43 |
* |
| 44 |
* @var Yatra_Cart |
| 45 |
*/ |
| 46 |
public $cart = null; |
| 47 |
|
| 48 |
/** |
| 49 |
* Session instance. |
| 50 |
* |
| 51 |
* @var Session |
| 52 |
*/ |
| 53 |
public $session = null; |
| 54 |
|
| 55 |
/** |
| 56 |
* Tracking instance. |
| 57 |
* |
| 58 |
* @var Tracking |
| 59 |
*/ |
| 60 |
public $tracking = null; |
| 61 |
|
| 62 |
/** |
| 63 |
* Tracking instance. |
| 64 |
* |
| 65 |
* @var TourFactory |
| 66 |
*/ |
| 67 |
public $tour_factory = null; |
| 68 |
|
| 69 |
/** |
| 70 |
* Tracking instance. |
| 71 |
* |
| 72 |
* @var Helper |
| 73 |
*/ |
| 74 |
public $helper = null; |
| 75 |
|
| 76 |
/** |
| 77 |
* Yatra_Error instance. |
| 78 |
* |
| 79 |
* @var WP_Error |
| 80 |
*/ |
| 81 |
public $yatra_error = null; |
| 82 |
|
| 83 |
/** |
| 84 |
* Yatra_Error instance. |
| 85 |
* |
| 86 |
* @var Yatra_Messages |
| 87 |
*/ |
| 88 |
public $yatra_messages = null; |
| 89 |
|
| 90 |
/** |
| 91 |
* @var Yatra\Core\Admin\Notices |
| 92 |
*/ |
| 93 |
public $admin_notice = null; |
| 94 |
|
| 95 |
/** |
| 96 |
* Yatra_Core_Exporter instance. |
| 97 |
* |
| 98 |
* @var Yatra_Core_Exporter |
| 99 |
*/ |
| 100 |
public $exporter = null; |
| 101 |
|
| 102 |
/** |
| 103 |
* Yatra_Core_Importer instance. |
| 104 |
* |
| 105 |
* @var Yatra_Core_Importer |
| 106 |
*/ |
| 107 |
public $importer = null; |
| 108 |
/** |
| 109 |
* The single instance of the class. |
| 110 |
* |
| 111 |
* @var Yatra |
| 112 |
* @since 1.0.0 |
| 113 |
*/ |
| 114 |
protected static $_instance = null; |
| 115 |
|
| 116 |
|
| 117 |
private static function is_instantiated() |
| 118 |
{ |
| 119 |
|
| 120 |
// Return true if instance is correct class |
| 121 |
if (!empty(self::$_instance) && (self::$_instance instanceof Yatra)) { |
| 122 |
return true; |
| 123 |
} |
| 124 |
|
| 125 |
// Return false if not instantiated correctly |
| 126 |
return false; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Main Yatra Instance. |
| 131 |
* |
| 132 |
* Ensures only one instance of Yatra is loaded or can be loaded. |
| 133 |
* |
| 134 |
* @return Yatra - Main instance. |
| 135 |
* @static |
| 136 |
*/ |
| 137 |
public static function instance($file = '') |
| 138 |
{ |
| 139 |
if (self::is_instantiated()) { |
| 140 |
return self::$_instance; |
| 141 |
} |
| 142 |
self::setup_instance($file); |
| 143 |
self::$_instance->session = new Session(); |
| 144 |
self::$_instance->tracking = new Tracking(); |
| 145 |
self::$_instance->helper = new Helper(); |
| 146 |
self::$_instance->tour_factory = new TourFactory(); |
| 147 |
return self::$_instance; |
| 148 |
} |
| 149 |
|
| 150 |
private static function setup_instance($file = '') |
| 151 |
{ |
| 152 |
self::$_instance = new Yatra; |
| 153 |
self::$_instance->file = $file; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Cloning is forbidden. |
| 158 |
* |
| 159 |
* @since 1.0.0 |
| 160 |
*/ |
| 161 |
public function __clone() |
| 162 |
{ |
| 163 |
_doing_it_wrong(__FUNCTION__, __('Cloning is forbidden.', 'yatra'), '1.0.0'); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Unserializing instances of this class is forbidden. |
| 168 |
* |
| 169 |
* @since 1.0.0 |
| 170 |
*/ |
| 171 |
public function __wakeup() |
| 172 |
{ |
| 173 |
_doing_it_wrong(__FUNCTION__, __('Unserializing instances of this class is forbidden.', 'yatra'), '1.0.0'); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Auto-load in-accessible properties on demand. |
| 178 |
* |
| 179 |
* @param mixed $key Key name. |
| 180 |
* @return mixed |
| 181 |
*/ |
| 182 |
public function __get($key) |
| 183 |
{ |
| 184 |
if (in_array($key, array(''), true)) { |
| 185 |
return $this->$key(); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Yatra Constructor. |
| 191 |
*/ |
| 192 |
public function __construct() |
| 193 |
{ |
| 194 |
|
| 195 |
$this->define_constants(); |
| 196 |
$this->includes(); |
| 197 |
$this->init_hooks(); |
| 198 |
|
| 199 |
|
| 200 |
do_action('yatra_loaded'); |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Hook into actions and filters. |
| 205 |
* |
| 206 |
* @since 1.0.0 |
| 207 |
*/ |
| 208 |
private function init_hooks() |
| 209 |
{ |
| 210 |
register_activation_hook(YATRA_FILE, array('Yatra_Install', 'install')); |
| 211 |
register_shutdown_function(array($this, 'log_errors')); |
| 212 |
add_action('init', array($this, 'init'), 0); |
| 213 |
add_action('init', array('Yatra_Shortcodes', 'init')); |
| 214 |
|
| 215 |
|
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Define Yatra Constants. |
| 220 |
*/ |
| 221 |
private function define_constants() |
| 222 |
{ |
| 223 |
|
| 224 |
$this->define('YATRA_ABSPATH', dirname(YATRA_FILE) . '/'); |
| 225 |
$this->define('YATRA_BASENAME', plugin_basename(YATRA_FILE)); |
| 226 |
$this->define('YATRA_ROUNDING_PRECISION', 6); |
| 227 |
$this->define('YATRA_REST_NAMESPACE', 'yatra/v1/'); |
| 228 |
$this->define('YATRA_REST_WEBHOOKS_NAMESPACE', YATRA_REST_NAMESPACE . 'webhooks'); |
| 229 |
$this->define('YATRA_REST_GENERAL_NAMESPACE', YATRA_REST_NAMESPACE . 'general'); |
| 230 |
|
| 231 |
// Admin Menu Slugs |
| 232 |
|
| 233 |
$this->define('YATRA_ADMIN_MENU_SLUG', 'yatra-dashboard'); |
| 234 |
$this->define('YATRA_TOUR_ADMIN_MENU_SLUG', 'edit.php?post_type=tour'); |
| 235 |
|
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Define constant if not already set. |
| 241 |
* |
| 242 |
* @param string $name Constant name. |
| 243 |
* @param string|bool $value Constant value. |
| 244 |
*/ |
| 245 |
private function define($name, $value) |
| 246 |
{ |
| 247 |
if (!defined($name)) { |
| 248 |
define($name, $value); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* What type of request is this? |
| 254 |
* |
| 255 |
* @param string $type admin, ajax, cron or frontend. |
| 256 |
* @return bool |
| 257 |
*/ |
| 258 |
private function is_request($type) |
| 259 |
{ |
| 260 |
switch ($type) { |
| 261 |
case 'admin': |
| 262 |
return is_admin(); |
| 263 |
case 'ajax': |
| 264 |
return defined('DOING_AJAX'); |
| 265 |
case 'cron': |
| 266 |
return defined('DOING_CRON'); |
| 267 |
case 'frontend': |
| 268 |
return (!is_admin() || defined('DOING_AJAX')) && !defined('DOING_CRON') && !defined('REST_REQUEST'); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Include required core files used in admin and on the frontend. |
| 274 |
*/ |
| 275 |
public function includes() |
| 276 |
{ |
| 277 |
|
| 278 |
|
| 279 |
// Autoloader |
| 280 |
|
| 281 |
include_once YATRA_ABSPATH . 'includes/class-yatra-autoloader.php'; |
| 282 |
|
| 283 |
/* |
| 284 |
* Abstract Class |
| 285 |
*/ |
| 286 |
|
| 287 |
include_once YATRA_ABSPATH . 'includes/static/class-yatra-tables.php'; |
| 288 |
include_once YATRA_ABSPATH . 'includes/abstracts/abstract-yatra-form.php'; |
| 289 |
include_once YATRA_ABSPATH . 'includes/abstracts/abstract-yatra-payment-gateways.php'; |
| 290 |
include_once YATRA_ABSPATH . 'includes/abstracts/abstract-yatra-log-levels.php'; |
| 291 |
include_once YATRA_ABSPATH . 'includes/abstracts/abstract-yatra-log-handler.php'; |
| 292 |
|
| 293 |
|
| 294 |
/** |
| 295 |
* Core Class. |
| 296 |
*/ |
| 297 |
include_once YATRA_ABSPATH . 'includes/class-yatra-modules.php'; |
| 298 |
|
| 299 |
|
| 300 |
include_once YATRA_ABSPATH . 'includes/classes/class-yatra-core-db.php'; |
| 301 |
include_once YATRA_ABSPATH . 'includes/classes/class-yatra-core-coupon.php'; |
| 302 |
include_once YATRA_ABSPATH . 'includes/class-yatra-tour-availability-validation.php'; |
| 303 |
include_once YATRA_ABSPATH . 'includes/settings/class-yatra-tour-settings.php'; |
| 304 |
include_once YATRA_ABSPATH . 'includes/payment-gateways/class-yatra-gateways-core.php'; |
| 305 |
include_once YATRA_ABSPATH . 'includes/functions.php'; |
| 306 |
include_once YATRA_ABSPATH . 'includes/yatra-hooks.php'; |
| 307 |
include_once YATRA_ABSPATH . 'includes/yatra-template-functions.php'; |
| 308 |
include_once YATRA_ABSPATH . 'includes/yatra-template-hooks.php'; |
| 309 |
include_once YATRA_ABSPATH . 'includes/class-yatra-page-templater.php'; |
| 310 |
include_once YATRA_ABSPATH . 'includes/class-yatra-email.php'; |
| 311 |
include_once YATRA_ABSPATH . 'includes/class-yatra-post-types.php'; |
| 312 |
include_once YATRA_ABSPATH . 'includes/class-yatra-metabox.php'; |
| 313 |
include_once YATRA_ABSPATH . 'includes/class-yatra-widgets.php'; |
| 314 |
include_once YATRA_ABSPATH . 'includes/class-yatra-shortcodes.php'; |
| 315 |
include_once YATRA_ABSPATH . 'includes/class-yatra-install.php'; |
| 316 |
include_once YATRA_ABSPATH . 'includes/class-yatra-ajax.php'; |
| 317 |
include_once YATRA_ABSPATH . 'includes/classes/class-yatra-core-tour-availability.php'; |
| 318 |
|
| 319 |
|
| 320 |
// Compatibility |
| 321 |
include_once YATRA_ABSPATH . 'includes/class-yatra-compatibility.php'; |
| 322 |
|
| 323 |
|
| 324 |
if ($this->is_request('admin')) { |
| 325 |
Yatra_Admin::instance(); |
| 326 |
} |
| 327 |
|
| 328 |
if ($this->is_request('frontend')) { |
| 329 |
Yatra_Frontend::instance(); |
| 330 |
} |
| 331 |
|
| 332 |
$this->yatra_error = new WP_Error; |
| 333 |
$this->yatra_messages = new Yatra_Messages; |
| 334 |
$this->admin_notice = new Notices(); |
| 335 |
Initialize::run(); |
| 336 |
|
| 337 |
|
| 338 |
include_once YATRA_ABSPATH . 'includes/admin/admin-bar.php'; |
| 339 |
|
| 340 |
|
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
/** |
| 345 |
* Init Yatra when WordPress Initialises. |
| 346 |
*/ |
| 347 |
public function init() |
| 348 |
{ |
| 349 |
// Before init action. |
| 350 |
do_action('before_yatra_init'); |
| 351 |
|
| 352 |
// Set up localisation. |
| 353 |
$this->load_plugin_textdomain(); |
| 354 |
|
| 355 |
if ($this->is_request('admin')) { |
| 356 |
$this->exporter = new Yatra_Core_Exporter(); |
| 357 |
$this->importer = new Yatra_Core_Importer(); |
| 358 |
|
| 359 |
} |
| 360 |
|
| 361 |
// Classes/actions loaded for the frontend and for ajax requests. |
| 362 |
//if ($this->is_request('frontend')) { |
| 363 |
if (is_null($this->cart) || !$this->cart instanceof Yatra_Cart) { |
| 364 |
|
| 365 |
$this->initialize_cart(); |
| 366 |
} |
| 367 |
|
| 368 |
// Init action. |
| 369 |
do_action('yatra_init'); |
| 370 |
} |
| 371 |
|
| 372 |
/** |
| 373 |
* Load Localisation files. |
| 374 |
* |
| 375 |
* Note: the first-loaded translation file overrides any following ones if the same translation is present. |
| 376 |
* |
| 377 |
* Locales found in: |
| 378 |
* - WP_LANG_DIR/yatra/yatra-LOCALE.mo |
| 379 |
* - WP_LANG_DIR/plugins/yatra-LOCALE.mo |
| 380 |
*/ |
| 381 |
public function load_plugin_textdomain() |
| 382 |
{ |
| 383 |
$locale = is_admin() && function_exists('get_user_locale') ? get_user_locale() : get_locale(); |
| 384 |
$locale = apply_filters('plugin_locale', $locale, 'yatra'); |
| 385 |
unload_textdomain('yatra'); |
| 386 |
|
| 387 |
load_textdomain('yatra', WP_LANG_DIR . '/yatra/yatra-' . $locale . '.mo'); |
| 388 |
load_plugin_textdomain('yatra', false, plugin_basename(dirname(YATRA_FILE)) . '/languages'); |
| 389 |
} |
| 390 |
|
| 391 |
/** |
| 392 |
* Ensure theme and server variable compatibility and setup image sizes. |
| 393 |
*/ |
| 394 |
public function setup_environment() |
| 395 |
{ |
| 396 |
|
| 397 |
$this->define('YATRA_TEMPLATE_PATH', $this->template_path()); |
| 398 |
|
| 399 |
} |
| 400 |
|
| 401 |
/** |
| 402 |
* Get the plugin url. |
| 403 |
* |
| 404 |
* @return string |
| 405 |
*/ |
| 406 |
public function plugin_url() |
| 407 |
{ |
| 408 |
return untrailingslashit(plugins_url('/', YATRA_FILE)); |
| 409 |
} |
| 410 |
|
| 411 |
/** |
| 412 |
* Get the plugin path. |
| 413 |
* |
| 414 |
* @return string |
| 415 |
*/ |
| 416 |
public function plugin_path() |
| 417 |
{ |
| 418 |
return untrailingslashit(plugin_dir_path(YATRA_FILE)); |
| 419 |
} |
| 420 |
|
| 421 |
/** |
| 422 |
* Get the template path. |
| 423 |
* |
| 424 |
* @return string |
| 425 |
*/ |
| 426 |
public function template_path() |
| 427 |
{ |
| 428 |
return apply_filters('yatra_template_path', 'yatra/'); |
| 429 |
} |
| 430 |
|
| 431 |
/** |
| 432 |
* Get the template path. |
| 433 |
* |
| 434 |
* @return string |
| 435 |
*/ |
| 436 |
public function plugin_template_path() |
| 437 |
{ |
| 438 |
return apply_filters('yatra_plugin_template_path', $this->plugin_path() . '/templates/'); |
| 439 |
} |
| 440 |
|
| 441 |
/** |
| 442 |
* Get Ajax URL. |
| 443 |
* |
| 444 |
* @return string |
| 445 |
*/ |
| 446 |
public function ajax_url() |
| 447 |
{ |
| 448 |
return admin_url('admin-ajax.php', 'relative'); |
| 449 |
} |
| 450 |
|
| 451 |
public function log_errors() |
| 452 |
{ |
| 453 |
$error = error_get_last(); |
| 454 |
if ($error && in_array($error['type'], array(E_ERROR, E_PARSE, E_COMPILE_ERROR, E_USER_ERROR, E_RECOVERABLE_ERROR), true)) { |
| 455 |
$logger = yatra_get_logger(); |
| 456 |
$logger->critical( |
| 457 |
/* translators: 1: error message 2: file name and path 3: line number */ |
| 458 |
sprintf(__('%1$s in %2$s on line %3$s', 'yatra'), $error['message'], $error['file'], $error['line']) . PHP_EOL, |
| 459 |
array( |
| 460 |
'source' => 'fatal-errors', |
| 461 |
) |
| 462 |
); |
| 463 |
do_action('yatra_shutdown_error', $error); |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
/** |
| 468 |
* Initialize the customer and cart objects and setup customer saving on shutdown. |
| 469 |
* |
| 470 |
* @return void |
| 471 |
* @since 2.0.0 |
| 472 |
*/ |
| 473 |
public function initialize_cart() |
| 474 |
{ |
| 475 |
|
| 476 |
if (is_null($this->cart) || !$this->cart instanceof Yatra_Cart) { |
| 477 |
$this->cart = new Yatra_Cart(); |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
public function get_log_dir($create_if_not_exists = true) |
| 482 |
{ |
| 483 |
$wp_upload_dir = wp_upload_dir(); |
| 484 |
|
| 485 |
$log_dir = $wp_upload_dir['basedir'] . '/yatra-logs/'; |
| 486 |
|
| 487 |
if (!file_exists(trailingslashit($log_dir) . 'index.html') && $create_if_not_exists) { |
| 488 |
|
| 489 |
$files = array( |
| 490 |
array( |
| 491 |
'base' => $log_dir, |
| 492 |
'file' => 'index.html', |
| 493 |
'content' => '', |
| 494 |
), |
| 495 |
array( |
| 496 |
'base' => $log_dir, |
| 497 |
'file' => '.htaccess', |
| 498 |
'content' => 'deny from all', |
| 499 |
) |
| 500 |
); |
| 501 |
|
| 502 |
$this->create_files($files, $log_dir); |
| 503 |
|
| 504 |
|
| 505 |
} |
| 506 |
return $log_dir; |
| 507 |
} |
| 508 |
|
| 509 |
public function get_upload_dir($create_if_not_exists = true) |
| 510 |
{ |
| 511 |
$wp_upload_dir = wp_upload_dir(); |
| 512 |
|
| 513 |
$upload_dir = $wp_upload_dir['basedir'] . '/yatra/'; |
| 514 |
|
| 515 |
if (!file_exists(trailingslashit($upload_dir) . 'index.html') && $create_if_not_exists) { |
| 516 |
|
| 517 |
$files = array( |
| 518 |
array( |
| 519 |
'base' => $upload_dir, |
| 520 |
'file' => 'index.html', |
| 521 |
'content' => '', |
| 522 |
), |
| 523 |
array( |
| 524 |
'base' => $upload_dir, |
| 525 |
'file' => '.htaccess', |
| 526 |
'content' => 'deny from all', |
| 527 |
) |
| 528 |
); |
| 529 |
|
| 530 |
$this->create_files($files, $upload_dir); |
| 531 |
|
| 532 |
|
| 533 |
} |
| 534 |
return $upload_dir; |
| 535 |
} |
| 536 |
|
| 537 |
private function create_files($files, $base_dir) |
| 538 |
{ |
| 539 |
// Bypass if filesystem is read-only and/or non-standard upload system is used. |
| 540 |
if (apply_filters('yatra_install_skip_create_files', false)) { |
| 541 |
return; |
| 542 |
} |
| 543 |
|
| 544 |
if (file_exists(trailingslashit($base_dir) . 'index.html')) { |
| 545 |
return true; |
| 546 |
} |
| 547 |
$has_created_dir = false; |
| 548 |
|
| 549 |
foreach ($files as $file) { |
| 550 |
if (wp_mkdir_p($file['base']) && !file_exists(trailingslashit($file['base']) . $file['file'])) { |
| 551 |
$file_handle = @fopen(trailingslashit($file['base']) . $file['file'], 'w'); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_system_read_fopen |
| 552 |
if ($file_handle) { |
| 553 |
fwrite($file_handle, $file['content']); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fwrite |
| 554 |
fclose($file_handle); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fclose |
| 555 |
if (!$has_created_dir) { |
| 556 |
$has_created_dir = true; |
| 557 |
} |
| 558 |
} |
| 559 |
} |
| 560 |
} |
| 561 |
if ($has_created_dir) { |
| 562 |
return true; |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
} |
| 567 |
|
| 568 |
} |
| 569 |
|