PluginProbe
Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification / 5.5.0
Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification v5.5.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 3.0.0 3.0.1 3.0.2 3.0.3 3.0.5 3.0.51 3.0.60 3.0.61 3.0.62 3.0.70 3.0.71 3.0.72 3.1.0 All 34 releases
double-opt-in / core / CategoryOptions.class.php

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

255 lines 7.7 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 ( ! isset( $_POST['f12_cf7_doubleoptin_ui_table_options_nonce'] )
98 || ! \wp_verify_nonce( wp_unslash( $_POST['f12_cf7_doubleoptin_ui_table_options_nonce'] ), 'f12_cf7_doubleoptin_ui_table_options_action' ) ) {
99 $this->get_logger()->warning( 'Nonce verification failed during category update.', [
100 'plugin' => 'double-opt-in',
101 'nonce' => $_POST['f12_cf7_doubleoptin_ui_table_options_nonce'] ?? 'not set',
102 ] );
103 return;
104 }
105
106 // Check if opt-in IDs are set and an array
107 if ( ! isset( $_POST['optin-id'] ) || ! is_array( $_POST['optin-id'] ) ) {
108 $this->get_logger()->warning( 'Invalid or missing opt-in IDs for category update.', [
109 'plugin' => 'double-opt-in',
110 'optin-id-status' => isset($_POST['optin-id']) ? gettype($_POST['optin-id']) : 'not set',
111 ] );
112 return;
113 }
114
115 // Check if category ID is set
116 if ( ! isset( $_POST['category'] ) ) {
117 $this->get_logger()->warning( 'Category ID is missing for the update process.', [
118 'plugin' => 'double-opt-in',
119 ] );
120 return;
121 }
122
123 $category_id = (int) $_POST['category'];
124
125 $this->get_logger()->info( 'Attempting to retrieve category with ID.', [
126 'plugin' => 'double-opt-in',
127 'category_id' => $category_id,
128 ] );
129
130 $Category = Category::get_by_id( $category_id );
131
132 // Check if category exists unless ID is 0
133 if ( null == $Category && 0 !== $category_id ) {
134 $this->get_logger()->error( 'Category not found for the given ID. Aborting update.', [
135 'plugin' => 'double-opt-in',
136 'category_id' => $category_id,
137 ] );
138 return;
139 }
140
141 $optinIds = array_map( 'intval', $_POST['optin-id'] );
142 $this->get_logger()->debug( 'Found a total of ' . count($optinIds) . ' opt-ins to update.', [
143 'plugin' => 'double-opt-in',
144 'optin_ids' => $optinIds,
145 ] );
146
147 foreach ( $optinIds as $id ) {
148 $this->get_logger()->info( 'Updating opt-in ID ' . $id . ' to category ID ' . $category_id . '.', [
149 'plugin' => 'double-opt-in',
150 'optin_id' => $id,
151 'new_category_id' => $category_id,
152 ] );
153 OptIn::update_category_by_id( $id, $category_id );
154 }
155
156 $this->get_logger()->notice( 'Finished updating all specified opt-ins.', [
157 'plugin' => 'double-opt-in',
158 'total_updated' => count($optinIds),
159 ] );
160 }
161
162 /**
163 * Return an Select Field for Categories
164 *
165 * @return string
166 */
167 public function select_category( $selected = 0 ) {
168 $this->get_logger()->info( 'Generating category selection dropdown.', [
169 'plugin' => 'double-opt-in',
170 'selected_id' => $selected,
171 ] );
172
173 $atts = array(
174 'perPage' => - 1,
175 'orderBy' => 'name',
176 'order' => 'ASC'
177 );
178
179 // Log the attributes used for the category list retrieval.
180 $this->get_logger()->debug( 'Fetching full list of categories for dropdown.', [
181 'plugin' => 'double-opt-in',
182 'attributes' => $atts,
183 ] );
184
185 $list = Category::get_list( $atts, $numberOfPages );
186
187 if ( empty( $list ) ) {
188 $this->get_logger()->warning( 'No categories found to populate the dropdown.', [
189 'plugin' => 'double-opt-in',
190 ] );
191 }
192
193 $response = '<option value="0">' . __( 'Please select', 'double-opt-in' ) . '</option>';
194
195 foreach ( $list as $Category /** @var Category $Category */ ) {
196 $selected_html = $selected == $Category->get_id() ? 'selected="selected"' : '';
197 $response .= '<option value="' . esc_attr( $Category->get_id() ) . '" ' . $selected_html . '>' . esc_attr( $Category->get_name() ) . '</option>';
198
199 // Log each option added to the dropdown.
200 $this->get_logger()->debug( 'Added category option to dropdown.', [
201 'plugin' => 'double-opt-in',
202 'category_id' => $Category->get_id(),
203 'category_name' => $Category->get_name(),
204 'is_selected' => ($selected == $Category->get_id()),
205 ] );
206 }
207
208 $final_html = '<select name="category">' . $response . '</select>';
209
210 // Log the successful generation of the dropdown.
211 $this->get_logger()->info( 'Category selection dropdown successfully generated.', [
212 'plugin' => 'double-opt-in',
213 'html_length' => strlen($final_html),
214 ] );
215
216 return $final_html;
217 }
218
219 /**
220 * Add a Option to update the given Categories
221 *
222 * @return string
223 */
224 public function addOption() {
225 $this->get_logger()->info( 'Adding category selection option to UI.', [
226 'plugin' => 'double-opt-in',
227 ] );
228 ?>
229 <li>
230 <?php _e( 'Change Category to: ' ); ?>
231 <?php
232 // Log the generation of the select input.
233 $this->get_logger()->info( 'Rendering select input for category change.', [
234 'plugin' => 'double-opt-in',
235 'function' => 'select_category',
236 ] );
237
238 echo wp_kses( $this->select_category(), array(
239 'select' => array( 'name' => array() ),
240 'option' => array(
241 'value' => array(),
242 'selected' => array()
243 )
244 ) );
245 ?>
246 <input type="submit" class="button" name="update-category"
247 value="<?php _e( 'Update', 'double-opt-in' ); ?>"/>
248 </li>
249 <?php
250 $this->get_logger()->notice( 'Category selection option successfully added to the UI.', [
251 'plugin' => 'double-opt-in',
252 ] );
253 }
254 }
255 }