types = $types; } public function render( $value, $type, $context = array() ) { switch ( $type ) { case 'multiline': $output = nl2br( esc_html( (string) $value ) ); break; case 'url': $output = esc_url( (string) $value ); break; case 'email': $output = esc_html( (string) $value ); break; case 'image': $output = $this->image( $value, $context ); break; default: $output = esc_html( (string) $value ); } $filtered = apply_filters( 'glovario_rendered_value', $output, $value, $type, $context ); // A callback may have added markup: allow only what post content allows. return $filtered === $output ? $output : wp_kses( (string) $filtered, self::allowed_html() ); } /** * Post content markup plus the responsive image attributes that * wp_get_attachment_image() prints, for output changed by a filter. * * @return array Allowed HTML for wp_kses(). */ private static function allowed_html() { $allowed = wp_kses_allowed_html( 'post' ); $allowed['img'] = array_merge( $allowed['img'] ?? array(), array( 'srcset' => true, 'sizes' => true, 'decoding' => true, 'fetchpriority' => true, ) ); return $allowed; } /** * A value as plain text, for places that cannot show markup such as titles, * menus, and search results; an image becomes its alternative text. * * @param mixed $value Resolved value. * @param string $type Variable type. * @return string */ public static function text( $value, $type ) { if ( 'image' !== $type ) { return (string) $value; } if ( ! TypeRegistry::is_image( $value ) ) { return ''; } $alt = trim( (string) get_post_meta( absint( $value ), '_wp_attachment_image_alt', true ) ); return '' !== $alt ? $alt : get_the_title( absint( $value ) ); } /** * Responsive image markup from the Media Library, with its alternative text. * * @param mixed $value Attachment ID. * @param array $context Render context; image_size selects a registered size. * @return string */ private function image( $value, $context ) { if ( ! TypeRegistry::is_image( $value ) ) { return ''; } $size = sanitize_key( (string) ( $context['image_size'] ?? '' ) ); if ( ! in_array( $size, array_merge( get_intermediate_image_sizes(), array( 'full' ) ), true ) ) { $size = 'full'; } return wp_get_attachment_image( absint( $value ), $size, false, array( 'class' => 'glovario-image' ) ); } }