Skip to content

Commit

Permalink
Safely parse fields from JSON (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
zaguiini authored Sep 3, 2024
1 parent d3b4550 commit 2c04bde
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion includes/runtime/class-content-model.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function __construct( WP_Post $content_model_post ) {

// TODO: Not load this eagerly.
$this->blocks = $this->inflate_template_blocks( $this->template );
$this->fields = json_decode( get_post_meta( $content_model_post->ID, 'fields', true ), true );
$this->fields = $this->parse_fields();
$this->register_meta_fields();

add_action( 'enqueue_block_editor_assets', array( $this, 'maybe_enqueue_templating_scripts' ) );
Expand Down Expand Up @@ -160,6 +160,27 @@ private function register_post_type() {
);
}

/**
* Parses the fields associated with the Content Model.
*
* @return array Fields associated with the Content Model.
*/
private function parse_fields() {
$fields = get_post_meta( $this->post_id, 'fields', true );

if ( ! $fields ) {
return array();
}

$decoded_files = json_decode( $fields, true );

if ( is_array( $decoded_files ) ) {
return $decoded_files;
}

return array();
}


/**
* Recursively inflates (i.e., maps the block into Content_Model_Block) the blocks.
Expand Down

0 comments on commit 2c04bde

Please sign in to comment.