-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-monitoramento.xmlrpc.php
524 lines (438 loc) · 16.5 KB
/
wp-monitoramento.xmlrpc.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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
<?php /* -*- Mode: php; c-basic-offset:4; -*- */
/* Copyright (C) 2011 Governo do Estado do Rio Grande do Sul
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Total number of comments in this post and your childs
*/
function monit_post_total_contributions($post){
// error_log("POST >>>>>>>>>>>>");
// error_log( print_r($post, True) );
// error_log("Buscando timeline de ID:".$post['ID']);
$timeline = get_pages("child_of=".$post['ID']."&post_type=gdobra&post_status=publish&sort_column=post_date&sort_order=desc");
if( $timeline ) {
// error_log("TIMELINE>>>>>>>>>>>>");
// error_log( print_r($timeline, True) );
// error_log("TIMELINE<<<<<<<<<<<<");
// error_log(count($timeline));
// error_log("COUNT<<<<<<<<<<<<");
return count($timeline);
} else {
return 0;
}
}
function monit_post_total_comments($post){
$comments_count = wp_count_comments( $post['ID'] );
return $comments_count->approved;
}
/**
* Total number of user that following this post
*/
function monit_post_total_follows($post){
global $wpdb;
$obraid = $post['ID'];
$querystr = "
SELECT count(1) follows FROM user_follow where obra_id = $obraid
";
$counts = $wpdb->get_results($querystr, OBJECT);
// error_log("COUNTS>>>>>>>>>>>>");
// error_log( print_r($counts, True) );
// error_log("COUNTS<<<<<<<<<<<<");
// error_log( $counts[0]->follows );
// error_log( $counts['follows'] );
// error_log("COUNTSS<<<<<<<<<<<<");
return $counts[0]->follows;
}
/**
* Total number of "like" in this post and your childs
*/
function monit_post_total_likes($post){
return 13;
}
function _monit_method_header(&$args) {
// We don't like smart-ass people
global $wp_xmlrpc_server;
$wp_xmlrpc_server->escape( $args );
// getting rid of blog_id
array_shift($args);
// Reading the attribute list
$username = array_shift($args);
$password = array_shift($args);
// All methods in this API are being protected
if (!$user = $wp_xmlrpc_server->login($username, $password))
return $wp_xmlrpc_server->error;
return $args;
}
function _monit_prepare_post($post, $params) {
global $wp_xmlrpc_server;
$pid = $post['ID'];
$post_date = monit_post_date($post);
// error_log("POST::::::::::::::::::");
// error_log(print_r($post, True));
return array(
'id' => (string) $pid,
'title' => $post['post_title'],
'slug' => $post['post_name'],
'date' => $post_date,
'tema' => monit_post_tema($pid),
'link' => post_permalink($pid),
'format' => get_post_format($pid),
'author' => monit_post_author($post),
'categories' => monit_post_categories($post),
'tags' => monit_post_tags($post),
'tags_object' => monit_post_tags_object($post),
'comments' => monit_post_comment_info($post),
'total_contributions' => monit_post_total_contributions($post),
'total_likes' => monit_post_total_likes($post),
'total_follows' => monit_post_total_follows($post),
'thumbs' => monit_post_thumb($post, $params),
'excerpt' => monit_post_excerpt($post),
'content' => monit_post_content($post),
'post_status' => $post['post_status'],
'post_type' => get_post_type( $pid ),
'custom_fields' => $wp_xmlrpc_server->get_custom_fields($pid),
'total_comments' => monit_post_total_comments($post),
);
}
function monitoramento_getObra($args) {
#
# Retorna a lista completa dos posts(gdobra)
#
if (!is_array($args = _monit_method_header($args)))
return $args;
// error_log(' ======================================== monitoramento_getObra ARGS ======================================== ');
// error_log(print_r($args, True));
$post_type = "gdobra"; #$args[1];
if (!isset($args[1]))
return null;
$the_slug = $args[1];
// error_log($the_slug);
$query=array(
'name' => $the_slug,
'post_type' => $post_type,
'post_status' => 'publish',
'numberposts' => 1
);
$my_posts = get_posts($query);
// error_log( print_r( (array)$my_posts[0], True) );
// if( $my_posts ) {
// error_log( 'ID on the first post found '.$my_posts[0]->ID );
// }
$post = _monit_prepare_post( (array)$my_posts[0], $args);
// return $my_posts[0];
return $post;
}
function monitoramento_getObraById($args) {
#
# Retorna a lista completa dos posts(gdobra)
#
if (!is_array($args = _monit_method_header($args)))
return $args;
// error_log(' ======================================== ARGS ======================================== ');
// error_log(print_r($args, True));
$post_type = "gdobra"; #$args[1];
if (!isset($args[1]))
return null;
$the_id = $args[1];
// error_log($the_id);
$query="include=$the_id&post_type=$post_type&post_status=publish&numberposts=1";
$my_posts = get_posts($query);
// error_log( print_r( (array)$my_posts[0], True) );
// if( $my_posts ) {
// error_log( 'ID on the first post found monitoramento_getObraById '.$my_posts[0]->ID );
// }
$post = _monit_prepare_post( (array)$my_posts[0], $args);
// return $my_posts[0];
return $post;
}
function monitoramento_getObras($args) {
global $wpdb;
#
# Retorna a lista completa dos posts(gdobra)
#
if (!is_array($args = _monit_method_header($args)))
return $args;
// error_log(' ======================================== monitoramento_getObras ARGS ======================================== ');
// error_log(print_r($args, True));
$post_type = "gdobra"; #$args[1];
// if (isset($args[1]))
// $the_slug = $args[1];
$query=array(
// 'name' => $the_slug,
'post_type' => $post_type,
'post_status' => 'publish',
'post_parent' => 0, # -> Somente os posts PAI
'numberposts' => -1
);
//Ordenação: andamento, valorglobal, atualizacao, previsaoconclusao
$filtros = $args[1];
$filtro = $filtros['filtro'];
$valor = $filtros['valor'];
$ordem = $filtros['ordem'];
if($ordem == 'atualizacao'){
// gdobra_last_child_update: Este campo é salvo na OBRA qdo qq
// post do tipo "gdobra" é salvo e é um filho.
$query['meta_key'] = 'gdobra_last_child_update';
$query['orderby'] = 'meta_value DESC';
}
if($ordem == 'andamento'){
// error_log("ordenando por ANDAMENTO");
$query['meta_key'] = 'gdobra_porc_concluido';
$query['orderby'] = 'meta_value_num DESC';
}
if($ordem == 'valorglobal'){
// error_log("ordenando por VALOR GLOBAL");
$query['meta_key'] = 'gdobra_valor_global';
$query['orderby'] = 'meta_value_num DESC';
}
if($ordem == 'previsaoconclusao'){
// error_log("ordenando por FIM PREVISTO");
$query['meta_key'] = 'gdobra_fim_previsto';
$query['orderby'] = 'meta_value DESC';
}
if($filtro != ""){
switch( $filtro ){
case "secretaria":
$query['tax_query'] = array(
array(
'taxonomy' => 'tema',
// 'field' => 'slug',
'terms' => intval($valor)
)
);
break;
case "municipio":
$query['meta_query'] = array(
array(
'key' => 'gdobra_municipio',
'value' => $valor,
'compare' => 'LIKE'
)
);
break;
case "regiao":
$query['meta_query'] = array(
array(
'key' => 'gdobra_regiao',
'value' => $valor
// 'compare' => 'LIKE'
)
);
break;
}
}
$my_posts = get_posts($query);
// Handling posts found
$obras = array( );
foreach ( (array)$my_posts as $post ) {
array_push($obras, _monit_prepare_post( (array)$post, $args) );
}
return $obras;
// $post = _monit_prepare_post( (array)$my_posts[0], $args);
// // return $my_posts[0];
// return $post;
}
function monitoramento_getObraTimeline($args) {
#
# Este método retorna todos os posts(gdobra) filhos de um gdobra específico.
# São os itens da 'timeline'
# Se um id for passado, retorna somente aquele item especifico
#
if (!is_array($args = _monit_method_header($args)))
return $args;
if (!isset($args[1]))
return null;
// error_log(' ======================================== monitoramento_getObraTimeline ARGS ======================================== ');
// error_log(print_r($args, True));
$post_type = "gdobra";
$the_parent = $args[1];
// Utilizado get_pages ao invés de get_posts pois get_pages retorna a arvore correta dos posts conforme hierarquia.
// error_log(' ======================================== CONSULTA A OBRA TIMELINE ======================================== ');
if (isset($args[2])){
$theid = $args[2];
$my_posts = get_pages("include=$theid&post_type=$post_type&post_status=publish&sort_column=post_date&sort_order=asc");
}else{
$my_posts = get_pages("child_of=$the_parent&post_type=$post_type&post_status=publish&sort_column=post_date&sort_order=asc");
}
// error_log(' ====================================== CONSULTA A OBRA TIMELINE FIM ====================================== ');
// error_log( print_r( (array)$my_posts[0], True) );
// if( $my_posts ) {
// error_log( 'ID on the first post found '.$my_posts[0]->ID );
// }
// Handling posts found
$struct = array( );
foreach ( (array)$my_posts as $post ) {
array_push($struct, _monit_prepare_post( (array)$post, $args) );
}
return $struct;
}
function monitoramento_getObraStatsFilhos($args){
global $wpdb;
if (!is_array($args = _monit_method_header($args)))
return $args;
// error_log(' ======================================== ARGS ======================================== ');
// error_log(print_r($args, True));
if (isset($args[1])){
// Conta todos os filhos de uma obra.
$post_pai = $args[1];
$querystr = "
SELECT count(1) filhos
FROM $wpdb->posts
WHERE $wpdb->posts.post_parent = $post_pai
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'gdobra'
";
}else{
// Conta todos os items de timeline de obras. TOTAL GERAL
$querystr = "
SELECT count(1) filhos
FROM $wpdb->posts
WHERE $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'gdobra'
";
}
// error_log($querystr);
$pageposts = $wpdb->get_results($querystr, OBJECT);
// error_log( print_r( $pageposts, True) );
foreach ($pageposts as $key) {
return $pageposts[0]->filhos;
}
return 0;
}
function monitoramento_getObraStatsVotos($args){
global $wpdb;
if (!is_array($args = _monit_method_header($args)))
return $args;
// error_log(' ======================================== ARGS ======================================== ');
// error_log(print_r($args, True));
if (isset($args[1])){
// Conta todos os filhos de uma obra.
$post_id = $args[1];
$querystr = "
SELECT sum(wpostmeta.meta_value) votos
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = $post_id
AND wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'gdobra_voto_up'
AND wposts.post_status = 'publish'
AND wposts.post_type = 'gdobra'
";
}else{
// Conta todos os items de timeline de obras. TOTAL GERAL
$querystr = "
SELECT sum(wpostmeta.meta_value) votos
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'gdobra_voto_up'
AND wposts.post_status = 'publish'
AND wposts.post_type = 'gdobra'
";
}
// error_log($querystr);
$pageposts = $wpdb->get_results($querystr, OBJECT);
// error_log( print_r( $pageposts, True) );
foreach ($pageposts as $key) {
return $pageposts[0]->votos;
}
return 0;
}
function monitoramento_getUltimaRespostaGovObra($args){
/*
Método que retorna o ultima resposta do governo (Status Publico), publicado, de uma obra
*/
global $wpdb;
if (!is_array($args = _monit_method_header($args)))
return $args;
// error_log(' ======================================== ARGS ======================================== ');
// error_log(print_r($args, True));
if (isset($args[1])){
// Conta todos os filhos de uma obra.
$post_pai = $args[1];
$querystr = "
select * from wp_posts
where ID = (
select max(ID) #, post_title, p.post_parent, format.name formato
from wp_posts p, wp_term_relationships r,
(select t.name, tt.term_taxonomy_id from wp_term_taxonomy tt, wp_terms t where t.term_id = tt.term_id and tt.taxonomy = 'post_format') format
where p.id = r.object_id
and r.term_taxonomy_id = format.term_taxonomy_id
and p.post_type = 'gdobra'
and p.post_status = 'publish'
and format.name = 'post-format-status'
and p.post_parent = $post_pai
)
";
}
// error_log($querystr);
$pageposts = $wpdb->get_results($querystr, OBJECT);
// error_log( print_r( $pageposts, True) );
// foreach ($pageposts as $key) {
// return $pageposts[0]->filhos;
// }
$post = _monit_prepare_post( (array)$pageposts[0], $args);
return $post;
}
function monitoramento_getCidadesDasObras($args){
global $wpdb;
$querystr = "
select meta_value
from wp_postmeta
where post_id in (
select ID from wp_posts
where post_type = 'gdobra'
and post_status = 'publish'
and post_parent = 0
)
and meta_key = 'gdobra_municipio'
";
$cidades = $wpdb->get_results($querystr, OBJECT);
return $cidades;
}
function monitoramento_getSecretarias($args){
global $wpdb;
$querystr = "
select distinct t.term_id id, t.name secretaria, t.slug
from wp_term_taxonomy tt, wp_terms t
where tt.taxonomy = 'tema' and tt.term_id = t.term_id
";
$secretarias = $wpdb->get_results($querystr, OBJECT);
return $secretarias;
}
function monitoramento_getTotalObras($args){
global $wpdb;
$querystr = "
select count(1) total
from wp_posts p
where p.post_type = 'gdobra'
and p.post_status = 'publish'
and p.post_parent = 0
";
$total_obras = $wpdb->get_results($querystr, OBJECT);
return $total_obras;
}
add_filter('xmlrpc_methods', function ($methods) {
$methods['monitoramento.getObras'] = 'monitoramento_getObras';
$methods['monitoramento.getObra'] = 'monitoramento_getObra';
$methods['monitoramento.getObraById'] = 'monitoramento_getObraById';
$methods['monitoramento.getObraTimeline'] = 'monitoramento_getObraTimeline';
$methods['monitoramento.getObraStatsFilhos'] = 'monitoramento_getObraStatsFilhos';
$methods['monitoramento.getObraStatsVotos'] = 'monitoramento_getObraStatsVotos';
$methods['monitoramento.getUltimaRespostaGovObra'] = 'monitoramento_getUltimaRespostaGovObra';
$methods['monitoramento.getCidadesDasObras'] = 'monitoramento_getCidadesDasObras';
$methods['monitoramento.getSecretarias'] = 'monitoramento_getSecretarias';
$methods['monitoramento.getTotalObras'] = 'monitoramento_getTotalObras';
return $methods;
});
?>