$cap; if ( $truncated ) { $query_ids = array_slice( $query_ids, 0, $cap ); } $notes = array(); foreach ( $query_ids as $post_id ) { if ( ! current_user_can( 'edit_post', $post_id ) ) { continue; } $post = get_post( $post_id ); if ( $post instanceof WP_Post ) { $notes[] = desktop_mode_sticky_notes_shape_guideline( $post ); } } $alive_ids = desktop_mode_sticky_notes_alive_known_ids( $sticky_term_id, $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, ); } /** * Query sticky guideline ids modified since the client's high-water mark. * * @since 0.8.8 * * @param int $sticky_term_id Sticky term id. * @param int $version Last-seen modified timestamp in milliseconds. * @param int $limit Max ids to return. * @return int[] */ function desktop_mode_sticky_notes_query_changed_ids( $sticky_term_id, $version, $limit ) { $args = array( 'post_type' => DESKTOP_MODE_STICKY_NOTES_POST_TYPE, 'post_status' => 'private', 'posts_per_page' => max( 1, (int) $limit ), 'fields' => 'ids', 'orderby' => 'modified', 'order' => 'ASC', 'no_found_rows' => true, 'tax_query' => array( array( 'taxonomy' => DESKTOP_MODE_STICKY_NOTES_TAXONOMY, 'field' => 'term_id', 'terms' => array( (int) $sticky_term_id ), ), ), ); if ( $version > 0 ) { $args['date_query'] = array( array( 'column' => 'post_modified_gmt', 'after' => gmdate( 'Y-m-d H:i:s', (int) floor( $version / 1000 ) ), 'inclusive' => true, ), ); } $query = new WP_Query( $args ); $ids = array_map( 'intval', (array) $query->posts ); wp_reset_postdata(); return $ids; } /** * Return the subset of known ids that still point at visible sticky notes. * * @since 0.8.8 * * @param int $sticky_term_id Sticky term id. * @param int[] $known_ids Client-known guideline ids. * @return int[] */ function desktop_mode_sticky_notes_alive_known_ids( $sticky_term_id, $known_ids ) { $known_ids = array_values( array_unique( array_filter( array_map( 'absint', (array) $known_ids ) ) ) ); if ( empty( $known_ids ) ) { return array(); } $query = new WP_Query( array( 'post_type' => DESKTOP_MODE_STICKY_NOTES_POST_TYPE, 'post_status' => 'private', 'post__in' => $known_ids, 'posts_per_page' => count( $known_ids ), 'fields' => 'ids', 'no_found_rows' => true, 'tax_query' => array( array( 'taxonomy' => DESKTOP_MODE_STICKY_NOTES_TAXONOMY, 'field' => 'term_id', 'terms' => array( (int) $sticky_term_id ), ), ), ) ); $ids = array(); foreach ( (array) $query->posts as $post_id ) { $post_id = (int) $post_id; if ( current_user_can( 'edit_post', $post_id ) ) { $ids[] = $post_id; } } wp_reset_postdata(); return $ids; } /** * Shape a guideline like the Gutenberg REST endpoint's edit context. * * @since 0.8.8 * * @param WP_Post $post Guideline post. * @return array */ function desktop_mode_sticky_notes_shape_guideline( $post ) { $term_ids = wp_get_object_terms( $post->ID, DESKTOP_MODE_STICKY_NOTES_TAXONOMY, array( 'fields' => 'ids' ) ); if ( is_wp_error( $term_ids ) ) { $term_ids = array(); } return array( 'id' => (int) $post->ID, 'slug' => (string) $post->post_name, 'status' => (string) get_post_status( $post ), 'title' => array( 'raw' => (string) get_post_field( 'post_title', $post, 'raw' ), ), 'content' => array( 'raw' => (string) get_post_field( 'post_content', $post, 'raw' ), ), 'excerpt' => array( 'raw' => (string) get_post_field( 'post_excerpt', $post, 'raw' ), ), 'modified' => mysql_to_rfc3339( $post->post_modified ), 'desktop_mode_modified_ms' => desktop_mode_sticky_notes_modified_ms( $post ), 'link' => (string) get_permalink( $post ), 'wp_guideline_type' => array_values( array_map( 'intval', (array) $term_ids ) ), ); } /** * Modified timestamp in milliseconds, derived from GMT post time. * * @since 0.8.8 * * @param WP_Post $post Post. * @return int */ function desktop_mode_sticky_notes_modified_ms( $post ) { return (int) get_post_modified_time( 'U', true, $post ) * 1000; }