From c1713fcc93fc43fd5c38e63e80673e6a02e61f30 Mon Sep 17 00:00:00 2001 From: Sasha Ivanenko <46720998+girafffee@users.noreply.github.com> Date: Thu, 18 Apr 2024 14:23:06 +0300 Subject: [PATCH] Remove placeholder for the post-type option in Insert/Update post action Crocoblock/issues-tracker#3056 --- .../post-modification/base-post-action.php | 8 +++- includes/actions/types/insert-post.php | 8 +--- .../InsertPost/PostTypePropertyTest.php | 47 +++++++++++++++++++ 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/includes/actions/methods/post-modification/base-post-action.php b/includes/actions/methods/post-modification/base-post-action.php index 86d205481..ac61f9383 100644 --- a/includes/actions/methods/post-modification/base-post-action.php +++ b/includes/actions/methods/post-modification/base-post-action.php @@ -59,8 +59,14 @@ public function add_inserted_post_id() { ); } + $post_type = $this->modifier->source_arr['post_type'] ?? ''; + + if ( ! $post_type ) { + $post_type = get_post_type( $this->inserted_id ); + } + $inserted_key = jet_fb_context()->get_unique_name( - 'inserted_' . $this->modifier->source_arr['post_type'] + 'inserted_' . $post_type ); jet_fb_context()->update_request( $this->inserted_id, $inserted_key ); diff --git a/includes/actions/types/insert-post.php b/includes/actions/types/insert-post.php index b24833cf3..7b0eefd1e 100644 --- a/includes/actions/types/insert-post.php +++ b/includes/actions/types/insert-post.php @@ -113,14 +113,8 @@ public function action_data() { ); } - $post_types = Tools::get_post_types_for_js(); - $post_types[0] = array( - 'label' => __( '-- Keep current (when updating post) --', 'jet-form-builder' ), - 'value' => '', - ); - return array( - 'postTypes' => $post_types, + 'postTypes' => Tools::get_post_types_for_js(), 'taxonomies' => Tools::get_taxonomies_for_modify(), 'postStatuses' => $this->get_post_statuses_for_options(), 'properties' => $properties, diff --git a/tests/wpunit/Actions/InsertPost/PostTypePropertyTest.php b/tests/wpunit/Actions/InsertPost/PostTypePropertyTest.php index 0a1436a73..c010353ed 100644 --- a/tests/wpunit/Actions/InsertPost/PostTypePropertyTest.php +++ b/tests/wpunit/Actions/InsertPost/PostTypePropertyTest.php @@ -5,6 +5,7 @@ use Codeception\AssertThrows; use Jet_Form_Builder\Actions\Methods\Post_Modification\Post_Modifier; use Jet_Form_Builder\Actions\Methods\Post_Modification\Post_Type_Property; +use Jet_Form_Builder\Actions\Types\Insert_Post; use Jet_Form_Builder\Exceptions\Action_Exception; use Jet_Form_Builder\Exceptions\Silence_Exception; @@ -103,5 +104,51 @@ public function testAllowChangePostType() { $this->assertEquals( 'page', $property->get_value( $this->modifier ) ); } + public function testComputedNames() { + $action = new Insert_Post(); + $action->_id = 7755; + + jet_fb_action_handler()->save_action( $action, array() ); + + $action->settings = array( + 'post_type' => 'page' + ); + + jet_fb_action_handler()->process_single_action( $action ); + + $this->assertNotEmpty( jet_fb_context()->get_value( 'inserted_page' ) ); + $this->assertIsNumeric( jet_fb_context()->get_value( 'inserted_page' ) ); + + } + + public function testComputedNamesOnUpdateWithEmptyPostType() { + $post_id = wp_insert_post( + array( + 'post_type' => 'page', + 'post_title' => 'something', + 'post_content' => '12345', + ) + ); + + $action = new Insert_Post(); + $action->_id = 7755; + + jet_fb_action_handler()->save_action( $action, array() ); + + $action->settings = array( + 'fields_map' => array( + '_post_id' => 'ID', + ), + ); + + + jet_fb_context()->update_request( $post_id, '_post_id' ); + jet_fb_action_handler()->process_single_action( $action ); + + $this->assertNotEmpty( jet_fb_context()->get_value( 'inserted_page' ) ); + $this->assertIsNumeric( jet_fb_context()->get_value( 'inserted_page' ) ); + + } + } \ No newline at end of file