Add some default field types. see #7171

git-svn-id: http://svn.automattic.com/wordpress/trunk@10725 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2009-03-06 00:50:19 +00:00
parent 551c0c8af9
commit d6ee6062a6
3 changed files with 42 additions and 21 deletions

View File

@ -1499,20 +1499,14 @@ function wp_insert_post($postarr = array(), $wp_error = false) {
} }
// expected_slashed (everything!) // expected_slashed (everything!)
$fields = array( 'post_author' => '%d', 'post_date' => '%s', 'post_date_gmt' => '%s', 'post_content' => '%s', 'post_content_filtered' => '%s', 'post_title' => '%s', $data = compact( array( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_content_filtered', 'post_title', 'post_excerpt', 'post_status', 'post_type', 'comment_status', 'ping_status', 'post_password', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_parent', 'menu_order', 'guid' ) );
'post_excerpt' => '%s', 'post_status' => '%s', 'post_type' => '%s', 'comment_status' => '%s', 'ping_status' => '%s', 'post_password' => '%s', 'post_name' => '%s',
'to_ping' => '%s', 'pinged' => '%s', 'post_modified' => '%s', 'post_modified_gmt' => '%s', 'post_parent' => '%d', 'menu_order' => '%d', 'guid' => '%s' );
$data = compact( array_keys( $fields) );
$data_formats = array_values( $fields );
$data = apply_filters('wp_insert_post_data', $data, $postarr); $data = apply_filters('wp_insert_post_data', $data, $postarr);
$data = stripslashes_deep( $data ); $data = stripslashes_deep( $data );
error_log(var_export($data, true));
$where = array( 'ID' => $post_ID ); $where = array( 'ID' => $post_ID );
$where_formats = array('%d');
if ( $update ) { if ($update) {
do_action( 'pre_post_update', $post_ID ); do_action( 'pre_post_update', $post_ID );
if ( false === $wpdb->update( $wpdb->posts, $data, $where, $data_formats, $where_formats ) ) { if ( false === $wpdb->update( $wpdb->posts, $data, $where ) ) {
if ( $wp_error ) if ( $wp_error )
return new WP_Error('db_update_error', __('Could not update post in the database'), $wpdb->last_error); return new WP_Error('db_update_error', __('Could not update post in the database'), $wpdb->last_error);
else else
@ -1528,7 +1522,7 @@ function wp_insert_post($postarr = array(), $wp_error = false) {
$data['ID'] = $import_id; $data['ID'] = $import_id;
} }
} }
if ( false === $wpdb->insert( $wpdb->posts, $data, $data_formats ) ) { if ( false === $wpdb->insert( $wpdb->posts, $data ) ) {
if ( $wp_error ) if ( $wp_error )
return new WP_Error('db_insert_error', __('Could not insert post into the database'), $wpdb->last_error); return new WP_Error('db_insert_error', __('Could not insert post into the database'), $wpdb->last_error);
else else

View File

@ -702,15 +702,23 @@ class wpdb {
* @param array|string $format The format of the field values. * @param array|string $format The format of the field values.
* @return mixed Results of $this->query() * @return mixed Results of $this->query()
*/ */
function insert($table, $data, $format = '%s') { function insert($table, $data, $format = null) {
$format = (array) $format; global $db_field_types;
$formats = $format = (array) $format;
$fields = array_keys($data); $fields = array_keys($data);
$formatted_fields = array(); $formatted_fields = array();
foreach ( $data as $field ) { foreach ( $fields as $field ) {
$form = ( $form = array_shift($format) ) ? $form : $formatted_fields[0]; if ( !empty($format) )
$form = ( $form = array_shift($formats) ) ? $form : $format[0];
elseif ( isset($db_field_types[$field]) )
$form = $db_field_types[$field];
else
$form = '%s';
$formatted_fields[] = $form; $formatted_fields[] = $form;
} }
$sql = "INSERT INTO $table (`" . implode( '`,`', $fields ) . "`) VALUES ('" . implode( "','", $formatted_fields ) . "')"; $sql = "INSERT INTO $table (`" . implode( '`,`', $fields ) . "`) VALUES ('" . implode( "','", $formatted_fields ) . "')";
error_log($sql);
return $this->query( $this->prepare( $sql, $data) ); return $this->query( $this->prepare( $sql, $data) );
} }
@ -726,24 +734,37 @@ class wpdb {
* @param array|string $where_format The format of the where field values. * @param array|string $where_format The format of the where field values.
* @return mixed Results of $this->query() * @return mixed Results of $this->query()
*/ */
function update($table, $data, $where, $format = '%s', $where_format = '%s') { function update($table, $data, $where, $format = null, $where_format = null) {
global $db_field_types;
if ( !is_array( $where ) ) if ( !is_array( $where ) )
return false; return false;
$formats = $format = (array) $format; $formats = $format = (array) $format;
$bits = $wheres = array(); $bits = $wheres = array();
foreach ( (array) array_keys($data) as $k ) { foreach ( (array) array_keys($data) as $field ) {
$form = ( $form = array_shift($formats) ) ? $form : $format[0]; if ( !empty($format) )
$bits[] = "`$k` = {$form}"; $form = ( $form = array_shift($formats) ) ? $form : $format[0];
elseif ( isset($db_field_types[$field]) )
$form = $db_field_types[$field];
else
$form = '%s';
$bits[] = "`$field` = {$form}";
} }
$where_formats = $where_format = (array) $where_format; $where_formats = $where_format = (array) $where_format;
foreach ( $where as $c => $v ) { foreach ( (array) array_keys($where) as $field ) {
$form = ( $form = array_shift($where_formats) ) ? $form : $where_format[0]; if ( !empty($where_format) )
$wheres[] = "$c = {$form}"; $form = ( $form = array_shift($where_formats) ) ? $form : $where_format[0];
elseif ( isset($db_field_types[$field]) )
$form = $db_field_types[$field];
else
$form = '%s';
$wheres[] = "$field = {$form}";
} }
$sql = "UPDATE $table SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres ); $sql = "UPDATE $table SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres );
error_log($sql);
return $this->query( $this->prepare( $sql, array_merge(array_values($data), array_values($where))) ); return $this->query( $this->prepare( $sql, array_merge(array_values($data), array_values($where))) );
} }

View File

@ -247,6 +247,12 @@ require (ABSPATH . WPINC . '/compat.php');
require (ABSPATH . WPINC . '/functions.php'); require (ABSPATH . WPINC . '/functions.php');
require (ABSPATH . WPINC . '/classes.php'); require (ABSPATH . WPINC . '/classes.php');
$db_field_types = array( 'post_author' => '%d', 'post_date' => '%s', 'post_date_gmt' => '%s', 'post_content' => '%s', 'post_content_filtered' => '%s', 'post_title' => '%s',
'post_excerpt' => '%s', 'post_status' => '%s', 'post_type' => '%s', 'comment_status' => '%s', 'ping_status' => '%s', 'post_password' => '%s', 'post_name' => '%s',
'to_ping' => '%s', 'pinged' => '%s', 'post_modified' => '%s', 'post_modified_gmt' => '%s', 'post_parent' => '%d', 'menu_order' => '%d', 'guid' => '%s', 'term_id' => '%d',
'name' => '%s', 'slug' => '%s', 'term_group' => '%d', 'term_taxonomy_id' => '%d', 'description' => '%s', 'taxonomy' => '%s', 'parent' => '%d', 'count' => '%d',
'object_id' => '%d', 'term_order' => '%d');
require_wp_db(); require_wp_db();
if ( !empty($wpdb->error) ) if ( !empty($wpdb->error) )