| 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 |
* Class UICategories |
| 13 |
* Show the Category and Assigned DOIs |
| 14 |
* |
| 15 |
* @package forge12\contactform7\CF7DoubleOptIn |
| 16 |
*/ |
| 17 |
class UICategoriesView extends UIPage { |
| 18 |
/** |
| 19 |
* Class constructor. |
| 20 |
* |
| 21 |
* @param TemplateHandler $templateHandler The template handler object. |
| 22 |
* @param string $domain The domain string. |
| 23 |
*/ |
| 24 |
public function __construct(LoggerInterface $logger, TemplateHandler $templateHandler, string $domain ) { |
| 25 |
|
| 26 |
parent::__construct($logger, $templateHandler, $domain, 'categories_view', __('Category View', 'double-opt-in') ); |
| 27 |
|
| 28 |
add_action( 'admin_init', array( $this, 'maybeUpdateCategory' ) ); |
| 29 |
$this->get_logger()->debug( 'Added "maybeUpdateCategory" method to "admin_init" action.', [ |
| 30 |
'plugin' => $domain, |
| 31 |
] ); |
| 32 |
} |
| 33 |
|
| 34 |
public function maybeUpdateCategory() { |
| 35 |
$this->get_logger()->info( 'Attempting to update a category.', [ |
| 36 |
'plugin' => $this->domain, |
| 37 |
] ); |
| 38 |
|
| 39 |
// Check for nonce and verify it for security |
| 40 |
if ( isset( $_POST['doi_settings_nonce'] ) && ! wp_verify_nonce( $_POST['doi_settings_nonce'], 'doi_settings_action' ) ) { |
| 41 |
$this->get_logger()->error( 'Nonce verification failed. Aborting category update.', [ |
| 42 |
'plugin' => $this->domain, |
| 43 |
] ); |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
// Check if the form was submitted by checking for the specific submit button's name |
| 48 |
if ( ! isset( $_POST['doubleoptin-settings-edit-submit'] ) ) { |
| 49 |
$this->get_logger()->debug( 'Form not submitted via the edit button. Aborting.', [ |
| 50 |
'plugin' => $this->domain, |
| 51 |
] ); |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
// Ensure the category ID is present |
| 56 |
if ( ! isset( $_POST['category_id'] ) ) { |
| 57 |
$this->get_logger()->warning( 'Category ID not found in the POST data. Aborting.', [ |
| 58 |
'plugin' => $this->domain, |
| 59 |
] ); |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
$category_id = (int) $_POST['category_id']; |
| 64 |
$this->get_logger()->debug( 'Processing request to update category with ID: ' . $category_id, [ |
| 65 |
'plugin' => $this->domain, |
| 66 |
] ); |
| 67 |
|
| 68 |
// Retrieve the existing category object from the database |
| 69 |
$Category = Category::get_by_id( $category_id ); |
| 70 |
|
| 71 |
if ( null === $Category ) { |
| 72 |
$this->get_logger()->error( 'Category with ID ' . $category_id . ' not found in the database. Aborting update.', [ |
| 73 |
'plugin' => $this->domain, |
| 74 |
] ); |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
// Sanitize and validate the new category name |
| 79 |
$name = sanitize_text_field( $_POST['categorie_name'] ); |
| 80 |
|
| 81 |
if ( empty( $name ) ) { |
| 82 |
$this->get_logger()->error( 'New category name is empty after sanitization. Aborting update.', [ |
| 83 |
'plugin' => $this->domain, |
| 84 |
] ); |
| 85 |
Messages::getInstance()->add( __( 'Category name cannot be empty.', 'double-opt-in' ), 'error' ); |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
// Set the new name and attempt to save the changes |
| 90 |
$this->get_logger()->debug( 'Updating category name from "' . $Category->get_name() . '" to "' . $name . '".', [ |
| 91 |
'plugin' => $this->domain, |
| 92 |
] ); |
| 93 |
$Category->set_name( $name ); |
| 94 |
|
| 95 |
if ( $Category->save() ) { |
| 96 |
Messages::getInstance()->add( __( 'Category updated', 'double-opt-in' ), 'success' ); |
| 97 |
$this->get_logger()->notice( 'Category with ID ' . $category_id . ' updated successfully.', [ |
| 98 |
'plugin' => $this->domain, |
| 99 |
] ); |
| 100 |
} else { |
| 101 |
Messages::getInstance()->add( __( 'Category update failed', 'double-opt-in' ), 'error' ); |
| 102 |
$this->get_logger()->error( 'Failed to save the updated category to the database.', [ |
| 103 |
'plugin' => $this->domain, |
| 104 |
'category_id' => $category_id, |
| 105 |
] ); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Hide this page in the menu. |
| 111 |
* |
| 112 |
* @return bool |
| 113 |
*/ |
| 114 |
public function hideInMenu() { |
| 115 |
return true; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Render the license subpage content |
| 120 |
*/ |
| 121 |
protected function theContent( $slug, $page, $settings ) { |
| 122 |
$this->get_logger()->info( 'Rendering the main content for the "Category View" UI page.', [ |
| 123 |
'plugin' => $this->domain, |
| 124 |
'page_slug' => $page, |
| 125 |
] ); |
| 126 |
|
| 127 |
/* |
| 128 |
* Ensure that the ID for the Categorie is set |
| 129 |
*/ |
| 130 |
if ( ! isset( $_GET['id'] ) ) { |
| 131 |
$this->get_logger()->error( 'Category ID not provided in URL. Redirecting to main page.', [ |
| 132 |
'plugin' => $this->domain, |
| 133 |
] ); |
| 134 |
wp_redirect( admin_url( 'admin.php' ) . '?page=f12-cf7-doubleoptin' ); |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
$id = (int) $_GET['id']; |
| 139 |
$this->get_logger()->debug( 'Category ID from URL: ' . $id, [ |
| 140 |
'plugin' => $this->domain, |
| 141 |
] ); |
| 142 |
|
| 143 |
/* |
| 144 |
* Load the assigned Category |
| 145 |
*/ |
| 146 |
$Category = Category::get_by_id( $id ); |
| 147 |
|
| 148 |
if ( null === $Category ) { |
| 149 |
$this->get_logger()->error( 'Category with ID ' . $id . ' not found. Redirecting to main page.', [ |
| 150 |
'plugin' => $this->domain, |
| 151 |
] ); |
| 152 |
wp_redirect( admin_url( 'admin.php' ) . '?page=f12-cf7-doubleoptin' ); |
| 153 |
return; |
| 154 |
} |
| 155 |
|
| 156 |
$this->get_logger()->notice( 'Category found: ' . $Category->get_name(), [ |
| 157 |
'plugin' => $this->domain, |
| 158 |
'category_id' => $id, |
| 159 |
] ); |
| 160 |
|
| 161 |
$atts = array( |
| 162 |
'perPage' => 10, |
| 163 |
'page' => 1, |
| 164 |
'order' => 'DESC', |
| 165 |
); |
| 166 |
|
| 167 |
// Set pagination and search attributes from URL parameters |
| 168 |
if ( isset( $_GET['pageNum'] ) ) { |
| 169 |
$atts['page'] = (int) $_GET['pageNum']; |
| 170 |
} |
| 171 |
|
| 172 |
if ( isset( $_GET['perPage'] ) ) { |
| 173 |
$atts['perPage'] = (int) $_GET['perPage']; |
| 174 |
} |
| 175 |
|
| 176 |
if ( isset( $_GET['keyword'] ) ) { |
| 177 |
$atts['keyword'] = sanitize_text_field( $_GET['keyword'] ); |
| 178 |
} |
| 179 |
|
| 180 |
$this->get_logger()->debug( 'Query attributes for OptIns: ' . json_encode($atts), [ |
| 181 |
'plugin' => $this->domain, |
| 182 |
] ); |
| 183 |
|
| 184 |
$numberOfPages = 0; |
| 185 |
$listOfOptIns = OptIn::get_list_by_category_id( $Category->get_id(), $atts, $numberOfPages ); |
| 186 |
|
| 187 |
$this->get_logger()->info( 'Found ' . count($listOfOptIns) . ' OptIns across ' . $numberOfPages . ' pages for this category.', [ |
| 188 |
'plugin' => $this->domain, |
| 189 |
] ); |
| 190 |
|
| 191 |
/* |
| 192 |
* Render the Template |
| 193 |
*/ |
| 194 |
?> |
| 195 |
<h1><?php _e( 'Category: ', 'double-opt-in' ) . esc_html( $Category->get_name() ); ?></h1> |
| 196 |
<?php |
| 197 |
$this->get_logger()->info( 'Rendering the "list-optins" template.', [ |
| 198 |
'plugin' => $this->domain, |
| 199 |
] ); |
| 200 |
|
| 201 |
$this->TemplateHandler->renderTemplate( 'list-optins', array( |
| 202 |
'domain' => $this->domain, |
| 203 |
'listOfOptIns' => $listOfOptIns, |
| 204 |
'numberOfPages' => $numberOfPages, |
| 205 |
'currentPage' => $atts['page'], |
| 206 |
'slug' => $slug |
| 207 |
) ); |
| 208 |
|
| 209 |
$this->get_logger()->notice( 'Content rendering for "Category View" completed successfully.', [ |
| 210 |
'plugin' => $this->domain, |
| 211 |
] ); |
| 212 |
} |
| 213 |
|
| 214 |
protected function theSidebar( $slug, $page ) { |
| 215 |
$this->get_logger()->info( 'Rendering the sidebar content for the "Category View" UI page.', [ |
| 216 |
'plugin' => $this->domain, |
| 217 |
'page_slug' => $page, |
| 218 |
] ); |
| 219 |
|
| 220 |
/* |
| 221 |
* Ensure that the ID for the Categorie is set |
| 222 |
*/ |
| 223 |
if ( ! isset( $_GET['id'] ) ) { |
| 224 |
$this->get_logger()->error( 'Category ID not provided in URL for sidebar rendering. Redirecting.', [ |
| 225 |
'plugin' => $this->domain, |
| 226 |
] ); |
| 227 |
wp_redirect( admin_url( 'admin.php' ) . '?page=f12-cf7-doubleoptin' ); |
| 228 |
return; |
| 229 |
} |
| 230 |
|
| 231 |
$id = (int) $_GET['id']; |
| 232 |
$this->get_logger()->debug( 'Category ID from URL for sidebar: ' . $id, [ |
| 233 |
'plugin' => $this->domain, |
| 234 |
] ); |
| 235 |
|
| 236 |
/* |
| 237 |
* Load the assigned Category |
| 238 |
*/ |
| 239 |
$Category = Category::get_by_id( $id ); |
| 240 |
|
| 241 |
if ( null === $Category ) { |
| 242 |
$this->get_logger()->error( 'Category with ID ' . $id . ' not found for sidebar. Redirecting.', [ |
| 243 |
'plugin' => $this->domain, |
| 244 |
] ); |
| 245 |
wp_redirect( admin_url( 'admin.php' ) . '?page=f12-cf7-doubleoptin' ); |
| 246 |
return; |
| 247 |
} |
| 248 |
|
| 249 |
$this->get_logger()->notice( 'Category object loaded for sidebar rendering.', [ |
| 250 |
'plugin' => $this->domain, |
| 251 |
'category_id' => $id, |
| 252 |
] ); |
| 253 |
?> |
| 254 |
<div class="box"> |
| 255 |
<h2> |
| 256 |
<?php _e( 'Edit Category:', 'double-opt-in' ); ?> |
| 257 |
</h2> |
| 258 |
|
| 259 |
<form action="" method="post"> |
| 260 |
<div class="option"> |
| 261 |
<input type="hidden" value="<?php esc_attr_e( $_GET['id'] ); ?>" name="category_id"/> |
| 262 |
<div class="input"> |
| 263 |
<input type="text" name="categorie_name" |
| 264 |
value="<?php esc_attr_e( $Category->get_name() ); ?>" |
| 265 |
placeholder="<?php _e( 'Please enter the Name of the Categorie', 'double-opt-in' ); ?>"/> |
| 266 |
<p> |
| 267 |
<?php _e( 'Enter the name of the Categorie and submit.', 'double-opt-in' ); ?> |
| 268 |
</p> |
| 269 |
</div> |
| 270 |
</div> |
| 271 |
<?php |
| 272 |
$this->get_logger()->debug( 'Rendering nonce field and submit button for category edit form.', [ |
| 273 |
'plugin' => $this->domain, |
| 274 |
] ); |
| 275 |
wp_nonce_field( 'doi_settings_action', 'doi_settings_nonce' ); |
| 276 |
?> |
| 277 |
|
| 278 |
<input type="submit" name="doubleoptin-settings-edit-submit" class="button button-primary" |
| 279 |
value=" <?php _e( 'Update', 'double-opt-in' ); ?>"/> |
| 280 |
</form> |
| 281 |
</div> |
| 282 |
<?php |
| 283 |
$this->get_logger()->notice( 'Sidebar content for "Category View" page rendered successfully.', [ |
| 284 |
'plugin' => $this->domain, |
| 285 |
] ); |
| 286 |
} |
| 287 |
|
| 288 |
public function getSettings( $settings ) { |
| 289 |
$this->get_logger()->info( 'getSettings method called. Returning the provided settings array without modification.', [ |
| 290 |
'plugin' => $this->domain, |
| 291 |
'page_slug' => $this->slug, |
| 292 |
'settings_keys' => is_array($settings) ? array_keys($settings) : 'not an array', |
| 293 |
] ); |
| 294 |
|
| 295 |
// This method is intentionally designed to be a pass-through |
| 296 |
// It's part of a filter chain and simply returns the value it receives. |
| 297 |
// Future subclasses can override this to inject or modify settings. |
| 298 |
return $settings; |
| 299 |
} |
| 300 |
} |
| 301 |
} |