forked from BeAPI/ACF-PHP-Recovery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacf-php-recovery.php
176 lines (151 loc) · 5.33 KB
/
acf-php-recovery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/*
Plugin Name: ACF PHP Recovery Tool
Description: Converts your PHP export back-in as an editable ACF fields. To be used when you lose the original database and json fieldsets.
Author: BE API
Author URI: http://www.beapi.fr
Version: 1.0.0
*/
define('ACFPR_GROUP_POST_TYPE', 'acf-field-group');
function acf_php_recovery_menu() {
add_submenu_page('edit.php?post_type=' . ACFPR_GROUP_POST_TYPE, __('ACF PHP Recovery', 'acf_php_recovery'), __('PHP Recovery', 'acf_php_recovery'), 'manage_options', 'acf-php-import', 'acf_php_recovery_page');
}
add_action('admin_menu', 'acf_php_recovery_menu', 100);
function acf_php_recovery_page() {
global $wpdb;
$acf_local_groups = version_compare(acf_get_setting('version'), '5.7.11') >= 0
? acf_get_local_field_groups()
: acf_local();
$acf_local_fields = acf_get_local_fields();
// process the form
if (isset($_POST['acf_php_recovery_action']) && $_POST['acf_php_recovery_action'] == 'import' && isset($_POST['fieldsets']) && check_admin_referer('acf_php_recovery')) {
$import_fieldsets = $_POST['fieldsets'];
$imported = array(); // Keep track of the imported
$key_to_post_id = array(); // Group or field key to post id
// Now we can import the groups
foreach ($acf_local_groups as $key => $group) {
$group['title'] = $group['title'] . ' (Recovered)';
// Only import those that were selected
if (in_array($key, $import_fieldsets)) {
$saved_group = acf_update_field_group($group);
$key_to_post_id[$key] = $saved_group['ID'];
// For displaying the success message
$imported[] = array(
'title' => $group['title'],
'id' => $saved_group['ID'],);
}
}
// This requires multipile runs to handle sub-fields that have their parent set to the parent field instead of the group
$field_parents = $import_fieldsets; // The groups and fields
$imported_fields = array(); // Keep track of the already imported
do {
$num_import = 0;
foreach ($acf_local_fields as $key => $field) {
if (! in_array($key, $imported_fields) && in_array($field['parent'], $field_parents)) {
$num_import = $num_import + 1;
$field_parents[] = $key;
$imported_fields[] = $key;
$field['parent'] = $key_to_post_id[$field['parent']]; // Convert the key into the post_parent
$saved_field = acf_update_field($field);
$key_to_post_id[$key] = $saved_field['ID'];
}
}
} while ($num_import > 0);
}
// output
?>
<div class="wrap">
<h2>ACF PHP Recovery Tool</h2>
<?php
// Check the version of ACF
$acf_version = explode('.', acf_get_setting('version'));
if ($acf_version[0] != '5'):
?>
<div id="message" class="error below-h2">
<p><?php printf(__('This tool was built for ACF version 5 and you have version %s.'), $acf_version[0]); ?></p>
</div>
<?php
endif;
?>
<?php
if (! empty($imported)) {
?>
<div id="message" class="updated below-h2"><p><?php _e('Fieldsets recovered'); ?>.</p>
<ul>
<?php
foreach ($imported as $import) {
?>
<li><?php edit_post_link($import['title'], '', '', $import['id']); ?></li>
<?php
}
?>
<li>
<strong><?php _e('Remove the PHP defined fields! The duplicate field IDs interfer with the editing of the fields.'); ?></strong>
</li>
</ul>
</div>
<?php
}
?>
<p>
<strong>This is a recovery tool. Do not use this as part of your workflow for importing and exporting ACF fieldsets.</strong>
</p>
<form method="POST">
<table class="widefat">
<thead>
<th>Import</th>
<th>Name</th>
<th>Possible Existing Matches</th>
</thead>
<tbody>
<?php
foreach ($acf_local_groups as $key => $field_group): ?>
<tr>
<td><input type="checkbox" name="fieldsets[]" value="<?php echo esc_attr($key); ?>"/></td>
<td><?php echo $field_group['title']; ?></td>
<td><?php
$sql = "SELECT ID, post_title FROM $wpdb->posts WHERE post_title LIKE '%s' AND post_type='" . ACFPR_GROUP_POST_TYPE . "'";
// Set post status
$post_status = apply_filters('acf_recovery\query\post_status', '');
if (! empty($post_status)) {
$sql .= ' AND post_status="' . esc_sql($post_status) . '"';
}
$matches = $wpdb->get_results($wpdb->prepare($sql, '%' . $wpdb->esc_like($field_group['title']) . '%'));
if (empty($matches)) {
echo '<em>none</em>';
}
else {
$links = array();
foreach ($matches as $match) {
$links[] = '<a href="' . get_edit_post_link($match->ID) . '">' . $match->post_title . '</a>';
}
echo implode(', ', $links);
}
?></td>
</tr>
<?php
endforeach;
?>
</tbody>
</table>
<?php wp_nonce_field('acf_php_recovery'); ?>
<input type="hidden" name="acf_php_recovery_action" value="import"/>
<p class="submit">
<input type="submit" value="Import" class="button-primary"/>
</p>
</form>
<h3>Registered Field Groups</h3>
<pre class="">
<?php echo var_export($acf_local_groups); ?>
</pre>
</div>
<?php
}
// Add settings link on plugin page
function acf_php_recovery_settings_link($links) {
$settings_link = '<a href="edit.php?post_type=acf-field-group&page=acf-php-import">ACF PHP Recovery</a>';
array_unshift($links, $settings_link);
return $links;
}
$plugin = plugin_basename(__FILE__);
add_filter("plugin_action_links_$plugin", 'acf_php_recovery_settings_link');