Completed
Push — update/plugins-install-endpoin... ( 6f8147...61fa39 )
by
unknown
70:17 queued 58:04
created

class.jetpack-bbpress-json-api-compat.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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