Skip to content

Commit 2c04bde

Browse files
authored
Safely parse fields from JSON (#111)
1 parent d3b4550 commit 2c04bde

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

includes/runtime/class-content-model.php

+22-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function __construct( WP_Post $content_model_post ) {
7979

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

8585
add_action( 'enqueue_block_editor_assets', array( $this, 'maybe_enqueue_templating_scripts' ) );
@@ -160,6 +160,27 @@ private function register_post_type() {
160160
);
161161
}
162162

163+
/**
164+
* Parses the fields associated with the Content Model.
165+
*
166+
* @return array Fields associated with the Content Model.
167+
*/
168+
private function parse_fields() {
169+
$fields = get_post_meta( $this->post_id, 'fields', true );
170+
171+
if ( ! $fields ) {
172+
return array();
173+
}
174+
175+
$decoded_files = json_decode( $fields, true );
176+
177+
if ( is_array( $decoded_files ) ) {
178+
return $decoded_files;
179+
}
180+
181+
return array();
182+
}
183+
163184

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

0 commit comments

Comments
 (0)