| @@ -1,16 +1,32 @@ | ||
| 1 | 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 | + */ | |
| 2 | 15 | |
| 3 | -function acym_escapeDB($value) | |
| 16 | +defined('ABSPATH') || die('Restricted Access'); | |
| 17 | + | |
| 18 | +function acym_escapeDB(?string $value): string | |
| 4 | 19 | { |
| 5 | 20 | if (is_null($value)) { |
| 6 | 21 | $value = ''; |
| 7 | 22 | } |
| 8 | 23 | |
| 24 | + // esc_sql replaces % by something like {svzzvzevzv} but it's normal, it will be replaced back by % before the query is executed | |
| 9 | 25 | return "'".esc_sql($value)."'"; |
| 10 | 26 | } |
| 11 | 27 | |
| 12 | -function acym_query($query) | |
| 28 | +function acym_query(string $query) | |
| 13 | 29 | { |
| 14 | 30 | global $wpdb; |
| 15 | 31 | $query = acym_prepareQuery($query); |
| 16 | 32 | |
| @@ -18,9 +34,9 @@ | ||
| 18 | 34 | |
| 19 | 35 | return $result === false ? null : $result; |
| 20 | 36 | } |
| 21 | 37 | |
| 22 | -function acym_loadObjectList($query, $key = '', $offset = null, $limit = null): array | |
| 38 | +function acym_loadObjectList(string $query, string $key = '', ?int $offset = null, ?int $limit = null): array | |
| 23 | 39 | { |
| 24 | 40 | global $wpdb; |
| 25 | 41 | $query = acym_prepareQuery($query); |
| 26 | 42 | |
| @@ -40,9 +56,9 @@ | ||
| 40 | 56 | |
| 41 | 57 | return $sorted; |
| 42 | 58 | } |
| 43 | 59 | |
| 44 | -function acym_prepareQuery($query) | |
| 60 | +function acym_prepareQuery(string $query): string | |
| 45 | 61 | { |
| 46 | 62 | global $wpdb; |
| 47 | 63 | $query = str_replace('#__', $wpdb->prefix, $query); |
| 48 | 64 | if (is_multisite()) { |
| @@ -52,9 +68,9 @@ | ||
| 52 | 68 | |
| 53 | 69 | return $query; |
| 54 | 70 | } |
| 55 | 71 | |
| 56 | -function acym_loadObject($query) | |
| 72 | +function acym_loadObject(string $query): ?object | |
| 57 | 73 | { |
| 58 | 74 | acym_addLimit($query); |
| 59 | 75 | |
| 60 | 76 | global $wpdb; |
| @@ -59,12 +75,14 @@ | ||
| 59 | 75 | |
| 60 | 76 | global $wpdb; |
| 61 | 77 | $query = acym_prepareQuery($query); |
| 62 | 78 | |
| 63 | - return $wpdb->get_row($query); | |
| 79 | + $object = $wpdb->get_row($query); | |
| 80 | + | |
| 81 | + return empty($object) ? null : $object; | |
| 64 | 82 | } |
| 65 | 83 | |
| 66 | -function acym_loadResult($query) | |
| 84 | +function acym_loadResult(string $query) | |
| 67 | 85 | { |
| 68 | 86 | global $wpdb; |
| 69 | 87 | $query = acym_prepareQuery($query); |
| 70 | 88 | |
| @@ -70,9 +88,9 @@ | ||
| 70 | 88 | |
| 71 | 89 | return $wpdb->get_var($query); |
| 72 | 90 | } |
| 73 | 91 | |
| 74 | -function acym_loadResultArray($query) | |
| 92 | +function acym_loadResultArray(string $query): array | |
| 75 | 93 | { |
| 76 | 94 | global $wpdb; |
| 77 | 95 | $query = acym_prepareQuery($query); |
| 78 | 96 | |
| @@ -78,9 +96,9 @@ | ||
| 78 | 96 | |
| 79 | 97 | return $wpdb->get_col($query); |
| 80 | 98 | } |
| 81 | 99 | |
| 82 | -function acym_getEscaped($text, $extra = false) | |
| 100 | +function acym_getEscaped(string $text, bool $extra = false) | |
| 83 | 101 | { |
| 84 | 102 | $result = esc_sql($text); |
| 85 | 103 | if ($extra) { |
| 86 | 104 | $result = addcslashes($result, '%_'); |
| @@ -88,16 +106,16 @@ | ||
| 88 | 106 | |
| 89 | 107 | return $result; |
| 90 | 108 | } |
| 91 | 109 | |
| 92 | -function acym_getDBError() | |
| 110 | +function acym_getDBError(): string | |
| 93 | 111 | { |
| 94 | 112 | global $wpdb; |
| 95 | 113 | |
| 96 | - return $wpdb->last_error; | |
| 114 | + return empty($wpdb->last_error) ? '' : $wpdb->last_error; | |
| 97 | 115 | } |
| 98 | 116 | |
| 99 | -function acym_insertObject($table, $element) | |
| 117 | +function acym_insertObject(string $table, object $element): ?int | |
| 100 | 118 | { |
| 101 | 119 | global $wpdb; |
| 102 | 120 | $element = get_object_vars($element); |
| 103 | 121 | $table = acym_prepareQuery($table); |
| @@ -102,21 +120,19 @@ | ||
| 102 | 120 | $element = get_object_vars($element); |
| 103 | 121 | $table = acym_prepareQuery($table); |
| 104 | 122 | $wpdb->insert($table, $element); |
| 105 | 123 | |
| 106 | - return $wpdb->insert_id; | |
| 124 | + $id = $wpdb->insert_id; | |
| 125 | + | |
| 126 | + return empty($id) ? null : (int)$id; | |
| 107 | 127 | } |
| 108 | 128 | |
| 109 | -function acym_updateObject($table, $element, $pkey) | |
| 129 | +function acym_updateObject(string $table, object $element, array $pkey): bool | |
| 110 | 130 | { |
| 111 | 131 | global $wpdb; |
| 112 | 132 | $element = get_object_vars($element); |
| 113 | 133 | $table = acym_prepareQuery($table); |
| 114 | 134 | |
| 115 | - if (!is_array($pkey)) { | |
| 116 | - $pkey = [$pkey]; | |
| 117 | - } | |
| 118 | - | |
| 119 | 135 | $where = []; |
| 120 | 136 | foreach ($pkey as $onePkey) { |
| 121 | 137 | $where[$onePkey] = $element[$onePkey]; |
| 122 | 138 | } |
| @@ -125,9 +141,9 @@ | ||
| 125 | 141 | |
| 126 | 142 | return $nbUpdated !== false; |
| 127 | 143 | } |
| 128 | 144 | |
| 129 | -function acym_getPrefix() | |
| 145 | +function acym_getPrefix(): string | |
| 130 | 146 | { |
| 131 | 147 | global $wpdb; |
| 132 | 148 | |
| 133 | 149 | return $wpdb->prefix; |
| @@ -132,9 +148,9 @@ | ||
| 132 | 148 | |
| 133 | 149 | return $wpdb->prefix; |
| 134 | 150 | } |
| 135 | 151 | |
| 136 | -function acym_getTableList() | |
| 152 | +function acym_getTableList(): array | |
| 137 | 153 | { |
| 138 | 154 | global $wpdb; |
| 139 | 155 | |
| 140 | 156 | return acym_loadResultArray( |
| @@ -141,9 +157,14 @@ | ||
| 141 | 157 | 'SELECT table_name FROM information_schema.tables WHERE table_schema = '.acym_escapeDB($wpdb->dbname).' AND table_name LIKE '.acym_escapeDB($wpdb->prefix.'%') |
| 142 | 158 | ); |
| 143 | 159 | } |
| 144 | 160 | |
| 145 | -function acym_getCMSConfig($varname, $default = null) | |
| 161 | +/** | |
| 162 | + * @param mixed $default | |
| 163 | + * | |
| 164 | + * @return mixed | |
| 165 | + */ | |
| 166 | +function acym_getCMSConfig(string $varname, $default = null) | |
| 146 | 167 | { |
| 147 | 168 | $map = [ |
| 148 | 169 | 'offset' => 'timezone_string', |
| 149 | 170 | 'list_limit' => 'posts_per_page', |
| @@ -156,8 +177,9 @@ | ||
| 156 | 177 | $varname = $map[$varname]; |
| 157 | 178 | } |
| 158 | 179 | $value = get_option($varname, $default); |
| 159 | 180 | |
| 181 | + // In WP there are multiple possible formats in the same option for the timezone | |
| 160 | 182 | if ($varname == 'timezone_string' && empty($value)) { |
| 161 | 183 | $value = acym_getCMSConfig('gmt_offset'); |
| 162 | 184 | |
| 163 | 185 | if (empty($value)) { |
| @@ -168,9 +190,10 @@ | ||
| 168 | 190 | $value = 'GMT+'.$value; |
| 169 | 191 | } |
| 170 | 192 | } |
| 171 | 193 | |
| 172 | - if ($varname == 'posts_per_page') { | |
| 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') { | |
| 173 | 196 | $possibilities = [5, 10, 15, 20, 25, 30, 50, 100]; |
| 174 | 197 | $closest = 5; |
| 175 | 198 | foreach ($possibilities as $possibility) { |
| 176 | 199 | if (abs($value - $closest) > abs($value - $possibility)) { |