PluginProbe
Floating Button – Easily Create Sticky, Fixed & Floating Buttons / 7.0
Floating Button – Easily Create Sticky, Fixed & Floating Buttons v7.0
trunk 2.0.2 3.0 4.0 4.1.2 4.3 5.0 5.0.1 5.1 5.1.1 5.2 5.3 5.3.1 6.0 6.0.1 6.0.10 6.0.11 6.0.12 6.0.13 6.0.2 6.0.3 6.0.4 6.0.5 6.0.6 6.0.7 All 31 releases
floating-button / classes / Admin / DBManager.php

DBManager.php in Floating Button – Easily Create Sticky, Fixed & Floating Buttons 7.0, at classes/Admin/DBManager.php

204 lines 6.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 Float Menu Pro plugin.
4 *
5 * @package FloatingButton\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 FloatingButton\Admin;
24
25 defined( 'ABSPATH' ) || exit;
26
27 use FloatingButton\WOWP_Plugin;
28
29 class DBManager {
30
31 public static function create( $columns ): void {
32 global $wpdb;
33
34 $table = esc_sql( $wpdb->prefix . WOWP_Plugin::PREFIX );
35 $charset_collate = $wpdb->get_charset_collate();
36
37 $sql = "CREATE TABLE $table ($columns) $charset_collate;";
38
39 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
40 dbDelta( $sql );
41 }
42
43 public static function get_columns() {
44 global $wpdb;
45 $table = esc_sql( $wpdb->prefix . WOWP_Plugin::PREFIX );
46
47 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
48 return $wpdb->get_results( "DESCRIBE $table" );
49 }
50
51 public static function insert( $data, $data_formats ) {
52 global $wpdb;
53 $table = esc_sql( $wpdb->prefix . WOWP_Plugin::PREFIX );
54 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
55 $result = $wpdb->insert( $table, $data, $data_formats );
56
57 if ( $result ) {
58 return $wpdb->insert_id;
59
60 }
61
62 return false;
63 }
64
65 public static function update( $data, $where, $data_formats ): void {
66 global $wpdb;
67 $table = esc_sql( $wpdb->prefix . WOWP_Plugin::PREFIX );
68 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
69 $result = $wpdb->update( $table, $data, $where, $data_formats );
70 }
71
72 public static function delete( $id ) {
73 if ( ! isset( $id ) ) {
74 return false;
75 }
76
77 global $wpdb;
78 $table = esc_sql( $wpdb->prefix . WOWP_Plugin::PREFIX );
79
80 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
81 return $wpdb->delete( $table, [ 'id' => $id ], [ '%d' ] );
82
83 }
84
85 public static function remove_item() {
86 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
87 $page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
88 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
89 $action = isset( $_GET['action'] ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : '';
90 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
91 $id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : '';
92
93 if ( ( $page !== WOWP_Plugin::SLUG ) || ( $action !== 'delete' ) || empty( $id ) ) {
94 return false;
95 }
96
97 global $wpdb;
98 $table = esc_sql( $wpdb->prefix . WOWP_Plugin::PREFIX );
99 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
100 $result = $wpdb->delete( $table, [ 'id' => $id ], [ '%d' ] );
101
102 if ( $result ) {
103 wp_safe_redirect( Link::remove_item() );
104 exit;
105 }
106
107 return false;
108 }
109
110 public static function get_all_data() {
111 global $wpdb;
112 $table = esc_sql( $wpdb->prefix . WOWP_Plugin::PREFIX );
113
114 $sql = "SELECT * FROM $table WHERE status = 0";
115
116 if ( ! current_user_can( 'manage_options' ) ) {
117 $sql .= " AND mode = 0";
118 }
119
120 $sql .= " ORDER BY id ASC";
121
122 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
123 $result = $wpdb->get_results( $sql );
124
125 return ( ! empty( $result ) && is_array( $result ) ) ? $result : false;
126 }
127
128 public static function get_data_by_id( $id = '' ) {
129 if ( empty( $id ) ) {
130 return false;
131 }
132 global $wpdb;
133 $table = esc_sql( $wpdb->prefix . WOWP_Plugin::PREFIX );
134
135 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
136 return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE id=%d", absint( $id ) ) );
137 }
138
139 public static function get_data_by_title( $title = '' ) {
140 if ( empty( $title ) ) {
141 return false;
142 }
143
144 global $wpdb;
145 $table = esc_sql( $wpdb->prefix . WOWP_Plugin::PREFIX );
146
147 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
148 return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE title=%s", sanitize_text_field( $title ) ) );
149 }
150
151 public static function get_param_id( $id = '' ) {
152 if ( empty( $id ) ) {
153 return false;
154 }
155 $result = self::get_data_by_id( $id );
156
157 return maybe_unserialize( $result->param );
158 }
159
160 public static function check_row( $id = '' ): bool {
161 global $wpdb;
162 $table = esc_sql( $wpdb->prefix . WOWP_Plugin::PREFIX );
163 if ( empty( $id ) ) {
164 return false;
165 }
166
167 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
168 $check_row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE id = %d", $id ) );
169 if ( ! empty( $check_row ) ) {
170 return true;
171 }
172
173 return false;
174 }
175
176 public static function get_tags_from_table() {
177 global $wpdb;
178 $table = esc_sql( $wpdb->prefix . WOWP_Plugin::PREFIX );
179 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
180 $all_tags = $wpdb->get_results( "SELECT DISTINCT tag FROM $table ORDER BY tag ASC", ARRAY_A );
181
182 return ! empty( $all_tags ) ? $all_tags : false;
183 }
184
185 public static function display_tags(): void {
186 global $wpdb;
187 $table = esc_sql( $wpdb->prefix . WOWP_Plugin::PREFIX );
188 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
189 $result = $wpdb->get_results( "SELECT * FROM $table order by tag desc", ARRAY_A );
190 $tags = [];
191 if ( ! empty( $result ) ) {
192 foreach ( $result as $column ) {
193 if ( ! empty( $column['tag'] ) ) {
194 $tags[ $column['tag'] ] = $column['tag'];
195 }
196 }
197 }
198 if ( ! empty( $tags ) ) {
199 foreach ( $tags as $tag ) {
200 echo '<option value="' . esc_attr( $tag ) . '">';
201 }
202 }
203 }
204 }