wpdb::_insert_replace_helper PHP Method

_insert_replace_helper() public method

Runs an insert or replace query based on $type argument.
See also: wpdb::prepare()
See also: wpdb::$field_types
See also: wp_set_wpdb_vars()
Since: 3.0.0
public _insert_replace_helper ( string $table, array $data, array | string $format = null, string $type = 'INSERT' ) : integer | false
$table string table name
$data array Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
$format array | string Optional. An array of formats to be mapped to each of the value in $data. If string, that format will be used for all of the values in $data. A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
$type string Optional. What type of operation is this? INSERT or REPLACE. Defaults to INSERT.
return integer | false The number of rows affected, or false on error.
    function _insert_replace_helper($table, $data, $format = null, $type = 'INSERT')
    {
        if (!in_array(strtoupper($type), ['REPLACE', 'INSERT'])) {
            return false;
        }
        $this->insert_id = 0;
        $formats = $format = (array) $format;
        $fields = array_keys($data);
        $formatted_fields = [];
        foreach ($fields as $field) {
            if (!empty($format)) {
                $form = ($form = array_shift($formats)) ? $form : $format[0];
            } elseif (isset($this->field_types[$field])) {
                $form = $this->field_types[$field];
            } else {
                $form = '%s';
            }
            $formatted_fields[] = $form;
        }
        $sql = "{$type} INTO `{$table}` (`" . implode('`,`', $fields) . "`) VALUES (" . implode(",", $formatted_fields) . ")";
        return $this->query($this->prepare($sql, $data));
    }