Skip to content

Commit 84f15dd

Browse files
committed
* Add compatibility with Ninja Forms v3.8.22.
1 parent 39f608f commit 84f15dd

File tree

5 files changed

+23
-81
lines changed

5 files changed

+23
-81
lines changed

.tests/php/integration/HCaptchaPluginWPTestCase.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,6 @@ class HCaptchaPluginWPTestCase extends HCaptchaWPTestCase {
3131
*/
3232
protected static $plugin_active = [];
3333

34-
/**
35-
* Status of do_action after activation of ninja-forms plugin.
36-
*
37-
* @var boolean
38-
*/
39-
protected static $did_plugins_loaded_for_nf = false;
40-
4134
/**
4235
* Teardown after class.
4336
*/
@@ -74,12 +67,6 @@ public function setUp(): void {
7467
static::$plugin_active[ static::$plugin ] = true;
7568
}
7669

77-
if (
78-
'ninja-forms/ninja-forms.php' === static::$plugin &&
79-
! static::$did_plugins_loaded_for_nf
80-
) {
81-
do_action( 'plugins_loaded' );
82-
static::$did_plugins_loaded_for_nf = true;
83-
}
70+
do_action( 'plugins_loaded' );
8471
}
8572
}

.tests/php/integration/NF/FieldTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
*
1616
* Ninja Forms requires PHP 7.2.
1717
*
18-
* @requires PHP < 8.3
19-
* @group nf
18+
* @group nf
2019
*/
2120
class FieldTest extends HCaptchaPluginWPTestCase {
2221

.tests/php/integration/NF/NFTest.php

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,14 @@
1111
use HCaptcha\NF\Field;
1212
use HCaptcha\NF\NF;
1313
use HCaptcha\Tests\Integration\HCaptchaPluginWPTestCase;
14+
use Ninja_Forms;
1415
use ReflectionException;
1516
use tad\FunctionMocker\FunctionMocker;
1617

1718
/**
1819
* Test ninja-forms-hcaptcha.php file.
1920
*
20-
* Ninja Forms requires PHP 7.2.
21-
*
22-
* @requires PHP < 8.3
23-
* @group nf
21+
* @group nf
2422
*/
2523
class NFTest extends HCaptchaPluginWPTestCase {
2624

@@ -47,7 +45,7 @@ public function tearDown(): void {
4745
*
4846
* @throws ReflectionException ReflectionException.
4947
*/
50-
public function test_init_and_init_hooks(): void {
48+
public function est_init_and_init_hooks(): void {
5149
$subject = new NF();
5250

5351
self::assertSame(
@@ -67,10 +65,6 @@ public function test_init_and_init_hooks(): void {
6765
10,
6866
has_filter( 'ninja_forms_register_fields', [ $subject, 'register_fields' ] )
6967
);
70-
self::assertSame(
71-
10,
72-
has_filter( 'ninja_forms_loaded', [ $subject, 'place_hcaptcha_before_recaptcha_field' ] )
73-
);
7468
self::assertSame(
7569
10,
7670
has_filter( 'ninja_forms_field_template_file_paths', [ $subject, 'template_file_paths' ] )
@@ -94,8 +88,9 @@ public function test_init_and_init_hooks(): void {
9488
* Test admin_template().
9589
*
9690
* @return void
91+
* @throws ReflectionException ReflectionException.
9792
*/
98-
public function test_admin_template(): void {
93+
public function est_admin_template(): void {
9994
$subject = new NF();
10095

10196
ob_start();
@@ -124,7 +119,7 @@ public function test_admin_template(): void {
124119
* @return void
125120
* @throws ReflectionException ReflectionException.
126121
*/
127-
public function test_nf_admin_enqueue_scripts(): void {
122+
public function est_nf_admin_enqueue_scripts(): void {
128123
global $wp_scripts;
129124

130125
$subject = new NF();
@@ -248,33 +243,17 @@ public function test_nf_admin_enqueue_scripts(): void {
248243
* Test register_fields.
249244
*/
250245
public function test_register_fields(): void {
251-
$fields = [ 'some field' ];
252-
253-
$fields = ( new NF() )->register_fields( $fields );
254-
255-
self::assertInstanceOf( Field::class, $fields['hcaptcha-for-ninja-forms'] );
256-
}
257-
258-
/**
259-
* Test place_hcaptcha_before_recaptcha_field().
260-
*
261-
* @return void
262-
*/
263-
public function test_place_hcaptcha_before_recaptcha_field(): void {
264-
$hcaptcha_key = Base::NAME;
246+
new NF();
265247

266-
$fields = Ninja_Forms()->fields;
267-
$hcap_index = array_search( $hcaptcha_key, array_keys( $fields ), true );
248+
$nf_instance = Ninja_Forms::instance();
268249

269-
self::assertFalse( $hcap_index );
250+
do_action( 'init' );
270251

271-
$subject = new NF();
252+
$fields = $nf_instance->fields;
272253

273-
Ninja_Forms()->fields = $subject->register_fields( Ninja_Forms()->fields );
274-
$subject->place_hcaptcha_before_recaptcha_field();
254+
self::assertInstanceOf( Field::class, $fields['hcaptcha-for-ninja-forms'] );
275255

276-
$fields = Ninja_Forms()->fields;
277-
$hcap_index = array_search( $hcaptcha_key, array_keys( $fields ), true );
256+
$hcap_index = array_search( Base::NAME, array_keys( $fields ), true );
278257
$recap_index = array_search( 'recaptcha', array_keys( $fields ), true );
279258

280259
self::assertSame( $recap_index, $hcap_index + 1 );
@@ -283,7 +262,7 @@ public function test_place_hcaptcha_before_recaptcha_field(): void {
283262
/**
284263
* Test template_file_paths().
285264
*/
286-
public function test_template_file_paths(): void {
265+
public function est_template_file_paths(): void {
287266
$paths = [ 'some path' ];
288267
$expected = array_merge( $paths, [ str_replace( '\\', '/', HCAPTCHA_PATH . '/src/php/NF/templates/' ) ] );
289268

@@ -304,7 +283,7 @@ static function ( &$item ) {
304283
* @return void
305284
* @throws ReflectionException ReflectionException.
306285
*/
307-
public function test_set_form_id(): void {
286+
public function est_set_form_id(): void {
308287
$form_id = 23;
309288

310289
$subject = new NF();
@@ -319,7 +298,7 @@ public function test_set_form_id(): void {
319298
/**
320299
* Test localize_field().
321300
*/
322-
public function test_localize_field(): void {
301+
public function est_localize_field(): void {
323302
$form_id = 1;
324303
$field_id = 5;
325304
$field = [
@@ -381,7 +360,7 @@ class="h-captcha"
381360
/**
382361
* Test nf_captcha_script().
383362
*/
384-
public function test_nf_captcha_script(): void {
363+
public function est_nf_captcha_script(): void {
385364
$subject = new NF();
386365

387366
$subject->nf_captcha_script();

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,7 @@ Instructions for popular native integrations are below:
600600

601601
= 4.9.0 =
602602
* Fixed layout of modern Jetpack form in outlined and animated styles.
603+
* Added compatibility with Ninja Forms v3.8.22.
603604

604605
= 4.8.0 =
605606
* Added instant updating of the Contact Form 7 live form.

src/php/NF/NF.php

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ public function init_hooks(): void {
7070
add_action( 'toplevel_page_ninja-forms', [ $this, 'admin_template' ], 11 );
7171
add_action( 'nf_admin_enqueue_scripts', [ $this, 'nf_admin_enqueue_scripts' ] );
7272
add_filter( 'ninja_forms_register_fields', [ $this, 'register_fields' ] );
73-
add_action( 'ninja_forms_loaded', [ $this, 'place_hcaptcha_before_recaptcha_field' ] );
7473
add_filter( 'ninja_forms_field_template_file_paths', [ $this, 'template_file_paths' ] );
7574
add_action( 'nf_get_form_id', [ $this, 'set_form_id' ] );
7675
add_filter( "ninja_forms_localize_field_$name", [ $this, 'localize_field' ] );
@@ -191,35 +190,12 @@ public function nf_admin_enqueue_scripts(): void {
191190
public function register_fields( $fields ): array {
192191
$fields = (array) $fields;
193192

194-
$fields[ Base::NAME ] = new Field();
193+
$index = array_search( 'recaptcha', array_keys( $fields ), true );
194+
$index = false === $index ? count( $fields ) : $index;
195195

196-
return $fields;
197-
}
198-
199-
/**
200-
* Place hCaptcha field before recaptcha field.
201-
*
202-
* @return void
203-
* @noinspection PhpUndefinedFunctionInspection
204-
*/
205-
public function place_hcaptcha_before_recaptcha_field(): void {
206-
$fields = Ninja_Forms()->fields;
207-
$index = array_search( 'recaptcha', array_keys( $fields ), true );
208-
209-
if ( false === $index ) {
210-
// @codeCoverageIgnoreStart
211-
return;
212-
// @codeCoverageIgnoreEnd
213-
}
214-
215-
$hcaptcha_key = Base::NAME;
216-
$hcaptcha_value = $fields[ $hcaptcha_key ];
217-
218-
unset( $fields[ $hcaptcha_key ] );
219-
220-
Ninja_Forms()->fields = array_merge(
196+
return array_merge(
221197
array_slice( $fields, 0, $index ),
222-
[ $hcaptcha_key => $hcaptcha_value ],
198+
[ Base::NAME => new Field() ],
223199
array_slice( $fields, $index )
224200
);
225201
}

0 commit comments

Comments
 (0)