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