PluginProbe
WP Coder – Insert & Manage Code Snippets / 3.2
WP Coder – Insert & Manage Code Snippets v3.2
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 3.2, at classes/Dashboard/DBManager.php

161 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPCoder\Dashboard;
4
5 defined( 'ABSPATH' ) || exit;
6
7 use WPCoder\WPCoder;
8
9 class DBManager {
10
11 public static function remove_item() {
12 $page = isset( $_GET['page'] ) ? sanitize_text_field( $_GET['page'] ) : '';
13 $action = isset( $_GET['action'] ) ? sanitize_text_field( $_GET['action'] ) : '';
14 $id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : '';
15
16 if ( ( $page !== WPCoder::SLUG ) || ( $action !== 'delete' ) || empty( $id ) ) {
17 return false;
18 }
19
20 global $wpdb;
21 $table = $wpdb->prefix . WPCoder::PREFIX;
22
23 $result = $wpdb->delete( $table, [ 'id' => $id ], [ '%d' ] );
24
25 if ( $result ) {
26 wp_safe_redirect( Link::remove_item() );
27 exit;
28 }
29
30 return false;
31 }
32
33
34 public static function delete( $id ) {
35 if ( ! isset( $id ) ) {
36 return false;
37 }
38
39 global $wpdb;
40 $table = $wpdb->prefix . WPCoder::PREFIX;
41
42 return $wpdb->delete( $table, [ 'id' => $id ], [ '%d' ] );
43 }
44
45 public static function create( $columns ): void {
46 global $wpdb;
47 $table = $wpdb->prefix . WPCoder::PREFIX;
48
49 $sql = "CREATE TABLE $table ($columns) DEFAULT CHARACTER SET {$wpdb->charset} COLLATE {$wpdb->collate};";
50 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
51 dbDelta( $sql );
52 }
53
54 public static function get_all_data() {
55 global $wpdb;
56 $table = $wpdb->prefix . WPCoder::PREFIX;
57 $result = $wpdb->get_results( "SELECT * FROM $table ORDER BY id ASC" );
58
59 return ! empty( $result ) ? $result : false;
60 }
61
62 public static function get_data_by_id( $id = '' ) {
63 if ( empty( $id ) ) {
64 return false;
65 }
66 global $wpdb;
67 $table = $wpdb->prefix . WPCoder::PREFIX;
68 $query = $wpdb->prepare( "SELECT * FROM $table WHERE id=%d", absint( $id ) );
69
70 return $wpdb->get_row( $query );
71 }
72
73 public static function get_data_by_title( $title = '' ) {
74 if ( empty( $title ) ) {
75 return false;
76 }
77
78 global $wpdb;
79 $table = $wpdb->prefix . WPCoder::PREFIX;
80 $query = $wpdb->prepare( "SELECT * FROM $table WHERE title=%s", sanitize_text_field( $title ) );
81
82 return $wpdb->get_row( $query );
83 }
84
85 public static function update( $data, $where, $data_formats ): void {
86 if ( ! current_user_can( 'unfiltered_html' ) ) {
87 return;
88 }
89
90 global $wpdb;
91 $table = $wpdb->prefix . WPCoder::PREFIX;
92 $result = $wpdb->update( $table, $data, $where, $data_formats );
93 }
94
95 public static function insert( $data, $data_formats ) {
96 if ( ! current_user_can( 'unfiltered_html' ) ) {
97 return false;
98 }
99
100 global $wpdb;
101 $table = $wpdb->prefix . WPCoder::PREFIX;
102
103 $result = $wpdb->insert( $table, $data, $data_formats );
104
105 if ( $result ) {
106 return $wpdb->insert_id;
107 }
108
109 return false;
110 }
111
112 public static function check_row( $id = '' ): bool {
113 global $wpdb;
114 $table = $wpdb->prefix . WPCoder::PREFIX;
115 if ( empty( $id ) ) {
116 return false;
117 }
118
119 $check_row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE id = %d", $id ) );
120 if ( ! empty( $check_row ) ) {
121 return true;
122 }
123
124 return false;
125 }
126
127 public static function get_columns() {
128 global $wpdb;
129 $table_name = $wpdb->prefix . WPCoder::PREFIX;
130
131 return $wpdb->get_results( "DESCRIBE $table_name" );
132 }
133
134 public static function display_tags(): void {
135 global $wpdb;
136 $table = $wpdb->prefix . WPCoder::PREFIX;
137 $result = $wpdb->get_results( "SELECT * FROM $table order by tag desc", ARRAY_A );
138 $tags = [];
139 if ( ! empty( $result ) ) {
140 foreach ( $result as $column ) {
141 if ( ! empty( $column['tag'] ) ) {
142 $tags[ $column['tag'] ] = $column['tag'];
143 }
144 }
145 }
146 if ( ! empty( $tags ) ) {
147 foreach ( $tags as $tag ) {
148 echo '<option value="' . esc_attr( $tag ) . '">';
149 }
150 }
151 }
152
153 public static function get_tags_from_table() {
154 global $wpdb;
155 $table = $wpdb->prefix . WPCoder::PREFIX;
156 $all_tags = $wpdb->get_results( "SELECT DISTINCT tag FROM $table ORDER BY tag ASC", ARRAY_A );
157
158 return ! empty( $all_tags ) ? $all_tags : false;
159 }
160
161 }