| @@ -9,18 +9,20 @@ | ||
| 9 | 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | 10 | exit; |
| 11 | 11 | } |
| 12 | 12 | |
| 13 | - require_once WP_FS__DIR_INCLUDES . '/class-fs-lock.php'; | |
| 14 | - | |
| 15 | 13 | /** |
| 16 | 14 | * Class FS_User_Lock |
| 17 | 15 | */ |
| 18 | 16 | class FS_User_Lock { |
| 19 | 17 | /** |
| 20 | - * @var FS_Lock | |
| 18 | + * @var int | |
| 21 | 19 | */ |
| 22 | - private $_lock; | |
| 20 | + private $_wp_user_id; | |
| 21 | + /** | |
| 22 | + * @var int | |
| 23 | + */ | |
| 24 | + private $_thread_id; | |
| 23 | 25 | |
| 24 | 26 | #-------------------------------------------------------------------------------- |
| 25 | 27 | #region Singleton |
| 26 | 28 | #-------------------------------------------------------------------------------- |
| @@ -46,12 +48,12 @@ | ||
| 46 | 48 | |
| 47 | 49 | #endregion |
| 48 | 50 | |
| 49 | 51 | private function __construct() { |
| 50 | - $current_user_id = Freemius::get_current_wp_user_id(); | |
| 52 | + $this->_wp_user_id = Freemius::get_current_wp_user_id(); | |
| 53 | + $this->_thread_id = mt_rand( 0, 32000 ); | |
| 54 | + } | |
| 51 | 55 | |
| 52 | - $this->_lock = new FS_Lock( "locked_{$current_user_id}" ); | |
| 53 | - } | |
| 54 | 56 | |
| 55 | 57 | /** |
| 56 | 58 | * Try to acquire lock. If the lock is already set or is being acquired by another locker, don't do anything. |
| 57 | 59 | * |
| @@ -62,9 +64,22 @@ | ||
| 62 | 64 | * |
| 63 | 65 | * @return bool TRUE if successfully acquired lock. |
| 64 | 66 | */ |
| 65 | 67 | function try_lock( $expiration = 0 ) { |
| 66 | - return $this->_lock->try_lock( $expiration ); | |
| 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; | |
| 67 | 82 | } |
| 68 | 83 | |
| 69 | 84 | /** |
| 70 | 85 | * Acquire lock regardless if it's already acquired by another locker or not. |
| @@ -74,12 +89,24 @@ | ||
| 74 | 89 | * |
| 75 | 90 | * @param int $expiration |
| 76 | 91 | */ |
| 77 | 92 | function lock( $expiration = 0 ) { |
| 78 | - $this->_lock->lock( $expiration ); | |
| 93 | + set_site_transient( "locked_{$this->_wp_user_id}", true, $expiration ); | |
| 79 | 94 | } |
| 80 | 95 | |
| 81 | 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 | + /** | |
| 82 | 109 | * Unlock the lock. |
| 83 | 110 | * |
| 84 | 111 | * @author Vova Feldman (@svovaf) |
| 85 | 112 | * @since 2.1.0 |
| @@ -84,7 +111,16 @@ | ||
| 84 | 111 | * @author Vova Feldman (@svovaf) |
| 85 | 112 | * @since 2.1.0 |
| 86 | 113 | */ |
| 87 | 114 | function unlock() { |
| 88 | - $this->_lock->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}" ) ); | |
| 89 | 125 | } |
| 90 | 126 | } |