Skip to content

Refactor document fields storage#27

Merged
erseco merged 1 commit intomainfrom
feature/remove-specified-fields-from-cpt-document
Oct 14, 2025
Merged

Refactor document fields storage#27
erseco merged 1 commit intomainfrom
feature/remove-specified-fields-from-cpt-document

Conversation

@erseco
Copy link
Copy Markdown
Collaborator

@erseco erseco commented Oct 14, 2025

Summary

  • remove legacy static meta fields from document edit flow in favour of taxonomy-defined schema
  • persist document section content within structured post_content markers and update generators to consume them
  • update legacy preview to read structured sections while keeping backwards compatibility for existing meta

Testing

  • php -l includes/custom-post-types/class-resolate-documents.php
  • php -l includes/class-resolate-document-generator.php
  • php -l includes/class-resolate-admin-helper.php
  • make test (fails: Docker environment unavailable)

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

@erseco erseco merged commit 6f582a9 into main Oct 14, 2025
3 of 4 checks passed
Comment on lines +850 to +889
private function get_structured_field_values( $post_id ) {
$post = get_post( $post_id );
if ( ! $post ) {
return array();
}

$fields = self::parse_structured_content( $post->post_content );
if ( ! empty( $fields ) ) {
return $fields;
}

$fallback = array();
$schema = $this->get_dynamic_fields_schema_for_post( $post_id );
if ( ! empty( $schema ) ) {
foreach ( $schema as $row ) {
if ( empty( $row['slug'] ) ) {
continue;
}
$slug = sanitize_key( $row['slug'] );
if ( '' === $slug ) {
continue;
}
$meta_key = 'resolate_field_' . $slug;
$value = get_post_meta( $post_id, $meta_key, true );
if ( '' === $value ) {
continue;
}
$type = isset( $row['type'] ) ? sanitize_key( $row['type'] ) : 'textarea';
if ( ! in_array( $type, array( 'single', 'textarea', 'rich' ), true ) ) {
$type = 'textarea';
}
$fallback[ $slug ] = array(
'value' => (string) $value,
'type' => $type,
);
}
} elseif ( $post_id > 0 ) {
$saved = get_post_meta( $post_id, 'resolate_annexes', true );
if ( is_array( $saved ) ) {
$annexes = $saved; }
}
if ( ! empty( $annexes ) ) {
$blocks[] = '<!-- wp:heading {"level":2} -->' . "\n" . '<h2>' . esc_html__( 'Anexos', 'resolate' ) . '</h2>' . "\n" . '<!-- /wp:heading -->';
$roman = function ( $num ) {
$map = array(
'M' => 1000,
'CM' => 900,
'D' => 500,
'CD' => 400,
'C' => 100,
'XC' => 90,
'L' => 50,
'XL' => 40,
'X' => 10,
'IX' => 9,
'V' => 5,
'IV' => 4,
'I' => 1,
);
$res = '';
foreach ( $map as $rom => $int ) {
while ( $num >= $int ) {
$res .= $rom;
$num -= $int; }
}
return $res;
};
foreach ( $annexes as $i => $anx ) {
$title = isset( $anx['title'] ) ? sanitize_text_field( $anx['title'] ) : '';
$text = isset( $anx['text'] ) ? wp_kses_post( $anx['text'] ) : '';
$blocks[] = '<!-- wp:heading {"level":3} -->' . "\n" . '<h3>' . esc_html( sprintf( 'Anexo %s', $roman( $i + 1 ) ) ) . ( $title ? ': ' . esc_html( $title ) : '' ) . '</h3>' . "\n" . '<!-- /wp:heading -->';
$blocks[] = '<!-- wp:html -->' . "\n" . $text . "\n" . '<!-- /wp:html -->';

return $fallback;
}

Check warning

Code scanning / PHPMD

Code Size Rules: CyclomaticComplexity Warning

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

https://github.com/erseco/wp-resolate/blob/21e3bbdfe946ccffadc5513c9f64a6d4bdca1c2a/includes/class-resolate-admin-helper.php#L843-L845
P1 Badge Legacy preview drops required wrapper markup

The refactor removed the initial HTML scaffolding and the <div id="content-flow"> element that used to wrap the preview content, but the JS at the bottom still does const flow = document.getElementById("content-flow") and immediately accesses flow.children. When render_legacy_preview() runs, flow is now null and the script throws before rendering pages, so the fallback preview stays blank and cannot print. Consider restoring the container markup or guarding against a missing node before iterating.

ℹ️ 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 on lines 290 to 293
$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'] : '' ),
'title' => get_the_title( $post_id ),
'margen' => wp_strip_all_tags( isset( $opts['doc_margin_text'] ) ? $opts['doc_margin_text'] : '' ),
);
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 Merge field builder no longer returns legacy meta when no schema

After the change, build_merge_fields() only seeds the array with title and margen (and then iterates over taxonomy schema). For documents that either lack a resolate_doc_type term or have no schema defined—common for posts created before this refactor—the legacy meta keys such as resolate_objeto, resolate_antecedentes, etc. are never read and their placeholders remain empty during export. The previous implementation always included those meta values regardless of taxonomy configuration, so this breaks existing templates for legacy content. A fallback to the old meta keys when no schema/structured sections are available would preserve compatibility.

Useful? React with 👍 / 👎.

@erseco erseco deleted the feature/remove-specified-fields-from-cpt-document branch November 30, 2025 18:29
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