PluginProbe
Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification / 3.1.0
Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification v3.1.0
5.12.0 5.13.0 5.13.1 5.11.0 5.10.0 5.9.0 5.8.0 5.8.1 5.7.0 5.6.2 5.6.3 5.6.1 5.6.0 5.5.0 5.4.0 5.3.2 5.3.1 5.1.6 5.1.5 trunk 2.1.5 2.11 2.12 2.13 2.15 All 47 releases
double-opt-in / core / CategoryOptions.class.php

CategoryOptions.class.php in Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification 3.1.0, at core/CategoryOptions.class.php

254 lines 7.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace forge12\contactform7\CF7DoubleOptIn {
4
5 use Forge12\Shared\Logger;
6 use Forge12\Shared\LoggerInterface;
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 /**
13 * Class UICategories
14 *
15 * @package forge12\contactform7\CF7DoubleOptIn
16 */
17 class CategoryOptions {
18 private static $_instance = null;
19 private LoggerInterface $logger;
20
21 public static function getInstance() {
22 if ( null === self::$_instance ) {
23 self::$_instance = new CategoryOptions( Logger::getInstance() );
24 }
25
26 return self::$_instance;
27 }
28
29 private function __construct( LoggerInterface $logger ) {
30 $this->logger = $logger;
31
32 $this->get_logger()->info( 'Plugin class initialized.', [
33 'plugin' => 'double-opt-in',
34 'method' => __METHOD__,
35 ] );
36
37 // Add additional actions
38 $this->get_logger()->info( 'Adding action for UI table options.', [
39 'plugin' => 'double-opt-in',
40 'hook' => 'f12_cf7_doubleoptin_ui_table_options',
41 'callback' => 'addOption',
42 ] );
43 add_action( 'f12_cf7_doubleoptin_ui_table_options', array( $this, 'addOption' ) );
44
45 $this->get_logger()->info( 'Adding action for admin initialization.', [
46 'plugin' => 'double-opt-in',
47 'hook' => 'admin_init',
48 'callback' => 'init',
49 ] );
50 add_action( 'admin_init', array( $this, 'init' ) );
51
52 $this->get_logger()->info( 'All actions added successfully during initialization.', [
53 'plugin' => 'double-opt-in',
54 ] );
55 }
56
57 public function get_logger() {
58 return $this->logger;
59 }
60
61 /**
62 * After initializing the WordPress Admi
63 *
64 * @return void
65 */
66 public function init() {
67 $this->get_logger()->info( 'Admin init hook triggered.', [
68 'plugin' => 'double-opt-in',
69 'method' => __METHOD__,
70 ] );
71
72 if ( isset( $_POST['update-category'] ) ) {
73 $this->get_logger()->info( 'Update categories action detected via POST.', [
74 'plugin' => 'double-opt-in',
75 'post_action' => 'update-category',
76 ] );
77 $this->update_categories();
78 } else {
79 $this->get_logger()->debug( 'No category update action detected.', [
80 'plugin' => 'double-opt-in',
81 'post_action_status' => 'not set',
82 ] );
83 }
84 }
85
86 /**
87 * Assign the new categories to the selected opt ins.
88 *
89 * @return void
90 */
91 private function update_categories() {
92 $this->get_logger()->info( 'Starting update categories process.', [
93 'plugin' => 'double-opt-in',
94 ] );
95
96 // Check for nonce verification
97 if ( ! \wp_verify_nonce( $_POST['f12_cf7_doubleoptin_ui_table_options_nonce'], 'f12_cf7_doubleoptin_ui_table_options_action' ) ) {
98 $this->get_logger()->warning( 'Nonce verification failed during category update.', [
99 'plugin' => 'double-opt-in',
100 'nonce' => $_POST['f12_cf7_doubleoptin_ui_table_options_nonce'] ?? 'not set',
101 ] );
102 return;
103 }
104
105 // Check if opt-in IDs are set and an array
106 if ( ! isset( $_POST['optin-id'] ) || ! is_array( $_POST['optin-id'] ) ) {
107 $this->get_logger()->warning( 'Invalid or missing opt-in IDs for category update.', [
108 'plugin' => 'double-opt-in',
109 'optin-id-status' => isset($_POST['optin-id']) ? gettype($_POST['optin-id']) : 'not set',
110 ] );
111 return;
112 }
113
114 // Check if category ID is set
115 if ( ! isset( $_POST['category'] ) ) {
116 $this->get_logger()->warning( 'Category ID is missing for the update process.', [
117 'plugin' => 'double-opt-in',
118 ] );
119 return;
120 }
121
122 $category_id = (int) $_POST['category'];
123
124 $this->get_logger()->info( 'Attempting to retrieve category with ID.', [
125 'plugin' => 'double-opt-in',
126 'category_id' => $category_id,
127 ] );
128
129 $Category = Category::get_by_id( $category_id );
130
131 // Check if category exists unless ID is 0
132 if ( null == $Category && 0 !== $category_id ) {
133 $this->get_logger()->error( 'Category not found for the given ID. Aborting update.', [
134 'plugin' => 'double-opt-in',
135 'category_id' => $category_id,
136 ] );
137 return;
138 }
139
140 $optinIds = array_map( 'intval', $_POST['optin-id'] );
141 $this->get_logger()->debug( 'Found a total of ' . count($optinIds) . ' opt-ins to update.', [
142 'plugin' => 'double-opt-in',
143 'optin_ids' => $optinIds,
144 ] );
145
146 foreach ( $optinIds as $id ) {
147 $this->get_logger()->info( 'Updating opt-in ID ' . $id . ' to category ID ' . $category_id . '.', [
148 'plugin' => 'double-opt-in',
149 'optin_id' => $id,
150 'new_category_id' => $category_id,
151 ] );
152 OptIn::update_category_by_id( $id, $category_id );
153 }
154
155 $this->get_logger()->notice( 'Finished updating all specified opt-ins.', [
156 'plugin' => 'double-opt-in',
157 'total_updated' => count($optinIds),
158 ] );
159 }
160
161 /**
162 * Return an Select Field for Categories
163 *
164 * @return string
165 */
166 public function select_category( $selected = 0 ) {
167 $this->get_logger()->info( 'Generating category selection dropdown.', [
168 'plugin' => 'double-opt-in',
169 'selected_id' => $selected,
170 ] );
171
172 $atts = array(
173 'perPage' => - 1,
174 'orderBy' => 'name',
175 'order' => 'ASC'
176 );
177
178 // Log the attributes used for the category list retrieval.
179 $this->get_logger()->debug( 'Fetching full list of categories for dropdown.', [
180 'plugin' => 'double-opt-in',
181 'attributes' => $atts,
182 ] );
183
184 $list = Category::get_list( $atts, $numberOfPages );
185
186 if ( empty( $list ) ) {
187 $this->get_logger()->warning( 'No categories found to populate the dropdown.', [
188 'plugin' => 'double-opt-in',
189 ] );
190 }
191
192 $response = '<option value="0">' . __( 'Please select', 'double-opt-in' ) . '</option>';
193
194 foreach ( $list as $Category /** @var Category $Category */ ) {
195 $selected_html = $selected == $Category->get_id() ? 'selected="selected"' : '';
196 $response .= '<option value="' . esc_attr( $Category->get_id() ) . '" ' . $selected_html . '>' . esc_attr( $Category->get_name() ) . '</option>';
197
198 // Log each option added to the dropdown.
199 $this->get_logger()->debug( 'Added category option to dropdown.', [
200 'plugin' => 'double-opt-in',
201 'category_id' => $Category->get_id(),
202 'category_name' => $Category->get_name(),
203 'is_selected' => ($selected == $Category->get_id()),
204 ] );
205 }
206
207 $final_html = '<select name="category">' . $response . '</select>';
208
209 // Log the successful generation of the dropdown.
210 $this->get_logger()->info( 'Category selection dropdown successfully generated.', [
211 'plugin' => 'double-opt-in',
212 'html_length' => strlen($final_html),
213 ] );
214
215 return $final_html;
216 }
217
218 /**
219 * Add a Option to update the given Categories
220 *
221 * @return string
222 */
223 public function addOption() {
224 $this->get_logger()->info( 'Adding category selection option to UI.', [
225 'plugin' => 'double-opt-in',
226 ] );
227 ?>
228 <li>
229 <?php _e( 'Change Category to: ' ); ?>
230 <?php
231 // Log the generation of the select input.
232 $this->get_logger()->info( 'Rendering select input for category change.', [
233 'plugin' => 'double-opt-in',
234 'function' => 'select_category',
235 ] );
236
237 echo wp_kses( $this->select_category(), array(
238 'select' => array( 'name' => array() ),
239 'option' => array(
240 'value' => array(),
241 'selected' => array()
242 )
243 ) );
244 ?>
245 <input type="submit" class="button" name="update-category"
246 value="<?php _e( 'Update', 'double-opt-in' ); ?>"/>
247 </li>
248 <?php
249 $this->get_logger()->notice( 'Category selection option successfully added to the UI.', [
250 'plugin' => 'double-opt-in',
251 ] );
252 }
253 }
254 }