@@ -237,31 +237,6 @@ function ( $entry ) use ( $uid_column ) {
237237
238238 $ columns = static ::get_columns ();
239239
240- $ entries = array_map (
241- function ( $ entry ) use ( $ columns ) {
242- foreach ( $ columns as $ column ) {
243- if ( ! isset ( $ entry [ $ column ->get_name () ] ) ) {
244- continue ;
245- }
246-
247- switch ( $ column ->get_php_type () ) {
248- case PHP_Types::JSON :
249- $ entry [ $ column ->get_name () ] = wp_json_encode ( $ entry [ $ column ->get_name () ] );
250- break ;
251- case PHP_Types::BLOB :
252- $ value = $ entry [ $ column ->get_name () ];
253- // Only encode if not already base64 encoded.
254- $ entry [ $ column ->get_name () ] = is_string ( $ value ) && base64_decode ( $ value , true ) !== false ? $ value : base64_encode ( (string ) $ value );
255- break ;
256- default :
257- break ;
258- }
259- }
260- return $ entry ;
261- },
262- $ entries
263- );
264-
265240 $ database = Config::get_db ();
266241 $ columns = array_keys ( $ entries [0 ] );
267242 $ prepared_columns = implode (
@@ -277,7 +252,7 @@ function ( $entry ) use ( $columns ) {
277252 $ prepared_values [ $ row_index ] = [];
278253 foreach ( $ entry as $ column => $ value ) {
279254 [ $ prepared_value , $ placeholder ] = self ::prepare_value_for_query ( $ column , $ value );
280- $ prepared_values [ $ row_index ][] = $ database ::prepare ( $ placeholder , $ prepared_value );
255+ $ prepared_values [ $ row_index ][] = ' NULL ' === $ placeholder ? $ placeholder : $ database ::prepare ( $ placeholder , $ prepared_value );
281256 }
282257 }
283258
@@ -390,7 +365,7 @@ public static function update_many( array $entries ): bool {
390365
391366 [ $ value , $ placeholder ] = self ::prepare_value_for_query ( $ column , $ value );
392367
393- $ set_statement [] = $ database ::prepare ( "` { $ column } ` = {$ placeholder }" , $ value );
368+ $ set_statement [] = $ database ::prepare ( "%i = {$ placeholder }" , ... array_filter ( [ $ column , $ value ], static fn ( $ v ) => null !== $ v ) );
394369 }
395370
396371 $ set_statement = implode ( ', ' , $ set_statement );
@@ -419,14 +394,13 @@ public static function update_many( array $entries ): bool {
419394 * @param string $join_table The table to join.
420395 * @param string $join_condition The condition to join on.
421396 * @param array $selectable_joined_columns The columns from the joined table to select.
422- * @param string $output The output type of the query, one of OBJECT, ARRAY_A, or ARRAY_N.
423397 *
424398 * @return array The items.
425399 * @throws InvalidArgumentException If the table to join is the same as the current table.
426400 * If the join condition does not contain an equal sign.
427401 * If the join condition does not contain valid columns.
428402 */
429- public static function paginate ( array $ args , int $ per_page = 20 , int $ page = 1 , array $ columns = [ '* ' ], string $ join_table = '' , string $ join_condition = '' , array $ selectable_joined_columns = [], string $ output = OBJECT ): array {
403+ public static function paginate ( array $ args , int $ per_page = 20 , int $ page = 1 , array $ columns = [ '* ' ], string $ join_table = '' , string $ join_condition = '' , array $ selectable_joined_columns = [] ): array {
430404 $ is_join = (bool ) $ join_table ;
431405
432406 if ( $ is_join && static ::table_name ( true ) === $ join_table ::table_name ( true ) ) {
@@ -481,7 +455,7 @@ public static function paginate( array $args, int $per_page = 20, int $page = 1,
481455 $ offset ,
482456 $ per_page
483457 ),
484- $ output
458+ ARRAY_A
485459 );
486460
487461 $ results = array_map ( fn ( $ result ) => self ::amend_value_types ( $ result ), $ results );
@@ -567,7 +541,7 @@ protected static function build_where_from_args( array $args = [] ): string {
567541 continue ;
568542 }
569543
570- if ( empty ( $ arg ['value ' ] ) ) {
544+ if ( ! isset ( $ arg ['value ' ] ) ) {
571545 // We check that the column has any value then.
572546 $ arg ['value ' ] = '' ;
573547 $ arg ['operator ' ] = '!= ' ;
@@ -595,6 +569,11 @@ protected static function build_where_from_args( array $args = [] ): string {
595569 continue ;
596570 }
597571
572+ if ( 'NULL ' === $ placeholder ) {
573+ $ where [] = $ query ;
574+ continue ;
575+ }
576+
598577 $ where [] = $ database ::prepare ( $ query , $ value );
599578 }
600579
@@ -686,7 +665,7 @@ public static function get_all_by( string $column, $value, string $operator = '=
686665
687666 $ database = Config::get_db ();
688667 $ results = [];
689- foreach ( static ::fetch_all_where ( $ database ::prepare ( "WHERE { $ column } {$ operator } {$ placeholder }" , $ value ), $ limit , ARRAY_A ) as $ task_array ) {
668+ foreach ( static ::fetch_all_where ( $ database ::prepare ( "WHERE %i {$ operator } {$ placeholder }" , ... array_filter ( [ $ column , $ value ], static fn ( $ v ) => null !== $ v ) ), $ limit , ARRAY_A ) as $ task_array ) {
690669 if ( empty ( $ task_array [ static ::uid_column () ] ) ) {
691670 continue ;
692671 }
@@ -713,7 +692,7 @@ public static function get_first_by( string $column, $value ) {
713692 [ $ value , $ placeholder ] = self ::prepare_value_for_query ( $ column , $ value );
714693
715694 $ database = Config::get_db ();
716- $ task_array = static ::fetch_first_where ( $ database ::prepare ( "WHERE { $ column } = {$ placeholder }" , $ value ), ARRAY_A );
695+ $ task_array = static ::fetch_first_where ( $ database ::prepare ( "WHERE %i = {$ placeholder }" , ... array_filter ( [ $ column , $ value ], static fn ( $ v ) => null !== $ v ) ), ARRAY_A );
717696
718697 if ( empty ( $ task_array [ static ::uid_column () ] ) ) {
719698 return null ;
@@ -746,6 +725,10 @@ private static function prepare_value_for_query( string $column, $value ): array
746725
747726 $ column_type = $ column ->get_php_type ();
748727
728+ if ( null === $ value && $ column ->get_nullable () ) {
729+ return [ null , 'NULL ' ];
730+ }
731+
749732 switch ( $ column ->get_php_type () ) {
750733 case PHP_Types::INT :
751734 $ value = is_array ( $ value ) ? array_map ( fn ( $ v ) => (int ) $ v , $ value ) : (int ) $ value ;
@@ -775,7 +758,7 @@ private static function prepare_value_for_query( string $column, $value ): array
775758 if ( is_array ( $ value ) ) {
776759 $ value = array_map ( fn ( $ v ) => is_string ( $ v ) ? $ v : base64_encode ( (string ) $ v ), $ value );
777760 } else {
778- $ value = is_string ( $ value ) && base64_decode ( $ value , true ) !== false ? $ value : base64_encode ( (string ) $ value );
761+ $ value = is_string ( $ value ) ? base64_encode ( (string ) $ value ) : $ value ;
779762 }
780763 $ placeholder = '%s ' ;
781764 break ;
@@ -901,7 +884,7 @@ public static function cast_value_based_on_type( string $type, $value ) {
901884 return $ new_value ;
902885 case PHP_Types::BLOB :
903886 // Decode base64 encoded blob data.
904- if ( is_string ( $ value ) && base64_decode ( $ value , true ) !== false ) {
887+ if ( is_string ( $ value ) ) {
905888 return base64_decode ( $ value );
906889 }
907890 return (string ) $ value ;
0 commit comments