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

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