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