PluginProbe
Search Atlas SEO – OTTO AI SEO Automation for WordPress / trunk
Search Atlas SEO – OTTO AI SEO Automation for WordPress vtrunk
2.6.26 2.6.25 2.6.24 2.6.23 2.6.22 2.6.21 2.6.20 2.6.19 2.6.18 2.6.17 2.6.16 2.6.15 2.6.14 2.6.13 2.6.12 2.6.11 2.6.10 2.6.9 2.6.8 2.6.7 2.6.6 2.6.5 2.6.4 2.6.3 2.5.23 All 138 releases
metasync / includes / class-metasync-session-helper.php

class-metasync-session-helper.php in Search Atlas SEO – OTTO AI SEO Automation for WordPress trunk, at includes/class-metasync-session-helper.php

239 lines 7.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Session Helper for MetaSync Plugin
4 *
5 * Provides safe session handling with fallback directory support
6 * Centralizes session management to avoid code duplication
7 *
8 * @package MetaSync
9 * @subpackage MetaSync/includes
10 * @since 1.0.0
11 * @deprecated 2.5.12 Use Metasync_Auth_Manager instead for authentication
12 *
13 * DEPRECATION NOTICE:
14 * This class is deprecated and should not be used for new implementations.
15 * For authentication purposes, use Metasync_Auth_Manager which provides:
16 * - Better compatibility with all hosting environments (no PHP session dependencies)
17 * - WordPress-native solutions (transients + user meta)
18 * - Support for Redis, Memcached, and object caching
19 * - OOP design with clear API
20 *
21 * @see Metasync_Auth_Manager For authentication and access control
22 */
23
24 // Prevent direct access
25 if (!defined('ABSPATH')) {
26 exit;
27 }
28
29 class Metasync_Session_Helper {
30
31 /**
32 * Custom session directory path
33 *
34 * @var string|null
35 */
36 private static $custom_session_path = null;
37
38 /**
39 * Safely start a session with error handling
40 * Fixes session directory permission issues by using custom WordPress upload directory
41 *
42 * @return bool True if session started successfully, false otherwise
43 */
44 public static function safe_start() {
45 // Check if session is already started
46 if (session_status() == PHP_SESSION_ACTIVE) {
47 return true;
48 }
49
50 // Check if we have a session ID already
51 if (session_id()) {
52 return true;
53 }
54
55 // Don't start sessions during REST API requests, AJAX requests, or cron
56 if (self::should_skip_session()) {
57 return false;
58 }
59
60 // Set up custom session path
61 self::setup_custom_session_path();
62
63 // Suppress errors and try to start session
64 try {
65 @session_start();
66 return session_status() == PHP_SESSION_ACTIVE;
67 } catch (Exception $e) {
68 // Log error but don't break the site
69 error_log('MetaSync: Failed to start session: ' . $e->getMessage());
70 return false;
71 }
72 }
73
74 /**
75 * Check if session should be skipped based on request context
76 *
77 * @return bool True if session should be skipped, false otherwise
78 */
79 private static function should_skip_session() {
80 return (defined('REST_REQUEST') && REST_REQUEST) ||
81 (defined('DOING_AJAX') && DOING_AJAX) ||
82 (defined('DOING_CRON') && DOING_CRON);
83 }
84
85 /**
86 * Set up custom session save path
87 * Creates directory if it doesn't exist and configures PHP to use it
88 * Skips path modification if a custom session handler (like Redis) is configured
89 *
90 * @return bool True if custom path was set successfully, false otherwise
91 */
92 private static function setup_custom_session_path() {
93 // Check if a custom session handler is configured (Redis, Memcached, etc.)
94 $session_handler = ini_get('session.save_handler');
95
96 // If using a custom handler (not 'files'), don't override session.save_path
97 // Custom handlers like Redis use session.save_path for connection parameters, not file paths
98 // This prevents "Failed to read session data: redis" errors
99 if ($session_handler !== 'files' && $session_handler !== '' && $session_handler !== false) {
100 // Custom handler detected (Redis, Memcached, etc.) - don't modify session.save_path
101 return false;
102 }
103
104 // Get custom session path (cached after first call)
105 if (self::$custom_session_path === null) {
106 $upload_dir = wp_upload_dir();
107 self::$custom_session_path = $upload_dir['basedir'] . '/metasync-sessions';
108 }
109
110 // Create custom session directory if it doesn't exist
111 if (!file_exists(self::$custom_session_path)) {
112 @mkdir(self::$custom_session_path, 0755, true);
113 }
114
115 // Only set custom session save path if using default 'files' handler
116 if (is_dir(self::$custom_session_path)) {
117 @ini_set('session.save_path', self::$custom_session_path);
118 return true;
119 }
120
121 return false;
122 }
123
124 /**
125 * Close session and write data
126 * Safe wrapper for session_write_close()
127 *
128 * @return bool True if session was closed, false if no active session
129 */
130 public static function close() {
131 if (session_status() == PHP_SESSION_ACTIVE) {
132 session_write_close();
133 return true;
134 }
135 return false;
136 }
137
138 /**
139 * Check if session is active
140 *
141 * @return bool True if session is active, false otherwise
142 */
143 public static function is_active() {
144 return session_status() == PHP_SESSION_ACTIVE;
145 }
146
147 /**
148 * Get session value
149 *
150 * @param string $key Session key
151 * @param mixed $default Default value if key doesn't exist
152 * @return mixed Session value or default
153 */
154 public static function get($key, $default = null) {
155 if (self::is_active() && isset($_SESSION[$key])) {
156 return $_SESSION[$key];
157 }
158 return $default;
159 }
160
161 /**
162 * Set session value
163 * Automatically starts session if not active
164 *
165 * @param string $key Session key
166 * @param mixed $value Session value
167 * @return bool True if value was set, false otherwise
168 */
169 public static function set($key, $value) {
170 if (!self::is_active()) {
171 self::safe_start();
172 }
173
174 if (self::is_active()) {
175 $_SESSION[$key] = $value;
176 return true;
177 }
178
179 return false;
180 }
181
182 /**
183 * Delete session value
184 *
185 * @param string $key Session key
186 * @return bool True if value was deleted, false otherwise
187 */
188 public static function delete($key) {
189 if (self::is_active() && isset($_SESSION[$key])) {
190 unset($_SESSION[$key]);
191 return true;
192 }
193 return false;
194 }
195
196 /**
197 * Destroy session
198 *
199 * @return bool True if session was destroyed, false otherwise
200 */
201 public static function destroy() {
202 if (self::is_active()) {
203 session_destroy();
204 return true;
205 }
206 return false;
207 }
208
209 /**
210 * Get custom session directory path
211 *
212 * @return string|null Custom session path or null if not set
213 */
214 public static function get_session_path() {
215 if (self::$custom_session_path === null) {
216 $upload_dir = wp_upload_dir();
217 self::$custom_session_path = $upload_dir['basedir'] . '/metasync-sessions';
218 }
219 return self::$custom_session_path;
220 }
221
222 /**
223 * Check if custom session directory exists and is writable
224 *
225 * @return array Status information about session directory
226 */
227 public static function get_status() {
228 $path = self::get_session_path();
229 return array(
230 'custom_path' => $path,
231 'exists' => is_dir($path),
232 'writable' => is_writable($path),
233 'session_active' => self::is_active(),
234 'session_id' => self::is_active() ? session_id() : null,
235 'should_skip' => self::should_skip_session()
236 );
237 }
238 }
239