PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 8.1.1
Jetpack – WP Security, Backup, Speed, & Growth v8.1.1
12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 14.4.2 All 500 releases
jetpack / class.jetpack-bbpress-json-api-compat.php
class.jetpack-bbpress-json-api-compat.php
103 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
8 private static $instance;
9
10 public static function instance() {
11 if ( isset( self::$instance ) ) {
12 return self::$instance;
13 }
14
15 self::$instance = new self();
16 }
17
18 private function __construct() {
19 add_filter( 'rest_api_allowed_post_types', array( $this, 'allow_bbpress_post_types' ) );
20 add_filter( 'bbp_map_meta_caps', array( $this, 'adjust_meta_caps' ), 10, 4 );
21 add_filter( 'rest_api_allowed_public_metadata', array( $this, 'allow_bbpress_public_metadata' ) );
22 }
23
24 function allow_bbpress_post_types( $allowed_post_types ) {
25 $allowed_post_types[] = 'forum';
26 $allowed_post_types[] = 'topic';
27 $allowed_post_types[] = 'reply';
28 return $allowed_post_types;
29 }
30
31 function allow_bbpress_public_metadata( $allowed_meta_keys ) {
32 $allowed_meta_keys[] = '_bbp_forum_id';
33 $allowed_meta_keys[] = '_bbp_topic_id';
34 $allowed_meta_keys[] = '_bbp_status';
35 $allowed_meta_keys[] = '_bbp_forum_type';
36 $allowed_meta_keys[] = '_bbp_forum_subforum_count';
37 $allowed_meta_keys[] = '_bbp_reply_count';
38 $allowed_meta_keys[] = '_bbp_total_reply_count';
39 $allowed_meta_keys[] = '_bbp_topic_count';
40 $allowed_meta_keys[] = '_bbp_total_topic_count';
41 $allowed_meta_keys[] = '_bbp_topic_count_hidden';
42 $allowed_meta_keys[] = '_bbp_last_topic_id';
43 $allowed_meta_keys[] = '_bbp_last_reply_id';
44 $allowed_meta_keys[] = '_bbp_last_active_time';
45 $allowed_meta_keys[] = '_bbp_last_active_id';
46 $allowed_meta_keys[] = '_bbp_sticky_topics';
47 $allowed_meta_keys[] = '_bbp_voice_count';
48 $allowed_meta_keys[] = '_bbp_reply_count_hidden';
49 $allowed_meta_keys[] = '_bbp_anonymous_reply_count';
50
51 return $allowed_meta_keys;
52 }
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
61 // only modify caps for meta caps and for bbPress meta keys
62 if ( ! in_array( $cap, array( 'edit_post_meta', 'delete_post_meta', 'add_post_meta' ) ) || empty( $args[1] ) || false === strpos( $args[1], '_bbp_' ) ) {
63 return $caps;
64 }
65
66 // $args[0] could be a post ID or a post_type string
67 if ( is_int( $args[0] ) ) {
68 $_post = get_post( $args[0] );
69 if ( ! empty( $_post ) ) {
70 $post_type = get_post_type_object( $_post->post_type );
71 }
72 } elseif ( is_string( $args[0] ) ) {
73 $post_type = get_post_type_object( $args[0] );
74 }
75
76 // no post type found, bail
77 if ( empty( $post_type ) ) {
78 return $caps;
79 }
80
81 // reset the needed caps
82 $caps = array();
83
84 // Add 'do_not_allow' cap if user is spam or deleted
85 if ( bbp_is_user_inactive( $user_id ) ) {
86 $caps[] = 'do_not_allow';
87
88 // Moderators can always edit meta
89 } elseif ( user_can( $user_id, 'moderate' ) ) {
90 $caps[] = 'moderate';
91
92 // Unknown so map to edit_posts
93 } else {
94 $caps[] = $post_type->cap->edit_posts;
95 }
96
97 return $caps;
98 }
99
100 }
101
102 bbPress_Jetpack_REST_API::instance();
103