Skip to content

Commit c90ac70

Browse files
authored
Merge pull request #129 from BeAPI/feature/acf-block
Add translate acf block for the sync
2 parents 5328bee + 133c0ad commit c90ac70

File tree

2 files changed

+107
-1
lines changed

2 files changed

+107
-1
lines changed

classes/addons/advanced-custom-fields.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Manage dynamic ACF fields
45
*
@@ -19,6 +20,7 @@ public function __construct() {
1920
add_action( 'bea_csf.client.attachment.merge', array( __CLASS__, 'bea_csf_client_posttype_merge' ), 10, 3 );
2021

2122
add_action( 'bea_csf.client.taxonomy.merge', array( __CLASS__, 'bea_csf_client_taxonomy_merge' ), 10, 3 );
23+
add_filter( 'bea_csf_gutenberg_translate_block_attributes', array( __CLASS__, 'translate_acf_blocks' ), 10, 4 );
2224

2325
return true;
2426
}
@@ -216,4 +218,108 @@ public static function prepare_acf_fields( $fields ) {
216218
}
217219
}
218220

221+
/**
222+
* Translate attributs for a acf block.
223+
*
224+
* @param array $attributes current block's attributs.
225+
* @param string $block_name current block's name.
226+
* @param int $emitter_blog_id ID of the emitter site.
227+
* @param int $receiver_blog_id ID of the receiver site.
228+
*
229+
* @return array translated attributs.
230+
* @author Egidio CORICA
231+
*/
232+
public static function translate_acf_blocks( array $attributes, string $block_name, int $emitter_blog_id, int $receiver_blog_id ): array {
233+
// Skip if it's not an acf block
234+
if ( false === strpos( $block_name, 'acf' ) ) {
235+
return $attributes;
236+
}
237+
238+
foreach ( $attributes['data'] as $field_name => $field_value ) {
239+
if ( 0 === strpos( $field_name, '_' ) ) {
240+
continue;
241+
}
242+
243+
$field_object = acf_get_field( $field_name );
244+
$type_relation = self::get_type_relation( $field_object );
245+
246+
if ( empty( $type_relation ) ) {
247+
continue;
248+
}
249+
250+
// Multiple values
251+
if ( is_array( $field_value ) ) {
252+
$translate_fields = [];
253+
254+
foreach ( $field_value as $value ) {
255+
if ( ! is_numeric( $value ) ) {
256+
$translate_fields[] = $value;
257+
continue;
258+
}
259+
260+
$translate_fields[] = (string) self::translate_field( (int) $value, $emitter_blog_id, $receiver_blog_id, $type_relation );
261+
}
262+
263+
$attributes['data'][ $field_name ] = $translate_fields;
264+
265+
continue;
266+
}
267+
268+
// Single value
269+
if ( is_numeric( $field_value ) ) {
270+
$attributes['data'][ $field_name ] = (string) self::translate_field( (int) $field_value, $emitter_blog_id, $receiver_blog_id, $type_relation );
271+
}
272+
}
273+
274+
return $attributes;
275+
}
276+
277+
278+
/**
279+
* Translate field if a relationship exists
280+
*
281+
* @param int $value
282+
* @param int $emitter_blog_id
283+
* @param int $receiver_blog_id
284+
* @param string $type_relation
285+
*
286+
* @return int
287+
* @author Egidio CORICA
288+
*/
289+
public static function translate_field( int $value, int $emitter_blog_id, int $receiver_blog_id, string $type_relation ): int {
290+
$local_id = BEA_CSF_Relations::get_object_for_any(
291+
$type_relation,
292+
$emitter_blog_id,
293+
$receiver_blog_id,
294+
$value,
295+
$value
296+
);
297+
298+
return ! empty( $local_id ) ? $local_id : $value;
299+
}
300+
301+
/**
302+
* Get type relation by acf field
303+
*
304+
* @param $field_object
305+
*
306+
* @return string
307+
* @author Egidio CORICA
308+
*/
309+
public static function get_type_relation( $field_object ): string {
310+
$types_relation = [
311+
'posttype' => apply_filters( 'bea_csf_addon_acf_match_fields_posttype', [ 'post_object', 'relationship', 'page_link' ], $field_object ),
312+
'attachment' => apply_filters( 'bea_csf_addon_acf_match_fields_attachment', [ 'image', 'gallery', 'file' ], $field_object ),
313+
'taxonomy' => apply_filters( 'bea_csf_addon_acf_match_fields_taxonomy', [ 'taxonomy' ], $field_object ),
314+
];
315+
316+
foreach ( $types_relation as $type_relation_name => $field_type ) {
317+
if ( in_array( $field_object['type'], $field_type, true ) ) {
318+
return $type_relation_name;
319+
}
320+
}
321+
322+
return '';
323+
}
324+
219325
}

classes/addons/gutenberg.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ private function translate_block_attributes( array $attributes, string $block_na
146146
break;
147147
}
148148

149-
return $attributes;
149+
return apply_filters( 'bea_csf_gutenberg_translate_block_attributes', $attributes, $block_name, $emitter_blog_id, $receiver_blog_id );
150150
}
151151

152152
/**

0 commit comments

Comments
 (0)