Test Failed
Push — feature/donor-tables ( 8c4924 )
by Ravinder
06:19
created

Give_DB_Donor_Meta::create_table()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 5

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 18
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Donor Meta DB class
4
 *
5
 * @package     Give
6
 * @subpackage  Classes/DB Donor Meta
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       1.6
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Class Give_DB_Donor_Meta
19
 *
20
 * This class is for interacting with the donor meta database table.
21
 *
22
 * @since 1.6
23
 */
24
class Give_DB_Donor_Meta extends Give_DB_Meta {
25
26
	/**
27
	 * Meta type
28
	 *
29
	 * @since  2.0
30
	 * @access protected
31
	 *
32
	 * @var string
33
	 */
34
	protected $meta_type = 'donor';
35
36
	/**
37
	 * Meta supports.
38
	 *
39
	 * @since  2.0
40
	 * @access protected
41
	 * @var array
42
	 */
43
	protected $supports = array();
44
45
	/**
46
	 * Give_DB_Donor_Meta constructor.
47
	 *
48
	 * @access  public
49
	 * @since   1.6
50
	 */
51
	public function __construct() {
52
		/* @var WPDB $wpdb */
53
		global $wpdb;
54
55
		$wpdb->donormeta   = $this->table_name = $wpdb->prefix . 'give_donormeta';
56
		$this->primary_key = 'meta_id';
57
		$this->version     = '1.0';
58
59
		parent::__construct();
60
61
		$this->bc_200_params();
62
		$this->register_table();
63
	}
64
65
	/**
66
	 * Get table columns and data types.
67
	 *
68
	 * @access  public
69
	 * @since   1.6
70
	 *
71
	 * @return  array  Columns and formats.
72
	 */
73
	public function get_columns() {
74
		return array(
75
			'meta_id'     => '%d',
76
			'donor_id' => '%d',
77
			'meta_key'    => '%s',
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
78
			'meta_value'  => '%s',
0 ignored issues
show
introduced by
Detected usage of meta_value, possible slow query.
Loading history...
79
		);
80
	}
81
82
	/**
83
	 * Create the table
84
	 *
85
	 * @access public
86
	 * @since  1.6
87
	 *
88
	 * @return void
89
	 */
90 View Code Duplication
	public function create_table() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
92
		require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
93
94
		$sql = "CREATE TABLE {$this->table_name} (
95
			meta_id bigint(20) NOT NULL AUTO_INCREMENT,
96
			donor_id bigint(20) NOT NULL,
97
			meta_key varchar(255) DEFAULT NULL,
98
			meta_value longtext,
99
			PRIMARY KEY  (meta_id),
100
			KEY donor_id (donor_id),
101
			KEY meta_key (meta_key)
102
			) CHARACTER SET utf8 COLLATE utf8_general_ci;";
103
104
		dbDelta( $sql );
105
106
		update_option( $this->table_name . '_db_version', $this->version );
107
	}
108
109
	/**
110
	 * Add backward compatibility for old table name
111
	 *
112
	 * @since  2.0
113
	 * @access private
114
	 * @global wpdb $wpdb
115
	 */
116
	private function bc_200_params() {
117
		/* @var wpdb $wpdb */
118
		global $wpdb;
119
120
		if ( ! give_has_upgrade_completed( 'v20_rename_donor_tables' ) ) {
121
			$wpdb->donormeta = $this->table_name = "{$wpdb->prefix}give_customermeta";
122
			$this->meta_type = 'customer';
123
		}
124
125
		$wpdb->customermeta = $wpdb->donormeta;
126
	}
127
128
	/**
129
	 * Check if current id is valid
130
	 *
131
	 * @since  2.0
132
	 * @access protected
133
	 *
134
	 * @param $ID
135
	 *
136
	 * @return bool
137
	 */
138
	protected function is_valid_post_type( $ID ) {
139
		return $ID && true;
140
	}
141
142
}
143