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

425 lines 10.7 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 /**
30 * Flags that the one-time canvas layout pin has already run.
31 *
32 * @since 3.12.13
33 */
34 const BUILDER_MODE_PINNED_OPTION = 'wpfunnels_builder_mode_pinned';
35
36 private static $db_updates = array(
37 '3.5.0' => array(
38 'wpf_create_350_stat_table',
39 'wpf_create_350_optin_entries_table'
40 ),
41 '3.11.0' => array(
42 'wpf_create_3110_checkout_visits_table',
43 ),
44 '3.12.12' => array(
45 'wpf_add_31212_stats_funnel_status_index',
46 ),
47 );
48
49
50 /**
51 * Init hook
52 *
53 * @return void
54 */
55 public static function init() {
56 add_action( 'init', array( __CLASS__, 'maybe_pin_legacy_builder_mode' ), 1 );
57 add_action( 'init', array( __CLASS__, 'update' ), 20 );
58 add_action('wpfunnels_run_update_callback', array( __CLASS__, 'run_update_callback' ) );
59 }
60
61 /**
62 * Push all db updates to ActionScheduler
63 *
64 * @return void
65 * @since 3.5.0
66 */
67 public static function update() {
68 $current_db_version = get_option( 'wpfunnels_db_version' );
69 $loop = 0;
70
71 $db_update_callbacks= self::get_db_update_callbacks();
72
73 foreach ( $db_update_callbacks as $version => $update_callbacks ) {
74 if ( version_compare( $current_db_version, $version, '<' ) ) {
75 foreach ( $update_callbacks as $update_callback ) {
76 if (!as_next_scheduled_action('wpfunnels_run_update_callback', array(
77 'update_callback' => $update_callback,
78 ),
79 'wpfunnels-db-updates'
80 )) {
81 as_schedule_single_action(
82 time() + $loop,
83 'wpfunnels_run_update_callback',
84 array(
85 'update_callback' => $update_callback,
86 ),
87 'wpfunnels-db-updates'
88 );
89 ++$loop;
90 }
91 }
92 }
93 }
94
95 // After the callbacks finish, update the db version to the current WPFunnel version.
96 $current_wpfnl_version = wpfnl()->get_version();
97 if ( version_compare( $current_db_version, $current_wpfnl_version, '<' ) ) {
98 update_option( 'wpfunnels_db_version', $current_wpfnl_version );
99 }
100 }
101
102
103 /**
104 * Run an update callback when triggered by ActionScheduler.
105 *
106 * @param $update_callback
107 * @since 3.2.0
108 */
109 public static function run_update_callback( $update_callback ) {
110 include_once WPFNL_DIR . '/includes/wpf-update-functions.php';
111 if ( is_callable( $update_callback ) ) {
112 self::run_update_callback_start( $update_callback );
113 $result = (bool) call_user_func( $update_callback );
114 self::run_update_callback_end( $update_callback, $result );
115 }
116 }
117
118
119 /**
120 * Triggered when a callback will run.
121 *
122 * @since 3.2.0
123 * @param string $callback Callback name.
124 */
125 protected static function run_update_callback_start( $callback ) {
126 if ( ! defined( 'WPF_UPDATING' ) ) {
127 define( 'WPF_UPDATING', true );
128 }
129 }
130
131
132 /**
133 * Triggered when a callback has ran.
134 *
135 * @param $callback
136 * @param $result
137 */
138 protected static function run_update_callback_end( $callback, $result ) {
139 if ( $result ) {
140 as_schedule_single_action(
141 time(),
142 'wpfunnels_run_update_callback',
143 array(
144 'update_callback' => $callback,
145 ),
146 'wpfunnels-db-updates'
147 );
148 } else {
149 update_option('wpfunnels_'.$callback.'_update', 'completed');
150 }
151 }
152
153
154
155 /**
156 * Initiate the activation process
157 *
158 * @since 1.0.0
159 */
160 public static function activate(){
161 $is_new_install = self::is_new_install(); // capture before update_wpfunnles_version() sets the flag
162
163 set_transient( 'wpfunnels_just_activated', true, 60 );
164 self::set_wpfunnels_activation_transients();
165
166 // added from version 3.1.7
167 self::create_tables();
168
169 self::update_wpfunnles_version();
170 self::update_wpfunnels_db_version();
171 self::update_installed_time();
172
173 if ( $is_new_install ) {
174 self::seed_default_builder_mode();
175 }
176
177 // add funnel type meta
178 Wpfnl_functions::add_type_meta();
179 }
180
181
182
183
184 /**
185 * Update WP Funnels version to current.
186 *
187 * @since 1.0.0
188 */
189 private static function update_wpfunnles_version()
190 {
191 update_site_option('wpfunnels_version', Wpfnl::get_instance()->get_version());
192 }
193
194
195 /**
196 * See if we need to redirect the admin to setup wizard or not.
197 *
198 * @since 1.0.0
199 */
200 private static function set_wpfunnels_activation_transients()
201 {
202 if (self::is_new_install()) {
203 set_transient('_wpfunnels_activation_redirect', 1, 30);
204 }
205 }
206
207 /**
208 * Brand new install of wpfunnels
209 *
210 * @return bool
211 * @since 1.0.0
212 */
213 public static function is_new_install()
214 {
215 return is_null(get_site_option('wpfunnels_version', null));
216 }
217
218 /**
219 * Seed funnel_builder_mode for brand-new installs.
220 *
221 * Writes the current default explicitly so the value in the database always
222 * reflects a decision, and marks the legacy pin as handled — a fresh install
223 * has no old default to preserve.
224 *
225 * @since 3.11.0
226 */
227 private static function seed_default_builder_mode()
228 {
229 $general_settings = get_option( '_wpfunnels_general_settings', array() );
230 if ( ! isset( $general_settings['funnel_builder_mode'] ) ) {
231 $general_settings['funnel_builder_mode'] = Wpfnl_functions::DEFAULT_FUNNEL_BUILDER_MODE;
232 update_option( '_wpfunnels_general_settings', $general_settings );
233 }
234
235 update_option( self::BUILDER_MODE_PINNED_OPTION, 'yes' );
236 }
237
238
239 /**
240 * Keep pre-existing sites on the horizontal canvas.
241 *
242 * The default canvas layout became vertical in 3.12.13. A site that has been
243 * running without a stored funnel_builder_mode has been on the old horizontal
244 * default all along, so flipping the fallback would silently rearrange every
245 * canvas its users know. This writes horizontal in explicitly, once, leaving
246 * the new default to apply only to installs created after the change. Users
247 * opt in to vertical from Settings or the canvas view switcher.
248 *
249 * Runs synchronously on `init` rather than through the Action Scheduler db
250 * updates, because a queued job could land after the user has already opened
251 * the builder and seen the wrong layout.
252 *
253 * @since 3.12.13
254 */
255 public static function maybe_pin_legacy_builder_mode()
256 {
257 if ( 'yes' === get_option( self::BUILDER_MODE_PINNED_OPTION ) ) {
258 return;
259 }
260
261 $general_settings = get_option( '_wpfunnels_general_settings', array() );
262 $general_settings = is_array( $general_settings ) ? $general_settings : array();
263
264 if ( ! isset( $general_settings['funnel_builder_mode'] ) ) {
265 $general_settings['funnel_builder_mode'] = 'horizontal';
266 update_option( '_wpfunnels_general_settings', $general_settings );
267 }
268
269 update_option( self::BUILDER_MODE_PINNED_OPTION, 'yes' );
270 }
271
272 /**
273 * Update db version to current
274 *
275 * @param null $version
276 *
277 * @since 1.0.0
278 */
279 private static function update_wpfunnels_db_version($version = null)
280 {
281 if ( self::needs_db_update() ) {
282 self::update();
283 } else {
284 update_site_option('wpfunnels_db_version', is_null($version) ? Wpfnl::get_instance()->get_version() : $version);
285 }
286 }
287
288
289 /**
290 * Retrieve the time when funnel is installed
291 *
292 * @return int|mixed|void
293 * @since 2.0.0
294 */
295 public static function get_installed_time() {
296 $installed_time = get_option( 'wpfunnels_installed_time' );
297 if ( ! $installed_time ) {
298 $installed_time = time();
299 update_site_option( 'wpfunnels_installed_time', $installed_time );
300 }
301 return $installed_time;
302 }
303
304
305 public static function update_installed_time() {
306 self::get_installed_time();
307 }
308
309
310 /**
311 * Create necessary databases on plugin installation process
312 *
313 * @since 3.1.7
314 */
315 public static function create_tables() {
316
317 if ( !self::should_create_table() ) {
318 return;
319 }
320
321 global $wpdb;
322 $wpdb->hide_errors();
323 $charset_collate = $wpdb->get_charset_collate();
324 $table_name = $wpdb->prefix . 'wpfnl_stats';
325
326 $sql = "CREATE TABLE IF NOT EXISTS $table_name (
327 id mediumint(9) NOT NULL AUTO_INCREMENT,
328 order_id bigint(20) unsigned NOT NULL,
329 funnel_id bigint(20) unsigned NOT NULL,
330 parent_id bigint(20) unsigned NOT NULL,
331 customer_id bigint(20) unsigned NOT NULL,
332 total_sales double DEFAULT 0 NOT NULL,
333 orderbump_sales double DEFAULT 0 NOT NULL,
334 upsell_sales double DEFAULT 0 NOT NULL,
335 downsell_sales double DEFAULT 0 NOT NULL,
336 gateway double DEFAULT 0 NOT NULL,
337 status varchar(20) NOT NULL,
338 paid_date datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
339 date_created datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
340 date_created_gmt datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
341 PRIMARY KEY (id)
342 ) $charset_collate;";
343
344 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
345 dbDelta( $sql );
346
347
348 /**
349 * Create checkout visits table for conversion rate tracking
350 */
351 $table_name = $wpdb->prefix . 'wpfnl_checkout_visits';
352 $sql = "CREATE TABLE IF NOT EXISTS $table_name (
353 id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
354 type varchar(10) NOT NULL DEFAULT 'store',
355 funnel_id bigint(20) unsigned NOT NULL DEFAULT 0,
356 session_hash varchar(32) NOT NULL,
357 visit_date date NOT NULL,
358 date_created datetime NOT NULL,
359 PRIMARY KEY (id),
360 UNIQUE KEY unique_visit (session_hash, type, funnel_id, visit_date)
361 ) $charset_collate;";
362 dbDelta( $sql );
363
364 /**
365 * Create table for optin entries
366 */
367 $table_name = $wpdb->prefix . 'wpfnl_optin_entries';
368 $sql = "CREATE TABLE IF NOT EXISTS $table_name (
369 id mediumint(9) NOT NULL AUTO_INCREMENT,
370 funnel_id bigint(20) unsigned NOT NULL,
371 step_id bigint(20) unsigned NOT NULL,
372 user_id bigint(20) unsigned NOT NULL,
373 email varchar(100) NOT NULL,
374 hash varchar(100) NOT NULL,
375 data LONGTEXT NULL DEFAULT NULL,
376 date_created datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
377 PRIMARY KEY (id)
378 ) $charset_collate;";
379 dbDelta( $sql );
380 }
381
382
383 /**
384 * Check if we need to create new tables
385 *
386 * @return bool
387 * @since 3.1.7
388 */
389 public static function should_create_table() {
390 $db_version = get_option( 'wpfunnels_db_version', '3.2.0' );
391
392 if ( version_compare('3.2.1', $db_version, '>') ) {
393 return true;
394 }
395 return false;
396 }
397
398
399 /**
400 * Get list of DB update callbacks.
401 *
402 * @return array
403 * @since 3.5.0
404 */
405 public static function get_db_update_callbacks() {
406 return self::$db_updates;
407 }
408
409
410 /**
411 * Is a DB update needed?
412 *
413 * @return bool
414 * @since 3.5.0
415 */
416 public static function needs_db_update() {
417 $current_db_version = get_option( 'wpfunnels_db_version', null );
418 $updates = self::get_db_update_callbacks();
419 $update_versions = array_keys( $updates );
420 usort( $update_versions, 'version_compare' );
421
422 return ! is_null( $current_db_version ) && version_compare( $current_db_version, end( $update_versions ), '<' );
423 }
424 }
425