Check_Screen.php
1 month ago
Localization.php
1 month ago
REST_Save.php
1 month ago
Revisions.php
1 month ago
Check_Screen.php
89 lines
| 1 | <?php |
| 2 | /** |
| 3 | * SCF datastore integration. |
| 4 | * |
| 5 | * @package wordpress/secure-custom-fields |
| 6 | */ |
| 7 | |
| 8 | namespace SCF\Datastore; |
| 9 | |
| 10 | /** |
| 11 | * Attaches datastore field data to the acf/ajax/check_screen response. |
| 12 | * |
| 13 | * The check_screen AJAX endpoint runs when WordPress loads metaboxes |
| 14 | * dynamically (the meta-box-loader path). When new field groups appear |
| 15 | * on screen, the JS-side datastore needs to know about their fields and |
| 16 | * values. This class collects that data and merges it into the response |
| 17 | * as `storeData` for groups that were not already on the page. |
| 18 | */ |
| 19 | class Check_Screen { |
| 20 | |
| 21 | /** |
| 22 | * Constructor. |
| 23 | * |
| 24 | * @since ACF 6.8.1 |
| 25 | */ |
| 26 | public function __construct() { |
| 27 | add_filter( 'acf/ajax/check_screen/response', array( $this, 'attach_store_data' ), 10, 3 ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Attaches datastore data for newly-loaded field groups to the response. |
| 32 | * |
| 33 | * @since ACF 6.8.1 |
| 34 | * |
| 35 | * @param array $response The check_screen response array. |
| 36 | * @param array $field_groups The field groups returned for this screen. |
| 37 | * @param array $args The check_screen request args (post_id, screen, exists, ...). |
| 38 | * @return array |
| 39 | */ |
| 40 | public function attach_store_data( $response, $field_groups, $args ) { |
| 41 | if ( ! acf_is_using_datastore() ) { |
| 42 | return $response; |
| 43 | } |
| 44 | |
| 45 | $store_data = array( |
| 46 | 'context' => array( |
| 47 | 'postId' => (int) $args['post_id'], |
| 48 | 'postType' => get_post_type( (int) $args['post_id'] ) ? get_post_type( (int) $args['post_id'] ) : '', |
| 49 | ), |
| 50 | 'fields' => array(), |
| 51 | 'values' => array(), |
| 52 | 'fieldGroups' => array(), |
| 53 | ); |
| 54 | |
| 55 | $localization = acf_get_instance( 'SCF\\Datastore\\Localization' ); |
| 56 | $exists = isset( $args['exists'] ) ? (array) $args['exists'] : array(); |
| 57 | |
| 58 | foreach ( (array) $field_groups as $field_group ) { |
| 59 | // Only collect data for field groups not already on the page. |
| 60 | if ( in_array( $field_group['key'], $exists, true ) ) { |
| 61 | continue; |
| 62 | } |
| 63 | |
| 64 | $fields = acf_get_fields( $field_group ); |
| 65 | if ( ! $fields ) { |
| 66 | continue; |
| 67 | } |
| 68 | |
| 69 | $localization->collect_field_data( $fields, $args['post_id'], $field_group['key'], $store_data ); |
| 70 | |
| 71 | $store_data['fieldGroups'][] = array( |
| 72 | 'key' => $field_group['key'], |
| 73 | 'title' => acf_esc_html( acf_get_field_group_title( $field_group ) ), |
| 74 | 'position' => $field_group['position'], |
| 75 | 'style' => $field_group['style'], |
| 76 | 'label_placement' => $field_group['label_placement'], |
| 77 | 'instruction_placement' => $field_group['instruction_placement'], |
| 78 | 'edit_url' => esc_url( acf_get_field_group_edit_link( $field_group['ID'] ) ), |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | if ( ! empty( $store_data['fields'] ) ) { |
| 83 | $response['storeData'] = $store_data; |
| 84 | } |
| 85 | |
| 86 | return $response; |
| 87 | } |
| 88 | } |
| 89 |