From 74fde8b456088c6631fc26c51c6d2aebe22a8da8 Mon Sep 17 00:00:00 2001 From: kagg-design Date: Tue, 24 Sep 2024 23:43:25 +0300 Subject: [PATCH] CF7 live form in admin option. --- .tests/php/integration/CF7/AdminTest.php | 57 +++++++++++---------- .tests/php/unit/HCaptchaTestCase.php | 1 + .tests/php/unit/Settings/SystemInfoTest.php | 1 + src/php/CF7/Admin.php | 11 ++-- src/php/CF7/Base.php | 8 +++ src/php/Migrations/Migrations.php | 18 +++++++ src/php/Settings/Integrations.php | 1 + 7 files changed, 65 insertions(+), 32 deletions(-) diff --git a/.tests/php/integration/CF7/AdminTest.php b/.tests/php/integration/CF7/AdminTest.php index 99ead8c6..af5e5102 100644 --- a/.tests/php/integration/CF7/AdminTest.php +++ b/.tests/php/integration/CF7/AdminTest.php @@ -47,15 +47,14 @@ public function tearDown(): void { /** * Test init_hooks(). * - * @param bool $mode_auto Mode auto. * @param bool $mode_embed Mode embed. + * @param bool $mode_live Mode live. * @param bool $is_admin Admin mode. - * @param bool $expected Hooks expected to be added. * * @dataProvider dp_test_init_hooks */ - public function test_init_hooks( bool $mode_auto, bool $mode_embed, bool $is_admin, bool $expected ): void { - $cf7_status = array_filter( [ $mode_auto ? 'form' : '', $mode_embed ? 'embed' : '' ] ); + public function test_init_hooks( bool $mode_embed, bool $mode_live, bool $is_admin ): void { + $cf7_status = array_filter( [ $mode_embed ? 'embed' : '', $mode_live ? 'live' : '' ] ); $cf7_screen = 'toplevel_page_wpcf7'; if ( $is_admin ) { @@ -76,15 +75,22 @@ public function test_init_hooks( bool $mode_auto, bool $mode_embed, bool $is_adm $subject = new Admin(); - if ( $is_admin && $cf7_status ) { + if ( $is_admin ) { set_current_screen( $cf7_screen ); } - if ( $expected ) { + if ( $mode_embed && $is_admin ) { self::assertSame( 54, has_action( 'wpcf7_admin_init', [ $subject, 'add_tag_generator_hcaptcha' ] ) ); + } else { + self::assertFalse( + has_action( 'wpcf7_admin_init', [ $subject, 'add_tag_generator_hcaptcha' ] ) + ); + } + + if ( $mode_live && $is_admin ) { self::assertSame( 10, has_action( 'current_screen', [ $subject, 'current_screen' ] ) @@ -106,9 +112,6 @@ public function test_init_hooks( bool $mode_auto, bool $mode_embed, bool $is_adm has_action( 'admin_enqueue_scripts', [ $subject, 'enqueue_admin_scripts_after_cf7' ] ) ); } else { - self::assertFalse( - has_action( 'wpcf7_admin_init', [ $subject, 'add_tag_generator_hcaptcha' ] ) - ); self::assertFalse( has_action( 'current_screen', [ $subject, 'current_screen' ] ) ); @@ -125,6 +128,24 @@ public function test_init_hooks( bool $mode_auto, bool $mode_embed, bool $is_adm } } + /** + * Data provider for test_init_hooks(). + * + * @return array + */ + public function dp_test_init_hooks(): array { + return [ + [ false, false, false ], + [ false, false, true ], + [ false, true, false ], + [ false, true, true ], + [ true, false, false ], + [ true, false, true ], + [ true, true, false ], + [ true, true, true ], + ]; + } + /** * Test init_hooks() when not on CF7 admin page. * @@ -152,24 +173,6 @@ public function test_init_hooks_NOT_on_cf7_admin_page(): void { ); } - /** - * Data provider for test_init_hooks(). - * - * @return array - */ - public function dp_test_init_hooks(): array { - return [ - [ false, false, false, false ], - [ false, false, true, false ], - [ false, true, false, false ], - [ false, true, true, true ], - [ true, false, false, false ], - [ true, false, true, true ], - [ true, true, false, false ], - [ true, true, true, true ], - ]; - } - /** * Test current_screen(). * diff --git a/.tests/php/unit/HCaptchaTestCase.php b/.tests/php/unit/HCaptchaTestCase.php index 4aec706f..f3ba7dd3 100644 --- a/.tests/php/unit/HCaptchaTestCase.php +++ b/.tests/php/unit/HCaptchaTestCase.php @@ -884,6 +884,7 @@ protected function get_test_integrations_form_fields(): array { [ 'form' => 'Form Auto-Add', 'embed' => 'Form Embed', + 'live' => 'Live Form in Admin', ], ], 'divi_status' => diff --git a/.tests/php/unit/Settings/SystemInfoTest.php b/.tests/php/unit/Settings/SystemInfoTest.php index b2f82b6c..4cb58ef6 100644 --- a/.tests/php/unit/Settings/SystemInfoTest.php +++ b/.tests/php/unit/Settings/SystemInfoTest.php @@ -317,6 +317,7 @@ public function test_get_system_info(): void { Contact Form 7: Form Auto-Add: On Form Embed: Off + Live Form in Admin: Off WP Core: Comment Form: Off Login Form: Off diff --git a/src/php/CF7/Admin.php b/src/php/CF7/Admin.php index e91e085a..d2285def 100644 --- a/src/php/CF7/Admin.php +++ b/src/php/CF7/Admin.php @@ -32,16 +32,17 @@ class Admin extends Base { public function init_hooks(): void { parent::init_hooks(); - if ( ( ! $this->mode_auto && ! $this->mode_embed ) || ! is_admin() ) { + if ( ! Pages::is_cf7_edit_page() ) { return; } - if ( ! Pages::is_cf7_edit_page() ) { - return; + if ( $this->mode_embed ) { + add_action( 'wpcf7_admin_init', [ $this, 'add_tag_generator_hcaptcha' ], 54 ); } - add_action( 'wpcf7_admin_init', [ $this, 'add_tag_generator_hcaptcha' ], 54 ); - add_action( 'current_screen', [ $this, 'current_screen' ] ); + if ( $this->mode_live ) { + add_action( 'current_screen', [ $this, 'current_screen' ] ); + } } /** diff --git a/src/php/CF7/Base.php b/src/php/CF7/Base.php index a25685de..a5ad7c53 100644 --- a/src/php/CF7/Base.php +++ b/src/php/CF7/Base.php @@ -26,6 +26,13 @@ class Base { */ protected $mode_embed = false; + /** + * Whether to show the live hCaptcha form in the form editor. + * + * @var bool + */ + protected $mode_live = false; + /** * Base constructor. */ @@ -41,6 +48,7 @@ public function __construct() { public function init_hooks(): void { $this->mode_auto = hcaptcha()->settings()->is( 'cf7_status', 'form' ); $this->mode_embed = hcaptcha()->settings()->is( 'cf7_status', 'embed' ); + $this->mode_live = hcaptcha()->settings()->is( 'cf7_status', 'live' ); } /** diff --git a/src/php/Migrations/Migrations.php b/src/php/Migrations/Migrations.php index 4c93e46c..f6a6ecfc 100644 --- a/src/php/Migrations/Migrations.php +++ b/src/php/Migrations/Migrations.php @@ -361,6 +361,24 @@ protected function migrate_4_0_0(): ?bool { return true; } + /** + * Migrate to 4.6.0 + * + * @return bool|null + * @noinspection PhpUnused + */ + protected function migrate_4_6_0(): ?bool { + $option = get_option( PluginSettingsBase::OPTION_NAME, [] ); + $cf7_status = $option['cf7_status'] ?? []; + + // Turn on Live Form in admin by default. + $option['cf7_status'] = array_merge( $cf7_status, [ 'live' ] ); + + update_option( PluginSettingsBase::OPTION_NAME, $option ); + + return true; + } + /** * Save license level in settings. * diff --git a/src/php/Settings/Integrations.php b/src/php/Settings/Integrations.php index 8f5deb36..f1df263a 100644 --- a/src/php/Settings/Integrations.php +++ b/src/php/Settings/Integrations.php @@ -278,6 +278,7 @@ public function init_form_fields(): void { 'options' => [ 'form' => __( 'Form Auto-Add', 'hcaptcha-for-forms-and-more' ), 'embed' => __( 'Form Embed', 'hcaptcha-for-forms-and-more' ), + 'live' => __( 'Live Form in Admin', 'hcaptcha-for-forms-and-more' ), ], ], 'divi_status' => [