/
AddActionsAndFilters_DataModel.php
279 lines (249 loc) · 8.43 KB
/
AddActionsAndFilters_DataModel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
/*
"Add Shortcodes, Actions and Filters" Copyright (C) 2013-2023 Michael Simpson (email : michael.d.simpson@gmail.com)
This file is part of Add Actions and Filters for WordPress.
Add Actions and Filters is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Add Actions and Filters is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Contact Form to Database Extension.
If not, see <http://www.gnu.org/licenses/>.
*/
require_once('AddActionsAndFilters_DataModelConfig.php');
class AddActionsAndFilters_DataModel
{
/**
* @var AddActionsAndFilters_Plugin
*/
var $plugin;
/**
* @var AddActionsAndFilters_DataModelConfig
*/
var $config;
public function __construct($plugin, $config)
{
$this->plugin = $plugin;
$this->config = $config;
}
/**
* @param $ids String|array of ids
* @param $bool boolean activate (true) or deactivate (false)
* @return int|false number of rows affected or false
*/
public function activate($ids, $bool)
{
global $wpdb;
$this->plugin->ensureDatabaseTableInstalled(); // ensure created in multisite
$table = $this->plugin->getTableName();
$activate = $bool ? 'true' : 'false';
if (is_array($ids)) {
$count = count($ids);
$placeholders = array_fill(0, $count, '%d');
$placeholders = implode(', ', $placeholders);
$sql = "update $table set enabled = $activate where id in ( $placeholders )";
$sql = $wpdb->prepare($sql, $ids);
} else {
$sql = "update $table set enabled = $activate where id = %d";
$sql = $wpdb->prepare($sql, $ids);
}
return $wpdb->query($sql);
}
/**
* @param $ids String|array of ids
* @return int|false number of rows affected or false
*/
public function delete($ids)
{
global $wpdb;
$this->plugin->ensureDatabaseTableInstalled(); // ensure created in multisite
$table = $this->plugin->getTableName();
if (is_array($ids)) {
$count = count($ids);
$placeholders = array_fill(0, $count, '%d');
$placeholders = implode(', ', $placeholders);
$sql = "delete from $table where id in ( $placeholders )";
$sql = $wpdb->prepare($sql, $ids);
} else {
$sql = "delete from $table where id = %d";
$sql = $wpdb->prepare($sql, $ids);
}
return $wpdb->query($sql);
}
/**
* @param null|false|string $search
* @return array|null|object
*/
public function getDataItemList($search = null)
{
global $wpdb;
$this->plugin->ensureDatabaseTableInstalled(); // ensure created in multisite
$table = $this->plugin->getTableName();
$orderby = $this->config->getOrderby();
$asc = $this->config->isAsc() ? 'asc' : 'desc';
$numPerPage = $this->config->getNumberPerPage();
$limit = sprintf(
'%d,%d',
($this->config->getPage() - 1) * $numPerPage,
$numPerPage
);
$sql = "select id, enabled, shortcode, name, capability, description from $table";
if ($search) {
$param = "%$search%";
$sql .= $wpdb->prepare(' where name like %s or description like %s', $param, $param);
}
$sql .= " order by $orderby $asc limit $limit";
return $wpdb->get_results($sql, ARRAY_A);
}
/**
* @return int
*/
public function getNumberOfDataItems()
{
global $wpdb;
$this->plugin->ensureDatabaseTableInstalled(); // ensure created in multisite
$table = $this->plugin->getTableName();
return $wpdb->get_var("select count(id) from $table");
}
/**
* @param $id int
* @return array
*/
public function getDataItem($id)
{
global $wpdb;
$this->plugin->ensureDatabaseTableInstalled(); // ensure created in multisite
$table = $this->plugin->getTableName();
$sql = $wpdb->prepare("select * from $table where id = %d", $id);
return $wpdb->get_row($sql, ARRAY_A);
}
/**
* @param $ids array|null
* @return array|null|object
*/
public function getDataItems($ids)
{
global $wpdb;
$this->plugin->ensureDatabaseTableInstalled(); // ensure created in multisite
$table = $this->plugin->getTableName();
$sql = "select * from $table ";
if (is_array($ids) && !empty($ids)) {
$sql .= ' where id in ( %d';
$sql .= str_repeat(', %d', count($ids) - 1);
$sql .= ' )';
$sql = $wpdb->prepare($sql, $ids);
}
$sql .= ' order by id asc';
return $wpdb->get_results($sql, ARRAY_A);
}
/**
* @param $item array
* @return int|false
*/
public function saveItem($item)
{
if (isset($item['id'])) {
return $this->updateItem($item);
} else {
return $this->insertItem($item);
}
}
public function insertItem($item)
{
$this->ensureAllIndexesSet($item);
$this->conformInputValues($item);
global $wpdb;
$this->plugin->ensureDatabaseTableInstalled(); // ensure created in multisite
$table = $this->plugin->getTableName();
$data = array(
'name' => $item['name'],
'description' => $item['description'],
'enabled' => $item['enabled'],
'shortcode' => $item['shortcode'],
'buffer' => $item['buffer'],
'inadmin' => $item['inadmin'],
'capability' => $item['capability'],
'code' => $item['code'],
);
$format = array('%s', '%s', '%d', '%d', '%d', '%d', '%s', '%s');
$wpdb->insert($table, $data, $format);
return $wpdb->insert_id;
}
public function updateItem($item)
{
$this->ensureAllIndexesSet($item);
$this->conformInputValues($item);
global $wpdb;
$this->plugin->ensureDatabaseTableInstalled(); // ensure created in multisite
$table = $this->plugin->getTableName();
$sql = $wpdb->prepare(
"update $table set name = %s, description = %s, capability = %s, enabled = %d, shortcode = %d, buffer = %d, inadmin = %d, code = %s where id = %d",
$item['name'],
$item['description'],
$item['capability'],
$item['enabled'],
$item['shortcode'],
$item['buffer'],
$item['inadmin'],
$item['code'],
$item['id']);
$wpdb->query($sql);
return $item['id'];
}
protected function ensureAllIndexesSet(&$item)
{
$defaults = array(
'name' => '',
'description' => '',
'capability' => '',
'code' => '',
'enabled' => 0,
'shortcode' => 0,
'buffer' => 1,
'inadmin' => 0
);
foreach ($defaults as $key => $value) {
if (!isset($item[$key])) {
$item[$key] = $value;
}
}
}
protected function conformInputValues(&$item)
{
$strings = array(
'name',
'description',
'capability',
'code'
);
$bools = array(
'enabled',
'shortcode',
'buffer',
'inadmin'
);
$doStripSlashes = isset($item['stripslashes']) ? $item['stripslashes'] : true;
if ($doStripSlashes) {
foreach ($strings as $key) {
$item[$key] = stripslashes($item[$key]);
}
}
foreach ($bools as $key) {
$item[$key] = $this->convertBooleanValue($item[$key]);
}
}
protected function convertBooleanValue($value)
{
if (is_numeric($value)) {
return $value;
}
if (is_bool($value)) {
return $value ? 1 : 0;
}
return strtolower($value) === 'true' ? 1 : 0;
}
}