forked from humanmade/hm-core
/
hm-core.functions.php
executable file
·1340 lines (1027 loc) · 31.9 KB
/
hm-core.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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* hm_human_post_time function.
*
* @access public
* @param string $timestamp. (default: 'current')
* @return void
*/
function hm_human_post_time( $timestamp = 'current' ) {
if ( empty( $timestamp ) ) return false;
if ( $timestamp === 'current' ) $timestamp = time();
if ( abs( time() - date( 'G', $timestamp ) ) < 86400 )
return human_time_diff( date( 'G', $timestamp ) );
else return date( 'Y/m/d g:i:s A', $timestamp );
}
/**
* recursive_in_array function.
*
* @access public
* @param mixed $needle
* @param mixed $haystack
* @return void
*/
function recursive_in_array($needle, $haystack) {
foreach ($haystack as $stalk) {
if ($needle == $stalk || (is_array($stalk) && recursive_in_array($needle, $stalk))) {
return true;
}
}
return false;
}
/**
* Teh same as array_filter(), but will recursilvy traverse arrays.
*
* @param array $input
* @param function $callback. (default: null)
* @return array
*/
function hm_array_filter_recursive($input, $callback = null) {
foreach ($input as &$value) {
if (is_array($value)) {
$value = hm_array_filter_recursive($value, $callback);
}
}
if ( $callback )
return array_filter($input, $callback);
return array_filter($input);
}
/**
* hm_get_permalink function.
*
* @access public
* @param mixed $post
* @return void
*/
function hm_get_permalink( $post ) {
$post = hm_parse_post( $post );
return get_permalink( $post->ID );
}
/**
* hm_shorten_string function.
*
* @access public
* @param mixed $string
* @param mixed $length
* @return void
*/
function hm_shorten_string( $string, $length ) {
if ( strlen( $string ) > (int)$length )
return substr( $string, 0, $length - 3 ) . '...';
return $string;
}
function hm_sort_array_by_object_key( $array, $object_key ) {
global $hm_sort_array_by_object_key;
$hm_sort_array_by_object_key = $object_key;
usort( $array, '_hm_sort_array_by_object_key_cmp' );
return $array;
}
function _hm_sort_array_by_object_key_cmp( $a, $b ) {
global $hm_sort_array_by_object_key;
if( is_object( $a ) ) {
$valuea = is_numeric( $a->{$hm_sort_array_by_object_key} ) ? (int) $a->{$hm_sort_array_by_object_key} : $a->{$hm_sort_array_by_object_key};
$valueb = is_numeric( $b->{$hm_sort_array_by_object_key} ) ? (int) $b->{$hm_sort_array_by_object_key} : $b->{$hm_sort_array_by_object_key};
} elseif( is_array( $a ) ) {
$valuea = is_numeric( $a[$hm_sort_array_by_object_key] ) ? (int) $a[$hm_sort_array_by_object_key] : $a[$hm_sort_array_by_object_key];
$valueb = is_numeric( $b[$hm_sort_array_by_object_key] ) ? (int) $b[$hm_sort_array_by_object_key] : $b[$hm_sort_array_by_object_key];
}
return $valuea > $valueb;
}
/**
* array_map_preserve_keys function. Customized array_map function which preserves keys/associate array indexes. Note that this costs a descent amount more memory (eg. 1.5k per call)
*
* @access public
* @param callback $callback Callback function to run for each element in each array.
* @param mixed $arr1 An array to run through the callback function.
* @param array $array Variable list of array arugments to run through the callback function.
* @return array Array containing all the elements of $arr1 after applying the callback function to each one, recursively, maintain keys.
*/
function array_map_preserve_keys($callback,$arr1) {
$results = array();
$args = array();
if (func_num_args()>2)
$args = (array) array_shift(array_slice(func_get_args(),2));
foreach($arr1 as $key=>$value) {
$temp = $args;
array_unshift($temp,$value);
if (is_array($value)) {
$results[$key] = call_user_func_array($callback,$temp);
} else {
$results[$key] = call_user_func_array($callback,$temp);
}
}
return $results;
}
function hm_count( $count, $none, $one, $more = null ) {
if ( $count > 1 )
echo str_replace( '%', $count, $more );
elseif ( $count == 1 )
echo $one;
else
echo $none;
}
function hm_unsanitize_title( $title ) {
return ucwords( str_replace( '_', ' ', $title ) );
}
function get_post_meta_by( $field = 'post_id', $value ) {
return get_metadata_by( $field, $value, 'post' );
}
function get_term_meta_by( $field = 'term_id', $value ) {
return get_metadata_by( $field, $value, 'term' );
}
function get_metadata_by( $fields, $values, $type = 'post', $col = '*' ) {
global $wpdb;
if ( empty( $fields ) || ( !is_array( $fields ) && empty( $values ) ) )
return array();
if ( !is_array( $fields ) && !empty( $fields ) && !is_array( $values ) && !empty( $values ) )
$fields = array( $fields => $values );
if ( is_array( $fields ) && is_array( $values ) )
$fields = array_combine( $fields, $values );
foreach ( (array) $fields as $field => $value )
$where[] = "" . $field . " = '" . $value . "'";
$table = $wpdb->prefix . $type . 'meta';
return $wpdb->get_results( "SELECT $col FROM $table WHERE " . implode( ' AND ' , (array) $where ) );
}
/**
* Get array of a terms children (across taxonomy)
*
* @param object $parent term object
* @return array
*/
function hm_get_term_children( $parent, $taxonomy = null ) {
if ( ! is_numeric( $parent ) )
return false;
global $wpdb;
$where = "WHERE tt.parent = $parent";
if ( $taxonomy )
$where .= " AND tt.taxonomy = '$taxonomy'";
$query = "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id $where ORDER BY t.name ASC";
$terms = $wpdb->get_results( $query );
return $terms;
}
function hm_get_post_image( $post = null, $w = 0, $h = 0, $crop = false, $id = null, $default = null ) {
if ( $post === null ) global $post;
// stop images with no post_id slipping in
if ( $post->ID == 0 && !$id )
return;
$id = $id ? $id : hm_get_post_image_id( $post );
if ( $id )
return wpthumb( get_attached_file( $id ), $w, $h, $crop, true, wpthumb_wm_get_options( $id ) );
$att_id = hm_get_post_attached_image_id( $post );
if ( $att_id )
return wpthumb( get_attached_file( $att_id ), $w, $h, $crop, true, wpthumb_wm_get_options( $id ) );
//if there is no id, then try search the content for an image
if ( $return = wpthumb(hm_get_post_internal_image($post), $w, $h, $crop, true, wpthumb_wm_get_options( $id )) )
return $return;
if ( $return = hm_get_post_external_image($post) )
return $return;
if ( $default ) {
$file = $default === 'default' ? dirname( __FILE__ ) . '/includes/image-unavailable.png' : $default;
return wpthumb( $file, $w, $h, $crop, true );
}
}
function hm_get_post_image_id( $post = null ) {
if ( $post === null ) global $post;
if ( $post->ID == 0 )
return;
$id = (int) get_post_meta( $post->ID, 'hm_post_image', true );
if ( $id )
return $id;
return hm_get_post_attached_image_id($post);
}
function hm_get_post_attached_image_id( $post = null, $return = 'file' ) {
if ( $post === null ) global $post;
$images = array();
foreach( (array) get_children( array( 'post_parent' => $post->ID, 'post_type' => 'attachment', 'orderby' => 'menu_order', 'order' => 'ASC' ) ) as $attachment ) {
if ( !wp_attachment_is_image( $attachment->ID ) || !file_exists( get_attached_file( $attachment->ID ) ) )
continue;
return $attachment->ID;
}
}
function hm_get_post_attached_images( $post = null ) {
if ( $post === null ) global $post;
$images = array();
foreach( (array) get_children( array( 'post_parent' => $post->ID, 'post_type' => 'attachment', 'orderby' => 'menu_order', 'order' => 'ASC' ) ) as $attachment ) {
if ( !wp_attachment_is_image( $attachment->ID ) || !file_exists( get_attached_file( $attachment->ID ) ) )
continue;
$images[] = $attachment;
}
return $images;
}
function hm_get_post_attached_images_id( $post = null ) {
$images = array();
foreach( hm_get_post_attached_images( $post ) as $attachment ) {
$images[] = $attachment->ID;
}
return $images;
}
/**
* Get the first image from inside the post content.
*
* @access public
* @param int $post_id. (default: null)
* @return int
*/
function hm_get_post_internal_image( $post_id ) {
return reset( hm_get_post_internal_images( $post_id ) );
}
/**
* hm_get_post_internal_images function.
*
* @access public
* @param int $post_id
* @return Array attachment id's
*/
function hm_get_post_internal_images( $post_id ) {
$post = get_post( $post_id );
$images = array();
if ( empty( $post->post_content ) )
return array();
preg_match_all( '/(img|src)=("|\')[^"\'>]+/i', $post->post_content, $media );
$data = preg_replace( '/(img|src)("|\'|="|=\')(.*)/i', "$3", reset( $media ) );
if ( empty( $data ) )
return array();
foreach( $data as $url )
if ( strpos( $url, get_bloginfo( 'url' ) ) === 0 && file_exists( $path = str_ireplace( trailingslashit( get_bloginfo( 'url' ) ), trailingslashit( ABSPATH ), $url ) ) )
$images[] = $path;
return $images;
}
/**
* Strip all images from a string
*
* @access public
* @param mixed $post_content
* @param mixed $post_id. (default: null)
* @return null
*/
function strip_images( $content, $post_id = null ) {
if ( is_null( $post_id ) )
return preg_replace( '/(<img[\s\S]*?\>)/i', '', $content );
return preg_replace( '/<img[\s\S]*?wp-image-' . $post_id . '[\s\S]*?\>/i', '', $content );
}
/**
* Return an array of external images in a post
*
* @access public
* @param mixed $post. (default: null)
* @return null
*/
function hm_get_post_external_image( $post_id = null ) {
$post = get_post( $post_id );
$images = array();
preg_match_all( '/(img|src)=("|\')[^"\'>]+/i', $post->post_content, $media );
$data = preg_replace( '/(img|src)("|\'|="|=\')(.*)/i', "$3", reset( $media ) );
foreach( $data as $url) {
$ext = end( explode( '.', $url ) );
if ( strpos( $url, 'http://' ) === 0 && strpos( $url, get_bloginfo( 'url' ) ) === false && $ext == 'png' || $ext == 'jpg' || $ext == 'bmp' || $ext == 'jpeg' || $ext == 'gif' )
$images[] = $url;
}
return reset( $images );
}
function hm_remote_get_file( $url, $cache = true ) {
//check for stuff
$upload_dir = wp_upload_dir();
$dest_folder = $upload_dir['basedir'] . '/remote_files/';
$dest_file = $dest_folder . sanitize_title($url) . '.' . end(explode('.', $url));
// cache file 404s in options
$file_404s = (array) get_option( 'remote_404s' );
if ( isset( $file_404s[$url] ) && (int) $file_404s[$url] > ( time() - ( 60 * 60 *12 ) ) && $cache === true ) {
return null;
}
if ( !is_dir( $dest_folder ) ) {
mkdir( $dest_folder );
}
if ( file_exists( $dest_file ) && file_get_contents( $dest_file ) && $cache === true ) {
return $dest_file;
}
do_action( 'start_operation', $operation = ( 'Remote get file: ' . $url ) );
if ( $fp = @fopen($url, 'r') ) {
$content = '';
// keep reading until there's nothing left
while ($line = fread($fp, 1024)) {
$content .= $line;
}
}
if ( empty( $content ) ) {
$file_404s[$url] = time();
update_option( 'remote_404s', $file_404s );
do_action( 'end_operation', $operation );
return null;
}
preg_match('/Content-Length: ([0-9]+)/', $content, $parts);
$image_data = substr($content, - $parts[1]);
$ptr = fopen($dest_file, 'wb');
fwrite($ptr, $image_data);
fclose($ptr);
do_action( 'end_operation', $operation );
return $dest_file;
}
function hm_get_countries( $popular = null ) {
$countries = array(
'Afghanistan',
'Albania',
'Algeria',
'Andorra',
'Angola',
'Antigua and Barbuda',
'Argentina',
'Armenia',
'Australia',
'Austria',
'Azerbaijan',
'Bahamas',
'Bahrain',
'Bangladesh',
'Barbados',
'Belarus',
'Belgium',
'Belize',
'Benin',
'Bhutan',
'Bolivia',
'Bosnia and Herzegovina',
'Botswana',
'Brazil',
'Brunei',
'Bulgaria',
'Burkina Faso',
'Burundi',
'Cambodia',
'Cameroon',
'Canada',
'Cape Verde',
'Central African Republic',
'Chad',
'Chile',
'China',
'Colombi',
'Comoros',
'Congo (Brazzaville)',
'Congo',
'Costa Rica',
'Cote d\'Ivoire',
'Croatia',
'Cuba',
'Cyprus',
'Czech Republic',
'Denmark',
'Djibouti',
'Dominica',
'Dominican Republic',
'East Timor (Timor Timur)',
'Ecuador',
'Egypt',
'El Salvador',
'Equatorial Guinea',
'Eritrea',
'Estonia',
'Ethiopia',
'Fiji',
'Finland',
'France',
'Gabon',
'Gambia, The',
'Georgia',
'Germany',
'Ghana',
'Greece',
'Grenada',
'Guatemala',
'Guinea',
'Guinea-Bissau',
'Guyana',
'Haiti',
'Honduras',
'Hungary',
'Iceland',
'India',
'Indonesia',
'Iran',
'Iraq',
'Ireland',
'Israel',
'Italy',
'Jamaica',
'Japan',
'Jordan',
'Kazakhstan',
'Kenya',
'Kiribati',
'Korea, North',
'Korea, South',
'Kuwait',
'Kyrgyzstan',
'Laos',
'Latvia',
'Lebanon',
'Lesotho',
'Liberia',
'Libya',
'Liechtenstein',
'Lithuania',
'Luxembourg',
'Macedonia',
'Madagascar',
'Malawi',
'Malaysia',
'Maldives',
'Mali',
'Malta',
'Marshall Islands',
'Mauritania',
'Mauritius',
'Mexico',
'Micronesia',
'Moldova',
'Monaco',
'Mongolia',
'Morocco',
'Mozambique',
'Myanmar',
'Namibia',
'Nauru',
'Nepa',
'Netherlands',
'New Zealand',
'Nicaragua',
'Niger',
'Nigeria',
'Norway',
'Oman',
'Pakistan',
'Palau',
'Panama',
'Papua New Guinea',
'Paraguay',
'Peru',
'Philippines',
'Poland',
'Portugal',
'Qatar',
'Romania',
'Russia',
'Rwanda',
'Saint Kitts and Nevis',
'Saint Lucia',
'Saint Vincent',
'Samoa',
'San Marino',
'Sao Tome and Principe',
'Saudi Arabia',
'Senegal',
'Serbia and Montenegro',
'Seychelles',
'Sierra Leone',
'Singapore',
'Slovakia',
'Slovenia',
'Solomon Islands',
'Somalia',
'South Africa',
'Spain',
'Sri Lanka',
'Sudan',
'Suriname',
'Swaziland',
'Sweden',
'Switzerland',
'Syria',
'Taiwan',
'Tajikistan',
'Tanzania',
'Thailand',
'Togo',
'Tonga',
'Trinidad and Tobago',
'Tunisia',
'Turkey',
'Turkmenistan',
'Tuvalu',
'Uganda',
'Ukraine',
'United Arab Emirates',
'United Kingdom',
'United States',
'Uruguay',
'Uzbekistan',
'Vanuatu',
'Vatican City',
'Venezuela',
'Vietnam',
'Yemen',
'Zambia',
'Zimbabwe'
);
if ( !is_null( $popular ) )
$countries = $popular + $countries;
return $countries;
}
function hm_time_to_local( $time = null ) {
if ( is_null( $time ) )
$time = time();
return $time + ( get_option( 'gmt_offset' ) * 3600 );
}
function hm_local_to_time( $local_time ) {
return $local_time - ( get_option( 'gmt_offset' ) * 3600 );
}
function hm_time_to_dst_offset( $time ) {
// Set TZ so localtime works.
date_default_timezone_set( get_option('timezone_string') );
if ( date( 'I', $time ) !== date( 'I', time() ) ) {
if ( date( 'I', $time ) > date( 'I', time() ) ) {
//post was created in DST (+1 hour)
$time += 3600;
} else {
//post was created in standard time (-1 Hour)
$time -= 3600;
}
}
// Set back to UTC.
date_default_timezone_set('UTC');
return $time;
}
// This forces the inbuilt mail function to send html emails instead of plain text emails.
function wp_mail_content_type_html( $content_type ) {
return 'text/html';
}
function hm_wp_mail_from( $mail ) {
return apply_filters( 'hm_wp_mail_from', str_replace( 'wordpress@', 'noreply@', $mail ) );
}
function hm_array_value( $array, $key ) {
return $array[$key];
}
function hm_wp_mail_from_name() {
return apply_filters( 'hm_wp_mail_from_name', get_bloginfo( 'name' ) );
}
/**
* Insert a variable into an array at a givven position, shunting keys down.
*
* @param array $array
* @param int $pos
* @param mixed $val
* @return array
*/
function hm_array_insert( $array, $pos, $val, $key = null) {
$array2 = array_splice($array,$pos);
if ( $key )
$array[$key] = $val;
else
$array[] = $val;
$array = array_merge($array,$array2);
return $array;
}
/**
* Implode an array into a list of items separated by $separator.
* Use $last_separator for the last list item.
*
* Useful for natural language lists (e.g first, second & third).
*
* @param array $array
* @param string $separator. (default: ', ')
* @param string $last_separator. (default: ' & ')
* @return string a list of array values
*/
function hm_multi_implode( $array, $separator = ', ', $last_separator = ' & ' ) {
if ( count( $array ) == 1 )
return reset( $array );
$end_value = array_pop( $array );
$list = implode( $separator, $array );
return $list . $last_separator . $end_value;
}
/**
* Take into account draft posts when you specify hide_empty in get_terms()
*
* @param StdClass[] $terms
* @param string $taxonomies
* @param array $args
* @return StdClass[]
*/
function hm_add_exclude_draft_to_get_terms_hide_empty( $terms, $taxonomies, $args ) {
if ( $args['hide_empty'] == false || ( $args['hide_empty'] == true && empty( $args['hide_empty_exclude_drafts'] ) ) )
return $terms;
global $wpdb;
// Check that each term has posts in it which are published
foreach( $terms as $key => $term ) {
$post_id = $wpdb->get_var( "SELECT ID FROM $wpdb->posts INNER JOIN $wpdb->term_relationships ON $wpdb->posts.ID = $wpdb->term_relationships.object_id WHERE post_status != 'draft' AND $wpdb->term_relationships.term_taxonomy_id = $term->term_taxonomy_id LIMIT 0, 1" );
if ( !$post_id )
unset( $terms[$key] );
}
return $terms;
}
add_filter( 'get_terms', 'hm_add_exclude_draft_to_get_terms_hide_empty', 1, 3 );
/**
* Retuns pagination html for list pages
*
* TODO - this should use a walker?
*
* @param object $wp_query. (default: global $wp_query)
* @param int $current_page. (default: $wp_query['query_vars']['paged'])
* @param int $ppp. (posts per page) (default: 10)
* @return pagination hmtl
*/
function hm_get_pagination( $wp_query = null, $current_page = null, $ppp = null, $args = array() ) {
global $wp_rewrite;
if ( is_null( $wp_query ) )
global $wp_query;
if ( is_null( $ppp ) )
$ppp = $wp_query->query_vars['posts_per_page'] ? $wp_query->query_vars['posts_per_page'] : get_option( 'posts_per_page' );
$number_pages = ceil ( $wp_query->found_posts / $ppp );
if ( is_null( $current_page ) )
$current_page = get_query_var( 'paged' );
$current_page = $current_page ? $current_page : 1;
$defaults = array(
'next_text' => 'Next »',
'prev_text' => '« Prev',
'show_all' => false
);
$args = wp_parse_args( $args, $defaults );
if ( $wp_rewrite->pagination_base )
$wp_rewrite->pagination_base = trailingslashit( $wp_rewrite->pagination_base );
$base = str_replace( $wp_rewrite->pagination_base . $current_page . '/', '', $_SERVER['REQUEST_URI'] );
$base = remove_query_arg( 'paged', $base );
// Strip any query args, put them on the end (after /page/%number%/
$bases = explode( '?', $base );
$base = current( $bases );
if ( isset( $bases[1] ) )
$query_params = $bases[1];
// Mid-size depends on what page you are on, as it applies to each side of current_page
if ( $current_page > ( $number_pages - 3 ) )
$mid_size = 5 - ( $number_pages - ( $current_page ) + 1 );
elseif ( $current_page >= 5 )
$mid_size = 2;
elseif ( $current_page == 1 )
$mid_size = 4;
else
$mid_size = 5 - $current_page + 1;
$page_links = paginate_links( array(
'base' => trailingslashit( $base ) . ( ( isset( $_GET['s'] ) && $_GET['s'] ) ? '' : $wp_rewrite->pagination_base . '%#%/' ) . ( ( isset( $query_params ) && $query_params ) ? '?' . $query_params : '' ) . ( ( isset( $_GET['s'] ) && $_GET['s'] ) ? '&paged=%#%' : '' ),
'format' => '',
'prev_text' => $args['prev_text'],
'next_text' => $args['next_text'],
'total' => $number_pages,
'current' => $current_page,
'mid_size' => $mid_size,
'end_size' => 1,
'show_all' => $args['show_all'],
'type' => 'array'
) );
if ( ! is_array( $page_links ) || empty( $page_links ) )
return;
foreach ( $page_links as &$page_link ) {
if ( strpos( $page_link, $args['prev_text'] ) )
$page_link = str_replace( '>', ' rel="prev">', $page_link );
if ( strpos( $page_link, $args['next_text'] ) )
$page_link = str_replace( '>', ' rel="next">', $page_link );
}
// Loop through the page links, removing any unwanted ones as paginate_links() does not provide such fine control
$real_counter = 0;
$output = '';
foreach( $page_links as $counter => $pagination_item ) :
if ( ( strpos( $pagination_item, '...' ) && $counter == 2 ) || ( $counter == 1 && strpos( $page_links[2], '...' ) ) || ( $counter == 1 && $current_page == 4 && $args['show_all'] === false ) )
continue;
// Strip ..., last page
if ( strpos( $pagination_item, '...' ) || ( strpos( $page_links[$counter ? $counter - 1 : 0], '...') && $counter == count( $page_links ) - 2 ) || ( $counter == 1 && strpos( $page_links[ 2 ], '...' ) ) || ( $counter == 1 && strpos( $page_links[0], $args['prev_text'] ) && $current_page == 4 ) )
$real_counter--;
if ( $real_counter >= 6 && strpos( $pagination_item, $args['next_text'] ) === false && $args['show_all'] === false )
continue;
$real_counter++;
$output .= $pagination_item;
endforeach;
// Exception for page 1
if ( isset( $_GET['s'] ) && $_GET['s'] )
$output = str_replace( "&paged=1'", "'", $output );
else
$output = str_replace( $wp_rewrite->pagination_base . '1/', '', $output );
return '<div class="pagination">' . $output . '</div>';
}
/**
* Echoes hm_get_pagination
*
*/
function hm_pagination() {
echo hm_get_pagination();
}
/**
* Returns post pagination html
*
* @param object $post. (default: global $post)
* @param int $current_page. (default: global $numposts)
* @return string - pagination html
*/
function hm_get_post_pagination( $post = null, $current_page = null ) {
global $numpages;
// set number_pages to the global if we are using global $post
if ( $post === null ) $number_pages = $numpages;
if ( $post === null ) global $post;
if ( !$current_page ) $current_page = ( ( $page = get_query_var( 'page' ) ) ? $page : 1 );
if ( $number_pages == null ) {
$pages = explode('<!--nextpage-->', $post->post_content);
$number_pages = count($pages);
}
$defaults = array(
'next_text' => __('Next »'),
'prev_text' => __('« Prev')
);
$args = wp_parse_args( $args, $defaults );
// mid-size depends on what page you are on, as it applies to each side of current_page
if ( $current_page > ($number_pages - 3) ) {
$mid_size = 5 - ($number_pages - ($current_page) + 1 );
} elseif ( $current_page >= 5 ) {
$mid_size = 2;
} elseif ( $current_page == 1 ) {
$mid_size = 4;
} else {
$mid_size = 5 - $current_page + 1;
}
if ( is_preview() ) {
$base = get_permalink( $post->ID ) . '&page=%#%';
} else {
$base = trailingslashit( get_permalink( $post->ID ) ) . '%#%/';
}
$page_links = paginate_links( array(
'base' => $base,
'format' => '',
'prev_text' => $args['prev_text'],
'next_text' => $args['next_text'],
'total' => $number_pages,
'current' => $current_page,
'mid_size' => $mid_size,
'end_size' => 1,
'type' => 'array'
));
if ( !is_array( $page_links ) || empty( $page_links ) )
return;
// loop through the page links, removing any unwanted ones as paginate_links() does not provide such fine control
$real_counter = 0;
foreach( $page_links as $counter => $pagination_item ) :
//strip ..., last page
if ( strpos($pagination_item, '...') || ( strpos($page_links[ $counter - 1 ], '...') && $counter == count( $page_links ) - 2 ) || ( $counter == 1 && strpos($page_links[ 2 ], '...') ) || ( $counter == 1 && strpos($page_links[ 0 ], $args['prev_text']) && $current_page == 4 ) )
continue;
if ( $real_counter >= 6 && strpos( $pagination_item, $args['next_text'] ) === false )
continue;
$real_counter++;
$output .= $pagination_item;
endforeach;
// exception for page 1
$output = str_replace( '/1/', '/', $output );
return '<div class="post-pagination">' . $output . '</div>';
}
/**
* Echoes hm_get_post_pagination
*
*/
function hm_post_pagination() {
echo hm_get_post_pagination();
}
/**
* Add a submenu class to parent menus
*
* @access public
* @param array $classes
* @param object $item
* @todo not sure this actually works?
* @return null
*/
function hm_submenu_class( $classes, $item ) {
if ( get_post_meta_by( array( 'meta_value', 'meta_key' ), array( $item->ID, '_menu_item_menu_item_parent' ) ) )
$classes[] = 'menu_parent';
return $classes;