-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathacf-sync.php
More file actions
273 lines (219 loc) · 8.52 KB
/
Copy pathacf-sync.php
File metadata and controls
273 lines (219 loc) · 8.52 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
/**
* Plugin Name: ACF Sync
* Plugin URI: https://github.com/johnssondane/acf-sync
* Description: Save ACF fields to JSON files all at once.
* Version: 1.0.0
* Author: dane
* Author URI: https://github.com/johnssondane
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: acf-sync
*/
if (!defined('ABSPATH')) {
exit;
}
if (!class_exists('ACFSync')) {
class ACFSync
{
private $initialized = false;
private $key_path_array = [
'ui_options_page' => 'options-pages',
'post_type' => 'post-types',
'taxonomy' => 'taxonomies',
'group' => 'field-groups',
];
private $option_name = 'acf_sync_json_relative_dir';
private $option_default = 'uploads/acf-json';
private function get_json_base_path()
{
$relative = get_option($this->option_name, $this->option_default);
$relative = trim($relative, "/\\");
$relative = sanitize_text_field($relative);
return path_join(WP_CONTENT_DIR, $relative);
}
private function sanitize_relative_content_path($path)
{
$path = sanitize_text_field($path);
$path = trim($path, "/\\");
if ($path === '') return 'acf-json';
if (
str_starts_with($path, '/') ||
str_starts_with($path, '\\') ||
str_contains($path, '..') ||
preg_match('/^[a-zA-Z]:\\\\/', $path)
) {
return 'acf-json';
}
return $path;
}
public function __construct() {}
public function init()
{
if ($this->initialized) return;
if (!$this->check_dependencies()) {
add_action('admin_notices', [$this, 'missing_dependencies_notice']);
return;
}
add_action('admin_menu', [$this, 'sync_options_page'], PHP_INT_MAX, 1);
add_filter('acf/json/save_paths', [$this, 'set_save_paths'], PHP_INT_MAX, 2);
add_filter('acf/json/load_paths', [$this, 'set_load_paths'], PHP_INT_MAX, 1);
add_action('admin_bar_menu', [$this, 'add_sync_button'], PHP_INT_MAX, 1);
add_action('admin_post_acf_sync', [$this, 'handle_sync']);
add_action('admin_notices', [$this, 'successful_sync_notice'], PHP_INT_MAX, 1);
$this->initialized = true;
}
public function check_dependencies()
{
if (!function_exists('is_plugin_active')) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
return is_plugin_active('advanced-custom-fields/acf.php') ||
is_plugin_active('advanced-custom-fields-pro/acf.php');
}
public function missing_dependencies_notice()
{
echo '<div class="notice notice-error"><p>';
echo esc_html__('Advanced Custom Fields Sync requires the Advanced Custom Fields plugin to be installed and active.', 'acf-sync');
echo '</p></div>';
}
public function sync_options_page()
{
add_submenu_page(
'edit.php?post_type=acf-field-group',
'Sync',
'Sync',
'manage_options',
'acf-sync',
[$this, 'sync_options_page_content'],
);
}
public function sync_options_page_content()
{
$relative_dir = get_option($this->option_name, $this->option_default);
?>
<div class="wrap">
<h1>JSON Sync</h1>
<form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>">
<?php wp_nonce_field('acf_sync_action'); ?>
<table class="form-table" role="presentation">
<tr>
<th scope="row">
<label for="acf_sync_json_relative_dir">ACF JSON directory</label>
</th>
<td>
<code>wp-content/</code>
<input
type="text"
id="acf_sync_json_relative_dir"
name="acf_sync_json_relative_dir"
value="<?php echo esc_attr($relative_dir); ?>"
class="regular-text"
placeholder="acf-json">
<p class="description">
Directory relative to wp-content. Example: <code>acf-json</code> or <code>themes/my-theme/acf-json</code>.
</p>
</td>
</tr>
</table>
<input type="hidden" name="action" value="acf_sync">
<?php submit_button('Save & Sync'); ?>
</form>
</div>
<?php
}
public function set_save_paths($paths, $post)
{
$base = $this->get_json_base_path();
if (!is_dir($base)) {
wp_mkdir_p($base);
}
$key = $post['key'] ?? '';
foreach ($this->key_path_array as $key_prefix => $path) {
if (!str_starts_with($key, $key_prefix)) {
continue;
}
$dir = path_join($base, $path);
if (!is_dir($dir)) {
wp_mkdir_p($dir);
}
return [$dir];
}
return $paths;
}
public function set_load_paths($paths)
{
$base = $this->get_json_base_path();
foreach ($this->key_path_array as $_ => $path) {
$paths[] = path_join($base, $path);
}
return $paths;
}
public function add_sync_button($wp_admin_bar)
{
if (!current_user_can('manage_options')) {
return;
}
$url = admin_url('admin-post.php?action=acf_sync');
$url = wp_nonce_url($url, 'acf_sync_action');
$wp_admin_bar->add_node([
'id' => 'acf-sync',
'title' => 'Sync',
'href' => $url,
]);
}
public function handle_sync()
{
if (!current_user_can('manage_options')) {
wp_die(__('You do not have permission to run ACF sync.', 'acf-sync'));
}
check_admin_referer('acf_sync_action');
if (isset($_POST['acf_sync_json_relative_dir'])) {
$relative_dir = $this->sanitize_relative_content_path(
wp_unslash($_POST['acf_sync_json_relative_dir'])
);
update_option($this->option_name, $relative_dir);
}
$acf_content = array_merge(
acf_get_raw_post_types(),
acf_get_raw_taxonomies(),
acf_get_field_groups()
);
if (function_exists('acf_get_options_pages')) {
$acf_content = array_merge($acf_content, acf_get_options_pages());
}
foreach ($acf_content as $content) {
acf_write_json_field_group($content);
}
wp_safe_redirect(
add_query_arg(
[
'page' => 'acf-sync',
'acf_sync_done' => 1,
'acf_sync_count' => count($acf_content),
],
admin_url('edit.php?post_type=acf-field-group')
)
);
exit;
}
public function successful_sync_notice()
{
if (!isset($_GET['acf_sync_done'])) return;
$count = absint($_GET['acf_sync_count'] ?? 0);
echo '<div class="notice notice-success is-dismissible"><p>';
echo esc_html(sprintf('ACF Sync: %d items processed.', $count));
echo '</p></div>';
}
}
function acf_sync()
{
global $acf_sync;
if (!$acf_sync) {
$acf_sync = new ACFSync();
$acf_sync->init();
}
return $acf_sync;
}
acf_sync();
}