1
|
|
|
<?php |
2
|
|
|
namespace NirjharLo\Cgss\Src; |
3
|
|
|
|
4
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* DB installation class |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
class Db { |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
|
15
|
|
|
public $table; //string. Declare before using |
16
|
|
|
public $sql; //string. Declare before using |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
|
20
|
|
|
public function __construct() { |
21
|
|
|
|
22
|
|
|
$this->up_path = ABSPATH . 'wp-admin/includes/upgrade.php'; |
|
|
|
|
23
|
|
|
$this->build(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
|
28
|
|
|
//Define the necessary database tables |
29
|
|
|
public function build() { |
30
|
|
|
|
31
|
|
|
global $wpdb; |
32
|
|
|
$wpdb->hide_errors(); |
33
|
|
|
$this->table_name = $wpdb->prefix . $this->table; |
|
|
|
|
34
|
|
|
update_option( '_plugin_db_exist', 0 ); |
|
|
|
|
35
|
|
|
if ( $wpdb->get_var("SHOW TABLES LIKE '$this->table_name'") != $this->table_name ) { |
36
|
|
|
$execute_sql = $this->execute( $this->table_name, $this->collate(), $this->sql ); |
37
|
|
|
dbDelta( $execute_sql ); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
|
43
|
|
|
//Define the variables for db table creation |
44
|
|
|
public function collate() { |
45
|
|
|
|
46
|
|
|
global $wpdb; |
47
|
|
|
$wpdb->hide_errors(); |
48
|
|
|
$collate = ""; |
49
|
|
|
if ( $wpdb->has_cap( 'collation' ) ) { |
50
|
|
|
if( ! empty($wpdb->charset ) ) |
51
|
|
|
$collate .= "DEFAULT CHARACTER SET $wpdb->charset"; |
52
|
|
|
if( ! empty($wpdb->collate ) ) |
53
|
|
|
$collate .= " COLLATE $wpdb->collate"; |
54
|
|
|
} |
55
|
|
|
require_once( $this->up_path ); |
56
|
|
|
return $collate; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
|
61
|
|
|
//SQL query to create the main plugin table. |
62
|
|
|
public function execute( $table_name, $collate, $sql ) { |
63
|
|
|
return "CREATE TABLE $table_name ( $sql ) $collate;"; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
|
68
|
|
|
//Check options and tables and output the info to check if db install is successful |
69
|
|
|
public function __destruct() { |
70
|
|
|
global $wpdb; |
71
|
|
|
$this->table_name = $wpdb->prefix . $this->table; |
|
|
|
|
72
|
|
|
if ( $wpdb->get_var("SHOW TABLES LIKE '$this->table_name'") == $this->table_name ) { |
73
|
|
|
update_option( '_plugin_db_exist', 1 ); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} ?> |
|
|
|
|
77
|
|
|
|