(int) $form_id, '_wpnonce' => wp_create_nonce( 'fwren_preview_' . (int) $form_id ), ), home_url( '/' ) ); } /** @return string Endpoint the editor posts unsaved content to. */ public static function endpoint( $form_id ) { return add_query_arg( 'fwren_preview', (int) $form_id, home_url( '/' ) ); } /** Render the preview page and exit. */ public static function maybe_render() { if ( empty( $_REQUEST['fwren_preview'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification return; } $nonce = isset( $_REQUEST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ) : ''; if ( 'tpl' === $_REQUEST['fwren_preview'] ) { // phpcs:ignore WordPress.Security.NonceVerification self::render_template( sanitize_key( wp_unslash( $_REQUEST['tpl'] ?? '' ) ), $nonce ); // phpcs:ignore WordPress.Security.NonceVerification return; } $form_id = absint( $_REQUEST['fwren_preview'] ); // phpcs:ignore WordPress.Security.NonceVerification if ( ! current_user_can( 'fwren_manage_forms' ) || ! wp_verify_nonce( $nonce, 'fwren_preview_' . $form_id ) || ! Form::exists( $form_id ) ) { wp_die( esc_html__( 'This preview link has expired. Reopen the preview from the form editor.', 'fieldwren' ), esc_html__( 'Preview', 'fieldwren' ), array( 'response' => 403 ) ); } $args = array( 'preview' => true ); $method = isset( $_SERVER['REQUEST_METHOD'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) : ''; if ( 'POST' === $method && isset( $_POST['content'] ) ) { // Unsaved blocks from the editor. Rendered for the editor's own user only; never stored. $args['content'] = (string) wp_unslash( $_POST['content'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Block markup, rendered by the same callbacks as saved content. } self::page( $form_id, get_the_title( $form_id ), Renderer::render( $form_id, $args ) ); } /** Preview a library template (nothing is saved). */ private static function render_template( $id, $nonce ) { $tpl = Templates::get( $id ); if ( ! current_user_can( 'fwren_manage_forms' ) || ! wp_verify_nonce( $nonce, 'fwren_tpl_preview' ) || ! $tpl ) { wp_die( esc_html__( 'This preview link has expired.', 'fieldwren' ), esc_html__( 'Preview', 'fieldwren' ), array( 'response' => 403 ) ); } $post = new \WP_Post( (object) array( 'ID' => 0, 'post_type' => Plugin::POST_TYPE, 'post_status' => 'publish', 'post_title' => $tpl['title'], 'post_content' => $tpl['content'], 'filter' => 'raw', ) ); self::page( 0, $tpl['title'], Renderer::render( 0, array( 'preview' => true, 'post' => $post ) ), $tpl['description'] ); } /** * Print the standalone preview page and exit. * * @param int $form_id Form id (0 for templates). * @param string $title Heading. * @param string $html Rendered form. * @param string $intro Optional line under the heading. */ private static function page( $form_id, $title, $html, $intro = '' ) { show_admin_bar( false ); nocache_headers(); header( 'X-Robots-Tag: noindex, nofollow' ); ?> >