class-acf-ajax-check-screen.php
2 months ago
class-acf-ajax-local-json-diff.php
1 year ago
class-acf-ajax-query-users.php
4 months ago
class-acf-ajax-query.php
5 months ago
class-acf-ajax-upgrade.php
2 months ago
class-acf-ajax-user-setting.php
1 year ago
class-acf-ajax.php
1 year ago
index.php
1 year ago
class-acf-ajax-local-json-diff.php
106 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; // Exit if accessed directly |
| 5 | } |
| 6 | |
| 7 | if ( ! class_exists( 'ACF_Ajax_Local_JSON_Diff' ) ) : |
| 8 | |
| 9 | class ACF_Ajax_Local_JSON_Diff extends ACF_Ajax { |
| 10 | |
| 11 | |
| 12 | /** |
| 13 | * The AJAX action name. |
| 14 | * |
| 15 | * @var string |
| 16 | */ |
| 17 | public $action = 'acf/ajax/local_json_diff'; |
| 18 | |
| 19 | /** |
| 20 | * Prevents access for non-logged in users. |
| 21 | * |
| 22 | * @var boolean |
| 23 | */ |
| 24 | public $public = false; |
| 25 | |
| 26 | /** |
| 27 | * Returns the response data to sent back. |
| 28 | * |
| 29 | * @date 31/7/18 |
| 30 | * @since ACF 5.7.2 |
| 31 | * |
| 32 | * @param array $request The request args. |
| 33 | * @return array|WP_Error The response data or WP_Error. |
| 34 | */ |
| 35 | public function get_response( $request ) { |
| 36 | // Bail early if the current user can't access the ACF admin. |
| 37 | if ( ! acf_current_user_can_admin() ) { |
| 38 | return new WP_Error( 'acf_not_allowed', __( 'Sorry, you do not have permission to do that.', 'secure-custom-fields' ), array( 'status' => 403 ) ); |
| 39 | } |
| 40 | |
| 41 | $json = array(); |
| 42 | |
| 43 | // Extract props. |
| 44 | $id = isset( $request['id'] ) ? intval( $request['id'] ) : 0; |
| 45 | |
| 46 | // Bail early if missing props. |
| 47 | if ( ! $id ) { |
| 48 | return new WP_Error( 'acf_invalid_param', __( 'Invalid field group parameter(s).', 'secure-custom-fields' ), array( 'status' => 404 ) ); |
| 49 | } |
| 50 | |
| 51 | $post_type = get_post_type( $id ); |
| 52 | if ( ! in_array( $post_type, acf_get_internal_post_types(), true ) ) { |
| 53 | return new WP_Error( 'acf_invalid_post_type', __( 'Invalid post type selected for review.', 'secure-custom-fields' ), array( 'status' => 404 ) ); |
| 54 | } |
| 55 | |
| 56 | // Disable filters and load the post directly from database. |
| 57 | acf_disable_filters(); |
| 58 | |
| 59 | $post = acf_get_internal_post_type( $id, $post_type ); |
| 60 | if ( ! $post ) { |
| 61 | return new WP_Error( 'acf_invalid_id', __( 'Invalid post ID.', 'secure-custom-fields' ), array( 'status' => 404 ) ); |
| 62 | } |
| 63 | |
| 64 | // Field groups also load in fields. |
| 65 | if ( 'acf-field-group' === $post_type ) { |
| 66 | $post['fields'] = acf_get_fields( $post ); |
| 67 | } |
| 68 | |
| 69 | $post['modified'] = get_post_modified_time( 'U', true, $post['ID'] ); |
| 70 | $post = acf_prepare_internal_post_type_for_export( $post, $post_type ); |
| 71 | |
| 72 | // Load local field group file. |
| 73 | $files = acf_get_local_json_files( $post_type ); |
| 74 | $key = $post['key']; |
| 75 | if ( ! isset( $files[ $key ] ) ) { |
| 76 | return new WP_Error( 'acf_cannot_compare', __( 'Sorry, this post is unavailable for diff comparison.', 'secure-custom-fields' ), array( 'status' => 404 ) ); |
| 77 | } |
| 78 | $local_post = json_decode( file_get_contents( $files[ $key ] ), true ); |
| 79 | |
| 80 | // Render diff HTML. |
| 81 | $date_format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' ); |
| 82 | /* translators: %s: Date/time. */ |
| 83 | $date_template = __( 'Last updated: %s', 'secure-custom-fields' ); |
| 84 | $json['html'] = ' |
| 85 | <div class="acf-diff"> |
| 86 | <div class="acf-diff-title"> |
| 87 | <div class="acf-diff-title-left"> |
| 88 | <strong>' . __( 'Original', 'secure-custom-fields' ) . '</strong> |
| 89 | <span>' . sprintf( $date_template, wp_date( $date_format, $post['modified'] ) ) . '</span> |
| 90 | </div> |
| 91 | <div class="acf-diff-title-right"> |
| 92 | <strong>' . __( 'JSON (newer)', 'secure-custom-fields' ) . '</strong> |
| 93 | <span>' . sprintf( $date_template, wp_date( $date_format, $local_post['modified'] ) ) . '</span> |
| 94 | </div> |
| 95 | </div> |
| 96 | <div class="acf-diff-content"> |
| 97 | ' . wp_text_diff( acf_json_encode( $post ), acf_json_encode( $local_post ) ) . ' |
| 98 | </div> |
| 99 | </div>'; |
| 100 | return $json; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | acf_new_instance( 'ACF_Ajax_Local_JSON_Diff' ); |
| 105 | endif; // class_exists check |
| 106 |