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

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