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 '
' . HCaptcha::form( $args ) . '
'; + } + /** * Get form hash. * @@ -177,4 +288,13 @@ private function get_submitted_form_hash(): ?string { : null; // phpcs:enable WordPress.Security.NonceVerification.Missing } + + /** + * Check if the current page is a post page containing a Jetpack form. + * + * @return bool + */ + private function is_jetpack_post_page(): bool { + return true; + } } diff --git a/src/php/Jetpack/JetpackForm.php b/src/php/Jetpack/Form.php similarity index 71% rename from src/php/Jetpack/JetpackForm.php rename to src/php/Jetpack/Form.php index d6033d57..058e4862 100644 --- a/src/php/Jetpack/JetpackForm.php +++ b/src/php/Jetpack/Form.php @@ -1,18 +1,16 @@ get_form_hash( $matches[0] ); - - $args = [ - 'action' => self::ACTION, - 'name' => self::NAME, - 'id' => [ - 'source' => HCaptcha::get_class_source( __CLASS__ ), - 'form_id' => 'contact' . $hash, - ], - ]; - - $hcaptcha = '
' . HCaptcha::form( $args ) . '
'; + $hash = $this->get_form_hash( $matches[0] ); + $args = $this->get_args( $hash ); + $hcaptcha = $this->get_hcaptcha( $args ); if ( false !== strpos( $matches[0], $hcaptcha ) ) { return $matches[0]; diff --git a/src/php/Main.php b/src/php/Main.php index 5dff55ee..e412de55 100644 --- a/src/php/Main.php +++ b/src/php/Main.php @@ -25,7 +25,6 @@ use HCaptcha\Helpers\HCaptcha; use HCaptcha\Helpers\Pages; use HCaptcha\Helpers\Request; -use HCaptcha\Jetpack\JetpackForm; use HCaptcha\Migrations\Migrations; use HCaptcha\NF\NF; use HCaptcha\Quform\Quform; @@ -1083,7 +1082,7 @@ public function load_modules(): void { 'Jetpack' => [ [ 'jetpack_status', 'contact' ], 'jetpack/jetpack.php', - JetpackForm::class, + Jetpack\Form::class, ], 'Kadence Form' => [ [ 'kadence_status', 'form' ],