Make WordPress Core

Opened 6 years ago

Last modified 2 years ago

#52389 accepted defect (bug)

Consistently check for non-empty post ID in attachment functions

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

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
6 years ago

  • Owner set to SergeyBiryukov
  • Status newaccepted

#2 @lukecarbis
6 years ago

  • Keywords needs-patch added
  • Milestone 5.7Future Release

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


2 years 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:


2 years ago
#4

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

Note: See TracTickets for help on using tickets.