$types, 'post_status' => 'publish', 'has_password' => false, 'posts_per_page' => -1, 'orderby' => 'date', 'order' => 'DESC', 'no_found_rows' => true, 'suppress_filters' => false, 'fields' => 'ids', ) ) ); // Category-based permalinks look up each post's categories: load them per chunk, not per post. $terms_in_link = false !== strpos( (string) get_option( 'permalink_structure' ), '%category%' ); foreach ( array_chunk( $ids, 500 ) as $chunk ) { if ( $terms_in_link ) { update_object_term_cache( $chunk, $types ); } foreach ( self::light_posts( $chunk, $excerpt ) as $post ) { $url = CFSP_Exporter::normalize( get_permalink( $post ) ); if ( ! $url || CFSP_Exporter::is_excluded( $url ) ) { continue; } $entry = array( 't' => html_entity_decode( wp_strip_all_tags( get_the_title( $post ) ), ENT_QUOTES, 'UTF-8' ), 'u' => CFSP_Exporter::replace_origin( $url ), 'd' => get_the_date( 'Y-m-d', $post ), ); if ( $excerpt ) { $text = $post->post_excerpt ? $post->post_excerpt : strip_shortcodes( $post->post_content ); $entry['e'] = html_entity_decode( wp_trim_words( wp_strip_all_tags( excerpt_remove_blocks( $text ) ), 30, '…' ), ENT_QUOTES, 'UTF-8' ); } $entries[] = $entry; } } } CFSP_Exporter::write_file( self::INDEX, wp_json_encode( $entries, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) ); CFSP_Exporter::write_file( self::SCRIPT, file_get_contents( CFSP_DIR . 'assets/search.js' ) ); return count( $entries ); } /** * Posts with only the fields the index needs (title, address, date; content only for excerpts), * in the order of $ids. Loading full posts would read every post's content and meta, which on a * large site turns each push into minutes of work. * * @param int[] $ids * @return WP_Post[] */ private static function light_posts( array $ids, $with_content ) { global $wpdb; if ( ! $ids ) { return array(); } // phpcs:disable WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Read-only lookup of core columns by ID, one %d placeholder per ID. if ( $with_content ) { $rows = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_author, post_date, post_date_gmt, post_title, post_excerpt, post_status, post_name, post_modified, post_modified_gmt, post_parent, post_type, menu_order, post_password, comment_status, ping_status, post_content FROM {$wpdb->posts} WHERE ID IN (" . implode( ',', array_fill( 0, count( $ids ), '%d' ) ) . ')', $ids ) ); } else { $rows = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_author, post_date, post_date_gmt, post_title, post_excerpt, post_status, post_name, post_modified, post_modified_gmt, post_parent, post_type, menu_order, post_password, comment_status, ping_status FROM {$wpdb->posts} WHERE ID IN (" . implode( ',', array_fill( 0, count( $ids ), '%d' ) ) . ')', $ids ) ); } // phpcs:enable $by_id = array(); foreach ( (array) $rows as $row ) { $row->filter = 'raw'; $by_id[ (int) $row->ID ] = new WP_Post( $row ); } $out = array(); foreach ( $ids as $id ) { if ( isset( $by_id[ $id ] ) ) { $out[] = $by_id[ $id ]; } } return $out; } /** Add the search script to a page. */ public static function inject( $html ) { if ( false !== strpos( $html, self::SCRIPT ) ) { return $html; } $base = CFSP_Settings::url_base(); // Fuse.js does the matching; search.js falls back to exact-word matching if it can't load. // The tag goes into the saved static file, after WordPress has rendered the page. $tag = wp_get_script_tag( array( 'src' => $base . '/' . self::SCRIPT, 'data-index' => $base . '/' . self::INDEX, 'data-engine' => 'fuse', 'data-fuse' => $base . '/' . self::FUSE, 'data-threshold' => number_format( CFSP_Settings::get( 'search_fuzziness' ) / 10, 1, '.', '' ), 'data-min' => (string) (int) CFSP_Settings::get( 'search_min_chars' ), 'data-limit' => (string) (int) CFSP_Settings::get( 'search_limit' ), 'defer' => true, ) ); if ( false !== stripos( $html, '' ) ) { return preg_replace( '##i', addcslashes( trim( $tag ), '\\$' ) . ' ', $html, 1 ); } return $html . trim( $tag ); } }