Bindings_Editor.php
52 lines
| 1 | <?php |
| 2 | /** |
| 3 | * SCF block bindings editor integration. |
| 4 | * |
| 5 | * @package wordpress/secure-custom-fields |
| 6 | */ |
| 7 | |
| 8 | namespace SCF\Blocks; |
| 9 | |
| 10 | /** |
| 11 | * Enqueues the JS layer that powers SCF block bindings in the block editor. |
| 12 | * |
| 13 | * The JS bindings layer registers a block binding source via the stable |
| 14 | * registerBlockBindingsSource API (WP 6.7+), enabling live preview and |
| 15 | * editing of SCF field values bound to block attributes. It runs alongside |
| 16 | * the shared server-side SCF\Blocks\Bindings class. |
| 17 | */ |
| 18 | class Bindings_Editor { |
| 19 | |
| 20 | /** |
| 21 | * Constructor. |
| 22 | */ |
| 23 | public function __construct() { |
| 24 | // The JS bindings layer relies on the stable registerBlockBindingsSource |
| 25 | // API, which is only available on WP 6.7+. |
| 26 | global $wp_version; |
| 27 | if ( version_compare( $wp_version, '6.7', '<' ) ) { |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Enqueues the JS block bindings source script on block editor screens. |
| 36 | * |
| 37 | * @return void |
| 38 | */ |
| 39 | public function enqueue_block_editor_assets() { |
| 40 | if ( ! acf_get_setting( 'enable_block_bindings' ) ) { |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | // JS bindings layer requires the datastore for live editor preview/editing. |
| 45 | if ( ! acf_is_using_datastore() ) { |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | wp_enqueue_script( 'acf-field-bindings' ); |
| 50 | } |
| 51 | } |
| 52 |