WP_Widget_Media_Image::form( array $instance )
Back-end widget form.
Description
See also
Parameters
- $instance
-
(Required) Previously saved values from database.
Source
File: wp-includes/widgets/class-wp-widget-media-image.php
public function form( $instance ) {
$instance = array_merge( wp_list_pluck( $this->get_instance_schema(), 'default' ), $instance );
$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
$attachment_id = ! empty( $instance['attachment_id'] ) ? $instance['attachment_id'] : 0;
$size = ! empty( $instance['size'] ) ? $instance['size'] : 'full';
$alt = ! empty( $instance['alt'] ) ? $instance['alt'] : '';
$link_type = ! empty( $instance['link_type'] ) ? $instance['link_type'] : 'none';
$link_url = ! empty( $instance['link_url'] ) ? $instance['link_url'] : '';
$caption = ! empty( $instance['caption'] ) ? $instance['caption'] : '';
$url = ! empty( $instance['url'] ) ? $instance['url'] : '';
$width = ! empty( $instance['width'] ) ? absint( $instance['width'] ) : 0;
$height = ! empty( $instance['height'] ) ? absint( $instance['height'] ) : 0;
$image_classes = ! empty( $instance['image_classes'] ) ? $instance['image_classes'] : '';
$link_classes = ! empty( $instance['link_classes'] ) ? $instance['link_classes'] : '';
$link_rel = ! empty( $instance['link_rel'] ) ? $instance['link_rel'] : '';
$link_target_blank = ! empty( $instance['link_target_blank'] ) ? '_blank' : '';
$link_image_title = ! empty( $instance['link_image_title'] ) ? $instance['link_image_title'] : '';
$attributes = 'alt="' . $alt . '"';
$aria_label = '';
if ( $attachment_id && $url === '' ) {
$url = wp_get_attachment_url( $attachment_id );
}
// Create an aria-label attribute if the image has no alt attribute.
if ( $url && $alt === '' ) {
$aria_label = esc_attr(
sprintf(
/* translators: %s: The image file name. */
__( 'The current image has no alternative text. The file name is: %s' ),
basename( $url )
)
);
$attributes .= ' aria-label="' . $aria_label . '"';
}
/**
* Filter media image attributes within media image widget
*
* @since CP-2.5.0
*
* @param string $attributes The default attributes.
* @param string $alt The default alt attribute.
* @param string $aria-label The default aria-label attribute.
* @param int $attachment_id The attachment ID.
* @param string $url The image file URL.
*/
$attributes = apply_filters( 'cp_media_image_widget_image_attributes', $attributes, $alt, $aria_label, $attachment_id, $url );
$image_html = '';
if ( $url ) {
if ( $attachment_id === 0 ) {
$attachment_id = attachment_url_to_postid( $url );
}
$image_html = '<img class="attachment-thumb" src="' . esc_url( $url ) . '" draggable="false" ' . $attributes . '>';
if ( $caption !== '' ) {
$image_html = '<figure style="margin:auto;">' . $image_html . '<figcaption>' . esc_html( $caption ) . '</figcaption></figure>';
}
} else {
$image_html = '<div class="notice-error notice-alt" style="border-left: 3px solid #d63638;"><p style="padding: 0.5em 0">' . esc_html__( 'Unable to preview media due to an unknown error.' ) . '</p></div>';
}
$size_options = '';
$attachment_metadata = array();
if ( $attachment_id !== 0 ) {
$attachment_metadata = wp_prepare_attachment_for_js( $attachment_id );
if ( ! empty( $attachment_metadata ) ) {
$sizes_array = $attachment_metadata['sizes'];
foreach ( $sizes_array as $key => $option ) {
$option_text = ucwords( str_replace( '_', ' ', $key ) ) . ' – ' . $option['width'] . ' x ' . $option['height'];
$size_options .= '<option value="' . esc_attr( $key ) . '"' . selected( $key, $size, false ) . '>' . esc_html( $option_text ) . '</option>';
}
}
}
?>
<div class="media-widget-control selected">
<fieldset>
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:' ); ?></label>
<input id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" class="widefat" type="text" value="<?php echo esc_attr( $title ); ?>">
</fieldset>
<?php
if ( $url && $attachment_metadata ) {
if ( $attachment_id === 0 ) {
$edit_nonce = '';
} else {
$edit_nonce = 'data-edit-nonce="' . esc_attr( $attachment_metadata['nonces']['edit'] ) . '"';
$update_nonce = $attachment_metadata['nonces']['update'];
}
?>
<div class="media-widget-preview media_image populated"><?php echo $image_html; ?></div>
<fieldset class="media-widget-buttons">
<?php
if ( ! empty( $edit_nonce ) || $attachment_id === 0 ) {
?>
<button type="button" class="button edit-media selected" <?php echo $edit_nonce; ?>><?php esc_html_e( 'Edit Image' ); ?></button>
<?php
}
if ( ! empty( $update_nonce ) || $attachment_id === 0 ) {
?>
<button type="button" class="button change-media select-media selected"><?php esc_html_e( 'Replace Image' ); ?></button>
<?php
}
?>
</fieldset>
<fieldset class="media-widget-image-link">
<label for="<?php echo esc_attr( $this->get_field_id( 'link_url' ) ); ?>"><?php esc_html_e( 'Link to:' ); ?></label>
<input id="<?php echo esc_attr( $this->get_field_id( 'link_url' ) ); ?>" name="<?php echo $this->get_field_name( 'link_url' ); ?>" class="widefat" type="url" value="<?php echo esc_url( $link_url ); ?>" placeholder="https://" data-property="link_url">
</fieldset>
<?php
} else {
?>
<div class="media-widget-preview media_image">
<div class="attachment-media-view">
<button type="button" class="select-media button-add-media"><?php esc_html_e( 'Add Image' ); ?></button>
</div>
</div>
<?php
}
?>
<input id="<?php echo esc_attr( $this->get_field_id( 'size' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'size' ) ); ?>" type="hidden" data-property="size" class="media-widget-instance-property" value="<?php echo esc_attr( esc_attr( $size ) ); ?>">
<input id="<?php echo esc_attr( $this->get_field_id( 'width' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'width' ) ); ?>" type="hidden" data-property="width" class="media-widget-instance-property" value="<?php echo esc_attr( $width ); ?>">
<input id="<?php echo esc_attr( $this->get_field_id( 'height' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'height' ) ); ?>" type="hidden" data-property="height" class="media-widget-instance-property" value="<?php echo esc_attr( $height ); ?>">
<input id="<?php echo esc_attr( $this->get_field_id( 'caption' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'caption' ) ); ?>" type="hidden" data-property="caption" class="media-widget-instance-property" value="<?php echo esc_attr( $caption ); ?>">
<input id="<?php echo esc_attr( $this->get_field_id( 'alt' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'alt' ) ); ?>" type="hidden" data-property="alt" class="media-widget-instance-property" value="<?php echo esc_attr( $alt ); ?>">
<input id="<?php echo esc_attr( $this->get_field_id( 'link_type' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'link_type' ) ); ?>" type="hidden" data-property="link_type" class="media-widget-instance-property" value="<?php echo esc_attr( $link_type ); ?>">
<input id="<?php echo esc_attr( $this->get_field_id( 'image_classes' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'image_classes' ) ); ?>" type="hidden" data-property="image_classes" class="media-widget-instance-property" value="<?php echo esc_attr( $image_classes ); ?>">
<input id="<?php echo esc_attr( $this->get_field_id( 'link_classes' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'link_classes' ) ); ?>" type="hidden" data-property="link_classes" class="media-widget-instance-property" value="<?php echo esc_attr( $link_classes ); ?>">
<input id="<?php echo esc_attr( $this->get_field_id( 'link_rel' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'link_rel' ) ); ?>" type="hidden" data-property="link_rel" class="media-widget-instance-property" value="<?php echo esc_url( $link_rel ); ?>">
<input id="<?php echo esc_attr( $this->get_field_id( 'link_target_blank' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'link_target_blank' ) ); ?>" type="hidden" data-property="link_target_blank" class="media-widget-instance-property" value="<?php echo esc_attr( $link_target_blank ); ?>">
<input id="<?php echo esc_attr( $this->get_field_id( 'link_image_title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'link_image_title' ) ); ?>" type="hidden" data-property="link_image_title" class="media-widget-instance-property" value="<?php echo esc_attr( $link_image_title ); ?>">
<input id="<?php echo esc_attr( $this->get_field_id( 'attachment_id' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'attachment_id' ) ); ?>" type="hidden" data-property="attachment_id" class="media-widget-instance-property" value="<?php echo esc_attr( $attachment_id ); ?>">
<input id="<?php echo esc_attr( $this->get_field_id( 'url' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'url' ) ); ?>" type="hidden" data-property="url" class="media-widget-instance-property" value="<?php echo esc_url( $url ); ?>">
<input id="<?php echo esc_attr( $this->get_field_id( 'size_options' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'size_options' ) ); ?>" type="hidden" data-property="size_options" class="media-widget-instance-property" value="<?php echo esc_attr( $size_options ); ?>">
</div>
<?php
}
Changelog
| Version | Description |
|---|---|
| CP-2.5.0 | Introduced. CP-2.5.0 |