| 1 |
<?php |
| 2 |
namespace Easyel\EasyElements\Widgets; |
| 3 |
use Elementor\Controls_Manager; |
| 4 |
use Elementor\Widget_Base; |
| 5 |
|
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
|
| 10 |
class Easyel_Free_Excerpt_Widget extends \Elementor\Widget_Base { |
| 11 |
|
| 12 |
|
| 13 |
public function get_name() { |
| 14 |
return 'eel-excerpt'; |
| 15 |
} |
| 16 |
|
| 17 |
public function get_title() { |
| 18 |
return __( 'Post Excerpt', 'easy-elements' ); |
| 19 |
} |
| 20 |
|
| 21 |
public function get_icon() { |
| 22 |
return 'easyicon easyelIcon-post-excerpt'; |
| 23 |
} |
| 24 |
|
| 25 |
public function get_categories() { |
| 26 |
return [ 'easyelements_category' ]; |
| 27 |
} |
| 28 |
|
| 29 |
public function get_keywords() { |
| 30 |
return [ 'excerpt', 'content', 'link', 'text', 'post-excerpt' ]; |
| 31 |
} |
| 32 |
|
| 33 |
public function get_style_depends() { |
| 34 |
return [ |
| 35 |
'eel-excerpt', |
| 36 |
]; |
| 37 |
} |
| 38 |
|
| 39 |
protected function register_controls() { |
| 40 |
|
| 41 |
$this->start_controls_section( |
| 42 |
'content_section', |
| 43 |
[ |
| 44 |
'label' => esc_html__('Excerpt Settings', 'easy-elements'), |
| 45 |
'tab' => Controls_Manager::TAB_CONTENT, |
| 46 |
] |
| 47 |
); |
| 48 |
|
| 49 |
$this->add_control( |
| 50 |
'excerpt_length', |
| 51 |
[ |
| 52 |
'label' => esc_html__('Excerpt Length', 'easy-elements'), |
| 53 |
'type' => Controls_Manager::NUMBER, |
| 54 |
'default' => 55, |
| 55 |
] |
| 56 |
); |
| 57 |
|
| 58 |
$this->add_control( |
| 59 |
'excerpt_color', |
| 60 |
[ |
| 61 |
'label' => esc_html__('Excerpt Color', 'easy-elements'), |
| 62 |
'type' => Controls_Manager::COLOR, |
| 63 |
'selectors' => [ |
| 64 |
'{{WRAPPER}} .eel-excerpt' => 'color: {{VALUE}};', |
| 65 |
], |
| 66 |
] |
| 67 |
); |
| 68 |
|
| 69 |
$this->add_group_control( |
| 70 |
\Elementor\Group_Control_Typography::get_type(), |
| 71 |
[ |
| 72 |
'name' => 'excerpt_typography', |
| 73 |
'selector' => '{{WRAPPER}} .eel-excerpt', |
| 74 |
] |
| 75 |
); |
| 76 |
|
| 77 |
// Read More Button |
| 78 |
$this->add_control( |
| 79 |
'display_button', |
| 80 |
[ |
| 81 |
'label' => esc_html__('Read More Enable', 'easy-elements'), |
| 82 |
'type' => Controls_Manager::SWITCHER, |
| 83 |
'label_on' => esc_html__('Show', 'easy-elements'), |
| 84 |
'label_off' => esc_html__('Hide', 'easy-elements'), |
| 85 |
'return_value' => 'yes', |
| 86 |
'default' => 'yes', |
| 87 |
] |
| 88 |
); |
| 89 |
$this->add_control( |
| 90 |
'read_more_text', |
| 91 |
[ |
| 92 |
'label' => esc_html__('Read More', 'easy-elements'), |
| 93 |
'type' => Controls_Manager::TEXT, |
| 94 |
'default' => esc_html__('Read More', 'easy-elements'), |
| 95 |
'condition' => [ |
| 96 |
'display_button' => 'yes', |
| 97 |
], |
| 98 |
] |
| 99 |
); |
| 100 |
|
| 101 |
// Start Button Style Tabs |
| 102 |
$this->start_controls_tabs('button_style_tabs', [ |
| 103 |
'condition' => ['display_button' => 'yes'], |
| 104 |
]); |
| 105 |
// Normal State Tab |
| 106 |
$this->start_controls_tab( |
| 107 |
'button_normal_tab', |
| 108 |
[ |
| 109 |
'label' => esc_html__('Normal', 'easy-elements'), |
| 110 |
] |
| 111 |
); |
| 112 |
|
| 113 |
$this->add_control( |
| 114 |
'button_color', |
| 115 |
[ |
| 116 |
'label' => esc_html__('Button Color', 'easy-elements'), |
| 117 |
'type' => Controls_Manager::COLOR, |
| 118 |
'selectors' => [ |
| 119 |
'{{WRAPPER}} .eel-excerpt-readmore' => 'color: {{VALUE}};', |
| 120 |
], |
| 121 |
] |
| 122 |
); |
| 123 |
$this->add_group_control( |
| 124 |
\Elementor\Group_Control_Background::get_type(), |
| 125 |
[ |
| 126 |
'name' => 'background_buttpm', |
| 127 |
'types' => [ 'classic', 'gradient', 'video' ], |
| 128 |
'selector' => '{{WRAPPER}} .eel-excerpt-readmore', |
| 129 |
] |
| 130 |
); |
| 131 |
$this->add_group_control( |
| 132 |
\Elementor\Group_Control_Border::get_type(), |
| 133 |
[ |
| 134 |
'name' => 'button_border', |
| 135 |
'selector' => '{{WRAPPER}} .eel-excerpt-readmore', |
| 136 |
] |
| 137 |
); |
| 138 |
$this->end_controls_tab(); |
| 139 |
|
| 140 |
// Hover State Tab |
| 141 |
$this->start_controls_tab( |
| 142 |
'button_hover_tab', |
| 143 |
[ |
| 144 |
'label' => esc_html__('Hover', 'easy-elements'), |
| 145 |
] |
| 146 |
); |
| 147 |
$this->add_control( |
| 148 |
'button_hover_color', |
| 149 |
[ |
| 150 |
'label' => esc_html__('Button Hover Color', 'easy-elements'), |
| 151 |
'type' => Controls_Manager::COLOR, |
| 152 |
'selectors' => [ |
| 153 |
'{{WRAPPER}} .eel-excerpt-readmore:hover' => 'color: {{VALUE}};', |
| 154 |
], |
| 155 |
] |
| 156 |
); |
| 157 |
$this->add_group_control( |
| 158 |
\Elementor\Group_Control_Background::get_type(), |
| 159 |
[ |
| 160 |
'name' => 'button_hover_background', |
| 161 |
'types' => [ 'classic', 'gradient' ], |
| 162 |
'selector' => '{{WRAPPER}} .eel-excerpt-readmore:hover', |
| 163 |
] |
| 164 |
); |
| 165 |
$this->add_group_control( |
| 166 |
\Elementor\Group_Control_Border::get_type(), |
| 167 |
[ |
| 168 |
'name' => 'button_hover_border', |
| 169 |
'selector' => '{{WRAPPER}} .eel-excerpt-readmore:hover', |
| 170 |
] |
| 171 |
); |
| 172 |
$this->end_controls_tab(); |
| 173 |
|
| 174 |
$this->end_controls_tabs(); |
| 175 |
|
| 176 |
// Add Padding control for the button |
| 177 |
$this->add_responsive_control( |
| 178 |
'button_padding', |
| 179 |
[ |
| 180 |
'label' => esc_html__('Button Padding', 'easy-elements'), |
| 181 |
'type' => Controls_Manager::DIMENSIONS, |
| 182 |
'size_units' => [ 'px', 'em', '%' ], |
| 183 |
'selectors' => [ |
| 184 |
'{{WRAPPER}} .eel-excerpt-readmore' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 185 |
], |
| 186 |
'separator' => 'before', |
| 187 |
'condition' => [ |
| 188 |
'display_button' => 'yes', |
| 189 |
], |
| 190 |
] |
| 191 |
); |
| 192 |
|
| 193 |
// Add Margin control for the button |
| 194 |
$this->add_responsive_control( |
| 195 |
'button_margin', |
| 196 |
[ |
| 197 |
'label' => esc_html__('Button Margin', 'easy-elements'), |
| 198 |
'type' => Controls_Manager::DIMENSIONS, |
| 199 |
'size_units' => [ 'px', 'em', '%' ], |
| 200 |
'selectors' => [ |
| 201 |
'{{WRAPPER}} .eel-excerpt-readmore' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 202 |
], |
| 203 |
'condition' => [ |
| 204 |
'display_button' => 'yes', |
| 205 |
], |
| 206 |
] |
| 207 |
); |
| 208 |
|
| 209 |
$this->end_controls_section(); |
| 210 |
|
| 211 |
} |
| 212 |
|
| 213 |
protected function render() { |
| 214 |
$settings = $this->get_settings_for_display(); |
| 215 |
$excerpt_length = !empty($settings['excerpt_length']) ? intval($settings['excerpt_length']) : 55; |
| 216 |
$read_more_text = !empty($settings['read_more_text']) ? $settings['read_more_text'] : esc_html__('Read More', 'easy-elements'); |
| 217 |
$display_button = !isset($settings['display_button']) || $settings['display_button'] === 'yes'; |
| 218 |
|
| 219 |
$post_id = function_exists('easyel_get_prepared_post_id') |
| 220 |
? easyel_get_prepared_post_id() |
| 221 |
: get_the_ID(); |
| 222 |
|
| 223 |
$post_obj = get_post( $post_id ); |
| 224 |
if ( ! $post_obj instanceof \WP_Post ) { |
| 225 |
echo '<p>' . esc_html__('No valid post found.', 'easy-elements') . '</p>'; |
| 226 |
return; |
| 227 |
} |
| 228 |
|
| 229 |
global $post; |
| 230 |
$original_post = $post; |
| 231 |
$post = $post_obj; |
| 232 |
setup_postdata( $post ); |
| 233 |
|
| 234 |
$excerpt = get_the_excerpt(); |
| 235 |
$post_url = get_permalink(); |
| 236 |
|
| 237 |
$excerpt = wp_trim_words( $excerpt, $excerpt_length, '...' ); |
| 238 |
|
| 239 |
?> |
| 240 |
<div class="eel-excerpt"> |
| 241 |
<div class="eel-excerpt-text"><?php echo esc_html($excerpt); ?></div> |
| 242 |
<?php if ( !empty($read_more_text) && $display_button ) : ?> |
| 243 |
<a href="<?php echo esc_url($post_url); ?>" class="eel-excerpt-readmore"><?php echo esc_html($read_more_text); ?></a> |
| 244 |
<?php endif; ?> |
| 245 |
</div> |
| 246 |
<?php |
| 247 |
|
| 248 |
wp_reset_postdata(); |
| 249 |
$post = $original_post; |
| 250 |
} |
| 251 |
|
| 252 |
} |
| 253 |
|