]> _ Git - couzy.git/blob
662cdae89dc7ff315772fe0200d5f1d07f7b93d2
[couzy.git] /
1 <?php
2 $error = array();
3         class Cherry_Pinterest_Embed_Widget extends WP_Widget {
4
5                 /* constructor */
6                 public function Cherry_Pinterest_Embed_Widget() {
7                         parent::__construct(false, __('Cherry - Pinterest Embed', CHERRY_PLUGIN_DOMAIN), array('description' => __('Widget for Pinterest embed content', CHERRY_PLUGIN_DOMAIN)));
8                 }
9
10                 /** @see WP_Widget::widget */
11                 public function widget($args, $instance) {
12                         extract(array_merge($args , $instance));
13
14                         $url = strtolower(esc_html($url));
15                         $lang = get_bloginfo('language');
16
17                         $output = $before_widget;
18                         $output .= $title ? $before_title . $title . $after_title : '' ;
19
20                         if($instance['error'] === '') {
21                                 $output .= '<div id="pin-container">
22                                                                 <script type="text/javascript">
23
24                                                                         jQuery(document).ready(function() {
25                                                                                 var boardWidth, scaleWidth;
26
27                                                                                 jQuery(window).resize(
28                                                                                         function(){
29                                                                                                 boardWidth = jQuery("#pin-container").width();
30                                                                                                 scaleWidth = Math.floor(boardWidth/4.1111);
31                                                                                         }
32                                                                                 ).trigger(\'resize\');
33                                                                         });
34
35                                                                 </script>
36                                                                 <a data-pin-do="embedUser" href="'.$url.'"></a>
37                                                                 <script type="text/javascript" async src="//assets.pinterest.com/js/pinit.js"></script>
38                                                         </div>';
39                         } else {
40                                 $output .= '<div style="margin:20px 0 15px;padding:10px;background:#ff9b9b">'.__('There is some errors. Please check widgets page.', CHERRY_PLUGIN_DOMAIN).'</div>';
41                         }
42                         $output .= $after_widget;
43
44                         echo $output;
45                 }
46
47                 /** @see WP_Widget::update */
48                 public function update( $new_instance, $old_instance ) {
49
50                         $instance = $old_instance;
51
52                         // keep all of the fields passed from the new instance
53                         foreach ( $new_instance as $key => $value ){
54                                 $instance[ $key ] = strip_tags($value);
55                         }
56
57                         if ( empty( $instance['url'] ) ) {
58                                 $instance['error'] = __('Field is empty', CHERRY_PLUGIN_DOMAIN);
59                         } else {
60                                 $instance['error'] = '';
61                         }
62
63                         return $instance;
64                 }
65
66                 /** @see WP_Widget::form */
67                 public function form($instance) {
68                         $defaults = array(
69                                 'title' => '',
70                                 'url' => '',
71                                 'error' => ''
72                         );
73
74                         extract(array_merge($defaults, $instance));
75
76                         $form_field_type = array(
77                                 'title' => array('type' => 'text', 'class' => 'widefat', 'inline_style' => '',  'title' => __('Widget Title', CHERRY_PLUGIN_DOMAIN), 'description' => '', 'value' => $title),
78                                 'url' => array('type' => 'text', 'class' => 'widefat', 'inline_style' => '', 'title' => __('Pinterest User URL', CHERRY_PLUGIN_DOMAIN), 'description' => __('Enter Pinterest User URL.', CHERRY_PLUGIN_DOMAIN), 'value' => $url),
79                         );
80                         $output = '';
81
82                         $title = esc_attr($title);
83
84                         if ($error) {
85                                 $output .= '<div style="margin:20px 0 15px;padding:10px;background:#ff9b9b">'.$error.'</div>';
86                         }
87
88                         foreach ($form_field_type as $key => $args) {
89
90                                 $field_id = esc_attr($this->get_field_id($key));
91                                 $field_name = esc_attr($this->get_field_name($key));
92
93                                 $field_class = $args['class'];
94                                 $field_title = $args['title'];
95                                 $field_description = $args['description'];
96                                 $field_value = $args['value'];
97                                 $field_options = isset($args['value_options']) ? $args['value_options'] : array() ;
98                                 $inline_style = $args['inline_style'] ? 'style="'.$args['inline_style'].'"' : '' ;
99
100                                 $output .= '<p>';
101
102                                 switch ($args['type']) {
103                                         case 'text':
104                                                 $output .= '<label for="'.$field_id.'">'.$field_title.': <input '.$inline_style.' class="'.$field_class.'" id="'.$field_id.'" name="'.$field_name.'" type="text" value="'.esc_attr($field_value).'"/></label>';
105                                         break;
106                                         case 'checkbox':
107                                                 $checked = isset($instance[$key]) ? 'checked' : '' ;
108                                                 $output .= '<label for="'.$field_id.'"><input value="on" '.$inline_style.' class="'.$field_class.'" id="'.$field_id.'" name="'.$field_name.'" type="checkbox" '.$checked.' />'.$field_title.'</label>';
109
110                                         break;
111                                         case 'select':
112                                                 $output .= '<label for="'.$field_id.'">'.$field_title.':</label>';
113                                                 $output .= '<select id="'.$field_id.'" name="'.$field_name.'" '.$inline_style.' class="'.$field_class.'">';
114                                                         if(!empty($field_options)){
115                                                                 foreach ($field_options as $key_options => $value_options) {
116                                                                         $selected = $key_options == $field_value ? ' selected' : '' ;
117                                                                         $output .= '<option value="'.$key_options.'" '.$selected.'>'.$value_options.'</option>';
118                                                                 }
119                                                         }
120                                                 $output .= '</select>';
121                                         break;
122                                 }
123                                 $output .= '<br><small>'.$field_description.'</small>';
124
125                                 $output .= '</p>';
126                         }
127                         echo $output;
128                 }
129         }
130 ?>