# double-opt-in/3.0.3/core/CategoryOptions.class.php

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

- Page: https://pluginprobe.com/plugins/double-opt-in/3.0.3/code/core/CategoryOptions.class.php
- Raw: https://pluginprobe.com/plugins/double-opt-in/3.0.3/raw/core/CategoryOptions.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/3.0.3/code/core/CategoryOptions.class.php#L10-L20`.

```php
<?php

namespace forge12\contactform7\CF7DoubleOptIn {
    if (!defined('ABSPATH')) {
        exit;
    }

    /**
     * Class UICategories
     *
     * @package forge12\contactform7\CF7DoubleOptIn
     */
    class CategoryOptions
    {
        private static $_instance = null;

        public static function getInstance()
        {
            if (null === self::$_instance) {
                self::$_instance = new CategoryOptions();
            }
            return self::$_instance;
        }

        private function __construct()
        {
            // Add additional actions
            add_action('f12_cf7_doubleoptin_ui_table_options', array($this, 'addOption'));

            add_action('admin_init', array($this, 'init'));
        }

        /**
         * After initializing the WordPress Admi
         *
         * @return void
         */
        public function init()
        {
            if (isset($_POST['update-category'])) {
                $this->update_categories();
            }
        }

        /**
         * Assign the new categories to the selected opt ins.
         *
         * @return void
         */
        private function update_categories()
        {
            if (!\wp_verify_nonce($_POST['f12_cf7_doubleoptin_ui_table_options_nonce'], 'f12_cf7_doubleoptin_ui_table_options_action')) {
                return;
            }

            if (!isset($_POST['optin-id']) || !is_array($_POST['optin-id'])) {
                return;
            }

            if (!isset($_POST['category'])) {
                return;
            }
            $category_id = (int)$_POST['category'];

            $Category = Category::get_by_id($category_id);

            if (null == $Category && 0 !== $category_id) {
                return;
            }

            $optinIds = array_map('intval', $_POST['optin-id']);

            foreach ($optinIds as $id) {
                OptIn::update_category_by_id($id, $category_id);
            }
        }

        /**
         * Return an Select Field for Categories
         *
         * @return string
         */
        public function select_category($selected = 0)
        {
            $atts = array(
                'perPage' => -1,
                'orderBy' => 'name',
                'order' => 'ASC'
            );

            $list = Category::get_list($atts, $numberOfPages);

            $response = '<option value="0">' . __('Please select', 'double-opt-in') . '</option>';
            foreach ($list as $Category/** @var Category $Category */) {
                $selected_html = $selected == $Category->get_id() ? 'selected="selected"' : '';
                $response .= '<option value="' . esc_attr($Category->get_id()) . '" '.$selected_html.'>' . esc_attr($Category->get_name()) . '</option>';
            }
            return '<select name="category">' . $response . '</select>';
        }

        /**
         * Add a Option to update the given Categories
         *
         * @return string
         */
        public function addOption()
        {
            ?>
            <li>
                <?php _e('Change Category to: '); ?>
                <?php echo wp_kses($this->select_category(), array('select' => array('name' => array()), 'option' => array('value' => array(), 'selected' => array()))); ?>
                <input type="submit" class="button" name="update-category"
                       value="<?php _e('Update', 'double-opt-in'); ?>"/>
            </li>
            <?php
        }
    }
}
```
