| 1 |
<?php |
| 2 |
|
| 3 |
namespace ISC\Image_Sources\Renderer; |
| 4 |
|
| 5 |
use ISC\Image_Sources\Renderer; |
| 6 |
use ISC\Image_Sources\Image_Sources; |
| 7 |
use ISC\Standard_Source; |
| 8 |
|
| 9 |
/** |
| 10 |
* Render the caption. |
| 11 |
*/ |
| 12 |
class Image_Source_String extends Renderer { |
| 13 |
|
| 14 |
/** |
| 15 |
* Main render function that can be called in the frontend. |
| 16 |
* |
| 17 |
* @param int $image_id Image ID. |
| 18 |
*/ |
| 19 |
public static function render( int $image_id ) { |
| 20 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- we need to allow the HTML here and use a custom sanitizer. |
| 21 |
echo Image_Sources::sanitize_source_html( self::get( $image_id ) ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Render source string of single image by its id |
| 26 |
* this only returns the string with source and license (and urls), |
| 27 |
* but no wrapping, because the string is used in a lot of functions |
| 28 |
* (e.g. image source list where title is prepended) |
| 29 |
* |
| 30 |
* @param int|string $image_id id of the image. |
| 31 |
* @param string[] $data metadata. |
| 32 |
* @param array $args arguments |
| 33 |
* use "disable-links" = (any value), to disable any working links. |
| 34 |
* |
| 35 |
* @return bool|string false if no source was given, else string with source |
| 36 |
*/ |
| 37 |
public static function get( $image_id, array $data = [], array $args = [] ) { |
| 38 |
$id = (int) $image_id; |
| 39 |
|
| 40 |
if ( ! $id ) { |
| 41 |
return false; |
| 42 |
} |
| 43 |
|
| 44 |
$options = self::get_options(); |
| 45 |
$metadata['source'] = $data['source'] ?? Image_Sources::get_image_source_text_raw( $id ); |
| 46 |
$metadata['own'] = $data['own'] ?? Standard_Source::use_standard_source( $id ); |
| 47 |
$metadata['licence'] = $data['licence'] ?? Image_Sources::get_image_license( $id ); |
| 48 |
|
| 49 |
if ( ! isset( $args['disable-links'] ) ) { |
| 50 |
$metadata['source_url'] = $data['source_url'] ?? Image_Sources::get_image_source_url( $id ); |
| 51 |
} else { |
| 52 |
$metadata['source_url'] = ''; |
| 53 |
} |
| 54 |
|
| 55 |
$source = ''; |
| 56 |
|
| 57 |
if ( $metadata['own'] ) { |
| 58 |
$source = Standard_Source::get_standard_source_text_for_attachment( $id ); |
| 59 |
} elseif ( '' !== $metadata['source'] ) { |
| 60 |
$source = $metadata['source']; |
| 61 |
} |
| 62 |
|
| 63 |
if ( $source === '' ) { |
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
// wrap link around source, if given |
| 68 |
if ( '' !== $metadata['source_url'] ) { |
| 69 |
$classes = apply_filters( 'isc_public_source_url_html_classes', [], $id, $data, $args, $metadata ); |
| 70 |
$class_string = count( $classes ) > 0 ? ' class="' . esc_attr( implode( ' ', $classes ) ) . '"' : ''; |
| 71 |
|
| 72 |
$source = apply_filters( |
| 73 |
'isc_public_source_url_html', |
| 74 |
sprintf( '<a href="%2$s" target="_blank" rel="nofollow"%3$s>%1$s</a>', $source, esc_url_raw( $metadata['source_url'] ), $class_string ), |
| 75 |
$id, |
| 76 |
$metadata |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
// add license if enabled |
| 81 |
if ( $options['enable_licences'] && isset( $metadata['licence'] ) && $metadata['licence'] ) { |
| 82 |
$licences = \ISC\Image_Sources\Utils::licences_text_to_array( $options['licences'] ); |
| 83 |
if ( ! isset( $args['disable-links'] ) && isset( $licences[ $metadata['licence'] ]['url'] ) ) { |
| 84 |
$licence_url = $licences[ $metadata['licence'] ]['url']; |
| 85 |
} |
| 86 |
|
| 87 |
if ( isset( $licence_url ) && $licence_url !== '' ) { |
| 88 |
$source = sprintf( '%1$s | <a href="%3$s" target="_blank" rel="nofollow">%2$s</a>', $source, $metadata['licence'], $licence_url ); |
| 89 |
} else { |
| 90 |
$source = sprintf( '%1$s | %2$s', $source, $metadata['licence'] ); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
return $source; |
| 95 |
} |
| 96 |
} |
| 97 |
|