pluginever/wc-serial-numbers

order_date not set to NULL when a serial number is set to 'available' status

Opened this issue · 0 comments

gmn42 commented

When a serial number is set to 'available' status (via the UI or through the Key class), the order_id is set to 0 and the order_date should be set to NULL (see Key.php line 518). However, this does not happen because wp_array_slice_assoc() is used during the save() method in the parent Model class, and this function cannot handle keys in the input array that are set to NULL (probably a WP bug, it uses isset() on the input array, which returns false if a key exists but is set to null).

This can be resolved with the following patch (I can't submit a PR because Model.php is not in this repo):

--- ../wc-serial-numbers/lib/Lib/Model.php	2023-08-31 23:59:24.424829309 -0700
+++ lib/Lib/Model.php	2023-09-02 12:48:06.633619770 -0700
@@ -467,7 +467,7 @@ abstract class Model {
 		 */
 		do_action( $this->get_hook_prefix() . '_pre_update', $this, $changes );
 
-		$data = wp_array_slice_assoc( $changes, $this->get_core_data_keys() );
+		$data = $this->array_slice_assoc( $changes, $this->get_core_data_keys() );
 
 		/**
 		 * Filters the data to be updated in the database.
@@ -503,6 +503,10 @@ abstract class Model {
 		return true;
 	}
 
+	function array_slice_assoc($array,$keys) {
+		return array_intersect_key($array,array_flip($keys));
+	}
+
 	/**
 	 * Deletes the object from database.
 	 *

See the Notes section at https://www.php.net/manual/en/function.array-slice.php for more info about this solution.