forked from COPIM/copim_pub_wordpress_theme
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
executable file
·992 lines (819 loc) · 30.1 KB
/
functions.php
File metadata and controls
executable file
·992 lines (819 loc) · 30.1 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
<?php
/**
*
* @package copim
*/
if (! defined('_S_VERSION')) {
define('_S_VERSION', '1.0.0');
}
function copim_setup()
{
add_theme_support('automatic-feed-links');
add_theme_support('title-tag');
add_theme_support('post-thumbnails');
add_theme_support(
'html5',
[
'search-form',
'comment-form',
'comment-list',
'gallery',
'caption',
'style',
'script',
]
);
}
add_action('after_setup_theme', 'copim_setup');
// REMOVE ADMIN / EDITOR MENU ITEMS
function copim_setup_editor_capabilities()
{
$role = get_role('editor');
if ($role && ! $role->has_cap('edit_theme_options')) {
$role->add_cap('edit_theme_options');
}
}
add_action('after_switch_theme', 'copim_setup_editor_capabilities');
function restrict_admin_menus_for_editors()
{
if (! current_user_can('administrator')) {
// Appearance > Menus only
global $submenu;
if (isset($submenu['themes.php'])) {
foreach ($submenu['themes.php'] as $index => $item) {
$slug = $item[2];
if ($slug !== 'nav-menus.php') {
unset($submenu['themes.php'][$index]);
}
}
}
// Hide entire Tools menu
remove_menu_page('tools.php');
// Hide Ajax Load More
remove_menu_page('ajax-load-more');
}
}
add_action('admin_menu', 'restrict_admin_menus_for_editors', 999);
// Register menus
register_nav_menus(
[
'header-menu' => esc_html__('Header Menu', 'copim'),
'footer-menu-1' => esc_html__('Footer Menu 1', 'copim'),
'footer-menu-2' => esc_html__('Footer Menu 2', 'copim'),
'footer-menu-credits' => esc_html__('Footer Menu Credits', 'copim'),
]
);
/**
* Custom Walker Nav Menu With Icons - Add div around main nav so it can form a mega menu and add icons
*/
class Custom_Walker_Nav_Menu_With_Icons extends Walker_Nav_Menu
{
/**
* Maximum allowed depth - theme supports 2 levels maximum
* Level 0: Main navigation
* Level 1: Dropdown submenu
*/
private const MAX_DEPTH = 2;
/**
* Track if we're in a valid menu level
*/
private $current_depth = 0;
/**
* Add wrapper before submenu with error handling
*/
public function start_lvl(&$output, $depth = 0, $args = null)
{
// Validate output parameter
if (! is_string($output)) {
$output = '';
}
// Validate and sanitize depth
$depth = $this->validate_depth($depth);
// Depth is now controlled by the walk() method override
// Generate indentation safely
$indent = str_repeat("\t", $depth);
$submenu_class = ($depth === 0) ? 'sub-menu' : 'sub-sub-menu';
$output .= "\n$indent<ul class=\"$submenu_class\">\n";
// Only add wrapper to top-level submenus (depth 0 = main nav, depth 1 = dropdown)
if ($depth === 0) {
$output .= "$indent\t<div class=\"sub-menu-inner\">\n";
}
}
/**
* Close the submenu wrapper with error handling
*/
public function end_lvl(&$output, $depth = 0, $args = null)
{
// Validate output parameter
if (! is_string($output)) {
$output = '';
}
// Validate and sanitize depth
$depth = $this->validate_depth($depth);
// Generate indentation safely
$indent = str_repeat("\t", $depth);
if ($depth === 0) {
$output .= "$indent\t</div>\n";
}
$output .= "$indent</ul>\n";
}
/**
* Add icons to top-level items with children and comprehensive error handling
*/
public function start_el(&$output, $item, $depth = 0, $args = null, $id = 0)
{
// Validate output parameter
if (! is_string($output)) {
$output = '';
}
// Validate item parameter
if (! $this->validate_menu_item($item)) {
return; // Skip invalid items
}
// Validate and sanitize depth
$depth = $this->validate_depth($depth);
// Depth is now controlled by the walk() method override
// Set up the base list item classes with validation
$classes = $this->get_safe_classes($item);
$class_names = join(' ', array_filter($classes));
$class_names = $class_names ? ' class="' . esc_attr($class_names) . '"' : '';
$output .= '<li' . $class_names . '>';
// Build the <a> tag with safe attribute handling
$attributes = $this->build_safe_attributes($item);
$attributes .= ' class="menu-link"';
// Get safe title with fallback
$title = $this->get_safe_title($item, $args, $depth);
$item_output = '<a' . $attributes . '>';
$item_output .= $title;
// Append icons ONLY to top-level items with children (with validation)
if ($this->should_show_icons($args, $depth, $item)) {
$item_output .= $this->get_icon_markup();
}
$item_output .= '</a>';
$output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $args, $depth);
}
/**
* Validate and sanitize depth parameter
*/
private function validate_depth($depth)
{
// Ensure depth is numeric
if (! is_numeric($depth)) {
$depth = 0;
}
// Convert to integer
$depth = (int) $depth;
// Ensure depth is within safe bounds
if ($depth < 0) {
$depth = 0;
} elseif ($depth > self::MAX_DEPTH) {
$depth = self::MAX_DEPTH;
}
return $depth;
}
/**
* Validate menu item object
*/
private function validate_menu_item($item)
{
// Check if item exists and is an object
if (! $item || ! is_object($item)) {
return false;
}
// Check for required properties
$required_properties = [ 'ID', 'title', 'url', 'classes' ];
foreach ($required_properties as $property) {
if (! property_exists($item, $property)) {
return false;
}
}
return true;
}
/**
* Get safe classes array with fallback
*/
private function get_safe_classes($item)
{
if (! isset($item->classes) || ! is_array($item->classes)) {
return [];
}
// Filter out any non-string classes
return array_filter($item->classes, 'is_string');
}
/**
* Build safe HTML attributes
*/
private function build_safe_attributes($item)
{
$attributes = '';
// Title attribute
if (! empty($item->attr_title) && is_string($item->attr_title)) {
$attributes .= ' title="' . esc_attr($item->attr_title) . '"';
}
// Target attribute
if (! empty($item->target) && is_string($item->target)) {
$attributes .= ' target="' . esc_attr($item->target) . '"';
}
// XFN attribute
if (! empty($item->xfn) && is_string($item->xfn)) {
$attributes .= ' rel="' . esc_attr($item->xfn) . '"';
}
// URL attribute
if (! empty($item->url) && is_string($item->url)) {
$attributes .= ' href="' . esc_url($item->url) . '"';
}
return $attributes;
}
/**
* Get safe title with fallback
*/
private function get_safe_title($item, $args, $depth)
{
// Ensure title exists and is string
if (empty($item->title) || ! is_string($item->title)) {
$title = 'Untitled';
} else {
$title = $item->title;
}
// Apply WordPress filters safely
$title = apply_filters('the_title', $title, $item->ID);
$title = apply_filters('nav_menu_item_title', $title, $item, $args, $depth);
// Ensure final title is string
if (! is_string($title)) {
$title = 'Untitled';
}
return $title;
}
/**
* Determine if icons should be shown
*/
private function should_show_icons($args, $depth, $item)
{
// Check if args is valid object
if (! is_object($args)) {
return false;
}
// Check if theme_location is set and matches header-menu
if (! isset($args->theme_location) || $args->theme_location !== 'header-menu') {
return false;
}
// Check if depth is 0 (top level)
if ($depth !== 0) {
return false;
}
// Check if item has classes and is array
if (! isset($item->classes) || ! is_array($item->classes)) {
return false;
}
// Check if item has children
return in_array('menu-item-has-children', $item->classes);
}
/**
* Get icon markup
*/
private function get_icon_markup()
{
return '<span class="icon-wrapper menu-icon-toggle" aria-hidden="true">' .
'<svg class="icon icon-closed"><use href="#ph--plus-bold"></use></svg>' .
'<svg class="icon icon-open"><use href="#ph--minus-bold"></use></svg>' .
'</span>';
}
/**
* Override the walk method to enforce two-level structure
*/
public function walk($elements, $max_depth, ...$args)
{
// Force maximum depth to 2 levels
$max_depth = 2;
// Call parent walk method with enforced depth
return parent::walk($elements, $max_depth, ...$args);
}
}
/* CUSTOM GRAVATAR */
function custom_comment_author_avatar($avatar, $id_or_email, $size, $default, $alt, $args)
{
// Only modify if it's default Gravatar
if (strpos($avatar, 'gravatar.com/avatar/') !== false && strpos($avatar, 'd=mm') !== false) {
$name = '';
// Get the comment author's name
if (is_object($id_or_email) && isset($id_or_email->comment_ID)) {
$comment = $id_or_email;
$name = $comment->comment_author;
} elseif (is_numeric($id_or_email)) {
$comment = get_comment($id_or_email);
if ($comment) {
$name = $comment->comment_author;
}
} elseif (is_string($id_or_email)) {
// Try to find comment by email
$comments = get_comments(['author_email' => $id_or_email, 'number' => 1]);
if ($comments) {
$name = $comments[0]->comment_author;
}
}
if (! empty($name)) {
// Get initials (first letter of first and last name)
$name_parts = explode(' ', $name);
$initials = '';
$initials .= isset($name_parts[0]) ? strtoupper(substr($name_parts[0], 0, 1)) : '';
$initials .= isset($name_parts[count($name_parts) - 1]) ? strtoupper(substr($name_parts[count($name_parts) - 1], 0, 1)) : '';
if (! empty($initials)) {
// Generate consistent color based on name
// $background_color = '#' . substr(md5($name), 0, 6);
$text_color = '#ffffff';
$colors = [ 'var( --brand-400 )', 'var( --brand-600 )', 'var( --warm-gray-500 )', 'var( --warm-gray-600 )', 'var( --warm-gray-800 )' ];
$background_color = $colors[abs(crc32($name)) % count($colors)];
// Create the avatar HTML
$avatar = sprintf(
'<div class="comment-author-initials" style="width:%dpx; height:%dpx; background-color:%s; color:%s; border-radius:50%%; display:inline-flex; align-items:center; justify-content:center; ">%s</div>',
$size,
$size,
$background_color,
$text_color,
$initials
);
}
}
}
return $avatar;
}
add_filter('get_avatar', 'custom_comment_author_avatar', 10, 6);
// Remove "Category:", "Tag:", etc. from archive titles
add_filter('get_the_archive_title', 'remove_archive_title_prefix');
function remove_archive_title_prefix($title)
{
if (is_category()) {
$title = single_cat_title('', false);
} elseif (is_tag()) {
$title = single_tag_title('', false);
} elseif (is_author()) {
$title = get_the_author();
} elseif (is_tax()) {
$title = single_term_title('', false);
} elseif (is_post_type_archive()) {
$title = post_type_archive_title('', false);
}
return $title;
}
/* * * Responsive images * * */
function copim_image_sizes()
{
// Modify defaults
update_option('thumbnail_size_w', 300);
update_option('thumbnail_size_h', 300);
update_option('thumbnail_crop', 1);
update_option('medium_size_w', 600);
update_option('medium_size_h', 0);
update_option('large_size_w', 1200);
update_option('large_size_h', 0);
// Custom sizes
add_image_size('hero', 2048, 0, false); // For 4K displays and retina screens
}
add_action('after_switch_theme', 'copim_image_sizes');
/* * * Enqueue scripts and styles * * */
function copim_enqueue_styles()
{
wp_register_style('copim-styles', get_template_directory_uri() . '/resources/css/main.min.css', '1.0', true);
wp_enqueue_style('copim-styles');
// Swiper homepage
if (is_page_template('page-home.php')) {
wp_register_style('swiper-styles', get_template_directory_uri() . '/resources/css/swiper-bundle.min.css', '11.2.6', true);
wp_enqueue_style('swiper-styles');
}
}
add_action('wp_enqueue_scripts', 'copim_enqueue_styles');
function copim_enqueue_scripts()
{
// Add JS to display years only on 'all posts' template
if (is_page_template('page-all-posts.php')) {
wp_enqueue_script(
'alm-year-headings',
get_template_directory_uri() . '/resources/js/alm-year-headings.min.js',
['jquery', 'ajax-load-more'],
null,
true
);
}
// Swiper homepage
if (is_page_template('page-home.php')) {
wp_register_script('swiper_scripts', get_template_directory_uri() . '/resources/js/swiper-bundle.min.js', '11.2.6', true);
wp_enqueue_script('swiper_scripts');
}
// Add comments if required
if (is_singular() && comments_open() && get_option('thread_comments')) {
wp_enqueue_script('comment-reply');
}
wp_register_script('copim_scripts', get_template_directory_uri() . '/resources/js/scripts.min.js', ['jquery'], '1.0', true);
wp_enqueue_script('copim_scripts');
}
add_action('wp_enqueue_scripts', 'copim_enqueue_scripts');
/* * * ADD SVG SPRITE TO HEADER * * */
function copim_output_svg_sprite()
{
$svg_sprite_path = get_template_directory() . '/resources/img/icons.svg';
if (file_exists($svg_sprite_path) && is_readable($svg_sprite_path)) {
$svg_content = file_get_contents($svg_sprite_path);
if ($svg_content && strpos($svg_content, '<svg') !== false) {
echo $svg_content;
}
}
}
add_action('wp_head', 'copim_output_svg_sprite', 1);
/* * * CHANGE DEFAULT TABLE PRESS HEADING * * */
add_filter('tablepress_print_name_html_tag', 'change_tablepress_heading_tag');
function change_tablepress_heading_tag($tag)
{
return 'h5';
}
/* * * VIDEO EMBED RESPONSIVE RESIZE * * */
add_filter('embed_oembed_html', function ($html, $url, $attr, $post_id) {
if (strpos($html, 'youtube.com') !== false ||
strpos($html, 'youtu.be') !== false ||
strpos($html, 'vimeo.com') !== false) {
return '<div class="embed-responsive embed-responsive-16by9">' . $html . '</div>';
}
return $html;
}, 10, 4);
/* * * PREPOPULATE ACF POSTS * * */
add_filter('acf/load_value/name=rte_flexible_content', 'set_default_flexible_layout', 10, 3);
function set_default_flexible_layout($value, $post_id, $field)
{
// Only set default if no existing value
if (! $value) {
$value = [
[
'acf_fc_layout' => 'rte_content',
'rte_content_block' => '',
],
];
}
return $value;
}
/* * * COMMENTS OUTPUT * * */
function custom_comment_template($comment, $args, $depth)
{
$GLOBALS['comment'] = $comment;
$comment_reply_count = get_comments([
'parent' => $comment->comment_ID,
'count' => true,
]);
?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
<div id="comment-<?php comment_ID(); ?>" class="comment-wrapper">
<div class="comment-author vcard">
<?php echo get_avatar($comment, $args['avatar_size']); ?>
<div class="comment-meta commentmetadata">
<cite class="comment-author-name"><h6><?php echo get_comment_author_link(); ?></h6></cite>
<div class="comment-date"><?php
// Date & Time (without link)
printf(
esc_html__('%1$s at %2$s', 'copim'),
get_comment_date(),
get_comment_time()
); ?></div>
</div>
<?php // Show "Replies (x)" only if there are replies
if ($comment_reply_count > 0) {
echo ' <div class="reply-count">' . sprintf(
_n('Reply (%d)', 'Replies (%d)', $comment_reply_count, 'copim'),
$comment_reply_count
) . '</div>';
}
?>
</div>
<?php if ($comment->comment_approved == '0') : ?>
<em class="comment-awaiting-moderation"><?php esc_html_e('Your comment is awaiting moderation.', 'copim'); ?></em>
<?php endif; ?>
<div class="comment-text">
<?php comment_text(); ?>
</div>
<div class="reply">
<?php
// Normal reply button (without count)
comment_reply_link(array_merge(
$args,
[
'depth' => $depth,
'max_depth' => $args['max_depth'],
'reply_text' => __('Reply', 'copim'),
]
));
?>
</div>
</div>
<?php
}
/* ACF POST SUMMARIES */
function get_intro_excerpt($post_id = null, $char_limit = 120)
{
if (! $post_id) {
$post_id = get_the_ID();
}
// Get raw WYSIWYG content
$raw_content = get_field('rte_content_introduction', $post_id);
$raw_content = is_string($raw_content) ? wp_kses_post($raw_content) : '';
if (! $raw_content) {
return '';
}
// Strip all HTML tags (WYSIWYG formatting)
$text_content = wp_strip_all_tags($raw_content);
// Trim to character limit without breaking words
if (strlen($text_content) > $char_limit) {
$text_content = substr($text_content, 0, $char_limit);
$text_content = preg_replace('/\s+?(\S+)?$/', '', $text_content); // don't cut mid-word
$text_content .= '…';
}
return $text_content;
}
/* ACF PAGE SUMMARIES */
function get_page_excerpt($post_id = null, $char_limit = 120)
{
if (! $post_id) {
$post_id = get_the_ID();
}
// Get raw WYSIWYG content
$raw_content = get_field('header_subtitle', $post_id);
$raw_content = is_string($raw_content) ? wp_kses_post($raw_content) : '';
if (! $raw_content) {
return '';
}
// Strip all HTML tags (WYSIWYG formatting)
$text_content = wp_strip_all_tags($raw_content);
// Trim to character limit without breaking words
if (strlen($text_content) > $char_limit) {
$text_content = substr($text_content, 0, $char_limit);
$text_content = preg_replace('/\s+?(\S+)?$/', '', $text_content); // don't cut mid-word
$text_content .= '…';
}
return $text_content;
}
// Auto-populate author names field for searchability
add_action('acf/save_post', 'populate_author_names_field', 20);
function populate_author_names_field($post_id)
{
if (get_post_type($post_id) !== 'post') {
return;
}
$authors = get_field('post_authors', $post_id);
if ($authors && is_array($authors)) {
$author_names = [];
foreach ($authors as $author) {
if (is_object($author) && isset($author->post_title)) {
$author_names[] = $author->post_title;
}
}
$author_string = implode(', ', $author_names);
// Update the hidden SCF field
update_field('author_names_hidden', $author_string, $post_id);
} else {
// Clear the field if no authors
update_field('author_names_hidden', '', $post_id);
}
}
// Hide the author_names_hidden field from non-admin users and make it read-only for admins
add_filter('acf/prepare_field/name=author_names_hidden', function ($field) {
if (! current_user_can('manage_options')) {
return false; // hide from non-admins
}
// Make field read-only for admins
$field['readonly'] = true;
$field['disabled'] = true;
return $field;
});
// Safe sanitization function
function safe_kses_post($content)
{
if (is_string($content) && ! empty($content)) {
return wp_kses_post($content);
} elseif (is_array($content)) {
error_log('Attempted to sanitize array with wp_kses_post: ' . print_r($content, true));
return '';
} else {
return '';
}
}
// Safe URL function
function safe_esc_url($url)
{
if (is_string($url) && ! empty($url)) {
return esc_url($url);
} else {
return '';
}
}
// Create default menus on theme activation
function copim_create_default_menus()
{
// Define menu configurations
$menu_configs = [
'header-menu' => [
'name' => 'Header Menu',
'items' => [
['title' => 'Home', 'url' => home_url('/')],
],
],
'footer-menu-1' => [
'name' => 'Footer Menu 1',
'items' => [
['title' => 'About', 'url' => home_url('/about/')],
],
],
'footer-menu-2' => [
'name' => 'Footer Menu 2',
'items' => [
['title' => 'Contact', 'url' => home_url('/contact/')],
],
],
'footer-menu-credits' => [
'name' => 'Footer Menu Credits',
'items' => [
['title' => 'Privacy Policy', 'url' => home_url('/privacy-policy/')],
],
],
];
// Get current theme locations
$locations = get_theme_mod('nav_menu_locations', []);
foreach ($menu_configs as $location => $config) {
// Skip if location already has a menu
if (! empty($locations[$location]) && wp_get_nav_menu_object($locations[$location])) {
continue;
}
// Check if menu exists by name
$existing_menu = get_term_by('name', $config['name'], 'nav_menu');
if ($existing_menu) {
// Assign existing menu to location if not already assigned
if (empty($locations[$location])) {
$locations[$location] = $existing_menu->term_id;
set_theme_mod('nav_menu_locations', $locations);
}
continue;
}
// Create new menu
$menu_id = wp_create_nav_menu($config['name']);
if (is_wp_error($menu_id)) {
// Log error but continue with other menus
error_log("Copim Theme: Failed to create menu '{$config['name']}': " . $menu_id->get_error_message());
continue;
}
// Add menu items
foreach ($config['items'] as $item) {
$menu_item_id = wp_update_nav_menu_item($menu_id, 0, [
'menu-item-title' => $item['title'],
'menu-item-url' => $item['url'],
'menu-item-status' => 'publish',
'menu-item-type' => 'custom',
]);
// Log any errors with menu item creation
if (is_wp_error($menu_item_id)) {
error_log("Copim Theme: Failed to create menu item '{$item['title']}' for menu '{$config['name']}': " . $menu_item_id->get_error_message());
}
}
// Assign to theme location
$locations[$location] = $menu_id;
set_theme_mod('nav_menu_locations', $locations);
}
}
// Run only on theme activation
add_action('after_switch_theme', 'copim_create_default_menus');
// Optional: Run once on init if no menus exist (more conservative approach)
function copim_check_menus_once()
{
// Only run this check once per session
if (! get_transient('copim_menus_checked')) {
$locations = get_theme_mod('nav_menu_locations', []);
$has_menus = ! empty(array_filter($locations));
if (! $has_menus) {
copim_create_default_menus();
}
set_transient('copim_menus_checked', true, DAY_IN_SECONDS);
}
}
add_action('init', 'copim_check_menus_once');
// Check for Theme dependencies upon activation and add notices for missing plugins
function copim_check_theme_dependency()
{
if (! function_exists('get_field')) {
add_action('admin_notices', function () {
echo '<div class="notice notice-error"><p>';
echo '<strong>Copim Theme:</strong> This theme requires either the Advanced or Secure Custom Fields plugin to function properly. ';
echo '<a href="' . admin_url('plugin-install.php?s=secure+custom+fields&tab=search&type=term') . '">Install SCF or ACF now</a>';
echo '</p></div>';
});
}
if (! function_exists('ajax_load_more')) {
add_action('admin_notices', function () {
echo '<div class="notice notice-error"><p>';
echo '<strong>Copim Theme:</strong> This theme requires the Ajax Load More plugin to function properly. ';
echo '<a href="' . admin_url('plugin-install.php?s=ajax+load+more&tab=search&type=term') . '">Install Ajax Load More now</a>';
echo '</p></div>';
});
}
if (! file_exists(WP_PLUGIN_DIR . '/classic-editor/classic-editor.php') || ! is_plugin_active('classic-editor/classic-editor.php')) {
add_action('admin_notices', function () {
echo '<div class="notice notice-error"><p>';
echo '<strong>Copim Theme:</strong> This theme requires the Classic Editor plugin to be installed and activated. ';
echo '<a href="' . admin_url('plugin-install.php?s=classic+editor&tab=search&type=term') . '">Install Classic Editor now</a>';
echo '</p></div>';
});
}
}
add_action('admin_init', 'copim_check_theme_dependency');
// Remove Block Editor CSS this theme uses Classic Editor only
function remove_block_editor_styles()
{
wp_dequeue_style('wp-block-library');
wp_deregister_style('wp-block-library');
}
add_action('wp_enqueue_scripts', 'remove_block_editor_styles', 100);
/* YOAST SEO FEATURED IMAGE FOR ACF FIELDS */
add_action('acf/save_post', 'auto_set_featured_image_from_header', 20);
function auto_set_featured_image_from_header($post_id) {
// Only run on posts and pages
if (!in_array(get_post_type($post_id), ['post', 'page'])) {
return;
}
// Check if header_image field has a value
$header_image = get_field('header_image', $post_id);
if ($header_image && isset($header_image['ID'])) {
// Set as featured image
set_post_thumbnail($post_id, $header_image['ID']);
} else {
// Remove featured image if no header image
delete_post_thumbnail($post_id);
}
}
/* YOAST SEO CUSTOM VARIABLE WITH FALLBACK LOGIC - %%smart_desc%% */
// This creates a smart fallback system that Yoast's built-in %%cf_%% variables can't do
// Based on: https://gist.github.com/amboutwe/550c10ede7d065d9264930f5480ca748
// Define the custom replacement callback
function get_smart_description() {
if (!is_singular()) {
return '';
}
$post_id = get_queried_object_id();
// Validate post_id
if (!is_numeric($post_id) || $post_id <= 0) {
return '';
}
// Helper to normalize any field to plain text
$to_text = function ($val) {
if (empty($val)) {
return '';
}
if (is_array($val)) {
$val = implode(' ', array_map('wp_strip_all_tags', $val));
}
$val = (string) $val;
$val = wp_strip_all_tags($val);
$val = sanitize_text_field($val);
return trim($val);
};
// Get ACF field with fallback to post_meta
$get_meta = function ($key) use ($post_id, $to_text) {
if (empty($key) || !is_string($key)) {
return '';
}
if (function_exists('get_field')) {
$v = get_field($key, $post_id);
if (!empty($v)) return $to_text($v);
}
$v = get_post_meta($post_id, $key, true);
return $to_text($v);
};
// Priority order of fields to check
$candidates = [
$get_meta('rte_content_introduction'),
$get_meta('post_excerpt'),
$get_meta('header_subtitle'),
$get_meta('page_excerpt'),
$get_meta('header_subtitle'),
$get_meta('author_biography'),
has_excerpt($post_id) ? get_the_excerpt($post_id) : '',
get_post_field('post_content', $post_id),
];
foreach ($candidates as $val) {
$val = $to_text($val);
if ($val !== '') {
// Additional sanitization for meta description
$val = wp_strip_all_tags($val);
$val = html_entity_decode($val, ENT_QUOTES | ENT_HTML5);
// Remove line breaks and normalize whitespace
$val = preg_replace('/\s+/', ' ', $val);
$val = trim($val);
// Ensure we have content after all cleaning
if (!empty($val)) {
return mb_substr($val, 0, 158) . (mb_strlen($val) > 158 ? '…' : '');
}
}
}
return '';
}
// Define the action for register yoast_variable replacements
function register_smart_desc_variable() {
if (!function_exists('wpseo_register_var_replacement')) {
return;
}
wpseo_register_var_replacement('%%smart_desc%%', 'get_smart_description', 'advanced', 'Smart description with ACF field fallbacks');
}
// Add action
add_action('wpseo_register_extra_replacements', 'register_smart_desc_variable');