ai-image.php
59 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Jetpack AI Image Block. |
| 4 | * |
| 5 | * @since 11.8 |
| 6 | * |
| 7 | * @package automattic/jetpack |
| 8 | */ |
| 9 | |
| 10 | namespace Automattic\Jetpack\Extensions\AIImage; |
| 11 | |
| 12 | use Automattic\Jetpack\Blocks; |
| 13 | use Jetpack_Gutenberg; |
| 14 | |
| 15 | const FEATURE_NAME = 'ai-image'; |
| 16 | const BLOCK_NAME = 'jetpack/' . FEATURE_NAME; |
| 17 | |
| 18 | /** |
| 19 | * Registers our block for use in Gutenberg |
| 20 | * This is done via an action so that we can disable |
| 21 | * registration if we need to. |
| 22 | */ |
| 23 | function register_block() { |
| 24 | if ( ! class_exists( 'Jetpack_AI_Helper' ) ) { |
| 25 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/class-jetpack-ai-helper.php'; |
| 26 | } |
| 27 | |
| 28 | if ( ! \Jetpack_AI_Helper::is_enabled() ) { |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | Blocks::jetpack_register_block( |
| 33 | BLOCK_NAME, |
| 34 | array( 'render_callback' => __NAMESPACE__ . '\load_assets' ) |
| 35 | ); |
| 36 | } |
| 37 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| 38 | |
| 39 | /** |
| 40 | * Jetpack AI image block registration/dependency declaration. |
| 41 | * |
| 42 | * @param array $attr Array containing the Jetpack AI image block attributes. |
| 43 | * @param string $content String containing the Jetpack AI image block content. |
| 44 | * |
| 45 | * @return string |
| 46 | */ |
| 47 | function load_assets( $attr, $content ) { |
| 48 | /* |
| 49 | * Enqueue necessary scripts and styles. |
| 50 | */ |
| 51 | Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME ); |
| 52 | |
| 53 | return sprintf( |
| 54 | '<div class="%1$s">%2$s</div>', |
| 55 | esc_attr( Blocks::classes( FEATURE_NAME, $attr ) ), |
| 56 | $content |
| 57 | ); |
| 58 | } |
| 59 |