| 1 |
<?php |
| 2 |
|
| 3 |
namespace ISC\Image_Sources\Renderer; |
| 4 |
|
| 5 |
use ISC\Image_Sources\Image_Sources; |
| 6 |
use ISC\Image_Sources\Renderer; |
| 7 |
use ISC\Standard_Source; |
| 8 |
use ISC_Log; |
| 9 |
|
| 10 |
/** |
| 11 |
* Render the caption. |
| 12 |
*/ |
| 13 |
class Caption extends Renderer { |
| 14 |
|
| 15 |
/** |
| 16 |
* Main render function that can be called in the frontend. |
| 17 |
* |
| 18 |
* @param int $image_id Image ID. |
| 19 |
*/ |
| 20 |
public static function render( int $image_id ) { |
| 21 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 22 |
echo self::get( $image_id ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Render caption string / markup |
| 27 |
* including the style wrapper, if enabled in the plugin settings |
| 28 |
* |
| 29 |
* @param int $image_id id of the image. |
| 30 |
* @param string[] $data metadata. |
| 31 |
* @param array $args additional arguments |
| 32 |
* use "disable-links" = (any value), to disable any working links. |
| 33 |
* use "styled" = false to disable the style wrapper. |
| 34 |
* |
| 35 |
* @return string |
| 36 |
*/ |
| 37 |
public static function get( int $image_id, array $data = [], array $args = [] ) { |
| 38 |
$source = Image_Source_String::get( $image_id, $data, $args ); |
| 39 |
if ( ! $source ) { |
| 40 |
ISC_Log::log( sprintf( 'skipped overlay for empty sources string for ID "%s"', $image_id ) ); |
| 41 |
return ''; |
| 42 |
} |
| 43 |
|
| 44 |
// don’t render the caption for own images if the admin choose not to do so |
| 45 |
if ( Standard_Source::hide_standard_source_for_image( $image_id ) ) { |
| 46 |
ISC_Log::log( sprintf( 'skipped overlay for "own" image ID "%s"', $image_id ) ); |
| 47 |
return ''; |
| 48 |
} |
| 49 |
|
| 50 |
$options = self::get_options(); |
| 51 |
$remove_wrapper_if_source_empty = ! empty( $options['ai_images']['remove_wrapper_if_source_empty'] ) && self::source_is_empty( $image_id, $data ); |
| 52 |
|
| 53 |
// add the prefix if not disabled |
| 54 |
if ( ( ! array_key_exists( 'prefix', $args ) || $args['prefix'] ) && ! $remove_wrapper_if_source_empty ) { |
| 55 |
$source = self::add_prefix( $source ); |
| 56 |
} |
| 57 |
|
| 58 |
// add style wrapper if not disabled |
| 59 |
if ( ! array_key_exists( 'styled', $args ) || $args['styled'] ) { |
| 60 |
$source = self::add_style( $source, $image_id, $remove_wrapper_if_source_empty ); |
| 61 |
} |
| 62 |
|
| 63 |
return $source; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Add style |
| 68 |
* |
| 69 |
* @param string $source Source string. |
| 70 |
* @param int $image_id Image ID. |
| 71 |
* @param bool $remove_wrapper_if_source_empty Whether wrapper styling should be removed. |
| 72 |
* @return string |
| 73 |
*/ |
| 74 |
public static function add_style( string $source, int $image_id, bool $remove_wrapper_if_source_empty = false ) { |
| 75 |
if ( self::has_caption_style() && apply_filters( 'isc_caption_apply_default_style', '__return_true' ) ) { |
| 76 |
$source = '<span class="isc-source-text' . ( $remove_wrapper_if_source_empty ? ' isc-source-text-empty-source' : '' ) . '">' . $source . '</span>'; |
| 77 |
} |
| 78 |
|
| 79 |
return apply_filters( 'isc_overlay_html_source', $source, $image_id ); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Check whether the source text is empty before AI labels are added. |
| 84 |
* |
| 85 |
* @param int $image_id Image ID. |
| 86 |
* @param string[] $data Metadata. |
| 87 |
* |
| 88 |
* @return bool |
| 89 |
*/ |
| 90 |
private static function source_is_empty( int $image_id, array $data = [] ): bool { |
| 91 |
$source = $data['source'] ?? Image_Sources::get_image_source_text_raw( $image_id ); |
| 92 |
$own = $data['own'] ?? Standard_Source::use_standard_source( $image_id ); |
| 93 |
|
| 94 |
if ( $own && ! Standard_Source::hide_standard_source_for_image( $image_id ) ) { |
| 95 |
$source = Standard_Source::get_standard_source_text_for_attachment( $image_id ); |
| 96 |
} |
| 97 |
|
| 98 |
return '' === $source; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Add pre-text |
| 103 |
* |
| 104 |
* @param string $source Source string. |
| 105 |
* @return string |
| 106 |
*/ |
| 107 |
public static function add_prefix( $source ) { |
| 108 |
$options = self::get_options(); |
| 109 |
|
| 110 |
if ( empty( $options['source_pretext'] ) ) { |
| 111 |
return $source; |
| 112 |
} |
| 113 |
|
| 114 |
return $options['source_pretext'] . ' ' . $source; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Check if the caption has a style in general |
| 119 |
* |
| 120 |
* @return bool |
| 121 |
*/ |
| 122 |
public static function has_caption_style(): bool { |
| 123 |
$style = self::get_options()['caption_style']; |
| 124 |
return $style !== 'none'; |
| 125 |
} |
| 126 |
} |
| 127 |
|