| 1 |
<?php |
| 2 |
|
| 3 |
class Thumbnail_Widget extends WP_Widget { |
| 4 |
|
| 5 |
public function __construct() { |
| 6 |
parent::__construct( |
| 7 |
'thumb_wid', |
| 8 |
'Thumbnail Widget', |
| 9 |
array( 'description' => __( 'This is a widget to display thumbnail images.', 'thumbnail-editor' ), ) |
| 10 |
); |
| 11 |
} |
| 12 |
|
| 13 |
public function widget( $args, $instance ) { |
| 14 |
echo $args['before_widget']; |
| 15 |
if ( ! empty( $instance['wid_title'] ) ) { |
| 16 |
echo $args['before_title'] . apply_filters( 'widget_title', $instance['wid_title'] ) . $args['after_title']; |
| 17 |
} |
| 18 |
if ( empty( $instance['wid_thumb_type'] ) ) { |
| 19 |
$instance[ 'wid_thumb_type' ] = 'full'; |
| 20 |
} |
| 21 |
$image_attributes = wp_get_attachment_image_src( $instance['wid_thumb'], $instance[ 'wid_thumb_type' ] ); |
| 22 |
if($image_attributes[0] || ! empty( $instance['wid_thumb_caption'] )){ |
| 23 |
echo '<div class="thumb-widget '.$args['widget_id'].'">'; |
| 24 |
if($image_attributes[0]){ |
| 25 |
echo '<div class="thumb-widget-image"><img src="'.$image_attributes[0].'" alt="'.get_post_meta( $instance['wid_thumb'], '_wp_attachment_image_alt', true ).'"></div>'; |
| 26 |
} |
| 27 |
if ( ! empty( $instance['wid_thumb_caption'] ) ) { |
| 28 |
echo '<div class="thumb-widget-image-caption">'.html_entity_decode($instance['wid_thumb_caption']).'</div>'; |
| 29 |
} |
| 30 |
echo '</div>'; |
| 31 |
} |
| 32 |
echo $args['after_widget']; |
| 33 |
} |
| 34 |
|
| 35 |
public function update( $new_instance, $old_instance ) { |
| 36 |
$instance = array(); |
| 37 |
$instance['wid_title'] = esc_html( $new_instance['wid_title'] ); |
| 38 |
$instance['wid_thumb'] = sanitize_text_field( $new_instance['wid_thumb'] ); |
| 39 |
$instance['wid_thumb_type'] = sanitize_text_field( $new_instance['wid_thumb_type'] ); |
| 40 |
$instance['wid_thumb_caption'] = esc_html( $new_instance['wid_thumb_caption'] ); |
| 41 |
return $instance; |
| 42 |
} |
| 43 |
|
| 44 |
public function form( $instance ) { |
| 45 |
$wid_title = ''; |
| 46 |
if(!empty($instance[ 'wid_title' ])){ |
| 47 |
$wid_title = esc_html($instance[ 'wid_title' ]); |
| 48 |
} |
| 49 |
if(!empty($instance[ 'wid_thumb' ])){ |
| 50 |
$wid_thumb = sanitize_text_field($instance[ 'wid_thumb' ]); |
| 51 |
} |
| 52 |
if(!empty($instance[ 'wid_thumb_type' ])){ |
| 53 |
$wid_thumb_type = sanitize_text_field($instance[ 'wid_thumb_type' ]); |
| 54 |
} |
| 55 |
if(!empty($instance[ 'wid_thumb_caption' ])){ |
| 56 |
$wid_thumb_caption = esc_html($instance[ 'wid_thumb_caption' ]); |
| 57 |
} |
| 58 |
$thumb_image = ''; |
| 59 |
if($wid_thumb){ |
| 60 |
$thumb_image .= '<img src="'.wp_get_attachment_url( $wid_thumb ).'" class="wid-thumb">'; |
| 61 |
$thumb_image .= '<p>'; |
| 62 |
$thumb_image .= '<a href="javascript:void(0);" onclick="ap_thumb_remove(\''.$this->get_field_id('selected_thumb').'\', \''.$this->get_field_id('wid_thumb').'\')">Remove</a>'; |
| 63 |
$thumb_image .= ' | '; |
| 64 |
$thumb_image .= '<a href="options-general.php?page=thumbnail_editor_setup_data&att_id='.$wid_thumb.'" title="Crop Thumbnail">'.__('Crop Thumbnail','thumbnail-editor').'</a>'; |
| 65 |
$thumb_image .= '</p>'; |
| 66 |
} |
| 67 |
?> |
| 68 |
<p><label for="<?php echo $this->get_field_id('wid_title'); ?>"><?php _e('Title','thumbnail-editor'); ?> </label> |
| 69 |
<input type="text" name="<?php echo $this->get_field_name('wid_title');?>" id="<?php echo $this->get_field_id('wid_title');?>" value="<?php echo $wid_title;?>" class="widefat"> |
| 70 |
</p> |
| 71 |
<p> |
| 72 |
<input type="hidden" name="<?php echo $this->get_field_name('wid_thumb');?>" id="<?php echo $this->get_field_id('wid_thumb');?>" value="<?php echo $wid_thumb;?>"> |
| 73 |
<a href="javascript:ap_thumb_upload('<?php echo $this->get_field_id('selected_thumb');?>', '<?php echo $this->get_field_id('wid_thumb');?>')" class="button button-text-center button-ap-large widefat"><?php _e('Upload Thumbnail','thumbnail-editor'); ?></a> |
| 74 |
</p> |
| 75 |
<div id="<?php echo $this->get_field_id('selected_thumb');?>"><?php echo $thumb_image;?></div> |
| 76 |
<p> |
| 77 |
<label for="<?php echo $this->get_field_id('wid_thumb_type'); ?>"><?php _e('Thumbnail Type','thumbnail-editor'); ?> </label> |
| 78 |
<select name="<?php echo $this->get_field_name('wid_thumb_type');?>" id="<?php echo $this->get_field_id('wid_thumb_type');?>" class="widefat"> |
| 79 |
<option value="">-</option> |
| 80 |
<?php |
| 81 |
$sizes = get_intermediate_image_sizes(); |
| 82 |
if(is_array($sizes)){ |
| 83 |
foreach($sizes as $key => $value){ |
| 84 |
if( $wid_thumb_type == $value){ |
| 85 |
echo '<option value="'.$value.'" selected>'.$value.'</option>'; |
| 86 |
} else { |
| 87 |
echo '<option value="'.$value.'">'.$value.'</option>'; |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
?> |
| 92 |
</select> |
| 93 |
</p> |
| 94 |
<p><label for="<?php echo $this->get_field_id('wid_thumb_caption'); ?>"><?php _e('Caption','thumbnail-editor'); ?> </label> |
| 95 |
<input type="text" name="<?php echo $this->get_field_name('wid_thumb_caption');?>" id="<?php echo $this->get_field_id('wid_thumb_caption');?>" value="<?php echo $wid_thumb_caption;?>" class="widefat"> |
| 96 |
</p> |
| 97 |
<?php |
| 98 |
} |
| 99 |
} |