| 1 |
<?php |
| 2 |
/** |
| 3 |
* Standalone (theme_id 990) single-property section blocks (generic adapter). |
| 4 |
* |
| 5 |
* One adapter that loops the section manifest and registers a dynamic Gutenberg |
| 6 |
* block (mlsimport/property-<slug>) per entry, whose render_callback hands off to |
| 7 |
* the one dispatcher — so block, shortcode and Elementor widget emit identical |
| 8 |
* markup. A single editor script registers every block in the editor with the |
| 9 |
* brand icon. Adding a section needs no change here. See docs/adr/0005. |
| 10 |
* |
| 11 |
* @package Mlsimport |
| 12 |
*/ |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
require_once __DIR__ . '/property-section-registry.php'; |
| 19 |
|
| 20 |
/** |
| 21 |
* Registers a dynamic block per registered property section. |
| 22 |
*/ |
| 23 |
class Mlsimport_Property_Section_Blocks { |
| 24 |
|
| 25 |
/** |
| 26 |
* Register one dynamic block for every manifest entry. |
| 27 |
* |
| 28 |
* @return void |
| 29 |
*/ |
| 30 |
public static function register(): void { |
| 31 |
if ( ! function_exists( 'register_block_type' ) ) { |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
mlsimport_register_builtin_property_sections(); |
| 36 |
$editor_script = self::register_editor_script(); |
| 37 |
|
| 38 |
foreach ( mlsimport_get_property_sections() as $slug => $def ) { |
| 39 |
$block_name = 'mlsimport/property-' . str_replace( '_', '-', $slug ); |
| 40 |
if ( WP_Block_Type_Registry::get_instance()->is_registered( $block_name ) ) { |
| 41 |
continue; |
| 42 |
} |
| 43 |
register_block_type( |
| 44 |
$block_name, |
| 45 |
array( |
| 46 |
'api_version' => 2, |
| 47 |
'attributes' => array( 'id' => array( 'type' => 'number', 'default' => 0 ) ), |
| 48 |
'render_callback' => static function ( $attributes ) use ( $slug ) { |
| 49 |
return mlsimport_render_property_section( $slug, isset( $attributes['id'] ) ? (int) $attributes['id'] : 0 ); |
| 50 |
}, |
| 51 |
'editor_script' => $editor_script, |
| 52 |
) |
| 53 |
); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Register + localize the shared editor script that registers all section |
| 59 |
* blocks in the editor. Returns the handle (or '' if scripts unavailable). |
| 60 |
* |
| 61 |
* @return string |
| 62 |
*/ |
| 63 |
private static function register_editor_script(): string { |
| 64 |
if ( ! function_exists( 'wp_register_script' ) ) { |
| 65 |
return ''; |
| 66 |
} |
| 67 |
|
| 68 |
$handle = 'mlsimport-property-sections-block'; |
| 69 |
if ( ! wp_script_is( $handle, 'registered' ) ) { |
| 70 |
$url = defined( 'MLSIMPORT_PLUGIN_URL' ) ? MLSIMPORT_PLUGIN_URL . 'admin/js/mlsimport-property-sections-block.js' : ''; |
| 71 |
wp_register_script( |
| 72 |
$handle, |
| 73 |
$url, |
| 74 |
array( 'wp-blocks', 'wp-element', 'wp-server-side-render' ), |
| 75 |
defined( 'MLSIMPORT_VERSION' ) ? MLSIMPORT_VERSION : false, |
| 76 |
true |
| 77 |
); |
| 78 |
|
| 79 |
$sections = array(); |
| 80 |
foreach ( mlsimport_get_property_sections() as $slug => $def ) { |
| 81 |
$sections[] = array( |
| 82 |
'slug' => str_replace( '_', '-', $slug ), |
| 83 |
'title' => $def['label'], |
| 84 |
); |
| 85 |
} |
| 86 |
$icon = defined( 'MLSIMPORT_PLUGIN_URL' ) ? MLSIMPORT_PLUGIN_URL . 'img/mlsimport_menu.png' : ''; |
| 87 |
wp_localize_script( $handle, 'MLSImportSections', $sections ); |
| 88 |
wp_add_inline_script( $handle, 'window.MLSImportSectionsIcon=' . wp_json_encode( $icon ) . ';', 'before' ); |
| 89 |
} |
| 90 |
|
| 91 |
return $handle; |
| 92 |
} |
| 93 |
} |
| 94 |
|