customizer
2 weeks ago
debug
2 weeks ago
entities
2 weeks ago
managers
2 weeks ago
sdk
2 weeks ago
supplements
2 weeks ago
class-freemius-abstract.php
2 weeks ago
class-freemius.php
2 weeks ago
class-fs-admin-notices.php
2 weeks ago
class-fs-api.php
2 weeks ago
class-fs-garbage-collector.php
2 weeks ago
class-fs-hook-snapshot.php
2 weeks ago
class-fs-lock.php
2 weeks ago
class-fs-logger.php
2 weeks ago
class-fs-options.php
2 weeks ago
class-fs-plugin-updater.php
2 weeks ago
class-fs-security.php
2 weeks ago
class-fs-storage.php
2 weeks ago
class-fs-user-lock.php
2 weeks ago
fs-core-functions.php
2 weeks ago
fs-essential-functions.php
2 weeks ago
fs-html-escaping-functions.php
2 weeks ago
fs-plugin-info-dialog.php
2 weeks ago
index.php
2 weeks ago
l10n.php
2 weeks ago
class-fs-lock.php
110 lines
| 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.5.1 |
| 7 | */ |
| 8 | |
| 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | exit; |
| 11 | } |
| 12 | |
| 13 | /** |
| 14 | * Class FS_Lock |
| 15 | * |
| 16 | * @author Vova Feldman (@svovaf) |
| 17 | * @since 2.5.1 |
| 18 | */ |
| 19 | class FS_Lock { |
| 20 | /** |
| 21 | * @var int Random ID representing the current PHP thread. |
| 22 | */ |
| 23 | private static $_thread_id; |
| 24 | /** |
| 25 | * @var string |
| 26 | */ |
| 27 | private $_lock_id; |
| 28 | |
| 29 | /** |
| 30 | * @param string $lock_id |
| 31 | */ |
| 32 | function __construct( $lock_id ) { |
| 33 | if ( ! fs_starts_with( $lock_id, WP_FS___OPTION_PREFIX ) ) { |
| 34 | $lock_id = WP_FS___OPTION_PREFIX . $lock_id; |
| 35 | } |
| 36 | |
| 37 | $this->_lock_id = $lock_id; |
| 38 | |
| 39 | if ( ! isset( self::$_thread_id ) ) { |
| 40 | self::$_thread_id = mt_rand( 0, 32000 ); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Try to acquire lock. If the lock is already set or is being acquired by another locker, don't do anything. |
| 46 | * |
| 47 | * @param int $expiration |
| 48 | * |
| 49 | * @return bool TRUE if successfully acquired lock. |
| 50 | */ |
| 51 | function try_lock( $expiration = 0 ) { |
| 52 | if ( $this->is_locked() ) { |
| 53 | // Already locked. |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | set_site_transient( $this->_lock_id, self::$_thread_id, $expiration ); |
| 58 | |
| 59 | if ( $this->has_lock() ) { |
| 60 | $this->lock($expiration); |
| 61 | |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Acquire lock regardless if it's already acquired by another locker or not. |
| 70 | * |
| 71 | * @author Vova Feldman (@svovaf) |
| 72 | * @since 2.1.0 |
| 73 | * |
| 74 | * @param int $expiration |
| 75 | */ |
| 76 | function lock( $expiration = 0 ) { |
| 77 | set_site_transient( $this->_lock_id, true, $expiration ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Checks if lock is currently acquired. |
| 82 | * |
| 83 | * @author Vova Feldman (@svovaf) |
| 84 | * @since 2.1.0 |
| 85 | * |
| 86 | * @return bool |
| 87 | */ |
| 88 | function is_locked() { |
| 89 | return ( false !== get_site_transient( $this->_lock_id ) ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Unlock the lock. |
| 94 | * |
| 95 | * @author Vova Feldman (@svovaf) |
| 96 | * @since 2.1.0 |
| 97 | */ |
| 98 | function unlock() { |
| 99 | delete_site_transient( $this->_lock_id ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Checks if lock is currently acquired by the current locker. |
| 104 | * |
| 105 | * @return bool |
| 106 | */ |
| 107 | protected function has_lock() { |
| 108 | return ( self::$_thread_id == get_site_transient( $this->_lock_id ) ); |
| 109 | } |
| 110 | } |