| 1 |
<?php |
| 2 |
/* |
| 3 |
* Ascend Image Widget for compatiblity |
| 4 |
*/ |
| 5 |
|
| 6 |
if ( ! class_exists( 'ascend_image_widget' ) ) { |
| 7 |
class ascend_image_widget extends WP_Widget{ |
| 8 |
private static $instance = 0; |
| 9 |
public function __construct() { |
| 10 |
$widget_ops = array('classname' => 'kadence_about_with_image', 'description' => __('This allows for an image and a simple about text.', 'virtue-toolkit')); |
| 11 |
parent::__construct('kadence_about_with_image', __('Ascend: Image', 'virtue-toolkit'), $widget_ops); |
| 12 |
} |
| 13 |
|
| 14 |
public function widget($args, $instance){ |
| 15 |
if ( ! isset( $args['widget_id'] ) ) { |
| 16 |
$args['widget_id'] = $this->id; |
| 17 |
} |
| 18 |
extract( $args ); |
| 19 |
if (!empty($instance['image_link_open']) && $instance['image_link_open'] == "none") { |
| 20 |
$uselink = false; |
| 21 |
$link = ''; |
| 22 |
$linktype = ''; |
| 23 |
} else if(empty($instance['image_link_open']) || $instance['image_link_open'] == "lightbox") { |
| 24 |
$uselink = true; |
| 25 |
$link = esc_url($instance['image_uri']); |
| 26 |
$linktype = 'data-rel="lightbox"'; |
| 27 |
} else if($instance['image_link_open'] == "_blank") { |
| 28 |
$uselink = true; |
| 29 |
if(!empty($instance['image_link'])) {$link = $instance['image_link'];} else {$link = esc_url($instance['image_uri']);} |
| 30 |
$linktype = 'target="_blank"'; |
| 31 |
} else if($instance['image_link_open'] == "_self") { |
| 32 |
$uselink = true; |
| 33 |
if(!empty($instance['image_link'])) {$link = $instance['image_link'];} else {$link = esc_url($instance['image_uri']);} |
| 34 |
$linktype = 'target="_self"'; |
| 35 |
} |
| 36 |
if(!empty($instance['image_id'])) { |
| 37 |
$alt = esc_attr( get_post_meta($instance['image_id'], '_wp_attachment_image_alt', true) ); |
| 38 |
} else { |
| 39 |
$alt = ''; |
| 40 |
} |
| 41 |
if(isset($instance['image_size']) && !empty($instance['image_size'])) { |
| 42 |
$size = $instance['image_size']; |
| 43 |
} else { |
| 44 |
$size = 'full'; |
| 45 |
} |
| 46 |
|
| 47 |
echo $before_widget; |
| 48 |
echo '<div class="kad_img_upload_widget kt-image-widget-'.esc_attr($args['widget_id']).'">'; |
| 49 |
if($uselink == true) { |
| 50 |
echo '<a href="'.esc_url($link).'" '.wp_kses($linktype, array('a' => array('target' => array(),'data' => array()))).'>'; |
| 51 |
} |
| 52 |
if($size == 'custom') { |
| 53 |
$img = ascend_get_image_array($instance['width'], $instance['height'], true, null, null, $instance['image_id'], true); |
| 54 |
echo '<img src="'.esc_url($img['src']).'" width="'.esc_attr($img['width']).'" height="'.esc_attr($img['height']).'" '.$img['srcset'].' class="'.esc_attr($img['class']).'" itemprop="contentUrl" alt="'.esc_attr($img['alt']).'">'; |
| 55 |
} else { |
| 56 |
echo wp_get_attachment_image( $instance['image_id'], $size ); |
| 57 |
} |
| 58 |
if($uselink == true) { |
| 59 |
echo '</a>'; |
| 60 |
} |
| 61 |
if(!empty($instance['text'])) { |
| 62 |
echo '<div class="kadence_image_widget_caption">'.$instance['text'].'</div>'; |
| 63 |
} |
| 64 |
echo '</div>'; |
| 65 |
echo $after_widget; |
| 66 |
} |
| 67 |
|
| 68 |
public function update($new_instance, $old_instance) { |
| 69 |
$instance = $old_instance; |
| 70 |
$instance['text'] = wp_filter_post_kses($new_instance['text']); |
| 71 |
$instance['alttext'] = sanitize_text_field($new_instance['alttext']); |
| 72 |
$instance['image_id'] = (int) $new_instance['image_id']; |
| 73 |
$instance['image_uri'] = esc_url_raw( $new_instance['image_uri'] ); |
| 74 |
$instance['image_link'] = esc_url_raw($new_instance['image_link']); |
| 75 |
$instance['image_link_open'] = sanitize_text_field($new_instance['image_link_open']); |
| 76 |
$instance['image_size'] = sanitize_text_field($new_instance['image_size']); |
| 77 |
$instance['width'] = (int) $new_instance['width']; |
| 78 |
$instance['height'] = (int) $new_instance['height']; |
| 79 |
return $instance; |
| 80 |
} |
| 81 |
|
| 82 |
public function form($instance){ |
| 83 |
$image_uri = isset($instance['image_uri']) ? esc_attr($instance['image_uri']) : ''; |
| 84 |
$image_link = isset($instance['image_link']) ? esc_attr($instance['image_link']) : ''; |
| 85 |
$width = isset($instance['width']) ? esc_attr($instance['width']) : ''; |
| 86 |
$height = isset($instance['height']) ? esc_attr($instance['height']) : ''; |
| 87 |
$image_id = isset($instance['image_id']) ? esc_attr($instance['image_id']) : ''; |
| 88 |
if (isset($instance['image_link_open'])) { $image_link_open = esc_attr($instance['image_link_open']); } else {$image_link_open = 'lightbox';} |
| 89 |
if (isset($instance['image_size'])) { $image_size = esc_attr($instance['image_size']); } else {$image_size = 'full';} |
| 90 |
$link_options = array(); |
| 91 |
$link_options_array = array(); |
| 92 |
$sizes = ascend_basic_image_sizes(); |
| 93 |
$link_options[] = array("slug" => "lightbox", "name" => __('Lightbox', 'virtue-toolkit')); |
| 94 |
$link_options[] = array("slug" => "_blank", "name" => __('New Window', 'virtue-toolkit')); |
| 95 |
$link_options[] = array("slug" => "_self", "name" => __('Same Window', 'virtue-toolkit')); |
| 96 |
$link_options[] = array("slug" => "none", "name" => __('No Link', 'virtue-toolkit')); |
| 97 |
|
| 98 |
foreach ($link_options as $link_option) { |
| 99 |
if ($image_link_open == $link_option['slug']) { $selected=' selected="selected"';} else { $selected=""; } |
| 100 |
$link_options_array[] = '<option value="' . esc_attr($link_option['slug']) .'"' . $selected . '>' . esc_html($link_option['name']) . '</option>'; |
| 101 |
} |
| 102 |
foreach ($sizes as $size => $size_info) { |
| 103 |
if ($image_size == $size) { $selected=' selected="selected"';} else { $selected=""; } |
| 104 |
$sizes_array[] = '<option value="' . esc_attr($size) .'"' . $selected . '>' . esc_html($size_info) .'</option>'; |
| 105 |
} |
| 106 |
?> |
| 107 |
<div class="kad_img_upload_widget"> |
| 108 |
<p> |
| 109 |
<img class="kad_custom_media_image" src="<?php if(!empty($instance['image_uri'])){echo esc_attr($instance['image_uri']);} ?>" style="margin:0;padding:0;max-width:100px;display:block" /> |
| 110 |
</p> |
| 111 |
<p> |
| 112 |
<label for="<?php echo $this->get_field_id('image_uri'); ?>"><?php _e('Image URL', 'virtue-toolkit'); ?></label><br /> |
| 113 |
<input type="text" class="widefat kad_custom_media_url" name="<?php echo $this->get_field_name('image_uri'); ?>" id="<?php echo $this->get_field_id('image_uri'); ?>" value="<?php echo esc_attr($image_uri); ?>"> |
| 114 |
<input type="hidden" value="<?php echo esc_attr($image_id); ?>" class="kad_custom_media_id" name="<?php echo $this->get_field_name('image_id'); ?>" id="<?php echo $this->get_field_id('image_id'); ?>" /> |
| 115 |
<input type="button" value="<?php esc_attr_e('Upload', 'virtue-toolkit'); ?>" class="button kad_custom_media_upload" id="kad_custom_image_uploader" /> |
| 116 |
</p> |
| 117 |
<p> |
| 118 |
<label for="<?php echo $this->get_field_id('image_size'); ?>"><?php _e('Image size', 'virtue-toolkit'); ?></label><br /> |
| 119 |
<select id="<?php echo $this->get_field_id('image_size'); ?>" name="<?php echo $this->get_field_name('image_size'); ?>"><?php echo implode('', $sizes_array);?></select> |
| 120 |
</p> |
| 121 |
<p> |
| 122 |
<label for="<?php echo $this->get_field_id('width'); ?>"><?php _e('Custom Width', 'virtue-toolkit'); ?></label><br /> |
| 123 |
<input type="text" class="widefat kad_img_widget_link" name="<?php echo $this->get_field_name('width'); ?>" id="<?php echo $this->get_field_id('width'); ?>" value="<?php echo esc_attr($width); ?>"> |
| 124 |
</p> |
| 125 |
<p> |
| 126 |
<label for="<?php echo $this->get_field_id('height'); ?>"><?php _e('Custom Height', 'virtue-toolkit'); ?></label><br /> |
| 127 |
<input type="text" class="widefat kad_img_widget_link" name="<?php echo $this->get_field_name('height'); ?>" id="<?php echo $this->get_field_id('height'); ?>" value="<?php echo esc_attr($height); ?>"> |
| 128 |
</p> |
| 129 |
<p> |
| 130 |
<label for="<?php echo $this->get_field_id('image_link_open'); ?>"><?php _e('Image opens in', 'virtue-toolkit'); ?></label><br /> |
| 131 |
<select id="<?php echo $this->get_field_id('image_link_open'); ?>" name="<?php echo $this->get_field_name('image_link_open'); ?>"><?php echo implode('', $link_options_array);?></select> |
| 132 |
</p> |
| 133 |
<p> |
| 134 |
<label for="<?php echo $this->get_field_id('image_link'); ?>"><?php _e('Image Link (optional)', 'virtue-toolkit'); ?></label><br /> |
| 135 |
<input type="text" class="widefat kad_img_widget_link" name="<?php echo $this->get_field_name('image_link'); ?>" id="<?php echo $this->get_field_id('image_link'); ?>" value="<?php echo esc_attr($image_link); ?>"> |
| 136 |
</p> |
| 137 |
<p> |
| 138 |
<label for="<?php echo $this->get_field_id('text'); ?>"><?php _e('Text/Caption (optional)', 'virtue-toolkit'); ?></label><br /> |
| 139 |
<textarea name="<?php echo $this->get_field_name('text'); ?>" id="<?php echo $this->get_field_id('text'); ?>" class="widefat" ><?php if(!empty($instance['text'])) echo esc_textarea($instance['text']); ?></textarea> |
| 140 |
</p> |
| 141 |
</div> |
| 142 |
<?php |
| 143 |
} |
| 144 |
|
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
} |