Completed
Push — master ( 944d90...469279 )
by Gary
02:53
created
lib/class-wp-rest-react-controller.php 2 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -131,7 +131,7 @@
 block discarded – undo
131 131
 	 * Check if a given request has access to create a reaction
132 132
 	 *
133 133
 	 * @param  WP_REST_Request $request Full details about the request.
134
-	 * @return WP_Error|boolean
134
+	 * @return boolean
135 135
 	 */
136 136
 	public function create_item_permissions_check( $request ) {
137 137
 		return true;
Please login to merge, or discard this patch.
Spacing   +57 added lines, -57 removed lines patch added patch discarded remove patch
@@ -30,21 +30,21 @@  discard block
 block discarded – undo
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_permission_callback' ),
36
+				'callback'            => array($this, 'get_items'),
37
+				'permission_callback' => array($this, 'get_items_permission_callback'),
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_permission_callback' ),
42
+				'callback'            => array($this, 'create_item'),
43
+				'permission_callback' => array($this, 'create_item_permission_callback'),
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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,7 +133,7 @@  discard block
 block discarded – undo
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 ) {
136
+	public function create_item_permissions_check($request) {
137 137
 		return true;
138 138
 	}
139 139
 
@@ -143,7 +143,7 @@  discard block
 block discarded – undo
143 143
 	 * @param  WP_REST_Request $request Full details about the request.
144 144
 	 * @return WP_Error|WP_REST_Response
145 145
 	 */
146
-	public function create_item( $request ) {
146
+	public function create_item($request) {
147 147
 	}
148 148
 
149 149
 	/**
@@ -154,10 +154,10 @@  discard block
 block discarded – undo
154 154
 	 * @param object $post Post object.
155 155
 	 * @return boolean Can we read it?
156 156
 	 */
157
-	public function check_read_post_permission( $post ) {
158
-		$posts_controller = new WP_REST_Posts_Controller( $post->post_type );
157
+	public function check_read_post_permission($post) {
158
+		$posts_controller = new WP_REST_Posts_Controller($post->post_type);
159 159
 
160
-		return $posts_controller->check_read_permission( $post );
160
+		return $posts_controller->check_read_permission($post);
161 161
 	}
162 162
 
163 163
 	/**
@@ -167,7 +167,7 @@  discard block
 block discarded – undo
167 167
 	 * @param  WP_REST_Request  $request  Request object.
168 168
 	 * @return WP_REST_Response $response
169 169
 	 */
170
-	public function prepare_item_for_response( $reaction, $request ) {
170
+	public function prepare_item_for_response($reaction, $request) {
171 171
 		$data = array(
172 172
 			'emoji'   => $reaction['emoji'],
173 173
 			'count'   => (int) $reaction['count'],
@@ -175,9 +175,9 @@  discard block
 block discarded – undo
175 175
 		);
176 176
 
177 177
 		// Wrap the data in a response object
178
-		$response = rest_ensure_response( $data );
178
+		$response = rest_ensure_response($data);
179 179
 
180
-		$response->add_links( $this->prepare_links( $reaction ) );
180
+		$response->add_links($this->prepare_links($reaction));
181 181
 
182 182
 		/**
183 183
 		 * Filter a reaction group returned from the API.
@@ -188,7 +188,7 @@  discard block
 block discarded – undo
188 188
 		 * @param array             $reaction   The original reaction data.
189 189
 		 * @param WP_REST_Request   $request    Request used to generate the response.
190 190
 		 */
191
-		return apply_filters( 'rest_prepare_comment', $response, $reaction, $request );
191
+		return apply_filters('rest_prepare_comment', $response, $reaction, $request);
192 192
 	}
193 193
 
194 194
 	/**
@@ -197,14 +197,14 @@  discard block
 block discarded – undo
197 197
 	 * @param WP_REST_Response $response Response object.
198 198
 	 * @return array Response data, ready for insertion into collection data.
199 199
 	 */
200
-	public function prepare_response_for_collection( $response ) {
201
-		if ( ! ( $response instanceof WP_REST_Response ) ) {
200
+	public function prepare_response_for_collection($response) {
201
+		if (!($response instanceof WP_REST_Response)) {
202 202
 			return $response;
203 203
 		}
204 204
 
205 205
 		$data = (array) $response->get_data();
206
-		$links = WP_REST_Server::get_response_links( $response );
207
-		if ( ! empty( $links ) ) {
206
+		$links = WP_REST_Server::get_response_links($response);
207
+		if (!empty($links)) {
208 208
 			$data['_links'] = $links;
209 209
 		}
210 210
 
@@ -217,23 +217,23 @@  discard block
 block discarded – undo
217 217
 	 * @param array $reaction Reaction.
218 218
 	 * @return array Links for the given reaction.
219 219
 	 */
220
-	protected function prepare_links( $reaction ) {
220
+	protected function prepare_links($reaction) {
221 221
 		$links = array(
222 222
 			'self' => array(
223
-				'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $reaction->emoji ) ),
223
+				'href' => rest_url(sprintf('/%s/%s/%s', $this->namespace, $this->rest_base, $reaction->emoji)),
224 224
 			),
225 225
 			'collection' => array(
226
-				'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
226
+				'href' => rest_url(sprintf('/%s/%s', $this->namespace, $this->rest_base)),
227 227
 			),
228 228
 		);
229 229
 
230
-		if ( 0 !== (int) $reaction['post_id'] ) {
231
-			$post = get_post( $reaction['post_id'] );
232
-			if ( ! empty( $post->ID ) ) {
233
-				$obj = get_post_type_object( $post->post_type );
234
-				$base = ! empty( $obj->rest_base ) ? $obj->rest_base : $obj->name;
230
+		if (0 !== (int) $reaction['post_id']) {
231
+			$post = get_post($reaction['post_id']);
232
+			if (!empty($post->ID)) {
233
+				$obj = get_post_type_object($post->post_type);
234
+				$base = !empty($obj->rest_base) ? $obj->rest_base : $obj->name;
235 235
 				$links['up'] = array(
236
-					'href'       => rest_url( '/wp/v2/' . $base . '/' . $reaction['post_id'] ),
236
+					'href'       => rest_url('/wp/v2/' . $base . '/' . $reaction['post_id']),
237 237
 					'embeddable' => true,
238 238
 					'post_type'  => $post->post_type,
239 239
 				);
@@ -251,9 +251,9 @@  discard block
 block discarded – undo
251 251
 	public function get_collection_params() {
252 252
 		$query_params = array();
253 253
 
254
-		$query_params['post']   = array(
254
+		$query_params['post'] = array(
255 255
 			'default'           => array(),
256
-			'description'       => __( 'Limit result set to resources assigned to specific post ids.' ),
256
+			'description'       => __('Limit result set to resources assigned to specific post ids.'),
257 257
 			'type'              => 'array',
258 258
 			'sanitize_callback' => 'wp_parse_id_list',
259 259
 			'validate_callback' => 'rest_validate_request_arg',
@@ -269,17 +269,17 @@  discard block
 block discarded – undo
269 269
 	public function get_creation_params() {
270 270
 		$query_params = array();
271 271
 
272
-		$query_params['post']   = array(
272
+		$query_params['post'] = array(
273 273
 			'default'           => array(),
274
-			'description'       => __( 'The post ID to add a reaction to.' ),
274
+			'description'       => __('The post ID to add a reaction to.'),
275 275
 			'type'              => 'integer',
276 276
 			'sanitize_callback' => 'absint',
277 277
 			'validate_callback' => 'rest_validate_request_arg',
278 278
 		);
279 279
 
280
-		$query_params['emoji']  = array(
280
+		$query_params['emoji'] = array(
281 281
 			'default'           => array(),
282
-			'description'       => __( 'The reaction emoji.' ),
282
+			'description'       => __('The reaction emoji.'),
283 283
 			'type'              => 'string',
284 284
 			'validate_callback' => 'rest_validate_request_arg',
285 285
 		);
Please login to merge, or discard this patch.
react.php 1 patch
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -5,18 +5,18 @@
 block discarded – undo
Please login to merge, or discard this patch.
lib/class-react.php 1 patch
Spacing   +26 added lines, -26 removed lines patch added patch discarded remove patch
@@ -18,18 +18,18 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 
@@ -66,9 +66,9 @@  discard block
 block discarded – undo
66 66
 	 * Enqueue relevant JS and CSS
67 67
 	 */
68 68
 	public function enqueue() {
69
-		wp_enqueue_style( 'react-emoji', REACT_URL . '/static/react.css' );
69
+		wp_enqueue_style('react-emoji', REACT_URL . '/static/react.css');
70 70
 
71
-		wp_enqueue_script( 'react-emoji', REACT_URL . '/static/react.js', array(), false, true );
71
+		wp_enqueue_script('react-emoji', REACT_URL . '/static/react.js', array(), false, true);
72 72
 	}
73 73
 
74 74
 	/**
@@ -76,34 +76,34 @@  discard block
 block discarded – undo
76 76
 	 * @param  string $content The content HTML
77 77
 	 * @return string The content HTML, with the react buttons attached
78 78
 	 */
79
-	public function the_content( $content ) {
79
+	public function the_content($content) {
80 80
 		$post_id = get_the_ID();
81
-		if ( ! $post_id ) {
81
+		if (!$post_id) {
82 82
 			return $content;
83 83
 		}
84 84
 
85
-		$reactions = get_comments( array(
85
+		$reactions = get_comments(array(
86 86
 			'post_id' => $post_id,
87 87
 			'type'    => 'reaction',
88
-		) );
88
+		));
89 89
 
90 90
 		$reactions_summary = array();
91
-		foreach ( $reactions as $reaction ) {
92
-			if ( ! isset( $reactions_summary[ $reaction->comment_content ] ) ) {
93
-				$reactions_summary[ $reaction->comment_content ] = 0;
91
+		foreach ($reactions as $reaction) {
92
+			if (!isset($reactions_summary[$reaction->comment_content])) {
93
+				$reactions_summary[$reaction->comment_content] = 0;
94 94
 			}
95 95
 
96
-			$reactions_summary[ $reaction->comment_content ]++;
96
+			$reactions_summary[$reaction->comment_content]++;
97 97
 		}
98 98
 
99 99
 		$content .= '<div class="emoji-reactions">';
100 100
 
101
-		foreach ( $reactions_summary as $emoji => $count ) {
101
+		foreach ($reactions_summary as $emoji => $count) {
102 102
 			$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>";
103 103
 		}
104 104
 
105 105
 		/* translators: This is the emoji used for the "Add new emoji reaction" button */
106
-		$content .= "<div data-post='$post_id' class='emoji-reaction-add'><div class='emoji'>" . __( '
Please login to merge, or discard this patch.
tools/compile_emoji.php 1 patch
Spacing   +20 added lines, -20 removed lines patch added patch discarded remove patch
@@ -1,52 +1,52 @@
 block discarded – undo
1 1
 <?php
2 2
 // Compile emoji data list into an autocompleteable list
3 3
 
4
-$contents = file_get_contents( 'https://raw.githubusercontent.com/iamcal/emoji-data/master/emoji.json' );
5
-file_put_contents( dirname( __DIR__ ) . '/static/emoji-raw.json', $contents );
4
+$contents = file_get_contents('https://raw.githubusercontent.com/iamcal/emoji-data/master/emoji.json');
5
+file_put_contents(dirname(__DIR__) . '/static/emoji-raw.json', $contents);
6 6
 
7
-$data = json_decode( $contents );
7
+$data = json_decode($contents);
8 8
 $map = array();
9 9
 
10
-$categories = array( 'People', 'Nature', 'Foods', 'Activity', 'Places', 'Objects', 'Symbols', 'Flags' );
10
+$categories = array('People', 'Nature', 'Foods', 'Activity', 'Places', 'Objects', 'Symbols', 'Flags');
11 11
 
12
-foreach ( $data as $emoji ) {
12
+foreach ($data as $emoji) {
13 13
 	// Exclude any not supported by Twemoji
14
-	if ( empty( $emoji->has_img_twitter ) ) {
14
+	if (empty($emoji->has_img_twitter)) {
15 15
 		continue;
16 16
 	}
17 17
 
18
-	$category = array_search( $emoji->category, $categories );
19
-	if ( false === $category ) {
20
-		if ( 0 === strpos( $emoji->short_name, 'flag-' ) ) {
18
+	$category = array_search($emoji->category, $categories);
19
+	if (false === $category) {
20
+		if (0 === strpos($emoji->short_name, 'flag-')) {
21 21
 			$category = 7;
22 22
 		} else {
23 23
 			$category = 100;
24 24
 		}
25 25
 	}
26 26
 	$code = "0x" . $emoji->unified;
27
-	$code = str_replace( '-', "-0x", $code );
28
-	$code = explode( '-', $code );
27
+	$code = str_replace('-', "-0x", $code);
28
+	$code = explode('-', $code);
29 29
 
30
-	$map[ $category ][] = array(
30
+	$map[$category][] = array(
31 31
 		'code'       => $code,
32 32
 		'sort_order' => $emoji->sort_order,
33 33
 	);
34 34
 }
35 35
 
36
-ksort( $map );
36
+ksort($map);
37 37
 
38
-foreach ( $map as $category => $emoji_list ) {
39
-	usort( $map[ $category ], function( $a, $b ) {
40
-		if ( $a['sort_order'] == $b['sort_order'] ) {
38
+foreach ($map as $category => $emoji_list) {
39
+	usort($map[$category], function($a, $b) {
40
+		if ($a['sort_order'] == $b['sort_order']) {
41 41
 			return 0;
42 42
 		}
43 43
 
44
-		return ( $a['sort_order'] < $b['sort_order'] ) ? -1 : 1;
44
+		return ($a['sort_order'] < $b['sort_order']) ? -1 : 1;
45 45
 	} );
46 46
 
47
-	foreach ( $map[ $category ] as $id => $emoji ) {
48
-		$map[ $category ][ $id ] = $emoji['code'];
47
+	foreach ($map[$category] as $id => $emoji) {
48
+		$map[$category][$id] = $emoji['code'];
49 49
 	}
50 50
 }
51 51
 
52
-file_put_contents( dirname( __DIR__ ) . '/static/emoji.json', json_encode( $map ) );
52
+file_put_contents(dirname(__DIR__) . '/static/emoji.json', json_encode($map));
Please login to merge, or discard this patch.