diff --git a/includes/Classifai/Blocks/key-takeaways/render.php b/includes/Classifai/Blocks/key-takeaways/render.php index a755c864b..5614a363a 100644 --- a/includes/Classifai/Blocks/key-takeaways/render.php +++ b/includes/Classifai/Blocks/key-takeaways/render.php @@ -30,23 +30,7 @@
%s
', - esc_html( $takeaway ) - ); - } - } + echo wp_kses_post( ( new \Classifai\Features\KeyTakeaways() )->render_takeaways_html( (array) $takeaways, $layout ) ); ?>%s
', esc_html( $takeaway ) ); + } + } + + return (string) ob_get_clean(); + } + + /** + * Render the on-demand "Key Takeaways" button on the front-end of singular posts. + * + * @param string $content The post content. + * @return string + */ + public function render_takeaways_button( string $content ): string { + $post = get_post(); + + if ( + ! $post instanceof \WP_Post || + ! is_singular( $post->post_type ) || + ! in_array( $post->post_type, $this->get_supported_post_types(), true ) || + ! in_the_loop() || + ! is_main_query() + ) { + return $content; + } + + // If the post already includes the Key Takeaways block, it renders its + // own takeaways inline, so the on-demand button would be redundant. + if ( has_block( 'classifai/key-takeaways', $post ) ) { + return $content; + } + + $render = $this->get_settings( 'render' ); + $render = in_array( $render, array( 'list', 'paragraph' ), true ) ? $render : 'list'; + + $label = $this->get_settings( 'button_label' ); + $label = is_string( $label ) && '' !== $label ? $label : esc_html__( 'Key Takeaways', 'classifai' ); + + $stored = get_post_meta( $post->ID, self::TAKEAWAYS_META_KEY, true ); + $has_results = ! empty( $stored ) && is_array( $stored ); + $panel_html = $has_results ? $this->render_takeaways_html( $stored, $render ) : ''; + + $this->enqueue_frontend_assets(); + + $panel_id = 'classifai-key-takeaways-panel-' . $post->ID; + + ob_start(); + ?> +