PluginProbe
WP Coder – Insert & Manage Code Snippets / trunk
WP Coder – Insert & Manage Code Snippets vtrunk
4.5.1 1.1 2.3.1 2.3.2 2.4.1 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1 3.1.1 3.2 3.2.1 3.3 3.4 3.5 3.5.1 All 39 releases
wp-coder / classes / Dashboard / DBManager.php

DBManager.php in WP Coder – Insert & Manage Code Snippets trunk, at classes/Dashboard/DBManager.php

221 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * DBManager class for WP Coder plugin.
4 *
5 * @package WPCoder\Admin
6 *
7 * Methods:
8 * - create() Create database table
9 * - get_columns() Get table column structure
10 * - insert() Insert new row
11 * - update() Update existing row
12 * - delete() Delete row by ID
13 * - remove_item() Handle item removal from GET request
14 * - get_all_data() Get all rows from table
15 * - get_data_by_id() Get single row by ID
16 * - get_data_by_title() Get row by title
17 * - check_row() Check if row exists by ID
18 * - get_tags_from_table() Get unique tags
19 * - display_tags() Output HTML <option> tags for tags
20 */
21
22 namespace WPCoder\Dashboard;
23
24 defined( 'ABSPATH' ) || exit;
25
26 use WPCoder\WPCoder;
27
28 class DBManager {
29
30 public static function remove_item() {
31 // phpcs:disable WordPress.Security.NonceVerification.Recommended
32 $page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
33 $action = isset( $_GET['action'] ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : '';
34 $id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : '';
35 // phpcs:enable
36 if ( ( $page !== WPCoder::SLUG ) || ( $action !== 'delete' ) || empty( $id ) ) {
37 return false;
38 }
39
40 global $wpdb;
41 $table = $wpdb->prefix . WPCoder::PREFIX;
42
43 $result = $wpdb->delete( $table, [ 'id' => $id ], [ '%d' ] );
44
45 if ( $result ) {
46 wp_safe_redirect( Link::remove_item() );
47 exit;
48 }
49
50 return false;
51 }
52
53
54 public static function delete( $id ) {
55 if ( ! isset( $id ) ) {
56 return false;
57 }
58
59 global $wpdb;
60 $table = $wpdb->prefix . WPCoder::PREFIX;
61
62 return $wpdb->delete( $table, [ 'id' => $id ], [ '%d' ] );
63 }
64
65 public static function create( $columns ): void {
66 global $wpdb;
67 $table = $wpdb->prefix . WPCoder::PREFIX;
68 $charset_collate = $wpdb->get_charset_collate();
69
70 $sql = "CREATE TABLE $table ($columns) $charset_collate;";
71
72 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
73 dbDelta( $sql );
74 }
75
76 public static function get_all_data() {
77 global $wpdb;
78 $table = $wpdb->prefix . WPCoder::PREFIX;
79 $result = $wpdb->get_results( "SELECT * FROM $table ORDER BY id ASC" );
80
81 return ! empty( $result ) ? $result : false;
82 }
83
84 public static function get_data_by_tag( $tag = '' ) {
85 if ( empty( $tag ) ) {
86 return false;
87 }
88
89 global $wpdb;
90 $table = esc_sql( $wpdb->prefix . WPCoder::PREFIX );
91 $query = $wpdb->prepare( "SELECT * FROM {$table} WHERE tag = %s ORDER BY id ASC", sanitize_text_field( $tag ) );
92 $result = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.*
93
94 return ! empty( $result ) ? $result : false;
95 }
96
97 public static function get_data_by_id( $id = '' ) {
98 if ( empty( $id ) ) {
99 return false;
100 }
101 global $wpdb;
102 $table = esc_sql( $wpdb->prefix . WPCoder::PREFIX );
103
104 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
105 return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE id=%d", absint( $id ) ) );
106 }
107
108 public static function get_data_by_ids( $ids = [] ) {
109 if ( empty( $ids ) || ! is_array( $ids ) ) {
110 return false;
111 }
112
113 global $wpdb;
114 $table = esc_sql( $wpdb->prefix . WPCoder::PREFIX );
115
116 $ids = array_filter( array_map( 'absint', $ids ) );
117
118 if ( empty( $ids ) ) {
119 return false;
120 }
121
122 $placeholders = implode( ',', array_fill( 0, count( $ids ), '%d' ) );
123 $query = $wpdb->prepare(
124 "SELECT * FROM {$table} WHERE id IN ($placeholders)",
125 ...$ids
126 );
127
128 $result = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.*
129
130 return ! empty( $result ) ? $result : false;
131 }
132
133 public static function get_data_by_title( $title = '' ) {
134 if ( empty( $title ) ) {
135 return false;
136 }
137
138 global $wpdb;
139 $table = esc_sql( $wpdb->prefix . WPCoder::PREFIX );
140
141 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
142 return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE title=%s", esc_attr( $title ) ) );
143 }
144
145 public static function update( $data, $where, $data_formats ): void {
146 if ( ! current_user_can( 'unfiltered_html' ) ) {
147 return;
148 }
149
150 global $wpdb;
151 $table = $wpdb->prefix . WPCoder::PREFIX;
152 $result = $wpdb->update( $table, $data, $where, $data_formats );
153 }
154
155 public static function insert( $data, $data_formats ) {
156 if ( ! current_user_can( 'unfiltered_html' ) ) {
157 return false;
158 }
159
160 global $wpdb;
161 $table = $wpdb->prefix . WPCoder::PREFIX;
162
163 $result = $wpdb->insert( $table, $data, $data_formats );
164
165 if ( $result ) {
166 return $wpdb->insert_id;
167 }
168
169 return false;
170 }
171
172 public static function check_row( $id = '' ): bool {
173 global $wpdb;
174 $table = $wpdb->prefix . WPCoder::PREFIX;
175 if ( empty( $id ) ) {
176 return false;
177 }
178
179 $check_row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE id = %d", $id ) );
180 if ( ! empty( $check_row ) ) {
181 return true;
182 }
183
184 return false;
185 }
186
187 public static function get_columns() {
188 global $wpdb;
189 $table_name = $wpdb->prefix . WPCoder::PREFIX;
190
191 return $wpdb->get_results( "DESCRIBE $table_name" );
192 }
193
194 public static function display_tags(): void {
195 global $wpdb;
196 $table = $wpdb->prefix . WPCoder::PREFIX;
197 $result = $wpdb->get_results( "SELECT * FROM $table order by tag desc", ARRAY_A );
198 $tags = [];
199 if ( ! empty( $result ) ) {
200 foreach ( $result as $column ) {
201 if ( ! empty( $column['tag'] ) ) {
202 $tags[ $column['tag'] ] = $column['tag'];
203 }
204 }
205 }
206 if ( ! empty( $tags ) ) {
207 foreach ( $tags as $tag ) {
208 echo '<option value="' . esc_attr( $tag ) . '">';
209 }
210 }
211 }
212
213 public static function get_tags_from_table() {
214 global $wpdb;
215 $table = $wpdb->prefix . WPCoder::PREFIX;
216 $all_tags = $wpdb->get_results( "SELECT DISTINCT tag FROM $table ORDER BY tag ASC", ARRAY_A );
217
218 return ! empty( $all_tags ) ? $all_tags : false;
219 }
220
221 }