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

162 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 IF NOT EXISTS $table ($columns) DEFAULT CHARACTER SET {$wpdb->charset} COLLATE {$wpdb->collate};";
50
51 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
52 dbDelta( $sql );
53 }
54
55 public static function get_all_data() {
56 global $wpdb;
57 $table = $wpdb->prefix . WPCoder::PREFIX;
58 $result = $wpdb->get_results( "SELECT * FROM $table ORDER BY id ASC" );
59
60 return ! empty( $result ) ? $result : false;
61 }
62
63 public static function get_data_by_id( $id = '' ) {
64 if ( empty( $id ) ) {
65 return false;
66 }
67 global $wpdb;
68 $table = $wpdb->prefix . WPCoder::PREFIX;
69 $query = $wpdb->prepare( "SELECT * FROM $table WHERE id=%d", absint( $id ) );
70
71 return $wpdb->get_row( $query );
72 }
73
74 public static function get_data_by_title( $title = '' ) {
75 if ( empty( $title ) ) {
76 return false;
77 }
78
79 global $wpdb;
80 $table = $wpdb->prefix . WPCoder::PREFIX;
81 $query = $wpdb->prepare( "SELECT * FROM $table WHERE title=%s", sanitize_text_field( $title ) );
82
83 return $wpdb->get_row( $query );
84 }
85
86 public static function update( $data, $where, $data_formats ): void {
87 if ( ! current_user_can( 'unfiltered_html' ) ) {
88 return;
89 }
90
91 global $wpdb;
92 $table = $wpdb->prefix . WPCoder::PREFIX;
93 $result = $wpdb->update( $table, $data, $where, $data_formats );
94 }
95
96 public static function insert( $data, $data_formats ) {
97 if ( ! current_user_can( 'unfiltered_html' ) ) {
98 return false;
99 }
100
101 global $wpdb;
102 $table = $wpdb->prefix . WPCoder::PREFIX;
103
104 $result = $wpdb->insert( $table, $data, $data_formats );
105
106 if ( $result ) {
107 return $wpdb->insert_id;
108 }
109
110 return false;
111 }
112
113 public static function check_row( $id = '' ): bool {
114 global $wpdb;
115 $table = $wpdb->prefix . WPCoder::PREFIX;
116 if ( empty( $id ) ) {
117 return false;
118 }
119
120 $check_row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE id = %d", $id ) );
121 if ( ! empty( $check_row ) ) {
122 return true;
123 }
124
125 return false;
126 }
127
128 public static function get_columns() {
129 global $wpdb;
130 $table_name = $wpdb->prefix . WPCoder::PREFIX;
131
132 return $wpdb->get_results( "DESCRIBE $table_name" );
133 }
134
135 public static function display_tags(): void {
136 global $wpdb;
137 $table = $wpdb->prefix . WPCoder::PREFIX;
138 $result = $wpdb->get_results( "SELECT * FROM $table order by tag desc", ARRAY_A );
139 $tags = [];
140 if ( ! empty( $result ) ) {
141 foreach ( $result as $column ) {
142 if ( ! empty( $column['tag'] ) ) {
143 $tags[ $column['tag'] ] = $column['tag'];
144 }
145 }
146 }
147 if ( ! empty( $tags ) ) {
148 foreach ( $tags as $tag ) {
149 echo '<option value="' . esc_attr( $tag ) . '">';
150 }
151 }
152 }
153
154 public static function get_tags_from_table() {
155 global $wpdb;
156 $table = $wpdb->prefix . WPCoder::PREFIX;
157 $all_tags = $wpdb->get_results( "SELECT DISTINCT tag FROM $table ORDER BY tag ASC", ARRAY_A );
158
159 return ! empty( $all_tags ) ? $all_tags : false;
160 }
161
162 }