Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/jetpack/modules/widgets
File: image-widget.php
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
[0] Fix | Delete
/**
[1] Fix | Delete
* Module Name: Image Widget
[2] Fix | Delete
* Module Description: Easily add images to your theme's sidebar.
[3] Fix | Delete
* Sort Order: 20
[4] Fix | Delete
* First Introduced: 1.2
[5] Fix | Delete
*/
[6] Fix | Delete
[7] Fix | Delete
if ( ! defined( 'ABSPATH' ) ) {
[8] Fix | Delete
exit( 0 );
[9] Fix | Delete
}
[10] Fix | Delete
[11] Fix | Delete
// phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files.
[12] Fix | Delete
[13] Fix | Delete
add_action( 'widgets_init', 'jetpack_image_widget_init', 11 );
[14] Fix | Delete
/**
[15] Fix | Delete
* Register the widget for use in Appearance -> Widgets
[16] Fix | Delete
*/
[17] Fix | Delete
function jetpack_image_widget_init() {
[18] Fix | Delete
if ( class_exists( 'WP_Widget_Media_Image' ) && Jetpack_Options::get_option( 'image_widget_migration' ) ) {
[19] Fix | Delete
return;
[20] Fix | Delete
}
[21] Fix | Delete
register_widget( 'Jetpack_Image_Widget' );
[22] Fix | Delete
}
[23] Fix | Delete
[24] Fix | Delete
/**
[25] Fix | Delete
* Jetpack_Image_Widget main class.
[26] Fix | Delete
*/
[27] Fix | Delete
class Jetpack_Image_Widget extends WP_Widget {
[28] Fix | Delete
/**
[29] Fix | Delete
* Register widget with WordPress.
[30] Fix | Delete
*/
[31] Fix | Delete
public function __construct() {
[32] Fix | Delete
parent::__construct(
[33] Fix | Delete
'image',
[34] Fix | Delete
/** This filter is documented in modules/widgets/facebook-likebox.php */
[35] Fix | Delete
apply_filters( 'jetpack_widget_name', esc_html__( 'Image', 'jetpack' ) ),
[36] Fix | Delete
array(
[37] Fix | Delete
'classname' => 'widget_image',
[38] Fix | Delete
'description' => __( 'Display an image in your sidebar', 'jetpack' ),
[39] Fix | Delete
'customize_selective_refresh' => true,
[40] Fix | Delete
)
[41] Fix | Delete
);
[42] Fix | Delete
}
[43] Fix | Delete
[44] Fix | Delete
/**
[45] Fix | Delete
* Loads file for front-end widget style.
[46] Fix | Delete
*
[47] Fix | Delete
* @uses wp_enqueue_style(), plugins_url()
[48] Fix | Delete
*/
[49] Fix | Delete
public function enqueue_style() {
[50] Fix | Delete
wp_enqueue_style( 'jetpack_image_widget', plugins_url( 'image-widget/style.css', __FILE__ ), array(), '20140808' );
[51] Fix | Delete
}
[52] Fix | Delete
[53] Fix | Delete
/**
[54] Fix | Delete
* Front-end display of widget.
[55] Fix | Delete
*
[56] Fix | Delete
* @see WP_Widget::widget()
[57] Fix | Delete
*
[58] Fix | Delete
* @param array $args Widget arguments.
[59] Fix | Delete
* @param array $instance Saved values from database.
[60] Fix | Delete
*/
[61] Fix | Delete
public function widget( $args, $instance ) {
[62] Fix | Delete
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
[63] Fix | Delete
[64] Fix | Delete
$instance = wp_parse_args(
[65] Fix | Delete
$instance,
[66] Fix | Delete
array(
[67] Fix | Delete
'title' => '',
[68] Fix | Delete
'img_url' => '',
[69] Fix | Delete
)
[70] Fix | Delete
);
[71] Fix | Delete
[72] Fix | Delete
/** This filter is documented in core/src/wp-includes/default-widgets.php */
[73] Fix | Delete
$title = apply_filters( 'widget_title', $instance['title'] );
[74] Fix | Delete
if ( $title ) {
[75] Fix | Delete
echo $args['before_title'] . $title . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
[76] Fix | Delete
}
[77] Fix | Delete
[78] Fix | Delete
if ( $instance['img_url'] ) {
[79] Fix | Delete
[80] Fix | Delete
// Enqueue front end assets.
[81] Fix | Delete
$this->enqueue_style();
[82] Fix | Delete
[83] Fix | Delete
$output = '<img src="' . esc_url( $instance['img_url'] ) . '" ';
[84] Fix | Delete
[85] Fix | Delete
if ( '' !== (string) $instance['alt_text'] ) {
[86] Fix | Delete
$output .= 'alt="' . esc_attr( $instance['alt_text'] ) . '" ';
[87] Fix | Delete
}
[88] Fix | Delete
if ( '' !== (string) $instance['img_title'] ) {
[89] Fix | Delete
$output .= 'title="' . esc_attr( $instance['img_title'] ) . '" ';
[90] Fix | Delete
}
[91] Fix | Delete
if ( '' !== (string) $instance['caption'] ) {
[92] Fix | Delete
$output .= 'class="align' . esc_attr( $instance['align'] ) . '" ';
[93] Fix | Delete
}
[94] Fix | Delete
if ( '' !== (string) $instance['img_width'] ) {
[95] Fix | Delete
$output .= 'width="' . esc_attr( $instance['img_width'] ) . '" ';
[96] Fix | Delete
}
[97] Fix | Delete
if ( '' !== (string) $instance['img_height'] ) {
[98] Fix | Delete
$output .= 'height="' . esc_attr( $instance['img_height'] ) . '" ';
[99] Fix | Delete
}
[100] Fix | Delete
$output .= '/>';
[101] Fix | Delete
[102] Fix | Delete
$output = apply_filters( 'jetpack_image_cdn_content', $output );
[103] Fix | Delete
[104] Fix | Delete
if ( $instance['link'] ) {
[105] Fix | Delete
$target = ! empty( $instance['link_target_blank'] )
[106] Fix | Delete
? 'target="_blank"'
[107] Fix | Delete
: '';
[108] Fix | Delete
$output = '<a ' . $target . ' href="' . esc_url( $instance['link'] ) . '">' . $output . '</a>';
[109] Fix | Delete
}
[110] Fix | Delete
if ( '' !== (string) $instance['caption'] ) {
[111] Fix | Delete
/** This filter is documented in core/src/wp-includes/default-widgets.php */
[112] Fix | Delete
$caption = apply_filters( 'widget_text', $instance['caption'] );
[113] Fix | Delete
$img_width = ( ! empty( $instance['img_width'] ) ? 'style="width: ' . esc_attr( $instance['img_width'] ) . 'px"' : '' );
[114] Fix | Delete
$output = '<figure ' . $img_width . ' class="wp-caption align' . esc_attr( $instance['align'] ) . '">
[115] Fix | Delete
' . $output . '
[116] Fix | Delete
<figcaption class="wp-caption-text">' . $caption . '</figcaption>
[117] Fix | Delete
</figure>'; // wp_kses_post caption on update.
[118] Fix | Delete
}
[119] Fix | Delete
echo '<div class="jetpack-image-container">' . do_shortcode( $output ) . '</div>';
[120] Fix | Delete
} elseif ( current_user_can( 'edit_theme_options' ) ) {
[121] Fix | Delete
echo '<p>' . wp_kses(
[122] Fix | Delete
sprintf(
[123] Fix | Delete
/* translators: %s link to the widget settings page. */
[124] Fix | Delete
__( 'Image missing or invalid URL. Please check the Image widget URL in your <a href="%s">widget settings</a>.', 'jetpack' ),
[125] Fix | Delete
admin_url( 'widgets.php' )
[126] Fix | Delete
),
[127] Fix | Delete
array(
[128] Fix | Delete
'a' => array(
[129] Fix | Delete
'href' => array(),
[130] Fix | Delete
),
[131] Fix | Delete
)
[132] Fix | Delete
) . '</p>';
[133] Fix | Delete
}
[134] Fix | Delete
[135] Fix | Delete
echo "\n" . $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
[136] Fix | Delete
[137] Fix | Delete
/** This action is documented in modules/widgets/gravatar-profile.php */
[138] Fix | Delete
do_action( 'jetpack_stats_extra', 'widget_view', 'image' );
[139] Fix | Delete
}
[140] Fix | Delete
[141] Fix | Delete
/**
[142] Fix | Delete
* Sanitize widget form values as they are saved.
[143] Fix | Delete
*
[144] Fix | Delete
* @see WP_Widget::update()
[145] Fix | Delete
*
[146] Fix | Delete
* @param array $new_instance Values just sent to be saved.
[147] Fix | Delete
* @param array $old_instance Previously saved values from database.
[148] Fix | Delete
*
[149] Fix | Delete
* @return array Updated safe values to be saved.
[150] Fix | Delete
*/
[151] Fix | Delete
public function update( $new_instance, $old_instance ) {
[152] Fix | Delete
$allowed_caption_html = array(
[153] Fix | Delete
'a' => array(
[154] Fix | Delete
'href' => array(),
[155] Fix | Delete
'title' => array(),
[156] Fix | Delete
),
[157] Fix | Delete
'b' => array(),
[158] Fix | Delete
'em' => array(),
[159] Fix | Delete
'i' => array(),
[160] Fix | Delete
'p' => array(),
[161] Fix | Delete
'strong' => array(),
[162] Fix | Delete
);
[163] Fix | Delete
[164] Fix | Delete
$instance = $old_instance;
[165] Fix | Delete
[166] Fix | Delete
$instance['title'] = wp_strip_all_tags( $new_instance['title'] );
[167] Fix | Delete
$instance['img_url'] = esc_url( trim( $new_instance['img_url'] ) );
[168] Fix | Delete
$instance['alt_text'] = wp_strip_all_tags( $new_instance['alt_text'] );
[169] Fix | Delete
$instance['img_title'] = wp_strip_all_tags( $new_instance['img_title'] );
[170] Fix | Delete
$instance['caption'] = wp_kses( stripslashes( $new_instance['caption'] ), $allowed_caption_html );
[171] Fix | Delete
$instance['align'] = $new_instance['align'];
[172] Fix | Delete
$instance['link'] = esc_url( trim( $new_instance['link'] ) );
[173] Fix | Delete
$instance['link_target_blank'] = isset( $new_instance['link_target_blank'] ) ? (bool) $new_instance['link_target_blank'] : false;
[174] Fix | Delete
[175] Fix | Delete
$new_img_width = absint( $new_instance['img_width'] );
[176] Fix | Delete
$new_img_height = absint( $new_instance['img_height'] );
[177] Fix | Delete
[178] Fix | Delete
if ( ! empty( $instance['img_url'] ) && 0 === $new_img_width && 0 === $new_img_height ) {
[179] Fix | Delete
// Download the url to a local temp file and then process it with getimagesize so we can optimize browser layout.
[180] Fix | Delete
$tmp_file = download_url( $instance['img_url'], 10 );
[181] Fix | Delete
if ( ! is_wp_error( $tmp_file ) ) {
[182] Fix | Delete
$size = getimagesize( $tmp_file );
[183] Fix | Delete
[184] Fix | Delete
$width = $size[0];
[185] Fix | Delete
$instance['img_width'] = absint( $width );
[186] Fix | Delete
[187] Fix | Delete
$height = $size[1];
[188] Fix | Delete
$instance['img_height'] = absint( $height );
[189] Fix | Delete
[190] Fix | Delete
wp_delete_file( $tmp_file );
[191] Fix | Delete
} else {
[192] Fix | Delete
$instance['img_width'] = $new_img_width;
[193] Fix | Delete
$instance['img_height'] = $new_img_height;
[194] Fix | Delete
}
[195] Fix | Delete
} else {
[196] Fix | Delete
$instance['img_width'] = $new_img_width;
[197] Fix | Delete
$instance['img_height'] = $new_img_height;
[198] Fix | Delete
}
[199] Fix | Delete
[200] Fix | Delete
return $instance;
[201] Fix | Delete
}
[202] Fix | Delete
[203] Fix | Delete
/**
[204] Fix | Delete
* Back end widget form.
[205] Fix | Delete
*
[206] Fix | Delete
* @see WP_Widget::form()
[207] Fix | Delete
*
[208] Fix | Delete
* @param array $instance Previously saved values from database.
[209] Fix | Delete
* @return string|void
[210] Fix | Delete
*/
[211] Fix | Delete
public function form( $instance ) {
[212] Fix | Delete
// Defaults.
[213] Fix | Delete
$instance = wp_parse_args(
[214] Fix | Delete
(array) $instance,
[215] Fix | Delete
array(
[216] Fix | Delete
'title' => '',
[217] Fix | Delete
'img_url' => '',
[218] Fix | Delete
'alt_text' => '',
[219] Fix | Delete
'img_title' => '',
[220] Fix | Delete
'caption' => '',
[221] Fix | Delete
'align' => 'none',
[222] Fix | Delete
'img_width' => '',
[223] Fix | Delete
'img_height' => '',
[224] Fix | Delete
'link' => '',
[225] Fix | Delete
'link_target_blank' => false,
[226] Fix | Delete
)
[227] Fix | Delete
);
[228] Fix | Delete
[229] Fix | Delete
$title = esc_attr( $instance['title'] );
[230] Fix | Delete
$img_url = esc_url( $instance['img_url'], null, 'display' );
[231] Fix | Delete
$alt_text = esc_attr( $instance['alt_text'] );
[232] Fix | Delete
$img_title = esc_attr( $instance['img_title'] );
[233] Fix | Delete
$caption = esc_textarea( $instance['caption'] );
[234] Fix | Delete
$align = esc_attr( $instance['align'] );
[235] Fix | Delete
$img_width = esc_attr( $instance['img_width'] );
[236] Fix | Delete
$img_height = esc_attr( $instance['img_height'] );
[237] Fix | Delete
$link_target_blank = checked( $instance['link_target_blank'], true, false );
[238] Fix | Delete
[239] Fix | Delete
$link = esc_url( $instance['link'], null, 'display' );
[240] Fix | Delete
[241] Fix | Delete
echo '<p><label for="' . esc_attr( $this->get_field_id( 'title' ) ) . '">' . esc_html__( 'Widget title:', 'jetpack' ) . '
[242] Fix | Delete
<input class="widefat" id="' . esc_attr( $this->get_field_id( 'title' ) ) . '" name="' . esc_attr( $this->get_field_name( 'title' ) ) . '" type="text" value="' . esc_attr( $title ) . '" />
[243] Fix | Delete
</label></p>
[244] Fix | Delete
<p><label for="' . esc_attr( $this->get_field_id( 'img_url' ) ) . '">' . esc_html__( 'Image URL:', 'jetpack' ) . '
[245] Fix | Delete
<input class="widefat" id="' . esc_attr( $this->get_field_id( 'img_url' ) ) . '" name="' . esc_attr( $this->get_field_name( 'img_url' ) ) . '" type="text" value="' . esc_attr( $img_url ) . '" />
[246] Fix | Delete
</label></p>
[247] Fix | Delete
<p><label for="' . esc_attr( $this->get_field_id( 'alt_text' ) ) . '">' . esc_html__( 'Alternate text:', 'jetpack' ) . ' <a href="https://support.wordpress.com/widgets/image-widget/#image-widget-alt-text" target="_blank">( ? )</a>
[248] Fix | Delete
<input class="widefat" id="' . esc_attr( $this->get_field_id( 'alt_text' ) ) . '" name="' . esc_attr( $this->get_field_name( 'alt_text' ) ) . '" type="text" value="' . esc_attr( $alt_text ) . '" />
[249] Fix | Delete
</label></p>
[250] Fix | Delete
<p><label for="' . esc_attr( $this->get_field_id( 'img_title' ) ) . '">' . esc_html__( 'Image title:', 'jetpack' ) . ' <a href="https://support.wordpress.com/widgets/image-widget/#image-widget-title" target="_blank">( ? )</a>
[251] Fix | Delete
<input class="widefat" id="' . esc_attr( $this->get_field_id( 'img_title' ) ) . '" name="' . esc_attr( $this->get_field_name( 'img_title' ) ) . '" type="text" value="' . esc_attr( $img_title ) . '" />
[252] Fix | Delete
</label></p>
[253] Fix | Delete
<p><label for="' . esc_attr( $this->get_field_id( 'caption' ) ) . '">' . esc_html__( 'Caption:', 'jetpack' ) . ' <a href="https://support.wordpress.com/widgets/image-widget/#image-widget-caption" target="_blank">( ? )</a>
[254] Fix | Delete
<textarea class="widefat" id="' . esc_attr( $this->get_field_id( 'caption' ) ) . '" name="' . esc_attr( $this->get_field_name( 'caption' ) ) . '" rows="2" cols="20">' . esc_textarea( $caption ) . '</textarea>
[255] Fix | Delete
</label></p>';
[256] Fix | Delete
[257] Fix | Delete
$alignments = array(
[258] Fix | Delete
'none' => __( 'None', 'jetpack' ),
[259] Fix | Delete
'left' => __( 'Left', 'jetpack' ),
[260] Fix | Delete
'center' => __( 'Center', 'jetpack' ),
[261] Fix | Delete
'right' => __( 'Right', 'jetpack' ),
[262] Fix | Delete
);
[263] Fix | Delete
echo '<p><label for="' . esc_attr( $this->get_field_id( 'align' ) ) . '">' . esc_html__( 'Image Alignment:', 'jetpack' ) . '
[264] Fix | Delete
<select id="' . esc_attr( $this->get_field_id( 'align' ) ) . '" name="' . esc_attr( $this->get_field_name( 'align' ) ) . '">';
[265] Fix | Delete
foreach ( $alignments as $alignment => $alignment_name ) {
[266] Fix | Delete
echo '<option value="' . esc_attr( $alignment ) . '" ';
[267] Fix | Delete
if ( $alignment === $align ) {
[268] Fix | Delete
echo 'selected="selected" ';
[269] Fix | Delete
}
[270] Fix | Delete
echo '>' . esc_html( $alignment_name ) . "</option>\n";
[271] Fix | Delete
}
[272] Fix | Delete
echo '</select></label></p>';
[273] Fix | Delete
[274] Fix | Delete
echo '<p><label for="' . esc_attr( $this->get_field_id( 'img_width' ) ) . '">' . esc_html__( 'Width in pixels:', 'jetpack' ) . '
[275] Fix | Delete
<input size="3" id="' . esc_attr( $this->get_field_id( 'img_width' ) ) . '" name="' . esc_attr( $this->get_field_name( 'img_width' ) ) . '" type="text" value="' . esc_attr( $img_width ) . '" />
[276] Fix | Delete
</label>
[277] Fix | Delete
<label for="' . esc_attr( $this->get_field_id( 'img_height' ) ) . '">' . esc_html__( 'Height in pixels:', 'jetpack' ) . '
[278] Fix | Delete
<input size="3" id="' . esc_attr( $this->get_field_id( 'img_height' ) ) . '" name="' . esc_attr( $this->get_field_name( 'img_height' ) ) . '" type="text" value="' . esc_attr( $img_height ) . '" />
[279] Fix | Delete
</label><br />
[280] Fix | Delete
<small>' . esc_html__( 'If empty, we will attempt to determine the image size.', 'jetpack' ) . '</small></p>
[281] Fix | Delete
<p><label for="' . esc_attr( $this->get_field_id( 'link' ) ) . '">' . esc_html__( 'Link URL (when the image is clicked):', 'jetpack' ) . '
[282] Fix | Delete
<input class="widefat" id="' . esc_attr( $this->get_field_id( 'link' ) ) . '" name="' . esc_attr( $this->get_field_name( 'link' ) ) . '" type="text" value="' . esc_attr( $link ) . '" />
[283] Fix | Delete
</label>
[284] Fix | Delete
<label for="' . esc_attr( $this->get_field_id( 'link_target_blank' ) ) . '">
[285] Fix | Delete
<input type="checkbox" name="' . esc_attr( $this->get_field_name( 'link_target_blank' ) ) . '" id="' . esc_attr( $this->get_field_id( 'link_target_blank' ) ) . '" value="1"' . esc_attr( $link_target_blank ) . '/>
[286] Fix | Delete
' . esc_html__( 'Open link in a new window/tab', 'jetpack' ) . '
[287] Fix | Delete
</label></p>';
[288] Fix | Delete
}
[289] Fix | Delete
} // Class Jetpack_Image_Widget
[290] Fix | Delete
[291] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function