-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathacf-blocks-v2-iframe-compat.php
More file actions
101 lines (85 loc) · 3.13 KB
/
Copy pathacf-blocks-v2-iframe-compat.php
File metadata and controls
101 lines (85 loc) · 3.13 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
<?php
/**
* Plugin Name: ACF Blocks V2 Iframe Compatibility
* Plugin URI: https://advancedcustomfields.com
* Description: Forces the WordPress block editor to not use the iframe canvas, if there's an ACF v2 block on the site. This plugin is intended to give site owners more time to migrate to the iframe experience provided by core. It should not be used permanently.
* Version: 1.0.0
* Requires PHP: 7.4
* Author: ACF
* Author URI: https://advancedcustomfields.com
* Text Domain: abic
* Requires Plugins: advanced-custom-fields-pro
*/
defined( 'ABSPATH' ) || exit;
// No-op below WP 7.1 so this plugin can be installed ahead of the upgrade and activate on its own.
if ( version_compare( get_bloginfo( 'version' ), '7.1', '<' ) ) {
return;
}
if ( defined( 'ABIC_LOADED' ) ) {
return;
}
define( 'ABIC_LOADED', true );
define( 'ABIC_VERSION', '1.0.0' );
/**
* Cap ACF's default block version for blocks registered without an explicit
* ACF block version.
*
* ACF bumps its default to 3 on WordPress 7.1+, which changes the type of any
* block registered without acf_block_version set. Users of this plugin want to
* stay on the pre-7.1 defaults without touching their block registrations, so
* mirror them: PHP-registered blocks (acf_register_block_type) get v1, JSON-
* registered blocks (block.json) get v2. Blocks that explicitly opt into v3
* are unaffected; the filter default only applies when no version is set.
*
* @param integer $version The default block version ACF would have used.
* @param array|null $context The block settings/metadata array (unused).
* @param string|null $source Registration source: 'php', 'json', or null on
* ACF versions that don't pass this arg — in which
* case both paths fall through to v2.
* @return integer
*/
add_filter( 'acf/blocks/default_block_version', 'abic_cap_default_block_version', 10, 3 );
function abic_cap_default_block_version( $version, $context = null, $source = null ) {
return 'php' === $source ? 1 : 2;
}
/**
* Check if any registered ACF blocks are using ACF Block Version 2 (or lower).
*
* @return bool
*/
function abic_has_acf_v2_blocks(): bool {
if ( ! function_exists( 'acf_get_block_types' ) ) {
return false;
}
$blocks = acf_get_block_types();
if ( empty( $blocks ) ) {
return false;
}
foreach ( $blocks as $block ) {
if ( empty( $block['acf_block_version'] ) || $block['acf_block_version'] < 3 ) {
return true;
}
}
return false;
}
/**
* Enqueue the script that forces the editor canvas into an iframe.
*/
add_action( 'enqueue_block_editor_assets', 'abic_enqueue', 1 );
function abic_enqueue(): void {
if ( ! abic_has_acf_v2_blocks() ) {
return;
}
$path = __DIR__ . '/assets/js/force-iframeless-canvas.js';
if ( ! is_file( $path ) ) {
return;
}
$js = (string) file_get_contents( $path );
if ( wp_script_is( 'react-jsx-runtime', 'registered' ) ) {
wp_add_inline_script( 'react-jsx-runtime', $js, 'after' );
return;
}
if ( wp_script_is( 'wp-element', 'registered' ) ) {
wp_add_inline_script( 'wp-element', $js, 'after' );
}
}