-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
197 lines (170 loc) · 5.47 KB
/
Copy pathfunctions.php
File metadata and controls
197 lines (170 loc) · 5.47 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
<?php
/**
* WM Base Theme Functions
*
* @package WM_Base_Theme
*/
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly.
}
/**
* Theme version
*/
define('WM_THEME_VERSION', '1.0.0');
define('WM_THEME_PATH', get_template_directory());
define('WM_THEME_URI', get_template_directory_uri());
/**
* Theme setup
*/
function wm_theme_setup() {
// Add theme support
add_theme_support('automatic-feed-links');
add_theme_support('title-tag');
add_theme_support('post-thumbnails');
add_theme_support('html5', array(
'search-form',
'comment-form',
'comment-list',
'gallery',
'caption',
'style',
'script',
));
add_theme_support('custom-logo');
add_theme_support('responsive-embeds');
add_theme_support('align-wide');
add_theme_support('editor-styles');
add_editor_style('dist/css/main.css');
// Register navigation menus
register_nav_menus(array(
'primary' => __('Primary Menu', 'wm-base-theme'),
'footer' => __('Footer Menu', 'wm-base-theme'),
));
// Set content width
$GLOBALS['content_width'] = 1200;
}
add_action('after_setup_theme', 'wm_theme_setup');
/**
* Load and cache the Vite asset manifest.
* Cached in a transient keyed by file modification time — auto-invalidates on rebuild.
*/
function wm_get_manifest(): ?array {
$manifest_path = WM_THEME_PATH . '/dist/.vite/manifest.json';
if (!file_exists($manifest_path)) {
return null;
}
$mtime = filemtime($manifest_path);
$cache_key = 'wm_manifest_' . $mtime;
$cached = get_transient($cache_key);
if ($cached !== false) {
return $cached;
}
$manifest = json_decode(file_get_contents($manifest_path), true);
set_transient($cache_key, $manifest, DAY_IN_SECONDS);
return $manifest;
}
/**
* Enqueue scripts and styles
*/
function wm_enqueue_assets() {
$manifest = wm_get_manifest();
if (!$manifest) {
if (defined('WP_DEBUG') && WP_DEBUG) {
add_action('admin_notices', function () {
echo '<div class="notice notice-error"><p><strong>WM Theme:</strong> Asset manifest not found. Run <code>npm run build</code> to generate <code>dist/.vite/manifest.json</code>.</p></div>';
});
}
return;
}
// Enqueue main stylesheet
$css_entry = $manifest['src/assets/styles/main.scss'] ?? null;
if ($css_entry && isset($css_entry['file'])) {
wp_enqueue_style(
'wm-main-style',
WM_THEME_URI . '/dist/' . $css_entry['file'],
array(),
WM_THEME_VERSION
);
}
// Enqueue main script
$js_entry = $manifest['src/assets/scripts/main.ts'] ?? null;
if ($js_entry && isset($js_entry['file'])) {
wp_enqueue_script(
'wm-main-script',
WM_THEME_URI . '/dist/' . $js_entry['file'],
array(),
WM_THEME_VERSION,
true
);
}
// Localize script for WordPress data
wp_localize_script('wm-main-script', 'wmTheme', array(
'ajaxUrl' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('wm-theme-nonce'),
'themeUri' => WM_THEME_URI,
));
}
add_action('wp_enqueue_scripts', 'wm_enqueue_assets');
/**
* Enqueue admin assets
*/
function wm_enqueue_admin_assets($hook) {
$manifest = wm_get_manifest();
if ($manifest && isset($manifest['src/assets/scripts/admin.ts'])) {
$js_file = $manifest['src/assets/scripts/admin.ts'];
wp_enqueue_script(
'wm-admin-script',
WM_THEME_URI . '/dist/' . $js_file['file'],
array(),
WM_THEME_VERSION,
true
);
}
}
add_action('admin_enqueue_scripts', 'wm_enqueue_admin_assets');
/**
* Add favicon links
*/
function wm_add_favicon() {
$favicon_path = WM_THEME_URI . '/imgs/favicons';
// Check if favicon files exist and add them
$favicon_ico = WM_THEME_PATH . '/imgs/favicons/favicon.ico';
$favicon_png = WM_THEME_PATH . '/imgs/favicons/favicon.png';
$favicon_svg = WM_THEME_PATH . '/imgs/favicons/favicon.svg';
$apple_touch = WM_THEME_PATH . '/imgs/favicons/apple-touch-icon.png';
// Standard favicon
if (file_exists($favicon_ico)) {
echo '<link rel="icon" type="image/x-icon" href="' . esc_url($favicon_path . '/favicon.ico') . '">' . "\n";
}
// PNG favicon (modern browsers)
if (file_exists($favicon_png)) {
echo '<link rel="icon" type="image/png" sizes="32x32" href="' . esc_url($favicon_path . '/favicon.png') . '">' . "\n";
}
// SVG favicon (modern browsers)
if (file_exists($favicon_svg)) {
echo '<link rel="icon" type="image/svg+xml" href="' . esc_url($favicon_path . '/favicon.svg') . '">' . "\n";
}
// Apple Touch Icon
if (file_exists($apple_touch)) {
echo '<link rel="apple-touch-icon" sizes="180x180" href="' . esc_url($favicon_path . '/apple-touch-icon.png') . '">' . "\n";
}
// Web manifest (optional)
$manifest = WM_THEME_PATH . '/imgs/favicons/site.webmanifest';
if (file_exists($manifest)) {
echo '<link rel="manifest" href="' . esc_url($favicon_path . '/site.webmanifest') . '">' . "\n";
}
}
add_action('wp_head', 'wm_add_favicon', 1);
/**
* Include theme files
*/
require_once WM_THEME_PATH . '/src/inc/theme-functions.php';
require_once WM_THEME_PATH . '/src/inc/acf-setup.php';
require_once WM_THEME_PATH . '/src/inc/block-setup.php';
require_once WM_THEME_PATH . '/src/inc/cpt-setup.php';
require_once WM_THEME_PATH . '/src/inc/woocommerce-setup.php';
require_once WM_THEME_PATH . '/src/inc/schema.php';
require_once WM_THEME_PATH . '/src/inc/react-islands.php';
require_once WM_THEME_PATH . '/src/inc/performance.php';
require_once WM_THEME_PATH . '/src/inc/breadcrumbs.php';
require_once WM_THEME_PATH . '/src/inc/customizer.php';