Skip to content

Enable document conversions for exports#7

Merged
erseco merged 1 commit intomainfrom
feature/enable-pdf-button-and-implement-conversion
Oct 13, 2025
Merged

Enable document conversions for exports#7
erseco merged 1 commit intomainfrom
feature/enable-pdf-button-and-implement-conversion

Conversation

@erseco
Copy link
Copy Markdown
Collaborator

@erseco erseco commented Oct 13, 2025

Summary

  • allow DOCX exports even when only an ODT template exists by converting with ZetaJS
  • allow ODT exports from DOCX templates and reuse shared helpers for template resolution
  • update the admin actions metabox to enable the PDF button when conversion prerequisites are met

Testing

  • make lint (fails: Docker is not running in the container)

https://chatgpt.com/codex/tasks/task_e_68ecbcfb03208322865666559126a188

@erseco erseco merged commit 8b72063 into main Oct 13, 2025
3 of 4 checks passed
Comment on lines +206 to +255
public static function get_template_path( $post_id, $format ) {
$format = sanitize_key( $format );
if ( ! in_array( $format, array( 'docx', 'odt' ), true ) ) {
return '';
}

$tpl_id = 0;
$opts = get_option( 'resolate_settings', array() );
$types = wp_get_post_terms( $post_id, 'resolate_doc_type', array( 'fields' => 'ids' ) );
if ( ! is_wp_error( $types ) && ! empty( $types ) ) {
$type_id = intval( $types[0] );
$type_template = intval( get_term_meta( $type_id, 'resolate_type_template_id', true ) );
$template_kind = sanitize_key( (string) get_term_meta( $type_id, 'resolate_type_template_type', true ) );
if ( $type_template > 0 ) {
if ( $format === $template_kind ) {
$tpl_id = $type_template;
} elseif ( '' === $template_kind ) {
$path = get_attached_file( $type_template );
if ( $path && $format === strtolower( pathinfo( $path, PATHINFO_EXTENSION ) ) ) {
$tpl_id = $type_template;
}
}
}
if ( $tpl_id <= 0 ) {
$meta_key = 'resolate_type_' . $format . '_template';
$tpl_id = intval( get_term_meta( $type_id, $meta_key, true ) );
}
}

if ( $tpl_id <= 0 ) {
$opt_key = $format . '_template_id';
$tpl_id = isset( $opts[ $opt_key ] ) ? intval( $opts[ $opt_key ] ) : 0;
}

if ( $tpl_id <= 0 ) {
return '';
}

$template_path = get_attached_file( $tpl_id );
if ( ! $template_path || ! file_exists( $template_path ) ) {
return '';
}

$ext = strtolower( pathinfo( $template_path, PATHINFO_EXTENSION ) );
if ( $format !== $ext ) {
return '';
}

return $template_path;
}

Check warning

Code scanning / PHPMD

Code Size Rules: CyclomaticComplexity Warning

The method get_template_path() has a Cyclomatic Complexity of 14. The configured cyclomatic complexity threshold is 10.
Comment on lines +206 to +255
public static function get_template_path( $post_id, $format ) {
$format = sanitize_key( $format );
if ( ! in_array( $format, array( 'docx', 'odt' ), true ) ) {
return '';
}

$tpl_id = 0;
$opts = get_option( 'resolate_settings', array() );
$types = wp_get_post_terms( $post_id, 'resolate_doc_type', array( 'fields' => 'ids' ) );
if ( ! is_wp_error( $types ) && ! empty( $types ) ) {
$type_id = intval( $types[0] );
$type_template = intval( get_term_meta( $type_id, 'resolate_type_template_id', true ) );
$template_kind = sanitize_key( (string) get_term_meta( $type_id, 'resolate_type_template_type', true ) );
if ( $type_template > 0 ) {
if ( $format === $template_kind ) {
$tpl_id = $type_template;
} elseif ( '' === $template_kind ) {
$path = get_attached_file( $type_template );
if ( $path && $format === strtolower( pathinfo( $path, PATHINFO_EXTENSION ) ) ) {
$tpl_id = $type_template;
}
}
}
if ( $tpl_id <= 0 ) {
$meta_key = 'resolate_type_' . $format . '_template';
$tpl_id = intval( get_term_meta( $type_id, $meta_key, true ) );
}
}

if ( $tpl_id <= 0 ) {
$opt_key = $format . '_template_id';
$tpl_id = isset( $opts[ $opt_key ] ) ? intval( $opts[ $opt_key ] ) : 0;
}

if ( $tpl_id <= 0 ) {
return '';
}

$template_path = get_attached_file( $tpl_id );
if ( ! $template_path || ! file_exists( $template_path ) ) {
return '';
}

$ext = strtolower( pathinfo( $template_path, PATHINFO_EXTENSION ) );
if ( $format !== $ext ) {
return '';
}

return $template_path;
}

Check warning

Code scanning / PHPMD

Code Size Rules: NPathComplexity Warning

The method get_template_path() has an NPath complexity of 336. The configured NPath complexity threshold is 200.
Comment on lines +290 to +332
private static function build_merge_fields( $post_id ) {
$opts = get_option( 'resolate_settings', array() );

$fields = array(
'title' => get_the_title( $post_id ),
'objeto' => wp_strip_all_tags( (string) get_post_meta( $post_id, 'resolate_objeto', true ) ),
'antecedentes' => wp_strip_all_tags( (string) get_post_meta( $post_id, 'resolate_antecedentes', true ) ),
'fundamentos' => wp_strip_all_tags( (string) get_post_meta( $post_id, 'resolate_fundamentos', true ) ),
'dispositivo' => wp_strip_all_tags( (string) get_post_meta( $post_id, 'resolate_dispositivo', true ) ),
'firma' => wp_strip_all_tags( (string) get_post_meta( $post_id, 'resolate_firma', true ) ),
'margen' => wp_strip_all_tags( isset( $opts['doc_margin_text'] ) ? $opts['doc_margin_text'] : '' ),
);

$types = wp_get_post_terms( $post_id, 'resolate_doc_type', array( 'fields' => 'ids' ) );
if ( ! is_wp_error( $types ) && ! empty( $types ) ) {
$type_id = intval( $types[0] );
$schema = self::get_type_schema( $type_id );
foreach ( $schema as $def ) {
if ( empty( $def['slug'] ) ) {
continue;
}
$slug = sanitize_key( $def['slug'] );
$val = get_post_meta( $post_id, 'resolate_field_' . $slug, true );
$fields[ $slug ] = wp_strip_all_tags( (string) $val );
}

$logos = get_term_meta( $type_id, 'resolate_type_logos', true );
if ( is_array( $logos ) && ! empty( $logos ) ) {
$i = 1;
foreach ( $logos as $att_id ) {
$att_id = intval( $att_id );
if ( $att_id <= 0 ) {
continue;
}
$fields[ 'logo' . $i . '_path' ] = get_attached_file( $att_id );
$fields[ 'logo' . $i . '_url' ] = wp_get_attachment_url( $att_id );
$i++;
}
}
}

return $fields;
}

Check warning

Code scanning / PHPMD

Code Size Rules: CyclomaticComplexity Warning

The method build_merge_fields() has a Cyclomatic Complexity of 10. The configured cyclomatic complexity threshold is 10.
@erseco erseco deleted the feature/enable-pdf-button-and-implement-conversion branch February 26, 2026 16:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants