| 1 |
<?php |
| 2 |
/** |
| 3 |
* DBManager class for Float Menu Lite plugin. |
| 4 |
* |
| 5 |
* @package FloatMenuLite\Admin |
| 6 |
* |
| 7 |
* Methods: |
| 8 |
* - create() — Create database table |
| 9 |
* - get_columns() — Get table column structure |
| 10 |
* - insert() — Insert new row |
| 11 |
* - update() — Update existing row |
| 12 |
* - delete() — Delete row by ID |
| 13 |
* - remove_item() — Handle item removal from GET request |
| 14 |
* - get_all_data() — Get all rows from table |
| 15 |
* - get_data_by_id() — Get single row by ID |
| 16 |
* - get_data_by_title() — Get row by title |
| 17 |
* - get_param_id() — Get and unserialize param by ID |
| 18 |
* - check_row() — Check if row exists by ID |
| 19 |
* - get_tags_from_table() — Get unique tags |
| 20 |
* - display_tags() — Output HTML <option> tags for tags |
| 21 |
*/ |
| 22 |
|
| 23 |
namespace FloatMenuLite\Admin; |
| 24 |
|
| 25 |
defined( 'ABSPATH' ) || exit; |
| 26 |
|
| 27 |
use FloatMenuLite\WOWP_Plugin; |
| 28 |
|
| 29 |
class DBManager { |
| 30 |
|
| 31 |
public static function create( $columns ): void { |
| 32 |
global $wpdb; |
| 33 |
|
| 34 |
$table = esc_sql( $wpdb->prefix . WOWP_Plugin::PREFIX ); |
| 35 |
$charset_collate = $wpdb->get_charset_collate(); |
| 36 |
|
| 37 |
$sql = "CREATE TABLE $table ($columns) $charset_collate;"; |
| 38 |
|
| 39 |
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 40 |
dbDelta( $sql ); |
| 41 |
} |
| 42 |
|
| 43 |
public static function get_columns() { |
| 44 |
global $wpdb; |
| 45 |
$table = esc_sql( $wpdb->prefix . WOWP_Plugin::PREFIX ); |
| 46 |
|
| 47 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 48 |
return $wpdb->get_results( "DESCRIBE $table" ); |
| 49 |
} |
| 50 |
|
| 51 |
public static function insert( $data, $data_formats ) { |
| 52 |
global $wpdb; |
| 53 |
$table = esc_sql( $wpdb->prefix . WOWP_Plugin::PREFIX ); |
| 54 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 55 |
$result = $wpdb->insert( $table, $data, $data_formats ); |
| 56 |
|
| 57 |
if ( $result ) { |
| 58 |
return $wpdb->insert_id; |
| 59 |
|
| 60 |
} |
| 61 |
|
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
public static function update( $data, $where, $data_formats ): void { |
| 66 |
global $wpdb; |
| 67 |
$table = esc_sql( $wpdb->prefix . WOWP_Plugin::PREFIX ); |
| 68 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 69 |
$result = $wpdb->update( $table, $data, $where, $data_formats ); |
| 70 |
} |
| 71 |
|
| 72 |
public static function delete( $id ) { |
| 73 |
if ( ! isset( $id ) ) { |
| 74 |
return false; |
| 75 |
} |
| 76 |
|
| 77 |
global $wpdb; |
| 78 |
$table = esc_sql( $wpdb->prefix . WOWP_Plugin::PREFIX ); |
| 79 |
|
| 80 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 81 |
return $wpdb->delete( $table, [ 'id' => $id ], [ '%d' ] ); |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
public static function remove_item() { |
| 86 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 87 |
$page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; |
| 88 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 89 |
$action = isset( $_GET['action'] ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : ''; |
| 90 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 91 |
$id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : ''; |
| 92 |
|
| 93 |
if ( ( $page !== WOWP_Plugin::SLUG ) || ( $action !== 'delete' ) || empty( $id ) ) { |
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
global $wpdb; |
| 98 |
$table = esc_sql( $wpdb->prefix . WOWP_Plugin::PREFIX ); |
| 99 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 100 |
$result = $wpdb->delete( $table, [ 'id' => $id ], [ '%d' ] ); |
| 101 |
|
| 102 |
if ( $result ) { |
| 103 |
wp_safe_redirect( Link::remove_item() ); |
| 104 |
exit; |
| 105 |
} |
| 106 |
|
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
public static function get_all_data() { |
| 111 |
global $wpdb; |
| 112 |
$table = esc_sql( $wpdb->prefix . WOWP_Plugin::PREFIX ); |
| 113 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 114 |
$result = $wpdb->get_results( "SELECT * FROM $table ORDER BY id ASC" ); |
| 115 |
|
| 116 |
return ( ! empty( $result ) && is_array( $result ) ) ? $result : false; |
| 117 |
} |
| 118 |
|
| 119 |
public static function get_data_by_id( $id = '' ) { |
| 120 |
if ( empty( $id ) ) { |
| 121 |
return false; |
| 122 |
} |
| 123 |
global $wpdb; |
| 124 |
$table = esc_sql( $wpdb->prefix . WOWP_Plugin::PREFIX ); |
| 125 |
|
| 126 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 127 |
return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE id=%d", absint( $id ) ) ); |
| 128 |
} |
| 129 |
|
| 130 |
public static function get_data_by_title( $title = '' ) { |
| 131 |
if ( empty( $title ) ) { |
| 132 |
return false; |
| 133 |
} |
| 134 |
|
| 135 |
global $wpdb; |
| 136 |
$table = esc_sql( $wpdb->prefix . WOWP_Plugin::PREFIX ); |
| 137 |
|
| 138 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 139 |
return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE title=%s", sanitize_text_field( $title ) ) ); |
| 140 |
} |
| 141 |
|
| 142 |
public static function get_param_id( $id = '' ) { |
| 143 |
if ( empty( $id ) ) { |
| 144 |
return false; |
| 145 |
} |
| 146 |
$result = self::get_data_by_id( $id ); |
| 147 |
|
| 148 |
return maybe_unserialize( $result->param ); |
| 149 |
} |
| 150 |
|
| 151 |
public static function check_row( $id = '' ): bool { |
| 152 |
global $wpdb; |
| 153 |
$table = esc_sql( $wpdb->prefix . WOWP_Plugin::PREFIX ); |
| 154 |
if ( empty( $id ) ) { |
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 159 |
$check_row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE id = %d", $id ) ); |
| 160 |
if ( ! empty( $check_row ) ) { |
| 161 |
return true; |
| 162 |
} |
| 163 |
|
| 164 |
return false; |
| 165 |
} |
| 166 |
|
| 167 |
public static function get_tags_from_table() { |
| 168 |
global $wpdb; |
| 169 |
$table = esc_sql( $wpdb->prefix . WOWP_Plugin::PREFIX ); |
| 170 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 171 |
$all_tags = $wpdb->get_results( "SELECT DISTINCT tag FROM $table ORDER BY tag ASC", ARRAY_A ); |
| 172 |
|
| 173 |
return ! empty( $all_tags ) ? $all_tags : false; |
| 174 |
} |
| 175 |
|
| 176 |
public static function display_tags(): void { |
| 177 |
global $wpdb; |
| 178 |
$table = esc_sql( $wpdb->prefix . WOWP_Plugin::PREFIX ); |
| 179 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 180 |
$result = $wpdb->get_results( "SELECT * FROM $table order by tag desc", ARRAY_A ); |
| 181 |
$tags = []; |
| 182 |
if ( ! empty( $result ) ) { |
| 183 |
foreach ( $result as $column ) { |
| 184 |
if ( ! empty( $column['tag'] ) ) { |
| 185 |
$tags[ $column['tag'] ] = $column['tag']; |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
if ( ! empty( $tags ) ) { |
| 190 |
foreach ( $tags as $tag ) { |
| 191 |
echo '<option value="' . esc_attr( $tag ) . '">'; |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
} |