| @@ -1,207 +1,0 @@ | ||
| 1 | -<?php | |
| 2 | -/** | |
| 3 | - * Database abstraction layer. | |
| 4 | - * | |
| 5 | - * Queries are built internally with table prefix substitution via prepareQuery(). | |
| 6 | - * Dynamic values are escaped upstream via escapeDB() / esc_sql() before being | |
| 7 | - * passed here. Direct $wpdb calls and absence of per-query caching are intentional | |
| 8 | - * in this low-level layer — caching is handled at the business logic level. | |
| 9 | - * | |
| 10 | - * phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery | |
| 11 | - * phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 12 | - * phpcs:disable WordPress.DB.PreparedSQL.NotPrepared | |
| 13 | - * phpcs:disable PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 14 | - */ | |
| 15 | - | |
| 16 | -defined('ABSPATH') || die('Restricted Access'); | |
| 17 | - | |
| 18 | -function acym_escapeDB(?string $value): string | |
| 19 | -{ | |
| 20 | - if (is_null($value)) { | |
| 21 | - $value = ''; | |
| 22 | - } | |
| 23 | - | |
| 24 | - // esc_sql replaces % by something like {svzzvzevzv} but it's normal, it will be replaced back by % before the query is executed | |
| 25 | - return "'".esc_sql($value)."'"; | |
| 26 | -} | |
| 27 | - | |
| 28 | -function acym_query(string $query) | |
| 29 | -{ | |
| 30 | - global $wpdb; | |
| 31 | - $query = acym_prepareQuery($query); | |
| 32 | - | |
| 33 | - $result = $wpdb->query($query); | |
| 34 | - | |
| 35 | - return $result === false ? null : $result; | |
| 36 | -} | |
| 37 | - | |
| 38 | -function acym_loadObjectList(string $query, string $key = '', ?int $offset = null, ?int $limit = null): array | |
| 39 | -{ | |
| 40 | - global $wpdb; | |
| 41 | - $query = acym_prepareQuery($query); | |
| 42 | - | |
| 43 | - if (isset($offset)) { | |
| 44 | - $query .= ' LIMIT '.intval($offset).','.intval($limit); | |
| 45 | - } | |
| 46 | - | |
| 47 | - $results = $wpdb->get_results($query); | |
| 48 | - if (empty($key)) { | |
| 49 | - return empty($results) ? [] : $results; | |
| 50 | - } | |
| 51 | - | |
| 52 | - $sorted = []; | |
| 53 | - foreach ($results as $oneRes) { | |
| 54 | - $sorted[$oneRes->$key] = $oneRes; | |
| 55 | - } | |
| 56 | - | |
| 57 | - return $sorted; | |
| 58 | -} | |
| 59 | - | |
| 60 | -function acym_prepareQuery(string $query): string | |
| 61 | -{ | |
| 62 | - global $wpdb; | |
| 63 | - $query = str_replace('#__', $wpdb->prefix, $query); | |
| 64 | - if (is_multisite()) { | |
| 65 | - $query = str_replace($wpdb->prefix.'users', $wpdb->base_prefix.'users', $query); | |
| 66 | - $query = str_replace($wpdb->prefix.'usermeta', $wpdb->base_prefix.'usermeta', $query); | |
| 67 | - } | |
| 68 | - | |
| 69 | - return $query; | |
| 70 | -} | |
| 71 | - | |
| 72 | -function acym_loadObject(string $query): ?object | |
| 73 | -{ | |
| 74 | - acym_addLimit($query); | |
| 75 | - | |
| 76 | - global $wpdb; | |
| 77 | - $query = acym_prepareQuery($query); | |
| 78 | - | |
| 79 | - $object = $wpdb->get_row($query); | |
| 80 | - | |
| 81 | - return empty($object) ? null : $object; | |
| 82 | -} | |
| 83 | - | |
| 84 | -function acym_loadResult(string $query) | |
| 85 | -{ | |
| 86 | - global $wpdb; | |
| 87 | - $query = acym_prepareQuery($query); | |
| 88 | - | |
| 89 | - return $wpdb->get_var($query); | |
| 90 | -} | |
| 91 | - | |
| 92 | -function acym_loadResultArray(string $query): array | |
| 93 | -{ | |
| 94 | - global $wpdb; | |
| 95 | - $query = acym_prepareQuery($query); | |
| 96 | - | |
| 97 | - return $wpdb->get_col($query); | |
| 98 | -} | |
| 99 | - | |
| 100 | -function acym_getEscaped(string $text, bool $extra = false) | |
| 101 | -{ | |
| 102 | - $result = esc_sql($text); | |
| 103 | - if ($extra) { | |
| 104 | - $result = addcslashes($result, '%_'); | |
| 105 | - } | |
| 106 | - | |
| 107 | - return $result; | |
| 108 | -} | |
| 109 | - | |
| 110 | -function acym_getDBError(): string | |
| 111 | -{ | |
| 112 | - global $wpdb; | |
| 113 | - | |
| 114 | - return empty($wpdb->last_error) ? '' : $wpdb->last_error; | |
| 115 | -} | |
| 116 | - | |
| 117 | -function acym_insertObject(string $table, object $element): ?int | |
| 118 | -{ | |
| 119 | - global $wpdb; | |
| 120 | - $element = get_object_vars($element); | |
| 121 | - $table = acym_prepareQuery($table); | |
| 122 | - $wpdb->insert($table, $element); | |
| 123 | - | |
| 124 | - $id = $wpdb->insert_id; | |
| 125 | - | |
| 126 | - return empty($id) ? null : (int)$id; | |
| 127 | -} | |
| 128 | - | |
| 129 | -function acym_updateObject(string $table, object $element, array $pkey): bool | |
| 130 | -{ | |
| 131 | - global $wpdb; | |
| 132 | - $element = get_object_vars($element); | |
| 133 | - $table = acym_prepareQuery($table); | |
| 134 | - | |
| 135 | - $where = []; | |
| 136 | - foreach ($pkey as $onePkey) { | |
| 137 | - $where[$onePkey] = $element[$onePkey]; | |
| 138 | - } | |
| 139 | - | |
| 140 | - $nbUpdated = $wpdb->update($table, $element, $where); | |
| 141 | - | |
| 142 | - return $nbUpdated !== false; | |
| 143 | -} | |
| 144 | - | |
| 145 | -function acym_getPrefix(): string | |
| 146 | -{ | |
| 147 | - global $wpdb; | |
| 148 | - | |
| 149 | - return $wpdb->prefix; | |
| 150 | -} | |
| 151 | - | |
| 152 | -function acym_getTableList(): array | |
| 153 | -{ | |
| 154 | - global $wpdb; | |
| 155 | - | |
| 156 | - return acym_loadResultArray( | |
| 157 | - 'SELECT table_name FROM information_schema.tables WHERE table_schema = '.acym_escapeDB($wpdb->dbname).' AND table_name LIKE '.acym_escapeDB($wpdb->prefix.'%') | |
| 158 | - ); | |
| 159 | -} | |
| 160 | - | |
| 161 | -/** | |
| 162 | - * @param mixed $default | |
| 163 | - * | |
| 164 | - * @return mixed | |
| 165 | - */ | |
| 166 | -function acym_getCMSConfig(string $varname, $default = null) | |
| 167 | -{ | |
| 168 | - $map = [ | |
| 169 | - 'offset' => 'timezone_string', | |
| 170 | - 'list_limit' => 'posts_per_page', | |
| 171 | - 'sitename' => 'blogname', | |
| 172 | - 'mailfrom' => 'new_admin_email', | |
| 173 | - 'feed_email' => 'new_admin_email', | |
| 174 | - ]; | |
| 175 | - | |
| 176 | - if (!empty($map[$varname])) { | |
| 177 | - $varname = $map[$varname]; | |
| 178 | - } | |
| 179 | - $value = get_option($varname, $default); | |
| 180 | - | |
| 181 | - // In WP there are multiple possible formats in the same option for the timezone | |
| 182 | - if ($varname == 'timezone_string' && empty($value)) { | |
| 183 | - $value = acym_getCMSConfig('gmt_offset'); | |
| 184 | - | |
| 185 | - if (empty($value)) { | |
| 186 | - $value = 'UTC'; | |
| 187 | - } elseif ($value < 0) { | |
| 188 | - $value = 'GMT'.$value; | |
| 189 | - } else { | |
| 190 | - $value = 'GMT+'.$value; | |
| 191 | - } | |
| 192 | - } | |
| 193 | - | |
| 194 | - // In WP this could be any number, but Acy pagination only works with 5,10,15,20,25,30,50 or 100 | |
| 195 | - if ($varname === 'posts_per_page') { | |
| 196 | - $possibilities = [5, 10, 15, 20, 25, 30, 50, 100]; | |
| 197 | - $closest = 5; | |
| 198 | - foreach ($possibilities as $possibility) { | |
| 199 | - if (abs($value - $closest) > abs($value - $possibility)) { | |
| 200 | - $closest = $possibility; | |
| 201 | - } | |
| 202 | - } | |
| 203 | - $value = $closest; | |
| 204 | - } | |
| 205 | - | |
| 206 | - return $value; | |
| 207 | -} | |