1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
<?php
/**
* Module Name: Image Widget
* Module Description: Easily add images to your theme's sidebar.
* Sort Order: 20
* First Introduced: 1.2
*/
class Jetpack_Image_Widget extends WP_Widget {
function Jetpack_Image_Widget() {
$widget_ops = array( 'classname' => 'widget_image', 'description' => __( "Display an image in your sidebar", 'jetpack' ) );
$control_ops = array( 'width' => 400 );
$this->WP_Widget( 'image', __( 'Image (Jetpack)', 'jetpack' ), $widget_ops, $control_ops );
}
function widget( $args, $instance ) {
extract( $args );
echo $before_widget;
$title = apply_filters( 'widget_title', $instance['title'] );
if ( $title )
echo $before_title . esc_html( $title ) . $after_title;
if ( '' != $instance['img_url'] ) {
$output = '<img src="' . esc_attr( $instance['img_url'] ) .'" ';
if ( '' != $instance['alt_text'] )
$output .= 'alt="' . esc_attr( $instance['alt_text'] ) .'" ';
if ( '' != $instance['img_title'] )
$output .= 'title="' . esc_attr( $instance['img_title'] ) .'" ';
if ( '' == $instance['caption'] )
$output .= 'class="align' . esc_attr( $instance['align'] ) . '" ';
if ( '' != $instance['img_width'] )
$output .= 'width="' . esc_attr( $instance['img_width'] ) .'" ';
if ( '' != $instance['img_height'] )
$output .= 'height="' . esc_attr( $instance['img_height'] ) .'" ';
$output .= '/>';
if ( '' != $instance['link'] )
$output = '<a href="' . esc_attr( $instance['link'] ) . '">' . $output . '</a>';
if ( '' != $instance['caption'] )
$output = '[caption align="align' . esc_attr( $instance['align'] ) . '" width="' . esc_attr( $instance['img_width'] ) .'" caption="' . esc_attr( $instance['caption'] ) . '"]' . $output . '[/caption]';
echo '<div class="jetpack-image-container">' . do_shortcode( $output ) . '</div>';
}
echo "\n" . $after_widget;
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['img_url'] = esc_url( $new_instance['img_url'], null, 'display' );
$instance['alt_text'] = strip_tags( $new_instance['alt_text'] );
$instance['img_title'] = strip_tags( $new_instance['img_title'] );
$instance['caption'] = strip_tags( $new_instance['caption'] );
$instance['align'] = $new_instance['align'];
$instance['img_width'] = absint( $new_instance['img_width'] );
$instance['img_height'] = absint( $new_instance['img_height'] );
$instance['link'] = esc_url( $new_instance['link'], null, 'display' );
return $instance;
}
function form( $instance ) {
// Defaults
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'img_url' => '', 'alt_text' => '', 'img_title' => '', 'caption' => '', 'align' => 'none', 'img_width' => '', 'img_height' => '', 'link' => '' ) );
$title = esc_attr( $instance['title'] );
$img_url = esc_url( $instance['img_url'], null, 'display' );
$alt_text = esc_attr( $instance['alt_text'] );
$img_title = esc_attr( $instance['img_title'] );
$caption = esc_attr( $instance['caption'] );
$align = esc_attr( $instance['align'] );
$img_width = esc_attr( $instance['img_width'] );
$img_height = esc_attr( $instance['img_height'] );
if ( !empty( $instance['img_url'] ) ) {
// Download the url to a local temp file and then process it with getimagesize so we can filter out domains which are blocking us
$tmp_file = download_url( $instance['img_url'], 30 );
if ( ! is_wp_error( $tmp_file ) ) {
$size = getimagesize( $tmp_file );
if ( '' == $instance['img_width'] ) {
$width = $size[0];
$img_width = $width;
} else {
$img_width = absint( $instance['img_width'] );
}
if ( '' == $instance['img_height'] ) {
$height = $size[1];
$img_height = $height;
} else {
$img_height = absint( $instance['img_height'] );
}
unlink( $tmp_file );
}
}
$link = esc_url( $instance['link'], null, 'display' );
echo '<p><label for="' . $this->get_field_id( 'title' ) . '">' . esc_html__( 'Widget title:', 'jetpack' ) . '
<input class="widefat" id="' . $this->get_field_id( 'title' ) . '" name="' . $this->get_field_name( 'title' ) . '" type="text" value="' . $title . '" />
</label></p>
<p><label for="' . $this->get_field_id( 'img_url' ) . '">' . esc_html__( 'Image URL:', 'jetpack' ) . '
<input class="widefat" id="' . $this->get_field_id( 'img_url' ) . '" name="' . $this->get_field_name( 'img_url' ) . '" type="text" value="' . $img_url . '" />
</label></p>
<p><label for="' . $this->get_field_id( 'alt_text' ) . '">' . esc_html__( 'Alternate text:', 'jetpack' ) . ' <a href="http://support.wordpress.com/widgets/image-widget/#image-widget-alt-text" target="_blank">( ? )</a>
<input class="widefat" id="' . $this->get_field_id( 'alt_text' ) . '" name="' . $this->get_field_name( 'alt_text' ) . '" type="text" value="' . $alt_text . '" />
</label></p>
<p><label for="' . $this->get_field_id( 'img_title' ) . '">' . esc_html__( 'Image title:', 'jetpack' ) . ' <a href="http://support.wordpress.com/widgets/image-widget/#image-widget-title" target="_blank">( ? )</a>
<input class="widefat" id="' . $this->get_field_id( 'img_title' ) . '" name="' . $this->get_field_name( 'img_title' ) . '" type="text" value="' . $img_title . '" />
</label></p>
<p><label for="' . $this->get_field_id( 'caption' ) . '">' . esc_html__( 'Caption:', 'jetpack' ) . ' <a href="http://support.wordpress.com/widgets/image-widget/#image-widget-caption" target="_blank">( ? )</a>
<input class="widefat" id="' . $this->get_field_id( 'caption' ) . '" name="' . $this->get_field_name( 'caption' ) . '" type="text" value="' . $caption . '" />
</label></p>';
$alignments = array(
'none' => __( 'None', 'jetpack' ),
'left' => __( 'Left', 'jetpack' ),
'center' => __( 'Center', 'jetpack' ),
'right' => __( 'Right', 'jetpack' ),
);
echo '<p><label for="' . $this->get_field_id( 'align' ) . '">' . esc_html__( 'Image Alignment:', 'jetpack' ) . '
<select id="' . $this->get_field_id( 'align' ) . '" name="' . $this->get_field_name( 'align' ) . '">';
foreach ( $alignments as $alignment => $alignment_name ) {
echo '<option value="' . esc_attr( $alignment ) . '" ';
if ( $alignment == $align )
echo 'selected="selected" ';
echo '>' . esc_html($alignment_name) . "</option>\n";
}
echo '</select></label></p>';
echo '<p><label for="' . $this->get_field_id( 'img_width' ) . '">' . esc_html__( 'Width:', 'jetpack' ) . '
<input size="3" id="' . $this->get_field_id( 'img_width' ) . '" name="' . $this->get_field_name( 'img_width' ) . '" type="text" value="' . $img_width . '" />
</label>
<label for="' . $this->get_field_id( 'img_height' ) . '">' . esc_html__( 'Height:', 'jetpack' ) . '
<input size="3" id="' . $this->get_field_id( 'img_height' ) . '" name="' . $this->get_field_name( 'img_height' ) . '" type="text" value="' . $img_height . '" />
</label><br />
<small>' . esc_html__( "If empty, we will attempt to determine the image size.", 'jetpack' ) . '</small></p>
<p><label for="' . $this->get_field_id( 'link' ) . '">' . esc_html__( 'Link URL (when the image is clicked):', 'jetpack' ) . '
<input class="widefat" id="' . $this->get_field_id( 'link' ) . '" name="' . $this->get_field_name( 'link' ) . '" type="text" value="' . $link . '" />
</label></p>';
}
} //Class Jetpack_Image_Widget
function jetpack_image_widget_init() {
register_widget( 'Jetpack_Image_Widget' );
}
add_action( 'widgets_init', 'jetpack_image_widget_init' );
|