kollapsminoriteten/wp-content/plugins/jetpack/extensions/blocks/image-compare/image-compare.php

92 lines
2.3 KiB
PHP
Raw Normal View History

2020-06-23 13:49:54 +02:00
<?php
/**
* Image Compare Block.
*
* @since 8.6
*
2021-04-27 08:32:47 +02:00
* @package automattic/jetpack
2020-06-23 13:49:54 +02:00
*/
namespace Automattic\Jetpack\Extensions\ImageCompare;
2020-10-20 18:05:12 +02:00
use Automattic\Jetpack\Blocks;
2020-06-23 13:49:54 +02:00
use Jetpack_Gutenberg;
2025-12-12 13:13:07 +01:00
if ( ! defined( 'ABSPATH' ) ) {
exit( 0 );
}
2020-06-23 13:49:54 +02:00
/**
* Registers the block for use in Gutenberg
* This is done via an action so that we can disable
* registration if we need to.
*/
function register_block() {
2020-11-18 09:10:44 +01:00
Blocks::jetpack_register_block(
2023-12-07 09:44:11 +01:00
__DIR__,
2020-06-23 13:49:54 +02:00
array( 'render_callback' => __NAMESPACE__ . '\load_assets' )
);
}
add_action( 'init', __NAMESPACE__ . '\register_block' );
/**
* Image Compare block registration/dependency declaration.
*
* @param array $attr Array containing the image-compare block attributes.
* @param string $content String containing the image-compare block content.
*
* @return string
*/
function load_assets( $attr, $content ) {
2023-12-07 09:44:11 +01:00
Jetpack_Gutenberg::load_assets_as_required( __DIR__ );
2020-11-18 09:10:44 +01:00
wp_localize_script(
2023-12-07 09:44:11 +01:00
'jetpack-block-' . sanitize_title_with_dashes( Blocks::get_block_feature( __DIR__ ) ),
2020-11-18 09:10:44 +01:00
'imageCompareHandle',
2021-04-27 08:32:47 +02:00
array(
'msg' => __( 'Slide to compare images', 'jetpack' ),
)
2020-11-18 09:10:44 +01:00
);
2020-10-20 18:05:12 +02:00
if ( Blocks::is_amp_request() ) {
$content = preg_replace(
'#<div class="juxtapose".+?</div>#s',
render_amp( $attr ),
$content
);
2020-06-23 13:49:54 +02:00
}
return $content;
}
/**
* Render image compare block for AMP
*
* @param array $attr Array containing the image-compare block attributes.
*
2020-10-20 18:05:12 +02:00
* @return string Markup for amp-image-slider.
2020-06-23 13:49:54 +02:00
*/
function render_amp( $attr ) {
$img_before = $attr['imageBefore'];
$img_after = $attr['imageAfter'];
2020-10-20 18:05:12 +02:00
$width = ! empty( $img_before['width'] ) ? absint( $img_before['width'] ) : 0;
$height = ! empty( $img_before['height'] ) ? absint( $img_before['height'] ) : 0;
// As fallback, give 1:1 aspect ratio.
if ( ! $width || ! $height ) {
$width = 1;
$height = 1;
}
2020-06-23 13:49:54 +02:00
return sprintf(
2020-10-20 18:05:12 +02:00
'<amp-image-slider layout="responsive" width="%1$s" height="%2$s"> <amp-img id="%3$d" src="%4$s" alt="%5$s" layout="fill"></amp-img> <amp-img id="%6$d" src="%7$s" alt="%8$s" layout="fill"></amp-img></amp-image-slider>',
esc_attr( $width ),
esc_attr( $height ),
2025-12-12 13:13:07 +01:00
absint( $img_before['id'] ?? 0 ),
esc_url( $img_before['url'] ?? '' ),
esc_attr( $img_before['alt'] ?? '' ),
absint( $img_after['id'] ?? 0 ),
esc_url( $img_after['url'] ?? '' ),
esc_attr( $img_after['alt'] ?? '' )
2020-06-23 13:49:54 +02:00
);
}