PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.2.6
Yatra – Travel Booking & Tour Operator Software v3.0.2.6
3.0.15 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 All 83 releases
yatra / app / Shortcodes / LoginShortcode.php

LoginShortcode.php in Yatra – Travel Booking & Tour Operator Software 3.0.2.6, at app/Shortcodes/LoginShortcode.php

207 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Yatra\Shortcodes;
6
7 /**
8 * Login Shortcode
9 *
10 * Displays customer login form
11 */
12 class LoginShortcode extends BaseShortcode
13 {
14 public function __construct()
15 {
16 parent::__construct('yatra_login', [
17 'show_register' => 'yes',
18 'show_forgot_password' => 'yes',
19 'redirect_url' => '',
20 'remember_me' => 'yes',
21 'title' => 'Customer Login',
22 'subtitle' => 'Login to access your bookings and account'
23 ]);
24
25 // Enqueue assets
26 add_action('wp_enqueue_scripts', [$this, 'enqueueAssets']);
27 }
28
29 /**
30 * Enqueue shortcode-specific assets
31 */
32 public function enqueueAssets(): void
33 {
34 // Only enqueue assets if shortcode is present on the page
35 global $post;
36 if (!$post || !has_shortcode($post->post_content, 'yatra_login') && !is_page_template('login-page.php')) {
37 return;
38 }
39
40 // Enqueue CSS
41 wp_enqueue_style(
42 'yatra-login-shortcode',
43 YATRA_PLUGIN_URL . 'assets/css/shortcodes/login-shortcode.css',
44 [],
45 YATRA_VERSION
46 );
47
48 // Enqueue JavaScript
49 wp_enqueue_script(
50 'yatra-login-shortcode',
51 YATRA_PLUGIN_URL . 'assets/js/login-shortcode.js',
52 ['jquery'],
53 YATRA_VERSION,
54 true
55 );
56
57 // Localize script for AJAX with security and debugging
58 wp_localize_script('yatra-login-shortcode', 'yatra_ajax', [
59 'ajax_url' => admin_url('admin-ajax.php'),
60 'nonce' => wp_create_nonce('yatra_login_nonce'),
61 'debug' => defined('WP_DEBUG') && WP_DEBUG,
62 'strings' => [
63 'login_error' => __('Login failed. Please try again.', 'yatra'),
64 'network_error' => __('Network error. Please check your connection.', 'yatra'),
65 'validation_error' => __('Please fill in all required fields.', 'yatra')
66 ]
67 ]);
68 }
69
70 /**
71 * Render the login shortcode content
72 */
73 protected function renderContent(array $atts): string
74 {
75 $atts = shortcode_atts($this->default_attributes, $atts, $this->tag);
76
77 // Sanitize and validate attributes
78 $atts = $this->sanitizeAttributes($atts);
79
80 // Check if user is already logged in
81 if (is_user_logged_in()) {
82 return $this->renderLoggedInMessage($atts);
83 }
84
85 // Set secure redirect URL
86 $redirect_url = $this->getSecureRedirectUrl($atts['redirect_url']);
87
88 // Make variables available to template
89 set_query_var('yatra_login_atts', $atts);
90 set_query_var('yatra_redirect_url', $redirect_url);
91
92 // Load the login template with error handling
93 $template_path = YATRA_PLUGIN_PATH . 'templates/shortcodes/login.php';
94
95 if (!file_exists($template_path)) {
96 return $this->renderFallbackError();
97 }
98
99 ob_start();
100 try {
101 include $template_path;
102 $content = ob_get_clean();
103 } catch (Exception $e) {
104 ob_end_clean();
105 if (defined('WP_DEBUG') && WP_DEBUG) {
106 error_log('Yatra Login Shortcode Error: ' . $e->getMessage());
107 }
108 $content = $this->renderFallbackError();
109 }
110
111 // Clean up query vars
112 set_query_var('yatra_login_atts', null);
113 set_query_var('yatra_redirect_url', null);
114
115 return $content;
116 }
117
118 /**
119 * Render message for logged in users
120 */
121 private function renderLoggedInMessage(array $atts): string
122 {
123 $user = wp_get_current_user();
124 $account_url = home_url('/my-account');
125
126 ob_start();
127 ?>
128 <div class="yatra-login-logged-in">
129 <div class="yatra-logged-in-content">
130 <div class="yatra-logged-in-icon">
131 <?php echo yatra_svg_icon('user', 'yatra-logged-in-icon-svg'); ?>
132 </div>
133 <h3><?php esc_html_e('Already Logged In', 'yatra'); ?></h3>
134 <p>
135 <?php
136 printf(
137 esc_html__('You are logged in as %s.', 'yatra'),
138 '<strong>' . esc_html($user->display_name) . '</strong>'
139 );
140 ?>
141 </p>
142 <div class="yatra-logged-in-actions">
143 <a href="<?php echo esc_url($account_url); ?>" class="yatra-btn yatra-btn-primary">
144 <?php esc_html_e('My Account', 'yatra'); ?>
145 </a>
146 <a href="<?php echo esc_url(wp_logout_url(get_permalink())); ?>" class="yatra-btn yatra-btn-outline">
147 <?php esc_html_e('Logout', 'yatra'); ?>
148 </a>
149 </div>
150 </div>
151 </div>
152 <?php
153 return ob_get_clean();
154 }
155
156 /**
157 * Sanitize and validate shortcode attributes
158 */
159 private function sanitizeAttributes(array $atts): array
160 {
161 return [
162 'show_register' => in_array($atts['show_register'], ['yes', 'no'], true) ? $atts['show_register'] : 'yes',
163 'show_forgot_password' => in_array($atts['show_forgot_password'], ['yes', 'no'], true) ? $atts['show_forgot_password'] : 'yes',
164 'redirect_url' => sanitize_url($atts['redirect_url'] ?? ''),
165 'remember_me' => in_array($atts['remember_me'], ['yes', 'no'], true) ? $atts['remember_me'] : 'yes',
166 'title' => sanitize_text_field($atts['title'] ?? 'Customer Login'),
167 'subtitle' => sanitize_text_field($atts['subtitle'] ?? 'Login to access your bookings and account')
168 ];
169 }
170
171 /**
172 * Get secure redirect URL
173 */
174 private function getSecureRedirectUrl(string $redirect_url): string
175 {
176 if (!empty($redirect_url)) {
177 // Validate URL is safe
178 if (wp_http_validate_url($redirect_url)) {
179 $redirect_host = parse_url($redirect_url, PHP_URL_HOST);
180 $site_host = parse_url(home_url(), PHP_URL_HOST);
181
182 // Only allow redirects to same host
183 if ($redirect_host === $site_host) {
184 return $redirect_url;
185 }
186 }
187 }
188
189 // Fallback to safe defaults
190 return wp_get_referer() ?: home_url('/my-account');
191 }
192
193 /**
194 * Render fallback error message
195 */
196 private function renderFallbackError(): string
197 {
198 ob_start();
199 ?>
200 <div class="yatra-login-error">
201 <p><?php esc_html_e('Login form is currently unavailable. Please try again later.', 'yatra'); ?></p>
202 </div>
203 <?php
204 return ob_get_clean();
205 }
206 }
207