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