Skip to content

Commit f00ebb8

Browse files
committed
Add Tutor LMS integration.
1 parent 7f95e56 commit f00ebb8

File tree

9 files changed

+396
-2
lines changed

9 files changed

+396
-2
lines changed

assets/images/logo/tutor-lms.svg

Lines changed: 34 additions & 0 deletions
Loading

readme.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,10 @@ Theme My Login
307307
`$source: 'theme-my-login/theme-my-login.php'`
308308
`$form_id: 'login', 'lost_password' or 'register'`
309309

310+
Tutor LMS
311+
`$source: 'tutor/tutor.php'`
312+
`$form_id: 'checkout', ''login', 'lost_password' or 'register'`
313+
310314
Ultimate Member
311315
`$source: 'ultimate-member/ultimate-member.php'`
312316
`$form_id: form_id or 'password'`
@@ -567,6 +571,7 @@ If this feature is enabled, anonymized statistics on your plugin configuration,
567571
* Subscriber Form
568572
* Support Candy New Ticket Form
569573
* Theme My Login — Login, Lost Password, and Register Form
574+
* Tutor LMS — Checkout, Login, Lost Password, and Register Form
570575
* Ultimate Member Login, Lost Password, and Member Register Forms
571576
* UsersWP Forgot Password, Login, and Register Forms
572577
* WooCommerce Login, Registration, Lost Password, Checkout, and Order Tracking Forms
@@ -599,6 +604,7 @@ Instructions for popular native integrations are below:
599604
== Changelog ==
600605

601606
= 4.9.0 =
607+
* Added Tutor LMS integration.
602608
* Added compatibility with Ninja Forms v3.8.22.
603609
* Added the ability to install plugins from the Integrations page.
604610
* Fixed layout of a modern Jetpack form in outlined and animated styles.

src/php/LearnDash/Register.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ class Register {
1818
/**
1919
* Nonce action.
2020
*/
21-
private const ACTION = 'hcaptcha_theme_my_login_register';
21+
private const ACTION = 'hcaptcha_learn_dash_register';
2222

2323
/**
2424
* Nonce name.
2525
*/
26-
private const NONCE = 'hcaptcha_theme_my_login_register_nonce';
26+
private const NONCE = 'hcaptcha_learn_dash_register_nonce';
2727

2828
/**
2929
* Constructor.

src/php/Main.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,6 +1283,26 @@ public function load_modules(): void {
12831283
'theme-my-login/theme-my-login.php',
12841284
ThemeMyLogin\Register::class,
12851285
],
1286+
'Tutor Checkout' => [
1287+
[ 'tutor_status', 'checkout' ],
1288+
'tutor/tutor.php',
1289+
Tutor\Checkout::class,
1290+
],
1291+
'Tutor LMS Login' => [
1292+
[ 'tutor_status', 'login' ],
1293+
'tutor/tutor.php',
1294+
Tutor\Login::class,
1295+
],
1296+
'Tutor LMS LostPassword' => [
1297+
[ 'tutor_status', 'lost_pass' ],
1298+
'tutor/tutor.php',
1299+
Tutor\LostPassword::class,
1300+
],
1301+
'Tutor LMS Register' => [
1302+
[ 'tutor_status', 'register' ],
1303+
'tutor/tutor.php',
1304+
Tutor\Register::class,
1305+
],
12861306
'Ultimate Member Login' => [
12871307
[ 'ultimate_member_status', 'login' ],
12881308
'ultimate-member/ultimate-member.php',

src/php/Settings/Integrations.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,17 @@ public function init_form_fields(): void {
598598
'register' => __( 'Register Form', 'hcaptcha-for-forms-and-more' ),
599599
],
600600
],
601+
'tutor_status' => [
602+
'label' => 'Tutor LMS',
603+
'logo' => 'svg',
604+
'type' => 'checkbox',
605+
'options' => [
606+
'checkout' => __( 'Checkout Form', 'hcaptcha-for-forms-and-more' ),
607+
'login' => __( 'Login Form', 'hcaptcha-for-forms-and-more' ),
608+
'lost_pass' => __( 'Lost Password Form', 'hcaptcha-for-forms-and-more' ),
609+
'register' => __( 'Register Form', 'hcaptcha-for-forms-and-more' ),
610+
],
611+
],
601612
'ultimate_member_status' => [
602613
'label' => 'Ultimate Member',
603614
'type' => 'checkbox',

src/php/Tutor/Checkout.php

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
/**
3+
* Checkout class file.
4+
*
5+
* @package hcaptcha-wp
6+
*/
7+
8+
namespace HCaptcha\Tutor;
9+
10+
use HCaptcha\Helpers\HCaptcha;
11+
use Tutor\Ecommerce\CheckoutController;
12+
13+
/**
14+
* Class Checkout
15+
*/
16+
class Checkout {
17+
18+
/**
19+
* Nonce action.
20+
*/
21+
private const ACTION = 'hcaptcha_tutor_checkout';
22+
23+
/**
24+
* Nonce name.
25+
*/
26+
private const NONCE = 'hcaptcha_tutor_checkout_nonce';
27+
28+
/**
29+
* Constructor.
30+
*/
31+
public function __construct() {
32+
$this->init_hooks();
33+
}
34+
35+
/**
36+
* Init hooks.
37+
*
38+
* @return void
39+
*/
40+
private function init_hooks(): void {
41+
add_action( 'tutor_load_template_before', [ $this, 'template_before' ], 10, 2 );
42+
add_action( 'tutor_load_template_after', [ $this, 'template_after' ], 10, 2 );
43+
add_action( 'tutor_action_tutor_pay_now', [ $this, 'verify' ], 0 );
44+
}
45+
46+
/**
47+
* Before template.
48+
*
49+
* @param string|mixed $template The template.
50+
* @param array|mixed $variables The variables.
51+
*
52+
* @noinspection PhpUnusedParameterInspection
53+
*/
54+
public function template_before( $template, $variables ): void {
55+
$template = (string) $template;
56+
57+
if ( 'ecommerce.checkout' !== $template ) {
58+
return;
59+
}
60+
61+
ob_start();
62+
}
63+
64+
/**
65+
* After template.
66+
*
67+
* @param string|mixed $template The template.
68+
* @param array|mixed $variables The variables.
69+
*
70+
* @noinspection PhpUnusedParameterInspection*/
71+
public function template_after( $template, $variables ): void {
72+
$template = (string) $template;
73+
74+
if ( 'ecommerce.checkout' !== $template ) {
75+
return;
76+
}
77+
78+
$template = ob_get_clean();
79+
80+
$args = [
81+
'action' => self::ACTION,
82+
'name' => self::NONCE,
83+
'id' => [
84+
'source' => HCaptcha::get_class_source( __CLASS__ ),
85+
'form_id' => 'checkout',
86+
],
87+
];
88+
89+
$hcaptcha = HCaptcha::form( $args );
90+
91+
$search = '<button type="submit"';
92+
$replace = $hcaptcha . "\n" . $search;
93+
$template = str_replace( $search, $replace, $template );
94+
95+
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
96+
echo $template;
97+
}
98+
99+
/**
100+
* Verify checkout form.
101+
*
102+
* @return void
103+
*/
104+
public function verify(): void {
105+
$error_message = hcaptcha_get_verify_message(
106+
self::NONCE,
107+
self::ACTION
108+
);
109+
110+
if ( null !== $error_message ) {
111+
$current_user_id = get_current_user_id();
112+
113+
set_transient( CheckoutController::PAY_NOW_ERROR_TRANSIENT_KEY . $current_user_id, [ $error_message ] );
114+
remove_all_actions( 'tutor_action_tutor_pay_now' );
115+
}
116+
}
117+
}

src/php/Tutor/Login.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Login class file.
4+
*
5+
* @package hcaptcha-wp
6+
*/
7+
8+
namespace HCaptcha\Tutor;
9+
10+
use HCaptcha\Abstracts\LoginBase;
11+
12+
/**
13+
* Class Login.
14+
*/
15+
class Login extends LoginBase {
16+
17+
/**
18+
* Init hooks.
19+
*
20+
* @return void
21+
*/
22+
protected function init_hooks(): void {
23+
parent::init_hooks();
24+
25+
add_action( 'tutor_login_form_middle', [ $this, 'add_captcha' ] );
26+
add_filter( 'wp_authenticate_user', [ $this, 'login_base_verify' ], PHP_INT_MAX, 2 );
27+
}
28+
}

src/php/Tutor/LostPassword.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
/**
3+
* LostPassword class file.
4+
*
5+
* @package hcaptcha-wp
6+
*/
7+
8+
namespace HCaptcha\Tutor;
9+
10+
use HCaptcha\Helpers\HCaptcha;
11+
use WP_Error;
12+
13+
/**
14+
* Class LostPassword.
15+
*/
16+
class LostPassword {
17+
18+
/**
19+
* Nonce action.
20+
*/
21+
private const ACTION = 'hcaptcha_tutor_lost_password';
22+
23+
/**
24+
* Nonce name.
25+
*/
26+
private const NONCE = 'hcaptcha_tutor_lost_password_nonce';
27+
28+
/**
29+
* Constructor.
30+
*/
31+
public function __construct() {
32+
$this->init_hooks();
33+
}
34+
35+
/**
36+
* Init hooks.
37+
*
38+
* @return void
39+
*/
40+
protected function init_hooks(): void {
41+
add_action( 'tutor_lostpassword_form', [ $this, 'add_hcaptcha' ] );
42+
add_filter( 'tutor_before_retrieve_password_form_process', [ $this, 'verify' ] );
43+
}
44+
45+
/**
46+
* Add hCaptcha to the Lost Password form.
47+
*
48+
* @return void
49+
*/
50+
public function add_hcaptcha(): void {
51+
$args = [
52+
'action' => HCAPTCHA_ACTION,
53+
'name' => HCAPTCHA_NONCE,
54+
'id' => [
55+
'source' => HCaptcha::get_class_source( __CLASS__ ),
56+
'form_id' => 'lost_password',
57+
],
58+
];
59+
60+
?>
61+
<div class="tutor-form-row">
62+
<div class="tutor-form-col-12">
63+
<div class="tutor-form-group">
64+
<?php HCaptcha::form_display( $args ); ?>
65+
</div>
66+
</div>
67+
</div>
68+
<?php
69+
}
70+
71+
/**
72+
* Verify hCaptcha.
73+
*
74+
* @param WP_Error|null|mixed $errors A WP_Error object containing any errors encountered during registration.
75+
* @return WP_Error|null|mixed
76+
*/
77+
public function verify( $errors ) {
78+
$error_message = hcaptcha_verify_post(
79+
self::NONCE,
80+
self::ACTION
81+
);
82+
83+
if ( ! $error_message ) {
84+
return $errors;
85+
}
86+
87+
return HCaptcha::add_error_message( $errors, $error_message );
88+
}
89+
}

0 commit comments

Comments
 (0)