Make WordPress Core

Changeset 62895


Ignore:
Timestamp:
07/29/2026 12:07:38 PM (7 weeks ago)
Author:
SergeyBiryukov
Message:

Code Modernization: Use the null coalescing operator for isset() return checks.

This commit replaces a handful of verbose isset() guard patterns with the null coalescing operator (??):

<?php
// Before
if ( isset( $x ) ) {
    return $x;
} else {
    return $default;
}
    
// After
return $x ?? $default;

Both forms are functionally identical; ?? is shorter and clearer.

Developed in https://github.com/WordPress/wordpress-develop/pull/12602.

Follow-up to [61463], [61470], [62870].

Props Soean, westonruter.
See #64897.

Location:
trunk/src/wp-includes
Files:
4 edited

Legend:

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

    r62178 r62895  
    11641164        global $super_admins;
    11651165
    1166         if ( isset( $super_admins ) ) {
    1167                 return $super_admins;
    1168         } else {
    1169                 return get_site_option( 'site_admins', array( 'admin' ) );
    1170         }
     1166        return $super_admins ?? get_site_option( 'site_admins', array( 'admin' ) );
    11711167}
    11721168
  • trunk/src/wp-includes/customize/class-wp-customize-selective-refresh.php

    r60909 r62895  
    121121         */
    122122        public function get_partial( $id ) {
    123                 if ( isset( $this->partials[ $id ] ) ) {
    124                         return $this->partials[ $id ];
    125                 } else {
    126                         return null;
    127                 }
     123                return $this->partials[ $id ] ?? null;
    128124        }
    129125
  • trunk/src/wp-includes/functions.php

    r62835 r62895  
    14461446        }
    14471447
    1448         if ( isset( $wp_header_to_desc[ $code ] ) ) {
    1449                 return $wp_header_to_desc[ $code ];
    1450         } else {
    1451                 return '';
    1452         }
     1448        return $wp_header_to_desc[ $code ] ?? '';
    14531449}
    14541450
  • trunk/src/wp-includes/http.php

    r62293 r62895  
    856856        );
    857857
    858         if ( isset( $translation[ $constant ] ) ) {
    859                 return $translation[ $constant ];
    860         } else {
    861                 return false;
    862         }
    863 }
     858        return $translation[ $constant ] ?? false;
     859}
Note: See TracChangeset for help on using the changeset viewer.