bp_blogs_format_activity_action_new_blog_post

Format 'new_blog_post' activity actions.

Description

(string) bp_blogs_format_activity_action_new_blog_post( (string) $action, (object) $activity ); 

Returns (string)

Constructed activity action.

Parameters (2)

0. $action (string)
Static activity action.
1. $activity (object)
Activity data object.

Usage

  1. if ( !function_exists( 'bp_blogs_format_activity_action_new_blog_post' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-blogs/bp-blogs-activity.php'; 
  3.  
  4. // Static activity action. 
  5. $action = ''; 
  6.  
  7. // Activity data object. 
  8. $activity = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = bp_blogs_format_activity_action_new_blog_post($action, $activity); 
  12.  

Defined (1)

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

/bp-blogs/bp-blogs-activity.php  
  1. function bp_blogs_format_activity_action_new_blog_post( $action, $activity ) { 
  2. $blog_url = bp_blogs_get_blogmeta( $activity->item_id, 'url' ); 
  3. $blog_name = bp_blogs_get_blogmeta( $activity->item_id, 'name' ); 
  4.  
  5. if ( empty( $blog_url ) || empty( $blog_name ) ) { 
  6. $blog_url = get_home_url( $activity->item_id ); 
  7. $blog_name = get_blog_option( $activity->item_id, 'blogname' ); 
  8.  
  9. bp_blogs_update_blogmeta( $activity->item_id, 'url', $blog_url ); 
  10. bp_blogs_update_blogmeta( $activity->item_id, 'name', $blog_name ); 
  11.  
  12. /** 
  13. * When the post is published we are faking an activity object 
  14. * to which we add 2 properties : 
  15. * - the post url 
  16. * - the post title 
  17. * This is done to build the 'post link' part of the activity 
  18. * action string. 
  19. * NB: in this case the activity has not yet been created. 
  20. */ 
  21. if ( isset( $activity->post_url ) ) { 
  22. $post_url = $activity->post_url; 
  23.  
  24. /** 
  25. * The post_url property is not set, we need to build the url 
  26. * thanks to the post id which is also saved as the secondary 
  27. * item id property of the activity object. 
  28. */ 
  29. } else { 
  30. $post_url = add_query_arg( 'p', $activity->secondary_item_id, trailingslashit( $blog_url ) ); 
  31.  
  32. // Should be the case when the post has just been published. 
  33. if ( isset( $activity->post_title ) ) { 
  34. $post_title = $activity->post_title; 
  35.  
  36. // If activity already exists try to get the post title from activity meta. 
  37. } else if ( ! empty( $activity->id ) ) { 
  38. $post_title = bp_activity_get_meta( $activity->id, 'post_title' ); 
  39.  
  40. /** 
  41. * In case the post was published without a title 
  42. * or the activity meta was not found. 
  43. */ 
  44. if ( empty( $post_title ) ) { 
  45. // Defaults to no title. 
  46. $post_title = esc_html__( '(no title)', buddypress ); 
  47.  
  48. switch_to_blog( $activity->item_id ); 
  49.  
  50. $post = get_post( $activity->secondary_item_id ); 
  51. if ( is_a( $post, 'WP_Post' ) ) { 
  52. // Does the post have a title ? 
  53. if ( ! empty( $post->post_title ) ) { 
  54. $post_title = $post->post_title; 
  55.  
  56. // Make sure the activity exists before saving the post title in activity meta. 
  57. if ( ! empty( $activity->id ) ) { 
  58. bp_activity_update_meta( $activity->id, 'post_title', $post_title ); 
  59.  
  60.  
  61. // Build the 'post link' part of the activity action string. 
  62. $post_link = '<a href="' . esc_url( $post_url ) . '">' . $post_title . '</a>'; 
  63.  
  64. $user_link = bp_core_get_userlink( $activity->user_id ); 
  65.  
  66. // Build the complete activity action string. 
  67. if ( is_multisite() ) { 
  68. $action = sprintf( __( '%1$s wrote a new post, %2$s, on the site %3$s', buddypress ), $user_link, $post_link, '<a href="' . esc_url( $blog_url ) . '">' . esc_html( $blog_name ) . '</a>' ); 
  69. } else { 
  70. $action = sprintf( __( '%1$s wrote a new post, %2$s', buddypress ), $user_link, $post_link ); 
  71.  
  72. // Legacy filter - requires the post object. 
  73. if ( has_filter( 'bp_blogs_activity_new_post_action' ) ) { 
  74. switch_to_blog( $activity->item_id ); 
  75. $post = get_post( $activity->secondary_item_id ); 
  76.  
  77. if ( ! empty( $post ) && ! is_wp_error( $post ) ) { 
  78. $action = apply_filters( 'bp_blogs_activity_new_post_action', $action, $post, $post_url ); 
  79.  
  80. /** 
  81. * Filters the new blog post action for the new blog. 
  82. * @since 2.0.0 
  83. * @param string $action Constructed activity action. 
  84. * @param object $activity Activity data object. 
  85. */