bbPress_Jetpack_REST_API

BbPress & Jetpack REST API Compatibility Enables bbPress to work with the Jetpack REST API.

Defined (1)

The class is defined in the following location(s).

/class.jetpack-bbpress-json-api-compat.php  
  1. class bbPress_Jetpack_REST_API { 
  2.  
  3. private static $instance; 
  4.  
  5. public static function instance() { 
  6. if ( isset( self::$instance ) ) 
  7. return self::$instance; 
  8.  
  9. self::$instance = new self; 
  10.  
  11. private function __construct() { 
  12. add_filter( 'rest_api_allowed_post_types', array( $this, 'allow_bbpress_post_types' ) ); 
  13. add_filter( 'bbp_map_meta_caps', array( $this, 'adjust_meta_caps' ), 10, 4 ); 
  14. add_filter( 'rest_api_allowed_public_metadata', array( $this, 'allow_bbpress_public_metadata' ) ); 
  15.  
  16. function allow_bbpress_post_types( $allowed_post_types ) { 
  17.  
  18. // only run for REST API requests 
  19. if ( ! defined( 'REST_API_REQUEST' ) || ! REST_API_REQUEST ) 
  20. return $allowed_post_types; 
  21.  
  22. $allowed_post_types[] = 'forum'; 
  23. $allowed_post_types[] = 'topic'; 
  24. $allowed_post_types[] = 'reply'; 
  25. return $allowed_post_types; 
  26.  
  27. function allow_bbpress_public_metadata( $allowed_meta_keys ) { 
  28.  
  29. // only run for REST API requests 
  30. if ( ! defined( 'REST_API_REQUEST' ) || ! REST_API_REQUEST ) 
  31. return $allowed_meta_keys; 
  32.  
  33. $allowed_meta_keys[] = '_bbp_forum_id'; 
  34. $allowed_meta_keys[] = '_bbp_topic_id'; 
  35. $allowed_meta_keys[] = '_bbp_status'; 
  36. $allowed_meta_keys[] = '_bbp_forum_type'; 
  37. $allowed_meta_keys[] = '_bbp_forum_subforum_count'; 
  38. $allowed_meta_keys[] = '_bbp_reply_count'; 
  39. $allowed_meta_keys[] = '_bbp_total_reply_count'; 
  40. $allowed_meta_keys[] = '_bbp_topic_count'; 
  41. $allowed_meta_keys[] = '_bbp_total_topic_count'; 
  42. $allowed_meta_keys[] = '_bbp_topic_count_hidden'; 
  43. $allowed_meta_keys[] = '_bbp_last_topic_id'; 
  44. $allowed_meta_keys[] = '_bbp_last_reply_id'; 
  45. $allowed_meta_keys[] = '_bbp_last_active_time'; 
  46. $allowed_meta_keys[] = '_bbp_last_active_id'; 
  47. $allowed_meta_keys[] = '_bbp_sticky_topics'; 
  48. $allowed_meta_keys[] = '_bbp_voice_count'; 
  49. $allowed_meta_keys[] = '_bbp_reply_count_hidden'; 
  50. $allowed_meta_keys[] = '_bbp_anonymous_reply_count'; 
  51.  
  52. return $allowed_meta_keys; 
  53.  
  54. function adjust_meta_caps( $caps, $cap, $user_id, $args ) { 
  55.  
  56. // only run for REST API requests 
  57. if ( ! defined( 'REST_API_REQUEST' ) || ! REST_API_REQUEST ) 
  58. return $caps; 
  59.  
  60. // only modify caps for meta caps and for bbPress meta keys 
  61. if ( ! in_array( $cap, array( 'edit_post_meta', 'delete_post_meta', 'add_post_meta' ) ) || empty( $args[1] ) || false === strpos( $args[1], '_bbp_' ) ) 
  62. return $caps; 
  63.  
  64. // $args[0] could be a post ID or a post_type string 
  65. if ( is_int( $args[0] ) ) { 
  66. $_post = get_post( $args[0] ); 
  67. if ( ! empty( $_post ) ) { 
  68. $post_type = get_post_type_object( $_post->post_type ); 
  69. } elseif ( is_string( $args[0] ) ) { 
  70. $post_type = get_post_type_object( $args[0] ); 
  71.  
  72. // no post type found, bail 
  73. if ( empty( $post_type ) ) 
  74. return $caps; 
  75.  
  76. // reset the needed caps 
  77. $caps = array(); 
  78.  
  79. // Add 'do_not_allow' cap if user is spam or deleted 
  80. if ( bbp_is_user_inactive( $user_id ) ) { 
  81. $caps[] = 'do_not_allow'; 
  82.  
  83. // Moderators can always edit meta 
  84. } elseif ( user_can( $user_id, 'moderate' ) ) { 
  85. $caps[] = 'moderate'; 
  86.  
  87. // Unknown so map to edit_posts 
  88. } else { 
  89. $caps[] = $post_type->cap->edit_posts; 
  90.  
  91. return $caps; 
  92.