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