Created
November 4, 2023 14:19
-
-
Save nextab/0a9ea233949ed55218cc27db8852ee4a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#region Update CCT helper function | |
/** | |
* | |
* Helper function to update the row(s) of a CCT table | |
* | |
* @param array $values column names and value pairings | |
* @param array $where specify what to update | |
* @param $table_name slug of the CCT; global WPDB prefix and 'jet_cct_' will be appended; defaults to 'user_information' | |
* | |
* @return bool returns true on success; false on fail | |
* | |
* Example: | |
* | |
* $values = [ | |
* 'user_name' => 'Oli', | |
* 'activation_code' => '123456', | |
* 'user_email' => '[email protected]' | |
* ]; | |
* $where = [ | |
* '_ID' => 3, | |
* ]; | |
* | |
*/ | |
function update_cct( $values, $where, $table_name = 'user_information'): bool { | |
global $wpdb; | |
$table_name_with_prefix = $wpdb->prefix . 'jet_cct_' . $table_name; | |
// Prepare formats for the values - '%s' for strings, '%d' for integers, '%f' for floats | |
$data_format = array_map( function( $value ) { | |
if ( is_float( $value ) ) return '%f'; | |
if ( is_int( $value ) ) return '%d'; | |
return '%s'; | |
}, $values ); | |
// Prepare formats for the where clause | |
$where_format = array_map( function( $value ) { | |
if ( is_float( $value ) ) return '%f'; | |
if ( is_int( $value ) ) return '%d'; | |
return '%s'; | |
}, $where ); | |
// Update the data in the database | |
$updated = $wpdb->update( $table_name_with_prefix, $values, $where, $data_format, $where_format ); | |
if ( false === $updated ) { | |
// TODO: add debug information into the error log | |
return false; | |
} | |
return true; | |
} | |
#endregion Update CCT helper function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment