Created
September 17, 2014 01:30
-
-
Save codex-m/cce29a8f102ca8d04d92 to your computer and use it in GitHub Desktop.
Code to fix 01.01.1970 date problem in Types plugin (starting version 1.6.2+)
This file contains 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
/** START OF CODE | |
* Patch to correct wrong date format in dB in Types fields | |
* Add this code to your functions.php | |
* AUTHOR: wp-types.com | |
*/ | |
add_action('wp_loaded','mass_update_date_fields_typesfunc'); | |
function mass_update_date_fields_typesfunc() { | |
//Make sure $types_field_name is the exact field name as saved in database. | |
$types_field_name_in_db='wpcf-data-ogloszenia'; | |
/* That's all, stop editing below! */ | |
$types_unixdate_update_done=get_option('types_unixdate_update_done'); | |
if ((!(empty($types_field_name_in_db))) && (!($types_unixdate_update_done))) { | |
$wpcf_fields=get_option('wpcf-fields'); | |
$str_length=strlen($types_field_name_in_db); | |
$length=$str_length-5; | |
$field_name= substr($types_field_name_in_db,5,$length); | |
if (($wpcf_fields) && (isset($wpcf_fields[$field_name]))) { | |
$types_field_information=$wpcf_fields[$field_name]; | |
//Validate if this field is a date field | |
if ((isset($types_field_information['type'])) && ('date'==$types_field_information['type'])) { | |
global $wpdb; | |
//Get all post IDs, meta key and meta value | |
$posts_meta_table=$wpdb->prefix."postmeta"; | |
$postid_array = $wpdb->get_results($wpdb->prepare("SELECT post_id,meta_value FROM $posts_meta_table WHERE meta_key = %s AND meta_value <> ''",$types_field_name_in_db), ARRAY_A); | |
if ((is_array($postid_array)) && (!(empty($postid_array))) && (function_exists('Types_isValidTimeStamp'))) { | |
//Loop through the meta values | |
foreach ($postid_array as $k=>$values) { | |
if (isset($values['meta_value'])) { | |
//Validate if in Unix time | |
$result_check=Types_isValidTimeStamp($values['meta_value']); | |
if (!($result_check)) { | |
//Attempt conversion to unix time | |
$unix_time_equivalent=strtotime($values['meta_value']); | |
if ($unix_time_equivalent) { | |
//Successful conversion, update back | |
$successful_updating=update_post_meta($values['post_id'], $types_field_name_in_db, $unix_time_equivalent); | |
} | |
} | |
} | |
} | |
//Loop done | |
//Marked as done to make this script run only once | |
update_option('types_unixdate_update_done','yes'); | |
} | |
} | |
} | |
} | |
} | |
function Types_isValidTimeStamp($timestamp) | |
{ | |
return ((string) (int) $timestamp === $timestamp) | |
&& ($timestamp <= PHP_INT_MAX) | |
&& ($timestamp >= ~PHP_INT_MAX); | |
} | |
/** END OF CODE */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment