Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
02/07/2016 04:36:40 PM (9 years ago)
Author:
imath
Message:

Post Type Activities: introduce new functions to track the post type comments

These functions are generalizing to any post type what was only performed for the "post" post type so far.

We are also editing the bp_activity_new_comment() so that it is possible to post an activity comment without sending notifications.

Props shanebp, r-a-y.

See #6482

File:
1 edited

Legend:

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

    r10542 r10543  
    22322232     *
    22332233     * @since 2.2.0
    2234      *
    2235      * @param WP_Post              $post     Post object.
    2236      * @param BP_Activity_Activity $activity Activity object.
    2237      */
    2238     do_action( 'bp_activity_post_type_updated', $post, $activity );
     2234     * @since 2.5.0 Add the post type tracking args parameter
     2235     *
     2236     * @param WP_Post              $post                 Post object.
     2237     * @param BP_Activity_Activity $activity             Activity object.
     2238     * @param object               $activity_post_object The post type tracking args object.
     2239     */
     2240    do_action( 'bp_activity_post_type_updated', $post, $activity, $activity_post_object );
    22392241
    22402242    return $updated;
     
    22922294
    22932295/**
     2296 * Create an activity item for a newly posted post type comment.
     2297 *
     2298 * @since 2.5.0
     2299 *
     2300 * @param  int  $comment_id  ID of the comment.
     2301 * @param  bool $is_approved Whether the comment is approved or not.
     2302 * @param  object $activity_post_object the post type tracking args object.
     2303 *
     2304 * @return int|bool The ID of the activity on success. False on error.
     2305 */
     2306function bp_activity_post_type_comment( $comment_id = 0, $is_approved = true, $activity_post_object = null ) {
     2307    // Get the users comment
     2308    $post_type_comment = get_comment( $comment_id );
     2309
     2310    // Don't record activity if the comment hasn't been approved
     2311    if ( empty( $is_approved ) ) {
     2312        return false;
     2313    }
     2314
     2315    // Don't record activity if no email address has been included
     2316    if ( empty( $post_type_comment->comment_author_email ) ) {
     2317        return false;
     2318    }
     2319
     2320    // Don't record activity if the comment has already been marked as spam
     2321    if ( 'spam' === $is_approved ) {
     2322        return false;
     2323    }
     2324
     2325    // Get the user by the comment author email.
     2326    $user = get_user_by( 'email', $post_type_comment->comment_author_email );
     2327
     2328    // If user isn't registered, don't record activity
     2329    if ( empty( $user ) ) {
     2330        return false;
     2331    }
     2332
     2333    // Get the user_id
     2334    $user_id = (int) $user->ID;
     2335
     2336    // Get blog and post data
     2337    $blog_id = get_current_blog_id();
     2338
     2339    // Get the post
     2340    $post_type_comment->post = get_post( $post_type_comment->comment_post_ID );
     2341
     2342    if ( ! is_a( $post_type_comment->post, 'WP_Post' ) ) {
     2343        return false;
     2344    }
     2345
     2346    /**
     2347     * Filters whether to publish activities about the comment regarding the post status
     2348     *
     2349     * @since 2.5.0
     2350     *
     2351     * @param bool true to bail, false otherwise.
     2352     */
     2353    $is_post_status_not_allowed = (bool) apply_filters( 'bp_activity_post_type_is_post_status_allowed', 'publish' !== $post_type_comment->post->post_status || ! empty( $post_type_comment->post->post_password ) );
     2354
     2355    // If this is a password protected post, or not a public post don't record the comment
     2356    if ( $is_post_status_not_allowed ) {
     2357        return false;
     2358    }
     2359
     2360    // Set post type
     2361    $post_type = $post_type_comment->post->post_type;
     2362
     2363    if ( empty( $activity_post_object ) ) {
     2364        // Get the post type tracking args.
     2365        $activity_post_object = bp_activity_get_post_type_tracking_args( $post_type );
     2366
     2367        // Bail if the activity type does not exist
     2368        if ( empty( $activity_post_object->comments_tracking->action_id ) ) {
     2369            return false;
     2370        }
     2371    }
     2372
     2373    // Set the $activity_comment_object
     2374    $activity_comment_object = $activity_post_object->comments_tracking;
     2375
     2376    /**
     2377     * Filters whether or not to post the activity about the comment.
     2378     *
     2379     * This is a variable filter, dependent on the post type,
     2380     * that lets components or plugins bail early if needed.
     2381     *
     2382     * @since 2.5.0
     2383     *
     2384     * @param bool $value      Whether or not to continue.
     2385     * @param int  $blog_id    ID of the current site.
     2386     * @param int  $post_id    ID of the current post being commented.
     2387     * @param int  $user_id    ID of the current user.
     2388     * @param int  $comment_id ID of the current comment being posted.
     2389     */
     2390    if ( false === apply_filters( "bp_activity_{$post_type}_pre_comment", true, $blog_id, $post_type_comment->post->ID, $user_id, $comment_id ) ) {
     2391        return false;
     2392    }
     2393
     2394    // Is this an update ?
     2395    $activity_id = bp_activity_get_activity_id( array(
     2396        'user_id'           => $user_id,
     2397        'component'         => $activity_comment_object->component_id,
     2398        'type'              => $activity_comment_object->action_id,
     2399        'item_id'           => $blog_id,
     2400        'secondary_item_id' => $comment_id,
     2401    ) );
     2402
     2403    // Record this in activity streams.
     2404    $comment_link = get_comment_link( $post_type_comment->comment_ID );
     2405
     2406    // Backward compatibility filters for the 'blogs' component.
     2407    if ( 'blogs' == $activity_comment_object->component_id )  {
     2408        $activity_content      = apply_filters_ref_array( 'bp_blogs_activity_new_comment_content',      array( $post_type_comment->comment_content, &$post_type_comment, $comment_link ) );
     2409        $activity_primary_link = apply_filters_ref_array( 'bp_blogs_activity_new_comment_primary_link', array( $comment_link, &$post_type_comment ) );
     2410    } else {
     2411        $activity_content      = $post_type_comment->comment_content;
     2412        $activity_primary_link = $comment_link;
     2413    }
     2414
     2415    $activity_args = array(
     2416        'id'            => $activity_id,
     2417        'user_id'       => $user_id,
     2418        'content'       => $activity_content,
     2419        'primary_link'  => $activity_primary_link,
     2420        'component'     => $activity_comment_object->component_id,
     2421        'recorded_time' => $post_type_comment->comment_date_gmt,
     2422    );
     2423
     2424    if ( bp_disable_blogforum_comments() ) {
     2425        $blog_url = get_home_url( $blog_id );
     2426        $post_url = add_query_arg(
     2427            'p',
     2428            $post_type_comment->post->ID,
     2429            trailingslashit( $blog_url )
     2430        );
     2431
     2432        $activity_args['type']              = $activity_comment_object->action_id;
     2433        $activity_args['item_id']           = $blog_id;
     2434        $activity_args['secondary_item_id'] = $post_type_comment->comment_ID;
     2435
     2436        if ( ! empty( $activity_args['content'] ) ) {
     2437            // Create the excerpt.
     2438            $activity_summary = bp_activity_create_summary( $activity_args['content'], $activity_args );
     2439
     2440            // Backward compatibility filter for blog comments.
     2441            if ( 'blogs' == $activity_post_object->component_id )  {
     2442                $activity_args['content'] = apply_filters( 'bp_blogs_record_activity_content', $activity_summary, $activity_args['content'], $activity_args, $post_type );
     2443            } else {
     2444                $activity_args['content'] = $activity_summary;
     2445            }
     2446        }
     2447
     2448        // Set up the action by using the format functions.
     2449        $action_args = array_merge( $activity_args, array(
     2450            'post_title' => $post_type_comment->post->post_title,
     2451            'post_url'   => $post_url,
     2452            'blog_url'   => $blog_url,
     2453            'blog_name'  => get_blog_option( $blog_id, 'blogname' ),
     2454        ) );
     2455
     2456        $activity_args['action'] = call_user_func_array( $activity_comment_object->format_callback, array( '', (object) $action_args ) );
     2457
     2458        // Make sure the action is set.
     2459        if ( empty( $activity_args['action'] ) ) {
     2460            return;
     2461        } else {
     2462            // Backward compatibility filter for the blogs component.
     2463            if ( 'blogs' === $activity_post_object->component_id )  {
     2464                $activity_args['action'] = apply_filters( 'bp_blogs_record_activity_action', $activity_args['action'] );
     2465            }
     2466        }
     2467
     2468        $activity_id = bp_activity_add( $activity_args );
     2469    }
     2470
     2471    /**
     2472     * Fires after the publishing of an activity item for a newly published post type post.
     2473     *
     2474     * @since 2.5.0
     2475     *
     2476     * @param int        $activity_id          ID of the newly published activity item.
     2477     * @param WP_Comment $post_type_comment    Comment object.
     2478     * @param array      $activity_args        Array of activity arguments.
     2479     * @param object     $activity_post_object the post type tracking args object.
     2480     */
     2481    do_action_ref_array( 'bp_activity_post_type_comment', array( &$activity_id, $post_type_comment, $activity_args, $activity_post_object ) );
     2482
     2483    return $activity_id;
     2484}
     2485
     2486/**
     2487 * Remove an activity item when a comment about a post type is deleted.
     2488 *
     2489 * @since 2.5.0
     2490 *
     2491 * @param  int    $comment_id           ID of the comment.
     2492 * @param  object $activity_post_object The post type tracking args object.
     2493 *
     2494 * @return bool True on success. False on error.
     2495 */
     2496function bp_activity_post_type_remove_comment( $comment_id = 0, $activity_post_object = null ) {
     2497    if ( empty( $activity_post_object ) ) {
     2498        $comment = get_comment( $comment_id );
     2499        if ( ! $comment ) {
     2500            return;
     2501        }
     2502
     2503        $post_type = get_post_type( $comment->comment_post_ID );
     2504        if ( ! $post_type ) {
     2505            return;
     2506        }
     2507
     2508        // Get the post type tracking args.
     2509        $activity_post_object = bp_activity_get_post_type_tracking_args( $post_type );
     2510
     2511        // Bail if the activity type does not exist
     2512        if ( empty( $activity_post_object->comments_tracking->action_id ) ) {
     2513            return false;
     2514        }
     2515    }
     2516
     2517    // Set the $activity_comment_object
     2518    $activity_comment_object = $activity_post_object->comments_tracking;
     2519
     2520    if ( empty( $activity_comment_object->action_id ) ) {
     2521        return false;
     2522    }
     2523
     2524    $deleted = false;
     2525
     2526    if ( bp_disable_blogforum_comments() ) {
     2527        $deleted = bp_activity_delete_by_item_id( array(
     2528            'item_id'           => get_current_blog_id(),
     2529            'secondary_item_id' => $comment_id,
     2530            'component'         => $activity_comment_object->component_id,
     2531            'type'              => $activity_comment_object->action_id,
     2532            'user_id'           => false,
     2533        ) );
     2534    }
     2535
     2536    /**
     2537     * Fires after the custom post type comment activity was removed.
     2538     *
     2539     * @since 2.5.0
     2540     *
     2541     * @param bool       $deleted              True if the activity was deleted false otherwise
     2542     * @param WP_Comment $comment              Comment object.
     2543     * @param object     $activity_post_object The post type tracking args object.
     2544     * @param string     $value                The post type comment activity type.
     2545     */
     2546    do_action( 'bp_activity_post_type_remove_comment', $deleted, $comment_id, $activity_post_object, $activity_comment_object->action_id );
     2547
     2548    return $deleted;
     2549}
     2550
     2551/**
    22942552 * Add an activity comment.
    22952553 *
    22962554 * @since 1.2.0
     2555 * @since 2.5.0 Add a new possible parameter $skip_notification for the array of arguments.
     2556 *              Add the $primary_link parameter for the array of arguments.
    22972557 *
    22982558 * @uses wp_parse_args()
     
    23042564 *
    23052565 * @param array|string $args {
    2306  *     @type int    $id          Optional. Pass an ID to update an existing comment.
    2307  *     @type string $content     The content of the comment.
    2308  *     @type int    $user_id     Optional. The ID of the user making the comment.
    2309  *                               Defaults to the ID of the logged-in user.
    2310  *     @type int    $activity_id The ID of the "root" activity item, ie the oldest
    2311  *                               ancestor of the comment.
    2312  *     @type int    $parent_id   Optional. The ID of the parent activity item, ie the item to
    2313  *                               which the comment is an immediate reply. If not provided,
    2314  *                               this value defaults to the $activity_id.
     2566 *     @type int    $id                Optional. Pass an ID to update an existing comment.
     2567 *     @type string $content           The content of the comment.
     2568 *     @type int    $user_id           Optional. The ID of the user making the comment.
     2569 *                                     Defaults to the ID of the logged-in user.
     2570 *     @type int    $activity_id       The ID of the "root" activity item, ie the oldest
     2571 *                                     ancestor of the comment.
     2572 *     @type int    $parent_id         Optional. The ID of the parent activity item, ie the item to
     2573 *                                     which the comment is an immediate reply. If not provided,
     2574 *                                     this value defaults to the $activity_id.
     2575 *     @type string $primary_link      Optional. the primary link for the comment.
     2576 *                                     Defaults to an empty string.
     2577 *     @type bool   $skip_notification Optional. false to send a comment notification, false otherwise.
     2578 *                                     Defaults to false.
    23152579 * }
    23162580 * @return int|bool The ID of the comment on success, otherwise false.
     
    23262590
    23272591    $r = wp_parse_args( $args, array(
    2328         'id'          => false,
    2329         'content'     => false,
    2330         'user_id'     => bp_loggedin_user_id(),
    2331         'activity_id' => false, // ID of the root activity item.
    2332         'parent_id'   => false  // ID of a parent comment (optional).
     2592        'id'                => false,
     2593        'content'           => false,
     2594        'user_id'           => bp_loggedin_user_id(),
     2595        'activity_id'       => false, // ID of the root activity item.
     2596        'parent_id'         => false, // ID of a parent comment (optional).
     2597        'primary_link'      => '',
     2598        'skip_notification' => false,
    23332599    ) );
    23342600
     
    23772643        'component'         => buddypress()->activity->id,
    23782644        'type'              => 'activity_comment',
     2645        'primary_link'      => $r['primary_link'],
    23792646        'user_id'           => $r['user_id'],
    23802647        'item_id'           => $activity_id,
     
    23952662    wp_cache_delete( $activity_id, 'bp_activity' );
    23962663
    2397     /**
    2398      * Fires near the end of an activity comment posting, before the returning of the comment ID.
    2399      *
    2400      * @since 1.2.0
    2401      *
    2402      * @param int                  $comment_id ID of the newly posted activity comment.
    2403      * @param array                $r          Array of parsed comment arguments.
    2404      * @param BP_Activity_Activity $activity   Activity item being commented on.
    2405      */
    2406     do_action( 'bp_activity_comment_posted', $comment_id, $r, $activity );
     2664    if ( empty( $r[ 'skip_notification' ] ) ) {
     2665        /**
     2666         * Fires near the end of an activity comment posting, before the returning of the comment ID.
     2667         * Sends a notification to the user @see bp_activity_new_comment_notification_helper().
     2668         *
     2669         * @since 1.2.0
     2670         *
     2671         * @param int   $comment_id ID of the newly posted activity comment.
     2672         * @param array $r          Array of parsed comment arguments.
     2673         * @param int   $activity   ID of the activity item being commented on.
     2674         */
     2675        do_action( 'bp_activity_comment_posted', $comment_id, $r, $activity );
     2676    } else {
     2677        /**
     2678         * Fires near the end of an activity comment posting, before the returning of the comment ID.
     2679         * without sending a notification to the user
     2680         *
     2681         * @since 2.5.0
     2682         *
     2683         * @param int   $comment_id ID of the newly posted activity comment.
     2684         * @param array $r          Array of parsed comment arguments.
     2685         * @param int   $activity   ID of the activity item being commented on.
     2686         */
     2687        do_action( 'bp_activity_comment_posted_notification_skipped', $comment_id, $r, $activity );
     2688    }
    24072689
    24082690    if ( empty( $comment_id ) ) {
     
    26732955 */
    26742956function bp_activity_delete_comment( $activity_id, $comment_id ) {
     2957    $deleted = false;
    26752958
    26762959    /**
     
    26812964     *
    26822965     * @since 1.2.0
     2966     * @since 2.5.0 Add the deleted parameter (passed by reference)
    26832967     *
    26842968     * @param bool $value       Whether BuddyPress should continue or not.
    26852969     * @param int  $activity_id ID of the root activity item being deleted.
    26862970     * @param int  $comment_id  ID of the comment being deleted.
    2687      */
    2688     if ( ! apply_filters( 'bp_activity_delete_comment_pre', true, $activity_id, $comment_id ) ) {
    2689         return false;
     2971     * @param bool $deleted     Whether the activity comment has been deleted or not.
     2972     */
     2973    if ( ! apply_filters_ref_array( 'bp_activity_delete_comment_pre', array( true, $activity_id, $comment_id, &$deleted ) ) ) {
     2974        return $deleted;
    26902975    }
    26912976
     
    26962981    if ( ! bp_activity_delete( array( 'id' => $comment_id, 'type' => 'activity_comment' ) ) ) {
    26972982        return false;
     2983    } else {
     2984        $deleted = true;
    26982985    }
    26992986
     
    27143001    do_action( 'bp_activity_delete_comment', $activity_id, $comment_id );
    27153002
    2716     return true;
     3003    return $deleted;
    27173004}
    27183005
Note: See TracChangeset for help on using the changeset viewer.