Make WordPress Core

Opened 4 years ago

Last modified 4 months ago

#52389 accepted defect (bug)

Consistently check for non-empty post ID in attachment functions

Reported by: sergeybiryukov's profile SergeyBiryukov Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: has-patch
Focuses: Cc:

Description

Background: #50679, #52196.

As a result of the changes in [49084] and [50039], wp_get_attachment_metadata() conditionally calls get_post() if the attachment ID is not passed:

$attachment_id = (int) $attachment_id;

if ( ! $attachment_id ) {
	$post = get_post();

	if ( ! $post ) {
		return false;
	}

	$attachment_id = $post->ID;
}

This is not really consistent with other attachment functions, which just always call get_post() unconditionally:

$attachment_id = (int) $attachment_id;

$post = get_post( $attachment_id );

Let's bring some consistency here, there is no reason for these minor differences.

This applies at least to:

  • wp_get_attachment_metadata()
  • wp_get_attachment_url()
  • wp_get_attachment_caption()
  • wp_get_attachment_thumb_file()
  • wp_get_attachment_thumb_url()

Change History (4)

#1 @SergeyBiryukov
4 years ago

  • Owner set to SergeyBiryukov
  • Status changed from new to accepted

#2 @lukecarbis
4 years ago

  • Keywords needs-patch added
  • Milestone changed from 5.7 to Future Release

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


4 months ago
#3

  • Keywords has-patch added; needs-patch removed

Trac Ticket: Core-52389

## Summary
This PR enhances the consistency of attachment functions in WordPress by ensuring that wp_get_attachment_metadata(), wp_get_attachment_url(), wp_get_attachment_caption(), and wp_get_attachment_thumb_url() consistently check for a non-empty post ID.

## Problem Statement

Currently, the wp_get_attachment_metadata() function checks for the ID conditionally, calling get_post() only when an ID is not provided. In contrast, other attachment functions, such as wp_get_attachment_url(), wp_get_attachment_caption(), and wp_get_attachment_thumb_url(), always assume a valid ID. This inconsistency can create confusion for developers who expect uniform behavior across all attachment functions. To improve usability and maintainability, it is essential to standardize how these functions validate and retrieve the post ID.

## Proposed Changes

  • Update the specified attachment functions to implement a consistent check for non-empty post IDs.

Ensure that get_post() is called only when necessary to retrieve the current post if no attachment ID is provided.

## Benefits:

  • Consistency: Ensuring that all relevant functions perform uniform checks for post IDs will reduce confusion for developers and improve code clarity.
  • Simplicity: This change simplifies the logic in attachment functions, making them easier to understand and maintain.

This PR seeks to improve the consistency and clarity of attachment functions in WordPress.

@debarghyabanerjee commented on PR #7451:


4 months ago
#4

Hi @SergeyBiryukov, can you please take a look into this PR. Thanks.

Note: See TracTickets for help on using tickets.