@@ -24,6 +24,27 @@ protected function get_block_type_uses_context() {
2424 return [ 'postId ' ];
2525 }
2626
27+ /**
28+ * Inject dialog into the product gallery HTML.
29+ *
30+ * @param string $gallery_html The gallery HTML.
31+ * @param string $dialog_html The dialog HTML.
32+ *
33+ * @return string
34+ */
35+ protected function inject_dialog ( $ gallery_html , $ dialog_html ) {
36+
37+ // Find the position of the last </div>.
38+ $ pos = strrpos ( $ gallery_html , '</div> ' );
39+
40+ if ( false !== $ pos ) {
41+ // Inject the dialog_html at the correct position.
42+ $ html = substr_replace ( $ gallery_html , $ dialog_html , $ pos , 0 );
43+
44+ return $ html ;
45+ }
46+ }
47+
2748 /**
2849 * Return the dialog content.
2950 *
@@ -48,21 +69,6 @@ function( $carry, $item ) {
4869 return $ gallery_dialog ;
4970 }
5071
51-
52- /**
53- * This function remove the div wrapper.
54- * The content has a <div> with the class wp-block-woocommerce-product-gallery>.
55- * We don't need since that we add it in the render method.
56- *
57- * @param string $content Block content.
58- * @return string Rendered block type output.
59- */
60- private function remove_div_wrapper ( $ content ) {
61- $ parsed_string = preg_replace ( '/<div class="wp-block-woocommerce-product-gallery">/ ' , '' , $ content );
62- $ parsed_string = preg_replace ( '/<\/div>$/ ' , '' , $ parsed_string );
63- return $ parsed_string ;
64- }
65-
6672 /**
6773 * Include and render the block.
6874 *
@@ -83,22 +89,13 @@ protected function render( $attributes, $content, $block ) {
8389 $ classname_single_image = 'is-single-product-gallery-image ' ;
8490 }
8591
86- $ classname = $ attributes ['className ' ] ?? '' ;
87- $ wrapper_attributes = get_block_wrapper_attributes ( array ( 'class ' => trim ( sprintf ( 'wc-block-product-gallery %1$s %2$s ' , $ classname , $ classname_single_image ) ) ) );
88- $ gallery = ( true === $ attributes ['fullScreenOnClick ' ] && isset ( $ attributes ['mode ' ] ) && 'full ' !== $ attributes ['mode ' ] ) ? $ this ->render_dialog () : '' ;
89- $ html = sprintf (
90- '<div %1$s>
91- %2$s
92- %3$s
93- </div> ' ,
94- $ wrapper_attributes ,
95- $ content ,
96- $ gallery
97- );
92+ $ classname = $ attributes ['className ' ] ?? '' ;
93+ $ dialog = ( true === $ attributes ['fullScreenOnClick ' ] && isset ( $ attributes ['mode ' ] ) && 'full ' !== $ attributes ['mode ' ] ) ? $ this ->render_dialog () : '' ;
94+ $ post_id = $ block ->context ['postId ' ] ?? '' ;
95+ $ product = wc_get_product ( $ post_id );
9896
99- $ post_id = $ block ->context ['postId ' ] ?? '' ;
100- $ product = wc_get_product ( $ post_id );
101- $ p = new \WP_HTML_Tag_Processor ( $ html );
97+ $ html = $ this ->inject_dialog ( $ content , $ dialog );
98+ $ p = new \WP_HTML_Tag_Processor ( $ html );
10299
103100 if ( $ p ->next_tag () ) {
104101 $ p ->set_attribute ( 'data-wc-interactive ' , true );
@@ -113,6 +110,8 @@ protected function render( $attributes, $content, $block ) {
113110 )
114111 )
115112 );
113+ $ p ->add_class ( $ classname );
114+ $ p ->add_class ( $ classname_single_image );
116115 $ html = $ p ->get_updated_html ();
117116 }
118117
0 commit comments