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