BP_REST_Forums_Endpoint::prepare_item_for_response( array $forum, WP_REST_Request $request )

Prepares component data for return as an object.

Description

Parameters

$forum

(Required) The component and its values.

$request

(Required) Full details about the request.

Return

(WP_REST_Response)

Source

File: bp-forums/classes/class-bp-rest-forums-endpoint.php

514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
public function prepare_item_for_response( $forum, $request ) {
    $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
 
    // Base fields for every post.
    $data = array(
        'id'                 => $forum->ID,
        'date'               => $this->prepare_date_response( $forum->post_date_gmt, $forum->post_date ),
        'date_gmt'           => $this->prepare_date_response( $forum->post_date_gmt ),
        'guid'               => array(
            'rendered' => apply_filters( 'get_the_guid', $forum->guid ),
            'raw'      => $forum->guid,
        ),
        'modified'           => $this->prepare_date_response( $forum->post_modified_gmt, $forum->post_modified ),
        'modified_gmt'       => $this->prepare_date_response( $forum->post_modified_gmt ),
        'password'           => $forum->post_password,
        'slug'               => $forum->post_name,
        'status'             => $forum->post_status,
        'link'               => bbp_get_forum_permalink( $forum->ID ),
        'author'             => (int) $forum->post_author,
        'parent'             => (int) $forum->post_parent,
        'menu_order'         => (int) $forum->menu_order,
        'sticky'             => is_sticky( $forum->ID ),
        'featured_media'     => array(),
        'total_topic_count'  => (int) get_post_meta( $forum->ID, '_bbp_total_topic_count', true ),
        'last_topic_id'      => (int) get_post_meta( $forum->ID, '_bbp_last_topic_id', true ),
        'total_reply_count'  => (int) get_post_meta( $forum->ID, '_bbp_total_reply_count', true ),
        'last_reply_id'      => (int) get_post_meta( $forum->ID, '_bbp_last_reply_id', true ),
        'last_active_author' => $this->get_forum_last_active_author_id( $forum->ID ),
        'last_active_time'   => $this->get_forum_last_active_time( $forum->ID ),
        'is_closed'          => bbp_is_forum_closed( $forum->ID ),
        'is_forum_category'  => bbp_is_forum_category( $forum->ID ),
    );
 
    $data['featured_media']['full'] = (
        function_exists( 'bbp_get_forum_thumbnail_src' )
        ? bbp_get_forum_thumbnail_src( $forum->ID, 'full', 'full' )
        : get_the_post_thumbnail_url( $forum->ID, 'full' )
    );
 
    $data['featured_media']['thumb'] = (
        function_exists( 'bbp_get_forum_thumbnail_src' )
        ? bbp_get_forum_thumbnail_src( $forum->ID, 'large', 'large' )
        : get_the_post_thumbnail_url( $forum->ID, 'large' )
    );
 
    $data['title'] = array(
        'raw'      => $forum->post_title,
        'rendered' => bbp_get_forum_title( $forum->ID ),
    );
 
    /* Prepare content */
    if ( ! empty( $forum->post_password ) ) {
        $this->prepare_password_response( $forum->post_password );
    }
 
    $data['short_content'] = wp_trim_excerpt( $forum->post_content );
 
    $content = apply_filters( 'the_content', $forum->post_content );
 
    $data['content'] = array(
        'raw'      => $forum->post_content,
        'rendered' => $content,
    );
 
    // Don't leave our cookie lying around: https://github.com/WP-API/WP-API/issues/1055.
    if ( ! empty( $forum->post_password ) ) {
        $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] = '';
    }
    /* -- Prepare content */
 
    $data['group'] = (
        (
            function_exists( 'bbp_is_forum_group_forum' )
            && bbp_is_forum_group_forum( $forum->ID )
            && function_exists( 'groups_get_group' )
        )
        ? $this->bp_rest_get_group( $forum->ID )
        : ''
    );
 
    // Setup subscribe/unsubscribe state.
    $data['action_states'] = $this->get_forum_action_states( $forum->ID );
 
    // current user permission.
    $data['user_permission'] = $this->get_forum_current_user_permissions( $forum->ID );
 
    $data['sub_forums'] = $this->get_sub_forums(
        array(
            'post_parent'    => $forum->ID,
            'posts_per_page' => - 1,
        )
    );
 
    $data = $this->add_additional_fields_to_object( $data, $request );
    $data = $this->filter_response_by_context( $data, $context );
 
    // @todo add prepare_links
    $response = rest_ensure_response( $data );
 
    $response->add_links( $this->prepare_links( $forum ) );
 
    /**
     * Filter a component value returned from the API.
     *
     * @param WP_REST_Response $response  The Response data.
     * @param WP_REST_Request  $request   Request used to generate the response.
     * @param array            $component The component and its values.
     *
     * @since 0.1.0
     */
    return apply_filters( 'bp_rest_forums_prepare_value', $response, $request, $forum );
}

Changelog

Changelog
Version Description
0.1.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.