-
Notifications
You must be signed in to change notification settings - Fork 0
/
transforms.php
57 lines (49 loc) · 1.51 KB
/
transforms.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
/**
* Include: transforms.php
*
* Registers term query term meta transform filters.
*
* @author Cory Hughart <[email protected]>
* @copyright 2024 Cory Hughart
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL-3.0-or-later
* @link https://github.com/cr0ybot/term-query
* @package term-query
*/
namespace cr0ybot\Term_Query\Transforms;
add_filter( 'term_query_term_meta_transform_attachment_id_to_url', __NAMESPACE__ . '\\attachment_id_to_url', 10, 2 );
add_filter( 'term_query_term_meta_transform_attachment_id_to_image_alt', __NAMESPACE__ . '\\attachment_id_to_image_alt', 10, 2 );
/**
* Transform to attachment URL.
*
* @param mixed $meta_value The meta value, an attachment ID.
* @param array $args The binding args.
* @return string|null The attachment URL or null.
*/
function attachment_id_to_url( mixed $meta_value, array $args ) {
if ( ! is_numeric( $meta_value ) ) {
return null;
}
$url = wp_get_attachment_image_url( $meta_value, $args['size'] ?? 'full' );
if ( ! $url ) {
return null;
}
return $url;
}
/**
* Transform to attachment image alt text.
*
* @param mixed $meta_value The meta value, an attachment ID.
* @param array $args The binding args.
* @return string|null The attachment alt text or null.
*/
function attachment_id_to_image_alt( mixed $meta_value, array $args ) {
if ( ! is_numeric( $meta_value ) ) {
return null;
}
$alt = get_post_meta( $meta_value, '_wp_attachment_id_to_image_alt', true );
if ( ! $alt ) {
return null;
}
return $alt;
}