/
hm-core.functions.php
executable file
·1759 lines (1360 loc) · 43.2 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 );
}
/**
* hm_parse_user function.
*
* @access public
* @param mixed $user. (default: null)
* @return void
*/
function hm_parse_user( $user = null ) {
// We're we passed an object with ID
if ( is_object( $user ) && is_numeric( $user->ID ) )
return get_userdata( $user->ID );
// We're we passed an object with user_id
if ( is_object( $user ) && is_numeric( $user->user_id ) )
return get_userdata( $user->user_id );
// We're we passed an array
if ( is_array( $user ) && is_numeric( $user['ID'] ) )
return get_userdata( $user['ID'] );
// ID
if ( is_numeric( $user ) )
return get_userdata( $user );
// username
if ( is_string( $user ) )
return get_userdatabylogin( $user );
// null
global $current_user;
return get_userdata( $current_user->ID );
}
/**
* hm_parse_post function.
*
* @access public
* @param mixed $post. (default: null)
* @return void
*/
function hm_parse_post( $post = null ) {
if ( is_object( $post ) || is_array( $post ) )
return (object) $post;
if ( is_numeric( $post ) )
return get_post( $post );
if ( is_null( $post ) )
return get_post( $post );
if ( is_string( $post ) && get_page_by_title( $post ) )
return get_page_by_title( $post );
if ( is_string( $post ) && get_page_by_path( $post ) )
return get_page_by_path( $post );
if ( is_string( $post ) ) :
global $wpdb;
return get_post( $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_name = '$post'" ) );
endif;
}
/**
* 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;
}
/**
* Sorts a 2 dimentional array by a given 2nd level array key.
*
* @param array &$data
* @param string $sortby - array key
* @return array
*/
function masort($array, $id, $sort_ascending = true) {
$temp_array = array();
$original_array = $array;
foreach( $array as $key => $value ) {
$original_keys[] = $key;
}
$array = array_values( $array );
while(count($array)>0) {
$lowest_id = 0;
$index=0;
$index_to_keys = array();
foreach ($array as $key => $item) {
if ( isset($item[$id]) ) {
if ($array[$lowest_id][$id]) {
if ( is_numeric( $item[$id] ) ) {
if ( $item[$id] < $array[$lowest_id][$id] ) {
$lowest_id = $index;
$index_to_keys[$index] = $key;
}
} else {
if (strtolower($item[$id]) < strtolower($array[$lowest_id][$id])) {
$lowest_id = $index;
$index_to_keys[$index] = $key;
}
}
}
}
$index++;
}
$temp_array[ array_search( $array[$lowest_id], $original_array ) ] = $array[$lowest_id];
$array = array_merge(array_slice($array, 0,$lowest_id), array_slice($array, $lowest_id+1));
}
if ($sort_ascending) {
return $temp_array;
} else {
return array_reverse($temp_array);
}
}
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;
}
/**
* is_odd function.
*
* @param int
* @return bool
*/
function is_odd( $int ) {
return $int & 1;
}
/**
* in_array_multi function.
*
* @param mixed $needle array value
* @param array $haystack
* @return array - found results
*/
function in_array_multi( $needle, $haystack ) {
foreach( (array) $haystack as $key => $stack ) {
if ( is_array( $stack ) && in_array_multi( $needle, $stack ) )
return true;
if ( $stack === $needle )
return true;
}
return false;
}
/**
* multi_array_key_exists function.
*
* @param mixed $needle The key you want to check for
* @param mixed $haystack The array you want to search
* @return bool
*/
function multi_array_key_exists( $needle, $haystack ) {
foreach ( $haystack as $key => $value ) :
if ( $needle === $key ) {
return true;
}
if ( is_array( $value ) ) :
if ( multi_array_key_exists( $needle, $value ) == true )
return true;
else
continue;
endif;
endforeach;
return false;
}
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;
}
// term meta functions
//
function hm_add_term_meta_table() {
global $wpdb;
if ( !current_theme_supports( 'term-meta' ) )
return false;
//only creates if needed
hm_create_term_meta_table();
$wpdb->tables[] = 'termmeta';
$wpdb->termmeta = $wpdb->prefix . 'termmeta';
}
add_action( 'init', 'hm_add_term_meta_table' );
/**
* Creates the termmeta table if it deos not exist
*
* @todo this causes database error on sites which have termmeta already
*/
function hm_create_term_meta_table() {
global $wpdb;
// check if the table is already exists
if ( get_option( 'hm_created_term_meta_table' ) )
return;
$wpdb->query( "
CREATE TABLE `{$wpdb->prefix}termmeta` (
`meta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`term_id` bigint(20) unsigned NOT NULL DEFAULT '0',
`meta_key` varchar(255) DEFAULT NULL,
`meta_value` longtext,
PRIMARY KEY (`meta_id`),
KEY `term_id` (`term_id`),
KEY `meta_key` (`meta_key`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;" );
update_option( 'hm_created_term_meta_table', true );
return true;
}
if ( !function_exists( 'add_term_meta' ) ) :
/**
* Add meta data field to a term.
*
* @param int $term_id term ID.
* @param string $key Metadata name.
* @param mixed $value Metadata value.
* @param bool $unique Optional, default is false. Whether the same key should not be added.
* @return bool False for failure. True for success.
*/
function add_term_meta($term_id, $meta_key, $meta_value, $unique = false) {
return add_metadata('term', $term_id, $meta_key, $meta_value, $unique);
}
endif;
if ( !function_exists( 'delete_term_meta' ) ) :
/**
* Remove metadata matching criteria from a term.
*
* You can match based on the key, or key and value. Removing based on key and
* value, will keep from removing duplicate metadata with the same key. It also
* allows removing all metadata matching key, if needed.
*
* @param int $term_id term ID
* @param string $meta_key Metadata name.
* @param mixed $meta_value Optional. Metadata value.
* @return bool False for failure. True for success.
*/
function delete_term_meta($term_id, $meta_key, $meta_value = '') {
return delete_metadata('term', $term_id, $meta_key, $meta_value);
}
endif;
if ( !function_exists( 'get_term_meta' ) ) :
/**
* Retrieve term meta field for a term.
*
* @param int $term_id term ID.
* @param string $key The meta key to retrieve.
* @param bool $single Whether to return a single value.
* @return mixed Will be an array if $single is false. Will be value of meta data field if $single
* is true.
*/
function get_term_meta($term_id, $key, $single = false) {
return get_metadata('term', $term_id, $key, $single);
}
endif;
if ( !function_exists( 'update_term_meta' ) ) :
/**
* Update term meta field based on term ID.
*
* Use the $prev_value parameter to differentiate between meta fields with the
* same key and term ID.
*
* If the meta field for the term does not exist, it will be added.
*
* @param int $term_id term ID.
* @param string $key Metadata key.
* @param mixed $value Metadata value.
* @param mixed $prev_value Optional. Previous value to check before removing.
* @return bool False on failure, true if success.
*/
function update_term_meta($term_id, $meta_key, $meta_value, $prev_value = '') {
return update_metadata('term', $term_id, $meta_key, $meta_value, $prev_value);
}
endif;
if ( !function_exists( 'get_term_custom' ) ) :
/**
* Retrieve term meta fields, based on post ID.
*
* The term meta fields are retrieved from the cache, so the function is
* optimized to be called more than once. It also applies to the functions, that
* use this function.
*
* @param int $term_id term ID
* @return array
*/
function get_term_custom($term_id = 0) {
$term_id = (int) $term_id;
if ( ! wp_cache_get($term_id, 'term_meta') )
update_termmeta_cache($term_id);
return wp_cache_get($term_id, 'term_meta');
}
endif;
if ( !function_exists( 'update_termmeta_cache' ) ) :
/**
* Updates metadata cache for list of term_ids.
*
* Performs SQL query to retrieve the metadata for the term_idss and updates the
* metadata cache for the terms. Therefore, the functions, which call this
* function, do not need to perform SQL queries on their own.
*
* @param array $term_ids List of term_idss.
* @return bool|array Returns false if there is nothing to update or an array of metadata.
*/
function update_termmeta_cache($term_ids) {
return update_meta_cache('term', $term_ids);
}
endif;
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 ( $reutrn = 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_get_breadcrumb_tree() {
}
function hm_breadcrumbs() {
}
// Multiple meta value hackkks
add_filter( 'posts_join_paged', 'hm_add_multiple_meta_joins_to_wp_query', 10, 2 );
function hm_add_multiple_meta_joins_to_wp_query( $join, $wp_query ) {
global $wpdb;
if ( !is_array( $wp_query->query_vars['meta_key'] ) || count( $wp_query->query_vars['meta_key'] ) == 1 || empty( $wp_query->query_vars['meta_value'] ) )
return $join;
$meta_value_count = count( $wp_query->query_vars['meta_key'] ) - 1;
for( $i = 0; $i < $meta_value_count; $i++ ) {
$join .= " JOIN $wpdb->postmeta as hm_extra_meta_{$i} ON $wpdb->posts.ID = hm_extra_meta_{$i}.post_id";
}
return $join;
}
function hm_add_multiple_meta_to_wp_query( $where, $wp_query ) {
global $wpdb;
if ( !is_array( $wp_query->query_vars['meta_key'] ) )
return $where;