Skip to:
Content

BuddyPress.org

Changeset 10731


Ignore:
Timestamp:
05/04/2016 05:25:57 PM (6 years ago)
Author:
r-a-y
Message:

Blogs: Move activity-related code from bp-blogs-functions.php to bp-blogs-activity.php.

Instead of hardcoding activity code and needing to check for the Activity
component in bp-blogs-functions.php, this commit moves this activity code
code to bp-blogs-activity.php and also hooks this code to appropriate
blog actions.

This allows developers to easily remove said activity hook if needed.

Fixes #6937.

Location:
trunk/src/bp-blogs
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-blogs/bp-blogs-activity.php

    r10544 r10731  
    607607}
    608608
     609/** SITE TRACKING *******************************************************/
     610
     611/**
     612 * Add an activity entry for a newly-created site.
     613 *
     614 * Hooked to the 'bp_blogs_new_blog' action.
     615 *
     616 * @since 2.6.0
     617 *
     618 * @param BP_Blogs_Blog $recorded_blog Current site being recorded. Passed by reference.
     619 * @param bool          $is_private    Whether the current site being recorded is private.
     620 * @param bool          $is_recorded   Whether the current site was recorded.
     621 */
     622function bp_blogs_record_activity_on_site_creation( $recorded_blog, $is_private, $is_recorded, $no_activity ) {
     623    // Only record this activity if the blog is public.
     624    if ( ! $is_private && ! $no_activity && bp_blogs_is_blog_trackable( $recorded_blog->blog_id, $recorded_blog->user_id ) ) {
     625        bp_blogs_record_activity( array(
     626            'user_id'      => $recorded_blog->user_id,
     627            'primary_link' => apply_filters( 'bp_blogs_activity_created_blog_primary_link', bp_blogs_get_blogmeta( $recorded_blog->blog_id, 'url' ), $recorded_blog->blog_id ),
     628            'type'         => 'new_blog',
     629            'item_id'      => $recorded_blog->blog_id
     630        ) );
     631    }
     632}
     633add_action( 'bp_blogs_new_blog', 'bp_blogs_record_activity_on_site_creation', 10, 4 );
     634
     635/**
     636 * Deletes the 'new_blog' activity entry when a site is deleted.
     637 *
     638 * @since 2.6.0
     639 *
     640 * @param int $blog_id Site ID.
     641 */
     642function bp_blogs_delete_new_blog_activity_for_site( $blog_id ) {
     643    bp_blogs_delete_activity( array(
     644        'item_id'   => $blog_id,
     645        'component' => buddypress()->blogs->id,
     646        'type'      => 'new_blog'
     647    ) );
     648}
     649add_action( 'bp_blogs_remove_blog',          'bp_blogs_delete_new_blog_activity_for_site' );
     650add_action( 'bp_blogs_remove_blog_for_user', 'bp_blogs_delete_new_blog_activity_for_site' );
     651
     652/**
     653 * Delete all 'blogs' activity items for a site when the site is deleted.
     654 *
     655 * @since 2.6.0
     656 *
     657 * @param int $blog_id Site ID.
     658 */
     659function bp_blogs_delete_activity_for_site( $blog_id ) {
     660    bp_blogs_delete_activity( array(
     661        'item_id'   => $blog_id,
     662        'component' => buddypress()->blogs->id,
     663        'type'      => false
     664    ) );
     665}
     666add_action( 'bp_blogs_remove_data_for_blog', 'bp_blogs_delete_activity_for_site' );
     667
     668/**
     669 * Remove a blog post activity item from the activity stream.
     670 *
     671 * @since 1.0.0
     672 *
     673 * @param int $post_id ID of the post to be removed.
     674 * @param int $blog_id Optional. Defaults to current blog ID.
     675 * @param int $user_id Optional. Defaults to the logged-in user ID. This param
     676 *                     is currently unused in the function (but is passed to hooks).
     677 * @return bool
     678 */
     679function bp_blogs_remove_post( $post_id, $blog_id = 0, $user_id = 0 ) {
     680    global $wpdb;
     681
     682    if ( empty( $wpdb->blogid ) ) {
     683        return false;
     684    }
     685
     686    $post_id = (int) $post_id;
     687
     688    if ( ! $blog_id ) {
     689        $blog_id = (int) $wpdb->blogid;
     690    }
     691
     692    if ( ! $user_id ) {
     693        $user_id = bp_loggedin_user_id();
     694    }
     695
     696    /**
     697     * Fires before removal of a blog post activity item from the activity stream.
     698     *
     699     * @since 1.5.0
     700     *
     701     * @param int $blog_id ID of the blog associated with the post that was removed.
     702     * @param int $post_id ID of the post that was removed.
     703     * @param int $user_id ID of the user having the blog removed for.
     704     */
     705    do_action( 'bp_blogs_before_remove_post', $blog_id, $post_id, $user_id );
     706
     707    bp_blogs_delete_activity( array(
     708        'item_id'           => $blog_id,
     709        'secondary_item_id' => $post_id,
     710        'component'         => buddypress()->blogs->id,
     711        'type'              => 'new_blog_post'
     712    ) );
     713
     714    /**
     715     * Fires after removal of a blog post activity item from the activity stream.
     716     *
     717     * @since 1.0.0
     718     *
     719     * @param int $blog_id ID of the blog associated with the post that was removed.
     720     * @param int $post_id ID of the post that was removed.
     721     * @param int $user_id ID of the user having the blog removed for.
     722     */
     723    do_action( 'bp_blogs_remove_post', $blog_id, $post_id, $user_id );
     724}
     725add_action( 'delete_post', 'bp_blogs_remove_post' );
     726
    609727/** POST COMMENT SYNCHRONIZATION ****************************************/
    610728
  • trunk/src/bp-blogs/bp-blogs-functions.php

    r10730 r10731  
    280280    $is_private = !apply_filters( 'bp_is_new_blog_public', !$is_private );
    281281
    282     // Only record this activity if the activity component is active and the blog is public.
    283     if ( bp_is_active( 'activity' ) && !$is_private && !$no_activity && bp_blogs_is_blog_trackable( $blog_id, $user_id ) ) {
    284 
    285         // Record this in activity streams.
    286         bp_blogs_record_activity( array(
    287             'user_id'      => $recorded_blog->user_id,
    288             'primary_link' => apply_filters( 'bp_blogs_activity_created_blog_primary_link', $url, $recorded_blog->blog_id ),
    289             'type'         => 'new_blog',
    290             'item_id'      => $recorded_blog->blog_id
    291         ) );
    292     }
    293 
    294282    /**
    295283     * Fires after BuddyPress has been made aware of a new site for activity tracking.
     
    819807
    820808    /**
     809     * Fires after a "blog created" item has been removed from blogs
     810     * tracker and activity stream.
     811     *
     812     * @since 1.0.0
     813     *
     814     * @param int $blog_id ID of the blog who had its item removed.
     815     */
     816    do_action( 'bp_blogs_remove_blog', $blog_id );
     817}
     818add_action( 'delete_blog', 'bp_blogs_remove_blog' );
     819
     820/**
     821 * Remove a blog from the tracker for a specific user.
     822 *
     823 * @param int $user_id ID of the user for whom the blog is being removed.
     824 * @param int $blog_id ID of the blog being removed.
     825 */
     826function bp_blogs_remove_blog_for_user( $user_id, $blog_id ) {
     827
     828    $blog_id = (int) $blog_id;
     829    $user_id = (int) $user_id;
     830
     831    /**
     832     * Fires before a blog is removed from the tracker for a specific user.
     833     *
     834     * @since 1.5.0
     835     *
     836     * @param int $blog_id ID of the blog being removed.
     837     * @param int $user_id ID of the user having the blog removed for.
     838     */
     839    do_action( 'bp_blogs_before_remove_blog_for_user', $blog_id, $user_id );
     840
     841    BP_Blogs_Blog::delete_blog_for_user( $blog_id, $user_id );
     842
     843    /**
    821844     * Delete activity stream item only if the Activity component is active
    822845     *
     
    832855
    833856    /**
    834      * Fires after a "blog created" item has been removed from blogs
    835      * tracker and activity stream.
    836      *
    837      * @since 1.0.0
    838      *
    839      * @param int $blog_id ID of the blog who had its item removed.
    840      */
    841     do_action( 'bp_blogs_remove_blog', $blog_id );
    842 }
    843 add_action( 'delete_blog', 'bp_blogs_remove_blog' );
    844 
    845 /**
    846  * Remove a blog from the tracker for a specific user.
    847  *
    848  * @param int $user_id ID of the user for whom the blog is being removed.
    849  * @param int $blog_id ID of the blog being removed.
    850  */
    851 function bp_blogs_remove_blog_for_user( $user_id, $blog_id ) {
    852 
    853     $blog_id = (int) $blog_id;
    854     $user_id = (int) $user_id;
    855 
    856     /**
    857      * Fires before a blog is removed from the tracker for a specific user.
    858      *
    859      * @since 1.5.0
    860      *
    861      * @param int $blog_id ID of the blog being removed.
    862      * @param int $user_id ID of the user having the blog removed for.
    863      */
    864     do_action( 'bp_blogs_before_remove_blog_for_user', $blog_id, $user_id );
    865 
    866     BP_Blogs_Blog::delete_blog_for_user( $blog_id, $user_id );
    867 
    868     /**
    869      * Delete activity stream item only if the Activity component is active
    870      *
    871      * @see https://buddypress.trac.wordpress.org/ticket/6937
    872      */
    873     if ( bp_is_active( 'activity' ) ) {
    874         bp_blogs_delete_activity( array(
    875             'item_id'   => $blog_id,
    876             'component' => buddypress()->blogs->id,
    877             'type'      => 'new_blog'
    878         ) );
    879     }
    880 
    881     /**
    882857     * Fires after a blog has been removed from the tracker for a specific user.
    883858     *
     
    890865}
    891866add_action( 'remove_user_from_blog', 'bp_blogs_remove_blog_for_user', 10, 2 );
    892 
    893 /**
    894  * Remove a blog post activity item from the activity stream.
    895  *
    896  * @param int $post_id ID of the post to be removed.
    897  * @param int $blog_id Optional. Defaults to current blog ID.
    898  * @param int $user_id Optional. Defaults to the logged-in user ID. This param
    899  *                     is currently unused in the function (but is passed to hooks).
    900  * @return bool
    901  */
    902 function bp_blogs_remove_post( $post_id, $blog_id = 0, $user_id = 0 ) {
    903     global $wpdb;
    904 
    905     if ( empty( $wpdb->blogid ) )
    906         return false;
    907 
    908     $post_id = (int) $post_id;
    909 
    910     if ( !$blog_id )
    911         $blog_id = (int) $wpdb->blogid;
    912 
    913     if ( !$user_id )
    914         $user_id = bp_loggedin_user_id();
    915 
    916     /**
    917      * Fires before removal of a blog post activity item from the activity stream.
    918      *
    919      * @since 1.5.0
    920      *
    921      * @param int $blog_id ID of the blog associated with the post that was removed.
    922      * @param int $post_id ID of the post that was removed.
    923      * @param int $user_id ID of the user having the blog removed for.
    924      */
    925     do_action( 'bp_blogs_before_remove_post', $blog_id, $post_id, $user_id );
    926 
    927     /**
    928      * Delete activity stream item only if the Activity component is active
    929      *
    930      * @see https://buddypress.trac.wordpress.org/ticket/6937
    931      */
    932     if ( bp_is_active( 'activity' ) ) {
    933         bp_blogs_delete_activity( array(
    934             'item_id'           => $blog_id,
    935             'secondary_item_id' => $post_id,
    936             'component'         => buddypress()->blogs->id,
    937             'type'              => 'new_blog_post'
    938         ) );
    939     }
    940 
    941     /**
    942      * Fires after removal of a blog post activity item from the activity stream.
    943      *
    944      * @since 1.0.0
    945      *
    946      * @param int $blog_id ID of the blog associated with the post that was removed.
    947      * @param int $post_id ID of the post that was removed.
    948      * @param int $user_id ID of the user having the blog removed for.
    949      */
    950     do_action( 'bp_blogs_remove_post', $blog_id, $post_id, $user_id );
    951 }
    952 add_action( 'delete_post', 'bp_blogs_remove_post' );
    953867
    954868/**
     
    11301044
    11311045    /**
    1132      * Delete activity stream item only if the Activity component is active
    1133      *
    1134      * @see https://buddypress.trac.wordpress.org/ticket/6937
    1135      */
    1136     if ( bp_is_active( 'activity' ) ) {
    1137         bp_blogs_delete_activity( array(
    1138             'item_id'   => $blog_id,
    1139             'component' => buddypress()->blogs->id,
    1140             'type'      => false
    1141         ) );
    1142     }
    1143 
    1144     /**
    11451046     * Fires after all data related to a given blog has been removed from blogs tracker
    11461047     * and activity stream.
Note: See TracChangeset for help on using the changeset viewer.