PluginProbe
bbPress / 2.6.6
bbPress v2.6.6
trunk 2.0 2.0-beta-1 2.0-beta-2b 2.0-beta-3 2.0-beta-3b 2.0-rc-2 2.0-rc-3 2.0-rc-4 2.0-rc-5 2.0.1 2.0.2 2.0.3 2.1 2.1-beta-1 2.1-rc1 2.1-rc2 2.1-rc3 2.1-rc4 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.2.2 All 71 releases
bbpress / includes / common / locks.php

locks.php in bbPress 2.6.6, at includes/common/locks.php

83 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * bbPress Locking
5 *
6 * @package bbPress
7 * @subpackage Common
8 */
9
10 // Exit if accessed directly
11 defined( 'ABSPATH' ) || exit;
12
13 /**
14 * Check to see if the post is currently being edited by another user.
15 *
16 * @see wp_check_post_lock()
17 *
18 * @since 2.6.0 bbPress (r6340)
19 *
20 * @param int $post_id ID of the post to check for editing
21 * @return integer False: not locked or locked by current user. Int: user ID of user with lock.
22 */
23 function bbp_check_post_lock( $post_id = 0 ) {
24
25 // Bail if no post
26 if ( !$post = get_post( $post_id ) ) {
27 return false;
28 }
29
30 // Bail if no lock
31 if ( !$lock = get_post_meta( $post->ID, '_edit_lock', true ) ) {
32 return false;
33 }
34
35 // Get lock
36 $lock = explode( ':', $lock );
37 $time = $lock[0];
38 $user = (int) isset( $lock[1] )
39 ? $lock[1]
40 : get_post_meta( $post->ID, '_edit_last', true );
41
42 // Filter editing window duration
43 $time_window = apply_filters( 'bbp_check_post_lock_window', 3 * MINUTE_IN_SECONDS );
44
45 // Return user who is or last edited
46 if ( ! empty( $time ) && ( $time > ( time() - $time_window ) ) && ( $user !== bbp_get_current_user_id() ) ) {
47 return (int) $user;
48 }
49
50 return false;
51 }
52
53 /**
54 * Mark the post as currently being edited by the current user
55 *
56 * @since 2.6.0 bbPress (r6340)
57 *
58 * @param int $post_id ID of the post to being edited
59 * @return bool|array Returns false if the post doesn't exist of there is no current user, or
60 * an array of the lock time and the user ID.
61 */
62 function bbp_set_post_lock( $post_id = 0 ) {
63
64 // Bail if no post
65 if ( !$post = get_post( $post_id ) ) {
66 return false;
67 }
68
69 // Bail if no user
70 if ( 0 == ( $user_id = get_current_user_id() ) ) {
71 return false;
72 }
73
74 // Get time & lock value
75 $now = time();
76 $lock = "{$now}:{$user_id}";
77
78 // Set lock value
79 update_post_meta( $post->ID, '_edit_lock', $lock );
80
81 return array( $now, $user_id );
82 }
83