| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Freemius |
| 4 |
* @copyright Copyright (c) 2015, Freemius, Inc. |
| 5 |
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3 |
| 6 |
* @since 2.1.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Class FS_User_Lock |
| 15 |
*/ |
| 16 |
class FS_User_Lock { |
| 17 |
/** |
| 18 |
* @var int |
| 19 |
*/ |
| 20 |
private $_wp_user_id; |
| 21 |
/** |
| 22 |
* @var int |
| 23 |
*/ |
| 24 |
private $_thread_id; |
| 25 |
|
| 26 |
#-------------------------------------------------------------------------------- |
| 27 |
#region Singleton |
| 28 |
#-------------------------------------------------------------------------------- |
| 29 |
|
| 30 |
/** |
| 31 |
* @var FS_User_Lock |
| 32 |
*/ |
| 33 |
private static $_instance; |
| 34 |
|
| 35 |
/** |
| 36 |
* @author Vova Feldman (@svovaf) |
| 37 |
* @since 2.1.0 |
| 38 |
* |
| 39 |
* @return FS_User_Lock |
| 40 |
*/ |
| 41 |
static function instance() { |
| 42 |
if ( ! isset( self::$_instance ) ) { |
| 43 |
self::$_instance = new self(); |
| 44 |
} |
| 45 |
|
| 46 |
return self::$_instance; |
| 47 |
} |
| 48 |
|
| 49 |
#endregion |
| 50 |
|
| 51 |
private function __construct() { |
| 52 |
$this->_wp_user_id = Freemius::get_current_wp_user_id(); |
| 53 |
$this->_thread_id = mt_rand( 0, 32000 ); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
/** |
| 58 |
* Try to acquire lock. If the lock is already set or is being acquired by another locker, don't do anything. |
| 59 |
* |
| 60 |
* @author Vova Feldman (@svovaf) |
| 61 |
* @since 2.1.0 |
| 62 |
* |
| 63 |
* @param int $expiration |
| 64 |
* |
| 65 |
* @return bool TRUE if successfully acquired lock. |
| 66 |
*/ |
| 67 |
function try_lock( $expiration = 0 ) { |
| 68 |
if ( $this->is_locked() ) { |
| 69 |
// Already locked. |
| 70 |
return false; |
| 71 |
} |
| 72 |
|
| 73 |
set_site_transient( "locked_{$this->_wp_user_id}", $this->_thread_id, $expiration ); |
| 74 |
|
| 75 |
if ( $this->has_lock() ) { |
| 76 |
set_site_transient( "locked_{$this->_wp_user_id}", true, $expiration ); |
| 77 |
|
| 78 |
return true; |
| 79 |
} |
| 80 |
|
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Acquire lock regardless if it's already acquired by another locker or not. |
| 86 |
* |
| 87 |
* @author Vova Feldman (@svovaf) |
| 88 |
* @since 2.1.0 |
| 89 |
* |
| 90 |
* @param int $expiration |
| 91 |
*/ |
| 92 |
function lock( $expiration = 0 ) { |
| 93 |
set_site_transient( "locked_{$this->_wp_user_id}", true, $expiration ); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Checks if lock is currently acquired. |
| 98 |
* |
| 99 |
* @author Vova Feldman (@svovaf) |
| 100 |
* @since 2.1.0 |
| 101 |
* |
| 102 |
* @return bool |
| 103 |
*/ |
| 104 |
function is_locked() { |
| 105 |
return ( false !== get_site_transient( "locked_{$this->_wp_user_id}" ) ); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Unlock the lock. |
| 110 |
* |
| 111 |
* @author Vova Feldman (@svovaf) |
| 112 |
* @since 2.1.0 |
| 113 |
*/ |
| 114 |
function unlock() { |
| 115 |
delete_site_transient( "locked_{$this->_wp_user_id}" ); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Checks if lock is currently acquired by the current locker. |
| 120 |
* |
| 121 |
* @return bool |
| 122 |
*/ |
| 123 |
private function has_lock() { |
| 124 |
return ( $this->_thread_id == get_site_transient( "locked_{$this->_wp_user_id}" ) ); |
| 125 |
} |
| 126 |
} |