Make WordPress Core

Opened 7 weeks ago

Last modified 10 days ago

#64620 assigned defect (bug)

function read_json_file throws an error if the file is missing

Reported by: pbearne's profile pbearne Owned by: pbearne's profile pbearne
Milestone: 7.1 Priority: normal
Severity: normal Version: 5.8
Component: Themes Keywords: has-patch reporter-feedback changes-requested
Focuses: Cc:

Description

I got a notice if the file trying to load is missing

Change History (9)

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


7 weeks ago
#1

  • Keywords has-patch added

… potential errors.

#2 @pbearne
7 weeks ago

  • Milestone changed from Awaiting Review to 7.0
  • Owner set to pbearne
  • Status changed from new to assigned
  • Version set to 6.8

#3 @westonruter
7 weeks ago

  • Keywords needs-testing added

How are you able to reproduce the issue?

#4 @mukesh27
6 weeks ago

  • Keywords reporter-feedback changes-requested added
  • Version changed from 6.8 to 5.8

The function introduce in 5.8

#5 @ozgursar
6 weeks ago

  • Keywords needs-testing removed

Patch Testing Report

Patch Tested: https://github.com/WordPress/wordpress-develop/pull/10898

Environment

  • WordPress: 7.0-alpha-61215-src
  • PHP: 8.2.29
  • Server: nginx/1.29.4
  • Database: mysqli (Server: 8.4.7 / Client: mysqlnd 8.2.29)
  • Browser: Chrome 145.0.0.0
  • OS: macOS
  • Theme: Twenty Twenty-Five 1.4
  • MU Plugins: None activated
  • Plugins:
    • Code Snippets 3.9.5
    • Test Reports 1.2.1

Steps taken

As read_json_file is protected, I had to subclass it to reproduce the notice using the code snippet below.

  1. Add the snippet via Code Snippets plugin or functions.php to print out the notice
  2. View any page in the Dashboard to display the notice
  3. ✅ Patch is solving the problem

Expected result

  • When read_json_file() is called with a path to a non-existent file, it should return an empty array silently without triggering a PHP notice.

Screenshots/Screencast with results

Before applying patch:
https://i.imgur.com/9F63wM3.png

After applying patch:
https://i.imgur.com/9kbD2Ir.png

Support Content

  • Code snippet below is created with the help of Claude.ai
add_action( 'admin_notices', function() {
    class Test_Read_Json_File extends WP_Theme_JSON_Resolver {
        public static function test_read_json_file( $file_path ) {
            return parent::read_json_file( $file_path );
        }
    }

    $missing_file = get_theme_file_path( 'missing-file.json' );

    ob_start();
    $result = Test_Read_Json_File::test_read_json_file( $missing_file );
    $output = ob_get_clean();

    echo '<div class="notice ' . ( $output ? 'notice-error' : 'notice-success' ) . '"><p>';
    echo '<strong>Testing <code>read_json_file()</code> with missing file:</strong><br>';
    echo 'File exists: ' . ( file_exists( $missing_file ) ? 'YES' : 'NO' ) . '<br>';
    echo 'Result: <code>' . esc_html( var_export( $result, true ) ) . '</code><br>';
    echo 'Notice thrown: ' . ( $output ? '<strong style="color:red">' . esc_html( $output ) . '</strong>' : 'none ✓' );
    echo '</p></div>';
} );

#6 @westonruter
5 weeks ago

@pbearne Is this a duplicate of #64087?

Last edited 5 weeks ago by westonruter (previous) (diff)

This ticket was mentioned in Slack in #core-performance by westonruter. View the logs.


5 weeks ago

This ticket was mentioned in Slack in #core by audrasjb. View the logs.


10 days ago

#9 @audrasjb
10 days ago

  • Milestone changed from 7.0 to 7.1

As per today's 7.0 pre-RC1 bug scrub:
There's not much time before 7.0 RC1. Moving to 7.1.

Note: See TracTickets for help on using tickets.