| 1 |
<?php |
| 2 |
|
| 3 |
namespace ISC\Image_Sources\Renderer; |
| 4 |
|
| 5 |
use ISC\Image_Sources\Renderer; |
| 6 |
use ISC\Standard_Source; |
| 7 |
use ISC_Log; |
| 8 |
|
| 9 |
/** |
| 10 |
* Render the caption. |
| 11 |
*/ |
| 12 |
class Caption 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 |
| 21 |
echo self::get( $image_id ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Render caption string / markup |
| 26 |
* including the style wrapper, if enabled in the plugin settings |
| 27 |
* |
| 28 |
* @param int $image_id id of the image. |
| 29 |
* @param string[] $data metadata. |
| 30 |
* @param array $args additional arguments |
| 31 |
* use "disable-links" = (any value), to disable any working links. |
| 32 |
* use "styled" = false to disable the style wrapper. |
| 33 |
* |
| 34 |
* @return string |
| 35 |
*/ |
| 36 |
public static function get( int $image_id, array $data = [], array $args = [] ) { |
| 37 |
$source = Image_Source_String::get( $image_id, $data, $args ); |
| 38 |
if ( ! $source ) { |
| 39 |
ISC_Log::log( sprintf( 'skipped overlay for empty sources string for ID "%s"', $image_id ) ); |
| 40 |
return ''; |
| 41 |
} |
| 42 |
|
| 43 |
// don’t render the caption for own images if the admin choose not to do so |
| 44 |
if ( Standard_Source::hide_standard_source_for_image( $image_id ) ) { |
| 45 |
ISC_Log::log( sprintf( 'skipped overlay for "own" image ID "%s"', $image_id ) ); |
| 46 |
return ''; |
| 47 |
} |
| 48 |
|
| 49 |
// add the prefix if not disabled |
| 50 |
if ( ! array_key_exists( 'prefix', $args ) || $args['prefix'] ) { |
| 51 |
$source = self::add_prefix( $source ); |
| 52 |
} |
| 53 |
|
| 54 |
// add style wrapper if not disabled |
| 55 |
if ( ! array_key_exists( 'styled', $args ) || $args['styled'] ) { |
| 56 |
$source = self::add_style( $source, $image_id ); |
| 57 |
} |
| 58 |
|
| 59 |
return $source; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Add style |
| 64 |
* |
| 65 |
* @param string $source Source string. |
| 66 |
* @param int $image_id Image ID. |
| 67 |
* @return string |
| 68 |
*/ |
| 69 |
public static function add_style( string $source, int $image_id ) { |
| 70 |
if ( self::has_caption_style() && apply_filters( 'isc_caption_apply_default_style', '__return_true' ) ) { |
| 71 |
$source = '<span class="isc-source-text">' . $source . '</span>'; |
| 72 |
} |
| 73 |
|
| 74 |
return apply_filters( 'isc_overlay_html_source', $source, $image_id ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Add pre-text |
| 79 |
* |
| 80 |
* @param string $source Source string. |
| 81 |
* @return string |
| 82 |
*/ |
| 83 |
public static function add_prefix( $source ) { |
| 84 |
$options = self::get_options(); |
| 85 |
|
| 86 |
if ( empty( $options['source_pretext'] ) ) { |
| 87 |
return $source; |
| 88 |
} |
| 89 |
|
| 90 |
return $options['source_pretext'] . ' ' . $source; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Check if the caption has a style in general |
| 95 |
* |
| 96 |
* @return bool |
| 97 |
*/ |
| 98 |
public static function has_caption_style(): bool { |
| 99 |
$style = self::get_options()['caption_style']; |
| 100 |
return $style !== 'none'; |
| 101 |
} |
| 102 |
} |
| 103 |
|