| 1 |
<?php |
| 2 |
|
| 3 |
if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 4 |
|
| 5 |
if( ! class_exists('acf_json') ) : |
| 6 |
|
| 7 |
class acf_json { |
| 8 |
|
| 9 |
function __construct() { |
| 10 |
|
| 11 |
// update setting |
| 12 |
acf_update_setting('save_json', get_stylesheet_directory() . '/acf-json'); |
| 13 |
acf_append_setting('load_json', get_stylesheet_directory() . '/acf-json'); |
| 14 |
|
| 15 |
|
| 16 |
// actions |
| 17 |
add_action('acf/update_field_group', array($this, 'update_field_group'), 10, 1); |
| 18 |
add_action('acf/duplicate_field_group', array($this, 'update_field_group'), 10, 1); |
| 19 |
add_action('acf/untrash_field_group', array($this, 'update_field_group'), 10, 1); |
| 20 |
add_action('acf/trash_field_group', array($this, 'delete_field_group'), 10, 1); |
| 21 |
add_action('acf/delete_field_group', array($this, 'delete_field_group'), 10, 1); |
| 22 |
add_action('acf/include_fields', array($this, 'include_json_folders'), 10, 0); |
| 23 |
|
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
/* |
| 28 |
* update_field_group |
| 29 |
* |
| 30 |
* This function is hooked into the acf/update_field_group action and will save all field group data to a .json file |
| 31 |
* |
| 32 |
* @type function |
| 33 |
* @date 10/03/2014 |
| 34 |
* @since 5.0.0 |
| 35 |
* |
| 36 |
* @param $field_group (array) |
| 37 |
* @return n/a |
| 38 |
*/ |
| 39 |
|
| 40 |
function update_field_group( $field_group ) { |
| 41 |
|
| 42 |
// validate |
| 43 |
if( !acf_get_setting('json') ) return; |
| 44 |
|
| 45 |
|
| 46 |
// get fields |
| 47 |
$field_group['fields'] = acf_get_fields( $field_group ); |
| 48 |
|
| 49 |
|
| 50 |
// save file |
| 51 |
acf_write_json_field_group( $field_group ); |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
/* |
| 57 |
* delete_field_group |
| 58 |
* |
| 59 |
* This function will remove the field group .json file |
| 60 |
* |
| 61 |
* @type function |
| 62 |
* @date 10/03/2014 |
| 63 |
* @since 5.0.0 |
| 64 |
* |
| 65 |
* @param $field_group (array) |
| 66 |
* @return n/a |
| 67 |
*/ |
| 68 |
|
| 69 |
function delete_field_group( $field_group ) { |
| 70 |
|
| 71 |
// validate |
| 72 |
if( !acf_get_setting('json') ) return; |
| 73 |
|
| 74 |
|
| 75 |
// WP appends '__trashed' to end of 'key' (post_name) |
| 76 |
$field_group['key'] = str_replace('__trashed', '', $field_group['key']); |
| 77 |
|
| 78 |
|
| 79 |
// delete |
| 80 |
acf_delete_json_field_group( $field_group['key'] ); |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
/* |
| 86 |
* include_json_folders |
| 87 |
* |
| 88 |
* This function will include all registered .json files |
| 89 |
* |
| 90 |
* @type function |
| 91 |
* @date 10/03/2014 |
| 92 |
* @since 5.0.0 |
| 93 |
* |
| 94 |
* @param n/a |
| 95 |
* @return n/a |
| 96 |
*/ |
| 97 |
|
| 98 |
function include_json_folders() { |
| 99 |
|
| 100 |
// validate |
| 101 |
if( !acf_get_setting('json') ) return; |
| 102 |
|
| 103 |
|
| 104 |
// vars |
| 105 |
$paths = acf_get_setting('load_json'); |
| 106 |
|
| 107 |
|
| 108 |
// loop through and add to cache |
| 109 |
foreach( $paths as $path ) { |
| 110 |
|
| 111 |
$this->include_json_folder( $path ); |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
/* |
| 119 |
* include_json_folder |
| 120 |
* |
| 121 |
* This function will include all .json files within a folder |
| 122 |
* |
| 123 |
* @type function |
| 124 |
* @date 1/5/17 |
| 125 |
* @since 5.5.13 |
| 126 |
* |
| 127 |
* @param n/a |
| 128 |
* @return n/a |
| 129 |
*/ |
| 130 |
|
| 131 |
function include_json_folder( $path = '' ) { |
| 132 |
|
| 133 |
// remove trailing slash |
| 134 |
$path = untrailingslashit( $path ); |
| 135 |
|
| 136 |
|
| 137 |
// bail early if path does not exist |
| 138 |
if( !is_dir($path) ) return false; |
| 139 |
|
| 140 |
|
| 141 |
// open |
| 142 |
$dir = opendir( $path ); |
| 143 |
|
| 144 |
// bail early if not valid |
| 145 |
if( !$dir ) return false; |
| 146 |
|
| 147 |
// loop over files |
| 148 |
while(false !== ( $file = readdir($dir)) ) { |
| 149 |
|
| 150 |
// validate type |
| 151 |
if( pathinfo($file, PATHINFO_EXTENSION) !== 'json' ) continue; |
| 152 |
|
| 153 |
|
| 154 |
// read json |
| 155 |
$json = file_get_contents("{$path}/{$file}"); |
| 156 |
|
| 157 |
|
| 158 |
// validate json |
| 159 |
if( empty($json) ) continue; |
| 160 |
|
| 161 |
|
| 162 |
// decode |
| 163 |
$json = json_decode($json, true); |
| 164 |
|
| 165 |
|
| 166 |
// add local |
| 167 |
$json['local'] = 'json'; |
| 168 |
|
| 169 |
|
| 170 |
// add field group |
| 171 |
acf_add_local_field_group( $json ); |
| 172 |
|
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
// return |
| 177 |
return true; |
| 178 |
|
| 179 |
} |
| 180 |
|
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
// initialize |
| 185 |
acf()->json = new acf_json(); |
| 186 |
|
| 187 |
endif; // class_exists check |
| 188 |
|
| 189 |
|
| 190 |
/* |
| 191 |
* acf_write_json_field_group |
| 192 |
* |
| 193 |
* This function will save a field group to a json file within the current theme |
| 194 |
* |
| 195 |
* @type function |
| 196 |
* @date 5/12/2014 |
| 197 |
* @since 5.1.5 |
| 198 |
* |
| 199 |
* @param $field_group (array) |
| 200 |
* @return (boolean) |
| 201 |
*/ |
| 202 |
|
| 203 |
function acf_write_json_field_group( $field_group ) { |
| 204 |
|
| 205 |
// vars |
| 206 |
$path = acf_get_setting('save_json'); |
| 207 |
$file = $field_group['key'] . '.json'; |
| 208 |
|
| 209 |
|
| 210 |
// remove trailing slash |
| 211 |
$path = untrailingslashit( $path ); |
| 212 |
|
| 213 |
|
| 214 |
// bail early if dir does not exist |
| 215 |
if( !is_writable($path) ) return false; |
| 216 |
|
| 217 |
|
| 218 |
// prepare for export |
| 219 |
$id = acf_extract_var( $field_group, 'ID' ); |
| 220 |
$field_group = acf_prepare_field_group_for_export( $field_group ); |
| 221 |
|
| 222 |
|
| 223 |
// add modified time |
| 224 |
$field_group['modified'] = get_post_modified_time('U', true, $id, true); |
| 225 |
|
| 226 |
|
| 227 |
// write file |
| 228 |
$f = fopen("{$path}/{$file}", 'w'); |
| 229 |
fwrite($f, acf_json_encode( $field_group )); |
| 230 |
fclose($f); |
| 231 |
|
| 232 |
|
| 233 |
// return |
| 234 |
return true; |
| 235 |
|
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
/* |
| 240 |
* acf_delete_json_field_group |
| 241 |
* |
| 242 |
* This function will delete a json field group file |
| 243 |
* |
| 244 |
* @type function |
| 245 |
* @date 5/12/2014 |
| 246 |
* @since 5.1.5 |
| 247 |
* |
| 248 |
* @param $key (string) |
| 249 |
* @return (boolean) |
| 250 |
*/ |
| 251 |
|
| 252 |
function acf_delete_json_field_group( $key ) { |
| 253 |
|
| 254 |
// vars |
| 255 |
$path = acf_get_setting('save_json'); |
| 256 |
$file = $key . '.json'; |
| 257 |
|
| 258 |
|
| 259 |
// remove trailing slash |
| 260 |
$path = untrailingslashit( $path ); |
| 261 |
|
| 262 |
|
| 263 |
// bail early if file does not exist |
| 264 |
if( !is_readable("{$path}/{$file}") ) { |
| 265 |
|
| 266 |
return false; |
| 267 |
|
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
// remove file |
| 272 |
unlink("{$path}/{$file}"); |
| 273 |
|
| 274 |
|
| 275 |
// return |
| 276 |
return true; |
| 277 |
|
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
?> |