Skip to content

Commit

Permalink
fix php lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
madhusudhand committed Sep 6, 2024
1 parent 408cf80 commit 3716092
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 22 deletions.
60 changes: 41 additions & 19 deletions packages/block-library/src/gallery/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,17 @@ function block_core_gallery_data_id_backcompatibility( $parsed_block ) {

add_filter( 'render_block_data', 'block_core_gallery_data_id_backcompatibility' );

function block_core_gallery_interactivity_state($block_content, $block) {
/**
* Handles interactivity state initialization for Gallery Block.
*
* @since 6.7.0
*
* @param string $block_content Rendered block content.
* @param array $block Block object.
*
* @return string Filtered block content.
*/
function block_core_gallery_interactivity_state( $block_content, $block ) {
if ( 'core/gallery' !== $block['blockName'] ) {
return $block_content;
}
Expand All @@ -43,7 +53,7 @@ function block_core_gallery_interactivity_state($block_content, $block) {
wp_interactivity_state(
'core/gallery',
array(
'images' => array(),
'images' => array(),
'galleryId' => $unique_gallery_id,
)
);
Expand Down Expand Up @@ -139,8 +149,8 @@ function block_core_gallery_render( $attributes, $content ) {
'context' => 'block-supports',
)
);
$state = wp_interactivity_state('core/gallery');

$state = wp_interactivity_state( 'core/gallery' );
$gallery_id = $state['galleryId'];

$processed_content->set_attribute( 'data-wp-interactive', 'core/gallery' );
Expand Down Expand Up @@ -203,35 +213,47 @@ static function () use ( $image_blocks, &$i ) {
return $content;
}

/**
* Handles state updates needed for the lightbox behavior in a Gallery Block.
*
* Now that the Gallery Block contains inner Image Blocks,
* we add translations for the screen reader text before rendering the gallery
* so that the Image Block can pick it up in its render_callback.
*
* @since 6.7.0
*
* @param string $block_content Rendered block content.
*
* @return string Filtered block content.
*/
function block_core_gallery_render_lightbox( $block_content ) {
$state = wp_interactivity_state('core/gallery');
$gallery_id = $state['galleryId'];

$images = $state['images'][$gallery_id] ?? array();
$state = wp_interactivity_state( 'core/gallery' );
$gallery_id = $state['galleryId'];
$images = $state['images'][ $gallery_id ] ?? array();
$translations = array();

if (!empty($images)) {
if (1 == count($images)) {
$image_id = $images[0];
$translations[$image_id] = __( 'Enlarged image', 'gutenberg' );
if ( ! empty( $images ) ) {
if ( 1 === count( $images ) ) {
$image_id = $images[0];
$translations[ $image_id ] = __( 'Enlarged image', 'gutenberg' );
} else {
for ($i=0; $i < count($images); $i++) {
$image_id = $images[$i];
for ( $i = 0; $i < count( $images ); $i++ ) {
$image_id = $images[ $i ];
/* translators: %1$s: current image index, %2$s: total number of images */
$translations[$image_id] = sprintf( __( 'Enlarged image %1$s of %2$s', 'gutenberg' ), $i + 1, count($images) );
$translations[ $image_id ] = sprintf( __( 'Enlarged image %1$s of %2$s', 'gutenberg' ), $i + 1, count( $images ) );
}
}

$image_state = wp_interactivity_state('core/image');
$image_state = wp_interactivity_state( 'core/image' );

foreach ($translations as $image_id => $translation) {
$alt = $image_state['metadata'][$image_id]['alt'];
foreach ( $translations as $image_id => $translation ) {
$alt = $image_state['metadata'][ $image_id ]['alt'];
wp_interactivity_state(
'core/image',
array(
'metadata' => array(
$image_id => array(
'screenReaderText' => empty($alt) ? $translation : "$translation: $alt",
'screenReaderText' => empty( $alt ) ? $translation : "$translation: $alt",
),
),
)
Expand Down
6 changes: 3 additions & 3 deletions packages/block-library/src/image/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,11 @@ function block_core_image_render_lightbox( $block_content, $block ) {
)
);

$state = wp_interactivity_state( 'core/gallery' );
$state = wp_interactivity_state( 'core/gallery' );
$gallery_id = $state['galleryId'];
if ( isset( $gallery_id ) ) {
$images = $state['images'][$gallery_id];
if (!isset($images)) {
$images = $state['images'][ $gallery_id ];
if ( ! isset( $images ) ) {
$images = array();
}
$images[] = $unique_image_id;
Expand Down

0 comments on commit 3716092

Please sign in to comment.