ai-image.php
56 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 | /** |
| 16 | * Registers our block for use in Gutenberg |
| 17 | * This is done via an action so that we can disable |
| 18 | * registration if we need to. |
| 19 | */ |
| 20 | function register_block() { |
| 21 | if ( ! class_exists( 'Jetpack_AI_Helper' ) ) { |
| 22 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/class-jetpack-ai-helper.php'; |
| 23 | } |
| 24 | |
| 25 | if ( ! \Jetpack_AI_Helper::is_enabled() ) { |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | Blocks::jetpack_register_block( |
| 30 | __DIR__, |
| 31 | array( 'render_callback' => __NAMESPACE__ . '\load_assets' ) |
| 32 | ); |
| 33 | } |
| 34 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| 35 | |
| 36 | /** |
| 37 | * Jetpack AI image block registration/dependency declaration. |
| 38 | * |
| 39 | * @param array $attr Array containing the Jetpack AI image block attributes. |
| 40 | * @param string $content String containing the Jetpack AI image block content. |
| 41 | * |
| 42 | * @return string |
| 43 | */ |
| 44 | function load_assets( $attr, $content ) { |
| 45 | /* |
| 46 | * Enqueue necessary scripts and styles. |
| 47 | */ |
| 48 | Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); |
| 49 | |
| 50 | return sprintf( |
| 51 | '<div class="%1$s">%2$s</div>', |
| 52 | esc_attr( Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attr ) ), |
| 53 | $content |
| 54 | ); |
| 55 | } |
| 56 |