publish ) ? (int) $counts->publish : 0; } /** * Collect the most recent published posts. * * @param int $count How many posts to collect. * @return array */ private static function recent_posts( $count ) { $count = max( 3, min( 30, (int) $count ) ); $query = new WP_Query( array( 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => $count, 'orderby' => 'date', 'order' => 'DESC', 'ignore_sticky_posts' => true, 'no_found_rows' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, ) ); $posts = array(); foreach ( $query->posts as $post ) { // Password protected posts are not read: their text would end up in // the style guide, which every editor can open. if ( '' !== (string) $post->post_password ) { continue; } $body = wp_strip_all_tags( strip_shortcodes( (string) $post->post_content ) ); $body = trim( (string) preg_replace( '/\s+/u', ' ', $body ) ); if ( '' === $body ) { continue; } $posts[] = array( 'title' => (string) $post->post_title, 'extract' => mb_substr( $body, 0, self::EXTRACT_CHARS ), ); } wp_reset_postdata(); return $posts; } /** * Build the analysis prompt. * * @param array $posts Posts to analyse. * @return string */ private static function build_prompt( $posts ) { $samples = ''; $number = 0; foreach ( $posts as $post ) { ++$number; $samples .= "### Post {$number}: {$post['title']}\n{$post['extract']}\n\n"; } $language = ACSAGEN_Settings::language_for_prompt(); return "You are an editor writing a house style guide.\n" . "Below are extracts from posts that have already been published on one WordPress site.\n" . "Read them and describe, concretely, how this particular site writes.\n\n" . "{$samples}" . "---\n\n" . "Cover each of the following, and quote short real examples from the extracts wherever you can:\n\n" . "1. Sentence endings and register: how formal or casual the writing is, and which endings repeat.\n" . "2. Heading style: how h2 and h3 headings are phrased, and how long they are.\n" . "3. The usual shape of an article, as a list of sections in order.\n" . "4. Calls to action and the wording used to move the reader on.\n" . "5. Vocabulary level: how technical terms are handled and how close the writer stands to the reader.\n" . "6. Typical article length, and how lists, tables and images are used.\n" . "7. Phrases and turns of speech that clearly belong to this site.\n\n" . "Write the style guide in {$language}. Use Markdown. Describe only what is actually in the extracts; " . 'do not invent rules the posts do not show.'; } }