PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / 1.1.4
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster v1.1.4
2.7.0 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 All 43 releases
sellkit / includes / core / install.php

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

197 lines 4.0 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 ];
36
37 /**
38 * Install constructor.
39 *
40 * @since 1.1.0
41 */
42 public function __construct() {
43 sellkit()->load_files( [
44 'core/admin/feedback',
45 ] );
46
47 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_scripts' ] );
48
49 $current_version = $this->get_current_database_version();
50 $last_version = array_key_last( self::$updates );
51
52 if ( $current_version === $last_version ) {
53 return;
54 }
55
56 sellkit()->load_files( [
57 'core/update/db-updater',
58 'core/update/updater-functions',
59 ] );
60
61 $this->maybe_update();
62 }
63
64 /**
65 * If there are some updates it start to update.
66 *
67 * @since 1.1.0
68 */
69 public function maybe_update() {
70 $current_db_version = $this->get_current_database_version();
71 $current_db_version = ! empty( $current_db_version ) ? $current_db_version : 0;
72 self::$updater = new Db_Updater();
73 $has_new_update = false;
74
75 foreach ( self::$updates as $version => $update_callbacks ) {
76 if ( version_compare( $current_db_version, $version, '<' ) ) {
77 foreach ( $update_callbacks as $update_callback ) {
78 self::$updater->push_to_queue( [
79 'callback_function' => $update_callback,
80 'db_version' => $version,
81 ] );
82 }
83
84 $has_new_update = true;
85 }
86 }
87
88 if ( $has_new_update ) {
89 self::$updater->save()->dispatch();
90 }
91 }
92
93 /**
94 * Gets the current db version.
95 *
96 * @since 1.1.0
97 * @return bool|mixed
98 */
99 public function get_current_database_version() {
100 $current_db_version = sellkit_get_option( 'current_db_version' );
101
102 if ( empty( $current_db_version ) ) {
103 return false;
104 }
105
106 return $current_db_version;
107 }
108
109 /**
110 * Gets all tables which are not installed.
111 *
112 * @since 1.1.0
113 */
114 public static function not_installed_tables() {
115 global $wpdb;
116
117 $database_name = DB_NAME;
118 $sellkit_prefix = Database::DATABASE_PREFIX;
119
120 // phpcs:disable
121 $installed_tables = $wpdb->get_results(
122 "SHOW TABLES from {$database_name}
123 where Tables_in_{$database_name} like '%applied_funnel%'
124 or Tables_in_{$database_name} like '%contact_segmentation%'
125 " );
126 // phpcs:enable
127
128 $neat_installed_tables = [];
129
130 foreach ( $installed_tables as $installed_table ) {
131 $table_data = (array) $installed_table;
132 $neat_installed_tables[] = str_replace( $wpdb->prefix . $sellkit_prefix, '', $table_data[ "Tables_in_{$database_name}" ] );
133 }
134
135 return array_diff( array_keys( Database::tables() ), $neat_installed_tables );
136 }
137
138 /**
139 * Creates necessary tables.
140 *
141 * @since 1.1.0
142 * @param array $tables Tables.
143 */
144 public static function create_necessary_tables( $tables ) {
145 foreach ( $tables as $table ) {
146 Database::create_new_table( $table );
147 }
148 }
149
150 /**
151 * Checks database tables.
152 *
153 * @since 1.1.0
154 */
155 public static function check_database_tables() {
156 $not_installed_tables = self::not_installed_tables();
157
158 if ( ! empty( $not_installed_tables ) ) {
159 self::create_necessary_tables( $not_installed_tables );
160 }
161 }
162
163 /**
164 * Enqueues admin scripts.
165 *
166 * @since 1.1.0
167 */
168 public function enqueue_admin_scripts() {
169 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
170
171 wp_enqueue_script(
172 'sellkit-admin',
173 sellkit()->plugin_url() . 'assets/dist/js/admin' . $suffix . '.js',
174 [ 'lodash', 'wp-element', 'wp-i18n', 'wp-util' ],
175 sellkit()->version(),
176 true
177 );
178
179 wp_enqueue_style(
180 'sellkit-admin',
181 sellkit()->plugin_url() . 'assets/dist/css/admin' . $suffix . '.css',
182 [],
183 sellkit()->version()
184 );
185
186 wp_localize_script(
187 'sellkit-admin',
188 'sellkitAdmin',
189 [
190 'nonce' => wp_create_nonce( 'sellkit_admin' ),
191 ],
192 );
193 }
194 }
195
196 new Install();
197