Skip to content

Commit

Permalink
Switch to our own session handler
Browse files Browse the repository at this point in the history
This seems to fix some race conditions when storing _mf_captcha_code.
  • Loading branch information
lichtmetzger committed May 17, 2023
1 parent a1c76bb commit 1a316b9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion mega-forms-local-captcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Text Domain: mega-forms-local-captcha
* Domain Path: /languages
* Description: Integrates a local captcha by MobiCMS into Mega Forms.
* Version: 1.1
* Version: 1.2
* Requires at least: 6.0
* Author: Qbus Internetagentur GmbH
* Author URI: https://qbus.de
Expand Down
8 changes: 8 additions & 0 deletions src/Initialize.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@

class Initialize {
public function __construct() {
add_action( 'init', [$this, 'startSession'], 1 );

// Register translations before everything else
$translations = new Translations;
add_action( 'init', [$translations, 'register'], 5 );

add_action( 'init', [$this, 'loadMain'], 10 );
}

public function startSession() {
if ( ! session_id() ) {
session_start();
}
}

public function loadMain() {
new Settings;
new Views;
Expand Down
3 changes: 2 additions & 1 deletion src/Submissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public function validate($object) {
// If MobiCMS captcha is enabled, validate it
if (mfget_option('mobicaptcha_status', false)) {
$result = mfpost('_mf_captcha_code', $object->posted, '');
$session = mf_session()->get('_mf_captcha_code');
//$session = mf_session()->get('_mf_captcha_code');
$session = $_SESSION['_mf_captcha_code'];
$codes = array_map('strtolower', explode(',', $session));

if ($result !== null && $codes !== null) {
Expand Down
6 changes: 3 additions & 3 deletions src/Views.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function afterHiddenInputs() {
// If MobiCMS Captcha is enabled, load it
if (mfget_option('mobicaptcha_status', false)) {
$code = (string) new Code;
$storedCode = mf_session()->get('_mf_captcha_code');
$storedCode = $_SESSION['_mf_captcha_code'];

/*
* When embedding a contact form on an Elementor page, _mf_captcha_code is set multiple times,
Expand All @@ -34,9 +34,9 @@ public function afterHiddenInputs() {
*
*/
if(!$storedCode) {
mf_session()->set('_mf_captcha_code', $code);
$_SESSION['_mf_captcha_code'] = $code;
} else {
mf_session()->set('_mf_captcha_code', $storedCode.','.$code);
$_SESSION['_mf_captcha_code'] = $storedCode.','.$code;
}

echo
Expand Down

0 comments on commit 1a316b9

Please sign in to comment.