Skip to content

Commit

Permalink
#2 Save and Redirect to new post create action
Browse files Browse the repository at this point in the history
  • Loading branch information
goncaloasimoes committed Nov 14, 2022
1 parent 1b75440 commit 2a7f44c
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 22 deletions.
1 change: 1 addition & 0 deletions lib/Admin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function register() {
// Whitelist 'lang' as a query_var.
add_filter( 'query_vars', function( $query_vars ) {
$query_vars[] = 'lang';
$query_vars[] = 'ubb_source';
return $query_vars;
} );

Expand Down
24 changes: 16 additions & 8 deletions lib/LangInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,27 @@ class LangInterface {
public static function get_current_language() : string {
$options = Options::get();
$lang = get_query_var( 'lang', null );
// TODO: check for lang in the Allowed languages.

// TODO: Auto-draft saving does not put the query var.
if ( ! isset( $lang ) && isset( $_GET['lang'] ) ) {
$lang = $_GET['lang'];
}

if ( ! isset( $lang ) && is_admin() ) {
$lang = $_COOKIE['ubb_lang'];
}

if ( ! isset( $lang ) ) {
$lang = $options['default_language'];
}
return apply_filters( 'ubb_current_lang', $lang );

$options = Options::get();
if ( ! in_array( $lang, $options['allowed_languages'] ) ) {
$lang = $options['default_language'];
}

// TODO: which sanitize to use.
return apply_filters( 'ubb_current_lang', \sanitize_text_field( $lang ) );
}

/**
Expand Down Expand Up @@ -170,19 +183,14 @@ public static function change_post_language( int $post_id, string $lang ) : bool
return false;
}

// TODO: Update terms
// TODO: Update terms.
$allowed_taxonomies = Options::get_allowed_taxonomies();
error_log( print_r( $terms, true ) );
error_log( print_r( $allowed_taxonomies, true ) );
foreach ( $terms as $term ) {
if ( ! in_array( $term->taxonomy, $allowed_taxonomies, true ) ) {
continue;
}
error_log( print_r( $term->term_id, true ) );
error_log( print_r( $term->taxonomy, true ) );
$term_translation = LangInterface::get_term_translation( $term->term_id, $lang );

error_log( print_r( $term_translation, true ) );
wp_remove_object_terms( $post_id, $term->term_id, $term->taxonomy );

if ( $term_translation != null ) {
Expand Down
73 changes: 72 additions & 1 deletion lib/Posts/CreateTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,80 @@ public function register() {
if ( Options::only_one_language_allowed() ) {
return;
}
\add_action( 'save_post', [ $this, 'create_and_redirect' ], PHP_INT_MAX );
// Redirect to create new post page to create a translation.
// FIXME: Saving an auto-draft (no title) does not call save_post and so source is not set.
\add_action( 'save_post', [ $this, 'redirect_to_new' ], PHP_INT_MAX );
\add_action( 'save_post', [ $this, 'set_source' ], PHP_INT_MAX );

// TODO: Use Yoast's duplicate-post plugin to duplicate post before redirect.
// \add_action( 'save_post', [ $this, 'create_and_redirect' ], PHP_INT_MAX );
}

public function redirect_to_new( int $post_id ) : void {
$post_type = get_post_type( $post_id );
if ( $post_type === 'revision' || ! in_array( $post_type, Options::get_allowed_post_types(), true ) ) {
return;
}
if ( ! ( $_POST['ubb_redirect_new'] ?? false ) ) {
return;
}

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

wp_safe_redirect(
add_query_arg(
[
'lang' => $lang_create,
'ubb_source' => $post_id,
],
admin_url( 'post-new.php' )
),
302,
'Unbabble'
);
exit;
}

public function set_source( int $post_id ) : void {
$post_type = get_post_type( $post_id );
$allowed_post_types = Options::get_allowed_post_types();
if (
$post_type === 'revision'
|| ! in_array( $post_type, $allowed_post_types, true )
|| ! isset( $_POST['ubb_source'] )
) {
return;
}

if ( ! is_numeric( $_POST['ubb_source'] ) ) {
return;
}

$src_post = get_post( \sanitize_text_field( $_POST['ubb_source'] ) );
if ( $src_post === null || ! in_array( $src_post->post_type, $allowed_post_types, true ) ) {
return;
}

$original_source = LangInterface::get_post_source( $src_post->ID );
if ( $original_source === null ) {
$original_source = $src_post->ID;
LangInterface::set_post_source( $src_post->ID, $src_post->ID );
}

LangInterface::set_post_source( $post_id, $original_source );
}

// TODO: Refactor to use duplicate-post for copying and redirecting.
public function create_and_redirect( int $post_id ) : void {
$post_type = get_post_type( $post_id );
if ( $post_type === 'revision' || ! in_array( $post_type, Options::get_allowed_post_types(), true ) ) {
Expand Down
28 changes: 20 additions & 8 deletions lib/Posts/LangMetaBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ public function post_language_selector_callback( \WP_Post $post ) : void {
}

$this->print_language_select( 'ubb_lang', $lang, $options['allowed_languages'], 'ubb_language_metabox_nonce', 'ubb_language_metabox' );

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

$current_screen = get_current_screen();
if ( $current_screen->base == "post" && $current_screen->action == "add" ) {
return;
Expand Down Expand Up @@ -154,9 +162,10 @@ public function post_language_selector_callback( \WP_Post $post ) : void {
printf(
'<p><b>Create Translation</b></p>
<div>To: %1$s</div>
<input type="submit" %2$s name="ubb_save_create" value="Save and Create" class="button"/>',
<input type="submit" %2$s name="ubb_redirect_new" value="Save and Create" class="button"/>
<input type="submit" %2$s name="ubb_copy_new" value="Save and Copy" class="button"/>',
$this->print_language_select( 'ubb_create', '', $available_languages, '', '', false ),
$post->post_status === 'draft' ? 'id="save-post"' : ''
$post->post_status === 'draft' ? 'id="save-post"' : '',
);
}

Expand All @@ -169,7 +178,7 @@ public function post_language_selector_callback( \WP_Post $post ) : void {
* @return void
*/
public function save_post_language( int $post_id ) : void {
if ( 'auto-draft' === get_post( $post_id )->post_status) {
if ( 'auto-draft' === get_post( $post_id )->post_status ) {
LangInterface::set_post_language( $post_id, LangInterface::get_current_language() );
return;
}
Expand Down Expand Up @@ -205,15 +214,18 @@ public function save_post_language( int $post_id ) : void {
* @return string
*/
private function print_language_select( string $name, $selected, $options, string $nonce_action, string $nonce_name, $echo = true ) : string {
$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 All @@ -230,7 +242,7 @@ function ( $text, $lang ) use ( $selected ) {
%2$s
</select>',
$name,
implode( '', $langs )
implode( '', $langs ),
);

return ! $echo ? $output : printf( $output );
Expand Down
4 changes: 0 additions & 4 deletions lib/Router/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,6 @@ public function pre_redirect_guess_404_permalink( $pre ) {
return false;
}

error_log( print_r( $post_id, true ) );
error_log( print_r( LangInterface::get_post_language( $post_id ), true ) );
error_log( print_r( LangInterface::get_current_language(), true ) );

// If the post language is not the same as the current language, then don't redirect.
if ( LangInterface::get_post_language( $post_id ) !== LangInterface::get_current_language() ) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion lib/Terms/ChangeLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function change_language( int $term_id ) : void {
// TODO: Finish this.
return;

$ubb_lang = $_POST['ubb_lang'] ?? '';
$ubb_lang = \sanitize_text_field( $_POST['ubb_lang'] ?? '' );

$status = LangInterface::change_term_language( $term_id, $ubb_lang );

Expand Down

0 comments on commit 2a7f44c

Please sign in to comment.