Fix embedded PDF preview by streaming inline response#21
Merged
Conversation
Comment on lines
+290
to
+355
| public function handle_preview_stream() { | ||
| $post_id = isset( $_GET['post_id'] ) ? intval( $_GET['post_id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended | ||
|
|
||
| if ( ! $post_id || ! current_user_can( 'edit_post', $post_id ) ) { | ||
| wp_die( esc_html__( 'Permisos insuficientes.', 'resolate' ) ); | ||
| } | ||
|
|
||
| if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'resolate_preview_stream_' . $post_id ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended | ||
| wp_die( esc_html__( 'Nonce no válido.', 'resolate' ) ); | ||
| } | ||
|
|
||
| $user_id = get_current_user_id(); | ||
| if ( $user_id <= 0 ) { | ||
| wp_die( esc_html__( 'Usuario no autenticado.', 'resolate' ) ); | ||
| } | ||
|
|
||
| $key = $this->get_preview_stream_transient_key( $post_id, $user_id ); | ||
| $filename = get_transient( $key ); | ||
|
|
||
| if ( false === $filename || '' === $filename ) { | ||
| $this->ensure_document_generator(); | ||
| $result = Resolate_Document_Generator::generate_pdf( $post_id ); | ||
| if ( is_wp_error( $result ) ) { | ||
| wp_die( esc_html__( 'No se pudo generar el PDF para la vista previa.', 'resolate' ) ); | ||
| } | ||
|
|
||
| $filename = basename( $result ); | ||
| $this->remember_preview_stream_file( $post_id, $filename ); | ||
| } | ||
|
|
||
| $filename = sanitize_file_name( (string) $filename ); | ||
| if ( '' === $filename ) { | ||
| wp_die( esc_html__( 'Archivo de vista previa no disponible.', 'resolate' ) ); | ||
| } | ||
|
|
||
| $upload_dir = wp_upload_dir(); | ||
| $path = trailingslashit( $upload_dir['basedir'] ) . 'resolate/' . $filename; | ||
|
|
||
| if ( ! file_exists( $path ) || ! is_readable( $path ) ) { | ||
| wp_die( esc_html__( 'No se pudo acceder al archivo PDF generado.', 'resolate' ) ); | ||
| } | ||
|
|
||
| $filesize = filesize( $path ); | ||
| $download_name = wp_basename( $filename ); | ||
| $encoded_name = rawurlencode( $download_name ); | ||
| $disposition = 'inline; filename="' . $download_name . '"; filename*=UTF-8\'\'' . $encoded_name; | ||
|
|
||
| status_header( 200 ); | ||
| nocache_headers(); | ||
| header( 'Content-Type: application/pdf' ); | ||
| header( 'Content-Disposition: ' . $disposition ); | ||
| if ( $filesize > 0 ) { | ||
| header( 'Content-Length: ' . $filesize ); | ||
| } | ||
|
|
||
| $handle = fopen( $path, 'rb' ); | ||
| if ( false === $handle ) { | ||
| wp_die( esc_html__( 'No se pudo leer el archivo PDF.', 'resolate' ) ); | ||
| } | ||
|
|
||
| while ( ! feof( $handle ) ) { | ||
| echo fread( $handle, 8192 ); | ||
| } | ||
| fclose( $handle ); | ||
| exit; | ||
| } |
Check warning
Code scanning / PHPMD
Code Size Rules: CyclomaticComplexity Warning
Comment on lines
+290
to
+355
| public function handle_preview_stream() { | ||
| $post_id = isset( $_GET['post_id'] ) ? intval( $_GET['post_id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended | ||
|
|
||
| if ( ! $post_id || ! current_user_can( 'edit_post', $post_id ) ) { | ||
| wp_die( esc_html__( 'Permisos insuficientes.', 'resolate' ) ); | ||
| } | ||
|
|
||
| if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'resolate_preview_stream_' . $post_id ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended | ||
| wp_die( esc_html__( 'Nonce no válido.', 'resolate' ) ); | ||
| } | ||
|
|
||
| $user_id = get_current_user_id(); | ||
| if ( $user_id <= 0 ) { | ||
| wp_die( esc_html__( 'Usuario no autenticado.', 'resolate' ) ); | ||
| } | ||
|
|
||
| $key = $this->get_preview_stream_transient_key( $post_id, $user_id ); | ||
| $filename = get_transient( $key ); | ||
|
|
||
| if ( false === $filename || '' === $filename ) { | ||
| $this->ensure_document_generator(); | ||
| $result = Resolate_Document_Generator::generate_pdf( $post_id ); | ||
| if ( is_wp_error( $result ) ) { | ||
| wp_die( esc_html__( 'No se pudo generar el PDF para la vista previa.', 'resolate' ) ); | ||
| } | ||
|
|
||
| $filename = basename( $result ); | ||
| $this->remember_preview_stream_file( $post_id, $filename ); | ||
| } | ||
|
|
||
| $filename = sanitize_file_name( (string) $filename ); | ||
| if ( '' === $filename ) { | ||
| wp_die( esc_html__( 'Archivo de vista previa no disponible.', 'resolate' ) ); | ||
| } | ||
|
|
||
| $upload_dir = wp_upload_dir(); | ||
| $path = trailingslashit( $upload_dir['basedir'] ) . 'resolate/' . $filename; | ||
|
|
||
| if ( ! file_exists( $path ) || ! is_readable( $path ) ) { | ||
| wp_die( esc_html__( 'No se pudo acceder al archivo PDF generado.', 'resolate' ) ); | ||
| } | ||
|
|
||
| $filesize = filesize( $path ); | ||
| $download_name = wp_basename( $filename ); | ||
| $encoded_name = rawurlencode( $download_name ); | ||
| $disposition = 'inline; filename="' . $download_name . '"; filename*=UTF-8\'\'' . $encoded_name; | ||
|
|
||
| status_header( 200 ); | ||
| nocache_headers(); | ||
| header( 'Content-Type: application/pdf' ); | ||
| header( 'Content-Disposition: ' . $disposition ); | ||
| if ( $filesize > 0 ) { | ||
| header( 'Content-Length: ' . $filesize ); | ||
| } | ||
|
|
||
| $handle = fopen( $path, 'rb' ); | ||
| if ( false === $handle ) { | ||
| wp_die( esc_html__( 'No se pudo leer el archivo PDF.', 'resolate' ) ); | ||
| } | ||
|
|
||
| while ( ! feof( $handle ) ) { | ||
| echo fread( $handle, 8192 ); | ||
| } | ||
| fclose( $handle ); | ||
| exit; | ||
| } |
Check warning
Code scanning / PHPMD
Code Size Rules: NPathComplexity Warning
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Testing
https://chatgpt.com/codex/tasks/task_e_68eddd4f38588322a049ca41434f06d0