class.jetpack-bbpress-json-api-compat.php 2.9 KB

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