-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_towns.php
102 lines (90 loc) · 2.48 KB
/
get_towns.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
<?php
/**
* Define constant for enabled town IDs.
*/
define('PUEBLOS_HABILITADOS', [2312]); // ID del post
/**
* Define constant for town category ID.
*/
define('TOWN_CATEGORY_ID', 51); // ID de la categoría Pueblo
/**
* Register REST API endpoint for towns.
*/
add_action('rest_api_init', 'register_rest_towns');
/**
* Register REST route for towns.
*/
function register_rest_towns() {
register_rest_route('api/v2', 'towns', [
'methods' => WP_REST_SERVER::READABLE,
'callback' => 'rest_towns_callback',
]);
}
/**
* Callback function for the towns REST API endpoint.
*
* @param array $data Request data.
* @return WP_REST_Response Response data.
*/
function rest_towns_callback($data) {
// Get the page number from the request, default to 1.
$page_number = isset($data['p']) ? absint(sanitize_text_field($data['p'])) : 1;
// Query posts with specific parameters.
$args = [
'category' => TOWN_CATEGORY_ID,
'post_type' => 'post',
'posts_per_page' => 10,
'paged' => $page_number,
'post_status' => 'publish',
];
$posts = get_posts($args);
$response = [];
foreach ($posts as $post) {
// Get category information for the post.
$post_category = get_post_category($post->ID);
// Build the response array for each post.
$response[] = [
'post_id' => $post->ID,
'name' => $post->post_title,
'featured_img_url' => get_the_post_thumbnail_url($post->ID),
'description' => get_post_description($post->post_content),
'category_id' => $post_category['id'],
'enabled' => in_array($post->ID, PUEBLOS_HABILITADOS),
];
}
// Ensure the response is properly formatted.
return rest_ensure_response($response);
}
/**
* Get category information for a post.
*
* @param int $post_id Post ID.
* @return array Category information.
*/
function get_post_category($post_id) {
$categories = wp_get_post_categories($post_id);
$post_category = [
'id' => 0,
'name' => '',
];
foreach ($categories as $category_id) {
$category = get_category($category_id);
if ($category->name !== 'Pueblo') {
$post_category = [
'id' => $category->term_id,
'name' => $category->name,
];
break;
}
}
return $post_category;
}
/**
* Get post description by stripping tags.
*
* @param string $post_content Post content.
* @return string Stripped post description.
*/
function get_post_description($post_content) {
return wp_strip_all_tags(html_entity_decode(apply_filters('the_content', $post_content)));
}