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

172 lines 3.5 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 $has_new_update = false;
81
82 foreach ( self::$updates as $version => $update_callbacks ) {
83 if ( version_compare( $current_db_version, $version, '<' ) ) {
84 foreach ( $update_callbacks as $update_callback ) {
85 self::$updater->push_to_queue( [
86 'callback_function' => $update_callback,
87 'db_version' => $version,
88 ] );
89 }
90
91 $has_new_update = true;
92 }
93 }
94
95 if ( $has_new_update ) {
96 self::$updater->save()->dispatch();
97 }
98 }
99
100 /**
101 * Gets the current db version.
102 *
103 * @since 1.1.0
104 * @return bool|mixed
105 */
106 public function get_current_database_version() {
107 $current_db_version = sellkit_get_option( 'current_db_version' );
108
109 if ( empty( $current_db_version ) ) {
110 return false;
111 }
112
113 return $current_db_version;
114 }
115
116 /**
117 * Gets all tables which are not installed.
118 *
119 * @since 1.1.0
120 */
121 public static function not_installed_tables() {
122 global $wpdb;
123
124 $database_name = DB_NAME;
125 $sellkit_prefix = Database::DATABASE_PREFIX;
126
127 // phpcs:disable
128 $installed_tables = $wpdb->get_results(
129 "SHOW TABLES from `{$database_name}`
130 where Tables_in_{$database_name} like '%applied_funnel%'
131 or Tables_in_{$database_name} like '%contact_segmentation%'
132 " );
133 // phpcs:enable
134
135 $neat_installed_tables = [];
136
137 foreach ( $installed_tables as $installed_table ) {
138 $table_data = (array) $installed_table;
139 $neat_installed_tables[] = str_replace( $wpdb->prefix . $sellkit_prefix, '', $table_data[ "Tables_in_{$database_name}" ] );
140 }
141
142 return array_diff( array_keys( Database::tables() ), $neat_installed_tables );
143 }
144
145 /**
146 * Creates necessary tables.
147 *
148 * @since 1.1.0
149 * @param array $tables Tables.
150 */
151 public static function create_necessary_tables( $tables ) {
152 foreach ( $tables as $table ) {
153 Database::create_new_table( $table );
154 }
155 }
156
157 /**
158 * Checks database tables.
159 *
160 * @since 1.1.0
161 */
162 public static function check_database_tables() {
163 $not_installed_tables = self::not_installed_tables();
164
165 if ( ! empty( $not_installed_tables ) ) {
166 self::create_necessary_tables( $not_installed_tables );
167 }
168 }
169 }
170
171 new Install();
172