|
<?php |
|
|
|
add_action('init', function () { |
|
global $wpdb; |
|
|
|
if ( ! isset($_GET['acf_link_migration'])) { |
|
return; |
|
} |
|
|
|
$run = isset($_GET['run_migration']); |
|
|
|
$formatTarget = function ($target) { |
|
if ($target === '1') { |
|
$target = '_blank'; |
|
} elseif ($target === '0') { |
|
$target = ''; |
|
} |
|
return $target; |
|
}; |
|
|
|
$replaceCommentMeta = function ($name) use ($wpdb, $run, $formatTarget) { |
|
echo "\nMigrating comments\n"; |
|
|
|
$rows = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->commentmeta WHERE meta_key = %s", $name), ARRAY_A); |
|
|
|
foreach ($rows as $row) { |
|
$value = maybe_unserialize($row['meta_value']); |
|
if (is_array($value) && isset($value['target'])) { |
|
$orig = $value['target']; |
|
$value['target'] = $formatTarget($value['target']); |
|
|
|
if ($orig !== $value['target']) { |
|
echo " Updating comment #{$row['comment_id']} link target from '{$orig}' to '{$value['target']}'\n"; |
|
|
|
if ($run) { |
|
update_field($name, $value, 'comment_'.$row['comment_id']); |
|
} |
|
} |
|
} |
|
} |
|
}; |
|
|
|
$replacePostmeta = function ($name) use ($wpdb, $run, $formatTarget) { |
|
echo "\nMigrating posts\n"; |
|
|
|
$rows = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->postmeta WHERE meta_key = %s", $name), ARRAY_A); |
|
|
|
foreach ($rows as $row) { |
|
$value = maybe_unserialize($row['meta_value']); |
|
if (is_array($value) && isset($value['target'])) { |
|
$orig = $value['target']; |
|
$value['target'] = $formatTarget($value['target']); |
|
|
|
if ($orig !== $value['target']) { |
|
echo " Updating post #{$row['post_id']} link target from '{$orig}' to '{$value['target']}'\n"; |
|
|
|
if ($run) { |
|
update_field($name, $value, $row['post_id']); |
|
} |
|
} |
|
} |
|
} |
|
}; |
|
|
|
$replaceTermmeta = function ($name) use ($wpdb, $run, $formatTarget) { |
|
echo "\nMigrating terms\n"; |
|
|
|
$rows = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->termmeta WHERE meta_key = %s", $name), ARRAY_A); |
|
|
|
foreach ($rows as $row) { |
|
$value = maybe_unserialize($row['meta_value']); |
|
if (is_array($value) && isset($value['target'])) { |
|
$orig = $value['target']; |
|
$value['target'] = $formatTarget($value['target']); |
|
|
|
if ($orig !== $value['target']) { |
|
echo " Updating term #{$row['post_id']} link target from '{$orig}' to '{$value['target']}'\n"; |
|
|
|
if ($run) { |
|
update_field($name, $value, 'term_'.$row['term_id']); |
|
} |
|
} |
|
} |
|
} |
|
}; |
|
|
|
$replaceUsermeta = function ($name) use ($wpdb, $run, $formatTarget) { |
|
echo "\nMigrating users\n"; |
|
|
|
$rows = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->usermeta WHERE meta_key = %s", $name), ARRAY_A); |
|
|
|
foreach ($rows as $row) { |
|
$value = maybe_unserialize($row['meta_value']); |
|
if (is_array($value) && isset($value['target'])) { |
|
$orig = $value['target']; |
|
$value['target'] = $formatTarget($value['target']); |
|
|
|
if ($orig !== $value['target']) { |
|
echo " Updating user #{$row['user_id']} link target from '{$orig}' to '{$value['target']}'\n"; |
|
|
|
if ($run) { |
|
update_field($name, $value, 'user_'.$row['user_id']); |
|
} |
|
} |
|
} |
|
} |
|
}; |
|
|
|
$replaceOptions = function ($name) use ($wpdb, $run, $formatTarget) { |
|
echo "\nMigrating options\n"; |
|
|
|
$rows = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->options WHERE option_name = %s", $name), ARRAY_A); |
|
|
|
foreach ($rows as $row) { |
|
$value = maybe_unserialize($row['meta_value']); |
|
if (is_array($value) && isset($value['target'])) { |
|
$orig = $value['target']; |
|
$value['target'] = $formatTarget($value['target']); |
|
|
|
if ($orig !== $value['target']) { |
|
echo " Updating option #{$row['option_name']} link target from '{$orig}' to '{$value['target']}'\n"; |
|
|
|
if ($run) { |
|
update_field($name, $value, 'option_'.$row['option_name']); |
|
} |
|
} |
|
} |
|
} |
|
}; |
|
|
|
header('Content-Type: text/plain'); |
|
|
|
if ( ! $run) { |
|
echo "THIS IS A DRY-RUN/SIMULATION\n"; |
|
echo "Append `&run_migration` to the URL to actually run the migration\n"; |
|
echo "after you have verified what will happen is correct\n"; |
|
} |
|
|
|
echo "\nMigration begin\n"; |
|
|
|
foreach (acf_get_field_groups() as $group) { |
|
foreach (acf_get_fields($group) as $field) { |
|
if ($field['type'] !== 'link') { |
|
continue; |
|
} |
|
|
|
echo "\n----------\n"; |
|
echo "\nFound field: {$field['label']} [{$field['name']}:{$field['key']}]\n"; |
|
|
|
$name = $field['name']; |
|
$replaceCommentMeta($name); |
|
$replacePostmeta($name); |
|
$replaceTermmeta($name); |
|
$replaceUsermeta($name); |
|
$replaceOptions($name); |
|
} |
|
} |
|
|
|
echo "\nMIGRATION COMPLETE!\n"; |
|
die; |
|
}); |
Great work, thanks!