8000 Merge pull request #2841 from peterwilsoncc/in-array-strict · WP-API/WP-API@c7fa382 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Sep 24, 2018. It is now read-only.

Commit c7fa382

Browse files
authored
Merge pull request #2841 from peterwilsoncc/in-array-strict
Use strict comparisons for in_array.
2 parents 96ae9b7 + 37a52e6 commit c7fa382

6 files changed

+20
-20
lines changed

lib/endpoints/class-wp-rest-attachments-controller.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
1212
*/
1313
protected function prepare_items_query( $prepared_args = array(), $request = null ) {
1414
$query_args = parent::prepare_items_query( $prepared_args, $request );
15-
if ( empty( $query_args['post_status'] ) || ! in_array( $query_args['post_status'], array( 'inherit', 'private', 'trash' ) ) ) {
15+
if ( empty( $query_args['post_status'] ) || ! in_array( $query_args['post_status'], array( 'inherit', 'private', 'trash' ), true ) ) {
1616
$query_args['post_status'] = 'inherit';
1717
}
1818
$media_types = $this->get_media_types();
19-
if ( ! empty( $request['media_type'] ) && in_array( $request['media_type'], array_keys( $media_types ) ) ) {
19+
if ( ! empty( $request['media_type'] ) && in_array( $request['media_type'], array_keys( $media_types ), true ) ) {
2020
$query_args['post_mime_type'] = $media_types[ $request['media_type'] ];
2121
}
2222
if ( ! empty( $request['mime_type'] ) ) {
2323
$parts = explode( '/', $request['mime_type'] );
24-
if ( isset( $media_types[ $parts[0] ] ) && in_array( $request['mime_type'], $media_types[ $parts[0] ] ) ) {
24+
if ( isset( $media_types[ $parts[0] ] ) && in_array( $request['mime_type'], $media_types[ $parts[0] ], true ) ) {
2525
$query_args['post_mime_type'] = $request['mime_type'];
2626
}
2727
}
@@ -64,7 +64,7 @@ public function create_item_permissions_check( $request ) {
6464
*/
6565
public function create_item( $request ) {
6666

67-
if ( ! empty( $request['post'] ) && in_array( get_post_type( $request['post'] ), array( 'revision', 'attachment' ) ) ) {
67+
if ( ! empty( $request['post'] ) && in_array( get_post_type( $request['post'] ), array( 'revision', 'attachment' ), true ) ) {
6868
return new WP_Error( 'rest_invalid_param', __( 'Invalid parent type.' ), array( 'status' => 400 ) );
6969
}
7070

@@ -114,7 +114,7 @@ public function create_item( $request ) {
114114

115115
$id = wp_insert_post( $attachment, true );
116116
if ( is_wp_error( $id ) ) {
117-
if ( in_array( $id->get_error_code(), array( 'db_update_error' ) ) ) {
117+
if ( in_array( $id->get_error_code(), array( 'db_update_error' ), true ) ) {
118118
$id->add_data( array( 'status' => 500 ) );
119119
} else {
120120
$id->add_data( array( 'status' => 400 ) );
@@ -163,7 +163,7 @@ public function create_item( $request ) {
163163
* @return WP_Error|WP_REST_Response
164164
*/
165165
public function update_item( $request ) {
166-
if ( ! empty( $request['post'] ) && in_array( get_post_type( $request['post'] ), array( 'revision', 'attachment' ) ) ) {
166+
if ( ! empty( $request['post'] ) && in_array( get_post_type( $request['post'] ), array( 'revision', 'attachment' ), true ) ) {
167167
return new WP_Error( 'rest_invalid_param', __( 'Invalid parent type.' ), array( 'status' => 400 ) );
168168
}
169169
$response = parent::update_item( $request );

lib/endpoints/class-wp-rest-controller.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public function filter_response_by_context( $data, $context ) {
187187
continue;
188188
}
189189

190-
if ( ! in_array( $context, $schema['properties'][ $key ]['context'] ) ) {
190+
if ( ! in_array( $context, $schema['properties'][ $key ]['context'], true ) ) {
191191
unset( $data[ $key ] );
192192
continue;
193193
}
@@ -197,7 +197,7 @@ public function filter_response_by_context( $data, $context ) {
197197
if ( empty( $details['context'] ) ) {
198198
continue;
199199
}
200-
if ( ! in_array( $context, $details['context'] ) ) {
200+
if ( ! in_array( $context, $details['context'], true ) ) {
201201
if ( isset( $data[ $key ][ $attribute ] ) ) {
202202
unset( $data[ $key ][ $attribute ] );
203203
}

lib/endpoints/class-wp-rest-posts-controller.php

Lines changed: 9 additions & 9 deletions
1398
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ public function create_item( $request ) {
413413

414414
if ( is_wp_error( $post_id ) ) {
415415

416-
if ( in_array( $post_id->get_error_code(), array( 'db_insert_error' ) ) ) {
416+
if ( in_array( $post_id->get_error_code(), array( 'db_insert_error' ), true ) ) {
417417
$post_id->add_data( array( 'status' => 500 ) );
418418
} else {
419419
$post_id->add_data( array( 'status' => 400 ) );
@@ -526,7 +526,7 @@ public function update_item( $request ) {
526526
// convert the post object to an array, otherwise wp_update_post will expect non-escaped input
527527
$post_id = wp_update_post( (array) $post, true );
528528
if ( is_wp_error( $post_id ) ) {
529-
if ( in_array( $post_id->get_error_code(), array( 'db_update_error' ) ) ) {
529+
if ( in_array( $post_id->get_error_code(), array( 'db_update_error' ), true ) ) {
530530
$post_id->add_data( array( 'status' => 500 ) );
531531
} else {
532532
$post_id->add_data( array( 'status' => 400 ) );
@@ -1021,7 +1021,7 @@ protected function handle_featured_media( $featured_media, $post_id ) {
10211021
* @param integer $post_id
10221022
*/
10231023
public function handle_template( $template, $post_id ) {
1024-
if ( in_array( $template, array_keys( wp_get_theme()->get_page_templates( $this->get_post( $post_id ) ) ) ) ) {
1024+
if ( in_array( $template, array_keys( wp_get_theme()->get_page_templates( $this->get_post( $post_id ) ) ), true ) ) {
10251025
update_post_meta( $post_id, '_wp_page_template', $template );
10261026
} else {
10271027
update_post_meta( $post_id, '_wp_page_template', '' );
@@ -1375,15 +1375,15 @@ protected function prepare_links( $post ) {
13751375
),
13761376
);
13771377

1378-
if ( ( in_array( $post->post_type, array( 'post', 'page' ) ) || post_type_supports( $post->post_type, 'author' ) )
1378+
if ( ( in_array( $post->post_type, array( 'post', 'page' ), true ) || post_type_supports( $post->post_type, 'author' ) )
13791379
&& ! empty( $post->post_author ) ) {
13801380
$links['author'] = array(
13811381
'href' => rest_url( 'wp/v2/users/' . $post->post_author ),
13821382
'embeddable' => true,
13831383
);
13841384
};
13851385

1386-
if ( in_array( $post->post_type, array( 'post', 'page' ) ) || post_type_supports( $post->post_type, 'comments' ) ) {
1386+
if ( in_array( $post->post_type, array( 'post', 'page' ), true ) || post_type_supports( $post->post_type, 'comments' ) ) {
13871387
$replies_url = rest_url( 'wp/v2/comments' );
13881388
$replies_url = add_query_arg( 'post', $post->ID, $replies_url );
13891389
$links['replies'] = array(
@@ -1392,7 +1392,7 @@ protected function prepare_links( $post ) {
13921392
);
13931393
}
13941394

1395-
if ( in_array( $post->post_type, array( 'post', 'page' ) ) || post_type_supports( $post->post_type, 'revisions' ) ) {
1395+
if ( in_array( $post->post_type, array( 'post', 'page' ), true ) || post_type_supports( $post->post_type, 'revisions' ) ) {
13961396
$links['version-history'] = array(
13971397
'href' => rest_url( trailingslashit( $base ) . $post->ID . '/revisions' ),
1398
);
@@ -1413,7 +1413,7 @@ protected function prepare_links( $post ) {
14131413
'embeddable' => true,
14141414
);
14151415
}
1416-
if ( ! in_array( $post->post_type, array( 'attachment', 'nav_menu_item', 'revision' ) ) ) {
1416+
if ( ! in_array( $post->post_type, array( 'attachment', 'nav_menu_item', 'revision' ), true ) ) {
14171417
$attachments_url = rest_url( 'wp/v2/media' );
14181418
$attachments_url = add_query_arg( 'parent', $post->ID, $attachments_url );
14191419
$links['https://api.w.org/attachment'] = array(
@@ -1600,9 +1600,9 @@ public function get_item_schema() {
16001600
),
16011601
);
16021602
foreach ( $post_type_attributes as $attribute ) {
1603-
if ( isset( $fixed_schemas[ $this->post_type ] ) && ! in_array( $attribute, $fixed_schemas[ $this->post_type ] ) ) {
1603+
if ( isset( $fixed_schemas[ $this->post_type ] ) && ! in_array( $attribute, $fixed_schemas[ $this->post_type ], true ) ) {
16041604
continue;
1605-
} elseif ( ! in_array( $this->post_type, array_keys( $fixed_schemas ) ) && ! post_type_supports( $this->post_type, $attribute ) ) {
1605+
} elseif ( ! in_array( $this->post_type, array_keys( $fixed_schemas ), true ) && ! post_type_supports( $this->post_type, $attribute ) ) {
16061606
continue;
16071607
}
16081608

lib/endpoints/class-wp-rest-terms-controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ protected function get_terms_for_post( $prepared_args ) {
236236
* get_items() verifies that we don't have `include` set, and default
237237
* ordering is by `name`.
238238
*/
239-
if ( ! in_array( $prepared_args['orderby'], array( 'name', 'none', 'include' ) ) ) {
239+
if ( ! in_array( $prepared_args['orderby'], array( 'name', 'none', 'include' ), true ) ) {
240240
switch ( $prepared_args['orderby'] ) {
241241
case 'id':
242242
$this->sort_column = 'term_id';

plugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ function rest_validate_request_arg( $value, $request, $param ) {
493493
}
494494
}
495495

496-
if ( in_array( $args['type'], array( 'numeric', 'integer' ) ) && ( isset( $args['minimum'] ) || isset( $args['maximum'] ) ) ) {
496+
if ( in_array( $args['type'], array( 'numeric', 'integer' ), true ) && ( isset( $args['minimum'] ) || isset( $args['maximum'] ) ) ) {
497497
if ( isset( $args['minimum'] ) && ! isset( $args['maximum'] ) ) {
498498
if ( ! empty( $args['exclusiveMinimum'] ) && $value <= $args['minimum'] ) {
499499
return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be greater than %2$d (exclusive)' ), $param, $args['minimum'] ) );

tests/class-wp-test-rest-post-type-controller-testcase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ protected function check_post_data( $post, $data, $context, $links ) {
187187
$this->assertEquals( $links['up'][0]['href'], rest_url( 'wp/v2/' . $post_type->rest_base . '/' . $data['parent'] ) );
188188
}
189189

190-
if ( ! in_array( $data['type'], array( 'attachment', 'nav_menu_item', 'revision' ) ) ) {
190+
if ( ! in_array( $data['type'], array( 'attachment', 'nav_menu_item', 'revision' ), true ) ) {
191191
$this->assertEquals( $links['https://api.w.org/attachment'][0]['href'], add_query_arg( 'parent', $data['id'], rest_url( 'wp/v2/media' ) ) );
192192
}
193193

0 commit comments

Comments
 (0)
0