-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
executable file
·329 lines (270 loc) · 9.61 KB
/
functions.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
<?php
// Estilos
function theme_enqueue_styles() {
$parent_style = 'parent-style';
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'child-style',
get_stylesheet_directory_uri() . '/style.css',
array( $parent_style )
);
wp_enqueue_script('npds', get_stylesheet_directory_uri() . '/assets/js/npd.js', array('jquery'));
}
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles', 99 );
// Criando novos post types
function new_post_types() {
register_post_type('NPD',array(
'public' => true,
'labels' => array(
'name' => 'NPDs',
'add_new' => 'Adicionar novo NPD',
'edit_item' => 'Editar NPD',
'all_items' => 'Todos os NPDs',
'attributes' => 'Configurações',
'singular_name' => 'NPD'
),
'show_in_rest' => true,
'supports' => [
'editor',
'title',
'page-attributes'
],
'menu_icon' => 'dashicons-align-left',
'hierarchical' => true,
'has_archive' => true
));
register_post_type('profissional',array(
'public' => true,
'labels' => array(
'name' => 'Profissionais',
'add_new' => 'Adicionar Novo Profissional',
'edit_item' => 'Editar Profissional',
'all_items' => 'Todos os profissionais',
'singular_name' => 'Profissional'
),
'show_in_rest' => true,
'supports' => [
'title',
'page-attributes',
'thumbnail'
],
'menu_icon' => 'dashicons-groups',
'has_archive' => true
));
}
add_action('init','new_post_types');
// Criando os meta boxes
function add_custom_meta_box() {
add_meta_box( 'meta-box-mapas-culturais', 'Link para mapas culturais', 'add_meta_box_npd', 'NPD', 'side' );
add_meta_box( 'meta-box-profissionais', 'Dados complementares', 'add_meta_box_profissionais', 'profissional' );
}
add_action( 'add_meta_boxes', 'add_custom_meta_box' );
// Criando os campos dos meta boxes
function add_meta_box_npd( $post ) {
$mapas_culturais = get_post_custom( $post -> ID );
$mapas_culturais_box = isset( $mapas_culturais[ 'mapas_culturais' ] ) ? esc_attr( $mapas_culturais[ 'mapas_culturais' ][ 0 ] ) : '';
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<p>
<label for="mapas_culturais">
Link para página do NPD na plataforma Mapas Culturais. (ex: http://mapas.cultura.gov.br/espaco/1234)
</label>
<input type="text" class="widefat" name="mapas_culturais" id="mapas_culturais" value="<?php echo $mapas_culturais_box; ?>"/>
</p>
<?php
}
function add_meta_box_profissionais( $post ) {
$profissionais = get_post_custom( $post -> ID );
$profissionais_cidade_box = isset( $profissionais[ 'profissionais_cidade' ] ) ? esc_attr( $profissionais[ 'profissionais_cidade' ][ 0 ] ) : '';
$profissionais_estado_box = isset( $profissionais[ 'profissionais_estado' ] ) ? esc_attr( $profissionais[ 'profissionais_estado' ][ 0 ] ) : '';
$profissionais_telefone_box = isset( $profissionais[ 'profissionais_telefone' ] ) ? esc_attr( $profissionais[ 'profissionais_telefone' ][ 0 ] ) : '';
$profissionais_email_box = isset( $profissionais[ 'profissionais_email' ] ) ? esc_attr( $profissionais[ 'profissionais_email' ][ 0 ] ) : '';
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<p>
<label for="profissionais_cidade">Cidade</label>
<input type="text" class="widefat" name="profissionais_cidade" id="profissionais_cidade" value="<?php echo $profissionais_cidade_box; ?>"/>
</p>
<p>
<label for="profissionais_estado">Estado</label>
<input type="text" class="widefat" name="profissionais_estado" id="profissionais_estado" value="<?php echo $profissionais_estado_box; ?>"/>
</p>
<p>
<label for="profissionais_telefone">Telefone</label>
<input type="text" class="widefat" name="profissionais_telefone" id="profissionais_telefone" value="<?php echo $profissionais_telefone_box; ?>"/>
</p>
<p>
<label for="profissionais_email">E-mail</label>
<input type="text" class="widefat" name="profissionais_email" id="profissionais_email" value="<?php echo $profissionais_email_box; ?>"/>
</p>
<?php
}
// Salvando os meta boxes
function save_meta_box_npd( $post_id ) {
// Return/Bail out if doing autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Checking if the nonce isn't there, or we can't verify it, then we should return
if ( ! isset( $_POST[ 'meta_box_nonce' ] ) || ! wp_verify_nonce( $_POST[ 'meta_box_nonce' ], 'my_meta_box_nonce' ) ) {
return;
}
// Checking if the current user can't edit this post, then we should return
if ( ! current_user_can( 'edit_posts' ) ) {
return;
}
// Saving the data in meta box
// Saving the team designation in the meta box
if ( isset( $_POST[ 'mapas_culturais' ] ) ) {
$current_value = get_post_meta($post_id, 'mapas_culturais', true);
if ($_POST['mapas_culturais'] == $current_value && get_post_meta($post_id, '_mapas_id', true)) {
return;
}
if ( preg_match_all('/^.+\/espaco\/(\d+)\/?$/', $_POST[ 'mapas_culturais' ], $m) ) {
update_post_meta( $post_id, 'mapas_culturais', sanitize_text_field( $_POST[ 'mapas_culturais' ] ) );
if (isset($m[1][0])) {
$id = $m[1][0];
$api_baseurl = 'http://mapas.cultura.gov.br/';
$fields = [
'id',
'location',
'name',
'emailPublico',
'emailPrivado',
'telefonePublico',
'endereco',
'En_Municipio',
'En_Estado',
'site',
'facebook',
'twitter',
'instagram'
];
$query_url = $api_baseurl . "api/space/find?id=EQ($id)&@select=" . implode(',', $fields);
$query = wp_remote_get($query_url);
if ( isset($query['body']) ) {
$response = json_decode($query['body']);
if (is_array($response) && isset($response[0]) && is_object($response[0])) {
$response = $response[0];
} else {
return;
}
foreach ( $fields as $field ) {
if (isset($response->$field)) {
update_post_meta( $post_id, '_mapas_' . $field, $response->$field);
}
}
}
}
}
}
}
add_action( 'save_post', 'save_meta_box_npd' );
// Salvando os meta boxes
function save_meta_box_profissional( $post_id ) {
// Return/Bail out if doing autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Checking if the nonce isn't there, or we can't verify it, then we should return
if ( ! isset( $_POST[ 'meta_box_nonce' ] ) || ! wp_verify_nonce( $_POST[ 'meta_box_nonce' ], 'my_meta_box_nonce' ) ) {
return;
}
// Checking if the current user can't edit this post, then we should return
if ( ! current_user_can( 'edit_posts' ) ) {
return;
}
// Saving the data in meta box
// Saving the team designation in the meta box
if ( isset( $_POST[ 'profissionais_cidade' ] ) ) {
update_post_meta( $post_id, 'profissionais_cidade', sanitize_text_field( $_POST[ 'profissionais_cidade' ] ) );
}
if ( isset( $_POST[ 'profissionais_estado' ] ) ) {
update_post_meta( $post_id, 'profissionais_estado', sanitize_text_field( $_POST[ 'profissionais_estado' ] ) );
}
if ( isset( $_POST[ 'profissionais_telefone' ] ) ) {
update_post_meta( $post_id, 'profissionais_telefone', sanitize_text_field( $_POST[ 'profissionais_telefone' ] ) );
}
if ( isset( $_POST[ 'profissionais_email' ] ) ) {
update_post_meta( $post_id, 'profissionais_email', sanitize_text_field( $_POST[ 'profissionais_email' ] ) );
}
}
add_action( 'save_post', 'save_meta_box_profissional' );
/*
* Taxonomia
*/
function create_taxonomy_type() {
register_taxonomy(
'area_profissional',
'profissional',
array(
'labels' => array(
'name' => 'Área',
'add_new_item' => 'Adicionar Nova Área',
'edit_item' => 'Editar Área',
'all_items' => 'Todas as Áreas',
'singular_name' => 'Área'
),
'rewrite' => array( 'slug' => 'area' ),
'hierarchical' => true,
)
);
register_taxonomy(
'especialidade_profissional',
'profissional',
array(
'labels' => array(
'name' => 'Especialidades',
'add_new_item' => 'Adicionar Nova Especialidade',
'edit_item' => 'Editar Especialidade',
'all_items' => 'Todas as Especialidades',
'singular_name' => 'Especialidade'
),
'rewrite' => array( 'slug' => 'especialidade' ),
'hierarchical' => true,
)
);
}
add_action( 'init', 'create_taxonomy_type' );
/**
* Para usar dentro do loop do post type NPDs ou em uma single de NPG
* Imprime a lista de eventos, conseguidas através dos Mapas Culturais
* do NPD atual, desde que ele tenha o metadado do Mapas Culturais preenchido e válido
*
*/
function npds_the_events() {
$post = get_post();
if (!$post || !isset($post->post_type) || 'npd' != $post->post_type) {
return;
}
$mapas_url = get_post_meta($post->ID, 'mapas_culturais', true);
if ( preg_match_all('/^.+\/espaco\/(\d+)\/?$/', $mapas_url, $m) ) {
if (isset($m[1][0])) {
$id = $m[1][0];
echo do_shortcode('[list_events url=http://museus.cultura.gov.br space=' . $id . ']');
}
}
}
/**
* Imprime a lista de todos os eventos, conseguidas através dos Mapas Culturais
*
*/
function npds_all_events() {
echo do_shortcode('[list_events url=http://museus.cultura.gov.br]');
}
add_action('pre_get_posts', function($query) {
if (!is_admin() && $query->is_post_type_archive('npd') && $query->is_main_query()) {
$query->set('post_parent', 0);
$query->set('orderby', 'meta_value');
$query->set('meta_key', '_mapas_En_Municipio');
}
});
require_once('inc/list-events-shortcode/listevents_shortcode.php');
require_once('inc/rewrite-rules.php');
/**
* Adiciona classes extras à lista de elementos que mudam de cor de acordo com a preferência do usuário
*
*/
function add_class_customize($colors) {
return ".box-noticias__titulo { color: {$colors['link_color']}; }";
}
add_filter('tainacan-customize-css-class', 'add_class_customize');