├── README.md ├── bb-api.php └── lib ├── class-bbp-json-forums.php ├── class-bbp-json-replies.php └── class-bbp-json-topics.php /README.md: -------------------------------------------------------------------------------- 1 | BB-API 2 | ====== 3 | 4 | Adds bbPress forum, topic and reply routes to the [WP JSON API](https://github.com/WP-API/WP-API). 5 | 6 | Example endpoints: 7 | 8 | * `http://example.com/wp-json/forums` 9 | * `http://example.com/wp-json/forums/` 10 | * `http://example.com/wp-json/topics` 11 | * `http://example.com/wp-json/topics/` 12 | * `http://example.com/wp-json/topics//replies` 13 | * `http://example.com/wp-json/users//topics` 14 | * `http://example.com/wp-json/replies` 15 | * `http://example.com/wp-json/replies/` 16 | 17 | 18 | Usage 19 | ===== 20 | 21 | Install an activate like any other WordPress plugin. 22 | 23 | Alpha version. Use in development environments only. 24 | -------------------------------------------------------------------------------- /bb-api.php: -------------------------------------------------------------------------------- 1 | WP API to create a JSON-based REST API for bbPress forums, topics & replies. 5 | Author: Brent Shepherd 6 | Author URI: http://brent.io 7 | Version: 1.0 8 | 9 | This program is free software: you can redistribute it and/or modify 10 | it under the terms of the GNU General Public License as published by 11 | the Free Software Foundation, either version 3 of the License, or 12 | (at your option) any later version. 13 | 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | 19 | You should have received a copy of the GNU General Public License 20 | along with this program. If not, see . 21 | */ 22 | /** 23 | * @package bbPress JSON API 24 | * @version 1.0 25 | * @author Brent Shepherd 26 | * @copyright Copyright (c) 2013 Brent Shepherd 27 | * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html 28 | */ 29 | 30 | 31 | /** 32 | * Register the default JSON API filters 33 | * 34 | * Order is important here, because a topic has many replies, the $bbp_json_replies global 35 | * needs to be set before creating an instace of 36 | * 37 | * @internal This will live in default-filters.php 38 | */ 39 | function bbp_json_api_filters( $server ) { 40 | global $bbp_json_forums, $bbp_json_topics, $bbp_json_replies; 41 | 42 | // Replies 43 | if ( ! class_exists( 'BBP_JSON_Replies' ) ) { 44 | require_once( 'lib/class-bbp-json-replies.php' ); 45 | } 46 | 47 | $bbp_json_replies = new BBP_JSON_Replies( $server ); 48 | $bbp_json_replies->register_filters(); 49 | 50 | // Topics 51 | if ( ! class_exists( 'BBP_JSON_Topics' ) ) { 52 | require_once( 'lib/class-bbp-json-topics.php' ); 53 | } 54 | 55 | $bbp_json_topics = new BBP_JSON_Topics( $server ); 56 | $bbp_json_topics->register_filters(); 57 | 58 | // Forums 59 | if ( ! class_exists( 'BBP_JSON_Forums' ) ) { 60 | require_once( 'lib/class-bbp-json-forums.php' ); 61 | } 62 | 63 | $bbp_json_forums = new BBP_JSON_Forums( $server ); 64 | $bbp_json_forums->register_filters(); 65 | 66 | } 67 | add_action( 'wp_json_server_before_serve', 'bbp_json_api_filters', 10, 1 ); 68 | 69 | -------------------------------------------------------------------------------- /lib/class-bbp-json-forums.php: -------------------------------------------------------------------------------- 1 | base . '/(?P\d+)/topics'] = array( 49 | array( array( $this, 'get_posts_by_parent' ), WP_JSON_Server::READABLE ), 50 | ); 51 | 52 | return $routes; 53 | } 54 | 55 | /** 56 | * Prepare post data 57 | * 58 | * @param array $post The unprepared post data 59 | * @param array $fields The subset of post type fields to return 60 | * @return array The prepared post data 61 | */ 62 | protected function prepare_post( $post, $context = 'view' ) { 63 | $_post = parent::prepare_post( $post, $context ); 64 | 65 | // Override entity meta keys with the correct links 66 | $_post['meta']['links']['self'] = json_url( $this->base . '/' . get_page_uri( $post['ID'] ) ); 67 | 68 | if ( ! empty( $post['post_parent'] ) ) 69 | $_post['meta']['links']['up'] = json_url( $this->base . '/' . get_page_uri( (int) $post['post_parent'] ) ); 70 | 71 | return apply_filters( 'json_prepare_page', $_post, $post, $context ); 72 | } 73 | 74 | /** 75 | * Get the topics that a user created 76 | * 77 | * Kind of like bbp_get_user_topics_started() 78 | * 79 | * @see WP_JSON_Posts::get_posts() 80 | */ 81 | public function get_posts_by_parent( $id, $context = 'view', $page = 1 ) { 82 | global $bbp_json_topics; 83 | 84 | return $bbp_json_topics->get_posts( array( 'post_parent' => $id ), $context ); 85 | } 86 | } -------------------------------------------------------------------------------- /lib/class-bbp-json-replies.php: -------------------------------------------------------------------------------- 1 | base . '/' . get_page_uri( $post['ID'] ) ); 61 | 62 | if ( ! empty( $post['post_parent'] ) ) 63 | $_post['meta']['links']['up'] = json_url( $this->base . '/' . get_page_uri( (int) $post['post_parent'] ) ); 64 | 65 | return apply_filters( 'json_prepare_page', $_post, $post, $context ); 66 | } 67 | 68 | /** 69 | * When inserting a new reply, make sure the protected meta data is set correctly. 70 | * 71 | * We can't use WP_JSON_Posts::add_meta() here because the required meta is deemed 72 | * protected by @see is_protected_meta(). 73 | * 74 | * @see WP_JSON_Posts::insert_post() 75 | * @see WP_JSON_Posts::add_meta() 76 | */ 77 | public function add_protected_meta( $post, $data, $update ) { 78 | 79 | if ( ! $update && $this->type == $post['post_type'] ) { 80 | 81 | // Forum meta 82 | $reply_meta = array( 83 | 'author_ip' => bbp_current_author_ip(), 84 | 'forum_id' => bbp_get_topic_forum_id( $post['post_parent'] ), 85 | 'topic_id' => $post['post_parent'], 86 | ); 87 | 88 | // Insert reply meta 89 | foreach ( $reply_meta as $meta_key => $meta_value ) { 90 | update_post_meta( $post['ID'], '_bbp_' . $meta_key, $meta_value ); 91 | } 92 | 93 | // Update the topic 94 | $topic_id = bbp_get_reply_topic_id( $post['ID'] ); 95 | if ( !empty( $topic_id ) ) { 96 | bbp_update_topic( $topic_id ); 97 | } 98 | } 99 | } 100 | } -------------------------------------------------------------------------------- /lib/class-bbp-json-topics.php: -------------------------------------------------------------------------------- 1 | base . '/(?P\d+)/replies'] = array( 61 | array( array( $this, 'get_posts_by_parent' ), WP_JSON_Server::READABLE ), 62 | ); 63 | 64 | // Add user's topics route 65 | $routes['/users/(?P\d+)' . $this->base] = array( 66 | array( array( $this, 'get_users_posts' ), WP_JSON_Server::READABLE ), 67 | ); 68 | 69 | return $routes; 70 | } 71 | 72 | /** 73 | * Prepare post data 74 | * 75 | * @param array $post The unprepared post data 76 | * @param array $fields The subset of post type fields to return 77 | * @return array The prepared post data 78 | */ 79 | protected function prepare_post( $post, $context = 'view' ) { 80 | $_post = parent::prepare_post( $post, $context ); 81 | 82 | // Override entity meta keys with the correct links 83 | $_post['meta']['links']['self'] = json_url( $this->base . '/' . get_page_uri( $post['ID'] ) ); 84 | 85 | if ( ! empty( $post['post_parent'] ) ) 86 | $_post['meta']['links']['up'] = json_url( $this->base . '/' . get_page_uri( (int) $post['post_parent'] ) ); 87 | 88 | return apply_filters( 'json_prepare_page', $_post, $post, $context ); 89 | } 90 | 91 | /** 92 | * Get the topics that a user created 93 | * 94 | * Kind of like bbp_get_user_topics_started() 95 | * 96 | * @see WP_JSON_Posts::get_posts() 97 | */ 98 | public function get_users_posts( $user, $context = 'view' ) { 99 | 100 | return parent::get_posts( array( 'author' => $user ), $context ); 101 | } 102 | 103 | /** 104 | * Get the topics that a user created 105 | * 106 | * Kind of like bbp_get_user_topics_started() 107 | * 108 | * @see WP_JSON_Posts::get_posts() 109 | */ 110 | public function get_posts_by_parent( $id, $context = 'view' ) { 111 | global $bbp_json_replies; 112 | 113 | return $bbp_json_replies->get_posts( array( 'post_parent' => $id ), $context ); 114 | } 115 | 116 | /** 117 | * When inserting a new topic, make sure the protected meta data is set correctly. 118 | * 119 | * We can't use WP_JSON_Posts::add_meta() here because the required meta is deemed 120 | * protected by @see is_protected_meta(). 121 | * 122 | * @see WP_JSON_Posts::insert_post() 123 | * @see WP_JSON_Posts::add_meta() 124 | */ 125 | public function add_protected_meta( $post, $data, $update ) { 126 | 127 | if ( ! $update && $this->type == $post['post_type'] ) { 128 | 129 | $topic_meta = array( 130 | 'author_ip' => bbp_current_author_ip(), 131 | 'forum_id' => $post['post_parent'], 132 | 'topic_id' => $post['ID'], 133 | 'voice_count' => 1, 134 | 'reply_count' => 0, 135 | 'reply_count_hidden' => 0, 136 | 'last_reply_id' => 0, 137 | 'last_active_id' => $post['ID'], 138 | 'last_active_time' => get_post_field( 'post_date', $post['ID'], 'db' ), 139 | ); 140 | 141 | // Insert topic meta 142 | foreach ( $topic_meta as $meta_key => $meta_value ) { 143 | update_post_meta( $post['ID'], '_bbp_' . $meta_key, $meta_value ); 144 | } 145 | 146 | // Update the forum 147 | if ( ! empty( $post['post_parent'] ) ) { 148 | bbp_update_forum( array( 'forum_id' => $forum_id ) ); 149 | } 150 | } 151 | } 152 | } --------------------------------------------------------------------------------