forked from Automattic/newspack-blocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-newspack-blocks-api.php
163 lines (141 loc) · 4.08 KB
/
class-newspack-blocks-api.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
/**
* Register Newspack Blocks rest fields
*
* @package Newspack_Blocks
*/
/**
* `Newspack_Blocks_API` is a wrapper for `register_rest_fields()`
*/
class Newspack_Blocks_API {
/**
* Register Newspack REST fields.
*/
public static function register_rest_fields() {
register_rest_field(
array( 'post', 'page' ),
'newspack_featured_image_src',
array(
'get_callback' => array( 'Newspack_Blocks_API', 'newspack_blocks_get_image_src' ),
'update_callback' => null,
'schema' => null,
)
);
register_rest_field(
array( 'post', 'page' ),
'newspack_featured_image_caption',
array(
'get_callback' => array( 'Newspack_Blocks_API', 'newspack_blocks_get_image_caption' ),
'update_callback' => null,
'schema' => null,
)
);
/* Add author info source */
register_rest_field(
'post',
'newspack_author_info',
array(
'get_callback' => array( 'Newspack_Blocks_API', 'newspack_blocks_get_author_info' ),
'update_callback' => null,
'schema' => null,
)
);
/* Add first category source */
register_rest_field(
'post',
'newspack_category_info',
array(
'get_callback' => array( 'Newspack_Blocks_API', 'newspack_blocks_get_primary_category' ),
'update_callback' => null,
'schema' => null,
)
);
}
/**
* Get thumbnail featured image source for the rest field.
*
* @param Array $object The object info.
*/
public static function newspack_blocks_get_image_src( $object ) {
if ( 0 === $object['featured_media'] ) {
return;
}
// Landscape image.
$landscape_size = newspack_blocks_image_size_for_orientation( 'landscape' );
$feat_img_array_landscape = wp_get_attachment_image_src(
$object['featured_media'],
$landscape_size,
false
);
$featured_image_set['landscape'] = $feat_img_array_landscape[0];
// Portrait image.
$portrait_size = newspack_blocks_image_size_for_orientation( 'portrait' );
$feat_img_array_portrait = wp_get_attachment_image_src(
$object['featured_media'],
$portrait_size,
false
);
$featured_image_set['portrait'] = $feat_img_array_portrait[0];
// Square image.
$square_size = newspack_blocks_image_size_for_orientation( 'square' );
$feat_img_array_square = wp_get_attachment_image_src(
$object['featured_media'],
$square_size,
false
);
$featured_image_set['square'] = $feat_img_array_square[0];
return $featured_image_set;
}
/**
* Get thumbnail featured image captions for the rest field.
*
* @param Array $object The object info.
*/
public static function newspack_blocks_get_image_caption( $object ) {
return (int) $object['featured_media'] > 0 ? trim( wp_get_attachment_caption( $object['featured_media'] ) ) : null;
}
/**
* Get author info for the rest field.
*
* @param Array $object The object info.
*/
public static function newspack_blocks_get_author_info( $object ) {
$author_data = array(
/* Get the author name */
'display_name' => get_the_author_meta( 'display_name', $object['author'] ),
/* Get the author link */
'author_link' => get_author_posts_url( $object['author'] ),
/* Get the author avatar */
'avatar' => get_avatar( $object['author'], 48 ),
);
/* Return the author data */
return $author_data;
}
/**
* Get primary category for the rest field.
*
* @param Array $object The object info.
*/
public static function newspack_blocks_get_primary_category( $object ) {
$category = false;
// Use Yoast primary category if set.
if ( class_exists( 'WPSEO_Primary_Term' ) ) {
$primary_term = new WPSEO_Primary_Term( 'category', $object['id'] );
$category_id = $primary_term->get_primary_term();
if ( $category_id ) {
$category = get_term( $category_id );
}
}
if ( ! $category ) {
$categories_list = get_the_category( $object['id'] );
if ( ! empty( $categories_list ) ) {
$category = $categories_list[0];
}
}
if ( ! $category ) {
return '';
}
return $category->name;
}
}
add_action( 'rest_api_init', array( 'Newspack_Blocks_API', 'register_rest_fields' ) );