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