renderer = $renderer; } public function init() { add_action( 'init', [ $this, 'register_block' ] ); add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_editor_assets' ] ); } public function register_block() { if ( ! AIT_AIRCE_Settings::get( 'enable_shortcode_block' ) ) { return; } register_block_type( 'ait-airce/related-content', [ 'api_version' => 2, 'attributes' => [ 'count' => [ 'type' => 'number', 'default' => 3, ], 'label' => [ 'type' => 'string', 'default' => '', ], 'showThumbnails' => [ 'type' => 'boolean', 'default' => true, ], ], 'render_callback' => [ $this, 'render_block' ], ] ); } public function enqueue_editor_assets() { if ( ! AIT_AIRCE_Settings::get( 'enable_shortcode_block' ) ) { return; } wp_enqueue_script( 'ait-airce-editor', AIT_AIRCE_URL . 'assets/js/ait-airce-editor.js', [ 'wp-blocks', 'wp-element', 'wp-components', 'wp-editor', 'wp-block-editor', 'wp-i18n' ], AIT_AIRCE_VERSION, true ); wp_enqueue_style( 'ait-airce-editor', AIT_AIRCE_URL . 'assets/css/ait-airce-editor.css', [ 'wp-edit-blocks' ], AIT_AIRCE_VERSION ); } public function render_block( $attributes, $content, $block ) { $post_id = 0; if ( is_object( $block ) && isset( $block->context['postId'] ) ) { $post_id = intval( $block->context['postId'] ); } if ( $post_id <= 0 ) { $post_id = get_the_ID(); } if ( $post_id <= 0 ) { return ''; } $count = isset( $attributes['count'] ) ? intval( $attributes['count'] ) : 3; if ( $count < 1 ) { $count = 1; } $label = isset( $attributes['label'] ) ? (string) $attributes['label'] : ''; if ( '' === $label ) { $label = strval( AIT_AIRCE_Settings::get( 'related_label' ) ); } $show = isset( $attributes['showThumbnails'] ) ? (bool) $attributes['showThumbnails'] : (bool) AIT_AIRCE_Settings::get( 'show_thumbnails' ); return $this->renderer->render( $post_id, [ 'context_key' => 'default', 'location' => 'block', 'count' => $count, 'label' => $label, 'show_thumbnails' => $show, ] ); } }