rest.php
4.69 KB
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
<?php
function pull_posts_for_post_list( $data ) {
$request_uri = sanitize_text_field($_SERVER['REQUEST_URI']);
$request_uri = base64_encode($request_uri);
// $BYPASS_CACHE = false;
// $client = new Predis\Client();
// if(!$BYPASS_CACHE) {
// if($client->exists($request_uri)) {
// return unserialize($client->get($request_uri));
// }
// }
$categories = sanitize_textarea_field($_REQUEST['categories']);
$segment = trim(sanitize_textarea_field($_REQUEST['segment']));
$exclude = get_for_excluded_cats();
$exclude_posts = get_global_excluded_posts();
if($segment == 'all') {
$new_posts = [];
$numberposts = 500;
if(isset($_REQUEST['limit'])) {
$numberposts = $_REQUEST['limit'];
}
if(!empty($categories)) {
$posts = get_posts( array(
'category' => $categories,
'numberposts' => $numberposts,
'suppress_filters' => false,
'orderby' => 'menu_order',
'order' => 'ASC'
) );
} else {
$posts = get_posts( array(
'numberposts' => $numberposts,
'suppress_filters' => false,
'category_not_in'=>$exclude,
'exclude' => $exclude_posts,
'orderby' => 'menu_order',
'order' => 'ASC'
) );
}
foreach($posts as $post) {
$excerpt = wp_filter_nohtml_kses(get_the_excerpt($post));
$excerpt = trim(substr($excerpt, 0, strpos($excerpt, '[...]')));
$article_author = get_field('article_author', $post->ID) ?: null;
$author_title = get_field('author_title', $post->ID) ?: null;
$author_company = get_field('author_company', $post->ID) ?: null;
$img_id = get_post_thumbnail_id( $post->ID);
$alt = get_post($img_id)->post_excerpt;
$img = get_the_post_thumbnail_url($post, 'medium');
if(!$img) {
$img = get_post_meta($post->ID, 'photo_from_source', true);
}
$new_posts[] = ['date'=>en_fr_date( get_post_timestamp($post->ID)),
'article_author'=>$article_author,'author_title'=>$author_title,
'author_company'=>$author_company,'href'=>get_permalink($post),'title'=>$post->post_title,
'text'=>$excerpt,'img_src'=>$img,'img_alt'=>$alt];
}
//$client->set($request_uri, serialize(['posts'=>$new_posts,'last_index'=>0]), 'ex', 3600);
return ['posts'=>$new_posts,'last_index'=>0];
} else {
$segment = (int)$segment;
if(!empty($categories)) {
$posts = get_posts( array(
'category' => $categories,
'numberposts' => 12,
'orderby' => 'menu_order',
'order' => 'ASC',
'suppress_filters' => false
) );
} else {
$posts = get_posts( array(
'numberposts' => 12,
'orderby' => 'menu_order',
'order' => 'ASC',
'suppress_filters' => false,
'category__not_in'=>$exclude,
'exclude' => $exclude_posts
) );
}
if ( empty( $posts ) ) {
return null;
}
$segments = array_chunk($posts, 4);
$the_segment = $segments[$segment];
$posts = [];
foreach($the_segment as $post) {
$excerpt = wp_filter_nohtml_kses(get_the_excerpt($post));
//$excerpt = trim(substr($excerpt, 0, strpos($excerpt, '[...]')));
$article_author = get_field('article_author', $post->ID) ?: null;
$author_title = get_field('author_title', $post->ID) ?: null;
$author_company = get_field('author_company', $post->ID) ?: null;
$img_id = get_post_thumbnail_id( $post->ID);
$alt = get_post($img_id)->post_excerpt;
$img = get_the_post_thumbnail_url($post, 'medium_large');
if(!$img) {
$img = get_post_meta($post->ID, 'photo_from_source', true);
}
$posts[] = ['date'=>en_fr_date( get_post_timestamp($post->ID)),
'article_author'=>$article_author,'author_title'=>$author_title,
'author_company'=>$author_company,'href'=>get_permalink($post),'title'=>$post->post_title,
'text'=>$excerpt,'img_src'=>$img,'img_alt'=>$alt];
}
$last_index = false;
if( $segment >= (count($segments)-1) ) {
$last_index = true;
}
//$client->set($request_uri, serialize(['posts'=>$posts,'last_index'=>$last_index]), 'ex', 3600);
return ['posts'=>$posts,'last_index'=>$last_index];
}
}
add_action( 'rest_api_init', function () {
register_rest_route( '/v1', '/post_list', array(
'methods' => 'GET',
'callback' => 'pull_posts_for_post_list',
'permission_callback' => '__return_true'
) );
} );