diff --git a/.tests/php/integration/AAAMainTest.php b/.tests/php/integration/AAAMainTest.php index cec7315e..53a2039c 100644 --- a/.tests/php/integration/AAAMainTest.php +++ b/.tests/php/integration/AAAMainTest.php @@ -23,7 +23,6 @@ use HCaptcha\Divi\EmailOptin; use HCaptcha\DownloadManager\DownloadManager; use HCaptcha\FluentForm\Form; -use HCaptcha\Jetpack\JetpackForm; use HCaptcha\Main; use HCaptcha\ElementorPro\HCaptchaHandler; use HCaptcha\Migrations\Migrations; @@ -1505,7 +1504,7 @@ public function dp_test_load_modules(): array { 'Jetpack' => [ [ 'jetpack_status', 'contact' ], 'jetpack/jetpack.php', - JetpackForm::class, + \HCaptcha\Jetpack\Form::class, ], 'Kadence Form' => [ [ 'kadence_status', 'form' ], diff --git a/.tests/php/integration/Jetpack/JetpackBaseTest.php b/.tests/php/integration/Jetpack/BaseTest.php similarity index 89% rename from .tests/php/integration/Jetpack/JetpackBaseTest.php rename to .tests/php/integration/Jetpack/BaseTest.php index ee32efde..3776fcf0 100644 --- a/.tests/php/integration/Jetpack/JetpackBaseTest.php +++ b/.tests/php/integration/Jetpack/BaseTest.php @@ -1,24 +1,24 @@ prepare_hcaptcha_get_verify_message( 'hcaptcha_jetpack_nonce', 'hcaptcha_jetpack' ); - $subject = new JetpackForm(); + $subject = new Form(); self::assertFalse( $subject->verify() ); self::assertTrue( $subject->verify( true ) ); @@ -84,7 +84,7 @@ public function test_jetpack_verify_not_verified(): void { $this->prepare_hcaptcha_get_verify_message( 'hcaptcha_jetpack_nonce', 'hcaptcha_jetpack', false ); - $subject = new JetpackForm(); + $subject = new Form(); self::assertEquals( $error, $subject->verify() ); self::assertNull( $this->get_protected_property( $subject, 'error_form_hash' ) ); @@ -113,7 +113,7 @@ public function test_error_message(): void { ], ]; - $subject = new JetpackForm(); + $subject = new Form(); self::assertSame( $hcaptcha_content, $subject->error_message( $hcaptcha_content ) ); @@ -163,7 +163,7 @@ static function ( $name ) { CSS; $expected = "\n"; - $subject = new JetpackForm(); + $subject = new Form(); ob_start(); diff --git a/.tests/php/integration/Jetpack/JetpackFormTest.php b/.tests/php/integration/Jetpack/FormTest.php similarity index 93% rename from .tests/php/integration/Jetpack/JetpackFormTest.php rename to .tests/php/integration/Jetpack/FormTest.php index 18fb99e1..79aa44a2 100644 --- a/.tests/php/integration/Jetpack/JetpackFormTest.php +++ b/.tests/php/integration/Jetpack/FormTest.php @@ -1,21 +1,21 @@ add_captcha( $content ) ); + self::assertSame( $expected, $subject->add_hcaptcha( $content ) ); } /** diff --git a/assets/js/admin-jetpack.js b/assets/js/admin-jetpack.js new file mode 100644 index 00000000..8d4c28a2 --- /dev/null +++ b/assets/js/admin-jetpack.js @@ -0,0 +1,21 @@ +/* global HCaptchaJetpackObject */ + +wp.hooks.addFilter( + 'hcaptcha.formSelector', + 'hcaptcha', + ( formSelector ) => { + return formSelector + ', div.jetpack-contact-form'; + } +); + +document.addEventListener( 'hCaptchaBeforeBindEvents', function() { + const buttons = [ ...document.querySelectorAll( '.wp-block .jetpack-contact-form .wp-block-jetpack-button' ) ]; + + buttons.map( ( button ) => { + const newElement = document.createElement( 'div' ); + newElement.innerHTML = HCaptchaJetpackObject.hCaptcha; + button.parentNode.insertBefore( newElement, button ); + + return button; + } ); +} ); diff --git a/src/js/hcaptcha/app.js b/src/js/hcaptcha/app.js index 4729420d..4762af8e 100644 --- a/src/js/hcaptcha/app.js +++ b/src/js/hcaptcha/app.js @@ -30,6 +30,7 @@ window.hCaptchaSubmit = () => { window.hCaptchaOnLoad = () => { function hCaptchaOnLoad() { + document.dispatchEvent( new CustomEvent( 'hCaptchaBeforeBindEvents' ) ); window.hCaptchaBindEvents(); document.dispatchEvent( new CustomEvent( 'hCaptchaLoaded' ) ); } diff --git a/src/php/Jetpack/JetpackBase.php b/src/php/Jetpack/Base.php similarity index 54% rename from src/php/Jetpack/JetpackBase.php rename to src/php/Jetpack/Base.php index 13bf6fa7..b5c33989 100644 --- a/src/php/Jetpack/JetpackBase.php +++ b/src/php/Jetpack/Base.php @@ -1,6 +1,6 @@ post_content ?? ''; + + if ( false === strpos( $content, '' ) ) { + return $status; + } + + return true; + } + + /** + * Enqueue script in admin. + * + * @return void + */ + public function admin_enqueue_scripts(): void { + if ( ! $this->is_jetpack_post_page() ) { + return; + } + + $min = hcap_min_suffix(); + + wp_enqueue_script( + self::ADMIN_HANDLE, + constant( 'HCAPTCHA_URL' ) . "/assets/js/admin-jetpack$min.js", + [ 'hcaptcha' ], + constant( 'HCAPTCHA_VERSION' ), + true + ); + + wp_localize_script( + self::ADMIN_HANDLE, + self::OBJECT, + [ + 'hCaptcha' => $this->get_hcaptcha( $this->get_args() ), + ] + ); + } + /** * Print inline styles. * @@ -152,6 +234,24 @@ public function print_inline_styles(): void { HCaptcha::css_display( $css ); } + /** + * Get hCaptcha arguments. + * + * @param string $hash Form hash. + * + * @return array + */ + protected function get_args( string $hash = '' ): array { + return [ + 'action' => self::ACTION, + 'name' => self::NAME, + 'id' => [ + 'source' => HCaptcha::get_class_source( __CLASS__ ), + 'form_id' => 'contact' . $hash, + ], + ]; + } + /** * Get form hash. * @@ -165,6 +265,17 @@ protected function get_form_hash( string $form ): string { : ''; } + /** + * Get hCaptcha. + * + * @param array $args The hCaptcha arguments. + * + * @return string + */ + protected function get_hcaptcha( array $args ): string { + return '