converter = $converter ?? new Markdown_Converter(); } /** * Renders the current main query as a Markdown feed document. * * @since 1.4.0 * * @return string Markdown document. */ public function render(): string { $use_excerpt = (bool) get_option( 'rss_use_excerpt' ); $blocks = array( '# ' . $this->converter->decode_entities( (string) get_bloginfo( 'name' ) ), ); $description = $this->converter->decode_entities( (string) get_bloginfo( 'description' ) ); if ( '' !== $description ) { $blocks[] = $description; } $blocks[] = '- ' . sprintf( /* translators: %s: site home URL. */ __( 'Site: %s', 'ai' ), home_url( '/' ) ); while ( have_posts() ) { the_post(); $post = get_post(); if ( ! $post instanceof WP_Post ) { continue; } $blocks[] = $this->render_item( $post, $use_excerpt ); } wp_reset_postdata(); $blocks = array_filter( $blocks, static function ( string $block ): bool { return '' !== $block; } ); return implode( "\n\n", $blocks ) . "\n"; } /** * Renders one post as a Markdown feed item. * * @since 1.4.0 * * @param \WP_Post $post Post to render (must be the current loop post). * @param bool $use_excerpt Whether to render the excerpt instead of full content. * @return string Markdown block for this item. */ private function render_item( WP_Post $post, bool $use_excerpt ): string { $permalink = (string) get_permalink( $post ); if ( $use_excerpt ) { $content_markdown = trim( $this->converter->decode_entities( wp_strip_all_tags( (string) get_the_excerpt( $post ), true ) ) ); } else { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Core hook. $content_html = (string) apply_filters( 'the_content', get_the_content( null, false, $post ) ); $content_markdown = $this->converter->convert( $content_html, $permalink ); } $meta_lines = array( '- ' . sprintf( /* translators: %s: post permalink URL. */ __( 'Link: %s', 'ai' ), $permalink ), '- ' . sprintf( /* translators: %s: post publish date. */ __( 'Published: %s', 'ai' ), (string) get_the_date( 'c', $post ) ), '- ' . sprintf( /* translators: %s: post author display name. */ __( 'Author: %s', 'ai' ), (string) get_the_author_meta( 'display_name', (int) $post->post_author ) ), ); $sections = array( 'title' => '## ' . $this->converter->decode_entities( get_the_title( $post ) ), 'meta' => implode( "\n", $meta_lines ), 'content' => $content_markdown, ); /** * Filters the Markdown sections for a single feed item. * * Each entry is a named block of Markdown; blocks are joined with * blank lines in array order. Add, remove, or reorder entries to * customize the output (e.g. inject custom fields). * * @since 1.4.0 * * @param array $sections Named Markdown sections. * @param \WP_Post $post Post being rendered. */ $sections = apply_filters( 'wpai_markdown_feed_item_sections', $sections, $post ); $sections = array_filter( array_map( 'strval', $sections ), static function ( string $section ): bool { return '' !== $section; } ); return implode( "\n\n", $sections ); } }