local-json.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. if ( ! class_exists( 'ACF_Local_JSON' ) ) :
  6. class ACF_Local_JSON {
  7. /**
  8. * The found JSON field group files.
  9. *
  10. * @since 5.9.0
  11. * @var array
  12. */
  13. private $files = array();
  14. /**
  15. * Constructor.
  16. *
  17. * @date 14/4/20
  18. * @since 5.9.0
  19. *
  20. * @param void
  21. * @return void
  22. */
  23. public function __construct() {
  24. // Update settings.
  25. acf_update_setting( 'save_json', get_stylesheet_directory() . '/acf-json' );
  26. acf_append_setting( 'load_json', get_stylesheet_directory() . '/acf-json' );
  27. // Add listeners.
  28. add_action( 'acf/update_field_group', array( $this, 'update_field_group' ) );
  29. add_action( 'acf/untrash_field_group', array( $this, 'update_field_group' ) );
  30. add_action( 'acf/trash_field_group', array( $this, 'delete_field_group' ) );
  31. add_action( 'acf/delete_field_group', array( $this, 'delete_field_group' ) );
  32. // Include fields.
  33. add_action( 'acf/include_fields', array( $this, 'include_fields' ) );
  34. }
  35. /**
  36. * Returns true if this component is enabled.
  37. *
  38. * @date 14/4/20
  39. * @since 5.9.0
  40. *
  41. * @param void
  42. * @return bool.
  43. */
  44. public function is_enabled() {
  45. return (bool) acf_get_setting( 'json' );
  46. }
  47. /**
  48. * Writes field group data to JSON file.
  49. *
  50. * @date 14/4/20
  51. * @since 5.9.0
  52. *
  53. * @param array $field_group The field group.
  54. * @return void
  55. */
  56. public function update_field_group( $field_group ) {
  57. // Bail early if disabled.
  58. if ( ! $this->is_enabled() ) {
  59. return false;
  60. }
  61. // Append fields.
  62. $field_group['fields'] = acf_get_fields( $field_group );
  63. // Save to file.
  64. $this->save_file( $field_group['key'], $field_group );
  65. }
  66. /**
  67. * Deletes a field group JSON file.
  68. *
  69. * @date 14/4/20
  70. * @since 5.9.0
  71. *
  72. * @param array $field_group The field group.
  73. * @return void
  74. */
  75. public function delete_field_group( $field_group ) {
  76. // Bail early if disabled.
  77. if ( ! $this->is_enabled() ) {
  78. return false;
  79. }
  80. // WP appends '__trashed' to end of 'key' (post_name).
  81. $key = str_replace( '__trashed', '', $field_group['key'] );
  82. // Delete file.
  83. $this->delete_file( $key );
  84. }
  85. /**
  86. * Includes all local JSON fields.
  87. *
  88. * @date 14/4/20
  89. * @since 5.9.0
  90. *
  91. * @param void
  92. * @return void
  93. */
  94. public function include_fields() {
  95. // Bail early if disabled.
  96. if ( ! $this->is_enabled() ) {
  97. return false;
  98. }
  99. // Get load paths.
  100. $files = $this->scan_field_groups();
  101. foreach ( $files as $key => $file ) {
  102. $json = json_decode( file_get_contents( $file ), true );
  103. $json['local'] = 'json';
  104. $json['local_file'] = $file;
  105. acf_add_local_field_group( $json );
  106. }
  107. }
  108. /**
  109. * Scans for JSON field groups.
  110. *
  111. * @date 14/4/20
  112. * @since 5.9.0
  113. *
  114. * @param void
  115. * @return array
  116. */
  117. function scan_field_groups() {
  118. $json_files = array();
  119. // Loop over "local_json" paths and parse JSON files.
  120. $paths = (array) acf_get_setting( 'load_json' );
  121. foreach ( $paths as $path ) {
  122. if ( is_dir( $path ) ) {
  123. $files = scandir( $path );
  124. if ( $files ) {
  125. foreach ( $files as $filename ) {
  126. // Ignore hidden files.
  127. if ( $filename[0] === '.' ) {
  128. continue;
  129. }
  130. // Ignore sub directories.
  131. $file = untrailingslashit( $path ) . '/' . $filename;
  132. if ( is_dir( $file ) ) {
  133. continue;
  134. }
  135. // Ignore non JSON files.
  136. $ext = pathinfo( $filename, PATHINFO_EXTENSION );
  137. if ( $ext !== 'json' ) {
  138. continue;
  139. }
  140. // Read JSON data.
  141. $json = json_decode( file_get_contents( $file ), true );
  142. if ( ! is_array( $json ) || ! isset( $json['key'] ) ) {
  143. continue;
  144. }
  145. // Append data.
  146. $json_files[ $json['key'] ] = $file;
  147. }
  148. }
  149. }
  150. }
  151. // Store data and return.
  152. $this->files = $json_files;
  153. return $json_files;
  154. }
  155. /**
  156. * Returns an array of found JSON field group files.
  157. *
  158. * @date 14/4/20
  159. * @since 5.9.0
  160. *
  161. * @param void
  162. * @return array
  163. */
  164. public function get_files() {
  165. return $this->files;
  166. }
  167. /**
  168. * Saves a field group JSON file.
  169. *
  170. * @date 17/4/20
  171. * @since 5.9.0
  172. *
  173. * @param string $key The field group key.
  174. * @param array $field_group The field group.
  175. * @return bool
  176. */
  177. public function save_file( $key, $field_group ) {
  178. $path = acf_get_setting( 'save_json' );
  179. $file = untrailingslashit( $path ) . '/' . $key . '.json';
  180. if ( ! is_writable( $path ) ) {
  181. return false;
  182. }
  183. // Append modified time.
  184. if ( $field_group['ID'] ) {
  185. $field_group['modified'] = get_post_modified_time( 'U', true, $field_group['ID'] );
  186. } else {
  187. $field_group['modified'] = strtotime( 'now' );
  188. }
  189. // Prepare for export.
  190. $field_group = acf_prepare_field_group_for_export( $field_group );
  191. // Save and return true if bytes were written.
  192. $result = file_put_contents( $file, acf_json_encode( $field_group ) );
  193. return is_int( $result );
  194. }
  195. /**
  196. * Deletes a field group JSON file.
  197. *
  198. * @date 17/4/20
  199. * @since 5.9.0
  200. *
  201. * @param string $key The field group key.
  202. * @return bool True on success.
  203. */
  204. public function delete_file( $key ) {
  205. $path = acf_get_setting( 'save_json' );
  206. $file = untrailingslashit( $path ) . '/' . $key . '.json';
  207. if ( is_readable( $file ) ) {
  208. unlink( $file );
  209. return true;
  210. }
  211. return false;
  212. }
  213. /**
  214. * Includes all local JSON files.
  215. *
  216. * @date 10/03/2014
  217. * @since 5.0.0
  218. * @deprecated 5.9.0
  219. *
  220. * @param void
  221. * @return void
  222. */
  223. public function include_json_folders() {
  224. _deprecated_function( __METHOD__, '5.9.0', 'ACF_Local_JSON::include_fields()' );
  225. $this->include_fields();
  226. }
  227. /**
  228. * Includes local JSON files within a specific folder.
  229. *
  230. * @date 01/05/2017
  231. * @since 5.5.13
  232. * @deprecated 5.9.0
  233. *
  234. * @param string $path The path to a specific JSON folder.
  235. * @return void
  236. */
  237. public function include_json_folder( $path = '' ) {
  238. _deprecated_function( __METHOD__, '5.9.0' );
  239. // Do nothing.
  240. }
  241. }
  242. // Initialize.
  243. acf_new_instance( 'ACF_Local_JSON' );
  244. endif; // class_exists check
  245. /**
  246. * Returns an array of found JSON field group files.
  247. *
  248. * @date 14/4/20
  249. * @since 5.9.0
  250. *
  251. * @param type $var Description. Default.
  252. * @return type Description.
  253. */
  254. function acf_get_local_json_files() {
  255. return acf_get_instance( 'ACF_Local_JSON' )->get_files();
  256. }
  257. /**
  258. * Saves a field group JSON file.
  259. *
  260. * @date 5/12/2014
  261. * @since 5.1.5
  262. *
  263. * @param array $field_group The field group.
  264. * @return bool
  265. */
  266. function acf_write_json_field_group( $field_group ) {
  267. return acf_get_instance( 'ACF_Local_JSON' )->save_file( $field_group['key'], $field_group );
  268. }
  269. /**
  270. * Deletes a field group JSON file.
  271. *
  272. * @date 5/12/2014
  273. * @since 5.1.5
  274. *
  275. * @param string $key The field group key.
  276. * @return bool True on success.
  277. */
  278. function acf_delete_json_field_group( $key ) {
  279. return acf_get_instance( 'ACF_Local_JSON' )->delete_file( $key );
  280. }