form
6 years ago
author.php
6 years ago
autosync.php
6 years ago
dev.php
6 years ago
dynamic-block-type.php
6 years ago
dynamic-form.php
6 years ago
dynamic-options-page.php
6 years ago
dynamic-post-type.php
6 years ago
dynamic-taxonomy.php
6 years ago
taxonomy.php
6 years ago
autosync.php
298 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')) |
| 4 | exit; |
| 5 | |
| 6 | /** |
| 7 | * Auto Sync: Includes |
| 8 | */ |
| 9 | $acfe_php = acf_get_setting('acfe/php'); |
| 10 | $acfe_php_load = acf_get_setting('acfe/php_load'); |
| 11 | |
| 12 | if(!empty($acfe_php) && !empty($acfe_php_load)){ |
| 13 | foreach($acfe_php_load as $path){ |
| 14 | if(!is_readable($path)) |
| 15 | continue; |
| 16 | |
| 17 | acf_update_setting('acfe/php_found', true); |
| 18 | |
| 19 | $files = glob($path . '/*.php'); |
| 20 | if(empty($files)) |
| 21 | continue; |
| 22 | |
| 23 | foreach($files as $file){ |
| 24 | require_once($file); |
| 25 | } |
| 26 | |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Auto Sync: Disable json |
| 32 | */ |
| 33 | add_action('acf/update_field_group', 'acfe_autosync_json_update_field_group', 9); |
| 34 | function acfe_autosync_json_update_field_group($field_group){ |
| 35 | |
| 36 | // Validate |
| 37 | if(!acf_get_setting('json')) |
| 38 | return; |
| 39 | |
| 40 | // Disable json sync for this field group |
| 41 | if(!acfe_has_field_group_autosync($field_group, 'json')) |
| 42 | add_filter('acf/settings/json', 'acfe_autosync_temp_disable_json'); |
| 43 | |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Auto Sync: Re-enable json |
| 48 | */ |
| 49 | add_action('acf/update_field_group', 'acfe_autosync_json_after_update_field_group', 11); |
| 50 | function acfe_autosync_json_after_update_field_group($field_group){ |
| 51 | |
| 52 | // Validate |
| 53 | if(!acf_get_setting('json')) |
| 54 | return; |
| 55 | |
| 56 | remove_filter('acf/settings/json', 'acfe_autosync_temp_disable_json'); |
| 57 | |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Auto Sync: Disable json (function) |
| 62 | */ |
| 63 | function acfe_autosync_temp_disable_json(){ |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Auto Sync: PHP |
| 69 | */ |
| 70 | add_action('acf/update_field_group', 'acfe_autosync_php_update_field_group'); |
| 71 | function acfe_autosync_php_update_field_group($field_group){ |
| 72 | |
| 73 | // Validate |
| 74 | if(!acf_get_setting('acfe/php')) |
| 75 | return; |
| 76 | |
| 77 | if(!acfe_has_field_group_autosync($field_group, 'php')) |
| 78 | return; |
| 79 | |
| 80 | $field_group['fields'] = acf_get_fields($field_group); |
| 81 | acfe_autosync_write_php($field_group); |
| 82 | |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Auto Sync: Write PHP |
| 87 | */ |
| 88 | function acfe_autosync_write_php($field_group){ |
| 89 | |
| 90 | $path = acf_get_setting('acfe/php_save'); |
| 91 | if(empty($path)) |
| 92 | return false; |
| 93 | |
| 94 | // vars |
| 95 | $path = untrailingslashit($path); |
| 96 | $file = $field_group['key'] . '.php'; |
| 97 | |
| 98 | // bail early if dir does not exist |
| 99 | if(!is_writable($path)) |
| 100 | return false; |
| 101 | |
| 102 | // prepare for export |
| 103 | $id = acf_extract_var($field_group, 'ID'); |
| 104 | $field_group = acf_prepare_field_group_for_export($field_group); |
| 105 | |
| 106 | // add modified time |
| 107 | $field_group['modified'] = get_post_modified_time('U', true, $id, true); |
| 108 | |
| 109 | |
| 110 | // Prepare |
| 111 | $str_replace = array( |
| 112 | " " => "\t", |
| 113 | "'!!__(!!\'" => "__('", |
| 114 | "!!\', !!\'" => "', '", |
| 115 | "!!\')!!'" => "')", |
| 116 | "array (" => "array(" |
| 117 | ); |
| 118 | |
| 119 | $preg_replace = array( |
| 120 | '/([\t\r\n]+?)array/' => 'array', |
| 121 | '/[0-9]+ => array/' => 'array' |
| 122 | ); |
| 123 | |
| 124 | ob_start(); |
| 125 | |
| 126 | echo "<?php " . "\r\n" . "\r\n"; |
| 127 | echo "if( function_exists('acf_add_local_field_group') ):" . "\r\n" . "\r\n"; |
| 128 | |
| 129 | // code |
| 130 | $code = var_export($field_group, true); |
| 131 | |
| 132 | |
| 133 | // change double spaces to tabs |
| 134 | $code = str_replace( array_keys($str_replace), array_values($str_replace), $code ); |
| 135 | |
| 136 | |
| 137 | // correctly formats "=> array(" |
| 138 | $code = preg_replace( array_keys($preg_replace), array_values($preg_replace), $code ); |
| 139 | |
| 140 | |
| 141 | // esc_textarea |
| 142 | $code = $code; |
| 143 | |
| 144 | // echo |
| 145 | echo "acf_add_local_field_group({$code});" . "\r\n" . "\r\n"; |
| 146 | |
| 147 | echo "endif;"; |
| 148 | |
| 149 | $output = ob_get_clean(); |
| 150 | |
| 151 | // write file |
| 152 | $f = fopen("{$path}/{$file}", 'w'); |
| 153 | fwrite($f, $output); |
| 154 | fclose($f); |
| 155 | |
| 156 | // return |
| 157 | return true; |
| 158 | |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Auto Sync: Helper - is field group json desync |
| 163 | */ |
| 164 | function acfe_is_field_group_json_desync($field_group){ |
| 165 | |
| 166 | acf_enable_filter('local'); |
| 167 | $group = acf_get_local_field_group($field_group['key']); |
| 168 | acf_disable_filter('local'); |
| 169 | |
| 170 | $private = acf_maybe_get($group, 'private', false); |
| 171 | $local = acf_maybe_get($group, 'local', false); |
| 172 | $modified = acf_maybe_get($group, 'modified', 0); |
| 173 | |
| 174 | if($private){ |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | elseif($local !== 'json'){ |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | elseif($modified && $modified > get_post_modified_time('U', true, $field_group['ID'], true)){ |
| 183 | return true; |
| 184 | } |
| 185 | |
| 186 | return false; |
| 187 | |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Auto Sync: Helper - Has field group autosync |
| 192 | */ |
| 193 | function acfe_has_field_group_autosync($field_group, $type = false){ |
| 194 | $acfe_autosync = acf_maybe_get($field_group, 'acfe_autosync', array()); |
| 195 | |
| 196 | if(!$type) |
| 197 | return acf_is_array($acfe_autosync); |
| 198 | |
| 199 | if($type === 'json') |
| 200 | return is_array($acfe_autosync) && in_array('json', $acfe_autosync); |
| 201 | |
| 202 | elseif($type === 'php') |
| 203 | return is_array($acfe_autosync) && in_array('php', $acfe_autosync); |
| 204 | |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Auto Sync: Helper - Has field group autosync found register/file |
| 210 | */ |
| 211 | function acfe_has_field_group_autosync_file($field_group, $type = 'json'){ |
| 212 | |
| 213 | if($type === 'json'){ |
| 214 | |
| 215 | // acf_is_local_field_group = true if json file found |
| 216 | $found = false; |
| 217 | |
| 218 | if(acf_is_local_field_group($field_group['key'])){ |
| 219 | |
| 220 | $local_field_group = acf_get_local_field_group($field_group['key']); |
| 221 | $get_local = acf_maybe_get($local_field_group, 'local', false); |
| 222 | |
| 223 | if($get_local === 'json'){ |
| 224 | |
| 225 | $found = true; |
| 226 | |
| 227 | }else{ |
| 228 | |
| 229 | $paths = acf_get_setting('load_json'); |
| 230 | |
| 231 | if(!empty($paths)){ |
| 232 | foreach($paths as $path){ |
| 233 | |
| 234 | $path = untrailingslashit($path); |
| 235 | $file = $field_group['key'] . '.json'; |
| 236 | |
| 237 | if(is_readable("{$path}/{$file}")){ |
| 238 | |
| 239 | $found = true; |
| 240 | break; |
| 241 | |
| 242 | } |
| 243 | |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | } |
| 248 | |
| 249 | } |
| 250 | |
| 251 | else{ |
| 252 | |
| 253 | $paths = acf_get_setting('load_json'); |
| 254 | |
| 255 | if(!empty($paths)){ |
| 256 | foreach($paths as $path){ |
| 257 | |
| 258 | $path = untrailingslashit($path); |
| 259 | $file = $field_group['key'] . '.json'; |
| 260 | |
| 261 | if(is_readable("{$path}/{$file}")){ |
| 262 | |
| 263 | $found = true; |
| 264 | break; |
| 265 | |
| 266 | } |
| 267 | |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | } |
| 272 | |
| 273 | return $found; |
| 274 | |
| 275 | } |
| 276 | |
| 277 | elseif($type === 'php'){ |
| 278 | |
| 279 | // acf_is_local_field_group = true if php registered |
| 280 | $found = false; |
| 281 | |
| 282 | if(acf_is_local_field_group($field_group['key'])){ |
| 283 | |
| 284 | $local_field_group = acf_get_local_field_group($field_group['key']); |
| 285 | $get_local = acf_maybe_get($local_field_group, 'local', false); |
| 286 | |
| 287 | if($get_local === 'php') |
| 288 | $found = true; |
| 289 | |
| 290 | } |
| 291 | |
| 292 | return $found; |
| 293 | |
| 294 | } |
| 295 | |
| 296 | return false; |
| 297 | |
| 298 | } |