PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / trunk
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster vtrunk
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 trunk, at includes/core/install.php

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