PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/mobile-menu/freemius/includes/class-fs-user-lock.php

https://bitbucket.org/RickCalder/durawp_new
PHP | 126 lines | 42 code | 18 blank | 66 comment | 5 complexity | 73ecd350873b5bccd9531df35caedaee MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, Apache-2.0, 0BSD
  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. if ( ! defined( 'ABSPATH' ) ) {
  9. exit;
  10. }
  11. /**
  12. * Class FS_User_Lock
  13. */
  14. class FS_User_Lock {
  15. /**
  16. * @var int
  17. */
  18. private $_wp_user_id;
  19. /**
  20. * @var int
  21. */
  22. private $_thread_id;
  23. #--------------------------------------------------------------------------------
  24. #region Singleton
  25. #--------------------------------------------------------------------------------
  26. /**
  27. * @var FS_User_Lock
  28. */
  29. private static $_instance;
  30. /**
  31. * @author Vova Feldman (@svovaf)
  32. * @since 2.1.0
  33. *
  34. * @return FS_User_Lock
  35. */
  36. static function instance() {
  37. if ( ! isset( self::$_instance ) ) {
  38. self::$_instance = new self();
  39. }
  40. return self::$_instance;
  41. }
  42. #endregion
  43. private function __construct() {
  44. $this->_wp_user_id = Freemius::get_current_wp_user_id();
  45. $this->_thread_id = mt_rand( 0, 32000 );
  46. }
  47. /**
  48. * Try to acquire lock. If the lock is already set or is being acquired by another locker, don't do anything.
  49. *
  50. * @author Vova Feldman (@svovaf)
  51. * @since 2.1.0
  52. *
  53. * @param int $expiration
  54. *
  55. * @return bool TRUE if successfully acquired lock.
  56. */
  57. function try_lock( $expiration = 0 ) {
  58. if ( $this->is_locked() ) {
  59. // Already locked.
  60. return false;
  61. }
  62. set_site_transient( "locked_{$this->_wp_user_id}", $this->_thread_id, $expiration );
  63. if ( $this->has_lock() ) {
  64. set_site_transient( "locked_{$this->_wp_user_id}", true, $expiration );
  65. return true;
  66. }
  67. return false;
  68. }
  69. /**
  70. * Acquire lock regardless if it's already acquired by another locker or not.
  71. *
  72. * @author Vova Feldman (@svovaf)
  73. * @since 2.1.0
  74. *
  75. * @param int $expiration
  76. */
  77. function lock( $expiration = 0 ) {
  78. set_site_transient( "locked_{$this->_wp_user_id}", true, $expiration );
  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( "locked_{$this->_wp_user_id}" ) );
  90. }
  91. /**
  92. * Unlock the lock.
  93. *
  94. * @author Vova Feldman (@svovaf)
  95. * @since 2.1.0
  96. */
  97. function unlock() {
  98. delete_site_transient( "locked_{$this->_wp_user_id}" );
  99. }
  100. /**
  101. * Checks if lock is currently acquired by the current locker.
  102. *
  103. * @return bool
  104. */
  105. private function has_lock() {
  106. return ( $this->_thread_id == get_site_transient( "locked_{$this->_wp_user_id}" ) );
  107. }
  108. }