(int) $id, '_bakeout' => wp_create_nonce( 'bakeout_preview_' . (int) $id ), ), $base ); } /** * The current page as it is (for the "before" side). * * @param int $id Post ID. * @return string */ public static function before_url( $id ) { return 'publish' === get_post_status( $id ) ? get_permalink( $id ) : get_preview_post_link( $id ); } /** * Replace the post's content in the main query with the converted markup. * * @param \WP_Post[] $posts Posts. * @param \WP_Query $query Query. * @return \WP_Post[] */ public static function swap( $posts, $query ) { // phpcs:disable WordPress.Security.NonceVerification.Recommended -- verified below with wp_verify_nonce. if ( is_admin() || ! $query->is_main_query() || empty( $_GET['bakeout_preview'] ) || empty( $_GET['_bakeout'] ) ) { return $posts; } $id = absint( $_GET['bakeout_preview'] ); $nonce = sanitize_text_field( wp_unslash( $_GET['_bakeout'] ) ); // phpcs:enable if ( ! wp_verify_nonce( $nonce, 'bakeout_preview_' . $id ) || ! current_user_can( 'edit_post', $id ) ) { return $posts; } foreach ( $posts as $post ) { if ( (int) $post->ID === $id ) { $result = Store::preview( $id ); if ( ! is_wp_error( $result ) ) { $post->post_content = $result['markup']; } } } if ( ! headers_sent() ) { nocache_headers(); } return $posts; } }