helpers
1 week ago
tables
1 week ago
aIProviderInterface.php
1 week ago
assets.php
1 week ago
baseObject.php
1 week ago
builderBlock.php
1 week ago
controller.php
1 week ago
date.php
1 week ago
db.php
1 week ago
dispatcher.php
1 week ago
errors.php
1 week ago
field.php
1 week ago
fieldAdapter.php
1 week ago
frame.php
1 week ago
helper.php
1 week ago
html.php
1 week ago
installer.php
1 week ago
installerDbUpdater.php
1 week ago
integration.php
1 week ago
modInstaller.php
1 week ago
model.php
1 week ago
module.php
1 week ago
req.php
1 week ago
response.php
1 week ago
table.php
1 week ago
uri.php
1 week ago
user.php
1 week ago
utils.php
1 week ago
validator.php
1 week ago
view.php
1 week ago
db.php
245 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | /** |
| 6 | * Shell - class to work with $wpdb global object |
| 7 | */ |
| 8 | class WaicDb { |
| 9 | public static $prepareQ = false; |
| 10 | /** |
| 11 | * Execute query and return results |
| 12 | * |
| 13 | * @param string $query query to be executed |
| 14 | * @param string $get what must be returned - one value (one), one row (row), one col (col) or all results (all - by default) |
| 15 | * @param const $outputType type of returned data |
| 16 | * @return mixed data from DB |
| 17 | */ |
| 18 | public static $query = ''; |
| 19 | public static function get( $query, $get = 'all', $outputType = ARRAY_A, $args = array() ) { |
| 20 | global $wpdb; |
| 21 | $get = strtolower($get); |
| 22 | $res = null; |
| 23 | $query = self::prepareQuery($query, $args); |
| 24 | self::$query = $query; |
| 25 | $wpdb->waic_prepared_query = $wpdb->prepare($query, $args); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 26 | |
| 27 | switch ($get) { |
| 28 | case 'one': |
| 29 | $res = $wpdb->get_var($wpdb->waic_prepared_query); // phpcs:ignore WordPress.DB.DirectDatabaseQuery |
| 30 | break; |
| 31 | case 'row': |
| 32 | $res = $wpdb->get_row($wpdb->waic_prepared_query, $outputType); // phpcs:ignore WordPress.DB.DirectDatabaseQuery |
| 33 | break; |
| 34 | case 'col': |
| 35 | $res = $wpdb->get_col($wpdb->waic_prepared_query); // phpcs:ignore WordPress.DB.DirectDatabaseQuery |
| 36 | break; |
| 37 | case 'all': |
| 38 | default: |
| 39 | $res = $wpdb->get_results($wpdb->waic_prepared_query, $outputType); // phpcs:ignore WordPress.DB.DirectDatabaseQuery |
| 40 | break; |
| 41 | } |
| 42 | return $res; |
| 43 | } |
| 44 | public static function placeholders( $values, $type = '%d' ) { |
| 45 | $count = is_array($values) ? count($values) : (int) $values; |
| 46 | if ($count <= 0) { |
| 47 | return ''; |
| 48 | } |
| 49 | $type = in_array($type, array('%d', '%s', '%f'), true) ? $type : '%d'; |
| 50 | return implode(',', array_fill(0, $count, $type)); |
| 51 | } |
| 52 | /** |
| 53 | * Execute one query |
| 54 | * |
| 55 | * @return query results |
| 56 | */ |
| 57 | public static function query( $query, $affected = false, $args = array(1) ) { |
| 58 | global $wpdb; |
| 59 | $wpdb->waic_prepared_query = self::prepareQuery($query, $args); |
| 60 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery |
| 61 | return $affected ? $wpdb->query($wpdb->waic_prepared_query) : ( $wpdb->query($wpdb->waic_prepared_query) === false ? false : true ); |
| 62 | } |
| 63 | /** |
| 64 | * Get last insert ID |
| 65 | * |
| 66 | * @return int last ID |
| 67 | */ |
| 68 | public static function insertID() { |
| 69 | global $wpdb; |
| 70 | return $wpdb->insert_id; |
| 71 | } |
| 72 | /** |
| 73 | * Get number of rows returned by last query |
| 74 | * |
| 75 | * @return int number of rows |
| 76 | */ |
| 77 | public static function numRows() { |
| 78 | global $wpdb; |
| 79 | return $wpdb->num_rows; |
| 80 | } |
| 81 | /** |
| 82 | * Replace prefixes in custom query. Suported next prefixes: |
| 83 | * #__ Wordpress prefix |
| 84 | * ^__ Store plugin tables prefix (@see WAIC_DB_PREF if config.php) |
| 85 | * |
| 86 | * @__ Compared of WP table prefix + Store plugin prefix (@example wp_s_) |
| 87 | * @param string $query query to be executed |
| 88 | */ |
| 89 | public static function prepareQuery( $query, &$args = array(1) ) { |
| 90 | global $wpdb; |
| 91 | if (self::$prepareQ) { |
| 92 | $query = $wpdb->prepare($query); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 93 | } |
| 94 | if (empty($args)) { |
| 95 | $args = array(1); |
| 96 | $found = false; |
| 97 | $q = strtolower($query); |
| 98 | $where = strpos($q, ' where ') !== false; |
| 99 | |
| 100 | if (strpos($q, ' where ') !== false) { |
| 101 | if (strpos($query, ' WHERE ') !== false) { |
| 102 | $query = str_replace(' WHERE ', ' WHERE 1=%d AND ', $query); |
| 103 | } else if (strpos($query, ' where ') !== false) { |
| 104 | $query = str_replace(' where ', ' where 1=%d AND ', $query); |
| 105 | } |
| 106 | } else { |
| 107 | $elements = array(' group by ' => ' GROUP BY ', ' order by ' => ' ORDER BY ', ' limit ' => ' LIMIT '); |
| 108 | foreach ($elements as $l => $u) { |
| 109 | if (strpos($q, $l) !== false) { |
| 110 | if (strpos($query, $u) !== false) { |
| 111 | $query = str_replace($u, ' WHERE 1=%d' . $u, $query); |
| 112 | $found = true; |
| 113 | } else if (strpos($query, $l) !== false) { |
| 114 | $query = str_replace($l, ' where 1=%d' . $l, $query); |
| 115 | $found = true; |
| 116 | } |
| 117 | } |
| 118 | if ($found) { |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | if (!$found) { |
| 123 | $query .= ' WHERE 1=%d'; |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | return str_replace( |
| 128 | array('#__', '^__', '@__'), |
| 129 | array($wpdb->prefix, WAIC_DB_PREF, $wpdb->prefix . WAIC_DB_PREF), |
| 130 | $query); |
| 131 | } |
| 132 | public static function getError() { |
| 133 | global $wpdb; |
| 134 | return $wpdb->last_error; |
| 135 | } |
| 136 | public static function lastID() { |
| 137 | global $wpdb; |
| 138 | return $wpdb->insert_id; |
| 139 | } |
| 140 | public static function timeToDate( $timestamp = 0 ) { |
| 141 | if ($timestamp) { |
| 142 | if (!is_numeric($timestamp)) { |
| 143 | $timestamp = waicDateToTimestamp($timestamp); |
| 144 | } |
| 145 | return gmdate('Y-m-d', $timestamp); |
| 146 | } else { |
| 147 | return gmdate('Y-m-d'); |
| 148 | } |
| 149 | } |
| 150 | public static function dateToTime( $date ) { |
| 151 | if (empty($date)) { |
| 152 | return ''; |
| 153 | } |
| 154 | if (strpos($date, WAIC_DATE_DL)) { |
| 155 | return waicDateToTimestamp($date); |
| 156 | } |
| 157 | $arr = explode('-', $date); |
| 158 | return waicDateToTimestamp($arr[2] . WAIC_DATE_DL . $arr[1] . WAIC_DATE_DL . $arr[0]); |
| 159 | } |
| 160 | public static function exist( $table, $column = '', $value = '' ) { |
| 161 | $table = self::controlTableName($table); |
| 162 | if (empty($column) && empty($value)) { //Check if table exist |
| 163 | $args = array(1); |
| 164 | $res = self::get('SHOW TABLES LIKE %s', 'one', ARRAY_A, array(self::prepareQuery($table, $args))); |
| 165 | } elseif (empty($value)) { //Check if column exist |
| 166 | $res = self::get('SHOW COLUMNS FROM ' . $table . ' LIKE %s', 'one', ARRAY_A, array($column)); |
| 167 | } else { //Check if value in column table exist |
| 168 | $res = self::get("SELECT COUNT(*) AS total FROM `{$table}` WHERE {$column} = %s", 'one', ARRAY_A, array($value)); |
| 169 | } |
| 170 | return !empty($res); |
| 171 | } |
| 172 | public static function prepareHtml( $d ) { |
| 173 | if (is_array($d)) { |
| 174 | foreach ($d as $i => $el) { |
| 175 | $d[ $i ] = self::prepareHtml( $el ); |
| 176 | } |
| 177 | } else { |
| 178 | $d = esc_html($d); |
| 179 | } |
| 180 | return $d; |
| 181 | } |
| 182 | public static function prepareHtmlIn( $d ) { |
| 183 | if (is_array($d)) { |
| 184 | foreach ($d as $i => $el) { |
| 185 | $d[ $i ] = self::prepareHtml( $el ); |
| 186 | } |
| 187 | } else { |
| 188 | $d = wp_filter_nohtml_kses($d); |
| 189 | } |
| 190 | return $d; |
| 191 | } |
| 192 | public static function escape( $data ) { |
| 193 | global $wpdb; |
| 194 | return $wpdb->_escape($data); |
| 195 | } |
| 196 | public static function getTableColumns( $table ) { |
| 197 | $table = self::controlTableName($table); |
| 198 | return self::get("SHOW COLUMNS FROM {$table}"); |
| 199 | } |
| 200 | public static function getAutoIncrement( $table ) { |
| 201 | $table = self::controlTableName($table); |
| 202 | return (int) self::get('SELECT AUTO_INCREMENT |
| 203 | FROM information_schema.tables |
| 204 | WHERE table_name = %s |
| 205 | AND table_schema = DATABASE( );', 'one', ARRAY_A, array($table)); |
| 206 | } |
| 207 | public static function setAutoIncrement( $table, $autoIncrement ) { |
| 208 | $table = self::controlTableName($table); |
| 209 | return self::query("ALTER TABLE `{$table}` AUTO_INCREMENT = %d", false, array($autoIncrement)); |
| 210 | } |
| 211 | public static function createTemporaryTable( $table, $sql, $strusture = false ) { |
| 212 | $resultTable = $table; |
| 213 | if (!self::query('DROP TEMPORARY TABLE IF EXISTS ' . $table )) { |
| 214 | return false; |
| 215 | } |
| 216 | if (!empty($sql)) { |
| 217 | $sql = str_replace('SQL_CALC_FOUND_ROWS', '', $sql); |
| 218 | $orderPos = strpos($sql, 'ORDER'); |
| 219 | if ($orderPos) { |
| 220 | $sql = substr($sql, 0, $orderPos); |
| 221 | } |
| 222 | } |
| 223 | $query = 'CREATE TEMPORARY TABLE ' . $table . |
| 224 | ' (' . ( $strusture ? $strusture : 'index my_pkey (id)' ) . ')' . |
| 225 | ( empty($sql) ? '' : ' AS ' . $sql ); |
| 226 | if (self::query($query, false) === false ) { |
| 227 | $resultTable = empty($sql) ? false : '(' . $sql . ')'; |
| 228 | } |
| 229 | |
| 230 | return $resultTable; |
| 231 | } |
| 232 | public static function existsTableColumn( $table, $column ) { |
| 233 | $table = self::controlTableName($table); |
| 234 | return self::get("SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name=%s AND table_schema=DATABASE( ) AND column_name=%s", 'one', ARRAY_A, array($table, $column)) == 1; |
| 235 | } |
| 236 | public static function controlTableName( $table ) { |
| 237 | global $wpdb; |
| 238 | $table = str_replace( |
| 239 | array('#__', '^__', '@__'), |
| 240 | array($wpdb->prefix, WAIC_DB_PREF, $wpdb->prefix . WAIC_DB_PREF), |
| 241 | $table); |
| 242 | return sanitize_key($table); |
| 243 | } |
| 244 | } |
| 245 |