Make WordPress Core


Ignore:
Timestamp:
09/06/2026 06:38:39 PM (13 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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(
Note: See TracChangeset for help on using the changeset viewer.