| 1 |
<?php |
| 2 |
|
| 3 |
namespace Codelight\GDPR\Modules\ContactForm7; |
| 4 |
|
| 5 |
use Codelight\GDPR\Components\Consent\ConsentManager; |
| 6 |
use Codelight\GDPR\DataSubject\DataSubjectManager; |
| 7 |
|
| 8 |
class ContactForm7 |
| 9 |
{ |
| 10 |
public function __construct(DataSubjectManager $dataSubjectManager, ConsentManager $consentManager) |
| 11 |
{ |
| 12 |
$this->dataSubjectManager = $dataSubjectManager; |
| 13 |
$this->consentManager = $consentManager; |
| 14 |
|
| 15 |
//add_action('wpcf7_init', [$this, 'addFormTags'], 10, 3); |
| 16 |
//add_action('wpcf7_admin_init', [$this, 'addFormTagGenerators'], 9999, 3); |
| 17 |
|
| 18 |
add_action('wpcf7_before_send_mail', [$this, 'processFormSubmission'], 10, 3); |
| 19 |
} |
| 20 |
|
| 21 |
public function addFormTags() |
| 22 |
{ |
| 23 |
wpcf7_add_form_tag( |
| 24 |
['gdpr_consent_text'], |
| 25 |
[$this, 'renderPrivacyTag'] |
| 26 |
); |
| 27 |
} |
| 28 |
|
| 29 |
public function addFormTagGenerators() |
| 30 |
{ |
| 31 |
$generator = \WPCF7_TagGenerator::get_instance(); |
| 32 |
|
| 33 |
$generator->add( |
| 34 |
'gdpr_privacy', |
| 35 |
_x('gdpr terms txt', '(Admin)', 'gdpr-framework'), |
| 36 |
[$this, 'generatePrivacyTag'] |
| 37 |
); |
| 38 |
} |
| 39 |
|
| 40 |
public function generatePrivacyTag($contactForm, $args) |
| 41 |
{ |
| 42 |
//$args = wp_parse_args( $args, array() ); |
| 43 |
//$description = _x( "Generate the default text for your Privacy Policy acceptance checkbox. For more details, see %s.", '(Admin)', 'gdpr-framework' ); |
| 44 |
//$descLink = wpcf7_link( _x( 'https://TODO', '(Admin)', 'gdpr-framework' ), _x( 'GDPR Terms', '(Admin)', 'gdpr-framework' ) ); |
| 45 |
//$content = $this->renderPrivacyTag(); |
| 46 |
|
| 47 |
echo gdpr('view')->render( |
| 48 |
'modules/contact-form-7/generator-privacy', |
| 49 |
compact('description', 'descLink', 'args', 'content') |
| 50 |
); |
| 51 |
} |
| 52 |
|
| 53 |
public function renderPrivacyTag() |
| 54 |
{ |
| 55 |
global $gdpr; |
| 56 |
$privacyPolicyUrl = get_permalink($gdpr->Options->get('policy_page')); |
| 57 |
add_filter( 'gdpr_custom_policy_link', 'gdprfPrivacyPolicyurl' ); |
| 58 |
$privacyPolicyUrl = apply_filters( 'gdpr_custom_policy_link',$privacyPolicyUrl); |
| 59 |
return gdpr('view')->render( |
| 60 |
'modules/contact-form-7/content-privacy', |
| 61 |
compact('privacyPolicyUrl') |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
public function processFormSubmission(\WPCF7_ContactForm $form, $abort, \WPCF7_Submission $submission) |
| 66 |
{ |
| 67 |
$consents = $this->findConsents($form, $submission); |
| 68 |
|
| 69 |
if (!count($consents)) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
$email = $this->findEmail($form,$submission); |
| 74 |
|
| 75 |
if (!$email) { |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
$dataSubject = $this->dataSubjectManager->getByEmail($email); |
| 80 |
|
| 81 |
foreach ($consents as $consent) { |
| 82 |
$dataSubject->giveConsent($consent); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
public function findConsents(\WPCF7_ContactForm $form, \WPCF7_Submission $submission) |
| 87 |
{ |
| 88 |
$consents = []; |
| 89 |
|
| 90 |
foreach ($form->scan_form_tags() as $tag) { |
| 91 |
/* @var $tag \WPCF7_FormTag */ |
| 92 |
if ('acceptance' === $tag->type && $submission->get_posted_data()[$tag->name] && $this->consentManager->isRegisteredConsent($tag->name)) { |
| 93 |
$consents[] = $tag->name; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
return $consents; |
| 98 |
} |
| 99 |
|
| 100 |
public function findEmail($form_id,\WPCF7_Submission $submission) |
| 101 |
{ |
| 102 |
$email_key = get_post_meta($form_id->id(), 'gdpr_cf7_email_field', true ); |
| 103 |
if (isset($submission->get_posted_data()['your-email'])) { |
| 104 |
return $submission->get_posted_data()['your-email']; |
| 105 |
} |
| 106 |
if(isset($email_key)){ |
| 107 |
if($email_key!=""){ |
| 108 |
if (isset($submission->get_posted_data()[$email_key])) { |
| 109 |
return $submission->get_posted_data()[$email_key]; |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|