| 1 |
<?php |
| 2 |
|
| 3 |
namespace Sellkit\Core; |
| 4 |
|
| 5 |
use Sellkit\Core\Update\Db_Updater; |
| 6 |
use Sellkit\Database; |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || die(); |
| 9 |
|
| 10 |
/** |
| 11 |
* Class Install. |
| 12 |
* |
| 13 |
* @since 1.1.0 |
| 14 |
*/ |
| 15 |
class Install { |
| 16 |
|
| 17 |
/** |
| 18 |
* Background process object. |
| 19 |
* |
| 20 |
* @since 1.1.0 |
| 21 |
* @var Db_Updater |
| 22 |
*/ |
| 23 |
public static $updater; |
| 24 |
|
| 25 |
/** |
| 26 |
* DB updates and callbacks that need to be run per version. |
| 27 |
* |
| 28 |
* @since 1.1.0 |
| 29 |
* @var array |
| 30 |
*/ |
| 31 |
private static $updates = [ |
| 32 |
'1.0.0' => [ |
| 33 |
'check_database_tables', |
| 34 |
], |
| 35 |
'1.2.1' => [ |
| 36 |
'add_ip_column_to_contact_segmentation', |
| 37 |
], |
| 38 |
'1.2.3' => [ // @since 1.2.3 |
| 39 |
'add_url_query_string_column_to_contact_segmentation', |
| 40 |
], |
| 41 |
]; |
| 42 |
|
| 43 |
/** |
| 44 |
* Install constructor. |
| 45 |
* |
| 46 |
* @since 1.1.0 |
| 47 |
*/ |
| 48 |
public function __construct() { |
| 49 |
sellkit()->load_files( [ |
| 50 |
'core/admin/feedback', |
| 51 |
] ); |
| 52 |
|
| 53 |
$current_version = $this->get_current_database_version(); |
| 54 |
$last_version = array_key_last( self::$updates ); |
| 55 |
|
| 56 |
if ( $current_version === $last_version ) { |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
sellkit()->load_files( [ |
| 61 |
'core/update/db-updater', |
| 62 |
'core/update/updater-functions', |
| 63 |
] ); |
| 64 |
|
| 65 |
$this->maybe_update(); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* If there are some updates it start to update. |
| 70 |
* |
| 71 |
* @since 1.1.0 |
| 72 |
*/ |
| 73 |
public function maybe_update() { |
| 74 |
$current_db_version = $this->get_current_database_version(); |
| 75 |
$current_db_version = ! empty( $current_db_version ) ? $current_db_version : 0; |
| 76 |
self::$updater = new Db_Updater(); |
| 77 |
$has_new_update = false; |
| 78 |
|
| 79 |
foreach ( self::$updates as $version => $update_callbacks ) { |
| 80 |
if ( version_compare( $current_db_version, $version, '<' ) ) { |
| 81 |
foreach ( $update_callbacks as $update_callback ) { |
| 82 |
self::$updater->push_to_queue( [ |
| 83 |
'callback_function' => $update_callback, |
| 84 |
'db_version' => $version, |
| 85 |
] ); |
| 86 |
} |
| 87 |
|
| 88 |
$has_new_update = true; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
if ( $has_new_update ) { |
| 93 |
self::$updater->save()->dispatch(); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Gets the current db version. |
| 99 |
* |
| 100 |
* @since 1.1.0 |
| 101 |
* @return bool|mixed |
| 102 |
*/ |
| 103 |
public function get_current_database_version() { |
| 104 |
$current_db_version = sellkit_get_option( 'current_db_version' ); |
| 105 |
|
| 106 |
if ( empty( $current_db_version ) ) { |
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
return $current_db_version; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Gets all tables which are not installed. |
| 115 |
* |
| 116 |
* @since 1.1.0 |
| 117 |
*/ |
| 118 |
public static function not_installed_tables() { |
| 119 |
global $wpdb; |
| 120 |
|
| 121 |
$database_name = DB_NAME; |
| 122 |
$sellkit_prefix = Database::DATABASE_PREFIX; |
| 123 |
|
| 124 |
// phpcs:disable |
| 125 |
$installed_tables = $wpdb->get_results( |
| 126 |
"SHOW TABLES from {$database_name} |
| 127 |
where Tables_in_{$database_name} like '%applied_funnel%' |
| 128 |
or Tables_in_{$database_name} like '%contact_segmentation%' |
| 129 |
" ); |
| 130 |
// phpcs:enable |
| 131 |
|
| 132 |
$neat_installed_tables = []; |
| 133 |
|
| 134 |
foreach ( $installed_tables as $installed_table ) { |
| 135 |
$table_data = (array) $installed_table; |
| 136 |
$neat_installed_tables[] = str_replace( $wpdb->prefix . $sellkit_prefix, '', $table_data[ "Tables_in_{$database_name}" ] ); |
| 137 |
} |
| 138 |
|
| 139 |
return array_diff( array_keys( Database::tables() ), $neat_installed_tables ); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Creates necessary tables. |
| 144 |
* |
| 145 |
* @since 1.1.0 |
| 146 |
* @param array $tables Tables. |
| 147 |
*/ |
| 148 |
public static function create_necessary_tables( $tables ) { |
| 149 |
foreach ( $tables as $table ) { |
| 150 |
Database::create_new_table( $table ); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Checks database tables. |
| 156 |
* |
| 157 |
* @since 1.1.0 |
| 158 |
*/ |
| 159 |
public static function check_database_tables() { |
| 160 |
$not_installed_tables = self::not_installed_tables(); |
| 161 |
|
| 162 |
if ( ! empty( $not_installed_tables ) ) { |
| 163 |
self::create_necessary_tables( $not_installed_tables ); |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
new Install(); |
| 169 |
|