PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 10.11.1
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v10.11.1
11.1.1 11.1.0 11.0.5 11.0.4 11.0.3 11.0.2 11.0.1 11.0.0 10.11.1 10.11.0 10.10.2 10.10.1 10.10.0 10.9.1 trunk 10.0.0 10.0.1 10.1.0 10.1.1 10.1.2 10.1.3 10.1.4 10.2.0 10.2.1 10.2.2 All 62 releases
acymailing / back / Core / wordpress / database.php

database.php in AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress 10.11.1, at back/Core/wordpress/database.php

185 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 function acym_escapeDB(?string $value): string
4 {
5 if (is_null($value)) {
6 $value = '';
7 }
8
9 return "'".esc_sql($value)."'";
10 }
11
12 function acym_query(string $query)
13 {
14 global $wpdb;
15 $query = acym_prepareQuery($query);
16
17 $result = $wpdb->query($query);
18
19 return $result === false ? null : $result;
20 }
21
22 function acym_loadObjectList(string $query, string $key = '', ?int $offset = null, ?int $limit = null): array
23 {
24 global $wpdb;
25 $query = acym_prepareQuery($query);
26
27 if (isset($offset)) {
28 $query .= ' LIMIT '.intval($offset).','.intval($limit);
29 }
30
31 $results = $wpdb->get_results($query);
32 if (empty($key)) {
33 return empty($results) ? [] : $results;
34 }
35
36 $sorted = [];
37 foreach ($results as $oneRes) {
38 $sorted[$oneRes->$key] = $oneRes;
39 }
40
41 return $sorted;
42 }
43
44 function acym_prepareQuery(string $query): string
45 {
46 global $wpdb;
47 $query = str_replace('#__', $wpdb->prefix, $query);
48 if (is_multisite()) {
49 $query = str_replace($wpdb->prefix.'users', $wpdb->base_prefix.'users', $query);
50 $query = str_replace($wpdb->prefix.'usermeta', $wpdb->base_prefix.'usermeta', $query);
51 }
52
53 return $query;
54 }
55
56 function acym_loadObject(string $query): ?object
57 {
58 acym_addLimit($query);
59
60 global $wpdb;
61 $query = acym_prepareQuery($query);
62
63 $object = $wpdb->get_row($query);
64
65 return empty($object) ? null : $object;
66 }
67
68 function acym_loadResult(string $query)
69 {
70 global $wpdb;
71 $query = acym_prepareQuery($query);
72
73 return $wpdb->get_var($query);
74 }
75
76 function acym_loadResultArray(string $query): array
77 {
78 global $wpdb;
79 $query = acym_prepareQuery($query);
80
81 return $wpdb->get_col($query);
82 }
83
84 function acym_getEscaped(string $text, bool $extra = false)
85 {
86 $result = esc_sql($text);
87 if ($extra) {
88 $result = addcslashes($result, '%_');
89 }
90
91 return $result;
92 }
93
94 function acym_getDBError(): string
95 {
96 global $wpdb;
97
98 return empty($wpdb->last_error) ? '' : $wpdb->last_error;
99 }
100
101 function acym_insertObject(string $table, object $element): ?int
102 {
103 global $wpdb;
104 $element = get_object_vars($element);
105 $table = acym_prepareQuery($table);
106 $wpdb->insert($table, $element);
107
108 $id = $wpdb->insert_id;
109
110 return empty($id) ? null : (int)$id;
111 }
112
113 function acym_updateObject(string $table, object $element, array $pkey): bool
114 {
115 global $wpdb;
116 $element = get_object_vars($element);
117 $table = acym_prepareQuery($table);
118
119 $where = [];
120 foreach ($pkey as $onePkey) {
121 $where[$onePkey] = $element[$onePkey];
122 }
123
124 $nbUpdated = $wpdb->update($table, $element, $where);
125
126 return $nbUpdated !== false;
127 }
128
129 function acym_getPrefix(): string
130 {
131 global $wpdb;
132
133 return $wpdb->prefix;
134 }
135
136 function acym_getTableList(): array
137 {
138 global $wpdb;
139
140 return acym_loadResultArray(
141 'SELECT table_name FROM information_schema.tables WHERE table_schema = '.acym_escapeDB($wpdb->dbname).' AND table_name LIKE '.acym_escapeDB($wpdb->prefix.'%')
142 );
143 }
144
145 function acym_getCMSConfig(string $varname, $default = null)
146 {
147 $map = [
148 'offset' => 'timezone_string',
149 'list_limit' => 'posts_per_page',
150 'sitename' => 'blogname',
151 'mailfrom' => 'new_admin_email',
152 'feed_email' => 'new_admin_email',
153 ];
154
155 if (!empty($map[$varname])) {
156 $varname = $map[$varname];
157 }
158 $value = get_option($varname, $default);
159
160 if ($varname == 'timezone_string' && empty($value)) {
161 $value = acym_getCMSConfig('gmt_offset');
162
163 if (empty($value)) {
164 $value = 'UTC';
165 } elseif ($value < 0) {
166 $value = 'GMT'.$value;
167 } else {
168 $value = 'GMT+'.$value;
169 }
170 }
171
172 if ($varname === 'posts_per_page') {
173 $possibilities = [5, 10, 15, 20, 25, 30, 50, 100];
174 $closest = 5;
175 foreach ($possibilities as $possibility) {
176 if (abs($value - $closest) > abs($value - $possibility)) {
177 $closest = $possibility;
178 }
179 }
180 $value = $closest;
181 }
182
183 return $value;
184 }
185