|
1
|
|
|
<?php |
|
2
|
|
|
namespace NirjharLo\WP_Plugin_Framework\Src; |
|
3
|
|
|
|
|
4
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* DB installation class |
|
8
|
|
|
* |
|
9
|
|
|
* @author Nirjhar Lo |
|
10
|
|
|
* @package wp-plugin-framework |
|
11
|
|
|
*/ |
|
12
|
|
|
if ( ! class_exists( 'Db' ) ) { |
|
13
|
|
|
|
|
14
|
|
|
class Db { |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var String |
|
18
|
|
|
*/ |
|
19
|
|
|
public $table; |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var String |
|
24
|
|
|
*/ |
|
25
|
|
|
public $sql; |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Instantiate the db class |
|
30
|
|
|
* |
|
31
|
|
|
* @return Void |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct() { |
|
34
|
|
|
|
|
35
|
|
|
$this->up_path = ABSPATH . 'wp-admin/includes/upgrade.php'; |
|
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Define the necessary database tables |
|
41
|
|
|
* |
|
42
|
|
|
* @return Void |
|
43
|
|
|
*/ |
|
44
|
|
|
public function build() { |
|
45
|
|
|
|
|
46
|
|
|
global $wpdb; |
|
47
|
|
|
$wpdb->hide_errors(); |
|
48
|
|
|
$this->table_name = $wpdb->prefix . $this->table; |
|
|
|
|
|
|
49
|
|
|
update_option( '_plugin_db_exist', 0 ); |
|
|
|
|
|
|
50
|
|
|
if ( $wpdb->get_var( "SHOW TABLES LIKE '$this->table_name'" ) != $this->table_name ) { |
|
51
|
|
|
$execute_sql = $this->execute( $this->table_name, $this->collate(), $this->sql ); |
|
52
|
|
|
dbDelta( $execute_sql ); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Define the variables for db table creation |
|
59
|
|
|
* |
|
60
|
|
|
* @return String |
|
61
|
|
|
*/ |
|
62
|
|
|
public function collate() { |
|
63
|
|
|
|
|
64
|
|
|
global $wpdb; |
|
65
|
|
|
$wpdb->hide_errors(); |
|
66
|
|
|
$collate = ""; |
|
67
|
|
|
if ( $wpdb->has_cap( 'collation' ) ) { |
|
68
|
|
|
if( ! empty($wpdb->charset ) ) |
|
69
|
|
|
$collate .= "DEFAULT CHARACTER SET $wpdb->charset"; |
|
70
|
|
|
if( ! empty($wpdb->collate ) ) |
|
71
|
|
|
$collate .= " COLLATE $wpdb->collate"; |
|
72
|
|
|
} |
|
73
|
|
|
require_once( $this->up_path ); |
|
74
|
|
|
return $collate; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* SQL query to create the main plugin table. |
|
80
|
|
|
* |
|
81
|
|
|
* @param String $table_name |
|
82
|
|
|
* @param String $collate |
|
83
|
|
|
* @param String $sql |
|
84
|
|
|
* |
|
85
|
|
|
* @return String |
|
86
|
|
|
*/ |
|
87
|
|
|
public function execute( $table_name, $collate, $sql ) { |
|
88
|
|
|
|
|
89
|
|
|
return "CREATE TABLE $table_name ( $sql ) $collate;"; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Check options and tables and output the info to check if db install is successful |
|
95
|
|
|
* |
|
96
|
|
|
* @return String |
|
97
|
|
|
*/ |
|
98
|
|
|
public function __destruct() { |
|
99
|
|
|
|
|
100
|
|
|
global $wpdb; |
|
101
|
|
|
|
|
102
|
|
|
$this->table_name = $wpdb->prefix . $this->table; |
|
|
|
|
|
|
103
|
|
|
if ( $wpdb->get_var( "SHOW TABLES LIKE '$this->table_name'" ) == $this->table_name ) { |
|
104
|
|
|
|
|
105
|
|
|
update_option( '_plugin_db_exist', 1 ); |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} ?> |
|
|
|
|
|
|
110
|
|
|
|