Changeset 2292976
- Timestamp:
- 04/27/2020 08:14:36 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
virtual-queue/trunk/public/class-virtual-queue-public.php
r2278061 r2292976 21 21 * @author Alex <[email protected]> 22 22 */ 23 class Virtual_Queue_Public { 24 25 /** 26 * The ID of this plugin. 27 * 28 * @since 1.0.0 29 * @access private 30 * @var string $plugin_name The ID of this plugin. 31 */ 32 private $plugin_name; 33 34 /** 35 * The version of this plugin. 36 * 37 * @since 1.0.0 38 * @access private 39 * @var string $version The current version of this plugin. 40 */ 41 private $version; 42 43 /** 44 * Initialize the class and set its properties. 45 * 46 * @param string $plugin_name The name of the plugin. 47 * @param string $version The version of this plugin. 48 * 49 * @since 1.0.0 50 */ 51 public function __construct( $plugin_name, $version ) { 52 53 $this->plugin_name = $plugin_name; 54 $this->version = $version; 55 56 } 57 58 public function virtual_queue_query_vars( $vars ) { 59 $vars[] = '__virtual_queue'; 60 61 return $vars; 62 } 63 64 /** 65 * Maintenance Endpoint 66 * Used for the cron job 67 */ 68 public function virtual_queue_endpoint() { 69 add_rewrite_rule( '^virtual-queue/maintenance/?', 'index.php?__virtual_queue=maintenance', 'top' ); 70 } 71 72 73 /** 74 * Virtual Queue Maintenance 75 */ 76 public function virtual_queue_parse_request() { 77 global $wp; 78 $__virtual_queue = $wp->query_vars['__virtual_queue']; 79 if ( $__virtual_queue == 'maintenance' ): 80 self::maintenance(); 81 endif; 82 } 83 84 85 /** 86 * Register the stylesheets for the public-facing side of the site. 87 * 88 * @since 1.0.0 89 */ 90 public function enqueue_styles() { 91 92 //wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/virtual-queue-public.css', array(), $this->version, 'all' ); 93 94 } 95 96 /** 97 * Add Custom Meta 98 * 99 * @since 1.0.0 100 */ 101 public function add_meta() { 102 $request_url = isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : ''; 103 $vq_landing_page_url = get_option( 'vq_landing_page_url' ); 104 $vq_refresh_seconds = get_option( 'vq_refresh_seconds' ); 105 106 if ( $request_url === $vq_landing_page_url ): 107 if ( intval( $vq_refresh_seconds ) > 0 ):?> 108 <meta http-equiv="Refresh" content="<?= intval( $vq_refresh_seconds ) ?>"> 109 <?php 110 endif; 111 endif; 112 } 113 114 /** 115 * Validate the current queue 116 */ 117 public function virtual_queue_validation() { 118 global $wpdb, $wp; 119 120 $request_url = isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : ''; 121 $vq_sessions_limit_number = get_option( 'vq_sessions_limit_number' ); 122 $vq_landing_page_url = get_option( 'vq_landing_page_url' ); 123 $vq_cookie_expire_hours = get_option( 'vq_cookie_expire_hours' ); 124 125 if ( ! is_admin() 126 && ! current_user_can( 'administrator' ) 127 && strpos( $request_url, 'wp-admin' ) == false 128 && strpos( $request_url, 'wp-login' ) == false 129 && strpos( $request_url, 'virtual-queue/maintenance' ) == false 130 ): 131 132 $table_name = $wpdb->prefix . 'vq_sessions'; 133 $session_create_id = uniqid() . '-' . session_create_id(); 134 135 $current_cookie = isset( $_COOKIE['vq_session_id'] ) ? $_COOKIE['vq_session_id'] : $session_create_id; 136 $vq_status = self::vq_status( $wpdb, $wpdb->prefix . 'vq_status' ); 137 $pending_sessions = is_object( $vq_status ) ? ( isset( $vq_status->pending ) ? $vq_status->pending : 0 ) : 0; 138 $active_sessions = is_object( $vq_status ) ? ( isset( $vq_status->active ) ? $vq_status->active : 0 ) : 0; 139 140 /** 141 * Calculate the status based on: 142 * - current sessions 143 * - sessions limit 144 * - pending list 145 */ 146 $status = ( ( $pending_sessions + $active_sessions ) >= $vq_sessions_limit_number ) ? 0 : ( $pending_sessions > 0 ? 0 : 1 ); 147 148 if ( isset( $_COOKIE['vq_session_id'] ) ): 149 150 /** 151 * Update the latest updated time 152 */ 153 self::update_timestamp( $wpdb, $table_name, $current_cookie ); 154 155 /** 156 * Validate this cookie 157 */ 158 $validate_cookie = self::validate_cookie( $wpdb, $table_name, $current_cookie ); 159 160 if ( empty( $validate_cookie ) ): 161 /** 162 * Add a new cookie and send this one to the end of the queue 163 */ 164 setcookie( 'vq_session_id', false, time() + ( - 3600 * $vq_cookie_expire_hours ), '/' ); 165 wp_redirect( $vq_landing_page_url ); 166 else: 167 /** 168 * Continue 169 */ 170 if ( $status ): 171 /** 172 * Allow this user to access the website 173 */ 174 self::set_active_status( $wpdb, $table_name, $current_cookie ); 175 else: 176 if ( $validate_cookie->status ): 177 $status = 1; 178 endif; 179 endif; 180 endif; 181 else: 182 /** 183 * Insert this row into the database 184 */ 185 $add_to_queue = self::add_to_queue( $wpdb, $table_name, $session_create_id, $status ); 186 187 /** 188 * If data saved, set the cookie 189 */ 190 if ( $add_to_queue ): 191 setcookie( 'vq_session_id', $session_create_id, time() + ( 3600 * $vq_cookie_expire_hours ), '/' ); 192 /** 193 * Set the current position 194 */ 195 self::set_queue_position(); 196 endif; 197 endif; 198 199 if ( ! $status ): 200 /** 201 * See if someone left the queue 202 */ 203 /** 204 * Redirect to the landing page 205 */ 206 if ( $request_url !== $vq_landing_page_url ): 207 wp_redirect( $vq_landing_page_url ); 208 exit(); 209 endif; 210 else: 211 if ( $request_url === $vq_landing_page_url ): 212 wp_redirect( "/" ); 213 exit(); 214 endif; 215 endif; 216 endif; 217 } 218 219 /** 220 * @param $wpdb 221 * @param $table_name 222 * @param $session_create_id 223 * @param $status 224 * 225 * @return mixed 226 */ 227 private static function add_to_queue( $wpdb, $table_name, $session_create_id, $status = 0 ) { 228 $wpdb->insert( 229 $table_name, 230 array( 231 'session_id' => $session_create_id, 232 'estimated_time' => 0, 233 'registered_date' => time(), 234 'updated_date' => time(), 235 'status' => $status ? 1 : 0 236 ), 237 array( 238 '%s', 239 '%d', 240 '%d', 241 '%d', 242 '%d' 243 ) 244 ); 245 /** 246 * Keep the last ID 247 */ 248 $lastId = $wpdb->insert_id; 249 250 /** 251 * Update the status 252 */ 253 if ( $status ): 254 self::increment_active(); 255 else: 256 self::increment_pending(); 257 endif; 258 259 return $lastId; 260 } 261 262 /** 263 * @param $wpdb 264 * @param $table_name 265 * 266 * @return int 267 */ 268 private static function vq_status( $wpdb, $table_name ) { 269 $vq_status = $wpdb->get_row( "SELECT `active`, `pending` FROM $table_name where id='1'" ); 270 271 return $vq_status; 272 } 273 274 /** 275 * @param $wpdb 276 * @param $session_id 277 * 278 * @return mixed 279 */ 280 private static function validate_cookie( $wpdb, $table_name, $session_id ) { 281 /** 282 * Return the current status 283 */ 284 return $wpdb->get_row( "SELECT status FROM $table_name where session_id='$session_id'" ); 285 } 286 287 /** 288 * @param $wpdb 289 * @param $table_name 290 * @param $session_id 291 */ 292 private static function set_active_status( $wpdb, $table_name, $session_id ) { 293 /** 294 * Get the current status 295 */ 296 $vq_status = self::vq_status( $wpdb, $wpdb->prefix . 'vq_status' ); 297 298 /** 299 * Update the status 300 */ 301 $wpdb->update( $wpdb->prefix . 'vq_status', 302 array( 'active' => $vq_status->active + 1, 'pending' => $vq_status->pending - 1 ), 303 array( 'id' => 1 ) 304 ); 305 306 $wpdb->update( $table_name, array( 'status' => 1 ), array( 'session_id' => $session_id ) ); 307 } 308 309 /** 310 * @param $wpdb 311 * @param $table_name 312 * @param $session_id 313 */ 314 private static function update_timestamp( $wpdb, $table_name, $session_id ) { 315 $wpdb->update( $table_name, array( 'updated_date' => time() ), array( 'session_id' => $session_id ) ); 316 } 317 318 /** 319 * Increment the active visitors 320 */ 321 private static function increment_active() { 322 global $wpdb; 323 324 /** 325 * Get the current status 326 */ 327 $vq_status = self::vq_status( $wpdb, $wpdb->prefix . 'vq_status' ); 328 329 $wpdb->update( $wpdb->prefix . 'vq_status', 330 array( 'active' => $vq_status->active + 1 ), 331 array( 'id' => 1 ) 332 ); 333 } 334 335 /** 336 * Increment the pending visitors 337 */ 338 private static function increment_pending() { 339 global $wpdb; 340 341 /** 342 * Get the current status 343 */ 344 $vq_status = self::vq_status( $wpdb, $wpdb->prefix . 'vq_status' ); 345 346 $wpdb->update( $wpdb->prefix . 'vq_status', 347 array( 'pending' => $vq_status->pending + 1 ), 348 array( 'id' => 1 ) 349 ); 350 } 351 352 /** 353 * Maintenance 354 */ 355 private static function maintenance() { 356 global $wpdb; 357 /** 358 * Remove all the inactive visitors from the queue 359 */ 360 $vq_sessions_limit_number = get_option( 'vq_sessions_limit_number' ); 361 $vq_inactive_minutes = get_option( 'vq_inactive_minutes' ); 362 $time = time() - ( $vq_inactive_minutes * 60 ); 363 $table = $wpdb->prefix . 'vq_sessions'; 364 365 $delete_pending = $wpdb->query( "DELETE FROM $table WHERE updated_date < $time and status=0" ); 366 $delete_active = $wpdb->query( "DELETE FROM $table WHERE updated_date < $time and status=1" ); 367 368 /** 369 * Get the current status 370 */ 371 $vq_status = self::vq_status( $wpdb, $wpdb->prefix . 'vq_status' ); 372 373 /** 374 * Update the Active and Pending counters 375 */ 376 $pending_count = $vq_status->pending - $delete_pending; 377 $active_count = $vq_status->active - $delete_active; 378 379 /** 380 * Let's allow someone else to navigate 381 */ 382 $allow_counter = $vq_sessions_limit_number - $active_count; 383 $approved_visitors = $wpdb->query( "update $table set status=1 ORDER BY id ASC LIMIT $allow_counter" ); 384 385 /** 386 * Update the stats 387 */ 388 $wpdb->update( $wpdb->prefix . 'vq_status', 389 array( 390 'pending' => ( $pending_count - $approved_visitors ) < 0 ? 0 : $pending_count - $approved_visitors, 391 'active' => ( $active_count + $approved_visitors ) < 0 ? 0 : $active_count + $approved_visitors, 392 ), 393 array( 'id' => 1 ) 394 ); 395 396 /** 397 * Update the queue position 398 */ 399 self::set_queue_position(); 400 exit( 0 ); 401 } 402 403 /** 404 * Update the queue position 405 */ 406 private static function set_queue_position() { 407 global $wpdb; 408 $table = $wpdb->prefix . 'vq_sessions'; 409 410 /** 411 * Set the current position 412 */ 413 $wpdb->query( "SET @virtual_queue_position := 0;" ); 414 $wpdb->query( "UPDATE $table SET estimated_time = ( SELECT @virtual_queue_position := @virtual_queue_position + 1 ) where status=0 ORDER BY id ASC;" ); 415 416 } 23 class Virtual_Queue_Public 24 { 25 26 /** 27 * The ID of this plugin. 28 * 29 * @since 1.0.0 30 * @access private 31 * @var string $plugin_name The ID of this plugin. 32 */ 33 private $plugin_name; 34 35 /** 36 * The version of this plugin. 37 * 38 * @since 1.0.0 39 * @access private 40 * @var string $version The current version of this plugin. 41 */ 42 private $version; 43 44 /** 45 * Initialize the class and set its properties. 46 * 47 * @param string $plugin_name The name of the plugin. 48 * @param string $version The version of this plugin. 49 * 50 * @since 1.0.0 51 */ 52 public function __construct($plugin_name, $version) 53 { 54 55 $this->plugin_name = $plugin_name; 56 $this->version = $version; 57 58 } 59 60 public function virtual_queue_query_vars($vars) 61 { 62 $vars[] = '__virtual_queue'; 63 64 return $vars; 65 } 66 67 /** 68 * Maintenance Endpoint 69 * Used for the cron job 70 */ 71 public function virtual_queue_endpoint() 72 { 73 add_rewrite_rule('^virtual-queue/maintenance/?', 'index.php?__virtual_queue=maintenance', 'top'); 74 } 75 76 77 /** 78 * Virtual Queue Maintenance 79 */ 80 public function virtual_queue_parse_request() 81 { 82 global $wp; 83 $__virtual_queue = $wp->query_vars['__virtual_queue']; 84 if ($__virtual_queue == 'maintenance'): 85 self::maintenance(); 86 endif; 87 } 88 89 90 /** 91 * Register the stylesheets for the public-facing side of the site. 92 * 93 * @since 1.0.0 94 */ 95 public function enqueue_styles() 96 { 97 98 //wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/virtual-queue-public.css', array(), $this->version, 'all' ); 99 100 } 101 102 /** 103 * Add Custom Meta 104 * 105 * @since 1.0.0 106 */ 107 public function add_meta() 108 { 109 $request_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : ''; 110 $vq_landing_page_url = get_option('vq_landing_page_url'); 111 $vq_refresh_seconds = get_option('vq_refresh_seconds'); 112 113 if ($request_url === $vq_landing_page_url): 114 if (intval($vq_refresh_seconds) > 0):?> 115 <meta http-equiv="Refresh" content="<?= intval($vq_refresh_seconds) ?>"> 116 <?php 117 endif; 118 endif; 119 } 120 121 /** 122 * Validate the current queue 123 */ 124 public function virtual_queue_validation() 125 { 126 global $wpdb, $wp; 127 128 $request_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : ''; 129 $vq_sessions_limit_number = get_option('vq_sessions_limit_number'); 130 $vq_landing_page_url = get_option('vq_landing_page_url'); 131 $vq_cookie_expire_hours = get_option('vq_cookie_expire_hours'); 132 133 if (!is_admin() 134 && !current_user_can('administrator') 135 && strpos($request_url, 'wp-admin') == false 136 && strpos($request_url, 'wp-login') == false 137 && strpos($request_url, 'virtual-queue/maintenance') == false 138 ): 139 140 $table_name = $wpdb->prefix . 'vq_sessions'; 141 $session_create_id = uniqid() . '-' . session_create_id(); 142 143 $current_cookie = isset($_COOKIE['vq_session_id']) ? $_COOKIE['vq_session_id'] : $session_create_id; 144 $vq_status = self::vq_status($wpdb, $wpdb->prefix . 'vq_status'); 145 $pending_sessions = is_object($vq_status) ? (isset($vq_status->pending) ? $vq_status->pending : 0) : 0; 146 $active_sessions = is_object($vq_status) ? (isset($vq_status->active) ? $vq_status->active : 0) : 0; 147 148 /** 149 * Calculate the status based on: 150 * - current sessions 151 * - sessions limit 152 * - pending list 153 */ 154 $status = (($pending_sessions + $active_sessions) >= $vq_sessions_limit_number) ? 0 : ($pending_sessions > 0 ? 0 : 1); 155 156 if (isset($_COOKIE['vq_session_id'])): 157 158 /** 159 * Update the latest updated time 160 */ 161 self::update_timestamp($wpdb, $table_name, $current_cookie); 162 163 /** 164 * Validate this cookie 165 */ 166 $validate_cookie = self::validate_cookie($wpdb, $table_name, $current_cookie); 167 168 if (empty($validate_cookie)): 169 /** 170 * Add a new cookie and send this one to the end of the queue 171 */ 172 setcookie('vq_session_id', false, time() + (-3600 * $vq_cookie_expire_hours), '/'); 173 wp_redirect($vq_landing_page_url); 174 else: 175 /** 176 * Continue 177 */ 178 if ($status): 179 /** 180 * Allow this user to access the website 181 */ 182 self::set_active_status($wpdb, $table_name, $current_cookie); 183 else: 184 if ($validate_cookie->status): 185 $status = 1; 186 endif; 187 endif; 188 endif; 189 else: 190 /** 191 * Insert this row into the database 192 */ 193 $add_to_queue = self::add_to_queue($wpdb, $table_name, $session_create_id, $status); 194 195 /** 196 * If data saved, set the cookie 197 */ 198 if ($add_to_queue): 199 setcookie('vq_session_id', $session_create_id, time() + (3600 * $vq_cookie_expire_hours), '/'); 200 /** 201 * Set the current position 202 */ 203 self::set_queue_position(); 204 endif; 205 endif; 206 207 if (!$status): 208 /** 209 * See if someone left the queue 210 */ 211 /** 212 * Redirect to the landing page 213 */ 214 if ($request_url !== $vq_landing_page_url): 215 wp_redirect($vq_landing_page_url); 216 exit(); 217 endif; 218 else: 219 if ($request_url === $vq_landing_page_url): 220 wp_redirect("/"); 221 exit(); 222 endif; 223 endif; 224 endif; 225 } 226 227 /** 228 * @param $wpdb 229 * @param $table_name 230 * @param $session_create_id 231 * @param $status 232 * 233 * @return mixed 234 */ 235 private static function add_to_queue($wpdb, $table_name, $session_create_id, $status = 0) 236 { 237 $wpdb->insert( 238 $table_name, 239 array( 240 'session_id' => $session_create_id, 241 'estimated_time' => 0, 242 'registered_date' => time(), 243 'updated_date' => time(), 244 'status' => $status ? 1 : 0 245 ), 246 array( 247 '%s', 248 '%d', 249 '%d', 250 '%d', 251 '%d' 252 ) 253 ); 254 /** 255 * Keep the last ID 256 */ 257 $lastId = $wpdb->insert_id; 258 259 /** 260 * Update the status 261 */ 262 263 self::update_statistics(); 264 265 return $lastId; 266 } 267 268 /** 269 * @param $wpdb 270 * @param $table_name 271 * 272 * @return int 273 */ 274 private static function vq_status($wpdb, $table_name) 275 { 276 $vq_status = $wpdb->get_row("SELECT `active`, `pending` FROM $table_name where id='1'"); 277 278 return $vq_status; 279 } 280 281 /** 282 * @param $wpdb 283 * @param $session_id 284 * 285 * @return mixed 286 */ 287 private static function validate_cookie($wpdb, $table_name, $session_id) 288 { 289 /** 290 * Return the current status 291 */ 292 return $wpdb->get_row("SELECT status FROM $table_name where session_id='$session_id'"); 293 } 294 295 /** 296 * @param $wpdb 297 * @param $table_name 298 * @param $session_id 299 */ 300 private static function set_active_status($wpdb, $table_name, $session_id) 301 { 302 $wpdb->update($table_name, array('status' => 1), array('session_id' => $session_id)); 303 304 self::update_statistics(); 305 } 306 307 /** 308 * @param $wpdb 309 * @param $table_name 310 * @param $session_id 311 */ 312 private static function update_timestamp($wpdb, $table_name, $session_id) 313 { 314 $wpdb->update($table_name, array('updated_date' => time()), array('session_id' => $session_id)); 315 } 316 317 318 /** 319 * Maintenance 320 */ 321 private static function maintenance() 322 { 323 global $wpdb; 324 325 /** 326 * Remove all the inactive visitors from the queue 327 */ 328 $vq_sessions_limit_number = get_option('vq_sessions_limit_number'); 329 $vq_inactive_minutes = get_option('vq_inactive_minutes'); 330 $time = time() - ($vq_inactive_minutes * 60); 331 $table = $wpdb->prefix . 'vq_sessions'; 332 333 /** 334 * Delete Pending & Active Sessions 335 */ 336 $wpdb->query("DELETE FROM $table WHERE updated_date < $time"); 337 338 /** 339 * Let's allow someone else to navigate 340 */ 341 $allow_counter = $vq_sessions_limit_number - self::activeCounter(); 342 $wpdb->query("update $table set status=1 ORDER BY id ASC LIMIT $allow_counter"); 343 344 /** 345 * Update the statistics 346 */ 347 self::update_statistics(); 348 349 /** 350 * Update the queue position 351 */ 352 self::set_queue_position(); 353 exit(0); 354 } 355 356 /** 357 * Update the queue position 358 */ 359 private static function set_queue_position() 360 { 361 global $wpdb; 362 $table = $wpdb->prefix . 'vq_sessions'; 363 364 /** 365 * Set the current position 366 */ 367 $wpdb->query("SET @virtual_queue_position := 0;"); 368 $wpdb->query("UPDATE $table SET estimated_time = ( SELECT @virtual_queue_position := @virtual_queue_position + 1 ) where status=0 ORDER BY id ASC;"); 369 370 } 371 372 /** 373 * Update Statistics 374 */ 375 private static function update_statistics() 376 { 377 global $wpdb; 378 379 $wpdb->update($wpdb->prefix . 'vq_status', 380 array( 381 'pending' => self::pendingCounter(), 382 'active' => self::activeCounter() 383 ), 384 array('id' => 1) 385 ); 386 } 387 388 /** 389 * Active Counter 390 * 391 * @return mixed 392 */ 393 private static function activeCounter() 394 { 395 global $wpdb; 396 $table = $wpdb->prefix . 'vq_sessions'; 397 398 /** 399 * Count the active sessions 400 */ 401 $sessions = $wpdb->get_row("SELECT count(id) as counter FROM $table where status='1'"); 402 403 return $sessions->counter; 404 } 405 406 /** 407 * Pending Counter 408 * 409 * @return mixed 410 */ 411 private static function pendingCounter() 412 { 413 global $wpdb; 414 $table = $wpdb->prefix . 'vq_sessions'; 415 416 /** 417 * Count the pending sessions 418 * $pending_sessions->counter 419 */ 420 $sessions = $wpdb->get_row("SELECT count(id) as counter FROM $table where status='0'"); 421 422 return $sessions->counter; 423 } 417 424 }
Note: See TracChangeset
for help on using the changeset viewer.