Skip to content

Commit

Permalink
#2 Redirect to create new for terms
Browse files Browse the repository at this point in the history
  • Loading branch information
goncaloasimoes committed Nov 14, 2022
1 parent de5ff1b commit f332082
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 7 deletions.
71 changes: 70 additions & 1 deletion lib/Terms/CreateTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,78 @@ public function register() {
if ( Options::only_one_language_allowed() ) {
return;
}
\add_action( 'saved_term', [ $this, 'create_and_redirect' ], PHP_INT_MAX, 4 );
// Redirect to create new translation.
\add_action( 'saved_term', [ $this, 'redirect_to_new' ], PHP_INT_MAX, 4 );
\add_action( 'saved_term', [ $this, 'set_new_source' ], PHP_INT_MAX, 4 );

// TODO: Refactor for copy.
// \add_action( 'saved_term', [ $this, 'create_and_redirect' ], PHP_INT_MAX, 4 );
}

public function redirect_to_new( int $term_id, int $tt_id, string $taxonomy, bool $update ) : void {
if (
! $update
|| ! in_array( $taxonomy, Options::get_allowed_taxonomies(), true )
|| ! ( $_POST['ubb_redirect_new'] ?? false )
) {
return;
}

// Language to set to the new term.
$lang_create = $_POST['ubb_create'] ?? '';
if (
empty( $lang_create )
|| ! in_array( $lang_create, Options::get()['allowed_languages'] )
// TODO: check if term_id has this language already
) {
// TODO: What else to do when this happens.
error_log( print_r( 'CreateTranslation - lang create failed', true ) );
return;
}

// TODO: Add something in the page to show that a translation is being saved. Use existence of ubb_source.
wp_safe_redirect(
add_query_arg(
[
'taxonomy' => $taxonomy,
'lang' => $lang_create,
'ubb_source' => $term_id,
],
admin_url( 'edit-tags.php' )
),
302,
'Unbabble'
);
exit;
}

public function set_new_source( int $term_id, int $tt_id, string $taxonomy, bool $update ) : void {
$allowed_taxonomies = Options::get_allowed_taxonomies();
if (
$update
|| ! in_array( $taxonomy, $allowed_taxonomies, true )
|| ! isset( $_POST['ubb_source'] )
|| ! is_numeric( $_POST['ubb_source'] )
) {
return;
}

$src_term = get_term( \sanitize_text_field( $_POST['ubb_source'] ), $taxonomy );
if ( $src_term === null || ! in_array( $src_term->taxonomy, $allowed_taxonomies, true ) ) {
return;
}

$original_source = LangInterface::get_term_source( $src_term->term_id );
if ( $original_source === null ) {
$original_source = $src_term->term_id;
LangInterface::set_term_source( $src_term->term_id, $src_term->term_id );
}

LangInterface::set_term_source( $term_id, $original_source );
}


// TODO: Refactor for copy.
public function create_and_redirect( int $term_id, int $tt_id, string $taxonomy, bool $update ) : void {
if ( ! $update ) {
return;
Expand Down
23 changes: 17 additions & 6 deletions lib/Terms/LangMetaBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ public function new_term_language_metabox() {
$this->print_language_select( 'ubb_lang', LangInterface::get_current_language(), $options['allowed_languages'], 'ubb_language_metabox_nonce', 'ubb_language_metabox', false ),
esc_html__( 'The term only appears on the site for this language.', 'unbabble' )
);

if ( is_numeric( $_GET['ubb_source'] ?? '' ) ) {
printf(
'<input type="hidden" id="ubb_source" name="ubb_source" value="%s">',
esc_sql( $_GET['ubb_source'] )
);
}
}

public function edit_term_language_metabox( $term ) {
Expand Down Expand Up @@ -147,7 +154,8 @@ public function edit_term_language_metabox( $term ) {
<th scope="row"><label for="language">%1$s</label></th>
<td>
%2$s
<input type="submit" name="ubb_save_create" value="Save and Create" class="button"/>
<input type="submit" name="ubb_redirect_new" value="Save and Create" class="button"/>
<input type="submit" name="ubb_copy_new" value="Save and Copy" class="button"/>
</td>
</tr>',
esc_html__( 'Create Translation', 'unbabble' ),
Expand All @@ -161,7 +169,7 @@ public function save_term_language( $term_id ) {
return;
}

if ( isset( $_POST['ubb_save_create'] ) ) { // Don't set post language when creating a translation.
if ( isset( $_POST['ubb_copy_new'] ) ) { // Don't set post language when creating a translation.
return;
}

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

// TODO: Duplicated in Posts/LangMetaBox
private function print_language_select( string $name, $selected, $options, string $nonce_action, string $nonce_name, $echo = true ) {
$langs = array_map(
function ( $text, $lang ) use ( $selected ) {
$create_mode = is_numeric( $_GET['ubb_source'] ?? '' );
$langs = array_map(
function ( $text, $lang ) use ( $selected, $create_mode ) {
if ( is_int( $text ) ) {
$text = $lang;
}
$selected_str = \selected( $lang, $selected, false );
return sprintf(
'<option value="%1$s" %2$s>%3$s</option>',
'<option value="%1$s" %2$s %3$s>%4$s</option>',
$lang,
\selected( $lang, $selected, false ),
$selected_str,
$create_mode && empty( $selected_str ) ? 'disabled' : '',
$text
);
},
Expand Down

0 comments on commit f332082

Please sign in to comment.