bp_activity_post_type_publish

Create an activity item for a newly published post type post.

Description

(int|bool) bp_activity_post_type_publish( (int) $post_id = 0, (constant) $post = null, (int) $user_id = 0 ); 

Returns (int|bool)

The ID of the activity on success. False on error.

Parameters (3)

0. $post_id — Optional. (int)
ID of the new post.
1. $post — Optional. (constant) => null
Post object.
2. $user_id — Optional. (int)
ID of the post author.

Usage

  1. if ( !function_exists( 'bp_activity_post_type_publish' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-activity/bp-activity-functions.php'; 
  3.  
  4. // ID of the new post. 
  5. $post_id = -1; 
  6.  
  7. // Post object. 
  8. $post = null; 
  9.  
  10. // ID of the post author. 
  11. $user_id = -1; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = bp_activity_post_type_publish($post_id, $post, $user_id); 
  15.  

Defined (1)

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

/bp-activity/bp-activity-functions.php  
  1. function bp_activity_post_type_publish( $post_id = 0, $post = null, $user_id = 0 ) { 
  2.  
  3. if ( ! is_a( $post, 'WP_Post' ) ) { 
  4. return; 
  5.  
  6. // Get the post type tracking args. 
  7. $activity_post_object = bp_activity_get_post_type_tracking_args( $post->post_type ); 
  8.  
  9. if ( 'publish' != $post->post_status || ! empty( $post->post_password ) || empty( $activity_post_object->action_id ) ) { 
  10. return; 
  11.  
  12. if ( empty( $post_id ) ) { 
  13. $post_id = $post->ID; 
  14.  
  15. $blog_id = get_current_blog_id(); 
  16.  
  17. if ( empty( $user_id ) ) { 
  18. $user_id = (int) $post->post_author; 
  19.  
  20. // Bail if an activity item already exists for this post. 
  21. $existing = bp_activity_get( array( 
  22. 'filter' => array( 
  23. 'action' => $activity_post_object->action_id,  
  24. 'primary_id' => $blog_id,  
  25. 'secondary_id' => $post_id,  
  26. ) ); 
  27.  
  28. if ( ! empty( $existing['activities'] ) ) { 
  29. return; 
  30.  
  31. /** 
  32. * Filters whether or not to post the activity. 
  33. * This is a variable filter, dependent on the post type,  
  34. * that lets components or plugins bail early if needed. 
  35. * @since 2.2.0 
  36. * @param bool $value Whether or not to continue. 
  37. * @param int $blog_id ID of the current site. 
  38. * @param int $post_id ID of the current post being published. 
  39. * @param int $user_id ID of the current user or post author. 
  40. */ 
  41. if ( false === apply_filters( "bp_activity_{$post->post_type}_pre_publish", true, $blog_id, $post_id, $user_id ) ) { 
  42. return; 
  43.  
  44. // Record this in activity streams. 
  45. $blog_url = get_home_url( $blog_id ); 
  46. $post_url = add_query_arg
  47. 'p',  
  48. $post_id,  
  49. trailingslashit( $blog_url ) 
  50. ); 
  51.  
  52. // Backward compatibility filters for the 'blogs' component. 
  53. if ( 'blogs' == $activity_post_object->component_id ) { 
  54. $activity_content = apply_filters( 'bp_blogs_activity_new_post_content', $post->post_content, $post, $post_url, $post->post_type ); 
  55. $activity_primary_link = apply_filters( 'bp_blogs_activity_new_post_primary_link', $post_url, $post_id, $post->post_type ); 
  56. } else { 
  57. $activity_content = $post->post_content; 
  58. $activity_primary_link = $post_url; 
  59.  
  60. $activity_args = array( 
  61. 'user_id' => $user_id,  
  62. 'content' => $activity_content,  
  63. 'primary_link' => $activity_primary_link,  
  64. 'component' => $activity_post_object->component_id,  
  65. 'type' => $activity_post_object->action_id,  
  66. 'item_id' => $blog_id,  
  67. 'secondary_item_id' => $post_id,  
  68. 'recorded_time' => $post->post_date_gmt,  
  69. ); 
  70.  
  71. if ( ! empty( $activity_args['content'] ) ) { 
  72. // Create the excerpt. 
  73. $activity_summary = bp_activity_create_summary( $activity_args['content'], $activity_args ); 
  74.  
  75. // Backward compatibility filter for blog posts. 
  76. if ( 'blogs' == $activity_post_object->component_id ) { 
  77. $activity_args['content'] = apply_filters( 'bp_blogs_record_activity_content', $activity_summary, $activity_args['content'], $activity_args, $post->post_type ); 
  78. } else { 
  79. $activity_args['content'] = $activity_summary; 
  80.  
  81. // Set up the action by using the format functions. 
  82. $action_args = array_merge( $activity_args, array( 
  83. 'post_title' => $post->post_title,  
  84. 'post_url' => $post_url,  
  85. ) ); 
  86.  
  87. $activity_args['action'] = call_user_func_array( $activity_post_object->format_callback, array( '', (object) $action_args ) ); 
  88.  
  89. // Make sure the action is set. 
  90. if ( empty( $activity_args['action'] ) ) { 
  91. return; 
  92. } else { 
  93. // Backward compatibility filter for the blogs component. 
  94. if ( 'blogs' == $activity_post_object->component_id ) { 
  95. $activity_args['action'] = apply_filters( 'bp_blogs_record_activity_action', $activity_args['action'] ); 
  96.  
  97. $activity_id = bp_activity_add( $activity_args ); 
  98.  
  99. /** 
  100. * Fires after the publishing of an activity item for a newly published post type post. 
  101. * @since 2.2.0 
  102. * @param int $activity_id ID of the newly published activity item. 
  103. * @param WP_Post $post Post object. 
  104. * @param array $activity_args Array of activity arguments. 
  105. */ 
  106. do_action( 'bp_activity_post_type_published', $activity_id, $post, $activity_args ); 
  107.  
  108. return $activity_id;