-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
154 lines (128 loc) · 3.65 KB
/
functions.php
File metadata and controls
154 lines (128 loc) · 3.65 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
<?php
/**
* Core theme functions.
*
* @package Quad
*/
/**
* Theme setup.
*/
function quad_setup() {
// Add default posts and comments RSS feed links to head.
add_theme_support( 'automatic-feed-links' );
// Let WordPress manage the document title.
add_theme_support( 'title-tag' );
// Enable support for Post Thumbnails on posts and pages.
add_theme_support( 'post-thumbnails' );
// Menu registrations.
register_nav_menus(
array(
'primary' => __( 'Primary', 'quad' ),
)
);
// Switch default core markup for search form, comment form, and comments to output valid HTML5.
add_theme_support(
'html5',
array(
'search-form',
'comment-form',
'comment-list',
'gallery',
'caption',
)
);
}
/**
* Helper: read the Vite manifest so we can enqueue hashed asset filenames.
*
* @return array|null
*/
function quad_asset_manifest() {
static $manifest = null;
static $manifest_path = null;
if ( null !== $manifest ) {
return $manifest;
}
// Prefer Vite 5+ default location (dist/.vite/manifest.json), fall back to dist/manifest.json.
$paths = array(
get_theme_file_path( '/dist/.vite/manifest.json' ),
get_theme_file_path( '/dist/manifest.json' ),
);
foreach ( $paths as $path ) {
if ( file_exists( $path ) ) {
$manifest_path = $path;
break;
}
}
if ( ! $manifest_path ) {
return null;
}
if ( function_exists( 'wp_json_file_decode' ) ) {
$manifest = wp_json_file_decode( $manifest_path, array( 'associative' => true ) );
} else {
$manifest = json_decode( file_get_contents( $manifest_path ), true ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
}
if ( ! is_array( $manifest ) ) {
return null;
}
return $manifest;
}
/**
* Build asset URLs from the manifest entry.
*
* @param string $entry Entry key in the manifest.
* @return array|null
*/
function quad_asset_urls( $entry ) {
$manifest = quad_asset_manifest();
if ( ! $manifest || ! isset( $manifest[ $entry ] ) ) {
return null;
}
$asset = $manifest[ $entry ];
$base = trailingslashit( get_stylesheet_directory_uri() . '/dist' );
return array(
'file' => isset( $asset['file'] ) ? $base . $asset['file'] : null,
'css' => ! empty( $asset['css'] ) ? array_map(
static function ( $path ) use ( $base ) {
return $base . $path;
},
$asset['css']
) : array(),
);
}
/**
* Enqueue scripts and styles.
*/
function quad_scripts() {
$theme = wp_get_theme();
$version = $theme->get( 'Version' );
// Remove jQuery.
wp_deregister_script( 'jquery' );
$asset = quad_asset_urls( 'src/app.js' );
// Styles.
if ( $asset && ! empty( $asset['css'] ) ) {
foreach ( $asset['css'] as $index => $css_path ) {
$handle = 0 === $index ? 'css/app' : "css/app-{$index}";
wp_enqueue_style( $handle, $css_path, array(), $version );
}
} else {
wp_enqueue_style( 'css/app', get_stylesheet_directory_uri() . '/dist/css/app.css', array(), $version );
}
// Main JavaScript.
$script_path = ( $asset && $asset['file'] )
? $asset['file']
: get_stylesheet_directory_uri() . '/dist/app.js';
wp_enqueue_script( 'js/app', $script_path, array(), $version, true );
}
// Implement setup + scripts.
add_action( 'after_setup_theme', 'quad_setup' );
add_action( 'wp_enqueue_scripts', 'quad_scripts' );
// Function extras.
require get_template_directory() . '/inc/acf.php';
require get_template_directory() . '/inc/assets.php';
require get_template_directory() . '/inc/customizer.php';
require get_template_directory() . '/inc/gutenberg.php';
// Disable redirection if site in development mode.
if ( isset( $_ENV['WP_ENV'] ) && 'development' === $_ENV['WP_ENV'] ) {
remove_action( 'template_redirect', 'redirect_canonical' );
}