Skip to content

Commit

Permalink
Fix CF7 admin scripts upon CF7 update.
Browse files Browse the repository at this point in the history
  • Loading branch information
kagg-design committed Nov 3, 2024
1 parent 5f9837b commit 14b1004
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
9 changes: 4 additions & 5 deletions .tests/php/integration/CF7/AdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use HCaptcha\CF7\Admin;
use HCaptcha\Tests\Integration\HCaptchaPluginWPTestCase;
use Mockery;
use WPCF7_TagGenerator;

/**
Expand Down Expand Up @@ -503,16 +502,16 @@ public function test_enqueue_admin_scripts(): void {

wpcf7_admin_enqueue_scripts( 'wpcf7' );

$data = $wp_scripts->registered['wpcf7-admin']->extra['data'];
preg_match( '/var wpcf7 = ({.+});/', $data, $m );
$data = $wp_scripts->registered['wpcf7-admin']->extra['before'][1];
preg_match( '/var wpcf7 = ({.+});/s', $data, $m );
$wpcf7 = json_decode( $m[1], true );

self::assertArrayNotHasKey( 'api', $wpcf7 );

$subject->enqueue_admin_scripts_after_cf7();

$data = $wp_scripts->registered['wpcf7-admin']->extra['data'];
preg_match( '/var wpcf7 = ({.+});/', $data, $m );
$data = $wp_scripts->registered['wpcf7-admin']->extra['before'][1];
preg_match( '/var wpcf7 = ({.+});/s', $data, $m );
$wpcf7 = json_decode( $m[1], true );

self::assertArrayHasKey( 'api', $wpcf7 );
Expand Down
6 changes: 3 additions & 3 deletions src/php/CF7/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,12 @@ public function enqueue_admin_scripts_after_cf7(): void {
],
];

$data = $wp_scripts->registered['wpcf7-admin']->extra['data'];
$data = $wp_scripts->registered['wpcf7-admin']->extra['before'][1];

if ( preg_match( '/var wpcf7 = ({.+});/', $data, $m ) ) {
if ( preg_match( '/var wpcf7 = ({.+});/s', $data, $m ) ) {
$wpcf7 = array_merge( $wpcf7, json_decode( $m[1], true ) );

$wp_scripts->registered['wpcf7-admin']->extra['data'] = 'var wpcf7 = ' . wp_json_encode( $wpcf7 ) . ';';
$wp_scripts->registered['wpcf7-admin']->extra['before'][1] = 'var wpcf7 = ' . wp_json_encode( $wpcf7 ) . ';';
}
}
}

0 comments on commit 14b1004

Please sign in to comment.