From f332082d25c94f66437183e4e770d46623a8ab1d Mon Sep 17 00:00:00 2001 From: goncaloasimoes <32324608+goncaloasimoes@users.noreply.github.com> Date: Mon, 14 Nov 2022 23:05:55 +0000 Subject: [PATCH] #2 Redirect to create new for terms --- lib/Terms/CreateTranslation.php | 71 ++++++++++++++++++++++++++++++++- lib/Terms/LangMetaBox.php | 23 ++++++++--- 2 files changed, 87 insertions(+), 7 deletions(-) diff --git a/lib/Terms/CreateTranslation.php b/lib/Terms/CreateTranslation.php index e5dde1d..fd0f503 100644 --- a/lib/Terms/CreateTranslation.php +++ b/lib/Terms/CreateTranslation.php @@ -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; diff --git a/lib/Terms/LangMetaBox.php b/lib/Terms/LangMetaBox.php index 4ccb79f..adad8b0 100644 --- a/lib/Terms/LangMetaBox.php +++ b/lib/Terms/LangMetaBox.php @@ -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( + '', + esc_sql( $_GET['ubb_source'] ) + ); + } } public function edit_term_language_metabox( $term ) { @@ -147,7 +154,8 @@ public function edit_term_language_metabox( $term ) { %2$s - + + ', esc_html__( 'Create Translation', 'unbabble' ), @@ -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; } @@ -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( - '', + '', $lang, - \selected( $lang, $selected, false ), + $selected_str, + $create_mode && empty( $selected_str ) ? 'disabled' : '', $text ); },