Skip to content

Commit f332082

Browse files
#2 Redirect to create new for terms
1 parent de5ff1b commit f332082

File tree

2 files changed

+87
-7
lines changed

2 files changed

+87
-7
lines changed

lib/Terms/CreateTranslation.php

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,78 @@ public function register() {
1717
if ( Options::only_one_language_allowed() ) {
1818
return;
1919
}
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 );
2126
}
2227

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.
2392
public function create_and_redirect( int $term_id, int $tt_id, string $taxonomy, bool $update ) : void {
2493
if ( ! $update ) {
2594
return;

lib/Terms/LangMetaBox.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ public function new_term_language_metabox() {
5757
$this->print_language_select( 'ubb_lang', LangInterface::get_current_language(), $options['allowed_languages'], 'ubb_language_metabox_nonce', 'ubb_language_metabox', false ),
5858
esc_html__( 'The term only appears on the site for this language.', 'unbabble' )
5959
);
60+
61+
if ( is_numeric( $_GET['ubb_source'] ?? '' ) ) {
62+
printf(
63+
'<input type="hidden" id="ubb_source" name="ubb_source" value="%s">',
64+
esc_sql( $_GET['ubb_source'] )
65+
);
66+
}
6067
}
6168

6269
public function edit_term_language_metabox( $term ) {
@@ -147,7 +154,8 @@ public function edit_term_language_metabox( $term ) {
147154
<th scope="row"><label for="language">%1$s</label></th>
148155
<td>
149156
%2$s
150-
<input type="submit" name="ubb_save_create" value="Save and Create" class="button"/>
157+
<input type="submit" name="ubb_redirect_new" value="Save and Create" class="button"/>
158+
<input type="submit" name="ubb_copy_new" value="Save and Copy" class="button"/>
151159
</td>
152160
</tr>',
153161
esc_html__( 'Create Translation', 'unbabble' ),
@@ -161,7 +169,7 @@ public function save_term_language( $term_id ) {
161169
return;
162170
}
163171

164-
if ( isset( $_POST['ubb_save_create'] ) ) { // Don't set post language when creating a translation.
172+
if ( isset( $_POST['ubb_copy_new'] ) ) { // Don't set post language when creating a translation.
165173
return;
166174
}
167175

@@ -177,15 +185,18 @@ public function save_term_language( $term_id ) {
177185

178186
// TODO: Duplicated in Posts/LangMetaBox
179187
private function print_language_select( string $name, $selected, $options, string $nonce_action, string $nonce_name, $echo = true ) {
180-
$langs = array_map(
181-
function ( $text, $lang ) use ( $selected ) {
188+
$create_mode = is_numeric( $_GET['ubb_source'] ?? '' );
189+
$langs = array_map(
190+
function ( $text, $lang ) use ( $selected, $create_mode ) {
182191
if ( is_int( $text ) ) {
183192
$text = $lang;
184193
}
194+
$selected_str = \selected( $lang, $selected, false );
185195
return sprintf(
186-
'<option value="%1$s" %2$s>%3$s</option>',
196+
'<option value="%1$s" %2$s %3$s>%4$s</option>',
187197
$lang,
188-
\selected( $lang, $selected, false ),
198+
$selected_str,
199+
$create_mode && empty( $selected_str ) ? 'disabled' : '',
189200
$text
190201
);
191202
},

0 commit comments

Comments
 (0)