Make WordPress Core


Ignore:
Timestamp:
04/05/2020 03:00:44 AM (6 years ago)
Author:
SergeyBiryukov
Message:

Coding Standards: Use strict type check for in_array() and array_search() where strings are involved.

This reduces the number of WordPress.PHP.StrictInArray.MissingTrueStrict issues from 486 to 50.

Includes minor code layout fixes for better readability.

See #49542.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r47461 r47550  
    816816        while ( $ancestor = get_post( $id ) ) {
    817817                // Loop detection: If the ancestor has been seen before, break.
    818                 if ( empty( $ancestor->post_parent ) || ( $ancestor->post_parent == $post->ID ) || in_array( $ancestor->post_parent, $ancestors ) ) {
     818                if ( empty( $ancestor->post_parent ) || ( $ancestor->post_parent == $post->ID ) || in_array( $ancestor->post_parent, $ancestors, true ) ) {
    819819                        break;
    820820                }
     
    16171617
    16181618        foreach ( $capabilities as $core => $custom ) {
    1619                 if ( in_array( $core, array( 'read_post', 'delete_post', 'edit_post' ) ) ) {
     1619                if ( in_array( $core, array( 'read_post', 'delete_post', 'edit_post' ), true ) ) {
    16201620                        $post_type_meta_caps[ $custom ] = $core;
    16211621                }
     
    23632363function sanitize_post_field( $field, $value, $post_id, $context = 'display' ) {
    23642364        $int_fields = array( 'ID', 'post_parent', 'menu_order' );
    2365         if ( in_array( $field, $int_fields ) ) {
     2365        if ( in_array( $field, $int_fields, true ) ) {
    23662366                $value = (int) $value;
    23672367        }
     
    23692369        // Fields which contain arrays of integers.
    23702370        $array_int_fields = array( 'ancestors' );
    2371         if ( in_array( $field, $array_int_fields ) ) {
     2371        if ( in_array( $field, $array_int_fields, true ) ) {
    23722372                $value = array_map( 'absint', $value );
    23732373                return $value;
     
    24182418                }
    24192419
    2420                 if ( in_array( $field, $format_to_edit ) ) {
     2420                if ( in_array( $field, $format_to_edit, true ) ) {
    24212421                        if ( 'post_content' == $field ) {
    24222422                                $value = format_to_edit( $value, user_can_richedit() );
     
    27792779
    27802780        foreach ( $post_mime_types as $group => $labels ) {
    2781                 if ( in_array( $group, array( 'image', 'audio', 'video' ) ) ) {
     2781                if ( in_array( $group, array( 'image', 'audio', 'video' ), true ) ) {
    27822782                        continue;
    27832783                }
     
    29052905                $mime_pattern = preg_replace( '/\*+/', '%', $mime_pattern );
    29062906
    2907                 if ( in_array( $mime_type, $wildcards ) ) {
     2907                if ( in_array( $mime_type, $wildcards, true ) ) {
    29082908                        return '';
    29092909                }
     
    36983698         */
    36993699        if ( empty( $post_name ) ) {
    3700                 if ( ! in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) {
     3700                if ( ! in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ), true ) ) {
    37013701                        $post_name = sanitize_title( $post_title );
    37023702                } else {
     
    39473947        }
    39483948
    3949         if ( empty( $data['post_name'] ) && ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
     3949        if ( empty( $data['post_name'] ) && ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ), true ) ) {
    39503950                $data['post_name'] = wp_unique_post_slug( sanitize_title( $data['post_title'], $post_ID ), $post_ID, $data['post_status'], $post_type, $post_parent );
    39513951                $wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where );
     
    42004200
    42014201        // Drafts shouldn't be assigned a date unless explicitly done so by the user.
    4202         if ( isset( $post['post_status'] ) && in_array( $post['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) && empty( $postarr['edit_date'] ) &&
    4203                         ( '0000-00-00 00:00:00' == $post['post_date_gmt'] ) ) {
     4202        if ( isset( $post['post_status'] )
     4203                && in_array( $post['post_status'], array( 'draft', 'pending', 'auto-draft' ), true )
     4204                && empty( $postarr['edit_date'] ) && ( '0000-00-00 00:00:00' == $post['post_date_gmt'] )
     4205        ) {
    42044206                $clear_date = true;
    42054207        } else {
     
    43314333 */
    43324334function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent ) {
    4333         if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) || ( 'inherit' == $post_status && 'revision' == $post_type ) || 'user_request' === $post_type ) {
     4335        if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ), true )
     4336                || ( 'inherit' == $post_status && 'revision' == $post_type ) || 'user_request' === $post_type
     4337        ) {
    43344338                return $slug;
    43354339        }
     
    43774381                 * @param string $slug     The post slug.
    43784382                 */
    4379                 if ( $post_name_check || in_array( $slug, $feeds ) || 'embed' === $slug || apply_filters( 'wp_unique_post_slug_is_bad_attachment_slug', false, $slug ) ) {
     4383                $is_bad_attachment_slug = apply_filters( 'wp_unique_post_slug_is_bad_attachment_slug', false, $slug );
     4384
     4385                if ( $post_name_check
     4386                        || in_array( $slug, $feeds, true ) || 'embed' === $slug
     4387                        || $is_bad_attachment_slug
     4388                ) {
    43804389                        $suffix = 2;
    43814390                        do {
     
    44084417                 * @param int    $post_parent Post parent ID.
    44094418                 */
    4410                 if ( $post_name_check || in_array( $slug, $feeds ) || 'embed' === $slug || preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug ) || apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent ) ) {
     4419                $is_bad_hierarchical_slug = apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent );
     4420
     4421                if ( $post_name_check
     4422                        || in_array( $slug, $feeds, true ) || 'embed' === $slug
     4423                        || preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug )
     4424                        || $is_bad_hierarchical_slug
     4425                ) {
    44114426                        $suffix = 2;
    44124427                        do {
     
    44574472                 * @param string $post_type Post type.
    44584473                 */
    4459                 if ( $post_name_check || in_array( $slug, $feeds ) || 'embed' === $slug || $conflicts_with_date_archive || apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type ) ) {
     4474                $is_bad_flat_slug = apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type );
     4475
     4476                if ( $post_name_check
     4477                        || in_array( $slug, $feeds, true ) || 'embed' === $slug
     4478                        || $conflicts_with_date_archive
     4479                        || $is_bad_flat_slug
     4480                ) {
    44604481                        $suffix = 2;
    44614482                        do {
     
    52665287        // Make sure the post type is hierarchical.
    52675288        $hierarchical_post_types = get_post_types( array( 'hierarchical' => true ) );
    5268         if ( ! in_array( $parsed_args['post_type'], $hierarchical_post_types ) ) {
     5289        if ( ! in_array( $parsed_args['post_type'], $hierarchical_post_types, true ) ) {
    52695290                return false;
    52705291        }
     
    54045425        foreach ( explode( ',', $parsed_args['sort_column'] ) as $orderby ) {
    54055426                $orderby = trim( $orderby );
    5406                 if ( ! in_array( $orderby, $allowed_keys ) ) {
     5427                if ( ! in_array( $orderby, $allowed_keys, true ) ) {
    54075428                        continue;
    54085429                }
     
    54345455
    54355456        $sort_order = strtoupper( $parsed_args['sort_order'] );
    5436         if ( '' !== $sort_order && ! in_array( $sort_order, array( 'ASC', 'DESC' ) ) ) {
     5457        if ( '' !== $sort_order && ! in_array( $sort_order, array( 'ASC', 'DESC' ), true ) ) {
    54375458                $sort_order = 'ASC';
    54385459        }
     
    60706091                case 'image':
    60716092                        $image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png' );
    6072                         return in_array( $ext, $image_exts );
     6093                        return in_array( $ext, $image_exts, true );
    60736094
    60746095                case 'audio':
    6075                         return in_array( $ext, wp_get_audio_extensions() );
     6096                        return in_array( $ext, wp_get_audio_extensions(), true );
    60766097
    60776098                case 'video':
    6078                         return in_array( $ext, wp_get_video_extensions() );
     6099                        return in_array( $ext, wp_get_video_extensions(), true );
    60796100
    60806101                default:
     
    61806201                                                        continue;
    61816202                                                }
    6182                                                 if ( ! in_array( strtolower( substr( $file, -4 ) ), array( '.png', '.gif', '.jpg' ) ) ) {
     6203
     6204                                                $ext = strtolower( substr( $file, -4 ) );
     6205                                                if ( ! in_array( $ext, array( '.png', '.gif', '.jpg' ), true ) ) {
    61836206                                                        if ( is_dir( "$dir/$file" ) ) {
    61846207                                                                $dirs[ "$dir/$file" ] = "$uri/$file";
     
    62706293
    62716294        // If we haven't added this old slug before, add it now.
    6272         if ( ! empty( $post_before->post_name ) && ! in_array( $post_before->post_name, $old_slugs ) ) {
     6295        if ( ! empty( $post_before->post_name ) && ! in_array( $post_before->post_name, $old_slugs, true ) ) {
    62736296                add_post_meta( $post_id, '_wp_old_slug', $post_before->post_name );
    62746297        }
    62756298
    62766299        // If the new slug was used previously, delete it from the list.
    6277         if ( in_array( $post->post_name, $old_slugs ) ) {
     6300        if ( in_array( $post->post_name, $old_slugs, true ) ) {
    62786301                delete_post_meta( $post_id, '_wp_old_slug', $post->post_name );
    62796302        }
     
    63026325        $previous_date = gmdate( 'Y-m-d', strtotime( $post_before->post_date ) );
    63036326        $new_date      = gmdate( 'Y-m-d', strtotime( $post->post_date ) );
     6327
    63046328        // Don't bother if it hasn't changed.
    63056329        if ( $new_date == $previous_date ) {
    63066330                return;
    63076331        }
     6332
    63086333        // We're only concerned with published, non-hierarchical objects.
    63096334        if ( ! ( 'publish' === $post->post_status || ( 'attachment' === get_post_type( $post ) && 'inherit' === $post->post_status ) ) || is_post_type_hierarchical( $post->post_type ) ) {
    63106335                return;
    63116336        }
     6337
    63126338        $old_dates = (array) get_post_meta( $post_id, '_wp_old_date' );
     6339
    63136340        // If we haven't added this old date before, add it now.
    6314         if ( ! empty( $previous_date ) && ! in_array( $previous_date, $old_dates ) ) {
     6341        if ( ! empty( $previous_date ) && ! in_array( $previous_date, $old_dates, true ) ) {
    63156342                add_post_meta( $post_id, '_wp_old_date', $previous_date );
    63166343        }
     6344
    63176345        // If the new slug was used previously, delete it from the list.
    6318         if ( in_array( $new_date, $old_dates ) ) {
     6346        if ( in_array( $new_date, $old_dates, true ) ) {
    63196347                delete_post_meta( $post_id, '_wp_old_date', $new_date );
    63206348        }
     
    65256553        global $wpdb;
    65266554
    6527         if ( ! in_array( $field, array( 'date', 'modified' ) ) ) {
     6555        if ( ! in_array( $field, array( 'date', 'modified' ), true ) ) {
    65286556                return false;
    65296557        }
Note: See TracChangeset for help on using the changeset viewer.