| @@ -1,118 +1,255 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace forge12\contactform7\CF7DoubleOptIn { |
| 4 | - if (!defined('ABSPATH')) { | |
| 5 | - exit; | |
| 6 | - } | |
| 7 | 4 | |
| 8 | - /** | |
| 9 | - * Class UICategories | |
| 10 | - * | |
| 11 | - * @package forge12\contactform7\CF7DoubleOptIn | |
| 12 | - */ | |
| 13 | - class CategoryOptions | |
| 14 | - { | |
| 15 | - private static $_instance = null; | |
| 5 | + use Forge12\Shared\Logger; | |
| 6 | + use Forge12\Shared\LoggerInterface; | |
| 16 | 7 | |
| 17 | - public static function getInstance() | |
| 18 | - { | |
| 19 | - if (null === self::$_instance) { | |
| 20 | - self::$_instance = new CategoryOptions(); | |
| 21 | - } | |
| 22 | - return self::$_instance; | |
| 23 | - } | |
| 8 | + if ( ! defined( 'ABSPATH' ) ) { | |
| 9 | + exit; | |
| 10 | + } | |
| 24 | 11 | |
| 25 | - private function __construct() | |
| 26 | - { | |
| 27 | - // Add additional actions | |
| 28 | - add_action('f12_cf7_doubleoptin_ui_table_options', array($this, 'addOption')); | |
| 12 | + /** | |
| 13 | + * Class UICategories | |
| 14 | + * | |
| 15 | + * @package forge12\contactform7\CF7DoubleOptIn | |
| 16 | + */ | |
| 17 | + class CategoryOptions { | |
| 18 | + private static $_instance = null; | |
| 19 | + private LoggerInterface $logger; | |
| 29 | 20 | |
| 30 | - add_action('admin_init', array($this, 'init')); | |
| 31 | - } | |
| 21 | + public static function getInstance() { | |
| 22 | + if ( null === self::$_instance ) { | |
| 23 | + self::$_instance = new CategoryOptions( Logger::getInstance() ); | |
| 24 | + } | |
| 32 | 25 | |
| 33 | - /** | |
| 34 | - * After initializing the WordPress Admi | |
| 35 | - * | |
| 36 | - * @return void | |
| 37 | - */ | |
| 38 | - public function init() | |
| 39 | - { | |
| 40 | - if (isset($_POST['update-category'])) { | |
| 41 | - $this->update_categories(); | |
| 42 | - } | |
| 43 | - } | |
| 26 | + return self::$_instance; | |
| 27 | + } | |
| 44 | 28 | |
| 45 | - /** | |
| 46 | - * Assign the new categories to the selected opt ins. | |
| 47 | - * | |
| 48 | - * @return void | |
| 49 | - */ | |
| 50 | - private function update_categories() | |
| 51 | - { | |
| 52 | - if (!\wp_verify_nonce($_POST['f12_cf7_doubleoptin_ui_table_options_nonce'], 'f12_cf7_doubleoptin_ui_table_options_action')) { | |
| 53 | - return; | |
| 54 | - } | |
| 29 | + private function __construct( LoggerInterface $logger ) { | |
| 30 | + $this->logger = $logger; | |
| 55 | 31 | |
| 56 | - if (!isset($_POST['optin-id']) || !is_array($_POST['optin-id'])) { | |
| 57 | - return; | |
| 58 | - } | |
| 32 | + $this->get_logger()->info( 'Plugin class initialized.', [ | |
| 33 | + 'plugin' => 'double-opt-in', | |
| 34 | + 'method' => __METHOD__, | |
| 35 | + ] ); | |
| 59 | 36 | |
| 60 | - if (!isset($_POST['category'])) { | |
| 61 | - return; | |
| 62 | - } | |
| 63 | - $category_id = (int)$_POST['category']; | |
| 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' ) ); | |
| 64 | 44 | |
| 65 | - $Category = Category::get_by_id($category_id); | |
| 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' ) ); | |
| 66 | 51 | |
| 67 | - if (null == $Category && 0 !== $category_id) { | |
| 68 | - return; | |
| 69 | - } | |
| 52 | + $this->get_logger()->info( 'All actions added successfully during initialization.', [ | |
| 53 | + 'plugin' => 'double-opt-in', | |
| 54 | + ] ); | |
| 55 | + } | |
| 70 | 56 | |
| 71 | - $optinIds = array_map('intval', $_POST['optin-id']); | |
| 57 | + public function get_logger() { | |
| 58 | + return $this->logger; | |
| 59 | + } | |
| 72 | 60 | |
| 73 | - foreach ($optinIds as $id) { | |
| 74 | - OptIn::update_category_by_id($id, $category_id); | |
| 75 | - } | |
| 76 | - } | |
| 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 | + ] ); | |
| 77 | 71 | |
| 78 | - /** | |
| 79 | - * Return an Select Field for Categories | |
| 80 | - * | |
| 81 | - * @return string | |
| 82 | - */ | |
| 83 | - public function select_category($selected = 0) | |
| 84 | - { | |
| 85 | - $atts = array( | |
| 86 | - 'perPage' => -1, | |
| 87 | - 'orderBy' => 'name', | |
| 88 | - 'order' => 'ASC' | |
| 89 | - ); | |
| 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 | + } | |
| 90 | 85 | |
| 91 | - $list = Category::get_list($atts, $numberOfPages); | |
| 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 | + ] ); | |
| 92 | 95 | |
| 93 | - $response = '<option value="0">' . __('Please select', 'double-opt-in') . '</option>'; | |
| 94 | - foreach ($list as $Category/** @var Category $Category */) { | |
| 95 | - $selected_html = $selected == $Category->get_id() ? 'selected="selected"' : ''; | |
| 96 | - $response .= '<option value="' . esc_attr($Category->get_id()) . '" '.$selected_html.'>' . esc_attr($Category->get_name()) . '</option>'; | |
| 97 | - } | |
| 98 | - return '<select name="category">' . $response . '</select>'; | |
| 99 | - } | |
| 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 | + } | |
| 100 | 105 | |
| 101 | - /** | |
| 102 | - * Add a Option to update the given Categories | |
| 103 | - * | |
| 104 | - * @return string | |
| 105 | - */ | |
| 106 | - public function addOption() | |
| 107 | - { | |
| 108 | - ?> | |
| 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 | + ?> | |
| 109 | 229 | <li> |
| 110 | - <?php _e('Change Category to: '); ?> | |
| 111 | - <?php echo wp_kses($this->select_category(), array('select' => array('name' => array()), 'option' => array('value' => array(), 'selected' => array()))); ?> | |
| 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 | + ?> | |
| 112 | 246 | <input type="submit" class="button" name="update-category" |
| 113 | - value="<?php _e('Update', 'double-opt-in'); ?>"/> | |
| 247 | + value="<?php _e( 'Update', 'double-opt-in' ); ?>"/> | |
| 114 | 248 | </li> |
| 115 | - <?php | |
| 116 | - } | |
| 117 | - } | |
| 249 | + <?php | |
| 250 | + $this->get_logger()->notice( 'Category selection option successfully added to the UI.', [ | |
| 251 | + 'plugin' => 'double-opt-in', | |
| 252 | + ] ); | |
| 253 | + } | |
| 254 | + } | |
| 118 | 255 | } |