| 1 |
<?php |
| 2 |
|
| 3 |
namespace forge12\contactform7\CF7DoubleOptIn { |
| 4 |
if (!defined('ABSPATH')) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* Class Category |
| 10 |
* Handle the Categories |
| 11 |
* |
| 12 |
* @package forge12\contactform7\CF7DoubleOptIn |
| 13 |
*/ |
| 14 |
class Category |
| 15 |
{ |
| 16 |
/** |
| 17 |
* Stores the DB ID for the entry |
| 18 |
* |
| 19 |
* @var int |
| 20 |
*/ |
| 21 |
private $id = 0; |
| 22 |
|
| 23 |
/** |
| 24 |
* The name of the category |
| 25 |
* |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
private $name = ''; |
| 29 |
/** |
| 30 |
* Stores the timestamp the opt-in mail has been sent to the user. |
| 31 |
* |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
private $createtime = ""; |
| 35 |
/** |
| 36 |
* Stores the timestamp the opt-in has been confirmed by the user. |
| 37 |
*/ |
| 38 |
private $updatetime = ""; |
| 39 |
|
| 40 |
/** |
| 41 |
* The constructor |
| 42 |
*/ |
| 43 |
public function __construct(array $properties = array()) |
| 44 |
{ |
| 45 |
foreach ($properties as $name => $value) { |
| 46 |
if (isset($this->{$name})) { |
| 47 |
$this->{$name} = $value; |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Return the ID of the Optin |
| 54 |
* |
| 55 |
* @return int |
| 56 |
*/ |
| 57 |
public function get_id() |
| 58 |
{ |
| 59 |
return $this->id; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Return the name of the category |
| 64 |
* |
| 65 |
* @return string |
| 66 |
*/ |
| 67 |
public function get_name() |
| 68 |
{ |
| 69 |
return $this->name; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Set the name of the category |
| 74 |
* |
| 75 |
* @param string $name |
| 76 |
* |
| 77 |
* @return void |
| 78 |
*/ |
| 79 |
public function set_name($name) |
| 80 |
{ |
| 81 |
$this->name = $name; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* @param string $timestamp |
| 86 |
*/ |
| 87 |
public function set_createtime($timestamp) |
| 88 |
{ |
| 89 |
$this->createtime = $timestamp; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Return a timestamp |
| 94 |
* |
| 95 |
* @param string $view Select how to return the content. raw is the stored db value. use formatted to return a |
| 96 |
* formatted string. |
| 97 |
* |
| 98 |
* @return string |
| 99 |
*/ |
| 100 |
public function get_createtime($view = "raw") |
| 101 |
{ |
| 102 |
if (empty($this->createtime)) { |
| 103 |
$this->set_createtime(time()); |
| 104 |
} |
| 105 |
|
| 106 |
if ($view != "raw") { |
| 107 |
if (!is_numeric($this->createtime)) { |
| 108 |
$date = date('d.m.Y', strtotime($this->createtime)); |
| 109 |
$time = date('H:i:s', strtotime($this->createtime)); |
| 110 |
} else { |
| 111 |
$date = date('d.m.Y', $this->createtime); |
| 112 |
$time = date('H:i:s', $this->createtime); |
| 113 |
} |
| 114 |
|
| 115 |
return $date . ' ' . __('/', 'double-opt-in') . ' ' . $time; |
| 116 |
} else { |
| 117 |
return $this->createtime; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* @param string $timestamp |
| 123 |
*/ |
| 124 |
public function set_updatetime($timestamp) |
| 125 |
{ |
| 126 |
$this->updatetime = $timestamp; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Return a timestamp |
| 131 |
* |
| 132 |
* @param string $view Select how to return the content. raw is the stored db value. use formatted to return a |
| 133 |
* formatted string. |
| 134 |
* |
| 135 |
* @return string |
| 136 |
*/ |
| 137 |
public function get_updatetime($view = 'raw') |
| 138 |
{ |
| 139 |
if (empty($this->updatetime)) { |
| 140 |
$this->set_updatetime(time()); |
| 141 |
} |
| 142 |
|
| 143 |
if ($view != "raw") { |
| 144 |
if (!is_numeric($this->updatetime)) { |
| 145 |
$date = date('d.m.Y', strtotime($this->updatetime)); |
| 146 |
$time = date('H:i:s', strtotime($this->updatetime)); |
| 147 |
} else { |
| 148 |
$date = date('d.m.Y', $this->updatetime); |
| 149 |
$time = date('H:i:s', $this->updatetime); |
| 150 |
} |
| 151 |
return $date . ' ' . __('/', 'double-opt-in') . ' ' . $time; |
| 152 |
} else { |
| 153 |
return $this->updatetime; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Return the link to the category |
| 159 |
* @return string |
| 160 |
*/ |
| 161 |
public function get_link_ui(){ |
| 162 |
return admin_url('admin.php?page=f12-cf7-doubleoptin_categories_view&id='.$this->get_id()); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Return a list of opt ins stored in the database. |
| 167 |
* |
| 168 |
* @param $atts array ( |
| 169 |
* 'perPage' => 10, // Posts per page |
| 170 |
* 'page' => 0, // Current page |
| 171 |
* 'order' => DESC, // Order (ASC/DESC) |
| 172 |
* ); |
| 173 |
* |
| 174 |
* @param null $numberOfPages - Stores the number of pages found if limited |
| 175 |
* |
| 176 |
* @return array<Category> |
| 177 |
*/ |
| 178 |
public static function get_list($atts = array(), &$numberOfPages = null) |
| 179 |
{ |
| 180 |
global $wpdb; |
| 181 |
|
| 182 |
$attr = array( |
| 183 |
'perPage' => 10, |
| 184 |
'page' => 1, |
| 185 |
'order' => 'DESC', |
| 186 |
'orderBy' => 'ID', |
| 187 |
); |
| 188 |
|
| 189 |
foreach ($atts as $key => $value) { |
| 190 |
if (isset($attr[$key])) { |
| 191 |
$attr[$key] = $atts[$key]; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
$tableName = $wpdb->prefix . 'f12_cf7_doubleoptin_categories'; |
| 196 |
$pageNum = (int)$attr['page'] - 1; |
| 197 |
|
| 198 |
if ($numberOfPages !== null) { |
| 199 |
$result = $wpdb->get_row('SELECT count(*) AS counter FROM ' . $tableName); |
| 200 |
|
| 201 |
$itemCounter = $result->counter; |
| 202 |
|
| 203 |
$numberOfPages = 1; |
| 204 |
|
| 205 |
if ($attr['perPage'] != -1) { |
| 206 |
$numberOfPages = ceil($itemCounter / (int)$attr['perPage']); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
$offset = $pageNum * (int)$attr['perPage']; |
| 211 |
|
| 212 |
$limit = ''; |
| 213 |
|
| 214 |
if ($attr['perPage'] != -1) { |
| 215 |
$limit = ' LIMIT ' . (int)$attr['perPage'] . ' OFFSET ' . $offset; |
| 216 |
} |
| 217 |
|
| 218 |
$rows = $wpdb->get_results('SELECT * FROM ' . $tableName . ' ORDER BY ' . $attr['orderBy'] . ' ' . $attr['order'] . $limit, ARRAY_A); |
| 219 |
|
| 220 |
$list = array(); |
| 221 |
foreach ($rows as $row) { |
| 222 |
$list[] = new Category($row); |
| 223 |
} |
| 224 |
return $list; |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Return a Category by the given ID |
| 229 |
* |
| 230 |
* @param $id |
| 231 |
* |
| 232 |
* @return Category|null |
| 233 |
*/ |
| 234 |
public static function get_by_id($id) |
| 235 |
{ |
| 236 |
global $wpdb; |
| 237 |
$table = $wpdb->prefix . 'f12_cf7_doubleoptin_categories'; |
| 238 |
|
| 239 |
$rows = $wpdb->get_results($wpdb->prepare('SELECT * FROM ' . $table . ' WHERE id=%d', $id), ARRAY_A); |
| 240 |
|
| 241 |
if (is_array($rows) && !empty($rows)) { |
| 242 |
$rows = $rows[0]; |
| 243 |
return new Category($rows); |
| 244 |
} |
| 245 |
|
| 246 |
return null; |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Delete a Category by the given id. All assigned Opt-Ins will be changed to be uncategorized. |
| 251 |
* |
| 252 |
* @param $id |
| 253 |
* |
| 254 |
* @return bool|int|\mysqli_result|resource|null |
| 255 |
*/ |
| 256 |
public static function delete_by_id($id) |
| 257 |
{ |
| 258 |
global $wpdb; |
| 259 |
|
| 260 |
$table = $wpdb->prefix . 'f12_cf7_doubleoptin_categories'; |
| 261 |
|
| 262 |
// Ensure that the opt in class exists otherwise do nothing. |
| 263 |
if (class_exists('forge12\contactform7\CF7DoubleOptIn\OptIn')) { |
| 264 |
OptIn::bulk_update_category($id, 0); |
| 265 |
|
| 266 |
return $wpdb->delete($table, array('id' => $id), array('%d')); |
| 267 |
} |
| 268 |
|
| 269 |
return false; |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Update or create the given Object |
| 274 |
*/ |
| 275 |
public function save() |
| 276 |
{ |
| 277 |
global $wpdb; |
| 278 |
$table = $wpdb->prefix . 'f12_cf7_doubleoptin_categories'; |
| 279 |
|
| 280 |
if (!empty($this->get_id()) || 0 < $this->get_id()) { |
| 281 |
return $wpdb->update($table, array( |
| 282 |
'name' => $this->get_name(), |
| 283 |
'updatetime' => $this->get_updatetime() |
| 284 |
), array( |
| 285 |
'id' => $this->get_id() |
| 286 |
)); |
| 287 |
} else { |
| 288 |
$result = $wpdb->insert($table, array( |
| 289 |
'name' => $this->get_name(), |
| 290 |
'createtime' => $this->get_createtime(), |
| 291 |
'updatetime' => $this->get_updatetime(), |
| 292 |
)); |
| 293 |
|
| 294 |
if ($result > 0) { |
| 295 |
$this->id = $wpdb->insert_id; |
| 296 |
return $this->id; |
| 297 |
} |
| 298 |
} |
| 299 |
return false; |
| 300 |
} |
| 301 |
} |
| 302 |
} |