| 1 |
<?php |
| 2 |
/** |
| 3 |
* Standalone (theme_id 990) single-property section Elementor widget (adapter). |
| 4 |
* |
| 5 |
* One generic Elementor widget, "MLSImport Property Section", with a Section |
| 6 |
* picker control (options from the manifest) + a property-id control. Its render |
| 7 |
* delegates to the one dispatcher, so Elementor emits the same markup as the |
| 8 |
* shortcode and the block. |
| 9 |
* |
| 10 |
* Why a picker rather than one widget per manifest entry: Elementor rebuilds a |
| 11 |
* widget from its registered prototype's CLASS with empty constructor args, so a |
| 12 |
* slug baked in via the constructor is lost at render time. A single widget with |
| 13 |
* a Section control is the robust, no-eval equivalent. The render logic lives in |
| 14 |
* render_settings() so it is testable without Elementor loaded. See docs/adr/0005 |
| 15 |
* (controls are behavioral-only; styling is our own CSS). |
| 16 |
* |
| 17 |
* @package Mlsimport |
| 18 |
*/ |
| 19 |
|
| 20 |
if ( ! defined( 'ABSPATH' ) ) { |
| 21 |
exit; |
| 22 |
} |
| 23 |
|
| 24 |
require_once __DIR__ . '/property-section-registry.php'; |
| 25 |
|
| 26 |
/** |
| 27 |
* Registers and backs the generic Elementor property-section widget. |
| 28 |
*/ |
| 29 |
class Mlsimport_Property_Section_Elementor { |
| 30 |
|
| 31 |
/** |
| 32 |
* Hook the Elementor widget registration. Inert when Elementor is absent. |
| 33 |
* |
| 34 |
* @return void |
| 35 |
*/ |
| 36 |
public static function register(): void { |
| 37 |
add_action( 'elementor/widgets/register', array( __CLASS__, 'register_widget' ) ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Register the generic widget with Elementor's widget manager. |
| 42 |
* |
| 43 |
* @param mixed $widgets_manager Elementor\Widgets_Manager. |
| 44 |
* @return void |
| 45 |
*/ |
| 46 |
public static function register_widget( $widgets_manager ): void { |
| 47 |
if ( ! class_exists( '\Elementor\Widget_Base' ) ) { |
| 48 |
return; |
| 49 |
} |
| 50 |
mlsimport_register_builtin_property_sections(); |
| 51 |
require_once __DIR__ . '/elementor/class-mlsimport-elementor-property-section-widget.php'; |
| 52 |
if ( is_object( $widgets_manager ) && method_exists( $widgets_manager, 'register' ) ) { |
| 53 |
$widgets_manager->register( new Mlsimport_Elementor_Property_Section_Widget() ); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Section picker options for the widget control: slug => label. |
| 59 |
* |
| 60 |
* @return array |
| 61 |
*/ |
| 62 |
public static function section_options(): array { |
| 63 |
mlsimport_register_builtin_property_sections(); |
| 64 |
$options = array(); |
| 65 |
foreach ( mlsimport_get_property_sections() as $slug => $def ) { |
| 66 |
$options[ $slug ] = $def['label']; |
| 67 |
} |
| 68 |
return $options; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Render from a settings array (the widget's get_settings_for_display()). |
| 73 |
* Pure delegation to the dispatcher — the testable core of the widget. |
| 74 |
* |
| 75 |
* @param array $settings Elementor control values. |
| 76 |
* @return string |
| 77 |
*/ |
| 78 |
public static function render_settings( array $settings ): string { |
| 79 |
$slug = isset( $settings['section'] ) ? (string) $settings['section'] : ''; |
| 80 |
if ( '' === $slug ) { |
| 81 |
return ''; |
| 82 |
} |
| 83 |
$id = isset( $settings['property_id'] ) ? (int) $settings['property_id'] : 0; |
| 84 |
return mlsimport_render_property_section( $slug, $id ); |
| 85 |
} |
| 86 |
} |
| 87 |
|