PluginProbe
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell / 3.12.3
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell v3.12.3
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.12.3, at includes/utils/class-wpfnl-activator.php

375 lines 8.9 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 $is_new_install = self::is_new_install(); // capture before update_wpfunnles_version() sets the flag
151
152 set_transient( 'wpfunnels_just_activated', true, 60 );
153 self::set_wpfunnels_activation_transients();
154
155 // added from version 3.1.7
156 self::create_tables();
157
158 self::update_wpfunnles_version();
159 self::update_wpfunnels_db_version();
160 self::update_installed_time();
161
162 if ( $is_new_install ) {
163 self::seed_default_builder_mode();
164 }
165
166 // add funnel type meta
167 Wpfnl_functions::add_type_meta();
168 }
169
170
171
172
173 /**
174 * Update WP Funnels version to current.
175 *
176 * @since 1.0.0
177 */
178 private static function update_wpfunnles_version()
179 {
180 update_site_option('wpfunnels_version', Wpfnl::get_instance()->get_version());
181 }
182
183
184 /**
185 * See if we need to redirect the admin to setup wizard or not.
186 *
187 * @since 1.0.0
188 */
189 private static function set_wpfunnels_activation_transients()
190 {
191 if (self::is_new_install()) {
192 set_transient('_wpfunnels_activation_redirect', 1, 30);
193 }
194 }
195
196 /**
197 * Brand new install of wpfunnels
198 *
199 * @return bool
200 * @since 1.0.0
201 */
202 public static function is_new_install()
203 {
204 return is_null(get_site_option('wpfunnels_version', null));
205 }
206
207 /**
208 * Seed funnel_builder_mode to 'vertical' for brand-new installs.
209 * Existing users keep their stored value; horizontal remains the fallback default.
210 *
211 * @since 3.11.0
212 */
213 private static function seed_default_builder_mode()
214 {
215 $general_settings = get_option( '_wpfunnels_general_settings', array() );
216 if ( ! isset( $general_settings['funnel_builder_mode'] ) ) {
217 $general_settings['funnel_builder_mode'] = 'vertical';
218 update_option( '_wpfunnels_general_settings', $general_settings );
219 }
220 }
221
222 /**
223 * Update db version to current
224 *
225 * @param null $version
226 *
227 * @since 1.0.0
228 */
229 private static function update_wpfunnels_db_version($version = null)
230 {
231 if ( self::needs_db_update() ) {
232 self::update();
233 } else {
234 update_site_option('wpfunnels_db_version', is_null($version) ? Wpfnl::get_instance()->get_version() : $version);
235 }
236 }
237
238
239 /**
240 * Retrieve the time when funnel is installed
241 *
242 * @return int|mixed|void
243 * @since 2.0.0
244 */
245 public static function get_installed_time() {
246 $installed_time = get_option( 'wpfunnels_installed_time' );
247 if ( ! $installed_time ) {
248 $installed_time = time();
249 update_site_option( 'wpfunnels_installed_time', $installed_time );
250 }
251 return $installed_time;
252 }
253
254
255 public static function update_installed_time() {
256 self::get_installed_time();
257 }
258
259
260 /**
261 * Create necessary databases on plugin installation process
262 *
263 * @since 3.1.7
264 */
265 public static function create_tables() {
266
267 if ( !self::should_create_table() ) {
268 return;
269 }
270
271 global $wpdb;
272 $wpdb->hide_errors();
273 $charset_collate = $wpdb->get_charset_collate();
274 $table_name = $wpdb->prefix . 'wpfnl_stats';
275
276 $sql = "CREATE TABLE IF NOT EXISTS $table_name (
277 id mediumint(9) NOT NULL AUTO_INCREMENT,
278 order_id bigint(20) unsigned NOT NULL,
279 funnel_id bigint(20) unsigned NOT NULL,
280 parent_id bigint(20) unsigned NOT NULL,
281 customer_id bigint(20) unsigned NOT NULL,
282 total_sales double DEFAULT 0 NOT NULL,
283 orderbump_sales double DEFAULT 0 NOT NULL,
284 upsell_sales double DEFAULT 0 NOT NULL,
285 downsell_sales double DEFAULT 0 NOT NULL,
286 gateway double DEFAULT 0 NOT NULL,
287 status varchar(20) NOT NULL,
288 paid_date datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
289 date_created datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
290 date_created_gmt datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
291 PRIMARY KEY (id)
292 ) $charset_collate;";
293
294 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
295 dbDelta( $sql );
296
297
298 /**
299 * Create checkout visits table for conversion rate tracking
300 */
301 $table_name = $wpdb->prefix . 'wpfnl_checkout_visits';
302 $sql = "CREATE TABLE IF NOT EXISTS $table_name (
303 id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
304 type varchar(10) NOT NULL DEFAULT 'store',
305 funnel_id bigint(20) unsigned NOT NULL DEFAULT 0,
306 session_hash varchar(32) NOT NULL,
307 visit_date date NOT NULL,
308 date_created datetime NOT NULL,
309 PRIMARY KEY (id),
310 UNIQUE KEY unique_visit (session_hash, type, funnel_id, visit_date)
311 ) $charset_collate;";
312 dbDelta( $sql );
313
314 /**
315 * Create table for optin entries
316 */
317 $table_name = $wpdb->prefix . 'wpfnl_optin_entries';
318 $sql = "CREATE TABLE IF NOT EXISTS $table_name (
319 id mediumint(9) NOT NULL AUTO_INCREMENT,
320 funnel_id bigint(20) unsigned NOT NULL,
321 step_id bigint(20) unsigned NOT NULL,
322 user_id bigint(20) unsigned NOT NULL,
323 email varchar(100) NOT NULL,
324 hash varchar(100) NOT NULL,
325 data LONGTEXT NULL DEFAULT NULL,
326 date_created datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
327 PRIMARY KEY (id)
328 ) $charset_collate;";
329 dbDelta( $sql );
330 }
331
332
333 /**
334 * Check if we need to create new tables
335 *
336 * @return bool
337 * @since 3.1.7
338 */
339 public static function should_create_table() {
340 $db_version = get_option( 'wpfunnels_db_version', '3.2.0' );
341
342 if ( version_compare('3.2.1', $db_version, '>') ) {
343 return true;
344 }
345 return false;
346 }
347
348
349 /**
350 * Get list of DB update callbacks.
351 *
352 * @return array
353 * @since 3.5.0
354 */
355 public static function get_db_update_callbacks() {
356 return self::$db_updates;
357 }
358
359
360 /**
361 * Is a DB update needed?
362 *
363 * @return bool
364 * @since 3.5.0
365 */
366 public static function needs_db_update() {
367 $current_db_version = get_option( 'wpfunnels_db_version', null );
368 $updates = self::get_db_update_callbacks();
369 $update_versions = array_keys( $updates );
370 usort( $update_versions, 'version_compare' );
371
372 return ! is_null( $current_db_version ) && version_compare( $current_db_version, end( $update_versions ), '<' );
373 }
374 }
375