Skip to content

Commit

Permalink
#2 Save and Copy to new translation action
Browse files Browse the repository at this point in the history
  • Loading branch information
goncaloasimoes committed Nov 14, 2022
1 parent 2a7f44c commit de5ff1b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 27 deletions.
47 changes: 21 additions & 26 deletions lib/Posts/CreateTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
namespace TwentySixB\WP\Plugin\Unbabble\Posts;

use TwentySixB\WP\Plugin\Unbabble\LangInterface;
use WP_Embed;
use WP_Error;
use TwentySixB\WP\Plugin\Unbabble\Options;


/**
* For hooks related to creating a translations from an existing post.
*
Expand All @@ -18,13 +16,20 @@ public function register() {
if ( Options::only_one_language_allowed() ) {
return;
}
// 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 );
\add_action( 'admin_init', function() {
// 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_new_source' ], PHP_INT_MAX );

// TODO: Move to separate class for integration.
// Use Yoast's duplicate-post plugin to duplicate post before redirect.
if ( \is_plugin_active( 'duplicate-post/duplicate-post.php' ) ) {
\add_action( 'save_post', [ $this, 'copy_and_redirect' ], PHP_INT_MAX );
}
} );

}

public function redirect_to_new( int $post_id ) : void {
Expand Down Expand Up @@ -62,7 +67,7 @@ public function redirect_to_new( int $post_id ) : void {
exit;
}

public function set_source( int $post_id ) : void {
public function set_new_source( int $post_id ) : void {
$post_type = get_post_type( $post_id );
$allowed_post_types = Options::get_allowed_post_types();
if (
Expand Down Expand Up @@ -91,16 +96,15 @@ public function set_source( int $post_id ) : void {
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 {
public function copy_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 ) ) {
return;
}
if ( ! ( $_POST['ubb_save_create'] ?? false ) ) {
if ( ! ( $_POST['ubb_copy_new'] ?? false ) ) {
return;
}
$_POST['ubb_save_create'] = false; // Used to stop recursion and stop saving in the LangMetaBox.php.
$_POST['ubb_copy_new'] = false; // Used to stop recursion and stop saving in the LangMetaBox.php.

// Language to set to the new post.
$lang_create = $_POST['ubb_create'] ?? '';
Expand All @@ -114,26 +118,17 @@ public function create_and_redirect( int $post_id ) : void {
return;
}

add_filter( 'wp_insert_post_empty_content', '__return_false' );

// TODO: slug needs to be different.
$new_post_id = wp_insert_post( [
'post_title' => "({$lang_create})-" . get_post( $post_id )->post_title,
'post_content' => '',
'post_status' => 'draft',
'post_type' => get_post_type( $post_id ),
'post_name' => get_post( $post_id )->post_title . "-{$lang_create}",
], true );

// TODO: check if its being removed correctly.
remove_filter( 'wp_insert_post_empty_content', '__return_false' );
$post_duplicator = new \Yoast\WP\Duplicate_Post\Post_Duplicator();
$new_post_id = $post_duplicator->create_duplicate( get_post( $post_id ), [] );

if ( $new_post_id instanceof WP_Error ) {
error_log( print_r( 'CreateTranslation - New post error', true ) );
// TODO: How to show error.
return;
}

\delete_post_meta( $new_post_id, '_dp_original' );

// Set language in the custom post lang table.
if ( ! LangInterface::set_post_language( $new_post_id, $lang_create ) ) {
error_log( print_r( 'CreateTranslation - language set failed', true ) );
Expand Down
3 changes: 2 additions & 1 deletion lib/Posts/LangMetaBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ public function post_language_selector_callback( \WP_Post $post ) : void {
$available_languages = array_keys( $available_languages );

// Display language selector and button to create new translation.
// TODO: Only show `ubb_copy_new` input if duplicate-post is active. Add filter and move the input to an integration class.
printf(
'<p><b>Create Translation</b></p>
<div>To: %1$s</div>
Expand Down Expand Up @@ -189,7 +190,7 @@ public function save_post_language( int $post_id ) : void {

if (
get_post_type( $post_id ) === 'revision'
|| isset( $_POST['ubb_save_create'] ) // Don't set post language when creating a translation.
|| isset( $_POST['ubb_copy_new'] ) // Don't set post language when copying to a translation.
) {
return;
}
Expand Down

0 comments on commit de5ff1b

Please sign in to comment.