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

374 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 'horizontal' for brand-new installs.
209 *
210 * @since 3.11.0
211 */
212 private static function seed_default_builder_mode()
213 {
214 $general_settings = get_option( '_wpfunnels_general_settings', array() );
215 if ( ! isset( $general_settings['funnel_builder_mode'] ) ) {
216 $general_settings['funnel_builder_mode'] = 'horizontal';
217 update_option( '_wpfunnels_general_settings', $general_settings );
218 }
219 }
220
221 /**
222 * Update db version to current
223 *
224 * @param null $version
225 *
226 * @since 1.0.0
227 */
228 private static function update_wpfunnels_db_version($version = null)
229 {
230 if ( self::needs_db_update() ) {
231 self::update();
232 } else {
233 update_site_option('wpfunnels_db_version', is_null($version) ? Wpfnl::get_instance()->get_version() : $version);
234 }
235 }
236
237
238 /**
239 * Retrieve the time when funnel is installed
240 *
241 * @return int|mixed|void
242 * @since 2.0.0
243 */
244 public static function get_installed_time() {
245 $installed_time = get_option( 'wpfunnels_installed_time' );
246 if ( ! $installed_time ) {
247 $installed_time = time();
248 update_site_option( 'wpfunnels_installed_time', $installed_time );
249 }
250 return $installed_time;
251 }
252
253
254 public static function update_installed_time() {
255 self::get_installed_time();
256 }
257
258
259 /**
260 * Create necessary databases on plugin installation process
261 *
262 * @since 3.1.7
263 */
264 public static function create_tables() {
265
266 if ( !self::should_create_table() ) {
267 return;
268 }
269
270 global $wpdb;
271 $wpdb->hide_errors();
272 $charset_collate = $wpdb->get_charset_collate();
273 $table_name = $wpdb->prefix . 'wpfnl_stats';
274
275 $sql = "CREATE TABLE IF NOT EXISTS $table_name (
276 id mediumint(9) NOT NULL AUTO_INCREMENT,
277 order_id bigint(20) unsigned NOT NULL,
278 funnel_id bigint(20) unsigned NOT NULL,
279 parent_id bigint(20) unsigned NOT NULL,
280 customer_id bigint(20) unsigned NOT NULL,
281 total_sales double DEFAULT 0 NOT NULL,
282 orderbump_sales double DEFAULT 0 NOT NULL,
283 upsell_sales double DEFAULT 0 NOT NULL,
284 downsell_sales double DEFAULT 0 NOT NULL,
285 gateway double DEFAULT 0 NOT NULL,
286 status varchar(20) NOT NULL,
287 paid_date datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
288 date_created datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
289 date_created_gmt datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
290 PRIMARY KEY (id)
291 ) $charset_collate;";
292
293 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
294 dbDelta( $sql );
295
296
297 /**
298 * Create checkout visits table for conversion rate tracking
299 */
300 $table_name = $wpdb->prefix . 'wpfnl_checkout_visits';
301 $sql = "CREATE TABLE IF NOT EXISTS $table_name (
302 id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
303 type varchar(10) NOT NULL DEFAULT 'store',
304 funnel_id bigint(20) unsigned NOT NULL DEFAULT 0,
305 session_hash varchar(32) NOT NULL,
306 visit_date date NOT NULL,
307 date_created datetime NOT NULL,
308 PRIMARY KEY (id),
309 UNIQUE KEY unique_visit (session_hash, type, funnel_id, visit_date)
310 ) $charset_collate;";
311 dbDelta( $sql );
312
313 /**
314 * Create table for optin entries
315 */
316 $table_name = $wpdb->prefix . 'wpfnl_optin_entries';
317 $sql = "CREATE TABLE IF NOT EXISTS $table_name (
318 id mediumint(9) NOT NULL AUTO_INCREMENT,
319 funnel_id bigint(20) unsigned NOT NULL,
320 step_id bigint(20) unsigned NOT NULL,
321 user_id bigint(20) unsigned NOT NULL,
322 email varchar(100) NOT NULL,
323 hash varchar(100) NOT NULL,
324 data LONGTEXT NULL DEFAULT NULL,
325 date_created datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
326 PRIMARY KEY (id)
327 ) $charset_collate;";
328 dbDelta( $sql );
329 }
330
331
332 /**
333 * Check if we need to create new tables
334 *
335 * @return bool
336 * @since 3.1.7
337 */
338 public static function should_create_table() {
339 $db_version = get_option( 'wpfunnels_db_version', '3.2.0' );
340
341 if ( version_compare('3.2.1', $db_version, '>') ) {
342 return true;
343 }
344 return false;
345 }
346
347
348 /**
349 * Get list of DB update callbacks.
350 *
351 * @return array
352 * @since 3.5.0
353 */
354 public static function get_db_update_callbacks() {
355 return self::$db_updates;
356 }
357
358
359 /**
360 * Is a DB update needed?
361 *
362 * @return bool
363 * @since 3.5.0
364 */
365 public static function needs_db_update() {
366 $current_db_version = get_option( 'wpfunnels_db_version', null );
367 $updates = self::get_db_update_callbacks();
368 $update_versions = array_keys( $updates );
369 usort( $update_versions, 'version_compare' );
370
371 return ! is_null( $current_db_version ) && version_compare( $current_db_version, end( $update_versions ), '<' );
372 }
373 }
374