| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Create API fields for additional info |
| 5 |
*/ |
| 6 |
if (!function_exists('blockspare_post_register_rest_fields')) { |
| 7 |
function blockspare_post_register_rest_fields() |
| 8 |
{ |
| 9 |
|
| 10 |
register_rest_field( |
| 11 |
'post', |
| 12 |
'featured_image_urls', |
| 13 |
array( |
| 14 |
'get_callback' => 'blockspare_featured_image_urls', |
| 15 |
'update_callback' => null, |
| 16 |
'schema' => array( |
| 17 |
'description' => __('Different sized featured images', 'blockspare'), |
| 18 |
'type' => 'array', |
| 19 |
), |
| 20 |
) |
| 21 |
); |
| 22 |
|
| 23 |
// Excer |
| 24 |
|
| 25 |
/* Add author info */ |
| 26 |
register_rest_field( |
| 27 |
'post', |
| 28 |
'author_info', |
| 29 |
array( |
| 30 |
'get_callback' => 'blockspare_get_author_infos', |
| 31 |
'update_callback' => null, |
| 32 |
'schema' => null, |
| 33 |
) |
| 34 |
); |
| 35 |
|
| 36 |
/* Add author info */ |
| 37 |
register_rest_field( |
| 38 |
'post', |
| 39 |
'category_info', |
| 40 |
array( |
| 41 |
'get_callback' => 'blockspare_get_category_infos', |
| 42 |
'update_callback' => null, |
| 43 |
'schema' => null, |
| 44 |
) |
| 45 |
); |
| 46 |
|
| 47 |
/* Add author info */ |
| 48 |
register_rest_field( |
| 49 |
'post', |
| 50 |
'tag_info', |
| 51 |
array( |
| 52 |
'get_callback' => 'blockspare_get_tag_infos', |
| 53 |
'update_callback' => null, |
| 54 |
'schema' => null, |
| 55 |
) |
| 56 |
); |
| 57 |
|
| 58 |
register_rest_field( |
| 59 |
'post', |
| 60 |
'comment_count', |
| 61 |
array( |
| 62 |
'get_callback' => 'blockspare_get_comment_count', |
| 63 |
'update_callback' => null, |
| 64 |
'schema' => null, |
| 65 |
) |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
add_action('rest_api_init', 'blockspare_post_register_rest_fields'); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
/** |
| 74 |
* Get author info for the rest field |
| 75 |
* |
| 76 |
* @param String $object The object type. |
| 77 |
* @param String $field_name Name of the field to retrieve. |
| 78 |
* @param String $request The current request object. |
| 79 |
*/ |
| 80 |
if (!function_exists('blockspare_get_author_infos')) { |
| 81 |
function blockspare_get_author_infos($object, $field_name, $request) |
| 82 |
{ |
| 83 |
if (!isset($object['author'])) |
| 84 |
return; |
| 85 |
|
| 86 |
global $post; |
| 87 |
$author_id = get_post_field('post_author', $object['author']); |
| 88 |
$blocspare = new BlocksapreMultiAuthorForBackend(); |
| 89 |
$author_data = $blocspare->blockspare_by_author($post); |
| 90 |
|
| 91 |
// return $author_id; |
| 92 |
/* Get the author name */ |
| 93 |
// $author_data['display_name'] = get_the_author_meta('display_name', $object['author']); |
| 94 |
|
| 95 |
/* Get the author link */ |
| 96 |
// $author_data['author_link'] = get_author_posts_url($object['author']); |
| 97 |
|
| 98 |
/* Return the author data */ |
| 99 |
return $author_data; |
| 100 |
} |
| 101 |
} |
| 102 |
if (!function_exists('blockspare_get_category_infos')) { |
| 103 |
function blockspare_get_category_infos($object, $field_name, $request) |
| 104 |
{ |
| 105 |
return get_the_category_list(' ', '', $object['id']); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
if (!function_exists('blockspare_get_tag_infos')) { |
| 110 |
function blockspare_get_tag_infos($object, $field_name, $request) |
| 111 |
{ |
| 112 |
|
| 113 |
ob_start(); |
| 114 |
$cate_name = ''; |
| 115 |
if (!empty($object)) { |
| 116 |
foreach ($object['categories'] as $cat_id) { |
| 117 |
$cate_name = get_cat_name($cat_id); |
| 118 |
} |
| 119 |
} |
| 120 |
ob_clean(); |
| 121 |
return $cate_name; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
if (!function_exists('blockspare_get_comment_count')) { |
| 126 |
function blockspare_get_comment_count($object, $field_name, $reques) |
| 127 |
{ |
| 128 |
|
| 129 |
return get_comments_number($object['id']); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
if (!function_exists('blockspare_featured_image_urls')) { |
| 134 |
/** |
| 135 |
* Get the different featured image sizes that the blog will use. |
| 136 |
* Used in the custom REST API endpoint. |
| 137 |
* |
| 138 |
* @since 1.7 |
| 139 |
*/ |
| 140 |
function blockspare_featured_image_urls($object, $field_name, $request) |
| 141 |
{ |
| 142 |
return blockspare_featured_image_urls_from_url(!empty($object['featured_media']) ? $object['featured_media'] : ''); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
if (!function_exists('blockspare_featured_image_urls_from_url')) { |
| 148 |
/** |
| 149 |
* Get the different featured image sizes that the blog will use. |
| 150 |
* |
| 151 |
* @since 2.0 |
| 152 |
*/ |
| 153 |
function blockspare_featured_image_urls_from_url($attachment_id) |
| 154 |
{ |
| 155 |
|
| 156 |
$image = wp_get_attachment_image_src($attachment_id, 'full', false); |
| 157 |
$sizes = get_intermediate_image_sizes(); |
| 158 |
|
| 159 |
$imageSizes = array( |
| 160 |
'full' => is_array($image) ? $image : '', |
| 161 |
); |
| 162 |
|
| 163 |
foreach ($sizes as $size) { |
| 164 |
$imageSizes[$size] = is_array($image) ? wp_get_attachment_image_src($attachment_id, $size, false) : ''; |
| 165 |
} |
| 166 |
|
| 167 |
return $imageSizes; |
| 168 |
} |
| 169 |
} |
| 170 |
|