@@ -17,9 +17,78 @@ public function register() {
17
17
if ( Options::only_one_language_allowed () ) {
18
18
return ;
19
19
}
20
- \add_action ( 'saved_term ' , [ $ this , 'create_and_redirect ' ], PHP_INT_MAX , 4 );
20
+ // Redirect to create new translation.
21
+ \add_action ( 'saved_term ' , [ $ this , 'redirect_to_new ' ], PHP_INT_MAX , 4 );
22
+ \add_action ( 'saved_term ' , [ $ this , 'set_new_source ' ], PHP_INT_MAX , 4 );
23
+
24
+ // TODO: Refactor for copy.
25
+ // \add_action( 'saved_term', [ $this, 'create_and_redirect' ], PHP_INT_MAX, 4 );
21
26
}
22
27
28
+ public function redirect_to_new ( int $ term_id , int $ tt_id , string $ taxonomy , bool $ update ) : void {
29
+ if (
30
+ ! $ update
31
+ || ! in_array ( $ taxonomy , Options::get_allowed_taxonomies (), true )
32
+ || ! ( $ _POST ['ubb_redirect_new ' ] ?? false )
33
+ ) {
34
+ return ;
35
+ }
36
+
37
+ // Language to set to the new term.
38
+ $ lang_create = $ _POST ['ubb_create ' ] ?? '' ;
39
+ if (
40
+ empty ( $ lang_create )
41
+ || ! in_array ( $ lang_create , Options::get ()['allowed_languages ' ] )
42
+ // TODO: check if term_id has this language already
43
+ ) {
44
+ // TODO: What else to do when this happens.
45
+ error_log ( print_r ( 'CreateTranslation - lang create failed ' , true ) );
46
+ return ;
47
+ }
48
+
49
+ // TODO: Add something in the page to show that a translation is being saved. Use existence of ubb_source.
50
+ wp_safe_redirect (
51
+ add_query_arg (
52
+ [
53
+ 'taxonomy ' => $ taxonomy ,
54
+ 'lang ' => $ lang_create ,
55
+ 'ubb_source ' => $ term_id ,
56
+ ],
57
+ admin_url ( 'edit-tags.php ' )
58
+ ),
59
+ 302 ,
60
+ 'Unbabble '
61
+ );
62
+ exit ;
63
+ }
64
+
65
+ public function set_new_source ( int $ term_id , int $ tt_id , string $ taxonomy , bool $ update ) : void {
66
+ $ allowed_taxonomies = Options::get_allowed_taxonomies ();
67
+ if (
68
+ $ update
69
+ || ! in_array ( $ taxonomy , $ allowed_taxonomies , true )
70
+ || ! isset ( $ _POST ['ubb_source ' ] )
71
+ || ! is_numeric ( $ _POST ['ubb_source ' ] )
72
+ ) {
73
+ return ;
74
+ }
75
+
76
+ $ src_term = get_term ( \sanitize_text_field ( $ _POST ['ubb_source ' ] ), $ taxonomy );
77
+ if ( $ src_term === null || ! in_array ( $ src_term ->taxonomy , $ allowed_taxonomies , true ) ) {
78
+ return ;
79
+ }
80
+
81
+ $ original_source = LangInterface::get_term_source ( $ src_term ->term_id );
82
+ if ( $ original_source === null ) {
83
+ $ original_source = $ src_term ->term_id ;
84
+ LangInterface::set_term_source ( $ src_term ->term_id , $ src_term ->term_id );
85
+ }
86
+
87
+ LangInterface::set_term_source ( $ term_id , $ original_source );
88
+ }
89
+
90
+
91
+ // TODO: Refactor for copy.
23
92
public function create_and_redirect ( int $ term_id , int $ tt_id , string $ taxonomy , bool $ update ) : void {
24
93
if ( ! $ update ) {
25
94
return ;
0 commit comments