block-type
1 month ago
dev
1 month ago
form
1 month ago
option
7 months ago
options-page
1 month ago
performance
1 month ago
post-type
1 month ago
taxonomy
1 month ago
ui
1 month ago
author.php
1 month ago
autosync-json.php
1 month ago
autosync-php.php
1 month ago
autosync-php.php
396 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')){ |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | if(!class_exists('ACFE_AutoSync_Php')): |
| 8 | |
| 9 | class ACFE_AutoSync_Php{ |
| 10 | |
| 11 | // vars |
| 12 | private $php_files = array(); |
| 13 | |
| 14 | /** |
| 15 | * construct |
| 16 | */ |
| 17 | function __construct(){ |
| 18 | |
| 19 | // hooks |
| 20 | add_action('acf/update_field_group', array($this, 'update_field_group')); |
| 21 | add_action('acf/untrash_field_group', array($this, 'update_field_group')); |
| 22 | add_action('acf/trash_field_group', array($this, 'delete_field_group')); |
| 23 | add_action('acf/delete_field_group', array($this, 'delete_field_group')); |
| 24 | |
| 25 | // setup |
| 26 | $this->setup_php(); |
| 27 | |
| 28 | } |
| 29 | |
| 30 | |
| 31 | /** |
| 32 | * is_php_enabled |
| 33 | * |
| 34 | * @return bool |
| 35 | */ |
| 36 | function is_php_enabled(){ |
| 37 | return (bool) acf_get_setting('acfe/php'); |
| 38 | } |
| 39 | |
| 40 | |
| 41 | /** |
| 42 | * setup_php |
| 43 | */ |
| 44 | function setup_php(){ |
| 45 | |
| 46 | if(!$this->is_php_enabled()){ |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | // vars |
| 51 | global $pagenow; |
| 52 | $files = $this->scan_php_folders(); |
| 53 | |
| 54 | // conditions |
| 55 | $acf_field_groups = $pagenow === 'edit.php' && acf_maybe_get_GET('post_type') === 'acf-field-group' && !acf_maybe_get_GET('page'); |
| 56 | $acf_field_group = $pagenow === 'post.php' && get_post_type(acf_maybe_get_GET('post')) === 'acf-field-group'; |
| 57 | |
| 58 | // do not include php files in acf ui |
| 59 | if($acf_field_groups || $acf_field_group){ |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | // loop files and require |
| 64 | foreach($files as $key => $file){ |
| 65 | require_once($file); |
| 66 | } |
| 67 | |
| 68 | } |
| 69 | |
| 70 | |
| 71 | /** |
| 72 | * scan_php_folders |
| 73 | * |
| 74 | * @return array |
| 75 | */ |
| 76 | function scan_php_folders(){ |
| 77 | |
| 78 | $php_files = array(); |
| 79 | $paths = (array) acf_get_setting('acfe/php_load'); |
| 80 | |
| 81 | foreach($paths as $path){ |
| 82 | |
| 83 | // normalize folder |
| 84 | $path = untrailingslashit($path); |
| 85 | if(!is_dir($path)){ |
| 86 | continue; |
| 87 | } |
| 88 | |
| 89 | // update setting |
| 90 | acf_update_setting('acfe/php_found', true); |
| 91 | |
| 92 | // normalize files |
| 93 | $files = glob($path . '/group_*.php'); |
| 94 | if(!$files){ |
| 95 | continue; |
| 96 | } |
| 97 | |
| 98 | // loop files |
| 99 | foreach($files as $file){ |
| 100 | |
| 101 | // extract key |
| 102 | $key = pathinfo($file, PATHINFO_FILENAME); |
| 103 | |
| 104 | // append data |
| 105 | $php_files[ $key ] = $file; |
| 106 | |
| 107 | } |
| 108 | |
| 109 | } |
| 110 | |
| 111 | // store data and return |
| 112 | $this->php_files = $php_files; |
| 113 | |
| 114 | // return |
| 115 | return $php_files; |
| 116 | |
| 117 | } |
| 118 | |
| 119 | |
| 120 | /** |
| 121 | * get_php_files |
| 122 | * |
| 123 | * @return array |
| 124 | */ |
| 125 | public function get_php_files(){ |
| 126 | return $this->php_files; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | /** |
| 131 | * update_field_group |
| 132 | * |
| 133 | * @param $field_group |
| 134 | */ |
| 135 | function update_field_group($field_group){ |
| 136 | |
| 137 | // save file |
| 138 | if(acfe_has_php_sync($field_group)){ |
| 139 | $this->save_file($field_group); |
| 140 | |
| 141 | // delete file |
| 142 | }else{ |
| 143 | $this->delete_file($field_group); |
| 144 | } |
| 145 | |
| 146 | } |
| 147 | |
| 148 | |
| 149 | /** |
| 150 | * delete_field_group |
| 151 | * |
| 152 | * @param $field_group |
| 153 | */ |
| 154 | function delete_field_group($field_group){ |
| 155 | |
| 156 | // wp appends '__trashed' to end of 'key' (post_name) |
| 157 | $field_group['key'] = str_replace('__trashed', '', $field_group['key']); |
| 158 | |
| 159 | $this->delete_file($field_group); |
| 160 | |
| 161 | } |
| 162 | |
| 163 | |
| 164 | /** |
| 165 | * save_file |
| 166 | * |
| 167 | * @param $field_group |
| 168 | * |
| 169 | * @return bool |
| 170 | */ |
| 171 | function save_file($field_group){ |
| 172 | |
| 173 | // bail early |
| 174 | if(!$this->is_php_enabled()){ |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | // vars |
| 179 | $path = $this->get_php_save_path($field_group); |
| 180 | $file = untrailingslashit($path) . '/' . $field_group['key'] . '.php'; |
| 181 | |
| 182 | // validate path |
| 183 | if(!is_writable($path)){ |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | // translation |
| 188 | $l10n = acf_get_setting('l10n'); |
| 189 | $l10n_textdomain = acf_get_setting('l10n_textdomain'); |
| 190 | |
| 191 | if(!$l10n || !$l10n_textdomain){ |
| 192 | $field_group['fields'] = acf_get_fields($field_group); |
| 193 | |
| 194 | }else{ |
| 195 | |
| 196 | acf_update_setting('l10n_var_export', true); |
| 197 | |
| 198 | $field_group = acf_translate_field_group($field_group); |
| 199 | |
| 200 | // reset store to allow fields translation |
| 201 | $store = acf_get_store('fields'); |
| 202 | $store->reset(); |
| 203 | |
| 204 | $field_group['fields'] = acf_get_fields($field_group); |
| 205 | |
| 206 | acf_update_setting('l10n_var_export', false); |
| 207 | |
| 208 | // reset store again to avoid storing translated fields |
| 209 | // this fix an issue with acfml 2.0.2 which update fields as "!!__(!!'My Field!!', !!'my-textdomain!!')!!" in ACF UI |
| 210 | $store->reset(); |
| 211 | |
| 212 | } |
| 213 | |
| 214 | // prepare for export |
| 215 | $id = acf_extract_var($field_group, 'ID'); |
| 216 | $field_group = acf_prepare_field_group_for_export($field_group); |
| 217 | |
| 218 | // add modified time |
| 219 | $field_group['modified'] = get_post_modified_time('U', true, $id, true); |
| 220 | |
| 221 | // prepare |
| 222 | $str_replace = array( |
| 223 | " " => "\t", |
| 224 | "'!!__(!!\'" => "__('", |
| 225 | "!!\', !!\'" => "', '", |
| 226 | "!!\')!!'" => "')", |
| 227 | "array (" => "array(" |
| 228 | ); |
| 229 | |
| 230 | $preg_replace = array( |
| 231 | '/([\t\r\n]+?)array/' => 'array', |
| 232 | '/[0-9]+ => array/' => 'array' |
| 233 | ); |
| 234 | |
| 235 | ob_start(); |
| 236 | |
| 237 | echo "<?php " . "\r\n" . "\r\n"; |
| 238 | echo "if( function_exists('acf_add_local_field_group') ):" . "\r\n" . "\r\n"; |
| 239 | |
| 240 | // code |
| 241 | $code = var_export($field_group, true); |
| 242 | |
| 243 | // change double spaces to tabs |
| 244 | $code = str_replace( array_keys($str_replace), array_values($str_replace), $code ); |
| 245 | |
| 246 | // correctly formats "=> array(" |
| 247 | $code = preg_replace( array_keys($preg_replace), array_values($preg_replace), $code ); |
| 248 | |
| 249 | // echo |
| 250 | echo "acf_add_local_field_group({$code});" . "\r\n" . "\r\n"; |
| 251 | |
| 252 | echo "endif;"; |
| 253 | |
| 254 | $output = ob_get_clean(); |
| 255 | |
| 256 | // save and return true if bytes were written. |
| 257 | $result = file_put_contents($file, $output); |
| 258 | |
| 259 | // return |
| 260 | return is_int($result); |
| 261 | |
| 262 | } |
| 263 | |
| 264 | |
| 265 | /** |
| 266 | * delete_file |
| 267 | * |
| 268 | * @param $field_group |
| 269 | * |
| 270 | * @return void |
| 271 | */ |
| 272 | function delete_file($field_group){ |
| 273 | |
| 274 | // validate |
| 275 | if(!$this->is_php_enabled()){ |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | // filters |
| 280 | $delete = true; |
| 281 | $delete = apply_filters("acfe/settings/should_delete_php", $delete, $field_group); |
| 282 | $delete = apply_filters("acfe/settings/should_delete_php/ID={$field_group['ID']}", $delete, $field_group); |
| 283 | $delete = apply_filters("acfe/settings/should_delete_php/key={$field_group['key']}", $delete, $field_group); |
| 284 | |
| 285 | // do not delete |
| 286 | if(!$delete){ |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | // vars |
| 291 | $path = $this->get_php_save_path($field_group); |
| 292 | $file = untrailingslashit($path) . '/' . $field_group['key'] . '.php'; |
| 293 | |
| 294 | // delete |
| 295 | if(is_readable($file)){ |
| 296 | unlink($file); |
| 297 | } |
| 298 | |
| 299 | } |
| 300 | |
| 301 | |
| 302 | /** |
| 303 | * get_php_save_path |
| 304 | * |
| 305 | * @param $field_group |
| 306 | * |
| 307 | * @return mixed|null |
| 308 | */ |
| 309 | function get_php_save_path($field_group){ |
| 310 | |
| 311 | // default |
| 312 | $path = untrailingslashit(acf_get_setting('acfe/php_save')); |
| 313 | |
| 314 | // filters |
| 315 | $path = apply_filters("acfe/settings/php_save/all", $path, $field_group); |
| 316 | $path = apply_filters("acfe/settings/php_save/ID={$field_group['ID']}", $path, $field_group); |
| 317 | $path = apply_filters("acfe/settings/php_save/key={$field_group['key']}", $path, $field_group); |
| 318 | |
| 319 | return $path; |
| 320 | |
| 321 | } |
| 322 | |
| 323 | } |
| 324 | |
| 325 | acf_new_instance('ACFE_AutoSync_Php'); |
| 326 | |
| 327 | endif; |
| 328 | |
| 329 | |
| 330 | /** |
| 331 | * acfe_get_local_php_files |
| 332 | * |
| 333 | * @return mixed |
| 334 | */ |
| 335 | function acfe_get_local_php_files(){ |
| 336 | return acf_get_instance('ACFE_AutoSync_Php')->get_php_files(); |
| 337 | } |
| 338 | |
| 339 | |
| 340 | /** |
| 341 | * acfe_has_php_sync |
| 342 | * |
| 343 | * @param $item |
| 344 | * |
| 345 | * @return bool |
| 346 | */ |
| 347 | function acfe_has_php_sync($item){ |
| 348 | |
| 349 | // default item sync path |
| 350 | $path = 'acfe_autosync'; |
| 351 | |
| 352 | // exception: field group use acfe.autosync |
| 353 | if(!empty($item['key']) && acf_is_field_group_key($item['key'])){ |
| 354 | $path = 'acfe.autosync'; |
| 355 | } |
| 356 | |
| 357 | return in_array('php', (array) acfe_get($item, $path, array())); |
| 358 | } |
| 359 | |
| 360 | |
| 361 | /** |
| 362 | * acfe_get_local_php_file |
| 363 | * |
| 364 | * @param $field_group |
| 365 | * |
| 366 | * @return false|mixed |
| 367 | */ |
| 368 | function acfe_get_local_php_file($field_group){ |
| 369 | |
| 370 | $key = $field_group; |
| 371 | |
| 372 | if(is_array($field_group) && isset($field_group['key'])){ |
| 373 | $key = $field_group['key']; |
| 374 | } |
| 375 | |
| 376 | $php_files = acfe_get_local_php_files(); |
| 377 | |
| 378 | if(isset($php_files[ $key ])){ |
| 379 | return $php_files[ $key ]; |
| 380 | } |
| 381 | |
| 382 | return false; |
| 383 | |
| 384 | } |
| 385 | |
| 386 | |
| 387 | /** |
| 388 | * acfe_get_php_save_path |
| 389 | * |
| 390 | * @param $field_group |
| 391 | * |
| 392 | * @return mixed |
| 393 | */ |
| 394 | function acfe_get_php_save_path($field_group){ |
| 395 | return acf_get_instance('ACFE_AutoSync_Php')->get_php_save_path($field_group); |
| 396 | } |