Make WordPress Themes

source: jehanne/1.1.4/inc/widget.php

Last change on this file was 96863, checked in by themedropbox, 7 years ago

New version of Jehanne - 1.1.4

File size: 55.5 KB
Line 
1<?php
2/**
3 * Add a widget
4 */
5class jehanne_ExtendedWidget extends WP_Widget {
6
7        function jehanne_ExtendedWidget() {
8
9                /* Widget settings. */
10                $widget_ops = array(
11                'classname' => 'jehanne_extended',
12                'description' => __('Display posts, pages, images, slideshow, news, related posts and more.', 'jehanne' ));
13
14                /* Widget control settings. */
15                $control_ops = array(
16                'width' => 250,
17                'height' => 250,
18                'id_base' => 'jehanne_extended_widget');
19
20                /* Create the widget. */
21                parent::__construct( 'jehanne_extended_widget', __( 'Jehanne: News, Related, Slideshow', 'jehanne' ), $widget_ops, $control_ops );
22               
23                add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
24                add_action( 'admin_footer-widgets.php', array( $this, 'print_scripts' ), 9999 );
25        }
26        public function enqueue_scripts( $hook_suffix ) {
27                if ( 'widgets.php' !== $hook_suffix ) {
28                        return;
29                }
30
31                wp_enqueue_style( 'wp-color-picker' );
32                wp_enqueue_script( 'wp-color-picker' );
33
34                wp_enqueue_media();
35                wp_enqueue_script( 'jehanne-upload-image', get_template_directory_uri() . '/js/meta-box-image.js', array('jquery') );
36
37        }
38        public function print_scripts() {
39        ?>
40                <script>
41                        ( function( $ ){
42                                        function initColorPicker( widget ) {
43                                                        widget.find( '.color-picker' ).wpColorPicker( {
44                                                                        change: _.throttle( function() { // For Customizer
45                                                                                        $(this).trigger( 'change' );
46                                                                        }, 3000 )
47                                                        });
48                                        }
49                                                function onFormUpdate( event, widget ) {
50                                                        initColorPicker( widget );
51                                        }
52                                        $( document ).on( 'widget-added widget-updated', onFormUpdate );
53
54                                        $( document ).ready( function() {
55                                                        $( '#widgets-right .widget:has(.color-picker)' ).each( function () {
56                                                                        initColorPicker( $( this ) );
57                                                        } );
58                                        } );
59                        }( jQuery ) );
60                </script>
61        <?php
62        }
63
64        function widget( $args, $instance ) {
65                $instance = wp_parse_args( (array) $instance, $this->defaults( $instance ) );
66                $instance = wp_parse_args( (array) $instance, $this->defaults_for_count($instance, $instance['number_of_posts']) ); 
67                // Widget output
68                extract($args);
69                $sidebar_id = $args['id'];
70                $widget_id = $args['widget_id'];
71                $style = $instance['style'];
72
73                $width = $this->getWidth($sidebar_id, $instance['columns'], $instance['padding-right'], $instance['padding-left']);
74                $post_thumbnail_size = $this->get_image_width( $width, $style, $instance['image_size'] );
75               
76                $excludelist = '';
77                $catids = '';
78                $hidden = '';
79                $is_slide_buttons = 0;
80                $slide_buttons = '';
81                $script = '';
82                $on_slide = 0;
83
84                //add special classes to animated widgets
85                if($instance['is_animate'] != '' && isset($instance['animate_style'])) {
86                        $hidden .= ' hidden-widget '.$instance['animate_style'];//is it an animated widget
87                }
88                else if($instance['is_slider'] != '') {
89                        $hidden .= ' slider-widget '.$instance['animate_style'].' '.$widget_id;//or is it a slider
90                        if($instance['text_on_slide'] != '') {
91                                $hidden .= ' text-on-slide '.$instance['animate_style'];
92                                $on_slide = 1;
93                        }
94                        //add buttons for slider navigation
95                        $is_slide_buttons = 1;
96                        $str_id = 0;
97                        preg_match_all('!\d+!', $widget_id, $matches);
98                        $str_id = absint(implode(' ', $matches[0]));
99                        //print global variables for js script
100                        $script = '<script type="text/javascript">
101                                                        if( typeof jehanne_animtype == "undefined") {
102                                                                var jehanne_animtype = [];
103                                                                typeof jehanne_animtype;
104                                                        }
105                                                        if( typeof jehanne_slidespeed == "undefined") {
106                                                                var jehanne_slidespeed = [];
107                                                                typeof jehanne_slidespeed;
108                                                        }
109                                                        if( typeof jehanne_timerinterval == "undefined") {
110                                                                var jehanne_timerinterval = [];
111                                                                typeof jehanne_timerinterval;
112                                                        }                                       
113                                                        if( typeof jehanne_textonslide == "undefined") {
114                                                                var jehanne_textonslide = [];
115                                                                typeof jehanne_textonslide;
116                                                        }                                                       
117                                                        jehanne_animtype['.$str_id.'] = '. esc_js($instance['slider_style']).';
118                                                        jehanne_slidespeed['.$str_id.'] = '. esc_js($instance['slide_effect_speed']).';
119                                                        jehanne_timerinterval['.$str_id.'] = '. esc_js($instance['slide_speed']).';
120                                                        jehanne_textonslide['.$str_id.'] = '. esc_js($on_slide).';
121                                                       
122                                           </script>';
123                }
124               
125                $out = '<div class="wrap-list'.esc_attr($hidden).'">';//add classes to .wrap-list in case of animation
126
127                if( $instance['is_show_custom_blocks'] == ''  ) {
128                        if($instance['is_exclude_current_page_title'] != ''){//exclude current post from the output
129                                global $post;
130                                if($post) {
131                                        $excludelist = '&exclude='.$post->ID;
132                                }
133                        }
134                        if($instance['is_use_current_category'] != '') {//posts from the current category only
135                                global $post;
136                                if($post) {
137                                        $catids = implode(wp_get_post_categories($post->ID), ',');
138                                }
139                        }
140                       
141                        if(trim($instance['page_ids']) != '') {//pages ids from user list only
142                                $posts = get_pages('include='.$instance['page_ids']);
143                        }
144                        else if(trim($instance['post_ids']) != '') {//or posts ids from user list only
145                                $posts = get_posts('has_password=0&include='.($instance['post_ids']));
146                        }
147                        else if($catids != '') {//else if we have cat ids, get it
148                                $posts = get_posts('has_password=0&numberposts='.$instance['number_of_posts'].'&category='.$catids.$excludelist);
149                        }
150                        else {//get posts
151                                $posts = get_posts('has_password=0&numberposts='.$instance['number_of_posts'].'&category='.($instance['cat_id'] == -1 ? '' : $instance['cat_id']).
152                                                'include='.$instance['post_ids'].$excludelist);
153                        }
154                        $buttont = '';
155                        foreach($posts as $currpost) { 
156                                $content = '';
157                                $buttont = '';
158                                $img = '';
159                                if( $instance['is_use_icon'] != '' && ($style == '0' || $style == '3') ){
160                                        $img = '<img class="icon" src="'.esc_url($instance['icon']).'" width="'.esc_attr($instance['icon_w']).'" height="'.esc_attr($instance['icon_h']).'">';
161                                }
162                                if( $instance['is_show_title'] != '') {
163                                        $content = '<a class="w-head" href="'.esc_url(get_permalink($currpost->ID)).'">'.$img.$this->notEmpty(strip_tags($currpost->post_title)).'</a>';
164                                }
165                                if( $instance['is_show_content'] != '') {
166                                        $content .= '<br>'.$this->getContent(apply_filters( 'the_content', $currpost->post_content), $instance['content_length']);
167                                }
168
169                                if( $instance['is_use_link_button'] != '' ){//add button
170                                        if( $instance['is_use_link_more_custom_link'] != '' ) {
171                                                $link = esc_url($instance['link_more_custom_link']);
172                                        }
173                                        else {
174                                                $link = esc_url(get_permalink($currpost->ID));
175                                        }
176                                        $buttont = '<br><a class="link-read-more" href="'.$link.'">'.$this->notEmpty(sanitize_text_field($instance['link_more_text']), __('Read more...', 'jehanne')).'</a>';
177                                }
178
179                                switch ( $style ) {
180                                //image on top
181                                        case '0':
182                                                $out .= '<span class="style-'.$style.' header-list"><a href="'.esc_url(get_permalink($currpost->ID)).'">'.get_the_post_thumbnail($currpost->ID, $post_thumbnail_size).'</a>';
183                                                $out .= '<span class="style-'.$style.' footer-list">'.$content.'</span>'.$buttont.'</span>';
184                                        break;
185                                //image on right
186                                        case '1':       
187                                                $out .= '<span class="style-1 header-list"><a href="'.esc_url(get_permalink($currpost->ID)).'">'.get_the_post_thumbnail($currpost->ID, $post_thumbnail_size).'</a>'.$content.$buttont.'</span>';
188                                        break;
189                                //image on left
190                                        case '2':
191                                                $out .= '<span class="style-2 header-list"><a href="'.esc_url(get_permalink($currpost->ID)).'">'.get_the_post_thumbnail($currpost->ID, $post_thumbnail_size).'</a>'.$content.$buttont.'</span>';
192                                        break;
193                                //no image
194                                        case '3':
195                                                $out .= '<span class="style-'.$style.' header-list">';
196                                                $out .= '<span class="style-'.$style.' footer-list">'.$content.'</span>'.$buttont.'</span>';                           
197                                        break;
198                                }
199                               
200                                if($is_slide_buttons) {//add one more button for slider
201                                        $slide_buttons .= '<li></li>';
202                                }
203                        }
204                } else {
205               
206                        for($i = 0; $i < $instance['number_of_posts']; $i++) { 
207                                $content = '';
208                                $img = '';
209                                $buttont = '';
210                               
211                                if( $instance['is_show_custom_link'] != '' ) {
212                                        $buttont  = '<br><a class="link-read-more" href="'.esc_url($instance['link_'.$i]).'">'.$this->notEmpty(sanitize_text_field($instance['link_caption_'.$i]), __('Read more...', 'jehanne')).'</a>';
213                                }
214                                elseif( $instance['is_use_link_button'] != '' ){//add button
215                                        $buttont = esc_url($instance['link_more_custom_link']);
216                                        $buttont = '<br><a class="link-read-more" href="'.$buttont.'">'.$this->notEmpty(sanitize_text_field($instance['link_more_text']), __('Read more...', 'jehanne')).'</a>';
217                                }
218                               
219                                if( $instance['is_use_icon'] != '' && ($style == '0' || $style == '3') ){
220                                        $img = '<img class="icon" src="'.esc_url($instance['icon']).'" width="'.esc_attr($instance['icon_w']).'" height="'.esc_attr($instance['icon_h']).'">';
221                                }
222                                if( $instance['is_show_custom_title'] != '') {
223                                        $content = '<a class="w-head" href="'.esc_url($instance['link_'.$i]).'">'.esc_html($instance['title_'.$i]).'</a>';
224                                }
225                                if( $instance['is_show_custom_content'] != '') {
226                                        $content .= esc_html($instance['text_'.$i]); 
227                                }
228                               
229                                $img = '';
230                                if( '3' != $style ) {
231                                        if('' == $instance['is_use_image_url'])
232                                                $img = wp_get_attachment_image($instance['image_'.$i], $post_thumbnail_size);
233                                        else
234                                                $img = '<img src='.esc_url($instance['image_url_'.$i]).'>';
235                                }
236
237                                switch ( $style ) {
238                                //image on top
239                                        case '0':
240                                                $out .= '<span class="style-'.$style.' header-list"><a href="'.esc_url($instance['link_'.$i]).'">'.$img.'</a>';
241                                                $out .= '<span class="style-'.$style.' footer-list">'.$content.'</span>'.$buttont.'</span>';
242                                        break;
243                                //image on right
244                                        case '1':
245                                                $out .= '<span class="style-1 header-list"><a href="'.esc_url($instance['link_'.$i]).'">'.$img.'</a>'.$content.$buttont.'</span>';
246                                        break;
247                                //image on left
248                                        case '2':
249                                                $out .= '<span class="style-2 header-list"><a href="'.esc_url(get_permalink($currpost->ID)).'">'.$img.'</a>'.$content.$buttont.'</span>';
250                                        break;
251                                //no image
252                                        case '3':
253                                                $out .= '<span class="style-'.$style.' header-list">';
254                                                $out .= '<span class="style-'.$style.' footer-list">'.$content.'</span>'.$buttont.'</span>';                           
255                                        break;
256                                }
257                               
258                                if($is_slide_buttons) {//add one more button for slider
259                                        $slide_buttons .= '<li></li>';
260                                }
261                        }
262                }
263       
264                if($is_slide_buttons) {//for slider
265                        $out .= '<ul class="slider-buttons">'.$slide_buttons.'</ul>';
266                }
267               
268                $out .= '</div>';       
269
270                //print the widget for the sidebar
271                echo ($script);//variables for js script (time and amination type)
272                echo $before_widget;
273                if(trim($instance['title']) !== '') echo $before_title.esc_html($instance['title']).$after_title;
274                echo $out;
275                echo $after_widget;
276        }
277
278        function update( $new_instance, $old_instance ) {
279                // Save widget options
280                foreach ( $new_instance as $key => $instance ) {
281                        $new_instance[$key] = strip_tags($new_instance[$key], '<a><b><i>');
282                }
283                $new_instance['number_of_posts'] = absint($new_instance['number_of_posts']);
284                $new_instance['number_of_posts'] = ( $new_instance['number_of_posts'] == 0 ? 1 : $new_instance['number_of_posts']);
285               
286                if( isset($new_instance['is_exclude_current_page_title']) )
287                        $new_instance['is_exclude_current_page_title'] = 1;             
288                if( isset($new_instance['is_image_fixed']) )
289                        $new_instance['is_image_fixed'] = 1;
290                if( isset($new_instance['is_use_current_category']) )
291                        $new_instance['is_use_current_category'] = 1;
292                if( isset($new_instance['is_use_background_image']) )
293                        $new_instance['is_use_background_image'] = 1;
294                if( isset($new_instance['is_hide_on_small_screen']) )
295                        $new_instance['is_hide_on_small_screen'] = 1;
296                if( isset($new_instance['is_repeat_x']) )
297                        $new_instance['is_repeat_x'] = 1;
298                if( isset($new_instance['is_repeat_y']) )
299                        $new_instance['is_repeat_y'] = 1;
300                if( isset($new_instance['is_use_background_color']) )
301                        $new_instance['is_use_background_color'] = 1;
302                if( isset($new_instance['is_show_title']) )
303                        $new_instance['is_show_title'] = 1;
304                if( isset($new_instance['is_show_custom_title']) )
305                        $new_instance['is_show_custom_title'] = 1;
306                if( isset($new_instance['is_show_custom_content']) )
307                        $new_instance['is_show_custom_content'] = 1;
308                if( isset($new_instance['is_show_custom_image']) )
309                        $new_instance['is_show_custom_image'] = 1;
310                if( isset($new_instance['is_show_custom_link']) )
311                        $new_instance['is_show_custom_link'] = 1;
312                if( isset($new_instance['is_use_border']) )
313                        $new_instance['is_use_border'] = 1;
314                if( isset($new_instance['is_use_link_button']) ) 
315                        $new_instance['is_use_link_button'] = 1;
316                if( isset($new_instance['is_use_link_more_custom_link']) )
317                        $new_instance['is_use_link_more_custom_link'] = 1;
318                if( isset($new_instance['is_use_icon']) )
319                        $new_instance['is_use_icon'] = 1;
320                if( isset($new_instance['is_animate']) )
321                        $new_instance['is_animate'] = 1;
322                if( isset($new_instance['is_slider']) )
323                        $new_instance['is_slider'] = 1;
324                if( isset($new_instance['text_on_slide']) )
325                        $new_instance['text_on_slide'] = 1;             
326                if( isset($new_instance['is_use_image_url']) )
327                        $new_instance['is_use_image_url'] = 1;
328                       
329               
330                $possible_values = array( '1', '2', '4', '8' );
331                $new_instance['columns'] = ( in_array( $new_instance['columns'], $possible_values ) ? $new_instance['columns'] : '4' );
332                $new_instance['cat_id'] = absint($new_instance['cat_id']);
333                $new_instance['post_ids'] = $this->sanitizeIDs($new_instance['post_ids']);
334                $new_instance['page_ids'] = $this->sanitizeIDs($new_instance['page_ids']);
335                $possible_values = array( '0', '1', '2', '3' );
336                $new_instance['style'] = ( in_array( $new_instance['style'], $possible_values ) ? $new_instance['style'] : '0' );
337                $new_instance['image_size'] = absint($new_instance['image_size']);
338                $new_instance['background_image'] = esc_url_raw($new_instance['background_image']);
339                $new_instance['background_color'] = $this->sanitize_hex_color($new_instance['background_color']);
340                $new_instance['background_text_color'] = $this->sanitize_hex_color($new_instance['background_text_color']);
341                $new_instance['main_title_color'] = $this->sanitize_hex_color($new_instance['main_title_color']);
342                $new_instance['title_color'] = $this->sanitize_hex_color($new_instance['title_color']);
343                $new_instance['text_color'] = $this->sanitize_hex_color($new_instance['text_color']);
344                $new_instance['link_color'] = $this->sanitize_hex_color($new_instance['link_color']);
345                $possible_values = array( 'right', 'left', 'center' );
346                $new_instance['pos_x'] = ( in_array( $new_instance['pos_x'], $possible_values ) ? $new_instance['pos_x'] : '0' );
347                $possible_values = array( 'top', 'bottom', 'center' );
348                $new_instance['pos_y'] = ( in_array( $new_instance['pos_y'], $possible_values ) ? $new_instance['pos_y'] : '0' );
349                $new_instance['title_size'] = absint($new_instance['title_size']);
350                $new_instance['title_size'] = ($new_instance['title_size'] < 90 ? $new_instance['title_size'] : 90);
351                $new_instance['text_size'] = absint($new_instance['text_size']);
352                $new_instance['text_size'] = ($new_instance['text_size'] < 90 ? $new_instance['text_size'] : 90);
353                $new_instance['link_size'] = absint($new_instance['link_size']);
354                $new_instance['link_size'] = ($new_instance['link_size'] < 90 ? $new_instance['link_size'] : 90);
355                $new_instance['content_length'] = absint($new_instance['content_length']);
356                $new_instance['custom_title'] = esc_html($new_instance['custom_title']);
357                $new_instance['custom_content'] = esc_html($new_instance['custom_content']);
358                $new_instance['border_color'] = $this->sanitize_hex_color($new_instance['border_color']);
359                $new_instance['link_more_background_color'] = $this->sanitize_hex_color($new_instance['link_more_background_color']);
360                $new_instance['link_more_text_color'] = $this->sanitize_hex_color($new_instance['link_more_text_color']);
361                $new_instance['link_more_border_color'] = $this->sanitize_hex_color($new_instance['link_more_border_color']);
362                $new_instance['link_more_custom_link'] = esc_url_raw($new_instance['link_more_custom_link']);
363                $possible_values = array( 'dotted', 'dashed', 'solid', 'double', 'groove', 'ridge', 'inset', 'outset' );
364                $new_instance['border_style'] = ( in_array( $new_instance['border_style'], $possible_values ) ? $new_instance['border_style'] : 'double');
365                $new_instance['link_more_border_style'] = ( in_array( $new_instance['link_more_border_style'], $possible_values ) ? $new_instance['link_more_border_style'] : 'solid');
366                $new_instance['link_more_text'] = esc_html($new_instance['link_more_text']);
367                $new_instance['border_width'] = absint($new_instance['border_width']);
368                $new_instance['border_radius'] = absint($new_instance['border_radius']);
369                $new_instance['link_more_text_size'] = absint($new_instance['link_more_text_size']);
370                $new_instance['link_more_border_width'] = absint($new_instance['link_more_border_width']);
371                $new_instance['link_more_border_radius'] = absint($new_instance['link_more_border_radius']);
372                $new_instance['padding-top'] = absint($new_instance['padding-top']);
373                $new_instance['padding-right'] = absint($new_instance['padding-right']);
374                $new_instance['padding-right'] = ($new_instance['padding-right'] < 1349 ? $new_instance['padding-right'] : 0);
375                $new_instance['padding-right'] = absint($new_instance['padding-right']);
376                $new_instance['padding-bottom'] = absint($new_instance['padding-bottom']);
377                $new_instance['padding-left'] = absint($new_instance['padding-left']);
378                $new_instance['padding-left'] = ($new_instance['padding-left'] < 1349 ? $new_instance['padding-left'] : 0);
379                $new_instance['icon_h'] = absint($new_instance['icon_h']);
380                $new_instance['icon_w'] = absint($new_instance['icon_w']);
381                $possible_values = array( 'step', 'all' );
382                $new_instance['animate_style'] = ( in_array( $new_instance['animate_style'], $possible_values ) ? $new_instance['animate_style'] : 'step');
383                $new_instance['element_margin'] = absint($new_instance['element_margin']);
384                $new_instance['element_margin'] = ($new_instance['element_margin'] < 80 ? $new_instance['element_margin'] : 80);
385               
386                for( $i = 0; $i < $new_instance[$count]; $i++ ) {
387                        $new_instance['title_'.$i] = esc_html($new_instance['title_'.$i]); 
388                        $new_instance['text_'.$i] = esc_html($new_instance['text_'.$i]); 
389                        $new_instance['link_'.$i] = esc_url_raw($new_instance['link_'.$i]);
390                        $new_instance['image_'.$i] = absint($new_instance['image_'.$i]);
391                        $new_instance['link_caption_'.$i] = esc_html($new_instance['link_caption_'.$i]); 
392                        $new_instance['image_url_'.$i] = esc_url_raw($new_instance['image_url_'.$i]);                           
393                }
394               
395                //Slider
396                if(array_key_exists('is_slider', $new_instance)) {//force 1 column/image on top/padding 0
397                        $new_instance['columns'] = 1;
398                        $new_instance['style'] = 0;
399                       
400                        $new_instance['padding-top'] = 0;
401                        $new_instance['padding-right'] = 0;
402                        $new_instance['padding-bottom'] = 0;
403                        $new_instance['padding-left'] = 0;
404                }
405               
406                $possible_values = array( '1', '2', '3', '4' );
407                $new_instance['slider_style'] = ( in_array( $new_instance['slider_style'], $possible_values ) ? $new_instance['slider_style'] : '3');
408                $new_instance['slide_speed'] = absint($new_instance['slide_speed']);
409                $new_instance['slide_effect_speed'] = absint($new_instance['slide_effect_speed']);
410
411                return $new_instance;
412        }
413
414        function form( $instance ) {
415                // Output admin widget options form
416                $isnotsaved = 0;
417                if( $instance == null ) {
418                        $isnotsaved = 1;
419                }
420                // Set up some default widget settings.
421                $instance = wp_parse_args( (array) $instance, $this->defaults( $instance ) );
422                $instance = wp_parse_args( (array) $instance, $this->defaults_for_count($instance, $instance['number_of_posts']) ); 
423       
424            $this->echo_input_text('title', $instance, __( 'Title: ', 'jehanne' )); ?>
425                <p>
426                <?php $this->echo_input_text('number_of_posts', $instance, __( 'Number of posts: ', 'jehanne'), 0, 2); ?>
427                </p>   
428               
429                <?php $this->echo_input_checkbox('is_show_custom_blocks', $instance, __( 'Display custom blocks', 'jehanne')); ?>
430
431                <hr>
432                <?php esc_html_e('Number of columns:', 'jehanne'); ?>
433                <select id="<?php echo $this->get_field_id('columns'); ?>" name="<?php echo esc_attr($this->get_field_name('columns')); ?>" style="width:100%;">
434                <?php 
435                        $styles_ids=array('1', '2', '4', '8');
436
437                        for ($i=0; $i<4; $i++) {
438                                echo '<option value="'.esc_attr($styles_ids[$i]).'" ';
439                                selected( $instance['columns'], $styles_ids[$i] );
440                                echo '>'.esc_html($styles_ids[$i]).'</option>';
441                        }
442                ?>
443                </select>
444                <hr>
445                <p>
446                <?php $this->echo_input_text('element_margin', $instance, __( 'Space Between Elements (%): ', 'jehanne'), 0, 2); ?>
447                </p>
448
449                <?php if($instance['is_show_custom_blocks'] == '') : ?>
450                        <p>
451                                <label for="<?php echo $this->get_field_id('cat_id'); ?>"><?php _e('Category ID:', 'jehanne'); ?></label>
452                                <?php wp_dropdown_categories('show_option_all='.__('any', 'jehanne').'&hide_empty=0&hierarchical=1&id='.$this->get_field_id('cat_id').'&name='.$this->get_field_name('cat_id').'&selected='.esc_attr($instance['cat_id'])); ?>
453                        </p>
454
455                        <p>
456                        <?php $this->echo_input_text('post_ids', $instance, __( 'Post Ids(the value for this field must be a list of integers, separated by commas):', 'jehanne'), 0, 15); ?>
457                        </p>
458                        <p>
459                        <?php $this->echo_input_text('page_ids', $instance, __( 'Page Ids(The value for this field must be a list of integers, separated by commas):', 'jehanne'), 0, 15); ?>
460                        </p>
461                        <?php $this->echo_input_checkbox('is_exclude_current_page_title', $instance, __( 'Exclude current post (Yes/No).', 'jehanne')); ?>
462                        <?php $this->echo_input_checkbox('is_use_current_category', $instance, __( 'Display recent posts from current category (Yes/No).', 'jehanne')); ?>
463
464                        <hr>
465                <?php endif; ?>
466               
467                <label for="<?php echo $this->get_field_id('style'); ?>"><?php _e('Choose how to display an Image:', 'jehanne'); ?></label>
468                <hr>
469                <select id="<?php echo $this->get_field_id('style'); ?>" name="<?php echo $this->get_field_name('style'); ?>" style="width:100%;">
470                <?php 
471                        $styles=array( __('Top', 'jehanne'), __('Right', 'jehanne'), __('Left', 'jehanne'), __('None', 'jehanne'));
472                        for ($i=0;$i<4;$i++) {
473                                echo '<option value="'.esc_attr($i).'" ';
474                                selected( $instance['style'], $i );
475                                echo '>'.esc_html($styles[$i]).'</option>';
476                        }
477                ?>
478                </select>
479                <hr>
480                <?php $this->echo_input_text('image_size', $instance, __( 'Image size (%) for images with left and right alignment: ', 'jehanne'), 0, 2); ?>
481                <?php $this->echo_input_checkbox('is_use_background_image', $instance, __( 'Display background Image(Yes/No)', 'jehanne')); ?>
482                <?php $this->echo_input_checkbox('is_image_fixed', $instance, __( 'Fixed', 'jehanne')); ?>
483                <?php $this->echo_input_checkbox('is_hide_on_small_screen', $instance, __( 'Hide on small screen(Yes/No)', 'jehanne')); ?>
484                <?php $this->echo_input_upload('background_image', $instance, __( 'Upload background Image:', 'jehanne')); ?>
485                <hr>
486                <p>
487                <?php $this->echo_input_checkbox('is_repeat_x', $instance, __( 'Repeat X', 'jehanne'), 0); ?>
488                <?php $this->echo_input_checkbox('is_repeat_y', $instance, __( 'Repeat Y', 'jehanne'), 0); ?>
489                </p>
490                <hr>
491                <?php esc_html_e('Display image on:', 'jehanne'); ?>
492                <hr>
493                <?php esc_html_e('X Position:', 'jehanne'); ?>
494                <select id="<?php echo $this->get_field_id('pos_x'); ?>" name="<?php echo $this->get_field_name('pos_x'); ?>" style="width:100%;">
495                <?php 
496                        $styles=array( __('Left', 'jehanne'), __('Right', 'jehanne'), __('Center', 'jehanne'));
497                        $styles_ids=array('left', 'right', 'center');
498
499                        for ($i=0; $i<3; $i++) {
500                                echo '<option value="'.esc_attr($styles_ids[$i]).'" ';
501                                selected( $instance['pos_x'], $styles_ids[$i] );
502                                echo '>'.esc_html($styles[$i]).'</option>';
503                        }
504                ?>
505                </select>
506                <hr>
507               
508                <?php esc_html_e('Y Position:', 'jehanne'); ?>
509                <select id="<?php echo $this->get_field_id('pos_y'); ?>" name="<?php echo $this->get_field_name('pos_y'); ?>" style="width:100%;">
510                <?php 
511                        $styles=array( __('Top', 'jehanne'), __('Bottom', 'jehanne'), __('Center', 'jehanne'));
512                        $styles_ids=array('top', 'bottom', 'center');
513
514                        for ($i=0; $i<3; $i++) {
515                                echo '<option value="'.esc_attr($styles_ids[$i]).'" ';
516                                selected( $instance['pos_y'], $styles_ids[$i] );
517                                echo '>'.esc_html($styles[$i]).'</option>';
518                        }
519                ?>
520                </select>
521                <hr>
522                <?php esc_html_e('Colors:', 'jehanne'); ?>
523                <hr>
524               
525                <?php $this->echo_input_checkbox('is_use_background_color', $instance, __( 'Set Custom Colors(Yes/No)', 'jehanne')); ?>
526                <?php $this->echo_input_color('background_color', $instance, __( 'Background:', 'jehanne'), '#ffffff'); ?>
527                <?php $this->echo_input_color('main_title_color', $instance, __( 'Widget Title:', 'jehanne'), '#000'); ?>
528                <?php $this->echo_input_color('title_color', $instance, __( 'Title:', 'jehanne'), '#118412'); ?>
529                <?php $this->echo_input_color('text_color', $instance, __( 'Text:', 'jehanne'), '#757575'); ?>
530                <?php $this->echo_input_color('link_color', $instance, __( 'Link:', 'jehanne'), '#d57b00'); ?>
531                <hr>
532               
533                <?php esc_html_e('Font size:', 'jehanne'); ?>
534               
535                <hr>
536                <p>
537                <?php $this->echo_input_text('title_size', $instance, __( 'Title:', 'jehanne'), 0, 2); ?>
538                <?php $this->echo_input_text('text_size', $instance, __( 'Text:', 'jehanne'), 0, 2); ?>
539                <?php $this->echo_input_text('link_size', $instance, __( 'Link:', 'jehanne'), 0, 2); ?>
540                </p>
541               
542                <?php if($instance['is_show_custom_blocks'] == '') : ?>
543                        <?php $this->echo_input_checkbox('is_show_title', $instance, __( 'Display the title', 'jehanne')); ?>
544                       
545                        <?php $this->echo_input_checkbox('is_show_content', $instance, __( 'Display the content', 'jehanne')); ?>
546                        <p>
547                        <?php $this->echo_input_text('content_length', $instance, __( 'Number of characters: ', 'jehanne'), 0, 4); ?>
548                        </p>
549                <?php endif; ?>
550               
551                <?php if($instance['is_show_custom_blocks'] != '') :
552                        $this->echo_input_checkbox('is_show_custom_title', $instance, __( 'Display custom title', 'jehanne'));
553                        $this->echo_input_checkbox('is_show_custom_content', $instance, __( 'Display custom content', 'jehanne'));
554                        $this->echo_input_checkbox('is_show_custom_image', $instance, __( 'Display custom Image', 'jehanne'));
555                        $this->echo_input_checkbox('is_show_custom_link', $instance, __( 'Display custom Link', 'jehanne'));
556                       
557                        for( $i = 0; $i < $instance['number_of_posts']; $i++) {
558                                ?> 
559                                <hr>
560                                <hr>
561                                <p style="font-size: 30px; color: red; ">
562                                        <?php 
563                                                esc_html_e('Block  ', 'jehanne'); 
564                                                echo ($i + 1); 
565                                        ?>
566                                </p>
567                                <hr>
568                                <hr>
569
570                                <?php 
571                                if( $instance['is_show_custom_image'])
572                                        $this->echo_input_upload_id('image_'.$i, $instance, __( 'Image: ', 'jehanne' ), 0);
573                                if( $instance['is_show_custom_title'] )
574                                        $this->echo_input_text('title_'.$i, $instance, __( 'Header: ', 'jehanne' ), 0);
575                                if( $instance['is_show_custom_content'] )
576                                        $this->echo_input_textarea('text_'.$i, $instance, __( 'Text: ', 'jehanne' ), 2);
577                                if( $instance['is_show_custom_link'] ) {
578                                        $this->echo_input_text('link_'.$i, $instance, __( 'Link: ', 'jehanne' ), 0);
579                                        echo '<br>';
580                                        $this->echo_input_text('link_caption_'.$i, $instance, __( 'Button Caption: ', 'jehanne' ), 0);
581                                }
582                        } 
583                endif; 
584                ?>
585               
586                <hr>
587               
588                <?php esc_html_e('Border:', 'jehanne'); ?>
589               
590                <hr>
591                <?php $this->echo_input_checkbox('is_use_border', $instance, __( 'Display an Image Border (Yes/No)', 'jehanne')); ?>
592                <?php $this->echo_select_border_style('border_style', $instance); ?>
593                <?php $this->echo_input_color('border_color', $instance, __( 'Border color:', 'jehanne'), '#ffffff'); ?>
594                <p>
595                <?php $this->echo_input_text('border_width', $instance, __( 'Border width:', 'jehanne'), 0, 2); ?>
596                <?php $this->echo_input_text('border_radius', $instance, __( 'Border radius', 'jehanne'), 0, 2); ?>
597                </p>
598                <hr>           
599                <?php $this->echo_input_checkbox('is_use_link_button', $instance, __( 'Display the "Read More" button (Yes/No)', 'jehanne'), '#ffffff'); ?>
600                <hr>
601                <?php $this->echo_input_color('link_more_background_color', $instance, __( 'Button background:', 'jehanne'), '#ffffff'); ?>
602                <?php $this->echo_input_color('link_more_text_color', $instance, __( 'Button text color:', 'jehanne'), '#000000'); ?>
603                <?php $this->echo_input_text('link_more_text_size', $instance, __( 'Button text size:', 'jehanne')); ?>
604                <?php $this->echo_input_text('link_more_text', $instance, __( 'Button caption text:', 'jehanne')); ?>
605                <hr>           
606                <?php $this->echo_input_checkbox('is_use_link_more_custom_link', $instance, __( 'Display a custom link (Yes/No):', 'jehanne')); ?>
607                <hr>
608                <?php $this->echo_input_text('link_more_custom_link', $instance, __( 'Button URL:', 'jehanne')); ?>
609               
610                <?php $this->echo_select_border_style('link_more_border_style', $instance); ?>
611                <?php $this->echo_input_color('link_more_border_color', $instance, __( 'Button border color:', 'jehanne'), '#000000'); ?>
612                <hr>
613                <?php $this->echo_input_text('link_more_border_width', $instance, __( 'Button border width:', 'jehanne'),1, 1); ?>
614                <?php $this->echo_input_text('link_more_border_radius', $instance, __( 'Button border radius', 'jehanne'), 1, 2); ?>
615               
616                <hr>
617               
618                <?php esc_html_e('Margins:', 'jehanne'); ?>
619               
620                <p>
621                <?php $this->echo_input_text('padding-top', $instance, __( 'Top:', 'jehanne'), 0, 2); ?>
622                <?php $this->echo_input_text('padding-right', $instance, __( 'Right:', 'jehanne'), 0, 2); ?>
623                </p>
624                <p>
625                <?php $this->echo_input_text('padding-bottom', $instance, __( 'Bottom:', 'jehanne'), 0, 2); ?>
626                <?php $this->echo_input_text('padding-left', $instance, __( 'Left:', 'jehanne'), 0, 2); ?>
627                </p>
628                <hr>
629                <hr>           
630                <?php $this->echo_input_checkbox('is_use_icon', $instance, __( 'Display an Icon (Yes/No)', 'jehanne')); ?>
631                <hr>
632                <?php $this->echo_input_upload('icon', $instance, __( 'Upload an Image for the Icon:', 'jehanne')); ?>
633                <p>
634                <?php $this->echo_input_text('icon_h', $instance, __( 'Icon Height:', 'jehanne'), 0, 2); ?>
635                <?php $this->echo_input_text('icon_w', $instance, __( 'Icon Width:', 'jehanne'), 0, 2); ?>
636                </p>
637                <hr>
638                <?php esc_html_e('Animate:', 'jehanne'); ?>
639                <hr>
640                <?php $this->echo_input_checkbox('is_animate', $instance, __( '(Yes/No).', 'jehanne')); ?>
641                <hr>
642               
643                <select id="<?php echo $this->get_field_id('animate_style'); ?>" name="<?php echo $this->get_field_name('animate_style'); ?>" style="width:100%;">
644                <?php 
645                        $styles=array( __('Step', 'jehanne'), __('All', 'jehanne'));
646                        $styles_ids=array('step', 'all');
647
648                        for ($i=0; $i<2; $i++) {
649                                echo '<option value="'.esc_attr($styles_ids[$i]).'" ';
650                                selected( $instance['animate_style'], $styles_ids[$i] );
651                                echo '>'.esc_html($styles[$i]).'</option>';
652                        }
653                ?>
654                </select>
655               
656                <hr>
657                <?php $this->echo_input_checkbox('is_slider', $instance, __( 'Slideshow(This option may override the options given in earlier fields)', 'jehanne')); ?>
658
659                <?php $this->echo_input_checkbox('text_on_slide', $instance, __('Place Text over an Image(Yes/No).', 'jehanne')); ?>
660
661               
662                <select id="<?php echo $this->get_field_id('slider_style'); ?>" name="<?php echo $this->get_field_name('slider_style'); ?>" style="width:100%;">
663                <?php 
664                        $styles=array( __('Fade In', 'jehanne'), __('Top', 'jehanne'), __('Right', 'jehanne'), __('Left', 'jehanne'));
665                        $styles_ids=array('1', '2', '3', '4');
666
667                        for ($i=0; $i<4; $i++) {
668                                echo '<option value="'.esc_attr($styles_ids[$i]).'" ';
669                                selected( $instance['slider_style'], $styles_ids[$i] );
670                                echo '>'.esc_html($styles[$i]).'</option>';
671                        }
672                ?>
673                </select>
674                <p>
675                <?php $this->echo_input_text('slide_speed', $instance, __( 'Enter the desired transition time in milliseconds:', 'jehanne'), 0, 2); ?>
676                </p>
677                <p>
678                <?php $this->echo_input_text('slide_effect_speed', $instance, __( 'Enter the desired effect time in milliseconds:', 'jehanne'), 0, 2); ?>
679                </p>
680                <?php $this->echo_input_color('background_text_color', $instance, __( 'Text Background:', 'jehanne'), '#ffffff'); ?>
681
682                <?php 
683        }
684
685        function echo_input_upload($name, $instance, $title) { ?>
686                <p>
687                        <?php if(trim($instance[$name]) != '') : ?>
688                                <img src="<?php echo esc_url(($instance[$name])); ?>" style="max-width:100%;" alt="<?php esc_attr_e('Upload', 'jehanne'); ?>" />
689                        <?php endif; ?>
690                        <br>
691            <label for="<?php echo $this->get_field_id( $name ); ?>"><?php esc_html_e( 'Url:', 'jehanne' ); ?></label>
692            <input name="<?php echo $this->get_field_name( $name ); ?>" id="<?php echo $this->get_field_id( $name ); ?>" class="widefat" type="text" size="36"  value="<?php echo esc_url( $instance[$name] ); ?>" />           
693                    <input id="<?php echo $this->get_field_id( $name ); ?>_b" class="upload_image_button button button-primary" type="button" value="<?php esc_html_e( 'Upload Image', 'jehanne'); ?>" />
694        </p>
695                <?php
696        }       
697        function echo_input_upload_id($name, $instance, $title) { ?>
698                <p>
699                        <?php echo wp_get_attachment_image($instance[$name]); ?>
700                        <br>
701                       
702            <label for="<?php echo $this->get_field_id( $name ); ?>"><?php esc_html_e( 'Url:', 'jehanne' ); ?></label>
703            <input name="<?php echo $this->get_field_name( $name ); ?>" id="<?php echo $this->get_field_id( $name ); ?>" class="widefat" type="text" size="36"  value="<?php echo esc_attr($instance[$name]); ?>" />           
704                    <input id="<?php echo $this->get_field_id( $name ); ?>_b" class="upload_id_button button button-primary" type="button" value="<?php esc_html_e( 'Upload Image', 'jehanne'); ?>" />
705        </p>
706                <?php
707        }
708        function echo_input_text($name, $instance, $title, $isp = 1, $size = 20) { ?>
709                <?php echo ($isp ? '<p>' : '');?>
710                        <label for="<?php echo $this->get_field_id( $name );?>"><?php echo esc_html($title); ?></label>
711                        <input size="<?php echo $size;?>" type="text" name="<?php echo $this->get_field_name( $name ) ?>" id="<?php echo $this->get_field_id( $name ); ?>" value="<?php echo esc_html($instance[$name]); ?>" />         
712                <?php echo($isp ? '</p>' : '');?>
713                <?php
714        }       
715        function echo_input_textarea($name, $instance, $title, $rows=10, $cols=30) { ?>
716                <p>
717                        <label for="<?php echo $this->get_field_id( $name ); ?>"><?php echo esc_html($title); ?></label>
718                        <br>
719                        <textarea name="<?php echo $this->get_field_name( $name ) ?>" cols="<?php echo $cols;?>" rows="<?php echo $rows;?>" id="<?php echo $this->get_field_id( $name ); ?>"><?php echo esc_textarea($instance[$name]); ?></textarea>           
720                </p>
721                <?php
722        }       
723        function echo_input_checkbox($name, $instance, $title, $isp = 1) { ?>
724                <?php echo ($isp ? '<p>' : '');?>
725                        <input type="checkbox" name="<?php echo $this->get_field_name( $name ); ?>" id="<?php echo $this->get_field_id( $name ); ?>"  value="1" <?php checked( $instance[$name], '1'); ?> />
726                        <label for="<?php echo $this->get_field_id( $name ); ?>"><?php echo esc_html($title); ?></label>
727                <?php echo($isp ? '</p>' : '');?>
728                <?php
729        }
730        function echo_input_color($name, $instance, $title, $def_color) { ?>
731                <p>
732                        <label for="<?php echo $this->get_field_id( $name ); ?>"><?php echo esc_html($title); ?></label>
733                        <br>
734                        <input type="text" name="<?php echo $this->get_field_name( $name );?>" id="<?php echo $this->get_field_id( $name ); ?>" value="<?php echo esc_attr($instance[$name]); ?>" class="color-picker" data-default-color="<?php echo esc_attr($def_color); ?>" />             
735                </p>
736                <?php
737        }       
738        function echo_select_border_style($name, $instance) { ?>
739                        <label for="<?php echo $this->get_field_id($name);?>"><?php esc_html_e('Border style:', 'jehanne'); ?></label>
740                        <hr>
741                        <select id="<?php echo $this->get_field_id($name);?>" name="<?php echo $this->get_field_name($name);?>" style="width:100%;">
742                        <?php 
743                                $border_styles=array('dotted', 'dashed', 'solid', 'double', 'groove', 'ridge', 'inset', 'outset');
744                                for ($i=0; $i<8; $i++) {
745                                        echo '<option value="'.esc_attr($border_styles[$i]).'" ';
746                                        selected( $instance[$name], $border_styles[$i] );
747                                        echo '>'.esc_html($border_styles[$i]).'</option>';
748                                }
749                        ?>
750                        </select>
751        <?php
752        }
753       
754        /**
755         * Return array Defaults
756         *
757         * @since jehanne 1.0.2
758         */
759        function defaults( $instance ){
760       
761                // Set up some default widget settings.
762                $defaults = array('title'=>'',
763                                                'is_show_custom_blocks' => '',
764                                                'number_of_posts' => '4',
765                                                'columns' => '4',
766                                                'cat_id' => '',
767                                                'post_ids' => '',
768                                                'page_ids' => '',
769                                                'style' => '0',
770                                                'is_exclude_current_page_title' => '',
771                                                'is_use_current_category' => '',
772                                                'image_size' => '50',
773                                                'is_use_background_color' => '',
774                                                'is_use_background_image' => '',
775                                                'background_color' => '#ffffff',
776                                                'background_text_color' => '#fff',
777                                                'background_image' => '',
778                                                'is_image_fixed' => '',
779                                                'is_repeat_x' => '',
780                                                'is_repeat_y' => '',
781                                                'pos_x' => 'left',
782                                                'pos_y' => 'top',
783                                                'main_title_color' => '#1e73be',
784                                                'title_color' => '#1e73be',
785                                                'text_color' => '#757575',
786                                                'link_color' => '#1e73be',
787                                                'title_size' => '28',
788                                                'text_size' => '18',
789                                                'link_size' => '20',
790                                                'is_show_title' => ($instance == null ? 1 : ''),
791                                                'is_show_content' => ($instance == null ? 1 :''),
792                                                'content_length' => '100', 
793                                                'is_show_custom_title' => ($instance == null ? 1 : ''),
794                                                'is_show_custom_content' => ($instance == null ? 1 : ''),
795                                                'is_show_custom_link' => ($instance == null ? 1 : ''),
796                                                'is_show_custom_image' => ($instance == null ? 1 : ''),
797                                                'custom_title' => '',
798                                                'custom_content' => '',
799                                                'is_use_border' => '',
800                                                'border_color' => '#ffffff',
801                                                'border_style' => 'double',
802                                                'border_width' => '3',
803                                                'border_radius' => '20',
804                                                'is_use_link_button' => '',
805                                                'link_more_background_color' => '#3333cc',
806                                                'link_more_text_color' => '#ffffff',
807                                                'link_more_text_size' => '20',
808                                                'link_more_text' => __( 'Read more...', 'jehanne'),
809                                                'is_use_link_more_custom_link' => '',
810                                                'link_more_custom_link' => '#',
811                                                'link_more_border_style' => 'solid',
812                                                'link_more_border_color' => '#000000',
813                                                'link_more_border_width' => '1',
814                                                'link_more_border_radius' => '0',
815                                                'is_use_icon' => '',
816                                                'padding-top' => '10',
817                                                'padding-right' => '10',
818                                                'padding-bottom' => '0',
819                                                'padding-left' => '10',
820                                                'icon_h' => '20',
821                                                'icon_w' => '20',
822                                                'icon' => '',
823                                                'is_animate' => '',
824                                                'animate_style' => 'step',
825                                                'element_margin' => '4',
826                                                'is_hide_on_small_screen' => '',
827                                                'is_slider' => '',
828                                                'slider_style' => '3',
829                                                'text_on_slide' => ($instance == null ? 1 : 0),
830                                                'slide_speed' => 8000,
831                                                'slide_effect_speed' => 1000,
832                                                'is_use_image_url' => '',
833                                                );
834               
835                return $defaults;
836        }
837       
838        /**
839         * Return array Defaults
840         *
841         * @param int $count count of custom fields
842         * @since jehanne 1.0.2
843         */
844        function defaults_for_count( $instance, $count ){
845       
846                $defaults = array();
847                for( $i = 0; $i < $count; $i++ ) {
848                        $defaults['title_'.$i] = __('Title', 'jehanne'); 
849                        $defaults['text_'.$i] = __('Description', 'jehanne'); 
850                        $defaults['link_'.$i] = ''; 
851                        $defaults['link_caption_'.$i] = __('Read More...', 'jehanne');
852                        $defaults['image_'.$i] = ''; 
853                        $defaults['image_url_'.$i] = ''; 
854                }
855               
856                return $defaults;
857        }
858       
859        /* Sanitize ID list.
860         * @param string ids.
861         * @return string ids or ''.
862         */
863        function sanitizeIDs( $ids ) {
864                if (preg_match ('|^(([0-9]+[,]?[ ]?)+)$|', $ids))
865                        return $ids;
866                else
867                        return '';     
868        }
869        /* Sanitize hex color.
870         * @param string color.
871         * @return string color.
872         */
873        function sanitize_hex_color( $color ) {
874                if ( '' === $color )
875                        return '';
876
877                // 3 or 6 hex digits, or the empty string.
878                if ( preg_match('|^#([A-Fa-f0-9]{3}){1,2}$|', $color ) )
879                        return $color;
880
881                return null;
882        }
883        /* Return string or 'Link' string  */
884        function notEmpty( $str, $str2='Link' ) {
885                if( '' === $str ) $str = $str2; 
886                return $str;
887        }
888        /* width of widget column in %
889         * @param int $sidebar_id $sidebar id.
890         * @param int $columns number of $columns.
891         * @param int $i1 widget left margin.
892         * @param int $i2 widget right margin.
893         * @return int width.
894         */
895        function getWidthRate( $sidebar_id, $columns = 1, $i1 = 0, $i2 = 0, $rate = 4 ) {       
896                $columns = ( $columns > 0 ? $columns : 1);
897                $width = ( 100 - $columns*$rate + $rate)/$columns;
898                return $width;
899        }
900        /* width of widget column for smaller resolution
901         * @param int $sidebar_id $sidebar id.
902         * @param int $columns number of $columns.
903         * @param int $i1 widget left margin.
904         * @param int $i2 widget right margin.
905         * @return int width.
906         */
907        function getWidthRateSmall( $sidebar_id, $columns = 1, $i1 = 0, $i2 = 0, $rate = 4 ) { 
908                $columns = ( $columns > 0 ? $columns : 1 );
909                if( !$this->isConstantSidebar($sidebar_id) ) {
910                        $newcolumns = ( $columns > 1 ? $columns/2 : 1 );
911                }
912                else {
913                        $newcolumns = $columns;
914                }
915               
916                $width = ( 100 - $newcolumns*$rate + $rate)/$newcolumns;
917                return $width;         
918        }
919        /* width of widget column for smallest resolution
920         * @param int $sidebar_id $sidebar id.
921         * @param int $columns number of $columns.
922         * @param int $i1 widget left margin.
923         * @param int $i2 widget right margin.
924         * @return int width.
925         */
926        function getWidthRateSuperSmall( $sidebar_id, $columns = 1, $i1 = 0, $i2 = 0, $rate = 4 ) {     
927                $columns = ( $columns > 0 ? $columns : 1 );
928                if( !$this->isConstantSidebar($sidebar_id) ) {
929                        $newcolumns = ( $columns == 8 ? 2 : 1 );
930                } 
931                else {
932                        $newcolumns = $columns;
933                }
934                               
935                $width = ( 100 - $newcolumns*$rate + $rate)/$newcolumns;
936
937                return $width;         
938        }
939        /* widget column width
940         * @param int $sidebar_id $sidebar id.
941         * @param int $columns number of $columns.
942         * @param int $i1 widget left margin.
943         * @param int $i2 widget right margin.
944         * @return int width.
945         */
946        function getWidth( $sidebar_id, $columns, $i1 = 0, $i2 = 0 ) { 
947                $width = 300;
948                if($columns <= 0) $columns = 1;
949                switch ( $sidebar_id ) {
950                //columns
951                        case 'sidebar-1':
952                        case 'sidebar-6':
953                        case 'sidebar-12':
954                                $width = 280;
955                        break;
956                //25% sidebars
957                        case 'sidebar-2':
958                        case 'sidebar-22':
959                        case 'sidebar-23':
960                                $width = 341;
961                        break;
962                //footer sidebar
963                        case 'sidebar-3':
964                        case 'sidebar-14':
965                                $width = 350;
966                        break;
967                //100% sidebars
968                        case 'sidebar-4':
969                        case 'sidebar-7':
970                        case 'sidebar-15':
971                        case 'sidebar-10':
972                        case 'sidebar-20':
973                        case 'sidebar-40':
974                                $width = 1349;
975                        break;
976                //content sidebars
977                        case 'sidebar-5':
978                        case 'sidebar-11':
979                                $width = 987;
980                        break;
981                }
982                $width = ($width - $i1 - $i2)/$columns;
983                return $width;
984        }
985        /* image size
986         * @param int $width column width.
987         * @param int $style widget style.
988         * @param int $image_size image width.
989         * @return array image size.
990         */
991        function get_image_width( $width, $style, $image_size ) {
992                $size = array(0, 0);
993                if($style == 0) {
994                        $size[0] = $width;
995                        $size[1] = $width;
996                }
997                else {
998                        $size[0] = $size[1] = $width/100*$image_size;
999                }
1000                return $size;
1001        }
1002        /* return content excerpts
1003         * @param string $post_content content.
1004         * @param int $content_length max length.
1005         * @return string image size.
1006         */
1007        function getContent($post_content, $content_length) {
1008       
1009                $post_content = $this->truncate($post_content, $content_length, '[...]');
1010                $post_content = strip_tags($post_content, '<a><b><i>');
1011
1012                return $post_content;   
1013        }
1014        /* prevent 0px
1015         * @param array $instance array of options.
1016         * @param string $name array index.
1017         * @return Xpx or ' 0'.
1018         */
1019        function getPXCSS($instance, $name) {
1020                return (is_numeric($instance[$name]) ? ($instance[$name] > 0 ? esc_attr($instance[$name]).'px' : ' 0') : ' 0' );
1021        }
1022        /* Check if the sidebar is changing size on smaller screen or not.
1023         * @param int $sidebar_id sidebar id.
1024         * @return bool true if the sidebar is constant.
1025         */
1026        public function isConstantSidebar($sidebar_id) {
1027                $possible_values = array( 'sidebar-1',
1028                                                   'sidebar-2',
1029                                                   'sidebar-3',
1030                                                   'sidebar-14',
1031                                                   'sidebar-6',
1032                                                   'sidebar-12',
1033                                                   'sidebar-14' );
1034                return in_array( $sidebar_id,  $possible_values);
1035        }
1036        /* Generate css for widget
1037         * @param array $instance widget settings.
1038         * @param int $widget_id widget id.
1039         * @param int $sidebar_id sidebar id.
1040         * @return string widget css.
1041         */
1042        public function GetWidgetCss($instance, $widget_id, $sidebar_id) {
1043                $bc = '';
1044                $bcimg = '';
1045                $style = $instance['style'];
1046               
1047                $is_hide = ( array_key_exists('is_hide_on_small_screen', $instance) ? true : false);//hide background on small screens
1048                $element_margin = ( isset($instance['element_margin']) ? $instance['element_margin'] : 4);
1049                $instance['main_title_color'] = (array_key_exists('main_title_color', $instance) ? $instance['main_title_color'] : '#000');
1050                $width = $this->getWidthRate($sidebar_id, $instance['columns'], $instance['padding-right'], $instance['padding-left'], $element_margin);
1051                $widthSmallScreen = $this->getWidthRateSmall($sidebar_id, $instance['columns'], $instance['padding-right'], $instance['padding-left']. $element_margin);
1052                $columns = $instance['columns'];
1053                $columnssmall = $columns;
1054                if (! $this->isConstantSidebar($sidebar_id))
1055                        $columnssmall = ($columns/2 > 1 ? $columns/2 : 1 );
1056               
1057                $widthSuperSmallScreen = $this->getWidthRateSuperSmall($sidebar_id, $instance['columns'], $instance['padding-right'], $instance['padding-left'], $element_margin);
1058               
1059                $columnssupersmall = $columns;
1060                if( !$this->isConstantSidebar($sidebar_id) ) {
1061                        $columnssupersmall = ($columns == 8 ? 2 : 1 );
1062                }
1063
1064                if(array_key_exists('is_use_background_image', $instance)) {
1065                        $bcimg = 'background-image: url('.esc_url($instance['background_image']).');';
1066                       
1067                        //background repeat style
1068                        if( array_key_exists('is_repeat_x', $instance) && array_key_exists('is_repeat_y', $instance) )
1069                                $rep = 'background-repeat: repeat;';
1070                        else if(array_key_exists('is_repeat_x', $instance))
1071                                $rep = 'background-repeat: repeat-x;';                 
1072                        else if(array_key_exists('is_repeat_y', $instance))
1073                                $rep = 'background-repeat: repeat-y;';
1074                        else
1075                                $rep = 'background-repeat: no-repeat;';
1076                               
1077                        if(array_key_exists('is_image_fixed', $instance))
1078                                $rep .= 'background-attachment: fixed;';
1079
1080                        //background position
1081                        $impos = 'background-position:'.esc_attr($instance['pos_y']).' '.esc_attr($instance['pos_x']).';';
1082                        $bcimg .= $rep.$impos;
1083                }
1084                if(array_key_exists('is_use_background_color', $instance)) {
1085                        $bc = 'background-color:'.esc_attr(($instance['background_color'])).';';
1086                }
1087
1088                $margin = 'padding:'.$this->getPXCSS($instance, 'padding-top').' '.
1089                                                          $this->getPXCSS($instance, 'padding-right').' '.
1090                                                          $this->getPXCSS($instance, 'padding-bottom').' '.
1091                                                          $this->getPXCSS($instance, 'padding-left').';';
1092                $widget_style = '<style type="text/css">';
1093                if ($is_hide) {//add background with @media screen > 959px
1094                        if($bcimg != '') {
1095                                $widget_style .= '@media screen and (min-width: 959px) { #'.$widget_id.' {'.$bcimg.'}}';
1096                        }
1097                        if($bc != '') {
1098                                $widget_style .= '#'.$widget_id.' {'.$bc.'}';
1099                        }
1100                }
1101                else {
1102                        $widget_style .= '#'.$widget_id.' {'.$bcimg.$bc.'}';
1103                }
1104               
1105                $color = '#fff';//the text background with 70% opacity
1106                       
1107                if(isset($instance['background_text_color']))
1108                        $color = $instance['background_text_color'];
1109                       
1110                //widget css
1111                $widget_style .= '#'.$widget_id.' .header-list {padding-right:'.$element_margin.'%;}'.
1112                ( array_key_exists('is_use_background_color', $instance) ?
1113                                        '#'.$widget_id.' .widget-title { color:'.esc_attr($instance['main_title_color']).'}' : '').
1114                '#'.$widget_id.' .wrap-list { '.$margin.'}
1115                #'.$widget_id.' .w-head .icon {border:0;max-width:'.esc_attr($instance['icon_w']).'px}
1116                #'.$widget_id.' .wrap-list.slider-widget .footer-list { background: '.$color.'}
1117                #'.$widget_id.' .wrap-list.slider-widget .footer-list,
1118                #'.$widget_id.' .wrap-list.slider-widget .footer-list a {font-size: 0;}
1119                #'.$widget_id.' .wrap-list.slider-widget .footer-list a:first-child {font-size: '.esc_attr($instance['title_size']).'px;}
1120                #'.$widget_id.' .header-list .w-head,
1121                #'.$widget_id.' .footer-list .w-head {'
1122                        .( array_key_exists('is_use_background_color', $instance) ? 'color:'.esc_attr($instance['title_color']).';' : '')       
1123                        .'width: 100%;
1124                        font-size: '.esc_attr($instance['title_size']).'px;
1125                }
1126                #'.$widget_id.' .header-list,
1127                #'.$widget_id.' .footer-list {'
1128                        .( array_key_exists('is_use_background_color', $instance) ? 'color:'.esc_attr($instance['text_color']).';' : '')       
1129                        .'font-size: '.esc_attr($instance['text_size']).'px;
1130                }
1131                #'.$widget_id.' .header-list a,
1132                #'.$widget_id.' .footer-list a {'
1133                        .( array_key_exists('is_use_background_color', $instance) ? 'color:'.esc_attr($instance['link_color']).';' : '')       
1134                        .'font-size: '.esc_attr($instance['link_size']).'px;}
1135            @media screen and (max-width: 400px) {
1136                #'.$widget_id.' .header-list:nth-child('.$columnssupersmall.'n)
1137                {padding-right: 0%;}}
1138                @media screen and (max-width: 959px) {
1139                        #'.$widget_id.' .header-list:nth-child('.$columnssmall.'n) {
1140                                padding-right: 0%;
1141                        }
1142                }
1143                @media screen and (min-width: 960px) {
1144                        #'.$widget_id.' .header-list:nth-child('.$columns.'n) {
1145                                padding-right: 0%;
1146                        }
1147                        #'.$widget_id.' .wrap-list.slider-widget .footer-list,
1148                        #'.$widget_id.' .wrap-list.slider-widget .footer-list a {
1149                                font-size: '.esc_attr($instance['text_size']).'px;
1150                        }
1151                        #'.$widget_id.' .wrap-list.slider-widget .footer-list a:first-child {
1152                                font-size: '.esc_attr($instance['title_size']).'px;
1153                        }
1154                }';     
1155               
1156                if( array_key_exists('is_use_border', $instance) ){
1157                        $widget_style .= '@media screen and (min-width: 400px) {#'.$widget_id.' .header-list img
1158                        {border:'.esc_attr($instance['border_width']).'px '.esc_attr($instance['border_style']).' '.esc_attr($instance['border_color']).';
1159                        border-radius: '.esc_attr($instance['border_radius']).'px;}}';
1160                }
1161                if( array_key_exists('is_use_link_button', $instance) ){
1162                        $widget_style .= '#'.$widget_id.' .link-read-more {
1163                                background: '.esc_attr($instance['link_more_background_color']).'; color: '.esc_attr($instance['link_more_text_color']).';
1164                                border-radius: '.esc_attr($instance['link_more_border_radius']).'px;
1165                                border: '.esc_attr($instance['link_more_border_width']).'px '.esc_attr($instance['link_more_border_style']).' '.esc_attr($instance['link_more_border_color']).';}
1166                                #'.$widget_id.' .footer-list .link-read-more.active-button, #'.$widget_id.' .header-list .link-read-more.active-button {
1167                                background: '.esc_attr($instance['link_more_text_color']).'; color: '.esc_attr($instance['link_more_background_color']).';}
1168                                #'.$widget_id.' .footer-list .link-read-more, #'.$widget_id.' .header-list .link-read-more {text-decoration:none;font-size:'.esc_attr($instance['link_more_text_size']).'px; color: '.esc_attr($instance['link_more_text_color']).';}
1169                                #'.$widget_id.' .link-read-more:hover {background: '.esc_attr($instance['link_more_text_color']).'; color: '.esc_attr($instance['link_more_background_color']).';}';
1170                }
1171                if($style == 0 || $style == 3) {//image on top                 
1172                        $widget_style .= ' #'.$widget_id.' .header-list {width:'.esc_attr($widthSuperSmallScreen).'%; }
1173                        @media screen and (min-width: 400px) { #'.$widget_id.' .header-list {width:'.esc_attr($widthSmallScreen).'%;} }
1174                        @media screen and (min-width: 960px) { #'.$widget_id.' .header-list {width:'.esc_attr($width).'%;} }
1175                        #'.$widget_id.' .footer-list {height: auto; overflow: hidden;}';
1176                }
1177                else if($style == 1) {//image on right                         
1178                        $widget_style .= '#'.$widget_id.' .header-list img {'.$bc.'display:block;}
1179                        @media screen and (min-width: 400px) {
1180                        #'.$widget_id.' .header-list{display:inline-block;width:auto;} #'.$widget_id.' .header-list img{padding:5px;margin-left:10px;float:right; width:'.esc_attr($instance['image_size']).'%;}
1181                        #'.$widget_id.' .header-list {width:'.esc_attr($widthSuperSmallScreen).'%;}}
1182                        @media screen and (min-width: 500px) { #'.$widget_id.' .header-list {width:'.esc_attr($widthSmallScreen).'%;} }
1183                        @media screen and (min-width: 960px) { #'.$widget_id.' .header-list {width:'.esc_attr($width).'%;} }
1184                        #'.$widget_id.' .header-list .w-head {display: inline;}';
1185                }               
1186                else if($style == 2) {//image on left                           
1187                        $widget_style .= '#'.$widget_id.' .header-list img {'.$bc.'display:block;}
1188                        @media screen and (min-width: 400px) { #'.$widget_id.' .header-list{display:inline-block;width:auto;} #'.$widget_id.' .header-list img{padding:5px;margin-right:10px;float:left; width:'.esc_attr($instance['image_size']).'%;}
1189                        #'.$widget_id.' .header-list {width:'.esc_attr($widthSuperSmallScreen).'%;} }
1190                        @media screen and (min-width: 500px) { #'.$widget_id.' .header-list {width:'.esc_attr($widthSmallScreen).'%;} }
1191                        @media screen and (min-width: 960px) { #'.$widget_id.' .header-list {   width:'.esc_attr($width).'%;} }
1192                        #'.$widget_id.' .header-list .w-head {display: inline;}';
1193                }
1194                if(trim($instance['number_of_posts']) === '' || $instance['number_of_posts'] === '0') {
1195                        $widget_style .= '#'.$widget_id.'.widget { padding-bottom: 0; padding-top: 0;} #'.$widget_id.' .wrap-list {display: block;}';
1196                }
1197                $widget_style .= '</style>';   
1198                return $widget_style;
1199        }
1200        /* truncate html
1201         * @param string $html.
1202         * @param int $max_length max length.
1203         * @param string $indicator end of return string.
1204         * @return string truncated html.
1205         */
1206        function truncate($html, $max_length, $indicator = '[...]') {
1207                $output_length = 0; 
1208                $position = 0;     
1209                $tag_stack = array(); 
1210                $output = '';
1211                $truncated = false;
1212
1213                $unpaired_tags = array( 'doctype', '!doctype',
1214                'area','base','basefont','bgsound','br','col',
1215                'embed','frame','hr','img','input','link','meta',
1216                'param','sound','spacer','wbr');
1217
1218                while ($output_length < $max_length
1219                && preg_match('{</?([a-z]+)[^>]*>|&#?[a-zA-Z0-9]+;}', $html, $match, PREG_OFFSET_CAPTURE, $position)) {
1220                        list($tag, $tag_position) = $match[0];
1221
1222                        $text = mb_strcut($html, $position, $tag_position - $position);
1223                        if ($output_length + mb_strlen($text) > $max_length) {
1224                                $output .= mb_strcut($text, 0, $max_length - $output_length);
1225                                $truncated = true;
1226                                $output_length = $max_length;
1227                                break;
1228                        }
1229
1230                        $output .= $text;
1231                        $output_length += mb_strlen($text);
1232
1233                        if ($tag[0] == '&') {
1234                        $output .= $tag;
1235                        $output_length++; 
1236                        }
1237                        else {
1238                                $tag_inner = $match[1][0];
1239                                if ($tag[1] == '/') {
1240                                        $output .= $tag;
1241                                        if ( end($tag_stack) == $tag_inner ) {
1242                                                array_pop($tag_stack);
1243                                        }
1244                                }
1245                                else if ($tag[mb_strlen($tag) - 2] == '/'
1246                                        || in_array(strtolower($tag_inner),$unpaired_tags) ) {
1247                                        $output .= $tag;
1248                                        }
1249                                        else {
1250                                                $output .= $tag;
1251                                                $tag_stack[] = $tag_inner; 
1252                                        }
1253                                }               
1254                                $position = $tag_position + mb_strlen($tag);
1255                }
1256
1257                if ($output_length < $max_length && $position < mb_strlen($html)) {
1258                        $output .= mb_strcut($html, $position, $max_length - $output_length);
1259                }
1260
1261                $truncated = mb_strlen($html)-$position > $max_length - $output_length;
1262               
1263                if(mb_strlen($output)>$position) {
1264                        $last_word = mb_strrpos($output, ' ', $position);
1265                        if($last_word > 0){
1266                                $output = mb_substr($output, 0, $last_word);
1267                        }
1268                }
1269
1270                if ( $truncated )
1271                $output .= $indicator;
1272
1273                while (!empty($tag_stack))
1274                $output .= '</'.array_pop($tag_stack).'>';
1275
1276                return $output;
1277        }
1278}
1279/* Register widget*/
1280function jehanne_register_widgets() {
1281        register_widget( 'jehanne_ExtendedWidget' );
1282}
1283add_action( 'widgets_init', 'jehanne_register_widgets' );
1284/*
1285* Hook widget css to wp_head
1286*/
1287function jehanne_hook_into_head_widget_css() {
1288        $css = '';
1289        $myWidget = new jehanne_ExtendedWidget();
1290        $widgets = $myWidget->get_settings();
1291                       
1292        foreach ($widgets as $key => $instance) {
1293                $widget_id = 'jehanne_extended_widget-'.$key;
1294                $sidebar = is_active_widget( '', $widget_id, 'jehanne_extended_widget');
1295                if ( $sidebar ) {
1296                        if($widgets[$key] != null) {
1297                                $css .= $myWidget->GetWidgetCss($widgets[$key], $widget_id, $sidebar);
1298                        }
1299                }
1300        }
1301        echo $css;
1302}
1303add_action('wp_head','jehanne_hook_into_head_widget_css');
Note: See TracBrowser for help on using the repository browser.