Skip to content

Commit 5e1d399

Browse files
committed
JSON encode JSON columns before storage
1 parent 9cbaace commit 5e1d399

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/Schema/Traits/Custom_Table_Query_Methods.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,37 @@ function ( $c ) use ( $prepared_ids ) {
225225
* @return array<string> The prepared statements and values.
226226
*/
227227
protected static function prepare_statements_values( array $entries ): array {
228+
$uid_column = static::uid_column();
229+
$entries = array_map(
230+
function ( $entry ) use ( $uid_column ) {
231+
unset( $entry[ $uid_column ] );
232+
return $entry;
233+
},
234+
$entries
235+
);
236+
237+
$columns = static::get_columns();
238+
239+
$entries = array_map(
240+
function ( $entry ) use ( $columns ) {
241+
foreach ( $columns as $column ) {
242+
if ( ! isset( $entry[ $column->get_name() ] ) ) {
243+
continue;
244+
}
245+
246+
switch ( $column->get_php_type() ) {
247+
case Column::PHP_TYPE_JSON:
248+
$entry[ $column->get_name() ] = wp_json_encode( $entry[ $column->get_name() ] );
249+
break;
250+
default:
251+
break;
252+
}
253+
}
254+
return $entry;
255+
},
256+
$entries
257+
);
258+
228259
$database = Config::get_db();
229260
$columns = array_keys( $entries[0] );
230261
$prepared_columns = implode(

0 commit comments

Comments
 (0)