PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / 1.2.1
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster v1.2.1
2.6.0 trunk 1.1.0 1.1.4 1.2.1 1.2.2 1.2.3 1.2.5 1.2.9 1.3.1 1.3.2 1.5.0 1.5.1 1.5.4 1.5.7 1.5.8 1.5.9 1.6.2 1.6.5 1.6.8 1.7.2 1.7.4 1.7.5 1.7.9 1.8.1 All 42 releases
sellkit / includes / core / install.php

install.php in SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster 1.2.1, at includes/core/install.php

166 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 ];
39
40 /**
41 * Install constructor.
42 *
43 * @since 1.1.0
44 */
45 public function __construct() {
46 sellkit()->load_files( [
47 'core/admin/feedback',
48 ] );
49
50 $current_version = $this->get_current_database_version();
51 $last_version = array_key_last( self::$updates );
52
53 if ( $current_version === $last_version ) {
54 return;
55 }
56
57 sellkit()->load_files( [
58 'core/update/db-updater',
59 'core/update/updater-functions',
60 ] );
61
62 $this->maybe_update();
63 }
64
65 /**
66 * If there are some updates it start to update.
67 *
68 * @since 1.1.0
69 */
70 public function maybe_update() {
71 $current_db_version = $this->get_current_database_version();
72 $current_db_version = ! empty( $current_db_version ) ? $current_db_version : 0;
73 self::$updater = new Db_Updater();
74 $has_new_update = false;
75
76 foreach ( self::$updates as $version => $update_callbacks ) {
77 if ( version_compare( $current_db_version, $version, '<' ) ) {
78 foreach ( $update_callbacks as $update_callback ) {
79 self::$updater->push_to_queue( [
80 'callback_function' => $update_callback,
81 'db_version' => $version,
82 ] );
83 }
84
85 $has_new_update = true;
86 }
87 }
88
89 if ( $has_new_update ) {
90 self::$updater->save()->dispatch();
91 }
92 }
93
94 /**
95 * Gets the current db version.
96 *
97 * @since 1.1.0
98 * @return bool|mixed
99 */
100 public function get_current_database_version() {
101 $current_db_version = sellkit_get_option( 'current_db_version' );
102
103 if ( empty( $current_db_version ) ) {
104 return false;
105 }
106
107 return $current_db_version;
108 }
109
110 /**
111 * Gets all tables which are not installed.
112 *
113 * @since 1.1.0
114 */
115 public static function not_installed_tables() {
116 global $wpdb;
117
118 $database_name = DB_NAME;
119 $sellkit_prefix = Database::DATABASE_PREFIX;
120
121 // phpcs:disable
122 $installed_tables = $wpdb->get_results(
123 "SHOW TABLES from {$database_name}
124 where Tables_in_{$database_name} like '%applied_funnel%'
125 or Tables_in_{$database_name} like '%contact_segmentation%'
126 " );
127 // phpcs:enable
128
129 $neat_installed_tables = [];
130
131 foreach ( $installed_tables as $installed_table ) {
132 $table_data = (array) $installed_table;
133 $neat_installed_tables[] = str_replace( $wpdb->prefix . $sellkit_prefix, '', $table_data[ "Tables_in_{$database_name}" ] );
134 }
135
136 return array_diff( array_keys( Database::tables() ), $neat_installed_tables );
137 }
138
139 /**
140 * Creates necessary tables.
141 *
142 * @since 1.1.0
143 * @param array $tables Tables.
144 */
145 public static function create_necessary_tables( $tables ) {
146 foreach ( $tables as $table ) {
147 Database::create_new_table( $table );
148 }
149 }
150
151 /**
152 * Checks database tables.
153 *
154 * @since 1.1.0
155 */
156 public static function check_database_tables() {
157 $not_installed_tables = self::not_installed_tables();
158
159 if ( ! empty( $not_installed_tables ) ) {
160 self::create_necessary_tables( $not_installed_tables );
161 }
162 }
163 }
164
165 new Install();
166