" . preg_replace( '/\s+/', ' ', $summary ) . "\n"; } if ( '' !== $details ) { $out .= "\n" . str_replace( "\r\n", "\n", $details ) . "\n"; } $links = 0; $sections = array( 'Sections' => self::categories(), 'Pages' => self::pages(), 'Latest posts' => self::posts(), 'Optional' => self::optional(), ); foreach ( $sections as $title => $items ) { if ( ! $items ) { continue; } $out .= "\n## " . $title . "\n\n"; foreach ( $items as $item ) { $out .= '- [' . $item[0] . '](' . $item[1] . ')' . ( '' !== $item[2] ? ': ' . $item[2] : '' ) . "\n"; ++$links; } } return array( $out, $links ); } /** Categories with the most posts: [ title, url, description ]. */ private static function categories() { $count = (int) CFSP_Settings::get( 'llms_sections' ); if ( $count < 1 || ! CFSP_Settings::crawler( 'taxonomies' ) ) { return array(); } $terms = get_terms( array( 'taxonomy' => 'category', 'hide_empty' => true, 'orderby' => 'count', 'order' => 'DESC', 'number' => $count + 1, ) ); $items = array(); foreach ( is_wp_error( $terms ) ? array() : $terms as $term ) { if ( (int) $term->term_id === (int) get_option( 'default_category' ) && 'uncategorized' === $term->slug ) { continue; } $link = get_term_link( $term ); if ( is_wp_error( $link ) || CFSP_Exporter::is_excluded( $link ) ) { continue; } $items[] = array( self::plain( $term->name ), self::url( $link ), self::sentence( $term->description ) ); } return array_slice( $items, 0, $count ); } /** Published pages (not the front page): [ title, url, '' ]. */ private static function pages() { if ( ! CFSP_Settings::get( 'llms_pages' ) || ! in_array( 'page', CFSP_URLs::post_types(), true ) ) { return array(); } $ids = get_posts( array( 'post_type' => 'page', 'post_status' => 'publish', 'has_password' => false, 'orderby' => 'menu_order title', 'order' => 'ASC', 'numberposts' => 50, 'fields' => 'ids', ) ); $front = (int) get_option( 'page_on_front' ); $items = array(); foreach ( $ids as $id ) { $link = get_permalink( $id ); if ( (int) $id === $front || ! $link || CFSP_Exporter::is_excluded( $link ) ) { continue; } $items[] = array( self::plain( get_the_title( $id ) ), self::url( $link ), '' ); } return $items; } /** Newest posts: [ title, url, short excerpt ]. */ private static function posts() { $count = (int) CFSP_Settings::get( 'llms_posts' ); if ( $count < 1 || ! in_array( 'post', CFSP_URLs::post_types(), true ) ) { return array(); } $posts = get_posts( array( 'post_type' => 'post', 'post_status' => 'publish', 'has_password' => false, 'orderby' => 'date', 'order' => 'DESC', 'numberposts' => $count, ) ); $items = array(); foreach ( $posts as $post ) { $link = get_permalink( $post ); if ( ! $link || CFSP_Exporter::is_excluded( $link ) ) { continue; } $text = $post->post_excerpt ? $post->post_excerpt : strip_shortcodes( $post->post_content ); $items[] = array( self::plain( get_the_title( $post ) ), self::url( $link ), self::plain( wp_trim_words( excerpt_remove_blocks( $text ), 20, '…' ) ) ); } return $items; } /** Feed and sitemap: [ title, url, '' ]. */ private static function optional() { $items = array(); if ( CFSP_Settings::crawler( 'feeds' ) ) { $feed = CFSP_Settings::get( 'feed_xml' ) ? home_url( '/feed.xml' ) : get_feed_link(); $items[] = array( 'RSS feed', self::url( $feed ), 'The newest posts' ); } $sitemaps = CFSP_URLs::sitemap_urls(); if ( $sitemaps ) { $items[] = array( 'Sitemap', self::url( end( $sitemaps ) ), 'Every page on the site' ); } return $items; } /** A WordPress address as it is on the static site (absolute when the static address is known). */ private static function url( $url ) { $dest = CFSP_Settings::destination_url(); return '' !== $dest ? $dest . CFSP_Exporter::relative_path( $url ) : CFSP_Exporter::replace_origin( $url ); } /** Plain one-line text, safe inside a Markdown link label. */ private static function plain( $text ) { $text = html_entity_decode( wp_strip_all_tags( (string) $text ), ENT_QUOTES, 'UTF-8' ); return trim( str_replace( array( '[', ']' ), array( '(', ')' ), preg_replace( '/\s+/', ' ', $text ) ) ); } /** First sentence of a description, at most 25 words. */ private static function sentence( $text ) { $text = self::plain( $text ); if ( '' === $text ) { return ''; } if ( preg_match( '/^.+?[.!?](?=\s|$)/u', $text, $m ) ) { $text = $m[0]; } return wp_trim_words( $text, 25, '…' ); } }