PluginProbe ʕ •ᴥ•ʔ
AI Copilot – Content Generator / 1.4.21
AI Copilot – Content Generator v1.4.21
1.5.4 1.4.21 1.4.18 1.4.19 1.4.20 trunk 1.0.4 1.1.0 1.2.0 1.2.1 1.2.10 1.2.11 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 1.4.14 1.4.15 1.4.17 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.9
ai-copilot-content-generator / classes / db.php
ai-copilot-content-generator / classes Last commit date
helpers 1 month ago tables 1 month ago aIProviderInterface.php 1 month ago assets.php 1 month ago baseObject.php 1 month ago builderBlock.php 1 month ago controller.php 1 month ago date.php 1 month ago db.php 1 month ago dispatcher.php 1 month ago errors.php 1 month ago field.php 1 month ago fieldAdapter.php 1 month ago frame.php 1 month ago helper.php 1 month ago html.php 1 month ago installer.php 1 month ago installerDbUpdater.php 1 month ago integration.php 1 month ago modInstaller.php 1 month ago model.php 1 month ago module.php 1 month ago req.php 1 month ago response.php 1 month ago table.php 1 month ago uri.php 1 month ago user.php 1 month ago utils.php 1 month ago validator.php 1 month ago view.php 1 month ago
db.php
237 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 /**
45 * Execute one query
46 *
47 * @return query results
48 */
49 public static function query( $query, $affected = false, $args = array(1) ) {
50 global $wpdb;
51 $wpdb->waic_prepared_query = self::prepareQuery($query, $args);
52 // phpcs:ignore WordPress.DB.DirectDatabaseQuery
53 return $affected ? $wpdb->query($wpdb->waic_prepared_query) : ( $wpdb->query($wpdb->waic_prepared_query) === false ? false : true );
54 }
55 /**
56 * Get last insert ID
57 *
58 * @return int last ID
59 */
60 public static function insertID() {
61 global $wpdb;
62 return $wpdb->insert_id;
63 }
64 /**
65 * Get number of rows returned by last query
66 *
67 * @return int number of rows
68 */
69 public static function numRows() {
70 global $wpdb;
71 return $wpdb->num_rows;
72 }
73 /**
74 * Replace prefixes in custom query. Suported next prefixes:
75 * #__ Wordpress prefix
76 * ^__ Store plugin tables prefix (@see WAIC_DB_PREF if config.php)
77 *
78 * @__ Compared of WP table prefix + Store plugin prefix (@example wp_s_)
79 * @param string $query query to be executed
80 */
81 public static function prepareQuery( $query, &$args = array(1) ) {
82 global $wpdb;
83 if (self::$prepareQ) {
84 $query = $wpdb->prepare($query); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
85 }
86 if (empty($args)) {
87 $args = array(1);
88 $found = false;
89 $q = strtolower($query);
90 $where = strpos($q, ' where ') !== false;
91
92 if (strpos($q, ' where ') !== false) {
93 if (strpos($query, ' WHERE ') !== false) {
94 $query = str_replace(' WHERE ', ' WHERE 1=%d AND ', $query);
95 } else if (strpos($query, ' where ') !== false) {
96 $query = str_replace(' where ', ' where 1=%d AND ', $query);
97 }
98 } else {
99 $elements = array(' group by ' => ' GROUP BY ', ' order by ' => ' ORDER BY ', ' limit ' => ' LIMIT ');
100 foreach ($elements as $l => $u) {
101 if (strpos($q, $l) !== false) {
102 if (strpos($query, $u) !== false) {
103 $query = str_replace($u, ' WHERE 1=%d' . $u, $query);
104 $found = true;
105 } else if (strpos($query, $l) !== false) {
106 $query = str_replace($l, ' where 1=%d' . $l, $query);
107 $found = true;
108 }
109 }
110 if ($found) {
111 break;
112 }
113 }
114 if (!$found) {
115 $query .= ' WHERE 1=%d';
116 }
117 }
118 }
119 return str_replace(
120 array('#__', '^__', '@__'),
121 array($wpdb->prefix, WAIC_DB_PREF, $wpdb->prefix . WAIC_DB_PREF),
122 $query);
123 }
124 public static function getError() {
125 global $wpdb;
126 return $wpdb->last_error;
127 }
128 public static function lastID() {
129 global $wpdb;
130 return $wpdb->insert_id;
131 }
132 public static function timeToDate( $timestamp = 0 ) {
133 if ($timestamp) {
134 if (!is_numeric($timestamp)) {
135 $timestamp = waicDateToTimestamp($timestamp);
136 }
137 return gmdate('Y-m-d', $timestamp);
138 } else {
139 return gmdate('Y-m-d');
140 }
141 }
142 public static function dateToTime( $date ) {
143 if (empty($date)) {
144 return '';
145 }
146 if (strpos($date, WAIC_DATE_DL)) {
147 return waicDateToTimestamp($date);
148 }
149 $arr = explode('-', $date);
150 return waicDateToTimestamp($arr[2] . WAIC_DATE_DL . $arr[1] . WAIC_DATE_DL . $arr[0]);
151 }
152 public static function exist( $table, $column = '', $value = '' ) {
153 $table = self::controlTableName($table);
154 if (empty($column) && empty($value)) { //Check if table exist
155 $args = array(1);
156 $res = self::get('SHOW TABLES LIKE %s', 'one', ARRAY_A, array(self::prepareQuery($table, $args)));
157 } elseif (empty($value)) { //Check if column exist
158 $res = self::get('SHOW COLUMNS FROM ' . $table . ' LIKE %s', 'one', ARRAY_A, array($column));
159 } else { //Check if value in column table exist
160 $res = self::get("SELECT COUNT(*) AS total FROM `{$table}` WHERE {$column} = %s", 'one', ARRAY_A, array($value));
161 }
162 return !empty($res);
163 }
164 public static function prepareHtml( $d ) {
165 if (is_array($d)) {
166 foreach ($d as $i => $el) {
167 $d[ $i ] = self::prepareHtml( $el );
168 }
169 } else {
170 $d = esc_html($d);
171 }
172 return $d;
173 }
174 public static function prepareHtmlIn( $d ) {
175 if (is_array($d)) {
176 foreach ($d as $i => $el) {
177 $d[ $i ] = self::prepareHtml( $el );
178 }
179 } else {
180 $d = wp_filter_nohtml_kses($d);
181 }
182 return $d;
183 }
184 public static function escape( $data ) {
185 global $wpdb;
186 return $wpdb->_escape($data);
187 }
188 public static function getTableColumns( $table ) {
189 $table = self::controlTableName($table);
190 return self::get("SHOW COLUMNS FROM {$table}");
191 }
192 public static function getAutoIncrement( $table ) {
193 $table = self::controlTableName($table);
194 return (int) self::get('SELECT AUTO_INCREMENT
195 FROM information_schema.tables
196 WHERE table_name = %s
197 AND table_schema = DATABASE( );', 'one', ARRAY_A, array($table));
198 }
199 public static function setAutoIncrement( $table, $autoIncrement ) {
200 $table = self::controlTableName($table);
201 return self::query("ALTER TABLE `{$table}` AUTO_INCREMENT = %d", false, array($autoIncrement));
202 }
203 public static function createTemporaryTable( $table, $sql, $strusture = false ) {
204 $resultTable = $table;
205 if (!self::query('DROP TEMPORARY TABLE IF EXISTS ' . $table )) {
206 return false;
207 }
208 if (!empty($sql)) {
209 $sql = str_replace('SQL_CALC_FOUND_ROWS', '', $sql);
210 $orderPos = strpos($sql, 'ORDER');
211 if ($orderPos) {
212 $sql = substr($sql, 0, $orderPos);
213 }
214 }
215 $query = 'CREATE TEMPORARY TABLE ' . $table .
216 ' (' . ( $strusture ? $strusture : 'index my_pkey (id)' ) . ')' .
217 ( empty($sql) ? '' : ' AS ' . $sql );
218 if (self::query($query, false) === false ) {
219 $resultTable = empty($sql) ? false : '(' . $sql . ')';
220 }
221
222 return $resultTable;
223 }
224 public static function existsTableColumn( $table, $column ) {
225 $table = self::controlTableName($table);
226 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;
227 }
228 public static function controlTableName( $table ) {
229 global $wpdb;
230 $table = str_replace(
231 array('#__', '^__', '@__'),
232 array($wpdb->prefix, WAIC_DB_PREF, $wpdb->prefix . WAIC_DB_PREF),
233 $table);
234 return sanitize_key($table);
235 }
236 }
237