| 1 |
<?php |
| 2 |
|
| 3 |
namespace forge12\contactform7\CF7DoubleOptIn { |
| 4 |
|
| 5 |
use Forge12\Shared\LoggerInterface; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
|
| 11 |
|
| 12 |
/** |
| 13 |
* Class ConditionalFields Support |
| 14 |
* Add support for conditional fields if available. |
| 15 |
* |
| 16 |
* @package forge12\contactform7\CF7DoubleOptIn |
| 17 |
*/ |
| 18 |
class CF7ConditionalFields { |
| 19 |
private $conditionalFieldsParameter = array( |
| 20 |
'_wpcf7cf_hidden_group_fields' => '', |
| 21 |
'_wpcf7cf_hidden_groups' => '', |
| 22 |
'_wpcf7cf_visible_groups' => '', |
| 23 |
'_wpcf7cf_repeaters' => '', |
| 24 |
'_wpcf7cf_steps' => '', |
| 25 |
'_wpcf7cf_options' => '' |
| 26 |
); |
| 27 |
private LoggerInterface $logger; |
| 28 |
|
| 29 |
/** |
| 30 |
* Admin constructor. |
| 31 |
*/ |
| 32 |
public function __construct( LoggerInterface $logger ) { |
| 33 |
$this->logger = $logger; |
| 34 |
|
| 35 |
$this->get_logger()->debug( 'Conditional fields constructor called', [ |
| 36 |
'plugin' => 'double-opt-in', |
| 37 |
'class' => __CLASS__, |
| 38 |
'method' => __METHOD__, |
| 39 |
] ); |
| 40 |
|
| 41 |
add_filter( |
| 42 |
'f12_cf7_doubleoptin_add_request_parameter', |
| 43 |
[ $this, '_initConditionalFieldParameter' ], |
| 44 |
10, |
| 45 |
1 |
| 46 |
); |
| 47 |
$this->get_logger()->debug( 'Filter f12_cf7_doubleoptin_add_request_parameter registered', [ |
| 48 |
'plugin' => 'double-opt-in', |
| 49 |
] ); |
| 50 |
|
| 51 |
add_filter( |
| 52 |
'f12_cf7_doubleoptin_body', |
| 53 |
[ $this, '_getOptinBody' ], |
| 54 |
10, |
| 55 |
1 |
| 56 |
); |
| 57 |
$this->get_logger()->debug( 'Filter f12_cf7_doubleoptin_body registered', [ |
| 58 |
'plugin' => 'double-opt-in', |
| 59 |
] ); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
public function get_logger() { |
| 64 |
return $this->logger; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Add the option to add conditional fields also to the optin mail. |
| 69 |
* |
| 70 |
* @return string |
| 71 |
*/ |
| 72 |
public function _getOptinBody( $body ) { |
| 73 |
$this->get_logger()->debug( '_getOptinBody called', [ |
| 74 |
'plugin' => 'double-opt-in', |
| 75 |
'class' => __CLASS__, |
| 76 |
'method' => __METHOD__, |
| 77 |
] ); |
| 78 |
|
| 79 |
if ( ! defined( 'WPCF7CF_PLUGIN' ) || ! class_exists( '\Wpcf7cfMailParser' ) ) { |
| 80 |
$this->get_logger()->debug( 'Conditional fields plugin not available, returning original body', [ |
| 81 |
'plugin' => 'double-opt-in', |
| 82 |
] ); |
| 83 |
return $body; |
| 84 |
} |
| 85 |
|
| 86 |
$CFMP = new \Wpcf7cfMailParser( |
| 87 |
$body, |
| 88 |
$this->conditionalFieldsParameter['_wpcf7cf_visible_groups'], |
| 89 |
$this->conditionalFieldsParameter['_wpcf7cf_hidden_groups'], |
| 90 |
$this->conditionalFieldsParameter['_wpcf7cf_repeaters'], |
| 91 |
[] |
| 92 |
); |
| 93 |
|
| 94 |
$parsedBody = $CFMP->getParsedMail(); |
| 95 |
|
| 96 |
$this->get_logger()->debug( 'Optin body parsed with conditional fields', [ |
| 97 |
'plugin' => 'double-opt-in', |
| 98 |
] ); |
| 99 |
|
| 100 |
return $parsedBody; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
/** |
| 105 |
* Check if conditional field plugin is available and if yes, check for the parameter |
| 106 |
* which has to be stored within the content. |
| 107 |
* |
| 108 |
* @param $parameter |
| 109 |
*/ |
| 110 |
public function _initConditionalFieldParameter( $parameter ) { |
| 111 |
$this->get_logger()->debug( '_initConditionalFieldParameter called', [ |
| 112 |
'plugin' => 'double-opt-in', |
| 113 |
'class' => __CLASS__, |
| 114 |
'method' => __METHOD__, |
| 115 |
] ); |
| 116 |
|
| 117 |
if ( ! defined( 'WPCF7CF_PLUGIN' ) ) { |
| 118 |
$this->get_logger()->debug( 'Conditional fields plugin not available, returning parameter unchanged', [ |
| 119 |
'plugin' => 'double-opt-in', |
| 120 |
] ); |
| 121 |
return $parameter; |
| 122 |
} |
| 123 |
|
| 124 |
foreach ( $this->conditionalFieldsParameter as $key => $value ) { |
| 125 |
if ( isset( $_POST[ $key ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 126 |
$parameter[ $key ] = sanitize_text_field( $_POST[ $key ] ); |
| 127 |
$this->conditionalFieldsParameter[ $key ] = sanitize_text_field( |
| 128 |
json_decode( wp_unslash( $_POST[ $key ] ) ) |
| 129 |
); |
| 130 |
|
| 131 |
$this->get_logger()->debug( 'Conditional field parameter set', [ |
| 132 |
'plugin' => 'double-opt-in', |
| 133 |
'key' => $key, |
| 134 |
] ); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
return $parameter; |
| 139 |
} |
| 140 |
} |
| 141 |
} |