Db::build()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
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';
0 ignored issues
show
Bug introduced by
The constant NirjharLo\WP_Plugin_Framework\Src\ABSPATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug Best Practice introduced by
The property up_path does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The property table_name does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
49
			update_option( '_plugin_db_exist', 0 );
0 ignored issues
show
Bug introduced by
The function update_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
			/** @scrutinizer ignore-call */ 
50
   update_option( '_plugin_db_exist', 0 );
Loading history...
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 );
0 ignored issues
show
Bug introduced by
The function dbDelta was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
				/** @scrutinizer ignore-call */ 
53
    dbDelta( $execute_sql );
Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The property table_name does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
103
			if ( $wpdb->get_var( "SHOW TABLES LIKE '$this->table_name'" ) == $this->table_name ) {
104
105
				update_option( '_plugin_db_exist', 1 );
0 ignored issues
show
Bug introduced by
The function update_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

105
				/** @scrutinizer ignore-call */ 
106
    update_option( '_plugin_db_exist', 1 );
Loading history...
106
			}
107
		}
108
	}
109
} ?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
110