Repository navigation
Expand file tree
/
Copy pathfunctions.php
More file actions
258 lines (229 loc) · 8.19 KB
/
Copy pathfunctions.php
File metadata and controls
258 lines (229 loc) · 8.19 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
<?php
/**
* Winespring functions and definitions
*
* @package Winespring
* @since 0.1.0
*/
namespace Winespring;
require_once get_template_directory() . '/inc/store-features.php';
if ( is_admin() ) {
require_once get_template_directory() . '/inc/plugin-recommendations.php';
}
/**
* Set up theme defaults and register various WordPress features.
*/
function winespring_setup() {
// Make theme available for translation.
load_theme_textdomain( 'winespring', get_template_directory() . '/languages' );
// Enqueue editor styles.
add_editor_style( 'style.css' );
// Featured images: the templates place wp:post-featured-image blocks.
add_theme_support( 'post-thumbnails' );
// WooCommerce.
add_theme_support( 'woocommerce' );
add_theme_support( 'wc-product-gallery-zoom' );
add_theme_support( 'wc-product-gallery-lightbox' );
add_theme_support( 'wc-product-gallery-slider' );
}
add_action( 'after_setup_theme', __NAMESPACE__ . '\winespring_setup' );
/**
* Register the 'menu' template part area, required by the Aludra mega-menu block.
*
* @param array $areas Existing template part areas.
* @return array Modified template part areas.
*/
function winespring_template_part_areas( $areas ) {
$areas[] = array(
'area' => 'menu',
'area_tag' => 'nav',
'label' => __( 'Menu', 'winespring' ),
'description' => __( 'Template parts for navigation and mega menu content.', 'winespring' ),
'icon' => 'navigation',
);
return $areas;
}
add_filter( 'default_wp_template_part_areas', __NAMESPACE__ . '\winespring_template_part_areas' );
/**
* Register the pattern category used by the theme's page patterns.
*/
function winespring_register_pattern_categories() {
register_block_pattern_category(
'winespring-pages',
array( 'label' => __( 'Winespring Pages', 'winespring' ) )
);
}
add_action( 'init', __NAMESPACE__ . '\winespring_register_pattern_categories' );
/**
* Enqueue theme styles.
*/
function winespring_enqueue_styles() {
wp_enqueue_style(
'winespring-style',
get_template_directory_uri() . '/style.css',
array(),
wp_get_theme()->get( 'Version' )
);
}
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\winespring_enqueue_styles' );
/**
* Enqueue WooCommerce-specific styles, if present.
*/
function winespring_enqueue_woocommerce_styles() {
if ( ! class_exists( 'WooCommerce' ) ) {
return;
}
if ( file_exists( get_template_directory() . '/assets/css/woocommerce.css' ) ) {
wp_enqueue_style(
'winespring-woocommerce-style',
get_template_directory_uri() . '/assets/css/woocommerce.css',
array( 'winespring-style' ),
(string) filemtime( get_template_directory() . '/assets/css/woocommerce.css' )
);
}
}
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\winespring_enqueue_woocommerce_styles' );
/**
* WooCommerce block templates shipped by this theme.
*
* @return string[] Template slugs.
*/
function winespring_woocommerce_template_slugs() {
return array( 'archive-product', 'single-product', 'product-search-results', 'coming-soon', 'order-confirmation' );
}
/**
* WooCommerce block template parts shipped by this theme.
*
* These override WooCommerce's own per-product-type add-to-cart layouts, which
* the `woocommerce/add-to-cart-with-options` block renders.
*
* @return string[] Template part slugs.
*/
function winespring_woocommerce_template_part_slugs() {
return array(
'simple-product-add-to-cart-with-options',
'variable-product-add-to-cart-with-options',
);
}
/**
* Register the WooCommerce integration hooks that apply to this site.
*
* The theme ships store templates but does not require WooCommerce, so which
* hooks are needed depends on whether the plugin is active.
*/
function winespring_woocommerce_hooks() {
if ( class_exists( 'WooCommerce' ) ) {
return;
}
add_filter( 'default_wp_template_part_areas', __NAMESPACE__ . '\winespring_add_to_cart_template_part_area' );
add_filter( 'get_block_templates', __NAMESPACE__ . '\winespring_filter_woocommerce_templates', 10, 3 );
add_filter( 'get_block_file_template', __NAMESPACE__ . '\winespring_filter_woocommerce_file_template', 10, 3 );
}
add_action( 'after_setup_theme', __NAMESPACE__ . '\winespring_woocommerce_hooks' );
/**
* Hide the store templates, and the store blocks inside template parts, when
* WooCommerce is not active.
*
* Without this, `single-product` and `archive-product` are listed in the Site
* Editor on a site that cannot render them, and the header's mini cart shows an
* unsupported-block placeholder.
*
* @param \WP_Block_Template[] $query_result Templates found for the query.
* @param array $query Query arguments.
* @param string $template_type 'wp_template' or 'wp_template_part'.
* @return \WP_Block_Template[] Filtered templates.
*/
function winespring_filter_woocommerce_templates( $query_result, $query, $template_type ) {
if ( 'wp_template_part' === $template_type ) {
foreach ( $query_result as $template ) {
$template->content = winespring_strip_woocommerce_blocks( $template->content );
}
$store_slugs = winespring_woocommerce_template_part_slugs();
} else {
$store_slugs = winespring_woocommerce_template_slugs();
}
return array_values(
array_filter(
$query_result,
static function ( $template ) use ( $store_slugs ) {
return ! in_array( $template->slug, $store_slugs, true );
}
)
);
}
/**
* Strip store blocks from a file-based template part when WooCommerce is not active.
*
* Covers the front end, which resolves template parts through this filter rather
* than through `get_block_templates`.
*
* @param \WP_Block_Template|null $block_template Template returned for the file.
* @param string $id Template identifier.
* @param string $template_type 'wp_template' or 'wp_template_part'.
* @return \WP_Block_Template|null Filtered template.
*/
function winespring_filter_woocommerce_file_template( $block_template, $id, $template_type ) {
if ( 'wp_template_part' !== $template_type || ! $block_template instanceof \WP_Block_Template ) {
return $block_template;
}
$block_template->content = winespring_strip_woocommerce_blocks( $block_template->content );
return $block_template;
}
/**
* Remove store blocks from template part markup.
*
* Block markup in a file cannot be made conditional, so the blocks the theme
* places in `parts/` are removed from the part's content instead.
*
* @param string $content Template part markup.
* @return string Markup without store blocks.
*/
function winespring_strip_woocommerce_blocks( $content ) {
if ( ! is_string( $content ) || '' === $content ) {
return $content;
}
return (string) preg_replace(
'#<!--\s+wp:woocommerce/(?:mini-cart|customer-account)\b.*?/-->\s*#s',
'',
$content
);
}
/**
* Register the 'add-to-cart-with-options' template part area.
*
* WooCommerce registers this area itself; the theme only stands in when the
* plugin is inactive, so that its add-to-cart parts do not resolve to an
* unknown area while they are being filtered out.
*
* @param array $areas Existing template part areas.
* @return array Modified template part areas.
*/
function winespring_add_to_cart_template_part_area( $areas ) {
$areas[] = array(
'area' => 'add-to-cart-with-options',
'area_tag' => 'div',
'label' => __( 'Add to Cart + Options', 'winespring' ),
'description' => __( 'Add to cart layouts for each product type.', 'winespring' ),
'icon' => 'cart',
);
return $areas;
}
/**
* Register the store home patterns only where they can render.
*
* `winespring/home-store` and `winespring/home-shop` place a WooCommerce
* product collection, so without WooCommerce they would be offered in the
* inserter and insert an unsupported block. Pattern files register themselves,
* so they are unregistered again here when the plugin is inactive.
*/
function winespring_maybe_unregister_store_home_pattern() {
if ( class_exists( 'WooCommerce' ) ) {
return;
}
foreach ( array( 'winespring/home-store', 'winespring/home-shop' ) as $pattern ) {
if ( \WP_Block_Patterns_Registry::get_instance()->is_registered( $pattern ) ) {
unregister_block_pattern( $pattern );
}
}
}
add_action( 'init', __NAMESPACE__ . '\winespring_maybe_unregister_store_home_pattern', 999 );