| 1 |
<?php |
| 2 |
namespace Elementor\Modules\NestedElements\Base; |
| 3 |
|
| 4 |
use Elementor\Plugin; |
| 5 |
use Elementor\Widget_Base; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; // Exit if accessed directly. |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* Used to create a new widget that can be nested inside other widgets. |
| 13 |
*/ |
| 14 |
abstract class Widget_Nested_Base extends Widget_Base { |
| 15 |
|
| 16 |
/** |
| 17 |
* Get default children elements structure. |
| 18 |
* |
| 19 |
* @return array |
| 20 |
*/ |
| 21 |
abstract protected function get_default_children_elements(); |
| 22 |
|
| 23 |
/** |
| 24 |
* Get repeater title setting key name. |
| 25 |
* |
| 26 |
* @return string |
| 27 |
*/ |
| 28 |
abstract protected function get_default_repeater_title_setting_key(); |
| 29 |
|
| 30 |
/** |
| 31 |
* Get default children title for the navigator, using `%d` as index in the format. |
| 32 |
* |
| 33 |
* @note The title in this method is used to set the default title for each created child in nested element. |
| 34 |
* for handling the children title for new created widget(s), use `get_default_children_elements()` method, |
| 35 |
* eg: |
| 36 |
* [ |
| 37 |
* 'elType' => 'container', |
| 38 |
* 'settings' => [ |
| 39 |
* '_title' => __( 'Tab #1', 'elementor' ), |
| 40 |
* ], |
| 41 |
* ], |
| 42 |
* @return string |
| 43 |
*/ |
| 44 |
protected function get_default_children_title() { |
| 45 |
return esc_html__( 'Item #%d', 'elementor' ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Get default children placeholder selector, Empty string, means will be added at the end view. |
| 50 |
* |
| 51 |
* @return string |
| 52 |
*/ |
| 53 |
protected function get_default_children_placeholder_selector() { |
| 54 |
return ''; |
| 55 |
} |
| 56 |
|
| 57 |
protected function get_default_children_container_placeholder_selector() { |
| 58 |
return ''; |
| 59 |
} |
| 60 |
|
| 61 |
protected function is_dynamic_content(): bool { |
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* @inheritDoc |
| 67 |
* |
| 68 |
* To support nesting. |
| 69 |
*/ |
| 70 |
protected function _get_default_child_type( array $element_data ) { |
| 71 |
return Plugin::$instance->elements_manager->get_element_types( $element_data['elType'] ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* @inheritDoc |
| 76 |
* |
| 77 |
* Adding new 'defaults' config for handling children elements. |
| 78 |
*/ |
| 79 |
protected function get_initial_config() { |
| 80 |
return array_merge( parent::get_initial_config(), [ |
| 81 |
'defaults' => [ |
| 82 |
'elements' => $this->get_default_children_elements(), |
| 83 |
'elements_title' => $this->get_default_children_title(), |
| 84 |
'elements_placeholder_selector' => $this->get_default_children_placeholder_selector(), |
| 85 |
'child_container_placeholder_selector' => $this->get_default_children_container_placeholder_selector(), |
| 86 |
'repeater_title_setting' => $this->get_default_repeater_title_setting_key(), |
| 87 |
], |
| 88 |
'support_nesting' => true, |
| 89 |
] ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* @inheritDoc |
| 94 |
* |
| 95 |
* Each element including its children elements. |
| 96 |
*/ |
| 97 |
public function get_raw_data( $with_html_content = false ) { |
| 98 |
$elements = []; |
| 99 |
$data = $this->get_data(); |
| 100 |
|
| 101 |
$children = $this->get_children(); |
| 102 |
|
| 103 |
foreach ( $children as $child ) { |
| 104 |
$child_raw_data = $child->get_raw_data( $with_html_content ); |
| 105 |
|
| 106 |
$elements[] = $child_raw_data; |
| 107 |
} |
| 108 |
|
| 109 |
return [ |
| 110 |
'id' => $this->get_id(), |
| 111 |
'elType' => $data['elType'], |
| 112 |
'widgetType' => $data['widgetType'], |
| 113 |
'settings' => $data['settings'], |
| 114 |
'elements' => $elements, |
| 115 |
]; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Print child, helper method to print the child element. |
| 120 |
* |
| 121 |
* @param int $index |
| 122 |
*/ |
| 123 |
public function print_child( $index ) { |
| 124 |
$children = $this->get_children(); |
| 125 |
|
| 126 |
if ( ! empty( $children[ $index ] ) ) { |
| 127 |
$children[ $index ]->print_element(); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
protected function content_template_single_repeater_item() {} |
| 132 |
|
| 133 |
public function print_template() { |
| 134 |
parent::print_template(); |
| 135 |
if ( $this->get_initial_config()['support_improved_repeaters'] ?? false ) { |
| 136 |
?> |
| 137 |
<script type="text/html" id="tmpl-elementor-<?php echo esc_attr( $this->get_name() ); ?>-content-single"> |
| 138 |
<?php $this->content_template_single_repeater_item(); ?> |
| 139 |
</script> |
| 140 |
<?php |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|