Make WordPress Core

Changeset 63509


Ignore:
Timestamp:
09/06/2026 06:38:39 PM (8 hours ago)
Author:
SergeyBiryukov
Message:

Code Modernization: Use the null coalescing assignment operator for default value assignments.

Replaces verbose "assign a default only if not already set" patterns with the null coalescing assignment operator (??=):

// Before
$args['title'] = $args['title'] ?? '';

// After
$args['title'] ??= '';

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

Why this is safe

  • ??= requires PHP 7.4, which is already the minimum.
  • The operator is already used elsewhere in core (e.g. wp-includes/l10n.php, wp-includes/view-config.php), so this only makes existing usage consistent.
  • No behavior change.

Follow-up to r63443, r63501.

Props Soean, mukesh27.
See #65823.

Location:
trunk
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/edit-form-advanced.php

    r62696 r63509  
    6868 * @var int
    6969 */
    70 $post_ID = isset( $post_ID ) ? (int) $post_ID : 0;
    71 $user_ID = isset( $user_ID ) ? (int) $user_ID : 0;
    72 $action  = $action ?? '';
     70$post_ID  = isset( $post_ID ) ? (int) $post_ID : 0;
     71$user_ID  = isset( $user_ID ) ? (int) $user_ID : 0;
     72$action ??= '';
    7373
    7474if ( (int) get_option( 'page_for_posts' ) === $post->ID && empty( $post->post_content ) ) {
  • trunk/src/wp-includes/abilities-api/class-wp-ability.php

    r63391 r63509  
    370370                 * default, then the built-in default applies.
    371371                 */
    372                 $args['meta']['show_in_rest'] = $args['meta']['show_in_rest'] ?? $args['meta']['public'] ?? self::DEFAULT_SHOW_IN_REST;
    373                 $args['meta']['public']       = $args['meta']['public'] ?? self::DEFAULT_PUBLIC;
     372                $args['meta']['show_in_rest'] ??= $args['meta']['public'] ?? self::DEFAULT_SHOW_IN_REST;
     373                $args['meta']['public']       ??= self::DEFAULT_PUBLIC;
    374374
    375375                return $args;
  • trunk/src/wp-includes/block-supports/background.php

    r62718 r63509  
    8383
    8484                if ( ! empty( $background_styles['backgroundImage'] ) ) {
    85                         $background_styles['backgroundSize'] = $background_styles['backgroundSize'] ?? 'cover';
     85                        $background_styles['backgroundSize'] ??= 'cover';
    8686
    8787                        // If the background size is set to `contain` and no position is set, set the position to `center`.
  • trunk/src/wp-includes/class-wp-http-cookie.php

    r61435 r63509  
    190190
    191191                // Get details on the URL we're thinking about sending to.
    192                 $url         = parse_url( $url );
    193                 $url['port'] = $url['port'] ?? ( 'https' === $url['scheme'] ? 443 : 80 );
    194                 $url['path'] = $url['path'] ?? '/';
     192                $url           = parse_url( $url );
     193                $url['port'] ??= ( 'https' === $url['scheme'] ? 443 : 80 );
     194                $url['path'] ??= '/';
    195195
    196196                // Values to use for comparison against the URL.
  • trunk/src/wp-includes/class-wp-icons-registry.php

    r63443 r63509  
    321321                }
    322322
    323                 $icon            = $this->registered_icons[ $icon_name ];
    324                 $icon['content'] = $icon['content'] ?? $this->get_content( $icon_name );
     323                $icon              = $this->registered_icons[ $icon_name ];
     324                $icon['content'] ??= $this->get_content( $icon_name );
    325325
    326326                return $icon;
     
    347347                        }
    348348
    349                         $icon['content'] = $icon['content'] ?? $this->get_content( $icon['name'] );
    350                         $icons[]         = $icon;
     349                        $icon['content'] ??= $this->get_content( $icon['name'] );
     350                        $icons[]           = $icon;
    351351                }
    352352
  • trunk/src/wp-includes/functions.php

    r63491 r63509  
    62936293 */
    62946294function is_lighttpd_before_150() {
    6295         $server_parts    = explode( '/', $_SERVER['SERVER_SOFTWARE'] ?? '' );
    6296         $server_parts[1] = $server_parts[1] ?? '';
     6295        $server_parts      = explode( '/', $_SERVER['SERVER_SOFTWARE'] ?? '' );
     6296        $server_parts[1] ??= '';
    62976297
    62986298        return ( 'lighttpd' === $server_parts[0] && -1 === version_compare( $server_parts[1], '1.5.0' ) );
  • trunk/src/wp-includes/interactivity-api/class-wp-interactivity-api.php

    r63365 r63509  
    740740                                         * @since 6.9.0
    741741                                         */
    742                                         $this->derived_state_closures[ $ns ] = $this->derived_state_closures[ $ns ] ?? array();
     742                                        $this->derived_state_closures[ $ns ] ??= array();
    743743
    744744                                        // Builds path for the current property and add it to tracking if not already present.
  • trunk/src/wp-includes/link-template.php

    r63491 r63509  
    110110                $sample = true;
    111111        } else {
    112                 $post   = get_post( $post );
    113                 $sample = $sample ?? false;
     112                $post     = get_post( $post );
     113                $sample ??= false;
    114114        }
    115115
  • trunk/src/wp-includes/rest-api.php

    r63493 r63509  
    974974                                break 2;
    975975                        }
    976                         $ref[ $next ] = $ref[ $next ] ?? array();
    977                         $ref          = &$ref[ $next ];
     976                        $ref[ $next ] ??= array();
     977                        $ref            = &$ref[ $next ];
    978978                }
    979979                $last         = array_shift( $parts );
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

    r63200 r63509  
    12921292
    12931293                // Set new attachment post title with fallbacks.
    1294                 $new_attachment_post->post_title = $new_attachment_post->post_title ?? $original_attachment_post->post_title ?? $image_name;
     1294                $new_attachment_post->post_title ??= $original_attachment_post->post_title ?? $image_name;
    12951295
    12961296                // Set new attachment post caption (post_excerpt).
    1297                 $new_attachment_post->post_excerpt = $new_attachment_post->post_excerpt ?? $original_attachment_post->post_excerpt ?? '';
     1297                $new_attachment_post->post_excerpt ??= $original_attachment_post->post_excerpt ?? '';
    12981298
    12991299                // Set new attachment post description (post_content) with fallbacks.
    1300                 $new_attachment_post->post_content = $new_attachment_post->post_content ?? $original_attachment_post->post_content ?? '';
     1300                $new_attachment_post->post_content ??= $original_attachment_post->post_content ?? '';
    13011301
    13021302                // Set post parent if set in request, else the default of `0` (no parent).
    1303                 $new_attachment_post->post_parent = $new_attachment_post->post_parent ?? 0;
     1303                $new_attachment_post->post_parent ??= 0;
    13041304
    13051305                // Insert the new attachment post.
     
    15261526                                }
    15271527
    1528                                 $metadata['sizes'] = $metadata['sizes'] ?? array();
     1528                                $metadata['sizes'] ??= array();
    15291529
    15301530                                $fallback_sizes = array(
     
    33003300                                }
    33013301
    3302                                 $metadata['sizes'] = $metadata['sizes'] ?? array();
     3302                                $metadata['sizes'] ??= array();
    33033303
    33043304                                foreach ( $image_size as $name ) {
     
    33853385                                }
    33863386
    3387                                 $metadata['sizes'] = $metadata['sizes'] ?? array();
     3387                                $metadata['sizes'] ??= array();
    33883388
    33893389                                $metadata['sizes'][ $image_size ] = array(
  • trunk/src/wp-includes/widgets.php

    r63492 r63509  
    17211721        $inputs         = wp_parse_args( $inputs, $default_inputs );
    17221722
    1723         $args['title'] = $args['title'] ?? '';
    1724         $args['url']   = $args['url'] ?? '';
    1725         $args['items'] = (int) ( $args['items'] ?? 0 );
     1723        $args['title'] ??= '';
     1724        $args['url']   ??= '';
     1725        $args['items']   = (int) ( $args['items'] ?? 0 );
    17261726
    17271727        if ( $args['items'] < 1 || 20 < $args['items'] ) {
  • trunk/tests/phpunit/includes/wp-ai-client-mock-model-creation-trait.php

    r61942 r63509  
    172172                ?ModelMetadata $metadata = null
    173173        ): ModelInterface {
    174                 $metadata = $metadata ?? $this->create_test_text_model_metadata();
     174                $metadata ??= $this->create_test_text_model_metadata();
    175175
    176176                $provider_metadata = new ProviderMetadata(
     
    235235                ?ModelMetadata $metadata = null
    236236        ): ModelInterface {
    237                 $metadata = $metadata ?? $this->create_test_image_model_metadata();
     237                $metadata ??= $this->create_test_image_model_metadata();
    238238
    239239                $provider_metadata = new ProviderMetadata(
     
    294294                ?ModelMetadata $metadata = null
    295295        ): ModelInterface {
    296                 $metadata = $metadata ?? $this->create_test_speech_model_metadata();
     296                $metadata ??= $this->create_test_speech_model_metadata();
    297297
    298298                $provider_metadata = new ProviderMetadata(
     
    353353                ?ModelMetadata $metadata = null
    354354        ): ModelInterface {
    355                 $metadata = $metadata ?? $this->create_test_text_to_speech_model_metadata();
     355                $metadata ??= $this->create_test_text_to_speech_model_metadata();
    356356
    357357                $provider_metadata = new ProviderMetadata(
     
    412412                ?ModelMetadata $metadata = null
    413413        ): ModelInterface {
    414                 $metadata = $metadata ?? $this->create_test_video_model_metadata();
     414                $metadata ??= $this->create_test_video_model_metadata();
    415415
    416416                $provider_metadata = new ProviderMetadata(
     
    471471                ?ModelMetadata $metadata = null
    472472        ): ModelInterface {
    473                 $metadata = $metadata ?? $this->create_test_text_model_metadata();
     473                $metadata ??= $this->create_test_text_model_metadata();
    474474
    475475                $provider_metadata = new ProviderMetadata(
Note: See TracChangeset for help on using the changeset viewer.