| 1 |
<?php |
| 2 |
/** |
| 3 |
* Yatra admin setup |
| 4 |
* |
| 5 |
* @package Yatra |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
defined('ABSPATH') || exit; |
| 10 |
|
| 11 |
/** |
| 12 |
* Main Yatra_Admin Class. |
| 13 |
* |
| 14 |
* @class Yatra |
| 15 |
*/ |
| 16 |
final class Yatra_Admin |
| 17 |
{ |
| 18 |
|
| 19 |
/** |
| 20 |
* The single instance of the class. |
| 21 |
* |
| 22 |
* @var Yatra_Admin |
| 23 |
* @since 1.0.0 |
| 24 |
*/ |
| 25 |
protected static $_instance = null; |
| 26 |
|
| 27 |
|
| 28 |
/** |
| 29 |
* Main Yatra_Admin Instance. |
| 30 |
* |
| 31 |
* Ensures only one instance of Yatra_Admin is loaded or can be loaded. |
| 32 |
* |
| 33 |
* @return Yatra_Admin - Main instance. |
| 34 |
* @since 1.0.0 |
| 35 |
* @static |
| 36 |
*/ |
| 37 |
public static function instance() |
| 38 |
{ |
| 39 |
if (is_null(self::$_instance)) { |
| 40 |
self::$_instance = new self(); |
| 41 |
} |
| 42 |
return self::$_instance; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
/** |
| 47 |
* Yatra Constructor. |
| 48 |
*/ |
| 49 |
public function __construct() |
| 50 |
{ |
| 51 |
$this->includes(); |
| 52 |
$this->init_hooks(); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Hook into actions and filters. |
| 57 |
* |
| 58 |
* @since 1.0.0 |
| 59 |
*/ |
| 60 |
private function init_hooks() |
| 61 |
{ |
| 62 |
|
| 63 |
|
| 64 |
add_action('init', array($this, 'setup_wizard')); |
| 65 |
add_action('admin_init', array($this, 'admin_redirects')); |
| 66 |
add_action('admin_menu', array($this, 'yatra_submenu')); |
| 67 |
add_action('admin_menu', array($this, 'yatra_tour_submenu')); |
| 68 |
add_action('admin_notices', array($this, 'promotional_offer')); |
| 69 |
add_filter('plugin_action_links_' . plugin_basename(YATRA_PLUGIN_DIR . 'yatra.php'), [$this, 'settings_link'], 10, 4); |
| 70 |
|
| 71 |
add_filter('parent_file', array($this, 'menu_parent_fix')); |
| 72 |
|
| 73 |
add_filter('yatra_admin_main_submenu', array($this, 'submenu')); |
| 74 |
|
| 75 |
|
| 76 |
} |
| 77 |
|
| 78 |
public function menu_parent_fix($parent_file) |
| 79 |
{ |
| 80 |
global $submenu_file, $current_screen; |
| 81 |
|
| 82 |
$menu_post_types = array('yatra-booking', 'yatra-coupons', 'yatra-customers'); |
| 83 |
|
| 84 |
if (in_array($current_screen->post_type, $menu_post_types)) { |
| 85 |
|
| 86 |
$submenu_file = 'edit.php?post_type=' . $current_screen->post_type; |
| 87 |
|
| 88 |
$parent_file = YATRA_ADMIN_MENU_SLUG; |
| 89 |
} |
| 90 |
return $parent_file; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
public function promotional_offer() |
| 95 |
{ |
| 96 |
return; |
| 97 |
if (!current_user_can('manage_yatra')) { |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
$offer_key = 'yatra_black_friday_cyber_monday_promo'; |
| 102 |
|
| 103 |
$offer_start_date = strtotime('2022-10-28 00:00:01'); |
| 104 |
|
| 105 |
$offer_end_date = strtotime('2022-12-05 23:59:00'); |
| 106 |
|
| 107 |
$hide_notice = get_option($offer_key, 'show'); |
| 108 |
|
| 109 |
if ('hide' == $hide_notice) { |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
if ($offer_start_date < current_time('timestamp') && current_time('timestamp') < $offer_end_date) { |
| 114 |
|
| 115 |
yatra_load_admin_template('notices.promo'); |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
public function admin_redirects() |
| 122 |
{ |
| 123 |
|
| 124 |
if (!get_transient('_yatra_activation_redirect')) { |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
delete_transient('_yatra_activation_redirect'); |
| 129 |
|
| 130 |
if ((!empty($_GET['page']) && in_array($_GET['page'], array('yatra-setup'))) || is_network_admin() || isset($_GET['activate-multi']) || !current_user_can('manage_yatra')) { |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
// If it's the first time |
| 135 |
if (get_option('yatra_setup_wizard_ran') != '1') { |
| 136 |
wp_safe_redirect(admin_url('index.php?page=yatra-setup')); |
| 137 |
exit; |
| 138 |
|
| 139 |
// Otherwise, the welcome page |
| 140 |
} else { |
| 141 |
wp_safe_redirect(admin_url('edit.php?post_type=tour')); |
| 142 |
exit; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Include required files |
| 148 |
* |
| 149 |
* @return void |
| 150 |
*/ |
| 151 |
public function setup_wizard() |
| 152 |
{ |
| 153 |
// Setup/welcome |
| 154 |
if (!empty($_GET['page'])) { |
| 155 |
|
| 156 |
if ('yatra-setup' == $_GET['page']) { |
| 157 |
include_once YATRA_ABSPATH . 'includes/admin/setup/class-yatra-setup-wizard.php'; |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
public function submenu($submenu) |
| 163 |
{ |
| 164 |
|
| 165 |
|
| 166 |
$submenu[] = array( |
| 167 |
'parent_slug' => YATRA_ADMIN_MENU_SLUG, |
| 168 |
'page_title' => __('All Bookings', 'yatra'), |
| 169 |
'menu_title' => __('Bookings', 'yatra'), |
| 170 |
'capability' => 'manage_yatra', |
| 171 |
'menu_slug' => 'edit.php?post_type=yatra-booking', |
| 172 |
'callback' => '', |
| 173 |
'position' => 10, |
| 174 |
); |
| 175 |
|
| 176 |
$submenu[] = array( |
| 177 |
'parent_slug' => YATRA_ADMIN_MENU_SLUG, |
| 178 |
'page_title' => __('All Customers', 'yatra'), |
| 179 |
'menu_title' => __('Customers', 'yatra'), |
| 180 |
'capability' => 'manage_yatra', |
| 181 |
'menu_slug' => 'edit.php?post_type=yatra-customers', |
| 182 |
'callback' => '', |
| 183 |
'position' => 15, |
| 184 |
); |
| 185 |
|
| 186 |
|
| 187 |
$submenu[] = array( |
| 188 |
'parent_slug' => YATRA_ADMIN_MENU_SLUG, |
| 189 |
'page_title' => __('Coupons', 'yatra'), |
| 190 |
'menu_title' => __('Coupons', 'yatra'), |
| 191 |
'capability' => 'manage_yatra', |
| 192 |
'menu_slug' => 'edit.php?post_type=yatra-coupons', |
| 193 |
'callback' => '', |
| 194 |
'position' => 20, |
| 195 |
); |
| 196 |
|
| 197 |
$submenu[] = array( |
| 198 |
'parent_slug' => YATRA_ADMIN_MENU_SLUG, |
| 199 |
'page_title' => __('Settings', 'yatra'), |
| 200 |
'menu_title' => __('Settings', 'yatra'), |
| 201 |
'capability' => 'manage_yatra', |
| 202 |
'menu_slug' => 'yatra-settings', |
| 203 |
'callback' => array($this, 'settings'), |
| 204 |
'position' => 25, |
| 205 |
'load_action' => array($this, 'settings_page_init') |
| 206 |
); |
| 207 |
|
| 208 |
$submenu[] = array( |
| 209 |
'parent_slug' => YATRA_ADMIN_MENU_SLUG, |
| 210 |
'page_title' => esc_html__('Yatra Addons', 'yatra'), |
| 211 |
'menu_title' => '<span style="color:#28d01d">' . esc_html__('Addons', 'yatra') . '</span>', |
| 212 |
'capability' => 'manage_yatra', |
| 213 |
'menu_slug' => 'yatra-addons', |
| 214 |
'callback' => array($this, 'addon_page'), |
| 215 |
'position' => 30, |
| 216 |
); |
| 217 |
|
| 218 |
|
| 219 |
if (count(yatra_get_premium_addons()) < 1) { |
| 220 |
|
| 221 |
$submenu[] = array( |
| 222 |
'parent_slug' => YATRA_ADMIN_MENU_SLUG, |
| 223 |
'page_title' => esc_html__('Upgrade to Pro', 'yatra'), |
| 224 |
'menu_title' => '<span style="color:#e27730">' . esc_html__('Upgrade to Pro', 'yatra') . '</span>', |
| 225 |
'capability' => 'manage_yatra', |
| 226 |
'menu_slug' => esc_url('https://wpyatra.com/pricing/?utm_campaign=freeplugin&utm_medium=admin-menu&utm_source=WordPress&utm_content=Upgrade+to+Pro'), |
| 227 |
'callback' => '', |
| 228 |
'position' => 35, |
| 229 |
); |
| 230 |
} |
| 231 |
return $submenu; |
| 232 |
} |
| 233 |
|
| 234 |
public function yatra_submenu() |
| 235 |
{ |
| 236 |
|
| 237 |
$default_submenu_args = array( |
| 238 |
'parent_slug' => '', |
| 239 |
'page_title' => '', |
| 240 |
'menu_title' => '', |
| 241 |
'capability' => 'manage_yatra', |
| 242 |
'menu_slug' => '', |
| 243 |
'callback' => '', |
| 244 |
'position' => null, |
| 245 |
'load_action' => '', |
| 246 |
); |
| 247 |
|
| 248 |
$submenu_configurations = apply_filters('yatra_admin_main_submenu', array()); |
| 249 |
$submenu_columns = array_column($submenu_configurations, "position"); |
| 250 |
array_multisort($submenu_columns, SORT_ASC, $submenu_configurations); |
| 251 |
foreach ($submenu_configurations as $configuration) { |
| 252 |
|
| 253 |
$configuration = wp_parse_args($configuration, $default_submenu_args); |
| 254 |
|
| 255 |
$hookname = add_submenu_page( |
| 256 |
$configuration['parent_slug'], |
| 257 |
$configuration['page_title'], |
| 258 |
$configuration['menu_title'], |
| 259 |
$configuration['capability'], |
| 260 |
$configuration['menu_slug'], |
| 261 |
$configuration['callback'], |
| 262 |
$configuration['position'] |
| 263 |
); |
| 264 |
if ($configuration['load_action'] !== '') { |
| 265 |
|
| 266 |
add_action('load-' . $hookname, $configuration['load_action']); |
| 267 |
|
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
} |
| 273 |
|
| 274 |
public function yatra_tour_submenu() |
| 275 |
{ |
| 276 |
$availability_page = add_submenu_page( |
| 277 |
YATRA_TOUR_ADMIN_MENU_SLUG, |
| 278 |
'Availability', |
| 279 |
'Availability', |
| 280 |
'manage_yatra', |
| 281 |
'yatra-availability', |
| 282 |
array($this, 'availability'), |
| 283 |
500 |
| 284 |
); |
| 285 |
add_action('load-' . $availability_page, array($this, 'availability_page_init')); |
| 286 |
} |
| 287 |
|
| 288 |
public function settings() |
| 289 |
{ |
| 290 |
Yatra_Admin_Settings::output(); |
| 291 |
|
| 292 |
} |
| 293 |
|
| 294 |
public function settings_page_init() |
| 295 |
{ |
| 296 |
global $current_tab, $current_section; |
| 297 |
|
| 298 |
// Include settings pages. |
| 299 |
Yatra_Admin_Settings::get_settings_pages(); |
| 300 |
|
| 301 |
// Get current tab/section. |
| 302 |
$current_tab = empty($_GET['tab']) ? 'general' : sanitize_title(wp_unslash($_GET['tab'])); // WPCS: input var okay, CSRF ok. |
| 303 |
$current_section = empty($_REQUEST['section']) ? '' : sanitize_title(wp_unslash($_REQUEST['section'])); // WPCS: input var okay, CSRF ok. |
| 304 |
|
| 305 |
// Save settings if data has been posted. |
| 306 |
if ('' !== $current_section && apply_filters("yatra_save_settings_{$current_tab}_{$current_section}", !empty($_POST['save']))) { // WPCS: input var okay, CSRF ok. |
| 307 |
Yatra_Admin_Settings::save(); |
| 308 |
} elseif ('' === $current_section && apply_filters("yatra_save_settings_{$current_tab}", !empty($_POST['save']))) { // WPCS: input var okay, CSRF ok. |
| 309 |
Yatra_Admin_Settings::save(); |
| 310 |
} |
| 311 |
|
| 312 |
// Add any posted messages. |
| 313 |
if (!empty($_GET['yatra_error'])) { // WPCS: input var okay, CSRF ok. |
| 314 |
Yatra_Admin_Settings::add_error(wp_kses_post(wp_unslash($_GET['yatra_error']))); // WPCS: input var okay, CSRF ok. |
| 315 |
} |
| 316 |
|
| 317 |
if (!empty($_GET['yatra_message'])) { // WPCS: input var okay, CSRF ok. |
| 318 |
Yatra_Admin_Settings::add_message(wp_kses_post(wp_unslash($_GET['yatra_message']))); // WPCS: input var okay, CSRF ok. |
| 319 |
} |
| 320 |
|
| 321 |
do_action('yatra_settings_page_init'); |
| 322 |
|
| 323 |
|
| 324 |
} |
| 325 |
|
| 326 |
public function availability() |
| 327 |
{ |
| 328 |
do_action('yatra_availability_page_output'); |
| 329 |
|
| 330 |
|
| 331 |
} |
| 332 |
|
| 333 |
public function addon_page() |
| 334 |
{ |
| 335 |
do_action('yatra_admin_addon_page_output'); |
| 336 |
} |
| 337 |
|
| 338 |
public function availability_page_init() |
| 339 |
{ |
| 340 |
|
| 341 |
do_action('yatra_availability_page_init'); |
| 342 |
|
| 343 |
|
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
/** |
| 348 |
* Include required core files used in admin. |
| 349 |
*/ |
| 350 |
public function includes() |
| 351 |
{ |
| 352 |
include_once YATRA_ABSPATH . 'includes/classes/class-yatra-core-exporter.php'; |
| 353 |
include_once YATRA_ABSPATH . 'includes/classes/class-yatra-core-importer.php'; |
| 354 |
include_once YATRA_ABSPATH . 'includes/admin/yatra-admin-functions.php'; |
| 355 |
include_once YATRA_ABSPATH . 'includes/admin/dashboard/class-mantrabrain-admin-dashboard.php'; |
| 356 |
include_once YATRA_ABSPATH . 'includes/admin/class-yatra-admin-form-handler.php'; |
| 357 |
include_once YATRA_ABSPATH . 'includes/admin/class-yatra-admin-tour-enquiries.php'; |
| 358 |
include_once YATRA_ABSPATH . 'includes/admin/class-yatra-admin-export-import.php'; |
| 359 |
include_once YATRA_ABSPATH . 'includes/admin/class-yatra-admin-license-manager.php'; |
| 360 |
include_once YATRA_ABSPATH . 'includes/admin/class-yatra-admin-assets.php'; |
| 361 |
include_once YATRA_ABSPATH . 'includes/admin/class-yatra-admin-post-types.php'; |
| 362 |
include_once YATRA_ABSPATH . 'includes/admin/class-yatra-admin-permalinks.php'; |
| 363 |
include_once YATRA_ABSPATH . 'includes/admin/class-yatra-admin-review.php'; |
| 364 |
include_once YATRA_ABSPATH . 'includes/admin/class-yatra-admin-addons.php'; |
| 365 |
|
| 366 |
|
| 367 |
} |
| 368 |
|
| 369 |
public function settings_link($links, $plugin_file, $plugin_data, $context) |
| 370 |
{ |
| 371 |
|
| 372 |
$custom['pro'] = sprintf( |
| 373 |
'<a href="%1$s" aria-label="%2$s" target="_blank" rel="noopener noreferrer" |
| 374 |
style="color: #00a32a; font-weight: 700;" |
| 375 |
onmouseover="this.style.color=\'#008a20\';" |
| 376 |
onmouseout="this.style.color=\'#00a32a\';" |
| 377 |
>%3$s</a>', |
| 378 |
esc_url( |
| 379 |
add_query_arg( |
| 380 |
[ |
| 381 |
'utm_content' => 'Get+Yatra+Premium', |
| 382 |
'utm_campaign' => 'freeplugin', |
| 383 |
'utm_medium' => 'all-plugins', |
| 384 |
'utm_source' => 'WordPress', |
| 385 |
], |
| 386 |
'https://wpyatra.com/pricing/' |
| 387 |
) |
| 388 |
), |
| 389 |
esc_attr__('Get Yatra Pro', 'yatra'), |
| 390 |
esc_html__('Get Yatra Pro', 'yatra') |
| 391 |
); |
| 392 |
|
| 393 |
$custom['settings'] = sprintf( |
| 394 |
'<a href="%s" aria-label="%s">%s</a>', |
| 395 |
esc_url( |
| 396 |
add_query_arg( |
| 397 |
['page' => 'yatra-settings'], |
| 398 |
admin_url('admin.php') |
| 399 |
) |
| 400 |
), |
| 401 |
esc_attr__('Go to Yatra Settings page', 'yatra'), |
| 402 |
esc_html__('Settings', 'yatra') |
| 403 |
); |
| 404 |
|
| 405 |
$custom['docs'] = sprintf( |
| 406 |
'<a href="%1$s" aria-label="%2$s" target="_blank" rel="noopener noreferrer">%3$s</a>', |
| 407 |
esc_url( |
| 408 |
add_query_arg( |
| 409 |
[ |
| 410 |
'utm_content' => 'Documentation', |
| 411 |
'utm_campaign' => 'freeplugin', |
| 412 |
'utm_medium' => 'all-plugins', |
| 413 |
'utm_source' => 'WordPress', |
| 414 |
], |
| 415 |
'https://wpyatra.com/docs/' |
| 416 |
) |
| 417 |
), |
| 418 |
esc_attr__('Read the documentation', 'yatra'), |
| 419 |
esc_html__('Docs', 'yatra') |
| 420 |
); |
| 421 |
if (count(yatra_get_premium_addons()) > 0) { |
| 422 |
unset($custom['pro']); |
| 423 |
} |
| 424 |
|
| 425 |
return array_merge($custom, (array)$links); |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
} |
| 430 |
|