Plugin Directory

source: cowidgets-elementor-addons/trunk/inc/widgets-manager/widgets/content/class-ce-posts-grid.php

Last change on this file was 3099564, checked in by codelessthemes, 11 months ago

fixed security bugs

File size: 17.0 KB
Line 
1<?php
2/**
3 * Elementor Classes.
4 *
5 * @package CoWidgets
6 */
7
8namespace COWIDGETS\WidgetsManager\Widgets;
9
10use Elementor\Controls_Manager;
11use Elementor\Utils;
12use Elementor\Group_Control_Typography;
13use Elementor\Core\Schemes\Typography;
14use Elementor\Core\Schemes\Color;
15use Elementor\Widget_Base;
16use Elementor\Group_Control_Image_Size;
17
18if ( ! defined( 'ABSPATH' ) ) {
19        exit;   // Exit if accessed directly.
20}
21
22/**
23 * Elementor Portfolio Carousel
24 *
25 * Elementor widget for Portfolio Carousel.
26 *
27 * @since 1.0.0
28 */
29class Posts_Grid extends Widget_Base {
30
31        /**
32         * Retrieve the widget name.
33         *
34         * @since 1.0.0
35         *
36         * @access public
37         *
38         * @return string Widget name.
39         */
40        public function get_name() {
41                return 'ce-posts-grid';
42        }
43        /**
44         * Retrieve the widget title.
45         *
46         * @since 1.0.0
47         *
48         * @access public
49         *
50         * @return string Widget title.
51         */
52        public function get_title() {
53                return __( 'Posts Grid', 'cowidgets' );
54        }
55        /**
56         * Retrieve the widget icon.
57         *
58         * @since 1.0.0
59         *
60         * @access public
61         *
62         * @return string Widget icon.
63         */
64        public function get_icon() {
65                return 'ce-icon-posts-grid';
66        }
67        /**
68         * Retrieve the list of categories the widget belongs to.
69         *
70         * Used to determine where to display the widget in the editor.
71         *
72         * Note that currently Elementor supports only one category.
73         * When multiple categories passed, Elementor uses the first one.
74         *
75         * @since 1.0.0
76         *
77         * @access public
78         *
79         * @return array Widget categories.
80         */
81        public function get_categories() {
82                return [ 'ce-content-widgets' ];
83        }
84
85        public function get_script_depends() {
86                return [ 'ce-posts-grid' ];
87    }
88   
89        /**
90         * Register controls.
91         *
92         * @since 1.0.0
93         * @access protected
94         */
95        protected function register_controls() {
96                $this->register_posts_grid_controls();
97        }
98        /**
99         * Register General Controls.
100         *
101         * @since 1.0.0
102         * @access protected
103         */
104        protected function register_posts_grid_controls() {
105                \COWIDGETS_Helpers::postsSelectionSection( $this );
106                $this->start_controls_section(
107                        'section_content_style',
108                        [
109                                'label'     => __( 'Item Layout/Style', 'cowidgets' ),
110                                'tab'       => Controls_Manager::TAB_CONTENT,
111                        ]
112                );
113
114                $this->add_control(
115                        'item_style',
116                        [
117                                'label' => __( 'Style', 'cowidgets' ),
118                                'type' => \Elementor\Controls_Manager::SELECT,
119                                'default' => 'adair',
120                                'options' => apply_filters( 'ce_load_element_styles', [
121                                        'adair' => __( 'Adair', 'cowidgets' ),
122                                        'colm'  => __( 'Colm', 'cowidgets' ),
123                                        'birk'  => __( 'Birk', 'cowidgets' ),
124                                        'lark'  => __( 'Lark', 'cowidgets' ),
125                                        'box'   => __( 'Box', 'cowidgets' ),
126                                        'drake' => __( 'Drake', 'cowidgets' )
127                                ] ),
128                        ]
129        );
130
131        $this->add_group_control(
132                        Group_Control_Image_Size::get_type(),
133                        [
134                                'name'    => 'image_size',
135                                'label'   => __( 'Image Size', 'cowidgets' ),
136                                'default' => 'medium',
137                        ]
138                );
139       
140        $this->add_control(
141                        'columns',
142                        [
143                                'label' => __( 'Columns', 'cowidgets' ),
144                                'type' => \Elementor\Controls_Manager::SELECT,
145                                'default' => '3',
146                                'options' => [
147                    1                           => __( '1', 'cowidgets' ),
148                    2                           => __( '2', 'cowidgets' ),
149                    3                           => __( '3', 'cowidgets' ),
150                    4                           => __( '4', 'cowidgets' ),
151                    5                           => __( '5', 'cowidgets' ),
152                    6                           => __( '6', 'cowidgets' ),
153                    7                           => __( '7', 'cowidgets' ),
154                                ],
155                                'render_type' => 'template'
156                        ]
157                );
158
159                $this->add_control(
160                        'layout',
161                        [
162                                'label' => __( 'Layout', 'cowidgets' ),
163                                'type' => \Elementor\Controls_Manager::SELECT,
164                                'default' => 'masonry',
165                                'options' => [
166                    'masonry'                           => __( 'Masonry', 'cowidgets' ),
167                    'fitRows'                           => __( 'fitRows', 'cowidgets' )
168                                ],
169                                'render_type' => 'template'
170                        ]
171                );
172
173                $this->add_responsive_control(
174                        'space_between',
175                        [
176                                'label'       => __( 'Space between items', 'cowidgets' ),
177                                'type'        => Controls_Manager::SLIDER,
178                                'size_units'  => ['px'],
179                                'range'       => [
180                                        'px' => [
181                                                'max' => 500,
182                                                'min' => 0,
183                                        ],
184                                ],
185                                'default'     => [],
186                                'selectors'   => [
187                                        '{{WRAPPER}} .ce-posts-grid .ce-post-item' => 'padding: {{SIZE}}{{UNIT}}',
188                                        '{{WRAPPER}} .ce-posts-grid' => 'margin-left: -{{SIZE}}{{UNIT}}; margin-right: -{{SIZE}}{{UNIT}}'
189                                ],
190                                'render_type' => 'template'
191                        ]
192                );
193
194                $this->add_control(
195                        'entry_content',
196                        [
197                                'label' => __( 'Content?', 'cowidgets' ),
198                                'type' => \Elementor\Controls_Manager::SELECT,
199                                'default' => 'excerpt',
200                                'options' => [
201                    'excerpt'                           => __( 'Excerpt', 'cowidgets' ),
202                                        'content'                               => __( 'Content', 'cowidgets' ),
203                                        'none'                                  => __( 'None', 'cowidgets' ),
204                                ],
205                                'render_type' => 'template'
206                        ]
207                );
208
209                $this->add_responsive_control(
210                        'content_limit',
211                        [
212                                'label'       => __( 'Content Limit', 'cowidgets' ),
213                                'type'        => Controls_Manager::SLIDER,
214                                'size_units'  => [],
215                                'range'       => [
216                                                'max' => 500,
217                                                'min' => 10,
218                                ],
219                                'default'     => [],
220                                'render_type' => 'template',
221                                'condition' => [
222                                        'entry_content' => 'excerpt'
223                                ]
224                        ]
225                );
226
227                $this->add_control(
228                        'entry_readmore',
229                        [
230                                'label' => __( '"Continue Reading"?', 'cowidgets' ),
231                                'description' => __( 'Works only when style has a "Continue Reading" button by default', 'cowidgets' ),
232                                'type' => \Elementor\Controls_Manager::SELECT,
233                                'default' => 1,
234                                'options' => [
235                    0                           => __( 'No', 'cowidgets' ),
236                                        1                               => __( 'Yes', 'cowidgets' )
237                                ],
238                                'render_type' => 'template'
239                        ]
240                );
241
242                $this->add_control(
243                        'user_icon',
244                        [
245                                'label'       => __( 'User Icon', 'cowidgets' ),
246                                'type'        => Controls_Manager::ICONS,
247                                'label_block' => 'true',
248                                'default'     => [
249                                        'value'   => 'feather feather-user',
250                                        'library' => 'feather',
251                                ],
252                        ]
253                );
254
255                $this->add_control(
256                        'date_icon',
257                        [
258                                'label'       => __( 'Date Icon', 'cowidgets' ),
259                                'type'        => Controls_Manager::ICONS,
260                                'label_block' => 'true',
261                                'default'     => [
262                                        'value'   => 'feather feather-clock',
263                                        'library' => 'feather',
264                                ],
265                        ]
266                );
267               
268                $this->end_controls_section();
269
270
271                $this->start_controls_section(
272                        'section_style',
273                        [
274                                'label'     => __( 'Global', 'cowidgets' ),
275                                'tab'       => Controls_Manager::TAB_STYLE
276                        ]
277                );
278
279                $this->add_control(
280                        'global_important_note',
281                        [
282                                'type' => \Elementor\Controls_Manager::RAW_HTML,
283                                'raw' => __( 'This section contains global styling options of this element. Some of this options should not work on specific selected styles. Please check the section below for style-specific options.', 'cowidgets' ),
284                                'content_classes' => 'important_note'
285                        ]
286                );
287
288                $this->add_group_control(
289                        \Elementor\Group_Control_Typography::get_type(),
290                        [
291                                'name' => 'item_title_typography',
292                                'label' => __( 'Item Title Typography', 'plugin-domain' ),
293                                'scheme' => Typography::TYPOGRAPHY_1,
294                                'selector' => '{{WRAPPER}} .ce-post-item .entry-title',
295                        ]
296                );
297
298                $this->add_control(
299                        'item_title_color',
300                        [
301                                'label' => __( 'Item Title Color', 'cowidgets' ),
302                                'type' => \Elementor\Controls_Manager::COLOR,
303                               
304                                'selectors' => [
305                                        '{{WRAPPER}} .ce-post-item .entry-title' => 'color: {{VALUE}}',
306                                ],
307                        ]
308                );
309
310                $this->add_group_control(
311                        \Elementor\Group_Control_Typography::get_type(),
312                        [
313                                'name' => 'item_meta_typography',
314                                'label' => __( 'Item Meta Typography', 'cowidgets' ),
315                                'scheme' => Typography::TYPOGRAPHY_1,
316                                'selector' => '{{WRAPPER}} .ce-post-item .entry-meta-single',
317                        ]
318                );
319
320                $this->add_control(
321                        'item_meta_color',
322                        [
323                                'label' => __( 'Item Meta Color', 'cowidgets' ),
324                                'type' => \Elementor\Controls_Manager::COLOR,
325                               
326                                'selectors' => [
327                                        '{{WRAPPER}} .ce-post-item .entry-meta-single' => 'color: {{VALUE}}',
328                                ],
329                        ]
330                );
331
332                $this->add_group_control(
333                        \Elementor\Group_Control_Typography::get_type(),
334                        [
335                                'name' => 'item_content_typography',
336                                'label' => __( 'Item Content Typography', 'cowidgets' ),
337                                'scheme' => Typography::TYPOGRAPHY_1,
338                                'selector' => '{{WRAPPER}} .ce-post-item .entry-content',
339                        ]
340                );
341
342                $this->add_control(
343                        'item_content_color',
344                        [
345                                'label' => __( 'Item Content Color', 'cowidgets' ),
346                                'type' => \Elementor\Controls_Manager::COLOR,
347                               
348                                'selectors' => [
349                                        '{{WRAPPER}} .ce-post-item .entry-content' => 'color: {{VALUE}}',
350                                ],
351                        ]
352                );
353
354                $this->add_group_control(
355                        \Elementor\Group_Control_Typography::get_type(),
356                        [
357                                'name' => 'item_readmore_typography',
358                                'label' => __( '"Continue Reading" Typography', 'cowidgets' ),
359                                'scheme' => Typography::TYPOGRAPHY_1,
360                                'selector' => '{{WRAPPER}} .ce-post-item .entry-readmore',
361                        ]
362                );
363
364                $this->add_control(
365                        'item_readmore_color',
366                        [
367                                'label' => __( '"Continue Reading" Color', 'cowidgets' ),
368                                'type' => \Elementor\Controls_Manager::COLOR,
369                               
370                                'selectors' => [
371                                        '{{WRAPPER}} .ce-post-item a.entry-readmore' => 'color: {{VALUE}}',
372                                ],
373                        ]
374                );
375
376
377                $this->end_controls_section();
378
379                $this->start_controls_section(
380                        'section_specific_style',
381                        [
382                                'label'     => __( 'Style-specific', 'cowidgets' ),
383                                'tab'       => Controls_Manager::TAB_STYLE,
384                        ]
385                );
386
387                $this->add_control(
388                        'specific_important_note',
389                        [
390                                'type' => \Elementor\Controls_Manager::RAW_HTML,
391                                'raw' => __( 'This section contains style-specific options, these options may disappear when you change selected item-style', 'cowidgets' ),
392                                'content_classes' => 'important_note'
393                        ]
394                );
395
396                $this->add_control(
397                        'adair_social_color',
398                        [
399                                'label' => __( 'Adair Socials Color', 'cowidgets' ),
400                                'type' => \Elementor\Controls_Manager::COLOR,
401                               
402                                'selectors' => [
403                                        '{{WRAPPER}} .ce-post-item .ce-share-buttons' => 'color: {{VALUE}}',
404                                ],
405                                'condition' => [
406                                        'item_style' => 'adair'
407                                ]
408                        ]
409                );
410
411                $this->add_control(
412                        'adair_footer_borders',
413                        [
414                                'label' => __( 'Adair Footer Border Color', 'cowidgets' ),
415                                'type' => \Elementor\Controls_Manager::COLOR,
416                               
417                                'selectors' => [
418                                        '{{WRAPPER}} .ce-post-item .entry-footer' => 'border-color: {{VALUE}}',
419                                ],
420                                'condition' => [
421                                        'item_style' => 'adair'
422                                ]
423                        ]
424                );
425
426                $this->add_control(
427                        'birk_wrapper_bg',
428                        [
429                                'label' => __( 'Birk Wrapper BG', 'cowidgets' ),
430                                'type' => \Elementor\Controls_Manager::COLOR,
431                               
432                                'selectors' => [
433                                        '{{WRAPPER}} .ce-post-item .entry-wrapper-content' => 'background-color: {{VALUE}}',
434                                ],
435                                'condition' => [
436                                        'item_style' => 'birk'
437                                ]
438                        ]
439                );
440
441                do_action( 'ce_load_style_specific_options', $this );
442
443
444                $this->end_controls_section();
445       
446                \COWIDGETS_Helpers::animationSection( $this );
447               
448                $this->start_controls_section(
449                        'section_pagination',
450                        [
451                                'label'     => __( 'Pagination', 'cowidgets' ),
452                                'tab'       => Controls_Manager::TAB_CONTENT,
453                        ]
454                );
455
456                $this->add_control(
457                        'pagination',
458                        [
459                                'label' => __( 'Pagination Style', 'cowidgets' ),
460                                'type' => \Elementor\Controls_Manager::SELECT,
461                                'default' => 'none',
462                                'options' => [
463                                        'none'  => __( 'None', 'cowidgets' ),
464                                        'numbers'       => __( 'Numbers', 'cowidgets' ),
465                                        'next_prev'     => __( 'Next/Prev', 'cowidgets' ),
466                                        'load_more'     => __( 'Load More', 'cowidgets' ),
467                                        'infinity_scroll' => __( 'Infinity Scroll', 'cowidgets' )
468                                ],
469                        ]
470        );
471        }
472
473        /**
474         * Render Copyright output on the frontend.
475         *
476         * Written in PHP and used to generate the final HTML.
477         *
478         * @since 1.0.0
479         * @access protected
480         */
481        protected function render() {
482        $settings = $this->get_settings_for_display();
483
484        $this->add_render_attribute( 'ce-posts-grid', 'class', [ 'ce-posts-grid', 'ce-post-style-' . $settings['item_style'] ] );
485                $this->add_render_attribute( 'ce-posts-grid', 'data-columns', $settings['columns'] );
486                $this->add_render_attribute( 'ce-posts-grid', 'data-layout', $settings['layout'] );
487       
488   
489
490        if( $settings['ce_animation'] != 'none' ){
491            $this->add_render_attribute( 'ce-post-item', 'data-speed', $settings['ce_animation_speed'] );
492           
493                }
494               
495                $final_image_size = $settings['image_size_size'];
496
497                if($final_image_size == 'custom'){
498                        require_once ELEMENTOR_PATH . 'includes/libraries/bfi-thumb/bfi-thumb.php';
499
500                        $image_dimension = $settings['image_size_custom_dimension'];
501
502                        $final_image_size = [
503                                // Defaults sizes.
504                                0           => null, // Width.
505                                1           => null, // Height.
506
507                                'bfi_thumb' => true,
508                                'crop'      => true,
509                        ];
510
511                        $has_custom_size = false;
512                        if ( ! empty( $image_dimension['width'] ) ) {
513                                $has_custom_size = true;
514                                $final_image_size[0]   = $image_dimension['width'];
515                        }
516
517                        if ( ! empty( $image_dimension['height'] ) ) {
518                                $has_custom_size = true;
519                                $final_image_size[1]   = $image_dimension['height'];
520                        }
521
522                        if ( ! $has_custom_size ) {
523                                $final_image_size = 'full';
524                        }
525                }
526
527                ?>
528                <div <?php echo wp_kses_post( $this->get_render_attribute_string( 'ce-posts-grid' ) ); ?>>
529
530                        <?php   
531                                        if( $settings['entry_content'] == 'excerpt' && $settings['content_limit']['size'] != '' )
532                                                add_filter( 'excerpt_length', function(){
533                                                        $settings = $this->get_settings_for_display();
534                                                        return $settings['content_limit']['size'];
535                                                }, 999 );
536
537                    $new_query = array(
538                        'posts_per_page' => (int) $settings['items_per_page'],
539                    );
540
541                                        if( $settings['items_orderby'] != 'none' ){
542                                                $new_query['orderby'] = $settings['items_orderby'];
543                                                $new_query['order'] = $settings['items_order'];
544                                        }
545
546                    if( is_array( $settings['items_categories'] ) && !empty( $settings['items_categories'] ) ) {
547                   
548                        $new_query['tax_query'] = array(
549                           
550                            array(
551                                'taxonomy' => 'category',
552                                'field' => 'slug',
553                                'terms' => $settings['items_categories'],
554                                'operator' => 'IN' 
555                            ) 
556                        );
557                    }
558
559                    if( !empty( $settings['posts'] ) ){
560                        $new_query['ignore_sticky_posts'] = 1;
561                        $new_query['post__in'] = $settings['posts'];
562                        $new_query['ignore_custom_sort'] = true;
563                                        }
564                                       
565               
566
567                $the_query = new \WP_Query( $new_query );
568               
569                if ( is_object( $the_query ) && $the_query->have_posts() ) :
570                                        $counter = 0;
571                    // Start loop
572                    while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
573                                                <?php 
574                                                        $counter += 1;
575                                                        $delay = ( $counter * (int) $settings['ce_animation_delay'] );
576                                                        if( $counter > $settings['columns'] )
577                                                                $delay = 0;
578
579                                                        $this->add_render_attribute( 'ce-post-item-'.get_the_ID(), 'class', array_merge( ['ce-post-item', 'ce-animation', 'ce-animation--'.$settings['ce_animation'], 'ce-animation-manual'], get_post_class( '', get_the_ID() )  ) );
580                                                        $this->add_render_attribute( 'ce-post-item-'.get_the_ID(), 'data-delay', $delay );
581                                                ?>
582                        <?php
583                                                // Sanitize the item_style parameter
584                                                $sanitized_item_style = sanitize_file_name( $settings['item_style'] );
585
586                                                // Construct the file path
587                                                $file_path = COWIDGETS_DIR . '/inc/widgets-manager/widgets/content/partials/post/' . $sanitized_item_style . '.php';
588
589                                                // Validate the file path to ensure it's within the expected directory
590                                                if ( realpath($file_path) && strpos( realpath($file_path), realpath(COWIDGETS_DIR . '/inc/widgets-manager/widgets/content/partials/post/') ) === 0 ) {
591                                                        // Include the file if it exists and is within the expected directory
592                                                        include( $file_path );
593                                                } else {
594                                                        // Handle the error, e.g., show a default message or log the error
595                                                        echo 'Invalid file path';
596                                                }
597                                                ?>
598
599                    <?php endwhile; ?>
600                <?php wp_reset_postdata(); ?>
601                <?php endif; ?>
602                       
603                </div>
604                <?php
605        }
606
607
608
609        /**
610         * Render shortcode widget output in the editor.
611         *
612         * Written as a Backbone JavaScript template and used to generate the live preview.
613         *
614         * @since 1.0.0
615         * @access protected
616         */
617        protected function content_template() {}
618
619        /**
620         * Render shortcode output in the editor.
621         *
622         * Written as a Backbone JavaScript template and used to generate the live preview.
623         *
624         * Remove this after Elementor v3.3.0
625         *
626         * @since 1.0.0
627         * @access protected
628         */
629        protected function _content_template() {
630                $this->content_template();
631        }
632}
Note: See TracBrowser for help on using the repository browser.