| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class DBManager |
| 5 |
* |
| 6 |
* The DBManager class handles all database operations for the plugin. |
| 7 |
* |
| 8 |
* @package WowPlugin |
| 9 |
* @subpackage Admin |
| 10 |
* @author Dmytro Lobov <dev@wow-company.com>, Wow-Company |
| 11 |
* @copyright 2024 Dmytro Lobov |
| 12 |
* @license GPL-2.0+ |
| 13 |
* |
| 14 |
*/ |
| 15 |
|
| 16 |
namespace FloatMenuLite\Admin; |
| 17 |
|
| 18 |
defined( 'ABSPATH' ) || exit; |
| 19 |
|
| 20 |
use FloatMenuLite\WOWP_Plugin; |
| 21 |
|
| 22 |
class DBManager { |
| 23 |
|
| 24 |
public static function remove_item() { |
| 25 |
$page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; |
| 26 |
$action = isset( $_GET['action'] ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : ''; |
| 27 |
$id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : ''; |
| 28 |
|
| 29 |
if ( ( $page !== WOWP_Plugin::SLUG ) || ( $action !== 'delete' ) || empty( $id ) ) { |
| 30 |
return false; |
| 31 |
} |
| 32 |
|
| 33 |
global $wpdb; |
| 34 |
$table = $wpdb->prefix . WOWP_Plugin::PREFIX; |
| 35 |
|
| 36 |
$result = $wpdb->delete( $table, [ 'id' => $id ], [ '%d' ] ); |
| 37 |
|
| 38 |
if ( $result ) { |
| 39 |
wp_safe_redirect( Link::remove_item() ); |
| 40 |
exit; |
| 41 |
} |
| 42 |
|
| 43 |
return false; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
public static function delete( $id ) { |
| 48 |
if ( ! isset( $id ) ) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
global $wpdb; |
| 53 |
$table = $wpdb->prefix . WOWP_Plugin::PREFIX; |
| 54 |
|
| 55 |
return $wpdb->delete( $table, [ 'id' => $id ], [ '%d' ] ); |
| 56 |
|
| 57 |
} |
| 58 |
|
| 59 |
public static function create( $columns ): void { |
| 60 |
global $wpdb; |
| 61 |
|
| 62 |
$table = $wpdb->prefix . WOWP_Plugin::PREFIX; |
| 63 |
$charset_collate = $wpdb->get_charset_collate(); |
| 64 |
|
| 65 |
$sql = "CREATE TABLE $table ($columns) $charset_collate;"; |
| 66 |
|
| 67 |
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 68 |
dbDelta( $sql ); |
| 69 |
} |
| 70 |
|
| 71 |
public static function get_all_data() { |
| 72 |
global $wpdb; |
| 73 |
$table = $wpdb->prefix . WOWP_Plugin::PREFIX; |
| 74 |
$result = $wpdb->get_results( "SELECT * FROM $table ORDER BY id ASC" ); |
| 75 |
|
| 76 |
return ( ! empty( $result ) && is_array( $result ) ) ? $result : false; |
| 77 |
} |
| 78 |
|
| 79 |
public static function get_data_by_id( $id = '' ) { |
| 80 |
if ( empty( $id ) ) { |
| 81 |
return false; |
| 82 |
} |
| 83 |
global $wpdb; |
| 84 |
$table = $wpdb->prefix . WOWP_Plugin::PREFIX; |
| 85 |
|
| 86 |
return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE id=%d", absint( $id ) ) ); |
| 87 |
} |
| 88 |
|
| 89 |
public static function get_param_id( $id = '' ) { |
| 90 |
if ( empty( $id ) ) { |
| 91 |
return false; |
| 92 |
} |
| 93 |
$result = self::get_data_by_id( $id ); |
| 94 |
|
| 95 |
return maybe_unserialize( $result->param ); |
| 96 |
} |
| 97 |
|
| 98 |
public static function get_data_by_title( $title = '' ) { |
| 99 |
if ( empty( $title ) ) { |
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
global $wpdb; |
| 104 |
$table = $wpdb->prefix . WOWP_Plugin::PREFIX; |
| 105 |
|
| 106 |
return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE title=%s", sanitize_text_field( $title ) ) ); |
| 107 |
} |
| 108 |
|
| 109 |
public static function update( $data, $where, $data_formats ): void { |
| 110 |
global $wpdb; |
| 111 |
$table = $wpdb->prefix . WOWP_Plugin::PREFIX; |
| 112 |
$result = $wpdb->update( $table, $data, $where, $data_formats ); |
| 113 |
} |
| 114 |
|
| 115 |
public static function insert( $data, $data_formats ) { |
| 116 |
global $wpdb; |
| 117 |
$table = $wpdb->prefix . WOWP_Plugin::PREFIX; |
| 118 |
|
| 119 |
$result = $wpdb->insert( $table, $data, $data_formats ); |
| 120 |
|
| 121 |
if ( $result ) { |
| 122 |
return $wpdb->insert_id; |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
return false; |
| 127 |
} |
| 128 |
|
| 129 |
public static function check_row( $id = '' ): bool { |
| 130 |
global $wpdb; |
| 131 |
$table = $wpdb->prefix . WOWP_Plugin::PREFIX; |
| 132 |
if ( empty( $id ) ) { |
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
$check_row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE id = %d", $id ) ); |
| 137 |
if ( ! empty( $check_row ) ) { |
| 138 |
return true; |
| 139 |
} |
| 140 |
|
| 141 |
return false; |
| 142 |
} |
| 143 |
|
| 144 |
public static function get_columns() { |
| 145 |
global $wpdb; |
| 146 |
$table_name = $wpdb->prefix . WOWP_Plugin::PREFIX; |
| 147 |
|
| 148 |
return $wpdb->get_results( "DESCRIBE $table_name" ); |
| 149 |
} |
| 150 |
|
| 151 |
public static function display_tags(): void { |
| 152 |
global $wpdb; |
| 153 |
$table = $wpdb->prefix . WOWP_Plugin::PREFIX; |
| 154 |
$result = $wpdb->get_results( "SELECT * FROM $table order by tag desc", ARRAY_A ); |
| 155 |
$tags = []; |
| 156 |
if ( ! empty( $result ) ) { |
| 157 |
foreach ( $result as $column ) { |
| 158 |
if ( ! empty( $column['tag'] ) ) { |
| 159 |
$tags[ $column['tag'] ] = $column['tag']; |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
if ( ! empty( $tags ) ) { |
| 164 |
foreach ( $tags as $tag ) { |
| 165 |
echo '<option value="' . esc_attr( $tag ) . '">'; |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
public static function get_tags_from_table() { |
| 171 |
global $wpdb; |
| 172 |
$table = $wpdb->prefix . WOWP_Plugin::PREFIX; |
| 173 |
$all_tags = $wpdb->get_results( "SELECT DISTINCT tag FROM $table ORDER BY tag ASC", ARRAY_A ); |
| 174 |
|
| 175 |
return ! empty( $all_tags ) ? $all_tags : false; |
| 176 |
} |
| 177 |
|
| 178 |
} |