Repository navigation
Expand file tree
/
Copy pathdisplay-api.php
More file actions
245 lines (209 loc) 路 7.29 KB
/
Copy pathdisplay-api.php
File metadata and controls
245 lines (209 loc) 路 7.29 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
# Set Header
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *'); // open for all
# Define
# Amount of posts to display
$NUM_POSTS = get_field('quartiersdisplays_slides', 'option') ? get_field('quartiersdisplays_slides', 'option') : 10;
$SLIDE_DURATION = get_field('quartiersdisplays_slide_duration', 'option') ? get_field('quartiersdisplays_slide_duration', 'option') * 1000 : 8000;
$REMAINING_POSTS = 0;
$posts = [];
# Define Meta
$meta = array(
'name' => get_field('quartiersplattform-name', 'option'),
'slide_duration' => $SLIDE_DURATION,
);
# Query Posts
# Query 10 veranstaltugen chronologically not older than today
$args1 = array(
'post_type'=>'veranstaltungen',
'post_status'=>'publish',
'offset' => '0',
'meta_query' => array(
'relation' => 'AND',
'date_clause' => array(
'key' => 'event_date',
'value' => date("Y-m-d"),
'compare' => '>=',
'type' => 'DATE'
),
'time_clause' => array(
'key' => 'event_time',
'compare' => '=',
),
),
'orderby' => array(
'date_clause' => 'ASC',
'time_clause' => 'ASC',
),
'posts_per_page' => $NUM_POSTS * 0.5,
);
# Query 10 nachrichten chronologically
$args2 = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'nachrichten',
'post_status' => 'publish',
'suppress_filters' => true,
'posts_per_page' => $NUM_POSTS * 0.5,
'date_query' => array(
'after' => date('Y-m-d', strtotime('-1 months'))
),
);
# Query 10 umfragen chronologically not older than 1 week
$args3 = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'umfragen',
'post_status' => 'publish',
'suppress_filters' => true,
'date_query' => array(
'after' => date('Y-m-d', strtotime('-10 days'))
),
'posts_per_page' => $NUM_POSTS,
);
# Query random projekte
$args4 = array(
'orderby' => 'rand',
'post_type' => 'projekte',
'post_status' => 'publish',
'suppress_filters' => true,
'posts_per_page' => $NUM_POSTS > 2 ? $NUM_POSTS : 2,
);
# Get latest projekte
$args5 = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'projekte',
'post_status' => 'publish',
'suppress_filters' => true,
'posts_per_page' => 1,
);
# check if $args5 is already in $args4
if (in_array($args5, $args4)) {
$args5 = array();
}
# Funktion to remove seconds from time
function remove_seconds($time) {
return substr($time, 0, -3);
}
# Additional Posts
$additional_post = array();
# Energie Ampel
# Add Energie Ampel information to additional_post
# Get data from http://api.energiewetter.de/
$energie_wetter = file_get_contents('http://api.energiewetter.de/');
$energie_wetter = json_decode($energie_wetter, true);
# get the next phase from forecast which is not the current phase
if (isset($energie_wetter)) {
$energie_wetter_object = array_shift(array_filter($energie_wetter['forecast'], function($item) use ($energie_wetter) {
return $item['color'] != $energie_wetter['current']['color'];
}));
}
# write Energie Ampel to array
if ($energie_wetter['current']['color'] !== null) {
$additional_post[] = array(
'id' => 'energie_wetter',
'title' => 'Energiewetter f眉r Wuppertal',
'subtitle' => $energie_wetter['current']['color'],
'content' => $energie_wetter['current'],
'type' => 'energie_wetter',
'text' => 'Ab '.$energie_wetter_object['time'].' Uhr ist die '.$energie_wetter_object['label']['plural'].' Phase',
);
}
# Get Image from settings quartiersdisplays_office_image
$image_office = get_field('quartiersdisplays_office_image', 'option');
if (empty( $image_office )) {
$image_office = get_template_directory_uri()."/assets/images/quartier.png";
} else {
$image_office = $image_office['url'];
}
# Promote Quartiersplattform with Name and Link
$link = home_url();
$link = str_replace('https://', '', $link);
$link = str_replace('http://', '', $link);
get_field('qp_display_state', 'option', false) &&
$additional_post[] = array(
'id' => 'quartiersplattform',
'title' => get_field('qp_display_title', 'option') ? get_field('qp_display_title', 'option') : get_field('quartiersplattform-name', 'option'),
'subtitle' => get_field('qp_display_subtitle', 'option'),
'link' => $link,
'type' => 'info',
);
# Promote Aufbruch am Arrenberg
get_field('quartiersdisplays_office', 'option', false) &&
$additional_post[] = array(
'id' => 'office',
'title' => get_field('quartiersdisplays_office_title', 'option'),
'subtitle' => get_field('quartiersdisplays_office_subtitle', 'option'),
'type' => 'info',
// 'text' => get_field('quartiersdisplays_office_text', 'option'),
'image' => esc_url($image_office),
);
# Merge Posts
$posts = array_merge(get_posts($args1), get_posts($args2), get_posts($args3), get_posts($args4), get_posts($args5));
# Remove from posts to fit limit NUM_POSTS
$posts = array_slice($posts, 0, $NUM_POSTS);
# clean array
$content = array();
foreach ($posts as $post) {
$content[] = array(
'id' => $post->ID,
'title' => $post->post_title,
'subtitle' => get_post_meta($post->ID)['slogan'][0],
'content' => $post->post_content,
'date' => $post->post_date,
'image' => get_the_post_thumbnail_url($post->ID),
'type' => $post->post_type,
'author' => get_the_author_meta('display_name', $post->post_author),
// return projekt name
'project' => $post->post_type == 'projekte' ? get_the_title($post->ID) : get_the_title(get_the_terms($post->ID, 'projekt')[0]->description),
// veranstaltungen
'event_date' => get_field('event_date', $post->ID),
'event_time' => remove_seconds(get_field('event_time', $post->ID)),
'event_end_time' => remove_seconds(get_field('event_end_time', $post->ID)),
// text
'text' => get_post_meta($post->ID)['text'][0],
// emoji
'emoji' => $post->post_type == 'projekte' ? get_post_meta($post->ID)['emoji'][0] : get_post_meta(get_the_terms($post->ID, 'projekt')[0]->description)['emoji'][0],
// umfragen
'poll' => getPollData($post->ID),
);
}
# Formate Poll Data
function getPollData($id) {
if (!$id) return;
$array = get_post_meta($id, 'polls', true);
if (!$array) return;
// remove user from array
foreach ($array as $key => $value) {
unset($array[$key]['user']);
}
// format array
$formatted = array();
foreach ($array as $key => $value) {
$formatted[] = array(
'title' => $value['field'],
'votes' => $value['count'],
'percentage' => $value['percentage'],
'total-votes' => $value['total_voter'],
);
}
return $formatted;
}
# Combine posts and additional_post
$all_content = array_merge($content, $additional_post);
# Randomly distribute posts in array
shuffle($all_content);
# filter and remove all objects in $all_content with id dublicate
$all_content = array_filter($all_content, function($item) {
static $ids = array();
if (in_array($item['id'], $ids)) {
return false;
}
$ids[] = $item['id'];
return true;
});
# Return posts
echo json_encode(array('meta' => $meta, 'content' => $all_content));
?>