[ Index ]

PHP Cross Reference of BBPress

title

Body

[close]

/src/includes/common/ -> locks.php (source)

   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  }


Generated: Sat Dec 21 01:00:52 2024 Cross-referenced by PHPXref 0.7.1