Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/jetpack/extensio.../blocks/slidesho...
File: slideshow.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Slideshow Block.
[2] Fix | Delete
*
[3] Fix | Delete
* @since 7.1.0
[4] Fix | Delete
*
[5] Fix | Delete
* @package automattic/jetpack
[6] Fix | Delete
*/
[7] Fix | Delete
[8] Fix | Delete
namespace Automattic\Jetpack\Extensions\Slideshow;
[9] Fix | Delete
[10] Fix | Delete
use Automattic\Jetpack\Blocks;
[11] Fix | Delete
use Jetpack_Gutenberg;
[12] Fix | Delete
[13] Fix | Delete
if ( ! defined( 'ABSPATH' ) ) {
[14] Fix | Delete
exit( 0 );
[15] Fix | Delete
}
[16] Fix | Delete
[17] Fix | Delete
/**
[18] Fix | Delete
* Registers the block for use in Gutenberg
[19] Fix | Delete
* This is done via an action so that we can disable
[20] Fix | Delete
* registration if we need to.
[21] Fix | Delete
*/
[22] Fix | Delete
function register_block() {
[23] Fix | Delete
Blocks::jetpack_register_block(
[24] Fix | Delete
__DIR__,
[25] Fix | Delete
array(
[26] Fix | Delete
'render_callback' => __NAMESPACE__ . '\load_assets',
[27] Fix | Delete
'render_email_callback' => __NAMESPACE__ . '\render_email',
[28] Fix | Delete
)
[29] Fix | Delete
);
[30] Fix | Delete
}
[31] Fix | Delete
add_action( 'init', __NAMESPACE__ . '\register_block' );
[32] Fix | Delete
[33] Fix | Delete
/**
[34] Fix | Delete
* Slideshow block registration/dependency declaration.
[35] Fix | Delete
*
[36] Fix | Delete
* @param array $attr Array containing the slideshow block attributes.
[37] Fix | Delete
* @param string $content String containing the slideshow block content.
[38] Fix | Delete
*
[39] Fix | Delete
* @return string
[40] Fix | Delete
*/
[41] Fix | Delete
function load_assets( $attr, $content ) {
[42] Fix | Delete
Jetpack_Gutenberg::load_assets_as_required( __DIR__ );
[43] Fix | Delete
if ( Blocks::is_amp_request() ) {
[44] Fix | Delete
return render_amp( $attr );
[45] Fix | Delete
}
[46] Fix | Delete
[47] Fix | Delete
// Enqueue Swiper bundle for dynamic loading
[48] Fix | Delete
if ( ! is_admin() && ! Blocks::is_amp_request() ) {
[49] Fix | Delete
enqueue_swiper_library();
[50] Fix | Delete
}
[51] Fix | Delete
[52] Fix | Delete
// Fix for lazy loading conflict with slideshow images.
[53] Fix | Delete
if ( ! is_admin() ) {
[54] Fix | Delete
add_filter(
[55] Fix | Delete
'wp_content_img_tag',
[56] Fix | Delete
function ( $image ) {
[57] Fix | Delete
if ( str_contains( $image, 'loading="lazy"' ) && str_contains( $image, 'wp-block-jetpack-slideshow_image' ) ) {
[58] Fix | Delete
$image = str_replace( 'sizes="auto, ', 'sizes="', $image );
[59] Fix | Delete
}
[60] Fix | Delete
return $image;
[61] Fix | Delete
}
[62] Fix | Delete
);
[63] Fix | Delete
}
[64] Fix | Delete
[65] Fix | Delete
return $content;
[66] Fix | Delete
}
[67] Fix | Delete
[68] Fix | Delete
/**
[69] Fix | Delete
* Render slideshow block for email.
[70] Fix | Delete
*
[71] Fix | Delete
* @since 15.0
[72] Fix | Delete
*
[73] Fix | Delete
* @param string $block_content The original block HTML content.
[74] Fix | Delete
* @param array $parsed_block The parsed block data including attributes.
[75] Fix | Delete
* @param object $rendering_context Email rendering context.
[76] Fix | Delete
*
[77] Fix | Delete
* @return string
[78] Fix | Delete
*/
[79] Fix | Delete
function render_email( $block_content, array $parsed_block, $rendering_context ) {
[80] Fix | Delete
// Validate input parameters and required dependencies
[81] Fix | Delete
if ( ! isset( $parsed_block['attrs'] ) || ! is_array( $parsed_block['attrs'] ) ||
[82] Fix | Delete
! class_exists( '\Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks\Gallery' ) ) {
[83] Fix | Delete
return '';
[84] Fix | Delete
}
[85] Fix | Delete
[86] Fix | Delete
$attributes = $parsed_block['attrs'];
[87] Fix | Delete
[88] Fix | Delete
// Process images for email rendering
[89] Fix | Delete
$images = process_slideshow_images_for_email( $attributes );
[90] Fix | Delete
[91] Fix | Delete
if ( empty( $images ) ) {
[92] Fix | Delete
return '';
[93] Fix | Delete
}
[94] Fix | Delete
[95] Fix | Delete
// Build innerBlocks that WooCommerce's gallery renderer expects
[96] Fix | Delete
// The renderer looks for innerBlocks with blockName 'core/image' and innerHTML
[97] Fix | Delete
$inner_blocks = array();
[98] Fix | Delete
foreach ( $images as $image ) {
[99] Fix | Delete
// Build image HTML in the format that extract_image_from_html() can parse
[100] Fix | Delete
$img_html = sprintf(
[101] Fix | Delete
'<img src="%s" alt="%s"',
[102] Fix | Delete
esc_url( $image['url'] ),
[103] Fix | Delete
esc_attr( $image['alt'] )
[104] Fix | Delete
);
[105] Fix | Delete
[106] Fix | Delete
if ( ! empty( $image['id'] ) ) {
[107] Fix | Delete
$img_html .= sprintf( ' class="wp-image-%d"', absint( $image['id'] ) );
[108] Fix | Delete
}
[109] Fix | Delete
[110] Fix | Delete
$img_html .= ' />';
[111] Fix | Delete
[112] Fix | Delete
// Add caption if available (extract_image_from_html looks for figcaption)
[113] Fix | Delete
// Preserve HTML in captions - the gallery renderer will sanitize it
[114] Fix | Delete
if ( ! empty( $image['caption'] ) ) {
[115] Fix | Delete
$img_html .= sprintf(
[116] Fix | Delete
'<figcaption>%s</figcaption>',
[117] Fix | Delete
wp_kses_post( $image['caption'] )
[118] Fix | Delete
);
[119] Fix | Delete
}
[120] Fix | Delete
[121] Fix | Delete
// Create inner block structure that the gallery renderer expects
[122] Fix | Delete
// The renderer only uses innerHTML, not attrs
[123] Fix | Delete
$inner_blocks[] = array(
[124] Fix | Delete
'blockName' => 'core/image',
[125] Fix | Delete
'innerHTML' => $img_html,
[126] Fix | Delete
);
[127] Fix | Delete
}
[128] Fix | Delete
[129] Fix | Delete
// Build block content HTML for gallery caption extraction (if needed)
[130] Fix | Delete
// The renderer uses $block_content parameter to extract gallery-level captions
[131] Fix | Delete
$block_content_html = '<figure class="wp-block-gallery has-nested-images columns-default is-cropped"><ul class="blocks-gallery-grid"></ul></figure>';
[132] Fix | Delete
[133] Fix | Delete
// Create a mock parsed block that WooCommerce's gallery renderer can handle
[134] Fix | Delete
// The renderer uses columns from attrs (defaults to 3, but 2 works better for email)
[135] Fix | Delete
$mock_parsed_block = array(
[136] Fix | Delete
'innerBlocks' => $inner_blocks,
[137] Fix | Delete
'attrs' => array(
[138] Fix | Delete
'columns' => 2,
[139] Fix | Delete
),
[140] Fix | Delete
);
[141] Fix | Delete
[142] Fix | Delete
// Preserve email_attrs if present (used for width calculation)
[143] Fix | Delete
if ( ! empty( $parsed_block['email_attrs'] ) ) {
[144] Fix | Delete
$mock_parsed_block['email_attrs'] = $parsed_block['email_attrs'];
[145] Fix | Delete
}
[146] Fix | Delete
[147] Fix | Delete
// Use WooCommerce's core gallery renderer
[148] Fix | Delete
$woo_gallery_renderer = new \Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks\Gallery();
[149] Fix | Delete
[150] Fix | Delete
return $woo_gallery_renderer->render( $block_content_html, $mock_parsed_block, $rendering_context );
[151] Fix | Delete
}
[152] Fix | Delete
[153] Fix | Delete
/**
[154] Fix | Delete
* Render slideshow block for AMP
[155] Fix | Delete
*
[156] Fix | Delete
* @param array $attr Array containing the slideshow block attributes.
[157] Fix | Delete
*
[158] Fix | Delete
* @return string
[159] Fix | Delete
*/
[160] Fix | Delete
function render_amp( $attr ) {
[161] Fix | Delete
if ( empty( $attr['ids'] ) ) {
[162] Fix | Delete
return '';
[163] Fix | Delete
}
[164] Fix | Delete
[165] Fix | Delete
static $wp_block_jetpack_slideshow_id = 0;
[166] Fix | Delete
++$wp_block_jetpack_slideshow_id;
[167] Fix | Delete
[168] Fix | Delete
$ids = $attr['ids'];
[169] Fix | Delete
$autoplay = empty( $attr['autoplay'] ) ? false : true;
[170] Fix | Delete
$extras = array(
[171] Fix | Delete
'wp-amp-block',
[172] Fix | Delete
$autoplay ? 'wp-block-jetpack-slideshow__autoplay' : null,
[173] Fix | Delete
$autoplay ? 'wp-block-jetpack-slideshow__autoplay-playing' : null,
[174] Fix | Delete
);
[175] Fix | Delete
$classes = Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attr, $extras );
[176] Fix | Delete
[177] Fix | Delete
return sprintf(
[178] Fix | Delete
'<div class="%1$s" id="wp-block-jetpack-slideshow__%2$d"><div class="wp-block-jetpack-slideshow_container swiper">%3$s%4$s%5$s</div></div>',
[179] Fix | Delete
esc_attr( $classes ),
[180] Fix | Delete
absint( $wp_block_jetpack_slideshow_id ),
[181] Fix | Delete
amp_carousel( $attr, $wp_block_jetpack_slideshow_id ),
[182] Fix | Delete
$autoplay ? autoplay_ui( $wp_block_jetpack_slideshow_id ) : '',
[183] Fix | Delete
render_paginator( $ids, $wp_block_jetpack_slideshow_id )
[184] Fix | Delete
);
[185] Fix | Delete
}
[186] Fix | Delete
[187] Fix | Delete
/**
[188] Fix | Delete
* Generate amp-carousel markup
[189] Fix | Delete
*
[190] Fix | Delete
* @param array $attr Array of block attributes.
[191] Fix | Delete
* @param int $block_ordinal The ordinal number of the block, used in unique ID.
[192] Fix | Delete
*
[193] Fix | Delete
* @return string amp-carousel markup.
[194] Fix | Delete
*/
[195] Fix | Delete
function amp_carousel( $attr, $block_ordinal ) {
[196] Fix | Delete
$ids = empty( $attr['ids'] ) ? array() : $attr['ids'];
[197] Fix | Delete
$first_image = wp_get_attachment_metadata( $ids[0] );
[198] Fix | Delete
$delay = empty( $attr['delay'] ) ? 3 : absint( $attr['delay'] );
[199] Fix | Delete
$autoplay = empty( $attr['autoplay'] ) ? false : $attr['autoplay'];
[200] Fix | Delete
$width = empty( $first_image['width'] ) ? 800 : $first_image['width'];
[201] Fix | Delete
$height = empty( $first_image['height'] ) ? 600 : $first_image['height'];
[202] Fix | Delete
return sprintf(
[203] Fix | Delete
'<amp-carousel width="%1$d" height="%2$d" layout="responsive" type="slides" data-next-button-aria-label="%3$s" data-prev-button-aria-label="%4$s" controls loop %5$s id="wp-block-jetpack-slideshow__amp-carousel__%6$s" on="slideChange:wp-block-jetpack-slideshow__amp-pagination__%6$s.toggle(index=event.index, value=true)">%7$s</amp-carousel>',
[204] Fix | Delete
esc_attr( $width ),
[205] Fix | Delete
esc_attr( $height ),
[206] Fix | Delete
esc_attr__( 'Next Slide', 'jetpack' ),
[207] Fix | Delete
esc_attr__( 'Previous Slide', 'jetpack' ),
[208] Fix | Delete
$autoplay ? 'autoplay delay=' . esc_attr( $delay * 1000 ) : '',
[209] Fix | Delete
absint( $block_ordinal ),
[210] Fix | Delete
implode( '', slides( $ids, $width, $height ) )
[211] Fix | Delete
);
[212] Fix | Delete
}
[213] Fix | Delete
[214] Fix | Delete
/**
[215] Fix | Delete
* Generate array of slides markup
[216] Fix | Delete
*
[217] Fix | Delete
* @param array $ids Array of image ids.
[218] Fix | Delete
* @param int $width Width of the container.
[219] Fix | Delete
* @param int $height Height of the container.
[220] Fix | Delete
*
[221] Fix | Delete
* @return array Array of slides markup.
[222] Fix | Delete
*/
[223] Fix | Delete
function slides( $ids = array(), $width = 400, $height = 300 ) {
[224] Fix | Delete
return array_map(
[225] Fix | Delete
function ( $id ) use ( $width, $height ) {
[226] Fix | Delete
$caption = wp_get_attachment_caption( $id );
[227] Fix | Delete
$figcaption = $caption ? sprintf(
[228] Fix | Delete
'<figcaption class="wp-block-jetpack-slideshow_caption gallery-caption">%s</figcaption>',
[229] Fix | Delete
wp_kses_post( $caption )
[230] Fix | Delete
) : '';
[231] Fix | Delete
$image = wp_get_attachment_image(
[232] Fix | Delete
$id,
[233] Fix | Delete
array( $width, $height ),
[234] Fix | Delete
false,
[235] Fix | Delete
array(
[236] Fix | Delete
'class' => 'wp-block-jetpack-slideshow_image',
[237] Fix | Delete
'object-fit' => 'contain',
[238] Fix | Delete
)
[239] Fix | Delete
);
[240] Fix | Delete
return sprintf(
[241] Fix | Delete
'<div class="wp-block-jetpack-slideshow_slide"><figure>%s%s</figure></div>',
[242] Fix | Delete
$image,
[243] Fix | Delete
$figcaption
[244] Fix | Delete
);
[245] Fix | Delete
},
[246] Fix | Delete
$ids
[247] Fix | Delete
);
[248] Fix | Delete
}
[249] Fix | Delete
[250] Fix | Delete
/**
[251] Fix | Delete
* Render blocks paginator section
[252] Fix | Delete
*
[253] Fix | Delete
* @param array $ids Array of image ids.
[254] Fix | Delete
* @param int $block_ordinal The ordinal number of the block, used in unique ID.
[255] Fix | Delete
*
[256] Fix | Delete
* @return array Array of bullets markup.
[257] Fix | Delete
*/
[258] Fix | Delete
function render_paginator( $ids = array(), $block_ordinal = 0 ) {
[259] Fix | Delete
$total = count( $ids );
[260] Fix | Delete
[261] Fix | Delete
if ( $total < 6 ) {
[262] Fix | Delete
return bullets( $ids, $block_ordinal );
[263] Fix | Delete
}
[264] Fix | Delete
[265] Fix | Delete
return sprintf(
[266] Fix | Delete
'<div class="swiper-pagination-simple">%s / %s</div>',
[267] Fix | Delete
absint( $block_ordinal ),
[268] Fix | Delete
absint( $total )
[269] Fix | Delete
);
[270] Fix | Delete
}
[271] Fix | Delete
[272] Fix | Delete
/**
[273] Fix | Delete
* Generate array of bullets markup
[274] Fix | Delete
*
[275] Fix | Delete
* @param array $ids Array of image ids.
[276] Fix | Delete
* @param int $block_ordinal The ordinal number of the block, used in unique ID.
[277] Fix | Delete
*
[278] Fix | Delete
* @return array Array of bullets markup.
[279] Fix | Delete
*/
[280] Fix | Delete
function bullets( $ids = array(), $block_ordinal = 0 ) {
[281] Fix | Delete
$buttons = array_map(
[282] Fix | Delete
function ( $index ) {
[283] Fix | Delete
$aria_label = sprintf(
[284] Fix | Delete
/* translators: %d: Slide number. */
[285] Fix | Delete
__( 'Go to slide %d', 'jetpack' ),
[286] Fix | Delete
absint( $index + 1 )
[287] Fix | Delete
);
[288] Fix | Delete
return sprintf(
[289] Fix | Delete
'<button option="%d" class="swiper-pagination-bullet" tabindex="0" role="button" aria-label="%s" %s></button>',
[290] Fix | Delete
absint( $index ),
[291] Fix | Delete
esc_attr( $aria_label ),
[292] Fix | Delete
0 === $index ? 'selected' : ''
[293] Fix | Delete
);
[294] Fix | Delete
},
[295] Fix | Delete
array_keys( $ids )
[296] Fix | Delete
);
[297] Fix | Delete
[298] Fix | Delete
return sprintf(
[299] Fix | Delete
'<amp-selector id="wp-block-jetpack-slideshow__amp-pagination__%1$d" class="wp-block-jetpack-slideshow_pagination swiper-pagination swiper-pagination-custom amp-pagination" on="select:wp-block-jetpack-slideshow__amp-carousel__%1$d.goToSlide(index=event.targetOption)" layout="container">%2$s</amp-selector>',
[300] Fix | Delete
absint( $block_ordinal ),
[301] Fix | Delete
implode( '', $buttons )
[302] Fix | Delete
);
[303] Fix | Delete
}
[304] Fix | Delete
[305] Fix | Delete
/**
[306] Fix | Delete
* Generate autoplay play/pause UI.
[307] Fix | Delete
*
[308] Fix | Delete
* @param int $block_ordinal The ordinal number of the block, used in unique ID.
[309] Fix | Delete
*
[310] Fix | Delete
* @return string Autoplay UI markup.
[311] Fix | Delete
*/
[312] Fix | Delete
function autoplay_ui( $block_ordinal = 0 ) {
[313] Fix | Delete
$block_id = sprintf(
[314] Fix | Delete
'wp-block-jetpack-slideshow__%d',
[315] Fix | Delete
absint( $block_ordinal )
[316] Fix | Delete
);
[317] Fix | Delete
$amp_carousel_id = sprintf(
[318] Fix | Delete
'wp-block-jetpack-slideshow__amp-carousel__%d',
[319] Fix | Delete
absint( $block_ordinal )
[320] Fix | Delete
);
[321] Fix | Delete
$autoplay_pause = sprintf(
[322] Fix | Delete
'<a aria-label="%s" class="wp-block-jetpack-slideshow_button-pause" role="button" on="tap:%s.toggleAutoplay(toggleOn=false),%s.toggleClass(class=wp-block-jetpack-slideshow__autoplay-playing,force=false)"></a>',
[323] Fix | Delete
esc_attr__( 'Pause Slideshow', 'jetpack' ),
[324] Fix | Delete
esc_attr( $amp_carousel_id ),
[325] Fix | Delete
esc_attr( $block_id )
[326] Fix | Delete
);
[327] Fix | Delete
$autoplay_play = sprintf(
[328] Fix | Delete
'<a aria-label="%s" class="wp-block-jetpack-slideshow_button-play" role="button" on="tap:%s.toggleAutoplay(toggleOn=true),%s.toggleClass(class=wp-block-jetpack-slideshow__autoplay-playing,force=true)"></a>',
[329] Fix | Delete
esc_attr__( 'Play Slideshow', 'jetpack' ),
[330] Fix | Delete
esc_attr( $amp_carousel_id ),
[331] Fix | Delete
esc_attr( $block_id )
[332] Fix | Delete
);
[333] Fix | Delete
return $autoplay_pause . $autoplay_play;
[334] Fix | Delete
}
[335] Fix | Delete
[336] Fix | Delete
/**
[337] Fix | Delete
* Enqueue Swiper library assets for dynamic loading.
[338] Fix | Delete
*
[339] Fix | Delete
* @return void
[340] Fix | Delete
*/
[341] Fix | Delete
function enqueue_swiper_library() {
[342] Fix | Delete
$swiper_js_path = Jetpack_Gutenberg::get_blocks_directory() . 'swiper.js';
[343] Fix | Delete
$swiper_css_path = Jetpack_Gutenberg::get_blocks_directory() . 'swiper' . ( is_rtl() ? '.rtl' : '' ) . '.css';
[344] Fix | Delete
[345] Fix | Delete
if ( Jetpack_Gutenberg::block_has_asset( $swiper_js_path ) ) {
[346] Fix | Delete
wp_enqueue_script(
[347] Fix | Delete
'jetpack-swiper-library',
[348] Fix | Delete
plugins_url( $swiper_js_path, JETPACK__PLUGIN_FILE ),
[349] Fix | Delete
array(),
[350] Fix | Delete
JETPACK__VERSION,
[351] Fix | Delete
true
[352] Fix | Delete
);
[353] Fix | Delete
}
[354] Fix | Delete
[355] Fix | Delete
if ( Jetpack_Gutenberg::block_has_asset( $swiper_css_path ) ) {
[356] Fix | Delete
wp_enqueue_style(
[357] Fix | Delete
'jetpack-swiper-library',
[358] Fix | Delete
plugins_url( $swiper_css_path, JETPACK__PLUGIN_FILE ),
[359] Fix | Delete
array(),
[360] Fix | Delete
JETPACK__VERSION
[361] Fix | Delete
);
[362] Fix | Delete
}
[363] Fix | Delete
}
[364] Fix | Delete
[365] Fix | Delete
/**
[366] Fix | Delete
* Process slideshow images for email rendering.
[367] Fix | Delete
*
[368] Fix | Delete
* @param array $attr Block attributes containing image data.
[369] Fix | Delete
* @return array Processed image data for email rendering.
[370] Fix | Delete
*/
[371] Fix | Delete
function process_slideshow_images_for_email( $attr ) {
[372] Fix | Delete
$images = array();
[373] Fix | Delete
[374] Fix | Delete
// Get images from IDs (primary data source)
[375] Fix | Delete
if ( ! empty( $attr['ids'] ) && is_array( $attr['ids'] ) ) {
[376] Fix | Delete
foreach ( $attr['ids'] as $index => $id ) {
[377] Fix | Delete
// Validate ID is a positive integer
[378] Fix | Delete
$id = absint( $id );
[379] Fix | Delete
if ( ! $id ) {
[380] Fix | Delete
continue;
[381] Fix | Delete
}
[382] Fix | Delete
[383] Fix | Delete
$image_url = wp_get_attachment_image_url( $id, 'medium' );
[384] Fix | Delete
[385] Fix | Delete
// Sanitize alt text from post meta
[386] Fix | Delete
$alt_text = get_post_meta( $id, '_wp_attachment_image_alt', true );
[387] Fix | Delete
$alt_text = sanitize_text_field( $alt_text );
[388] Fix | Delete
[389] Fix | Delete
// Get caption from attachment post (stored in post_excerpt)
[390] Fix | Delete
$attachment_post = get_post( $id );
[391] Fix | Delete
$caption = '';
[392] Fix | Delete
[393] Fix | Delete
// First try to get caption from images array if available (with validation)
[394] Fix | Delete
// Preserve HTML in captions - the gallery renderer will sanitize it
[395] Fix | Delete
if ( ! empty( $attr['images'] ) && is_array( $attr['images'] ) && isset( $attr['images'][ $index ]['caption'] ) ) {
[396] Fix | Delete
$caption = wp_kses_post( $attr['images'][ $index ]['caption'] );
[397] Fix | Delete
}
[398] Fix | Delete
[399] Fix | Delete
// If no caption in images array, get it from attachment post
[400] Fix | Delete
if ( empty( $caption ) && $attachment_post && ! empty( $attachment_post->post_excerpt ) ) {
[401] Fix | Delete
$caption = wp_kses_post( $attachment_post->post_excerpt );
[402] Fix | Delete
}
[403] Fix | Delete
[404] Fix | Delete
if ( $image_url ) {
[405] Fix | Delete
$images[] = array(
[406] Fix | Delete
'url' => $image_url,
[407] Fix | Delete
'alt' => $alt_text,
[408] Fix | Delete
'caption' => $caption,
[409] Fix | Delete
'id' => $id,
[410] Fix | Delete
);
[411] Fix | Delete
}
[412] Fix | Delete
}
[413] Fix | Delete
} elseif ( ! empty( $attr['images'] ) && is_array( $attr['images'] ) ) {
[414] Fix | Delete
// Fall back to images array if IDs aren't available (for edge cases/testing)
[415] Fix | Delete
foreach ( $attr['images'] as $image_data ) {
[416] Fix | Delete
if ( ! empty( $image_data['url'] ) ) {
[417] Fix | Delete
// Sanitize URL - esc_url_raw returns empty string for invalid URLs
[418] Fix | Delete
$url = esc_url_raw( $image_data['url'] );
[419] Fix | Delete
if ( ! $url ) {
[420] Fix | Delete
continue;
[421] Fix | Delete
}
[422] Fix | Delete
[423] Fix | Delete
// Sanitize alt text
[424] Fix | Delete
$alt_text = ! empty( $image_data['alt'] ) ? sanitize_text_field( $image_data['alt'] ) : '';
[425] Fix | Delete
[426] Fix | Delete
// Preserve HTML in captions - the gallery renderer will sanitize it
[427] Fix | Delete
$caption = ! empty( $image_data['caption'] ) ? wp_kses_post( $image_data['caption'] ) : '';
[428] Fix | Delete
[429] Fix | Delete
// Validate ID if present
[430] Fix | Delete
$id = ! empty( $image_data['id'] ) ? absint( $image_data['id'] ) : 0;
[431] Fix | Delete
[432] Fix | Delete
$images[] = array(
[433] Fix | Delete
'url' => $url,
[434] Fix | Delete
'alt' => $alt_text,
[435] Fix | Delete
'caption' => $caption,
[436] Fix | Delete
'id' => $id,
[437] Fix | Delete
);
[438] Fix | Delete
}
[439] Fix | Delete
}
[440] Fix | Delete
}
[441] Fix | Delete
[442] Fix | Delete
return $images;
[443] Fix | Delete
}
[444] Fix | Delete
[445] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function