Issues (203)

Plugin/Src/db.php (8 issues)

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';
0 ignored issues
show
The constant NirjharLo\Cgss\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...
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;
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...
34
			update_option( '_plugin_db_exist', 0 );
0 ignored issues
show
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

34
			/** @scrutinizer ignore-call */ 
35
   update_option( '_plugin_db_exist', 0 );
Loading history...
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 );
0 ignored issues
show
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

37
				/** @scrutinizer ignore-call */ 
38
    dbDelta( $execute_sql );
Loading history...
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;
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...
72
			if ( $wpdb->get_var("SHOW TABLES LIKE '$this->table_name'") == $this->table_name ) {
73
				update_option( '_plugin_db_exist', 1 );
0 ignored issues
show
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

73
				/** @scrutinizer ignore-call */ 
74
    update_option( '_plugin_db_exist', 1 );
Loading history...
74
			}
75
		}
76
	} ?>
0 ignored issues
show
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...
77