# double-opt-in/2.15/core/Category.class.php

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

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

```php
<?php

namespace forge12\contactform7\CF7DoubleOptIn {
    if (!defined('ABSPATH')) {
        exit;
    }

    /**
     * Class Category
     * Handle the Categories
     *
     * @package forge12\contactform7\CF7DoubleOptIn
     */
    class Category
    {
        /**
         * Stores the DB ID for the entry
         *
         * @var int
         */
        private $id = 0;

        /**
         * The name of the category
         *
         * @var string
         */
        private $name = '';
        /**
         * Stores the timestamp the opt-in mail has been sent to the user.
         *
         * @var string
         */
        private $createtime = "";
        /**
         * Stores the timestamp the opt-in has been confirmed by the user.
         */
        private $updatetime = "";

        /**
         * The constructor
         */
        public function __construct(array $properties = array())
        {
            foreach ($properties as $name => $value) {
                if (isset($this->{$name})) {
                    $this->{$name} = $value;
                }
            }
        }

        /**
         * Return the ID of the Optin
         *
         * @return int
         */
        public function get_id()
        {
            return $this->id;
        }

        /**
         * Return the name of the category
         *
         * @return string
         */
        public function get_name()
        {
            return $this->name;
        }

        /**
         * Set the name of the category
         *
         * @param string $name
         *
         * @return void
         */
        public function set_name($name)
        {
            $this->name = $name;
        }

        /**
         * @param string $timestamp
         */
        public function set_createtime($timestamp)
        {
            $this->createtime = $timestamp;
        }

        /**
         * Return a timestamp
         *
         * @param string $view Select how to return the content. raw is the stored db value. use formatted to return a
         *                     formatted string.
         *
         * @return string
         */
        public function get_createtime($view = "raw")
        {
            if (empty($this->createtime)) {
                $this->set_createtime(time());
            }

            if ($view != "raw") {
                if (!is_numeric($this->createtime)) {
                    $date = date('d.m.Y', strtotime($this->createtime));
                    $time = date('H:i:s', strtotime($this->createtime));
                } else {
                    $date = date('d.m.Y', $this->createtime);
                    $time = date('H:i:s', $this->createtime);
                }

                return $date . ' ' . __('/', 'double-opt-in') . ' ' . $time;
            } else {
                return $this->createtime;
            }
        }

        /**
         * @param string $timestamp
         */
        public function set_updatetime($timestamp)
        {
            $this->updatetime = $timestamp;
        }

        /**
         * Return a timestamp
         *
         * @param string $view Select how to return the content. raw is the stored db value. use formatted to return a
         *                     formatted string.
         *
         * @return string
         */
        public function get_updatetime($view = 'raw')
        {
            if (empty($this->updatetime)) {
                $this->set_updatetime(time());
            }

            if ($view != "raw") {
                if (!is_numeric($this->updatetime)) {
                    $date = date('d.m.Y', strtotime($this->updatetime));
                    $time = date('H:i:s', strtotime($this->updatetime));
                } else {
                    $date = date('d.m.Y', $this->updatetime);
                    $time = date('H:i:s', $this->updatetime);
                }
                return $date . ' ' . __('/', 'double-opt-in') . ' ' . $time;
            } else {
                return $this->updatetime;
            }
        }

        /**
         * Return the link to the category
         * @return string
         */
        public function get_link_ui(){
            return admin_url('admin.php?page=f12-cf7-doubleoptin_categories_view&id='.$this->get_id());
        }

        /**
         * Return a list of opt ins stored in the database.
         *
         * @param      $atts            array (
         *                              'perPage' => 10, // Posts per page
         *                              'page' => 0, // Current page
         *                              'order' => DESC, // Order (ASC/DESC)
         *                              );
         *
         * @param null $numberOfPages   - Stores the number of pages found if limited
         *
         * @return array<Category>
         */
        public static function get_list($atts = array(), &$numberOfPages = null)
        {
            global $wpdb;

            $attr = array(
                'perPage' => 10,
                'page' => 1,
                'order' => 'DESC',
                'orderBy' => 'ID',
            );

            foreach ($atts as $key => $value) {
                if (isset($attr[$key])) {
                    $attr[$key] = $atts[$key];
                }
            }

            $tableName = $wpdb->prefix . 'f12_cf7_doubleoptin_categories';
            $pageNum = (int)$attr['page'] - 1;

            if ($numberOfPages !== null) {
                $result = $wpdb->get_row('SELECT count(*) AS counter FROM ' . $tableName);

                $itemCounter = $result->counter;

                $numberOfPages = 1;

                if ($attr['perPage'] != -1) {
                    $numberOfPages = ceil($itemCounter / (int)$attr['perPage']);
                }
            }

            $offset = $pageNum * (int)$attr['perPage'];

            $limit = '';

            if ($attr['perPage'] != -1) {
                $limit = ' LIMIT ' . (int)$attr['perPage'] . ' OFFSET ' . $offset;
            }

            $rows = $wpdb->get_results('SELECT * FROM ' . $tableName . ' ORDER BY ' . $attr['orderBy'] . ' ' . $attr['order'] . $limit, ARRAY_A);

            $list = array();
            foreach ($rows as $row) {
                $list[] = new Category($row);
            }
            return $list;
        }

        /**
         * Return a Category by the given ID
         *
         * @param $id
         *
         * @return Category|null
         */
        public static function get_by_id($id)
        {
            global $wpdb;
            $table = $wpdb->prefix . 'f12_cf7_doubleoptin_categories';

            $rows = $wpdb->get_results($wpdb->prepare('SELECT * FROM ' . $table . ' WHERE id=%d', $id), ARRAY_A);

            if (is_array($rows) && !empty($rows)) {
                $rows = $rows[0];
                return new Category($rows);
            }

            return null;
        }

        /**
         * Delete a Category by the given id. All assigned Opt-Ins will be changed to be uncategorized.
         *
         * @param $id
         *
         * @return bool|int|\mysqli_result|resource|null
         */
        public static function delete_by_id($id)
        {
            global $wpdb;

            $table = $wpdb->prefix . 'f12_cf7_doubleoptin_categories';

            // Ensure that the opt in class exists otherwise do nothing.
            if (class_exists('forge12\contactform7\CF7DoubleOptIn\OptIn')) {
                OptIn::bulk_update_category($id, 0);

                return $wpdb->delete($table, array('id' => $id), array('%d'));
            }

            return false;
        }

        /**
         * Update or create the given Object
         */
        public function save()
        {
            global $wpdb;
            $table = $wpdb->prefix . 'f12_cf7_doubleoptin_categories';

            if (!empty($this->get_id()) || 0 < $this->get_id()) {
                return $wpdb->update($table, array(
                    'name' => $this->get_name(),
                    'updatetime' => $this->get_updatetime()
                ), array(
                    'id' => $this->get_id()
                ));
            } else {
                $result = $wpdb->insert($table, array(
                    'name' => $this->get_name(),
                    'createtime' => $this->get_createtime(),
                    'updatetime' => $this->get_updatetime(),
                ));

                if ($result > 0) {
                    $this->id = $wpdb->insert_id;
                    return $this->id;
                }
            }
            return false;
        }
    }
}
```
