| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * Elementor Classes. |
|---|
| 4 | * |
|---|
| 5 | * @package CoWidgets |
|---|
| 6 | */ |
|---|
| 7 | |
|---|
| 8 | namespace COWIDGETS\WidgetsManager\Widgets; |
|---|
| 9 | |
|---|
| 10 | use Elementor\Controls_Manager; |
|---|
| 11 | use Elementor\Utils; |
|---|
| 12 | use Elementor\Group_Control_Typography; |
|---|
| 13 | use Elementor\Core\Schemes\Typography; |
|---|
| 14 | use Elementor\Core\Schemes\Color; |
|---|
| 15 | use Elementor\Widget_Base; |
|---|
| 16 | |
|---|
| 17 | if ( ! defined( 'ABSPATH' ) ) { |
|---|
| 18 | exit; // Exit if accessed directly. |
|---|
| 19 | } |
|---|
| 20 | |
|---|
| 21 | /** |
|---|
| 22 | * Elementor Post Navigation |
|---|
| 23 | * |
|---|
| 24 | * Elementor widget for Post Navigation. |
|---|
| 25 | * |
|---|
| 26 | * @since 1.0.0 |
|---|
| 27 | */ |
|---|
| 28 | class Post_Navigation extends Widget_Base { |
|---|
| 29 | |
|---|
| 30 | /** |
|---|
| 31 | * Retrieve the widget name. |
|---|
| 32 | * |
|---|
| 33 | * @since 1.0.0 |
|---|
| 34 | * |
|---|
| 35 | * @access public |
|---|
| 36 | * |
|---|
| 37 | * @return string Widget name. |
|---|
| 38 | */ |
|---|
| 39 | public function get_name() { |
|---|
| 40 | return 'ce-post-navigation'; |
|---|
| 41 | } |
|---|
| 42 | /** |
|---|
| 43 | * Retrieve the widget title. |
|---|
| 44 | * |
|---|
| 45 | * @since 1.0.0 |
|---|
| 46 | * |
|---|
| 47 | * @access public |
|---|
| 48 | * |
|---|
| 49 | * @return string Widget title. |
|---|
| 50 | */ |
|---|
| 51 | public function get_title() { |
|---|
| 52 | return __( 'Post Navigation', 'cowidgets' ); |
|---|
| 53 | } |
|---|
| 54 | /** |
|---|
| 55 | * Retrieve the widget icon. |
|---|
| 56 | * |
|---|
| 57 | * @since 1.0.0 |
|---|
| 58 | * |
|---|
| 59 | * @access public |
|---|
| 60 | * |
|---|
| 61 | * @return string Widget icon. |
|---|
| 62 | */ |
|---|
| 63 | public function get_icon() { |
|---|
| 64 | return 'ce-icon-post-navigation'; |
|---|
| 65 | } |
|---|
| 66 | /** |
|---|
| 67 | * Retrieve the list of categories the widget belongs to. |
|---|
| 68 | * |
|---|
| 69 | * Used to determine where to display the widget in the editor. |
|---|
| 70 | * |
|---|
| 71 | * Note that currently Elementor supports only one category. |
|---|
| 72 | * When multiple categories passed, Elementor uses the first one. |
|---|
| 73 | * |
|---|
| 74 | * @since 1.0.0 |
|---|
| 75 | * |
|---|
| 76 | * @access public |
|---|
| 77 | * |
|---|
| 78 | * @return array Widget categories. |
|---|
| 79 | */ |
|---|
| 80 | public function get_categories() { |
|---|
| 81 | return [ 'ce-content-widgets' ]; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | /** |
|---|
| 86 | * Register Copyright controls. |
|---|
| 87 | * |
|---|
| 88 | * @since 1.0.0 |
|---|
| 89 | * @access protected |
|---|
| 90 | */ |
|---|
| 91 | protected function register_controls() { //phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore |
|---|
| 92 | $this->register_post_navigation_controls(); |
|---|
| 93 | } |
|---|
| 94 | /** |
|---|
| 95 | * Register Copyright General Controls. |
|---|
| 96 | * |
|---|
| 97 | * @since 1.0.0 |
|---|
| 98 | * @access protected |
|---|
| 99 | */ |
|---|
| 100 | protected function register_post_navigation_controls() { |
|---|
| 101 | |
|---|
| 102 | $this->start_controls_section( |
|---|
| 103 | 'section_content_other', |
|---|
| 104 | [ |
|---|
| 105 | 'label' => __( 'Navigation Options', 'cowidgets' ), |
|---|
| 106 | 'tab' => Controls_Manager::TAB_CONTENT, |
|---|
| 107 | ] |
|---|
| 108 | ); |
|---|
| 109 | |
|---|
| 110 | $this->add_control( |
|---|
| 111 | 'style', |
|---|
| 112 | [ |
|---|
| 113 | 'label' => __( 'Style', 'cowidgets' ), |
|---|
| 114 | 'type' => \Elementor\Controls_Manager::SELECT, |
|---|
| 115 | 'default' => 'lark', |
|---|
| 116 | 'options' => apply_filters( 'ce_load_element_styles', [ |
|---|
| 117 | 'lark' => __( 'Lark', 'cowidgets' ) |
|---|
| 118 | ] ), |
|---|
| 119 | ] |
|---|
| 120 | ); |
|---|
| 121 | |
|---|
| 122 | $this->add_control( |
|---|
| 123 | 'back_to', |
|---|
| 124 | [ |
|---|
| 125 | 'label' => __( 'Back To', 'cowidgets' ), |
|---|
| 126 | 'type' => \Elementor\Controls_Manager::SELECT, |
|---|
| 127 | 'default' => 1, |
|---|
| 128 | 'options' => [ |
|---|
| 129 | 0 => __( 'No', 'cowidgets' ), |
|---|
| 130 | 1 => __( 'Yes', 'cowidgets' ) |
|---|
| 131 | ], |
|---|
| 132 | ] |
|---|
| 133 | ); |
|---|
| 134 | |
|---|
| 135 | $this->add_control( |
|---|
| 136 | 'back_to_link', |
|---|
| 137 | [ |
|---|
| 138 | 'label' => __( 'Link', 'cowidgets' ), |
|---|
| 139 | 'type' => \Elementor\Controls_Manager::URL, |
|---|
| 140 | 'placeholder' => __( 'https://your-link.com', 'cowidgets' ), |
|---|
| 141 | 'show_external' => true, |
|---|
| 142 | 'default' => [ |
|---|
| 143 | 'url' => '#', |
|---|
| 144 | 'is_external' => true, |
|---|
| 145 | 'nofollow' => true, |
|---|
| 146 | ], |
|---|
| 147 | 'condition' => [ |
|---|
| 148 | 'back_to' => '1' |
|---|
| 149 | ] |
|---|
| 150 | ] |
|---|
| 151 | |
|---|
| 152 | ); |
|---|
| 153 | |
|---|
| 154 | $this->add_control( |
|---|
| 155 | 'back_to_text', |
|---|
| 156 | [ |
|---|
| 157 | 'label' => __( 'Back To Text', 'cowidgets' ), |
|---|
| 158 | 'type' => \Elementor\Controls_Manager::TEXT, |
|---|
| 159 | 'default' => __( 'All Projects', 'cowidgets' ), |
|---|
| 160 | 'placeholder' => __( 'Type your title here', 'cowidgets' ), |
|---|
| 161 | 'condition' => [ |
|---|
| 162 | 'back_to' => '1' |
|---|
| 163 | ] |
|---|
| 164 | ] |
|---|
| 165 | |
|---|
| 166 | ); |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | $this->add_control( |
|---|
| 170 | 'same_term', |
|---|
| 171 | [ |
|---|
| 172 | 'label' => __( 'In the same term?', 'cowidgets' ), |
|---|
| 173 | 'description' => __( 'If true, will show the next/prev post of the same category as the actual post.', 'cowidgets' ), |
|---|
| 174 | 'type' => \Elementor\Controls_Manager::SELECT, |
|---|
| 175 | 'default' => 0, |
|---|
| 176 | 'options' => [ |
|---|
| 177 | 0 => __( 'No', 'cowidgets' ), |
|---|
| 178 | 1 => __( 'Yes', 'cowidgets' ) |
|---|
| 179 | ] |
|---|
| 180 | ] |
|---|
| 181 | ); |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | $this->end_controls_section(); |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | $this->start_controls_section( |
|---|
| 188 | 'section_style_style', |
|---|
| 189 | [ |
|---|
| 190 | 'label' => __( 'Navigation Style', 'cowidgets' ), |
|---|
| 191 | 'tab' => Controls_Manager::TAB_STYLE, |
|---|
| 192 | ] |
|---|
| 193 | ); |
|---|
| 194 | |
|---|
| 195 | $this->add_group_control( |
|---|
| 196 | Group_Control_Typography::get_type(), |
|---|
| 197 | [ |
|---|
| 198 | 'name' => 'label_typography', |
|---|
| 199 | 'label' => 'Label Typography', |
|---|
| 200 | 'separator' => 'before', |
|---|
| 201 | 'selector' => '{{WRAPPER}} .ce-post-navigation .item-label', |
|---|
| 202 | ] |
|---|
| 203 | ); |
|---|
| 204 | |
|---|
| 205 | $this->add_control( |
|---|
| 206 | 'label_color', |
|---|
| 207 | [ |
|---|
| 208 | 'label' => __( 'Label Color', 'cowidgets' ), |
|---|
| 209 | 'type' => Controls_Manager::COLOR, |
|---|
| 210 | 'default' => '#000000', |
|---|
| 211 | 'selectors' => [ |
|---|
| 212 | '{{WRAPPER}} .ce-post-navigation .item-label' => 'color: {{VALUE}}', |
|---|
| 213 | ], |
|---|
| 214 | |
|---|
| 215 | ] |
|---|
| 216 | ); |
|---|
| 217 | |
|---|
| 218 | $this->add_group_control( |
|---|
| 219 | Group_Control_Typography::get_type(), |
|---|
| 220 | [ |
|---|
| 221 | 'name' => 'title_typography', |
|---|
| 222 | 'label' => 'Post Title Typography', |
|---|
| 223 | 'separator' => 'before', |
|---|
| 224 | 'selector' => '{{WRAPPER}} .ce-post-navigation .item-title', |
|---|
| 225 | ] |
|---|
| 226 | ); |
|---|
| 227 | |
|---|
| 228 | $this->add_control( |
|---|
| 229 | 'title_color', |
|---|
| 230 | [ |
|---|
| 231 | 'label' => __( 'Title Color', 'cowidgets' ), |
|---|
| 232 | 'type' => Controls_Manager::COLOR, |
|---|
| 233 | 'default' => '#000000', |
|---|
| 234 | 'selectors' => [ |
|---|
| 235 | '{{WRAPPER}} .ce-post-navigation .item-title' => 'color: {{VALUE}}', |
|---|
| 236 | ], |
|---|
| 237 | |
|---|
| 238 | ] |
|---|
| 239 | ); |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | $this->end_controls_section(); |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | /** |
|---|
| 246 | * Render Copyright output on the frontend. |
|---|
| 247 | * |
|---|
| 248 | * Written in PHP and used to generate the final HTML. |
|---|
| 249 | * |
|---|
| 250 | * @since 1.0.0 |
|---|
| 251 | * @access protected |
|---|
| 252 | */ |
|---|
| 253 | protected function render() { |
|---|
| 254 | $settings = $this->get_settings_for_display(); |
|---|
| 255 | $this->add_render_attribute( 'ce-post-navigation', 'class', [ 'ce-post-navigation', 'ce-post-navigation-style-' . $settings['style'] ] ); |
|---|
| 256 | |
|---|
| 257 | // Sanitize the style parameter |
|---|
| 258 | $sanitized_style = sanitize_file_name( $settings['style'] ); |
|---|
| 259 | |
|---|
| 260 | // Construct the file path |
|---|
| 261 | $file_path = COWIDGETS_DIR . '/inc/widgets-manager/widgets/content/partials/post-navigation/' . $sanitized_style . '.php'; |
|---|
| 262 | |
|---|
| 263 | // Validate the file path to ensure it's within the expected directory |
|---|
| 264 | if ( realpath($file_path) && strpos( realpath($file_path), realpath(COWIDGETS_DIR . '/inc/widgets-manager/widgets/content/partials/post-navigation/') ) === 0 ) { |
|---|
| 265 | // Include the file if it exists and is within the expected directory |
|---|
| 266 | include( $file_path ); |
|---|
| 267 | } else { |
|---|
| 268 | // Handle the error, e.g., show a default message or log the error |
|---|
| 269 | echo 'Invalid file path'; |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | /** |
|---|
| 276 | * Render shortcode widget output in the editor. |
|---|
| 277 | * |
|---|
| 278 | * Written as a Backbone JavaScript template and used to generate the live preview. |
|---|
| 279 | * |
|---|
| 280 | * @since 1.0.0 |
|---|
| 281 | * @access protected |
|---|
| 282 | */ |
|---|
| 283 | protected function content_template() {} |
|---|
| 284 | |
|---|
| 285 | /** |
|---|
| 286 | * Render shortcode output in the editor. |
|---|
| 287 | * |
|---|
| 288 | * Written as a Backbone JavaScript template and used to generate the live preview. |
|---|
| 289 | * |
|---|
| 290 | * Remove this after Elementor v3.3.0 |
|---|
| 291 | * |
|---|
| 292 | * @since 1.0.0 |
|---|
| 293 | * @access protected |
|---|
| 294 | */ |
|---|
| 295 | protected function _content_template() { |
|---|
| 296 | $this->content_template(); |
|---|
| 297 | } |
|---|
| 298 | } |
|---|