]> _ Git - couzy.git/blob
b2eb35d52f28c6c4451ca08a74d21f10f27caa8c
[couzy.git] /
1 <?php
2 $error = array();
3         class Cherry_Twitter_Embed_Widget extends WP_Widget {
4
5                 /* constructor */
6                 public function Cherry_Twitter_Embed_Widget() {
7                         parent::__construct(false, __('Cherry - Twitter Embed', CHERRY_PLUGIN_DOMAIN), array('description' => __('Widget for Twitter 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                         $lang = get_bloginfo('language');
15
16                         $output = $before_widget;
17                         $output .= $title ? $before_title . $title . $after_title : '' ;
18                         if($limit) {
19                                 $limit_string = 'data-tweet-limit="'.$limit.'"';
20                         }
21                         if($instance['error'] === '') {
22                                 $output .= '<a class="twitter-timeline" data-screen-name="'.$userName.'" data-theme="'.$color_scheme.'" data-widget-id="'.$widgetId.'" '.$limit_string.'></a>
23                                                         <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?\'http\':\'https\';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>';
24                         } else {
25                                 $output .= '<div style="margin:20px 0 15px;padding:10px;background:#ff9b9b">'.__('There is some errors. Please check widgets page.', CHERRY_PLUGIN_DOMAIN).'</div>';
26                         }
27                         $output .= $after_widget;
28
29                         echo $output;
30                 }
31
32                 /** @see WP_Widget::update */
33                 public function update( $new_instance, $old_instance ) {
34
35                         $instance = $old_instance;
36
37                         // keep all of the fields passed from the new instance
38                         foreach ( $new_instance as $key => $value ){
39                                 $instance[ $key ] = strip_tags($value);
40                         }
41
42                         if ( empty( $instance['widgetId'] ) ) {
43                                 $instance['error'] = __('Field is empty', CHERRY_PLUGIN_DOMAIN);
44                         } else {
45                                 if ( !is_numeric ( $instance['widgetId'] ) ) {
46                                         $instance['error'] = __('Widget ID can contains only numbers', CHERRY_PLUGIN_DOMAIN);
47                                 } else {
48                                         $instance['error'] = '';
49                                 }
50                         }
51
52                         return $instance;
53                 }
54
55                 /** @see WP_Widget::form */
56                 public function form($instance) {
57                         $defaults = array(
58                                 'title' => '',
59                                 'widgetId' => '',
60                                 'userName' => '',
61                                 'color_scheme' => 'light',
62                                 'limit' => '',
63                                 'error' => ''
64                         );
65
66                         extract(array_merge($defaults, $instance));
67
68                         $form_field_type = array(
69                                 'title' => array('type' => 'text', 'class' => 'widefat', 'inline_style' => '',  'title' => __('Widget Title', CHERRY_PLUGIN_DOMAIN), 'description' => '', 'value' => $title),
70                                 'widgetId' => array('type' => 'text', 'class' => 'widefat', 'inline_style' => '', 'title' => __('Widget ID', CHERRY_PLUGIN_DOMAIN), 'description' => __('ID of your widget. First of all you need make a new one widget on page https://twitter.com/settings/widgets/new. After it copy and paste here ID of widget. You can find it after creating of widget in browser\'s URL field. It contents only numbers.', CHERRY_PLUGIN_DOMAIN), 'value' => $widgetId),
71                                 'userName' => array('type' => 'text', 'class' => 'widefat', 'inline_style' => '', 'title' => __('User Name', CHERRY_PLUGIN_DOMAIN), 'description' => __('Twitter username you need to show. If empty, then will shown twitter that you set in Twitter\'s configure page.', CHERRY_PLUGIN_DOMAIN), 'value' => $userName),
72                                 'color_scheme' => array('type' => 'select', 'class' => 'widefat', 'inline_style' => '', 'title' => __('Color Scheme', CHERRY_PLUGIN_DOMAIN), 'description' => '', 'value' => $color_scheme, 'value_options' => array('light' => __('Light', CHERRY_PLUGIN_DOMAIN), 'dark' => __('Dark', CHERRY_PLUGIN_DOMAIN)) ),
73                                 'limit' => array('type' => 'text', 'class' => 'widefat', 'inline_style' => '', 'title' => __('Tweet limit', CHERRY_PLUGIN_DOMAIN), 'description' => __('Number of tweets. Leave blank if you don\'t want to limit tweet\'s number.', CHERRY_PLUGIN_DOMAIN), 'value' => $limit),
74                         );
75                         $output = '';
76
77                         $title = esc_attr($title);
78
79                         if ($error) {
80                                 $output .= '<div style="margin:20px 0 15px;padding:10px;background:#ff9b9b">'.$error.'</div>';
81                         }
82
83                         foreach ($form_field_type as $key => $args) {
84
85                                 $field_id = esc_attr($this->get_field_id($key));
86                                 $field_name = esc_attr($this->get_field_name($key));
87
88                                 $field_class = $args['class'];
89                                 $field_title = $args['title'];
90                                 $field_description = $args['description'];
91                                 $field_value = $args['value'];
92                                 $field_options = isset($args['value_options']) ? $args['value_options'] : array() ;
93                                 $inline_style = $args['inline_style'] ? 'style="'.$args['inline_style'].'"' : '' ;
94
95                                 $output .= '<p>';
96
97                                 switch ($args['type']) {
98                                         case 'text':
99                                                 $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>';
100                                         break;
101                                         case 'checkbox':
102                                                 $checked = isset($instance[$key]) ? 'checked' : '' ;
103                                                 $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>';
104
105                                         break;
106                                         case 'select':
107                                                 $output .= '<label for="'.$field_id.'">'.$field_title.':</label>';
108                                                 $output .= '<select id="'.$field_id.'" name="'.$field_name.'" '.$inline_style.' class="'.$field_class.'">';
109                                                         if(!empty($field_options)){
110                                                                 foreach ($field_options as $key_options => $value_options) {
111                                                                         $selected = $key_options == $field_value ? ' selected' : '' ;
112                                                                         $output .= '<option value="'.$key_options.'" '.$selected.'>'.$value_options.'</option>';
113                                                                 }
114                                                         }
115                                                 $output .= '</select>';
116                                         break;
117                                 }
118                                 $output .= '<br><small>'.$field_description.'</small>';
119
120                                 $output .= '</p>';
121                         }
122                         echo $output;
123                 }
124         }
125 ?>