1 | <?php |
---|
2 | |
---|
3 | // BUG: doesn't work when multiple post meta entries exist under one key |
---|
4 | function get_post_lock_name($id, $name) { |
---|
5 | return "WPPost_" . $id . "_$name"; |
---|
6 | } |
---|
7 | |
---|
8 | function get_user_lock_name($id, $name) { |
---|
9 | return "WPUser_$id_$name"; |
---|
10 | } |
---|
11 | |
---|
12 | function lock_post_meta($id, $name) { |
---|
13 | $lock_name = get_post_lock_name($id, $name); |
---|
14 | $stall = $wpdb->get_col("SELECT GET_LOCK('$lock_name', 5)"); |
---|
15 | |
---|
16 | // TODO: check stall |
---|
17 | } |
---|
18 | |
---|
19 | function unlock_user_meta($id, $name) { |
---|
20 | global $wpdb; |
---|
21 | |
---|
22 | $lock_name = get_user_lock_name($id, $name); |
---|
23 | |
---|
24 | $result = $wpdb->get_col("SELECT RELEASE_LOCK('$lock_name')"); |
---|
25 | |
---|
26 | // TODO: check $result |
---|
27 | } |
---|
28 | |
---|
29 | function unlock_post_meta($id, $name) { |
---|
30 | global $wpdb; |
---|
31 | |
---|
32 | $lock_name = get_post_lock_name($id, $name); |
---|
33 | |
---|
34 | $result = $wpdb->get_col("SELECT RELEASE_LOCK('$lock_name')"); |
---|
35 | |
---|
36 | // TODO: check $result |
---|
37 | } |
---|
38 | |
---|
39 | function lock_user_meta($id, $name) { |
---|
40 | $lock_name = get_user_lock_name($id, $name); |
---|
41 | $stall = $wpdb->get_col("SELECT GET_LOCK('$lock_name', 5)"); |
---|
42 | |
---|
43 | // TODO: check stall |
---|
44 | } |
---|
45 | |
---|
46 | function get_and_lock_user_meta($id, $name, $single) { |
---|
47 | global $wpdb; |
---|
48 | |
---|
49 | $lock_name = get_user_lock_name($id, $name); |
---|
50 | |
---|
51 | $stall = $wpdb->get_col("SELECT GET_LOCK('$lock_name', 5)"); |
---|
52 | |
---|
53 | // TODO: check stall |
---|
54 | |
---|
55 | return get_user_meta($id, $name, $single); |
---|
56 | } |
---|
57 | |
---|
58 | function get_and_lock_post_meta($id, $name, $single) { |
---|
59 | global $wpdb; |
---|
60 | |
---|
61 | $lock_name = get_post_lock_name($id, $name); |
---|
62 | |
---|
63 | $stall = $wpdb->get_col("SELECT GET_LOCK('$lock_name', 5)"); |
---|
64 | |
---|
65 | // TODO: check stall |
---|
66 | |
---|
67 | return get_post_meta($id, $name, $single); |
---|
68 | } |
---|
69 | |
---|
70 | function get_and_update_post_meta($id, $name, $single, $callback) { |
---|
71 | $value = get_and_lock_post_meta($id, $name, $single); |
---|
72 | |
---|
73 | $val = $callback($value); |
---|
74 | |
---|
75 | if (isset($val)) { |
---|
76 | update_post_meta($id, $name, $val); |
---|
77 | } |
---|
78 | |
---|
79 | unlock_post_meta($id, $name); |
---|
80 | } |
---|
81 | |
---|
82 | function get_and_update_user_meta($id, $name, $single, $callback) { |
---|
83 | $value = get_and_lock_post_meta($id, $name, $single); |
---|
84 | |
---|
85 | $val = $callback($value); |
---|
86 | |
---|
87 | if (isset($val)) { |
---|
88 | update_user_meta($id, $name, $val); |
---|
89 | } |
---|
90 | |
---|
91 | unlock_user_meta($id, $name); |
---|
92 | } |
---|