PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / 6.4.0-beta2
Secure Custom Fields v6.4.0-beta2
6.9.5 6.9.4 6.9.3 6.9.2 6.9.1 6.9.0 6.8.9 6.8.7 6.8.8 6.8.6 6.8.4 6.8.5 trunk 6.4.0-beta1 6.4.0-beta2 6.4.1 6.4.1-beta3 6.4.1-beta4 6.4.1-beta5 6.4.1-beta6 6.4.1-beta7 6.4.2 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.5.5 6.5.6 6.5.7 6.6.0 6.7.0 6.7.1 6.8.0 6.8.1 6.8.2 6.8.3
secure-custom-fields / includes / acf-bidirectional-functions.php
secure-custom-fields / includes Last commit date
Blocks 1 year ago admin 1 year ago ajax 1 year ago api 1 year ago fields 1 year ago forms 1 year ago legacy 1 year ago locations 1 year ago post-types 1 year ago rest-api 1 year ago walkers 1 year ago acf-bidirectional-functions.php 1 year ago acf-field-functions.php 1 year ago acf-field-group-functions.php 1 year ago acf-form-functions.php 1 year ago acf-helper-functions.php 1 year ago acf-hook-functions.php 1 year ago acf-input-functions.php 1 year ago acf-internal-post-type-functions.php 1 year ago acf-meta-functions.php 1 year ago acf-post-functions.php 1 year ago acf-post-type-functions.php 1 year ago acf-taxonomy-functions.php 1 year ago acf-user-functions.php 1 year ago acf-utility-functions.php 1 year ago acf-value-functions.php 1 year ago acf-wp-functions.php 1 year ago assets.php 1 year ago class-acf-data.php 1 year ago class-acf-internal-post-type.php 1 year ago class-acf-site-health.php 1 year ago compatibility.php 1 year ago deprecated.php 1 year ago fields.php 1 year ago index.php 1 year ago l10n.php 1 year ago local-fields.php 1 year ago local-json.php 1 year ago local-meta.php 1 year ago locations.php 1 year ago loop.php 1 year ago media.php 1 year ago rest-api.php 1 year ago revisions.php 1 year ago third-party.php 1 year ago upgrades.php 1 year ago validation.php 1 year ago wpml.php 1 year ago
acf-bidirectional-functions.php
275 lines
1 <?php
2 /**
3 * General functions relating to the bidirectional feature of some fields.
4 *
5 * @package ACF
6 */
7
8 /**
9 * Process updating bidirectional fields.
10 *
11 * @since 6.2
12 *
13 * @param array $target_item_ids The post, user or term IDs which should be updated with the origin item ID.
14 * @param integer|string $post_id The ACF encoded origin post, user or term ID.
15 * @param array $field The field being updated on the origin post, user or term ID.
16 * @param string|false $target_prefix The ACF prefix for a post, user or term ID required for the update_field call for this field type.
17 */
18 function acf_update_bidirectional_values( $target_item_ids, $post_id, $field, $target_prefix = false ) {
19
20 // Bail early if we're already updating a bidirectional relationship to prevent recursion.
21 if ( acf_get_data( 'acf_doing_bidirectional_update' ) ) {
22 return;
23 }
24
25 // Support disabling bidirectionality globally.
26 if ( ! acf_get_setting( 'enable_bidirection' ) ) {
27 return;
28 }
29
30 if ( empty( $field['bidirectional'] ) || empty( $field['bidirectional_target'] ) ) {
31 return;
32 }
33
34 $decoded = acf_decode_post_id( $post_id );
35 $item_id = $decoded['id'];
36 $valid_target_types = acf_get_valid_bidirectional_target_types( $decoded['type'] );
37 $valid_targets = array();
38
39 foreach ( $field['bidirectional_target'] as $target_field ) {
40 $target_field_object = get_field_object( $target_field );
41 if ( empty( $target_field_object ) || ! is_array( $target_field_object ) ) {
42 continue;
43 }
44 if ( in_array( $target_field_object['type'], $valid_target_types, true ) ) {
45 $valid_targets[] = $target_field;
46 }
47 }
48
49 if ( ! empty( $valid_targets ) ) {
50
51 // Get current values for this field.
52 $current_values = array_filter( acf_get_array( get_field( $field['key'], $post_id, false ) ) );
53 $new_values = array_filter( acf_get_array( $target_item_ids ) );
54
55 $additions = array_diff( $new_values, $current_values );
56 $subtractions = array_diff( $current_values, $new_values );
57
58 // Prefix additions and subtractions for destinations which aren't posts.
59 if ( ! empty( $target_prefix ) ) {
60 $mapper = function ( $v ) use ( $target_prefix ) {
61 return $target_prefix . '_' . $v;
62 };
63 $additions = array_map( $mapper, $additions );
64 $subtractions = array_map( $mapper, $subtractions );
65 }
66
67 acf_set_data( 'acf_doing_bidirectional_update', true );
68
69 // Loop over each target, processing additions and removals.
70 foreach ( $valid_targets as $target_field ) {
71 foreach ( $additions as $addition ) {
72 $current_value = acf_get_array( get_field( $target_field, $addition, false ) );
73 update_field( $target_field, array_unique( array_merge( $current_value, array( $item_id ) ) ), $addition );
74 }
75
76 foreach ( $subtractions as $subtraction ) {
77 $current_value = acf_get_array( get_field( $target_field, $subtraction, false ) );
78 update_field( $target_field, array_unique( array_diff( $current_value, array( $item_id ) ) ), $subtraction );
79 }
80 }
81
82 acf_set_data( 'acf_doing_bidirectional_update', false );
83 }
84 }
85
86 /**
87 * Allows third party fields to enable support as a target field type for a particular object type
88 *
89 * @since 6.2
90 *
91 * @param string $object_type The object type that will be updated on the target field, such as 'term', 'user' or 'post'.
92 *
93 * @return array An array of valid field type names (slugs) for the target of the bidirectional field.
94 */
95 function acf_get_valid_bidirectional_target_types( $object_type ) {
96 $valid_target_types = array();
97 switch ( $object_type ) {
98 case 'term':
99 $valid_target_types = array( 'taxonomy' );
100 break;
101 case 'user':
102 $valid_target_types = array( 'user' );
103 break;
104 case 'post':
105 $valid_target_types = array( 'relationship', 'post_object' );
106 break;
107 }
108 return apply_filters( 'acf/bidirectional/supported_field_types_for_post', $valid_target_types, $object_type );
109 }
110
111 /**
112 * Build the complete choices argument for rendering the select2 field for bidirectional target based on the currently selected choices
113 *
114 * @since 6.2
115 *
116 * @param array $choices The currently selected choices (as an array of field keys).
117 *
118 * @return array
119 */
120 function acf_build_bidirectional_target_current_choices( $choices ) {
121 if ( empty( $choices ) ) {
122 return array();
123 }
124
125 $results = array();
126 foreach ( $choices as $choice ) {
127 if ( empty( $choice ) || ! is_string( $choice ) ) {
128 continue;
129 }
130
131 $field_object = get_field_object( $choice );
132 if ( is_array( $field_object ) && ! empty( $field_object['label'] ) ) {
133 $results[ $choice ] = $field_object['label'];
134 } else {
135 $results[ $choice ] = $choice;
136 }
137 }
138
139 return $results;
140 }
141
142 /**
143 * Build valid fields for a bidirectional relationship for select2 display
144 *
145 * @since 6.2
146 *
147 * @param array $results The original results array.
148 * @param array $options The options provided to the select2 AJAX search.
149 *
150 * @return array
151 */
152 function acf_build_bidirectional_relationship_field_target_args( $results, $options ) {
153 $valid_field_types = apply_filters( 'acf/bidirectional/supported_target_field_types', array( 'relationship', 'post_object', 'user', 'taxonomy' ) );
154
155 $field_groups = array_filter(
156 acf_get_field_groups(),
157 function ( $field_group ) {
158 return $field_group['active'];
159 }
160 );
161
162 $valid_fields = array();
163 foreach ( $field_groups as $field_group ) {
164 $fields = acf_get_fields( $field_group );
165 foreach ( $fields as $field ) {
166 if ( in_array( $field['type'], $valid_field_types, true ) ) {
167 if ( empty( $valid_fields[ $field_group['title'] ] ) ) {
168 $valid_fields[ $field_group['title'] ] = array();
169 }
170 $valid_fields[ $field_group['title'] ][ $field['key'] ] = array(
171 'type' => $field['type'],
172 'label' => $field['label'],
173 );
174
175 if ( isset( $options['parent_key'] ) && $options['parent_key'] === $field['key'] ) {
176 $valid_fields[ $field_group['title'] ][ $field['key'] ]['this_field'] = true;
177 }
178 }
179 }
180 }
181
182 foreach ( $valid_fields as $field_group_name => $fields ) {
183 $field_group = array(
184 'text' => $field_group_name,
185 'children' => array(),
186 );
187 foreach ( $fields as $key => $data ) {
188 $field_group['children'][] = array(
189 'id' => $key,
190 'text' => $data['label'],
191 'field_type' => $data['type'],
192 /* translators: %s A field type name, such as "Relationship" */
193 'human_field_type' => sprintf( __( '%s Field', 'acf' ), acf_get_field_type_prop( $data['type'], 'label' ) ),
194 'this_field' => ! empty( $data['this_field'] ),
195 );
196 }
197 $results['results'][] = $field_group;
198 }
199
200 return $results;
201 }
202
203 add_action( 'acf/fields/select/query/key=_acf_bidirectional_target', 'acf_build_bidirectional_relationship_field_target_args', 10, 2 );
204
205 /**
206 * Renders the field settings required for bidirectional fields
207 *
208 * @since 6.2
209 *
210 * @param array $field The field object passed into field setting functions.
211 */
212 function acf_render_bidirectional_field_settings( $field ) {
213 if ( ! acf_get_setting( 'enable_bidirection' ) ) {
214 return;
215 }
216
217 acf_render_field_setting(
218 $field,
219 array(
220 'label' => __( 'Bidirectional', 'acf' ),
221 'instructions' => __( 'Update a field on the selected values, referencing back to this ID', 'acf' ),
222 'type' => 'true_false',
223 'name' => 'bidirectional',
224 'ui' => 1,
225 )
226 );
227
228 acf_render_field_setting(
229 $field,
230 array(
231 'name' => 'bidirectional_notes',
232 'type' => 'message',
233 'message' => acf_get_bidirectional_field_settings_instruction_text(),
234 'conditions' => array(
235 'field' => 'bidirectional',
236 'operator' => '==',
237 'value' => 1,
238 ),
239 )
240 );
241
242 acf_render_field_setting(
243 $field,
244 array(
245 'type' => 'select',
246 'name' => 'bidirectional_target',
247 'label' => __( 'Target Field', 'acf' ),
248 'instructions' => __( 'Select field(s) to store the reference back to the item being updated. You may select this field. Target fields must be compatible with where this field is being displayed. For example, if this field is displayed on a Taxonomy, your target field should be of type Taxonomy', 'acf' ),
249 'class' => 'bidrectional_target',
250 'choices' => acf_build_bidirectional_target_current_choices( $field['bidirectional_target'] ),
251 'conditions' => array(
252 'field' => 'bidirectional',
253 'operator' => '==',
254 'value' => 1,
255 ),
256 'ui' => 1,
257 'multiple' => 1,
258 'ajax' => 1,
259 )
260 );
261 }
262
263 /**
264 * Returns the translated instructional text for the message field for the bidirectional field settings.
265 *
266 * @since 6.2
267 *
268 * @return string The html containing the instructional message.
269 */
270 function acf_get_bidirectional_field_settings_instruction_text() {
271 /* translators: %s the URL to ACF's bidirectional relationship documentation */
272 $message = '<p class="acf-feature-notice with-warning-icon">' . __( 'Enabling the bidirectional setting allows you to update a value in the target fields for each value selected for this field, adding or removing the Post ID, Taxonomy ID or User ID of the item being updated.', 'acf' ) . '</p>';
273 return $message;
274 }
275