From e54721c01b6a38062911013968c5b1ca15b026d2 Mon Sep 17 00:00:00 2001 From: Pedro de Carvalho
Date: Sun, 22 Feb 2026 20:00:13 +0000 Subject: [PATCH 1/3] Allow models to pass schema for meta fields --- src/Features/Meta/CodestarMeta.php | 57 ++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 18 deletions(-) diff --git a/src/Features/Meta/CodestarMeta.php b/src/Features/Meta/CodestarMeta.php index 481bafd..c631965 100644 --- a/src/Features/Meta/CodestarMeta.php +++ b/src/Features/Meta/CodestarMeta.php @@ -80,7 +80,7 @@ private function create_metabox( $box_id, $box_settings ) { $this->create_section( $box_id, $box_settings ); } - if ( isset( $box_settings['sections'] ) ) { + if ( isset( $box_settings['sections'] ) && is_array( $box_settings['sections'] ) ) { foreach ( $box_settings['sections'] as $section ) { if ( empty( $section['fields'] ) ) { continue; @@ -110,26 +110,32 @@ private function register_rest_api( $box_id, $box_settings ) { if ( ! empty( $box_settings['data_type'] ) && $box_settings['data_type'] === 'serialize' ) { $post_type = $this->name; - foreach ( $box_settings['sections'] as $section ) { - if ( ! empty( $section['fields'] ) ) { - $this->create_meta_fields_serialized( $section['fields'], $box_id, $post_type ); + if ( ! empty( $box_settings['sections'] ) && is_array( $box_settings['sections'] ) ) { + foreach ( $box_settings['sections'] as $section ) { + if ( ! empty( $section['fields'] ) ) { + $this->create_meta_fields_serialized( $section['fields'], $box_id, $post_type ); + } } + } elseif ( ! empty( $box_settings['fields'] ) ) { + $this->create_meta_fields_serialized( $box_settings['fields'], $box_id, $post_type ); } } if ( empty( $box_settings['data_type'] ) || $box_settings['data_type'] === 'unserialize' ) { $post_type = $this->name; - foreach ( $box_settings['sections'] as $section ) { - if ( ! empty( $section['fields'] ) ) { - foreach ( $section['fields'] as $meta_name => $want_to_register_fields ) { - $meta_type = 'object'; - if ( ! empty( $want_to_register_fields['type'] ) ) { - $meta_type = $want_to_register_fields['type']; + if ( ! empty( $box_settings['sections'] ) && is_array( $box_settings['sections'] ) ) { + foreach ( $box_settings['sections'] as $section ) { + if ( ! empty( $section['fields'] ) ) { + foreach ( $section['fields'] as $meta_name => $want_to_register_fields ) { + $this->create_meta_fields_not_serialized( $meta_name, $want_to_register_fields, $post_type ); } - $this->create_meta_fields_not_serialized( $meta_name, $meta_type, $post_type ); } } + } elseif ( ! empty( $box_settings['fields'] ) ) { + foreach ( $box_settings['fields'] as $meta_name => $want_to_register_fields ) { + $this->create_meta_fields_not_serialized( $meta_name, $want_to_register_fields, $post_type ); + } } } } @@ -168,14 +174,33 @@ private function setup_restapi_fields( $fields ) { * @param string $meta_type Type of the meta field * @param string $post_type Post type to register the meta field for */ - private function create_meta_fields_not_serialized( $meta_name, $meta_type, $post_type ) { + private function create_meta_fields_not_serialized( $meta_name, $field_args, $post_type ) { + + $meta_type = $field_args['type'] ?? 'object'; $rest_types = $this->match_fields(); $rest_type = $this->get_field_type( $meta_type, $rest_types ); add_action( 'rest_api_init', - function () use ( $post_type, $meta_name, $rest_type ) { + function () use ( $post_type, $meta_name, $rest_type, $field_args ) { + + $show_in_rest = true; + + if ( ! empty( $field_args['schema'] ) ) { + $show_in_rest = [ + 'schema' => $field_args['schema'], + ]; + } elseif ( $rest_type === 'array' ) { + $show_in_rest = [ + 'schema' => [ + 'items' => [ + 'type' => 'string', + ], + ], + ]; + } + register_meta( 'post', $meta_name, @@ -183,11 +208,7 @@ function () use ( $post_type, $meta_name, $rest_type ) { 'object_subtype' => $post_type, 'type' => $rest_type, 'single' => true, - 'show_in_rest' => [ - 'prepare_callback' => function ( $value ) { - return wp_json_encode( $value ); - }, - ], + 'show_in_rest' => $show_in_rest, ) ); } From f57a062f28d253709314c7cd64ce6ccd923b3224 Mon Sep 17 00:00:00 2001 From: Pedro de Carvalho
Date: Mon, 23 Feb 2026 03:42:00 +0000 Subject: [PATCH 2/3] Fix schema for other types of meta, like repeater --- src/Features/Meta/CodestarMeta.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Features/Meta/CodestarMeta.php b/src/Features/Meta/CodestarMeta.php index c631965..cce3a88 100644 --- a/src/Features/Meta/CodestarMeta.php +++ b/src/Features/Meta/CodestarMeta.php @@ -183,7 +183,7 @@ private function create_meta_fields_not_serialized( $meta_name, $field_args, $po add_action( 'rest_api_init', - function () use ( $post_type, $meta_name, $rest_type, $field_args ) { + function () use ( $post_type, $meta_name, $meta_type, $rest_type, $field_args ) { $show_in_rest = true; @@ -195,7 +195,7 @@ function () use ( $post_type, $meta_name, $rest_type, $field_args ) { $show_in_rest = [ 'schema' => [ 'items' => [ - 'type' => 'string', + 'type' => ( $meta_type === 'repeater' ) ? 'object' : 'string', ], ], ]; From 00d8967a449ea1bbe6014c139d1cdd9965e12cf8 Mon Sep 17 00:00:00 2001 From: Pedro de Carvalho
Date: Mon, 23 Feb 2026 04:19:31 +0000 Subject: [PATCH 3/3] update docs --- src/Features/Meta/CodestarMeta.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Features/Meta/CodestarMeta.php b/src/Features/Meta/CodestarMeta.php index cce3a88..b0910a2 100644 --- a/src/Features/Meta/CodestarMeta.php +++ b/src/Features/Meta/CodestarMeta.php @@ -171,12 +171,12 @@ private function setup_restapi_fields( $fields ) { * Hooks into REST API * * @param string $meta_name Name of the meta field - * @param string $meta_type Type of the meta field + * @param array $field_args All the field arguments * @param string $post_type Post type to register the meta field for */ private function create_meta_fields_not_serialized( $meta_name, $field_args, $post_type ) { - $meta_type = $field_args['type'] ?? 'object'; + $meta_type = is_array( $field_args ) ? ( $field_args['type'] ?? 'object' ) : $field_args; $rest_types = $this->match_fields(); $rest_type = $this->get_field_type( $meta_type, $rest_types );