Skip to content

Seed demo document types with bundled templates#6

Merged
erseco merged 1 commit intomainfrom
feature/add-test-document-types-on-initialization
Oct 13, 2025
Merged

Seed demo document types with bundled templates#6
erseco merged 1 commit intomainfrom
feature/add-test-document-types-on-initialization

Conversation

@erseco
Copy link
Copy Markdown
Collaborator

@erseco erseco commented Oct 13, 2025

Summary

  • seed demo document types on init using the bundled ODT and DOCX fixtures
  • populate associated term metadata including detected placeholders and template types
  • add a unit test that verifies the seeded document types and their schemas

Testing

  • php -l resolate.php

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

@erseco erseco merged commit 8ad3df0 into main Oct 13, 2025
2 of 3 checks passed
Comment thread resolate.php
Comment on lines +245 to +340
function resolate_maybe_seed_default_doc_types() {
if ( ! taxonomy_exists( 'resolate_doc_type' ) ) {
return;
}

resolate_ensure_default_media();

$options = get_option( 'resolate_settings', array() );

$definitions = array();

$odt_id = isset( $options['odt_template_id'] ) ? intval( $options['odt_template_id'] ) : 0;
if ( $odt_id > 0 ) {
$definitions[] = array(
'slug' => 'resolate-demo-odt',
'name' => __( 'Tipo de documento de prueba (ODT)', 'resolate' ),
'description' => __( 'Ejemplo creado automáticamente con la plantilla ODT incluida.', 'resolate' ),
'color' => '#37517e',
'template_id' => $odt_id,
'fixture_key' => 'resolate-demo-odt',
);
}

$docx_id = isset( $options['docx_template_id'] ) ? intval( $options['docx_template_id'] ) : 0;
if ( $docx_id > 0 ) {
$definitions[] = array(
'slug' => 'resolate-demo-docx',
'name' => __( 'Tipo de documento de prueba (DOCX)', 'resolate' ),
'description' => __( 'Ejemplo creado automáticamente con la plantilla DOCX incluida.', 'resolate' ),
'color' => '#2a7fb8',
'template_id' => $docx_id,
'fixture_key' => 'resolate-demo-docx',
);
}

if ( empty( $definitions ) ) {
return;
}

if ( ! class_exists( 'Resolate_Template_Parser' ) ) {
require_once plugin_dir_path( __FILE__ ) . 'includes/class-resolate-template-parser.php';
}

foreach ( $definitions as $definition ) {
$slug = $definition['slug'];
$template_id = intval( $definition['template_id'] );
if ( $template_id <= 0 ) {
continue;
}

$term = get_term_by( 'slug', $slug, 'resolate_doc_type' );
$term_id = $term instanceof WP_Term ? intval( $term->term_id ) : 0;

if ( $term_id <= 0 ) {
$created = wp_insert_term(
$definition['name'],
'resolate_doc_type',
array(
'slug' => $slug,
'description' => $definition['description'],
)
);

if ( is_wp_error( $created ) ) {
continue;
}

$term_id = intval( $created['term_id'] );
}

if ( $term_id <= 0 ) {
continue;
}

$fixture_key = get_term_meta( $term_id, '_resolate_fixture', true );
if ( ! empty( $fixture_key ) && $fixture_key !== $definition['fixture_key'] ) {
continue;
}

update_term_meta( $term_id, '_resolate_fixture', $definition['fixture_key'] );
update_term_meta( $term_id, 'resolate_type_color', $definition['color'] );
update_term_meta( $term_id, 'resolate_type_template_id', $template_id );

$path = get_attached_file( $template_id );
if ( ! $path ) {
continue;
}

$template_type = resolate_detect_template_type( $path );
$schema = resolate_build_schema_from_template( $path );

update_term_meta( $term_id, 'resolate_type_template_type', $template_type );
update_term_meta( $term_id, 'schema', $schema );
update_term_meta( $term_id, 'resolate_type_fields', $schema );
}
}

Check warning

Code scanning / PHPMD

Code Size Rules: CyclomaticComplexity Warning

The function resolate_maybe_seed_default_doc_types() has a Cyclomatic Complexity of 25. The configured cyclomatic complexity threshold is 10.
Comment thread resolate.php
Comment on lines +245 to +340
function resolate_maybe_seed_default_doc_types() {
if ( ! taxonomy_exists( 'resolate_doc_type' ) ) {
return;
}

resolate_ensure_default_media();

$options = get_option( 'resolate_settings', array() );

$definitions = array();

$odt_id = isset( $options['odt_template_id'] ) ? intval( $options['odt_template_id'] ) : 0;
if ( $odt_id > 0 ) {
$definitions[] = array(
'slug' => 'resolate-demo-odt',
'name' => __( 'Tipo de documento de prueba (ODT)', 'resolate' ),
'description' => __( 'Ejemplo creado automáticamente con la plantilla ODT incluida.', 'resolate' ),
'color' => '#37517e',
'template_id' => $odt_id,
'fixture_key' => 'resolate-demo-odt',
);
}

$docx_id = isset( $options['docx_template_id'] ) ? intval( $options['docx_template_id'] ) : 0;
if ( $docx_id > 0 ) {
$definitions[] = array(
'slug' => 'resolate-demo-docx',
'name' => __( 'Tipo de documento de prueba (DOCX)', 'resolate' ),
'description' => __( 'Ejemplo creado automáticamente con la plantilla DOCX incluida.', 'resolate' ),
'color' => '#2a7fb8',
'template_id' => $docx_id,
'fixture_key' => 'resolate-demo-docx',
);
}

if ( empty( $definitions ) ) {
return;
}

if ( ! class_exists( 'Resolate_Template_Parser' ) ) {
require_once plugin_dir_path( __FILE__ ) . 'includes/class-resolate-template-parser.php';
}

foreach ( $definitions as $definition ) {
$slug = $definition['slug'];
$template_id = intval( $definition['template_id'] );
if ( $template_id <= 0 ) {
continue;
}

$term = get_term_by( 'slug', $slug, 'resolate_doc_type' );
$term_id = $term instanceof WP_Term ? intval( $term->term_id ) : 0;

if ( $term_id <= 0 ) {
$created = wp_insert_term(
$definition['name'],
'resolate_doc_type',
array(
'slug' => $slug,
'description' => $definition['description'],
)
);

if ( is_wp_error( $created ) ) {
continue;
}

$term_id = intval( $created['term_id'] );
}

if ( $term_id <= 0 ) {
continue;
}

$fixture_key = get_term_meta( $term_id, '_resolate_fixture', true );
if ( ! empty( $fixture_key ) && $fixture_key !== $definition['fixture_key'] ) {
continue;
}

update_term_meta( $term_id, '_resolate_fixture', $definition['fixture_key'] );
update_term_meta( $term_id, 'resolate_type_color', $definition['color'] );
update_term_meta( $term_id, 'resolate_type_template_id', $template_id );

$path = get_attached_file( $template_id );
if ( ! $path ) {
continue;
}

$template_type = resolate_detect_template_type( $path );
$schema = resolate_build_schema_from_template( $path );

update_term_meta( $term_id, 'resolate_type_template_type', $template_type );
update_term_meta( $term_id, 'schema', $schema );
update_term_meta( $term_id, 'resolate_type_fields', $schema );
}
}

Check warning

Code scanning / PHPMD

Code Size Rules: NPathComplexity Warning

The function resolate_maybe_seed_default_doc_types() has an NPath complexity of 663616. The configured NPath complexity threshold is 200.
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting

Comment thread resolate.php
Comment on lines +319 to +338
$fixture_key = get_term_meta( $term_id, '_resolate_fixture', true );
if ( ! empty( $fixture_key ) && $fixture_key !== $definition['fixture_key'] ) {
continue;
}

update_term_meta( $term_id, '_resolate_fixture', $definition['fixture_key'] );
update_term_meta( $term_id, 'resolate_type_color', $definition['color'] );
update_term_meta( $term_id, 'resolate_type_template_id', $template_id );

$path = get_attached_file( $template_id );
if ( ! $path ) {
continue;
}

$template_type = resolate_detect_template_type( $path );
$schema = resolate_build_schema_from_template( $path );

update_term_meta( $term_id, 'resolate_type_template_type', $template_type );
update_term_meta( $term_id, 'schema', $schema );
update_term_meta( $term_id, 'resolate_type_fields', $schema );
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Skip reseeding demo document types once created

The new resolate_maybe_seed_default_doc_types() function is hooked to init and, for every page load, unconditionally calls Resolate_Template_Parser::extract_fields() and updates the term meta for the demo doc types even when those terms already exist with the same _resolate_fixture marker. Parsing both bundled templates via ZipArchive and writing multiple term meta rows on every request is expensive and will slow down normal page loads. Consider bailing out early when the term already has the expected fixture and schema so the costly extraction and updates run only once (e.g., during activation or when the template changes).

Useful? React with 👍 / 👎.

@erseco erseco deleted the feature/add-test-document-types-on-initialization 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