> */ public static function bands(): array { return array( array( 'id' => 'empty', 'min' => 0, 'max' => 49, 'label' => __( 'Practically empty', 'ai-rank-suite' ), 'note' => __( 'A landing page with all its content in a page builder can land here wrongly — check one before acting.', 'ai-rank-suite' ) ), array( 'id' => 'thin', 'min' => 50, 'max' => 299, 'label' => __( 'Thin', 'ai-rank-suite' ), 'note' => __( 'Rarely enough to answer a question fully enough to be quoted by an assistant.', 'ai-rank-suite' ) ), array( 'id' => 'standard', 'min' => 300, 'max' => 799, 'label' => __( 'Standard', 'ai-rank-suite' ), 'note' => __( 'The ordinary useful length. Most pages should be here.', 'ai-rank-suite' ) ), array( 'id' => 'deep', 'min' => 800, 'max' => 1999, 'label' => __( 'In depth', 'ai-rank-suite' ), 'note' => __( 'Room for sub-questions, which is what gets a page cited for more than one query.', 'ai-rank-suite' ) ), array( 'id' => 'long', 'min' => 2000, 'max' => PHP_INT_MAX, 'label' => __( 'Long form', 'ai-rank-suite' ), 'note' => __( 'Worth checking these have a table of contents — length without structure is harder to quote, not easier.', 'ai-rank-suite' ) ), ); } /** * Words in one post, from the rendered output. * * @param bool $fresh Re-render rather than trusting the stored value. */ public static function count( WP_Post $post, bool $fresh = false ): int { if ( ! $fresh ) { $stored = get_post_meta( $post->ID, self::META, true ); if ( '' !== (string) $stored ) { return (int) $stored; } } $words = Html::word_count( Html::rendered( $post ) ); update_post_meta( $post->ID, self::META, $words ); return $words; } public static function minutes( int $words ): int { return max( 1, (int) ceil( $words / self::WPM ) ); } /** Which band a count falls in. */ public static function band( int $words ): string { foreach ( self::bands() as $band ) { if ( $words >= $band['min'] && $words <= $band['max'] ) { return (string) $band['id']; } } return 'standard'; } /** * Site-wide distribution, per post type. * * One query over the stored meta. Posts that have never been analysed have * no stored count, and they are reported as unknown rather than as zero — * "you have 400 empty pages" would be a false alarm about a site that has * simply not been scanned yet. * * @param bool $fresh Skip the cache. * @return array */ public static function stats( bool $fresh = false ): array { if ( ! $fresh ) { $cached = get_transient( self::CACHE ); if ( is_array( $cached ) ) { return $cached; } } global $wpdb; $types = array_map( static fn( $t ) => $t->name, Library::types() ); if ( array() === $types ) { return array( 'types' => array(), 'bands' => array(), 'total' => 0 ); } $type_ph = implode( ', ', array_fill( 0, count( $types ), '%s' ) ); // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery $rows = $wpdb->get_results( $wpdb->prepare( "SELECT p.post_type AS type, COUNT(*) AS total, SUM( w.meta_value IS NULL ) AS unknown, SUM( w.meta_value IS NOT NULL AND w.meta_value + 0 < 50 ) AS b_empty, SUM( w.meta_value + 0 BETWEEN 50 AND 299 ) AS b_thin, SUM( w.meta_value + 0 BETWEEN 300 AND 799 ) AS b_standard, SUM( w.meta_value + 0 BETWEEN 800 AND 1999 ) AS b_deep, SUM( w.meta_value + 0 >= 2000 ) AS b_long, AVG( w.meta_value + 0 ) AS avg_words, SUM( w.meta_value + 0 ) AS sum_words, MAX( w.meta_value + 0 ) AS max_words FROM {$wpdb->posts} p LEFT JOIN {$wpdb->postmeta} w ON w.post_id = p.ID AND w.meta_key = %s WHERE p.post_type IN ( {$type_ph} ) AND p.post_status IN ( 'publish', 'draft', 'pending', 'future', 'private' ) GROUP BY p.post_type", array_merge( array( self::META ), $types ) ), ARRAY_A ); // phpcs:enable $labels = array(); foreach ( Library::types() as $type ) { $labels[ $type->name ] = (string) ( $type->labels->name ?? $type->name ); } $out = array(); $totals = array( 'empty' => 0, 'thin' => 0, 'standard' => 0, 'deep' => 0, 'long' => 0, 'unknown' => 0 ); $words = 0; $posts = 0; foreach ( (array) $rows as $row ) { $bands = array( 'empty' => (int) $row['b_empty'], 'thin' => (int) $row['b_thin'], 'standard' => (int) $row['b_standard'], 'deep' => (int) $row['b_deep'], 'long' => (int) $row['b_long'], 'unknown' => (int) $row['unknown'], ); foreach ( $bands as $key => $value ) { $totals[ $key ] += $value; } $words += (int) $row['sum_words']; $posts += (int) $row['total']; $out[] = array( 'type' => (string) $row['type'], 'label' => $labels[ (string) $row['type'] ] ?? (string) $row['type'], 'total' => (int) $row['total'], 'bands' => $bands, 'average' => null !== $row['avg_words'] ? (int) round( (float) $row['avg_words'] ) : null, 'longest' => (int) $row['max_words'], 'words' => (int) $row['sum_words'], ); } $result = array( 'types' => $out, 'bands' => $totals, 'catalog' => self::bands(), 'words' => $words, 'posts' => $posts, 'average' => $posts > 0 ? (int) round( $words / max( 1, $posts - $totals['unknown'] ) ) : 0, 'reading' => self::minutes( $words ), ); set_transient( self::CACHE, $result, 300 ); return $result; } /** * The shortest and longest pages, which is where the work is. * * @return array{shortest: array>, longest: array>} */ public static function extremes( int $limit = 10 ): array { global $wpdb; $types = array_map( static fn( $t ) => $t->name, Library::types() ); $type_ph = implode( ', ', array_fill( 0, max( 1, count( $types ) ), '%s' ) ); // The direction picks one of two literal statements; it is never part // of the text. $pick = static function ( bool $ascending ) use ( $wpdb, $types, $type_ph, $limit ): array { $args = array_merge( array( self::META ), $types, array( $limit ) ); // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery if ( $ascending ) { $rows = $wpdb->get_results( $wpdb->prepare( "SELECT p.ID, p.post_title, p.post_type, w.meta_value + 0 AS words FROM {$wpdb->posts} p INNER JOIN {$wpdb->postmeta} w ON w.post_id = p.ID AND w.meta_key = %s WHERE p.post_type IN ( {$type_ph} ) AND p.post_status = 'publish' ORDER BY words ASC LIMIT %d", $args ), ARRAY_A ); } else { $rows = $wpdb->get_results( $wpdb->prepare( "SELECT p.ID, p.post_title, p.post_type, w.meta_value + 0 AS words FROM {$wpdb->posts} p INNER JOIN {$wpdb->postmeta} w ON w.post_id = p.ID AND w.meta_key = %s WHERE p.post_type IN ( {$type_ph} ) AND p.post_status = 'publish' ORDER BY words DESC LIMIT %d", $args ), ARRAY_A ); } // phpcs:enable return array_map( static fn( array $r ): array => array( 'id' => (int) $r['ID'], 'title' => (string) $r['post_title'], 'type' => (string) $r['post_type'], 'words' => (int) $r['words'], ), (array) $rows ); }; return array( 'shortest' => $pick( true ), 'longest' => $pick( false ), ); } public static function flush(): void { delete_transient( self::CACHE ); } }