[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

/wp-includes/widgets/ -> class-wp-widget-media-image.php (source)

   1  <?php
   2  /**
   3   * Widget API: WP_Widget_Media_Image class
   4   *
   5   * @package WordPress
   6   * @subpackage Widgets
   7   * @since 4.8.0
   8   */
   9  
  10  /**
  11   * Core class that implements an image widget.
  12   *
  13   * @since 4.8.0
  14   *
  15   * @see WP_Widget_Media
  16   * @see WP_Widget
  17   */
  18  class WP_Widget_Media_Image extends WP_Widget_Media {
  19  
  20      /**
  21       * Constructor.
  22       *
  23       * @since 4.8.0
  24       */
  25  	public function __construct() {
  26          parent::__construct(
  27              'media_image',
  28              __( 'Image' ),
  29              array(
  30                  'description' => __( 'Displays an image.' ),
  31                  'mime_type'   => 'image',
  32              )
  33          );
  34  
  35          $this->l10n = array_merge(
  36              $this->l10n,
  37              array(
  38                  'no_media_selected'          => __( 'No image selected' ),
  39                  'add_media'                  => _x( 'Add Image', 'label for button in the image widget' ),
  40                  'replace_media'              => _x( 'Replace Image', 'label for button in the image widget; should preferably not be longer than ~13 characters long' ),
  41                  'edit_media'                 => _x( 'Edit Image', 'label for button in the image widget; should preferably not be longer than ~13 characters long' ),
  42                  'missing_attachment'         => sprintf(
  43                      /* translators: %s: URL to media library. */
  44                      __( 'We can&#8217;t find that image. Check your <a href="%s">media library</a> and make sure it wasn&#8217;t deleted.' ),
  45                      esc_url( admin_url( 'upload.php' ) )
  46                  ),
  47                  /* translators: %d: Widget count. */
  48                  'media_library_state_multi'  => _n_noop( 'Image Widget (%d)', 'Image Widget (%d)' ),
  49                  'media_library_state_single' => __( 'Image Widget' ),
  50              )
  51          );
  52      }
  53  
  54      /**
  55       * Get schema for properties of a widget instance (item).
  56       *
  57       * @since 4.8.0
  58       *
  59       * @see WP_REST_Controller::get_item_schema()
  60       * @see WP_REST_Controller::get_additional_fields()
  61       * @link https://core.trac.wordpress.org/ticket/35574
  62       * @return array Schema for properties.
  63       */
  64  	public function get_instance_schema() {
  65          return array_merge(
  66              array(
  67                  'size'              => array(
  68                      'type'        => 'string',
  69                      'enum'        => array_merge( get_intermediate_image_sizes(), array( 'full', 'custom' ) ),
  70                      'default'     => 'medium',
  71                      'description' => __( 'Size' ),
  72                  ),
  73                  'width'             => array( // Via 'customWidth', only when size=custom; otherwise via 'width'.
  74                      'type'        => 'integer',
  75                      'minimum'     => 0,
  76                      'default'     => 0,
  77                      'description' => __( 'Width' ),
  78                  ),
  79                  'height'            => array( // Via 'customHeight', only when size=custom; otherwise via 'height'.
  80                      'type'        => 'integer',
  81                      'minimum'     => 0,
  82                      'default'     => 0,
  83                      'description' => __( 'Height' ),
  84                  ),
  85  
  86                  'caption'           => array(
  87                      'type'                  => 'string',
  88                      'default'               => '',
  89                      'sanitize_callback'     => 'wp_kses_post',
  90                      'description'           => __( 'Caption' ),
  91                      'should_preview_update' => false,
  92                  ),
  93                  'alt'               => array(
  94                      'type'              => 'string',
  95                      'default'           => '',
  96                      'sanitize_callback' => 'sanitize_text_field',
  97                      'description'       => __( 'Alternative Text' ),
  98                  ),
  99                  'link_type'         => array(
 100                      'type'                  => 'string',
 101                      'enum'                  => array( 'none', 'file', 'post', 'custom' ),
 102                      'default'               => 'custom',
 103                      'media_prop'            => 'link',
 104                      'description'           => __( 'Link To' ),
 105                      'should_preview_update' => true,
 106                  ),
 107                  'link_url'          => array(
 108                      'type'                  => 'string',
 109                      'default'               => '',
 110                      'format'                => 'uri',
 111                      'media_prop'            => 'linkUrl',
 112                      'description'           => __( 'URL' ),
 113                      'should_preview_update' => true,
 114                  ),
 115                  'image_classes'     => array(
 116                      'type'                  => 'string',
 117                      'default'               => '',
 118                      'sanitize_callback'     => array( $this, 'sanitize_token_list' ),
 119                      'media_prop'            => 'extraClasses',
 120                      'description'           => __( 'Image CSS Class' ),
 121                      'should_preview_update' => false,
 122                  ),
 123                  'link_classes'      => array(
 124                      'type'                  => 'string',
 125                      'default'               => '',
 126                      'sanitize_callback'     => array( $this, 'sanitize_token_list' ),
 127                      'media_prop'            => 'linkClassName',
 128                      'should_preview_update' => false,
 129                      'description'           => __( 'Link CSS Class' ),
 130                  ),
 131                  'link_rel'          => array(
 132                      'type'                  => 'string',
 133                      'default'               => '',
 134                      'sanitize_callback'     => array( $this, 'sanitize_token_list' ),
 135                      'media_prop'            => 'linkRel',
 136                      'description'           => __( 'Link Rel' ),
 137                      'should_preview_update' => false,
 138                  ),
 139                  'link_target_blank' => array(
 140                      'type'                  => 'boolean',
 141                      'default'               => false,
 142                      'media_prop'            => 'linkTargetBlank',
 143                      'description'           => __( 'Open link in a new tab' ),
 144                      'should_preview_update' => false,
 145                  ),
 146                  'image_title'       => array(
 147                      'type'                  => 'string',
 148                      'default'               => '',
 149                      'sanitize_callback'     => 'sanitize_text_field',
 150                      'media_prop'            => 'title',
 151                      'description'           => __( 'Image Title Attribute' ),
 152                      'should_preview_update' => false,
 153                  ),
 154  
 155                  /*
 156                   * There are two additional properties exposed by the PostImage modal
 157                   * that don't seem to be relevant, as they may only be derived read-only
 158                   * values:
 159                   * - originalUrl
 160                   * - aspectRatio
 161                   * - height (redundant when size is not custom)
 162                   * - width (redundant when size is not custom)
 163                   */
 164              ),
 165              parent::get_instance_schema()
 166          );
 167      }
 168  
 169      /**
 170       * Render the media on the frontend.
 171       *
 172       * @since 4.8.0
 173       *
 174       * @param array $instance Widget instance props.
 175       * @return void
 176       */
 177  	public function render_media( $instance ) {
 178          $instance = array_merge( wp_list_pluck( $this->get_instance_schema(), 'default' ), $instance );
 179          $instance = wp_parse_args(
 180              $instance,
 181              array(
 182                  'size' => 'thumbnail',
 183              )
 184          );
 185  
 186          $attachment = null;
 187          if ( $this->is_attachment_with_mime_type( $instance['attachment_id'], $this->widget_options['mime_type'] ) ) {
 188              $attachment = get_post( $instance['attachment_id'] );
 189          }
 190          if ( $attachment ) {
 191              $caption = '';
 192              if ( ! isset( $instance['caption'] ) ) {
 193                  $caption = $attachment->post_excerpt;
 194              } elseif ( trim( $instance['caption'] ) ) {
 195                  $caption = $instance['caption'];
 196              }
 197  
 198              $image_attributes = array(
 199                  'class' => sprintf( 'image wp-image-%d %s', $attachment->ID, $instance['image_classes'] ),
 200                  'style' => 'max-width: 100%; height: auto;',
 201              );
 202              if ( ! empty( $instance['image_title'] ) ) {
 203                  $image_attributes['title'] = $instance['image_title'];
 204              }
 205  
 206              if ( $instance['alt'] ) {
 207                  $image_attributes['alt'] = $instance['alt'];
 208              }
 209  
 210              $size = $instance['size'];
 211              if ( 'custom' === $size || ! in_array( $size, array_merge( get_intermediate_image_sizes(), array( 'full' ) ), true ) ) {
 212                  $size = array( $instance['width'], $instance['height'] );
 213              }
 214              $image_attributes['class'] .= sprintf( ' attachment-%1$s size-%1$s', is_array( $size ) ? join( 'x', $size ) : $size );
 215  
 216              $image = wp_get_attachment_image( $attachment->ID, $size, false, $image_attributes );
 217  
 218              $caption_size = _wp_get_image_size_from_meta( $instance['size'], wp_get_attachment_metadata( $attachment->ID ) );
 219              $width        = empty( $caption_size[0] ) ? 0 : $caption_size[0];
 220  
 221          } else {
 222              if ( empty( $instance['url'] ) ) {
 223                  return;
 224              }
 225  
 226              $instance['size'] = 'custom';
 227              $caption          = $instance['caption'];
 228              $width            = $instance['width'];
 229              $classes          = 'image ' . $instance['image_classes'];
 230              if ( 0 === $instance['width'] ) {
 231                  $instance['width'] = '';
 232              }
 233              if ( 0 === $instance['height'] ) {
 234                  $instance['height'] = '';
 235              }
 236  
 237              $image = sprintf(
 238                  '<img class="%1$s" src="%2$s" alt="%3$s" width="%4$s" height="%5$s" />',
 239                  esc_attr( $classes ),
 240                  esc_url( $instance['url'] ),
 241                  esc_attr( $instance['alt'] ),
 242                  esc_attr( $instance['width'] ),
 243                  esc_attr( $instance['height'] )
 244              );
 245          } // End if().
 246  
 247          $url = '';
 248          if ( 'file' === $instance['link_type'] ) {
 249              $url = $attachment ? wp_get_attachment_url( $attachment->ID ) : $instance['url'];
 250          } elseif ( $attachment && 'post' === $instance['link_type'] ) {
 251              $url = get_attachment_link( $attachment->ID );
 252          } elseif ( 'custom' === $instance['link_type'] && ! empty( $instance['link_url'] ) ) {
 253              $url = $instance['link_url'];
 254          }
 255  
 256          if ( $url ) {
 257              $link = sprintf( '<a href="%s"', esc_url( $url ) );
 258              if ( ! empty( $instance['link_classes'] ) ) {
 259                  $link .= sprintf( ' class="%s"', esc_attr( $instance['link_classes'] ) );
 260              }
 261              if ( ! empty( $instance['link_rel'] ) ) {
 262                  $link .= sprintf( ' rel="%s"', esc_attr( $instance['link_rel'] ) );
 263              }
 264              if ( ! empty( $instance['link_target_blank'] ) ) {
 265                  $link .= ' target="_blank"';
 266              }
 267              $link .= '>';
 268              $link .= $image;
 269              $link .= '</a>';
 270              $image = wp_targeted_link_rel( $link );
 271          }
 272  
 273          if ( $caption ) {
 274              $image = img_caption_shortcode(
 275                  array(
 276                      'width'   => $width,
 277                      'caption' => $caption,
 278                  ),
 279                  $image
 280              );
 281          }
 282  
 283          echo $image;
 284      }
 285  
 286      /**
 287       * Loads the required media files for the media manager and scripts for media widgets.
 288       *
 289       * @since 4.8.0
 290       */
 291  	public function enqueue_admin_scripts() {
 292          parent::enqueue_admin_scripts();
 293  
 294          $handle = 'media-image-widget';
 295          wp_enqueue_script( $handle );
 296  
 297          $exported_schema = array();
 298          foreach ( $this->get_instance_schema() as $field => $field_schema ) {
 299              $exported_schema[ $field ] = wp_array_slice_assoc( $field_schema, array( 'type', 'default', 'enum', 'minimum', 'format', 'media_prop', 'should_preview_update' ) );
 300          }
 301          wp_add_inline_script(
 302              $handle,
 303              sprintf(
 304                  'wp.mediaWidgets.modelConstructors[ %s ].prototype.schema = %s;',
 305                  wp_json_encode( $this->id_base ),
 306                  wp_json_encode( $exported_schema )
 307              )
 308          );
 309  
 310          wp_add_inline_script(
 311              $handle,
 312              sprintf(
 313                  '
 314                      wp.mediaWidgets.controlConstructors[ %1$s ].prototype.mime_type = %2$s;
 315                      wp.mediaWidgets.controlConstructors[ %1$s ].prototype.l10n = _.extend( {}, wp.mediaWidgets.controlConstructors[ %1$s ].prototype.l10n, %3$s );
 316                  ',
 317                  wp_json_encode( $this->id_base ),
 318                  wp_json_encode( $this->widget_options['mime_type'] ),
 319                  wp_json_encode( $this->l10n )
 320              )
 321          );
 322      }
 323  
 324      /**
 325       * Render form template scripts.
 326       *
 327       * @since 4.8.0
 328       */
 329  	public function render_control_template_scripts() {
 330          parent::render_control_template_scripts();
 331  
 332          ?>
 333          <script type="text/html" id="tmpl-wp-media-widget-image-fields">
 334              <# var elementIdPrefix = 'el' + String( Math.random() ) + '_'; #>
 335              <# if ( data.url ) { #>
 336              <p class="media-widget-image-link">
 337                  <label for="{{ elementIdPrefix }}linkUrl"><?php esc_html_e( 'Link to:' ); ?></label>
 338                  <input id="{{ elementIdPrefix }}linkUrl" type="text" class="widefat link" value="{{ data.link_url }}" placeholder="https://" pattern="((\w+:)?\/\/\w.*|\w+:(?!\/\/$)|\/|\?|#).*">
 339              </p>
 340              <# } #>
 341          </script>
 342          <script type="text/html" id="tmpl-wp-media-widget-image-preview">
 343              <# if ( data.error && 'missing_attachment' === data.error ) { #>
 344                  <div class="notice notice-error notice-alt notice-missing-attachment">
 345                      <p><?php echo $this->l10n['missing_attachment']; ?></p>
 346                  </div>
 347              <# } else if ( data.error ) { #>
 348                  <div class="notice notice-error notice-alt">
 349                      <p><?php _e( 'Unable to preview media due to an unknown error.' ); ?></p>
 350                  </div>
 351              <# } else if ( data.url ) { #>
 352                  <img class="attachment-thumb" src="{{ data.url }}" draggable="false" alt="{{ data.alt }}"
 353                      <# if ( ! data.alt && data.currentFilename ) { #>
 354                          aria-label="
 355                          <?php
 356                          echo esc_attr(
 357                              sprintf(
 358                                  /* translators: %s: The image file name. */
 359                                  __( 'The current image has no alternative text. The file name is: %s' ),
 360                                  '{{ data.currentFilename }}'
 361                              )
 362                          );
 363                          ?>
 364                          "
 365                      <# } #>
 366                  />
 367              <# } #>
 368          </script>
 369          <?php
 370      }
 371  }


Generated: Tue Oct 22 08:20:01 2019 Cross-referenced by PHPXref 0.7