PluginProbe
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell / 3.11.0
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell v3.11.0
3.13.3 3.13.2 3.13.1 3.13.0 3.12.13 3.12.12 3.12.11 3.12.10 3.12.9 3.12.8 3.12.7 3.12.6 3.12.5 3.12.4 3.12.3 3.12.1 3.12.2 3.12.0 3.11.1 3.11.0 3.10.9 3.10.8 3.10.7 3.10.6 2.8.16 All 261 releases
wpfunnels / includes / utils / class-wpfnl-activator.php

class-wpfnl-activator.php in WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell 3.11.0, at includes/utils/class-wpfnl-activator.php

354 lines 8.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Fired during plugin activation
5 *
6 * @link https://rextheme.com
7 * @since 1.0.0
8 *
9 * @package Wpfnl
10 * @subpackage Wpfnl/includes
11 */
12
13 use WPFunnels\Wpfnl;
14 use WPFunnels\Wpfnl_functions;
15
16 /**
17 * Fired during plugin activation.
18 *
19 * This class defines all code necessary to run during the plugin's activation.
20 *
21 * @since 1.0.0
22 * @package Wpfnl
23 * @subpackage Wpfnl/includes
24 * @author RexTheme <[email protected]>
25 */
26 class Wpfnl_Activator
27 {
28
29 private static $db_updates = array(
30 '3.5.0' => array(
31 'wpf_create_350_stat_table',
32 'wpf_create_350_optin_entries_table'
33 ),
34 '3.11.0' => array(
35 'wpf_create_3110_checkout_visits_table',
36 ),
37 );
38
39
40 /**
41 * Init hook
42 *
43 * @return void
44 */
45 public static function init() {
46 add_action( 'init', array( __CLASS__, 'update' ), 20 );
47 add_action('wpfunnels_run_update_callback', array( __CLASS__, 'run_update_callback' ) );
48 }
49
50 /**
51 * Push all db updates to ActionScheduler
52 *
53 * @return void
54 * @since 3.5.0
55 */
56 public static function update() {
57 $current_db_version = get_option( 'wpfunnels_db_version' );
58 $loop = 0;
59
60 $db_update_callbacks= self::get_db_update_callbacks();
61
62 foreach ( $db_update_callbacks as $version => $update_callbacks ) {
63 if ( version_compare( $current_db_version, $version, '<' ) ) {
64 foreach ( $update_callbacks as $update_callback ) {
65 if (!as_next_scheduled_action('wpfunnels_run_update_callback', array(
66 'update_callback' => $update_callback,
67 ),
68 'wpfunnels-db-updates'
69 )) {
70 as_schedule_single_action(
71 time() + $loop,
72 'wpfunnels_run_update_callback',
73 array(
74 'update_callback' => $update_callback,
75 ),
76 'wpfunnels-db-updates'
77 );
78 ++$loop;
79 }
80 }
81 }
82 }
83
84 // After the callbacks finish, update the db version to the current WPFunnel version.
85 $current_wpfnl_version = wpfnl()->get_version();
86 if ( version_compare( $current_db_version, $current_wpfnl_version, '<' ) ) {
87 update_option( 'wpfunnels_db_version', $current_wpfnl_version );
88 }
89 }
90
91
92 /**
93 * Run an update callback when triggered by ActionScheduler.
94 *
95 * @param $update_callback
96 * @since 3.2.0
97 */
98 public static function run_update_callback( $update_callback ) {
99 include_once WPFNL_DIR . '/includes/wpf-update-functions.php';
100 if ( is_callable( $update_callback ) ) {
101 self::run_update_callback_start( $update_callback );
102 $result = (bool) call_user_func( $update_callback );
103 self::run_update_callback_end( $update_callback, $result );
104 }
105 }
106
107
108 /**
109 * Triggered when a callback will run.
110 *
111 * @since 3.2.0
112 * @param string $callback Callback name.
113 */
114 protected static function run_update_callback_start( $callback ) {
115 if ( ! defined( 'WPF_UPDATING' ) ) {
116 define( 'WPF_UPDATING', true );
117 }
118 }
119
120
121 /**
122 * Triggered when a callback has ran.
123 *
124 * @param $callback
125 * @param $result
126 */
127 protected static function run_update_callback_end( $callback, $result ) {
128 if ( $result ) {
129 as_schedule_single_action(
130 time(),
131 'wpfunnels_run_update_callback',
132 array(
133 'update_callback' => $callback,
134 ),
135 'wpfunnels-db-updates'
136 );
137 } else {
138 update_option('wpfunnels_'.$callback.'_update', 'completed');
139 }
140 }
141
142
143
144 /**
145 * Initiate the activation process
146 *
147 * @since 1.0.0
148 */
149 public static function activate(){
150 set_transient( 'wpfunnels_just_activated', true, 60 );
151 self::set_wpfunnels_activation_transients();
152
153 // added from version 3.1.7
154 self::create_tables();
155
156 self::update_wpfunnles_version();
157 self::update_wpfunnels_db_version();
158 self::update_installed_time();
159
160 // add funnel type meta
161 Wpfnl_functions::add_type_meta();
162 }
163
164
165
166
167 /**
168 * Update WP Funnels version to current.
169 *
170 * @since 1.0.0
171 */
172 private static function update_wpfunnles_version()
173 {
174 update_site_option('wpfunnels_version', Wpfnl::get_instance()->get_version());
175 }
176
177
178 /**
179 * See if we need to redirect the admin to setup wizard or not.
180 *
181 * @since 1.0.0
182 */
183 private static function set_wpfunnels_activation_transients()
184 {
185 if (self::is_new_install()) {
186 set_transient('_wpfunnels_activation_redirect', 1, 30);
187 }
188 }
189
190 /**
191 * Brand new install of wpfunnels
192 *
193 * @return bool
194 * @since 1.0.0
195 */
196 public static function is_new_install()
197 {
198 return is_null(get_site_option('wpfunnels_version', null));
199 }
200
201 /**
202 * Update db version to current
203 *
204 * @param null $version
205 *
206 * @since 1.0.0
207 */
208 private static function update_wpfunnels_db_version($version = null)
209 {
210 if ( self::needs_db_update() ) {
211 self::update();
212 } else {
213 update_site_option('wpfunnels_db_version', is_null($version) ? Wpfnl::get_instance()->get_version() : $version);
214 }
215 }
216
217
218 /**
219 * Retrieve the time when funnel is installed
220 *
221 * @return int|mixed|void
222 * @since 2.0.0
223 */
224 public static function get_installed_time() {
225 $installed_time = get_option( 'wpfunnels_installed_time' );
226 if ( ! $installed_time ) {
227 $installed_time = time();
228 update_site_option( 'wpfunnels_installed_time', $installed_time );
229 }
230 return $installed_time;
231 }
232
233
234 public static function update_installed_time() {
235 self::get_installed_time();
236 }
237
238
239 /**
240 * Create necessary databases on plugin installation process
241 *
242 * @since 3.1.7
243 */
244 public static function create_tables() {
245
246 if ( !self::should_create_table() ) {
247 return;
248 }
249
250 global $wpdb;
251 $wpdb->hide_errors();
252 $charset_collate = $wpdb->get_charset_collate();
253 $table_name = $wpdb->prefix . 'wpfnl_stats';
254
255 $sql = "CREATE TABLE IF NOT EXISTS $table_name (
256 id mediumint(9) NOT NULL AUTO_INCREMENT,
257 order_id bigint(20) unsigned NOT NULL,
258 funnel_id bigint(20) unsigned NOT NULL,
259 parent_id bigint(20) unsigned NOT NULL,
260 customer_id bigint(20) unsigned NOT NULL,
261 total_sales double DEFAULT 0 NOT NULL,
262 orderbump_sales double DEFAULT 0 NOT NULL,
263 upsell_sales double DEFAULT 0 NOT NULL,
264 downsell_sales double DEFAULT 0 NOT NULL,
265 gateway double DEFAULT 0 NOT NULL,
266 status varchar(20) NOT NULL,
267 paid_date datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
268 date_created datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
269 date_created_gmt datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
270 PRIMARY KEY (id)
271 ) $charset_collate;";
272
273 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
274 dbDelta( $sql );
275
276
277 /**
278 * Create checkout visits table for conversion rate tracking
279 */
280 $table_name = $wpdb->prefix . 'wpfnl_checkout_visits';
281 $sql = "CREATE TABLE IF NOT EXISTS $table_name (
282 id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
283 type varchar(10) NOT NULL DEFAULT 'store',
284 funnel_id bigint(20) unsigned NOT NULL DEFAULT 0,
285 session_hash varchar(32) NOT NULL,
286 visit_date date NOT NULL,
287 date_created datetime NOT NULL,
288 PRIMARY KEY (id),
289 UNIQUE KEY unique_visit (session_hash, type, funnel_id, visit_date)
290 ) $charset_collate;";
291 dbDelta( $sql );
292
293 /**
294 * Create table for optin entries
295 */
296 $table_name = $wpdb->prefix . 'wpfnl_optin_entries';
297 $sql = "CREATE TABLE IF NOT EXISTS $table_name (
298 id mediumint(9) NOT NULL AUTO_INCREMENT,
299 funnel_id bigint(20) unsigned NOT NULL,
300 step_id bigint(20) unsigned NOT NULL,
301 user_id bigint(20) unsigned NOT NULL,
302 email varchar(100) NOT NULL,
303 hash varchar(100) NOT NULL,
304 data LONGTEXT NULL DEFAULT NULL,
305 date_created datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
306 PRIMARY KEY (id)
307 ) $charset_collate;";
308 dbDelta( $sql );
309 }
310
311
312 /**
313 * Check if we need to create new tables
314 *
315 * @return bool
316 * @since 3.1.7
317 */
318 public static function should_create_table() {
319 $db_version = get_option( 'wpfunnels_db_version', '3.2.0' );
320
321 if ( version_compare('3.2.1', $db_version, '>') ) {
322 return true;
323 }
324 return false;
325 }
326
327
328 /**
329 * Get list of DB update callbacks.
330 *
331 * @return array
332 * @since 3.5.0
333 */
334 public static function get_db_update_callbacks() {
335 return self::$db_updates;
336 }
337
338
339 /**
340 * Is a DB update needed?
341 *
342 * @return bool
343 * @since 3.5.0
344 */
345 public static function needs_db_update() {
346 $current_db_version = get_option( 'wpfunnels_db_version', null );
347 $updates = self::get_db_update_callbacks();
348 $update_versions = array_keys( $updates );
349 usort( $update_versions, 'version_compare' );
350
351 return ! is_null( $current_db_version ) && version_compare( $current_db_version, end( $update_versions ), '<' );
352 }
353 }
354