class-acf-ajax-local-json-diff.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. if ( ! class_exists( 'ACF_Ajax_Local_JSON_Diff' ) ) :
  6. class ACF_Ajax_Local_JSON_Diff extends ACF_Ajax {
  7. /** @var string The AJAX action name. */
  8. var $action = 'acf/ajax/local_json_diff';
  9. /** @var bool Prevents access for non-logged in users. */
  10. var $public = false;
  11. /**
  12. * get_response
  13. *
  14. * Returns the response data to sent back.
  15. *
  16. * @date 31/7/18
  17. * @since 5.7.2
  18. *
  19. * @param array $request The request args.
  20. * @return mixed The response data or WP_Error.
  21. */
  22. function get_response( $request ) {
  23. $json = array();
  24. // Extract props.
  25. $id = isset( $request['id'] ) ? intval( $request['id'] ) : 0;
  26. // bail early if missing props.
  27. if ( ! $id ) {
  28. return new WP_Error( 'acf_invalid_param', __( 'Invalid field group parameter(s).', 'acf' ), array( 'status' => 404 ) );
  29. }
  30. // Disable filters and load field group directly from database.
  31. acf_disable_filters();
  32. $field_group = acf_get_field_group( $id );
  33. if ( ! $field_group ) {
  34. return new WP_Error( 'acf_invalid_id', __( 'Invalid field group ID.', 'acf' ), array( 'status' => 404 ) );
  35. }
  36. $field_group['fields'] = acf_get_fields( $field_group );
  37. $field_group['modified'] = get_post_modified_time( 'U', true, $field_group['ID'] );
  38. $field_group = acf_prepare_field_group_for_export( $field_group );
  39. // Load local field group file.
  40. $files = acf_get_local_json_files();
  41. $key = $field_group['key'];
  42. if ( ! isset( $files[ $key ] ) ) {
  43. return new WP_Error( 'acf_cannot_compare', __( 'Sorry, this field group is unavailable for diff comparison.', 'acf' ), array( 'status' => 404 ) );
  44. }
  45. $local_field_group = json_decode( file_get_contents( $files[ $key ] ), true );
  46. // Render diff HTML.
  47. $date_format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' );
  48. $date_template = __( 'Last updated: %s', 'acf' );
  49. $json['html'] = '
  50. <div class="acf-diff">
  51. <div class="acf-diff-title">
  52. <div class="acf-diff-title-left">
  53. <strong>' . __( 'Original field group', 'acf' ) . '</strong>
  54. <span>' . sprintf( $date_template, wp_date( $date_format, $field_group['modified'] ) ) . '</span>
  55. </div>
  56. <div class="acf-diff-title-right">
  57. <strong>' . __( 'JSON field group (newer)', 'acf' ) . '</strong>
  58. <span>' . sprintf( $date_template, wp_date( $date_format, $local_field_group['modified'] ) ) . '</span>
  59. </div>
  60. </div>
  61. <div class="acf-diff-content">
  62. ' . wp_text_diff( acf_json_encode( $field_group ), acf_json_encode( $local_field_group ) ) . '
  63. </div>
  64. </div>';
  65. return $json;
  66. }
  67. }
  68. acf_new_instance( 'ACF_Ajax_Local_JSON_Diff' );
  69. endif; // class_exists check