Make WordPress Core

Opened 2 months ago

Closed 2 months ago

#64784 closed defect (bug) (fixed)

Refactor: Remove unnecessary false check in oEmbed data fetching

Reported by: soean's profile Soean Owned by: westonruter's profile westonruter
Milestone: 7.0 Priority: normal
Severity: normal Version:
Component: Embeds Keywords: has-patch commit
Focuses: Cc:

Description

The get_data() method of the WP_oEmbed class contains an unnecessary return:

<?php
public function get_data( $url, $args = '' ) {

        // ...

        if ( false === $data ) {
                return false;
        }

        return $data;
}

We can remove the false check, and only return $data.

Change History (7)

This ticket was mentioned in PR #11126 on WordPress/wordpress-develop by @Soean.


2 months ago
#1

  • Keywords has-patch added

This pull request makes a small change to the get_data method in class-wp-oembed.php, simplifying the method by removing an unnecessary conditional check before returning the fetched data.

Trac ticket: https://core.trac.wordpress.org/ticket/64784

#2 @swissspidy
2 months ago

  • Keywords commit added
  • Milestone changed from Awaiting Review to 7.0

#3 @manhar
2 months ago

Tested in latest trunk. Verified YouTube and Vimeo embeds in the block editor and frontend. No regressions, PHP warnings, or console errors observed. Functionality works as expected after the refactor.

This ticket was mentioned in Slack in #core-test by r1k0. View the logs.


2 months ago

#5 @sajib1223
2 months ago

Patch Testing Report

Patch tested: https://github.com/WordPress/wordpress-develop/pull/11126.diff

Environment

  • WordPress: 7.0-beta2-20260303.130610
  • PHP: 7.4.33
  • Server: PHP.wasm
  • Database: WP_SQLite_Driver (Server: 8.0.38 / Client: 3.51.0)
  • Browser: Firefox 148.0
  • OS: Windows 10/11
  • Theme: Twenty Twenty-Five 1.4
  • MU Plugins: None activated
  • Plugins:
    • Test Reports 1.2.1

Steps taken

  • Created an mu-plugin (wp-content/mu-plugins/test-oembed-get-data.php) to directly call WP_oEmbed::get_data() and inspect the return values before and after applying the patch.
<?php
add_action( 'admin_init', function () {
    if ( ! isset( $_GET['test-oembed'] ) ) {
        return;
    }

    $oembed = _wp_oembed_get_object();

    // Test 1: Valid oEmbed URL.
    $valid = $oembed->get_data( 'https://www.youtube.com/watch?v=dQw4w9WgXcQ' );

    // Test 2: Invalid / non-embeddable URL.
    $invalid = $oembed->get_data( 'https://example.com/not-embeddable' );

    echo '<h2>oEmbed get_data() Test</h2>';

    echo '<h3>Test 1 – Valid URL (YouTube)</h3>';
    echo '<pre>';
    var_dump( $valid );
    echo '</pre>';

    echo '<h3>Test 2 – Invalid URL</h3>';
    echo '<pre>';
    var_dump( $invalid );
    echo '</pre>';

    exit;
});
  • Visited wp-admin/?test-oembed=1 before the patch and recorded the output.
  • Applied the patch.
  • Visited wp-admin/?test-oembed=1 after the patch and compared.

Expected outcome

  • ✅ The patch should keep the return data unchanged.
  • ✅ Valid oEmbed URL returns an object with embed data (type, title, html, etc.) — identical before and after.
  • ✅ Invalid URL returns bool(false) — identical before and after.
  • ✅ No PHP notices, warnings, or errors introduced.

Additional Notes

  • This patch is a code simplification only. The removed intermediate variable $data and the false === $data check were redundant since $this->fetch() already returns false on failure. Returning its result directly is functionally identical.
  • Networking must be available for the valid URL test to succeed (oEmbed fetches an external provider).
Last edited 2 months ago by sajib1223 (previous) (diff)

#6 @westonruter
2 months ago

  • Owner set to westonruter
  • Status changed from new to reviewing

#7 @westonruter
2 months ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 61797:

Embeds: Remove unnecessary false check in oEmbed data fetching.

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

Follow up to r40628.

Props soean, swissspidy, ocean90, sajib1223, manhar.
Fixes #64784.

Note: See TracTickets for help on using tickets.