WordPress.org

Make WordPress Core

Opened 5 months ago

Closed 5 months ago

#49449 closed defect (bug) (fixed)

Wrong require_once and misleading comment

Reported by: luisrivera Owned by: SergeyBiryukov
Milestone: 5.4 Priority: normal
Severity: normal Version: 5.3
Component: REST API Keywords:
Focuses: rest-api Cc:

Description

Location: class-wp-rest-attachments-controller.php, lines 167 - 168
https://core.trac.wordpress.org/browser/tags/5.3/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php#L167

<?php
// Include admin function to get access to wp_generate_attachment_metadata().
require_once ABSPATH . 'wp-admin/includes/media.php';

wp_generate_attachment_metadata() is actually located in 'wp-admin/includes/image.php'
https://core.trac.wordpress.org/browser/tags/5.3/src/wp-admin/includes/image.php#L458

This isn't breaking because the correct file is being required_once before by ('insert_attachment') another method in the same class but this should be corrected.

Change History (4)

#1 @SergeyBiryukov
5 months ago

Hi there, welcome to WordPress Trac! Thanks for the report.

Good catch, looks like that line has some history:

  • [43589] / #43757 switched wp-admin/includes/admin.php to image.php to reduce the scope.
  • [43604] / #43757 added an earlier image.php require, resulting in two calls in the same method.
  • [43850] / #43757 restored media.php for the required audio/video metadata functions.
  • [44206] / #45420 removed the duplicate image.php require.
  • [46422] / #47987 created a new ::insert_attachment() method and moved some code there.

To summarize, wp-admin/includes/image.php, while being necessary for wp_generate_attachment_metadata() in ::create_item(), is no longer required directly in the method. This only works because ::insert_attachment() is called earlier, which does require image.php for wp_read_image_metadata().

The safest option is probably to require both media.php and image.php in ::create_item(), like [43850] did.

Technically that would revert [44206], however, since ::create_item() and ::insert_attachment() are now separate methods, the require call would no longer be duplicate and would prevent an error in case at some point ::insert_attachment() is no longer called earlier in ::create_item().

#2 @SergeyBiryukov
5 months ago

  • Milestone changed from Awaiting Review to 5.4

#3 @SergeyBiryukov
5 months ago

In 47295:

Docs: Improve inline comments for require_once() calls in WP_REST_Attachments_Controller.

See #49449, #48303.

#4 @SergeyBiryukov
5 months ago

  • Owner set to SergeyBiryukov
  • Resolution set to fixed
  • Status changed from new to closed

In 47296:

REST API: Restore wp-admin/includes/image.php requirement in WP_REST_Attachments_Controller::create_item().

Although the file is already included via the ::insert_attachment() method, this addresses an inconsistency and corrects a misleading comment.

It also reduces the possibility of a future error in case ::insert_attachment() is no longer called earlier in ::create_item() at some point.

Follow-up to [43850] and [44206].

Props luisrivera.
Fixes #49449.

Note: See TracTickets for help on using tickets.