| 1 |
<?php |
| 2 |
namespace Elementor; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; // Exit if accessed directly. |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* Elementor menu anchor widget. |
| 10 |
* |
| 11 |
* Elementor widget that allows to link and menu to a specific position on the |
| 12 |
* page. |
| 13 |
* |
| 14 |
* @since 1.0.0 |
| 15 |
*/ |
| 16 |
class Widget_Menu_Anchor extends Widget_Base { |
| 17 |
|
| 18 |
/** |
| 19 |
* Get widget name. |
| 20 |
* |
| 21 |
* Retrieve menu anchor widget name. |
| 22 |
* |
| 23 |
* @since 1.0.0 |
| 24 |
* @access public |
| 25 |
* |
| 26 |
* @return string Widget name. |
| 27 |
*/ |
| 28 |
public function get_name() { |
| 29 |
return 'menu-anchor'; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Get widget title. |
| 34 |
* |
| 35 |
* Retrieve menu anchor widget title. |
| 36 |
* |
| 37 |
* @since 1.0.0 |
| 38 |
* @access public |
| 39 |
* |
| 40 |
* @return string Widget title. |
| 41 |
*/ |
| 42 |
public function get_title() { |
| 43 |
return esc_html__( 'Menu Anchor', 'elementor' ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Get widget icon. |
| 48 |
* |
| 49 |
* Retrieve menu anchor widget icon. |
| 50 |
* |
| 51 |
* @since 1.0.0 |
| 52 |
* @access public |
| 53 |
* |
| 54 |
* @return string Widget icon. |
| 55 |
*/ |
| 56 |
public function get_icon() { |
| 57 |
return 'eicon-anchor'; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get widget keywords. |
| 62 |
* |
| 63 |
* Retrieve the list of keywords the widget belongs to. |
| 64 |
* |
| 65 |
* @since 2.1.0 |
| 66 |
* @access public |
| 67 |
* |
| 68 |
* @return array Widget keywords. |
| 69 |
*/ |
| 70 |
public function get_keywords() { |
| 71 |
return [ 'menu', 'anchor', 'link' ]; |
| 72 |
} |
| 73 |
|
| 74 |
protected function is_dynamic_content(): bool { |
| 75 |
return false; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Get style dependencies. |
| 80 |
* |
| 81 |
* Retrieve the list of style dependencies the widget requires. |
| 82 |
* |
| 83 |
* @since 3.24.0 |
| 84 |
* @access public |
| 85 |
* |
| 86 |
* @return array Widget style dependencies. |
| 87 |
*/ |
| 88 |
public function get_style_depends(): array { |
| 89 |
return [ 'widget-menu-anchor' ]; |
| 90 |
} |
| 91 |
|
| 92 |
public function has_widget_inner_wrapper(): bool { |
| 93 |
return ! Plugin::$instance->experiments->is_feature_active( 'e_optimized_markup' ); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Register menu anchor widget controls. |
| 98 |
* |
| 99 |
* Adds different input fields to allow the user to change and customize the widget settings. |
| 100 |
* |
| 101 |
* @since 3.1.0 |
| 102 |
* @access protected |
| 103 |
*/ |
| 104 |
protected function register_controls() { |
| 105 |
$this->start_controls_section( |
| 106 |
'section_anchor', |
| 107 |
[ |
| 108 |
'label' => esc_html__( 'Menu Anchor', 'elementor' ), |
| 109 |
] |
| 110 |
); |
| 111 |
|
| 112 |
$this->add_control( |
| 113 |
'anchor', |
| 114 |
[ |
| 115 |
'label' => esc_html__( 'The ID of Menu Anchor.', 'elementor' ), |
| 116 |
'type' => Controls_Manager::TEXT, |
| 117 |
'ai' => [ |
| 118 |
'active' => false, |
| 119 |
], |
| 120 |
'placeholder' => esc_html__( 'For Example: About', 'elementor' ), |
| 121 |
'description' => esc_html__( 'This ID will be the CSS ID you will have to use in your own page, Without #.', 'elementor' ), |
| 122 |
'label_block' => true, |
| 123 |
'dynamic' => [ |
| 124 |
'active' => true, |
| 125 |
], |
| 126 |
] |
| 127 |
); |
| 128 |
|
| 129 |
$this->add_control( |
| 130 |
'anchor_note', |
| 131 |
[ |
| 132 |
'type' => Controls_Manager::ALERT, |
| 133 |
'alert_type' => 'warning', |
| 134 |
'content' => sprintf( |
| 135 |
/* translators: %s: Accepted chars. */ |
| 136 |
esc_html__( 'Note: The ID link ONLY accepts these chars: %s', 'elementor' ), |
| 137 |
'`A-Z, a-z, 0-9, _ , -`' |
| 138 |
), |
| 139 |
] |
| 140 |
); |
| 141 |
|
| 142 |
$this->end_controls_section(); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Render menu anchor widget output on the frontend. |
| 147 |
* |
| 148 |
* Written in PHP and used to generate the final HTML. |
| 149 |
* |
| 150 |
* @since 1.0.0 |
| 151 |
* @access protected |
| 152 |
*/ |
| 153 |
protected function render() { |
| 154 |
$anchor = $this->get_settings_for_display( 'anchor' ); |
| 155 |
|
| 156 |
if ( empty( $anchor ) ) { |
| 157 |
return; |
| 158 |
} |
| 159 |
|
| 160 |
$this->add_render_attribute( |
| 161 |
'inner', |
| 162 |
[ |
| 163 |
'class' => 'elementor-menu-anchor', |
| 164 |
'id' => sanitize_html_class( $anchor ), |
| 165 |
] |
| 166 |
); |
| 167 |
?> |
| 168 |
<div <?php $this->print_render_attribute_string( 'inner' ); ?>></div> |
| 169 |
<?php |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Render menu anchor widget output in the editor. |
| 174 |
* |
| 175 |
* Written as a Backbone JavaScript template and used to generate the live preview. |
| 176 |
* |
| 177 |
* @since 2.9.0 |
| 178 |
* @access protected |
| 179 |
*/ |
| 180 |
protected function content_template() { |
| 181 |
?> |
| 182 |
<# |
| 183 |
if ( '' === settings.anchor ) { |
| 184 |
return; |
| 185 |
} |
| 186 |
|
| 187 |
view.addRenderAttribute( |
| 188 |
'inner', |
| 189 |
{ |
| 190 |
'class': 'elementor-menu-anchor', |
| 191 |
'id': settings.anchor, |
| 192 |
} |
| 193 |
); |
| 194 |
#> |
| 195 |
<div {{{ view.getRenderAttributeString( 'inner' ) }}}></div> |
| 196 |
<?php |
| 197 |
} |
| 198 |
|
| 199 |
public function render_markdown(): string { |
| 200 |
return ''; |
| 201 |
} |
| 202 |
|
| 203 |
protected function on_save( array $settings ) { |
| 204 |
$settings['anchor'] = sanitize_html_class( $settings['anchor'] ); |
| 205 |
|
| 206 |
return $settings; |
| 207 |
} |
| 208 |
} |
| 209 |
|