Improve preview with loading and added tests#55
Merged
Conversation
Comment on lines
+1063
to
+1126
| public function ajax_generate_document() { | ||
| $post_id = isset( $_POST['post_id'] ) ? intval( $_POST['post_id'] ) : 0; | ||
| $format = isset( $_POST['format'] ) ? sanitize_key( $_POST['format'] ) : 'pdf'; | ||
| $output = isset( $_POST['output'] ) ? sanitize_key( $_POST['output'] ) : 'download'; | ||
|
|
||
| if ( ! $post_id || ! current_user_can( 'edit_post', $post_id ) ) { | ||
| wp_send_json_error( array( 'message' => __( 'Permisos insuficientes.', 'documentate' ) ) ); | ||
| } | ||
|
|
||
| if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'documentate_generate_' . $post_id ) ) { | ||
| wp_send_json_error( array( 'message' => __( 'Nonce no válido.', 'documentate' ) ) ); | ||
| } | ||
|
|
||
| $this->ensure_document_generator(); | ||
|
|
||
| $result = null; | ||
|
|
||
| switch ( $format ) { | ||
| case 'docx': | ||
| $result = Documentate_Document_Generator::generate_docx( $post_id ); | ||
| break; | ||
| case 'odt': | ||
| $result = Documentate_Document_Generator::generate_odt( $post_id ); | ||
| break; | ||
| case 'pdf': | ||
| default: | ||
| $result = Documentate_Document_Generator::generate_pdf( $post_id ); | ||
| break; | ||
| } | ||
|
|
||
| if ( is_wp_error( $result ) ) { | ||
| wp_send_json_error( array( 'message' => $result->get_error_message() ) ); | ||
| } | ||
|
|
||
| // Build the URL for download/preview. | ||
| $nonce_action = 'preview' === $output ? 'documentate_preview_' . $post_id : 'documentate_export_' . $post_id; | ||
| $nonce = wp_create_nonce( $nonce_action ); | ||
|
|
||
| if ( 'preview' === $output ) { | ||
| // For preview, use the preview stream URL. | ||
| $this->remember_preview_stream_file( $post_id, basename( $result ) ); | ||
| $url = add_query_arg( | ||
| array( | ||
| 'action' => 'documentate_preview_stream', | ||
| 'post_id' => $post_id, | ||
| '_wpnonce' => wp_create_nonce( 'documentate_preview_stream_' . $post_id ), | ||
| ), | ||
| admin_url( 'admin-post.php' ) | ||
| ); | ||
| } else { | ||
| // For download, use the export URL. | ||
| $action_name = 'documentate_export_' . $format; | ||
| $url = add_query_arg( | ||
| array( | ||
| 'action' => $action_name, | ||
| 'post_id' => $post_id, | ||
| '_wpnonce' => $nonce, | ||
| ), | ||
| admin_url( 'admin-post.php' ) | ||
| ); | ||
| } | ||
|
|
||
| wp_send_json_success( array( 'url' => $url ) ); | ||
| } |
Check warning
Code scanning / PHPMD
Code Size Rules: CyclomaticComplexity Warning
Comment on lines
+1063
to
+1126
| public function ajax_generate_document() { | ||
| $post_id = isset( $_POST['post_id'] ) ? intval( $_POST['post_id'] ) : 0; | ||
| $format = isset( $_POST['format'] ) ? sanitize_key( $_POST['format'] ) : 'pdf'; | ||
| $output = isset( $_POST['output'] ) ? sanitize_key( $_POST['output'] ) : 'download'; | ||
|
|
||
| if ( ! $post_id || ! current_user_can( 'edit_post', $post_id ) ) { | ||
| wp_send_json_error( array( 'message' => __( 'Permisos insuficientes.', 'documentate' ) ) ); | ||
| } | ||
|
|
||
| if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'documentate_generate_' . $post_id ) ) { | ||
| wp_send_json_error( array( 'message' => __( 'Nonce no válido.', 'documentate' ) ) ); | ||
| } | ||
|
|
||
| $this->ensure_document_generator(); | ||
|
|
||
| $result = null; | ||
|
|
||
| switch ( $format ) { | ||
| case 'docx': | ||
| $result = Documentate_Document_Generator::generate_docx( $post_id ); | ||
| break; | ||
| case 'odt': | ||
| $result = Documentate_Document_Generator::generate_odt( $post_id ); | ||
| break; | ||
| case 'pdf': | ||
| default: | ||
| $result = Documentate_Document_Generator::generate_pdf( $post_id ); | ||
| break; | ||
| } | ||
|
|
||
| if ( is_wp_error( $result ) ) { | ||
| wp_send_json_error( array( 'message' => $result->get_error_message() ) ); | ||
| } | ||
|
|
||
| // Build the URL for download/preview. | ||
| $nonce_action = 'preview' === $output ? 'documentate_preview_' . $post_id : 'documentate_export_' . $post_id; | ||
| $nonce = wp_create_nonce( $nonce_action ); | ||
|
|
||
| if ( 'preview' === $output ) { | ||
| // For preview, use the preview stream URL. | ||
| $this->remember_preview_stream_file( $post_id, basename( $result ) ); | ||
| $url = add_query_arg( | ||
| array( | ||
| 'action' => 'documentate_preview_stream', | ||
| 'post_id' => $post_id, | ||
| '_wpnonce' => wp_create_nonce( 'documentate_preview_stream_' . $post_id ), | ||
| ), | ||
| admin_url( 'admin-post.php' ) | ||
| ); | ||
| } else { | ||
| // For download, use the export URL. | ||
| $action_name = 'documentate_export_' . $format; | ||
| $url = add_query_arg( | ||
| array( | ||
| 'action' => $action_name, | ||
| 'post_id' => $post_id, | ||
| '_wpnonce' => $nonce, | ||
| ), | ||
| admin_url( 'admin-post.php' ) | ||
| ); | ||
| } | ||
|
|
||
| wp_send_json_success( array( 'url' => $url ) ); | ||
| } |
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.
This pull request introduces a new loading modal feature for document generation actions in the admin interface. The modal provides visual feedback during export and preview operations, handles errors gracefully, and improves the user experience by intercepting AJAX requests for document generation.
New loading modal functionality for document generation:
documentate-loading-modalcomponent styles toadmin/css/documentate-actions.css, including spinner animation, error state, and loading indicators for action buttons.admin/js/documentate-actions.js, using jQuery to intercept export/preview button clicks and manage AJAX requests for document generation.