3 class Cherry_Pinterest_Embed_Widget extends WP_Widget {
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)));
10 /** @see WP_Widget::widget */
11 public function widget($args, $instance) {
12 extract(array_merge($args , $instance));
14 $url = strtolower(esc_html($url));
15 $lang = get_bloginfo('language');
17 $output = $before_widget;
18 $output .= $title ? $before_title . $title . $after_title : '' ;
20 if($instance['error'] === '') {
21 $output .= '<div id="pin-container">
22 <script type="text/javascript">
24 jQuery(document).ready(function() {
25 var boardWidth, scaleWidth;
27 jQuery(window).resize(
29 boardWidth = jQuery("#pin-container").width();
30 scaleWidth = Math.floor(boardWidth/4.1111);
32 ).trigger(\'resize\');
36 <a data-pin-do="embedUser" href="'.$url.'"></a>
37 <script type="text/javascript" async src="//assets.pinterest.com/js/pinit.js"></script>
40 $output .= '<div style="margin:20px 0 15px;padding:10px;background:#ff9b9b">'.__('There is some errors. Please check widgets page.', CHERRY_PLUGIN_DOMAIN).'</div>';
42 $output .= $after_widget;
47 /** @see WP_Widget::update */
48 public function update( $new_instance, $old_instance ) {
50 $instance = $old_instance;
52 // keep all of the fields passed from the new instance
53 foreach ( $new_instance as $key => $value ){
54 $instance[ $key ] = strip_tags($value);
57 if ( empty( $instance['url'] ) ) {
58 $instance['error'] = __('Field is empty', CHERRY_PLUGIN_DOMAIN);
60 $instance['error'] = '';
66 /** @see WP_Widget::form */
67 public function form($instance) {
74 extract(array_merge($defaults, $instance));
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),
82 $title = esc_attr($title);
85 $output .= '<div style="margin:20px 0 15px;padding:10px;background:#ff9b9b">'.$error.'</div>';
88 foreach ($form_field_type as $key => $args) {
90 $field_id = esc_attr($this->get_field_id($key));
91 $field_name = esc_attr($this->get_field_name($key));
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'].'"' : '' ;
102 switch ($args['type']) {
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>';
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>';
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>';
120 $output .= '</select>';
123 $output .= '<br><small>'.$field_description.'</small>';