-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathacf-sync.php
331 lines (221 loc) · 8.34 KB
/
acf-sync.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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
/*
Plugin Name: ACF Sync
Plugin URI: https://github.com/FreshFlesh/ACF-Sync
Description: Keep your ACF field groups synchronized between different environments
Version: 1.1.2
Author: Thomas Charbit
Author URI: https://twitter.com/thomascharbit
Author Email: [email protected]
*/
if ( ! defined( 'WPINC' ) ) {
die;
}
class ACFSync {
/*--------------------------------------------*
* Constructor
*--------------------------------------------*/
/**
* Initializes the plugin
*/
function __construct() {
// Sync fields on admin_init if needed
add_action( 'admin_init', array( $this, 'check_acf_fields_version' ) );
// Load plugin text domain
add_action( 'admin_init', array( $this, 'plugin_textdomain' ) );
// Add Admin UI for manual sync
add_action('admin_footer', array( $this, 'render_admin_view' ) );
// Handle Admin form
add_action( 'admin_post_acf-manual-sync', array( $this, 'manual_sync_action' ) );
// Admin notices
add_action( 'admin_notices', array( $this, 'admin_notices' ) );
// On prod or staging, let ACF simply read fields from JSON
if ( defined( 'WP_ENV' ) && 'development' != WP_ENV ) {
// Don't save fields to JSON
add_filter( 'acf/settings/save_json', '__return_null', 99 );
// Don't show ACF UI
add_filter( 'acf/settings/show_admin', '__return_false' );
}
} // end constructor
/**
* Loads the plugin text domain for translation
*/
public function plugin_textdomain() {
$domain = 'acfsync';
$locale = apply_filters( 'plugin_locale', get_locale(), $domain );
load_textdomain( $domain, WP_LANG_DIR.'/'.$domain.'/'.$domain.'-'.$locale.'.mo' );
if ( false !== strpos( __FILE__, basename( WPMU_PLUGIN_DIR ) ) ) {
load_muplugin_textdomain( $domain, dirname( plugin_basename( __FILE__ ) ) . '/lang/' );
}
else {
load_plugin_textdomain( $domain, FALSE, dirname( plugin_basename( __FILE__ ) ) . '/lang/' );
}
} // end plugin_textdomain
/*--------------------------------------------*
* Core Functions
*--------------------------------------------*/
/*
* check_acf_fields_version
*
* Compare fields version and import newer version if needed
*
* @param n/a
* @return n/a
*/
public function check_acf_fields_version() {
if ( defined( 'ACF_FIELDS_VERSION' ) && acf_get_setting('json') ) {
$db_version = get_option( 'acf_fields_version' );
// Import fields from local JSON if they are newer than in DB
if ( version_compare( ACF_FIELDS_VERSION, $db_version ) > 0 ) {
$success = $this->import_json_field_groups();
if ( $success ) {
update_option( 'acf_fields_version', ACF_FIELDS_VERSION );
}
}
}
}
/*
* render_admin_view
*
* Render admin form on ACF settings-export page
*
* @param n/a
* @return n/a
*/
public function render_admin_view() {
if ( !acf_get_setting('json') ) return;
include( 'admin/views/json-import.php' );
}
/*
* manual_sync_action
*
* Validate form data and import field groups
*
* @param n/a
* @return n/a
*/
public function manual_sync_action() {
if ( ! wp_verify_nonce( $_POST[ '_acfnonce' ], 'acfsync' ) ) {
die( 'Invalid nonce.' );
}
$success = $this->import_json_field_groups();
if ( $success ) {
if ( defined( 'ACF_FIELDS_VERSION' ) ) {
update_option( 'acf_fields_version', ACF_FIELDS_VERSION );
}
}
$url = add_query_arg( array( 'fields-sync' => $success ), $_SERVER['HTTP_REFERER'] );
wp_safe_redirect( $url );
exit;
}
/*
* admin_notices
*
* Display relevant admin notice after manual import
*
* @param n/a
* @return n/a
*/
public function admin_notices() {
if ( !isset( $_GET['fields-sync']) ) return;
if ( $_GET['fields-sync'] == true ) {
echo '<div class="updated"><p>' . esc_html__( 'Field groups updated !', 'acfsync' ) . '</p></div>';
}
else {
echo '<div class="error"><p>' . esc_html__( 'Sorry, unable to sync your field groups. Make sure you have the local JSON feature enabled and that your JSON folder is readable.', 'acfsync' ) . '</p></div>';
}
}
/*
* import_json_field_groups
*
* Parse json load points paths and import all JSON files
*
* @param n/a
* @return bool
*/
private function import_json_field_groups() {
if ( !acf_get_setting('json') ) return false;
// Check if JSON paths are readable
$json_paths = acf_get_setting('load_json');
foreach ($json_paths as $json_path) {
if ( !is_readable( $json_path ) ) {
return false;
}
}
// Tell ACF NOT to save to local JSON while we delete groups in DB
add_filter('acf/settings/save_json', '__return_null', 99 );
// Remove previous field groups
$args = array(
'post_type' => 'acf-field-group',
'post_status' => 'any',
'posts_per_page' => -1
);
$query = new WP_Query( $args );
foreach ( $query->posts as $acf_group ) {
wp_delete_post( $acf_group->ID, true);
}
// Parse local JSON load points directories
foreach ($json_paths as $json_path) {
$dir = new DirectoryIterator( $json_path );
foreach( $dir as $file ) {
if ( !$file->isDot() && 'json' == $file->getExtension() ) {
$json = json_decode( file_get_contents( $file->getPathname() ), true );
$this->import_json_field_group( $json );
}
}
}
return true;
}
/*
* import_json_field_group
*
* import ACF field group from JSON data
*
* @param $json (array)
* @return n/a
*/
private function import_json_field_group( $json ) {
// What follows is basically a copy of import() in ACF admin/settings-export.php
// if importing an auto-json, wrap field group in array
if( isset($json['key']) ) {
$json = array( $json );
}
// vars
$added = array();
$ignored = array();
$ref = array();
$order = array();
foreach( $json as $field_group ) {
// remove fields
$fields = acf_extract_var($field_group, 'fields');
// format fields
$fields = acf_prepare_fields_for_import( $fields );
// save field group
$field_group = acf_update_field_group( $field_group );
// add to ref
$ref[ $field_group['key'] ] = $field_group['ID'];
// add to order
$order[ $field_group['ID'] ] = 0;
// add fields
foreach( $fields as $field ) {
// add parent
if( empty($field['parent']) ) {
$field['parent'] = $field_group['ID'];
} elseif( isset($ref[ $field['parent'] ]) ) {
$field['parent'] = $ref[ $field['parent'] ];
}
// add field menu_order
if( !isset($order[ $field['parent'] ]) ) {
$order[ $field['parent'] ] = 0;
}
$field['menu_order'] = $order[ $field['parent'] ];
$order[ $field['parent'] ]++;
// save field
$field = acf_update_field( $field );
// add to ref
$ref[ $field['key'] ] = $field['ID'];
}
}
}
}
new ACFSync();