bp_activity_post_type_publish( int $post_id, WP_Post|null $post = null, int $user_id )

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

Description

Parameters

$post_id

(Required) ID of the new post.

$post

(Optional) Post object.

Default value: null

$user_id

(Required) ID of the post author.

Return

(null|WP_Error|bool|int) The ID of the activity on success. False on error.

Source

File: bp-activity/bp-activity-functions.php

2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
function bp_activity_post_type_publish( $post_id = 0, $post = null, $user_id = 0 ) {
 
    if ( ! is_a( $post, 'WP_Post' ) ) {
        return;
    }
 
    // Get the post type tracking args.
    $activity_post_object = bp_activity_get_post_type_tracking_args( $post->post_type );
 
    if ( 'publish' != $post->post_status || ! empty( $post->post_password ) || empty( $activity_post_object->action_id ) ) {
        return;
    }
 
    if ( empty( $post_id ) ) {
        $post_id = $post->ID;
    }
 
    $blog_id = get_current_blog_id();
 
    if ( empty( $user_id ) ) {
        $user_id = (int) $post->post_author;
    }
 
    // Bail if an activity item already exists for this post.
    $existing = bp_activity_get( array(
        'filter' => array(
            'action'       => $activity_post_object->action_id,
            'primary_id'   => $blog_id,
            'secondary_id' => $post_id,
        )
    ) );
 
    if ( ! empty( $existing['activities'] ) ) {
        return;
    }
 
    /**
     * Filters whether or not to post the activity.
     *
     * This is a variable filter, dependent on the post type,
     * that lets components or plugins bail early if needed.
     *
     * @since BuddyPress 2.2.0
     *
     * @param bool $value   Whether or not to continue.
     * @param int  $blog_id ID of the current site.
     * @param int  $post_id ID of the current post being published.
     * @param int  $user_id ID of the current user or post author.
     */
    if ( false === apply_filters( "bp_activity_{$post->post_type}_pre_publish", true, $blog_id, $post_id, $user_id ) ) {
        return;
    }
 
    // Record this in activity feeds.
    $blog_url = get_home_url( $blog_id );
    $post_url = add_query_arg(
        'p',
        $post_id,
        trailingslashit( $blog_url )
    );
 
    // Backward compatibility filters for the 'blogs' component.
    if ( 'blogs' == $activity_post_object->component_id )  {
        $activity_content      = apply_filters( 'bp_blogs_activity_new_post_content', $post->post_content, $post, $post_url, $post->post_type );
        $activity_primary_link = apply_filters( 'bp_blogs_activity_new_post_primary_link', $post_url, $post_id, $post->post_type );
    } else {
        $activity_content      = $post->post_content;
        $activity_primary_link = $post_url;
    }
 
    $activity_args = array(
        'user_id'           => $user_id,
        'content'           => $activity_content,
        'primary_link'      => $activity_primary_link,
        'component'         => $activity_post_object->component_id,
        'type'              => $activity_post_object->action_id,
        'item_id'           => $blog_id,
        'secondary_item_id' => $post_id,
        'recorded_time'     => $post->post_date_gmt,
    );
 
    if ( ! empty( $activity_args['content'] ) ) {
        // Create the excerpt.
        $activity_summary = bp_activity_create_summary( $activity_args['content'], $activity_args );
 
        // Backward compatibility filter for blog posts.
        if ( 'blogs' == $activity_post_object->component_id )  {
            $activity_args['content'] = apply_filters( 'bp_blogs_record_activity_content', $activity_summary, $activity_args['content'], $activity_args, $post->post_type );
        } else {
            $activity_args['content'] = $activity_summary;
        }
    }
 
    // Set up the action by using the format functions.
    $action_args = array_merge( $activity_args, array(
        'post_title' => $post->post_title,
        'post_url'   => $post_url,
    ) );
 
    $activity_args['action'] = call_user_func_array( $activity_post_object->format_callback, array( '', (object) $action_args ) );
 
    // Make sure the action is set.
    if ( empty( $activity_args['action'] ) ) {
        return;
    } else {
        // Backward compatibility filter for the blogs component.
        if ( 'blogs' == $activity_post_object->component_id )  {
            $activity_args['action'] = apply_filters( 'bp_blogs_record_activity_action', $activity_args['action'] );
        }
    }
 
    $activity_id = bp_activity_add( $activity_args );
 
    /**
     * Fires after the publishing of an activity item for a newly published post type post.
     *
     * @since BuddyPress 2.2.0
     *
     * @param int     $activity_id   ID of the newly published activity item.
     * @param WP_Post $post          Post object.
     * @param array   $activity_args Array of activity arguments.
     */
    do_action( 'bp_activity_post_type_published', $activity_id, $post, $activity_args );
 
    return $activity_id;
}

Changelog

Changelog
Version Description
BuddyPress 2.2.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.