$cap; if ( $truncated ) { $query_ids = array_slice( $query_ids, 0, $cap ); } $notes = array(); foreach ( $query_ids as $post_id ) { $post = get_post( $post_id ); if ( $post instanceof WP_Post ) { $notes[] = openstation_notes_prepare( $post ); } } $alive_ids = openstation_notes_alive_known_ids( $known_ids ); $removed = array_values( array_diff( array_map( 'intval', $known_ids ), $alive_ids ) ); return array( 'notes' => $notes, 'removed' => $removed, 'serverTimeMs' => (int) floor( microtime( true ) * 1000 ), 'truncated' => $truncated, ); } /** * The `WP_Query` visibility clause shared by the delta queries: the * viewer's own notes in any live status, or anyone's public notes. * * Expressed as two queries merged in PHP (like the REST list) rather * than a hand-built OR — keeps every callsite on standard WP_Query. * * @param array $extra Extra WP_Query args merged into both halves. * @return int[] Matching post ids, own notes first. */ function openstation_notes_query_visible_ids( $extra ) { $user_id = get_current_user_id(); $base = array( 'post_type' => OPENSTATION_NOTES_POST_TYPE, 'fields' => 'ids', 'no_found_rows' => true, ); $own = new WP_Query( array_merge( $base, $extra, array( 'post_status' => array( 'private', 'publish' ), 'author' => $user_id, ) ) ); $public = new WP_Query( array_merge( $base, $extra, array( 'post_status' => 'publish', 'author__not_in' => array( $user_id ), ) ) ); $ids = array_map( 'intval', array_merge( (array) $own->posts, (array) $public->posts ) ); wp_reset_postdata(); return array_values( array_unique( $ids ) ); } /** * Query visible note ids modified since the client's high-water mark. * * @param int $since_ms Last-seen modified timestamp in milliseconds. * @param int $limit Max ids to return (per visibility half). * @return int[] */ function openstation_notes_query_changed_ids( $since_ms, $limit ) { $extra = array( 'posts_per_page' => max( 1, (int) $limit ), 'orderby' => 'modified', 'order' => 'ASC', ); if ( $since_ms > 0 ) { $extra['date_query'] = array( array( 'column' => 'post_modified_gmt', 'after' => gmdate( 'Y-m-d H:i:s', (int) floor( $since_ms / 1000 ) ), 'inclusive' => true, ), ); } return openstation_notes_query_visible_ids( $extra ); } /** * Return the subset of known ids still visible to the viewer. * * @param int[] $known_ids Client-known note ids. * @return int[] */ function openstation_notes_alive_known_ids( $known_ids ) { $known_ids = array_values( array_unique( array_filter( array_map( 'absint', (array) $known_ids ) ) ) ); if ( empty( $known_ids ) ) { return array(); } return openstation_notes_query_visible_ids( array( 'post__in' => $known_ids, 'posts_per_page' => count( $known_ids ), ) ); }