PluginProbe
Image Source Control Lite – Show Image Credits and Captions / trunk
Image Source Control Lite – Show Image Credits and Captions vtrunk
3.12.0 3.11.0 trunk 1.1 1.1.1 1.1.2 1.1.2.1 1.1.3 1.10 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.2 1.2.0.1 1.2.0.2 1.2.0.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.4.1 1.3.5 All 110 releases
image-source-control-isc / includes / image-sources / renderer / caption.php

caption.php in Image Source Control Lite – Show Image Credits and Captions trunk, at includes/image-sources/renderer/caption.php

127 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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