Seed demo document types with bundled templates#6
Conversation
| 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
| 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
There was a problem hiding this comment.
💡 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
| $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 ); |
There was a problem hiding this comment.
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 👍 / 👎.
Summary
Testing
https://chatgpt.com/codex/tasks/task_e_68ecbd4c12148322922c370790dc7dfd