PluginProbe
Counter Box – Add Countdowns, Timers & Dynamic Counters to WordPress / trunk
Counter Box – Add Countdowns, Timers & Dynamic Counters to WordPress vtrunk
2.0.14 trunk 1.0 1.2 1.2.1 1.2.2 1.2.3 1.2.4 2.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9
counter-box / classes / Admin / DBManager.php

DBManager.php in Counter Box – Add Countdowns, Timers & Dynamic Counters to WordPress trunk, at classes/Admin/DBManager.php

243 lines 7.7 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 Counter Box plugin.
4 *
5 * @package CounterBox\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 * - get_param_id() Get and unserialize param by ID
18 * - check_row() Check if row exists by ID
19 * - get_tags_from_table() Get unique tags
20 * - display_tags() Output HTML <option> tags for tags
21 */
22
23 namespace CounterBox\Admin;
24
25 defined( 'ABSPATH' ) || exit;
26
27 use CounterBox\WOWP_Plugin;
28
29 class DBManager {
30
31 /**
32 * Create database table.
33 */
34 public static function create( $columns ): void {
35 global $wpdb;
36
37 $table = $wpdb->prefix . WOWP_Plugin::PREFIX;
38 $charset_collate = $wpdb->get_charset_collate();
39 $sql = "CREATE TABLE {$table} ($columns) $charset_collate;";
40
41 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
42 dbDelta( $sql );
43 }
44
45 /**
46 * Get table columns.
47 */
48 public static function get_columns() {
49 global $wpdb;
50 $table = esc_sql( $wpdb->prefix . WOWP_Plugin::PREFIX );
51
52 return $wpdb->get_results( "DESCRIBE {$table}" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
53 }
54
55 /**
56 * Insert new row.
57 */
58 public static function insert( $data, $data_formats ) {
59 global $wpdb;
60 $table = esc_sql( $wpdb->prefix . WOWP_Plugin::PREFIX );
61
62 $wpdb->insert( $table, $data, $data_formats ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
63
64 return $wpdb->insert_id ?: false;
65 }
66
67 /**
68 * Update row.
69 */
70 public static function update( $data, $where, $data_formats ): void {
71 global $wpdb;
72 $table = esc_sql( $wpdb->prefix . WOWP_Plugin::PREFIX );
73
74 $wpdb->update( $table, $data, $where, $data_formats ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
75 }
76
77 /**
78 * Delete row by ID.
79 */
80 public static function delete( $id ) {
81 if ( empty( $id ) ) {
82 return false;
83 }
84
85 global $wpdb;
86 $table = $wpdb->prefix . WOWP_Plugin::PREFIX;
87
88 return $wpdb->delete( $table, [ 'id' => $id ], [ '%d' ] ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
89 }
90
91 /**
92 * Remove item via GET request with nonce verification.
93 */
94 public static function remove_item() {
95 if ( ! AdminActions::verify( WOWP_Plugin::PREFIX . '_remove_item' ) ) {
96 return false;
97 }
98 // phpcs:disable WordPress.Security.NonceVerification.Recommended
99 $page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
100 $action = isset( $_GET['action'] ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : '';
101 $id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : '';
102 // phpcs:enable
103
104 if ( ( $page !== WOWP_Plugin::SLUG ) || ( $action !== 'delete' ) || empty( $id ) ) {
105 return false;
106 }
107
108 global $wpdb;
109 $table = $wpdb->prefix . WOWP_Plugin::PREFIX;
110 $result = $wpdb->delete( $table, [ 'id' => $id ], [ '%d' ] ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
111
112 if ( $result ) {
113 wp_safe_redirect( Link::remove_item() );
114 exit;
115 }
116
117 return false;
118 }
119
120 /**
121 * Get all rows.
122 */
123 public static function get_all_data() {
124 global $wpdb;
125
126 $table = esc_sql( $wpdb->prefix . WOWP_Plugin::PREFIX );
127 $result = $wpdb->get_results( "SELECT * FROM {$table} ORDER BY id ASC" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
128
129 return ( ! empty( $result ) && is_array( $result ) ) ? $result : false;
130 }
131
132 /**
133 * Get row by ID.
134 */
135 public static function get_data_by_id( $id = 0 ) {
136 if ( empty( $id ) ) {
137 return false;
138 }
139 global $wpdb;
140 $table = esc_sql( $wpdb->prefix . WOWP_Plugin::PREFIX );
141
142 return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE id=%d", absint( $id ) ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
143 }
144
145 /**
146 * Get row by title.
147 */
148 public static function get_data_by_title( $title = '' ) {
149 if ( empty( $title ) ) {
150 return false;
151 }
152
153 global $wpdb;
154 $table = esc_sql( $wpdb->prefix . WOWP_Plugin::PREFIX );
155
156 return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE title=%s", sanitize_text_field( $title ) ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
157 }
158
159 /**
160 * Get param value from row by ID.
161 */
162 public static function get_param_id( $id = 0 ) {
163 if ( empty( $id ) ) {
164 return false;
165 }
166 $result = self::get_data_by_id( $id );
167
168 return isset( $result->param ) ? self::safe_unserialize( $result->param ) : false;
169 }
170
171 /**
172 * Safely unserialize a stored value.
173 *
174 * The `param` field always holds a serialized array of settings, never an
175 * object. Passing `allowed_classes => false` prevents PHP Object Injection:
176 * a crafted serialized object (e.g. imported via JSON) can no longer be
177 * instantiated, so magic methods such as __wakeup()/__destruct() never fire.
178 *
179 * @param mixed $data Value to unserialize.
180 *
181 * @return mixed Unserialized array/scalar, or the original value if not serialized.
182 */
183 public static function safe_unserialize( $data ) {
184 if ( ! is_string( $data ) || ! is_serialized( $data ) ) {
185 return $data;
186 }
187
188 return unserialize( trim( $data ), [ 'allowed_classes' => false ] ); // phpcs:ignore WordPress.PHP.NoSilencedErrors, PHPCompatibility.FunctionUse.NewFunctionParameters.unserialize_optionsFound
189 }
190
191 /**
192 * Check if row exists by ID.
193 */
194 public static function check_row( $id = 0 ): bool {
195 if ( empty( $id ) ) {
196 return false;
197 }
198
199 global $wpdb;
200 $table = esc_sql( $wpdb->prefix . WOWP_Plugin::PREFIX );
201
202 $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE id = %d", absint( $id ) ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
203
204 return ! empty( $row );
205 }
206
207 /**
208 * Get unique tags from the table.
209 */
210 public static function get_tags_from_table() {
211 global $wpdb;
212 $table = esc_sql( $wpdb->prefix . WOWP_Plugin::PREFIX );
213
214 $all_tags = $wpdb->get_results( "SELECT DISTINCT tag FROM {$table} ORDER BY tag ASC", ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
215
216 return ! empty( $all_tags ) ? $all_tags : false;
217 }
218
219 /**
220 * Output <option> tags for unique tags.
221 */
222 public static function display_tags(): void {
223 global $wpdb;
224 $table = esc_sql( $wpdb->prefix . WOWP_Plugin::PREFIX );
225 $tags = [];
226
227 $result = $wpdb->get_results( "SELECT * FROM {$table} order by tag DESC", ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
228
229 if ( ! empty( $result ) ) {
230 foreach ( $result as $column ) {
231 if ( ! empty( $column['tag'] ) ) {
232 $tags[ $column['tag'] ] = $column['tag'];
233 }
234 }
235 }
236 if ( ! empty( $tags ) ) {
237 foreach ( $tags as $tag ) {
238 printf( '<option value="%s"></option>', esc_attr( $tag ) );
239 }
240 }
241 }
242
243 }