| 1 |
<?php |
| 2 |
/** |
| 3 |
* Data structure class |
| 4 |
* |
| 5 |
* @since 1.0 |
| 6 |
*/ |
| 7 |
namespace dologin; |
| 8 |
|
| 9 |
defined( 'WPINC' ) || exit; |
| 10 |
|
| 11 |
class Data extends Instance { |
| 12 |
private $_db_updater = array( |
| 13 |
'1.4.1' => array( |
| 14 |
'dologin_update_1_4_1', |
| 15 |
), |
| 16 |
'4.0' => array( |
| 17 |
'dologin_update_4_0_0', |
| 18 |
), |
| 19 |
'4.5' => array( |
| 20 |
'dologin_update_4_5_0', |
| 21 |
), |
| 22 |
'4.6' => array( |
| 23 |
'dologin_update_4_6_0', |
| 24 |
), |
| 25 |
'4.7.4' => array( |
| 26 |
'dologin_update_4_7_4', |
| 27 |
), |
| 28 |
'4.7.5' => array( |
| 29 |
'dologin_update_4_7_5', |
| 30 |
), |
| 31 |
); |
| 32 |
|
| 33 |
const TB_FAILURE = 'dologin_failure'; |
| 34 |
const TB_PSWDLESS = 'dologin_pswdless'; |
| 35 |
const TB_SITE = 'dologin_site'; |
| 36 |
|
| 37 |
/** |
| 38 |
* Data upgrade |
| 39 |
* |
| 40 |
* @since 1.4.1 |
| 41 |
*/ |
| 42 |
public function conf_upgrade() { |
| 43 |
require_once DOLOGIN_DIR . 'src/data.upgrade.func.php'; |
| 44 |
|
| 45 |
foreach ( $this->_db_updater as $k => $v ) { |
| 46 |
if ( version_compare( DOLOGIN_CUR_V, $k, '<' ) ) { |
| 47 |
// run each callback |
| 48 |
foreach ( $v as $v2 ) { |
| 49 |
defined( 'debug' ) && debug( '[Data] Updating [ori_v] ' . DOLOGIN_CUR_V . " \t[to] $k \t[func] $v2" ); |
| 50 |
call_user_func( $v2 ); |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
// Repair tables that may be missing after an older or multisite activation. |
| 56 |
$this->tables_create(); |
| 57 |
|
| 58 |
Conf::delete( '_ver' ); |
| 59 |
Conf::add( '_ver', Core::VER ); |
| 60 |
|
| 61 |
Util::version_check( 'upgrade' ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Get the table name |
| 66 |
* |
| 67 |
* @since 1.3 |
| 68 |
* @access public |
| 69 |
*/ |
| 70 |
public function tb( $tb ) { |
| 71 |
global $wpdb; |
| 72 |
|
| 73 |
switch ( $tb ) { |
| 74 |
case 'failure': |
| 75 |
return $wpdb->prefix . self::TB_FAILURE; |
| 76 |
|
| 77 |
case 'pswdless': |
| 78 |
return $wpdb->prefix . self::TB_PSWDLESS; |
| 79 |
|
| 80 |
case 'site': |
| 81 |
return $wpdb->prefix . self::TB_SITE; |
| 82 |
|
| 83 |
default: |
| 84 |
break; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Check if one table exists or not |
| 90 |
* |
| 91 |
* @since 1.3 |
| 92 |
* @access public |
| 93 |
*/ |
| 94 |
public function tb_exist( $tb ) { |
| 95 |
global $wpdb; |
| 96 |
return $wpdb->get_var( 'SHOW TABLES LIKE "' . $this->tb( $tb ) . '"' ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.DirectDatabaseQuery -- table name is a hardcoded internal identifier. |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Get data structure of one table |
| 101 |
* |
| 102 |
* @since 1.3 |
| 103 |
* @access private |
| 104 |
*/ |
| 105 |
private function _tb_structure( $tb ) { |
| 106 |
return f::read( DOLOGIN_DIR . 'src/data_structure/' . $tb . '.sql' ); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Create img optm table and sync data from wp_postmeta |
| 111 |
* |
| 112 |
* @since 1.3 |
| 113 |
* @access public |
| 114 |
*/ |
| 115 |
public function tb_create( $tb ) { |
| 116 |
global $wpdb; |
| 117 |
|
| 118 |
defined( 'debug' ) && debug2( '[Data] Checking table ' . $tb ); |
| 119 |
|
| 120 |
// Check if table exists first |
| 121 |
if ( $this->tb_exist( $tb ) ) { |
| 122 |
defined( 'debug' ) && debug2( '[Data] Existed' ); |
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
defined( 'debug' ) && debug( '[Data] Creating ' . $tb ); |
| 127 |
|
| 128 |
$sql = sprintf( |
| 129 |
'CREATE TABLE IF NOT EXISTS `%1$s` (' . $this->_tb_structure( $tb ) . ') %2$s;', |
| 130 |
$this->tb( $tb ), |
| 131 |
$wpdb->get_charset_collate() // 'DEFAULT CHARSET=utf8' |
| 132 |
); |
| 133 |
|
| 134 |
$res = $wpdb->query( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.DirectDatabaseQuery -- table name is a hardcoded internal identifier. |
| 135 |
if ( $res !== true ) { |
| 136 |
defined( 'debug' ) && debug( '[Data] Warning! Creating table failed!', $sql ); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Drop table |
| 142 |
* |
| 143 |
* @since 1.3 |
| 144 |
* @access public |
| 145 |
*/ |
| 146 |
public function tb_del( $tb ) { |
| 147 |
global $wpdb; |
| 148 |
|
| 149 |
if ( ! $this->tb_exist( $tb ) ) { |
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
defined( 'debug' ) && debug( '[Data] Deleting table ' . $tb ); |
| 154 |
|
| 155 |
$q = 'DROP TABLE IF EXISTS ' . $this->tb( $tb ); |
| 156 |
$wpdb->query( $q ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.DirectDatabaseQuery -- table name is a hardcoded internal identifier. |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Create all tables |
| 161 |
* |
| 162 |
* @since 1.3 |
| 163 |
* @access public |
| 164 |
*/ |
| 165 |
public function tables_create() { |
| 166 |
global $wpdb; |
| 167 |
|
| 168 |
$this->tb_create( 'failure' ); |
| 169 |
$this->tb_create( 'pswdless' ); |
| 170 |
$this->tb_create( 'site' ); |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Drop generated tables |
| 175 |
* |
| 176 |
* @since 1.3 |
| 177 |
* @access public |
| 178 |
*/ |
| 179 |
public function tables_del() { |
| 180 |
global $wpdb; |
| 181 |
|
| 182 |
$this->tb_del( 'failure' ); |
| 183 |
$this->tb_del( 'pswdless' ); |
| 184 |
$this->tb_del( 'site' ); |
| 185 |
} |
| 186 |
} |
| 187 |
|