-
Notifications
You must be signed in to change notification settings - Fork 4
/
functions.php
698 lines (524 loc) · 21.8 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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
<?php
include('lib/rkv-admin.php');
include('lib/rkv-structure.php');
include('lib/rkv-builds.php');
include('lib/rkv-api-calls.php');
include('lib/rkv-ajax.php');
include('lib/rkv-types.php');
include('lib/rkv-meta.php');
include('lib/rkv-widgets.php');
include('lib/rkv-shortcodes.php');
// Start up the engine
class NorcrossVersionFour {
/**
* This is our constructor. There are many like it, but this one is mine.
*
* @return Norcrossv4
*/
public function __construct() {
add_action ( 'after_setup_theme', array( $this, 'after_setup_theme' ) );
add_action ( 'wp_enqueue_scripts', array( $this, 'scripts_styles' ), 10 );
add_action ( 'pre_get_posts', array( $this, 'ordered_sort' ) );
add_action ( 'pre_get_posts', array( $this, 'rss_include' ) );
add_action ( 'wp_footer', array( $this, 'social_scripts' ) );
add_action ( 'wp_footer', array( $this, 'piwik_script' ) );
add_action ( 'template_redirect', array( $this, 'redirects' ), 1 );
add_action ( 'gists_cron', array( $this, 'run_gists_cron' ) );
add_action ( 'insta_cron', array( $this, 'run_insta_cron' ) );
add_filter ( 'the_content', array( $this, 'snippet_display' ), 25 );
add_filter ( 'wp_nav_menu_items', array( $this, 'nav_search' ), 10, 2 );
add_filter ( 'post_thumbnail_html', array( $this, 'fix_thumbs' ), 10 );
add_filter ( 'image_send_to_editor', array( $this, 'fix_thumbs' ), 10 );
add_filter ( 'nav_menu_css_class', array( $this, 'nav_active' ), 10, 2 );
add_filter ( 'wp_editor_set_quality', array( $this, 'image_quality' ) );
add_filter ( 'get_avatar', array( $this, 'burt_avatar' ), 10, 5 );
add_filter ( 'body_class', array( $this, 'body_class' ) );
}
/**
* Set up various theme items
*
* @return Norcrossv4
*/
public function after_setup_theme() {
// add main menu
register_nav_menu( 'primary', 'Primary Menu' );
// add sidebars
register_sidebar(array(
'name' => __( 'Blog Sidebar' ),
'id' => 'blog-sidebar',
'description' => __( 'Widgets for blog posts.' ),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h4 class="nav-header">',
'after_title' => '</h4>'
));
register_sidebar(array(
'name' => __( 'Tutorials Sidebar' ),
'id' => 'tutorials-sidebar',
'description' => __( 'Widgets for tutorials pages.' ),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h4 class="nav-header">',
'after_title' => '</h4>'
));
register_sidebar(array(
'name' => __( 'Main Sidebar' ),
'id' => 'main-sidebar',
'description' => __( 'Widgets for general use.' ),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h4 class="nav-header">',
'after_title' => '</h4>'
));
// set up thumbnails
add_theme_support ( 'post-thumbnails' );
add_image_size ( 'blog-page', 130, 130, true );
add_image_size ( 'speaking', 250, 250, true );
add_image_size ( 'ig-thumb', 150, 150, true );
add_image_size ( 'ig-regular', 306, 306, true );
add_image_size ( 'ig-large', 612, 612, true );
remove_action ( 'wp_head', 'feed_links_extra', 3 );
remove_action ( 'wp_head', 'feed_links', 2 );
// add cron job for gists
if ( !wp_next_scheduled( 'gists_cron' ) )
wp_schedule_event(time(), 'twicedaily', 'gists_cron');
if ( !wp_next_scheduled( 'insta_cron' ) )
wp_schedule_event(time(), 'twicedaily', 'insta_cron');
}
/**
* Add search to nav bar
*
* @return Norcrossv4
*/
public function nav_search($items, $args) {
ob_start();
get_search_form();
$searchform = ob_get_contents();
ob_end_clean();
$items .= '<li id="menu-item-0" class="menu-item search-nav">' . $searchform . '</li>';
return $items;
}
/**
* Set some custom classes
*
* @return Norcrossv4
*/
public function body_class($classes) {
if (is_post_type_archive('photos') ):
$classes[] = 'instagram';
endif;
return $classes;
}
/**
* Remove height/width attributes on images so they can be responsive
*
* @return Norcrossv4
*/
public function fix_thumbs( $html ) {
$html = preg_replace( '/(width|height)=\"\d*\"\s/', "", $html );
return $html;
}
/**
* Add 'active' class to match up with bootstrap
*
* @return Norcrossv4
*/
public function nav_active($classes, $item) {
if($item->menu_item_parent == 0 && in_array('current-menu-item', $classes)) {
$classes[] = "active";
}
return $classes;
}
/**
* set image quality to 100%
*
* @return Norcrossv4
*/
public function image_quality($quality) {
return 100;
}
/**
* Load CSS and JS files
*
* @return Norcrossv4
*/
public function scripts_styles() {
// CSS first
wp_enqueue_style( 'main', get_bloginfo('stylesheet_directory').'/main.css', array(), null, 'all' );
// wp_enqueue_style( 'bootstrap-custom', get_bloginfo('stylesheet_directory').'/lib/css/bootstrap.custom.min.css', array(), null, 'all' );
wp_enqueue_style( 'bootstrap-custom', get_bloginfo('stylesheet_directory').'/lib/css/bootstrap.custom.css', array(), null, 'all' );
wp_enqueue_style( 'bootstrap-core', get_bloginfo('stylesheet_directory').'/lib/css/bootstrap.responsive.min.css', array(), null, 'all' );
if (is_singular('plugins') || is_post_type_archive('photos') ) :
wp_enqueue_style( 'colorbox', get_bloginfo('stylesheet_directory').'/lib/css/colorbox.css', array(), null, 'all' );
wp_enqueue_script( 'colorbox', get_bloginfo('stylesheet_directory').'/lib/js/jquery.colorbox.js', array('jquery'), null, true );
endif;
if (is_singular(array( 'post', 'tutorials' ) ) ) :
wp_enqueue_script( 'expander', get_bloginfo('stylesheet_directory').'/lib/js/expander.js', array('jquery'), null, true );
wp_enqueue_script( 'comment-reply');
endif;
// now scripts
wp_enqueue_script( 'bootstrap', get_bloginfo('stylesheet_directory').'/lib/js/bootstrap.min.js', array('jquery'), null, true );
wp_enqueue_script( 'rkv-init', get_bloginfo('stylesheet_directory').'/lib/js/rkv.init.js', array('jquery'), null, true );
}
/**
* sort and counts on plugin archive page
*
* @return Norcrossv4
*/
public function ordered_sort($query) {
if (is_admin())
return;
if ( is_post_type_archive('plugins') && $query->is_main_query() ) {
$query->query_vars['order'] = 'ASC';
$query->query_vars['orderby'] = 'menu_order';
$query->query_vars['posts_per_page'] = -1;
return;
}
if ( is_post_type_archive('snippets') && $query->is_main_query() ) {
$query->query_vars['posts_per_page'] = 18;
return;
}
if ( is_post_type_archive('speaking') && $query->is_main_query() ) {
$query->query_vars['order'] = 'ASC';
$query->query_vars['orderby'] = 'menu_order';
$query->query_vars['posts_per_page'] = 4;
return;
}
}
/**
* add tutorials to RSS
*
* @return Norcrossv4
*/
public function rss_include( $query ) {
if (!$query->is_feed)
return $query;
$query->set( 'post_type' , array( 'post', 'tutorials' ) );
return $query;
}
/**
* filter in GitHub Gist after post content
*
* @return Norcrossv4
*/
function snippet_display( $content ) {
// bail on non-snippets
if ( !is_singular('snippets') )
return $content;
global $post;
$gist_id = get_post_meta($post->ID, '_rkv_gist_id', true);
// bail if we don't have a Gist ID
if(empty($gist_id))
return $content;
$gist = '<div class="github-gist-block">';
$gist .= '<script src="https://gist.github.com/norcross/'.$gist_id.'.js"></script>';
$gist .= '</div>';
// Returns the content with the gist.
return $content.$gist;
}
/**
* load Piwik analytics
*
* @return Norcrossv4
*/
public function piwik_script() {
if (is_user_logged_in())
return;
?>
<!-- Piwik -->
<script type="text/javascript">
var pkBaseURL = (("https:" == document.location.protocol) ? "https://stats.andrewnorcross.com/" : "http://stats.andrewnorcross.com/");
document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 1);
<?php if (is_404() ) : ?>
piwikTracker.setDocumentTitle('404/URL = ' + String(document.location.pathname + document.location.search).replace(/\//g,'%2f') + '/From = ' + String(document.referrer).replace(/\//g,'%2f'));
<?php endif; ?>
piwikTracker.trackPageView();
piwikTracker.enableLinkTracking();
} catch( err ) {}
</script>
<noscript><p><img src="http://stats.andrewnorcross.com/piwik.php?idsite=1" style="border:0" alt="" /></p></noscript>
<!-- End Piwik Tracking Code -->
<?php
}
/**
* load social sharing scripts
*
* @return Norcrossv4
*/
public function social_scripts() {
?>
<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
<script type="text/javascript">
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
</script>
<?php
}
/**
* set missing gravatars to Burt Reynolds
*
* @return Norcrossv4
*/
public function validate_gravatar($email) {
// Build the Gravatar URL by hasing the email address
$url = 'http://www.gravatar.com/avatar/' . md5( strtolower( trim ( $email ) ) ) . '?d=404';
// Now check the headers...
$headers = @get_headers( $url );
// If 200 is found, the user has a Gravatar; otherwise, they don't.
return preg_match( '|200|', $headers[0] ) ? true : false;
}
public function gimme_burt() {
$burt_array = scandir( get_stylesheet_directory(__FILE__) . '/images/burt' );
$burt_faces = array_filter( $burt_array, create_function('$x','return in_array(strtolower(substr($x,-3)),array("png","jpg","gif"));'));
$burt_face = array_rand(array_flip($burt_faces), 1);
return $burt_face;
}
public function burt_avatar($avatar, $id_or_email, $size, $default, $alt) {
if (is_admin() )
return $avatar;
if (isset($id_or_email) && $id_or_email === 1) {
$u_id = get_current_user_id();
$email = get_the_author_meta( 'user_email', $u_id );
$grav_img = 'http://www.gravatar.com/avatar/'.md5(strtolower($email)) . '?d=' . urlencode($default) . '&s=' . $size;
$gravatar = '<img class="avatar" src="'.$grav_img.'"/>';
return $gravatar;
} else {
$email = $id_or_email->comment_author_email;
$exist = $this->validate_gravatar($email);
}
if($exist == true ) {
$grav_img = 'http://www.gravatar.com/avatar/'.md5(strtolower($email)) . '?d=' . urlencode($default) . '&s=' . $size;
$gravatar = '<img class="avatar" src="'.$grav_img.'"/>';
return $gravatar;
} else {
$burt_img = $this->gimme_burt();
$gravatar = '<img class="avatar" src="'.get_stylesheet_directory_uri().'/images/burt/'.$burt_img.'" height="'.$size.'" width="'.$size.'"/>';
return $gravatar;
}
// return $gravatar;
}
/**
* call download redirects
*
* @return Norcrossv4
*/
public function redirects() {
global $wp_query;
// downloads redirect
if ( is_singular('downloads') ) :
global $post;
$dl_file = get_post_meta($post->ID, '_rkv_download_url', true);
if(empty( $dl_file ))
return;
wp_redirect( esc_url_raw( $dl_file ), 301 );
exit();
endif;
}
/**
* helper function for getting Gists
*
* @return Norcrossv4
*/
public function gist_check() {
$args = array (
'fields' => 'ids',
'post_type' => 'snippets',
'post_status' => 'publish',
'nopaging' => true,
'meta_key' => '_rkv_gist_id',
);
$gist_query = get_posts( $args );
$gist_count = (count($gist_query) > 0 ) ? true : false;
if($gist_count == false)
$gist_ids[] = 0;
if($gist_count == true) {
foreach ($gist_query as $gist):
$gist_ids[] = get_post_meta($gist, '_rkv_gist_id', true);
endforeach;
}
return $gist_ids;
}
/**
* run cron for gists
*
* @return Norcrossv4
*/
public function run_gists_cron() {
$args = array(
'sslverify' => false
);
$request = new WP_Http;
$url = 'https://api.github.com/users/norcross/gists?&per_page=100';
$response = wp_remote_get ( $url, $args );
if( is_wp_error( $response ) )
return;
$data_raw = $response['body'];
if (!$data_raw)
return;
$data_array = json_decode($data_raw);
if (!is_array($data_array))
return;
// loop through each gist and create a post
foreach ($data_array as $data) {
$gist_id = $data->id;
$gist_check = $this->gist_check();
if (!in_array($gist_id, $gist_check)) {
$gist_url = $data->html_url;
$gist_title = $data->description;
// run conversion for empty titles
$snip_title = empty($gist_title) ? 'Gist: '.$gist_id : $gist_title;
// get and convert timestamp
$date = $data->created_at;
$stamp = strtotime($date);
$gmtdt = date('Y-m-d H:i:s', $stamp );
$pubdt = date('Y-m-d H:i:s', ($stamp - 18000 ));
// build new snippet array
$snippet = array(
'post_type' => 'snippets',
'post_title' => $snip_title,
'post_name' => 'snippet-'.$gist_id,
'post_content' => '',
'post_excerpt' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_date' => $pubdt,
'post_date_gmt' => $gmtdt
);
// add the post to the database
$new_gist = wp_insert_post($snippet, true);
// add some postmeta
if (!is_wp_error($new_gist) ) {
add_post_meta($new_gist, '_rkv_gist_id', $gist_id);
add_post_meta($new_gist, '_rkv_gist_url', $gist_url);
}
} // end the array comparison
} // end the foreach
} // end the function
/**
* helper function for Instagram
*
* @return Norcrossv4
*/
public function insta_check() {
$args = array (
'fields' => 'ids',
'post_type' => 'photos',
'nopaging' => true,
'meta_key' => '_rkv_photo_id',
);
$photo_query = get_posts( $args );
$photo_count = (count($photo_query) > 0 ) ? true : false;
if($photo_count == false)
$photo_ids[] = 0;
if($photo_count == true) {
foreach ($photo_query as $photo):
$photo_ids[] = get_post_meta($photo, '_rkv_photo_id', true);
endforeach;
}
return $photo_ids;
}
/**
* helper function for making it a sideloaded image
*
* @return Norcrossv4
*/
public function insta_helper($new_photo) {
// load media handlers
require_once(ABSPATH . 'wp-admin' . '/includes/image.php');
require_once(ABSPATH . 'wp-admin' . '/includes/file.php');
require_once(ABSPATH . 'wp-admin' . '/includes/media.php');
$pid = $new_photo;
$image = get_post_meta($new_photo, '_rkv_photo_full', true);
$title = get_the_title($new_photo);
media_sideload_image($image, $pid, $title);
// now set as an attachment
$args = array(
'fields' => 'ids',
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $pid
);
$photo_attach = get_posts($args);
if ($photo_attach) :
foreach ( $photo_attach as $attach_id ) :
update_post_meta($pid, '_thumbnail_id', $attach_id);
endforeach;
endif;
}
/**
* run cron for instagram
*
* @return Norcrossv4
*/
public function run_insta_cron() {
$args = array(
'sslverify' => false
);
// grab username and total photos to grab
$user = akm_get_key('IG_USER');
$token = akm_get_key('IG_TOKEN');
$count = 60;
// set number of items to return
if (!empty ($number) ) { $max = $number; } else { $max = 60; } // 60 is the max return in the Instagram API
$request = new WP_Http;
$url = 'https://api.instagram.com/v1/users/self/media/recent?access_token='.$token.'&count='.$count.'';
$response = wp_remote_get ( $url, $args );
$data_array = json_decode( $response['body'] );
$photos = $data_array->data;
// loop through each gist and create a post
foreach ($photos as $photo) {
$photo_id = $photo->created_time;
$photo_check = $this->insta_check();
if (!in_array($photo_id, $photo_check)) {
$stamp = $photo_id - 18000;
$pubdt = date('Y-m-d H:i:s', $stamp);
// make caption with fallback
$caption_base = $photo->caption;
$image_caption = empty($caption_base->text) ? '' : $caption_base->text;
$image_caption = str_replace('@', '', $image_caption);
// build new photo array
$snapshot = array(
'post_type' => 'photos',
'post_title' => $photo_id,
'post_name' => $photo_id,
'post_content' => $image_caption,
'post_excerpt' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_date' => $pubdt,
);
// add the post to the database
$new_photo = wp_insert_post($snapshot, true);
// add some postmeta
if (!is_wp_error($new_photo) ) {
// build thumbnail 150px
$thumb_base = $photo->images->thumbnail;
$thumb_url = $thumb_base->url;
// build standard 306px
$standard_base = $photo->images->low_resolution;
$standard_url = $standard_base->url;
// build fullsize 612px
$fullsize_base = $photo->images->standard_resolution;
$fullsize_url = $fullsize_base->url;
// now make some meta
add_post_meta($new_photo, '_rkv_photo_id', $photo_id);
add_post_meta($new_photo, '_rkv_photo_thumb', $thumb_url);
add_post_meta($new_photo, '_rkv_photo_stand', $standard_url);
add_post_meta($new_photo, '_rkv_photo_full', $fullsize_url);
$this->insta_helper($new_photo);
}
} // end the array comparison
} // end the foreach
} // end the function
/// end class
}
// Instantiate our class
$NorcrossVersionFour = new NorcrossVersionFour();