-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Experiment: Add lightbox to Image block using directives #49621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
ebba7b4
af59781
cca3d60
160a029
7f16f27
f4b2ee8
378b041
409d161
29e11ab
f4b6c0a
3d94473
6ae760f
9d6869e
c6d02d8
af55917
7c1f2d1
9507199
3f89d9b
cb7f89a
437d873
7bd6b25
109344c
f95a2cd
e37e228
e17e564
f481a35
848a8fa
2204a50
6244e14
c388721
9754edb
b337272
ee679e6
ddbf32c
f82d8f5
8ce00cd
1427e8a
58b0ef5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,24 @@ | |
| * @package WordPress | ||
| */ | ||
|
|
||
|
|
||
| function processor_img_src_is_null( $processor ) { | ||
| return $processor->get_attribute( 'src' ) === null; | ||
| } | ||
|
|
||
| function process_img_data_id( $processor, $attributes ) { | ||
|
|
||
| if ( isset( $attributes['data-id'] ) ) { | ||
| // Add the data-id="$id" attribute to the img element | ||
| // to provide backwards compatibility for the Gallery Block, | ||
| // which now wraps Image Blocks within innerBlocks. | ||
| // The data-id attribute is added in a core/gallery `render_block_data` hook. | ||
| $processor->set_attribute( 'data-id', $attributes['data-id'] ); | ||
| } | ||
|
|
||
| return $processor; | ||
| } | ||
artemiomorales marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Renders the `core/image` block on the server, | ||
| * adding a data-id attribute to the element if core/gallery has added on pre-render. | ||
|
|
@@ -14,44 +32,49 @@ | |
| * @return string Returns the block content with the data-id attribute added. | ||
| */ | ||
| function render_block_core_image( $attributes, $content ) { | ||
| $processor = new WP_HTML_Tag_Processor( $content ); | ||
|
|
||
| if( isset( $attributes['enableLightbox'] ) && $attributes['enableLightbox'] === true ) { | ||
| $processor->next_tag( 'figure' ); | ||
| $processor->set_attribute( 'data-wp-class.isZoomed', 'context.core.isZoomed'); | ||
| $processor->set_attribute( 'data-wp-init.closeZoomOnEsc', 'actions.core.closeZoomOnEsc'); | ||
| } | ||
| $body_content = new WP_HTML_Tag_Processor( $content ); | ||
|
|
||
| $processor->next_tag( 'img' ); | ||
| $body_content->next_tag( 'figure' ); | ||
| $body_content->set_attribute( 'data-wp-class.isZoomed', 'context.core.isZoomed'); | ||
| $body_content->set_attribute( 'data-wp-init.closeZoomOnEsc', 'actions.core.closeZoomOnEsc'); | ||
|
|
||
| if ( $processor->get_attribute( 'src' ) === null ) { | ||
| return ''; | ||
| } | ||
| $body_content->next_tag( 'img' ); | ||
|
|
||
| if ( isset( $attributes['data-id'] ) ) { | ||
| // Add the data-id="$id" attribute to the img element | ||
| // to provide backwards compatibility for the Gallery Block, | ||
| // which now wraps Image Blocks within innerBlocks. | ||
| // The data-id attribute is added in a core/gallery `render_block_data` hook. | ||
| $processor->set_attribute( 'data-id', $attributes['data-id'] ); | ||
| $content = $processor->get_updated_html(); | ||
| } | ||
| if ( processor_img_src_is_null( $body_content ) ) { | ||
| return ''; | ||
| } | ||
|
|
||
| $body_content = process_img_data_id($body_content, $attributes); | ||
|
||
| $body_content->set_attribute( 'data-wp-on.click', 'actions.core.imageZoom'); | ||
| $body_content = $body_content->get_updated_html(); | ||
|
|
||
| $modal_content = new WP_HTML_Tag_Processor( $content ); | ||
| $modal_content->next_tag( 'img' ); | ||
| $modal_content = process_img_data_id($modal_content, $attributes); | ||
| $modal_content = $modal_content->get_updated_html(); | ||
|
||
|
|
||
| if( isset( $attributes['enableLightbox'] ) && $attributes['enableLightbox'] === true ) { | ||
| /// Include conditional to detect if user has activated lightbox in settings #2 | ||
| $processor->set_attribute( 'data-wp-on.click', 'actions.core.imageZoom'); | ||
| $content = $processor->get_updated_html(); | ||
| return <<<HTML | ||
| <div class="lightbox-container" data-wp-context='{ "core": { "isZoomed": false } }'> | ||
| $content | ||
| <div data-wp-portal="body" data-wp-class.overlay="context.core.isZoomed"> | ||
| <div data-wp-on.click="actions.core.closeZoom"></div> | ||
| <div class="lightbox-container" data-wp-context='{ "core": { "initialized": false, "isZoomed": false } }'> | ||
| $body_content | ||
| <div data-wp-portal="body" class="overlay" data-wp-class.initialized="context.core.initialized" data-wp-class.active="context.core.isZoomed"> | ||
| $modal_content | ||
| <div class="hide" data-wp-on.click="actions.core.closeZoom"></div> | ||
| </div> | ||
| </div> | ||
| HTML; | ||
artemiomorales marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return $content; | ||
| $processor = new WP_HTML_Tag_Processor( $content ); | ||
| $processor->next_tag( 'img' ); | ||
|
|
||
| if ( processor_img_src_is_null( $processor ) ) { | ||
| return ''; | ||
| } | ||
|
|
||
| $processor = process_img_data_id($processor, $attributes); | ||
|
|
||
| return $processor->get_updated_html(); | ||
| } | ||
|
|
||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.