Plugin Directory

source: wp-ticket/tags/6.0.4/includes/class-emd-widget.php

Last change on this file was 3356297, checked in by emarket-design, 3 months ago

version 6.0.4

File size: 13.1 KB
Line 
1<?php
2/**
3 * Class for frontend widgets
4 *
5 * @package     EMD
6 * @copyright   Copyright (c) 2014, Emarket Design
7 * @since       1.0
8 */
9if (!defined('ABSPATH')) exit;
10/**
11 * Emd_Widget Class
12 *
13 * Widget class to setup and display frontend widgets.
14 *
15 * @since WPAS 4.0
16 */
17class Emd_Widget extends WP_Widget {
18        /**
19         * Instantiate widget class
20         * @since WPAS 4.0
21         *
22         * @param string $title
23         * @param string $class_label
24         * @param string $description
25         *
26         */
27        public function __construct($id, $title, $class_label, $description) {
28                $this->title = $title;
29                $this->class_label = $class_label;
30                parent::__construct($id, $this->title, array(
31                        'description' => $description,
32                ));
33        }
34        /**
35         * Widget display on frontend
36         * @since WPAS 4.0
37         *
38         * @param array $args
39         * @param array $instance
40         *
41         */
42        public function widget($args, $instance) {
43                if (!isset($args['id'])) {
44                        $args['id'] = $this->id;
45                }
46                extract($args);
47                if(empty($instance['title'])){
48                        $instance['title'] = '';
49                }
50                $title = apply_filters('widget_title', $instance['title']);
51                if(!empty($instance['count'])){
52                        $count = $instance['count'];
53                }
54                else {
55                        $count = 1;
56                }
57                add_filter( 'safe_style_css', function( $styles ) {
58                        $styles[] = 'display';
59                        return $styles;
60                });
61                $allowed = Array(
62                        'input' => Array(
63                                'value' => array() ,
64                                'class' => array() ,
65                                'name' => array() ,
66                                'id' => Array() ,
67                                'type' => Array(),
68                                'placeholder' => Array(),
69                        ),
70                        'div' => array('style'=>array(),'id' => array(), 'class'=>array(), 'data-field'=> array()),
71                        'tr' => array('style'=>array()),
72                        'td' => array('style'=>array()),
73                        'ul' => array('class' => array(), 'style'=>array()),
74                        'li' => array('class' => array(),'style'=>array()),
75                        'form' => array('id'=>array(), 'action' => array(), 'method' => array(), 'class' => array(), 'novalidate' => array()),
76                        'label' => array('class'=>array(), 'for' => array()),
77                        'span' => array('class'=>array(), 'style' => array(), 'id' => array()), 
78                        'img' => array('src'=>array(), 'style' => array(), 'alt' => array()), 
79                        'a' => array(
80                                'style'=> array(),
81                                'href'   => array(),
82                                'target' => array(),
83                                'rel'    => array(),
84                                'data-html'=>array(),
85                                'data-toggle'=>array(),
86                                'title' => array(),
87                                'id' => array(),
88                                'class' => array(),
89                        ),
90                        'button' => Array(
91                                'value' => array(),
92                                'class' => array(),
93                                'name' => array(),
94                                'id' => Array(),
95                                'type' => Array(),
96                        ),
97                        'select' => array(
98                                'id' => array(),
99                                'class' => array(),
100                                'name' => array(),
101                                'placeholder'=> array(),
102                                'data-options'=> array(),
103                                'multiple' => array(),
104                        ),
105                        'option' => array('value'=>array(), 'selected' => array(), 'disabled' => array()),
106                );
107                echo wp_kses($before_widget,$allowed);
108                $pids = Array();
109                $app = str_replace('-', '_', $this->text_domain);
110                $front_ents = emd_find_limitby('frontend', $app);
111                if(!empty($front_ents) && in_array($this->class,$front_ents) && $this->type != 'integration'){
112                        $pids = apply_filters('emd_limit_by', $pids, $app, $this->class,'frontend');
113                }
114                if ($this->type == 'entity') {
115                        $args['filter'] = $this->filter;
116                        if(!empty($instance['pagination'])){
117                                $args['has_pages'] = $instance['pagination'];
118                        }
119                        if(!empty($instance['count_per_page'])){
120                                $args['posts_per_page'] = $instance['count_per_page'];
121                        }
122                        if(!empty($instance['pagination_size'])){
123                                $args['pagination_size'] = $instance['pagination_size'];
124                        }
125                        $args['class'] = $this->class;
126                        $args['cname'] = get_class($this);
127                        $args['app'] = str_replace("-","_",$this->text_domain);
128                        $args['query_args'] = $this->query_args;
129                        $widg_layout = self::get_ent_widget_layout($count, $pids,$args);
130                } elseif ($this->type == 'comment') {
131                        $widg_layout = $this->get_comm_widget_layout($count, $pids);
132                }
133                elseif($this->type == 'integration') {
134                        $widg_layout = $this->layout();
135                }
136                if ($widg_layout) {
137                        if($this->type != 'integration'){
138                                $this->get_header_footer();
139                        }
140                        echo "<div class='emd-container'>";
141                        if ($title) {
142                                echo wp_kses($before_title . $title . $after_title,$allowed);
143                        }
144                        echo "<div class='emd-widg-results' id='wres-" . esc_attr($this->id) . "'>";
145                        echo '<input type="hidden" id="emd_app" value="' . esc_attr($app) . '">';
146                        if($this->type == 'comment'){
147                                echo "<ul class='" . esc_attr($this->css_label) . "-list emd-widget'>";
148                        }
149                        elseif($this->type != 'integration'){
150                                echo wp_kses($this->header,$allowed);
151                        }
152                        echo wp_kses($widg_layout,$allowed);
153                        if($this->type == 'comment'){
154                                echo "</ul>";
155                        }
156                        elseif($this->type != 'integration'){
157                                echo wp_kses($this->footer,$allowed);
158                        }
159                        echo "</div>";
160                        echo "</div>";
161                }
162                echo wp_kses($after_widget,$allowed);
163                $this->enqueue_scripts();
164        }
165        /**
166         * Widget update from admin
167         * @since WPAS 4.0
168         *
169         * @param array $new_instance
170         * @param array $old_instance
171         *
172         * @return array $instance
173         */
174        public function update($new_instance, $old_instance) {
175                $instance = $old_instance;
176                $instance['title'] = strip_tags($new_instance['title']);
177                $instance['count'] = ( int )$new_instance['count'];
178                $instance['pagination'] = ( int )$new_instance['pagination'];
179                if($new_instance['pagination']){
180                        $instance['count_per_page'] = ( int )$new_instance['count_per_page'];
181                        $instance['pagination_size'] = $new_instance['pagination_size'];
182                }
183                else {
184                        $instance['count_per_page'] = '';
185                        $instance['pagination_size'] = '';
186                }
187                return $instance;
188        }
189        /**
190         * Widget form in admin
191         * @since WPAS 4.0
192         *
193         * @param array $instance
194         *
195         */
196        public function form($instance) {
197                $defaults = array(
198                        'title' => $this->title,
199                        'count' => 5,
200                        'count_per_page' => 5,
201                        'pagination' => 0,
202                        'pagination_size' => '',
203                );
204                $instance = wp_parse_args(( array )$instance, $defaults);
205                if (( int )$instance['count'] < 1) {
206                        ( int )$instance['count'] = 5;
207                }
208                if ($instance['pagination'] == 1){
209                        if((int)$instance['count_per_page'] < 1) {
210                                ( int )$instance['count_per_page'] = 5;
211                        }
212                }
213                elseif (empty($instance['pagination'])){
214                        $paginate_style = ' style="display:none;"';
215                }
216?>
217                        <p>
218                        <label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('Title', 'wp-ticket-com'); ?></label><br />
219                        <input class="widefat" type="text" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" value="<?php echo esc_attr($instance['title']); ?>" />
220                        </p>
221                <?php if($this->type != 'integration'){                 ?>
222                        <p>
223                        <label for="<?php echo esc_attr($this->get_field_id('count')); ?>"><?php printf(esc_html__('Max number of %s to show', 'wp-ticket-com') , $this->class_label); ?></label>
224                        <input type="text" id="<?php echo esc_attr($this->get_field_id('count')); ?>" name="<?php echo esc_attr($this->get_field_name('count')); ?>" value="<?php echo esc_attr($instance['count']); ?>" size="3" maxlength="4" /> <br>
225                        </p>
226                        <p>
227                        <input type="checkbox" class="emd-enable-pagination" id="<?php echo esc_attr($this->get_field_id('pagination')); ?>" name="<?php echo esc_attr($this->get_field_name('pagination')); ?>" value="1"
228                        <?php if($instance['pagination']) { echo 'checked'; } ?>/>
229                        <label for="<?php echo esc_attr($this->get_field_id('pagination')); ?>"><?php printf(esc_html__('Enable pagination', 'wp-ticket-com') , $this->class_label); ?></label>
230                        </p>
231                        <p class="emd-paginate-show" <?php echo esc_attr($paginate_style); ?>>
232                        <label for="<?php echo esc_attr($this->get_field_id('pagination_size')); ?>"><?php echo esc_html__('Pagination size', 'wp-ticket-com'); ?></label>
233                        <select id="<?php echo esc_attr($this->get_field_id('pagination_size')); ?>" name="<?php echo esc_attr($this->get_field_name('pagination_size')); ?>">
234                        <option value="small" <?php if($instance['pagination_size'] == 'small') { echo 'selected'; } ?>><?php echo esc_html__('Small','wp-ticket-com'); ?></option>
235                        <option value="medium" <?php if($instance['pagination_size'] == 'medium') { echo 'selected'; } ?>><?php echo esc_html__('Medium','wp-ticket-com'); ?></option>
236                        <option value="large" <?php if($instance['pagination_size'] == 'large') { echo 'selected'; } ?>><?php echo esc_html__('Large','wp-ticket-com'); ?></option>
237                        </select>
238                        </p>
239                        <p class="emd-paginate-show" <?php echo esc_attr($paginate_style); ?>>
240                        <label for="<?php echo esc_attr($this->get_field_id('count_per_page')); ?>"><?php printf(esc_html__('Number of %s to show per pagination', 'wp-ticket-com') , $this->class_label); ?></label>
241                        <input type="text" id="<?php echo esc_attr($this->get_field_id('count_per_page')); ?>" name="<?php echo esc_attr($this->get_field_name('count_per_page')); ?>" value="<?php echo esc_attr($instance['count_per_page']); ?>" size="2" maxlength="2" /> <br>
242                        </p>
243                        <?php
244                }
245        }
246        /**
247         * Runs wp query and creates layout for entity widgets
248         * @since WPAS 4.0
249         *
250         * @param string $posts_per_page
251         * @param array $pids
252         * @param array $args
253         *
254         * @return string $layout
255         */
256        public static function get_ent_widget_layout($posts_per_page, $pids, $args = Array()) {
257                $paged = 1;
258                $layout = "";
259                if(!empty($args['filter'])){
260                        $emd_query = new Emd_Query($args['class'],$args['app'],$args['query_args']['context']);
261                        $emd_query->args_filter($args['filter']);
262                        $args['query_args'] = array_merge($args['query_args'],$emd_query->args);
263                }
264       
265                if(!empty($args['query_args']['paged'])){
266                        $paged = $args['query_args']['paged'];
267                }       
268                if (!empty($args['has_pages'])) {
269                        $args['query_args']['posts_per_page'] = $args['posts_per_page'];
270                        $total_pages = round($posts_per_page / $args['posts_per_page']);
271                        if (get_query_var('paged')) $paged = get_query_var('paged');
272                }
273                else {
274                        $total_pages = 1;
275                        $args['query_args']['posts_per_page'] = $posts_per_page;
276                }
277                $args['query_args']['paged'] = $paged;
278                $args['query_args']['post__in'] = $pids;
279                if($paged <= $total_pages){
280                        $mywidget = new WP_Query($args['query_args']);
281                        if($mywidget->max_num_pages < $total_pages){
282                                $total_pages = $mywidget->max_num_pages;
283                        }
284                        if(!isset($args['posts_per_page'])){
285                                $args['posts_per_page'] = 0;
286                        }
287                        $record_count = ($paged - 1) * intval($args['posts_per_page']) + 1;
288                        while ($mywidget->have_posts()) {
289                                $mywidget->the_post();
290                                if(!isset($args['has_pages']) || (isset($args['has_pages']) && $record_count <= $posts_per_page)){
291                                        if (isset($args['fname'])) {
292                                                $layout.= $args['fname']();
293                                        } else {
294                                                $layout .= call_user_func(array($args['cname'],"layout"));
295                                        }
296                                        $record_count ++;
297                                }
298                        }
299                        wp_reset_postdata();
300                }
301                if(!empty($args['filter'])){
302                        $emd_query->remove_filters();
303                }
304                if (isset($args['has_pages'])) {
305                        $paging_text = paginate_links(array(
306                                'total' => $total_pages,
307                                'current' => $paged,
308                                //'base' => get_pagenum_link() . '&%_%',
309                                'base' => '&%_%',
310                                'format' => 'paged=%#%',
311                                'type' => 'array',
312                                'add_args' => true,
313                        ));
314                        if(!empty($paging_text)){
315                                $paging_html = "<ul class='emd-pagination"; 
316                                switch($args['pagination_size']){
317                                        case 'medium':
318                                                break;
319                                        case 'large':
320                                                $paging_html .= " emd-pagination-lg";
321                                                break;
322                                        case 'small':
323                                        default:
324                                                $paging_html .= " emd-pagination-sm";
325                                                break;
326                                }
327                                $paging_html .= "'>";
328                                foreach ($paging_text as $key_paging => $my_paging) {
329                                        $paging_html .= "<li";
330                                        if(strpos($my_paging,'page-numbers current') !== false){
331                                                $paging_html .= " class='active'";
332                                        }
333                                        $paging_html .= ">" . $my_paging . "</li>";
334                                }
335                                $paging_html .= "</ul>";
336                                $layout .= '<div class="widg-pagination">' . $paging_html . '</div>';
337                        }
338                }
339                return $layout;
340        }
341        /**
342         * Runs wp query and creates layout for comment widgets
343         * @since WPAS 4.0
344         *
345         * @param string $posts_per_page
346         * @param array $pids
347         *
348         * @return string $output
349         */
350        public function get_comm_widget_layout($posts_per_page, $pids) {
351                $ccount = 0;
352                $output = "";
353                $this->query_args['number'] = $posts_per_page;
354                if (empty($pids)) {
355                        $comments = get_comments(apply_filters('widget_comments_args', $this->query_args));
356                        if ($comments) {
357                                foreach ((array)$comments as $comment) {
358                                        $output.= '<li class="recentcomments">' . sprintf(esc_html__('%1$s on %2$s', 'wp-ticket-com') , get_comment_author_link($comment->comment_ID) , '<a href="' . esc_url(get_comment_link($comment->comment_ID)) . '">' . get_the_title($comment->comment_post_ID) . '</a>') . '</li>';
359                                }
360                        }
361                        return $output;
362                }
363                foreach ($pids as $cpid) {
364                        if ($ccount < $posts_per_page && $cpid != 0) {
365                                $this->query_args['post_id'] = $cpid;
366                                $comments = get_comments(apply_filters('widget_comments_args', $this->query_args));
367                                if ($comments) {
368                                        foreach ((array)$comments as $comment) {
369                                                if ($ccount < $posts_per_page) {
370                                                        $ccount++;
371                                                        $output.= '<li class="recentcomments">' . sprintf(esc_html__('%1$s on %2$s', 'wp-ticket-com') , get_comment_author_link($comment->comment_ID) , '<a href="' . esc_url(get_comment_link($comment->comment_ID)) . '">' . get_the_title($comment->comment_post_ID) . '</a>') . '</li>';
372                                                }
373                                        }
374                                }
375                        }
376                }
377                return $output;
378        }
379}
Note: See TracBrowser for help on using the repository browser.