| 1 |
<?php |
| 2 |
|
| 3 |
$version = get_option('acf_version','1.0.5'); |
| 4 |
|
| 5 |
|
| 6 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 7 |
|
| 8 |
|
| 9 |
/*--------------------------------------------------------------------------------------------- |
| 10 |
* Update to 1.1.0 - this version needs tables! |
| 11 |
* |
| 12 |
* @author Elliot Condon |
| 13 |
* @since 1.0.6 |
| 14 |
* |
| 15 |
---------------------------------------------------------------------------------------------*/ |
| 16 |
if(version_compare($version,'1.1.0') < 0) |
| 17 |
{ |
| 18 |
// Version is less than 1.1.0 |
| 19 |
|
| 20 |
global $wpdb; |
| 21 |
|
| 22 |
|
| 23 |
// set charset |
| 24 |
if ($wpdb->has_cap('collation')) |
| 25 |
{ |
| 26 |
if(!empty($wpdb->charset)) |
| 27 |
{ |
| 28 |
$charset_collate = " DEFAULT CHARACTER SET $wpdb->charset"; |
| 29 |
} |
| 30 |
if(!empty($wpdb->collate)) |
| 31 |
{ |
| 32 |
$charset_collate .= " COLLATE $wpdb->collate"; |
| 33 |
|
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
// create acf_fields table |
| 39 |
$table_name = $wpdb->prefix.'acf_fields'; |
| 40 |
if(!$wpdb->get_var("SHOW TABLES LIKE '".$table_name."'")) |
| 41 |
{ |
| 42 |
$sql = "CREATE TABLE " . $table_name . " ( |
| 43 |
id bigint(20) NOT NULL AUTO_INCREMENT, |
| 44 |
order_no int(9) NOT NULL DEFAULT '0', |
| 45 |
post_id bigint(20) NOT NULL DEFAULT '0', |
| 46 |
parent_id bigint(20) NOT NULL DEFAULT '0', |
| 47 |
label text NOT NULL, |
| 48 |
name text NOT NULL, |
| 49 |
type text NOT NULL, |
| 50 |
options text NOT NULL, |
| 51 |
UNIQUE KEY id (id) |
| 52 |
) ".$charset_collate.";"; |
| 53 |
dbDelta($sql); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
// create acf_options table |
| 58 |
$table_name = $wpdb->prefix.'acf_options'; |
| 59 |
if(!$wpdb->get_var("SHOW TABLES LIKE '".$table_name."'")) |
| 60 |
{ |
| 61 |
$sql = "CREATE TABLE " . $table_name . " ( |
| 62 |
id bigint(20) NOT NULL AUTO_INCREMENT, |
| 63 |
acf_id bigint(20) NOT NULL DEFAULT '0', |
| 64 |
name text NOT NULL, |
| 65 |
value text NOT NULL, |
| 66 |
type text NOT NULL, |
| 67 |
UNIQUE KEY id (id) |
| 68 |
) ".$charset_collate.";"; |
| 69 |
dbDelta($sql); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
// create acf_options table |
| 74 |
$table_name = $wpdb->prefix.'acf_values'; |
| 75 |
if(!$wpdb->get_var("SHOW TABLES LIKE '".$table_name."'")) |
| 76 |
{ |
| 77 |
$sql = "CREATE TABLE " . $table_name . " ( |
| 78 |
id bigint(20) NOT NULL AUTO_INCREMENT, |
| 79 |
order_no int(9) NOT NULL DEFAULT '0', |
| 80 |
field_id bigint(20) NOT NULL DEFAULT '0', |
| 81 |
value text NOT NULL, |
| 82 |
post_id bigint(20) NOT NULL DEFAULT '0', |
| 83 |
UNIQUE KEY id (id) |
| 84 |
) ".$charset_collate.";"; |
| 85 |
dbDelta($sql); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
$acfs = get_posts(array( |
| 91 |
'numberposts' => -1, |
| 92 |
'post_type' => 'acf', |
| 93 |
)); |
| 94 |
|
| 95 |
|
| 96 |
if($acfs) |
| 97 |
{ |
| 98 |
foreach($acfs as $acf) |
| 99 |
{ |
| 100 |
$keys = get_post_custom_keys($acf->ID); |
| 101 |
|
| 102 |
if(empty($keys)){continue;} |
| 103 |
|
| 104 |
|
| 105 |
// FIELDS |
| 106 |
$table_name = $wpdb->prefix.'acf_fields'; |
| 107 |
|
| 108 |
|
| 109 |
for($i = 0; $i < 99; $i++) |
| 110 |
{ |
| 111 |
if(in_array('_acf_field_'.$i.'_label',$keys)) |
| 112 |
{ |
| 113 |
$field = array( |
| 114 |
'label' => get_post_meta($acf->ID, '_acf_field_'.$i.'_label', true), |
| 115 |
'name' => get_post_meta($acf->ID, '_acf_field_'.$i.'_name', true), |
| 116 |
'type' => get_post_meta($acf->ID, '_acf_field_'.$i.'_type', true), |
| 117 |
'options' => unserialize(get_post_meta($acf->ID, '_acf_field_'.$i.'_options', true)) // explode choices! |
| 118 |
); |
| 119 |
|
| 120 |
// if choices, exlode them |
| 121 |
if($field['options']['choices']) |
| 122 |
{ |
| 123 |
// explode choices from each line |
| 124 |
if(strpos($field['options']['choices'], "\n") !== false) |
| 125 |
{ |
| 126 |
// found multiple lines, explode it |
| 127 |
$field['options']['choices'] = explode("\n", $field['options']['choices']); |
| 128 |
} |
| 129 |
else |
| 130 |
{ |
| 131 |
// no multiple lines! |
| 132 |
$field['options']['choices'] = array($field['options']['choices']); |
| 133 |
} |
| 134 |
|
| 135 |
$new_choices = array(); |
| 136 |
foreach($field['options']['choices'] as $choice) |
| 137 |
{ |
| 138 |
$new_choices[trim($choice)] = trim($choice); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
// return array containing all choices |
| 143 |
$field['options']['choices'] = $new_choices; |
| 144 |
|
| 145 |
} |
| 146 |
|
| 147 |
// now save field to database |
| 148 |
$data = array( |
| 149 |
'order_no' => $i, |
| 150 |
'post_id' => $acf->ID, |
| 151 |
'label' => $field['label'], |
| 152 |
'name' => $field['name'], |
| 153 |
'type' => $field['type'], |
| 154 |
'options' => serialize($field['options']), |
| 155 |
|
| 156 |
); |
| 157 |
|
| 158 |
|
| 159 |
// save field as row in database |
| 160 |
|
| 161 |
$new_id = $wpdb->insert($table_name, $data); |
| 162 |
} |
| 163 |
else |
| 164 |
{ |
| 165 |
// data doesnt exist, break loop |
| 166 |
break; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
// START LOCATION |
| 172 |
$table_name = $wpdb->prefix.'acf_options'; |
| 173 |
//$wpdb->query("DELETE FROM $table_name WHERE acf_id = '$acf->ID' AND type = 'location'"); |
| 174 |
|
| 175 |
$location = array( |
| 176 |
'post_types' => get_post_meta($acf->ID, '_acf_location_post_type', true), |
| 177 |
'page_slugs' => get_post_meta($acf->ID, '_acf_location_page_slug', true), |
| 178 |
'post_ids' => get_post_meta($acf->ID, '_acf_location_post_id', true), |
| 179 |
'page_templates' => get_post_meta($acf->ID, '_acf_location_page_template', true), |
| 180 |
'parent_ids' => get_post_meta($acf->ID, '_acf_location_parent_id', true), |
| 181 |
'ignore_other_acfs' => get_post_meta($acf->ID, '_acf_location_ignore_other_acf', true), |
| 182 |
); |
| 183 |
|
| 184 |
foreach($location as $key => $value) |
| 185 |
{ |
| 186 |
if(empty($value)) |
| 187 |
{ |
| 188 |
continue; |
| 189 |
} |
| 190 |
|
| 191 |
if(strpos($value, ',') !== false) |
| 192 |
{ |
| 193 |
// found ',', explode it |
| 194 |
$value = str_replace(', ',',',$value); |
| 195 |
$value = explode(',', $value); |
| 196 |
} |
| 197 |
else |
| 198 |
{ |
| 199 |
// no ','! |
| 200 |
$value = array($value); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
$new_id = $wpdb->insert($table_name, array( |
| 205 |
'acf_id' => $acf->ID, |
| 206 |
'name' => $key, |
| 207 |
'value' => serialize($value), |
| 208 |
'type' => 'location' |
| 209 |
)); |
| 210 |
} |
| 211 |
// END LOCATION |
| 212 |
|
| 213 |
|
| 214 |
// START OPTIONS |
| 215 |
$table_name = $wpdb->prefix.'acf_options'; |
| 216 |
//$wpdb->query("DELETE FROM $table_name WHERE acf_id = '$acf->ID' AND type = 'option'"); |
| 217 |
|
| 218 |
|
| 219 |
$show_on_page = get_post_meta($acf->ID, '_acf_option_show_on_page', true); |
| 220 |
|
| 221 |
|
| 222 |
if(!empty($show_on_page)) |
| 223 |
{ |
| 224 |
$show_on_page = str_replace(', ',',',$show_on_page); |
| 225 |
$show_on_page = explode(',',$show_on_page); |
| 226 |
|
| 227 |
|
| 228 |
$new_id = $wpdb->insert($table_name, array( |
| 229 |
'acf_id' => $acf->ID, |
| 230 |
'name' => 'show_on_page', |
| 231 |
'value' => serialize($show_on_page), |
| 232 |
'type' => 'option' |
| 233 |
)); |
| 234 |
} |
| 235 |
// END OPTIONS |
| 236 |
|
| 237 |
|
| 238 |
// delete data |
| 239 |
foreach(get_post_custom($acf->ID) as $key => $values) |
| 240 |
{ |
| 241 |
if(strpos($key, '_acf') !== false) |
| 242 |
{ |
| 243 |
// this custom field needs to be deleted! |
| 244 |
delete_post_meta($acf->ID, $key); |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
// START VALUES |
| 250 |
|
| 251 |
$table_name = $wpdb->prefix.'acf_values'; |
| 252 |
//$wpdb->query("DELETE FROM $table_name WHERE acf_id = '$acf->ID' AND type = 'option'"); |
| 253 |
|
| 254 |
$posts = get_posts(array( |
| 255 |
'numberposts' => -1, |
| 256 |
'post_type' => 'any' |
| 257 |
)); |
| 258 |
|
| 259 |
if($posts) |
| 260 |
{ |
| 261 |
foreach($posts as $post) |
| 262 |
{ |
| 263 |
foreach(get_post_custom($post->ID) as $key => $value) |
| 264 |
{ |
| 265 |
if(strpos($key, '_acf') !== false) |
| 266 |
{ |
| 267 |
// found an acf cusomt field! |
| 268 |
$name = str_replace('_acf_','',$key); |
| 269 |
|
| 270 |
if($name == 'id'){continue;} |
| 271 |
|
| 272 |
// get field id |
| 273 |
$table_name = $wpdb->prefix.'acf_fields'; |
| 274 |
$field_id = $wpdb->get_var("SELECT id FROM $table_name WHERE name = '$name'"); |
| 275 |
|
| 276 |
$table_name = $wpdb->prefix.'acf_values'; |
| 277 |
$new_id = $wpdb->insert($table_name, array( |
| 278 |
'field_id' => $field_id, |
| 279 |
'value' => $value[0], |
| 280 |
'post_id' => $post->ID, |
| 281 |
)); |
| 282 |
|
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
// delete data |
| 287 |
foreach(get_post_custom($post->ID) as $key => $values) |
| 288 |
{ |
| 289 |
if(strpos($key, '_acf') !== false) |
| 290 |
{ |
| 291 |
// this custom field needs to be deleted! |
| 292 |
delete_post_meta($post->ID, $key); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
// END VALUES |
| 301 |
|
| 302 |
|
| 303 |
update_option('acf_version','1.1.0'); |
| 304 |
$version = '1.1.0'; |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
/*--------------------------------------------------------------------------------------------- |
| 310 |
* Update to 1.1.0 - this version adds updates tables to be utf-8 |
| 311 |
* |
| 312 |
* @author Elliot Condon |
| 313 |
* @since 1.0.6 |
| 314 |
* |
| 315 |
---------------------------------------------------------------------------------------------*/ |
| 316 |
|
| 317 |
if(version_compare($version,'1.1.4') < 0) |
| 318 |
{ |
| 319 |
// Version is less than 1.1.4 |
| 320 |
|
| 321 |
global $wpdb; |
| 322 |
|
| 323 |
|
| 324 |
// set charset |
| 325 |
if(!empty($wpdb->charset)) |
| 326 |
{ |
| 327 |
$char = $wpdb->charset; |
| 328 |
} |
| 329 |
else |
| 330 |
{ |
| 331 |
$char = "utf8"; |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
// alter acf_fields table |
| 337 |
$table_name = $wpdb->prefix.'acf_fields'; |
| 338 |
if($wpdb->get_var("SHOW TABLES LIKE '".$table_name."'")) |
| 339 |
{ |
| 340 |
$sql = "ALTER TABLE $table_name charset=$char;"; |
| 341 |
$wpdb->query($sql); |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
// alter acf_options table |
| 346 |
$table_name = $wpdb->prefix.'acf_options'; |
| 347 |
if($wpdb->get_var("SHOW TABLES LIKE '".$table_name."'")) |
| 348 |
{ |
| 349 |
$sql = "ALTER TABLE $table_name charset=$char;"; |
| 350 |
$wpdb->query($sql); |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
// alter acf_values table |
| 355 |
$table_name = $wpdb->prefix.'acf_values'; |
| 356 |
if($wpdb->get_var("SHOW TABLES LIKE '".$table_name."'")) |
| 357 |
{ |
| 358 |
$sql = "ALTER TABLE $table_name charset=$char;"; |
| 359 |
$wpdb->query($sql); |
| 360 |
} |
| 361 |
|
| 362 |
update_option('acf_version','1.1.4'); |
| 363 |
$version = '1.1.4'; |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
/*--------------------------------------------------------------------------------------------- |
| 368 |
* Update to 2.0.1 - this version adds field description and save as custom field columns to acf_fields |
| 369 |
* |
| 370 |
* @author Elliot Condon |
| 371 |
* @since 2.0.6 |
| 372 |
* |
| 373 |
---------------------------------------------------------------------------------------------*/ |
| 374 |
|
| 375 |
if(version_compare($version,'2.0.1') < 0) |
| 376 |
{ |
| 377 |
|
| 378 |
global $wpdb; |
| 379 |
|
| 380 |
|
| 381 |
// set charset |
| 382 |
if(!empty($wpdb->charset)) |
| 383 |
{ |
| 384 |
$char = $wpdb->charset; |
| 385 |
} |
| 386 |
else |
| 387 |
{ |
| 388 |
$char = "utf8"; |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
// create acf_fields table |
| 393 |
$table_name = $wpdb->prefix.'acf_fields'; |
| 394 |
if($wpdb->get_var("SHOW TABLES LIKE '".$table_name."'")) |
| 395 |
{ |
| 396 |
$sql = "CREATE TABLE " . $table_name . " ( |
| 397 |
id bigint(20) NOT NULL AUTO_INCREMENT, |
| 398 |
order_no int(9) NOT NULL DEFAULT '0', |
| 399 |
post_id bigint(20) NOT NULL DEFAULT '0', |
| 400 |
parent_id bigint(20) NOT NULL DEFAULT '0', |
| 401 |
label text NOT NULL, |
| 402 |
name text NOT NULL, |
| 403 |
instructions text NOT NULL DEFAULT '', |
| 404 |
save_as_cf int(1) NOT NULL DEFAULT 0, |
| 405 |
type text NOT NULL, |
| 406 |
options text NOT NULL, |
| 407 |
UNIQUE KEY id (id) |
| 408 |
) ".$char.";"; |
| 409 |
dbDelta($sql); |
| 410 |
} |
| 411 |
|
| 412 |
update_option('acf_version','2.0.1'); |
| 413 |
$version = '2.0.1'; |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
/*--------------------------------------------------------------------------------------------- |
| 418 |
* Update to 2.0.1 - this version adds field description and save as custom field columns to acf_fields |
| 419 |
* |
| 420 |
* @author Elliot Condon |
| 421 |
* @since 2.0.6 |
| 422 |
* |
| 423 |
---------------------------------------------------------------------------------------------*/ |
| 424 |
|
| 425 |
if(version_compare($version,'2.0.2') < 0) |
| 426 |
{ |
| 427 |
global $wpdb; |
| 428 |
|
| 429 |
|
| 430 |
// 1. CREATE NEW RULES TABLE |
| 431 |
|
| 432 |
$table_name = $wpdb->prefix.'acf_rules'; |
| 433 |
|
| 434 |
if ($wpdb->has_cap('collation')) |
| 435 |
{ |
| 436 |
if(!empty($wpdb->charset)) |
| 437 |
{ |
| 438 |
$charset_collate = " DEFAULT CHARACTER SET $wpdb->charset"; |
| 439 |
} |
| 440 |
if(!empty($wpdb->collate)) |
| 441 |
{ |
| 442 |
$charset_collate .= " COLLATE $wpdb->collate"; |
| 443 |
|
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
if(!$wpdb->get_var("SHOW TABLES LIKE '".$table_name."'")) |
| 448 |
{ |
| 449 |
// rules table does not exist |
| 450 |
|
| 451 |
$sql = "CREATE TABLE " . $table_name . " ( |
| 452 |
id bigint(20) NOT NULL AUTO_INCREMENT, |
| 453 |
acf_id bigint(20) NOT NULL DEFAULT '0', |
| 454 |
order_no int(9) NOT NULL DEFAULT '0', |
| 455 |
param text NOT NULL, |
| 456 |
operator text NOT NULL, |
| 457 |
value text NOT NULL, |
| 458 |
UNIQUE KEY id (id) |
| 459 |
) ".$charset_collate.";"; |
| 460 |
dbDelta($sql); |
| 461 |
|
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
// 2. SAVE LOCATION DATA AS RULES |
| 466 |
|
| 467 |
$table_name = $wpdb->prefix.'acf_options'; |
| 468 |
$rules = array(); |
| 469 |
|
| 470 |
|
| 471 |
// get fields and add them to $options |
| 472 |
$locations = $wpdb->get_results("SELECT * FROM $table_name WHERE type = 'location'"); |
| 473 |
|
| 474 |
|
| 475 |
// rewrite into new format |
| 476 |
if($locations) |
| 477 |
{ |
| 478 |
foreach($locations as $location) |
| 479 |
{ |
| 480 |
|
| 481 |
$values = unserialize($location->value); |
| 482 |
|
| 483 |
foreach($values as $value) |
| 484 |
{ |
| 485 |
|
| 486 |
$rules[] = array( |
| 487 |
'acf_id' => $location->acf_id, |
| 488 |
'order_no' => 0, |
| 489 |
'param' => substr($location->name, 0, -1), |
| 490 |
'operator' => '==', |
| 491 |
'value' => $value, |
| 492 |
); |
| 493 |
|
| 494 |
} |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
// 3. SAVE USER TYPE OPTION DATA AS RULES |
| 500 |
|
| 501 |
|
| 502 |
// get fields and add them to $options |
| 503 |
$locations = $wpdb->get_results("SELECT * FROM $table_name WHERE type = 'option' AND name = 'user_roles'"); |
| 504 |
|
| 505 |
|
| 506 |
// rewrite into new format |
| 507 |
if($locations) |
| 508 |
{ |
| 509 |
|
| 510 |
$user_roles = array( |
| 511 |
'10' => 'administrator', |
| 512 |
'7' => 'editor', |
| 513 |
'4' => 'author', |
| 514 |
'1' => 'contributor', |
| 515 |
); |
| 516 |
|
| 517 |
|
| 518 |
foreach($locations as $location) |
| 519 |
{ |
| 520 |
|
| 521 |
$values = unserialize($location->value); |
| 522 |
|
| 523 |
foreach($values as $value) |
| 524 |
{ |
| 525 |
|
| 526 |
$rules[] = array( |
| 527 |
'acf_id' => $location->acf_id, |
| 528 |
'order_no' => 0, |
| 529 |
'param' => 'user_type', |
| 530 |
'operator' => '==', |
| 531 |
'value' => $user_roles[$value], |
| 532 |
); |
| 533 |
|
| 534 |
} |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
// 4. SAVE RULES INTO DATABASE |
| 540 |
|
| 541 |
$table_name = $wpdb->prefix.'acf_rules'; |
| 542 |
|
| 543 |
foreach($rules as $rule) |
| 544 |
{ |
| 545 |
$wpdb->insert($table_name, $rule); |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
// 5. SAVE OPTIONS |
| 550 |
|
| 551 |
$table_name = $wpdb->prefix.'acf_options'; |
| 552 |
$options = array(); |
| 553 |
|
| 554 |
$options = $wpdb->get_results("SELECT * FROM $table_name WHERE type = 'option' AND name = 'show_on_page'"); |
| 555 |
|
| 556 |
if($options) |
| 557 |
{ |
| 558 |
foreach($options as $option) |
| 559 |
{ |
| 560 |
update_post_meta($option->acf_id, 'show_on_page', $option->value); |
| 561 |
|
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
// 6. SAVE NEW POST META |
| 567 |
|
| 568 |
$options = $wpdb->get_results("SELECT DISTINCT acf_id FROM $table_name WHERE type = 'option'"); |
| 569 |
|
| 570 |
|
| 571 |
if($options) |
| 572 |
{ |
| 573 |
foreach($options as $option) |
| 574 |
{ |
| 575 |
update_post_meta($option->acf_id, 'allorany', 'any'); |
| 576 |
update_post_meta($option->acf_id, 'field_group_layout', 'default'); |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
// drop options table |
| 582 |
$wpdb->query("DROP TABLE $table_name"); |
| 583 |
|
| 584 |
|
| 585 |
update_option('acf_version','2.0.2'); |
| 586 |
$version = '2.0.2'; |
| 587 |
|
| 588 |
|
| 589 |
} |
| 590 |
|
| 591 |
update_option('acf_version',$this->version); |
| 592 |
|
| 593 |
?> |