-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
executable file
·964 lines (814 loc) · 26.9 KB
/
functions.php
File metadata and controls
executable file
·964 lines (814 loc) · 26.9 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
<?php
/**
* Theme bootstrap and feature registration.
*
* Contains core setup for the theme, including supports, assets,
* menus/widgets, editor behavior, helper loading, and admin settings.
*
* @package tailwind_wordpress_template
* @since 1.0.0
*/
if (!defined('ABSPATH')) {
exit;
}
/**
* Get the active theme version.
*
* @return string Theme version string.
*/
function tailwind_wordpress_template_get_version()
{
$theme = wp_get_theme();
return $theme->get('Version');
}
/**
* Get file modification time for cache-busting.
*
* @param string $file_path File path relative to the theme root.
* @return string File modification timestamp, or theme version as fallback.
*/
function tailwind_wordpress_template_get_file_version($file_path)
{
$full_path = get_template_directory() . '/' . $file_path;
if (file_exists($full_path)) {
return filemtime($full_path);
}
return tailwind_wordpress_template_get_version();
}
/**
*/
function tailwind_wordpress_template_enqueue_styles()
{
$css_file = 'assets/css/style.css';
$css_version = tailwind_wordpress_template_get_file_version($css_file);
wp_enqueue_style(
'tailwind_wordpress_template-style',
get_template_directory_uri() . '/' . $css_file,
array(),
$css_version,
'all'
);
}
add_action('wp_enqueue_scripts', 'tailwind_wordpress_template_enqueue_styles');
/**
*/
function tailwind_wordpress_template_enqueue_scripts()
{
$js_file = 'assets/js/main.js';
$js_version = tailwind_wordpress_template_get_file_version($js_file);
wp_enqueue_script(
'tailwind_wordpress_template-main',
get_template_directory_uri() . '/' . $js_file,
array(),
$js_version,
true
);
}
add_action('wp_enqueue_scripts', 'tailwind_wordpress_template_enqueue_scripts');
/**
*/
function tailwind_wordpress_template_theme_support()
{
add_theme_support('title-tag');
add_theme_support('post-thumbnails');
add_theme_support('custom-logo', array(
'height' => 100,
'width' => 400,
'flex-height' => true,
'flex-width' => true,
));
add_theme_support('html5', array(
'search-form',
'comment-form',
'comment-list',
'gallery',
'caption',
'style',
'script',
));
add_theme_support('automatic-feed-links');
add_theme_support('editor-styles');
add_theme_support('align-wide');
}
add_action('after_setup_theme', 'tailwind_wordpress_template_theme_support');
/**
*
*
*/
function tailwind_wordpress_template_add_editor_styles()
{
add_editor_style('assets/css/style.css');
}
add_action('after_setup_theme', 'tailwind_wordpress_template_add_editor_styles');
/**
*/
function tailwind_wordpress_template_register_menus()
{
register_nav_menus(array(
'primary' => __('Primary Menu', 'tailwind_wordpress_template'),
'footer' => __('Footer Menu', 'tailwind_wordpress_template'),
));
}
add_action('after_setup_theme', 'tailwind_wordpress_template_register_menus');
/**
*
*/
function tailwind_wordpress_template_disable_widgets_block_editor()
{
remove_theme_support('widgets-block-editor');
}
add_action('after_setup_theme', 'tailwind_wordpress_template_disable_widgets_block_editor');
/**
*/
function tailwind_wordpress_template_register_sidebars()
{
register_sidebar(array(
'name' => __('Primary Sidebar', 'tailwind_wordpress_template'),
'id' => 'sidebar-1',
'description' => __('Widget area shown on posts and pages sidebar', 'tailwind_wordpress_template'),
'before_widget' => '<section id="%1$s" class="widget %2$s">',
'after_widget' => '</section>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>',
));
}
add_action('widgets_init', 'tailwind_wordpress_template_register_sidebars');
/**
*/
function tailwind_wordpress_template_content_width()
{
$GLOBALS['content_width'] = apply_filters('tailwind_wordpress_template_content_width', 1200);
}
add_action('after_setup_theme', 'tailwind_wordpress_template_content_width', 0);
/**
*/
function tailwind_wordpress_template_load_textdomain()
{
load_theme_textdomain('tailwind_wordpress_template', get_template_directory() . '/languages');
}
add_action('after_setup_theme', 'tailwind_wordpress_template_load_textdomain');
/**
*
*/
$tailwind_wordpress_template_menu_config = array(
'all_menu_items' => array(
'relative lg:flex-grow',
'border-b last:border-b-0 lg:border-b-0 border-white',
),
'top_level_item' => array(
'group',
'relative',
),
'top_level' => array(
'lg:before:content-[""] lg:before:block lg:before:h-[60%] lg:before:w-[1px] lg:before:bg-white',
'lg:before:absolute lg:before:top-[25%] lg:before:left-0 lg:first:before:hidden',
),
'top_level_not_last' => array(
'border-white',
),
'submenu_ul' => array(
'text-sm bg-secondary-300/20 lg:bg-secondary-50 text-gray-900 lg:text-left overflow-hidden px-2 lg:pb-1',
),
'submenu_item' => array(
'sub-menu-item',
),
'menu_link' => array(
'block px-4 py-1 lg:py-2 lg:hover:bg-secondary-50 hover:text-primary-700 hover:font-bold',
),
'top_level_link' => array(
'lg:px-6',
),
'submenu_link' => array(
),
);
/**
*/
require_once get_template_directory() . '/utils/icon-helper.php';
require_once get_template_directory() . '/utils/classnames.php';
require_once get_template_directory() . '/utils/tabs-helper.php';
require_once get_template_directory() . '/utils/content-helper.php';
/**
*
*
* @param array $classes Current menu item classes.
* @param WP_Post $menu_item Current menu item object.
* @param stdClass $args `wp_nav_menu()` arguments.
* @param int $depth Current menu depth.
* @return array Updated menu item classes.
*/
function tailwind_wordpress_template_handle_menu($classes, $menu_item, $args, $depth)
{
global $tailwind_wordpress_template_menu_config;
if (!isset($args->theme_location) || $args->theme_location !== 'primary') {
return $classes;
}
// ============================================
// ============================================
if (!empty($tailwind_wordpress_template_menu_config['all_menu_items'])) {
$classes = array_merge($classes, $tailwind_wordpress_template_menu_config['all_menu_items']);
}
// ============================================
// ============================================
if ($depth === 0 && !empty($tailwind_wordpress_template_menu_config['top_level_item'])) {
static $menu_children_cache = array();
static $cached_menu_location = null;
if ($cached_menu_location !== $args->theme_location) {
$menu_children_cache = array();
$cached_menu_location = $args->theme_location;
$locations = get_nav_menu_locations();
if (isset($locations[$args->theme_location])) {
$menu_id = $locations[$args->theme_location];
$menu_items = wp_get_nav_menu_items($menu_id);
if ($menu_items) {
foreach ($menu_items as $item) {
if (!empty($item->menu_item_parent) && $item->menu_item_parent != '0') {
$parent_id = (int) $item->menu_item_parent;
if (!isset($menu_children_cache[$parent_id])) {
$menu_children_cache[$parent_id] = true;
}
}
}
}
}
}
if (isset($menu_children_cache[$menu_item->ID])) {
$classes = array_merge($classes, $tailwind_wordpress_template_menu_config['top_level_item']);
}
}
// ============================================
// ============================================
if ($depth === 0 && !empty($tailwind_wordpress_template_menu_config['top_level'])) {
$classes = array_merge($classes, $tailwind_wordpress_template_menu_config['top_level']);
}
// ============================================
// ============================================
if ($depth === 0 && !empty($tailwind_wordpress_template_menu_config['top_level_not_last'])) {
static $top_level_items = null;
static $menu_location = null;
if ($menu_location !== $args->theme_location) {
$top_level_items = null;
$menu_location = $args->theme_location;
}
if ($top_level_items === null) {
$locations = get_nav_menu_locations();
if (isset($locations[$args->theme_location])) {
$menu_id = $locations[$args->theme_location];
$menu_items = wp_get_nav_menu_items($menu_id);
if ($menu_items) {
$top_level_items = array_filter($menu_items, function ($item) {
return $item->menu_item_parent == '0';
});
$top_level_items = array_values($top_level_items);
}
}
}
if ($top_level_items !== null) {
$current_index = -1;
foreach ($top_level_items as $index => $item) {
if ($item->ID == $menu_item->ID) {
$current_index = $index;
break;
}
}
if ($current_index >= 0 && $current_index < count($top_level_items) - 1) {
$classes = array_merge($classes, $tailwind_wordpress_template_menu_config['top_level_not_last']);
}
}
}
// ============================================
// ============================================
if ($depth > 0 && !empty($tailwind_wordpress_template_menu_config['submenu_item'])) {
$classes = array_merge($classes, $tailwind_wordpress_template_menu_config['submenu_item']);
}
return $classes;
}
add_filter('nav_menu_css_class', 'tailwind_wordpress_template_handle_menu', 10, 4);
/**
*
* @param array $classes Current submenu `<ul>` classes.
* @param stdClass $args `wp_nav_menu()` arguments.
* @param int $depth Current submenu depth.
* @return array Updated submenu `<ul>` classes.
*/
function tailwind_wordpress_template_handle_submenu_ul($classes, $args, $depth)
{
global $tailwind_wordpress_template_menu_config;
if (!isset($args->theme_location) || $args->theme_location !== 'primary') {
return $classes;
}
if (!empty($tailwind_wordpress_template_menu_config['submenu_ul'])) {
$classes = array_merge($classes, $tailwind_wordpress_template_menu_config['submenu_ul']);
}
return $classes;
}
add_filter('nav_menu_submenu_css_class', 'tailwind_wordpress_template_handle_submenu_ul', 10, 3);
/**
*
* @param array $atts Current link attributes.
* @param WP_Post $menu_item Current menu item object.
* @param stdClass $args `wp_nav_menu()` arguments.
* @param int $depth Current menu depth.
* @return array Updated link attributes.
*/
function tailwind_wordpress_template_handle_menu_link($atts, $menu_item, $args, $depth)
{
global $tailwind_wordpress_template_menu_config;
if (!isset($args->theme_location) || $args->theme_location !== 'primary') {
return $atts;
}
if (!isset($atts['class'])) {
$atts['class'] = '';
}
$classes = !empty($atts['class']) ? explode(' ', trim($atts['class'])) : array();
// ============================================
// ============================================
if (!empty($tailwind_wordpress_template_menu_config['menu_link'])) {
$classes = array_merge($classes, $tailwind_wordpress_template_menu_config['menu_link']);
}
// ============================================
// ============================================
if ($depth === 0 && !empty($tailwind_wordpress_template_menu_config['top_level_link'])) {
$classes = array_merge($classes, $tailwind_wordpress_template_menu_config['top_level_link']);
}
// ============================================
// ============================================
if ($depth > 0 && !empty($tailwind_wordpress_template_menu_config['submenu_link'])) {
$classes = array_merge($classes, $tailwind_wordpress_template_menu_config['submenu_link']);
}
$classes = array_unique($classes);
$atts['class'] = implode(' ', array_filter($classes));
return $atts;
}
add_filter('nav_menu_link_attributes', 'tailwind_wordpress_template_handle_menu_link', 10, 4);
/**
*
*
* @param WP_Query $query Main query object.
*/
function tailwind_wordpress_template_sticky_posts_in_archives($query)
{
if (!$query->is_main_query() || !$query->is_archive()) {
return;
}
$query->set('ignore_sticky_posts', false);
}
add_action('pre_get_posts', 'tailwind_wordpress_template_sticky_posts_in_archives');
/**
*
*
* @param array $posts Queried posts.
* @param WP_Query $query Main query object.
* @return array Reordered posts.
*/
function tailwind_wordpress_template_sort_sticky_posts_in_archives($posts, $query)
{
if (!$query->is_main_query() || !$query->is_archive()) {
return $posts;
}
if ($query->get('paged') > 1) {
return $posts;
}
static $sticky_posts_cache = null;
if ($sticky_posts_cache === null) {
$sticky_posts = get_option('sticky_posts');
$sticky_posts_cache = !empty($sticky_posts) && is_array($sticky_posts)
? array_flip(array_map('intval', $sticky_posts))
: array();
}
if (empty($sticky_posts_cache)) {
return $posts;
}
$sticky = array();
$normal = array();
foreach ($posts as $post) {
if (isset($sticky_posts_cache[$post->ID])) {
$sticky[] = $post;
} else {
$normal[] = $post;
}
}
return array_merge($sticky, $normal);
}
add_filter('the_posts', 'tailwind_wordpress_template_sort_sticky_posts_in_archives', 10, 2);
/**
*
*
* @param array $posts Queried posts.
* @param WP_Query $query Main query object.
* @return array Original posts array (after preloading related data).
*/
function tailwind_wordpress_template_preload_archive_data($posts, $query)
{
if (!$query->is_main_query() || !$query->is_archive()) {
return $posts;
}
if (empty($posts)) {
return $posts;
}
$author_ids = array();
foreach ($posts as $post) {
if (!empty($post->post_author)) {
$author_ids[] = (int) $post->post_author;
}
}
if (!empty($author_ids)) {
$author_ids = array_unique($author_ids);
update_post_author_caches($posts);
}
$original_posts = $query->posts;
$query->posts = $posts;
update_post_thumbnail_cache($query);
$query->posts = $original_posts;
return $posts;
}
add_filter('the_posts', 'tailwind_wordpress_template_preload_archive_data', 20, 2);
/**
*
*
* @param string $more Default excerpt suffix.
* @return string Custom excerpt suffix.
*/
function tailwind_wordpress_template_excerpt_more( $more ) {
return '...';
}
add_filter( 'excerpt_more', 'tailwind_wordpress_template_excerpt_more' );
/**
*
*
* @param string $img_classes CSS classes applied to the logo `<img>` element.
* @return string Custom logo HTML, or empty string when no logo is set.
*
* @example
* echo tailwind_wordpress_template_get_custom_logo();
*
* echo tailwind_wordpress_template_get_custom_logo('max-h-8 w-auto');
*
* echo tailwind_wordpress_template_get_custom_logo('max-h-16 w-auto');
*/
function tailwind_wordpress_template_get_custom_logo($img_classes = 'max-h-12 w-auto max-w-auto')
{
if (!has_custom_logo()) {
return '';
}
$logo = get_custom_logo();
if (empty($logo)) {
return '';
}
$logo = str_replace('<img', '<img class="' . esc_attr($img_classes) . '"', $logo);
return $logo;
}
/**
*
*
* 3. LINE ID
*
*/
function tailwind_wordpress_template_register_custom_settings()
{
}
add_action('admin_init', 'tailwind_wordpress_template_register_custom_settings');
/**
*
*
* @param array $args Field callback arguments from WordPress settings API.
*/
function tailwind_wordpress_template_footer_text_callback($args)
{
$value = get_option('tailwind_wordpress_template_footer_text', '');
wp_editor(
$value,
'tailwind_wordpress_template_footer_text',
array(
'textarea_rows' => 5,
'media_buttons' => false,
'teeny' => true,
)
);
echo '<p class="description">' . __('You can enter footer text here. HTML is supported.', 'tailwind_wordpress_template') . '</p>';
}
/**
*
*/
function tailwind_wordpress_template_admin_enqueue_scripts($hook)
{
if ($hook !== 'options-general.php') {
return;
}
wp_enqueue_media();
wp_add_inline_script('jquery', '
jQuery(document).ready(function($) {
var mediaUploader;
$("#tailwind_wordpress_template_default_image_upload_btn").on("click", function(e) {
e.preventDefault();
if (mediaUploader) {
mediaUploader.open();
return;
}
mediaUploader = wp.media({
title: "' . __('Select Image', 'tailwind_wordpress_template') . '",
button: {
text: "' . __('Use This Image', 'tailwind_wordpress_template') . '"
},
multiple: false,
library: {
type: "image"
}
});
mediaUploader.on("select", function() {
var attachment = mediaUploader.state().get("selection").first().toJSON();
$("#tailwind_wordpress_template_default_image").val(attachment.id);
var preview = "";
if (attachment.sizes && attachment.sizes.medium) {
preview = "<img src=\"" + attachment.sizes.medium.url + "\" style=\"max-width: 300px; height: auto;\" />";
} else {
preview = "<img src=\"" + attachment.url + "\" style=\"max-width: 300px; height: auto;\" />";
}
$("#tailwind_wordpress_template_default_image_preview").html(preview);
if ($("#tailwind_wordpress_template_default_image_remove_btn").length === 0) {
$("#tailwind_wordpress_template_default_image_upload_btn").after(" <button type=\"button\" id=\"tailwind_wordpress_template_default_image_remove_btn\" class=\"button\">' . __('Remove Image', 'tailwind_wordpress_template') . '</button>");
}
});
mediaUploader.open();
});
$(document).on("click", "#tailwind_wordpress_template_default_image_remove_btn", function(e) {
e.preventDefault();
$("#tailwind_wordpress_template_default_image").val("");
$("#tailwind_wordpress_template_default_image_preview").html("");
$(this).remove();
});
});
');
}
add_action('admin_enqueue_scripts', 'tailwind_wordpress_template_admin_enqueue_scripts');
/**
*
*/
function tailwind_wordpress_template_register_disable_comments_setting()
{
register_setting(
'discussion',
'tailwind_wordpress_template_disable_comments',
array(
'type' => 'boolean',
'sanitize_callback' => 'rest_sanitize_boolean',
'default' => false,
)
);
add_settings_field(
'tailwind_wordpress_template_disable_comments',
__('Disable Comments', 'tailwind_wordpress_template'),
'tailwind_wordpress_template_disable_comments_callback',
'discussion',
'default',
array('label_for' => 'tailwind_wordpress_template_disable_comments')
);
}
add_action('admin_init', 'tailwind_wordpress_template_register_disable_comments_setting');
/**
*
*
* @param array $args Field callback arguments from WordPress settings API.
*/
function tailwind_wordpress_template_disable_comments_callback($args)
{
$value = get_option('tailwind_wordpress_template_disable_comments', false);
echo '<label for="tailwind_wordpress_template_disable_comments">';
echo '<input type="checkbox" id="tailwind_wordpress_template_disable_comments" name="tailwind_wordpress_template_disable_comments" value="1" ' . checked(1, $value, false) . ' />';
echo ' ' . __('Check this option to disable all comments across the site (frontend display and backend options).', 'tailwind_wordpress_template');
echo '</label>';
echo '<p class="description">' . __('After disabling, comment areas will not be shown on frontend, and comment options will be hidden in the backend editor.', 'tailwind_wordpress_template') . '</p>';
}
/**
*
*
*/
function tailwind_wordpress_template_register_classic_editor_setting()
{
register_setting(
'writing',
'tailwind_wordpress_template_use_classic_editor',
array(
'type' => 'boolean',
'sanitize_callback' => 'rest_sanitize_boolean',
'default' => false,
)
);
add_settings_field(
'tailwind_wordpress_template_use_classic_editor',
__('Use Classic Editor', 'tailwind_wordpress_template'),
'tailwind_wordpress_template_use_classic_editor_callback',
'writing',
'default',
array('label_for' => 'tailwind_wordpress_template_use_classic_editor')
);
}
add_action('admin_init', 'tailwind_wordpress_template_register_classic_editor_setting');
/**
*
*/
function tailwind_wordpress_template_use_classic_editor_callback($args)
{
$value = get_option('tailwind_wordpress_template_use_classic_editor', false);
echo '<label for="tailwind_wordpress_template_use_classic_editor">';
echo '<input type="checkbox" id="tailwind_wordpress_template_use_classic_editor" name="tailwind_wordpress_template_use_classic_editor" value="1" ' . checked(1, $value, false) . ' />';
echo ' ' . __('Check to use Classic Editor for posts and pages (disable block editor).', 'tailwind_wordpress_template');
echo '</label>';
echo '<p class="description">' . __('If you prefer the traditional editing interface, enable this option. It applies to both new and existing posts/pages.', 'tailwind_wordpress_template') . '</p>';
}
/**
*
*
*/
function tailwind_wordpress_template_maybe_disable_block_editor_for_post($use_block_editor, $post)
{
$use_classic = get_option('tailwind_wordpress_template_use_classic_editor', false);
if ($use_classic) {
return false;
}
return $use_block_editor;
}
add_filter('use_block_editor_for_post', 'tailwind_wordpress_template_maybe_disable_block_editor_for_post', 10, 2);
/**
*/
function tailwind_wordpress_template_maybe_disable_block_editor_for_post_type($use_block_editor, $post_type)
{
$use_classic = get_option('tailwind_wordpress_template_use_classic_editor', false);
if ($use_classic) {
return false;
}
return $use_block_editor;
}
add_filter('use_block_editor_for_post_type', 'tailwind_wordpress_template_maybe_disable_block_editor_for_post_type', 10, 2);
/**
*
*/
function tailwind_wordpress_template_disable_comments_support()
{
$disable_comments = get_option('tailwind_wordpress_template_disable_comments', false);
if ($disable_comments) {
$post_types = get_post_types(array('public' => true), 'names');
foreach ($post_types as $post_type) {
remove_post_type_support($post_type, 'comments');
remove_post_type_support($post_type, 'trackbacks');
}
}
}
add_action('admin_init', 'tailwind_wordpress_template_disable_comments_support');
/**
*
*/
function tailwind_wordpress_template_close_all_comments()
{
$disable_comments = get_option('tailwind_wordpress_template_disable_comments', false);
if ($disable_comments) {
add_filter('comments_open', '__return_false', 20);
add_filter('pings_open', '__return_false', 20);
}
}
add_action('init', 'tailwind_wordpress_template_close_all_comments');
/**
*
*/
function tailwind_wordpress_template_hide_comments_admin_menu()
{
$disable_comments = get_option('tailwind_wordpress_template_disable_comments', false);
if ($disable_comments) {
remove_menu_page('edit-comments.php');
}
}
add_action('admin_menu', 'tailwind_wordpress_template_hide_comments_admin_menu');
/**
*
*/
function tailwind_wordpress_template_hide_comments_admin_bar($wp_admin_bar)
{
$disable_comments = get_option('tailwind_wordpress_template_disable_comments', false);
if ($disable_comments) {
$wp_admin_bar->remove_menu('comments');
}
}
add_action('admin_bar_menu', 'tailwind_wordpress_template_hide_comments_admin_bar', 999);
/**
*
*/
function tailwind_wordpress_template_remove_comments_dashboard_widget()
{
$disable_comments = get_option('tailwind_wordpress_template_disable_comments', false);
if ($disable_comments) {
remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
remove_meta_box('dashboard_activity', 'dashboard', 'normal');
}
}
add_action('wp_dashboard_setup', 'tailwind_wordpress_template_remove_comments_dashboard_widget');
/**
*
*/
function tailwind_wordpress_template_remove_comments_meta_box()
{
$disable_comments = get_option('tailwind_wordpress_template_disable_comments', false);
if ($disable_comments) {
$post_types = get_post_types(array('public' => true), 'names');
foreach ($post_types as $post_type) {
remove_meta_box('commentstatusdiv', $post_type, 'normal');
}
}
}
add_action('admin_init', 'tailwind_wordpress_template_remove_comments_meta_box');
/**
*
*
* @return bool True when comments should be shown.
*/
function tailwind_wordpress_template_should_show_comments()
{
$disable_comments = get_option('tailwind_wordpress_template_disable_comments', false);
if ($disable_comments) {
return false;
}
return comments_open() || get_comments_number();
}
/**
*
*
* @param array $mimes Allowed mime types.
* @return array Updated mime types.
*/
function tailwind_wordpress_template_allow_svg_upload($mimes)
{
$mimes['svg'] = 'image/svg+xml';
$mimes['svgz'] = 'image/svg+xml';
return $mimes;
}
add_filter('upload_mimes', 'tailwind_wordpress_template_allow_svg_upload');
/**
*
*
* @param array $data Current filetype data.
* @param string $file Temporary file path.
* @param string $filename Original uploaded file name.
* @param array $mimes Allowed mime types.
* @return array Updated filetype data.
*/
function tailwind_wordpress_template_fix_svg_filetype($data, $file, $filename, $mimes)
{
$filetype = wp_check_filetype($filename, $mimes);
if ($filetype['ext'] === 'svg' || $filetype['ext'] === 'svgz') {
$data['ext'] = $filetype['ext'];
$data['type'] = 'image/svg+xml';
}
return $data;
}
add_filter('wp_check_filetype_and_ext', 'tailwind_wordpress_template_fix_svg_filetype', 10, 4);
/**
*
*
* @param string $hook Current admin page hook suffix.
*/
function tailwind_wordpress_template_show_svg_preview($hook)
{
if ($hook !== 'upload.php' && $hook !== 'post.php' && $hook !== 'post-new.php') {
return;
}
$css = '
.attachment-266x266, .thumbnail img {
width: 100% !important;
height: auto !important;
}
.attachment .thumbnail img[src$=".svg"] {
width: 100%;
height: auto;
max-width: 150px;
max-height: 150px;
}
';
wp_add_inline_style('wp-admin', $css);
}
add_action('admin_enqueue_scripts', 'tailwind_wordpress_template_show_svg_preview');
/**
*
*
* @param array $response Attachment response data.
* @param WP_Post $attachment Attachment post object.
* @param array $meta Attachment metadata.
* @return array Updated attachment response data.
*/
function tailwind_wordpress_template_fix_svg_attachment_response($response, $attachment, $meta)
{
if ($response['type'] === 'image' && $response['subtype'] === 'svg+xml') {
$response['image'] = array(
'src' => $response['url'],
'width' => isset($meta['width']) ? $meta['width'] : null,
'height' => isset($meta['height']) ? $meta['height'] : null,
);
$response['sizes'] = array(
'full' => array(
'url' => $response['url'],
'width' => isset($meta['width']) ? $meta['width'] : null,
'height' => isset($meta['height']) ? $meta['height'] : null,
'orientation' => isset($meta['width']) && isset($meta['height']) && $meta['width'] > $meta['height'] ? 'landscape' : 'portrait',
),
);
}
return $response;
}
add_filter('wp_prepare_attachment_for_js', 'tailwind_wordpress_template_fix_svg_attachment_response', 10, 3);