| 1 |
<?php |
| 2 |
|
| 3 |
namespace King_Addons\Widgets\Login_Register_Form; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) { |
| 6 |
exit; // Exit if accessed directly. |
| 7 |
} |
| 8 |
|
| 9 |
class Widget_Settings_Resolver |
| 10 |
{ |
| 11 |
/** |
| 12 |
* Resolve Elementor widget settings for a given post + widget (element) id. |
| 13 |
* |
| 14 |
* @param int $post_id |
| 15 |
* @param string $widget_id |
| 16 |
* @return array<string,mixed> |
| 17 |
*/ |
| 18 |
public static function resolve($post_id, $widget_id) |
| 19 |
{ |
| 20 |
$post_id = absint($post_id); |
| 21 |
$widget_id = sanitize_text_field($widget_id); |
| 22 |
|
| 23 |
if ($post_id <= 0 || $widget_id === '') { |
| 24 |
return []; |
| 25 |
} |
| 26 |
|
| 27 |
if (!class_exists('\\Elementor\\Plugin')) { |
| 28 |
return []; |
| 29 |
} |
| 30 |
|
| 31 |
try { |
| 32 |
$document = \Elementor\Plugin::$instance->documents->get($post_id); |
| 33 |
if (!$document) { |
| 34 |
return []; |
| 35 |
} |
| 36 |
|
| 37 |
$elements = $document->get_elements_data(); |
| 38 |
if (!is_array($elements)) { |
| 39 |
return []; |
| 40 |
} |
| 41 |
|
| 42 |
$settings = self::find_settings_by_element_id($elements, $widget_id); |
| 43 |
return is_array($settings) ? $settings : []; |
| 44 |
} catch (\Throwable $e) { |
| 45 |
return []; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* @param array $elements |
| 51 |
* @param string $widget_id |
| 52 |
* @return array<string,mixed>|null |
| 53 |
*/ |
| 54 |
private static function find_settings_by_element_id($elements, $widget_id) |
| 55 |
{ |
| 56 |
foreach ((array) $elements as $element) { |
| 57 |
if (!is_array($element)) { |
| 58 |
continue; |
| 59 |
} |
| 60 |
|
| 61 |
if (!empty($element['id']) && $element['id'] === $widget_id) { |
| 62 |
return isset($element['settings']) && is_array($element['settings']) ? $element['settings'] : []; |
| 63 |
} |
| 64 |
|
| 65 |
if (!empty($element['elements']) && is_array($element['elements'])) { |
| 66 |
$found = self::find_settings_by_element_id($element['elements'], $widget_id); |
| 67 |
if (is_array($found)) { |
| 68 |
return $found; |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
return null; |
| 74 |
} |
| 75 |
} |
| 76 |
|