Coding Standards: Use strict type check for in_array() and array_search().

This addresses all the remaining `WordPress.PHP.StrictInArray.MissingTrueStrict` issues in core.

Includes minor code layout fixes for better readability.

Follow-up to [47550].

See #49542.

git-svn-id: https://develop.svn.wordpress.org/trunk@47557 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov
2020-04-09 15:41:04 +00:00
parent 3df79c3380
commit d8c502e966
28 changed files with 172 additions and 77 deletions

View File

@@ -171,7 +171,7 @@ function wp_terms_checklist( $post_id = 0, $args = array() ) {
$keys = array_keys( $categories );
foreach ( $keys as $k ) {
if ( in_array( $categories[ $k ]->term_id, $args['selected_cats'] ) ) {
if ( in_array( $categories[ $k ]->term_id, $args['selected_cats'], true ) ) {
$checked_categories[] = $categories[ $k ];
unset( $categories[ $k ] );
}
@@ -228,13 +228,14 @@ function wp_popular_terms_checklist( $taxonomy, $default = 0, $number = 10, $ech
$tax = get_taxonomy( $taxonomy );
$popular_ids = array();
foreach ( (array) $terms as $term ) {
$popular_ids[] = $term->term_id;
if ( ! $echo ) { // Hack for Ajax use.
continue;
}
$id = "popular-$taxonomy-$term->term_id";
$checked = in_array( $term->term_id, $checked_terms ) ? 'checked="checked"' : '';
$checked = in_array( $term->term_id, $checked_terms, true ) ? 'checked="checked"' : '';
?>
<li id="<?php echo $id; ?>" class="popular-category">
@@ -291,7 +292,7 @@ function wp_link_category_checklist( $link_id = 0 ) {
/** This filter is documented in wp-includes/category-template.php */
$name = esc_html( apply_filters( 'the_category', $category->name, '', '' ) );
$checked = in_array( $cat_id, $checked_categories ) ? ' checked="checked"' : '';
$checked = in_array( $cat_id, $checked_categories, true ) ? ' checked="checked"' : '';
echo '<li id="link-category-', $cat_id, '"><label for="in-link-category-', $cat_id, '" class="selectit"><input value="', $cat_id, '" type="checkbox" name="link_category[]" id="in-link-category-', $cat_id, '"', $checked, '/> ', $name, '</label></li>';
}
}
@@ -339,6 +340,7 @@ function get_inline_data( $post ) {
}
$taxonomy_names = get_object_taxonomies( $post->post_type );
foreach ( $taxonomy_names as $taxonomy_name ) {
$taxonomy = get_taxonomy( $taxonomy_name );
@@ -721,7 +723,6 @@ function meta_form( $post = null ) {
<select id="metakeyselect" name="metakeyselect">
<option value="#NONE#"><?php _e( '&mdash; Select &mdash;' ); ?></option>
<?php
foreach ( $keys as $key ) {
if ( is_protected_meta( $key, 'post' ) || ! current_user_can( 'add_post_meta', $post->ID, $key ) ) {
continue;
@@ -834,6 +835,7 @@ function touch_time( $edit = 1, $for_post = 1, $tab_index = 0, $multi = 0 ) {
}
echo "\n\n";
$map = array(
'mm' => array( $mm, $cur_mm ),
'jj' => array( $jj, $cur_jj ),
@@ -841,6 +843,7 @@ function touch_time( $edit = 1, $for_post = 1, $tab_index = 0, $multi = 0 ) {
'hh' => array( $hh, $cur_hh ),
'mn' => array( $mn, $cur_mn ),
);
foreach ( $map as $timeunit => $value ) {
list( $unit, $curr ) = $value;
@@ -868,7 +871,9 @@ function touch_time( $edit = 1, $for_post = 1, $tab_index = 0, $multi = 0 ) {
*/
function page_template_dropdown( $default = '', $post_type = 'page' ) {
$templates = get_page_templates( null, $post_type );
ksort( $templates );
foreach ( array_keys( $templates ) as $template ) {
$selected = selected( $default, $templates[ $template ], false );
echo "\n\t<option value='" . esc_attr( $templates[ $template ] ) . "' $selected>" . esc_html( $template ) . '</option>';
@@ -1195,11 +1200,13 @@ function _get_plugin_from_callback( $callback ) {
// Only show errors if the meta box was registered by a plugin.
$filename = wp_normalize_path( $reflection->getFileName() );
$plugin_dir = wp_normalize_path( WP_PLUGIN_DIR );
if ( strpos( $filename, $plugin_dir ) === 0 ) {
$filename = str_replace( $plugin_dir, '', $filename );
$filename = preg_replace( '|^/([^/]*/).*$|', '\\1', $filename );
$plugins = get_plugins();
foreach ( $plugins as $name => $plugin ) {
if ( strpos( $name, $filename ) === 0 ) {
return $plugin;
@@ -1249,6 +1256,7 @@ function do_meta_boxes( $screen, $context, $object ) {
// Grab the ones the user has manually sorted.
// Pull them out of their previous context/priority and into the one the user chose.
$sorted = get_user_option( "meta-box-order_$page" );
if ( ! $already_sorted && $sorted ) {
foreach ( $sorted as $box_context => $ids ) {
foreach ( explode( ',', $ids ) as $id ) {
@@ -1780,11 +1788,13 @@ function get_settings_errors( $setting = '', $sanitize = false ) {
// Filter the results to those of a specific setting if one was set.
if ( $setting ) {
$setting_errors = array();
foreach ( (array) $wp_settings_errors as $key => $details ) {
if ( $setting == $details['setting'] ) {
$setting_errors[] = $wp_settings_errors[ $key ];
}
}
return $setting_errors;
}
@@ -1833,6 +1843,7 @@ function settings_errors( $setting = '', $sanitize = false, $hide_on_update = fa
}
$output = '';
foreach ( $settings_errors as $key => $details ) {
if ( 'updated' === $details['type'] ) {
$details['type'] = 'success';
@@ -1855,6 +1866,7 @@ function settings_errors( $setting = '', $sanitize = false, $hide_on_update = fa
$output .= "<p><strong>{$details['message']}</strong></p>";
$output .= "</div> \n";
}
echo $output;
}
@@ -2084,6 +2096,7 @@ function _post_states( $post, $echo = true ) {
$i = 0;
$post_states_string .= ' &mdash; ';
foreach ( $post_states as $state ) {
++$i;
( $i == $state_count ) ? $sep = '' : $sep = ', ';
@@ -2188,7 +2201,7 @@ function _media_states( $post ) {
if ( is_random_header_image() ) {
$header_images = wp_list_pluck( get_uploaded_header_images(), 'attachment_id' );
if ( $meta_header == $stylesheet && in_array( $post->ID, $header_images ) ) {
if ( $meta_header == $stylesheet && in_array( $post->ID, $header_images, true ) ) {
$media_states[] = __( 'Header Image' );
}
} else {
@@ -2242,7 +2255,9 @@ function _media_states( $post ) {
if ( ! empty( $media_states ) ) {
$state_count = count( $media_states );
$i = 0;
echo ' &mdash; ';
foreach ( $media_states as $state ) {
++$i;
( $i == $state_count ) ? $sep = '' : $sep = ', ';
@@ -2369,12 +2384,15 @@ function get_submit_button( $text = '', $type = 'primary large', $name = 'submit
$button_shorthand = array( 'primary', 'small', 'large' );
$classes = array( 'button' );
foreach ( $type as $t ) {
if ( 'secondary' === $t || 'button-secondary' === $t ) {
continue;
}
$classes[] = in_array( $t, $button_shorthand, true ) ? 'button-' . $t : $t;
}
// Remove empty items, remove duplicate items, and finally build a string.
$class = implode( ' ', array_unique( array_filter( $classes ) ) );