-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
executable file
·1587 lines (1234 loc) · 44.3 KB
/
functions.php
File metadata and controls
executable file
·1587 lines (1234 loc) · 44.3 KB
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
//---------------------------------------------------------------------------------
// Development Mode On / Off
//---------------------------------------------------------------------------------
if ( ( defined( 'WP_LOCAL_DEV' ) && WP_LOCAL_DEV ) || strpos( $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], '.dev' ) !== false ) {
add_action( 'houston_after_footer', function () {
$file = 'http://localhost:35729/livereload.js';
$file_headers = @get_headers( $file );
if ( $file_headers ) {
wp_enqueue_script( 'livereload', 'http://localhost:35729/livereload.js', '', '', true );
}
} );
}
//---------------------------------------------------------------------------------
// Load module with attribute passing
//---------------------------------------------------------------------------------
function load_module( $file, $attributes = false ) {
if ( $attributes ) {
/** @noinspection PhpParamsInspection */
/** @noinspection PhpParamsInspection */
/** @noinspection PhpParamsInspection */
/** @noinspection PhpParamsInspection */
/** @noinspection PhpParamsInspection */
/** @noinspection PhpParamsInspection */
extract( $attributes, EXTR_PREFIX_SAME, "module" );
}
include( locate_template( $file ) );
}
//---------------------------------------------------------------------------------
// Install plugins and add update functions for plugins & theme
//---------------------------------------------------------------------------------
include_once get_template_directory() . '/_framework/lib/TGM-Plugin-Activation/wally-plugin-activation.php';
//Initialize the update checker.
require get_template_directory() . '/_framework/lib/theme-update-checker.php';
new ThemeUpdateChecker(
'wally-theme-master',
'https://raw.githubusercontent.com/raket/wally-theme/master/meta.json'
);
//---------------------------------------------------------------------------------
// Require Files and load Wally
//---------------------------------------------------------------------------------
add_action( 'after_setup_theme', '_w_check_plugin_nopriv' );
function _w_check_plugin_nopriv() {
if ( ! class_exists( 'wally' ) && ! is_admin() && is_user_logged_in() ) {
//echo file_get_contents(get_stylesheet_directory() . '/install-plugin.php');
load_template( get_template_directory() . '/install-plugin.php' );
die;
//die(__('Var god aktivera Wally-tilläget.', 'wally-theme'));
}
}
//----------------------------------------------------
// Enqueue JS & CSS
//----------------------------------------------------
add_action( 'admin_init', '_w_check_plugin' );
function _w_check_plugin() {
if ( ! class_exists( 'Wally' ) && is_admin() ) {
add_action( 'admin_notices', function () {
$class = "update-nag";
$message = sprintf( __( 'Wally-tillägget är för närvarande inte aktiverat. <a href="%s">Klicka här</a> för att aktivera tillägget.', 'wally-theme' ), 'plugins.php' );
if ( get_current_screen()->base !== 'plugins' ) {
echo "<div class=\"$class\"><p>$message</p></div>";
}
} );
}
}
add_action( 'wp_enqueue_scripts', function () {
wp_enqueue_style( 'wally', get_template_directory_uri() . '/assets/css/app.css', array( 'fw-ext-builder-frontend-grid' ) );
if ( isset( $_COOKIE['wally_contrast'] ) ) {
wp_enqueue_style( 'wally_contrast', get_template_directory_uri() . '/assets/css/themes/contrast.css' );
add_filter( 'body_class', function ( $classes ) {
$classes[] = 'theme-contrast';
return $classes;
} );
} else {
$theme = fw_get_db_customizer_option( 'color_theme' );
wp_enqueue_style( 'wally_theme', get_template_directory_uri() . '/assets/css/themes/' . $theme . '.css' );
}
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'bower', get_template_directory_uri() . '/assets/bower/_bower.js', false, false, true );
wp_enqueue_script( 'wally', get_template_directory_uri() . '/assets/js/build/all.js', array( 'bower' ), false, true );
if ( is_user_logged_in() ) {
wp_enqueue_script( 'tota11y', 'https://cdnjs.cloudflare.com/ajax/libs/tota11y/0.1.3/tota11y.js', false, false, true );
}
wp_localize_script( 'wally', 'ajax', array(
'url' => admin_url( 'admin-ajax.php' )
) );
} );
add_action( 'after_setup_theme', '_w_init', 9 );
function _w_init() {
// Configure theme support
add_theme_support( 'html5', array(
'comment-list',
'comment-form',
'search-form',
'gallery',
'caption'
) );
add_theme_support( 'menus' );
add_theme_support( 'automatic-feed-links' );
add_theme_support( 'phpquery' );
if ( current_theme_supports( 'phpquery' ) ) {
// Don't throw errors on html5 tags
libxml_use_internal_errors( true );
}
// add_theme_support( 'clean-head' );
// add_theme_support( 'relative-urls' );
add_theme_support( 'pretty-search' );
add_theme_support( 'post-thumbnails', array( 'post', 'page' ) );
// Initialize stella
require_once( get_template_directory() . '/_framework/init.php' );
// Add Stella Admin files if we're logged in
if ( is_admin() ) {
require_once( get_template_directory() . '/_framework/admin.php' );
}
add_editor_style( 'editor-style.css' );
// Register navigation
register_nav_menus( array(
'primary_navigation' => __( 'Huvudmeny', 'wally-theme' ),
'mobile_primary_navigation' => __( 'Mobilmeny', 'wally-theme' ),
) );
}
//---------------------------------------------------------------------------------
// Remove WP's default ID:s for menu items
//---------------------------------------------------------------------------------
add_filter('nav_menu_item_id', '_w_css_attributes_filter', 100, 1);
function _w_css_attributes_filter($var) {
return is_array($var) ? array() : '';
}
// Register sidebars
add_action( 'widgets_init', function () {
register_sidebar( array(
'name' => __( 'Bloggmeny', 'wally-theme' ),
'id' => 'blog-sidebar',
'description' => __( 'Widgets in this area will be shown on all posts and pages.', 'wally-theme' ),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h2 class="widget__title">',
'after_title' => '</h2>',
) );
} );
//---------------------------------------------------------------------------------
// Register Sidebars
//---------------------------------------------------------------------------------
/**
* Adds a "navigation__item" class to navigation list items.
*/
add_filter( 'nav_menu_css_class', '_w_set_navigation_classes', 101, 2 );
function _w_set_navigation_classes( $classes ) {
$swap = array(
'current-menu-item' => 'is-current',
'current_page_item' => 'is-current',
'menu-item-has-children' => 'is-parent'
);
foreach ( $swap as $from => $to ) {
if ( in_array( $from, $classes ) ) {
$classes[ array_search( $from, $classes ) ] = $to;
}
}
return $classes;
}
/**
* Set nice classes to mobile navigation
*/
add_filter( 'w_mobile_navigation', '_w_mobile_navigation_markup', 10, 3 );
function _w_mobile_navigation_markup( $navigation ) {
if ( current_theme_supports( 'phpquery' ) ) {
$doc = phpQuery::newDocument( $navigation );
$list = $doc->children()->removeAttr( 'class' )
->addClass( 'off-canvas__navigation__list' )
->attr( 'role', 'menu' );
$items = $list->children( 'li' )
->addClass( 'off-canvas__navigation__item' );
$subitems = $items->find( 'li' )
->addClass( 'off-canvas__navigation__subitem' );
$sublists = $items->find( 'ul' )
->addClass( 'off-canvas__navigation__sublist' );
foreach ( $list->find( 'li' ) as $li ) {
$li = pq( $li );
if ( $li->hasClass( 'is-parent' ) ) {
$li->append( '<button class="off-canvas__navigation__toggle" role="button" title="Visa undersidor"></button>' );
};
}
$html = $doc->html();
phpQuery::unloadDocuments();
return $html;
}
return $navigation;
}
/**
* Set nice classes to desktop navigation
*/
add_filter( 'w_desktop_navigation', '_w_desktop_navigation_markup', 10, 3 );
function _w_desktop_navigation_markup( $navigation ) {
if ( current_theme_supports( 'phpquery' ) ) {
$doc = phpQuery::newDocument( $navigation );
$list = $doc->children()->removeAttr( 'class' )
->addClass( 'navigation__list' )
->attr( 'role', 'menu' );
$items = $list->children( 'li' )
->addClass( 'navigation__item' )->children( 'a' )->wrapInner( '<span>' );
$items->find( 'li' )
->addClass( 'navigation__subitem' );
$items->find( 'ul' )
->addClass( 'navigation__sublist' );
$html = $doc->html();
phpQuery::unloadDocuments();
return $html;
}
return $navigation;
}
/**
* Build a list group with phpQuery
*/
add_filter( 'w_blog_sidebar_output', '_w_build_list_group', 10 );
add_filter( 'w_list_group', '_w_build_list_group', 10 );
function _w_build_list_group( $output ) {
if ( current_theme_supports( 'phpquery' ) ) {
$doc = phpQuery::newDocument( $output );
$items = $doc->find( 'li' );
foreach ( pq( $items ) as $item ) {
$item = pq( $item );
//Add pills appearance for categories
if ( $item->hasClass( 'cat-item' ) ) {
$item_class = 'pills__item';
$subitem_class = $item_class;
$item->parents( 'ul' )->addClass( 'pills' );
$item->children( 'a' )->addClass( 'pills__link' );
} else {
$subitem_class = 'list-group__subitem';
$item_class = 'list-group__item';
}
if ( $item->parent()->parent()->is( 'li' ) ) {
$item->addClass( $subitem_class );
} else {
$item->addClass( $item_class );
}
$item->children( 'a' )->wrapInner( '<span>' );
// Filter wordpress classes
$classes = explode( ' ', $item->attr( 'class' ) );
$item->removeAttr( 'class' );
$item->attr( 'class', implode( ' ', _w_set_navigation_classes( $classes ) ) );
// Add sublists
$sub_list = pq( $item->children( 'ul' ) );
if ( $sub_list->length ) {
$sub_list->addClass( 'list-group__sublist is-open' );
$item->children( 'a' )->addClass( 'is-open' )->append( '<button role="button" title="Visa undersidor" data-collapse-trigger></button>' );
}
// Open current items parent lists
// if($item->hasClass('is-current')) {
// $parents = $item->parents('li');
// pq($parents)->children('ul, a')->addClass('is-open');
// }
}
$html = $doc->html();
phpQuery::unloadDocuments();
return $html;
}
return $output;
}
/**
* Build a child list group with phpQuery for vertical menu.
*/
add_filter( 'w_child_list_group', '_w_child_build_list_group', 10 );
function _w_child_build_list_group( $output ) {
$header_setting = ( fw_get_db_customizer_option( 'header_setting' ) ) ? fw_get_db_customizer_option( 'header_setting' ) : 'horizontal-header';
if ( $header_setting != 'vertical-header' ) {
return;
}
if ( current_theme_supports( 'phpquery' ) ) {
$doc = phpQuery::newDocument( $output );
$items = $doc->find( 'li' );
foreach ( pq( $items ) as $item ) {
$item = pq( $item );
//Add pills appearance for categories
if ( $item->hasClass( 'cat-item' ) ) {
$item_class = 'pills__item';
$subitem_class = $item_class;
$item->parents( 'ul' )->addClass( 'pills' );
$item->children( 'a' )->addClass( 'pills__link' );
} else {
$subitem_class = 'list-group__subitem';
$item_class = 'list-group__item';
}
if ( $item->parent()->parent()->is( 'li' ) ) {
$item->addClass( $subitem_class );
} else {
$item->addClass( $item_class );
}
$item->children( 'a' )->wrapInner( '<span>' );
// Filter wordpress classes
$classes = explode( ' ', $item->attr( 'class' ) );
$item->removeAttr( 'class' );
$item->attr( 'class', implode( ' ', _w_set_navigation_classes( $classes ) ) );
// Add sublists
$sub_list = pq( $item->children( 'ul' ) );
if ( $sub_list->length ) {
$sub_list->addClass( 'list-group__sublist is-open' );
$sub_list->parent()->append( '<button role="button" title="Visa undersidor" data-collapse-trigger></button>' );
$item->children( 'a' )->addClass( 'is-open' );
}
// Open current items parent lists
/* if($item->hasClass('is-current')) {
$parents = $item->parents('li');
pq($parents)->children('ul, a')->addClass('is-open');
}*/
//Add class so the jquery will function
if ( $item->hasClass( 'current-page-ancestor' ) ) {
$item->addClass( 'current_page_ancestor' );
}
}
$html = $doc->html();
phpQuery::unloadDocuments();
return $html;
}
return $output;
}
/**
* Build the latest comments template
*/
add_filter( 'w_blog_sidebar_output', '_w_build_latest_comments', 10 );
function _w_build_latest_comments( $output ) {
if ( current_theme_supports( 'phpquery' ) ) {
$doc = phpQuery::newDocument( $output );
$list = $doc->find( '#recentcomments' );
pq( $list )->addClass( 'recent-comments recent__list' );
$items = pq( $list )->find( 'li' );
if ( pq( $items )->length ) {
foreach ( pq( $items ) as $item ) {
// Get comment id
foreach ( pq( $item )->find( "a" ) as $link ) {
$href = pq( $link )->attr( "href" );
preg_match( '/#comment-(\d+)/', $href, $matches );
if ( ! empty( $matches ) ) {
$id = $matches[1];
}
}
pq( $item )->empty();
pq( $item )->addClass( "recent__item" );
if ( ! empty( $id ) && $comment = get_comment( $id ) ):
if ( post_password_required( $comment->comment_post_ID ) ) {
pq( $item )->remove();
continue;
}
$default_avatar = home_url( get_template_directory_uri() . '/assets/images/avatar.svg' );
if ( ( defined( 'WP_LOCAL_DEV' ) && WP_LOCAL_DEV ) ) {
$avatar = '<img alt="" src="' . $default_avatar . '" class="avatar avatar-72 photo" height="64" width="72">';
} else {
$avatar = get_avatar( $comment->comment_author_email, '72', $default_avatar );
}
$date = '<time class="recent__date">' . get_comment_date( 'j F, Y H:i', $comment->comment_ID ) . '</time>';
$author = '<div class="recent__author">' . $avatar . '<span>' . $comment->comment_author . '</span></div>';
$emotion = get_comment_meta( $comment->comment_ID, 'emotion', true );
$content = $comment->comment_content;
if ( $content == $emotion ) {
$content = '';
} else {
$content = '<q>' . get_comment_excerpt( ( $id ) ) . '</q>';
}
if ( $emotion && get_emotion( $emotion ) ) {
$content = '<img src="' . get_template_directory_uri() . '/assets/icons/twemojis/' . get_emotion( $emotion ) . '.svg" alt="" class="recent__emotion">' . $content;
}
$text = '<div class="recent__text">' . $author . ' · ' . $date . '<span class="recent__content">' . $content . '</span></div><a class="recent__link" href="' . get_comment_link( $comment->comment_ID ) . '">' . __( "Läs kommentar om", 'wally-theme' ) . ' ' . get_the_title( $comment->comment_post_ID ) . '</a>';
$article = '<h3>Om artikeln: <a href="' . get_permalink( $comment->comment_post_ID ) . '" class="recent__article">' . get_the_title( $comment->comment_post_ID ) . '</a></h3> <br>';
pq( $item )
->append( $article )
->append( $text );
endif;
}
} else {
pq( $list )->append( '<p class="recent__not-found">Inga kommentarer skrivna ännu.</p>' );
}
$html = $doc->html();
phpQuery::unloadDocuments();
return $html;
}
return $output;
}
/**
* Unyson form builder
*/
add_action( 'fw_ext_forms_frontend_submit', function ( $form ) {
global $wpdb;
global $post;
$form_builder_value = $form['instance']->get_form_builder_value( $form['id'] );
$form_fields = json_decode( $form_builder_value['json'] );
$data = "";
foreach ( $form_fields as $field ) {
if ( array_key_exists( $field->shortcode, $_POST ) ) {
$label = "<label><b>" . $field->options->label . "</b></label>";
if ( is_string( $_POST[ $field->shortcode ] ) ) {
$value = "<pre>" . $_POST[ $field->shortcode ] . "</pre>";
} else if ( is_array( $_POST[ $field->shortcode ] ) ) {
$value = "";
foreach ( $_POST[ $field->shortcode ] as $answer ) {
$value .= "<pre>" . $answer . "</pre>";
}
} else {
$value = "-";
}
$data .= "<div>" . $label . "<br>" . $value . "</div>";
}
}
wp_insert_post( array(
'post_content' => serialize( $data ),
'post_type' => 'survey',
'post_title' => __( 'Svar till formulär', 'wally-theme' ),
'post_status' => 'publish'
) );
} );
function _w_create_post_types() {
$types = array(
'survey' => array(
'args' => array(
'hierarchical' => true,
'public' => false,
'show_ui' => true,
'labels' => array(
'name' => _x( 'Enkätsvar', 'post type general name', 'wally-theme' ),
'singular_name' => _x( 'Enkätsvar', 'post type singular name', 'wally-theme' ),
'menu_name' => _x( 'Enkätsvar', 'admin menu', 'wally-theme' ),
'name_admin_bar' => _x( 'Enkätsvar', 'add new on admin bar', 'wally-theme' ),
'add_new' => _x( 'Skapa nytt', 'book', 'wally-theme' ),
'add_new_item' => __( 'Skapa nytt enkätsvar', 'wally-theme' ),
'new_item' => __( 'Nytt enkätsvar', 'wally-theme' ),
'edit_item' => __( 'Redigera enkätsvar', 'wally-theme' ),
'view_item' => __( 'Visa enkätsvar', 'wally-theme' ),
'all_items' => __( 'Alla enkätsvar', 'wally-theme' ),
'search_items' => __( 'Sök enkätsvar', 'wally-theme' ),
'parent_item_colon' => __( 'Föräldra-enkätsvar:', 'wally-theme' ),
'not_found' => __( 'Hittade inga enkätsvar.', 'wally-theme' ),
'not_found_in_trash' => __( 'Hittade inga enkätsvar i papperskorgen.', 'wally-theme' )
),
'has_archive' => false,
'menu_icon' => 'dashicons-format-status',
'supports' => array(
'editor' => false
)
),
'taxonomies' => array(// 'cpt_tax' => array('label' => 'CPT Tax','hierarchical' => true)
)
),
);
// Register them
foreach ( $types as $type => $cpt ) {
register_post_type( $type, $cpt['args'] );
foreach ( $cpt['taxonomies'] as $taxonomy => $args ) {
register_taxonomy( $taxonomy, $type, $args );
}
}
}
add_action( 'init', '_w_create_post_types' );
/**
* Adds a box to the main column on the Post and Page edit screens.
*/
function _w_survey_add_meta_box() {
add_meta_box(
'myplugin_sectionid',
__( 'Enkätsvar', 'wally-theme' ),
'_w_survey_meta_box_callback',
'survey',
'normal',
'high'
);
}
add_action( 'add_meta_boxes', '_w_survey_add_meta_box' );
/**
* Prints the box content.
*
* @param WP_Post $post The object for the current post/page.
*/
function _w_survey_meta_box_callback( $post ) {
$data = get_post_meta( $post->ID, '_w_survey_data', true );
echo unserialize( $post->post_content );
}
/**
* Hides metaboxes when editing the sitemap.
*/
add_action( 'admin_init', function () {
$post_id = @$_GET['post'] ? @$_GET['post'] : @$_POST['post_ID'];
if ( ! isset( $post_id ) ) {
return;
}
$template_file = get_post_meta( $post_id, '_wp_page_template', true );
if ( $template_file == 'sitemap.php' ) { // edit the template name
remove_post_type_support( 'page', 'editor' );
remove_post_type_support( 'page', 'title' );
remove_post_type_support( 'page', 'thumbnail' );
remove_post_type_support( 'page', 'discussion' );
remove_post_type_support( 'page', 'comments' );
}
} );
/**
* Show a notice when editing the sitemap.
*/
add_action( 'all_admin_notices', function () {
$post_id = @$_GET['post'] ? @$_GET['post'] : @$_POST['post_ID'];
if ( ! isset( $post_id ) ) {
return;
}
$template_file = get_post_meta( $post_id, '_wp_page_template', true );
if ( $template_file === 'sitemap.php' ): ?>
<div class="update-nag"><?php _e( 'Den här sidan används som webbplatskarta och går därför inte att redigera.' ) ?></div>
<?php endif;
} );
/**
* Function for toggling the contrast cookie.
*/
add_action( 'init', function () {
if ( isset( $_GET['toggle_contrast'] ) ) {
if ( isset( $_COOKIE['wally_contrast'] ) ) {
setcookie( 'wally_contrast', "", - 1, '/' );
unset( $_COOKIE['wally_contrast'] );
} else {
setcookie( 'wally_contrast', 1, time() + ( 60 * 60 * 24 * 30 ), '/' );
}
header( "Location: " . remove_query_arg( 'toggle_contrast' ) );
die();
}
} );
/**
* Add sizing options for the post thumbnail
*/
add_filter( 'admin_post_thumbnail_html', '_w_post_thumbnail_options' );
function _w_post_thumbnail_options( $html ) {
global $post;
$size = get_post_meta( $post->ID, 'thumbnail_size', true );
$text = get_post_meta( $post->ID, 'thumbnail_text', true );
$small = $size == 'small' ? 'checked' : '';
$large = $size == 'large' ? 'checked' : '';
if ( ! $small && ! $large ) {
$small = 'checked';
}
if ( $text === null ) {
$text = false;
}
$no_text = $text ? '' : 'checked';
$text = $text ? 'checked' : '';
return $html . '
<p style="border-top: solid 1px rgb(238, 238, 238);padding-top: 1rem;"><strong>' . __( 'Storlek', 'wally-theme' ) . '</strong></p>
<p>' . __( 'Välj i vilken storlek bilden ska visas.', 'wally-theme' ) . '</p>
<p>
<label for="small_thumbnail" style="padding-right: 15px">
<input type="radio" name="thumbnail_size" id="small_thumbnail" value="small" ' . $small . '>
' . __( 'Liten', 'wally-theme' ) . '
</label>
<label for="large_thumbnail">
<input type="radio" name="thumbnail_size" id="large_thumbnail" value="large" ' . $large . '>
' . __( 'Stor', 'wally-theme' ) . '
</label>
</p>
<p><strong>' . __( 'Bildtext', 'wally-theme' ) . '</strong></p>
<p>' . __( 'Välj om bildens bildtext ska visas.', 'wally-theme' ) . '</p>
<p>
<label for="thumbnail_text_off" style="padding-right: 15px">
<input type="radio" name="thumbnail_text" id="thumbnail_text_off" value="0" ' . $no_text . '>
' . __( 'Visa inte', 'wally-theme' ) . '
</label>
<label for="thumbnail_text_on">
<input type="radio" name="thumbnail_text" id="thumbnail_text_on" value="1" ' . $text . '>
' . __( 'Visa', 'wally-theme' ) . '
</label>
</p>
';
}
/**
* Allow SVG upload
*/
function allow_mime_types( $mimes ) {
$mimes['svg'] = 'image/svg+xml';
return $mimes;
}
add_filter( 'upload_mimes', 'allow_mime_types' );
/**
* Save the post thumbnail size as metadata
*/
add_action( 'save_post', '_w_save_post_thumbnail_options' );
function _w_save_post_thumbnail_options( $post_id ) {
if ( @isset( $_POST['thumbnail_size'] ) ) {
update_post_meta( $post_id, 'thumbnail_size', $_POST['thumbnail_size'] );
}
if ( @isset( $_POST['thumbnail_text'] ) ) {
update_post_meta( $post_id, 'thumbnail_text', $_POST['thumbnail_text'] );
}
}
/**
* Generates a pagination for all kinds of queries.
*
* @param string $numpages
* @param string $pagerange
* @param string $paged
*/
function custom_pagination( $numpages = '', $pagerange = '', $paged = '' ) {
if ( empty( $pagerange ) ) {
$pagerange = 2;
}
global $paged;
if ( empty( $paged ) ) {
$paged = 1;
}
if ( $numpages == '' ) {
global $wp_query;
$numpages = $wp_query->max_num_pages;
if ( ! $numpages ) {
$numpages = 1;
}
}
$pagination_args = array(
'base' => get_pagenum_link( 1 ) . '%_%',
'format' => 'page/%#%',
'total' => $numpages,
'current' => $paged,
'show_all' => false,
'end_size' => 1,
'mid_size' => $pagerange,
'prev_next' => true,
'prev_text' => __( '«' ),
'next_text' => __( '»' ),
'type' => 'plain',
'add_args' => false,
'add_fragment' => ''
);
$paginate_links = paginate_links( $pagination_args );
if ( $paginate_links ) {
echo '<div class="pagination">';
echo "<nav class='custom-pagination'>";
echo $paginate_links;
echo "</nav>";
echo "</div>";
}
}
/**
* Ajax actions for lazy loading posts.
*/
add_action( 'wp_ajax_nopriv_ajax_pagination', '_w_ajax_pagination' );
add_action( 'wp_ajax_ajax_pagination', '_w_ajax_pagination' );
function _w_ajax_pagination() {
$query = new WP_Query( array(
'post__not_in' => get_option( 'sticky_posts' ),
'post_type' => 'post',
'post_status' => 'publish',
'paged' => $_GET['page']
) );
// echo $query->max_num_pages;
$object = new stdClass();
$object->pages = intval( $query->max_num_pages );
ob_start();
if ( $query->have_posts() ): while ( $query->have_posts() ): $query->the_post();
get_template_part( 'parts/posts/loop' );
endwhile;
wp_reset_postdata(); endif;
$object->articles = ob_get_clean();
echo json_encode( $object );
die();
}
/**
* Ajax actions for autocomplete. The functions reads wordlist.txt inside the uploads dir
* and returns the top three matches.
*/
add_action( 'wp_ajax_nopriv_autocomplete', '_w_autocomplete' );
add_action( 'wp_ajax_autocomplete', '_w_autocomplete' );
function _w_autocomplete() {
$wordlist = get_template_directory() . '/assets/wordlist.txt';
$query = $_GET['query'];
if ( file_exists( $wordlist ) ) {
// Open and read words
$file = fopen( $wordlist, 'r' );
$words = ( fread( $file, filesize( $wordlist ) ) );
// Find words matching query
preg_match_all( '/(*UTF8)\R(' . $query . '.*)/i', $words, $matches );
fclose( $file );
// Return the top three results
$suggestions = array_slice( $matches[0], 0, 3 );
$object = new stdClass();
$object->suggestions = $suggestions;
echo json_encode( $object );
} else {
echo "File not found: " . $wordlist;
}
die();
}
/**
* Rename Unyson's "Blog posts" to "Blogginlägg"
*/
add_action( 'admin_menu', '_w_rename_post_menu', 999 );
function _w_rename_post_menu() {
global $menu;
if ( isset( $menu[5] ) ) {
$menu[5][0] = __( 'Blogginlägg', 'wally-theme' );
}
}
/**
* Checks if the specified email is connected to a Gravatar
*
* @param $email
*
* @return bool
*/
function has_gravatar( $email ) {
$hash = md5( strtolower( trim( $email ) ) );
$uri = 'http://www.gravatar.com/avatar/' . $hash . '?d=404';
$headers = @get_headers( $uri );
if ( ! preg_match( "|200|", $headers[0] ) ) {
$has_valid_avatar = false;
} else {
$has_valid_avatar = true;
}
return $has_valid_avatar;
}
/**
* Sets the query to highlight when searching.
*/
add_action( 'wp_print_scripts', '_w_set_highlight_query' );
function _w_set_highlight_query() {
$query = esc_attr( get_search_query() );
if ( strlen( $query ) > 0 ) {
echo '
<script type="text/javascript">
var wallySearchQuery = "' . $query . '";
</script>
';
}
}
/**
* @return array An array containing pairs of title => filename/character.
*/
function get_emotions() {
return array(
'Tummen upp' => '1f44d',
'Tummen ner' => '1f44e',
'Glad' => '1f600',
//'Skrattar' => '1f604',
// 'Hjärta som ögon' => '1f60d',
// 'Voíla' => '1f44c',
// 'Blinka med ena ögat' => '1f609',
'Neutral' => '1f610',
// 'Gråter' => '1f622',
'Ledsen' => '1f641',
// 'Ler' => '1f642',
// 'Frågetecken' => '2753',
// 'Utropstecken' => '2757',
// 'Hjärta' => '2764'
);
}
/**
* @param $emotion string A string starting with emotion and ending with a character-code/filename like "emotion1234".
*
* @return bool|string The corresponding filename/character or false if it doesn't exist.
*/
function get_emotion( $emotion ) {
$emotions = get_emotions();
if ( strpos( $emotion, 'emotion' ) !== false ) {
$emotion = substr( $emotion, strlen( 'emotion' ) );
if ( in_array( $emotion, $emotions ) ) {
return $emotions[ array_search( $emotion, $emotions ) ];
}
}
return false;
}
/**
* Add some feel-good to the comment form.
*/
add_action( 'comment_form_logged_in_after', '_w_comment_form_emotions' );
add_action( 'comment_form_after_fields', '_w_comment_form_emotions' );
function _w_comment_form_emotions() {
$emotions = get_emotions();
echo '<fieldset class="comment-form__emotion form__group">' . '<legend class="form__label">' . __( 'Välj en symbol för kommentaren:', 'wally-theme' ) . '</legend><br>';
$i = 0;
foreach ( $emotions as $key => $emotion ):
echo '<input id="emotion' . $emotion . '" class="form__control" name="commentFormEmotion" type="radio" value="emotion' . $emotion . '"><label class="form__label emotion" for="emotion' . $emotion . '"><img src="' . get_template_directory_uri() . '/assets/icons/twemojis/' . $emotion . '.svg" alt="' . __( $key, 'wally-theme' ) . '"></label>';
$i ++;
endforeach;
echo '</fieldset>';
}
add_action( 'comment_post', '_w_comment_form_save' );
function _w_comment_form_save( $comment_id ) {
if ( ( isset( $_POST['commentFormEmotion'] ) ) && $_POST['commentFormEmotion'] != '' && strpos( $_POST['commentFormEmotion'], 'emotion' ) !== false ) {
$emotion = wp_filter_nohtml_kses( $_POST['commentFormEmotion'] );
} else {
$emotion = '';
}
add_comment_meta( $comment_id, 'emotion', $emotion );