Media: Add short-circuit filter to attachment_url_to_postid().

Introduces the filter `pre_attachment_url_to_postid` to allow developers to short-circuit the function `attachment_url_to_postid()`.

The return values are expected to be an attachment ID, zero (`0`) to indicate no attachment was found or `null` to indicate the function should proceed as usual.

The function performs an expensive database query so developers making use of the function frequently may wish to use a custom table with appropriate indexes to reduce the load on their database server.

Props antpb, apermo, audrasjb, joedolson.
Fixes #61383.

Built from https://develop.svn.wordpress.org/trunk@59118


git-svn-id: http://core.svn.wordpress.org/trunk@58514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Peter Wilson 2024-09-30 03:19:17 +00:00
parent a03401429b
commit b7d4ca5298
2 changed files with 26 additions and 1 deletions

View File

@ -5383,6 +5383,31 @@ function wp_maybe_generate_attachment_metadata( $attachment ) {
function attachment_url_to_postid( $url ) {
global $wpdb;
/**
* Filters the attachment ID to allow short-circuit the function.
*
* Allows plugins to short-circuit attachment ID lookups. Plugins making
* use of this function should return:
*
* - 0 (integer) to indicate the attachment is not found,
* - attachment ID (integer) to indicate the attachment ID found,
* - null to indicate WordPress should proceed with the lookup.
*
* Warning: The post ID may be null or zero, both of which cast to a
* boolean false. For information about casting to booleans see the
* {@link https://www.php.net/manual/en/language.types.boolean.php PHP documentation}.
* Use the === operator for testing the post ID when developing filters using
* this hook.
*
* @param int|null $post_id The result of the post ID lookup. Null to indicate
* no lookup has been attempted. Default null.
* @param string $url The URL being looked up.
*/
$post_id = apply_filters( 'pre_attachment_url_to_postid', null, $url );
if ( null !== $post_id ) {
return (int) $post_id;
}
$dir = wp_get_upload_dir();
$path = $url;

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.7-alpha-59117';
$wp_version = '6.7-alpha-59118';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.