PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.4
Yatra – Travel Booking & Tour Operator Software v3.0.4
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 / app / Shortcodes / LoginShortcode.php

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

208 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 use Yatra\Services\SettingsService;
8
9 /**
10 * Login Shortcode
11 *
12 * Displays customer login form
13 */
14 class LoginShortcode extends BaseShortcode
15 {
16 public function __construct()
17 {
18 parent::__construct('yatra_login', [
19 'show_register' => 'yes',
20 'show_forgot_password' => 'yes',
21 'redirect_url' => '',
22 'remember_me' => 'yes',
23 'title' => 'Customer Login',
24 'subtitle' => 'Login to access your bookings and account'
25 ]);
26
27 // Enqueue assets
28 add_action('wp_enqueue_scripts', [$this, 'enqueueAssets']);
29 }
30
31 /**
32 * Enqueue shortcode-specific assets
33 */
34 public function enqueueAssets(): void
35 {
36 // Only enqueue assets if shortcode is present on the page
37 global $post;
38 if (!$post || !has_shortcode($post->post_content, 'yatra_login') && !is_page_template('login-page.php')) {
39 return;
40 }
41
42 // Enqueue CSS
43 wp_enqueue_style(
44 'yatra-login-shortcode',
45 YATRA_PLUGIN_URL . 'assets/css/shortcodes/login-shortcode.css',
46 [],
47 YATRA_VERSION
48 );
49
50 // Enqueue JavaScript
51 wp_enqueue_script(
52 'yatra-login-shortcode',
53 YATRA_PLUGIN_URL . 'assets/js/login-shortcode.js',
54 ['jquery'],
55 YATRA_VERSION,
56 true
57 );
58
59 // Localize script for AJAX with security and debugging
60 wp_localize_script('yatra-login-shortcode', 'yatra_ajax', [
61 'ajax_url' => admin_url('admin-ajax.php'),
62 'nonce' => wp_create_nonce('yatra_login_nonce'),
63 'debug' => defined('WP_DEBUG') && WP_DEBUG,
64 'strings' => [
65 'login_error' => __('Login failed. Please try again.', 'yatra'),
66 'network_error' => __('Network error. Please check your connection.', 'yatra'),
67 'validation_error' => __('Please fill in all required fields.', 'yatra')
68 ]
69 ]);
70 }
71
72 /**
73 * Render the login shortcode content
74 */
75 protected function renderContent(array $atts): string
76 {
77 $atts = shortcode_atts($this->default_attributes, $atts, $this->tag);
78
79 // Sanitize and validate attributes
80 $atts = $this->sanitizeAttributes($atts);
81
82 // Check if user is already logged in
83 if (is_user_logged_in()) {
84 return $this->renderLoggedInMessage($atts);
85 }
86
87 // Set secure redirect URL
88 $redirect_url = $this->getSecureRedirectUrl($atts['redirect_url']);
89
90 // Make variables available to template
91 set_query_var('yatra_login_atts', $atts);
92 set_query_var('yatra_redirect_url', $redirect_url);
93
94 // Load the login template with error handling
95 $template_path = YATRA_PLUGIN_PATH . 'templates/shortcodes/login.php';
96
97 if (!file_exists($template_path)) {
98 return $this->renderFallbackError();
99 }
100
101 ob_start();
102 try {
103 include $template_path;
104 $content = ob_get_clean();
105 } catch (\Exception $e) {
106 ob_end_clean();
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('/' . SettingsService::getAccountBase());
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 // Default: always send users to the account area after login.
190 // Avoid wp_get_referer() here to prevent redirect loops back to the login form.
191 return home_url('/' . SettingsService::getAccountBase());
192 }
193
194 /**
195 * Render fallback error message
196 */
197 private function renderFallbackError(): string
198 {
199 ob_start();
200 ?>
201 <div class="yatra-login-error">
202 <p><?php esc_html_e('Login form is currently unavailable. Please try again later.', 'yatra'); ?></p>
203 </div>
204 <?php
205 return ob_get_clean();
206 }
207 }
208