@@ -18,18 +18,18 @@ discard block |
||
18 | 18 | public function __construct() { |
19 | 19 | $this->api = new WP_REST_React_Controller(); |
20 | 20 | |
21 | - add_action( 'rest_api_init', array( $this->api, 'register_routes' ) ); |
|
21 | + add_action('rest_api_init', array($this->api, 'register_routes')); |
|
22 | 22 | |
23 | - if ( is_admin() ) { |
|
23 | + if (is_admin()) { |
|
24 | 24 | return; |
25 | 25 | } |
26 | 26 | |
27 | 27 | $this->enqueue(); |
28 | 28 | |
29 | - add_action( 'wp_head', array( $this, 'print_settings' ) ); |
|
30 | - add_action( 'wp_footer', array( $this, 'print_selector' ) ); |
|
29 | + add_action('wp_head', array($this, 'print_settings')); |
|
30 | + add_action('wp_footer', array($this, 'print_selector')); |
|
31 | 31 | |
32 | - add_filter( 'the_content', array( $this, 'the_content' ) ); |
|
32 | + add_filter('the_content', array($this, 'the_content')); |
|
33 | 33 | } |
34 | 34 | |
35 | 35 | /** |
@@ -40,7 +40,7 @@ discard block |
||
40 | 40 | public static function init() { |
41 | 41 | static $instance; |
42 | 42 | |
43 | - if ( ! $instance ) { |
|
43 | + if (!$instance) { |
|
44 | 44 | $instance = new React; |
45 | 45 | } |
46 | 46 | |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | window.wp.react = window.wp.react || {}; |
58 | 58 | window.wp.react.settings = { |
59 | 59 | emoji_url: '<?php echo REACT_URL . '/static/emoji.json' ?>', |
60 | - endpoint: '<?php echo get_rest_url( null, $this->api->namespace . '/' . $this->api->rest_base ); ?>' |
|
60 | + endpoint: '<?php echo get_rest_url(null, $this->api->namespace . '/' . $this->api->rest_base); ?>' |
|
61 | 61 | } |
62 | 62 | </script> |
63 | 63 | <?php |
@@ -67,9 +67,9 @@ discard block |
||
67 | 67 | * Enqueue relevant JS and CSS |
68 | 68 | */ |
69 | 69 | public function enqueue() { |
70 | - wp_enqueue_style( 'react-emoji', REACT_URL . '/static/react.css' ); |
|
70 | + wp_enqueue_style('react-emoji', REACT_URL . '/static/react.css'); |
|
71 | 71 | |
72 | - wp_enqueue_script( 'react-emoji', REACT_URL . '/static/react.js', array(), false, true ); |
|
72 | + wp_enqueue_script('react-emoji', REACT_URL . '/static/react.js', array(), false, true); |
|
73 | 73 | } |
74 | 74 | |
75 | 75 | /** |
@@ -77,34 +77,34 @@ discard block |
||
77 | 77 | * @param string $content The content HTML |
78 | 78 | * @return string The content HTML, with the react buttons attached |
79 | 79 | */ |
80 | - public function the_content( $content ) { |
|
80 | + public function the_content($content) { |
|
81 | 81 | $post_id = get_the_ID(); |
82 | - if ( ! $post_id ) { |
|
82 | + if (!$post_id) { |
|
83 | 83 | return $content; |
84 | 84 | } |
85 | 85 | |
86 | - $reactions = get_comments( array( |
|
86 | + $reactions = get_comments(array( |
|
87 | 87 | 'post_id' => $post_id, |
88 | 88 | 'type' => 'reaction', |
89 | - ) ); |
|
89 | + )); |
|
90 | 90 | |
91 | 91 | $reactions_summary = array(); |
92 | - foreach ( $reactions as $reaction ) { |
|
93 | - if ( ! isset( $reactions_summary[ $reaction->comment_content ] ) ) { |
|
94 | - $reactions_summary[ $reaction->comment_content ] = 0; |
|
92 | + foreach ($reactions as $reaction) { |
|
93 | + if (!isset($reactions_summary[$reaction->comment_content])) { |
|
94 | + $reactions_summary[$reaction->comment_content] = 0; |
|
95 | 95 | } |
96 | 96 | |
97 | - $reactions_summary[ $reaction->comment_content ]++; |
|
97 | + $reactions_summary[$reaction->comment_content]++; |
|
98 | 98 | } |
99 | 99 | |
100 | 100 | $content .= '<div class="emoji-reactions">'; |
101 | 101 | |
102 | - foreach ( $reactions_summary as $emoji => $count ) { |
|
102 | + foreach ($reactions_summary as $emoji => $count) { |
|
103 | 103 | $content .= "<div data-emoji='$emoji' data-count='$count' data-post='$post_id' class='emoji-reaction'><div class='emoji'>$emoji</div><div class='count'>$count</div></div>"; |
104 | 104 | } |
105 | 105 | |
106 | 106 | /* translators: This is the emoji used for the "Add new emoji reaction" button */ |
107 | - $content .= "<div data-post='$post_id' class='emoji-reaction-add'><div class='emoji'>" . __( ' |
@@ -30,21 +30,21 @@ discard block |
||
30 | 30 | * Register the routes for the objects of the controller. |
31 | 31 | */ |
32 | 32 | public function register_routes() { |
33 | - register_rest_route( $this->namespace, $this->rest_base, array( |
|
33 | + register_rest_route($this->namespace, $this->rest_base, array( |
|
34 | 34 | array( |
35 | 35 | 'methods' => WP_Rest_Server::READABLE, |
36 | - 'callback' => array( $this, 'get_items' ), |
|
37 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
36 | + 'callback' => array($this, 'get_items'), |
|
37 | + 'permission_callback' => array($this, 'get_items_permissions_check'), |
|
38 | 38 | 'args' => $this->get_collection_params(), |
39 | 39 | ), |
40 | 40 | array( |
41 | 41 | 'methods' => WP_Rest_Server::CREATABLE, |
42 | - 'callback' => array( $this, 'create_item' ), |
|
43 | - 'permission_callback' => array( $this, 'create_item_permissions_check' ), |
|
42 | + 'callback' => array($this, 'create_item'), |
|
43 | + 'permission_callback' => array($this, 'create_item_permissions_check'), |
|
44 | 44 | 'args' => $this->get_creation_params(), |
45 | 45 | ), |
46 | - 'schema' => array( $this, 'get_public_item_schema' ), |
|
47 | - ) ); |
|
46 | + 'schema' => array($this, 'get_public_item_schema'), |
|
47 | + )); |
|
48 | 48 | } |
49 | 49 | |
50 | 50 | /** |
@@ -53,14 +53,14 @@ discard block |
||
53 | 53 | * @param WP_REST_Request $request Full details about the request. |
54 | 54 | * @return WP_Error|boolean |
55 | 55 | */ |
56 | - public function get_items_permissions_check( $request ) { |
|
57 | - if ( ! empty( $request['post'] ) ) { |
|
58 | - foreach ( (array) $request['post'] as $post_id ) { |
|
59 | - $post = get_post( $post_id ); |
|
60 | - if ( ! empty( $post_id ) && $post && ! $this->check_read_post_permission( $post ) ) { |
|
61 | - return new WP_Error( 'rest_cannot_read_post', __( 'Sorry, you cannot read the post for this reaction.' ), array( 'status' => rest_authorization_required_code() ) ); |
|
62 | - } else if ( 0 === $post_id && ! current_user_can( 'moderate_comments' ) ) { |
|
63 | - return new WP_Error( 'rest_cannot_read', __( 'Sorry, you cannot read reactions without a post.' ), array( 'status' => rest_authorization_required_code() ) ); |
|
56 | + public function get_items_permissions_check($request) { |
|
57 | + if (!empty($request['post'])) { |
|
58 | + foreach ((array) $request['post'] as $post_id) { |
|
59 | + $post = get_post($post_id); |
|
60 | + if (!empty($post_id) && $post && !$this->check_read_post_permission($post)) { |
|
61 | + return new WP_Error('rest_cannot_read_post', __('Sorry, you cannot read the post for this reaction.'), array('status' => rest_authorization_required_code())); |
|
62 | + } else if (0 === $post_id && !current_user_can('moderate_comments')) { |
|
63 | + return new WP_Error('rest_cannot_read', __('Sorry, you cannot read reactions without a post.'), array('status' => rest_authorization_required_code())); |
|
64 | 64 | } |
65 | 65 | } |
66 | 66 | } |
@@ -74,7 +74,7 @@ discard block |
||
74 | 74 | * @param WP_REST_Request $request Full details about the request. |
75 | 75 | * @return WP_Error|WP_REST_Response |
76 | 76 | */ |
77 | - public function get_items( $request ) { |
|
77 | + public function get_items($request) { |
|
78 | 78 | $prepared_args = array( |
79 | 79 | 'post__in' => $request['post'], |
80 | 80 | 'type' => 'reaction', |
@@ -88,41 +88,41 @@ discard block |
||
88 | 88 | * @param array $prepared_args Array of arguments for WP_Comment_Query. |
89 | 89 | * @param WP_REST_Request $request The current request. |
90 | 90 | */ |
91 | - $prepared_args = apply_filters( 'rest_reaction_query', $prepared_args, $request ); |
|
91 | + $prepared_args = apply_filters('rest_reaction_query', $prepared_args, $request); |
|
92 | 92 | |
93 | 93 | $query = new WP_Comment_Query; |
94 | - $query_result = $query->query( $prepared_args ); |
|
94 | + $query_result = $query->query($prepared_args); |
|
95 | 95 | |
96 | 96 | $reactions_count = array(); |
97 | - foreach ( $query_result as $reaction ) { |
|
98 | - if ( empty( $reactions_count[ $reaction->comment_content ] ) ) { |
|
99 | - $reactions_count[ $reaction->comment_content ] = array( |
|
97 | + foreach ($query_result as $reaction) { |
|
98 | + if (empty($reactions_count[$reaction->comment_content])) { |
|
99 | + $reactions_count[$reaction->comment_content] = array( |
|
100 | 100 | 'count' => 0, |
101 | 101 | 'post_id' => $reaction->comment_post_ID, |
102 | 102 | ); |
103 | 103 | } |
104 | 104 | |
105 | - $reactions_count[ $reaction->comment_content ]++; |
|
105 | + $reactions_count[$reaction->comment_content]++; |
|
106 | 106 | } |
107 | 107 | |
108 | 108 | $reactions = array(); |
109 | - foreach ( $reactions_count as $emoji => $data ) { |
|
109 | + foreach ($reactions_count as $emoji => $data) { |
|
110 | 110 | $reaction = array( |
111 | 111 | 'emoji' => $emoji, |
112 | 112 | 'count' => $data['count'], |
113 | 113 | 'post_id' => $data['post_id'], |
114 | 114 | ); |
115 | 115 | |
116 | - $data = $this->prepare_item_for_response( $reaction, $request ); |
|
117 | - $reactions[] = $this->prepare_response_for_collection( $data ); |
|
116 | + $data = $this->prepare_item_for_response($reaction, $request); |
|
117 | + $reactions[] = $this->prepare_response_for_collection($data); |
|
118 | 118 | } |
119 | 119 | |
120 | 120 | $total_reactions = (int) $query->found_comments; |
121 | - $reaction_groups = count( $reactions ); |
|
121 | + $reaction_groups = count($reactions); |
|
122 | 122 | |
123 | - $response = rest_ensure_response( $reactions ); |
|
124 | - $response->header( 'X-WP-Total', $total_reactions ); |
|
125 | - $response->header( 'X-WP-TotalGroups', $reaction_groups ); |
|
123 | + $response = rest_ensure_response($reactions); |
|
124 | + $response->header('X-WP-Total', $total_reactions); |
|
125 | + $response->header('X-WP-TotalGroups', $reaction_groups); |
|
126 | 126 | |
127 | 127 | return $response; |
128 | 128 | } |
@@ -133,14 +133,14 @@ discard block |
||
133 | 133 | * @param WP_REST_Request $request Full details about the request. |
134 | 134 | * @return WP_Error|boolean |
135 | 135 | */ |
136 | - public function create_item_permissions_check( $request ) { |
|
137 | - if ( ! empty( $request['post'] ) && $post = get_post( (int) $request['post'] ) ) { |
|
138 | - if ( ! $this->check_read_post_permission( $post ) ) { |
|
139 | - return new WP_Error( 'rest_cannot_read_post', __( 'Sorry, you cannot read the post for this reaction.' ), array( 'status' => rest_authorization_required_code() ) ); |
|
136 | + public function create_item_permissions_check($request) { |
|
137 | + if (!empty($request['post']) && $post = get_post((int) $request['post'])) { |
|
138 | + if (!$this->check_read_post_permission($post)) { |
|
139 | + return new WP_Error('rest_cannot_read_post', __('Sorry, you cannot read the post for this reaction.'), array('status' => rest_authorization_required_code())); |
|
140 | 140 | } |
141 | 141 | |
142 | - if ( ! comments_open( $post->ID ) ) { |
|
143 | - return new WP_Error( 'rest_reactions_closed', __( 'Sorry, reactions are closed on this post.' ), array( 'status' => 403 ) ); |
|
142 | + if (!comments_open($post->ID)) { |
|
143 | + return new WP_Error('rest_reactions_closed', __('Sorry, reactions are closed on this post.'), array('status' => 403)); |
|
144 | 144 | } |
145 | 145 | } |
146 | 146 | return true; |
@@ -152,16 +152,16 @@ discard block |
||
152 | 152 | * @param WP_REST_Request $request Full details about the request. |
153 | 153 | * @return WP_Error|WP_REST_Response |
154 | 154 | */ |
155 | - public function create_item( $request ) { |
|
155 | + public function create_item($request) { |
|
156 | 156 | $comment = array( |
157 | 157 | 'comment_content' => $request['emoji'], |
158 | 158 | 'comment_post_ID' => $request['post'], |
159 | 159 | 'comment_type' => 'reaction', |
160 | 160 | ); |
161 | 161 | |
162 | - wp_insert_comment( $comment ); |
|
162 | + wp_insert_comment($comment); |
|
163 | 163 | |
164 | - return $this->get_items( $request ); |
|
164 | + return $this->get_items($request); |
|
165 | 165 | } |
166 | 166 | |
167 | 167 | /** |
@@ -172,10 +172,10 @@ discard block |
||
172 | 172 | * @param object $post Post object. |
173 | 173 | * @return boolean Can we read it? |
174 | 174 | */ |
175 | - public function check_read_post_permission( $post ) { |
|
176 | - $posts_controller = new WP_REST_Posts_Controller( $post->post_type ); |
|
175 | + public function check_read_post_permission($post) { |
|
176 | + $posts_controller = new WP_REST_Posts_Controller($post->post_type); |
|
177 | 177 | |
178 | - return $posts_controller->check_read_permission( $post ); |
|
178 | + return $posts_controller->check_read_permission($post); |
|
179 | 179 | } |
180 | 180 | |
181 | 181 | /** |
@@ -185,7 +185,7 @@ discard block |
||
185 | 185 | * @param WP_REST_Request $request Request object. |
186 | 186 | * @return WP_REST_Response $response |
187 | 187 | */ |
188 | - public function prepare_item_for_response( $reaction, $request ) { |
|
188 | + public function prepare_item_for_response($reaction, $request) { |
|
189 | 189 | $data = array( |
190 | 190 | 'emoji' => $reaction['emoji'], |
191 | 191 | 'count' => (int) $reaction['count'], |
@@ -193,9 +193,9 @@ discard block |
||
193 | 193 | ); |
194 | 194 | |
195 | 195 | // Wrap the data in a response object |
196 | - $response = rest_ensure_response( $data ); |
|
196 | + $response = rest_ensure_response($data); |
|
197 | 197 | |
198 | - $response->add_links( $this->prepare_links( $reaction ) ); |
|
198 | + $response->add_links($this->prepare_links($reaction)); |
|
199 | 199 | |
200 | 200 | /** |
201 | 201 | * Filter a reaction group returned from the API. |
@@ -206,7 +206,7 @@ discard block |
||
206 | 206 | * @param array $reaction The original reaction data. |
207 | 207 | * @param WP_REST_Request $request Request used to generate the response. |
208 | 208 | */ |
209 | - return apply_filters( 'rest_prepare_comment', $response, $reaction, $request ); |
|
209 | + return apply_filters('rest_prepare_comment', $response, $reaction, $request); |
|
210 | 210 | } |
211 | 211 | |
212 | 212 | /** |
@@ -215,14 +215,14 @@ discard block |
||
215 | 215 | * @param WP_REST_Response $response Response object. |
216 | 216 | * @return array Response data, ready for insertion into collection data. |
217 | 217 | */ |
218 | - public function prepare_response_for_collection( $response ) { |
|
219 | - if ( ! ( $response instanceof WP_REST_Response ) ) { |
|
218 | + public function prepare_response_for_collection($response) { |
|
219 | + if (!($response instanceof WP_REST_Response)) { |
|
220 | 220 | return $response; |
221 | 221 | } |
222 | 222 | |
223 | 223 | $data = (array) $response->get_data(); |
224 | - $links = WP_REST_Server::get_response_links( $response ); |
|
225 | - if ( ! empty( $links ) ) { |
|
224 | + $links = WP_REST_Server::get_response_links($response); |
|
225 | + if (!empty($links)) { |
|
226 | 226 | $data['_links'] = $links; |
227 | 227 | } |
228 | 228 | |
@@ -235,23 +235,23 @@ discard block |
||
235 | 235 | * @param array $reaction Reaction. |
236 | 236 | * @return array Links for the given reaction. |
237 | 237 | */ |
238 | - protected function prepare_links( $reaction ) { |
|
238 | + protected function prepare_links($reaction) { |
|
239 | 239 | $links = array( |
240 | 240 | 'self' => array( |
241 | - 'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $reaction['emoji'] ) ), |
|
241 | + 'href' => rest_url(sprintf('/%s/%s/%s', $this->namespace, $this->rest_base, $reaction['emoji'])), |
|
242 | 242 | ), |
243 | 243 | 'collection' => array( |
244 | - 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
|
244 | + 'href' => rest_url(sprintf('/%s/%s', $this->namespace, $this->rest_base)), |
|
245 | 245 | ), |
246 | 246 | ); |
247 | 247 | |
248 | - if ( 0 !== (int) $reaction['post_id'] ) { |
|
249 | - $post = get_post( $reaction['post_id'] ); |
|
250 | - if ( ! empty( $post->ID ) ) { |
|
251 | - $obj = get_post_type_object( $post->post_type ); |
|
252 | - $base = ! empty( $obj->rest_base ) ? $obj->rest_base : $obj->name; |
|
248 | + if (0 !== (int) $reaction['post_id']) { |
|
249 | + $post = get_post($reaction['post_id']); |
|
250 | + if (!empty($post->ID)) { |
|
251 | + $obj = get_post_type_object($post->post_type); |
|
252 | + $base = !empty($obj->rest_base) ? $obj->rest_base : $obj->name; |
|
253 | 253 | $links['up'] = array( |
254 | - 'href' => rest_url( '/wp/v2/' . $base . '/' . $reaction['post_id'] ), |
|
254 | + 'href' => rest_url('/wp/v2/' . $base . '/' . $reaction['post_id']), |
|
255 | 255 | 'embeddable' => true, |
256 | 256 | 'post_type' => $post->post_type, |
257 | 257 | ); |
@@ -269,9 +269,9 @@ discard block |
||
269 | 269 | public function get_collection_params() { |
270 | 270 | $query_params = array(); |
271 | 271 | |
272 | - $query_params['post'] = array( |
|
272 | + $query_params['post'] = array( |
|
273 | 273 | 'default' => array(), |
274 | - 'description' => __( 'Limit result set to resources assigned to specific post ids.' ), |
|
274 | + 'description' => __('Limit result set to resources assigned to specific post ids.'), |
|
275 | 275 | 'type' => 'array', |
276 | 276 | 'sanitize_callback' => 'wp_parse_id_list', |
277 | 277 | 'validate_callback' => 'rest_validate_request_arg', |
@@ -287,17 +287,17 @@ discard block |
||
287 | 287 | public function get_creation_params() { |
288 | 288 | $query_params = array(); |
289 | 289 | |
290 | - $query_params['post'] = array( |
|
290 | + $query_params['post'] = array( |
|
291 | 291 | 'default' => array(), |
292 | - 'description' => __( 'The post ID to add a reaction to.' ), |
|
292 | + 'description' => __('The post ID to add a reaction to.'), |
|
293 | 293 | 'type' => 'integer', |
294 | 294 | 'sanitize_callback' => 'absint', |
295 | 295 | 'validate_callback' => 'rest_validate_request_arg', |
296 | 296 | ); |
297 | 297 | |
298 | - $query_params['emoji'] = array( |
|
298 | + $query_params['emoji'] = array( |
|
299 | 299 | 'default' => array(), |
300 | - 'description' => __( 'The reaction emoji.' ), |
|
300 | + 'description' => __('The reaction emoji.'), |
|
301 | 301 | 'type' => 'string', |
302 | 302 | 'validate_callback' => 'rest_validate_request_arg', |
303 | 303 | ); |