Make WordPress Core


Ignore:
Timestamp:
04/09/2020 03:41:04 PM (5 years ago)
Author:
SergeyBiryukov
Message:

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.

File:
1 edited

Legend:

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

    r47550 r47557  
    22852285    $stickies = get_option( 'sticky_posts' );
    22862286
    2287     $is_sticky = is_array( $stickies ) && in_array( $post_id, $stickies );
     2287    if ( is_array( $stickies ) ) {
     2288        $stickies  = array_map( 'intval', $stickies );
     2289        $is_sticky = in_array( $post_id, $stickies, true );
     2290    } else {
     2291        $is_sticky = false;
     2292    }
    22882293
    22892294    /**
     
    25122517 */
    25132518function stick_post( $post_id ) {
     2519    $post_id  = (int) $post_id;
    25142520    $stickies = get_option( 'sticky_posts' );
    25152521
    25162522    if ( ! is_array( $stickies ) ) {
    2517         $stickies = array( $post_id );
    2518     }
    2519 
    2520     if ( ! in_array( $post_id, $stickies ) ) {
     2523        $stickies = array();
     2524    }
     2525
     2526    $stickies = array_map( 'intval', $stickies );
     2527
     2528    if ( ! in_array( $post_id, $stickies, true ) ) {
    25212529        $stickies[] = $post_id;
    25222530    }
     
    25462554 */
    25472555function unstick_post( $post_id ) {
     2556    $post_id  = (int) $post_id;
    25482557    $stickies = get_option( 'sticky_posts' );
    25492558
     
    25522561    }
    25532562
    2554     if ( ! in_array( $post_id, $stickies ) ) {
     2563    $stickies = array_map( 'intval', $stickies );
     2564
     2565    if ( ! in_array( $post_id, $stickies, true ) ) {
    25552566        return;
    25562567    }
    25572568
    2558     $offset = array_search( $post_id, $stickies );
     2569    $offset = array_search( $post_id, $stickies, true );
    25592570    if ( false === $offset ) {
    25602571        return;
     
    28422853        $mimes = array_map( 'trim', explode( ',', $type ) );
    28432854        foreach ( $mimes as $mime ) {
    2844             $regex                 = str_replace( '__wildcard__', $wild, preg_quote( str_replace( '*', '__wildcard__', $mime ) ) );
     2855            $regex = str_replace( '__wildcard__', $wild, preg_quote( str_replace( '*', '__wildcard__', $mime ) ) );
     2856
    28452857            $patternses[][ $type ] = "^$regex$";
     2858
    28462859            if ( false === strpos( $mime, '/' ) ) {
    28472860                $patternses[][ $type ] = "^$regex/";
     
    28552868        foreach ( $patterns as $type => $pattern ) {
    28562869            foreach ( (array) $real_mime_types as $real ) {
    2857                 if ( preg_match( "#$pattern#", $real ) && ( empty( $matches[ $type ] ) || false === array_search( $real, $matches[ $type ] ) ) ) {
     2870                if ( preg_match( "#$pattern#", $real )
     2871                    && ( empty( $matches[ $type ] ) || false === array_search( $real, $matches[ $type ], true ) )
     2872                ) {
    28582873                    $matches[ $type ][] = $real;
    28592874                }
     
    28612876        }
    28622877    }
     2878
    28632879    return $matches;
    28642880}
     
    29152931        }
    29162932    }
     2933
    29172934    if ( ! empty( $wheres ) ) {
    29182935        $where = ' AND (' . join( ' OR ', $wheres ) . ') ';
    29192936    }
     2937
    29202938    return $where;
    29212939}
     
    44374455        $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID ) );
    44384456
     4457        $post = get_post( $post_ID );
     4458
    44394459        // Prevent new post slugs that could result in URLs that conflict with date archives.
    4440         $post                        = get_post( $post_ID );
    44414460        $conflicts_with_date_archive = false;
    44424461        if ( 'post' === $post_type && ( ! $post || $post->post_name !== $slug ) && preg_match( '/^[0-9]+$/', $slug ) ) {
     
    44454464            if ( $slug_num ) {
    44464465                $permastructs   = array_values( array_filter( explode( '/', get_option( 'permalink_structure' ) ) ) );
    4447                 $postname_index = array_search( '%postname%', $permastructs );
     4466                $postname_index = array_search( '%postname%', $permastructs, true );
    44484467
    44494468                /*
     
    55015520        $num_pages = count( $pages );
    55025521        for ( $i = 0; $i < $num_pages; $i++ ) {
    5503             if ( in_array( $pages[ $i ]->ID, $exclude ) ) {
     5522            if ( in_array( $pages[ $i ]->ID, $exclude, true ) ) {
    55045523                unset( $pages[ $i ] );
    55055524            }
     
    58125831function wp_get_attachment_metadata( $attachment_id = 0, $unfiltered = false ) {
    58135832    $attachment_id = (int) $attachment_id;
    5814     $post          = get_post( $attachment_id );
     5833
     5834    $post = get_post( $attachment_id );
    58155835    if ( ! $post ) {
    58165836        return false;
     
    58465866function wp_update_attachment_metadata( $attachment_id, $data ) {
    58475867    $attachment_id = (int) $attachment_id;
    5848     $post          = get_post( $attachment_id );
     5868
     5869    $post = get_post( $attachment_id );
    58495870    if ( ! $post ) {
    58505871        return false;
     
    58795900function wp_get_attachment_url( $attachment_id = 0 ) {
    58805901    $attachment_id = (int) $attachment_id;
    5881     $post          = get_post( $attachment_id );
     5902
     5903    $post = get_post( $attachment_id );
    58825904    if ( ! $post ) {
    58835905        return false;
Note: See TracChangeset for help on using the changeset viewer.