-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathclass-lock-object.php
More file actions
86 lines (74 loc) · 1.67 KB
/
class-lock-object.php
File metadata and controls
86 lines (74 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
/**
* Lock_Object class for the Cloudinary plugin.
*
* @package Cloudinary
*/
namespace Cloudinary\Cron;
use Cloudinary\Cron;
/**
* Class Lock_Object
*/
class Lock_Object extends Lock_File {
/**
* Holds the prefix of the transient.
*/
const PREFIX = 'cld_';
/**
* Get a lock data.
*
* @param string|null $file The lock name.
*
* @return false|mixed|string
*/
public function get_lock_file( $file = null ) {
return get_transient( self::PREFIX . $this->get_lock_file_name( $file ) );
}
/**
* Get the lock name.
*
* @param string|null $file_name The name of the transient.
*
* @return mixed|string|null
*/
public function get_lock_file_name( $file_name = null ) {
if ( null === $file_name ) {
$file_name = 'cron-run';
}
return $file_name;
}
/**
* Check if a lock is in place.
*
* @param string|null $file The lock name.
*
* @return bool
*/
public function has_lock_file( $file = null ) {
return ! empty( $this->get_lock_file( $file ) );
}
/**
* Set a lock.
*
* @param string|null $file The name to set.
* @param mixed $data The data to set.
*
* @return mixed|string
*/
public function set_lock_file( $file = null, $data = null ) {
$time = time();
$bits = $data ? json_decode( $data, true ) : uniqid( $time );
if ( ! $this->has_lock_file( $file ) ) {
set_transient( self::PREFIX . $this->get_lock_file_name( $file ), $bits, Cron::$daemon_watcher_interval );
}
return $bits;
}
/**
* Delete a lock.
*
* @param string|null $file The name to set.
*/
public function delete_lock_file( $file = null ) {
delete_transient( self::PREFIX . $this->get_lock_file_name( $file ) );
}
}