| 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 |
$post = get_post( $post_id ); |
| 27 |
if ( empty( $post ) ) { |
| 28 |
return false; |
| 29 |
} |
| 30 |
|
| 31 |
// Bail if no lock |
| 32 |
$lock = get_post_meta( $post->ID, '_edit_lock', true ); |
| 33 |
if ( empty( $lock ) ) { |
| 34 |
return false; |
| 35 |
} |
| 36 |
|
| 37 |
// Get lock |
| 38 |
$lock = explode( ':', $lock ); |
| 39 |
$time = $lock[0]; |
| 40 |
$user = (int) isset( $lock[1] ) |
| 41 |
? $lock[1] |
| 42 |
: get_post_meta( $post->ID, '_edit_last', true ); |
| 43 |
|
| 44 |
// Filter editing window duration |
| 45 |
$time_window = apply_filters( 'bbp_check_post_lock_window', 3 * MINUTE_IN_SECONDS ); |
| 46 |
|
| 47 |
// Return user who is or last edited |
| 48 |
if ( ! empty( $time ) && ( $time > ( time() - $time_window ) ) && ( bbp_get_current_user_id() ) !== $user ) { |
| 49 |
return (int) $user; |
| 50 |
} |
| 51 |
|
| 52 |
return false; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Mark the post as currently being edited by the current user. |
| 57 |
* |
| 58 |
* @since 2.6.0 bbPress (r6340) |
| 59 |
* |
| 60 |
* @param int $post_id ID of the post to being edited. |
| 61 |
* @return bool|array Returns false if the post doesn't exist of there is no current user, or |
| 62 |
* an array of the lock time and the user ID. |
| 63 |
*/ |
| 64 |
function bbp_set_post_lock( $post_id = 0 ) { |
| 65 |
|
| 66 |
// Bail if no post |
| 67 |
$post = get_post( $post_id ); |
| 68 |
if ( empty( $post ) ) { |
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
// Bail if no user |
| 73 |
$user_id = get_current_user_id(); |
| 74 |
if ( empty( $user_id ) ) { |
| 75 |
return false; |
| 76 |
} |
| 77 |
|
| 78 |
// Get time & lock value |
| 79 |
$now = time(); |
| 80 |
$lock = "{$now}:{$user_id}"; |
| 81 |
|
| 82 |
// Set lock value |
| 83 |
update_post_meta( $post->ID, '_edit_lock', $lock ); |
| 84 |
|
| 85 |
return array( $now, $user_id ); |
| 86 |
} |
| 87 |
|