PluginProbe
Bubble Menu – Floating Button Menu with Sticky Navigation / trunk
Bubble Menu – Floating Button Menu with Sticky Navigation vtrunk
trunk 1.3 2.0 2.2 2.2.1 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1 3.1.1 4.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.1 4.1.1
bubble-menu / classes / Admin / DBManager.php

DBManager.php in Bubble Menu – Floating Button Menu with Sticky Navigation trunk, at classes/Admin/DBManager.php

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