Skip to content

Improve preview with loading and added tests#55

Merged
erseco merged 1 commit intomainfrom
feature/improve-print-preview
Nov 28, 2025
Merged

Improve preview with loading and added tests#55
erseco merged 1 commit intomainfrom
feature/improve-print-preview

Conversation

@erseco
Copy link
Copy Markdown
Collaborator

@erseco erseco commented Nov 28, 2025

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:

  • Added documentate-loading-modal component styles to admin/css/documentate-actions.css, including spinner animation, error state, and loading indicators for action buttons.
  • Implemented modal creation, display, error handling, and close logic in admin/js/documentate-actions.js, using jQuery to intercept export/preview button clicks and manage AJAX requests for document generation.
  • Enhanced AJAX flow to show the modal during document generation, update status based on action (preview/download), and handle success or error responses, including network errors.

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

The method ajax_generate_document() has a Cyclomatic Complexity of 14. The configured cyclomatic complexity threshold is 10.
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

The method ajax_generate_document() has an NPath complexity of 1152. The configured NPath complexity threshold is 500.
@erseco erseco merged commit 21903bd into main Nov 28, 2025
4 checks passed
@erseco erseco deleted the feature/improve-print-preview branch November 28, 2025 09:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants