Constants::POST_TYPE_PLACEMENT, 'post_status' => [ 'publish' ], 'posts_per_page' => -1, 'fields' => 'ids', 'no_found_rows' => true, 'cache_results' => false, ] ); $ad_ids = get_posts( [ 'post_type' => Constants::POST_TYPE_AD, 'post_status' => [ 'publish' ], 'posts_per_page' => -1, 'fields' => 'ids', 'no_found_rows' => true, 'cache_results' => false, ] ); $group_ids = get_posts( [ 'post_type' => Constants::POST_TYPE_GROUP, 'post_status' => [ 'publish', 'draft' ], 'posts_per_page' => -1, 'fields' => 'ids', 'no_found_rows' => true, 'cache_results' => false, ] ); $all_ids = array_values( array_unique( array_merge( $placement_ids, $ad_ids, $group_ids ) ) ); if ( empty( $all_ids ) ) { self::store( [], [], [] ); return []; } [ $posts, $meta ] = self::fetch_raw_rows( $all_ids ); // Image attachments referenced by any ad among $ad_ids - a second pass $attachment_ids = []; foreach ( $ad_ids as $ad_id ) { $image_id = isset( $meta[ $ad_id ][ Constants::META_PREFIX . 'image_id' ][0] ) ? (int) maybe_unserialize( $meta[ $ad_id ][ Constants::META_PREFIX . 'image_id' ][0] ) : 0; if ( $image_id > 0 ) { $attachment_ids[] = $image_id; } } $attachment_ids = array_values( array_unique( $attachment_ids ) ); if ( ! empty( $attachment_ids ) ) { [ $attachment_posts, $attachment_meta ] = self::fetch_raw_rows( $attachment_ids ); $posts += $attachment_posts; $meta += $attachment_meta; } self::store( $placement_ids, $posts, $meta ); // Feed WP's own cache too, so the pipeline about to run on this same request doesn't re-query what was just fetched here. self::seed( $posts, $meta ); return $placement_ids; } /** * Raw, unserialized post rows + raw (still-serialized) meta rows for the given IDs. * * @param int[] $ids Post IDs to fetch raw rows for. * * @return array{0: array, 1: array>>} */ private static function fetch_raw_rows( array $ids ): array { global $wpdb; $placeholders = implode( ',', array_fill( 0, count( $ids ), '%d' ) ); // {$placeholders} is a run-time-built '%d,%d,...' list, one per $ids entry, filled in by prepare()'s own $ids argument below. $posts_sql = "SELECT * FROM {$wpdb->posts} WHERE ID IN ({$placeholders})"; // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare $post_rows = $wpdb->get_results( $wpdb->prepare( $posts_sql, $ids ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared $posts = []; foreach ( $post_rows as $row ) { $posts[ (int) $row->ID ] = new WP_Post( $row ); } $meta = array_fill_keys( $ids, [] ); $meta_sql = "SELECT post_id, meta_key, meta_value FROM {$wpdb->postmeta} WHERE post_id IN ({$placeholders})"; // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare $meta_rows = $wpdb->get_results( $wpdb->prepare( $meta_sql, $ids ), ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared foreach ( $meta_rows as $row ) { $meta[ (int) $row['post_id'] ][ $row['meta_key'] ][] = $row['meta_value']; } return [ $posts, $meta ]; } /** * Seeds WordPress's own 'posts'/'post_meta' object-cache groups from persisted rows. * * @param array $posts Raw post rows, keyed by ID. * @param array>> $meta Raw meta rows, keyed by post ID. * * @return void */ private static function seed( array $posts, array $meta ): void { if ( ! empty( $posts ) ) { update_post_cache( $posts ); } foreach ( $meta as $post_id => $post_meta ) { wp_cache_add( $post_id, $post_meta, 'post_meta' ); } } /** * Persists a snapshot payload. * * @param int[] $placement_ids Published Placement IDs. * @param array $posts Raw post rows, keyed by ID. * @param array>> $meta Raw meta rows, keyed by post ID. * * @return void */ private static function store( array $placement_ids, array $posts, array $meta ): void { self::write( self::build_key(), [ 'placement_ids' => $placement_ids, 'posts' => $posts, 'meta' => $meta, ] ); } /** * The generation-suffixed cache key - see CacheGeneration's docblock. * * @return string */ private static function build_key(): string { return 'adpresso_snapshot_' . CacheGeneration::get(); } /** * Object cache when available, transient otherwise. * * @param string $key Cache key. * * @return mixed Null on a miss. */ private static function read( string $key ) { if ( wp_using_ext_object_cache() ) { $found = false; $value = wp_cache_get( $key, self::CACHE_GROUP, false, $found ); return $found ? $value : null; } $value = get_transient( $key ); return false === $value ? null : $value; } /** * Object cache when available, transient otherwise. * * @param string $key Cache key. * @param mixed $data Data to persist. * * @return void */ private static function write( string $key, $data ): void { if ( wp_using_ext_object_cache() ) { wp_cache_set( $key, $data, self::CACHE_GROUP, self::TRANSIENT_TTL ); return; } set_transient( $key, $data, self::TRANSIENT_TTL ); } }