| 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, sanitization is done in the get() function |
| 21 |
echo 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['ai_label'] = $data['ai_label'] ?? Image_Sources::get_ai_label( $id ); |
| 48 |
$metadata['licence'] = $data['licence'] ?? Image_Sources::get_image_license( $id ); |
| 49 |
|
| 50 |
if ( empty( $options['ai_images']['show_label'] ) ) { |
| 51 |
$metadata['ai_label'] = ''; |
| 52 |
} |
| 53 |
|
| 54 |
if ( ! isset( $args['disable-links'] ) ) { |
| 55 |
$metadata['source_url'] = $data['source_url'] ?? Image_Sources::get_image_source_url( $id ); |
| 56 |
} else { |
| 57 |
$metadata['source_url'] = ''; |
| 58 |
} |
| 59 |
|
| 60 |
$source = ''; |
| 61 |
|
| 62 |
if ( $metadata['own'] ) { |
| 63 |
$source = Standard_Source::get_standard_source_text_for_attachment( $id ); |
| 64 |
} elseif ( '' !== $metadata['source'] ) { |
| 65 |
$source = $metadata['source']; |
| 66 |
} |
| 67 |
|
| 68 |
if ( '' === $source && '' === $metadata['ai_label'] ) { |
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
// sanitize the source string, to avoid XSS attacks |
| 73 |
$source = Image_Sources::sanitize_source_html( $source ); |
| 74 |
|
| 75 |
// wrap link around source, if given |
| 76 |
if ( '' !== $metadata['source_url'] ) { |
| 77 |
$classes = apply_filters( 'isc_public_source_url_html_classes', [], $id, $data, $args, $metadata ); |
| 78 |
$class_string = count( $classes ) > 0 ? ' class="' . esc_attr( implode( ' ', $classes ) ) . '"' : ''; |
| 79 |
|
| 80 |
$source = apply_filters( |
| 81 |
'isc_public_source_url_html', |
| 82 |
sprintf( '<a href="%2$s" target="_blank" rel="nofollow"%3$s>%1$s</a>', $source, esc_url_raw( $metadata['source_url'] ), $class_string ), |
| 83 |
$id, |
| 84 |
$metadata |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
// add AI label icon if enabled |
| 89 |
$ai_icon = \ISC\Image_Sources\Ai_Labels::get_icon( $metadata['ai_label'] ); |
| 90 |
if ( '' !== $ai_icon ) { |
| 91 |
$source = trim( sprintf( '%1$s %2$s', $ai_icon, $source ) ); |
| 92 |
} |
| 93 |
|
| 94 |
// add license if enabled |
| 95 |
if ( $options['enable_licences'] && isset( $metadata['licence'] ) && $metadata['licence'] ) { |
| 96 |
$licences = \ISC\Image_Sources\Utils::licences_text_to_array( $options['licences'] ); |
| 97 |
if ( ! isset( $args['disable-links'] ) && isset( $licences[ $metadata['licence'] ]['url'] ) ) { |
| 98 |
$licence_url = $licences[ $metadata['licence'] ]['url']; |
| 99 |
} |
| 100 |
|
| 101 |
if ( isset( $licence_url ) && $licence_url !== '' ) { |
| 102 |
$source = sprintf( '%1$s | <a href="%3$s" target="_blank" rel="nofollow">%2$s</a>', $source, $metadata['licence'], $licence_url ); |
| 103 |
} else { |
| 104 |
$source = sprintf( '%1$s | %2$s', $source, $metadata['licence'] ); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
return $source; |
| 109 |
} |
| 110 |
} |
| 111 |
|