Make WordPress Core

Opened 8 months ago

Last modified 3 days ago

#64620 assigned defect (bug)

function read_json_file throws an error if the file is missing

Reported by: pbearne Owned by: pbearne
Priority: normal Milestone: 7.2
Component: Themes Version: 5.8
Severity: normal Keywords: has-patch reporter-feedback changes-requested has-unit-tests
Cc: Focuses:

Description

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

Change History (18)

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


8 months ago
#1

  • Keywords has-patch added

… potential errors.

#2 @pbearne
8 months ago

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

#3 @westonruter
8 months ago

  • Keywords needs-testing added

How are you able to reproduce the issue?

#4 @mukesh27
8 months ago

  • Keywords reporter-feedback changes-requested added
  • Version 6.8 → 5.8

The function introduce in 5.8

#5 @ozgursar
8 months 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
8 months ago

@pbearne Is this a duplicate of #64087?

Last edited 8 months ago by westonruter (previous) (diff)

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


8 months ago

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


7 months ago

#9 @audrasjb
7 months ago

  • Milestone 7.0 → 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.

#10 @westonruter
3 months ago

  • Keywords needs-unit-tests added

​@pbearne commented on ​PR #10898:


3 months ago
#11

@westonruter what would you like in the else
an error
an empty array is already returned as default

#12 @pbearne
3 months ago

  • Keywords has-unit-tests added; needs-unit-tests removed

@westonruter tests added

​@wildworks commented on ​PR #10898:


3 months ago
#13

@pbearne Can you address the feedback from Copilot? Both seem like reasonable points to me.

#14 @wildworks
2 months ago

  • Milestone 7.1 → 7.2

PR 10898 needs to address CI check errors and Copilot reviews. As the 7.1RC1 release is imminent, I'd like to punt this to 7.2.

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


3 days ago

#16 @im3dabasia1
3 days ago

As per today's 7.2 bug scrub:

I see the ​PR is open with a few review points left to address. @pbearne, would you be able to take a look so we can try for 7.2?

#17 @pbearne
3 days ago

looking

​@pbearne commented on ​PR #10898:


3 days ago
#18

@t-hamano fixed
But it has an error suppression warning
Not sure how to fix it other than removing the @

Note: See TracTickets for help on using tickets.