# wp-coder/3.4/classes/Dashboard/DBManager.php

WP Coder – Insert &amp; Manage Code Snippets, version 3.4. 174 lines.

- Page: https://pluginprobe.com/plugins/wp-coder/3.4/code/classes/Dashboard/DBManager.php
- Raw: https://pluginprobe.com/plugins/wp-coder/3.4/raw/classes/Dashboard/DBManager.php
- Modified: 2024-01-10T11:05:02+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/wp-coder/3.4/code/classes/Dashboard/DBManager.php#L10-L20`.

```php
<?php

namespace WPCoder\Dashboard;

defined( 'ABSPATH' ) || exit;

use WPCoder\WPCoder;

class DBManager {

	public static function remove_item() {
		$page   = isset( $_GET['page'] ) ? sanitize_text_field( $_GET['page'] ) : '';
		$action = isset( $_GET['action'] ) ? sanitize_text_field( $_GET['action'] ) : '';
		$id     = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : '';

		if ( ( $page !== WPCoder::SLUG ) || ( $action !== 'delete' ) || empty( $id ) ) {
			return false;
		}

		global $wpdb;
		$table = $wpdb->prefix . WPCoder::PREFIX;

		$result = $wpdb->delete( $table, [ 'id' => $id ], [ '%d' ] );

		if ( $result ) {
			wp_safe_redirect( Link::remove_item() );
			exit;
		}

		return false;
	}


	public static function delete( $id ) {
		if ( ! isset( $id ) ) {
			return false;
		}

		global $wpdb;
		$table = $wpdb->prefix . WPCoder::PREFIX;

		return $wpdb->delete( $table, [ 'id' => $id ], [ '%d' ] );
	}

	public static function create(): void {
		global $wpdb;
		$table_name      = $wpdb->prefix . WPCoder::PREFIX;
		$charset_collate = $wpdb->get_charset_collate();

		$sql = "CREATE TABLE $table_name (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        title VARCHAR(200) NOT NULL,
        html_code LONGTEXT,
        css_code LONGTEXT,
        js_code LONGTEXT,
        php_code LONGTEXT,
        param LONGTEXT,
        status BOOLEAN,
        mode BOOLEAN,
        tag TEXT,
        PRIMARY KEY (id)
    ) $charset_collate;";
		require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
		dbDelta( $sql );
	}

	public static function get_all_data() {
		global $wpdb;
		$table  = $wpdb->prefix . WPCoder::PREFIX;
		$result = $wpdb->get_results( "SELECT * FROM $table ORDER BY id ASC" );

		return ! empty( $result ) ? $result : false;
	}

	public static function get_data_by_id( $id = '' ) {
		if ( empty( $id ) ) {
			return false;
		}
		global $wpdb;
		$table = $wpdb->prefix . WPCoder::PREFIX;
		$query = $wpdb->prepare( "SELECT * FROM $table WHERE id=%d", absint( $id ) );

		return $wpdb->get_row( $query );
	}

	public static function get_data_by_title( $title = '' ) {
		if ( empty( $title ) ) {
			return false;
		}

		global $wpdb;
		$table = $wpdb->prefix . WPCoder::PREFIX;
		$query = $wpdb->prepare( "SELECT * FROM $table WHERE title=%s", sanitize_text_field( $title ) );

		return $wpdb->get_row( $query );
	}

	public static function update( $data, $where, $data_formats ): void {
		if ( ! current_user_can( 'unfiltered_html' ) ) {
			return;
		}

		global $wpdb;
		$table  = $wpdb->prefix . WPCoder::PREFIX;
		$result = $wpdb->update( $table, $data, $where, $data_formats );
	}

	public static function insert( $data, $data_formats ) {
		if ( ! current_user_can( 'unfiltered_html' ) ) {
			return false;
		}

		global $wpdb;
		$table = $wpdb->prefix . WPCoder::PREFIX;

		$result = $wpdb->insert( $table, $data, $data_formats );

		if ( $result ) {
			return $wpdb->insert_id;
		}

		return false;
	}

	public static function check_row( $id = '' ): bool {
		global $wpdb;
		$table = $wpdb->prefix . WPCoder::PREFIX;
		if ( empty( $id ) ) {
			return false;
		}

		$check_row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE id = %d", $id ) );
		if ( ! empty( $check_row ) ) {
			return true;
		}

		return false;
	}

	public static function get_columns() {
		global $wpdb;
		$table_name = $wpdb->prefix . WPCoder::PREFIX;

		return $wpdb->get_results( "DESCRIBE $table_name" );
	}

	public static function display_tags(): void {
		global $wpdb;
		$table  = $wpdb->prefix . WPCoder::PREFIX;
		$result = $wpdb->get_results( "SELECT * FROM $table order by tag desc", ARRAY_A );
		$tags   = [];
		if ( ! empty( $result ) ) {
			foreach ( $result as $column ) {
				if ( ! empty( $column['tag'] ) ) {
					$tags[ $column['tag'] ] = $column['tag'];
				}
			}
		}
		if ( ! empty( $tags ) ) {
			foreach ( $tags as $tag ) {
				echo '<option value="' . esc_attr( $tag ) . '">';
			}
		}
	}

	public static function get_tags_from_table() {
		global $wpdb;
		$table    = $wpdb->prefix . WPCoder::PREFIX;
		$all_tags = $wpdb->get_results( "SELECT DISTINCT tag FROM $table ORDER BY tag ASC", ARRAY_A );

		return ! empty( $all_tags ) ? $all_tags : false;
	}

}
```
