| 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 |
* The single instance of the class. |
| 35 |
* |
| 36 |
* @var Yatra |
| 37 |
* @since 1.0.0 |
| 38 |
*/ |
| 39 |
protected static $_instance = null; |
| 40 |
|
| 41 |
|
| 42 |
/** |
| 43 |
* Main Yatra Instance. |
| 44 |
* |
| 45 |
* Ensures only one instance of Yatra is loaded or can be loaded. |
| 46 |
* |
| 47 |
* @since 1.0.0 |
| 48 |
* @static |
| 49 |
* @see mb_aec_addons() |
| 50 |
* @return Yatra - Main instance. |
| 51 |
*/ |
| 52 |
public static function instance() |
| 53 |
{ |
| 54 |
if (is_null(self::$_instance)) { |
| 55 |
self::$_instance = new self(); |
| 56 |
} |
| 57 |
return self::$_instance; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Cloning is forbidden. |
| 62 |
* |
| 63 |
* @since 1.0.0 |
| 64 |
*/ |
| 65 |
public function __clone() |
| 66 |
{ |
| 67 |
_doing_it_wrong(__FUNCTION__, __('Cloning is forbidden.', 'yatra'), '1.0.0'); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Unserializing instances of this class is forbidden. |
| 72 |
* |
| 73 |
* @since 1.0.0 |
| 74 |
*/ |
| 75 |
public function __wakeup() |
| 76 |
{ |
| 77 |
_doing_it_wrong(__FUNCTION__, __('Unserializing instances of this class is forbidden.', 'yatra'), '1.0.0'); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Auto-load in-accessible properties on demand. |
| 82 |
* |
| 83 |
* @param mixed $key Key name. |
| 84 |
* @return mixed |
| 85 |
*/ |
| 86 |
public function __get($key) |
| 87 |
{ |
| 88 |
if (in_array($key, array(''), true)) { |
| 89 |
return $this->$key(); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Yatra Constructor. |
| 95 |
*/ |
| 96 |
public function __construct() |
| 97 |
{ |
| 98 |
$this->define_constants(); |
| 99 |
$this->includes(); |
| 100 |
$this->init_hooks(); |
| 101 |
do_action('yatra_loaded'); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Hook into actions and filters. |
| 106 |
* |
| 107 |
* @since 1.0.0 |
| 108 |
*/ |
| 109 |
private function init_hooks() |
| 110 |
{ |
| 111 |
|
| 112 |
register_activation_hook(YATRA_FILE, array('Yatra_Install', 'install')); |
| 113 |
|
| 114 |
add_action('init', array($this, 'init'), 0); |
| 115 |
add_action('init', array('Yatra_Shortcodes', 'init')); |
| 116 |
|
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Define Yatra Constants. |
| 122 |
*/ |
| 123 |
private function define_constants() |
| 124 |
{ |
| 125 |
|
| 126 |
$this->define('YATRA_ABSPATH', dirname(YATRA_FILE) . '/'); |
| 127 |
$this->define('YATRA_BASENAME', plugin_basename(YATRA_FILE)); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Define constant if not already set. |
| 132 |
* |
| 133 |
* @param string $name Constant name. |
| 134 |
* @param string|bool $value Constant value. |
| 135 |
*/ |
| 136 |
private function define($name, $value) |
| 137 |
{ |
| 138 |
if (!defined($name)) { |
| 139 |
define($name, $value); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* What type of request is this? |
| 145 |
* |
| 146 |
* @param string $type admin, ajax, cron or frontend. |
| 147 |
* @return bool |
| 148 |
*/ |
| 149 |
private function is_request($type) |
| 150 |
{ |
| 151 |
switch ($type) { |
| 152 |
case 'admin': |
| 153 |
return is_admin(); |
| 154 |
case 'ajax': |
| 155 |
return defined('DOING_AJAX'); |
| 156 |
case 'cron': |
| 157 |
return defined('DOING_CRON'); |
| 158 |
case 'frontend': |
| 159 |
return (!is_admin() || defined('DOING_AJAX')) && !defined('DOING_CRON') && !defined('REST_REQUEST'); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Include required core files used in admin and on the frontend. |
| 165 |
*/ |
| 166 |
public function includes() |
| 167 |
{ |
| 168 |
|
| 169 |
/** |
| 170 |
* Class autoloader. |
| 171 |
*/ |
| 172 |
include_once YATRA_ABSPATH . 'includes/class-yatra-autoloader.php'; |
| 173 |
include_once YATRA_ABSPATH . 'includes/functions.php'; |
| 174 |
include_once YATRA_ABSPATH . 'includes/yatra-hooks.php'; |
| 175 |
include_once YATRA_ABSPATH . 'includes/class-yatra-page-templater.php'; |
| 176 |
include_once YATRA_ABSPATH . 'includes/class-yatra-custom-post-type.php'; |
| 177 |
include_once YATRA_ABSPATH . 'includes/class-yatra-taxonomy.php'; |
| 178 |
include_once YATRA_ABSPATH . 'includes/class-yatra-metabox.php'; |
| 179 |
include_once YATRA_ABSPATH . 'includes/class-yatra-shortcodes.php'; |
| 180 |
include_once YATRA_ABSPATH . 'includes/class-yatra-ajax.php'; |
| 181 |
|
| 182 |
|
| 183 |
// Compatibility |
| 184 |
include_once YATRA_ABSPATH . 'includes/class-yatra-compatibility.php'; |
| 185 |
|
| 186 |
|
| 187 |
if ($this->is_request('admin')) { |
| 188 |
Yatra_Admin::instance(); |
| 189 |
} |
| 190 |
|
| 191 |
if ($this->is_request('frontend')) { |
| 192 |
Yatra_Frontend::instance(); |
| 193 |
} |
| 194 |
|
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
/** |
| 199 |
* Init Yatra when WordPress Initialises. |
| 200 |
*/ |
| 201 |
public function init() |
| 202 |
{ |
| 203 |
// Before init action. |
| 204 |
do_action('before_yatra_init'); |
| 205 |
|
| 206 |
// Set up localisation. |
| 207 |
$this->load_plugin_textdomain(); |
| 208 |
|
| 209 |
|
| 210 |
// Classes/actions loaded for the frontend and for ajax requests. |
| 211 |
if ($this->is_request('frontend')) { |
| 212 |
$this->initialize_cart(); |
| 213 |
} |
| 214 |
|
| 215 |
// Init action. |
| 216 |
do_action('yatra_init'); |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Load Localisation files. |
| 221 |
* |
| 222 |
* Note: the first-loaded translation file overrides any following ones if the same translation is present. |
| 223 |
* |
| 224 |
* Locales found in: |
| 225 |
* - WP_LANG_DIR/yatra/yatra-LOCALE.mo |
| 226 |
* - WP_LANG_DIR/plugins/yatra-LOCALE.mo |
| 227 |
*/ |
| 228 |
public function load_plugin_textdomain() |
| 229 |
{ |
| 230 |
$locale = is_admin() && function_exists('get_user_locale') ? get_user_locale() : get_locale(); |
| 231 |
$locale = apply_filters('plugin_locale', $locale, 'yatra'); |
| 232 |
unload_textdomain('yatra'); |
| 233 |
load_textdomain('yatra', WP_LANG_DIR . '/yatra/yatra-' . $locale . '.mo'); |
| 234 |
load_plugin_textdomain('yatra', false, plugin_basename(dirname(YATRA_FILE)) . '/i18n/languages'); |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Ensure theme and server variable compatibility and setup image sizes. |
| 239 |
*/ |
| 240 |
public function setup_environment() |
| 241 |
{ |
| 242 |
|
| 243 |
$this->define('YATRA_TEMPLATE_PATH', $this->template_path()); |
| 244 |
|
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Get the plugin url. |
| 249 |
* |
| 250 |
* @return string |
| 251 |
*/ |
| 252 |
public function plugin_url() |
| 253 |
{ |
| 254 |
return untrailingslashit(plugins_url('/', YATRA_FILE)); |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Get the plugin path. |
| 259 |
* |
| 260 |
* @return string |
| 261 |
*/ |
| 262 |
public function plugin_path() |
| 263 |
{ |
| 264 |
return untrailingslashit(plugin_dir_path(YATRA_FILE)); |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Get the template path. |
| 269 |
* |
| 270 |
* @return string |
| 271 |
*/ |
| 272 |
public function template_path() |
| 273 |
{ |
| 274 |
return apply_filters('yatra_template_path', 'yatra/'); |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Get the template path. |
| 279 |
* |
| 280 |
* @return string |
| 281 |
*/ |
| 282 |
public function plugin_template_path() |
| 283 |
{ |
| 284 |
return apply_filters('yatra_plugin_template_path', $this->plugin_path() . '/templates/'); |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* Get Ajax URL. |
| 289 |
* |
| 290 |
* @return string |
| 291 |
*/ |
| 292 |
public function ajax_url() |
| 293 |
{ |
| 294 |
return admin_url('admin-ajax.php', 'relative'); |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Initialize the customer and cart objects and setup customer saving on shutdown. |
| 299 |
* |
| 300 |
* @since 2.0.0 |
| 301 |
* @return void |
| 302 |
*/ |
| 303 |
public function initialize_cart() |
| 304 |
{ |
| 305 |
|
| 306 |
if (is_null($this->cart) || !$this->cart instanceof Yatra_Cart) { |
| 307 |
$this->cart = new Yatra_Cart(); |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
} |
| 313 |
|