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