Advertisement
designbymerovingi

myCRED: Change post author on purchase

Jul 11th, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.32 KB | None | 0 0
  1. /**
  2.  * Change Post Ownership
  3.  * Changes the post author to the user who purchased the post using
  4.  * the myCRED Sell Content add-on.
  5.  * @requires myCRED 1.4 or higher
  6.  * @version 1.1
  7.  */
  8. add_filter( 'mycred_add', 'change_post_ownership_on_content_purchase', 2, 3 );
  9. function change_post_ownership_on_content_purchase( $reply, $request, $mycred ) {
  10.  
  11.     // Make sure we hook in when someone purchases content
  12.     if ( $request['ref'] != 'buy_content' ) return $reply;
  13.  
  14.     // Make sure this is not the author payout (if used)
  15.     if ( !isset( $request['data']['seller'] ) ) return $reply;
  16.  
  17.     // Start by getting the post from the reference id
  18.     $post = get_post( (int) $request['ref_id'] );
  19.  
  20.     // Make sure the post still exist
  21.     if ( isset( $post->post_author ) && $post->post_author != $request['user_id'] ) {
  22.         $new_post_args = array();
  23.         // Supply ID to indicate that this is an update
  24.         $new_post_args['ID'] = (int) $request['ref_id'];
  25.         // Items required for post update
  26.         $new_post_args['post_title'] = $post->post_title;
  27.         $new_post_args['post_content'] = $post->post_content;
  28.         $new_post_args['post_status'] = $post->post_status;
  29.         $new_post_args['post_type'] = $post->post_type;
  30.         // New author
  31.         $new_post_args['post_author'] = $request['user_id'];
  32.         // Update
  33.         wp_insert_post( $new_post_args );
  34.     }
  35.  
  36.     return $reply;
  37.  
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement