| 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 UIDashboard |
| 13 |
* |
| 14 |
* @package forge12\contactform7\CF7DoubleOptIn |
| 15 |
*/ |
| 16 |
class UIDashboard extends UIPage |
| 17 |
{ |
| 18 |
/** |
| 19 |
* Constructor for the class. |
| 20 |
* |
| 21 |
* @param TemplateHandler $templateHandler The template handler object. |
| 22 |
* @param string $domain The text domain for translations. |
| 23 |
* |
| 24 |
* @return void |
| 25 |
*/ |
| 26 |
public function __construct(LoggerInterface $logger, TemplateHandler $templateHandler, string $domain) |
| 27 |
{ |
| 28 |
parent::__construct( |
| 29 |
$logger, |
| 30 |
$templateHandler, |
| 31 |
$domain, |
| 32 |
'f12-cf7-doubleoptin', |
| 33 |
__('Dashboard','double-opt-in'), |
| 34 |
0 |
| 35 |
); |
| 36 |
|
| 37 |
$this->get_logger()->notice( 'UIDashboard parent constructor called with a position of 0.', [ |
| 38 |
'plugin' => $domain, |
| 39 |
] ); |
| 40 |
} |
| 41 |
|
| 42 |
public function getSettings($settings) |
| 43 |
{ |
| 44 |
$this->get_logger()->info( 'getSettings method called. Returning the provided settings array without modification.', [ |
| 45 |
'plugin' => $this->domain, |
| 46 |
'page_slug' => $this->slug, |
| 47 |
'settings_keys' => is_array($settings) ? array_keys($settings) : 'not an array', |
| 48 |
] ); |
| 49 |
|
| 50 |
return $settings; |
| 51 |
} |
| 52 |
|
| 53 |
protected function onSave($settings) |
| 54 |
{ |
| 55 |
$this->get_logger()->info( 'onSave method for the dashboard UI page called. No modifications to settings are performed.', [ |
| 56 |
'plugin' => $this->domain, |
| 57 |
] ); |
| 58 |
|
| 59 |
// This method is a placeholder and intentionally does not modify the settings. |
| 60 |
// The dashboard UI page is typically for viewing information, not for saving settings. |
| 61 |
return $settings; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Maybe delete an given entry. |
| 66 |
*/ |
| 67 |
private function maybeDelete() |
| 68 |
{ |
| 69 |
$this->get_logger()->info('Attempting to handle a delete or status-based message display.', [ |
| 70 |
'plugin' => $this->domain, |
| 71 |
]); |
| 72 |
|
| 73 |
// Handle delete request |
| 74 |
if (isset($_GET['option']) && $_GET['option'] == 'delete' && isset($_GET['hash']) && !empty($_GET['hash']) && current_user_can('manage_options')) { |
| 75 |
$hash = esc_attr($_GET['hash']); |
| 76 |
$this->get_logger()->debug('Delete request received for hash: ' . $hash, [ |
| 77 |
'plugin' => $this->domain, |
| 78 |
'hash' => $hash, |
| 79 |
]); |
| 80 |
|
| 81 |
$CleanUp = new CleanUp($this->get_logger()); |
| 82 |
|
| 83 |
if ($CleanUp->deleteByHash($hash)) { |
| 84 |
$this->get_logger()->notice('Successfully deleted DOI with hash: ' . $hash . '. Redirecting.', [ |
| 85 |
'plugin' => $this->domain, |
| 86 |
]); |
| 87 |
wp_redirect('admin.php?page=f12-cf7-doubleoptin&status=deleted'); |
| 88 |
wp_die(); |
| 89 |
} else { |
| 90 |
$this->get_logger()->error('Failed to delete DOI with hash: ' . $hash . '. Redirecting.', [ |
| 91 |
'plugin' => $this->domain, |
| 92 |
]); |
| 93 |
wp_redirect('admin.php?page=f12-cf7-doubleoptin&status=error'); |
| 94 |
wp_die(); |
| 95 |
} |
| 96 |
|
| 97 |
// Handle status message display after a redirect |
| 98 |
} else if (isset($_GET['status'])) { |
| 99 |
$status = sanitize_text_field($_GET['status']); |
| 100 |
$this->get_logger()->info('Processing status message from URL: ' . $status, [ |
| 101 |
'plugin' => $this->domain, |
| 102 |
]); |
| 103 |
|
| 104 |
if ($status == 'deleted') { |
| 105 |
Messages::getInstance()->add(__('The given DOI has been removed.', 'double-opt-in'), 'success'); |
| 106 |
$this->get_logger()->notice('Added "DOI deleted" success message.', [ |
| 107 |
'plugin' => $this->domain, |
| 108 |
]); |
| 109 |
} |
| 110 |
if ($status == 'error') { |
| 111 |
Messages::getInstance()->add(__('Couldn\'t delete the DOI, please try again later.', 'double-opt-in'), 'error'); |
| 112 |
$this->get_logger()->error('Added "DOI deletion failed" error message.', [ |
| 113 |
'plugin' => $this->domain, |
| 114 |
]); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
public function theContent($slug, $page, $settings) |
| 120 |
{ |
| 121 |
$this->get_logger()->info('Rendering the main content for the Dashboard UI page.', [ |
| 122 |
'plugin' => $this->domain, |
| 123 |
'page_slug' => $page, |
| 124 |
]); |
| 125 |
|
| 126 |
// Check for and handle a deletion request. |
| 127 |
// The maybeDelete() method will handle logging, nonce checks, and redirection on its own. |
| 128 |
$this->maybeDelete(); |
| 129 |
|
| 130 |
// Render the latest Opt-Ins table. |
| 131 |
// The renderLatestOptIns() method is responsible for fetching data and rendering the HTML table. |
| 132 |
$this->get_logger()->info('Calling renderLatestOptIns() to display the latest entries.', [ |
| 133 |
'plugin' => $this->domain, |
| 134 |
]); |
| 135 |
$this->renderLatestOptIns($slug, $page); |
| 136 |
|
| 137 |
$this->get_logger()->notice('Dashboard content rendering completed successfully.', [ |
| 138 |
'plugin' => $this->domain, |
| 139 |
]); |
| 140 |
} |
| 141 |
|
| 142 |
private function renderLatestOptins($slug, $page) |
| 143 |
{ |
| 144 |
$this->get_logger()->info('Rendering the latest Opt-Ins table for the Dashboard UI page.', [ |
| 145 |
'plugin' => $this->domain, |
| 146 |
'page_slug' => $page, |
| 147 |
]); |
| 148 |
|
| 149 |
$atts = array( |
| 150 |
'perPage' => 10, |
| 151 |
'page' => 1, |
| 152 |
'order' => 'DESC', |
| 153 |
'keyword' => '' |
| 154 |
); |
| 155 |
|
| 156 |
// Set pagination and search attributes from URL parameters. |
| 157 |
if(isset($_GET['perPage'])){ |
| 158 |
$atts['perPage'] = (int)$_GET['perPage']; |
| 159 |
$this->get_logger()->debug('perPage attribute set from URL: ' . $atts['perPage'], [ |
| 160 |
'plugin' => $this->domain, |
| 161 |
]); |
| 162 |
} |
| 163 |
|
| 164 |
if (isset($_GET['pageNum'])) { |
| 165 |
$atts['page'] = (int)$_GET['pageNum']; |
| 166 |
$this->get_logger()->debug('pageNum attribute set from URL: ' . $atts['page'], [ |
| 167 |
'plugin' => $this->domain, |
| 168 |
]); |
| 169 |
} |
| 170 |
|
| 171 |
if(isset($_GET['keyword'])){ |
| 172 |
$atts['keyword'] = sanitize_text_field($_GET['keyword']); |
| 173 |
$this->get_logger()->debug('keyword attribute set from URL: ' . $atts['keyword'], [ |
| 174 |
'plugin' => $this->domain, |
| 175 |
]); |
| 176 |
} |
| 177 |
|
| 178 |
if(isset($_GET['cf_form_id'])){ |
| 179 |
$atts['cf_form_id'] = (int)$_GET['cf_form_id']; |
| 180 |
$this->get_logger()->debug('cf_form_id attribute set from URL: ' . $atts['cf_form_id'], [ |
| 181 |
'plugin' => $this->domain, |
| 182 |
]); |
| 183 |
} |
| 184 |
|
| 185 |
$numberOfPages = 0; |
| 186 |
$this->get_logger()->info('Fetching a list of Opt-Ins from the database.', [ |
| 187 |
'plugin' => $this->domain, |
| 188 |
'query_atts' => $atts, |
| 189 |
]); |
| 190 |
|
| 191 |
// Fetch the list of opt-ins based on the attributes. |
| 192 |
$listOfOptIns = OptIn::get_list($atts, $numberOfPages); |
| 193 |
|
| 194 |
$this->get_logger()->notice('Found ' . count($listOfOptIns) . ' Opt-Ins across ' . $numberOfPages . ' pages.', [ |
| 195 |
'plugin' => $this->domain, |
| 196 |
]); |
| 197 |
|
| 198 |
/* |
| 199 |
* Render the Template |
| 200 |
*/ |
| 201 |
?> |
| 202 |
<h1><?php _e('Latest Opt-ins', 'double-opt-in'); ?></h1> |
| 203 |
<?php |
| 204 |
$this->get_logger()->info('Rendering the "list-optins" template.', [ |
| 205 |
'plugin' => $this->domain, |
| 206 |
]); |
| 207 |
|
| 208 |
// Render the list-optins template with the fetched data. |
| 209 |
$this->TemplateHandler->renderTemplate('list-optins', array( |
| 210 |
'domain' => $this->domain, |
| 211 |
'listOfOptIns' => $listOfOptIns, |
| 212 |
'numberOfPages' => $numberOfPages, |
| 213 |
'currentPage' => $atts['page'], |
| 214 |
'slug' => $slug |
| 215 |
)); |
| 216 |
|
| 217 |
$this->get_logger()->notice('Finished rendering the latest Opt-Ins content.', [ |
| 218 |
'plugin' => $this->domain, |
| 219 |
]); |
| 220 |
} |
| 221 |
|
| 222 |
public function theSidebar($slug, $page) |
| 223 |
{ |
| 224 |
$this->get_logger()->info('Rendering the sidebar content for the Dashboard UI page.', [ |
| 225 |
'plugin' => $this->domain, |
| 226 |
'page_slug' => $page, |
| 227 |
]); |
| 228 |
?> |
| 229 |
<div class="box"> |
| 230 |
<h2> |
| 231 |
<?php _e('Hint:', 'double-opt-in'); ?> |
| 232 |
</h2> |
| 233 |
<p> |
| 234 |
<?php _e("In the table on the left side, you'll find an list containing all Opt-Ins and the required information from your customers. Click on the hash to open aditional informations about the form and the user data. All unconfirmed opt-ins will be deleted after 7 days.", 'double-opt-in'); ?> |
| 235 |
</p> |
| 236 |
</div> |
| 237 |
<div class="box"> |
| 238 |
<h2> |
| 239 |
<?php _e('Hooks:', 'double-opt-in'); ?> |
| 240 |
</h2> |
| 241 |
<p> |
| 242 |
<?php _e("You can use the following hooks to adjust the Opt-In to your requirements.", 'double-opt-in'); ?> |
| 243 |
</p> |
| 244 |
<p> |
| 245 |
<strong><?php _e("An Opt-in link has been called which is already confirmed or has been deleted.", 'double-opt-in'); ?></strong> |
| 246 |
</p> |
| 247 |
<div class="option"> |
| 248 |
<div class="input"> |
| 249 |
<p> |
| 250 |
add_action('f12_cf7_doubleoptin_already_confirmed', $hash, $OptIn) |
| 251 |
<br> |
| 252 |
</p> |
| 253 |
</div> |
| 254 |
</div> |
| 255 |
|
| 256 |
<p> |
| 257 |
<strong><?php _e("An Opt-in link has been called but not yet updated the database.", 'double-opt-in'); ?></strong> |
| 258 |
</p> |
| 259 |
<div class="option"> |
| 260 |
<div class="input"> |
| 261 |
<p> |
| 262 |
add_action('f12_cf7_doubleoptin_before_confirm', $hash, $OptIn) |
| 263 |
<br> |
| 264 |
</p> |
| 265 |
</div> |
| 266 |
</div> |
| 267 |
|
| 268 |
<p> |
| 269 |
<strong><?php _e("An Opt-in link has been called and the database has been updated already.", 'double-opt-in'); ?></strong> |
| 270 |
</p> |
| 271 |
<div class="option"> |
| 272 |
<div class="input"> |
| 273 |
<p> |
| 274 |
add_action('f12_cf7_doubleoptin_after_confirm', $hash, $OptIn) |
| 275 |
</p> |
| 276 |
</div> |
| 277 |
</div> |
| 278 |
|
| 279 |
</div> |
| 280 |
<?php |
| 281 |
$this->get_logger()->notice('Dashboard sidebar content rendered successfully.', [ |
| 282 |
'plugin' => $this->domain, |
| 283 |
]); |
| 284 |
} |
| 285 |
} |
| 286 |
} |