Skip to content

Commit

Permalink
CF7 live form in admin option.
Browse files Browse the repository at this point in the history
  • Loading branch information
kagg-design committed Sep 24, 2024
1 parent bdb1a74 commit 74fde8b
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 32 deletions.
57 changes: 30 additions & 27 deletions .tests/php/integration/CF7/AdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand All @@ -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' ] )
Expand All @@ -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' ] )
);
Expand All @@ -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.
*
Expand Down Expand Up @@ -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().
*
Expand Down
1 change: 1 addition & 0 deletions .tests/php/unit/HCaptchaTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' =>
Expand Down
1 change: 1 addition & 0 deletions .tests/php/unit/Settings/SystemInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions src/php/CF7/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ] );
}
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/php/CF7/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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' );
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/php/Migrations/Migrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
1 change: 1 addition & 0 deletions src/php/Settings/Integrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down

0 comments on commit 74fde8b

Please sign in to comment.