-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
648 lines (555 loc) · 19.1 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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
<?php
// site names should just be state names
DEFINE( 'SITE_NAME_PREFIX', 'StateImpact ' );
DEFINE( 'SW_ROOT', dirname(__FILE__));
DEFINE( 'INCLUDES', SW_ROOT . '/includes/' );
// for template checking in loop
// since constants can't be arrays, this is a space-separated list
DEFINE( 'RICH_CONTENT_TYPES', 'fusiontablesmap');
DEFINE( 'SINGLE_FULL_WIDTH', 'single-full-width.php' );
// includes
require_once( INCLUDES . 'users.php' );
require_once( INCLUDES . 'sidebars.php' );
require_once( INCLUDES . 'stations.php' );
require_once( INCLUDES . 'static-pages.php' );
require_once( INCLUDES . 'topics/topics.php' );
require_once( INCLUDES . 'sw-widgets.php');
require_once( INCLUDES . 'settings/statewatch.php' );
require_once( INCLUDES . 'settings/argo.php' );
require_once( INCLUDES . 'taxonomy.php' );
require_once( INCLUDES . 'template.php' );
require_once( INCLUDES . 'media.php' );
require_once( INCLUDES . 'nav.php');
require_once( INCLUDES . 'admin.php' );
require_once( INCLUDES . 'multimedia.php' );
require_once( INCLUDES . 'featured-posts.php' );
require_once( INCLUDES . 'editor.php' );
require_once( INCLUDES . 'feedburner.php' );
require_once( INCLUDES . 'ads.php' );
// add editor styles
add_editor_style();
function sw_loop_post_types() {
return array('post', 'fusiontablesmap', 'argolinksroundup');
}
function sw_headline_link() {
global $post;
if (get_post_type($post) == 'jiffypost'){
$sw_post_link = get_post_meta($post->ID, '_navis_embed_url', true);
} else {
$sw_post_link = the_permalink();
}
echo $sw_post_link;
}
add_filter('pre_get_posts', 'sw_filter_search');
function sw_filter_search($query) {
if (!is_admin()) {
if ($query->is_search && !$query->get('suppress_filters')) {
$query->set('post_type', array('post', 'topic', 'fusiontablesmap', 'argolinksroundup'));
// $query->set('orderby', 'modified');
};
}
return $query;
};
add_action( 'admin_init', 'sw_agg_settings' );
function sw_agg_settings() {
add_settings_field( 'network_feed_url', 'RSS Feed for Network Widget',
'sw_network_feed_url_callback', 'agg', 'agg_settings' );
register_setting( 'agg', 'network_feed_url' );
}
function sw_network_feed_url_callback() {
$option = get_option( 'network_feed_url' );
echo "<input type='text' value='$option' name='network_feed_url' />";
}
function sw_the_email_link() {
if ( get_option( 'show_email_link' ) ) {
echo '<li class="meta-email"><a href="mailto:?subject=';
the_title();
echo '&body=';
the_permalink();
echo '">Email</a></li>';
}
}
add_filter('autocreate_taxonomies', 'sw_autocreate_taxonomies');
function sw_autocreate_taxonomies($taxonomies) {
return array(
'National' => 'prominence',
'Slideshow' => 'feature'
);
}
//add_filter('autocreate_menus', 'sw_autocreate_menus');
function sw_autocreate_menus($menus) {
return array(
'categories' => 'Categories List',
);
}
add_action( 'init', 'remove_argo_actions' );
function remove_argo_actions() {
remove_action( 'navis_top_strip', 'argo_network_div' );
remove_action( 'navis_network_icon', 'argo_network_icon' );
remove_action( 'wp_footer', 'argo_build_network_panel' );
}
add_action( 'widgets_init', 'unload_argo_widgets', 15 );
function unload_argo_widgets() {
unregister_widget( 'Bio_Widget' );
unregister_widget( 'Feedback_Widget' );
unregister_widget( 'Network_Widget' );
unregister_widget( 'Related_Widget' );
unregister_widget( 'Support_Widget' );
}
add_filter( 'bloginfo_rss', 'sw_fix_feed_title', 10, 2);
function sw_fix_feed_title($info, $show) {
if ($show != 'name') {
// in this case, we only care about the 'name' option
return $info;
} else {
return SITE_NAME_PREFIX . $info;
}
}
add_filter( 'post_class', 'sw_add_feature_labels', 10, 3);
function sw_add_feature_labels($classes, $class, $post_id) {
global $wp_query;
if ($wp_query->current_post == 0) $classes[] = 'first';
if ($wp_query->current_post == ($wp_query->post_count - 1)) $classes[] = 'last';
if(navis_post_has_features($post_id)) {
$classes[] = 'has-features';
}
if (sw_is_rich_media()) $classes[] = 'rich-media';
return $classes;
}
// for some reason this only works here,
// but should be moved into the SW_Fancybox class at some point
add_filter('image_send_to_editor',
'sw_fancybox_image_send_to_editor', 10, 8);
function sw_fancybox_image_send_to_editor($html, $id, $caption, $title, $align, $url, $size, $alt) {
// we're only interested in images wrapped in links to uploaded images
if ( $url ) {
$uploads_dir = wp_upload_dir();
$uploads_dir = $uploads_dir['baseurl'];
if ( strpos($url, $uploads_dir) === 0 ) {
// it's an uploaded file, so fancybox it
$html = get_image_tag($id, $alt, $title, $align, $size);
$rel = $rel ? ' rel="post-' . esc_attr($id).'"' : '';
$url = esc_attr($url);
$caption = esc_attr($caption);
$html = "<a class='fancybox' href='{$url}' rel='$rel' title='{$caption}'>$html</a>";
}
}
return $html;
}
add_action('rss2_head', 'sw_feed_noindex');
add_action('rss_head', 'sw_feed_noindex');
add_action('atom_head', 'sw_feed_noindex');
function sw_feed_noindex() {
echo "<robots>noindex, follow</robots>";
}
add_action('admin_print_scripts-post.php', 'sw_add_wordcount_js');
add_action('admin_print_scripts-post-new.php', 'sw_add_wordcount_js');
function sw_add_wordcount_js() {
$js = get_bloginfo('stylesheet_directory') . "/js/wordcount.js";
wp_enqueue_script('wordcount', $js, array('jquery'), '0.1');
}
add_filter('fustiontablesmap_taxonomies', 'sw_add_map_taxonomies', 10, 1);
function sw_add_map_taxonomies($taxonomies) {
array_push($taxonomies, 'feature', 'prominence');
return $taxonomies;
}
add_filter('navis_related_content_post_types', 'sw_related_content_types');
function sw_related_content_types($post_types) {
return array('post', 'topic', 'fusiontablesmap');
}
add_action('wp_enqueue_scripts', 'sw_social');
function sw_social() {
$twitter = '//platform.twitter.com/widgets.js';
$fb = 'http://static.ak.fbcdn.net/connect.php/js/FB.Share';
//wp_enqueue_script('twitter-widgets', $twitter);
//wp_enqueue_script('facebook', $fb);
}
add_action('wp_enqueue_scripts', 'sw_analytics');
function sw_analytics() {
$src = get_bloginfo('stylesheet_directory') . '/js/analytics.js';
wp_enqueue_script('sw-analytics', $src, array(), '0.1', false);
}
//add_action('wp_head', 'sw_state');
function sw_state() { ?>
<script>window.SI_STATE_NAME = "<?php bloginfo('name'); ?>"</script>
<?php
}
add_filter('argolinkroundups_taxonomies', 'sw_linkroundups_taxonomies');
function sw_linkroundups_taxonomies($taxonomies) {
return array();
}
// argo-foundation functions.php
if ( ! function_exists( 'argo_the_page_number' ) ) :
/**
* Prints the page number currently being browsed, with a vertical bar before
* it.
*/
function argo_the_page_number() {
global $paged; // Contains page number.
if ( $paged >= 2 )
echo ' | ' . 'Page ' . $paged;
}
endif;
function argo_get_related_topics_for_category( $obj ) {
$MAX_RELATED_TOPICS = 5;
$cat_id = $obj->cat_ID;
if ( $obj->post_type ) {
if ( $obj->post_type == 'nav_menu_item' ) {
$cat_id = $obj->object_id;
}
}
$out = "<ul>";
// spit out the subcategories
$cats = _subcategories_for_category( $cat_id );
foreach ( $cats as $c ) {
$out .= sprintf( '<li><a href="%s">%s</a></li>',
get_category_link( $c->term_id ), $c->name
);
}
if ( count( $cats ) < $MAX_RELATED_TOPICS ) {
$tags = _tags_associated_with_category( $cat_id,
$MAX_RELATED_TOPICS - count( $cats ) );
foreach ( $tags as $t ) {
$out .= sprintf( '<li><a href="%s">%s</a></li>',
get_tag_link( $t->term_id ), $t->name
);
}
}
$out .= "</ul>";
return $out;
}
function _tags_associated_with_category( $cat_id, $max = 5 ) {
$query = new WP_Query( array(
'posts_per_page' => -1,
'cat' => $cat_id,
) );
// Get a list of the tags used in posts in this category.
$tags = array();
$tag_objs = array();
foreach ( $query->posts as $post ) {
$ptags = get_the_tags( $post->ID );
if ( $ptags ) {
foreach ( $ptags as $tag ) {
if (isset($tags[$tag->term_id])) {
$tags[ $tag->term_id ]++;
} else {
$tags[$tag->term_id] = 0;
}
$tag_objs[ $tag->term_id ] = $tag;
}
}
}
// Sort the most popular and get the $max results, or all results
// if max is -1
arsort( $tags, SORT_NUMERIC );
if ( $max == -1 ) {
$tag_keys = array_keys( $tags );
}
else {
$tag_keys = array_splice( array_keys( $tags ), 0, $max );
}
// Create an array of the selected tag objects
$return_tags = array();
foreach ( $tag_keys as $tk ) {
array_push( $return_tags, $tag_objs[ $tk ] );
}
return $return_tags;
}
function _subcategories_for_category( $cat_id ) {
// XXX: could also use get_term_children(). not sure which is better.
$cats = get_categories( array(
'child_of' => $cat_id,
) );
return $cats;
}
/**
* Builds links to the latest posts for a given category.
*
* @todo 3.1 replace caller_get_posts with ignore_sticky_posts
*
* @param object $cat Term object
* @return string
*/
function argo_get_latest_posts_for_category( $cat ) {
$query = new WP_Query( array(
'showposts' => 4,
'orderby' => 'date',
'order' => 'DESC',
'cat' => $cat->object_id,
'caller_get_posts' => 1,
) );
$output = '';
foreach ( $query->posts as $post ) {
$output .= '<h4><a href="' . get_permalink( $post->ID ) . '">' . $post->post_title . '</a></h4>';
}
return $output;
}
/**
* Same as argo_get_post_thumbnail_src(), but for use inside The Loop.
*/
if (!function_exists('argo_get_the_post_thumbnail_src')):
function argo_get_the_post_thumbnail_src( $size = 'npr_thumb' ) {
global $post;
return argo_get_post_thumbnail_src( $post, $size );
}
endif;
/*
* XXX: this may not be necessary the_post_thumbnail takes sizes. -- ML
*/
function argo_get_post_thumbnail_src( $post, $size = 'npr_thumb' ) {
if ( has_post_thumbnail( $post->ID ) ) {
$thumb = get_post_thumbnail_id( $post->ID );
$image = wp_get_attachment_image_src( $thumb, $size );
return $image[ 0 ]; // src
}
}
/* Tracking posts tagged Featured or National in the prominence taxonomy with Google Analytics */
function argo_prominence_tracker() {
global $post;
if (is_object_in_term($post->ID,'prominence',array('featured','national')) ) {
echo "<script>_gaq.push(['_setCustomVar',1,'prominence','Featured and/or National',3]);</script>";
}
}
if ( ! function_exists( 'argo_posted_on' ) ) :
/**
* Prints HTML with meta information for the current post date/time
*/
function argo_posted_on() {
printf( '<span class="entry-date">%1$s | %2$s</span>',
// XXX: set this as the default time format instead of overriding here
get_the_date(), esc_attr( get_the_time( 'g:i A' ) )
);
}
endif;
// XXX: better way to do this
function _filter_post_types( $t ) {
$filter_out = array( 'attachment', 'page', 'topic' );
return ! in_array( $t, $filter_out );
}
function _map_post_types( $t ) {
return 'post_type[]=' . $t;
}
function argo_post_types_qs() {
global $query_string;
$all_post_types = get_post_types( array( 'public' => true ) );
// XXX: there should be a better way to include our post types
$post_types = array_filter( $all_post_types, '_filter_post_types' );
$post_types = array_map( '_map_post_types', $post_types );
array_unshift( $post_types, $query_string );
$qs = join( '&', $post_types );
return $qs;
}
/**
* DEPRECATED
* ----------
* A pagination function
* @param integer $range: The range of the slider, works best with even numbers
* Used WP functions:
* get_pagenum_link($i) - creates the link, e.g. http://site.com/page/4
* previous_posts_link(' < '); - returns the Previous page link
* next_posts_link(' > '); - returns the Next page link
*/
function argo_pagination($range = 4){
// $paged - number of the current page
global $paged, $wp_query;
// How much pages do we have?
if ( !$max_page ) {
$max_page = $wp_query->max_num_pages;
}
// We need the pagination only if there are more than 1 page
if($max_page > 1){
if(!$paged){
$paged = 1;
}
// To the previous page
previous_posts_link(' ← Newer Posts');
// We need the sliding effect only if there are more pages than is the sliding range
if($max_page > $range){
// When closer to the beginning
if($paged < $range){
for($i = 1; $i <= ($range + 1); $i++){
$current = $i === $paged ? "current" : "";
$link = get_pagenum_link($i);
echo "<a class=\"page-numbers $current\" href=\"$link\">$i</a>";
//echo '<a class=\"page-numbers\" href=\"' . get_pagenum_link($i);
//if($i==$paged) echo "class='current'";
//echo ">$i</a>";
}
}
// When closer to the end
elseif($paged >= ($max_page - ceil(($range/2)))){
for($i = $max_page - $range; $i <= $max_page; $i++){
echo "<a href='" . get_pagenum_link($i) ."'";
if($i==$paged) echo "class='current'";
echo ">$i</a>";
}
}
// Somewhere in the middle
elseif($paged >= $range && $paged < ($max_page - ceil(($range/2)))){
for($i = ($paged - ceil($range/2)); $i <= ($paged + ceil(($range/2))); $i++){
echo "<a href='" . get_pagenum_link($i) ."'";
if($i==$paged) echo "class='current'";
echo ">$i</a>";
}
}
}
// Less pages than the range, no sliding effect needed
else{
for($i = 1; $i <= $max_page; $i++){
echo "<a href='" . get_pagenum_link($i) ."'";
if($i==$paged) echo "class='current'";
echo ">$i</a>";
}
}
// Next page
next_posts_link(' Older Posts → ');
// On the last page, don't put the Last page link
}
}
/**
* argo_get_twitter_screen_name(): provide the actual screen name or the blog's
* name.
*/
function argo_get_twitter_screen_name() {
// XXX: change twitter_link property to only contain screename.
$turl = get_option( 'twitter_link' );
if ( $turl ) {
$screen_name = preg_replace( '/https?:\/\/twitter.com\/\#?\!?\/?/', '', $turl );
return '@' . $screen_name;
}
else {
return get_bloginfo( 'name' );
}
}
function argo_get_sanitized_title( $max_title_length = 140 ) {
$title = html_entity_decode(get_the_title(), ENT_COMPAT, 'UTF-8');
if ( strlen( $title ) > $max_title_length ) {
$title = substr( $title, 0, $max_title_length - 3 ) . '...';
}
return rawurlencode( $title );
}
function argo_get_twitter_title() {
$screen_name = argo_get_twitter_screen_name();
// 22 = length of short url
// 7 = tweet prefix and suffix
$max_title_length = 140 - 22 - 7 - strlen( $screen_name );
$title = argo_get_sanitized_title( $max_title_length );
$tweet = sprintf( 'Via %s: %s |', $screen_name, $title );
return $tweet;
}
/**
* Determines whether a post has a "main" item from the feature custom
* taxonomy. Expects to be called from within The Loop.
*
* @uses global $post
* @return bool
*/
function navis_post_has_features() {
global $post;
$features = get_the_terms( $post->ID, 'feature' );
return ( $features ) ? true : false;
}
/**
* Echoes a post's manual excerpt so long as it's less than the given word
* limit.
*
* @param $word_limit maximum number of words to allow.
* @uses global $post
*/
function navis_the_raw_excerpt( $word_limit = 35 ) {
global $post;
$raw_excerpt = $post->post_excerpt;
if ( ! $raw_excerpt )
return;
if ( count( navis_split_words( $raw_excerpt ) ) > $word_limit )
return;
echo $post->post_excerpt;
}
function navis_split_words( $text, $split_limit = -1 ) {
// XXX: deal with the way argo_get_excerpt uses this limit to
// determine whether to cut off remaining text.
if ( $split_limit > -1 )
$split_limit += 1;
$words = preg_split( "/[\n\r\t ]+/", $text, $split_limit,
PREG_SPLIT_NO_EMPTY );
return $words;
}
function argo_term_to_label( $term ) {
return sprintf( '<li> <a href="%1$s">%2$s</a></li>',
get_term_link( $term, $term->taxonomy ),
strtoupper( $term->name ) );
}
if ( ! function_exists( 'argo_the_post_labels' ) ) :
function argo_the_post_labels( $post_id ) {
$post_terms = argo_custom_taxonomy_terms( $post_id );
foreach ( $post_terms as $term ) {
echo argo_term_to_label( $term );
}
}
endif;
if ( ! function_exists( 'argo_custom_taxonomy_terms' ) ) :
function argo_custom_taxonomy_terms( $post_id ) {
// XXX: need better way of introspecting custom taxonomies.
global $CUSTOM_TAXONOMIES;
$post_terms = array();
foreach ( (array)$CUSTOM_TAXONOMIES as $tax ) {
if ( taxonomy_exists( $tax ) ) {
$terms = get_the_terms( $post_id, $tax );
if ( $terms ) {
$post_terms = array_merge( $post_terms, $terms );
}
}
}
return $post_terms;
}
endif;
/**
* Builds the proper searchform action URL. This works around a condition with
* WordPress MultiSite that requires this URL to have a trailing slash.
*
*/
function navis_the_searchform_url() {
$url = get_bloginfo( 'url', 'display' );
echo ( $url ) ? trailingslashit( $url ) : $url;
}
/**
* Constructs the appropriate URL for theme-based script files.
*
* @param $filename relative filename of the file being requested
* @return url|null
*/
function navis_get_theme_script_url( $filename ) {
return navis_get_theme_resource_url( 'js', $filename );
}
/**
* Constructs the appropriate URL for theme-based style files.
*
* @param $filename relative filename of the file being requested
* @return url|null
*/
function navis_get_theme_style_url( $filename ) {
return navis_get_theme_resource_url( 'css', $filename );
}
/**
* Constructs the appropriate URL for theme-based resources.
*
* @todo make this look in the child theme as well, like get_template_part()
* @param $type the type of resource to find: js, css, etc.
* @param $filename relative filename of the file being requested
* @return url|null
*/
function navis_get_theme_resource_url( $type, $filename ) {
return trailingslashit( get_bloginfo( 'template_url' ) ) .
trailingslashit( $type ) . $filename;
}
function sw_modify_retrieve_password_messsage($message, $key){
// Replace first open bracket
$message = str_replace('<', '', $message);
// Replace second open bracket
$message = str_replace('>', '', $message);
// Convert line returns to <br>'s
$message = str_replace("\r\n", '<br>', $message);
return $message;
}
add_filter('retrieve_password_message', 'sw_modify_retrieve_password_messsage', 10, 2);
?>