Skip to content

Commit

Permalink
Add hCaptcha check to General page.
Browse files Browse the repository at this point in the history
  • Loading branch information
kagg-design committed Jul 30, 2023
1 parent a4a49c9 commit 8de8eb6
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .tests/js/assets-js-files/integrations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function getDom() {
<html lang="en">
<body>
<div id="wpwrap">
<div id="hcaptcha-integrations-message"></div>
<div id="hcaptcha-message"></div>
<table class="form-table">
<tbody>
<tr class="hcaptcha-integrations-wp-status">
Expand Down
6 changes: 6 additions & 0 deletions .tests/php/unit/HCaptchaTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,12 @@ protected function get_test_general_form_fields() {
'type' => 'password',
'section' => General::SECTION_KEYS,
],
'check_config' => [
'label' => 'Check Site Config',
'type' => 'button',
'text' => 'Check',
'section' => General::SECTION_KEYS,
],
'theme' => [
'label' => 'Theme',
'type' => 'select',
Expand Down
35 changes: 35 additions & 0 deletions .tests/php/unit/Settings/GeneralTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ public function dp_test_section_callback() {
General::SECTION_KEYS,
' <h2>
General </h2>
<div id="hcaptcha-message"></div>
<p>
To use <a href="https://www.hcaptcha.com/?r=wp&utm_source=wordpress&utm_medium=wpplugin&utm_campaign=sk" target="_blank">hCaptcha</a>, please register <a href="https://www.hcaptcha.com/signup-interstitial/?r=wp&utm_source=wordpress&utm_medium=wpplugin&utm_campaign=sk" target="_blank">here</a> to get your site and secret keys. </p>
<h3 class="hcaptcha-section-keys">Keys</h3>
Expand Down Expand Up @@ -236,6 +237,8 @@ public function test_admin_enqueue_scripts() {
$plugin_url = 'http://test.test/wp-content/plugins/hcaptcha-wordpress-plugin';
$plugin_version = '1.0.0';
$min_prefix = '.min';
$ajax_url = 'https://test.test/wp-admin/admin-ajax.php';
$nonce = 'some_nonce';

$subject = Mockery::mock( General::class )->makePartial();
$subject->shouldAllowMockingProtectedMethods();
Expand All @@ -257,6 +260,38 @@ static function ( $name ) use ( $plugin_url, $plugin_version ) {
}
);

WP_Mock::userFunction( 'wp_enqueue_script' )
->with(
General::HANDLE,
$plugin_url . "/assets/js/general$min_prefix.js",
[ 'jquery' ],
$plugin_version,
true
)
->once();

WP_Mock::userFunction( 'admin_url' )
->with( 'admin-ajax.php' )
->andReturn( $ajax_url )
->once();

WP_Mock::userFunction( 'wp_create_nonce' )
->with( General::CHECK_CONFIG_ACTION )
->andReturn( $nonce )
->once();

WP_Mock::userFunction( 'wp_localize_script' )
->with(
General::HANDLE,
General::OBJECT,
[
'ajaxUrl' => $ajax_url,
'action' => General::CHECK_CONFIG_ACTION,
'nonce' => $nonce,
]
)
->once();

WP_Mock::userFunction( 'wp_enqueue_style' )
->with(
General::HANDLE,
Expand Down
2 changes: 1 addition & 1 deletion .tests/php/unit/Settings/IntegrationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public function dp_test_section_callback() {
'',
' <h2>
Integrations </h2>
<div id="hcaptcha-integrations-message"></div>
<div id="hcaptcha-message"></div>
<p>
Manage integrations with popular plugins such as Contact Form 7, WPForms, Gravity Forms, and more. </p>
<p>
Expand Down
29 changes: 0 additions & 29 deletions assets/css/integrations.css
Original file line number Diff line number Diff line change
@@ -1,32 +1,3 @@
.hcaptcha-integrations-success,
.hcaptcha-integrations-error {
background: #fff;
box-shadow: 0 1px 1px rgb(0 0 0 / 4%);
margin: 5px 0 15px 0;
padding: 1px 12px;
border: 1px solid #c3c4c7;
border-left-width: 4px;
}

#hcaptcha-integrations-message {
z-index: 1;
}

#hcaptcha-integrations-message p {
font-size: 13px;
font-weight: 600;
line-height: 1.5;
margin: 0.7em 0;
}

.hcaptcha-integrations-success {
border-left-color: #00a32a;
}

.hcaptcha-integrations-error {
border-left-color: #d63638;
}

.hcaptcha-integrations table tbody {
display: grid;
grid-template-columns: repeat(4, minmax(100px, 1fr));
Expand Down
29 changes: 29 additions & 0 deletions assets/css/settings-base.css
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,35 @@
color: #fff;
}

.hcaptcha-success,
.hcaptcha-error {
background: #fff;
box-shadow: 0 1px 1px rgb(0 0 0 / 4%);
margin: 5px 0 15px 0;
padding: 1px 12px;
border: 1px solid #c3c4c7;
border-left-width: 4px;
}

#hcaptcha-message {
z-index: 1;
}

#hcaptcha-message p {
font-size: 13px;
font-weight: 600;
line-height: 1.5;
margin: 0.7em 0;
}

.hcaptcha-success {
border-left-color: #00a32a;
}

.hcaptcha-error {
border-left-color: #d63638;
}

@media (max-width: 600px) {
#hcaptcha-options table tbody {
grid-template-columns: 1fr;
Expand Down
57 changes: 57 additions & 0 deletions assets/js/general.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* global jQuery, HCaptchaGeneralObject */

const general = function( $ ) {
const msgSelector = '#hcaptcha-message';
const $message = $( msgSelector );

function clearMessage() {
$message.removeClass();
$message.html( '' );
}

function showMessage( message, msgClass ) {
$message.removeClass();
$message.addClass( msgClass );
$message.html( `<p>${ message }</p>` );
$message.show();
}

function showSuccessMessage( response ) {
showMessage( response, 'hcaptcha-success' );
}

function showErrorMessage( response ) {
showMessage( response, 'hcaptcha-error' );
}

$( '#check_config' ).on( 'click', function( event ) {
event.preventDefault();
clearMessage();

const data = {
action: HCaptchaGeneralObject.action,
nonce: HCaptchaGeneralObject.nonce,
};

// noinspection JSVoidFunctionReturnValueUsed
$.post( {
url: HCaptchaGeneralObject.ajaxUrl,
data,
} )
.done( function( response ) {
if ( ! response.success ) {
showErrorMessage( response.data );
return;
}

showSuccessMessage( response.data );
} )
.fail( function( response ) {
showErrorMessage( response.statusText );
} );
} );
};

window.hCaptchaGeneral = general;

jQuery( document ).ready( general );
7 changes: 4 additions & 3 deletions assets/js/integrations.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* global jQuery, HCaptchaIntegrationsObject */

const integrations = function( $ ) {
const msgSelector = '#hcaptcha-integrations-message';
const msgSelector = '#hcaptcha-message';
const $message = $( msgSelector );
const $wpwrap = $( '#wpwrap' );
const $adminmenuwrap = $( '#adminmenuwrap' );
Expand Down Expand Up @@ -37,11 +37,11 @@ const integrations = function( $ ) {
}

function showSuccessMessage( response ) {
showMessage( response, 'hcaptcha-integrations-success' );
showMessage( response, 'hcaptcha-success' );
}

function showErrorMessage( response ) {
showMessage( response, 'hcaptcha-integrations-error' );
showMessage( response, 'hcaptcha-error' );
}

function insertIntoTable( $table, key, $element ) {
Expand Down Expand Up @@ -106,6 +106,7 @@ const integrations = function( $ ) {

$tr.addClass( activateClass );

// noinspection JSVoidFunctionReturnValueUsed
$.post( {
url: HCaptchaIntegrationsObject.ajaxUrl,
data,
Expand Down
21 changes: 21 additions & 0 deletions src/php/Settings/Abstracts/SettingsBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,26 @@ private function print_table_field( array $arguments ) {
echo '</fieldset>';
}

/**
* Print button field.
*
* @param array $arguments Field arguments.
*
* @noinspection PhpUnusedPrivateMethodInspection
*/
private function print_button_field( array $arguments ) {
$disabled = $arguments['disabled'] ?? '';
$field_id = $arguments['field_id'] ?? '';
$text = $arguments['text'] ?? '';

printf(
'<button %1$s id="%2$s" class="button button-secondary" type="button"/>%3$s</button>',
disabled( $disabled, true, false ),
esc_attr( $field_id ),
esc_attr( $text )
);
}

/**
* Output settings field.
*
Expand All @@ -1045,6 +1065,7 @@ public function field_callback( array $arguments ) {
'select' => 'print_select_field',
'multiple' => 'print_multiple_select_field',
'table' => 'print_table_field',
'button' => 'print_button_field',
];

$type = $arguments['type'] ?? '';
Expand Down
Loading

0 comments on commit 8de8eb6

Please sign in to comment.