This repository has been archived by the owner on Mar 9, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
class-wp-compat.php
349 lines (310 loc) · 10.2 KB
/
class-wp-compat.php
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
<?php
/**
* ClassicPress polyfills for blocks.
*
* @package ClassicPress
* @since CP-2.0.0
*/
class WP_Compat {
public static $blocks_compatibility_level = null;
public function __construct() {
if ( null === self::$blocks_compatibility_level ) {
self::$blocks_compatibility_level = (int) get_option( 'blocks_compatibility_level', 1 );
}
add_action( 'update_option_blocks_compatibility_level', array( $this, 'purge_options' ), 10, 2 );
if ( 0 === self::$blocks_compatibility_level ) {
return;
}
$this->define_polyfills();
if ( 1 === self::$blocks_compatibility_level ) {
return;
}
// Define hooks to be used to warn users
add_action( 'after_plugin_row', array( $this, 'using_block_function_row' ), 10, 2 );
add_action( 'upgrader_process_complete', array( $this, 'update_extensions_using_blocks' ), 10, 2 );
add_action( 'delete_plugin', array( $this, 'delete_plugins_using_blocks' ), 10, 1 );
add_action( 'admin_notices', array( $this, 'using_block_function_theme' ), 10, 0 );
add_action( 'after_switch_theme', array( $this, 'delete_themes_using_blocks' ), 10, 0 );
}
public function purge_options( $old_value, $value ) {
if ( 2 === $value ) {
return;
}
delete_option( 'plugins_using_blocks' );
delete_option( 'theme_using_blocks' );
}
/**
* Action hooked to after_plugin_row to display plugins that may not work properly.
*
* @param string $plugin_file
* @param array $plugin_data
* @return void
*/
public function using_block_function_row( $plugin_file, $plugin_data ) {
$plugins_using_blocks = get_option( 'plugins_using_blocks', array() );
if ( ! array_key_exists( dirname( $plugin_file ), $plugins_using_blocks ) && ! array_key_exists( $plugin_file, $plugins_using_blocks ) ) {
return;
}
$wp_list_table = _get_list_table( 'WP_Plugins_List_Table' );
$active = is_plugin_active( $plugin_file ) ? 'active' : '';
$shadow = isset( $plugin_data['new_version'] ) ? 'style="box-shadow: none;"' : '';
?>
<tr class="plugin-update-tr <?php echo $active; ?>">
<td colspan="<?php echo $wp_list_table->get_column_count(); ?>" class="plugin-update colspanchange" <?php echo $shadow; ?>>
<div class="notice inline notice-warning">
<p>
<?php
// Translators: %1$s is the plugin name.
printf( esc_html__( '%1$s uses block-related functions and may have issues.' ), $plugin_data['Name'] );
?>
<a href="https://docs.classicpress.net/user-guides/using-classicpress/settings-general-screen/#blocks-compatibility"><?php _e( 'Learn more' ); ?></a> |
<a href="https://forums.classicpress.net/new-topic?category=plugins/plugin-support&tags=blocks-compatibility&title=<?php echo urlencode( $plugin_data['Name'] ); ?>+blocks+compatibility"><?php _e( 'Report an issue ›' ); ?></a>
</p>
</div>
</td>
</tr>
<script>
document.querySelector('tr[data-plugin="<?php echo $plugin_file; ?>"').classList.add('update');
</script>
<?php
}
/**
* Action hooked to upgrader_process_complete to clean up the list of plugins
* that may not work properly.
*
* @param WP_Upgrader $upgrader
* @param array $options
* @return void
*/
public function update_extensions_using_blocks( $upgrader, $options ) {
if ( 'update' !== $options['action'] ) {
return;
}
if ( 'theme' === $options['type'] ) {
update_option( 'theme_using_blocks', false );
return;
}
if ( 'plugin' !== $options['type'] ) {
return;
}
$plugins_using_blocks = get_option( 'plugins_using_blocks', array() );
foreach ( $options['plugins'] as $plugin ) {
if ( array_key_exists( $plugin, $plugins_using_blocks ) ) {
unset( $plugins_using_blocks[ $plugin ] );
}
}
update_option( 'plugins_using_blocks', $plugins_using_blocks );
}
/**
* Action hooked to delete_plugin to remove the plugin
* that may not work properly.
*
* @param string $options
* @return void
*/
public function delete_plugins_using_blocks( $plugin_file ) {
$plugins_using_blocks = get_option( 'plugins_using_blocks', array() );
if ( array_key_exists( $plugin_file, $plugins_using_blocks ) ) {
unset( $plugins_using_blocks[ $plugin_file ] );
}
update_option( 'plugins_using_blocks', $plugins_using_blocks );
}
/**
* Action hooked to admin_notices to display an admin notice
* on themes page.
*
* @return void
*/
public function using_block_function_theme() {
global $pagenow;
if ( 'themes.php' !== $pagenow ) {
return;
}
$theme_using_blocks = get_option( 'theme_using_blocks', '0' );
if ( ! in_array( $theme_using_blocks, array( '1', '2' ), true ) ) {
return;
}
if ( '1' === $theme_using_blocks ) {
// Translators: %1$s is the theme name.
$message = sprintf( esc_html__( '%1$s uses block-related functions and may have issues.' ), wp_get_theme()->get( 'Name' ) );
$message .= ' <a href="https://docs.classicpress.net/user-guides/using-classicpress/settings-general-screen/#blocks-compatibility">' . __( 'Learn more' ) . '</a> |';
$message .= ' <a href="https://forums.classicpress.net/new-topic?category=themes/theme-support&tags=blocks-compatibility&title=' . urlencode( wp_get_theme()->get( 'Name' ) ) . '+blocks+compatibility">' . __( 'Report an issue ›' ) . '</a>';
} else {
// Translators: %1$s is the theme name, %1$s is the parent theme name.
$message = sprintf( esc_html__( '%1$s parent theme (%2$s) uses block-related functions and may have issues.' ), wp_get_theme()->get( 'Name' ), wp_get_theme()->parent()->get( 'Name' ) );
$message .= ' <a href="https://docs.classicpress.net/user-guides/using-classicpress/settings-general-screen/#blocks-compatibility">' . __( 'Learn more' ) . '</a> |';
$message .= ' <a href="https://forums.classicpress.net/new-topic?category=themes/theme-support&tags=blocks-compatibility&title=' . urlencode( wp_get_theme()->parent()->get( 'Name' ) ) . '+blocks+compatibility">' . __( 'Report an issue ›' ) . '</a>';
}
?>
<div class="notice notice-warning">
<p>
<?php
echo $message;
?>
</div>
<?php
}
/**
* Get plugin folder name from a path.
*
* @param string $path
* @return string
*/
private static function plugin_folder( $path ) {
return preg_replace( '~^' . preg_quote( WP_PLUGIN_DIR ) . preg_quote( DIRECTORY_SEPARATOR ) . '([^' . preg_quote( DIRECTORY_SEPARATOR ) . ']*).*~', '$1', $path );
}
/**
* Action hooked to after_switch_theme to remove the theme
* that may not work properly.
*
* @return void
*/
public function delete_themes_using_blocks() {
update_option( 'theme_using_blocks', false );
}
/**
* This function have to be called from a polyfill
* to map themes and plugins calling those functions.
*
* @return void
*/
public static function using_block_function() {
if ( 2 !== self::$blocks_compatibility_level ) {
return;
}
$trace = debug_backtrace();
/*
* Fires after WP_Compat::using_block_function() is called.
*
* @since: CP-2.0.0
*
* @param array $trace debug_backtrace() output
*/
do_action( 'using_block_function', $trace );
if ( 0 === strpos( $trace[1]['file'], realpath( get_stylesheet_directory() ) ) ) {
// Current theme is calling the function
update_option( 'theme_using_blocks', '1' );
} elseif ( 0 === strpos( $trace[1]['file'], realpath( get_template_directory() ) ) ) {
// Parent theme is calling the function
update_option( 'theme_using_blocks', '2' );
} else {
// A plugin is calling the function
$traces = array_column( $trace, 'file' );
$traces = array_map(
function( $path ) {
return self::plugin_folder( $path );
},
$traces
);
$active = wp_get_active_and_valid_plugins();
$active = array_map(
function( $path ) {
return self::plugin_folder( $path );
},
$active
);
$plugins = array_intersect( $traces, $active );
$plugin = array_pop( $plugins );
if ( null === $plugin ) {
// Nothing found? Bail.
return;
}
$plugins_using_blocks = get_option( 'plugins_using_blocks', array() );
if ( ! array_key_exists( plugin_basename( $plugin ), $plugins_using_blocks ) ) {
$plugins_using_blocks[ plugin_basename( $plugin ) ] = true;
update_option( 'plugins_using_blocks', $plugins_using_blocks );
}
}
}
/**
* Polyfills that can not be defined elsewhere go here.
* that may not work properly.
*
* @return void
*/
private function define_polyfills() {
// Polyfills that must not have to be defined elsewhere go there.
if ( ! function_exists( 'register_block_type' ) ) {
/**
* Polyfill for block functions.
*
* @since CP-2.0.0
*
* @return bool False.
*/
function register_block_type( ...$args ) {
WP_Compat::using_block_function();
return false;
}
}
if ( ! function_exists( 'register_block_type_from_metadata' ) ) {
/**
* Polyfill for block functions.
*
* @since CP-2.0.0
*
* @return bool False.
*/
function register_block_type_from_metadata( ...$args ) {
WP_Compat::using_block_function();
return false;
}
}
if ( ! function_exists( 'has_block' ) ) {
/**
* Polyfill for block functions.
*
* @since CP-2.0.0
*
* @return bool False.
*/
function has_block( ...$args ) {
WP_Compat::using_block_function();
return false;
}
}
if ( ! function_exists( 'has_blocks' ) ) {
/**
* Polyfill for block functions.
*
* @since CP-2.0.0
*
* @return bool False.
*/
function has_blocks( ...$args ) {
WP_Compat::using_block_function();
return false;
}
}
if ( ! function_exists( 'register_block_pattern' ) ) {
/**
* Polyfill for block functions.
*
* @since CP-2.0.0
*
* @return bool False.
*/
function register_block_pattern( ...$args ) {
WP_Compat::using_block_function();
return false;
}
}
if ( ! function_exists( 'wp_is_block_theme' ) ) {
/**
* Polyfill for block functions.
*
* @since CP-2.0.0
*
* @return bool False.
*/
function wp_is_block_theme( ...$args ) {
WP_Compat::using_block_function();
return false;
}
}
// Load WP_Block_Type class file as polyfill.
require_once ABSPATH . WPINC . '/classicpress/class-wp-block-type.php';
require_once ABSPATH . WPINC . '/classicpress/class-wp-block-template.php';
}
}
$wp_compat = new WP_Compat;