PluginProbe
Image Source Control Lite – Show Image Credits and Captions / 3.11.0
Image Source Control Lite – Show Image Credits and Captions v3.11.0
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
← All changes | public/public.php +686 -653 1.10.43.11.0 View file →
@@ -1,761 +1,794 @@
1 1 <?php
2 -//avoid direct calls to this file
3 -if (!function_exists('add_action')) {
4 - header('Status: 403 Forbidden');
5 - header('HTTP/1.1 403 Forbidden');
6 - exit();
7 -}
8 -if (!class_exists('ISC_Public')) {
9 2
10 - /**
11 - * handles all admin functionalities
12 - *
13 - * @since 1.7
14 - * @todo move frontend-only functions from general class here
15 - */
16 - class ISC_Public extends ISC_Class {
3 +use ISC\Standard_Source;
4 +use ISC\Helpers;
17 5
18 - public function __construct() {
19 - parent::__construct();
6 +/**
7 + * Handles all frontend facing functionalities
8 + *
9 + * @todo move frontend-only functions from general class here
10 + */
11 +class ISC_Public extends \ISC\Image_Sources\Image_Sources {
20 12
21 - add_action('wp_enqueue_scripts', array($this, 'front_scripts'));
22 - add_action('wp_head', array($this, 'front_head'));
13 + /**
14 + * Instance of ISC_Public
15 + *
16 + * @var $instance
17 + */
18 + protected static $instance = null;
23 19
24 - /**
25 - * filters need to be above 10 in order to interpret also gallery shortcode
26 - */
27 - add_filter('the_content', array($this, 'content_filter'), 20);
28 - add_filter('the_excerpt', array($this, 'excerpt_filter'), 20);
29 - add_shortcode('isc_list', array($this, 'list_post_attachments_with_sources_shortcode'));
30 - add_shortcode('isc_list_all', array($this, 'list_all_post_attachments_sources_shortcode'));
31 - }
20 + /**
21 + * ISC_Public constructor.
22 + */
23 + public function __construct() {
24 + parent::__construct();
32 25
33 - /**
34 - * Enqueue scripts for the front-end.
35 - */
36 - public function front_scripts() {
37 - // inject in footer as we only do stuff after dom-ready
38 - wp_enqueue_script('isc_front_js', plugins_url('/assets/js/front-js.js', __FILE__), array('jquery'), ISCVERSION, true);
39 - }
26 + add_action( 'wp', [ $this, 'register_hooks' ] );
27 + }
40 28
41 - /**
42 - * Front-end scripts in <head /> section.
43 - */
44 - public function front_head()
45 - {
46 - $options = $this->get_isc_options();
47 - ?>
48 - <script type="text/javascript">
49 - /* <![CDATA[ */
50 - var isc_front_data =
51 - {
52 - caption_position : '<?php echo $options['caption_position']; ?>',
53 - }
54 - /* ]]> */
55 - </script>
56 - <style>
57 - .isc-source { position: relative; }
58 - </style>
59 - <?php
60 - }
29 + /**
30 + * Register hooks after the page is set up so that we have access to the post ID.
31 + */
32 + public function register_hooks() {
33 + // register the shortcode for the global list. Since this shortcode has to be placed manually and is normally only used once, checking if ISC is disabled on cetain pages doesn’t make sense
34 + add_shortcode( 'isc_list_all', [ '\ISC\Image_Sources\Renderer\Global_List', 'execute_shortcode' ] );
61 35
62 - /**
63 - * add captions to post content and include source into caption, if this setting is enabled
64 - *
65 - * @param string $content post content
66 - * @return string $content
67 - *
68 - * @update 1.4.3
69 - */
70 - public function content_filter($content)
71 - {
36 + if ( ! self::can_load_image_sources() ) {
37 + return;
38 + }
72 39
73 - // display inline sources
74 - $options = $this->get_isc_options();
75 - if( isset($options['display_type']) && is_array($options['display_type']) && in_array('overlay', $options['display_type'] ) ) {
76 - /**
77 - * split content where `isc_stop_overlay` is found to not display overlays starting there
78 - */
79 - if( strpos( $content, 'isc_stop_overlay' ) ){
80 - list( $content, $content_after ) = explode( 'isc_stop_overlay', $content, 2 );
81 - } else {
82 - $content_after = '';
83 - }
40 + // prepare the log
41 + $this->prepare_log();
84 42
85 - /**
86 - * removed [caption], because this check runs after the hook that interprets shortcodes
87 - * img tag is checked individually since there is a different order of attributes when images are used in gallery or individually
88 - *
89 - * 0 – full match
90 - * 1 - <figure> if set
91 - * 2 – alignment
92 - * 3 – inner code starting with <a>
93 - * 4 – opening link attribute
94 - * 5 – "rel" attribute from link tag
95 - * 6 – image id from link wp-att- value in "rel" attribute
96 - * 7 – full img tag
97 - * 8 – image URL
98 - * 9 – (unused)
99 - * 10 - </figure>
100 - *
101 - * tested with:
102 - * * with and without [caption]
103 - * * with and without link attibute
104 - *
105 - * potential issues:
106 - * * line breaks in the code
107 - */
108 - $pattern = '#(<[^>]*class="[^"]*(alignleft|alignright|alignnone|aligncenter).*)?((<a [^>]*(rel="[^"]*[^"]*wp-att-(\d+)"[^>]*)>)? *(<img [^>]*[^>]*src="(.+)".*\/?>).*(</a>)??[^<]*).*(<\/figure.*>)?#isU';
109 - $count = preg_match_all($pattern, $content, $matches);
43 + if ( ISC\Image_Sources\Renderer\Caption::has_caption_style() ) {
44 + add_action( 'wp_enqueue_scripts', [ $this, 'front_scripts' ] );
45 + add_action( 'wp_head', [ $this, 'front_head' ] );
46 + }
110 47
111 - // error_log(print_r($content, true)); error_log(print_r($matches, true));
112 - if (false !== $count) {
113 - for ($i=0; $i < $count; $i++) {
48 + // Content filters need to be above 10 in order to interpret also gallery shortcode
49 + self::register_the_content_filters();
50 + add_filter( 'the_excerpt', [ $this, 'excerpt_filter' ], 20 );
51 + add_filter( 'render_block', [ $this, 'add_featured_image_source_to_excerpt_block' ], 10, 2 );
114 52
115 - /**
116 - * interpret the image tag
117 - *
118 - * we only need the ID if we don’t have it yet
119 - * it can be retrieved from "wp-image-" class (single) or "aria-describedby="gallery-1-34" in gallery
120 - */
121 - $id = $matches[6][$i];
122 - $img_tag = $matches[7][$i];
53 + add_shortcode( 'isc_list', [ $this, 'list_post_attachments_with_sources_shortcode' ] );
54 + }
123 55
124 - if( ! $id ){
125 - $success = preg_match('#wp-image-(\d+)|aria-describedby="gallery-1-(\d+)#is', $img_tag, $matches_id);
126 - if( $success ){
127 - $id = $matches_id[1] ? intval( $matches_id[1] ) : intval( $matches_id[2] );
128 - }
129 - }
56 + /**
57 + * Check if the current frontend page can run ISC properly
58 + *
59 + * @return bool
60 + */
61 + public static function can_load_image_sources() {
130 62
131 - // if ID is still missing get image by URL
132 - if( ! $id ){
133 - $src = $matches[8][$i];
134 - $id = $this->get_image_by_url($src);
135 - }
63 + $post_id = get_the_ID();
136 64
137 - // don’t show caption for own image if admin choose not to do so
138 - if($options['exclude_own_images']){
139 - if(get_post_meta($id, 'isc_image_source_own', true)) {
140 - continue;
141 - }
142 - }
65 + if ( $post_id
66 + && is_singular()
67 + /**
68 + * Filter posts that should not output ISC information by their ID
69 + *
70 + * @param int[] void WP_Post IDs.
71 + */
72 + && in_array( $post_id, apply_filters( 'isc_public_excluded_post_ids', [] ), true ) ) {
73 + return false;
74 + }
143 75
144 - // don’t display empty sources
145 - if( !$source_string = $this->render_image_source_string( $id ) ) {
146 - continue;
147 - }
76 + return apply_filters( 'isc_can_load', ISC\Plugin::is_module_enabled( 'image_sources' ) );
77 + }
148 78
149 - // get any alignment from the original code
150 - preg_match('#alignleft|alignright|alignnone|aligncenter#is', $matches[0][$i], $matches_align);
151 - $alignment = isset( $matches_align[0] ) ? $matches_align[0] : '';
79 + /**
80 + * Prepare the log file
81 + */
82 + public function prepare_log() {
83 + if ( ISC_Log::clear_log() ) {
84 + ISC_Log::delete_log_file();
85 + }
152 86
153 - $source = '<span class="isc-source-text">' . $options['source_pretext'] . ' ' . $source_string . '</span>';
154 - $old_content = $matches[3][$i];
155 - $new_content = str_replace('wp-image-' . $id, 'wp-image-' . $id . ' with-source', $old_content);
87 + if ( ISC_Log::enabled() ) {
88 + ISC_Log::log( '---' );
89 + }
90 + }
156 91
157 - $content = str_replace($old_content, '<div id="isc_attachment_' . $id . '" class="isc-source ' . $alignment . '"> ' . $new_content . $source . '</div>', $content);
92 + /**
93 + * Register our the_content filters
94 + */
95 + public static function register_the_content_filters() {
158 96
159 - }
160 - }
161 - /**
162 - * attach follow content back
163 - */
164 - $content = $content . $content_after;
165 - }
97 + // Content filters need to be above 10 in order to interpret also gallery shortcode
98 + // needs to be added to remove_the_content_filters() as well to prevent infinite loops
99 + add_filter( 'the_content', [ self::get_instance(), 'add_sources_to_content' ], 20 );
100 + }
166 101
167 - // attach image source list to content, if option is enabled
168 - if ((isset($options['list_on_archives']) && $options['list_on_archives']) ||
169 - (is_singular() && isset($options['display_type']) && is_array($options['display_type']) && in_array('list', $options['display_type']))) {
170 - $content = $content . $this->list_post_attachments_with_sources();
171 - }
102 + /**
103 + * Unregister our the_content filters
104 + * used in places where we want to prevent infinite loops
105 + */
106 + public static function remove_the_content_filters() {
107 + remove_filter( 'the_content', [ self::get_instance(), 'add_sources_to_content' ], 20 );
108 + }
172 109
173 - return $content;
174 - }
110 + /**
111 + * Get an instance of ISC_Public
112 + *
113 + * @return ISC_Public|null
114 + */
115 + public static function get_instance() {
116 + if ( null === self::$instance ) {
117 + self::$instance = new self();
118 + }
119 + return self::$instance;
120 + }
175 121
176 - /**
177 - * add image source of featured image to post excerpts
178 - *
179 - * @param string $excerpt post excerpt
180 - * @return string $excerpt
181 - *
182 - * @update 1.4.3
183 - */
184 - public function excerpt_filter($excerpt)
185 - {
122 + /**
123 + * Enqueue scripts for the front-end.
124 + */
125 + public function front_scripts() {
126 + // don’t add the script on AMP pages
127 + if ( self::is_amp() ) {
128 + return;
129 + }
130 + // inject in footer as we can only reliably position captions when the DOM is fully loaded
131 + Helpers::enqueue_script( 'isc_caption', 'public/assets/js/captions.js' );
186 132
187 - // display inline sources
188 - $options = $this->get_isc_options();
189 - $post = get_post();
133 + $options = $this->get_options();
190 134
191 - if (empty($options['list_on_excerpts'])) return $excerpt;
135 + $caption_style = [
136 + 'position' => 'absolute',
137 + 'font-size' => '0.9em',
138 + 'background-color' => '#333',
139 + 'color' => '#fff',
140 + 'opacity' => '0.70',
141 + 'padding' => '0 0.15em',
142 + 'text-shadow' => 'none',
143 + 'display' => 'block',
144 + ];
192 145
193 - $source_string = $this->get_thumbnail_source_string($post->ID);
146 + $front_data = [
147 + 'caption_position' => isset( $options['caption_position'] ) ? esc_html( $options['caption_position'] ) : '',
148 + /**
149 + * Filter: isc_public_caption_default_style
150 + * Allows to change the default caption style.
151 + *
152 + * @param array $caption_style The default caption style.
153 + * @param array $options The options array.
154 + */
155 + 'caption_style' => apply_filters( 'isc_public_caption_default_style', $caption_style, $options ),
156 + ];
194 157
195 - $excerpt = $excerpt . $source_string;
158 + /**
159 + * Filter: isc_public_caption_script_options
160 + *
161 + * @param array $front_data The data to be localized.
162 + * @param array $options The options array.
163 + */
164 + $filtered_front_data = apply_filters( 'isc_public_caption_script_options', $front_data, $options );
196 165
197 - return $excerpt;
198 - }
166 + wp_localize_script(
167 + 'isc_caption',
168 + 'isc_front_data',
169 + $filtered_front_data
170 + );
171 + }
199 172
200 - /**
201 - * create image sources list for all images of this post
202 - *
203 - * @since 1.0
204 - * @updated 1.1, 1.3.5
205 - * @updated 1.5 use new render function to create basic image source string
206 - *
207 - * @param int $post_id id of the current post/page
208 - * @return echo output
209 - */
210 - public function list_post_attachments_with_sources($post_id = 0)
211 - {
212 - global $post;
213 - if (empty($post_id)) {
214 - if (!empty($post->ID)) {
215 - $post_id = $post->ID;
216 - } else {
217 - return;
218 - }
219 - }
173 + /**
174 + * Front-end scripts in <head /> section.
175 + */
176 + public function front_head() {
177 + // don’t add the script on AMP pages
178 + if ( self::is_amp() ) {
179 + return;
180 + }
220 181
221 - $attachments = get_post_meta($post_id, 'isc_post_images', true);
182 + $options = $this->get_options();
183 + ?>
184 + <style>
185 + .isc-source { position: relative; display: inline-block; line-height: initial; }
186 + /* Hides the caption initially until it is positioned via JavaScript */
187 + .isc-source > .isc-source-text { display: none; }
188 + .wp-block-cover .isc-source { position: static; }
189 + <?php
190 + // The 2022 theme adds display:block to the featured image block, which creates additional line breaks. `display: inline` fixes that.
191 + ?>
192 + span.isc-source-text a { display: inline; color: #fff; }
193 + <?php
194 + // force the overlay caption into the bottom left corner for lightboxes introduced in WP 6.4; only applied to the positions that are currently broken
195 + if ( in_array( $options['caption_position'], [ 'top-center', 'top-right', 'center' ], true ) ) {
196 + ?>
197 + .wp-lightbox-overlay.active .isc-source-text { top: initial !important; left: initial !important; bottom: 0! important; }
198 + <?php
199 + }
200 + ?>
201 + </style>
202 + <?php
203 + }
222 204
223 - // if attachments is an empty string, search for images in it
224 - if ($attachments == '') {
225 - // unregister our content filter in order to prevent infinite loops when calling the_content in the next steps
226 - remove_filter( 'the_content', array( $this, 'content_filter' ), 20 );
205 + /**
206 + * Find images in the content, create the index, and maybe add sources to it
207 + *
208 + * @param string $content post content.
209 + * @return string $content
210 + */
211 + public function add_sources_to_content( $content ) {
212 + // return if content is empty or null (the latter being an actual issue on a user’s site likely caused by a third-party plugin using the the_content filter wrongly)
213 + if ( empty( $content ) ) {
214 + ISC_Log::log( 'skipped adding sources because the content was empty' );
215 + return $content;
216 + }
227 217
228 - $this->save_image_information_on_load();
229 - $this->update_image_posts_meta($post_id, $post->post_content);
218 + if ( ISC\Indexer::is_global_list_page( $content ) ) {
219 + ISC_Log::log( 'skipped adding sources because the content contains the Global List' );
220 + return $content;
221 + }
230 222
231 - $attachments = get_post_meta($post_id, 'isc_post_images', true);
232 - }
223 + // return if this is not the main query or within the loop
224 + if ( ! self::is_main_loop() ) {
225 + ISC_Log::log( 'skipped adding sources because the content was loaded outside the main loop' );
226 + return $content;
227 + }
233 228
234 - $return = '';
235 - if (!empty($attachments)) {
236 - $atts = array();
237 - foreach ($attachments as $attachment_id => $attachment_array) {
229 + if ( ISC_Log::is_type( 'backtrace' ) ) {
230 + ISC_Log::log_stack_trace();
231 + }
238 232
239 - $own = get_post_meta($attachment_id, 'isc_image_source_own', true);
240 - $source = get_post_meta($attachment_id, 'isc_image_source', true);
233 + // maybe add source captions
234 + if ( self::captions_enabled() && apply_filters( 'isc_public_add_source_captions_to_content', true ) ) {
235 + $content = self::add_source_captions_to_content( $content );
236 + } else {
237 + ISC_Log::log( 'not creating image overlays because the option is disabled for post content' );
238 + }
241 239
242 - // check if source of own images can be displayed
243 - if ( ($own == '' && $source == '' ) || ($own != '' && $this->_options['exclude_own_images'])) {
244 - unset($atts[$attachment_id]);
245 - continue;
246 - } else {
247 - $atts[$attachment_id]['title'] = get_the_title($attachment_id);
248 - $atts[$attachment_id]['source'] = $this->render_image_source_string($attachment_id);
249 - }
240 + /**
241 + * Indexing the content for images here after we added overlays, so that we could also count
242 + * non-images with overlays
243 + */
244 + if ( apply_filters( 'isc_update_indexes_in_the_content', true ) ) {
245 + \ISC\Indexer::update_indexes( $content );
246 + }
250 247
251 - }
248 + /**
249 + * The sources list is rendered after indexing the content for images
250 + * since it is build on top of it
251 + */
252 + // maybe add source list
253 + return self::add_source_list_to_content( $content );
254 + }
252 255
253 - $return = $this->render_attachments($atts);
254 - }
256 + /**
257 + * Check main loop
258 + *
259 + * @return bool true if we are currently on something that could be called the "main loop"
260 + */
261 + public static function is_main_loop() {
255 262
256 - return $return;
257 - }
263 + // Exception: Oxygen builder, where `is_the_loop()` is false
264 + if ( defined( 'CT_VERSION' ) && defined( 'CT_FW_PATH' ) ) {
265 + return true;
266 + } elseif ( self::is_amp_reader_mode() && is_single() ) {
267 + // Exception: AMP reader mode and the AMPforWP plugin need this extra check
268 + return true;
269 + } elseif ( in_the_loop() && is_main_query() ) {
270 + return true;
271 + }
258 272
259 - /**
260 - * render attachment list
261 - *
262 - * @param array $attachments
263 - * @updated 1.3.5
264 - * @updated 1.5 removed rendering the license to an earlier function
265 - */
266 - protected function render_attachments($attachments)
267 - {
268 - // don't display anything, if no image sources displayed
269 - if ($attachments == array()) {
270 - return ;
271 - }
273 + return false;
274 + }
272 275
276 + /**
277 + * Add captions to post content and include source into caption, if this setting is enabled
278 + *
279 + * @param string $content post content.
280 + * @return string $content
281 + */
282 + public function add_source_captions_to_content( $content ) {
283 + ISC_Log::log( 'start creating source overlays' );
273 284
274 - $options = $this->get_isc_options();
285 + /**
286 + * Split content where `isc_stop_overlay` is found to not display overlays starting there
287 + */
288 + if ( strpos( $content, 'isc_stop_overlay' ) !== false ) {
289 + list( $content, $content_after ) = explode( 'isc_stop_overlay', $content, 2 );
290 + } else {
291 + $content_after = '';
292 + }
275 293
276 - ob_start();
277 - $headline = $this->_options['image_list_headline'];
278 - ?><div class="isc_image_list_box"><?php
279 - printf('<p class="isc_image_list_title">%1$s</p>', $headline); ?>
280 - <ul class="isc_image_list"><?php
294 + $content = apply_filters( 'isc_public_caption_regex_content', $content );
295 + $matches = $this->html_analyzer->extract_images_from_html( $content );
281 296
282 - foreach ($attachments as $atts_id => $atts_array) {
283 - if (empty($atts_array['source'])) {
284 - continue;
285 - }
286 - printf('<li>%1$s: %2$s</li>', $atts_array['title'], $atts_array['source']);
287 - }
288 - ?></ul></div><?php
289 - return ob_get_clean();
290 - }
297 + ISC_Log::log( 'embedded images found: ' . count( $matches ) );
291 298
292 - /**
293 - * shortcode function to list all image sources
294 - * @param arr $atts
295 - */
296 - public function list_post_attachments_with_sources_shortcode($atts = array())
297 - {
298 - global $post;
299 - extract(shortcode_atts(array('id' => 0), $atts));
299 + if ( ! count( $matches ) ) {
300 + return $content . $content_after;
301 + }
300 302
301 - // if $id not set, use the current ID from the post
302 - if (empty($id)) {
303 - $id = $post->ID;
304 - }
303 + // gather elements already replaced to prevent duplicate sources, see GitHub #105
304 + $replaced = [];
305 305
306 - if (empty($id)) {
307 - return;
308 - }
309 - return $this->list_post_attachments_with_sources($id);
310 - }
306 + foreach ( $matches as $_match ) {
307 + if ( ! $_match['img_src'] ) {
308 + ISC_Log::log( 'skipped an image because src is empty' );
309 + continue;
310 + }
311 + $hash = md5( $_match['inner_code'] );
311 312
312 - /**
313 - * create shortcode to list all image sources in the frontend
314 - * @param array $atts
315 - * @since 1.1.3
316 - */
317 - public function list_all_post_attachments_sources_shortcode($atts = array())
318 - {
319 - extract(shortcode_atts(array(
320 - 'per_page' => 99999,
321 - 'before_links' => '',
322 - 'after_links' => '',
323 - 'prev_text' => '&#171; Previous',
324 - 'next_text' => 'Next &#187;',
325 - 'included' => 'displayed'
326 - ),
327 - $atts));
313 + if ( in_array( $hash, $replaced, true ) ) {
314 + ISC_Log::log( 'skipped an image because it appears multiple times' );
315 + continue;
316 + } else {
317 + $replaced[] = $hash;
318 + }
328 319
329 - if ('&#171; Previous' == $prev_text)
330 - $prev_text = __('&#171; Previous', 'image-source-control-isc');
331 - if ('Next &#187;' == $next_text)
332 - $next_text = __('Next &#187;', 'image-source-control-isc');
320 + $id = $this->html_analyzer->extract_image_id( $_match['inner_code'] );
333 321
334 - // retrieve all attachments
335 - $args = array(
336 - 'post_type' => 'attachment',
337 - 'numberposts' => -1,
338 - 'post_status' => null,
339 - 'post_parent' => null
340 - );
322 + // if ID is still missing get the image by URL
323 + if ( ! $id ) {
324 + $id = ISC_Model::get_image_by_url( $_match['img_src'] );
325 + ISC_Log::log( sprintf( 'ID for source "%s": "%s"', $_match['img_src'], $id ) );
326 + }
341 327
342 - // check mode
343 - if( $included === 'all' ){
344 - // load all images
328 + $source = ISC\Image_Sources\Renderer\Caption::get( $id );
345 329
346 - } else { // load only images attached to posts
347 - $args['meta_query'] = array(
348 - array(
349 - 'key' => 'isc_image_posts',
350 - 'value' => 'a:0:{}',
351 - 'compare' => '!='
352 - )
353 - );
354 - }
330 + if ( ! $source ) {
331 + ISC_Log::log( sprintf( 'skipped empty sources string for ID "%s"', $id ) );
332 + continue;
333 + }
355 334
356 - $attachments = get_posts($args);
357 - if (empty($attachments)) {
358 - return;
359 - }
335 + // get any alignment from the original code
336 + preg_match( '#alignleft|alignright|alignnone|aligncenter#is', $_match['full'], $matches_align );
337 + $alignment = $matches_align[0] ?? '';
338 + $old_content = $_match['inner_code'];
339 + $markup_before = '';
340 + $markup_after = '';
360 341
361 - $options = $this->get_isc_options();
342 + // default style
343 + if ( ISC\Image_Sources\Renderer\Caption::has_caption_style() ) {
344 + $markup_before = '<span id="isc_attachment_' . $id . '" class="isc-source ' . $alignment . '">';
345 + $markup_after = '</span>';
346 + }
362 347
363 - $connected_atts = array();
348 + $content = str_replace(
349 + $old_content,
350 + sprintf(
351 + '%s%s%s%s',
352 + apply_filters( 'isc_overlay_html_markup_before', $markup_before, $id ),
353 + str_replace( 'wp-image-' . $id, 'wp-image-' . $id . ' with-source', $old_content ), // new content
354 + $source,
355 + apply_filters( 'isc_overlay_html_markup_after', $markup_after, $id )
356 + ),
357 + $content
358 + );
359 + }
360 + ISC_Log::log( 'number of unique images found: ' . count( $replaced ) );
364 361
365 - foreach ($attachments as $_attachment) {
366 - $connected_atts[$_attachment->ID]['source'] = get_post_meta($_attachment->ID, 'isc_image_source', true);
367 - $connected_atts[$_attachment->ID]['own'] = get_post_meta($_attachment->ID, 'isc_image_source_own', true);
368 - // jump to next element if author images are not to be included in the list
369 - if($options['exclude_own_images'] && '' != $connected_atts[$_attachment->ID]['own']) {
370 - unset($connected_atts[$_attachment->ID]);
371 - continue;
372 - }
362 + /**
363 + * Attach follow content back
364 + */
365 + return $content . $content_after;
366 + }
373 367
374 - $connected_atts[$_attachment->ID]['title'] = $_attachment->post_title;
375 - $connected_atts[$_attachment->ID]['author_name'] = '';
376 - if ('' != $connected_atts[$_attachment->ID]['own']) {
377 - $connected_atts[$_attachment->ID]['author_name'] = get_the_author_meta('display_name', $_attachment->post_author);
378 - }
368 + /**
369 + * Return true if captions are enabled
370 + *
371 + * @return bool
372 + */
373 + public static function captions_enabled(): bool {
374 + $options = self::get_instance()->get_options();
375 + return ! empty( $options['display_type'] ) && is_array( $options['display_type'] ) && in_array( 'overlay', $options['display_type'], true );
376 + }
379 377
380 - $metadata = get_post_meta($_attachment->ID, 'isc_image_posts', true);
381 - $usage_data = '';
378 + /**
379 + * Add the Per-page list if the option is enabled
380 + *
381 + * @param string $content post content.
382 + * @return string $content
383 + */
384 + public function add_source_list_to_content( $content ) {
382 385
383 - if (is_array($metadata) && array() != $metadata) {
384 - $usage_data .= "<ul style='margin: 0;'>";
385 - $usage_data_array = array();
386 - foreach($metadata as $data) {
387 - // only list published posts
388 - if(get_post_status($data) == 'publish') {
389 - $usage_data_array[] = sprintf(__('<li><a href="%1$s" title="View %2$s">%3$s</a></li>', 'image-source-control-isc'),
390 - esc_url(get_permalink($data)),
391 - esc_attr(get_the_title($data)),
392 - esc_html(get_the_title($data))
393 - );
394 - }
395 - }
396 - if ( 'all' !== $included && $usage_data_array === array() ) {
397 - unset($connected_atts[$_attachment->ID]);
398 - continue;
399 - }
400 - $usage_data .= implode( '', $usage_data_array );
401 - $usage_data .= "</ul>";
402 - }
386 + /**
387 + * Display the Per-page list
388 + * on single pages if the following option is enabled: How to display source in Frontend > list below content
389 + * on archive pages and home pages with posts if the following option is enabled: Archive Pages > Display sources list below full posts
390 + */
391 + if ( $this->can_add_list_to_content() ) {
392 + ISC_Log::log( 'start creating source list below content' );
393 + $content = $content . $this->list_post_attachments_with_sources();
394 + }
403 395
404 - $connected_atts[$_attachment->ID]['posts'] = $usage_data;
405 - }
396 + return $content;
397 + }
406 398
407 - $total = count($connected_atts);
399 + /**
400 + * Add image source of featured image to post excerpts
401 + *
402 + * @param string $excerpt post excerpt.
403 + * @return string $excerpt
404 + *
405 + * @update 1.4.3
406 + */
407 + public function excerpt_filter( $excerpt ) {
408 408
409 - if (0 == $total)
410 - return;
409 + // display inline sources
410 + $options = $this->get_options();
411 + $post = get_post();
411 412
412 - $page = isset($_GET['isc-page']) ? intval($_GET['isc-page']) : 1;
413 - $down_limit = 1; // First page
413 + if ( empty( $options['list_on_excerpts'] ) ) {
414 + return $excerpt;
415 + }
414 416
415 - $up_limit = 1;
417 + return $excerpt . $this->get_thumbnail_source_string( $post->ID );
418 + }
416 419
417 - if ($per_page < $total) {
418 - $rem = $total % $per_page; // The Remainder of $total/$per_page
419 - $up_limit = ($total - $rem) / $per_page;
420 - if (0 < $rem) {
421 - $up_limit++; //If rem is positive, add the last page that contains less than $per_page attachment;
422 - }
423 - }
420 + /**
421 + * Add image source of featured image to the post excerpt block in the frontend
422 + *
423 + * @param string $block_content rendered content of the block.
424 + * @param array $block full block details.
425 + * @return string $excerpt
426 + */
427 + public function add_featured_image_source_to_excerpt_block( $block_content, $block ) {
424 428
425 - ob_start();
426 - if ( 2 > $up_limit ) {
427 - $this->display_all_attachment_list($connected_atts);
428 - } else {
429 - $starting_atts = $per_page * ($page - 1); // for page 2 and 3 $per_page start display on $connected_atts[3*(2-1) = 3]
430 - $paged_atts = array_slice($connected_atts, $starting_atts, $per_page, true);
431 - $this->display_all_attachment_list($paged_atts);
432 - $this->pagination_links($up_limit, $before_links, $after_links, $prev_text, $next_text);
433 - }
434 - if (isset($options['webgilde']) && true == $options['webgilde']) {
435 - ?>
436 - <p class="isc-backlink"><?php printf(__('Image list created by <a href="%s" title="Image Source Control">Image Source Control Plugin</a>', 'image-source-control-isc'), WEBGILDE); ?></p>
437 - <?php
438 - }
429 + if ( isset( $block['blockName'] ) && $block['blockName'] !== 'core/post-excerpt' ) {
430 + return $block_content;
431 + }
439 432
440 - $output = ob_get_clean();
441 - return $output;
442 - }
433 + $options = $this->get_options();
434 + if ( empty( $options['list_on_excerpts'] ) ) {
435 + return $block_content;
436 + }
443 437
438 + $post = get_post();
439 + if ( ! $post || empty( $post->ID ) ) {
440 + return $block_content;
441 + }
444 442
445 - /**
446 - * performs rendering of all attachments list
447 - * @since 1.1.3
448 - * @update 1.5 added new method to get source
449 - */
450 - public function display_all_attachment_list($atts)
451 - {
452 - if (!is_array($atts) || $atts == array())
453 - return;
454 - $options = $this->get_isc_options();
443 + return str_replace(
444 + '</p></div>',
445 + $this->get_thumbnail_source_string( $post->ID ) . '</p></div>',
446 + $block_content
447 + );
448 + }
455 449
456 - /**
457 - * added comment `isc_stop_overlay` as a class to the table to suppress overlays within it starting at that point
458 - * todo: allow overlays to start again after the table
459 - *
460 - */
461 - ?><div class="isc_all_image_list_box isc_stop_overlay">
462 - <table>
463 - <thead>
464 - <?php if ($options['thumbnail_in_list']) : ?>
465 - <th><?php _e('Thumbnail', 'image-source-control-isc'); ?></th>
466 - <?php endif; ?>
467 - <th><?php _e("Attachment's ID", 'image-source-control-isc'); ?></th>
468 - <th><?php _e('Title', 'image-source-control-isc'); ?></th>
469 - <th><?php _e('Attached to', 'image-source-control-isc'); ?></th>
470 - <th><?php _e('Source', 'image-source-control-isc'); ?></th>
471 - </thead>
472 - <tbody>
473 - <?php foreach ($atts as $id => $data) : ?>
474 - <?php
475 - $source = $this->render_image_source_string($id);
476 - ?>
477 - <tr>
478 - <?php
479 - $v_align = '';
480 - if ($options['thumbnail_in_list']) :
481 - $v_align = 'style="vertical-align: top;"';
482 - ?>
483 - <?php if ('custom' != $options['thumbnail_size']) : ?>
484 - <td><?php echo wp_get_attachment_image($id, $options['thumbnail_size']); ?></td>
485 - <?php else : ?>
486 - <td><?php echo wp_get_attachment_image($id, array($options['thumbnail_width'], $options['thumbnail_height'])); ?></td>
487 - <?php endif; ?>
488 - <?php endif; ?>
489 - <td <?php echo $v_align;?>><?php echo $id; ?></td>
490 - <td <?php echo $v_align;?>><?php echo $data['title']; ?></td>
491 - <td <?php echo $v_align;?>><?php echo $data['posts']; ?></td>
492 - <td <?php echo $v_align;?>><?php echo $source; ?></td>
493 - </tr>
494 - <?php endforeach; ?>
495 - </tbody>
496 - </table></div>
497 - <?php
498 - }
450 + /**
451 + * Create image sources list for all images of this post
452 + *
453 + * @since 1.0
454 + * @updated 1.1, 1.3.5
455 + * @updated 1.5 use new render function to create basic image source string
456 + *
457 + * @param integer $post_id id of the current post/page.
458 + * @return string output
459 + */
460 + public function list_post_attachments_with_sources( $post_id = 0 ) {
461 + global $post;
499 462
500 - /**
501 - * Render pagination links, use $before_links and after_links to wrap pagination links inside an additional block
502 - * @param int $max_page total page count
503 - * @param string $before_links optional html to display before pagination links
504 - * @param string $after_links optional html to display after pagination links
505 - * @param string $prev_text text for the previous page link
506 - * @param string $next_text text for the next page link
507 - * @since 1.1.3
508 - *
509 - */
510 - public function pagination_links($max_page, $before_links, $after_links, $prev_text, $next_text)
511 - {
512 - if ((!isset($max_page)) || (!isset($before_links)) || (!isset($after_links)) || (!isset($prev_text)) || (!isset($next_text)))
513 - return;
514 - if (!empty($before_links))
515 - echo $before_links;
516 - ?>
517 - <div class="isc-paginated-links">
518 - <?php
519 - $page = isset($_GET['isc-page']) ? intval($_GET['isc-page']) : 1;
520 - if ($max_page < $page) {
521 - $page = $max_page;
522 - }
523 - if ($page < 1) {
524 - $page = 1;
525 - }
526 - $min_page = 1;
527 - $backward_distance = $page - $min_page;
528 - $forward_distance = $max_page - $page;
463 + if ( empty( $post_id ) && ! empty( $post->ID ) ) {
464 + $post_id = $post->ID;
465 + }
529 466
530 - $page_link = get_page_link();
467 + if ( isset( $_SERVER['REQUEST_URI'] ) ) {
468 + ISC_Log::log( 'enter list_post_attachments_with_sources() for ' . $_SERVER['REQUEST_URI'] . ' and post_id ' . $post_id );
469 + }
531 470
532 - /**
533 - * Remove the query_string of the page_link (?page_id=xyz for the standard permalink structure),
534 - * which is already captured in $_SERVER['QUERY_STRING'].
535 - * @todo replace regex with other value (does WP store the url path without attributes somewhere?
536 - * >get_page_link() returns the permalink but for the default WP permalink structure, the permalink looks like "http://domain.tld/?p=52", while $_GET
537 - * still has a field named 'page_id' with the same value of 52.
538 - */
471 + // don’t do anything on REST requests since that causes issues with the block editor rendering a "post" for each image
472 + // just in case, we also prevent output for "content" from attachments
473 + if ( defined( 'REST_REQUEST' ) || ! isset( $post->post_type ) || 'attachment' === $post->post_type ) {
474 + ISC_Log::log( 'exit because of invalid request' );
475 + return '';
476 + }
539 477
540 - $pos = strpos($page_link, '?');
541 - if (false !== $pos) {
542 - $page_link = substr($page_link, 0, $pos);
543 - }
478 + // do not render an empty source list on non-post pages unless explicitly stated.
479 + if ( empty( $post_id ) ) {
480 + /**
481 + * Filter: isc_source_list_empty_output
482 + * allow to return some output even if there is no post ID (e.g., on archive pages).
483 + */
484 + return apply_filters( 'isc_source_list_empty_output', '' );
485 + }
544 486
545 - /**
546 - * Unset the actual "$_GET['isc-page']" variable (if is set). Pagination variable will be appended to the new query string with a different value for each
547 - * pagination link.
548 - */
487 + // allow developers to override the output of the sources list
488 + $override = apply_filters( 'isc_sources_list_override_output', false, $post_id );
489 + if ( $override ) {
490 + ISC_Log::log( 'exit because override was set' );
491 + return $override;
492 + }
549 493
550 - if (isset($_GET['isc-page'])) {
551 - unset($_GET['isc-page']);
552 - }
494 + $attachments = get_post_meta( $post_id, 'isc_post_images', true );
495 + $exclude_standard = Standard_Source::standard_source_is( 'exclude' );
553 496
554 - $query_string = http_build_query($_GET);
497 + if ( ! empty( $attachments ) && is_array( $attachments ) ) {
498 + ISC_Log::log( sprintf( 'going through %d attachments', count( $attachments ) ) );
499 + $atts = [];
500 + foreach ( $attachments as $attachment_id => $attachment_array ) {
501 + $image_uses_standard_source = Standard_Source::use_standard_source( $attachment_id );
502 + $source = ISC\Image_Sources\Image_Sources::sanitize_source_html( self::get_image_source_text_raw( $attachment_id ) );
555 503
556 - $isc_query_tag = '';
557 - if (empty($query_string)) {
558 - $isc_query_tag = '?isc-page=';
559 - } else {
560 - $query_string = '?' . $query_string;
561 - $isc_query_tag = '&isc-page=';
562 - }
504 + // check if source of own images can be displayed
505 + if ( ( ! $image_uses_standard_source && $source === '' ) || ( $image_uses_standard_source && $exclude_standard ) ) {
506 + if ( $image_uses_standard_source && $exclude_standard ) {
507 + ISC_Log::log( sprintf( 'image %d: "own" sources are excluded', $attachment_id ) );
508 + } else {
509 + ISC_Log::log( sprintf( 'image %d: skipped because of empty source', $attachment_id ) );
510 + }
511 + unset( $atts[ $attachment_id ] );
512 + } else {
513 + $atts[ $attachment_id ]['title'] = get_the_title( $attachment_id );
514 + ISC_Log::log( sprintf( 'image %d: getting title "%s"', $attachment_id, $atts[ $attachment_id ]['title'] ) );
515 + $atts[ $attachment_id ]['source'] = ISC\Image_Sources\Renderer\Image_Source_String::get( $attachment_id );
516 + if ( ! $atts[ $attachment_id ]['source'] ) {
517 + ISC_Log::log( sprintf( 'image %d: skipped because of empty standard source', $attachment_id ) );
518 + unset( $atts[ $attachment_id ] );
519 + }
520 + }
521 + }
563 522
564 - if ($min_page != $page) {
565 - ?>
566 - <a href="<?php echo $page_link . $query_string . $isc_query_tag . ($page-1); ?>" class="prev page-numbers"><?php echo $prev_text; ?></a>
567 - <?php
568 - }
523 + return $this->render_attachments( $atts );
524 + } else {
525 + // see description above.
526 + ISC_Log::log( 'exit without any images found ' );
527 + // allow to return result if the source list is empty.
528 + return apply_filters( 'isc_source_list_empty_output', '' );
529 + }
530 + }
569 531
570 - if (5 < $max_page) {
532 + /**
533 + * Render attachment list
534 + *
535 + * @updated 1.3.5
536 + * @updated 1.5 removed rendering the license to an earlier function
537 + * @param array $attachments array of attachments.
538 + * @return string
539 + */
540 + public function render_attachments( $attachments ) {
541 + ISC_Log::log( 'start to render attachments list' );
571 542
572 - if (3 < $backward_distance) {
573 - ?>
574 - <a href="<?php echo $page_link . $query_string . $isc_query_tag; ?>1" class="page-numbers">1</a>
575 - <span class="page-numbers dots">...</span>
576 - <a href="<?php echo $page_link . $query_string . $isc_query_tag . ($page-2);?>" class="page-numbers"><?php echo $page-2; ?></a>
577 - <a href="<?php echo $page_link . $query_string . $isc_query_tag . ($page-1);?>" class="page-numbers"><?php echo $page-1; ?></a>
578 - <span class="page-numbers current"><?php echo $page; ?></span>
579 - <?php
580 - } else {
581 - for ($i = 1; $i <= $page; $i++) {
582 - if ($i == $page) {
583 - ?>
584 - <span class="page-numbers current"><?php echo $i; ?></span>
585 - <?php
586 - } else {
587 - ?>
588 - <a href="<?php echo $page_link . $query_string . $isc_query_tag . $i;?>" class="page-numbers"><?php echo $i; ?></a>
589 - <?php
590 - }
591 - }
592 - }
543 + // don't display anything, if no image sources displayed
544 + if ( $attachments === [] ) {
545 + ISC_Log::log( 'exit due to missing attachments' );
546 + return '';
547 + }
593 548
594 - if (3 < $forward_distance) {
595 - ?>
596 - <a href="<?php echo $page_link . $query_string . $isc_query_tag . ($page+1);?>" class="page-numbers"><?php echo $page+1; ?></a>
597 - <a href="<?php echo $page_link . $query_string . $isc_query_tag . ($page+2);?>" class="page-numbers"><?php echo $page+2; ?></a>
598 - <span class="page-numbers dots">...</span>
599 - <a href="<?php echo $page_link . $query_string . $isc_query_tag . $max_page;?>" class="page-numbers"><?php echo $max_page; ?></a>
600 - <?php
601 - } else {
602 - for ($i = $page+1; $i <= $max_page; $i++) {
603 - ?>
604 - <a href="<?php echo $page_link . $query_string . $isc_query_tag . $i;?>" class="page-numbers"><?php echo $i; ?></a>
605 - <?php
606 - }
607 - }
608 - } else {
609 - for ($i = 1; $i <= $max_page; $i++) {
610 - if ($i == $page) {
611 - ?>
612 - <span class="page-numbers current"><?php echo $i; ?></span>
613 - <?php
614 - } else {
615 - ?>
616 - <a href="<?php echo $page_link . $query_string . $isc_query_tag . $i;?>" class="page-numbers"><?php echo $i; ?></a>
617 - <?php
618 - }
619 - }
620 - }
621 - if ($page != $max_page) {
622 - ?>
623 - <a href="<?php echo $page_link . $query_string . $isc_query_tag . ($page+1);?>" class="next page-numbers"><?php echo $next_text; ?></a>
624 - <?php
625 - }
626 - ?>
627 - </div>
628 - <?php
629 - echo $after_links;
630 - }
549 + ob_start();
631 550
632 - /**
633 - * get source string of a feature image
634 - *
635 - * @since 1.8
636 - * @param $post_id
637 - * @return source string
638 - */
639 - function get_thumbnail_source_string($post_id = 0){
551 + ?>
552 + <ul class="isc_image_list">
553 + <?php
640 554
641 - if(empty($post_id)) return '';
555 + ISC_Log::log( sprintf( 'start listing %d attachments', count( $attachments ) ) );
642 556
643 - $options = $this->get_isc_options();
557 + foreach ( $attachments as $atts_id => $atts_array ) {
558 + if ( empty( $atts_array['source'] ) ) {
559 + ISC_Log::log( sprintf( 'skip image %d because of empty source', $atts_id ) );
560 + continue;
561 + }
562 + echo apply_filters(
563 + 'isc_source_list_line',
564 + sprintf( '<li>%1$s: %2$s</li>', $atts_array['title'], $atts_array['source'] ),
565 + $atts_id,
566 + $atts_array,
567 + $attachments
568 + );
569 + }
570 + ?>
571 + </ul>
572 + <?php
573 + $output = apply_filters( 'isc_source_list', ob_get_clean() );
644 574
645 - if(has_post_thumbnail($post_id)){
646 - $id = get_post_thumbnail_id($post_id);
647 - $thumb = get_post($post_id);
575 + return $this->render_image_source_box( $output );
576 + }
648 577
649 - // don’t show caption for own image if admin choose not to do so
650 - if($options['exclude_own_images']){
651 - if(get_post_meta($id, 'isc_image_source_own', true)) return '';
652 - }
653 - // don’t display empty sources
654 - $src = $thumb->guid;
655 - if(!$source_string = $this->render_image_source_string($id)) return '';
578 + /**
579 + * Shortcode function to list all image sources of a given page (per-page list)
580 + *
581 + * @param array $atts attributes.
582 + * @return string
583 + */
584 + public function list_post_attachments_with_sources_shortcode( $atts = [] ) {
585 + global $post;
656 586
657 - return '<p class="isc-source-text">' . $options['source_pretext'] . ' ' . $source_string . '</p>';
658 - }
587 + ISC_Log::log( 'enter for [isc_list] shortcode' );
659 588
660 - return '';
661 - }
589 + // hotfix for https://github.com/webgilde/image-source-control/issues/48 to prevent loops
590 + if ( defined( 'REST_REQUEST' ) ) {
591 + ISC_Log::log( 'exit due to calling through REST_REQUEST' );
592 + return '';
593 + }
662 594
663 - /**
664 - * load an image source string by url
665 - *
666 - * @updated 1.5
667 - * @deprecated since 1.9
668 - * @param string $url url of the image
669 - * @return type
670 - */
671 - public function get_source_by_url($url)
672 - {
673 - // get the id by the image source
674 - $id = $this->get_image_by_url($url);
595 + $a = shortcode_atts( [ 'id' => 0 ], $atts );
675 596
676 - return $this->render_image_source_string($id);
597 + // if $id not set, use the current ID from the post
598 + if ( ! $a['id'] && isset( $post->ID ) ) {
599 + $a['id'] = $post->ID;
600 + }
677 601
678 - }
602 + return $this->list_post_attachments_with_sources( $a['id'] );
603 + }
679 604
680 - /**
681 - * render source string of single image by its id
682 - * this only returns the string with source and license (and urls),
683 - * but no wrapping, because the string is used in a lot of functions
684 - * (e.g. image source list where title is prepended)
685 - *
686 - * @updated 1.5 wrapped source into source url
687 - *
688 - * @param int $id id of the image
689 - * @return bool|string false if no source was given, else string with source
690 - */
691 - public function render_image_source_string($id){
692 - $id = absint($id);
605 + /**
606 + * Get source string of a feature image
607 + *
608 + * @param integer $post_id post object ID.
609 + *
610 + * @return string source
611 + */
612 + public function get_thumbnail_source_string( int $post_id = 0 ): string {
613 + if ( empty( $post_id ) || ! has_post_thumbnail( $post_id ) ) {
614 + return '';
615 + }
693 616
694 - $options = $this->get_isc_options();
617 + $id = get_post_thumbnail_id( $post_id );
618 + $source_string = ISC\Image_Sources\Renderer\Caption::get(
619 + $id,
620 + [],
621 + [
622 + 'prefix' => false,
623 + 'styled' => false,
624 + ]
625 + );
626 + if ( ! $source_string ) {
627 + return '';
628 + }
695 629
696 - $metadata['source'] = get_post_meta($id, 'isc_image_source', true);
697 - $metadata['source_url'] = get_post_meta($id, 'isc_image_source_url', true);
698 - $metadata['own'] = get_post_meta($id, 'isc_image_source_own', true);
699 - $metadata['licence'] = get_post_meta($id, 'isc_image_licence', true);
630 + return '<p class="isc-source-text">' . apply_filters( 'isc_featured_image_source_pre_text', _x( 'Featured image: ', 'label of the featured image source, when displayed below post excerpts', 'image-source-control-isc' ) ) . ' ' . $source_string . '</p>';
631 + }
700 632
701 - $source = '';
633 + /**
634 + * Load an image source string by url
635 + *
636 + * @updated 1.5
637 + * @deprecated since 1.9
638 + * @param string $url url of the image.
639 + * @return string
640 + */
641 + public function get_source_by_url( $url ) {
642 + // get the id by the image source
643 + $id = ISC_Model::get_image_by_url( $url );
702 644
703 - $att_post = get_post($id);
645 + _deprecated_function( __METHOD__, '1.9.0' );
704 646
705 - if ('' != $metadata['own']) {
706 - if ($this->_options['use_authorname']) {
707 - if (!empty($att_post)) {
708 - $source = get_the_author_meta('display_name', $att_post->post_author);
709 - }
710 - } else {
711 - $source = $this->_options['by_author_text'];
712 - }
713 - } else {
714 - if ('' != $metadata['source']) {
715 - $source = $metadata['source'];
716 - }
717 - }
647 + return ISC\Image_Sources\Renderer\Image_Source_String::get( $id );
648 + }
718 649
719 - if($source == '') return false;
650 + /**
651 + * Render the image source box
652 + * dedicated to displaying an empty box as well so don’t add more visible elements
653 + *
654 + * @param string $content content of the source box, i.e., list of sources.
655 + * @param bool $create_placeholder if true, create a placeholder box without content.
656 + */
657 + public function render_image_source_box( string $content = '', bool $create_placeholder = false ): string {
658 + $options = $this->get_options();
659 + $headline = $options['image_list_headline'];
720 660
721 - // wrap link around source, if given
722 - if('' != $metadata['source_url']){
723 - $source = sprintf('<a href="%2$s" target="_blank" rel="nofollow">%1$s</a>', $source, $metadata['source_url']);
724 - }
661 + ob_start();
662 + require ISCPATH . 'public/views/image-source-box.php';
725 663
726 - // add license if enabled
727 - if($options['enable_licences'] && isset($metadata['licence']) && $metadata['licence']) {
728 - $licences = $this->licences_text_to_array($options['licences']);
729 - if(isset($licences[$metadata['licence']]['url'])) $licence_url = $licences[$metadata['licence']]['url'];
664 + ISC_Log::log( 'finished creating image source box' );
730 665
731 - if(isset($licence_url) && $licence_url != '') {
732 - $source = sprintf('%1$s | <a href="%3$s" target="_blank" rel="nofollow">%2$s</a>', $source, $metadata['licence'], $licence_url);
733 - } else {
734 - $source = sprintf('%1$s | %2$s', $source, $metadata['licence']);
735 - }
736 - }
666 + /**
667 + * Filter: isc_render_image_source_box
668 + * allow to modify the output of the image source box
669 + *
670 + * @param string $content content of the source box.
671 + * @param string $headline headline of the source box.
672 + * @param bool $create_placeholder if true, create a placeholder box without content.
673 + */
674 + return apply_filters( 'isc_render_image_source_box', ob_get_clean(), $content, $headline, $create_placeholder );
675 + }
737 676
738 - return $source;
739 - }
677 + /**
678 + * Render source string of single image by its id
679 + * this only returns the string with source and license (and urls),
680 + * but no wrapping, because the string is used in a lot of functions
681 + * (e.g. image source list where title is prepended)
682 + *
683 + * @updated 1.5 wrapped source into source url
684 + * @updated 2.4 accept metadata as an argument
685 + * @deprecated since 3.0
686 + *
687 + * @param int|string $id id of the image.
688 + * @param string[] $data metadata.
689 + * @param array $args arguments
690 + * use "disable-links" = (any value), to disable any working links.
691 + *
692 + * @return bool|string false if no source was given, else string with source
693 + */
694 + public function render_image_source_string( $id, array $data = [], array $args = [] ) {
695 + _deprecated_function( __METHOD__, '3.0.0', 'ISC\Image_Sources\Renderer\Image_Source_String::get' );
740 696
741 - /**
742 - * save image information for a post when it is viewed – only called when using isc_list function
743 - * (to help indexing old posts)
744 - *
745 - * @since 1.1
746 - */
747 - public function save_image_information_on_load()
748 - {
749 - global $post;
750 - if (empty($post->ID)) {
751 - return;
752 - }
697 + return ISC\Image_Sources\Renderer\Image_Source_String::get( $id, $data, $args );
698 + }
753 699
754 - $post_id = $post->ID;
755 - $_content = $post->post_content;
700 + /**
701 + * Check if the source list can be added to the content automatically
702 + *
703 + * @return bool true if the source list can be added to the content
704 + */
705 + public function can_add_list_to_content() {
706 + $options = $this->get_options();
756 707
757 - $this->save_image_information($post_id, $_content);
758 - }
708 + /**
709 + * Tests:
710 + * - is list allowed on archive pages?
711 + * - automatic injection of the list is enabled
712 + */
713 + if (
714 + (
715 + ( is_archive() || is_home() )
716 + && isset( $options['list_on_archives'] ) && $options['list_on_archives'] )
717 + || ( is_singular() && isset( $options['display_type'] ) && is_array( $options['display_type'] ) && in_array( 'list', $options['display_type'], true ) ) ) {
718 + return true;
719 + }
759 720
760 - }
721 + return false;
722 + }
723 +
724 + /**
725 + * Get image source string for public output
726 + *
727 + * @depreacted since 3.0
728 + *
729 + * @param int $attachment_id attachment ID.
730 + * @return string
731 + */
732 + public static function get_image_source_text( $attachment_id ) {
733 + _deprecated_function( __METHOD__, '3.0.0', 'ISC\Image_Sources\Image_Source::get_image_source_text' );
734 +
735 + return ISC\Image_Sources\Image_Sources::get_image_source_text( $attachment_id );
736 + }
737 +
738 + /**
739 + * Are we currently on an AMP URL?
740 + *
741 + * @return bool true if AMP url, false otherwise
742 + */
743 + public static function is_amp() {
744 + global $pagenow;
745 + if ( is_admin()
746 + || is_embed()
747 + || is_feed()
748 + || ( isset( $pagenow ) && in_array( $pagenow, [ 'wp-login.php', 'wp-signup.php', 'wp-activate.php' ], true ) )
749 + || ( defined( 'REST_REQUEST' ) && REST_REQUEST )
750 + || ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST )
751 + ) {
752 + return false;
753 + }
754 +
755 + if ( ! did_action( 'wp' ) ) {
756 + return false;
757 + }
758 +
759 + return ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() )
760 + || ( function_exists( 'is_wp_amp' ) && is_wp_amp() )
761 + || ( function_exists( 'ampforwp_is_amp_endpoint' ) && ampforwp_is_amp_endpoint() )
762 + || ( function_exists( 'is_penci_amp' ) && is_penci_amp() )
763 + || isset( $_GET ['wpamp'] );
764 + }
765 +
766 + /**
767 + * Are we currently on a URL using AMP reader mode?
768 + * This mode is optional in the official AMP plugin and the only choice in the AMPforWP plugin
769 + * in reader mode, the classic WordPress filters are not working
770 + *
771 + * @return bool true if AMP url, false otherwise
772 + */
773 + public static function is_amp_reader_mode() {
774 + // stop if we are not even on an AMP page
775 + if ( ! self::is_amp() ) {
776 + return false;
777 + }
778 +
779 + // official AMP plugin in reader mode
780 + if (
781 + class_exists( 'AMP_Options_Manager', false )
782 + && AMP_Options_Manager::get_option( 'theme_support' ) === 'reader'
783 + ) {
784 + return true;
785 + }
786 +
787 + // the AMPforWP plugin only uses reader mode since they base on an old version of the AMP plugin where only that one was available
788 + if ( function_exists( 'ampforwp_is_amp_endpoint' ) && ampforwp_is_amp_endpoint() ) {
789 + return true;
790 + }
791 +
792 + return false;
793 + }
761 794 }