| 1 |
<?php |
| 2 |
|
| 3 |
namespace forge12\contactform7\CF7DoubleOptIn { |
| 4 |
if (!defined('ABSPATH')) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* Class UICategories |
| 10 |
* |
| 11 |
* @package forge12\contactform7\CF7DoubleOptIn |
| 12 |
*/ |
| 13 |
class UICategories extends UIPageForm |
| 14 |
{ |
| 15 |
public function __construct($domain) |
| 16 |
{ |
| 17 |
parent::__construct($domain, 'categories', 'Categories'); |
| 18 |
|
| 19 |
$this->hideSubmitButton(true); |
| 20 |
|
| 21 |
// Check for error message |
| 22 |
if (isset($_GET['status'])) { |
| 23 |
$status = sanitize_text_field($_GET['status']); |
| 24 |
|
| 25 |
switch ($status) { |
| 26 |
case 'category-deleted': |
| 27 |
Messages::getInstance()->add(__('Category deleted', 'double-opt-in'), 'success'); |
| 28 |
break; |
| 29 |
case 'category-not-deleted': |
| 30 |
Messages::getInstance()->add(__('Category not-deleted', 'double-opt-in'), 'error'); |
| 31 |
break; |
| 32 |
default: |
| 33 |
break; |
| 34 |
} |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Check if the category should be deleted |
| 40 |
*/ |
| 41 |
private function maybeDelete() |
| 42 |
{ |
| 43 |
if (!isset($_GET['id'])) { |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
if (!isset($_GET['nonce'])) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
if (!isset($_GET['option'])) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
$option = sanitize_text_field($_GET['option']); |
| 56 |
$id = (int)$_GET['id']; |
| 57 |
$nonce = sanitize_text_field($_GET['nonce']); |
| 58 |
|
| 59 |
if ('delete' !== $option) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
$page = (int)$_GET['pageNum']; |
| 64 |
|
| 65 |
if (!wp_verify_nonce($_GET['nonce'], 'doi-delete-category-' . $id)) { |
| 66 |
wp_redirect(admin_url('admin.php') . '?page=f12-cf7-doubleoptin_categories&pageNum=' . esc_attr($page) . '&status=category-not-deleted'); |
| 67 |
} |
| 68 |
|
| 69 |
if (Category::delete_by_id($id)) { |
| 70 |
wp_redirect(admin_url('admin.php') . '?page=f12-cf7-doubleoptin_categories&pageNum=' . esc_attr($page) . '&status=category-deleted'); |
| 71 |
} else { |
| 72 |
wp_redirect(admin_url('admin.php') . '?page=f12-cf7-doubleoptin_categories&pageNum=' . esc_attr($page) . '&status=category-not-deleted'); |
| 73 |
} |
| 74 |
wp_die(); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Render the license subpage content |
| 79 |
*/ |
| 80 |
protected function theContent($slug, $page, $settings) |
| 81 |
{ |
| 82 |
// check for deletion |
| 83 |
$this->maybeDelete(); |
| 84 |
?> |
| 85 |
<h2> |
| 86 |
<?php _e('Categories', 'double-opt-in'); ?> |
| 87 |
</h2> |
| 88 |
|
| 89 |
<?php |
| 90 |
$atts = array( |
| 91 |
'perPage' => 10, |
| 92 |
'page' => 1, |
| 93 |
'order' => 'DESC' |
| 94 |
); |
| 95 |
|
| 96 |
if (isset($_GET['pageNum'])) { |
| 97 |
$atts['page'] = (int)$_GET['pageNum']; |
| 98 |
} |
| 99 |
|
| 100 |
$numberOfPages = 0; |
| 101 |
$listOfCategories = Category::get_list($atts, $numberOfPages); |
| 102 |
?> |
| 103 |
|
| 104 |
<table> |
| 105 |
<tr> |
| 106 |
<th> |
| 107 |
<?php _e('ID', 'double-opt-in'); ?> |
| 108 |
</th> |
| 109 |
<th> |
| 110 |
<?php _e('Name', 'double-opt-in'); ?> |
| 111 |
</th> |
| 112 |
<th> |
| 113 |
<?php _e('Registration Date', 'double-opt-in'); ?> |
| 114 |
</th> |
| 115 |
<th> |
| 116 |
<?php _e('Update Date', 'double-opt-in'); ?> |
| 117 |
</th> |
| 118 |
<th></th> |
| 119 |
</tr> |
| 120 |
<?php foreach ($listOfCategories as $Category/** @var Category $Category */) : ?> |
| 121 |
<tr> |
| 122 |
<td> |
| 123 |
<div> |
| 124 |
<?php echo esc_html($Category->get_id()); ?> |
| 125 |
</div> |
| 126 |
</td> |
| 127 |
<td> |
| 128 |
<a href="<?php echo admin_url('admin.php'); ?>?page=f12-cf7-doubleoptin_categories_view&id=<?php esc_attr_e($Category->get_id()); ?>" |
| 129 |
title="<?php _e('Show details', 'double-opt-in'); ?>"><?php echo esc_attr($Category->get_name()); ?></a> |
| 130 |
</td> |
| 131 |
<td> |
| 132 |
<?php |
| 133 |
echo esc_html($Category->get_createtime('formatted')); |
| 134 |
?> |
| 135 |
</td> |
| 136 |
<td> |
| 137 |
<?php |
| 138 |
echo esc_html($Category->get_updatetime('formatted')); |
| 139 |
?> |
| 140 |
</td> |
| 141 |
<td> |
| 142 |
<a href="<?php echo esc_url(admin_url('admin.php')); ?>?page=<?php esc_attr_e($slug . '_' . $page); ?>&pageNum=<?php esc_attr_e($atts['page']); ?>&option=delete&id=<?php esc_attr_e($Category->get_id()); ?>&nonce=<?php echo wp_create_nonce('doi-delete-category-' . $Category->get_id()); ?>"> |
| 143 |
<?php _e('Delete', 'double-opt-in'); ?> |
| 144 |
</a> |
| 145 |
</td> |
| 146 |
</tr> |
| 147 |
<?php endforeach; ?> |
| 148 |
</table> |
| 149 |
<div class="tablenav-pages"> |
| 150 |
<?php for ($i = 1; $i <= $numberOfPages; $i++): ?> |
| 151 |
<a href="<?php echo esc_url(admin_url('admin.php')); ?>?page=<?php echo esc_attr($slug . '_' . $page); ?>&pageNum=<?php echo esc_attr($i); ?>" |
| 152 |
class="button <?php echo ($atts['page']) == $i ? 'active' : ''; ?>"> |
| 153 |
<?php echo esc_html($i); ?> |
| 154 |
</a> |
| 155 |
<?php endfor; ?> |
| 156 |
</div> |
| 157 |
<?php |
| 158 |
} |
| 159 |
|
| 160 |
protected function theSidebar($slug, $page) |
| 161 |
{ |
| 162 |
?> |
| 163 |
<div class="box"> |
| 164 |
<h2> |
| 165 |
<?php _e('New Category:', 'double-opt-in'); ?> |
| 166 |
</h2> |
| 167 |
|
| 168 |
<form action="" method="post"> |
| 169 |
<div class="option"> |
| 170 |
<div class="input"> |
| 171 |
<input type="text" name="categorie_name" value="" |
| 172 |
placeholder="<?php _e('Please enter the Name of the Categorie', 'double-opt-in'); ?>"/> |
| 173 |
<p> |
| 174 |
<?php _e('Enter the name of the Categorie and submit.', 'double-opt-in'); ?> |
| 175 |
</p> |
| 176 |
</div> |
| 177 |
</div> |
| 178 |
<?php |
| 179 |
wp_nonce_field('doi_settings_action', 'doi_settings_nonce'); |
| 180 |
?> |
| 181 |
|
| 182 |
<input type="submit" name="doubleoptin-settings-submit" class="button" |
| 183 |
value=" <?php _e('Add', 'double-opt-in'); ?>"/> |
| 184 |
</form> |
| 185 |
</div> |
| 186 |
<?php |
| 187 |
} |
| 188 |
|
| 189 |
public function getSettings($settings) |
| 190 |
{ |
| 191 |
return $settings; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Create a new Category |
| 196 |
* |
| 197 |
* @return void |
| 198 |
*/ |
| 199 |
private function maybeAddCategory() |
| 200 |
{ |
| 201 |
$name = sanitize_text_field($_POST['categorie_name']); |
| 202 |
|
| 203 |
if (empty($name)) { |
| 204 |
Messages::getInstance()->add(__('Please enter a valid name for the categorie.', 'double-opt-in'), 'error'); |
| 205 |
return; |
| 206 |
} |
| 207 |
|
| 208 |
$Category = new Category(); |
| 209 |
$Category->set_name($name); |
| 210 |
|
| 211 |
if ($Category->save()) { |
| 212 |
Messages::getInstance()->add(__('New Category added', 'double-opt-in'), 'success'); |
| 213 |
} else { |
| 214 |
Messages::getInstance()->add(__('Something went wrong', 'double-opt-in'), 'error'); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
protected function onSave($settings) |
| 219 |
{ |
| 220 |
$this->maybeAddCategory(); |
| 221 |
return $settings; |
| 222 |
} |
| 223 |
} |
| 224 |
} |