-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransforms.js
53 lines (45 loc) · 1.42 KB
/
transforms.js
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
/**
* Editor: transforms.
*/
import { addFilter } from './hooks';
addFilter( 'termQuery.termMetaTransform.attachment_id_to_url', 'term-query/term-meta-transform/attachment_id_to_url', attachment_id_to_url );
addFilter( 'termQuery.termMetaTransform.attachment_id_to_image_alt', 'term-query/term-meta-transform/attachment_id_to_image_alt', attachment_id_to_image_alt );
/**
* Transform to attachment URL.
*
* @param {number} value Attachment ID.
* @param {Object} args Arguments.
* @param {Function} select Select function.
* @return {string} Attachment URL or empty string.
*/
function attachment_id_to_url( value, args, select ) {
// If value is not numeric, return early.
if ( isNaN( value ) ) {
return '';
}
const attachment = select( 'core' ).getMedia( value );
if ( ! attachment ) {
return '';
}
const size = args?.size ?? 'full';
return attachment.media_details.sizes[ size ]?.source_url ?? attachment.source_url;
};
/**
* Transform to attachment image alt.
*
* @param {number} value Attachment ID.
* @param {Object} args Arguments.
* @param {Function} select Select function.
* @return {string} Attachment image alt or empty string.
*/
function attachment_id_to_image_alt( value, args, select ) {
// If value is not numeric, return early.
if ( isNaN( value ) ) {
return '';
}
const attachment = select( 'core' ).getMedia( value );
if ( ! attachment ) {
return '';
}
return attachment.alt_text;
};