diff --git a/php/context/class-fieldmanager-context-submenu.php b/php/context/class-fieldmanager-context-submenu.php index f7ebb3ab58..645308cb83 100644 --- a/php/context/class-fieldmanager-context-submenu.php +++ b/php/context/class-fieldmanager-context-submenu.php @@ -151,6 +151,9 @@ public function save_submenu_data( $data = null ) { $current = get_option( $this->fm->name, null ); $data = $this->prepare_data( $current, $data ); $data = apply_filters( 'fm_submenu_presave_data', $data, $this ); + if ( $this->fm->skip_save ) { + return true; + } if ( isset( $current ) ) { update_option( $this->fm->name, $data ); diff --git a/tests/php/test-fieldmanager-context-submenu.php b/tests/php/test-fieldmanager-context-submenu.php index c5039f8770..e0c3d3e2ff 100644 --- a/tests/php/test-fieldmanager-context-submenu.php +++ b/tests/php/test-fieldmanager-context-submenu.php @@ -124,6 +124,35 @@ public function test_urls() { $this->assertEquals( admin_url( 'admin.php?page=' . $name_4 ), $context_4->url() ); } + public function test_skip_save() { + $name = 'skip_save'; + fm_register_submenu_page( $name, 'tools.php', 'Skip Save Fields' ); + // Should save the first time + $context = $this->get_context( 'skip_save' ); + $data = array( + 'name' => 'Foo', + 'email' => 'foo@alleyinteractive.com', + 'remember' => true, + 'number' => 11, + 'group' => array( 'preferences' => '' ), + ); + $this->assertTrue( $context->save_submenu_data( $data ) ); + $this->assertEquals( $data, get_option( 'skip_save' ) ); + // Shouldn't save the second time + $context->fm->skip_save = true; + delete_option( 'skip_save' ); + $this->assertFalse( get_option( 'skip_save' ) ); + $this->assertTrue( $context->save_submenu_data( $data ) ); + $this->assertFalse( get_option( 'skip_save' ) ); + // Permit saving the group, but not an individual field + $context->fm->skip_save = false; + $context->fm->children['name']->skip_save = true; + $this->assertTrue( $context->save_submenu_data( $data ) ); + $option = get_option( 'skip_save' ); + $this->assertFalse( isset( $option['name'] ) ); + $this->assertEquals( 'foo@alleyinteractive.com', $option['email'] ); + } + /** * Build a html from the default context and fields. * @@ -235,4 +264,4 @@ public function test_updated_message() { $this->assertContains( "
{$updated_message}