Skip to content

Commit 158a93e

Browse files
authored
Merge pull request #75 from WebDevStudios/hotfix/patterns
Hotfix/patterns
2 parents 4600529 + 306f772 commit 158a93e

File tree

6 files changed

+101
-71
lines changed

6 files changed

+101
-71
lines changed

assets/js/editor.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,23 @@ wp.domReady(() => {
3333
}
3434
}
3535
});
36+
37+
/* global alert */
38+
if (window.localStorage) {
39+
window.addEventListener('storage', function (event) {
40+
if (event.key === 'wdsbt_patterns_flushed') {
41+
if (window.wp && window.wp.data) {
42+
window.wp.data
43+
.dispatch('core')
44+
.invalidateResolution('getBlockPatterns');
45+
window.wp.data
46+
.dispatch('core')
47+
.invalidateResolution('getBlockPatternCategories');
48+
// eslint-disable-next-line no-alert
49+
alert(
50+
'Block patterns and categories have been refreshed. Please re-open the inserter to see new items.'
51+
);
52+
}
53+
}
54+
});
55+
}

assets/scss/base/_index.scss

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@
22
@use 'global';
33
@use 'pagination';
44
@use 'forms';
5-
@use 'options';

assets/scss/base/options.scss

Lines changed: 0 additions & 13 deletions
This file was deleted.

inc/hooks/register-block-pattern-categories.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ function register_dynamic_block_pattern_categories() {
3434
);
3535
}
3636
}
37-
add_action( 'init', __NAMESPACE__ . '\register_dynamic_block_pattern_categories' );
37+
add_action( 'init', __NAMESPACE__ . '\register_dynamic_block_pattern_categories', 9 );

inc/hooks/register-block-patterns.php

Lines changed: 0 additions & 27 deletions
This file was deleted.

inc/setup/theme-options.php

Lines changed: 80 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,22 @@ function render_settings_page() {
4949
echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__( 'Settings saved.', 'wdsbt' ) . '</p></div>';
5050
}
5151

52+
// Handle flush pattern registry request.
53+
if ( isset( $_POST['wdsbt_flush_patterns'] ) && check_admin_referer( 'wdsbt_flush_patterns_action' ) ) {
54+
wdsbt_flush_pattern_registry();
55+
// Clear object cache.
56+
if ( function_exists( 'wp_cache_flush' ) ) {
57+
wp_cache_flush();
58+
}
59+
// Delete all transients.
60+
global $wpdb;
61+
$wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE '_transient_%'" );
62+
echo '<div class="notice notice-success is-dismissible"><p>Block pattern registry, Object cache and transients flushed.</p>';
63+
echo '<p style="margin-top:1em;"><strong>Note:</strong> Due to WordPress caching, <u>new pattern categories and patterns may only appear after switching to another theme and back, or waiting for the cache to expire</u>. This is a WordPress limitation and does not affect production sites where patterns/categories are set before launch.</p></div>';
64+
// Inject JS to notify the block editor.
65+
echo "<script>if (window.localStorage) { localStorage.setItem('wdsbt_patterns_flushed', Date.now()); }</script>";
66+
}
67+
5268
// Determine current status summary.
5369
$loading_status = $global_value
5470
? __( 'Speculative Loading is currently <strong>disabled globally</strong>.', 'wdsbt' )
@@ -60,45 +76,80 @@ function render_settings_page() {
6076

6177
<div class="wrap">
6278
<h1><?php esc_html_e( 'WDSBT Settings', 'wdsbt' ); ?></h1>
63-
<small><?php esc_html_e( 'Control optional features for this block theme.', 'wdsbt' ); ?></small>
79+
<h2><?php esc_html_e( 'Control optional features for this block theme.', 'wdsbt' ); ?></h2>
6480

6581
<div class="notice notice-info" style="margin-top: 20px;">
6682
<p><?php echo wp_kses_post( $loading_status ); ?></p>
6783
</div>
6884

69-
<form method="post" style="margin-top: 2em;">
70-
<?php wp_nonce_field( 'save_settings', 'settings_nonce' ); ?>
85+
<div class="options-container" style="display: inline-flex; gap: 20px; width: 100%;">
7186

7287
<div class="card">
7388
<h2 class="title"><?php esc_html_e( 'Performance Options', 'wdsbt' ); ?></h2>
7489

75-
<label style="display: flex; align-items: center; gap: 12px;">
76-
<input type="checkbox" name="<?php echo esc_attr( $global_option ); ?>" <?php checked( $global_value ); ?> />
77-
<h3><?php esc_html_e( 'Disable Speculative Loading for the Entire Site', 'wdsbt' ); ?></h3>
78-
</label>
79-
<p class="description">
80-
<?php esc_html_e( 'No pages will be prefetched or prerendered.', 'wdsbt' ); ?>
81-
</p>
82-
83-
<label style="display: flex; align-items: center; gap: 12px; margin-top: 16px;">
84-
<input type="checkbox" name="<?php echo esc_attr( $exclude_option ); ?>" <?php checked( $exclude_value ); ?> />
85-
<h3><?php esc_html_e( 'Exclude sensitive pages only (e.g., Cart, Checkout)', 'wdsbt' ); ?></h3>
86-
</label>
87-
<p class="description">
88-
<?php esc_html_e( 'Recommended for e-commerce and membership sites.', 'wdsbt' ); ?>
89-
</p>
90-
91-
<label style="display: flex; align-items: center; gap: 12px; margin-top: 16px;">
92-
<input type="checkbox" name="<?php echo esc_attr( $debug_option ); ?>" <?php checked( $debug_value ); ?> />
93-
<h3><?php esc_html_e( 'Enable debug logging for speculative loading in console', 'wdsbt' ); ?></h3>
94-
</label>
95-
<p class="description">
96-
<?php esc_html_e( 'Logs whether a page was prerendered, is prerendering, or was loaded normally.', 'wdsbt' ); ?>
97-
</p>
98-
99-
<?php submit_button( __( 'Save Settings', 'wdsbt' ) ); ?>
90+
<form method="post" style="margin-top: 2em;">
91+
<?php wp_nonce_field( 'save_settings', 'settings_nonce' ); ?>
92+
93+
<label style="display: flex; align-items: center; gap: 12px;">
94+
<input type="checkbox" name="<?php echo esc_attr( $global_option ); ?>" <?php checked( $global_value ); ?> />
95+
<h3><?php esc_html_e( 'Disable Speculative Loading for the Entire Site', 'wdsbt' ); ?></h3>
96+
</label>
97+
<p class="description">
98+
<?php esc_html_e( 'No pages will be prefetched or prerendered.', 'wdsbt' ); ?>
99+
</p>
100+
101+
<label style="display: flex; align-items: center; gap: 12px; margin-top: 16px;">
102+
<input type="checkbox" name="<?php echo esc_attr( $exclude_option ); ?>" <?php checked( $exclude_value ); ?> />
103+
<h3><?php esc_html_e( 'Exclude sensitive pages only (e.g., Cart, Checkout)', 'wdsbt' ); ?></h3>
104+
</label>
105+
<p class="description">
106+
<?php esc_html_e( 'Recommended for e-commerce and membership sites.', 'wdsbt' ); ?>
107+
</p>
108+
109+
<label style="display: flex; align-items: center; gap: 12px; margin-top: 16px;">
110+
<input type="checkbox" name="<?php echo esc_attr( $debug_option ); ?>" <?php checked( $debug_value ); ?> />
111+
<h3><?php esc_html_e( 'Enable debug logging for speculative loading in console', 'wdsbt' ); ?></h3>
112+
</label>
113+
<p class="description">
114+
<?php esc_html_e( 'Logs whether a page was prerendered, is prerendering, or was loaded normally.', 'wdsbt' ); ?>
115+
</p>
116+
117+
<?php submit_button( __( 'Save Settings', 'wdsbt' ) ); ?>
118+
</form>
119+
</div>
120+
121+
<div class="card">
122+
<h2 class="title"><?php esc_html_e( 'Development Tools', 'wdsbt' ); ?></h2>
123+
124+
<form method="post">
125+
<?php wp_nonce_field( 'wdsbt_flush_patterns_action' ); ?>
126+
<p>
127+
<input type="submit" name="wdsbt_flush_patterns" class="button button-primary" value="<?php esc_attr_e( 'Flush object cache and transients', 'wdsbt' ); ?>">
128+
</p>
129+
</form>
100130
</div>
101-
</form>
131+
</div>
102132
</div>
103133
<?php
104134
}
135+
136+
// Add flush pattern registry function if not present.
137+
if ( ! function_exists( 'wdsbt_flush_pattern_registry' ) ) {
138+
139+
/**
140+
* Flush pattern registry.
141+
*/
142+
function wdsbt_flush_pattern_registry() {
143+
// Unregister all custom patterns (adjust 'wdsbt/' to your theme slug).
144+
$patterns = \WP_Block_Patterns_Registry::get_instance()->get_all_registered();
145+
foreach ( $patterns as $pattern ) {
146+
if ( strpos( $pattern['name'], 'wdsbt/' ) === 0 ) {
147+
unregister_block_pattern( $pattern['name'] );
148+
}
149+
}
150+
// Re-register categories and patterns.
151+
if ( function_exists( 'wdsbt_register_dynamic_block_pattern_categories' ) ) {
152+
wdsbt_register_dynamic_block_pattern_categories();
153+
}
154+
}
155+
}

0 commit comments

Comments
 (0)