# double-opt-in/2.11/ui/UICategories.class.php

Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification, version 2.11. 224 lines.

- Page: https://pluginprobe.com/plugins/double-opt-in/2.11/code/ui/UICategories.class.php
- Raw: https://pluginprobe.com/plugins/double-opt-in/2.11/raw/ui/UICategories.class.php
- Modified: 2022-12-16T11:04:54+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/double-opt-in/2.11/code/ui/UICategories.class.php#L10-L20`.

```php
<?php

namespace forge12\contactform7\CF7DoubleOptIn {
    if (!defined('ABSPATH')) {
        exit;
    }

    /**
     * Class UICategories
     *
     * @package forge12\contactform7\CF7DoubleOptIn
     */
    class UICategories extends UIPageForm
    {
        public function __construct($domain)
        {
            parent::__construct($domain, 'categories', 'Categories');

            $this->hideSubmitButton(true);

            // Check for error message
            if (isset($_GET['status'])) {
                $status = sanitize_text_field($_GET['status']);

                switch ($status) {
                    case 'category-deleted':
                        Messages::getInstance()->add(__('Category deleted', 'double-opt-in'), 'success');
                        break;
                    case 'category-not-deleted':
                        Messages::getInstance()->add(__('Category not-deleted', 'double-opt-in'), 'error');
                        break;
                    default:
                        break;
                }
            }
        }

        /**
         * Check if the category should be deleted
         */
        private function maybeDelete()
        {
            if (!isset($_GET['id'])) {
                return;
            }

            if (!isset($_GET['nonce'])) {
                return;
            }

            if (!isset($_GET['option'])) {
                return;
            }

            $option = sanitize_text_field($_GET['option']);
            $id = (int)$_GET['id'];
            $nonce = sanitize_text_field($_GET['nonce']);

            if ('delete' !== $option) {
                return;
            }

            $page = (int)$_GET['pageNum'];

            if (!wp_verify_nonce($_GET['nonce'], 'doi-delete-category-' . $id)) {
                wp_redirect(admin_url('admin.php') . '?page=f12-cf7-doubleoptin_categories&pageNum=' . esc_attr($page) . '&status=category-not-deleted');
            }

            if (Category::delete_by_id($id)) {
                wp_redirect(admin_url('admin.php') . '?page=f12-cf7-doubleoptin_categories&pageNum=' . esc_attr($page) . '&status=category-deleted');
            } else {
                wp_redirect(admin_url('admin.php') . '?page=f12-cf7-doubleoptin_categories&pageNum=' . esc_attr($page) . '&status=category-not-deleted');
            }
            wp_die();
        }

        /**
         * Render the license subpage content
         */
        protected function theContent($slug, $page, $settings)
        {
            // check for deletion
            $this->maybeDelete();
            ?>
            <h2>
                <?php _e('Categories', 'double-opt-in'); ?>
            </h2>

            <?php
            $atts = array(
                'perPage' => 10,
                'page' => 1,
                'order' => 'DESC'
            );

            if (isset($_GET['pageNum'])) {
                $atts['page'] = (int)$_GET['pageNum'];
            }

            $numberOfPages = 0;
            $listOfCategories = Category::get_list($atts, $numberOfPages);
            ?>

            <table>
                <tr>
                    <th>
                        <?php _e('ID', 'double-opt-in'); ?>
                    </th>
                    <th>
                        <?php _e('Name', 'double-opt-in'); ?>
                    </th>
                    <th>
                        <?php _e('Registration Date', 'double-opt-in'); ?>
                    </th>
                    <th>
                        <?php _e('Update Date', 'double-opt-in'); ?>
                    </th>
                    <th></th>
                </tr>
                <?php foreach ($listOfCategories as $Category/** @var Category $Category */) : ?>
                    <tr>
                        <td>
                            <div>
                                <?php echo esc_html($Category->get_id()); ?>
                            </div>
                        </td>
                        <td>
                            <a href="<?php echo admin_url('admin.php'); ?>?page=f12-cf7-doubleoptin_categories_view&id=<?php esc_attr_e($Category->get_id()); ?>"
                               title="<?php _e('Show details', 'double-opt-in'); ?>"><?php echo esc_attr($Category->get_name()); ?></a>
                        </td>
                        <td>
                            <?php
                            echo esc_html($Category->get_createtime('formatted'));
                            ?>
                        </td>
                        <td>
                            <?php
                            echo esc_html($Category->get_updatetime('formatted'));
                            ?>
                        </td>
                        <td>
                            <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()); ?>">
                                <?php _e('Delete', 'double-opt-in'); ?>
                            </a>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </table>
            <div class="tablenav-pages">
                <?php for ($i = 1; $i <= $numberOfPages; $i++): ?>
                    <a href="<?php echo esc_url(admin_url('admin.php')); ?>?page=<?php echo esc_attr($slug . '_' . $page); ?>&pageNum=<?php echo esc_attr($i); ?>"
                       class="button <?php echo ($atts['page']) == $i ? 'active' : ''; ?>">
                        <?php echo esc_html($i); ?>
                    </a>
                <?php endfor; ?>
            </div>
            <?php
        }

        protected function theSidebar($slug, $page)
        {
            ?>
            <div class="box">
                <h2>
                    <?php _e('New Category:', 'double-opt-in'); ?>
                </h2>

                <form action="" method="post">
                    <div class="option">
                        <div class="input">
                            <input type="text" name="categorie_name" value=""
                                   placeholder="<?php _e('Please enter the Name of the Categorie', 'double-opt-in'); ?>"/>
                            <p>
                                <?php _e('Enter the name of the Categorie and submit.', 'double-opt-in'); ?>
                            </p>
                        </div>
                    </div>
                    <?php
                    wp_nonce_field('doi_settings_action', 'doi_settings_nonce');
                    ?>

                    <input type="submit" name="doubleoptin-settings-submit" class="button"
                           value=" <?php _e('Add', 'double-opt-in'); ?>"/>
                </form>
            </div>
            <?php
        }

        public function getSettings($settings)
        {
            return $settings;
        }

        /**
         * Create a new Category
         *
         * @return void
         */
        private function maybeAddCategory()
        {
            $name = sanitize_text_field($_POST['categorie_name']);

            if (empty($name)) {
                Messages::getInstance()->add(__('Please enter a valid name for the categorie.', 'double-opt-in'), 'error');
                return;
            }

            $Category = new Category();
            $Category->set_name($name);

            if ($Category->save()) {
                Messages::getInstance()->add(__('New Category added', 'double-opt-in'), 'success');
            } else {
                Messages::getInstance()->add(__('Something went wrong', 'double-opt-in'), 'error');
            }
        }

        protected function onSave($settings)
        {
            $this->maybeAddCategory();
            return $settings;
        }
    }
}
```
