Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added some features that I wanted for my app. #27

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions bbp-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,93 @@
'methods' => WP_REST_Server::READABLE,
'callback' => 'bbp_api_stats',
) );

// SUBSCRIBE
$args = array(
array(
'args' => array(
'forum_id' => array(
'required' => True,
'description' => 'ID of the forum.',
'type' => 'integer',
),
'user_id' => array(
'required' => True,
'description' => 'ID of the user.',
'type' => 'integer',
),
),
'methods' => WP_REST_Server::CREATABLE,
'callback' => 'bbp_api_subscribe_forum',
),
);
// register /forum/subscribe
register_rest_route( 'bbp-api/v1', '/forum/subscribe/', $args );


$args = array(
array(
'args' => array(
'topic_id' => array(
'required' => True,
'description' => 'ID of the topic.',
'type' => 'integer',
),
'user_id' => array(
'required' => True,
'description' => 'ID of the user.',
'type' => 'integer',
),
),
'methods' => WP_REST_Server::CREATABLE,
'callback' => 'bbp_api_subscribe_topic',
),
);
// register /topic/subscribe
register_rest_route( 'bbp-api/v1', '/topic/subscribe/', $args );

// UNSUBSCRIBE
$args = array(
array(
'args' => array(
'forum_id' => array(
'required' => True,
'description' => 'ID of the forum.',
'type' => 'integer',
),
'user_id' => array(
'required' => True,
'description' => 'ID of the user.',
'type' => 'integer',
),
),
'methods' => WP_REST_Server::CREATABLE,
'callback' => 'bbp_api_unsubscribe_forum',
),
);
// register /forum/unsubscribe
register_rest_route( 'bbp-api/v1', '/forum/unsubscribe/', $args );


$args = array(
array(
'args' => array(
'topic_id' => array(
'required' => True,
'description' => 'ID of the topic.',
'type' => 'integer',
),
'user_id' => array(
'required' => True,
'description' => 'ID of the user.',
'type' => 'integer',
),
),
'methods' => WP_REST_Server::CREATABLE,
'callback' => 'bbp_api_unsubscribe_topic',
),
);
// register /topic/unsubscribe
register_rest_route( 'bbp-api/v1', '/topic/unsubscribe/', $args );

} );
110 changes: 109 additions & 1 deletion inc/forums.php
Original file line number Diff line number Diff line change
@@ -1,99 +1,207 @@
<?php

/*

* /bbp-api/forums

*/

function bbp_api_forums() {

$all_forums_data = $all_forums_ids = array();

if ( bbp_has_forums() ) {

// Get root list of forums

while ( bbp_forums() ) {

bbp_the_forum();

$forum_id = bbp_get_forum_id();

$all_forums_ids[] = $forum_id;

if ( $sublist = bbp_forum_get_subforums() ) {

foreach ( $sublist as $sub_forum ) {

$all_forums_ids[] = (int)$sub_forum->ID;

}

}

} // while

$i = 0;

foreach ( $all_forums_ids as $forum_id ) {

$all_forums_data[$i]['id'] = $forum_id;

$all_forums_data[$i]['title'] = bbp_get_forum_title( $forum_id );

$all_forums_data[$i]['parent'] = bbp_get_forum_parent_id( $forum_id );

$all_forums_data[$i]['topic_count'] = bbp_get_forum_topic_count( $forum_id );

$all_forums_data[$i]['reply_count'] = bbp_get_forum_reply_count( $forum_id );

$all_forums_data[$i]['permalink'] = bbp_get_forum_permalink( $forum_id );

$all_forums_data[$i]['content'] = bbp_get_forum_content( $forum_id );

$all_forums_data[$i]['type'] = bbp_get_forum_type( $forum_id );

$i++;

}

} // if()
return new WP_REST_Response($all_forums_data, 200);

if ( empty( $all_forums_data ) ) {

return null;

}

return $all_forums_data;

}

/*

* /bbp-api/forums/<id>

*

* per_page and page are following https://developer.wordpress.org/rest-api/using-the-rest-api/pagination/#pagination-parameters

* including the 100 maximum records

*/

function bbp_api_forums_one( $data ) {

$all_forum_data = array();

$forum_id = bbp_get_forum_id( $data['id'] );

if ($forum_id) {

$per_page = !isset($_GET['per_page']) ? 20 : $_GET['per_page'];

if ($per_page > 100) $per_page = 100;

$page = !isset($_GET['page']) ? 1 : $_GET['page'];

if(isset($_GET['user_id'])) {

$user_id = $_GET['user_id'];

$all_forum_data['subscribed'] = bbp_is_user_subscribed_to_forum ( $user_id, $forum_id);

}

$all_forum_data['id'] = $forum_id;

$all_forum_data['title'] = bbp_get_forum_title( $forum_id );

$all_forum_data['parent'] = bbp_get_forum_parent_id( $forum_id );

$all_forum_data['topic_count'] = bbp_get_forum_topic_count( $forum_id );

$all_forum_data['reply_count'] = bbp_get_forum_reply_count( $forum_id );

$all_forum_data['permalink'] = bbp_get_forum_permalink( $forum_id );

$all_forum_data['content'] = bbp_get_forum_content( $forum_id );

$all_forum_data['type'] = bbp_get_forum_type( $forum_id );

$all_forum_data['subforums'] = array();

$subforums = bbp_forum_query_subforum_ids( $forum_id );

$i = 0;

foreach ($subforums as $subforum_id) {

$all_forum_data['subforums'][$i]['id'] = $subforum_id;

$all_forum_data['subforums'][$i]['title'] = bbp_get_forum_title( $subforum_id );

$all_forum_data['subforums'][$i]['topic_count'] = bbp_get_forum_topic_count( $subforum_id );

$all_forum_data['subforums'][$i]['reply_count'] = bbp_get_forum_reply_count( $subforum_id );

$all_forum_data['subforums'][$i]['permalink'] = bbp_get_forum_permalink( $subforum_id );

$all_forum_data['subforums'][$i]['content'] = bbp_get_forum_content( $subforum_id );

$all_forum_data['subforums'][$i]['type'] = bbp_get_forum_type( $subforum_id );

$i++;

}



if ( ( $per_page * $page ) > $all_forum_data['topic_count'] ) {

// This is the last page

$all_forum_data['next_page'] = 0;

} else {

$all_forum_data['next_page'] = $page + 1;

$all_forum_data['next_page_url'] = get_site_url() . '/wp-json/bbp-api/v1/forums' . $forum_id . '?page=' . $all_forum_data['next_page'] . '&per_page=' . $per_page;

}



$i = 0;

if ( bbp_has_topics ( array( 'orderby' => 'date', 'order' => 'DESC', 'posts_per_page' => $per_page, 'paged' => $page, 'post_parent' => $forum_id ) ) );

while ( bbp_topics() ) : bbp_the_topic();

$topic_id = bbp_get_topic_id();

$all_forum_data['topics'][$i]['id'] = $topic_id;

$all_forum_data['topics'][$i]['title'] = bbp_get_topic_title( $topic_id );

$all_forum_data['topics'][$i]['reply_count'] = bbp_get_topic_reply_count( $topic_id );
$all_forum_data['topics'][$i]['voice_count'] = bbp_get_topic_voice_count( $topic_id );


$all_forum_data['topics'][$i]['permalink'] = bbp_get_topic_permalink( $topic_id );

$all_forum_data['topics'][$i]['author_name'] = bbp_get_topic_author_display_name( $topic_id );

$all_forum_data['topics'][$i]['author_avatar'] = bbp_get_topic_author_avatar( $topic_id );

$all_forum_data['topics'][$i]['post_date'] = bbp_get_topic_post_date( $topic_id );

$i++;

endwhile;

}
return new WP_REST_Response($all_forum_data, 200);

if ( empty( $all_forum_data ) ) {

return null;

}

return $all_forum_data;
}

}
Loading