AutoSync.php
1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
namespace WPML\TaxonomyTermTranslation;
use WPML\Element\API\Languages;
use WPML\FP\Fns;
use WPML\FP\Maybe;
use WPML\FP\Obj;
use WPML\FP\Relation;
use WPML\FP\Str;
use WPML\LIB\WP\Hooks;
use WPML\Settings\PostType\Automatic;
use WPML\Setup\Option;
use function WPML\FP\pipe;
use function WPML\FP\spreadArgs;
class AutoSync implements \IWPML_Backend_Action, \IWPML_REST_Action, \IWPML_AJAX_Action {
public function add_hooks() {
if ( Option::shouldTranslateEverything() ) {
Hooks::onAction( 'wpml_pro_translation_completed', 10, 3 )
->then( spreadArgs( self::syncTaxonomyHierarchy() ) );
remove_action( 'save_post', 'display_tax_sync_message' );
}
}
/**
* @return \Closure (int, array, object) -> void
*/
private static function syncTaxonomyHierarchy() {
return function( $newPostId, $fields, $job ) {
// $isPostJob :: object -> bool
$isPostJob = Relation::propEq( 'element_type_prefix', 'post' );
// $getPostType :: object -> string
$getPostType = pipe(
Obj::prop( 'original_post_type' ),
Str::replace( 'post_', '' )
);
// $isAutomaticPostType :: string -> bool
$isAutomaticPostType = [ Automatic::class, 'isAutomatic' ];
// $isTranslatableTax :: string -> bool
$isTranslatableTax = function( $taxonomy ) {
return \WPML_Element_Sync_Settings_Factory::createTax()->is_sync( $taxonomy );
};
// $syncTaxonomyHierarchies :: array -> void
$syncTaxonomyHierarchies = function( $taxonomies ) {
wpml_get_hierarchy_sync_helper( 'term' )->sync_element_hierarchy( $taxonomies, Languages::getDefaultCode() );
};
Maybe::of( $job )
->filter( $isPostJob )
->map( $getPostType )
->filter( $isAutomaticPostType )
->map( 'get_object_taxonomies' )
->map( Fns::filter( $isTranslatableTax ) )
->map( $syncTaxonomyHierarchies );
};
}
}