bbp_fix_post_author

Fix post author id on post save.

Description

(array) bbp_fix_post_author( (array) $data = array(), (array) $postarr = array() ); 

When a logged in user changes the status of an anonymous reply or topic, or edits it, the post_author field is set to the logged in user's id. This function fixes that.

Returns (array)

Data

Parameters (2)

0. $data — Optional. (array) => array()
The data.
1. $postarr — Optional. (array) => array()
Original post array (includes post id)

Usage

  1. if ( !function_exists( 'bbp_fix_post_author' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/common/functions.php'; 
  3.  
  4. // The data. 
  5. $data = array(); 
  6.  
  7. // Original post array (includes post id) 
  8. $postarr = array(); 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = bbp_fix_post_author($data, $postarr); 
  12.  

Defined (1)

The function is defined in the following location(s).

/includes/common/functions.php  
  1. function bbp_fix_post_author( $data = array(), $postarr = array() ) { 
  2.  
  3. // Post is not being updated or the post_author is already 0, return 
  4. if ( empty( $postarr['ID'] ) || empty( $data['post_author'] ) ) 
  5. return $data; 
  6.  
  7. // Post is not a topic or reply, return 
  8. if ( !in_array( $data['post_type'], array( bbp_get_topic_post_type(), bbp_get_reply_post_type() ) ) ) 
  9. return $data; 
  10.  
  11. // Is the post by an anonymous user? 
  12. if ( ( bbp_get_topic_post_type() === $data['post_type'] && !bbp_is_topic_anonymous( $postarr['ID'] ) ) || 
  13. ( bbp_get_reply_post_type() === $data['post_type'] && !bbp_is_reply_anonymous( $postarr['ID'] ) ) ) 
  14. return $data; 
  15.  
  16. // The post is being updated. It is a topic or a reply and is written by an anonymous user. 
  17. // Set the post_author back to 0 
  18. $data['post_author'] = 0; 
  19.  
  20. return $data;