-
Notifications
You must be signed in to change notification settings - Fork 10
/
functions.php
1953 lines (1627 loc) · 64.5 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
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
/*
* Institut Français functions
*
* Management system of categories, posts & pages
* The concept is bsed on the facts that every pages are posts and menu items are categories. And are all related to an antenna (top level category, the ones with no parent)
* WP pages are used only for general purpose and are common for all antenna.
*
* An antenna is a user (role antenna) associated with a top level category.
*
* First thing to do is to create a top level category nammed by the city you want (an Institut Français antenna)
* Then assign this top level category to the user of your choice. If you only have one, means it's the admin, so assign-it to the admin.
*
* et voilà.
*
* for more information contact anou(at)smol(dot)org
*/
function if_init() {
if (!is_admin()) {
/*
wp_deregister_script('jquery');
wp_register_script('jquery', ("//ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"), false);
wp_enqueue_script('jquery');
*/
wp_enqueue_script('masonry', get_bloginfo('stylesheet_directory') . '/js/jquery.masonry.min.js', array('jquery'));
wp_enqueue_script('moment', get_bloginfo('stylesheet_directory') . '/js/moment-with-locales.js', array('jquery'));
//RTL languages
if ( is_rtl() ) {
wp_enqueue_script( 'slides-rtl', get_bloginfo('stylesheet_directory') . '/js/slides.jquery.rtl.js', array('jquery'));
} else {
wp_enqueue_script('slides', get_bloginfo('stylesheet_directory') . '/js/slides.jquery.js', array('jquery'));
}
}
}
add_action('init', 'if_init');
//activate auto-update
//add_filter( 'auto_update_theme', '__return_true' );
/////////////////ERROR MESSAGE IF NO CATEG FOR USER ////////////////
add_action( 'admin_notices', 'iftheme_categtouser_error_notice' );
function iftheme_categtouser_error_notice($raw = false){
global $current_screen;
global $current_user;
$current_user = wp_get_current_user();
$usercat = get_cat_if_user($current_user->ID);
if ( $current_screen->base == 'appearance_page_theme_options' && !$usercat ) {
if(!$raw) {
echo '<div class="error"><p>';
printf( __('Warning - You must assign a category to the current user <a href="%2$s/wp-admin/users.php"><b>%1$s</b>!</a>', 'iftheme'), $current_user->data->display_name, get_bloginfo('wpurl') );
echo '</p></div>';
}
return 'user-categ-error';
}
}
/**
* Tell WordPress to run iftheme_setup() when the 'after_setup_theme' hook is run.
*/
add_action( 'after_setup_theme', 'iftheme_setup' );
if ( ! function_exists( 'iftheme_setup' ) ):
/**
* Sets up theme defaults and registers support for various WordPress features.
*
* Note that this function is hooked into the after_setup_theme hook, which runs
* before the init hook. The init hook is too late for some features, such as indicating
* support post thumbnails.
*
* To override iftheme_setup() in a child theme, add your own iftheme_setup to your child theme's
* functions.php file.
*
* @uses load_theme_textdomain() For translation/localization support.
* @uses add_editor_style() To style the visual editor.
* @uses add_theme_support() To add support for post thumbnails, automatic feed links, and Post Formats.
* @uses register_nav_menus() To add support for navigation menus.
* @uses add_custom_background() To add support for a custom background.
* @uses add_custom_image_header() To add support for a custom header.
* @uses register_default_headers() To register the default custom header images provided with the theme.
* @uses set_post_thumbnail_size() To set a custom post thumbnail size.
*
*/
function iftheme_setup() {
/* Make Institut Français available for translation.
* Translations can be added to the /languages/ directory.
*/
load_theme_textdomain( 'iftheme', get_template_directory() . '/languages' );
// Load up our theme options page and related code.
require( get_template_directory() . '/inc/theme-options.php' );
// Load up our post meta box .
require( get_template_directory() . '/inc/events/if-events.php' );
//include user-role antenna file
require_once( get_template_directory() . "/inc/if-user.php");
//include if widgets
require_once( get_template_directory() . "/inc/widgets/if-world-widget.php");
require_once( get_template_directory() . "/inc/widgets/if-categ-widget.php");
require_once( get_template_directory() . "/inc/widgets/if-partners-widget.php");
require_once( get_template_directory() . "/inc/widgets/if-antennas-widget.php");
require_once( get_template_directory() . "/inc/widgets/if-mobile-widget.php");
require_once( get_template_directory() . "/inc/widgets/if-nopadding-widget.php");
// require_once( get_template_directory() . "/inc/widgets/calendar/if-calendar-widget.php"); @TODO: dev the widget with new class calendar
//include antenna categories widget
require_once( get_template_directory() . "/inc/editor-styles/editor-styles.php");
//include the main classes file
// for category and post customization
require_once( get_template_directory() . "/inc/theme-classes.php");
require_once( get_template_directory() . "/inc/theme-taxo-classes.php");
}
endif; // iftheme_setup
/*
* MENU REGISTRATION
*/
// Register menus
/*
function iftheme_menu_register() {
register_nav_menus(
array(
'header-menu' => __('Header Menu','iftheme'),
'sidebar-menu' => __('Sidebar Menu','iftheme'),
'footer-menu' => __('Footer Menu','iftheme'),
)
);
}
add_action( 'init', 'iftheme_menu_register' );
*/
/*
* CATEGORIES
*/
//fix for categories
define('IF_CATEGORY_FIX_FIELDS', 'my_category_fields_option');
// your fields (the form)
function if_fix_category_fields($tag) {
global $current_user;
$current_user = wp_get_current_user();
$tag_extra_fields = get_option(IF_CATEGORY_FIX_FIELDS); ?>
<table class="form-table">
<tr class="form-field">
<td><input name="if_fix_field" id="if_fix_field" type="hidden" aria-required="false" value="<?php echo $current_user->ID; ?>" /></td>
</tr>
</table>
<?php
}
add_filter('edit_category_form', 'if_fix_category_fields');
// when the form gets submitted, and the category gets updated (in your case the option will get updated with the values of your custom fields above
function update_my_category_fields($term_id) {
if($_POST['taxonomy'] == 'category'):
$tag_extra_fields = get_option(IF_CATEGORY_FIX_FIELDS);
$tag_extra_fields[$term_id]['if_fix_field'] = strip_tags($_POST['if_fix_field']);
update_option(IF_CATEGORY_FIX_FIELDS, $tag_extra_fields);
endif;
}
add_filter('edited_terms', 'update_my_category_fields');
// when a category is removed
function remove_my_category_fields($term_id) {
if($_POST['taxonomy'] == 'category'):
$tag_extra_fields = get_option(IF_CATEGORY_FIX_FIELDS);
unset($tag_extra_fields[$term_id]);
update_option(IF_CATEGORY_FIX_FIELDS, $tag_extra_fields);
endif;
}
add_filter('deleted_term_taxonomy', 'remove_my_category_fields');
//get site language in short code
function get_site_lang() {
//to realize if the actual php version ist newer than '5.3'
if (strnatcmp(phpversion(),'5.3.0') >= 0) {
$lg = strstr(get_bloginfo('language'), '-', TRUE);
}
else {
$haystack = get_bloginfo('language');
$needle = '-';
$result = substr($haystack, 0, strpos($haystack, $needle)); // $result = php
}
$lg = $lg ? $lg : 'fr'; //if nothing is returned best to return at least 'fr'. We're assuming that default language is french...
return $lg;
}
//get level 1 (key=0) categories.
function get_if_top_categ( $args = array() ) {
$default_args = array(
'hide_empty' => 0,
'use_desc_for_title' => 0,
'title_li' => '',
'child_of' => 0,
'depth' => 1,
'hierarchical' => true,
);
if( !empty($args) && is_array($args) ) {
$default_args = array_merge($default_args,$args);
}
wp_list_categories($default_args);
}
/**
* get level 2 (key=1) categories.
*/
function get_if_level2_categ($raw = false, $args = array()) {
$default_args = array(
'taxonomy' => 'category',
'hide_empty' => 0,
'use_desc_for_title' => 0,
'title_li' => '',
'child_of' => get_current_parent_categ(),
'depth' => 2,
'echo' => 0
);
if(!empty($args) && is_array($args)) {
$default_args = array_merge($default_args,$args);
}
if (!$raw) {
return wp_list_categories($default_args);
}
else {
$default_args = array(
'taxonomy' => 'category',
'hide_empty' => 0,
'parent' => get_current_parent_categ(),
);
return apply_filters('iftheme_nav', get_terms( $default_args));
}
}
function str_lreplace($search, $replace, $subject) {
return preg_replace('~(.*)' . preg_quote($search, '~') . '~', '$1' . $replace, $subject, 1);
}
//add css class to categ listing
/*
function add_markup_categories($output) {
$patern = '/cat-item/';
$output = preg_replace($patern, ' first-cat-item cat-item', $output, 1);
$output = substr_replace($output, " last-cat-item cat-item", strripos($output, "cat-item"), strlen("cat-item"));
return $output;
}
add_filter('wp_list_categories', 'add_markup_categories');
*/
//current categ on single
function if_show_current_cat_on($output) {
global $post;
if( is_single() ) {
$categories = wp_get_post_categories($post->ID);
foreach( $categories as $catid ) {
$cat = get_category($catid);
$pcat = get_ifcat_parents($catid);
// Find cat-item-ID in the string
if(preg_match('#cat-item-' . $cat->cat_ID . '"#', $output)) {
$output = str_replace('cat-item-'.$cat->cat_ID, 'cat-item-'.$cat->cat_ID . ' current-cat', $output);
}
if(in_array($cat->parent, $pcat)){
foreach ($pcat as $k => $pcid) {
if(preg_match('#cat-item-' . $pcid . '"#', $output)) {
$output = str_replace('cat-item-'.$pcid, 'cat-item-'.$pcid . ' current-cat-parent', $output);
}
}
}
}
/*
if(preg_match('#cat-item-' . $cat->parent . '"#', $output)) {
$output = str_replace('cat-item-'.$cat->parent, 'cat-item-'.$cat->parent . ' current-cat-parent', $output);
}
*/
}
if (is_category()){
$cid = get_query_var('cat');
$tabcat = get_ifcat_parents($cid);
foreach ($tabcat as $k => $pcid) {
if(preg_match('#cat-item-' . $pcid . '"#', $output)) {
$output = str_replace('cat-item-'.$pcid, 'cat-item-'.$pcid . ' current-cat-parent', $output);
}
}
}
return $output;
}
add_filter('wp_list_categories', 'if_show_current_cat_on');
function get_ifcat_parents($cid){
$pcatz = get_category_parents($cid,false,'_/',true);
$pcatz = explode('_/', $pcatz);
foreach ($pcatz as $k => $slug) {
$pcato = get_category_by_slug($slug);
if($pcato) {
//$tabcat[] = $pcato; //get all cat object
$tabcat[] = $pcato->term_id; //get cat ID
}
}
return $tabcat;
}
//Get categ slug
function get_cat_slug($cat_id) {
$cat_id = (int) $cat_id;
remove_all_filters('get_term');
$category = get_category($cat_id);
if(is_object($category))
return property_exists($category,'slug') ? $category->slug : '';
}
//Prepare vars for IF category vs antenna system
function get_cat_if_user($uid){
$categ_id = false;
$user = get_user_meta( $uid );
//must have assigned a category to user (cf. edit profil page)
if( isset($user['categ_to_antenna']) ) $categ_id = $user['categ_to_antenna'][0];
//returns an categ ID
return $categ_id;
}
/*
* antenna's ID depending on language
*/
function get_cat_if_user_lang($uid){
$categ = 0;
$user = get_user_meta( $uid );
//must have assigned a category to user (cf. edit profil page)
if(isset($user['categ_to_antenna'])) $categ = $user['categ_to_antenna'][0];
$categ = array_key_exists( 'wpml_object_id' , $GLOBALS['wp_filter'] ) ? apply_filters( 'wpml_object_id', $categ, 'category', false, ICL_LANGUAGE_CODE) : $categ;
//returns an ID (dependant on language if any)
return $categ;
}
//Get info for IF Antenna
//only useful for theme option page (must be logged)
function get_antenna(){
global $current_user;
$current_user = wp_get_current_user();
$antenna = get_cat_slug(get_cat_if_user($current_user->ID));
return $antenna;
}
//Get antenna users
function get_antenna_users() {
$ua = get_users('role=antenna');
//administrator (user 1) is always an antenna
$admin = get_users('include=1');
$ua[] = $admin[0];
return $ua;
}
//Verify if it's a Multi antennas site
function multi_antennas() {
$multi = false;
$nb = count(get_antenna_users());
//if count(get_antenna_users()) == 1, than it means that we have only 1 antenna, the administrator one.
if( $nb > 1 ) $multi = true;
return $multi;
}
//Get the antennas details & options settings
function get_antennas_details(){
//get all antenna users
$users = get_antenna_users();
//count the number of user
$nb = count($users);
if($nb === 1) {//if only 1 user, we assume that it's the admin user so $user->ID = 1
$categ_admin = get_cat_if_user(1);//@todo: possibility to select who is the admin.
$antenna = get_cat_slug($categ_admin);
$options = get_option('iftheme_theme_options_' . $antenna, iftheme_get_default_theme_options() );//cf. theme-options.php for keys of the option array
//adding useful infos to $options
$options['aid'] = $categ_admin;
$options['slug'] = $antenna;
} else {//more than 1 user
foreach($users as $k => $o){
$categ = get_cat_if_user($o->ID);
$antenna = get_cat_slug($categ) ? get_cat_slug($categ) : __('You must assign a category to this user : ','iftheme').$o->display_name;
$options[$categ] = get_option('iftheme_theme_options_' . $antenna, iftheme_get_default_theme_options() );//cf. theme-options.php for keys of the option array
//unset country options for non admin user
//@todo: posibility to have multiple admin. Maybe check user's roles more then his ID
if($o->ID != 1) {
unset($options[$categ]['bg_frame_country']);
unset($options[$categ]['background_img_country']);
unset($options[$categ]['theme_home_categ_country']);
}
//adding useful infos to $options
$options[$categ]['aid'] = !$categ ? null : $categ;
$options[$categ]['slug'] = $antenna;
}
}
return $options;
}
function test(){
$cats = get_the_category();
return get_root_category(12);
return get_the_category();
}
// Add specific CSS class by filter
function iftheme_body_class($classes) {
$cid = get_current_parent_categ();
$class = 'category-'. $cid .' black';
if (is_home()) $class .= ' accueil';
// add $class to the $classes array
$classes[] = $class;
// return the $classes array
return $classes;
}
add_filter('body_class','iftheme_body_class');
//style for the wysiwyg
add_editor_style('style.css');
/**
* get top level categories
*/
function get_root_category($category_id) {
$rootID = false; //OR 1 ? > maybe admin categ ?
//returns cat's name
//$parent_cats = get_category_parents($category_id);
//returns cat's slug
$parent_cats = get_category_parents($category_id, false, '/', true);
if( !is_object($parent_cats) ) {
$split_arr = explode('/', $parent_cats);
//$return = get_cat_id($split_arr[0]);
$catObj = get_category_by_slug($split_arr[0]);
$rootID = $catObj->term_id;
}
return $rootID;
}
//function to get depth category
function get_level($cid, $level = 0) {
$max_depth_to_test = intval(9); //set this to highest level you might have
$last_depth = 0; //top level
$cat = get_category($cid);
if ($cat->category_parent == 0) {
return $level;
} else {
for ( $i = 1; $i <= $max_depth_to_test; $i += 1) {
if ($cat->category_parent) {
$cat = get_category($cat->category_parent);
$last_depth = $i;
}
}
//$last_depth +=1;
$level = $last_depth;
}
return $level;
}
//get current top level category/antenna
function get_current_antenna(){
global $sitepress;
$default_lg = isset($sitepress) ? $sitepress->get_default_language() : get_site_lang();
$categ_admin = get_cat_if_user(1) != 0 ? get_cat_if_user(1) : 1;
$current_id = array_key_exists( 'wpml_object_id' , $GLOBALS['wp_filter'] ) ? apply_filters( 'wpml_object_id', $categ_admin, 'category', true, $default_lg) : $categ_admin;//default category
if( is_category() ) {
//get root category (antenna)
$current_id = array_key_exists( 'wpml_object_id' , $GLOBALS['wp_filter'] ) ? apply_filters( 'wpml_object_id', get_root_category(get_query_var('cat')), 'category', true, $default_lg) : get_root_category(get_query_var('cat'));
}
elseif( is_single() ) {
//get the category id of post
$cats = get_the_category();
//return default categ if none found
if( empty($cats) ) return $current_id;
//get root category (antenna)
//if post has multiple categories, no problem we only need to get the root categ.
$current_id = array_key_exists( 'wpml_object_id' , $GLOBALS['wp_filter'] ) ? apply_filters( 'wpml_object_id', get_root_category($cats[0]->term_id), 'category', true, $default_lg) : get_root_category($cats[0]->term_id);
}
return $current_id;
}
/**
* get current top level category
*/
function get_current_parent_categ(){
global $sitepress;
$parent_id = 0;
$default_lg = isset($sitepress) ? $sitepress->get_default_language() : 'fr';//assuming that 'fr' should be default language
$check_top_categ = get_terms( 'category', 'parent=0&hide_empty=0' );
//default is category (or translation) from admin (user 1)
$categ_admin = get_cat_if_user(1) != 0 ? get_cat_if_user(1) : 1;
$current_id = array_key_exists( 'wpml_object_id' , $GLOBALS['wp_filter'] ) ? apply_filters( 'wpml_object_id', $categ_admin, 'category', true, $default_lg ) : $categ_admin;//default category
if(is_category()) {
//get root category (antenna)
$current_id = get_root_category(get_query_var('cat'));
//$current_id = function_exists('icl_object_id') ? icl_object_id($current_id, 'category', true, $default_lg) : $current_id;
} elseif( is_single() || is_search() ) {
//get the category id of post
$cats = get_the_category();
if( empty($cats) ) return $current_id;
// if( empty($cats) ) return 'front';
//get root category (antenna)
//if post has multiple categories, no problem we only need to get the root categ.
$current_id = get_root_category($cats[0]->term_id);
}
return $current_id;
}
/**
* for futur tests
*/
function get_current_parent_categ2(){
//firstly, load data for your child category
$child = get_category(31);
//from your child category, grab parent ID
$parent = $child->parent;
/*
//load object for parent category
$parent_name = get_category($parent);
//grab a category name
$parent_name = $parent_name->name;
*/
}
/*
* get meta data from category
*/
function get_categ_data( $cid ){
$data['img'] = get_term_meta($cid,'categ_img');
$data['children'] = get_term_meta($cid,'categ_children');
$data['posts'] = get_term_meta($cid,'categ_posts');
return $data;
}
//get slug in default language -- OBSOLETE --- use get_cat_slug() !!!
function get_category_slug($id) {
global $wpdb;
$term_id = $wpdb->get_var("SELECT term_id FROM {$wpdb->prefix}term_taxonomy WHERE term_taxonomy_id = {$id}");
if ($term_id) {
return $wpdb->get_var("SELECT slug FROM {$wpdb->prefix}terms WHERE term_id = {$term_id}");
} else {
return null;
}
}
/**
* Tests if any of a post's assigned categories are descendants of target categories
*
* @param int|array $cats The target categories. Integer ID or array of integer IDs
* @param int|object $_post The post. Omit to test the current post in the Loop or main query
* @return bool True if at least 1 of the post's categories is a descendant of any of the target categories
* @see get_term_by() You can get a category by name or slug, then pass ID to this function
* @uses get_term_children() Passes $cats
* @uses in_category() Passes $_post (can be empty)
* @version 2.7
* @link http://codex.wordpress.org/Function_Reference/in_category#Testing_if_a_post_is_in_a_descendant_category
*/
if ( ! function_exists( 'post_is_in_descendant_category' ) ) {
function post_is_in_descendant_category( $cats, $_post = null ) {
foreach ( (array) $cats as $cat ) {
// get_term_children() accepts integer ID only
$descendants = get_term_children( (int) $cat, 'category' );
if ( $descendants && in_category( $descendants, $_post ) )
return true;
}
return false;
}
}
/*
* ADMIN
*/
//Add css&js files to admin
function load_custom_wp_admin_style(){
//font-awesome
wp_enqueue_style( 'font_awesome', get_stylesheet_directory_uri() . '/fonts/font-awesome/css/font-awesome.min.css', array(), '4.7' );
wp_register_style( 'custom_wp_admin_css', get_bloginfo('stylesheet_directory') . '/inc/if-admin-style.css', false, '1.0.0' );
wp_enqueue_style( 'custom_wp_admin_css' );
wp_register_script( 'custom_wp_admin_js', get_bloginfo('stylesheet_directory') . '/inc/if-admin-script.js', false, '1.0.0' );
$test_user_categ = iftheme_categtouser_error_notice(true);
if($test_user_categ){
$params = array('id' => 'submit');
wp_localize_script( 'custom_wp_admin_js', 'ifAdmin', $params );
}
wp_enqueue_script( 'custom_wp_admin_js' );
}
add_action('admin_enqueue_scripts', 'load_custom_wp_admin_style');
//Add inline js to admin
function load_inline_js_to_admin(){
echo '<script type="text/javascript"> var templateDir = "'.get_template_directory_uri().'"</script>';
}
add_action('admin_head', 'load_inline_js_to_admin');
//Add inline css to admin for displaying right widget sidebar zone to users
function load_inline_css_to_admin(){
global $current_user;
$current_user = wp_get_current_user();
$out = '<style type="text/css">';
$out .= '#widgets-right .widgets-holder-wrap {display:none}';
$out .= $current_user->ID == 1 ? '#widgets-right .widgets-holder-wrap {display:block}':'#widgets-right .widgets-holder-wrap.sidebar-'.get_cat_if_user($current_user->ID).' {display:block}';
$out .= '</style>';
echo $out;
}
add_action('admin_head', 'load_inline_css_to_admin');
//Add theme JS & CSS
function if_scripts() {
//font-awesome
wp_enqueue_style( 'font_awesome_front', get_stylesheet_directory_uri() . '/fonts/font-awesome/css/font-awesome.min.css', array(), '4.6.1' );
wp_enqueue_script("jquery");
wp_enqueue_script('chosen', get_template_directory_uri() . '/js/chosen/chosen.jquery.js');
wp_register_style( 'chosen_css', get_bloginfo('stylesheet_directory') . '/js/chosen/chosen.css', false, '1.0.0' );
wp_enqueue_style( 'chosen_css' );
$script = is_rtl() ? 'if-script-rtl' : 'if-script';
wp_enqueue_script($script, get_template_directory_uri() . '/js/'. $script .'.js', array('jquery'));
$varForJS = array(
'select_txt' => '-- ' . __('Select' , 'iftheme') . ' --',
);
wp_localize_script( $script, 'ifvarJS', $varForJS );
wp_enqueue_script('if-ajax', get_template_directory_uri() . '/inc/calendar/ajax.js');
//custom CSS. @todo: add settings for this file to be loaded/included
wp_register_style( 'custom_css', get_bloginfo('stylesheet_directory') . '/css/custom.css' );
if( file_exists(get_template_directory() . '/css/custom.css') ) wp_enqueue_style( 'custom_css' );
}
add_action('wp_enqueue_scripts', 'if_scripts');
/**
* columns for posts (if events)
*/
function if_manage_post_columns( $columns ) {
//hide default post date
unset($columns['date']);
//hide tags column
unset($columns['tags']);
//unset($columns['icl_translations']);
//add our custom start and end dates to posts admin lists
$columns['if_startdate'] = __('Start Date','iftheme');
$columns['if_enddate'] = __('End Date','iftheme');
$columns['date'] = __('Date');
return $columns;
}
/**
* columns for categories
*/
function if_manage_categ_columns( $columns ) {
//hide categ description column
unset($columns['description']);
//add categ image column
$columns['categ_image'] = __('Image','iftheme');
return $columns;
}
/**
* Custom column for posts (if events)
*/
function manage_post_custom_fields( $column_name, $post_id ) {
switch ( $column_name ) {
case 'if_startdate':
$stardate = get_post_meta( $post_id, 'if_events_startdate', true );
echo $stardate ? date_i18n( get_option( 'date_format' ), $stardate ) : '';
break;
case 'if_enddate':
$enddate = get_post_meta( $post_id, 'if_events_enddate', true );
echo $enddate ? date_i18n( get_option( 'date_format' ), $enddate ) : '';
break;
}
}
add_action('manage_posts_custom_column','manage_post_custom_fields',10,2);
/**
* makes custom columns for posts sortable
*/
function sortable_if_dates_column( $columns ) {
$columns['if_startdate'] = 'startdate';
$columns['if_enddate'] = 'enddate';
//To make a column 'un-sortable' remove it from the array
//unset($columns['date']);
return $columns;
}
add_filter( 'manage_edit-post_sortable_columns', 'sortable_if_dates_column' );
/**
* Tell WP on what metakey sort IF Dates columns
*/
function if_dates_orderby( $query ) {
if( !is_admin() )
return;
$orderby = $query->get( 'orderby');
if( 'startdate' == $orderby ) {
$query->set('meta_key','if_events_startdate');
$query->set('orderby','meta_value_num');
}
if( 'enddate' == $orderby ) {
$query->set('meta_key','if_events_enddate');
$query->set('orderby','meta_value_num');
}
}
add_action( 'pre_get_posts', 'if_dates_orderby' );
/**
* Custom column for categories
*/
function manage_category_custom_fields( $val, $column_name, $term_id ) {
if ($column_name == 'categ_image') {
$cat_data = get_term_meta($term_id,'categ_img');
//array key : id,src
if(isset($cat_data['src'])) echo '<img src="'.$cat_data['src'].'" alt="'.get_cat_name($term_id).'" width="50" />';
}
}
add_action('manage_category_custom_column','manage_category_custom_fields',10,3);
/**
* Show sticky posts in posts columns
*/
/*
function display_posts_stickiness( $column, $post_id ) {
if ($column == 'sticky'){
echo '<input type="checkbox" disabled', ( is_sticky( $post_id ) ? ' checked' : ''), '/>';
}
}
add_action( 'manage_posts_custom_column', 'display_posts_stickiness', 10, 2 );
*/
/* Add custom column to post list */
/*
function add_sticky_column( $columns ) {
return array_merge( $columns, array( 'sticky' => __( 'Sticky', 'iftheme' ) ) );
}
add_filter( 'manage_posts_columns', 'add_sticky_column' );
*/
function if_restrict_categories( $categories ) {
global $current_user;
$current_user = wp_get_current_user();
$a = get_cat_if_user_lang($current_user->ID);
$onPostPage = (strpos($_SERVER['PHP_SELF'], 'edit-tags.php'));
if (is_admin() && $onPostPage && !current_user_can('level_10')) {
$size = count($categories);
for ($i = 0; $i < $size; $i++) {
if(is_object($categories[$i])){
if( !cat_is_ancestor_of($a, $categories[$i]->term_id) && $categories[$i]->term_id != $a){
unset($categories[$i]);
}
}
}
}
return $categories;
}
add_filter('get_terms', 'if_restrict_categories');
function if_column_init() {
add_filter( 'manage_post_posts_columns' , 'if_manage_post_columns', 200 );
add_filter( 'manage_edit-category_columns' , 'if_manage_categ_columns' );
}
add_action( 'admin_init' , 'if_column_init' );
//remove some meta box from admin post edit page
function if_remove_meta_boxes() {
//remove_meta_box( 'submitdiv', 'post', 'normal' ); // Publish meta box
remove_meta_box( 'commentsdiv', 'post', 'normal' ); // Comments meta box
remove_meta_box( 'revisionsdiv', 'post', 'normal' ); // Revisions meta box
//remove_meta_box( 'authordiv', 'post', 'normal' ); // Author meta box
//remove_meta_box( 'slugdiv', 'post', 'normal' ); // Slug meta box
remove_meta_box( 'tagsdiv-post_tag', 'post', 'side' ); // Post tags meta box
//remove_meta_box( 'categorydiv', 'post', 'side' ); // Category meta box
//remove_meta_box( 'postexcerpt', 'post', 'normal' ); // Excerpt meta box
//remove_meta_box( 'formatdiv', 'post', 'normal' ); // Post format meta box
//remove_meta_box( 'trackbacksdiv', 'post', 'normal' ); // Trackbacks meta box
//remove_meta_box( 'postcustom', 'post', 'normal' ); // Custom fields meta box
remove_meta_box( 'commentstatusdiv', 'post', 'normal' ); // Comment status meta box
//remove_meta_box( 'postimagediv', 'post', 'side' ); // Featured image meta box
//remove_meta_box( 'pageparentdiv', 'page', 'side' ); // Page attributes meta box
}
add_action( 'admin_menu', 'if_remove_meta_boxes' );
/*
* POSTS
*/
/**
* Alter main query to be aware of our meta field "if_events_startdate"
* order by startdate and special treatment for end date if start date allready passed
*
*/
function if_display_posts_listing ( $query ) {
if( $query->is_main_query() && is_category() && !is_admin() ) {
$time = ( current_time( 'timestamp' ) - (60*60*24) );
$compare = '>=';
$meta_query =
array(
array(
'key' => 'if_events_enddate',
'value' => $time,
'compare' => $compare,
'type' => 'numeric'
),
);
$query->set( 'meta_query', $meta_query );
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'meta_key', 'if_events_startdate' );
$query->set( 'order', 'DESC' );
$query->set( 'post_type', array( 'post', 'course' ) );
}
}
//add_action( 'pre_get_posts', 'if_display_posts_listing' );
/**
* Alter main query for archive pages
* to be aware of our meta field "if_events_startdate"
*/
function if_display_posts_on_archive_pages( $query ) {
//if( $query->is_main_query() && isset($query->query['year']) && !is_admin() ) {
if( $query->is_main_query() && $query->is_archive && !is_admin() && !$query->is_category) {
$year = $query->query['year'];
$month = isset($query->query['monthnum']) ? $query->query['monthnum'] : null;
$day = isset($query->query['day']) ? $query->query['day'] : null;
if($query->is_year){
$value = array(mktime(0, 0, 0, 01, 01, $year), mktime(23, 59, 59, 12, 31, $year));
$compare = 'BETWEEN';
} else if($query->is_month){
$next_month = sprintf("%02d",$month+1);
$value = array(mktime(0, 0, 0, $month, 01, $year), mktime(0, 0, 0, $next_month, 01, $year));
$compare = 'BETWEEN';
} else if($query->is_day){
$value = mktime(0, 0, 0, $month, $day, $year);
$compare = '<=';
$compare2 = '>=';
}
$meta_query[] =
array(
'key' => 'if_events_startdate',
'value' => $value,
'compare' => $compare,
);
if($query->is_month){
$meta_query[] =
array(
'key' => 'if_events_enddate',
'value' => $value,
'compare' => $compare,
);
$meta_query['relation'] = 'OR';
}
if($query->is_day){
$meta_query[] =
array(
'key' => 'if_events_enddate',
'value' => $value,
'compare' => $compare2
);
$meta_query['relation'] = 'AND';
}
$query->set( 'year','' );
$query->set( 'monthnum', '' );
$query->set( 'day','' );
if(!$query->is_month){
$query->set( 'meta_key', 'if_events_startdate' );
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'ASC' );
}
$query->set( 'posts_per_page', '10' );
$query->set( 'meta_query', $meta_query );
}
}
add_action( 'pre_get_posts', 'if_display_posts_on_archive_pages' );
//get meta data of post for display on page
function get_meta_if_post( $pid = '', $archive = false ){
global $post;
$pid = !$pid ? $post->ID : $pid;
$data['post_id'] = $pid;//for ref.
$type = get_post_type( $pid );
//get prefix
$prefix = apply_filters('event_prefix', 'if_events' );
setlocale(LC_ALL, get_locale());
$meta = get_post_meta($pid);
$post_categs = wp_get_post_categories($pid);
foreach( $post_categs as $cid ) {
$top_categ = get_root_category($cid);//getting top category
}
$data['antenna_id'] = $top_categ;
$start = isset($meta[$prefix . '_startdate']) ? $meta[$prefix . '_startdate'][0] : null;
$end = isset($meta[$prefix . '_enddate']) ? $meta[$prefix . '_enddate'][0] : null;
$date_format = $archive ? '%d %b %Y' : '%d %b';
$end = $end <= $start ? false : $end;
$date_format = !$end || ( strftime('%Y',$end) == strftime('%Y',$start) ) ? $date_format : '%d %b';
$data['start'] = !empty($start) ? utf8_encode(strftime($date_format,$start)) : null;
$end = !empty($end) ? utf8_encode(strftime($date_format,$end)) : null;
$time = isset($meta[$prefix . '_time']) ? $meta[$prefix . '_time'][0] : null;
$data['end'] = !$end ? (strlen($time) ? ' / '.$time : '') : ' / '.$end;