Nav apraksta

class-jetpack-bbpress-rest-api.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * REST API Compatibility: bbPress & Jetpack
  4. * Enables bbPress to work with the Jetpack REST API
  5. *
  6. * @package automattic/jetpack
  7. */
  8. /**
  9. * REST API Compatibility: bbPress.
  10. */
  11. class Jetpack_BbPress_REST_API {
  12. /**
  13. * Singleton
  14. *
  15. * @var Jetpack_BbPress_REST_API.
  16. */
  17. private static $instance;
  18. /**
  19. * Returns or creates the singleton.
  20. *
  21. * @return Jetpack_BbPress_REST_API
  22. */
  23. public static function instance() {
  24. if ( isset( self::$instance ) ) {
  25. return self::$instance;
  26. }
  27. self::$instance = new self();
  28. }
  29. /**
  30. * Jetpack_BbPress_REST_API constructor.
  31. */
  32. private function __construct() {
  33. add_filter( 'rest_api_allowed_post_types', array( $this, 'allow_bbpress_post_types' ) );
  34. add_filter( 'bbp_map_meta_caps', array( $this, 'adjust_meta_caps' ), 10, 4 );
  35. add_filter( 'rest_api_allowed_public_metadata', array( $this, 'allow_bbpress_public_metadata' ) );
  36. }
  37. /**
  38. * Adds the bbPress post types to the rest_api_allowed_post_types filter.
  39. *
  40. * @param array $allowed_post_types Allowed post types.
  41. *
  42. * @return array
  43. */
  44. public function allow_bbpress_post_types( $allowed_post_types ) {
  45. $allowed_post_types[] = 'forum';
  46. $allowed_post_types[] = 'topic';
  47. $allowed_post_types[] = 'reply';
  48. return $allowed_post_types;
  49. }
  50. /**
  51. * Adds the bbpress meta keys to the rest_api_allowed_public_metadata filter.
  52. *
  53. * @param array $allowed_meta_keys Allowed meta keys.
  54. *
  55. * @return array
  56. */
  57. public function allow_bbpress_public_metadata( $allowed_meta_keys ) {
  58. $allowed_meta_keys[] = '_bbp_forum_id';
  59. $allowed_meta_keys[] = '_bbp_topic_id';
  60. $allowed_meta_keys[] = '_bbp_status';
  61. $allowed_meta_keys[] = '_bbp_forum_type';
  62. $allowed_meta_keys[] = '_bbp_forum_subforum_count';
  63. $allowed_meta_keys[] = '_bbp_reply_count';
  64. $allowed_meta_keys[] = '_bbp_total_reply_count';
  65. $allowed_meta_keys[] = '_bbp_topic_count';
  66. $allowed_meta_keys[] = '_bbp_total_topic_count';
  67. $allowed_meta_keys[] = '_bbp_topic_count_hidden';
  68. $allowed_meta_keys[] = '_bbp_last_topic_id';
  69. $allowed_meta_keys[] = '_bbp_last_reply_id';
  70. $allowed_meta_keys[] = '_bbp_last_active_time';
  71. $allowed_meta_keys[] = '_bbp_last_active_id';
  72. $allowed_meta_keys[] = '_bbp_sticky_topics';
  73. $allowed_meta_keys[] = '_bbp_voice_count';
  74. $allowed_meta_keys[] = '_bbp_reply_count_hidden';
  75. $allowed_meta_keys[] = '_bbp_anonymous_reply_count';
  76. return $allowed_meta_keys;
  77. }
  78. /**
  79. * Adds the needed caps to the bbp_map_meta_caps filter.
  80. *
  81. * @param array $caps Capabilities for meta capability.
  82. * @param string $cap Capability name.
  83. * @param int $user_id User id.
  84. * @param array $args Arguments.
  85. *
  86. * @return array
  87. */
  88. public function adjust_meta_caps( $caps, $cap, $user_id, $args ) {
  89. // Return early if not a REST request or if not meta bbPress caps.
  90. if ( $this->should_adjust_meta_caps_return_early( $caps, $cap, $user_id, $args ) ) {
  91. return $caps;
  92. }
  93. // $args[0] could be a post ID or a post_type string.
  94. if ( is_int( $args[0] ) ) {
  95. $_post = get_post( $args[0] );
  96. if ( ! empty( $_post ) ) {
  97. $post_type = get_post_type_object( $_post->post_type );
  98. }
  99. } elseif ( is_string( $args[0] ) ) {
  100. $post_type = get_post_type_object( $args[0] );
  101. }
  102. // no post type found, bail.
  103. if ( empty( $post_type ) ) {
  104. return $caps;
  105. }
  106. // reset the needed caps.
  107. $caps = array();
  108. // Add 'do_not_allow' cap if user is spam or deleted.
  109. if ( bbp_is_user_inactive( $user_id ) ) {
  110. $caps[] = 'do_not_allow';
  111. // Moderators can always edit meta.
  112. } elseif ( user_can( $user_id, 'moderate' ) ) {
  113. $caps[] = 'moderate';
  114. // Unknown so map to edit_posts.
  115. } else {
  116. $caps[] = $post_type->cap->edit_posts;
  117. }
  118. return $caps;
  119. }
  120. /**
  121. * Should adjust_meta_caps return early?
  122. *
  123. * @param array $caps Capabilities for meta capability.
  124. * @param string $cap Capability name.
  125. * @param int $user_id User id.
  126. * @param array $args Arguments.
  127. *
  128. * @return bool
  129. */
  130. private function should_adjust_meta_caps_return_early( $caps, $cap, $user_id, $args ) {
  131. // only run for REST API requests.
  132. if ( ! defined( 'REST_API_REQUEST' ) || ! REST_API_REQUEST ) {
  133. return true;
  134. }
  135. // only modify caps for meta caps and for bbPress meta keys.
  136. if ( ! in_array( $cap, array( 'edit_post_meta', 'delete_post_meta', 'add_post_meta' ), true ) || empty( $args[1] ) || false === strpos( $args[1], '_bbp_' ) ) {
  137. return true;
  138. }
  139. return false;
  140. }
  141. }