-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.php
542 lines (449 loc) · 15.5 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
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
<?php
define('THEME_VERSION', '1.1.5');
define('THEME_ICON', get_stylesheet_directory_uri() . '/images/icon.png');
class PBA_Main {
private $section_param;
public function __construct($param) {
$this->section_param = $param;
}
public static function showSection($param) {
if($param && !empty($param)) {
include_once( 'includes/sections/'. $param .'.php' );
}
}
}
/**
* Setup
*/
function plandd_setup()
{
/**
* Registrar formatos de miniaturas
*/
add_theme_support('post-thumbnails');
set_post_thumbnail_size(242, 220, true);
if (function_exists('add_image_size')) {
add_image_size('destaque.grande', 960, 395, true);
add_image_size('destaque.medio', 300, 187, true);
add_image_size('destaque.pequeno', 140, 98, true);
add_image_size('destaque.rodape', 300, 286, true);
add_image_size('destaque.editoria', 300, 286, true);
add_image_size('destaque.colunista', 80, 80, true);
}
function new_excerpt_length($length) {
return 20;
}
add_filter('excerpt_length', 'new_excerpt_length');
remove_filter('the_excerpt', 'wpautop'); // sem paragrafo no resumo
/**
* Menus
*/
register_nav_menus( array(
'primary' => __( 'Menu principal', 'plandd' )
) );
}
add_action('init','plandd_setup');
/**
* Configure funções para campos personalizados da aplicação
*/
define( 'USE_LOCAL_ACF_CONFIGURATION', true ); // apenas conf. local
add_filter('acf/settings/path', 'plandd_acf_path');
function plandd_acf_path( $path ) {
// update path
$path = get_stylesheet_directory() . '/includes/acf-pro/';
// return
return $path;
}
add_filter('acf/settings/dir', 'plandd_acf_dir');
function plandd_acf_dir( $dir ) {
// update path
$dir = get_stylesheet_directory_uri() . '/includes/acf-pro/';
// return
return $dir;
}
/**
* Framework para personalização de campos
* (custom meta post)
*/
include_once( get_stylesheet_directory() . '/includes/acf-pro/acf.php' );
define( 'ACF_LITE' , true );
//include_once( get_stylesheet_directory() . '/includes/acf/preconfig.php' );
if( function_exists('acf_add_options_page') ) {
acf_add_options_page(array(
'page_title' => 'Opções gerais',
'menu_title' => 'PB Agora',
'menu_slug' => 'opcoes-gerais',
'capability' => 'edit_posts',
'redirect' => false
));
acf_add_options_sub_page(array(
'page_title' => 'Defina categorias para seções da página principal',
'menu_title' => 'Seções',
'parent_slug' => 'opcoes-gerais',
));
acf_add_options_sub_page(array(
'page_title' => 'Código de rastramento do analytics',
'menu_title' => 'Analytics',
'parent_slug' => 'opcoes-gerais',
));
acf_add_options_sub_page(array(
'page_title' => 'Otimizar indexação da página',
'menu_title' => 'SEO',
'parent_slug' => 'opcoes-gerais',
));
acf_add_options_sub_page(array(
'page_title' => 'Adicione código extra no site',
'menu_title' => 'Extra',
'parent_slug' => 'opcoes-gerais',
));
acf_add_options_sub_page(array(
'page_title' => 'Gerencie as publicidades do portal',
'menu_title' => 'Publicidades',
'parent_slug' => 'opcoes-gerais',
));
}
/**
* Função genérica para exibir publicidades
*/
function pba_show_ads($ads_imgs, $img_key, $ads_html) {
$ads = get_field($ads_imgs, 'option');
if(is_array($ads))
shuffle($ads);
$html = get_field($ads_html, 'option');
$i = 0;
if($html == "") {
if($ads) {
foreach ($ads as $img) {
if(1 == $i) break;
echo '<img src="'. $img[$img_key] .'">';
$i++;
}
} else {
echo '';
}
}
else {
echo $html;
}
}
/**
* Definir funções ajax
* */
add_action('wp_ajax_nopriv_load_sections', 'load_sections');
add_action('wp_ajax_load_sections', 'load_sections');
function load_sections() {
PBA_Main::showSection($_GET['param']);
exit();
}
/**
* Incorporar scripts */
function plandd_scripts() {
/*
Folha de estilo base para a aplicação
*/
wp_enqueue_style('style', get_stylesheet_directory_uri() . '/style.css', array(), THEME_VERSION, "screen");
/*
modernizr
*/
wp_enqueue_script('modernizr', '//cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.min.js', array(), THEME_VERSION);
wp_enqueue_script(
'angularjs',
'https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js'
);
wp_enqueue_script(
'angularjs-route',
'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular-route.min.js',
array('angularjs')
);
/*
Jquery
*/
wp_deregister_script('jquery');
wp_register_script('jquery', '//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js', false, THEME_VERSION);
wp_enqueue_script('jquery');
/*
Scripts essenciais minificados em
um arquivo unico e essenciais para
o funcionamento do lado cliente
*/
wp_enqueue_script('scripts', get_stylesheet_directory_uri() . '/scripts.js', array(), THEME_VERSION, true);
wp_enqueue_script('angular-scripts', get_stylesheet_directory_uri() . '/app.js', array(), THEME_VERSION, true);
}
add_action( 'wp_enqueue_scripts', 'plandd_scripts' );
/**
* Taxonomia para destacar postagens
*/
function featured_post() {
register_taxonomy('destaque', 'post', array(
'hierarchical' => true,
'labels' => array(
'name' => _x( 'Destaque', '' ),
'singular_name' => _x( 'Destaque', '' ),
'search_items' => __( 'Buscar Destaque' ),
'all_items' => __( 'Todos' ),
'parent_item' => __( 'Destaque' ),
'parent_item_colon' => __( 'Destaque:' ),
'edit_item' => __( 'Editar Destaque' ),
'update_item' => __( 'Atualizar Destaque' ),
'add_new_item' => __( 'Adicionar novo Destaque' ),
'new_item_name' => __( 'Novo' ),
'menu_name' => __( 'Destaque' ),
),
'rewrite' => array(
'slug' => 'destaque',
'with_front' => false,
'hierarchical' => true
),
));
}
add_action( 'init', 'featured_post' );
/**
* Funções utilitárias */
//Nome da 1a categoria de uma postagem em um loop
function get_first_category_name() {
$category = get_the_category();
if($category[0]){
return $category[0]->cat_name;
}
}
//retorna primeira tag
function get_first_tag() {
$posttags = get_the_tags();
$count=0;
if ($posttags) {
foreach($posttags as $tag) {
$count++;
if (1 == $count) {
return $tag->name . ' ';
}
}
} else {
get_first_category_name();
}
}
//mais lidas
function wpb_set_post_views($postID) {
$count_key = 'wpb_post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
function wpb_track_post_views ($post_id) {
if ( !is_single() ) return;
if ( empty ( $post_id) ) {
global $post;
$post_id = $post->ID;
}
wpb_set_post_views($post_id);
}
add_action( 'wp_head', 'wpb_track_post_views');
function wpb_get_post_views($postID){
$count_key = 'wpb_post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0 View";
}
return $count.' Views';
}
//Colunistas
include_once( get_stylesheet_directory() . '/includes/post-types/colunistas.php' );
//Ofertas
include_once( get_stylesheet_directory() . '/includes/post-types/ofertas.php' );
/*
Icones para post-types
(http://melchoyce.github.io/dashicons/)
edit.php?post_type=acf
*/
function add_menu_icons_styles(){
?>
<style>
#menu-posts-coluna div.wp-menu-image:before {
content: "\f122";
}
#menu-posts-oferta div.wp-menu-image:before {
content: "\f488";
}
</style>
<?php
}
add_action( 'admin_head', 'add_menu_icons_styles' );
//SEO
//Pegar descrição de página ativa
function getPageDescription() {
$desc = get_field('pab_desc', 'option');
if(is_home())
return $desc;
elseif(is_page() || is_single())
return the_excerpt();
elseif(is_category())
return get_query_var('cat');
else
return get_bloginfo( 'description' );
}
function getThumbUrl($size) {
global $post;
if(!$size || $size == null) {
$size = 'full';
}
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), $size );
echo $thumb[0];
}
function fb_opengraph() {
global $post;
$thumb = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'destaque.pequeno');
$th = (!empty($thumb[0])) ? $thumb[0] : '';
$site_description = get_bloginfo( 'description' );
$stylesheet_url = get_bloginfo( 'stylesheet_url' );
$stylesheet_directory = get_bloginfo( 'stylesheet_directory' );
$site_name = (is_home()) ? get_bloginfo( 'name' ) : get_the_title( $post->ID );
$site_url = (is_home()) ? get_bloginfo( 'siteurl' ) : get_permalink( $post->ID );
$keywords = get_field('pba_keywords', 'option');
?>
<meta name="description" content="<?php echo getPageDescription(); ?>">
<meta name="keywords" content="<?php echo $keywords; ?>">
<meta name="author" content="http://plandc.com.br">
<meta name="subject" content="Portal de notícias da Paraíba">
<title><?php bloginfo('name'); ?> | <?php is_home() ? bloginfo('description') : wp_title(''); ?></title>
<link rel="shortcut icon" href="<?php echo $stylesheet_directory; ?>/favicon.ico" type="image/vnd.microsoft.icon"/>
<link rel="icon" href="<?php echo $stylesheet_directory; ?>/favicon.ico" type="image/x-ico"/>
<link rel="stylesheet" href="<?php echo $stylesheet_url; ?>" />
<!-- Schema.org markup for Google+ -->
<meta itemprop="name" content="<?php echo $site_name; ?>">
<meta itemprop="description" content="<?php echo getPageDescription(); ?>">
<?php if(is_home()): ?>
<meta itemprop="image" content="<?php echo $stylesheet_directory; ?>/screenshot.png">
<?php else: ?><meta itemprop="image" content="<?php getThumbUrl('full'); ?>"><?php endif; ?>
<!-- Twitter Card data -->
<?php if(is_home()): ?>
<meta name="twitter:card" content="<?php echo $stylesheet_directory; ?>/screenshot.png">
<?php else: ?><meta name="twitter:card" content="<?php getThumbUrl('full'); ?>"><?php endif; ?>
<meta name="twitter:site" content="@pbagora">
<meta name="twitter:title" content="<?php echo $site_name; ?>">
<meta name="twitter:description" content="<?php echo getPageDescription(); ?>">
<meta name="twitter:creator" content="@plandc">
<!-- Twitter summary card with large image must be at least 280x150px -->
<?php if(is_home()): ?>
<meta name="twitter:image:src" content="<?php echo $stylesheet_directory; ?>/screenshot.png">
<?php else: ?><meta name="twitter:image:src" content="<?php getThumbUrl('full'); ?>"><?php endif; ?>
<!-- Open Graph data -->
<meta property="og:title" content="<?php echo $site_name; ?>" />
<meta property="og:type" content="article" />
<meta property="og:url" content="<?php echo $site_url; ?>" />
<?php if(is_home()): ?>
<meta property="og:image" content="<?php echo $stylesheet_directory; ?>/screenshot.png" />
<?php else: ?><meta property="og:image" content="<?php getThumbUrl('full'); ?>" /><?php endif; ?>
<meta property="og:description" content="<?php echo getPageDescription(); ?>" />
<meta property="og:site_name" content="<?php echo $site_name; ?>" />
<?php if(!is_home()): ?>
<meta property="article:section" content="Artigo" />
<meta property="article:tag" content="<?php echo $keywords; ?>" />
<?php endif; ?>
<?php
echo get_field('pba_analytics', 'option');
}
add_action('wp_head', 'fb_opengraph', 5);
/**
* Busca via ajax
*/
add_action('wp_ajax_nopriv_pba_search_form', 'pba_search_form');
add_action('wp_ajax_pba_search_form', 'pba_search_form');
function pba_search_form() {
$query = $_GET['keyword'];
$term = $query['term'];
$the_query = new WP_Query( array( 's' => $term, 'post_type' => 'post', 'posts_per_page' => 12, 'orderby' => 'date' ) );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
global $post;
$thumb = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'destaque.editoria');
$th = (!empty($thumb[0])) ? $thumb[0] : '';
?>
<figure class="small-12 left">
<figcaption class="small-12 left">
<time class="font-small divide-5" pubdate><?php the_time('j \d\e F, Y'); ?> às <?php the_time('g:i a'); ?></time>
<h6 class="post-tag divide-5"><?php echo get_first_tag(); ?></h6>
<h5><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h5>
</figcaption>
</figure>
<?php
endwhile; wp_reset_postdata(); endif;
exit();
}
//galeria do post
add_filter('post_gallery', 'my_post_gallery', 10, 2);
function my_post_gallery($output, $attr) {
global $post;
if (isset($attr['orderby'])) {
$attr['orderby'] = sanitize_sql_orderby($attr['orderby']);
if (!$attr['orderby'])
unset($attr['orderby']);
}
extract(shortcode_atts(array(
'order' => 'ASC',
'orderby' => 'menu_order ID',
'id' => $post->ID,
'itemtag' => 'dl',
'icontag' => 'dt',
'captiontag' => 'dd',
'columns' => 3,
'size' => 'thumbnail',
'include' => '',
'exclude' => ''
), $attr));
$id = intval($id);
if ('RAND' == $order) $orderby = 'none';
if (!empty($include)) {
$include = preg_replace('/[^0-9,]+/', '', $include);
$_attachments = get_posts(array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));
$attachments = array();
foreach ($_attachments as $key => $val) {
$attachments[$val->ID] = $_attachments[$key];
}
}
if (empty($attachments)) return '';
// Here's your actual output, you may customize it to your need
ob_start();
?>
<nav id="post-gallery" class="small-12 left rel cycle-slideshow"
data-cycle-fx="scrollHorz"
data-cycle-timeout="0"
data-cycle-next=".next-pic"
data-cycle-prev=".prev-pic"
data-cycle-pager="#adv-custom-pager"
data-cycle-slides="> figure"
data-cycle-swipe="true"
data-cycle-swipe-fx="scrollHorz"
>
<?php
foreach ($attachments as $id => $attachment):
$thumb = wp_get_attachment_image_src($id, 'thumbnail');
$full = wp_get_attachment_image_src($id, 'full');
?>
<figure class="small-12 full-height left d-iblock" data-thumb="<?php echo $full[0]; ?>" data-cycle-pager-template="<a href=#><img width=50 height=50 src=<?php echo $thumb[0]; ?>></a>"></figure>
<?php
endforeach;
?>
<a href="#" title="Próximo" class="d-table abs white nav-gallery next-pic">
<span class="d-table-cell text-center">
<span class="icon-keyboard_arrow_left"></span>
</span>
</a>
<a href="#" title="Próximo" class="d-table abs white nav-gallery prev-pic">
<span class="d-table-cell text-center">
<span class="icon-navigate_next"></span>
</span>
</a>
</nav>
<div id="adv-custom-pager"></div>
<?php
$output = ob_get_contents();
ob_end_clean();
return $output;
}