-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-mutex.php
57 lines (51 loc) · 1.34 KB
/
wp-mutex.php
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
<?php
if ( ! class_exists( 'WPMutex' ) ) :
class WPMutex {
/**
* Get an exclusive named lock.
*
* @param string $name
* @param integer $timeout
* @param bool $network_wide
* @return bool
*/
static function acquire( $name, $timeout = 0, $network_wide = false ) {
global $wpdb; /* @var wpdb $wpdb */
if ( ! $network_wide ) {
$name = WPMutex::_get_private_name( $name );
}
$state = $wpdb->get_var( $wpdb->prepare( 'SELECT GET_LOCK(%s, %d)', $name, $timeout ) );
return 1 == $state;
}
/**
* Release a named lock.
*
* @param string $name
* @param bool $network_wide
* @return bool
*/
static function release( $name, $network_wide = false ) {
global $wpdb; /* @var wpdb $wpdb */
if ( ! $network_wide ) {
$name = WPMutex::_get_private_name( $name );
}
$released = $wpdb->get_var( $wpdb->prepare( 'SELECT RELEASE_LOCK(%s)', $name ) );
return 1 == $released;
}
/**
* Given a generic lock name, create a new one that's unique to the current blog.
*
* @access private
*
* @param string $name
* @return string
*/
static function _get_private_name( $name ) {
global $current_blog;
if ( function_exists( 'is_multisite' ) && is_multisite() && isset( $current_blog->blog_id ) ) {
$name .= '-blog-' . $current_blog->blog_id;
}
return $name;
}
}
endif;