get_row( $wpdb->prepare( "SELECT {$columns} FROM {$table} WHERE post_id = %d AND paged = %d", $post_id, $paged ), ARRAY_A ); if ( ! is_array( $row ) ) { return null; } return new Entry( (int) $row['post_id'], (string) $row['markdown'], (string) $row['content_hash'], (string) $row['source'], (string) $row['generator_version'], (string) $row['generated_at'], (string) $row['state'], null === $row['noindex'] ? null : (bool) (int) $row['noindex'], null === $row['restricted'] ? null : (string) $row['restricted'], (int) $row['paged'] ); } /** * @param Entry $entry Copy to store, replacing any earlier one. * @return void */ public function save( Entry $entry ): void { global $wpdb; $table = Schema::table(); $previous = $wpdb->get_var( $wpdb->prepare( "SELECT content_hash FROM {$table} WHERE post_id = %d AND paged = %d", $entry->post_id, $entry->paged ) ); $wpdb->replace( Schema::table(), array( 'post_id' => $entry->post_id, 'paged' => $entry->paged, 'markdown' => $entry->markdown, 'content_hash' => $entry->content_hash, 'source' => $entry->source, 'generator_version' => $entry->generator_version, 'generated_at' => $entry->generated_at, 'state' => $entry->state, 'noindex' => null === $entry->noindex ? null : (int) $entry->noindex, 'restricted' => $entry->restricted, ), array( '%d', '%d', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%s' ) ); OutputCache::purge(); if ( $previous !== $entry->content_hash ) { $this->announce( $entry->post_id, $entry->paged ); } } /** * @param int $post_id Page ID. * @return void */ public function delete( int $post_id ): void { global $wpdb; $removed = $wpdb->delete( Schema::table(), array( 'post_id' => $post_id ), array( '%d' ) ); delete_post_meta( $post_id, PostMeta::PAGE_DESCRIPTION ); delete_post_meta( $post_id, PostMeta::TITLE ); delete_post_meta( $post_id, PostMeta::VIEWS ); OutputCache::purge(); if ( $removed ) { $this->announce( $post_id ); } } /** * @param int $post_id Page ID. * @return void */ public function mark_stale( int $post_id ): void { global $wpdb; $table = Schema::table(); $wpdb->query( $wpdb->prepare( "UPDATE {$table} SET state = %s WHERE post_id = %d", Entry::STALE, $post_id ) ); } /** * @return void */ public function mark_all_stale(): void { global $wpdb; $table = Schema::table(); $wpdb->query( $wpdb->prepare( "UPDATE {$table} SET state = %s", Entry::STALE ) ); } /** * @return void */ public function clear(): void { global $wpdb; $table = Schema::table(); $wpdb->query( "DELETE FROM {$table}" ); delete_post_meta_by_key( PostMeta::TITLE ); delete_post_meta_by_key( PostMeta::VIEWS ); OutputCache::purge(); /** * Fires after every stored copy has been cleared. */ do_action( 'agentwelcome_cache_cleared' ); } /** * Tells other code that the Markdown at a page's address has changed, so a host or CDN cache can be purged. * * @param int $post_id Page ID. * @param int $paged Number of the paginated view, 1 for the page itself. * @return void */ private function announce( int $post_id, int $paged = 1 ): void { $post = get_post( $post_id ); $url = $post instanceof WP_Post ? $this->urls->markdown_url( $post, $paged ) : null; if ( null === $url ) { return; } /** * Fires after the stored Markdown of a page changed or was removed. * * @param int $post_id Page ID. * @param string $url Markdown address of the page. */ do_action( 'agentwelcome_markdown_updated', $post_id, $url ); } /** * @param int[] $post_ids Page IDs. * @return array Result by page ID, for the pages that have a stored copy. */ public function noindex_flags( array $post_ids ): array { global $wpdb; $ids = array_values( array_filter( array_map( 'absint', $post_ids ) ) ); if ( array() === $ids ) { return array(); } $table = Schema::table(); $marks = implode( ',', array_fill( 0, count( $ids ), '%d' ) ); $rows = (array) $wpdb->get_results( $wpdb->prepare( "SELECT post_id, noindex FROM {$table} WHERE paged = 1 AND post_id IN ({$marks})", $ids ), ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- Placeholders are built from the count of IDs. $flags = array(); foreach ( $rows as $row ) { $flags[ (int) $row['post_id'] ] = null === $row['noindex'] ? null : (bool) (int) $row['noindex']; } return $flags; } /** * @param int[] $post_ids Page IDs. * @return array Reason by page ID, for the pages that have a stored copy. */ public function restricted_reasons( array $post_ids ): array { global $wpdb; $ids = array_values( array_filter( array_map( 'absint', $post_ids ) ) ); if ( array() === $ids ) { return array(); } $table = Schema::table(); $marks = implode( ',', array_fill( 0, count( $ids ), '%d' ) ); $rows = (array) $wpdb->get_results( $wpdb->prepare( "SELECT post_id, restricted FROM {$table} WHERE paged = 1 AND post_id IN ({$marks})", $ids ), ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- Placeholders are built from the count of IDs. $reasons = array(); foreach ( $rows as $row ) { $reasons[ (int) $row['post_id'] ] = null === $row['restricted'] ? null : (string) $row['restricted']; } return $reasons; } /** * @return array{fresh: int, stale: int} Copies per state. */ public function counts(): array { global $wpdb; $table = Schema::table(); $rows = (array) $wpdb->get_results( "SELECT state, COUNT(*) AS total FROM {$table} GROUP BY state", ARRAY_A ); $counts = array( 'fresh' => 0, 'stale' => 0, ); foreach ( $rows as $row ) { $counts[ Entry::STALE === $row['state'] ? 'stale' : 'fresh' ] += (int) $row['total']; } return $counts; } /** * @param int $after Only IDs above this one. * @param int $limit Largest number of IDs to return. * @return int[] Page IDs in ascending order. */ public function ids( int $after, int $limit ): array { global $wpdb; $table = Schema::table(); return array_map( 'intval', (array) $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT post_id FROM {$table} WHERE post_id > %d ORDER BY post_id ASC LIMIT %d", $after, $limit ) ) ); } }