Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ref. GetFieldsAny uses DTO. #534

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 20 additions & 27 deletions inc/cleantalk-common.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Cleantalk\ApbctWP\CleantalkSettingsTemplates;
use Cleantalk\ApbctWP\Cron;
use Cleantalk\ApbctWP\DB;
use Cleantalk\ApbctWP\DTO\GetFieldsAnyDTO;
use Cleantalk\ApbctWP\Firewall\SFW;
use Cleantalk\ApbctWP\GetFieldsAny;
use Cleantalk\ApbctWP\Helper;
Expand Down Expand Up @@ -1118,12 +1119,13 @@ function ct_get_fields_any($arr, $email = '', $nickname = '')
}

/**
* Get data from an ARRAY recursively
*
* @param array $input_array
* @param string $email
* @param string $nickname
* Get data as assoc array from an ARRAY recursively
*
* @see getFieldsAnyDTO to understand the structure of the result
* @param array $input_array maybe raw POST array or other preprocessed POST data.
* @param string $email email, rewriting result of process $input_array data
* @param string $nickname nickname, rewriting result of process $input_array data
* @deprecated since 6.48, use ct_gfa_dto() instead
* @return array
*/
function ct_gfa($input_array, $email = '', $nickname = '')
Expand All @@ -1133,30 +1135,21 @@ function ct_gfa($input_array, $email = '', $nickname = '')
return $gfa->getFields($email, $nickname);
}

//New ct_get_fields_any_postdata
function ct_get_fields_any_postdata($arr, $message = array())
/**
* Get data as GetFieldsAnyDTO object from an ARRAY recursively
*
* @see getFieldsAnyDTO to understand the structure of the result
* @param array $input_array maybe raw POST array or other preprocessed POST data.
* @param string $email email, rewriting result of process $input_array data
* @param string $nickname nickname, rewriting result of process $input_array data
*
* @return GetFieldsAnyDTO
*/
function ct_gfa_dto($input_array, $email = '', $nickname = '')
{
$skip_params = array(
'ipn_track_id', // PayPal IPN #
'txn_type', // PayPal transaction type
'payment_status', // PayPal payment status
);

foreach ( $arr as $key => $value ) {
if ( ! is_array($value) ) {
if ( $value == '' ) {
continue;
}
if ( ! (in_array($key, $skip_params) || preg_match("/^ct_checkjs/", $key)) && $value != '' ) {
$message[$key] = $value;
}
} else {
$temp = ct_get_fields_any_postdata($value);
$message = (count($temp) == 0 ? $message : array_merge($message, $temp));
}
}
$gfa = new GetFieldsAny($input_array);

return $message;
return $gfa->getFieldsDTO($email, $nickname);
}

/**
Expand Down
23 changes: 12 additions & 11 deletions inc/cleantalk-public-integrations.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Cleantalk\ApbctWP\DTO\GetFieldsAnyDTO;
use Cleantalk\ApbctWP\Escape;
use Cleantalk\ApbctWP\Helper;
use Cleantalk\ApbctWP\Honeypot;
Expand Down Expand Up @@ -2025,17 +2026,17 @@ function apbct_form__ninjaForms__testSpam()
$checkjs = apbct_js_test(Sanitize::cleanTextField(Cookie::get('ct_checkjs')), true);

try {
$params = apbct_form__ninjaForms__collect_fields_new();
$gfa_dto = apbct_form__ninjaForms__collect_fields_new();
} catch (\Exception $_e) {
// It is possible here check the reason if the new way collecting fields is not available.
$params = apbct_form__ninjaForms__collect_fields_old();
$gfa_dto = apbct_form__ninjaForms__collect_fields_old();
}

$sender_email = isset($params['email']) ? $params['email'] : '';
$sender_emails_array = isset($params['emails_array']) ? $params['emails_array'] : '';
$sender_nickname = isset($params['nickname']) ? $params['nickname'] : '';
$subject = isset($params['subject']) ? $params['subject'] : '';
$message = isset($params['message']) ? $params['message'] : array();
$sender_email = $gfa_dto->email;
$sender_emails_array = $gfa_dto->emails_array;
$sender_nickname = $gfa_dto->nickname;
$subject = $gfa_dto->subject;
$message = $gfa_dto->message;
if ( $subject != '' ) {
$message = array_merge(array('subject' => $subject), $message);
}
Expand Down Expand Up @@ -2096,7 +2097,7 @@ function apbct_form__ninjaForms__testSpam()
/**
* Old way to collecting NF fields data.
*
* @return array
* @return GetFieldsAnyDTO
*/
function apbct_form__ninjaForms__collect_fields_old()
{
Expand All @@ -2106,15 +2107,15 @@ function apbct_form__ninjaForms__collect_fields_old()
$input_array = apply_filters('apbct__filter_post', $_POST);

// Choosing between POST and GET
return ct_gfa(
return ct_gfa_dto(
Get::get('ninja_forms_ajax_submit') || Get::get('nf_ajax_submit') ? $_GET : $input_array
);
}

/**
* New way to collecting NF fields data - try to get username and email.
*
* @return array
* @return GetFieldsAnyDTO
* @throws Exception
* @psalm-suppress UndefinedClass
*/
Expand Down Expand Up @@ -2170,7 +2171,7 @@ function apbct_form__ninjaForms__collect_fields_new()
}
}

return ct_gfa($fields, $email, $nickname);
return ct_gfa_dto($fields, $email, $nickname);
}

/**
Expand Down
90 changes: 90 additions & 0 deletions lib/Cleantalk/ApbctWP/DTO/GetFieldsAnyDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace Cleantalk\ApbctWP\DTO;

use Cleantalk\Templates\DTO;

/**
* Class GetFieldsAnyDTO
*
* Used to correctly collect GetFieldsAny process data.
* Obligatory properties:
* <ul>
* <li>email(string)</li>
* <li>emails_array(string[])</li>
* <li>nickname(string)</li>
* <li>subject(string)</li>
* <li>contact(bool)</li>
* <li>message(string[])</li>
* </ul>
*
* To get assoc array of all properties use getArray() method.
* @since 6.48
* @version 1.0.0
* @package Cleantalk\ApbctWP\DTO
* @psalm-suppress InvalidClass
*/
class GetFieldsAnyDTO extends DTO
{
/**
* Sender email.
* @var string
*/
public $email = '';
/**
* Array of emails.
* @var array
*/
public $emails_array = array();
/**
* Nickname.
* Will be concatenated from nickname_first, nickname_last and nickname_nick if not provided during processing.
* @var string
*/
public $nickname = '';
/**
* Nickname first part.
* @var string
*/
public $nickname_first = '';
/**
* Nickname last part.
* @var string
*/
public $nickname_last = '';
/**
* Nickname nick part.
* @var string
*/
public $nickname_nick = '';
/**
* Subject.
* @var string
*/
public $subject = '';
/**
* Is contact form?
* @var bool
* @psalm-suppress PossiblyUnusedProperty
*/
public $contact = true;
/**
* Message array.
* @var array
*/
public $message = array();

protected $obligatory_properties = array(
'email',
'emails_array',
'nickname',
'subject',
'contact',
'message',
);

public function __construct($params)
{
parent::__construct($params);
}
}
Loading
Loading