diff --git a/bbp-api.php b/bbp-api.php index 7a63d5f..8fa5e5c 100644 --- a/bbp-api.php +++ b/bbp-api.php @@ -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 ); + } ); diff --git a/inc/forums.php b/inc/forums.php index a2e7255..f72fd3e 100644 --- a/inc/forums.php +++ b/inc/forums.php @@ -1,99 +1,207 @@ 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/ + * + * 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; -} + +} \ No newline at end of file diff --git a/inc/replies.php b/inc/replies.php index c0baa74..3d97dc7 100644 --- a/inc/replies.php +++ b/inc/replies.php @@ -1,66 +1,152 @@ '') ); + $all_reply_data['content'] = bbp_get_reply_content( $reply_id ); + } + return new WP_REST_Response($all_reply_data, 200); + if ( empty( $all_reply_data ) ) { + return null; + } + return $all_reply_data; + } + /* + * /bbp-api/replies + */ + function bbp_api_replies() { + // Prepared for future use + $all_replies_data = array(); + if ( empty( $all_replies_data ) ) { + return null; + } + return $all_replies_data; + } + /* + * /bbp-api/replies/ + */ + function bbp_api_replies_one( $data ) { + $all_reply_data = bbp_api_replies_info( $data['id'] ); + return new WP_REST_Response($all_reply_data, 200); + return $all_reply_data; + } + /* + * Setting up POST for new replies via API. + * Example code in BBPress here: includes/core/update.php + * Function code here: /includes/replies/functions.php + * array data: submitted information from POST requested + * required args - content, email + * return string reply_id: id number for accepted post + */ + function bbp_api_replies_post( $data ) { + //required fields in POST data - $all_reply_data = bbp_api_replies_info( $data['id'] ); + + + $all_reply_data = bbp_api_replies_info( $data['id'] )->data; + $all_reply_data['content'] = $data['content']; + $all_reply_data['email'] = $data['email']; + $myuser = get_user_by( "email", $data['email'] ); + $reply_id = bbp_insert_reply( + array( + 'post_parent' => $all_reply_data['topic_id'], + 'post_title' => $all_reply_data['title'], + 'post_content' => $all_reply_data['content'], + 'post_author' => $myuser->ID, + ), + array( + 'forum_id' => $all_reply_data['forum_id'], + 'topic_id' => $all_reply_data['topic_id'], + ) + ); + + $reply = []; + + $reply['id'] = $reply_id; + + $reply['title'] = bbp_get_reply_title( $reply_id ); + + $reply['permalink'] = bbp_get_reply_permalink( $reply_id ); + + $reply['author_name'] = bbp_get_reply_author_display_name( $reply_id ); + + $reply['author_avatar'] = bbp_get_reply_author_avatar( $reply_id ); + + $reply['post_date'] = bbp_get_reply_post_date( $reply_id ); + + $reply['content'] = bbp_get_reply_content( $reply_id ); + + return new WP_REST_Response($reply, 200); + return $reply_id; -} + +} \ No newline at end of file diff --git a/inc/subscribe.php b/inc/subscribe.php new file mode 100644 index 0000000..127f7e2 --- /dev/null +++ b/inc/subscribe.php @@ -0,0 +1,98 @@ + + */ + function bbp_api_topics_one( $data ) { + $all_topic_data = array(); + $topic_id = bbp_get_topic_id( $data['id'] ); + if ( $topic_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_topic_data['subscribed'] = bbp_is_user_subscribed_to_topic ( $user_id, $topic_id); + + } + $all_topic_data['id'] = $topic_id; + $all_topic_data['title'] = bbp_get_topic_title( $topic_id ); + $all_topic_data['reply_count'] = bbp_get_topic_reply_count( $topic_id ); + $all_topic_data['permalink'] = bbp_get_topic_permalink( $topic_id ); + $all_topic_data['tags'] = bbp_get_topic_tag_list( $topic_id, array('before' => '') ); + $all_topic_data['last_reply'] = bbp_get_topic_last_reply_id( $topic_id ); + $all_topic_data['author_name'] = bbp_get_topic_author_display_name( $topic_id ); + $all_topic_data['author_avatar'] = bbp_get_topic_author_avatar( $topic_id ); + $all_topic_data['post_date'] = bbp_get_topic_post_date( $topic_id ); + $all_topic_data['content'] = bbp_get_topic_content( $topic_id ); + + if ( ( $per_page * $page ) > $all_topic_data['reply_count'] ) { + // This is the last page + $all_topic_data['next_page'] = 0; + } else { + $all_topic_data['next_page'] = $page + 1; + $all_topic_data['next_page_url'] = get_site_url() . '/wp-json/bbp-api/v1/topics/' . $forum_id . '?page=' . $all_topic_data['next_page'] . '&per_page=' . $per_page; + } + + $i = 0; - if ( bbp_has_replies ( array( 'orderby' => 'date', 'order' => 'DESC', 'posts_per_page' => $per_page, 'paged' => $page, 'post_parent' => $topic_id ) ) ); + + if ( bbp_has_replies ( array( 'orderby' => 'date', 'order' => 'ASC', 'posts_per_page' => $per_page, 'paged' => $page, 'post_parent' => $topic_id ) ) ); + while ( bbp_replies() ) : bbp_the_reply(); + $reply_id = bbp_get_reply_id(); + if ($reply_id != $topic_id) { + // not sure why in the list the topic is seen as a reply too, so this 'if' should remove it + $all_topic_data['replies'][$i]['id'] = $reply_id; + $all_topic_data['replies'][$i]['title'] = bbp_get_reply_title( $reply_id ); + $all_topic_data['replies'][$i]['permalink'] = bbp_get_reply_permalink( $reply_id ); + $all_topic_data['replies'][$i]['author_name'] = bbp_get_reply_author_display_name( $reply_id ); + $all_topic_data['replies'][$i]['author_avatar'] = bbp_get_reply_author_avatar( $reply_id ); + $all_topic_data['replies'][$i]['post_date'] = bbp_get_reply_post_date( $reply_id ); + + $all_topic_data['replies'][$i]['content'] = bbp_get_reply_content( $reply_id ); + $i++; + } + endwhile; + + } + if ( empty( $all_topic_data ) ) { + return null; + } + + // Set Page count headers + $total_pages = ceil($all_topic_data['reply_count'] /$per_page); + header("X-WP-Total: " . $all_topic_data['reply_count'] ); + header("X-WP-TotalPages: " . $total_pages); + + return new WP_REST_Response($all_topic_data, 200); + return $all_topic_data; + } + /* + * Setting up POST for new topics via API. + * Example code in BBPress here: includes/core/update.php + * Function code here: /includes/topics/functions.php + * array data: submitted information from POST requested + * required args - content, title, forum_id, email + * return string reply_id: id number for accepted post + */ + function bbp_api_topics_post( $data ) { + //required fields in POST data + $all_topic_data['content'] = $data['content']; + $all_topic_data['title'] = $data['title']; + $all_topic_data['forum_id'] = $data['forum_id']; + $all_topic_data['email'] = $data['email']; + $myuser = get_user_by( "email", $data['email'] ); + $reply_id = bbp_insert_topic( + array( + 'post_parent' => $all_topic_data['forum_id'], + 'post_title' => $all_topic_data['title'], + 'post_content' => $all_topic_data['content'], + 'post_author' => $myuser->ID, + ), + array( + 'forum_id' => $all_topic_data['forum_id'], + ) + ); + + $reply = []; + + $reply['id'] = $reply_id; + + $reply['title'] = bbp_get_reply_title( $reply_id ); + + $reply['permalink'] = bbp_get_reply_permalink( $reply_id ); + + $reply['author_name'] = bbp_get_reply_author_display_name( $reply_id ); + + $reply['author_avatar'] = bbp_get_reply_author_avatar( $reply_id ); + + $reply['post_date'] = bbp_get_reply_post_date( $reply_id ); + + $reply['content'] = bbp_get_reply_content( $reply_id ); + + $reply['reply_count'] = bbp_get_topic_reply_count( $reply_id ); + + $reply['voice_count'] = bbp_get_topic_voice_count( $reply_id ); + + + return new WP_REST_Response($reply, 200); + return $reply_id; -} + +} \ No newline at end of file diff --git a/readme.txt b/readme.txt new file mode 100644 index 0000000..d689624 --- /dev/null +++ b/readme.txt @@ -0,0 +1,79 @@ +=== bbPress API === +Contributors: casiepa +Donate link: http://casier.eu/wp-dev/ +Tags: bbpress,api,rest,rest api +Requires at least: 4.7 +Tested up to: 4.8 +Stable tag: 1.0.4 +License: GPLv2 or later +License URI: http://www.gnu.org/licenses/gpl-2.0.html + +A first attempt for a bbPress API. + +== Description == +A first attempt for a bbPress API. + +**WARNING This API will show all forums, topics, replies that bbPress has access to. If you have any extra plugin to restrict bbPress content, please double and triple check that everything works correctly.** + +Current routes for READING (GET): + +* /wp-json/bbp-api/v1/forums/ (list all forums) +* /wp-json/bbp-api/v1/forums/*id* (includes latest topics and subforums) +* /wp-json/bbp-api/v1/topics/*id* (includes latest replies) +* /wp-json/bbp-api/v1/replies/*id* (show one reply) +* /wp-json/bbp-api/v1/topic-tags/ +* /wp-json/bbp-api/v1/stats/ + +Parameters for /forums/*id* and /topics/*id* (following https://developer.wordpress.org/rest-api/using-the-rest-api/pagination/#pagination-parameters ) + +* per_page (records per page) +* page (page number) + +Current routes for WRITING (POST): + +* /wp-json/bbp-api/v1/topics/ (create a new topic) +* /wp-json/bbp-api/v1/replies/*id* (create a reply to a reply) +* (Next version: /wp-json/bbp-api/v1/topics/*id* (create a reply to a topic) ) + +Follow development on https://github.com/ePascalC/bbp-API/ ! + +Many thanks and credits to: + +* Daniel Turton (mistertwo) for the topics and replies POST functions +* Tony Korologos (@tkserver) for his input and testing with his app + +Consider also the following plugins: + +* bbP Toolkit +* bbP Manage Subscriptions +* bbP Move Topics + +== Installation == +Option 1: + +1. On your dashboard, go to Plugins > Add new +1. search for *bbP API* +1. install and activate the plugin + +Option 2: + +1. Unzip the contents to the "/wp-content/plugins/" directory. +1. Activate the plugin through the "Plugins" menu in WordPress. + +== Frequently Asked Questions == += Can I make feature requests = +Of course ! Just post something on the support tab + += I love your tool = +Thanks. Please leave a review or donate 1 or 2 EUR/USD for a coffee. + +== Changelog == += 1.0.2 = +* Added the subforums to /forums/*id* +* Added topic-tags + += 1.0.1 = +* Added the stats route + += 1.0.0 = +* Initial release with basic routes \ No newline at end of file