..., 10, 2 ); * * @param int $post_id Post ID. */ public static function public_url( int $post_id ): string { return (string) apply_filters( 'outercite_live_url', (string) get_permalink( $post_id ), $post_id ); } /** * Whether the change on a post was marked live from this site. * * @param int $post_id Post ID. */ public static function is_shipped( int $post_id ): bool { return (bool) get_post_meta( $post_id, self::SHIPPED_AT, true ); } /** * Posts and pages linked to a change, any status but trash. * * @param string $change_id Outercite change id. * @return \WP_Post[] Posts and pages linked to a change, any status but trash. */ public static function posts_for_change( string $change_id ): array { return get_posts( array( 'post_type' => 'any', 'post_status' => array( 'publish', 'draft', 'pending', 'future', 'private' ), 'posts_per_page' => 20, 'meta_key' => self::CHANGE, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key 'meta_value' => $change_id, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value ) ); } /** * Linked posts, keyed by change id. * * @return array Linked posts keyed by change id. */ public static function all_linked(): array { $posts = get_posts( array( 'post_type' => 'any', 'post_status' => array( 'publish', 'draft', 'pending', 'future', 'private' ), // One page of linked posts for the Changes list; a site rarely links more than a few dozen. 'posts_per_page' => 100, 'no_found_rows' => true, 'meta_key' => self::CHANGE, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key ) ); $out = array(); foreach ( $posts as $post ) { $out[ (string) get_post_meta( $post->ID, self::CHANGE, true ) ][] = $post; } return $out; } /** * Link a post to a change. * * @param int $post_id Post ID. * @param string $change_id Outercite change id. * @param string $title Change title, kept for display. */ public static function link( int $post_id, string $change_id, string $title ): void { update_post_meta( $post_id, self::CHANGE, $change_id ); update_post_meta( $post_id, self::TITLE, $title ); delete_post_meta( $post_id, self::SHIPPED_AT ); delete_post_meta( $post_id, self::ASK_LIVE ); } /** * A new draft page holding the change's outline as headings, linked to * the change. Plain blocks only: an Elementor or Divi layout is never * written, the builder starts from an empty page. * * @param string $change_id Outercite change id. * @param array $change The change as GET /api/v1/changes/{id} returns it. * @return int|\WP_Error */ public static function create_draft( string $change_id, array $change ) { $title = isset( $change['title'] ) ? (string) $change['title'] : __( 'New page', 'outercite' ); $headings = isset( $change['brief']['headings'] ) && is_array( $change['brief']['headings'] ) ? $change['brief']['headings'] : array(); $content = ''; foreach ( $headings as $heading ) { $content .= "\n

" . esc_html( (string) $heading ) . "

\n\n\n"; $content .= "\n

\n\n\n"; } $post_id = wp_insert_post( array( 'post_type' => 'page', 'post_status' => 'draft', 'post_title' => $title, 'post_content' => $content, ), true ); if ( is_wp_error( $post_id ) ) { return $post_id; } self::link( (int) $post_id, $change_id, $title ); return (int) $post_id; } }