WooCommerce Wishlist Plugin - Version 1.5.9

Version Description

Release Date - 21 November 2017

  • Fixed Wishlist Products Counter issue when the wrong number of products was displaying if cache is enabled
  • Fixed an issue with W3 Total Cache plugin
  • Fixed an issue with wrong file path after website migration
  • Fixed an issue with extra scheduled cleanup events
  • Fixed an issue with reset some settings after plugin update
  • Added support for WP Fastest Cache plugin
Download this release

Release Info

Developer templateinvaders
Plugin Icon 128x128 WooCommerce Wishlist Plugin
Version 1.5.9
Comparing to
See all releases

Code changes from version 1.5.8 to 1.5.9

admin/settings/general.class.php CHANGED
@@ -97,7 +97,7 @@ class TInvWL_Admin_Settings_General extends TInvWL_Admin_BaseSection {
97
  'text' => __( 'Remove product from Wishlist on second click', 'ti-woocommerce-wishlist' ),
98
  'std' => false,
99
  'extra' => array(
100
- 'tiwl-show' => '.tiwl-general-simple-flow>td',
101
  'tiwl-hide' => '.tiwl-general-simple-flow-hide>td',
102
  ),
103
  ),
97
  'text' => __( 'Remove product from Wishlist on second click', 'ti-woocommerce-wishlist' ),
98
  'std' => false,
99
  'extra' => array(
100
+ 'tiwl-show' => '.tiwl-general-simple-flow>td, .tiwl-button-simple-flow',
101
  'tiwl-hide' => '.tiwl-general-simple-flow-hide>td',
102
  ),
103
  ),
admin/tinvwl.class.php CHANGED
@@ -1,473 +1,496 @@
1
- <?php
2
- /**
3
- * Admin pages class
4
- *
5
- * @since 1.0.0
6
- * @package TInvWishlist\Admin
7
- */
8
-
9
- // If this file is called directly, abort.
10
- if ( ! defined( 'ABSPATH' ) ) {
11
- die;
12
- }
13
-
14
- /**
15
- * Admin pages class
16
- */
17
- class TInvWL_Admin_TInvWL extends TInvWL_Admin_Base {
18
-
19
- /**
20
- * Constructor
21
- *
22
- * @param string $plugin_name Plugin name.
23
- * @param string $version Plugin version.
24
- */
25
- function __construct( $plugin_name, $version ) {
26
- $this->_n = $plugin_name;
27
- $this->_v = $version;
28
- }
29
-
30
- /**
31
- * Load functions.
32
- * Create Wishlist and Product class.
33
- * Load settings classes.
34
- */
35
- function load_function() {
36
- TII18n();
37
- TInvWL_Notice::instance()->add_notice( 'rating', '<p>' . __( 'Woo-Ha! It has been a month since the first wishlist was created with <strong>WooCommerce WishList plugin</strong>!', 'ti-woocommerce-wishlist' ) . '</p><p>' . __( 'What do you think about our plugin?', 'ti-woocommerce-wishlist' ) . '</p><p>' . __( 'Share your love with us.', 'ti-woocommerce-wishlist' ) . '</p>' )->add_trigger( 'admin_init', 'tinvwl_rating_notice_trigger_30' );
38
- $this->load_settings();
39
-
40
- $this->define_hooks();
41
- }
42
-
43
- /**
44
- * Load settings classes.
45
- *
46
- * @return boolean
47
- */
48
- function load_settings() {
49
- $dir = TINVWL_PATH . 'admin/settings/';
50
- if ( ! file_exists( $dir ) || ! is_dir( $dir ) ) {
51
- return false;
52
- }
53
- $files = scandir( $dir );
54
- foreach ( $files as $value ) {
55
- if ( preg_match( '/\.class\.php$/i', $value ) ) {
56
- $file = preg_replace( '/\.class\.php$/i', '', $value );
57
- $class = 'TInvWL_Admin_Settings_' . ucfirst( $file );
58
- $settings = new $class( $this->_n, $this->_v );
59
- }
60
- }
61
- return true;
62
- }
63
-
64
- /**
65
- * Define hooks
66
- */
67
- function define_hooks() {
68
- add_action( 'admin_menu', array( $this, 'action_menu' ) );
69
- if ( 'skip' === filter_input( INPUT_GET, $this->_n . '-wizard' ) ) {
70
- update_option( $this->_n . '_wizard', true );
71
- }
72
- if ( ! get_option( $this->_n . '_wizard' ) ) {
73
- add_action( 'admin_notices', array( $this, 'wizard_run_admin_notice' ) );
74
- } elseif ( ! tinv_get_option( 'page', 'wishlist' ) ) {
75
- add_action( 'admin_notices', array( $this, 'empty_page_admin_notice' ) );
76
- }
77
- add_action( 'woocommerce_system_status_report', array( $this, 'system_report_templates' ) );
78
-
79
- add_action( 'switch_theme', array( $this, 'admin_notice_outdated_templates' ) );
80
- add_action( 'tinvwl_updated', array( $this, 'admin_notice_outdated_templates' ) );
81
- add_action( 'wp_ajax_' . $this->_n . '_checker_hook', array( $this, 'validation_template' ) );
82
- add_action( 'switch_theme', array( $this, 'clear_notice_validation_template' ) );
83
- add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts_validate_template' ) );
84
- add_action( 'tinvwl_admin_promo_footer', array( $this, 'promo_footer' ) );
85
- add_action( 'tinvwl_remove_without_author_wishlist', array( $this, 'remove_empty_wishlists' ) );
86
- add_action( 'tinvwl_remove_without_author_wishlist', array( $this, 'remove_old_wishlists' ) );
87
- $this->scheduled_remove_wishlist();
88
- }
89
-
90
- /**
91
- * Error notice if wizard didn't run.
92
- */
93
- function wizard_run_admin_notice() {
94
- printf( '<div class="notice notice-error"><p>%1$s</p><p><a href="%2$s" class="button-primary">%3$s</a> <a href="%4$s" class="button-secondary">%5$s</a></p></div>',
95
- __( '<strong>Welcome to WooCommerce Wishlist Plugin<strong> You‘re almost ready to start :)', 'ti-woocommerce-wishlist' ), // @codingStandardsIgnoreLine WordPress.XSS.EscapeOutput.OutputNotEscaped
96
- esc_url( admin_url( 'index.php?page=tinvwl-wizard' ) ),
97
- esc_html__( 'Run the Setup Wizard', 'ti-woocommerce-wishlist' ),
98
- esc_url( admin_url( 'index.php?page=' . $this->_n . '&' . $this->_n . '-wizard=skip' ) ),
99
- esc_html__( 'Skip Setup', 'ti-woocommerce-wishlist' )
100
- );
101
- }
102
-
103
- /**
104
- * Error notice if wishlist page not set.
105
- */
106
- function empty_page_admin_notice() {
107
- printf( '<div class="notice notice-error is-dismissible" style="position: relative;"><p>%1$s <a href="%2$s">%3$s</a>%4$s<a href="%5$s">%6$s</a></p><button type="button" class="notice-dismiss"><span class="screen-reader-text">' . __( 'Dismiss' ) . '</span></button></div>', // @codingStandardsIgnoreLine WordPress.XSS.EscapeOutput.OutputNotEscaped
108
- esc_html__( 'Link to Wishlists does not work!', 'ti-woocommerce-wishlist' ),
109
- esc_url( $this->admin_url( '' ) . '#general' ),
110
- esc_html__( 'Please apply the Wishlist page', 'ti-woocommerce-wishlist' ),
111
- esc_html__( ' or ', 'ti-woocommerce-wishlist' ),
112
- esc_url( admin_url( 'index.php?page=tinvwl-wizard' ) ),
113
- esc_html__( 'Run the Setup Wizard', 'ti-woocommerce-wishlist' )
114
- );
115
- }
116
-
117
- /**
118
- * Creation mune and sub-menu
119
- */
120
- function action_menu() {
121
- $page = add_menu_page( 'TI Wishlists', 'TI Wishlists', 'manage_options', $this->_n, null, TINVWL_URL . 'asset/img/icon_menu.png', 56 );
122
- add_action( "load-$page", array( $this, 'onload' ) );
123
- $menu = apply_filters( $this->_n . '_admin_menu', array() );
124
- foreach ( $menu as $item ) {
125
- if ( ! array_key_exists( 'page_title', $item ) ) {
126
- $item['page_title'] = $item['title'];
127
- }
128
- if ( ! array_key_exists( 'parent', $item ) ) {
129
- $item['parent'] = $this->_n;
130
- }
131
- if ( ! array_key_exists( 'capability', $item ) ) {
132
- $item['capability'] = 'manage_options';
133
- }
134
- $item['slug'] = implode( '-', array_filter( array( $this->_n, $item['slug'] ) ) );
135
-
136
- $page = add_submenu_page( $item['parent'], $item['page_title'], $item['title'], $item['capability'], $item['slug'], $item['method'] );
137
- add_action( "load-$page", array( $this, 'onload' ) );
138
- }
139
- }
140
-
141
- /**
142
- * Load style and javascript
143
- */
144
- function onload() {
145
- add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) );
146
- add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
147
- add_filter( 'admin_footer_text', array( $this, 'footer_admin' ) );
148
- add_filter( 'screen_options_show_screen', array( $this, 'screen_options_hide_screen' ), 10, 2 );
149
-
150
- add_filter( $this->_n . '_view_panelstatus', array( $this, 'status_panel' ), 9999 );
151
- }
152
-
153
- /**
154
- * Load style
155
- */
156
- function enqueue_styles() {
157
- wp_enqueue_style( 'gfonts', ( is_ssl() ? 'https' : 'http' ) . '://fonts.googleapis.com/css?family=Open+Sans:400,600,700,800', '', null, 'all' );
158
- wp_enqueue_style( $this->_n, TINVWL_URL . 'asset/css/admin.css', array(), $this->_v, 'all' );
159
- wp_enqueue_style( $this->_n . '-font-awesome', TINVWL_URL . 'asset/css/font-awesome.min.css', array(), $this->_v, 'all' );
160
- wp_enqueue_style( $this->_n . '-form', TINVWL_URL . 'asset/css/admin-form.css', array(), $this->_v, 'all' );
161
- }
162
-
163
- /**
164
- * Load javascript
165
- */
166
- function enqueue_scripts() {
167
- $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
168
- wp_enqueue_script( $this->_n . '-bootstrap', TINVWL_URL . 'asset/js/bootstrap' . $suffix . '.js', array( 'jquery' ), $this->_v, 'all' );
169
- wp_register_script( $this->_n, TINVWL_URL . 'asset/js/admin' . $suffix . '.js', array( 'jquery', 'wp-color-picker' ), $this->_v, 'all' );
170
- wp_localize_script( $this->_n, 'tinvwl_comfirm', array(
171
- 'text_comfirm_reset' => __( 'Are you sure you want to reset the settings?', 'ti-woocommerce-wishlist' ),
172
- ) );
173
- wp_enqueue_script( $this->_n );
174
- }
175
-
176
- /**
177
- * Add plugin footer copywriting
178
- */
179
- function footer_admin() {
180
- do_action( 'tinvwl_admin_promo_footer' );
181
- }
182
-
183
- /**
184
- * Promo in footer for wishlist
185
- */
186
- function promo_footer() {
187
- echo 'Made with <i class="fa fa-heart"></i> by <a href="https://templateinvaders.com/?utm_source=' . TINVWL_UTM_SOURCE . '&utm_campaign=' . TINVWL_UTM_CAMPAIGN . '&utm_medium=' . TINVWL_UTM_MEDIUM . '&utm_content=made_by&partner=' . TINVWL_UTM_SOURCE . '">TemplateInvaders</a><br />If you like WooCommerce Wishlist Plugin please leave us a <a href="https://wordpress.org/support/plugin/ti-woocommerce-wishlist/reviews/#new-post"><span><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i></span></a> rating.'; // WPCS: xss ok.
188
- }
189
-
190
- /**
191
- * Create Upgrade button
192
- *
193
- * @param array $panel Panel Button.
194
- *
195
- * @return array
196
- */
197
- function status_panel( $panel ) {
198
- array_unshift( $panel, sprintf( '<a class="tinvwl-btn red w-icon smaller-txt" href="%s"><i class="fa fa-star"></i><span class="tinvwl-txt">%s</span></a>', 'https://templateinvaders.com/product/ti-woocommerce-wishlist-wordpress-plugin/?utm_source=' . TINVWL_UTM_SOURCE . '&utm_campaign=' . TINVWL_UTM_CAMPAIGN . '&utm_medium=' . TINVWL_UTM_MEDIUM . '&utm_content=header_upgrade&partner=' . TINVWL_UTM_SOURCE, __( 'Upgrade to Pro', 'ti-woocommerce-wishlist' ) ) );
199
-
200
- return $panel;
201
- }
202
-
203
- /**
204
- * Templates overriding status check.
205
- *
206
- * @param boolean $outdated Out date status.
207
- * @return string
208
- */
209
- function templates_status_check( $outdated = false ) {
210
-
211
- $found_files = array();
212
-
213
- $scanned_files = WC_Admin_Status::scan_template_files( TINVWL_PATH . '/templates/' );
214
-
215
- foreach ( $scanned_files as $file ) {
216
- if ( file_exists( get_stylesheet_directory() . '/' . $file ) ) {
217
- $theme_file = get_stylesheet_directory() . '/' . $file;
218
- } elseif ( file_exists( get_stylesheet_directory() . '/woocommerce/' . $file ) ) {
219
- $theme_file = get_stylesheet_directory() . '/woocommerce/' . $file;
220
- } elseif ( file_exists( get_template_directory() . '/' . $file ) ) {
221
- $theme_file = get_template_directory() . '/' . $file;
222
- } elseif ( file_exists( get_template_directory() . '/woocommerce/' . $file ) ) {
223
- $theme_file = get_template_directory() . '/woocommerce/' . $file;
224
- } else {
225
- $theme_file = false;
226
- }
227
-
228
- if ( ! empty( $theme_file ) ) {
229
- $core_version = WC_Admin_Status::get_file_version( TINVWL_PATH . '/templates/' . $file );
230
- $theme_version = WC_Admin_Status::get_file_version( $theme_file );
231
-
232
- if ( $core_version && ( empty( $theme_version ) || version_compare( $theme_version, $core_version, '<' ) ) ) {
233
- if ( $outdated ) {
234
- return 'outdated';
235
- }
236
- $found_files[] = sprintf( __( '<code>%1$s</code> version <strong style="color:red">%2$s</strong> is out of date. The core version is <strong style="color:red">%3$s</strong>', 'ti-woocommerce-wishlist' ), str_replace( WP_CONTENT_DIR . '/themes/', '', $theme_file ), $theme_version ? $theme_version : '-', $core_version );
237
- } else {
238
- $found_files[] = str_replace( WP_CONTENT_DIR . '/themes/', '', $theme_file );
239
- }
240
- }
241
- }
242
-
243
- return $found_files;
244
- }
245
-
246
- /**
247
- * Templates overriding status for WooCommerce Status report page.
248
- */
249
- function system_report_templates() {
250
-
251
- TInvWL_View::view( 'templates-status', array( 'found_files' => $this->templates_status_check() ) );
252
- }
253
-
254
- /**
255
- * Outdated templates notice.
256
- */
257
- function admin_notice_outdated_templates() {
258
- if ( 'outdated' === $this->templates_status_check( true ) ) {
259
-
260
- $theme = wp_get_theme();
261
-
262
- $html = sprintf( __( '<strong>Your theme (%1$s) contains outdated copies of some WooCommerce Wishlist Plugin template files.</strong><br> These files may need updating to ensure they are compatible with the current version of WooCommerce Wishlist Plugin.<br> You can see which files are affected from the <a href="%2$s">system status page</a>.<br> If in doubt, check with the author of the theme.', 'ti-woocommerce-wishlist' ), esc_html( $theme['Name'] ), esc_url( admin_url( 'admin.php?page=wc-status' ) ) );
263
-
264
- WC_Admin_Notices::add_custom_notice( 'outdated_templates', $html );
265
- } else {
266
- WC_Admin_Notices::remove_notice( 'outdated_templates' );
267
- }
268
- }
269
-
270
- /**
271
- * Load javascript for validation templates
272
- */
273
- function enqueue_scripts_validate_template() {
274
- $theme = wp_get_theme();
275
- $theme = $theme->get_template();
276
- if ( tinv_get_option( 'template_checker', 'theme' ) !== $theme ) {
277
- tinv_update_option( 'template_checker', '', array() );
278
- tinv_update_option( 'template_checker', 'theme', $theme );
279
- tinv_update_option( 'template_checker', 'checked', false );
280
- tinv_update_option( 'template_checker', 'time', 0 );
281
- }
282
- if ( tinv_get_option( 'template_checker', 'checked' ) && absint( tinv_get_option( 'template_checker', 'time' ) ) + HOUR_IN_SECONDS > time() ) {
283
- return;
284
- }
285
- $types = array_keys( wc_get_product_types() );
286
- foreach ( $types as $type => $type_name ) {
287
- if ( ! tinv_get_option( 'template_checker', 'missing_hook_' . $type ) ) {
288
- $data = filter_input_array( INPUT_GET, array(
289
- 'wc-hide-notice' => FILTER_DEFAULT,
290
- '_wc_notice_nonce' => FILTER_DEFAULT,
291
- ) );
292
- if ( 'missing_hook_' . $type === $data['wc-hide-notice'] && wp_verify_nonce( $data['_wc_notice_nonce'], 'woocommerce_hide_notices_nonce' ) ) {
293
- tinv_update_option( 'template_checker', 'missing_hook_' . $type, true );
294
- }
295
- }
296
- }
297
- if ( ! tinv_get_option( 'template_checker', 'hide_product_listing' ) ) {
298
- $data = filter_input_array( INPUT_GET, array(
299
- 'wc-hide-notice' => FILTER_DEFAULT,
300
- '_wc_notice_nonce' => FILTER_DEFAULT,
301
- ) );
302
- if ( 'missing_hook_listing' === $data['wc-hide-notice'] && wp_verify_nonce( $data['_wc_notice_nonce'], 'woocommerce_hide_notices_nonce' ) ) {
303
- tinv_update_option( 'template_checker', 'hide_product_listing', true );
304
- }
305
- }
306
-
307
- wp_enqueue_script( $this->_n . '-checker', TINVWL_URL . 'asset/js/admin.checker.min.js', array( 'jquery' ), $this->_v, 'all' );
308
- }
309
-
310
- /**
311
- * Validation templates hook from request remote page
312
- */
313
- function validation_template() {
314
- global $post, $product;
315
-
316
- if ( tinv_get_option( 'template_checker', 'checked' ) ) {
317
- return;
318
- }
319
- if ( absint( tinv_get_option( 'template_checker', 'time' ) ) + HOUR_IN_SECONDS > time() ) {
320
- return;
321
- }
322
- tinv_update_option( 'template_checker', 'time', time() );
323
- $tags = array(
324
- 'woocommerce_single_product_summary' => 'tinvwl_single_product_summary',
325
- 'woocommerce_before_add_to_cart_button' => 'tinvwl_before_add_to_cart_button',
326
- 'woocommerce_after_add_to_cart_button' => 'tinvwl_after_add_to_cart_button',
327
- );
328
- $tch = TInvWL_CheckerHook::instance();
329
- $tch->add_action( $tags );
330
- $tch->add_action( array_keys( $tags ) );
331
-
332
- $types = wc_get_product_types();
333
-
334
- $check = true;
335
- foreach ( $types as $type => $type_name ) {
336
- if ( tinv_get_option( 'template_checker', 'missing_hook_' . $type ) ) {
337
- continue;
338
- }
339
-
340
- if ( function_exists( 'wc_get_products' ) ) {
341
- $products = wc_get_products( array(
342
- 'status' => 'publish',
343
- 'type' => $type,
344
- 'limit' => 1,
345
- ) );
346
- } else {
347
- $products = array_map( 'wc_get_product', get_posts( array(
348
- 'post_type' => 'product',
349
- 'post_status' => 'publish',
350
- 'numberposts' => 1,
351
- 'tax_query' => array(
352
- array(
353
- 'taxonomy' => 'product_type',
354
- 'field' => 'slug',
355
- 'terms' => $type,
356
- ),
357
- ),
358
- ) ) );
359
- }
360
- if ( ! empty( $products ) ) {
361
- $product = array_shift( $products );
362
- $post = get_post( $product->get_id() ); // @codingStandardsIgnoreLine WordPress.Variables.GlobalVariables.OverrideProhibited
363
- $result = $tch->run( array(
364
- 'template' => array( 'content-single-product.php', 'single-product/add-to-cart/' . $type . '.php' ),
365
- 'template_args' => array(
366
- 'available_variations' => array( 1, 2, 3, 4, 5 ),
367
- 'attributes' => array(),
368
- ),
369
- 'url' => $product->get_permalink(),
370
- ) );
371
- if ( ! empty( $result ) ) {
372
- $result = array_keys( $result );
373
- foreach ( $result as $key => $tag ) {
374
- if ( array_key_exists( $tag, $tags ) ) {
375
- $tags[ $tag ];
376
- if ( ! array_key_exists( $tag, $tags ) ) {
377
- unset( $result[ $key ] );
378
- }
379
- } else {
380
- unset( $result[ $key ] );
381
- }
382
- }
383
- if ( ! empty( $result ) ) {
384
- WC_Admin_Notices::add_custom_notice( 'missing_hook_' . $type, sprintf( _n( 'The "Add to Wishlist" button may work improperly in a product type "%1$s" because the hook "%2$s" is missing.<br />Please, ask your theme developers to check the theme templates or <a href="https://templateinvaders.com/help/" target="_blank">contact us</a> for assistance.', 'The "Add to Wishlist" button may work improperly in a product type "%1$s" because the hooks "%2$s" are missing.<br />Please, ask your theme developers to check the theme templates or <a href="https://templateinvaders.com/help/" target="_blank">contact us</a> for assistance.', count( $result ), 'ti-woocommerce-wishlist' ), $type_name, '<strong>' . join( '</strong>, <strong>', $result ) . '</strong>' ) );
385
- $check = false;
386
- } else {
387
- WC_Admin_Notices::remove_notice( 'missing_hook_' . $type );
388
- }
389
- } else {
390
- WC_Admin_Notices::remove_notice( 'missing_hook_' . $type );
391
- }
392
- }
393
- } // End foreach().
394
-
395
- tinv_update_option( 'template_checker', 'checked', $check );
396
- wp_die();
397
- }
398
-
399
- /**
400
- * Clear notice validation template when theme switched
401
- */
402
- function clear_notice_validation_template() {
403
- WC_Admin_Notices::remove_notice( 'missing_hook_listing' );
404
- $types = wc_get_product_types();
405
- foreach ( $types as $type => $type_name ) {
406
- WC_Admin_Notices::remove_notice( 'missing_hook_' . $type );
407
- }
408
- tinv_update_option( 'template_checker', '', array() );
409
- }
410
-
411
- /**
412
- * Disable screen option on plugin pages
413
- *
414
- * @param boolean $show_screen Show screen.
415
- * @param \WP_Screen $_this Screen option page.
416
- * @return boolean
417
- */
418
- function screen_options_hide_screen( $show_screen, $_this ) {
419
- if ( $this->_n === $_this->parent_base || $this->_n === $_this->parent_file ) {
420
- return false;
421
- }
422
- return $show_screen;
423
- }
424
-
425
- /**
426
- * Check if there is a hook in the cron
427
- */
428
- function scheduled_remove_wishlist() {
429
- $timestamp = wp_next_scheduled( 'tinvwl_remove_without_author_wishlist' );
430
- if ( $timestamp ) {
431
- $time = strtotime( '00:00 today +1 HOURS' );
432
- wp_schedule_event( $time, 'daily', 'tinvwl_remove_without_author_wishlist' );
433
- }
434
- }
435
-
436
- /**
437
- * Removing empty wishlist without a user older than 7 days
438
- */
439
- public function remove_empty_wishlists() {
440
- $wl = new TInvWL_Wishlist();
441
- $wishlists = $wl->get( array(
442
- 'author' => 0,
443
- 'type' => 'default',
444
- 'sql' => 'SELECT * FROM {table} {where} AND `date` < DATE_SUB( CURDATE(), INTERVAL 7 DAY)',
445
- ) );
446
- foreach ( $wishlists as $wishlist ) {
447
- $wlp = new TInvWL_Product( $wishlist );
448
- $products = $wlp->get_wishlist( array(
449
- 'count' => 1,
450
- 'external' => true,
451
- ) );
452
- if ( empty( $products ) ) {
453
- $wl->remove( $wishlist['ID'] );
454
- }
455
- }
456
- }
457
-
458
- /**
459
- * Removing old wishlist without a user older than 34 days
460
- */
461
- public function remove_old_wishlists() {
462
- $wl = new TInvWL_Wishlist();
463
- $wishlists = $wl->get( array(
464
- 'author' => 0,
465
- 'type' => 'default',
466
- 'sql' => 'SELECT * FROM {table} {where} AND `date` < DATE_SUB( CURDATE(), INTERVAL 34 DAY)',
467
- ) );
468
- foreach ( $wishlists as $wishlist ) {
469
- $wl->remove( $wishlist['ID'] );
470
- }
471
- }
472
-
473
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Admin pages class
4
+ *
5
+ * @since 1.0.0
6
+ * @package TInvWishlist\Admin
7
+ */
8
+
9
+ // If this file is called directly, abort.
10
+ if ( ! defined( 'ABSPATH' ) ) {
11
+ die;
12
+ }
13
+
14
+ /**
15
+ * Admin pages class
16
+ */
17
+ class TInvWL_Admin_TInvWL extends TInvWL_Admin_Base {
18
+
19
+ /**
20
+ * Constructor
21
+ *
22
+ * @param string $plugin_name Plugin name.
23
+ * @param string $version Plugin version.
24
+ */
25
+ function __construct( $plugin_name, $version ) {
26
+ $this->_n = $plugin_name;
27
+ $this->_v = $version;
28
+ }
29
+
30
+ /**
31
+ * Load functions.
32
+ * Create Wishlist and Product class.
33
+ * Load settings classes.
34
+ */
35
+ function load_function() {
36
+ TII18n();
37
+ TInvWL_Notice::instance()->add_notice( 'rating', '<p>' . __( 'Woo-Ha! It has been a month since the first wishlist was created with <strong>WooCommerce WishList plugin</strong>!', 'ti-woocommerce-wishlist' ) . '</p><p>' . __( 'What do you think about our plugin?', 'ti-woocommerce-wishlist' ) . '</p><p>' . __( 'Share your love with us.', 'ti-woocommerce-wishlist' ) . '</p>' )->add_trigger( 'admin_init', 'tinvwl_rating_notice_trigger_30' );
38
+ $this->load_settings();
39
+
40
+ $this->define_hooks();
41
+ }
42
+
43
+ /**
44
+ * Load settings classes.
45
+ *
46
+ * @return boolean
47
+ */
48
+ function load_settings() {
49
+ $dir = TINVWL_PATH . 'admin/settings/';
50
+ if ( ! file_exists( $dir ) || ! is_dir( $dir ) ) {
51
+ return false;
52
+ }
53
+ $files = scandir( $dir );
54
+ foreach ( $files as $value ) {
55
+ if ( preg_match( '/\.class\.php$/i', $value ) ) {
56
+ $file = preg_replace( '/\.class\.php$/i', '', $value );
57
+ $class = 'TInvWL_Admin_Settings_' . ucfirst( $file );
58
+ $settings = new $class( $this->_n, $this->_v );
59
+ }
60
+ }
61
+
62
+ return true;
63
+ }
64
+
65
+ /**
66
+ * Define hooks
67
+ */
68
+ function define_hooks() {
69
+ add_action( 'admin_menu', array( $this, 'action_menu' ) );
70
+ if ( 'skip' === filter_input( INPUT_GET, $this->_n . '-wizard' ) ) {
71
+ update_option( $this->_n . '_wizard', true );
72
+ }
73
+ if ( ! get_option( $this->_n . '_wizard' ) ) {
74
+ add_action( 'admin_notices', array( $this, 'wizard_run_admin_notice' ) );
75
+ } elseif ( ! tinv_get_option( 'page', 'wishlist' ) ) {
76
+ add_action( 'admin_notices', array( $this, 'empty_page_admin_notice' ) );
77
+ }
78
+ add_action( 'woocommerce_system_status_report', array( $this, 'system_report_templates' ) );
79
+
80
+ add_action( 'switch_theme', array( $this, 'admin_notice_outdated_templates' ) );
81
+ add_action( 'tinvwl_updated', array( $this, 'admin_notice_outdated_templates' ) );
82
+ add_action( 'wp_ajax_' . $this->_n . '_checker_hook', array( $this, 'validation_template' ) );
83
+ add_action( 'switch_theme', array( $this, 'clear_notice_validation_template' ) );
84
+ add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts_validate_template' ) );
85
+ add_action( 'tinvwl_admin_promo_footer', array( $this, 'promo_footer' ) );
86
+ add_action( 'tinvwl_remove_without_author_wishlist', array( $this, 'remove_empty_wishlists' ) );
87
+ add_action( 'tinvwl_remove_without_author_wishlist', array( $this, 'remove_old_wishlists' ) );
88
+ $this->scheduled_remove_wishlist();
89
+ }
90
+
91
+ /**
92
+ * Error notice if wizard didn't run.
93
+ */
94
+ function wizard_run_admin_notice() {
95
+ printf( '<div class="notice notice-error"><p>%1$s</p><p><a href="%2$s" class="button-primary">%3$s</a> <a href="%4$s" class="button-secondary">%5$s</a></p></div>',
96
+ __( '<strong>Welcome to WooCommerce Wishlist Plugin<strong> – You‘re almost ready to start :)', 'ti-woocommerce-wishlist' ), // @codingStandardsIgnoreLine WordPress.XSS.EscapeOutput.OutputNotEscaped
97
+ esc_url( admin_url( 'index.php?page=tinvwl-wizard' ) ),
98
+ esc_html__( 'Run the Setup Wizard', 'ti-woocommerce-wishlist' ),
99
+ esc_url( admin_url( 'index.php?page=' . $this->_n . '&' . $this->_n . '-wizard=skip' ) ),
100
+ esc_html__( 'Skip Setup', 'ti-woocommerce-wishlist' )
101
+ );
102
+ }
103
+
104
+ /**
105
+ * Error notice if wishlist page not set.
106
+ */
107
+ function empty_page_admin_notice() {
108
+ printf( '<div class="notice notice-error is-dismissible" style="position: relative;"><p>%1$s <a href="%2$s">%3$s</a>%4$s<a href="%5$s">%6$s</a></p><button type="button" class="notice-dismiss"><span class="screen-reader-text">' . __( 'Dismiss' ) . '</span></button></div>', // @codingStandardsIgnoreLine WordPress.XSS.EscapeOutput.OutputNotEscaped
109
+ esc_html__( 'Link to Wishlists does not work!', 'ti-woocommerce-wishlist' ),
110
+ esc_url( $this->admin_url( '' ) . '#general' ),
111
+ esc_html__( 'Please apply the Wishlist page', 'ti-woocommerce-wishlist' ),
112
+ esc_html__( ' or ', 'ti-woocommerce-wishlist' ),
113
+ esc_url( admin_url( 'index.php?page=tinvwl-wizard' ) ),
114
+ esc_html__( 'Run the Setup Wizard', 'ti-woocommerce-wishlist' )
115
+ );
116
+ }
117
+
118
+ /**
119
+ * Creation mune and sub-menu
120
+ */
121
+ function action_menu() {
122
+ $page = add_menu_page( 'TI Wishlists', 'TI Wishlists', 'manage_options', $this->_n, null, TINVWL_URL . 'asset/img/icon_menu.png', 56 );
123
+ add_action( "load-$page", array( $this, 'onload' ) );
124
+ $menu = apply_filters( $this->_n . '_admin_menu', array() );
125
+ foreach ( $menu as $item ) {
126
+ if ( ! array_key_exists( 'page_title', $item ) ) {
127
+ $item['page_title'] = $item['title'];
128
+ }
129
+ if ( ! array_key_exists( 'parent', $item ) ) {
130
+ $item['parent'] = $this->_n;
131
+ }
132
+ if ( ! array_key_exists( 'capability', $item ) ) {
133
+ $item['capability'] = 'manage_options';
134
+ }
135
+ $item['slug'] = implode( '-', array_filter( array( $this->_n, $item['slug'] ) ) );
136
+
137
+ $page = add_submenu_page( $item['parent'], $item['page_title'], $item['title'], $item['capability'], $item['slug'], $item['method'] );
138
+ add_action( "load-$page", array( $this, 'onload' ) );
139
+ }
140
+ }
141
+
142
+ /**
143
+ * Load style and javascript
144
+ */
145
+ function onload() {
146
+ add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) );
147
+ add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
148
+ add_filter( 'admin_footer_text', array( $this, 'footer_admin' ) );
149
+ add_filter( 'screen_options_show_screen', array( $this, 'screen_options_hide_screen' ), 10, 2 );
150
+
151
+ add_filter( $this->_n . '_view_panelstatus', array( $this, 'status_panel' ), 9999 );
152
+ }
153
+
154
+ /**
155
+ * Load style
156
+ */
157
+ function enqueue_styles() {
158
+ wp_enqueue_style( 'gfonts', ( is_ssl() ? 'https' : 'http' ) . '://fonts.googleapis.com/css?family=Open+Sans:400,600,700,800', '', null, 'all' );
159
+ wp_enqueue_style( $this->_n, TINVWL_URL . 'asset/css/admin.css', array(), $this->_v, 'all' );
160
+ wp_enqueue_style( $this->_n . '-font-awesome', TINVWL_URL . 'asset/css/font-awesome.min.css', array(), $this->_v, 'all' );
161
+ wp_enqueue_style( $this->_n . '-form', TINVWL_URL . 'asset/css/admin-form.css', array(), $this->_v, 'all' );
162
+ }
163
+
164
+ /**
165
+ * Load javascript
166
+ */
167
+ function enqueue_scripts() {
168
+ $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
169
+ wp_enqueue_script( $this->_n . '-bootstrap', TINVWL_URL . 'asset/js/bootstrap' . $suffix . '.js', array( 'jquery' ), $this->_v, 'all' );
170
+ wp_register_script( $this->_n, TINVWL_URL . 'asset/js/admin' . $suffix . '.js', array(
171
+ 'jquery',
172
+ 'wp-color-picker'
173
+ ), $this->_v, 'all' );
174
+ wp_localize_script( $this->_n, 'tinvwl_comfirm', array(
175
+ 'text_comfirm_reset' => __( 'Are you sure you want to reset the settings?', 'ti-woocommerce-wishlist' ),
176
+ ) );
177
+ wp_enqueue_script( $this->_n );
178
+
179
+ $user_id = get_current_user_id();
180
+ $user_info = get_userdata( $user_id );
181
+
182
+ wp_add_inline_script( $this->_n, 'window.intercomSettings = {
183
+ app_id: "zyh6v0pc",
184
+ email: "' . $user_info->user_email . '",
185
+ name: "' . $user_info->user_nicename . '",
186
+ "Website": "' . get_site_url() . '",
187
+ "Plugin name": "WooCommerce Wishlist Plugin",
188
+ "Plugin version":"' . TINVWL_FVERSION . '",
189
+ };
190
+ (function(){var w=window;var ic=w.Intercom;if(typeof ic==="function"){ic("reattach_activator");ic("update",intercomSettings);}else{var d=document;var i=function(){i.c(arguments)};i.q=[];i.c=function(args){i.q.push(args)};w.Intercom=i;function l(){var s=d.createElement("script");s.type="text/javascript";s.async=true;s.src="https://widget.intercom.io/widget/zyh6v0pc";var x=d.getElementsByTagName("script")[0];x.parentNode.insertBefore(s,x);}if(w.attachEvent){w.attachEvent("onload",l);}else{w.addEventListener("load",l,false);}}})()' );
191
+ }
192
+
193
+ /**
194
+ * Add plugin footer copywriting
195
+ */
196
+ function footer_admin() {
197
+ do_action( 'tinvwl_admin_promo_footer' );
198
+ }
199
+
200
+ /**
201
+ * Promo in footer for wishlist
202
+ */
203
+ function promo_footer() {
204
+ echo 'Made with <i class="fa fa-heart"></i> by <a href="https://templateinvaders.com/?utm_source=' . TINVWL_UTM_SOURCE . '&utm_campaign=' . TINVWL_UTM_CAMPAIGN . '&utm_medium=' . TINVWL_UTM_MEDIUM . '&utm_content=made_by&partner=' . TINVWL_UTM_SOURCE . '">TemplateInvaders</a><br />If you like WooCommerce Wishlist Plugin please leave us a <a href="https://wordpress.org/support/plugin/ti-woocommerce-wishlist/reviews/#new-post"><span><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i></span></a> rating.'; // WPCS: xss ok.
205
+ }
206
+
207
+ /**
208
+ * Create Upgrade button
209
+ *
210
+ * @param array $panel Panel Button.
211
+ *
212
+ * @return array
213
+ */
214
+ function status_panel( $panel ) {
215
+ array_unshift( $panel, sprintf( '<a class="tinvwl-btn red w-icon smaller-txt" href="%s"><i class="fa fa-star"></i><span class="tinvwl-txt">%s</span></a>', 'https://templateinvaders.com/product/ti-woocommerce-wishlist-wordpress-plugin/?utm_source=' . TINVWL_UTM_SOURCE . '&utm_campaign=' . TINVWL_UTM_CAMPAIGN . '&utm_medium=' . TINVWL_UTM_MEDIUM . '&utm_content=header_upgrade&partner=' . TINVWL_UTM_SOURCE, __( 'Upgrade to Pro', 'ti-woocommerce-wishlist' ) ) );
216
+
217
+ return $panel;
218
+ }
219
+
220
+ /**
221
+ * Templates overriding status check.
222
+ *
223
+ * @param boolean $outdated Out date status.
224
+ *
225
+ * @return string
226
+ */
227
+ function templates_status_check( $outdated = false ) {
228
+
229
+ $found_files = array();
230
+
231
+ $scanned_files = WC_Admin_Status::scan_template_files( TINVWL_PATH . '/templates/' );
232
+
233
+ foreach ( $scanned_files as $file ) {
234
+ if ( file_exists( get_stylesheet_directory() . '/' . $file ) ) {
235
+ $theme_file = get_stylesheet_directory() . '/' . $file;
236
+ } elseif ( file_exists( get_stylesheet_directory() . '/woocommerce/' . $file ) ) {
237
+ $theme_file = get_stylesheet_directory() . '/woocommerce/' . $file;
238
+ } elseif ( file_exists( get_template_directory() . '/' . $file ) ) {
239
+ $theme_file = get_template_directory() . '/' . $file;
240
+ } elseif ( file_exists( get_template_directory() . '/woocommerce/' . $file ) ) {
241
+ $theme_file = get_template_directory() . '/woocommerce/' . $file;
242
+ } else {
243
+ $theme_file = false;
244
+ }
245
+
246
+ if ( ! empty( $theme_file ) ) {
247
+ $core_version = WC_Admin_Status::get_file_version( TINVWL_PATH . '/templates/' . $file );
248
+ $theme_version = WC_Admin_Status::get_file_version( $theme_file );
249
+
250
+ if ( $core_version && ( empty( $theme_version ) || version_compare( $theme_version, $core_version, '<' ) ) ) {
251
+ if ( $outdated ) {
252
+ return 'outdated';
253
+ }
254
+ $found_files[] = sprintf( __( '<code>%1$s</code> version <strong style="color:red">%2$s</strong> is out of date. The core version is <strong style="color:red">%3$s</strong>', 'ti-woocommerce-wishlist' ), str_replace( WP_CONTENT_DIR . '/themes/', '', $theme_file ), $theme_version ? $theme_version : '-', $core_version );
255
+ } else {
256
+ $found_files[] = str_replace( WP_CONTENT_DIR . '/themes/', '', $theme_file );
257
+ }
258
+ }
259
+ }
260
+
261
+ return $found_files;
262
+ }
263
+
264
+ /**
265
+ * Templates overriding status for WooCommerce Status report page.
266
+ */
267
+ function system_report_templates() {
268
+
269
+ TInvWL_View::view( 'templates-status', array( 'found_files' => $this->templates_status_check() ) );
270
+ }
271
+
272
+ /**
273
+ * Outdated templates notice.
274
+ */
275
+ function admin_notice_outdated_templates() {
276
+ if ( 'outdated' === $this->templates_status_check( true ) ) {
277
+
278
+ $theme = wp_get_theme();
279
+
280
+ $html = sprintf( __( '<strong>Your theme (%1$s) contains outdated copies of some WooCommerce Wishlist Plugin template files.</strong><br> These files may need updating to ensure they are compatible with the current version of WooCommerce Wishlist Plugin.<br> You can see which files are affected from the <a href="%2$s">system status page</a>.<br> If in doubt, check with the author of the theme.', 'ti-woocommerce-wishlist' ), esc_html( $theme['Name'] ), esc_url( admin_url( 'admin.php?page=wc-status' ) ) );
281
+
282
+ WC_Admin_Notices::add_custom_notice( 'outdated_templates', $html );
283
+ } else {
284
+ WC_Admin_Notices::remove_notice( 'outdated_templates' );
285
+ }
286
+ }
287
+
288
+ /**
289
+ * Load javascript for validation templates
290
+ */
291
+ function enqueue_scripts_validate_template() {
292
+ $theme = wp_get_theme();
293
+ $theme = $theme->get_template();
294
+ if ( tinv_get_option( 'template_checker', 'theme' ) !== $theme ) {
295
+ tinv_update_option( 'template_checker', '', array() );
296
+ tinv_update_option( 'template_checker', 'theme', $theme );
297
+ tinv_update_option( 'template_checker', 'checked', false );
298
+ tinv_update_option( 'template_checker', 'time', 0 );
299
+ }
300
+ if ( tinv_get_option( 'template_checker', 'checked' ) && absint( tinv_get_option( 'template_checker', 'time' ) ) + HOUR_IN_SECONDS > time() ) {
301
+ return;
302
+ }
303
+ $types = array_keys( wc_get_product_types() );
304
+ foreach ( $types as $type => $type_name ) {
305
+ if ( ! tinv_get_option( 'template_checker', 'missing_hook_' . $type ) ) {
306
+ $data = filter_input_array( INPUT_GET, array(
307
+ 'wc-hide-notice' => FILTER_DEFAULT,
308
+ '_wc_notice_nonce' => FILTER_DEFAULT,
309
+ ) );
310
+ if ( 'missing_hook_' . $type === $data['wc-hide-notice'] && wp_verify_nonce( $data['_wc_notice_nonce'], 'woocommerce_hide_notices_nonce' ) ) {
311
+ tinv_update_option( 'template_checker', 'missing_hook_' . $type, true );
312
+ }
313
+ }
314
+ }
315
+ if ( ! tinv_get_option( 'template_checker', 'hide_product_listing' ) ) {
316
+ $data = filter_input_array( INPUT_GET, array(
317
+ 'wc-hide-notice' => FILTER_DEFAULT,
318
+ '_wc_notice_nonce' => FILTER_DEFAULT,
319
+ ) );
320
+ if ( 'missing_hook_listing' === $data['wc-hide-notice'] && wp_verify_nonce( $data['_wc_notice_nonce'], 'woocommerce_hide_notices_nonce' ) ) {
321
+ tinv_update_option( 'template_checker', 'hide_product_listing', true );
322
+ }
323
+ }
324
+
325
+ wp_enqueue_script( $this->_n . '-checker', TINVWL_URL . 'asset/js/admin.checker.min.js', array( 'jquery' ), $this->_v, 'all' );
326
+ }
327
+
328
+ /**
329
+ * Validation templates hook from request remote page
330
+ */
331
+ function validation_template() {
332
+ global $post, $product;
333
+
334
+ if ( tinv_get_option( 'template_checker', 'checked' ) ) {
335
+ return;
336
+ }
337
+ if ( absint( tinv_get_option( 'template_checker', 'time' ) ) + HOUR_IN_SECONDS > time() ) {
338
+ return;
339
+ }
340
+ tinv_update_option( 'template_checker', 'time', time() );
341
+ $tags = array(
342
+ 'woocommerce_single_product_summary' => 'tinvwl_single_product_summary',
343
+ 'woocommerce_before_add_to_cart_button' => 'tinvwl_before_add_to_cart_button',
344
+ 'woocommerce_after_add_to_cart_button' => 'tinvwl_after_add_to_cart_button',
345
+ );
346
+ $tch = TInvWL_CheckerHook::instance();
347
+ $tch->add_action( $tags );
348
+ $tch->add_action( array_keys( $tags ) );
349
+
350
+ $types = wc_get_product_types();
351
+
352
+ $check = true;
353
+ foreach ( $types as $type => $type_name ) {
354
+ if ( tinv_get_option( 'template_checker', 'missing_hook_' . $type ) ) {
355
+ continue;
356
+ }
357
+
358
+ if ( function_exists( 'wc_get_products' ) ) {
359
+ $products = wc_get_products( array(
360
+ 'status' => 'publish',
361
+ 'type' => $type,
362
+ 'limit' => 1,
363
+ ) );
364
+ } else {
365
+ $products = array_map( 'wc_get_product', get_posts( array(
366
+ 'post_type' => 'product',
367
+ 'post_status' => 'publish',
368
+ 'numberposts' => 1,
369
+ 'tax_query' => array(
370
+ array(
371
+ 'taxonomy' => 'product_type',
372
+ 'field' => 'slug',
373
+ 'terms' => $type,
374
+ ),
375
+ ),
376
+ ) ) );
377
+ }
378
+ if ( ! empty( $products ) ) {
379
+ $product = array_shift( $products );
380
+ $post = get_post( $product->get_id() ); // @codingStandardsIgnoreLine WordPress.Variables.GlobalVariables.OverrideProhibited
381
+ $result = $tch->run( array(
382
+ 'template' => array(
383
+ 'content-single-product.php',
384
+ 'single-product/add-to-cart/' . $type . '.php'
385
+ ),
386
+ 'template_args' => array(
387
+ 'available_variations' => array( 1, 2, 3, 4, 5 ),
388
+ 'attributes' => array(),
389
+ ),
390
+ 'url' => $product->get_permalink(),
391
+ ) );
392
+ if ( ! empty( $result ) ) {
393
+ $result = array_keys( $result );
394
+ foreach ( $result as $key => $tag ) {
395
+ if ( array_key_exists( $tag, $tags ) ) {
396
+ $tags[ $tag ];
397
+ if ( ! array_key_exists( $tag, $tags ) ) {
398
+ unset( $result[ $key ] );
399
+ }
400
+ } else {
401
+ unset( $result[ $key ] );
402
+ }
403
+ }
404
+ if ( ! empty( $result ) ) {
405
+ WC_Admin_Notices::add_custom_notice( 'missing_hook_' . $type, sprintf( _n( 'The "Add to Wishlist" button may work improperly in a product type "%1$s" because the hook "%2$s" is missing.<br />Please, ask your theme developers to check the theme templates or <a href="https://templateinvaders.com/help/" target="_blank">contact us</a> for assistance.', 'The "Add to Wishlist" button may work improperly in a product type "%1$s" because the hooks "%2$s" are missing.<br />Please, ask your theme developers to check the theme templates or <a href="https://templateinvaders.com/help/" target="_blank">contact us</a> for assistance.', count( $result ), 'ti-woocommerce-wishlist' ), $type_name, '<strong>' . join( '</strong>, <strong>', $result ) . '</strong>' ) );
406
+ $check = false;
407
+ } else {
408
+ WC_Admin_Notices::remove_notice( 'missing_hook_' . $type );
409
+ }
410
+ } else {
411
+ WC_Admin_Notices::remove_notice( 'missing_hook_' . $type );
412
+ }
413
+ }
414
+ } // End foreach().
415
+
416
+ tinv_update_option( 'template_checker', 'checked', $check );
417
+ wp_die();
418
+ }
419
+
420
+ /**
421
+ * Clear notice validation template when theme switched
422
+ */
423
+ function clear_notice_validation_template() {
424
+ WC_Admin_Notices::remove_notice( 'missing_hook_listing' );
425
+ $types = wc_get_product_types();
426
+ foreach ( $types as $type => $type_name ) {
427
+ WC_Admin_Notices::remove_notice( 'missing_hook_' . $type );
428
+ }
429
+ tinv_update_option( 'template_checker', '', array() );
430
+ }
431
+
432
+ /**
433
+ * Disable screen option on plugin pages
434
+ *
435
+ * @param boolean $show_screen Show screen.
436
+ * @param \WP_Screen $_this Screen option page.
437
+ *
438
+ * @return boolean
439
+ */
440
+ function screen_options_hide_screen( $show_screen, $_this ) {
441
+ if ( $this->_n === $_this->parent_base || $this->_n === $_this->parent_file ) {
442
+ return false;
443
+ }
444
+
445
+ return $show_screen;
446
+ }
447
+
448
+ /**
449
+ * Check if there is a hook in the cron
450
+ */
451
+ function scheduled_remove_wishlist() {
452
+ $timestamp = wp_next_scheduled( 'tinvwl_remove_without_author_wishlist' );
453
+ if ( ! $timestamp ) {
454
+ $time = strtotime( '00:00 today +1 HOURS' );
455
+ wp_schedule_event( $time, 'daily', 'tinvwl_remove_without_author_wishlist' );
456
+ }
457
+ }
458
+
459
+ /**
460
+ * Removing empty wishlist without a user older than 7 days
461
+ */
462
+ public function remove_empty_wishlists() {
463
+ $wl = new TInvWL_Wishlist();
464
+ $wishlists = $wl->get( array(
465
+ 'author' => 0,
466
+ 'type' => 'default',
467
+ 'sql' => 'SELECT * FROM {table} {where} AND `date` < DATE_SUB( CURDATE(), INTERVAL 7 DAY)',
468
+ ) );
469
+ foreach ( $wishlists as $wishlist ) {
470
+ $wlp = new TInvWL_Product( $wishlist );
471
+ $products = $wlp->get_wishlist( array(
472
+ 'count' => 1,
473
+ 'external' => true,
474
+ ) );
475
+ if ( empty( $products ) ) {
476
+ $wl->remove( $wishlist['ID'] );
477
+ }
478
+ }
479
+ }
480
+
481
+ /**
482
+ * Removing old wishlist without a user older than 34 days
483
+ */
484
+ public function remove_old_wishlists() {
485
+ $wl = new TInvWL_Wishlist();
486
+ $wishlists = $wl->get( array(
487
+ 'author' => 0,
488
+ 'type' => 'default',
489
+ 'sql' => 'SELECT * FROM {table} {where} AND `date` < DATE_SUB( CURDATE(), INTERVAL 34 DAY)',
490
+ ) );
491
+ foreach ( $wishlists as $wishlist ) {
492
+ $wl->remove( $wishlist['ID'] );
493
+ }
494
+ }
495
+
496
+ }
asset/js/public.js CHANGED
@@ -97,7 +97,7 @@
97
  }, 4000);
98
  },
99
  wishlists: function (wishlist) {
100
- $(this).attr('tinv-wl-list', wishlist);
101
  },
102
  msg: function (html) {
103
  if (!html) {
@@ -123,11 +123,11 @@
123
  },
124
  removed: function (status) {
125
  if (status) {
126
- $(this).removeClass('tinvwl-product-in-list').removeClass('tinvwl-product-make-remove').attr('tinv-wl-action', 'addto');
127
  }
128
  },
129
  make_remove: function (status) {
130
- $( this ).toggleClass( 'tinvwl-product-make-remove', status ).attr( 'tinv-wl-action', status ? 'remove' : 'addto' );
131
  },
132
  top_wishlist_content: function(data) {
133
  $('.widget_wishlist_content').html(data);
@@ -143,10 +143,10 @@
143
  form:{},
144
  tinv_wishlist_id: id || '',
145
  tinv_wishlist_name: name || '',
146
- product_type: $(this).attr('tinv-wl-producttype'),
147
- product_id: $(this).attr('tinv-wl-product') || 0,
148
- product_variation: $(this).attr('tinv-wl-productvariation') || 0,
149
- product_action: $(this).attr('tinv-wl-action') || 'addto'
150
  },
151
  a = this;
152
  $('form.cart[method=post], .woocommerce-variation-add-to-cart').find('input, select, textarea').each(function () {
@@ -215,7 +215,7 @@
215
  };
216
  var s = $.extend(true, {}, sd, so);
217
  return $(this).each(function () {
218
- if (!$(this).attr('tinv-wl-list')) {
219
  return false;
220
  }
221
  if (s.dialogbox) {
@@ -256,7 +256,7 @@
256
  }
257
  if ('function' !== typeof this.tinvwl_dialog.show_list) {
258
  this.tinvwl_dialog.show_list = function () {
259
- var WList = $.parseJSON($(this).attr('tinv-wl-list')) || [];
260
  if (WList.length) {
261
  WList = s.onPrepareList.call(WList) || WList;
262
  this.tinvwl_dialog.update_list(WList);
@@ -311,7 +311,7 @@
311
  dialogbutton: '.tinvwl_button_move'
312
  },
313
  onPrepareDataAction: function (data) {
314
- data.tinv_from_wishlist_id = $(this).attr('tinv-wl');
315
  data.tinv_to_wishlist_id = data.tinv_wishlist_id;
316
  if (!data.tinv_wishlist_name) {
317
  data.tinv_wishlist_name = '-';
@@ -389,7 +389,7 @@
389
  a.preventDefault();
390
  e.addClass('disabled-add-wishlist');
391
  }).on('show_variation', function (a, b, d) {
392
- var f = JSON.parse(e.attr('tinv-wl-list')),
393
  j = false,
394
  g = '1' == window.tinvwl_add_to_wishlist['simple_flow'];
395
  for(var i in f) {
@@ -397,7 +397,7 @@
397
  j = true;
398
  }
399
  }
400
- e.toggleClass('tinvwl-product-in-list', j).toggleClass('tinvwl-product-make-remove', (j && g)).attr('tinv-wl-action', ( (j && g) ? 'remove' : 'addto' ) );
401
  a.preventDefault();
402
  e.removeClass('disabled-add-wishlist');
403
  });
@@ -477,4 +477,10 @@
477
  $(this).toggle(!!$(this).children().not('.look_in').length || !!$(this).children('.look_in').children().length);
478
  });
479
  })
 
 
 
 
 
 
480
  })(jQuery);
97
  }, 4000);
98
  },
99
  wishlists: function (wishlist) {
100
+ $(this).attr('data-tinv-wl-list', wishlist);
101
  },
102
  msg: function (html) {
103
  if (!html) {
123
  },
124
  removed: function (status) {
125
  if (status) {
126
+ $(this).removeClass('tinvwl-product-in-list').removeClass('tinvwl-product-make-remove').attr('data-tinv-wl-action', 'addto');
127
  }
128
  },
129
  make_remove: function (status) {
130
+ $( this ).toggleClass( 'tinvwl-product-make-remove', status ).attr( 'data-tinv-wl-action', status ? 'remove' : 'addto' );
131
  },
132
  top_wishlist_content: function(data) {
133
  $('.widget_wishlist_content').html(data);
143
  form:{},
144
  tinv_wishlist_id: id || '',
145
  tinv_wishlist_name: name || '',
146
+ product_type: $(this).attr('data-tinv-wl-producttype'),
147
+ product_id: $(this).attr('data-tinv-wl-product') || 0,
148
+ product_variation: $(this).attr('data-tinv-wl-productvariation') || 0,
149
+ product_action: $(this).attr('data-tinv-wl-action') || 'addto'
150
  },
151
  a = this;
152
  $('form.cart[method=post], .woocommerce-variation-add-to-cart').find('input, select, textarea').each(function () {
215
  };
216
  var s = $.extend(true, {}, sd, so);
217
  return $(this).each(function () {
218
+ if (!$(this).attr('data-tinv-wl-list')) {
219
  return false;
220
  }
221
  if (s.dialogbox) {
256
  }
257
  if ('function' !== typeof this.tinvwl_dialog.show_list) {
258
  this.tinvwl_dialog.show_list = function () {
259
+ var WList = $.parseJSON($(this).attr('data-tinv-wl-list')) || [];
260
  if (WList.length) {
261
  WList = s.onPrepareList.call(WList) || WList;
262
  this.tinvwl_dialog.update_list(WList);
311
  dialogbutton: '.tinvwl_button_move'
312
  },
313
  onPrepareDataAction: function (data) {
314
+ data.tinv_from_wishlist_id = $(this).attr('data-tinv-wl');
315
  data.tinv_to_wishlist_id = data.tinv_wishlist_id;
316
  if (!data.tinv_wishlist_name) {
317
  data.tinv_wishlist_name = '-';
389
  a.preventDefault();
390
  e.addClass('disabled-add-wishlist');
391
  }).on('show_variation', function (a, b, d) {
392
+ var f = JSON.parse(e.attr('data-tinv-wl-list')),
393
  j = false,
394
  g = '1' == window.tinvwl_add_to_wishlist['simple_flow'];
395
  for(var i in f) {
397
  j = true;
398
  }
399
  }
400
+ e.toggleClass('tinvwl-product-in-list', j).toggleClass('tinvwl-product-make-remove', (j && g)).attr('data-tinv-wl-action', ( (j && g) ? 'remove' : 'addto' ) );
401
  a.preventDefault();
402
  e.removeClass('disabled-add-wishlist');
403
  });
477
  $(this).toggle(!!$(this).children().not('.look_in').length || !!$(this).children('.look_in').children().length);
478
  });
479
  })
480
+ $( document.body ).on( 'wc_fragments_refreshed', function() {
481
+ $('.wishlist_products_counter').toggleClass('wishlist-counter-with-products', '0' != $('.wishlist_products_counter_number').html() );
482
+ } );
483
+ $( window ).load( function ( e ) {
484
+ $( document.body ).trigger( 'wc_fragment_refresh' );
485
+ } );
486
  })(jQuery);
asset/js/public.min.js CHANGED
@@ -1 +1 @@
1
- (function($){$.fn.tinvwl_to_wishlist=function(so){var sd={api_url:window.location.href,text_create:window.tinvwl_add_to_wishlist['text_create'],text_already_in:window.tinvwl_add_to_wishlist['text_already_in'],class:{dialogbox:'.tinvwl_add_to_select_wishlist',select:'.tinvwl_wishlist',newtitle:'.tinvwl_new_input',dialogbutton:'.tinvwl_button_add'},redirectTimer:null,onPrepareList:function(){},onGetDialogBox:function(){},onPrepareDialogBox:function(){if(!$('body > .tinv-wishlist').length){$('body').append($('<div>').addClass('tinv-wishlist'));} $(this).appendTo('body > .tinv-wishlist');},onCreateWishList:function(wishlist){$(this).append($('<option>').html(wishlist.title).attr('value',wishlist.ID).toggleClass('tinv_in_wishlist',wishlist.in));},onSelectWishList:function(){},onDialogShow:function(){$(this).addClass('tinv-modal-open');},onDialogHide:function(){$(this).removeClass('tinv-modal-open');},onInited:function(){},onClick:function(){if($(this).is('.disabled-add-wishlist')){return false;} if(this.tinvwl_dialog){this.tinvwl_dialog.show_list.call(this);}else{s.onActionProduct.call(this);}},onPrepareDataAction:function(){},filterProductAlreadyIn:function(WList){var WList=WList||[],data={};$('form.cart[method=post], .woocommerce-variation-add-to-cart').find('input, select').each(function(){var name_elm=$(this).attr('name'),type_elm=$(this).attr('type'),value_elm=$(this).val();if('checkbox'===type_elm||'radio'===type_elm){if($(this).is(':checked')){data['form'+name_elm]=value_elm;}}else{data['form'+name_elm]=value_elm;}});data=data['formvariation_id'];return WList.filter(function(wishlist){if('object'===typeof wishlist.in&&'string'===typeof data){var number=parseInt(data);return 0<=wishlist.in.indexOf(number);} return wishlist.in;});},onMultiProductAlreadyIn:function(WList){var WList=WList||[];WList=s.onPrepareList.call(WList)||WList;WList=s.filterProductAlreadyIn.call(this,WList)||WList;$(this).parent().parent().find('.already-in').remove();var text='';switch(WList.length){case 0:break;default:var text=$('<ul>');$.each(WList,function(k,wishlist){text.append($('<li>').html($('<a>').html(wishlist.title).attr({href:wishlist.url})).attr('value',wishlist.ID));});break;} if(text.length){$(this).closest('.tinv-modal-inner').find('img').after($('<div>').addClass('already-in').html(s.text_already_in+' ').append(text));}},onAction:{redirect:function(url){if(s.redirectTimer){clearTimeout(s.redirectTimer);} s.redirectTimer=window.setTimeout(function(){window.location.href=url;},4000);},wishlists:function(wishlist){$(this).attr('tinv-wl-list',wishlist);},msg:function(html){if(!html){return false;} var $msg=$(html).eq(0);if(!$('body > .tinv-wishlist').length){$('body').append($('<div>').addClass('tinv-wishlist'));} $('body > .tinv-wishlist').append($msg);$msg.on('click','.tinv-close-modal, .tinvwl_button_close, .tinv-overlay',function(e){e.preventDefault();$msg.remove();if(s.redirectTimer){clearTimeout(s.redirectTimer);}});},status:function(status){if(status){$(this).addClass('tinvwl-product-in-list');}},removed:function(status){if(status){$(this).removeClass('tinvwl-product-in-list').removeClass('tinvwl-product-make-remove').attr('tinv-wl-action','addto');}},make_remove:function(status){$(this).toggleClass('tinvwl-product-make-remove',status).attr('tinv-wl-action',status?'remove':'addto');},top_wishlist_content:function(data){$('.widget_wishlist_content').html(data);},top_wishlist_counter:function(data){$('.wishlist_products_counter').toggleClass('wishlist-counter-with-products',0<data);$('.wishlist_products_counter_number').html(data);}}};sd.onActionProduct=function(id,name){var data={form:{},tinv_wishlist_id:id||'',tinv_wishlist_name:name||'',product_type:$(this).attr('tinv-wl-producttype'),product_id:$(this).attr('tinv-wl-product')||0,product_variation:$(this).attr('tinv-wl-productvariation')||0,product_action:$(this).attr('tinv-wl-action')||'addto'},a=this;$('form.cart[method=post], .woocommerce-variation-add-to-cart').find('input, select, textarea').each(function(){var name_elm=$(this).attr('name'),type_elm=$(this).attr('type'),value_elm=$(this).val(),count=10,ti_merge_value=function(o1,o2){if('object'===typeof o2){if('undefined'===typeof o1){o1={};} for(var i in o2){if(''===i){var j=-1;for(j in o1){j=j;} j=parseInt(j)+1;o1[j]=ti_merge_value(o1[i],o2[i]);}else{o1[i]=ti_merge_value(o1[i],o2[i]);}} return o1;}else{return o2;}};while(/^(.+)\[([^\[\]]*?)\]$/.test(name_elm)&&0<count){var n_name=name_elm.match(/^(.+)\[([^\[\]]*?)\]$/);if(3===n_name.length){var _value_elm={};_value_elm[n_name[2]]=value_elm;value_elm=_value_elm;} name_elm=n_name[1];count--;} if('checkbox'===type_elm||'radio'===type_elm){if($(this).is(':checked')){if(!value_elm.length&&'object'!==typeof value_elm){value_elm=true;} data.form[name_elm]=ti_merge_value(data.form[name_elm],value_elm);}}else{data.form[name_elm]=ti_merge_value(data.form[name_elm],value_elm);}});data=s.onPrepareDataAction.call(a,data)||data;$.post(s.api_url,data,function(body){s.onDialogHide.call(a.tinvwl_dialog);if('object'===typeof body){for(var k in body){if('function'===typeof s.onAction[k]){s.onAction[k].call(a,body[k]);}}}else{if('function'===typeof s.onAction['msg']){s.onAction['msg'].call(a,body);}}});};var s=$.extend(true,{},sd,so);return $(this).each(function(){if(!$(this).attr('tinv-wl-list')){return false;} if(s.dialogbox){if(s.dialogbox.length){this.tinvwl_dialog=s.dialogbox;}} if(!this.tinvwl_dialog){this.tinvwl_dialog=s.onGetDialogBox.call(this);} if(!this.tinvwl_dialog){var _tinvwl_dialog=$(this).nextAll(s.class.dialogbox).eq(0);if(_tinvwl_dialog.length){this.tinvwl_dialog=_tinvwl_dialog;}} if(this.tinvwl_dialog){s.onPrepareDialogBox.call(this.tinvwl_dialog);if('function'!==typeof this.tinvwl_dialog.update_list){this.tinvwl_dialog.update_list=function(WL){var $select=$(this).find(s.class.select).eq(0);$(this).find(s.class.newtitle).hide().val('');$select.html('');$.each(WL,function(k,v){s.onCreateWishList.call($select,v);});if(s.text_create){s.onCreateWishList.call($select,{ID:'',title:s.text_create,in:false});} s.onMultiProductAlreadyIn.call($select,WL);s.onSelectWishList.call($select,WL);$(this).find(s.class.newtitle).toggle(''===$select.val());}} if('function'!==typeof this.tinvwl_dialog.show_list){this.tinvwl_dialog.show_list=function(){var WList=$.parseJSON($(this).attr('tinv-wl-list'))||[];if(WList.length){WList=s.onPrepareList.call(WList)||WList;this.tinvwl_dialog.update_list(WList);s.onDialogShow.call(this.tinvwl_dialog);}else{s.onActionProduct.call(this);}}} var a=this;$(this.tinvwl_dialog).find(s.class.dialogbutton).off('click').on('click',function(){var b=$(a.tinvwl_dialog).find(s.class.select),c=$(a.tinvwl_dialog).find(s.class.newtitle),d;if(b.val()||c.val()){s.onActionProduct.call(a,b.val(),c.val());}else{d=c.is(':visible')?c:b;d.addClass('empty-name-wishlist');window.setTimeout(function(){d.removeClass('empty-name-wishlist');},1000);}});} $(this).off('click').on('click',s.onClick);s.onInited.call(this,s);});};$(document).ready(function(){$('body').on('click','.tinvwl_add_to_wishlist_button',function(e){if($(this).is('.disabled-add-wishlist')){e.preventDefault();window.alert(tinvwl_add_to_wishlist.i18n_make_a_selection_text);return;} if($(this).is('.inited-add-wishlist')){return;} $(this).tinvwl_to_wishlist({onInited:function(s){$(this).addClass('inited-add-wishlist');s.onClick.call(this);}});});$('.tinvwl_move_product_button').tinvwl_to_wishlist({class:{dialogbox:'.tinvwl_wishlist_move',select:'.tinvwl_wishlist',newtitle:'.tinvwl_new_input',dialogbutton:'.tinvwl_button_move'},onPrepareDataAction:function(data){data.tinv_from_wishlist_id=$(this).attr('tinv-wl');data.tinv_to_wishlist_id=data.tinv_wishlist_id;if(!data.tinv_wishlist_name){data.tinv_wishlist_name='-';} delete data.tinv_wishlist_id;return data;},onPrepareList:function(){return this.filter(function(a){return!a.hide;});},onAction:{redirect:function(url){return window.location.href=url;}}});$('.global-cb').on('click',function(){$(this).closest('table').eq(0).find('.product-cb input[type=checkbox], .wishlist-cb input[type=checkbox]').prop('checked',$(this).is(':checked'))});});})(jQuery);(function($){$.fn.tinvwl_break_submit=function(so){var sd={selector:'input, select, textarea',ifempty:true,invert:false,validate:function(){return $(this).val();},rule:function(){var form_elements=$(this).parents('form').eq(0).find(s.selector),trigger=s.invert;if(0===form_elements.length){return s.ifempty;} form_elements.each(function(){if((trigger&&!s.invert)||(!trigger&&s.invert)){return;} trigger=Boolean(s.validate.call($(this)));});return trigger;}};var s=$.extend(true,{},sd,so);return $(this).each(function(){$(this).on('click',function(event){if(!s.rule.call($(this))){event.preventDefault();}});});};$(document).ready(function(){$('.tinvwl-break-input').tinvwl_break_submit({selector:'.tinvwl-break-input-filed'});$('.tinvwl-break-checkbox').tinvwl_break_submit({selector:'table td input[type=checkbox]',validate:function(){return $(this).is(':checked');}});});})(jQuery);(function($){$('.variations_form').each(function(){var c=$(this),e=c.find('.tinvwl_add_to_wishlist_button');if(e.length){c.on('hide_variation',function(a){a.preventDefault();e.addClass('disabled-add-wishlist');}).on('show_variation',function(a,b,d){var f=JSON.parse(e.attr('tinv-wl-list')),j=false,g='1'==window.tinvwl_add_to_wishlist['simple_flow'];for(var i in f){if(f[i].hasOwnProperty('in')&&Array.isArray(f[i]['in'])&&-1<(f[i]['in']||[]).indexOf(b.variation_id)){j=true;}} e.toggleClass('tinvwl-product-in-list',j).toggleClass('tinvwl-product-make-remove',(j&&g)).attr('tinv-wl-action',((j&&g)?'remove':'addto'));a.preventDefault();e.removeClass('disabled-add-wishlist');});}});})(jQuery);(function($){$(document).ready(function(){$.fn.tinvwl_modal=function(so){var sd={showClass:'tinv-modal-open',modal:'.tinv-modal',onPrepare:function(){if(!$('body > .tinv-wishlist').length){$('body').append($('<div>').addClass('tinv-wishlist'));} $(this).appendTo('body > .tinv-wishlist');}},s=$.extend(true,{},sd,so);return $(this).each(function(){var a=$(this),b=a.next(s.modal);s.onPrepare.call(b);a.on('click',function(){b.addClass(s.showClass);});});};$('.tinv-modal-btn').tinvwl_modal({});$('#tinvwl_manage_actions, #tinvwl_product_actions').addClass('form-control').parent().wrapInner('<div class="input-group tinvwl-no-full">').find('button').wrap('<span class="input-group-btn">');$('.tinv-lists-nav').each(function(){if(!$.trim($(this).html()).length){$(this).remove();}});$('body').on('click','.social-buttons .social[title!=email]',function(e){var newWind=window.open($(this).attr('href'),$(this).attr('title'),"width=420,height=320,resizable=yes,scrollbars=yes,status=yes");if(newWind){newWind.focus();e.preventDefault();}});$('.tinvwl-select-all').on('click',function(e){e.preventDefault();$(this).parent().parent().find('ul li input[type="checkbox"]').attr('checked',true);});$('.tinvwl-select-none').on('click',function(e){e.preventDefault();$(this).parent().parent().find('ul li input[type="checkbox"]').attr('checked',false);});$('body').on('click','.tinv-wishlist .tinv-overlay, .tinv-wishlist .tinv-close-modal, .tinv-wishlist .tinvwl_button_close',function(e){e.preventDefault();$(this).parents('.tinv-modal:first').removeClass('tinv-modal-open');});$('body').on('click','.tinv-wishlist .tinvwl-btn-onclick',function(e){var url=$(this).data('url');if(url){e.preventDefault();window.location=$(this).data('url');}});var navigationButton=$('.tinv-wishlist .navigation-button');if(navigationButton.length){navigationButton.each(function(){var navigationButtons=$(this).find('> li');if(navigationButtons.length<5){navigationButtons.parent().addClass('tinvwl-btns-count-'+navigationButtons.length);}});} $('.tinv-login .showlogin').unbind("click").on('click',function(e){e.preventDefault();$(this).closest('.tinv-login').find('.login').toggle();});$('.tinv-wishlist table.tinvwl-table-manage-list tfoot td').each(function(){$(this).toggle(!!$(this).children().not('.look_in').length||!!$(this).children('.look_in').children().length);});})})(jQuery);
1
+ (function($){$.fn.tinvwl_to_wishlist=function(so){var sd={api_url:window.location.href,text_create:window.tinvwl_add_to_wishlist['text_create'],text_already_in:window.tinvwl_add_to_wishlist['text_already_in'],class:{dialogbox:'.tinvwl_add_to_select_wishlist',select:'.tinvwl_wishlist',newtitle:'.tinvwl_new_input',dialogbutton:'.tinvwl_button_add'},redirectTimer:null,onPrepareList:function(){},onGetDialogBox:function(){},onPrepareDialogBox:function(){if(!$('body > .tinv-wishlist').length){$('body').append($('<div>').addClass('tinv-wishlist'));} $(this).appendTo('body > .tinv-wishlist');},onCreateWishList:function(wishlist){$(this).append($('<option>').html(wishlist.title).attr('value',wishlist.ID).toggleClass('tinv_in_wishlist',wishlist.in));},onSelectWishList:function(){},onDialogShow:function(){$(this).addClass('tinv-modal-open');},onDialogHide:function(){$(this).removeClass('tinv-modal-open');},onInited:function(){},onClick:function(){if($(this).is('.disabled-add-wishlist')){return false;};if(this.tinvwl_dialog){this.tinvwl_dialog.show_list.call(this);}else{s.onActionProduct.call(this);}},onPrepareDataAction:function(){},filterProductAlreadyIn:function(WList){var WList=WList||[],data={};$('form.cart[method=post], .woocommerce-variation-add-to-cart').find('input, select').each(function(){var name_elm=$(this).attr('name'),type_elm=$(this).attr('type'),value_elm=$(this).val();if('checkbox'===type_elm||'radio'===type_elm){if($(this).is(':checked')){data['form'+name_elm]=value_elm;}}else{data['form'+name_elm]=value_elm;}});data=data['formvariation_id'];return WList.filter(function(wishlist){if('object'===typeof wishlist.in&&'string'===typeof data){var number=parseInt(data);return 0<=wishlist.in.indexOf(number);};return wishlist.in;});},onMultiProductAlreadyIn:function(WList){var WList=WList||[];WList=s.onPrepareList.call(WList)||WList;WList=s.filterProductAlreadyIn.call(this,WList)||WList;$(this).parent().parent().find('.already-in').remove();var text='';switch(WList.length){case 0:break;default:var text=$('<ul>');$.each(WList,function(k,wishlist){text.append($('<li>').html($('<a>').html(wishlist.title).attr({href:wishlist.url})).attr('value',wishlist.ID));});break;};if(text.length){$(this).closest('.tinv-modal-inner').find('i.ti-icon').after($('<div>').addClass('already-in').html(s.text_already_in+' ').append(text));}},onAction:{redirect:function(url){if(s.redirectTimer){clearTimeout(s.redirectTimer);} s.redirectTimer=window.setTimeout(function(){window.location.href=url;},4000);},updatepage:function(is){var selector='.tinvwl_added_to_wishlist .tinv-close-modal, .tinvwl_added_to_wishlist .tinvwl_button_close, .tinvwl_added_to_wishlist .tinv-overlay';if(is){$('body > .tinv-wishlist').on('click',selector,function(e){window.location.reload();});}else{$('body > .tinv-wishlist').off('click',selector);}},wishlists:function(wishlist){$(this).attr('data-tinv-wl-list',wishlist);},msg:function(html){if(!html){return false;};var $msg=$(html).eq(0);if(!$('body > .tinv-wishlist').length){$('body').append($('<div>').addClass('tinv-wishlist'));} $('body > .tinv-wishlist').append($msg);return $msg.on('click','.tinv-close-modal, .tinvwl_button_close, .tinv-overlay',function(e){e.preventDefault();$msg.remove();if(s.redirectTimer){clearTimeout(s.redirectTimer);}});},status:function(status){if(status){$(this).addClass('tinvwl-product-in-list');}},removed:function(status){if(status){$(this).removeClass('tinvwl-product-in-list').removeClass('tinvwl-product-make-remove').attr('data-tinv-wl-action','addto');}},make_remove:function(status){$(this).toggleClass('tinvwl-product-make-remove',status).attr('data-tinv-wl-action',status?'remove':'addto');},top_wishlist_content:function(data){$('.tinv_mini_wishlist_list').replaceWith(data);},top_wishlist_counter:function(data){$('.wishlist_products_counter').toggleClass('wishlist-counter-with-products',0<data);$('.wishlist_products_counter_number').html(data);}}};sd.onActionProduct=function(id,name){var data={form:{},tinv_wishlist_id:id||'',tinv_wishlist_name:name||'',product_type:$(this).attr('data-tinv-wl-producttype'),product_id:$(this).attr('data-tinv-wl-product')||0,product_variation:$(this).attr('data-tinv-wl-productvariation')||0,product_action:$(this).attr('data-tinv-wl-action')||'addto'},a=this;$('form.cart[method=post], .woocommerce-variation-add-to-cart').find('input, select, textarea').each(function(){var name_elm=$(this).attr('name'),type_elm=$(this).attr('type'),value_elm=$(this).val(),count=10,ti_merge_value=function(o1,o2){if('object'===typeof o2){if('undefined'===typeof o1){o1={};} for(var i in o2){if(''===i){var j=-1;for(j in o1){j=j;} j=parseInt(j)+1;o1[j]=ti_merge_value(o1[i],o2[i]);}else{o1[i]=ti_merge_value(o1[i],o2[i]);}} return o1;}else{return o2;}};while(/^(.+)\[([^\[\]]*?)\]$/.test(name_elm)&&0<count){var n_name=name_elm.match(/^(.+)\[([^\[\]]*?)\]$/);if(3===n_name.length){var _value_elm={};_value_elm[n_name[2]]=value_elm;value_elm=_value_elm;} name_elm=n_name[1];count--;} if('checkbox'===type_elm||'radio'===type_elm){if($(this).is(':checked')){if(!value_elm.length&&'object'!==typeof value_elm){value_elm=true;} data.form[name_elm]=ti_merge_value(data.form[name_elm],value_elm);}}else{data.form[name_elm]=ti_merge_value(data.form[name_elm],value_elm);}});data=s.onPrepareDataAction.call(a,data)||data;$.post(s.api_url,data,function(body){s.onDialogHide.call(a.tinvwl_dialog);if('object'===typeof body){for(var k in body){if('function'===typeof s.onAction[k]){s.onAction[k].call(a,body[k]);}}}else{if('function'===typeof s.onAction['msg']){s.onAction['msg'].call(a,body);}}});};var s=$.extend(true,{},sd,so);return $(this).each(function(){if(!$(this).attr('data-tinv-wl-list')){return false;};if(s.dialogbox){if(s.dialogbox.length){this.tinvwl_dialog=s.dialogbox;}};if(!this.tinvwl_dialog){this.tinvwl_dialog=s.onGetDialogBox.call(this);};if(!this.tinvwl_dialog){var _tinvwl_dialog=$(this).nextAll(s.class.dialogbox).eq(0);if(_tinvwl_dialog.length){this.tinvwl_dialog=_tinvwl_dialog;}};if(this.tinvwl_dialog){s.onPrepareDialogBox.call(this.tinvwl_dialog);if('function'!==typeof this.tinvwl_dialog.update_list){this.tinvwl_dialog.update_list=function(WL){var $select=$(this).find(s.class.select).eq(0);$(this).find(s.class.newtitle).hide().val('');$select.html('');$.each(WL,function(k,v){s.onCreateWishList.call($select,v);});if(s.text_create){s.onCreateWishList.call($select,{ID:'',title:s.text_create,in:false});};s.onMultiProductAlreadyIn.call($select,WL);s.onSelectWishList.call($select,WL);$(this).find(s.class.newtitle).toggle(''===$select.val());};};if('function'!==typeof this.tinvwl_dialog.show_list){this.tinvwl_dialog.show_list=function(){var WList=$.parseJSON($(this).attr('data-tinv-wl-list'))||[];if(WList.length){WList=s.onPrepareList.call(WList)||WList;this.tinvwl_dialog.update_list(WList);s.onDialogShow.call(this.tinvwl_dialog);}else{s.onActionProduct.call(this);}};};var a=this;$(this.tinvwl_dialog).find(s.class.select).on('change',function(){$(a.tinvwl_dialog).find(s.class.newtitle).toggle(''===$(this).val());});$(this.tinvwl_dialog).find(s.class.dialogbutton).off('click').on('click',function(){var b=$(a.tinvwl_dialog).find(s.class.select),c=$(a.tinvwl_dialog).find(s.class.newtitle),d;if(b.val()||c.val()){s.onActionProduct.call(a,b.val(),c.val());}else{d=c.is(':visible')?c:b;d.addClass('empty-name-wishlist');window.setTimeout(function(){d.removeClass('empty-name-wishlist');},1000);}});};$(this).off('click').on('click',s.onClick);s.onInited.call(this,s);});};$(document).ready(function(){$('body').on('click','.tinvwl_add_to_wishlist_button',function(e){if($(this).is('.disabled-add-wishlist')){e.preventDefault();window.alert(tinvwl_add_to_wishlist.i18n_make_a_selection_text);return;} if($(this).is('.inited-add-wishlist')){return;} $(this).tinvwl_to_wishlist({onInited:function(s){$(this).addClass('inited-add-wishlist');s.onClick.call(this);}});});$('body').on('click','.tinvwl_cart_to_wishlist_button',function(e){$(this).tinvwl_to_wishlist({onInited:function(s){$(this).addClass('inited-add-wishlist');s.onClick.call(this);},onPrepareDataAction:function(data){data.tinv_product_key=$(this).attr('data-tinv-wl-cart-key');data.cart_to_wishlist_id=data.tinv_wishlist_id;if(!data.tinv_wishlist_name){data.tinv_wishlist_name='-';};delete data.tinv_wishlist_id;return data;},onAction:{updatepage:function(is){var selector='.tinv-close-modal, .tinvwl_button_close, .tinv-overlay';if(is){$(selector).on('click',function(e){window.location.reload();});}},}});});$('body').on('click','.tinvwl_all_cart_to_wishlist_button',function(e){$(this).tinvwl_to_wishlist({onInited:function(s){$(this).addClass('inited-add-wishlist');s.onClick.call(this);},onPrepareDataAction:function(data){data.all_cart_to_wishlist_id=data.tinv_wishlist_id;if(!data.tinv_wishlist_name){data.tinv_wishlist_name='-';};delete data.tinv_wishlist_id;return data;},onAction:{updatepage:function(is){var selector='.tinv-close-modal, .tinvwl_button_close, .tinv-overlay';if(is){$(selector).on('click',function(e){window.location.reload();});}},}});});$('.tinvwl_move_product_button').tinvwl_to_wishlist({class:{dialogbox:'.tinvwl_wishlist_move',select:'.tinvwl_wishlist',newtitle:'.tinvwl_new_input',dialogbutton:'.tinvwl_button_move'},onPrepareDataAction:function(data){data.tinv_from_wishlist_id=$(this).attr('data-tinv-wl');data.tinv_to_wishlist_id=data.tinv_wishlist_id;if(!data.tinv_wishlist_name){data.tinv_wishlist_name='-';};delete data.tinv_wishlist_id;return data;},onPrepareList:function(){return this.filter(function(a){return!a.hide;});},onAction:{redirect:function(url){return window.location.href=url;}}});$('.global-cb').on('click',function(){$(this).closest('table').eq(0).find('.product-cb input[type=checkbox], .wishlist-cb input[type=checkbox]').prop('checked',$(this).is(':checked'));});});})(jQuery);(function($){$.fn.tinvwl_validate_createwishlist=function(so){var sd={class:{field:'#tinv_wishlist_create_field',button:'#tinv_wishlist_create_button',error:'#tinv_wishlist_create_error'},show_error:function(c,msg){$(this).find(c.error).html(msg).show();$(this).find(c.field).parent().addClass('tinvwl-has-error');},hide_error:function(c){$(this).find(c.error).hide();$(this).find(c.field).parent().removeClass('tinvwl-has-error');},successfully_action:function(){var form=$(this),formdata={action:'tinwl_create_wishlist'};form.find('input, select, button').each(function(){switch($(this).attr('type')){case'radio':if($(this).is(':checked')){formdata[$(this).attr('name')]=$(this).val().trim();} break;default:formdata[$(this).attr('name')]=$(this).val().trim();}});form.find('input[type=text]').val('');if(form.hasClass('tinv-modal-inner')){form.parents('.tinv_wishlist_create_modal').removeClass('tinv-modal-open');} $.post(window.location.href,formdata,function(data){$('.tinv-wishlist.woocommerce').eq(0).append(data);},'html');},validation:[{condition:function(){return 0<$(this).val().trim().length;},message:tinvwl_create_wishlist.text_empty_name}]};var s=$.extend(true,{},sd,so);return $(this).each(function(){var form=$(this),field=form.find(s.class.field),button=form.find(s.class.button);if(0===field.length||0===button.length){return false;} button.on('click',function(e){for(var i in s.validation){var rule=s.validation[i];if(rule.condition.call(field)){s.hide_error.call(form,s.class);}else{s.show_error.call(form,s.class,rule.message);e.preventDefault();return false;}} s.successfully_action.call(form);});field.on('focus',function(){s.hide_error.call(form,s.class);});});};$(document).ready(function(){$('.tinv_wishlist_create').tinvwl_validate_createwishlist();});})(jQuery);(function($){$.fn.tinvwl_break_submit=function(so){var sd={selector:'input, select, textarea',ifempty:true,invert:false,validate:function(){return $(this).val();},rule:function(){var form_elements=$(this).parents('form').eq(0).find(s.selector),trigger=s.invert;if(0===form_elements.length){return s.ifempty;} form_elements.each(function(){if((trigger&&!s.invert)||(!trigger&&s.invert)){return;} trigger=Boolean(s.validate.call($(this)));});return trigger;}};var s=$.extend(true,{},sd,so);return $(this).each(function(){$(this).on('click',function(event){if(!s.rule.call($(this))){event.preventDefault();}});});};$(document).ready(function(){$('.tinvwl-break-input').tinvwl_break_submit({selector:'.tinvwl-break-input-field'});$('.tinvwl-break-checkbox').tinvwl_break_submit({selector:'table td input[type=checkbox]',validate:function(){return $(this).is(':checked');}});});})(jQuery);(function($){$('.variations_form').each(function(){var c=$(this),e=c.find('.tinvwl_add_to_wishlist_button');if(e.length){c.on('hide_variation',function(a){a.preventDefault();e.addClass('disabled-add-wishlist');}).on('show_variation',function(a,b,d){var f=JSON.parse(e.attr('data-tinv-wl-list')),j=false,g='1'==window.tinvwl_add_to_wishlist['simple_flow'];for(var i in f){if(f[i].hasOwnProperty('in')&&Array.isArray(f[i]['in'])&&-1<(f[i]['in']||[]).indexOf(b.variation_id)){j=true;}} e.toggleClass('tinvwl-product-in-list',j).toggleClass('tinvwl-product-make-remove',(j&&g)).attr('data-tinv-wl-action',((j&&g)?'remove':'addto'));a.preventDefault();e.removeClass('disabled-add-wishlist');});}});})(jQuery);(function($){$(document).ready(function(){$.fn.tinvwl_psform=function(so){var sd={container:'.ti-ps-form'},s=$.extend(true,{},sd,so);return $(this).each(function(){$(this).on('click',function(){var dform=$(this).closest(s.container),form=$('<form>').attr({'accept-charset':dform.data('accept-charset'),'action':dform.data('action'),'autocomplete':dform.data('autocomplete'),'enctype':dform.data('enctype'),'method':dform.data('method'),'name':dform.data('name'),'novalidate':dform.data('novalidate'),'target':dform.data('target'),}).html(dform.html());$('body').append(form);form.submit();});});};$.fn.tinvwl_modal=function(so){var sd={showClass:'tinv-modal-open',modal:'.tinv-modal',onPrepare:function(){if(!$('body > .tinv-wishlist').length){$('body').append($('<div>').addClass('tinv-wishlist'));} $(this).appendTo('body > .tinv-wishlist');}},s=$.extend(true,{},sd,so);return $(this).each(function(){var a=$(this),b=a.next(s.modal);s.onPrepare.call(b);a.on('click',function(){b.addClass(s.showClass);});});};$('button[name="tinvwl-wishlist-remove-btn"]').tinvwl_psform();$('.tinv-modal-btn').tinvwl_modal();$('#tinvwl_manage_actions, #tinvwl_product_actions').addClass('form-control').parent().wrapInner('<div class="input-group tinvwl-no-full">').find('button').wrap('<span class="input-group-btn">');$('.tinv-lists-nav').each(function(){if(!$.trim($(this).html()).length){$(this).remove();}});$('body').on('click','.social-buttons .social[title!=email]',function(e){var newWind=window.open($(this).attr('href'),$(this).attr('title'),"width=420,height=320,resizable=yes,scrollbars=yes,status=yes");if(newWind){newWind.focus();e.preventDefault();}});$('.tinvwl-select-all').on('click',function(e){e.preventDefault();$(this).parent().parent().find('ul li input[type="checkbox"]').attr('checked',true);$(this).parent().parent().find('#tinv_wishlist_follow_error').hide();});$('.tinvwl-select-none').on('click',function(e){e.preventDefault();$(this).parent().parent().find('ul li input[type="checkbox"]').attr('checked',false);});$('body').on('click','.tinv-wishlist .tinv-overlay, .tinv-wishlist .tinv-close-modal, .tinv-wishlist .tinvwl_button_close',function(e){e.preventDefault();$(this).parents('.tinv-modal:first').removeClass('tinv-modal-open');});$('body').on('click','.tinv-wishlist .tinvwl-btn-onclick',function(e){var url=$(this).data('url');if(url){e.preventDefault();window.location=$(this).data('url');}});var navigationButton=$('.tinv-wishlist .navigation-button');if(navigationButton.length){navigationButton.each(function(){var navigationButtons=$(this).find('> li');if(navigationButtons.length<5){navigationButtons.parent().addClass('tinvwl-btns-count-'+navigationButtons.length);}});} $('.tinv-login .showlogin').unbind("click").on('click',function(e){e.preventDefault();$(this).closest('.tinv-login').find('.login').toggle();});$('.tinvwl_subscribe_form').on('submit',function(event){if(!$(this).find('input[name="tinvwl_subscribes[]"]:checked').length){event.preventDefault();$(this).find('#tinv_wishlist_follow_error').show();}});$('.tinvwl_subscribe_form input[type=checkbox]').on('change',function(){$(this).parents('form').eq(0).find('#tinv_wishlist_follow_error').hide();});$('.tinv-wishlist table.tinvwl-table-manage-list tfoot td').each(function(){$(this).toggle(!!$(this).children().not('.look_in').length||!!$(this).children('.look_in').children().length);});$('body').on('click','a.wishlist_title_more',function(e){e.preventDefault();$(this).closest('.wishlist_list_titles').children('li').show();$(this).closest('li.wishlist_title_more').hide();});});})(jQuery);(function($){$(window).load(function(){function tinvwl_pcounter_show(){var a=$(this).find('.wishlist_products_counter_wishlist');a.show();var ao=a.offset(),al=ao.left,at=ao.top,aw=a.outerWidth(),ah=a.outerHeight(),la=parseFloat(a.css('left')),ta=parseFloat(a.css('top')),aol=al-la,aot=at-ta,_la=la,_ta=ta,ww=$(window).width(),dh=$(document).height(),os=50,r=ww-aol-aw-os,l=os-aol,b=dh-aot-ah-os;if(ww<=aw){_la=-1*aol;}else if(0>ww-(aw+os*2)){_la=(ww-aw)/2-aol;}else if(0<l){_la=l;}else if(0>r){_la=r;} if(dh<ah){a.height(dh-a.outerHeight()+a.height());ah=a.outerHeight();} if(dh<=ah){_ta=-1*aot;}else if(0>dh-(ah+os*2)){_ta=(dh-ah)/2-aot;}else if(0>b){_ta=b;} a.stop(true,true).show().css({left:_la,top:_ta,opacity:0}).animate({opacity:1},600);} function tinvwl_pcounter_hide(){var a=$(this).find('.wishlist_products_counter_wishlist');$('.tinvwl-first-touch').removeClass('tinvwl-first-touch');$('.tinvwl-first-click').removeClass('tinvwl-first-click');if(a.is(':visible')){a.stop(true,true).animate({opacity:0},600,function(){$(this).hide();});}} if(('ontouchstart'in window)||(navigator.maxTouchPoints>0)||(navigator.msMaxTouchPoints>0)){$('body').on('click','.wishlist_products_counter > a',function(e){if(!$(this).hasClass('tinvwl-first-click')){e.preventDefault();$(this).addClass('tinvwl-first-click');}});var tinwl_swipe_trigger;$('body').on('touchstart','.wishlist_products_counter',function(e){tinwl_swipe_trigger=false;}).on('touchmove','.wishlist_products_counter',function(e){tinwl_swipe_trigger=true;}).on('touchend','.wishlist_products_counter',function(e){if($(this).hasClass('tinvwl-first-touch')){if(!tinwl_swipe_trigger){tinvwl_pcounter_hide.call($('.wishlist_products_counter'),e);}}else{e.preventDefault();tinvwl_pcounter_show.call(this,e);$(this).addClass('tinvwl-first-touch');}});$('body').on('touchend',':not(.wishlist_products_counter)',function(e){if($(e.target).closest('.wishlist_products_counter').length===0){tinvwl_pcounter_hide.call($('.wishlist_products_counter'),e);}});}else{$('.wishlist_products_counter').hoverIntent({interval:0,timeout:100,over:tinvwl_pcounter_show,out:tinvwl_pcounter_hide});}});$(document.body).on('wc_fragments_refreshed',function(){$('.wishlist_products_counter').toggleClass('wishlist-counter-with-products','0'!=$('.wishlist_products_counter_number').html());});$(window).load(function(e){$(document.body).trigger('wc_fragment_refresh');});})(jQuery);
freemius/includes/class-freemius.php CHANGED
@@ -1,13647 +1,13651 @@
1
- <?php
2
- /**
3
- * @package Freemius
4
- * @copyright Copyright (c) 2015, Freemius, Inc.
5
- * @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3
6
- * @since 1.0.3
7
- */
8
- if ( ! defined( 'ABSPATH' ) ) {
9
- exit;
10
- }
11
-
12
- // "final class"
13
- class Freemius extends Freemius_Abstract {
14
- /**
15
- * SDK Version
16
- *
17
- * @var string
18
- */
19
- public $version = WP_FS__SDK_VERSION;
20
-
21
- #region Plugin Info
22
-
23
- /**
24
- * @since 1.0.1
25
- *
26
- * @var string
27
- */
28
- private $_slug;
29
-
30
- /**
31
- * @since 1.0.0
32
- *
33
- * @var string
34
- */
35
- private $_plugin_basename;
36
- /**
37
- * @since 1.0.0
38
- *
39
- * @var string
40
- */
41
- private $_free_plugin_basename;
42
- /**
43
- * @since 1.0.0
44
- *
45
- * @var string
46
- */
47
- private $_plugin_dir_path;
48
- /**
49
- * @since 1.0.0
50
- *
51
- * @var string
52
- */
53
- private $_plugin_dir_name;
54
- /**
55
- * @since 1.0.0
56
- *
57
- * @var string
58
- */
59
- private $_plugin_main_file_path;
60
- /**
61
- * @var string[]
62
- */
63
- private $_plugin_data;
64
- /**
65
- * @since 1.0.9
66
- *
67
- * @var string
68
- */
69
- private $_plugin_name;
70
- /**
71
- * @since 1.2.2
72
- *
73
- * @var string
74
- */
75
- private $_module_type;
76
-
77
- #endregion Plugin Info
78
-
79
- /**
80
- * @since 1.0.9
81
- *
82
- * @var bool If false, don't turn Freemius on.
83
- */
84
- private $_is_on;
85
-
86
- /**
87
- * @since 1.1.3
88
- *
89
- * @var bool If false, don't turn Freemius on.
90
- */
91
- private $_is_anonymous;
92
-
93
- /**
94
- * @since 1.0.9
95
- * @var bool If false, issues with connectivity to Freemius API.
96
- */
97
- private $_has_api_connection;
98
-
99
- /**
100
- * @since 1.0.9
101
- * @var bool Hints the SDK if plugin can support anonymous mode (if skip connect is visible).
102
- */
103
- private $_enable_anonymous;
104
-
105
- /**
106
- * @since 1.1.7.5
107
- * @var bool Hints the SDK if plugin should run in anonymous mode (only adds feedback form).
108
- */
109
- private $_anonymous_mode;
110
-
111
- /**
112
- * @since 1.1.9
113
- * @var bool Hints the SDK if plugin have any free plans.
114
- */
115
- private $_is_premium_only;
116
-
117
- /**
118
- * @since 1.2.1.6
119
- * @var bool Hints the SDK if plugin have premium code version at all.
120
- */
121
- private $_has_premium_version;
122
-
123
- /**
124
- * @since 1.2.1.6
125
- * @var bool Hints the SDK if plugin should ignore pending mode by simulating a skip.
126
- */
127
- private $_ignore_pending_mode;
128
-
129
- /**
130
- * @since 1.0.8
131
- * @var bool Hints the SDK if the plugin has any paid plans.
132
- */
133
- private $_has_paid_plans;
134
-
135
- /**
136
- * @since 1.2.1.5
137
- * @var int Hints the SDK if the plugin offers a trial period. If negative, no trial, if zero - has a trial but
138
- * without a specified period, if positive - the number of trial days.
139
- */
140
- private $_trial_days = - 1;
141
-
142
- /**
143
- * @since 1.2.1.5
144
- * @var bool Hints the SDK if the trial requires a payment method or not.
145
- */
146
- private $_is_trial_require_payment = false;
147
-
148
- /**
149
- * @since 1.0.7
150
- * @var bool Hints the SDK if the plugin is WordPress.org compliant.
151
- */
152
- private $_is_org_compliant;
153
-
154
- /**
155
- * @since 1.0.7
156
- * @var bool Hints the SDK if the plugin is has add-ons.
157
- */
158
- private $_has_addons;
159
-
160
- /**
161
- * @since 1.1.6
162
- * @var string[]bool.
163
- */
164
- private $_permissions;
165
-
166
- /**
167
- * @var FS_Key_Value_Storage
168
- */
169
- private $_storage;
170
-
171
- /**
172
- * @since 1.2.2.7
173
- * @var FS_Cache_Manager
174
- */
175
- private $_cache;
176
-
177
- /**
178
- * @since 1.0.0
179
- *
180
- * @var FS_Logger
181
- */
182
- private $_logger;
183
- /**
184
- * @since 1.0.4
185
- *
186
- * @var FS_Plugin
187
- */
188
- private $_plugin = false;
189
- /**
190
- * @since 1.0.4
191
- *
192
- * @var FS_Plugin|false
193
- */
194
- private $_parent_plugin = false;
195
- /**
196
- * @since 1.1.1
197
- *
198
- * @var Freemius
199
- */
200
- private $_parent = false;
201
- /**
202
- * @since 1.0.1
203
- *
204
- * @var FS_User
205
- */
206
- private $_user = false;
207
- /**
208
- * @since 1.0.1
209
- *
210
- * @var FS_Site
211
- */
212
- private $_site = false;
213
- /**
214
- * @since 1.0.1
215
- *
216
- * @var FS_Plugin_License
217
- */
218
- private $_license;
219
- /**
220
- * @since 1.0.2
221
- *
222
- * @var FS_Plugin_Plan[]
223
- */
224
- private $_plans = false;
225
- /**
226
- * @var FS_Plugin_License[]
227
- * @since 1.0.5
228
- */
229
- private $_licenses = false;
230
-
231
- /**
232
- * @since 1.0.1
233
- *
234
- * @var FS_Admin_Menu_Manager
235
- */
236
- private $_menu;
237
-
238
- /**
239
- * @var FS_Admin_Notice_Manager
240
- */
241
- private $_admin_notices;
242
-
243
- /**
244
- * @since 1.1.6
245
- *
246
- * @var FS_Admin_Notice_Manager
247
- */
248
- private static $_global_admin_notices;
249
-
250
- /**
251
- * @var FS_Logger
252
- * @since 1.0.0
253
- */
254
- private static $_static_logger;
255
-
256
- /**
257
- * @var FS_Option_Manager
258
- * @since 1.0.2
259
- */
260
- private static $_accounts;
261
-
262
- /**
263
- * @since 1.2.2
264
- *
265
- * @var number
266
- */
267
- private $_module_id;
268
-
269
- /**
270
- * @var Freemius[]
271
- */
272
- private static $_instances = array();
273
-
274
- #region Uninstall Reasons IDs
275
-
276
- const REASON_NO_LONGER_NEEDED = 1;
277
- const REASON_FOUND_A_BETTER_PLUGIN = 2;
278
- const REASON_NEEDED_FOR_A_SHORT_PERIOD = 3;
279
- const REASON_BROKE_MY_SITE = 4;
280
- const REASON_SUDDENLY_STOPPED_WORKING = 5;
281
- const REASON_CANT_PAY_ANYMORE = 6;
282
- const REASON_OTHER = 7;
283
- const REASON_DIDNT_WORK = 8;
284
- const REASON_DONT_LIKE_TO_SHARE_MY_INFORMATION = 9;
285
- const REASON_COULDNT_MAKE_IT_WORK = 10;
286
- const REASON_GREAT_BUT_NEED_SPECIFIC_FEATURE = 11;
287
- const REASON_NOT_WORKING = 12;
288
- const REASON_NOT_WHAT_I_WAS_LOOKING_FOR = 13;
289
- const REASON_DIDNT_WORK_AS_EXPECTED = 14;
290
- const REASON_TEMPORARY_DEACTIVATION = 15;
291
-
292
- #endregion
293
-
294
- /* Ctor
295
- ------------------------------------------------------------------------------------------------------------------*/
296
-
297
- /**
298
- * Main singleton instance.
299
- *
300
- * @author Vova Feldman (@svovaf)
301
- * @since 1.0.0
302
- *
303
- * @param number $module_id
304
- * @param string|bool $slug
305
- * @param bool $is_init Since 1.2.1 Is initiation sequence.
306
- */
307
- private function __construct( $module_id, $slug = false, $is_init = false ) {
308
- if ( $is_init && is_numeric( $module_id ) && is_string( $slug ) ) {
309
- $this->store_id_slug_type_path_map( $module_id, $slug );
310
- }
311
-
312
- $this->_module_id = $module_id;
313
- $this->_slug = $this->get_slug();
314
- $this->_module_type = $this->get_module_type();
315
-
316
- $this->_storage = FS_Key_Value_Storage::instance( $this->_module_type . '_data', $this->_slug );
317
- $this->_cache = FS_Cache_Manager::get_manager( WP_FS___OPTION_PREFIX . "cache_{$module_id}" );
318
-
319
- $this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_' . $this->get_unique_affix(), WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
320
-
321
- $this->_plugin_main_file_path = $this->_find_caller_plugin_file( $is_init );
322
- $this->_plugin_dir_path = plugin_dir_path( $this->_plugin_main_file_path );
323
- $this->_plugin_basename = $this->get_plugin_basename();
324
- $this->_free_plugin_basename = str_replace( '-premium/', '/', $this->_plugin_basename );
325
-
326
- $base_name_split = explode( '/', $this->_plugin_basename );
327
- $this->_plugin_dir_name = $base_name_split[0];
328
-
329
- if ( $this->_logger->is_on() ) {
330
- $this->_logger->info( 'plugin_main_file_path = ' . $this->_plugin_main_file_path );
331
- $this->_logger->info( 'plugin_dir_path = ' . $this->_plugin_dir_path );
332
- $this->_logger->info( 'plugin_basename = ' . $this->_plugin_basename );
333
- $this->_logger->info( 'free_plugin_basename = ' . $this->_free_plugin_basename );
334
- $this->_logger->info( 'plugin_dir_name = ' . $this->_plugin_dir_name );
335
- }
336
-
337
- // Remember link between file to slug.
338
- $this->store_file_slug_map();
339
-
340
- // Store plugin's initial install timestamp.
341
- if ( ! isset( $this->_storage->install_timestamp ) ) {
342
- $this->_storage->install_timestamp = WP_FS__SCRIPT_START_TIME;
343
- }
344
-
345
- if ( ! is_object( $this->_plugin ) ) {
346
- $this->_plugin = FS_Plugin_Manager::instance( $this->_module_id )->get();
347
- }
348
-
349
- $this->_admin_notices = FS_Admin_Notice_Manager::instance(
350
- $this->_slug . ( $this->is_theme() ? ':theme' : '' ),
351
- /**
352
- * Ensure that the admin notice will always have a title by using the stored plugin title if available and
353
- * retrieving the title via the "get_plugin_name" method if there is no stored plugin title available.
354
- *
355
- * @author Leo Fajardo (@leorw)
356
- * @since 1.2.2
357
- */
358
- ( is_object( $this->_plugin ) ? $this->_plugin->title : $this->get_plugin_name() ),
359
- $this->get_unique_affix()
360
- );
361
-
362
- if ( 'true' === fs_request_get( 'fs_clear_api_cache' ) ||
363
- 'true' === fs_request_is_action( 'restart_freemius' )
364
- ) {
365
- FS_Api::clear_cache();
366
- $this->_cache->clear();
367
- }
368
-
369
- $this->_register_hooks();
370
-
371
- $this->_load_account();
372
-
373
- $this->_version_updates_handler();
374
- }
375
-
376
- /**
377
- * Checks whether this module has a settings menu.
378
- *
379
- * @author Leo Fajardo (@leorw)
380
- * @since 1.2.2
381
- *
382
- * @return bool
383
- */
384
- function has_settings_menu() {
385
- return $this->_menu->has_menu();
386
- }
387
-
388
- /**
389
- * Check if the context module is free wp.org theme.
390
- *
391
- * This method is helpful because:
392
- * 1. wp.org themes are limited to a single submenu item,
393
- * and sub-submenu items are most likely not allowed (never verified).
394
- * 2. wp.org themes are not allowed to redirect the user
395
- * after the theme activation, therefore, the agreed UX
396
- * is showing the opt-in as a modal dialog box after
397
- * activation (approved by @otto42, @emiluzelac, @greenshady, @grapplerulrich).
398
- *
399
- * @author Vova Feldman (@svovaf)
400
- * @since 1.2.2.7
401
- *
402
- * @return bool
403
- */
404
- function is_free_wp_org_theme() {
405
- return (
406
- $this->is_theme() &&
407
- $this->is_org_repo_compliant() &&
408
- ! $this->is_premium()
409
- );
410
- }
411
-
412
- /**
413
- * Checks whether this a submenu item is visible.
414
- *
415
- * @author Vova Feldman (@svovaf)
416
- * @since 1.2.2.6
417
- * @since 1.2.2.7 Even if the menu item was specified to be hidden, when it is the context page, then show the submenu item so the user will have the right context page.
418
- *
419
- * @param string $slug
420
- *
421
- * @return bool
422
- */
423
- function is_submenu_item_visible( $slug ) {
424
- if ( $this->is_admin_page( $slug ) ) {
425
- /**
426
- * It is the current context page, so show the submenu item
427
- * so the user will have the right context page, even if it
428
- * was set to hidden.
429
- */
430
- return true;
431
- }
432
-
433
- if ( ! $this->has_settings_menu() ) {
434
- // No menu settings at all.
435
- return false;
436
- }
437
-
438
- if ( $this->is_free_wp_org_theme() ) {
439
- /**
440
- * wp.org themes are limited to a single submenu item, and
441
- * sub-submenu items are most likely not allowed (never verified).
442
- */
443
- return false;
444
- }
445
-
446
- return $this->_menu->is_submenu_item_visible( $slug );
447
- }
448
-
449
- /**
450
- * Check if a Freemius page should be accessible via the UI.
451
- *
452
- * @author Vova Feldman (@svovaf)
453
- * @since 1.2.2.7
454
- *
455
- * @param string $slug
456
- *
457
- * @return bool
458
- */
459
- function is_page_visible( $slug ) {
460
- if ( $this->is_admin_page( $slug ) ) {
461
- return true;
462
- }
463
-
464
- return $this->_menu->is_submenu_item_visible( $slug, true, true );
465
- }
466
-
467
- /**
468
- * @author Vova Feldman (@svovaf)
469
- * @since 1.0.9
470
- */
471
- private function _version_updates_handler() {
472
- if ( ! isset( $this->_storage->sdk_version ) || $this->_storage->sdk_version != $this->version ) {
473
- // Freemius version upgrade mode.
474
- $this->_storage->sdk_last_version = $this->_storage->sdk_version;
475
- $this->_storage->sdk_version = $this->version;
476
-
477
- if ( empty( $this->_storage->sdk_last_version ) ||
478
- version_compare( $this->_storage->sdk_last_version, $this->version, '<' )
479
- ) {
480
- $this->_storage->sdk_upgrade_mode = true;
481
- $this->_storage->sdk_downgrade_mode = false;
482
- } else {
483
- $this->_storage->sdk_downgrade_mode = true;
484
- $this->_storage->sdk_upgrade_mode = false;
485
-
486
- }
487
-
488
- $this->do_action( 'sdk_version_update', $this->_storage->sdk_last_version, $this->version );
489
- }
490
-
491
- $plugin_version = $this->get_plugin_version();
492
- if ( ! isset( $this->_storage->plugin_version ) || $this->_storage->plugin_version != $plugin_version ) {
493
- // Plugin version upgrade mode.
494
- $this->_storage->plugin_last_version = $this->_storage->plugin_version;
495
- $this->_storage->plugin_version = $plugin_version;
496
-
497
- if ( empty( $this->_storage->plugin_last_version ) ||
498
- version_compare( $this->_storage->plugin_last_version, $plugin_version, '<' )
499
- ) {
500
- $this->_storage->plugin_upgrade_mode = true;
501
- $this->_storage->plugin_downgrade_mode = false;
502
- } else {
503
- $this->_storage->plugin_downgrade_mode = true;
504
- $this->_storage->plugin_upgrade_mode = false;
505
- }
506
-
507
- if ( ! empty( $this->_storage->plugin_last_version ) ) {
508
- // Different version of the plugin was installed before, therefore it's an update.
509
- $this->_storage->is_plugin_new_install = false;
510
- }
511
-
512
- $this->do_action( 'plugin_version_update', $this->_storage->plugin_last_version, $plugin_version );
513
- }
514
- }
515
-
516
- /**
517
- * @author Vova Feldman (@svovaf)
518
- * @since 1.1.5
519
- *
520
- * @param string $sdk_prev_version
521
- * @param string $sdk_version
522
- */
523
- function _data_migration( $sdk_prev_version, $sdk_version ) {
524
- /**
525
- * @since 1.1.7.3 Fixed unwanted connectivity test cleanup.
526
- */
527
- if ( empty( $sdk_prev_version ) ) {
528
- return;
529
- }
530
-
531
- if ( version_compare( $sdk_prev_version, '1.1.5', '<' ) &&
532
- version_compare( $sdk_version, '1.1.5', '>=' )
533
- ) {
534
- // On version 1.1.5 merged connectivity and is_on data.
535
- if ( isset( $this->_storage->connectivity_test ) ) {
536
- if ( ! isset( $this->_storage->is_on ) ) {
537
- unset( $this->_storage->connectivity_test );
538
- } else {
539
- $connectivity_data = $this->_storage->connectivity_test;
540
- $connectivity_data['is_active'] = $this->_storage->is_on['is_active'];
541
- $connectivity_data['timestamp'] = $this->_storage->is_on['timestamp'];
542
-
543
- // Override.
544
- $this->_storage->connectivity_test = $connectivity_data;
545
-
546
- // Remove previous structure.
547
- unset( $this->_storage->is_on );
548
- }
549
-
550
- }
551
- }
552
- }
553
-
554
- /**
555
- * @author Vova Feldman (@svovaf)
556
- * @since 1.2.2.7
557
- *
558
- * @param string $plugin_prev_version
559
- * @param string $plugin_version
560
- */
561
- function _after_version_update( $plugin_prev_version, $plugin_version ) {
562
- if ( $this->is_theme() ) {
563
- // Expire the cache of the previous tabs since the theme may
564
- // have setting updates.
565
- $this->_cache->expire( 'tabs' );
566
- $this->_cache->expire( 'tabs_stylesheets' );
567
- }
568
- }
569
-
570
- /**
571
- * This action is connected to the 'plugins_loaded' hook and helps to determine
572
- * if this is a new plugin installation or a plugin update.
573
- *
574
- * There are 3 different use-cases:
575
- * 1) New plugin installation right with Freemius:
576
- * 1.1 _activate_plugin_event_hook() will be executed first
577
- * 1.2 Since $this->_storage->is_plugin_new_install is not set,
578
- * and $this->_storage->plugin_last_version is not set,
579
- * $this->_storage->is_plugin_new_install will be set to TRUE.
580
- * 1.3 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install will
581
- * be already set to TRUE.
582
- *
583
- * 2) Plugin update, didn't have Freemius before, and now have the SDK:
584
- * 2.1 _activate_plugin_event_hook() will not be executed, because
585
- * the activation hook do NOT fires on updates since WP 3.1.
586
- * 2.2 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install will
587
- * be empty, therefore, it will be set to FALSE.
588
- *
589
- * 3) Plugin update, had Freemius in prev version as well:
590
- * 3.1 _version_updates_handler() will be executed 1st, since FS was installed
591
- * before, $this->_storage->plugin_last_version will NOT be empty,
592
- * therefore, $this->_storage->is_plugin_new_install will be set to FALSE.
593
- * 3.2 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install is
594
- * already set, therefore, it will not be modified.
595
- *
596
- * Use-case #3 is backward compatible, #3.1 will be executed since 1.0.9.
597
- *
598
- * NOTE:
599
- * The only fallback of this mechanism is if an admin updates a plugin based on use-case #2,
600
- * and then, the next immediate PageView is the plugin's main settings page, it will not
601
- * show the opt-in right away. The reason it will happen is because Freemius execution
602
- * will be turned off till the plugin is fully loaded at least once
603
- * (till $this->_storage->was_plugin_loaded is TRUE).
604
- *
605
- * @author Vova Feldman (@svovaf)
606
- * @since 1.1.9
607
- *
608
- */
609
- function _plugins_loaded() {
610
- // Update flag that plugin was loaded with Freemius at least once.
611
- $this->_storage->was_plugin_loaded = true;
612
-
613
- /**
614
- * Bug fix - only set to false when it's a plugin, due to the
615
- * execution sequence of the theme hooks and our methods, if
616
- * this will be set for themes, Freemius will always assume
617
- * it's a theme update.
618
- *
619
- * @author Vova Feldman (@svovaf)
620
- * @since 1.2.2.2
621
- */
622
- if ( $this->is_plugin() &&
623
- ! isset( $this->_storage->is_plugin_new_install )
624
- ) {
625
- $this->_storage->is_plugin_new_install = false;
626
- }
627
- }
628
-
629
- /**
630
- * @author Vova Feldman (@svovaf)
631
- * @since 1.0.9
632
- */
633
- private function _register_hooks() {
634
- $this->_logger->entrance();
635
-
636
- if ( is_admin() ) {
637
- if ( $this->is_plugin() ) {
638
- $plugin_dir = dirname( $this->_plugin_dir_path ) . '/';
639
-
640
- /**
641
- * @since 1.2.2
642
- *
643
- * Hook to both free and premium version activations to support
644
- * auto deactivation on the other version activation.
645
- */
646
- register_activation_hook(
647
- $plugin_dir . $this->_free_plugin_basename,
648
- array( &$this, '_activate_plugin_event_hook' )
649
- );
650
-
651
- register_activation_hook(
652
- $plugin_dir . $this->premium_plugin_basename(),
653
- array( &$this, '_activate_plugin_event_hook' )
654
- );
655
- } else {
656
- add_action( 'after_switch_theme', array( &$this, '_activate_theme_event_hook' ), 10, 2 );
657
-
658
- /**
659
- * Include the required hooks to capture the theme settings' page tabs
660
- * and cache them.
661
- *
662
- * @author Vova Feldman (@svovaf)
663
- * @since 1.2.2.7
664
- */
665
- if ( ! $this->_cache->has_valid( 'tabs' ) ) {
666
- add_action( 'admin_footer', array( &$this, '_tabs_capture' ) );
667
- // Add license activation AJAX callback.
668
- $this->add_ajax_action( 'store_tabs', array( &$this, '_store_tabs_ajax_action' ) );
669
-
670
- add_action( 'admin_enqueue_scripts', array( &$this, '_store_tabs_styles' ), 9999999 );
671
- }
672
-
673
- add_action(
674
- 'admin_footer',
675
- array( &$this, '_add_freemius_tabs' ),
676
- /**
677
- * The tabs JS code must be executed after the tabs capture logic (_tabs_capture()).
678
- * That's why the priority is 11 while the tabs capture logic is added
679
- * with priority 10.
680
- *
681
- * @author Vova Feldman (@svovaf)
682
- */
683
- 11
684
- );
685
-
686
- add_action( 'admin_footer', array( &$this, '_style_premium_theme' ) );
687
- }
688
-
689
- /**
690
- * Part of the mechanism to identify new plugin install vs. plugin update.
691
- *
692
- * @author Vova Feldman (@svovaf)
693
- * @since 1.1.9
694
- */
695
- if ( empty( $this->_storage->was_plugin_loaded ) ) {
696
- if ( $this->is_plugin() &&
697
- $this->is_activation_mode( false ) &&
698
- 0 == did_action( 'plugins_loaded' )
699
- ) {
700
- add_action( 'plugins_loaded', array( &$this, '_plugins_loaded' ) );
701
- } else {
702
- // If was activated before, then it was already loaded before.
703
- $this->_plugins_loaded();
704
- }
705
- }
706
-
707
- if ( ! self::is_ajax() ) {
708
- if ( ! $this->is_addon() ) {
709
- add_action( 'init', array( &$this, '_add_default_submenu_items' ), WP_FS__LOWEST_PRIORITY );
710
- }
711
- }
712
- }
713
-
714
- if ( $this->is_plugin() ) {
715
- register_deactivation_hook( $this->_plugin_main_file_path, array( &$this, '_deactivate_plugin_hook' ) );
716
- }
717
-
718
- if ( $this->is_theme() && self::is_customizer() ) {
719
- // Register customizer upsell.
720
- add_action( 'customize_register', array( &$this, '_customizer_register' ) );
721
- }
722
-
723
- add_action( 'init', array( &$this, '_redirect_on_clicked_menu_link' ), WP_FS__LOWEST_PRIORITY );
724
-
725
- add_action( 'admin_init', array( &$this, '_add_tracking_links' ) );
726
- add_action( 'admin_init', array( &$this, '_add_license_activation' ) );
727
- $this->add_ajax_action( 'update_billing', array( &$this, '_update_billing_ajax_action' ) );
728
- $this->add_ajax_action( 'start_trial', array( &$this, '_start_trial_ajax_action' ) );
729
-
730
- $this->add_ajax_action( 'install_premium_version', array(
731
- &$this,
732
- '_install_premium_version_ajax_action'
733
- ) );
734
-
735
- $this->add_action( 'after_plans_sync', array( &$this, '_check_for_trial_plans' ) );
736
-
737
- $this->add_action( 'sdk_version_update', array( &$this, '_data_migration' ), WP_FS__DEFAULT_PRIORITY, 2 );
738
- $this->add_action( 'plugin_version_update', array( &$this, '_after_version_update' ), WP_FS__DEFAULT_PRIORITY, 2 );
739
- $this->add_filter( 'after_code_type_change', array( &$this, '_after_code_type_change' ) );
740
-
741
- add_action( 'admin_init', array( &$this, '_add_trial_notice' ) );
742
- add_action( 'admin_init', array( &$this, '_enqueue_common_css' ) );
743
-
744
- /**
745
- * Handle request to reset anonymous mode for `get_reconnect_url()`.
746
- *
747
- * @author Vova Feldman (@svovaf)
748
- * @since 1.2.1.5
749
- */
750
- if ( fs_request_is_action( 'reset_anonymous_mode' ) &&
751
- $this->get_unique_affix() === fs_request_get( 'fs_unique_affix' )
752
- ) {
753
- add_action( 'admin_init', array( &$this, 'connect_again' ) );
754
- }
755
- }
756
-
757
- /**
758
- * Keeping the uninstall hook registered for free or premium plugin version may result to a fatal error that
759
- * could happen when a user tries to uninstall either version while one of them is still active. Uninstalling a
760
- * plugin will trigger inclusion of the free or premium version and if one of them is active during the
761
- * uninstallation, a fatal error may occur in case the plugin's class or functions are already defined.
762
- *
763
- * @author Leo Fajardo (leorw)
764
- *
765
- * @since 1.2.0
766
- */
767
- private function unregister_uninstall_hook() {
768
- $uninstallable_plugins = (array) get_option( 'uninstall_plugins' );
769
- unset( $uninstallable_plugins[ $this->_free_plugin_basename ] );
770
- unset( $uninstallable_plugins[ $this->premium_plugin_basename() ] );
771
-
772
- update_option( 'uninstall_plugins', $uninstallable_plugins );
773
- }
774
-
775
- /**
776
- * @since 1.2.0 Invalidate module's main file cache, otherwise, FS_Plugin_Updater will not fetch updates.
777
- */
778
- private function clear_module_main_file_cache() {
779
- if ( ! isset( $this->_storage->plugin_main_file ) ||
780
- empty( $this->_storage->plugin_main_file->path )
781
- ) {
782
- return;
783
- }
784
-
785
- $plugin_main_file = clone $this->_storage->plugin_main_file;
786
-
787
- // Store cached path (2nd layer cache).
788
- $plugin_main_file->prev_path = $plugin_main_file->path;
789
-
790
- // Clear cached path.
791
- unset( $plugin_main_file->path );
792
-
793
- $this->_storage->plugin_main_file = $plugin_main_file;
794
-
795
- /**
796
- * Clear global cached path.
797
- *
798
- * @author Leo Fajardo (@leorw)
799
- * @since 1.2.2
800
- */
801
- $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map' );
802
- unset( $id_slug_type_path_map[ $this->_module_id ]['path'] );
803
- self::$_accounts->set_option( 'id_slug_type_path_map', $id_slug_type_path_map, true );
804
- }
805
-
806
- /**
807
- * @author Vova Feldman (@svovaf)
808
- * @since 1.0.9
809
- */
810
- private function _register_account_hooks() {
811
- if ( ! is_admin() ) {
812
- return;
813
- }
814
-
815
- /**
816
- * Always show the deactivation feedback form since we added
817
- * automatic free version deactivation upon premium code activation.
818
- *
819
- * @since 1.2.1.6
820
- */
821
- $this->add_ajax_action(
822
- 'submit_uninstall_reason',
823
- array( &$this, '_submit_uninstall_reason_action' )
824
- );
825
-
826
- if ( ( $this->is_plugin() && self::is_plugins_page() ) ||
827
- ( $this->is_theme() && self::is_themes_page() )
828
- ) {
829
- add_action( 'admin_footer', array( &$this, '_add_deactivation_feedback_dialog_box' ) );
830
- }
831
- }
832
-
833
- /**
834
- * Leverage backtrace to find caller plugin file path.
835
- *
836
- * @author Vova Feldman (@svovaf)
837
- * @since 1.0.6
838
- *
839
- * @param bool $is_init Is initiation sequence.
840
- *
841
- * @return string
842
- */
843
- private function _find_caller_plugin_file( $is_init = false ) {
844
- // Try to load the cached value of the file path.
845
- if ( isset( $this->_storage->plugin_main_file ) ) {
846
- $plugin_main_file = $this->_storage->plugin_main_file;
847
- if ( isset( $plugin_main_file->path ) && file_exists( $plugin_main_file->path ) ) {
848
- return $plugin_main_file->path;
849
- }
850
- }
851
-
852
- /**
853
- * @since 1.2.1
854
- *
855
- * `clear_module_main_file_cache()` is clearing the plugin's cached path on
856
- * deactivation. Therefore, if any plugin/theme was initiating `Freemius`
857
- * with that plugin's slug, it was overriding the empty plugin path with a wrong path.
858
- *
859
- * So, we've added a special mechanism with a 2nd layer of cache that uses `prev_path`
860
- * when the class instantiator isn't the module.
861
- */
862
- if ( ! $is_init ) {
863
- // Fetch prev path cache.
864
- if ( isset( $this->_storage->plugin_main_file ) &&
865
- isset( $this->_storage->plugin_main_file->prev_path )
866
- ) {
867
- if ( file_exists( $this->_storage->plugin_main_file->prev_path ) ) {
868
- return $this->_storage->plugin_main_file->prev_path;
869
- }
870
- }
871
-
872
- wp_die(
873
- $this->get_text( 'failed-finding-main-path' ) .
874
- " Module: {$this->_slug}; SDK: " . WP_FS__SDK_VERSION . ";",
875
- $this->get_text( 'error' ),
876
- array( 'back_link' => true )
877
- );
878
- }
879
-
880
- /**
881
- * @since 1.2.1
882
- *
883
- * Only the original instantiator that calls dynamic_init can modify the module's path.
884
- */
885
- // Find caller module.
886
- $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
887
- $this->_storage->plugin_main_file = (object) array(
888
- 'path' => $id_slug_type_path_map[ $this->_module_id ]['path'],
889
- );
890
-
891
- return $id_slug_type_path_map[ $this->_module_id ]['path'];
892
- }
893
-
894
- /**
895
- * @author Leo Fajardo (@leorw)
896
- *
897
- * @param number $module_id
898
- * @param string $slug
899
- *
900
- * @since 1.2.2
901
- */
902
- private function store_id_slug_type_path_map( $module_id, $slug ) {
903
- $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
904
-
905
- $store_option = false;
906
-
907
- if ( ! isset( $id_slug_type_path_map[ $module_id ] ) ) {
908
- $id_slug_type_path_map[ $module_id ] = array(
909
- 'slug' => $slug
910
- );
911
-
912
- $store_option = true;
913
- }
914
-
915
- if ( ! isset( $id_slug_type_path_map[ $module_id ]['path'] ) ) {
916
- $caller_main_file_and_type = $this->get_caller_main_file_and_type();
917
-
918
- $id_slug_type_path_map[ $module_id ]['type'] = $caller_main_file_and_type->module_type;
919
- $id_slug_type_path_map[ $module_id ]['path'] = $caller_main_file_and_type->path;
920
-
921
- $store_option = true;
922
- }
923
-
924
- if ( $store_option ) {
925
- self::$_accounts->set_option( 'id_slug_type_path_map', $id_slug_type_path_map, true );
926
- }
927
- }
928
-
929
- /**
930
- * Identifies the caller type: plugin or theme.
931
- *
932
- * @author Leo Fajardo (@leorw)
933
- * @since 1.2.2
934
- *
935
- * @author Vova Feldman (@svovaf)
936
- * @since 1.2.2.3 Find the earliest module in the call stack that calls to the SDK. This fix is for cases when
937
- * add-ons are relying on loading the SDK from the parent module, and also allows themes including the
938
- * SDK an internal file instead of directly from functions.php.
939
- * @since 1.2.1.7 Knows how to handle cases when an add-on includes the parent module logic.
940
- */
941
- private function get_caller_main_file_and_type() {
942
- self::require_plugin_essentials();
943
-
944
- $all_plugins = get_plugins();
945
- $all_plugins_paths = array();
946
-
947
- // Get active plugin's main files real full names (might be symlinks).
948
- foreach ( $all_plugins as $relative_path => &$data ) {
949
- if ( false === strpos( fs_normalize_path( $relative_path ), '/' ) ) {
950
- /**
951
- * Ignore plugins that don't have a folder (e.g. Hello Dolly) since they
952
- * can't really include the SDK.
953
- *
954
- * @author Vova Feldman
955
- * @since 1.2.1.7
956
- */
957
- continue;
958
- }
959
-
960
- $all_plugins_paths[] = fs_normalize_path( realpath( WP_PLUGIN_DIR . '/' . $relative_path ) );
961
- }
962
-
963
- $caller_file_candidate = false;
964
- $caller_map = array();
965
- $module_type = WP_FS__MODULE_TYPE_PLUGIN;
966
- $themes_dir = fs_normalize_path( get_theme_root() );
967
-
968
- for ( $i = 1, $bt = debug_backtrace(), $len = count( $bt ); $i < $len; $i ++ ) {
969
- if ( empty( $bt[ $i ]['file'] ) ) {
970
- continue;
971
- }
972
-
973
- if ( $i > 1 && ! empty( $bt[ $i - 1 ]['file'] ) && $bt[ $i ]['file'] === $bt[ $i - 1 ]['file'] ) {
974
- // If file same as the prev file in the stack, skip it.
975
- continue;
976
- }
977
-
978
- if ( ! empty( $bt[ $i ]['function'] ) && in_array( $bt[ $i ]['function'], array(
979
- 'do_action',
980
- 'apply_filter',
981
- // The string split is stupid, but otherwise, theme check
982
- // throws info notices.
983
- 'requir' . 'e_once',
984
- 'requir' . 'e',
985
- 'includ' . 'e_once',
986
- 'includ' . 'e'
987
- ) )
988
- ) {
989
- // Ignore call stack hooks and files inclusion.
990
- continue;
991
- }
992
-
993
- $caller_file_path = fs_normalize_path( $bt[ $i ]['file'] );
994
-
995
- if ( 'functions.php' === basename( $caller_file_path ) ) {
996
- /**
997
- * 1. Assumes that theme's starting execution file is functions.php.
998
- * 2. This complex logic fixes symlink issues (e.g. with Vargant).
999
- *
1000
- * @author Vova Feldman (@svovaf)
1001
- * @since 1.2.2.5
1002
- */
1003
-
1004
- if ( $caller_file_path == fs_normalize_path( realpath( trailingslashit( $themes_dir ) . basename( dirname( $caller_file_path ) ) . '/' . basename( $caller_file_path ) ) ) ) {
1005
- $module_type = WP_FS__MODULE_TYPE_THEME;
1006
- $caller_file_candidate = $caller_file_path;
1007
- continue;
1008
- }
1009
- }
1010
-
1011
- $caller_file_hash = md5( $caller_file_path );
1012
-
1013
- if ( ! isset( $caller_map[ $caller_file_hash ] ) ) {
1014
- foreach ( $all_plugins_paths as $plugin_path ) {
1015
- if ( false !== strpos( $caller_file_path, fs_normalize_path( dirname( $plugin_path ) . '/' ) ) ) {
1016
- $caller_map[ $caller_file_hash ] = fs_normalize_path( $plugin_path );
1017
- break;
1018
- }
1019
- }
1020
- }
1021
-
1022
- if ( isset( $caller_map[ $caller_file_hash ] ) ) {
1023
- $module_type = WP_FS__MODULE_TYPE_PLUGIN;
1024
- $caller_file_candidate = $caller_map[ $caller_file_hash ];
1025
- }
1026
- }
1027
-
1028
- return (object) array(
1029
- 'module_type' => $module_type,
1030
- 'path' => $caller_file_candidate
1031
- );
1032
- }
1033
-
1034
- #----------------------------------------------------------------------------------
1035
- #region Deactivation Feedback Form
1036
- #----------------------------------------------------------------------------------
1037
-
1038
- /**
1039
- * Displays a confirmation and feedback dialog box when the user clicks on the "Deactivate" link on the plugins
1040
- * page.
1041
- *
1042
- * @author Vova Feldman (@svovaf)
1043
- * @author Leo Fajardo (@leorw)
1044
- * @since 1.1.2
1045
- */
1046
- function _add_deactivation_feedback_dialog_box() {
1047
- /* Check the type of user:
1048
- * 1. Long-term (long-term)
1049
- * 2. Non-registered and non-anonymous short-term (non-registered-and-non-anonymous-short-term).
1050
- * 3. Short-term (short-term)
1051
- */
1052
- $is_long_term_user = true;
1053
-
1054
- // Check if the site is at least 2 days old.
1055
- $time_installed = $this->_storage->install_timestamp;
1056
-
1057
- // Difference in seconds.
1058
- $date_diff = time() - $time_installed;
1059
-
1060
- // Convert seconds to days.
1061
- $date_diff_days = floor( $date_diff / ( 60 * 60 * 24 ) );
1062
-
1063
- if ( $date_diff_days < 2 ) {
1064
- $is_long_term_user = false;
1065
- }
1066
-
1067
- $is_long_term_user = $this->apply_filters( 'is_long_term_user', $is_long_term_user );
1068
-
1069
- if ( $is_long_term_user ) {
1070
- $user_type = 'long-term';
1071
- } else {
1072
- if ( ! $this->is_registered() && ! $this->is_anonymous() ) {
1073
- $user_type = 'non-registered-and-non-anonymous-short-term';
1074
- } else {
1075
- $user_type = 'short-term';
1076
- }
1077
- }
1078
-
1079
- $uninstall_reasons = $this->_get_uninstall_reasons( $user_type );
1080
-
1081
- // Load the HTML template for the deactivation feedback dialog box.
1082
- $vars = array(
1083
- 'reasons' => $uninstall_reasons,
1084
- 'id' => $this->_module_id
1085
- );
1086
-
1087
- /**
1088
- * @todo Deactivation form core functions should be loaded only once! Otherwise, when there are multiple Freemius powered plugins the same code is loaded multiple times. The only thing that should be loaded differently is the various deactivation reasons object based on the state of the plugin.
1089
- */
1090
- fs_require_template( 'forms/deactivation/form.php', $vars );
1091
- }
1092
-
1093
- /**
1094
- * @author Leo Fajardo (leorw)
1095
- * @since 1.1.2
1096
- *
1097
- * @param string $user_type
1098
- *
1099
- * @return array The uninstall reasons for the specified user type.
1100
- */
1101
- function _get_uninstall_reasons( $user_type = 'long-term' ) {
1102
- $module_type = $this->_module_type;
1103
-
1104
- $internal_message_template_var = array(
1105
- 'id' => $this->_module_id
1106
- );
1107
-
1108
- if ( $this->is_registered() && false !== $this->get_plan() && $this->get_plan()->has_technical_support() ) {
1109
- $contact_support_template = fs_get_template( 'forms/deactivation/contact.php', $internal_message_template_var );
1110
- } else {
1111
- $contact_support_template = '';
1112
- }
1113
-
1114
- $reason_found_better_plugin = array(
1115
- 'id' => self::REASON_FOUND_A_BETTER_PLUGIN,
1116
- 'text' => sprintf( $this->get_text( 'reason-found-a-better-plugin' ), $module_type ),
1117
- 'input_type' => 'textfield',
1118
- 'input_placeholder' => sprintf( $this->get_text( 'placeholder-plugin-name' ), $module_type ),
1119
- );
1120
-
1121
- $reason_temporary_deactivation = array(
1122
- 'id' => self::REASON_TEMPORARY_DEACTIVATION,
1123
- 'text' => sprintf(
1124
- $this->get_text( 'reason-temporary-x' ),
1125
- strtolower( $this->is_plugin() ?
1126
- $this->get_text( 'deactivation' ) :
1127
- $this->get_text( 'theme-switch' )
1128
- )
1129
- ),
1130
- 'input_type' => '',
1131
- 'input_placeholder' => ''
1132
- );
1133
-
1134
- $reason_other = array(
1135
- 'id' => self::REASON_OTHER,
1136
- 'text' => $this->get_text( 'reason-other' ),
1137
- 'input_type' => 'textfield',
1138
- 'input_placeholder' => ''
1139
- );
1140
-
1141
- $long_term_user_reasons = array(
1142
- array(
1143
- 'id' => self::REASON_NO_LONGER_NEEDED,
1144
- 'text' => sprintf( $this->get_text( 'reason-no-longer-needed' ), $module_type ),
1145
- 'input_type' => '',
1146
- 'input_placeholder' => ''
1147
- ),
1148
- $reason_found_better_plugin,
1149
- array(
1150
- 'id' => self::REASON_NEEDED_FOR_A_SHORT_PERIOD,
1151
- 'text' => sprintf( $this->get_text( 'reason-needed-for-a-short-period' ), $module_type ),
1152
- 'input_type' => '',
1153
- 'input_placeholder' => ''
1154
- ),
1155
- array(
1156
- 'id' => self::REASON_BROKE_MY_SITE,
1157
- 'text' => sprintf( $this->get_text( 'reason-broke-my-site' ), $module_type ),
1158
- 'input_type' => '',
1159
- 'input_placeholder' => '',
1160
- 'internal_message' => $contact_support_template
1161
- ),
1162
- array(
1163
- 'id' => self::REASON_SUDDENLY_STOPPED_WORKING,
1164
- 'text' => sprintf( $this->get_text( 'reason-suddenly-stopped-working' ), $module_type ),
1165
- 'input_type' => '',
1166
- 'input_placeholder' => '',
1167
- 'internal_message' => $contact_support_template
1168
- )
1169
- );
1170
-
1171
- if ( $this->is_paying() ) {
1172
- $long_term_user_reasons[] = array(
1173
- 'id' => self::REASON_CANT_PAY_ANYMORE,
1174
- 'text' => $this->get_text( 'reason-cant-pay-anymore' ),
1175
- 'input_type' => 'textfield',
1176
- 'input_placeholder' => $this->get_text( 'placeholder-comfortable-price' )
1177
- );
1178
- }
1179
-
1180
- $reason_dont_share_info = array(
1181
- 'id' => self::REASON_DONT_LIKE_TO_SHARE_MY_INFORMATION,
1182
- 'text' => $this->get_text( 'reason-dont-like-to-share-my-information' ),
1183
- 'input_type' => '',
1184
- 'input_placeholder' => ''
1185
- );
1186
-
1187
- /**
1188
- * If the current user has selected the "don't share data" reason in the deactivation feedback modal, inform the
1189
- * user by showing additional message that he doesn't have to share data and can just choose to skip the opt-in
1190
- * (the Skip button is included in the message to show). This message will only be shown if anonymous mode is
1191
- * enabled and the user's account is currently not in pending activation state (similar to the way the Skip
1192
- * button in the opt-in form is shown/hidden).
1193
- */
1194
- if ( $this->is_enable_anonymous() && ! $this->is_pending_activation() ) {
1195
- $reason_dont_share_info['internal_message'] = fs_get_template( 'forms/deactivation/retry-skip.php', $internal_message_template_var );
1196
- }
1197
-
1198
- $uninstall_reasons = array(
1199
- 'long-term' => $long_term_user_reasons,
1200
- 'non-registered-and-non-anonymous-short-term' => array(
1201
- array(
1202
- 'id' => self::REASON_DIDNT_WORK,
1203
- 'text' => sprintf( $this->get_text( 'reason-didnt-work' ), $module_type ),
1204
- 'input_type' => '',
1205
- 'input_placeholder' => ''
1206
- ),
1207
- $reason_dont_share_info,
1208
- $reason_found_better_plugin
1209
- ),
1210
- 'short-term' => array(
1211
- array(
1212
- 'id' => self::REASON_COULDNT_MAKE_IT_WORK,
1213
- 'text' => $this->get_text( 'reason-couldnt-make-it-work' ),
1214
- 'input_type' => '',
1215
- 'input_placeholder' => '',
1216
- 'internal_message' => $contact_support_template
1217
- ),
1218
- $reason_found_better_plugin,
1219
- array(
1220
- 'id' => self::REASON_GREAT_BUT_NEED_SPECIFIC_FEATURE,
1221
- 'text' => sprintf( $this->get_text( 'reason-great-but-need-specific-feature' ), $module_type ),
1222
- 'input_type' => 'textarea',
1223
- 'input_placeholder' => $this->get_text( 'placeholder-feature' )
1224
- ),
1225
- array(
1226
- 'id' => self::REASON_NOT_WORKING,
1227
- 'text' => sprintf( $this->get_text( 'reason-not-working' ), $module_type ),
1228
- 'input_type' => 'textarea',
1229
- 'input_placeholder' => $this->get_text( 'placeholder-share-what-didnt-work' )
1230
- ),
1231
- array(
1232
- 'id' => self::REASON_NOT_WHAT_I_WAS_LOOKING_FOR,
1233
- 'text' => $this->get_text( 'reason-not-what-i-was-looking-for' ),
1234
- 'input_type' => 'textarea',
1235
- 'input_placeholder' => $this->get_text( 'placeholder-what-youve-been-looking-for' )
1236
- ),
1237
- array(
1238
- 'id' => self::REASON_DIDNT_WORK_AS_EXPECTED,
1239
- 'text' => sprintf( $this->get_text( 'reason-didnt-work-as-expected' ), $module_type ),
1240
- 'input_type' => 'textarea',
1241
- 'input_placeholder' => $this->get_text( 'placeholder-what-did-you-expect' )
1242
- )
1243
- )
1244
- );
1245
-
1246
- // Randomize the reasons for the current user type.
1247
- shuffle( $uninstall_reasons[ $user_type ] );
1248
-
1249
- // Keep the following reasons as the last items in the list.
1250
- $uninstall_reasons[ $user_type ][] = $reason_temporary_deactivation;
1251
- $uninstall_reasons[ $user_type ][] = $reason_other;
1252
-
1253
- $uninstall_reasons = $this->apply_filters( 'uninstall_reasons', $uninstall_reasons );
1254
-
1255
- return $uninstall_reasons[ $user_type ];
1256
- }
1257
-
1258
- /**
1259
- * Called after the user has submitted his reason for deactivating the plugin.
1260
- *
1261
- * @author Leo Fajardo (@leorw)
1262
- * @since 1.1.2
1263
- */
1264
- function _submit_uninstall_reason_action() {
1265
- $this->_logger->entrance();
1266
-
1267
- $this->check_ajax_referer( 'submit_uninstall_reason' );
1268
-
1269
- $reason_id = fs_request_get( 'reason_id' );
1270
-
1271
- // Check if the given reason ID is an unsigned integer.
1272
- if ( ! ctype_digit( $reason_id ) ) {
1273
- exit;
1274
- }
1275
-
1276
- $reason_info = trim( fs_request_get( 'reason_info', '' ) );
1277
- if ( ! empty( $reason_info ) ) {
1278
- $reason_info = substr( $reason_info, 0, 128 );
1279
- }
1280
-
1281
- $reason = (object) array(
1282
- 'id' => $reason_id,
1283
- 'info' => $reason_info,
1284
- 'is_anonymous' => fs_request_get_bool( 'is_anonymous' )
1285
- );
1286
-
1287
- $this->_storage->store( 'uninstall_reason', $reason );
1288
-
1289
- /**
1290
- * If the module type is "theme", trigger the uninstall event here (on theme deactivation) since themes do
1291
- * not support uninstall hook.
1292
- *
1293
- * @author Leo Fajardo (@leorw)
1294
- * @since 1.2.2
1295
- */
1296
- if ( $this->is_theme() ) {
1297
- $this->_uninstall_plugin_event( false );
1298
- $this->remove_sdk_reference();
1299
- }
1300
-
1301
- // Print '1' for successful operation.
1302
- echo 1;
1303
- exit;
1304
- }
1305
-
1306
- #endregion
1307
-
1308
- #----------------------------------------------------------------------------------
1309
- #region Instance
1310
- #----------------------------------------------------------------------------------
1311
-
1312
- /**
1313
- * Main singleton instance.
1314
- *
1315
- * @author Vova Feldman (@svovaf)
1316
- * @since 1.0.0
1317
- *
1318
- * @param number $module_id
1319
- * @param string|bool $slug
1320
- * @param bool $is_init Is initiation sequence.
1321
- *
1322
- * @return Freemius|false
1323
- */
1324
- static function instance( $module_id, $slug = false, $is_init = false ) {
1325
- if ( empty( $module_id ) ) {
1326
- return false;
1327
- }
1328
-
1329
- if ( ! is_numeric( $module_id ) ) {
1330
- if ( ! $is_init && true === $slug ) {
1331
- $is_init = true;
1332
- }
1333
-
1334
- $slug = $module_id;
1335
-
1336
- $module = FS_Plugin_Manager::instance( $slug )->get();
1337
-
1338
- if ( is_object( $module ) ) {
1339
- $module_id = $module->id;
1340
- }
1341
- }
1342
-
1343
- $key = 'm_' . $module_id;
1344
-
1345
- if ( ! isset( self::$_instances[ $key ] ) ) {
1346
- if ( 0 === count( self::$_instances ) ) {
1347
- self::_load_required_static();
1348
- }
1349
-
1350
- self::$_instances[ $key ] = new Freemius( $module_id, $slug, $is_init );
1351
- }
1352
-
1353
- return self::$_instances[ $key ];
1354
- }
1355
-
1356
- /**
1357
- * @author Vova Feldman (@svovaf)
1358
- * @since 1.0.6
1359
- *
1360
- * @param number $addon_id
1361
- *
1362
- * @return bool
1363
- */
1364
- private static function has_instance( $addon_id ) {
1365
- return isset( self::$_instances[ 'm_' . $addon_id ] );
1366
- }
1367
-
1368
- /**
1369
- * @author Leo Fajardo (@leorw)
1370
- * @since 1.2.2
1371
- *
1372
- * @param string|number $id_or_slug
1373
- *
1374
- * @return number|false
1375
- */
1376
- private static function get_module_id( $id_or_slug ) {
1377
- if ( is_numeric( $id_or_slug ) ) {
1378
- return $id_or_slug;
1379
- }
1380
-
1381
- foreach ( self::$_instances as $instance ) {
1382
- if ( $instance->is_plugin() && ( $id_or_slug === $instance->get_slug() ) ) {
1383
- return $instance->get_id();
1384
- }
1385
- }
1386
-
1387
- return false;
1388
- }
1389
-
1390
- /**
1391
- * @author Vova Feldman (@svovaf)
1392
- * @since 1.0.6
1393
- *
1394
- * @param number $id
1395
- *
1396
- * @return false|Freemius
1397
- */
1398
- static function get_instance_by_id( $id ) {
1399
- return isset ( self::$_instances[ 'm_' . $id ] ) ?
1400
- self::$_instances[ 'm_' . $id ] :
1401
- false;
1402
- }
1403
-
1404
- /**
1405
- *
1406
- * @author Vova Feldman (@svovaf)
1407
- * @since 1.0.1
1408
- *
1409
- * @param $plugin_file
1410
- *
1411
- * @return false|Freemius
1412
- */
1413
- static function get_instance_by_file( $plugin_file ) {
1414
- $slug = self::find_slug_by_basename( $plugin_file );
1415
-
1416
- return ( false !== $slug ) ?
1417
- self::instance( self::get_module_id( $slug ) ) :
1418
- false;
1419
- }
1420
-
1421
- /**
1422
- * @author Vova Feldman (@svovaf)
1423
- * @since 1.0.6
1424
- *
1425
- * @return false|Freemius
1426
- */
1427
- function get_parent_instance() {
1428
- return self::get_instance_by_id( $this->_plugin->parent_plugin_id );
1429
- }
1430
-
1431
- /**
1432
- * @author Vova Feldman (@svovaf)
1433
- * @since 1.0.6
1434
- *
1435
- * @param string|number $id_or_slug
1436
- *
1437
- * @return false|Freemius
1438
- */
1439
- function get_addon_instance( $id_or_slug ) {
1440
- $addon_id = self::get_module_id( $id_or_slug );
1441
-
1442
- return self::instance( $addon_id );
1443
- }
1444
-
1445
- #endregion ------------------------------------------------------------------
1446
-
1447
- /**
1448
- * @author Vova Feldman (@svovaf)
1449
- * @since 1.0.6
1450
- *
1451
- * @return bool
1452
- */
1453
- function is_parent_plugin_installed() {
1454
- $is_active = self::has_instance( $this->_plugin->parent_plugin_id );
1455
-
1456
- if ( $is_active ) {
1457
- return true;
1458
- }
1459
-
1460
- /**
1461
- * Parent module might be a theme. If that's the case, the add-on's FS
1462
- * instance will be loaded prior to the theme's FS instance, therefore,
1463
- * we need to check if it's active with a "look ahead".
1464
- *
1465
- * @author Vova Feldman
1466
- * @since 1.2.2.3
1467
- */
1468
- global $fs_active_plugins;
1469
- if ( is_object( $fs_active_plugins ) && is_array( $fs_active_plugins->plugins ) ) {
1470
- $active_theme = wp_get_theme();
1471
-
1472
- foreach ( $fs_active_plugins->plugins as $sdk => $module ) {
1473
- if ( WP_FS__MODULE_TYPE_THEME === $module->type ) {
1474
- if ( $module->plugin_path == $active_theme->get_stylesheet() ) {
1475
- // Parent module is a theme and it's currently active.
1476
- return true;
1477
- }
1478
- }
1479
- }
1480
- }
1481
-
1482
- return false;
1483
- }
1484
-
1485
- /**
1486
- * Check if add-on parent plugin in activation mode.
1487
- *
1488
- * @author Vova Feldman (@svovaf)
1489
- * @since 1.0.7
1490
- *
1491
- * @return bool
1492
- */
1493
- function is_parent_in_activation() {
1494
- $parent_fs = $this->get_parent_instance();
1495
- if ( ! is_object( $parent_fs ) ) {
1496
- return false;
1497
- }
1498
-
1499
- return ( $parent_fs->is_activation_mode() );
1500
- }
1501
-
1502
- /**
1503
- * Is plugin in activation mode.
1504
- *
1505
- * @author Vova Feldman (@svovaf)
1506
- * @since 1.0.7
1507
- *
1508
- * @param bool $and_on
1509
- *
1510
- * @return bool
1511
- */
1512
- function is_activation_mode( $and_on = true ) {
1513
- return (
1514
- ( $this->is_on() || ! $and_on ) &&
1515
- ( ! $this->is_registered() || ( $this->is_only_premium() && ! $this->has_features_enabled_license() ) ) &&
1516
- ( ! $this->is_enable_anonymous() ||
1517
- ( ! $this->is_anonymous() && ! $this->is_pending_activation() ) )
1518
- );
1519
- }
1520
-
1521
- /**
1522
- * Check if current page is the opt-in/pending-activation page.
1523
- *
1524
- * @author Vova Feldman (@svovaf)
1525
- * @since 1.2.1.7
1526
- *
1527
- * @return bool
1528
- */
1529
- function is_activation_page() {
1530
- if ( $this->_menu->is_main_settings_page() ) {
1531
- return true;
1532
- }
1533
-
1534
- if ( ! $this->is_activation_mode() ) {
1535
- return false;
1536
- }
1537
-
1538
- // Check if current page is matching the activation page.
1539
- return $this->is_matching_url( $this->get_activation_url() );
1540
- }
1541
-
1542
- /**
1543
- * Check if URL path's are matching and that all querystring
1544
- * arguments of the $sub_url exist in the $url with the same values.
1545
- *
1546
- * WARNING:
1547
- * 1. This method doesn't check if the sub/domain are matching.
1548
- * 2. Ignore case sensitivity.
1549
- *
1550
- * @author Vova Feldman (@svovaf)
1551
- * @since 1.2.1.7
1552
- *
1553
- * @param string $sub_url
1554
- * @param string $url If argument is not set, check if the sub_url matching the current's page URL.
1555
- *
1556
- * @return bool
1557
- */
1558
- private function is_matching_url( $sub_url, $url = '' ) {
1559
- if ( empty( $url ) ) {
1560
- $url = $_SERVER['REQUEST_URI'];
1561
- }
1562
-
1563
- $url = strtolower( $url );
1564
- $sub_url = strtolower( $sub_url );
1565
-
1566
- if ( parse_url( $sub_url, PHP_URL_PATH ) !== parse_url( $url, PHP_URL_PATH ) ) {
1567
- // Different path - DO NOT OVERRIDE PAGE.
1568
- return false;
1569
- }
1570
-
1571
- $url_params = array();
1572
- parse_str( parse_url( $url, PHP_URL_QUERY ), $url_params );
1573
-
1574
- $sub_url_params = array();
1575
- parse_str( parse_url( $sub_url, PHP_URL_QUERY ), $sub_url_params );
1576
-
1577
- foreach ( $sub_url_params as $key => $val ) {
1578
- if ( ! isset( $url_params[ $key ] ) || $val != $url_params[ $key ] ) {
1579
- // Not matching query string - DO NOT OVERRIDE PAGE.
1580
- return false;
1581
- }
1582
- }
1583
-
1584
- return true;
1585
- }
1586
-
1587
- /**
1588
- * Get collection of all active plugins.
1589
- *
1590
- * @author Vova Feldman (@svovaf)
1591
- * @since 1.0.9
1592
- *
1593
- * @return array[string]array
1594
- */
1595
- private static function get_active_plugins() {
1596
- self::require_plugin_essentials();
1597
-
1598
- $active_plugin = array();
1599
- $all_plugins = get_plugins();
1600
- $active_plugins_basenames = get_option( 'active_plugins' );
1601
-
1602
- foreach ( $active_plugins_basenames as $plugin_basename ) {
1603
- $active_plugin[ $plugin_basename ] = $all_plugins[ $plugin_basename ];
1604
- }
1605
-
1606
- return $active_plugin;
1607
- }
1608
-
1609
- /**
1610
- * Get collection of all plugins.
1611
- *
1612
- * @author Vova Feldman (@svovaf)
1613
- * @since 1.1.8
1614
- *
1615
- * @return array Key is the plugin file path and the value is an array of the plugin data.
1616
- */
1617
- private static function get_all_plugins() {
1618
- self::require_plugin_essentials();
1619
-
1620
- $all_plugins = get_plugins();
1621
- $active_plugins_basenames = get_option( 'active_plugins' );
1622
-
1623
- foreach ( $all_plugins as $basename => &$data ) {
1624
- // By default set to inactive (next foreach update the active plugins).
1625
- $data['is_active'] = false;
1626
- // Enrich with plugin slug.
1627
- $data['slug'] = self::get_plugin_slug( $basename );
1628
- }
1629
-
1630
- // Flag active plugins.
1631
- foreach ( $active_plugins_basenames as $basename ) {
1632
- if ( isset( $all_plugins[ $basename ] ) ) {
1633
- $all_plugins[ $basename ]['is_active'] = true;
1634
- }
1635
- }
1636
-
1637
- return $all_plugins;
1638
- }
1639
-
1640
-
1641
- /**
1642
- * Cached result of get_site_transient( 'update_plugins' )
1643
- *
1644
- * @author Vova Feldman (@svovaf)
1645
- * @since 1.1.8
1646
- *
1647
- * @var object
1648
- */
1649
- private static $_plugins_info;
1650
-
1651
- /**
1652
- * Helper function to get specified plugin's slug.
1653
- *
1654
- * @author Vova Feldman (@svovaf)
1655
- * @since 1.1.8
1656
- *
1657
- * @param $basename
1658
- *
1659
- * @return string
1660
- */
1661
- private static function get_plugin_slug( $basename ) {
1662
- if ( ! isset( self::$_plugins_info ) ) {
1663
- self::$_plugins_info = get_site_transient( 'update_plugins' );
1664
- }
1665
-
1666
- $slug = '';
1667
-
1668
- if ( is_object( self::$_plugins_info ) ) {
1669
- if ( isset( self::$_plugins_info->no_update ) &&
1670
- isset( self::$_plugins_info->no_update[ $basename ] ) &&
1671
- ! empty( self::$_plugins_info->no_update[ $basename ]->slug )
1672
- ) {
1673
- $slug = self::$_plugins_info->no_update[ $basename ]->slug;
1674
- } else if ( isset( self::$_plugins_info->response ) &&
1675
- isset( self::$_plugins_info->response[ $basename ] ) &&
1676
- ! empty( self::$_plugins_info->response[ $basename ]->slug )
1677
- ) {
1678
- $slug = self::$_plugins_info->response[ $basename ]->slug;
1679
- }
1680
- }
1681
-
1682
- if ( empty( $slug ) ) {
1683
- // Try to find slug from FS data.
1684
- $slug = self::find_slug_by_basename( $basename );
1685
- }
1686
-
1687
- if ( empty( $slug ) ) {
1688
- // Fallback to plugin's folder name.
1689
- $slug = dirname( $basename );
1690
- }
1691
-
1692
- return $slug;
1693
- }
1694
-
1695
- private static $_statics_loaded = false;
1696
-
1697
- /**
1698
- * Load static resources.
1699
- *
1700
- * @author Vova Feldman (@svovaf)
1701
- * @since 1.0.1
1702
- */
1703
- private static function _load_required_static() {
1704
- if ( self::$_statics_loaded ) {
1705
- return;
1706
- }
1707
-
1708
- self::$_static_logger = FS_Logger::get_logger( WP_FS__SLUG, WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
1709
-
1710
- self::$_static_logger->entrance();
1711
-
1712
- self::$_accounts = FS_Option_Manager::get_manager( WP_FS__ACCOUNTS_OPTION_NAME, true );
1713
-
1714
- self::$_global_admin_notices = FS_Admin_Notice_Manager::instance( 'global' );
1715
-
1716
- add_action( 'admin_menu', array( 'Freemius', '_add_debug_section' ) );
1717
-
1718
- add_action( "wp_ajax_fs_toggle_debug_mode", array( 'Freemius', '_toggle_debug_mode' ) );
1719
-
1720
- self::add_ajax_action_static( 'get_debug_log', array( 'Freemius', '_get_debug_log' ) );
1721
-
1722
- self::add_ajax_action_static( 'get_db_option', array( 'Freemius', '_get_db_option' ) );
1723
-
1724
- self::add_ajax_action_static( 'set_db_option', array( 'Freemius', '_set_db_option' ) );
1725
-
1726
- if ( 0 == did_action( 'plugins_loaded' ) ) {
1727
- add_action( 'plugins_loaded', array( 'Freemius', '_load_textdomain' ), 1 );
1728
- }
1729
-
1730
- self::$_statics_loaded = true;
1731
- }
1732
-
1733
- #----------------------------------------------------------------------------------
1734
- #region Localization
1735
- #----------------------------------------------------------------------------------
1736
-
1737
- /**
1738
- * Load framework's text domain.
1739
- *
1740
- * @author Vova Feldman (@svovaf)
1741
- * @since 1.2.1
1742
- */
1743
- static function _load_textdomain() {
1744
- if ( ! is_admin() ) {
1745
- return;
1746
- }
1747
-
1748
- global $fs_active_plugins;
1749
-
1750
- // Works both for plugins and themes.
1751
- load_plugin_textdomain(
1752
- 'freemius',
1753
- false,
1754
- $fs_active_plugins->newest->sdk_path . '/languages/'
1755
- );
1756
- }
1757
-
1758
- #endregion
1759
-
1760
- #----------------------------------------------------------------------------------
1761
- #region Debugging
1762
- #----------------------------------------------------------------------------------
1763
-
1764
- /**
1765
- * @author Vova Feldman (@svovaf)
1766
- * @since 1.0.8
1767
- */
1768
- static function _add_debug_section() {
1769
- if ( ! current_user_can( 'activate_plugins' )
1770
- && ! current_user_can( 'switch_themes' )
1771
- ) {
1772
- return;
1773
- }
1774
-
1775
- self::$_static_logger->entrance();
1776
-
1777
- $title = sprintf( '%s [v.%s]', fs_text( 'freemius-debug' ), WP_FS__SDK_VERSION );
1778
-
1779
- if ( WP_FS__DEV_MODE ) {
1780
- // Add top-level debug menu item.
1781
- $hook = FS_Admin_Menu_Manager::add_page(
1782
- $title,
1783
- $title,
1784
- 'manage_options',
1785
- 'freemius',
1786
- array( 'Freemius', '_debug_page_render' )
1787
- );
1788
- } else {
1789
- // Add hidden debug page.
1790
- $hook = FS_Admin_Menu_Manager::add_subpage(
1791
- null,
1792
- $title,
1793
- $title,
1794
- 'manage_options',
1795
- 'freemius',
1796
- array( 'Freemius', '_debug_page_render' )
1797
- );
1798
- }
1799
-
1800
- if ( ! empty( $hook ) ) {
1801
- add_action( "load-$hook", array( 'Freemius', '_debug_page_actions' ) );
1802
- }
1803
- }
1804
-
1805
- /**
1806
- * @author Vova Feldman (@svovaf)
1807
- * @since 1.1.7.3
1808
- */
1809
- static function _toggle_debug_mode() {
1810
- $is_on = fs_request_get( 'is_on', false, 'post' );
1811
-
1812
- if ( fs_request_is_post() && in_array( $is_on, array( 0, 1 ) ) ) {
1813
- update_option( 'fs_debug_mode', $is_on );
1814
-
1815
- // Turn on/off storage logging.
1816
- FS_Logger::_set_storage_logging( ( 1 == $is_on ) );
1817
- }
1818
-
1819
- exit;
1820
- }
1821
-
1822
- /**
1823
- * @author Vova Feldman (@svovaf)
1824
- * @since 1.2.1.6
1825
- */
1826
- static function _get_debug_log() {
1827
- $logs = FS_Logger::load_db_logs(
1828
- fs_request_get( 'filters', false, 'post' ),
1829
- ! empty( $_POST['limit'] ) && is_numeric( $_POST['limit'] ) ? $_POST['limit'] : 200,
1830
- ! empty( $_POST['offset'] ) && is_numeric( $_POST['offset'] ) ? $_POST['offset'] : 0
1831
- );
1832
-
1833
- self::shoot_ajax_success( $logs );
1834
- }
1835
-
1836
- /**
1837
- * @author Vova Feldman (@svovaf)
1838
- * @since 1.2.1.7
1839
- */
1840
- static function _get_db_option() {
1841
- $option_name = fs_request_get( 'option_name' );
1842
-
1843
- $value = get_option( $option_name );
1844
-
1845
- $result = array(
1846
- 'name' => $option_name,
1847
- );
1848
-
1849
- if ( false !== $value ) {
1850
- if ( ! is_string( $value ) ) {
1851
- $value = json_encode( $value );
1852
- }
1853
-
1854
- $result['value'] = $value;
1855
- }
1856
-
1857
- self::shoot_ajax_success( $result );
1858
- }
1859
-
1860
- /**
1861
- * @author Vova Feldman (@svovaf)
1862
- * @since 1.2.1.7
1863
- */
1864
- static function _set_db_option() {
1865
- $option_name = fs_request_get( 'option_name' );
1866
- $option_value = fs_request_get( 'option_value' );
1867
-
1868
- if ( ! empty( $option_value ) ) {
1869
- update_option( $option_name, $option_value );
1870
- }
1871
-
1872
- self::shoot_ajax_success();
1873
- }
1874
-
1875
- /**
1876
- * @author Vova Feldman (@svovaf)
1877
- * @since 1.0.8
1878
- */
1879
- static function _debug_page_actions() {
1880
- self::_clean_admin_content_section();
1881
-
1882
- if ( fs_request_is_action( 'restart_freemius' ) ) {
1883
- check_admin_referer( 'restart_freemius' );
1884
-
1885
- // Clear accounts data.
1886
- self::$_accounts->clear( true );
1887
-
1888
- // Clear SDK reference cache.
1889
- delete_option( 'fs_active_plugins' );
1890
- } else if ( fs_request_is_action( 'simulate_trial' ) ) {
1891
- check_admin_referer( 'simulate_trial' );
1892
-
1893
- $fs = freemius( fs_request_get( 'module_id' ) );
1894
-
1895
- // Update SDK install to at least 24 hours before.
1896
- $fs->_storage->install_timestamp = ( time() - WP_FS__TIME_24_HOURS_IN_SEC );
1897
- // Unset the trial shown timestamp.
1898
- unset( $fs->_storage->trial_promotion_shown );
1899
- } else if ( fs_request_is_action( 'delete_install' ) ) {
1900
- check_admin_referer( 'delete_install' );
1901
-
1902
- self::_delete_site_by_slug(
1903
- fs_request_get( 'slug' ),
1904
- fs_request_get( 'module_type' )
1905
- );
1906
- } else if ( fs_request_is_action( 'download_logs' ) ) {
1907
- check_admin_referer( 'download_logs' );
1908
-
1909
- $download_url = FS_Logger::download_db_logs(
1910
- fs_request_get( 'filters', false, 'post' )
1911
- );
1912
-
1913
- if ( false === $download_url ) {
1914
- wp_die( 'Oops... there was an error while generating the logs download file. Please try again and if it doesn\'t work contact support@freemius.com.' );
1915
- }
1916
-
1917
- fs_redirect( $download_url );
1918
- }
1919
- }
1920
-
1921
- /**
1922
- * @author Vova Feldman (@svovaf)
1923
- * @since 1.0.8
1924
- */
1925
- static function _debug_page_render() {
1926
- self::$_static_logger->entrance();
1927
-
1928
- $vars = array(
1929
- 'plugin_sites' => self::get_all_sites(),
1930
- 'theme_sites' => self::get_all_sites( WP_FS__MODULE_TYPE_THEME ),
1931
- 'users' => self::get_all_users(),
1932
- 'addons' => self::get_all_addons(),
1933
- 'account_addons' => self::get_all_account_addons(),
1934
- 'plugin_licenses' => self::get_all_licenses(),
1935
- 'theme_licenses' => self::get_all_licenses( WP_FS__MODULE_TYPE_THEME )
1936
- );
1937
-
1938
- fs_enqueue_local_style( 'fs_debug', '/admin/debug.css' );
1939
- fs_require_once_template( 'debug.php', $vars );
1940
- }
1941
-
1942
- #endregion
1943
-
1944
- #----------------------------------------------------------------------------------
1945
- #region Connectivity Issues
1946
- #----------------------------------------------------------------------------------
1947
-
1948
- /**
1949
- * Check if Freemius should be turned on for the current plugin install.
1950
- *
1951
- * Note:
1952
- * $this->_is_on is updated in has_api_connectivity()
1953
- *
1954
- * @author Vova Feldman (@svovaf)
1955
- * @since 1.0.9
1956
- *
1957
- * @return bool
1958
- */
1959
- function is_on() {
1960
- self::$_static_logger->entrance();
1961
-
1962
- if ( isset( $this->_is_on ) ) {
1963
- return $this->_is_on;
1964
- }
1965
-
1966
- // If already installed or pending then sure it's on :)
1967
- if ( $this->is_registered() || $this->is_pending_activation() ) {
1968
- $this->_is_on = true;
1969
-
1970
- return true;
1971
- }
1972
-
1973
- return false;
1974
- }
1975
-
1976
- /**
1977
- * @author Vova Feldman (@svovaf)
1978
- * @since 1.1.7.3
1979
- *
1980
- * @param bool $flush_if_no_connectivity
1981
- *
1982
- * @return bool
1983
- */
1984
- private function should_run_connectivity_test( $flush_if_no_connectivity = false ) {
1985
- if ( ! isset( $this->_storage->connectivity_test ) ) {
1986
- // Connectivity test was never executed, or cache was cleared.
1987
- return true;
1988
- }
1989
-
1990
- if ( WP_FS__PING_API_ON_IP_OR_HOST_CHANGES ) {
1991
- if ( WP_FS__IS_HTTP_REQUEST ) {
1992
- if ( $_SERVER['HTTP_HOST'] != $this->_storage->connectivity_test['host'] ) {
1993
- // Domain changed.
1994
- return true;
1995
- }
1996
-
1997
- if ( WP_FS__REMOTE_ADDR != $this->_storage->connectivity_test['server_ip'] ) {
1998
- // Server IP changed.
1999
- return true;
2000
- }
2001
- }
2002
- }
2003
-
2004
- if ( $this->_storage->connectivity_test['is_connected'] &&
2005
- $this->_storage->connectivity_test['is_active']
2006
- ) {
2007
- // API connected and Freemius is active - no need to run connectivity check.
2008
- return false;
2009
- }
2010
-
2011
- if ( $flush_if_no_connectivity ) {
2012
- /**
2013
- * If explicitly asked to flush when no connectivity - do it only
2014
- * if at least 10 sec passed from the last API connectivity test.
2015
- */
2016
- return ( isset( $this->_storage->connectivity_test['timestamp'] ) &&
2017
- ( WP_FS__SCRIPT_START_TIME - $this->_storage->connectivity_test['timestamp'] ) > 10 );
2018
- }
2019
-
2020
- /**
2021
- * @since 1.1.7 Don't check for connectivity on plugin downgrade.
2022
- */
2023
- $version = $this->get_plugin_version();
2024
- if ( version_compare( $version, $this->_storage->connectivity_test['version'], '>' ) ) {
2025
- // If it's a plugin version upgrade and Freemius is off or no connectivity, run connectivity test.
2026
- return true;
2027
- }
2028
-
2029
- return false;
2030
- }
2031
-
2032
- /**
2033
- * @author Vova Feldman (@svovaf)
2034
- * @since 1.1.7.4
2035
- *
2036
- * @return object|false
2037
- */
2038
- private function ping() {
2039
- if ( WP_FS__SIMULATE_NO_API_CONNECTIVITY ) {
2040
- return false;
2041
- }
2042
-
2043
- $version = $this->get_plugin_version();
2044
-
2045
- $is_update = $this->apply_filters( 'is_plugin_update', $this->is_plugin_update() );
2046
-
2047
- return $this->get_api_plugin_scope()->ping(
2048
- $this->get_anonymous_id(),
2049
- array(
2050
- 'is_update' => json_encode( $is_update ),
2051
- 'version' => $version,
2052
- 'sdk' => $this->version,
2053
- 'is_admin' => json_encode( is_admin() ),
2054
- 'is_ajax' => json_encode( self::is_ajax() ),
2055
- 'is_cron' => json_encode( self::is_cron() ),
2056
- 'is_http' => json_encode( WP_FS__IS_HTTP_REQUEST ),
2057
- )
2058
- );
2059
- }
2060
-
2061
- /**
2062
- * Check if there's any connectivity issue to Freemius API.
2063
- *
2064
- * @author Vova Feldman (@svovaf)
2065
- * @since 1.0.9
2066
- *
2067
- * @param bool $flush_if_no_connectivity
2068
- *
2069
- * @return bool
2070
- */
2071
- function has_api_connectivity( $flush_if_no_connectivity = false ) {
2072
- $this->_logger->entrance();
2073
-
2074
- if ( isset( $this->_has_api_connection ) && ( $this->_has_api_connection || ! $flush_if_no_connectivity ) ) {
2075
- return $this->_has_api_connection;
2076
- }
2077
-
2078
- if ( WP_FS__SIMULATE_NO_API_CONNECTIVITY &&
2079
- isset( $this->_storage->connectivity_test ) &&
2080
- true === $this->_storage->connectivity_test['is_connected']
2081
- ) {
2082
- unset( $this->_storage->connectivity_test );
2083
- }
2084
-
2085
- if ( ! $this->should_run_connectivity_test( $flush_if_no_connectivity ) ) {
2086
- $this->_has_api_connection = $this->_storage->connectivity_test['is_connected'];
2087
- /**
2088
- * @since 1.1.6 During dev mode, if there's connectivity - turn Freemius on regardless the configuration.
2089
- *
2090
- * @since 1.2.1.5 If the user running the premium version then ignore the 'is_active' flag and turn Freemius on to enable license key activation.
2091
- */
2092
- $this->_is_on = $this->_storage->connectivity_test['is_active'] ||
2093
- $this->is_premium() ||
2094
- ( WP_FS__DEV_MODE && $this->_has_api_connection && ! WP_FS__SIMULATE_FREEMIUS_OFF );
2095
-
2096
- return $this->_has_api_connection;
2097
- }
2098
-
2099
- $pong = $this->ping();
2100
- $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong );
2101
-
2102
- if ( ! $is_connected ) {
2103
- // API failure.
2104
- $this->_add_connectivity_issue_message( $pong );
2105
- }
2106
-
2107
- $this->store_connectivity_info( $pong, $is_connected );
2108
-
2109
- return $this->_has_api_connection;
2110
- }
2111
-
2112
- /**
2113
- * @author Vova Feldman (@svovaf)
2114
- * @since 1.1.7.4
2115
- *
2116
- * @param object $pong
2117
- * @param bool $is_connected
2118
- */
2119
- private function store_connectivity_info( $pong, $is_connected ) {
2120
- $this->_logger->entrance();
2121
-
2122
- $version = $this->get_plugin_version();
2123
-
2124
- if ( ! $is_connected || WP_FS__SIMULATE_FREEMIUS_OFF ) {
2125
- $is_active = false;
2126
- } else {
2127
- $is_active = ( isset( $pong->is_active ) && true == $pong->is_active );
2128
- }
2129
-
2130
- $is_active = $this->apply_filters(
2131
- 'is_on',
2132
- $is_active,
2133
- $this->is_plugin_update(),
2134
- $version
2135
- );
2136
-
2137
- $this->_storage->connectivity_test = array(
2138
- 'is_connected' => $is_connected,
2139
- 'host' => $_SERVER['HTTP_HOST'],
2140
- 'server_ip' => WP_FS__REMOTE_ADDR,
2141
- 'is_active' => $is_active,
2142
- 'timestamp' => WP_FS__SCRIPT_START_TIME,
2143
- // Last version with connectivity attempt.
2144
- 'version' => $version,
2145
- );
2146
-
2147
- $this->_has_api_connection = $is_connected;
2148
- $this->_is_on = $is_active || ( WP_FS__DEV_MODE && $is_connected && ! WP_FS__SIMULATE_FREEMIUS_OFF );
2149
- }
2150
-
2151
- /**
2152
- * Force turning Freemius on.
2153
- *
2154
- * @author Vova Feldman (@svovaf)
2155
- * @since 1.1.8.1
2156
- *
2157
- * @return bool TRUE if successfully turned on.
2158
- */
2159
- private function turn_on() {
2160
- $this->_logger->entrance();
2161
-
2162
- if ( $this->is_on() || ! isset( $this->_storage->connectivity_test['is_active'] ) ) {
2163
- return false;
2164
- }
2165
-
2166
- $updated_connectivity = $this->_storage->connectivity_test;
2167
- $updated_connectivity['is_active'] = true;
2168
- $updated_connectivity['timestamp'] = WP_FS__SCRIPT_START_TIME;
2169
- $this->_storage->connectivity_test = $updated_connectivity;
2170
-
2171
- $this->_is_on = true;
2172
-
2173
- return true;
2174
- }
2175
-
2176
- /**
2177
- * Anonymous and unique site identifier (Hash).
2178
- *
2179
- * @author Vova Feldman (@svovaf)
2180
- * @since 1.1.0
2181
- *
2182
- * @return string
2183
- */
2184
- function get_anonymous_id() {
2185
- $unique_id = self::$_accounts->get_option( 'unique_id' );
2186
-
2187
- if ( empty( $unique_id ) || ! is_string( $unique_id ) ) {
2188
- $key = get_site_url();
2189
-
2190
- // If localhost, assign microtime instead of domain.
2191
- if ( WP_FS__IS_LOCALHOST ||
2192
- false !== strpos( $key, 'localhost' ) ||
2193
- false === strpos( $key, '.' )
2194
- ) {
2195
- $key = microtime();
2196
- }
2197
-
2198
- $unique_id = md5( $key );
2199
-
2200
- self::$_accounts->set_option( 'unique_id', $unique_id, true );
2201
- }
2202
-
2203
- $this->_logger->departure( $unique_id );
2204
-
2205
- return $unique_id;
2206
- }
2207
-
2208
- /**
2209
- * @author Vova Feldman (@svovaf)
2210
- * @since 1.1.7.4
2211
- *
2212
- * @return \WP_User
2213
- */
2214
- static function _get_current_wp_user() {
2215
- self::require_pluggable_essentials();
2216
-
2217
- return wp_get_current_user();
2218
- }
2219
-
2220
- /**
2221
- * @author Vova Feldman (@svovaf)
2222
- * @since 1.2.1.7
2223
- *
2224
- * @param string $email
2225
- *
2226
- * @return bool
2227
- */
2228
- static function is_valid_email( $email ) {
2229
- if ( false === filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
2230
- return false;
2231
- }
2232
-
2233
- $parts = explode( '@', $email );
2234
-
2235
- if ( 2 !== count( $parts ) || empty( $parts[1] ) ) {
2236
- return false;
2237
- }
2238
-
2239
- $blacklist = array(
2240
- 'admin.',
2241
- 'webmaster.',
2242
- 'localhost.',
2243
- 'dev.',
2244
- 'development.',
2245
- 'test.',
2246
- 'stage.',
2247
- 'staging.',
2248
- );
2249
-
2250
- // Make sure domain is not one of the blacklisted.
2251
- foreach ( $blacklist as $invalid ) {
2252
- if ( 0 === strpos( $parts[1], $invalid ) ) {
2253
- return false;
2254
- }
2255
- }
2256
-
2257
- // Get the UTF encoded domain name.
2258
- $domain = idn_to_ascii( $parts[1] ) . '.';
2259
-
2260
- return ( checkdnsrr( $domain, 'MX' ) || checkdnsrr( $domain, 'A' ) );
2261
- }
2262
-
2263
- /**
2264
- * Generate API connectivity issue message.
2265
- *
2266
- * @author Vova Feldman (@svovaf)
2267
- * @since 1.0.9
2268
- *
2269
- * @param mixed $api_result
2270
- * @param bool $is_first_failure
2271
- */
2272
- function _add_connectivity_issue_message( $api_result, $is_first_failure = true ) {
2273
- if ( ! $this->is_premium() && $this->_enable_anonymous ) {
2274
- // Don't add message if it's the free version and can run anonymously.
2275
- return;
2276
- }
2277
-
2278
- if ( ! function_exists( 'wp_nonce_url' ) ) {
2279
- require_once ABSPATH . 'wp-includes/functions.php';
2280
- }
2281
-
2282
- $current_user = self::_get_current_wp_user();
2283
- // $admin_email = get_option( 'admin_email' );
2284
- $admin_email = $current_user->user_email;
2285
-
2286
- $message = false;
2287
- if ( is_object( $api_result ) &&
2288
- isset( $api_result->error ) &&
2289
- isset( $api_result->error->code )
2290
- ) {
2291
- switch ( $api_result->error->code ) {
2292
- case 'curl_missing':
2293
- $missing_methods = '';
2294
- if ( is_array( $api_result->missing_methods ) &&
2295
- ! empty( $api_result->missing_methods )
2296
- ) {
2297
- foreach ( $api_result->missing_methods as $m ) {
2298
- if ( 'curl_version' === $m ) {
2299
- continue;
2300
- }
2301
-
2302
- if ( ! empty( $missing_methods ) ) {
2303
- $missing_methods .= ', ';
2304
- }
2305
-
2306
- $missing_methods .= sprintf( '<code>%s</code>', $m );
2307
- }
2308
-
2309
- if ( ! empty( $missing_methods ) ) {
2310
- $missing_methods = sprintf(
2311
- '<br><br><b>%s</b> %s',
2312
- $this->get_text( 'curl-disabled-methods' ),
2313
- $missing_methods
2314
- );
2315
- }
2316
- }
2317
-
2318
- $message = sprintf(
2319
- $this->get_text( 'x-requires-access-to-api' ) . ' ' .
2320
- $this->get_text( 'curl-missing-message' ) . ' ' .
2321
- $missing_methods .
2322
- ' %s',
2323
- '<b>' . $this->get_plugin_name() . '</b>',
2324
- sprintf(
2325
- '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
2326
- sprintf(
2327
- '<a class="fs-resolve" data-type="curl" href="#"><b>%s</b></a>%s',
2328
- $this->get_text( 'curl-missing-no-clue-title' ),
2329
- ' - ' . sprintf(
2330
- $this->get_text( 'curl-missing-no-clue-desc' ),
2331
- '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
2332
- )
2333
- ),
2334
- sprintf(
2335
- '<b>%s</b> - %s',
2336
- $this->get_text( 'sysadmin-title' ),
2337
- sprintf( $this->get_text( 'curl-missing-sysadmin-desc' ), $this->_module_type )
2338
- ),
2339
- sprintf(
2340
- '<a href="%s"><b>%s</b></a>%s',
2341
- wp_nonce_url( 'plugins.php?action=deactivate&amp;plugin=' . $this->_plugin_basename . '&amp;plugin_status=' . 'all' . '&amp;paged=' . '1' . '&amp;s=' . '', 'deactivate-plugin_' . $this->_plugin_basename ),
2342
- $this->get_text( 'deactivate-plugin-title' ),
2343
- ' - ' . $this->get_text( 'deactivate-plugin-desc' )
2344
- )
2345
- )
2346
- );
2347
- break;
2348
- case 'cloudflare_ddos_protection':
2349
- $message = sprintf(
2350
- $this->get_text( 'x-requires-access-to-api' ) . ' ' .
2351
- $this->get_text( 'cloudflare-blocks-connection-message' ) . ' ' .
2352
- $this->get_text( 'happy-to-resolve-issue-asap' ) .
2353
- ' %s',
2354
- '<b>' . $this->get_plugin_name() . '</b>',
2355
- sprintf(
2356
- '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
2357
- sprintf(
2358
- '<a class="fs-resolve" data-type="cloudflare" href="#"><b>%s</b></a>%s',
2359
- $this->get_text( 'fix-issue-title' ),
2360
- ' - ' . sprintf(
2361
- $this->get_text( 'fix-issue-desc' ),
2362
- '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
2363
- )
2364
- ),
2365
- sprintf(
2366
- '<a href="%s" target="_blank"><b>%s</b></a>%s',
2367
- sprintf( 'https://wordpress.org/plugins/%s/download/', $this->_slug ),
2368
- $this->get_text( 'install-previous-title' ),
2369
- ' - ' . $this->get_text( 'install-previous-desc' )
2370
- ),
2371
- sprintf(
2372
- '<a href="%s"><b>%s</b></a>%s',
2373
- wp_nonce_url( 'plugins.php?action=deactivate&amp;plugin=' . $this->_plugin_basename . '&amp;plugin_status=' . 'all' . '&amp;paged=' . '1' . '&amp;s=' . '', 'deactivate-plugin_' . $this->_plugin_basename ),
2374
- $this->get_text( 'deactivate-plugin-title' ),
2375
- ' - ' . $this->get_text( 'deactivate-plugin-desc' )
2376
- )
2377
- )
2378
- );
2379
- break;
2380
- case 'squid_cache_block':
2381
- $message = sprintf(
2382
- $this->get_text( 'x-requires-access-to-api' ) . ' ' .
2383
- $this->get_text( 'squid-blocks-connection-message' ) .
2384
- ' %s',
2385
- '<b>' . $this->get_plugin_name() . '</b>',
2386
- sprintf(
2387
- '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
2388
- sprintf(
2389
- '<a class="fs-resolve" data-type="squid" href="#"><b>%s</b></a>%s',
2390
- $this->get_text( 'squid-no-clue-title' ),
2391
- ' - ' . sprintf(
2392
- $this->get_text( 'squid-no-clue-desc' ),
2393
- '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
2394
- )
2395
- ),
2396
- sprintf(
2397
- '<b>%s</b> - %s',
2398
- $this->get_text( 'sysadmin-title' ),
2399
- sprintf(
2400
- $this->get_text( 'squid-sysadmin-desc' ),
2401
- // We use a filter since the plugin might require additional API connectivity.
2402
- '<b>' . implode( ', ', $this->apply_filters( 'api_domains', array( 'api.freemius.com', 'wp.freemius.com' ) ) ) . '</b>',
2403
- $this->_module_type
2404
- )
2405
- ),
2406
- sprintf(
2407
- '<a href="%s"><b>%s</b></a>%s',
2408
- wp_nonce_url( 'plugins.php?action=deactivate&amp;plugin=' . $this->_plugin_basename . '&amp;plugin_status=' . 'all' . '&amp;paged=' . '1' . '&amp;s=' . '', 'deactivate-plugin_' . $this->_plugin_basename ),
2409
- $this->get_text( 'deactivate-plugin-title' ),
2410
- ' - ' . $this->get_text( 'deactivate-plugin-desc' )
2411
- )
2412
- )
2413
- );
2414
- break;
2415
- // default:
2416
- // $message = $this->get_text( 'connectivity-test-fails-message' );
2417
- // break;
2418
- }
2419
- }
2420
-
2421
- $message_id = 'failed_connect_api';
2422
- $type = 'error';
2423
-
2424
- if ( false === $message ) {
2425
- if ( $is_first_failure ) {
2426
- // First attempt failed.
2427
- $message = sprintf(
2428
- $this->get_text( 'x-requires-access-to-api' ) . ' ' .
2429
- $this->get_text( 'connectivity-test-fails-message' ) . ' ' .
2430
- $this->get_text( 'connectivity-test-maybe-temporary' ) . '<br><br>' .
2431
- '%s',
2432
- '<b>' . $this->get_plugin_name() . '</b>',
2433
- sprintf(
2434
- '<div id="fs_firewall_issue_options">%s %s</div>',
2435
- sprintf(
2436
- '<a class="button button-primary fs-resolve" data-type="retry_ping" href="#">%s</a>',
2437
- $this->get_text( 'yes-do-your-thing' )
2438
- ),
2439
- sprintf(
2440
- '<a href="%s" class="button">%s</a>',
2441
- wp_nonce_url( 'plugins.php?action=deactivate&amp;plugin=' . $this->_plugin_basename . '&amp;plugin_status=' . 'all' . '&amp;paged=' . '1' . '&amp;s=' . '', 'deactivate-plugin_' . $this->_plugin_basename ),
2442
- $this->get_text( 'no-deactivate' )
2443
- )
2444
- )
2445
- );
2446
-
2447
- $message_id = 'failed_connect_api_first';
2448
- $type = 'promotion';
2449
- } else {
2450
- // Second connectivity attempt failed.
2451
- $message = sprintf(
2452
- $this->get_text( 'x-requires-access-to-api' ) . ' ' .
2453
- $this->get_text( 'connectivity-test-fails-message' ) . ' ' .
2454
- $this->get_text( 'happy-to-resolve-issue-asap' ) .
2455
- ' %s',
2456
- '<b>' . $this->get_plugin_name() . '</b>',
2457
- sprintf(
2458
- '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
2459
- sprintf(
2460
- '<a class="fs-resolve" data-type="general" href="#"><b>%s</b></a>%s',
2461
- $this->get_text( 'fix-issue-title' ),
2462
- ' - ' . sprintf(
2463
- $this->get_text( 'fix-issue-desc' ),
2464
- '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
2465
- )
2466
- ),
2467
- sprintf(
2468
- '<a href="%s" target="_blank"><b>%s</b></a>%s',
2469
- sprintf( 'https://wordpress.org/plugins/%s/download/', $this->_slug ),
2470
- $this->get_text( 'install-previous-title' ),
2471
- ' - ' . $this->get_text( 'install-previous-desc' )
2472
- ),
2473
- sprintf(
2474
- '<a href="%s"><b>%s</b></a>%s',
2475
- wp_nonce_url( 'plugins.php?action=deactivate&amp;plugin=' . $this->_plugin_basename . '&amp;plugin_status=' . 'all' . '&amp;paged=' . '1' . '&amp;s=' . '', 'deactivate-plugin_' . $this->_plugin_basename ),
2476
- $this->get_text( 'deactivate-plugin-title' ),
2477
- ' - ' . $this->get_text( 'deactivate-plugin-desc' )
2478
- )
2479
- )
2480
- );
2481
- }
2482
- }
2483
-
2484
- $this->_admin_notices->add_sticky(
2485
- $message,
2486
- $message_id,
2487
- $this->get_text( 'oops' ) . '...',
2488
- $type
2489
- );
2490
- }
2491
-
2492
- /**
2493
- * Handle user request to resolve connectivity issue.
2494
- * This method will send an email to Freemius API technical staff for resolution.
2495
- * The email will contain server's info and installed plugins (might be caching issue).
2496
- *
2497
- * @author Vova Feldman (@svovaf)
2498
- * @since 1.0.9
2499
- */
2500
- function _email_about_firewall_issue() {
2501
- $this->_admin_notices->remove_sticky( 'failed_connect_api' );
2502
-
2503
- $pong = $this->ping();
2504
-
2505
- $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong );
2506
-
2507
- if ( $is_connected ) {
2508
- $this->store_connectivity_info( $pong, $is_connected );
2509
-
2510
- echo $this->get_after_plugin_activation_redirect_url();
2511
- exit;
2512
- }
2513
-
2514
- $current_user = self::_get_current_wp_user();
2515
- $admin_email = $current_user->user_email;
2516
-
2517
- $error_type = fs_request_get( 'error_type', 'general' );
2518
-
2519
- switch ( $error_type ) {
2520
- case 'squid':
2521
- $title = 'Squid ACL Blocking Issue';
2522
- break;
2523
- case 'cloudflare':
2524
- $title = 'CloudFlare Blocking Issue';
2525
- break;
2526
- default:
2527
- $title = 'API Connectivity Issue';
2528
- break;
2529
- }
2530
-
2531
- $custom_email_sections = array();
2532
-
2533
- // Add 'API Error' custom email section.
2534
- $custom_email_sections['api_error'] = array(
2535
- 'title' => 'API Error',
2536
- 'rows' => array(
2537
- 'ping' => array(
2538
- 'API Error',
2539
- is_string( $pong ) ? htmlentities( $pong ) : json_encode( $pong )
2540
- ),
2541
- )
2542
- );
2543
-
2544
- // Send email with technical details to resolve API connectivity issues.
2545
- $this->send_email(
2546
- 'api@freemius.com', // recipient
2547
- $title . ' [' . $this->get_plugin_name() . ']', // subject
2548
- $custom_email_sections,
2549
- array( "Reply-To: $admin_email <$admin_email>" ) // headers
2550
- );
2551
-
2552
- $this->_admin_notices->add_sticky(
2553
- sprintf(
2554
- $this->get_text( 'fix-request-sent-message' ),
2555
- '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
2556
- ),
2557
- 'server_details_sent'
2558
- );
2559
-
2560
- // Action was taken, tell that API connectivity troubleshooting should be off now.
2561
-
2562
- echo "1";
2563
- exit;
2564
- }
2565
-
2566
- /**
2567
- * Handle connectivity test retry approved by the user.
2568
- *
2569
- * @author Vova Feldman (@svovaf)
2570
- * @since 1.1.7.4
2571
- */
2572
- function _retry_connectivity_test() {
2573
- $this->_admin_notices->remove_sticky( 'failed_connect_api_first' );
2574
-
2575
- $pong = $this->ping();
2576
-
2577
- $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong );
2578
-
2579
- if ( $is_connected ) {
2580
- $this->store_connectivity_info( $pong, $is_connected );
2581
-
2582
- echo $this->get_after_plugin_activation_redirect_url();
2583
- } else {
2584
- // Add connectivity issue message after 2nd failed attempt.
2585
- $this->_add_connectivity_issue_message( $pong, false );
2586
-
2587
- echo "1";
2588
- }
2589
-
2590
- exit;
2591
- }
2592
-
2593
- static function _add_firewall_issues_javascript() {
2594
- $params = array();
2595
- fs_require_once_template( 'firewall-issues-js.php', $params );
2596
- }
2597
-
2598
- #endregion
2599
-
2600
- #----------------------------------------------------------------------------------
2601
- #region Email
2602
- #----------------------------------------------------------------------------------
2603
-
2604
- /**
2605
- * Generates and sends an HTML email with customizable sections.
2606
- *
2607
- * @author Leo Fajardo (@leorw)
2608
- * @since 1.1.2
2609
- *
2610
- * @param string $to_address
2611
- * @param string $subject
2612
- * @param array $sections
2613
- * @param array $headers
2614
- *
2615
- * @return bool Whether the email contents were sent successfully.
2616
- */
2617
- private function send_email(
2618
- $to_address,
2619
- $subject,
2620
- $sections = array(),
2621
- $headers = array()
2622
- ) {
2623
- $default_sections = $this->get_email_sections();
2624
-
2625
- // Insert new sections or replace the default email sections.
2626
- if ( is_array( $sections ) && ! empty( $sections ) ) {
2627
- foreach ( $sections as $section_id => $custom_section ) {
2628
- if ( ! isset( $default_sections[ $section_id ] ) ) {
2629
- // If the section does not exist, add it.
2630
- $default_sections[ $section_id ] = $custom_section;
2631
- } else {
2632
- // If the section already exists, override it.
2633
- $current_section = $default_sections[ $section_id ];
2634
-
2635
- // Replace the current section's title if a custom section title exists.
2636
- if ( isset( $custom_section['title'] ) ) {
2637
- $current_section['title'] = $custom_section['title'];
2638
- }
2639
-
2640
- // Insert new rows under the current section or replace the default rows.
2641
- if ( isset( $custom_section['rows'] ) && is_array( $custom_section['rows'] ) && ! empty( $custom_section['rows'] ) ) {
2642
- foreach ( $custom_section['rows'] as $row_id => $row ) {
2643
- $current_section['rows'][ $row_id ] = $row;
2644
- }
2645
- }
2646
-
2647
- $default_sections[ $section_id ] = $current_section;
2648
- }
2649
- }
2650
- }
2651
-
2652
- $vars = array( 'sections' => $default_sections );
2653
- $message = fs_get_template( 'email.php', $vars );
2654
-
2655
- // Set the type of email to HTML.
2656
- $headers[] = 'Content-type: text/html; charset=UTF-8';
2657
-
2658
- $header_string = implode( "\r\n", $headers );
2659
-
2660
- return wp_mail(
2661
- $to_address,
2662
- $subject,
2663
- $message,
2664
- $header_string
2665
- );
2666
- }
2667
-
2668
- /**
2669
- * Generates the data for the sections of the email content.
2670
- *
2671
- * @author Leo Fajardo (@leorw)
2672
- * @since 1.1.2
2673
- *
2674
- * @return array
2675
- */
2676
- private function get_email_sections() {
2677
- // Retrieve the current user's information so that we can get the user's email, first name, and last name below.
2678
- $current_user = self::_get_current_wp_user();
2679
-
2680
- // Retrieve the cURL version information so that we can get the version number below.
2681
- $curl_version_information = curl_version();
2682
-
2683
- $active_plugin = self::get_active_plugins();
2684
-
2685
- // Generate the list of active plugins separated by new line.
2686
- $active_plugin_string = '';
2687
- foreach ( $active_plugin as $plugin ) {
2688
- $active_plugin_string .= sprintf(
2689
- '<a href="%s">%s</a> [v%s]<br>',
2690
- $plugin['PluginURI'],
2691
- $plugin['Name'],
2692
- $plugin['Version']
2693
- );
2694
- }
2695
-
2696
- $server_ip = WP_FS__REMOTE_ADDR;
2697
-
2698
- // Add PHP info for deeper investigation.
2699
- ob_start();
2700
- phpinfo();
2701
- $php_info = ob_get_clean();
2702
-
2703
- $api_domain = substr( FS_API__ADDRESS, strpos( FS_API__ADDRESS, ':' ) + 3 );
2704
-
2705
- // Generate the default email sections.
2706
- $sections = array(
2707
- 'sdk' => array(
2708
- 'title' => 'SDK',
2709
- 'rows' => array(
2710
- 'fs_version' => array( 'FS Version', $this->version ),
2711
- 'curl_version' => array( 'cURL Version', $curl_version_information['version'] )
2712
- )
2713
- ),
2714
- 'plugin' => array(
2715
- 'title' => ucfirst( $this->get_module_type() ),
2716
- 'rows' => array(
2717
- 'name' => array( 'Name', $this->get_plugin_name() ),
2718
- 'version' => array( 'Version', $this->get_plugin_version() )
2719
- )
2720
- ),
2721
- 'api' => array(
2722
- 'title' => 'API Subdomain',
2723
- 'rows' => array(
2724
- 'dns' => array(
2725
- 'DNS_CNAME',
2726
- function_exists( 'dns_get_record' ) ?
2727
- var_export( dns_get_record( $api_domain, DNS_CNAME ), true ) :
2728
- 'dns_get_record() disabled/blocked'
2729
- ),
2730
- 'ip' => array(
2731
- 'IP',
2732
- function_exists( 'gethostbyname' ) ?
2733
- gethostbyname( $api_domain ) :
2734
- 'gethostbyname() disabled/blocked'
2735
- ),
2736
- ),
2737
- ),
2738
- 'site' => array(
2739
- 'title' => 'Site',
2740
- 'rows' => array(
2741
- 'unique_id' => array( 'Unique ID', $this->get_anonymous_id() ),
2742
- 'address' => array( 'Address', site_url() ),
2743
- 'host' => array(
2744
- 'HTTP_HOST',
2745
- ( ! empty( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : '' )
2746
- ),
2747
- 'hosting' => array(
2748
- 'Hosting Company' => fs_request_has( 'hosting_company' ) ?
2749
- fs_request_get( 'hosting_company' ) :
2750
- 'Unknown',
2751
- ),
2752
- 'server_addr' => array(
2753
- 'SERVER_ADDR',
2754
- '<a href="http://www.projecthoneypot.org/ip_' . $server_ip . '">' . $server_ip . '</a>'
2755
- )
2756
- )
2757
- ),
2758
- 'user' => array(
2759
- 'title' => 'User',
2760
- 'rows' => array(
2761
- 'email' => array( 'Email', $current_user->user_email ),
2762
- 'first' => array( 'First', $current_user->user_firstname ),
2763
- 'last' => array( 'Last', $current_user->user_lastname )
2764
- )
2765
- ),
2766
- 'plugins' => array(
2767
- 'title' => 'Plugins',
2768
- 'rows' => array(
2769
- 'active_plugins' => array( 'Active Plugins', $active_plugin_string )
2770
- )
2771
- ),
2772
- 'php_info' => array(
2773
- 'title' => 'PHP Info',
2774
- 'rows' => array(
2775
- 'info' => array( $php_info )
2776
- ),
2777
- )
2778
- );
2779
-
2780
- // Allow the sections to be modified by other code.
2781
- $sections = $this->apply_filters( 'email_template_sections', $sections );
2782
-
2783
- return $sections;
2784
- }
2785
-
2786
- #endregion
2787
-
2788
- #----------------------------------------------------------------------------------
2789
- #region Initialization
2790
- #----------------------------------------------------------------------------------
2791
-
2792
- /**
2793
- * Init plugin's Freemius instance.
2794
- *
2795
- * @author Vova Feldman (@svovaf)
2796
- * @since 1.0.1
2797
- *
2798
- * @param number $id
2799
- * @param string $public_key
2800
- * @param bool $is_live
2801
- * @param bool $is_premium
2802
- */
2803
- function init( $id, $public_key, $is_live = true, $is_premium = true ) {
2804
- $this->_logger->entrance();
2805
-
2806
- $this->dynamic_init( array(
2807
- 'id' => $id,
2808
- 'public_key' => $public_key,
2809
- 'is_live' => $is_live,
2810
- 'is_premium' => $is_premium,
2811
- ) );
2812
- }
2813
-
2814
- /**
2815
- * Dynamic initiator, originally created to support initiation
2816
- * with parent_id for add-ons.
2817
- *
2818
- * @author Vova Feldman (@svovaf)
2819
- * @since 1.0.6
2820
- *
2821
- * @param array $plugin_info
2822
- *
2823
- * @throws Freemius_Exception
2824
- */
2825
- function dynamic_init( array $plugin_info ) {
2826
- $this->_logger->entrance();
2827
-
2828
- $this->parse_settings( $plugin_info );
2829
-
2830
- if ( ! self::is_ajax() ) {
2831
- if ( ! $this->is_addon() || $this->is_only_premium() ) {
2832
- add_action( 'admin_menu', array( &$this, '_prepare_admin_menu' ), WP_FS__LOWEST_PRIORITY );
2833
- }
2834
- }
2835
-
2836
- if ( $this->should_stop_execution() ) {
2837
- return;
2838
- }
2839
-
2840
- if ( ! $this->is_registered() ) {
2841
- if ( $this->is_anonymous() ) {
2842
- // If user skipped, no need to test connectivity.
2843
- $this->_has_api_connection = true;
2844
- $this->_is_on = true;
2845
- } else {
2846
- if ( ! $this->has_api_connectivity() ) {
2847
- if ( $this->_admin_notices->has_sticky( 'failed_connect_api_first' ) ||
2848
- $this->_admin_notices->has_sticky( 'failed_connect_api' )
2849
- ) {
2850
- if ( ! $this->_enable_anonymous || $this->is_premium() ) {
2851
- // If anonymous mode is disabled, add firewall admin-notice message.
2852
- add_action( 'admin_footer', array( 'Freemius', '_add_firewall_issues_javascript' ) );
2853
-
2854
- $this->add_ajax_action( 'resolve_firewall_issues', array(
2855
- &$this,
2856
- '_email_about_firewall_issue'
2857
- ) );
2858
-
2859
- $this->add_ajax_action( 'retry_connectivity_test', array(
2860
- &$this,
2861
- '_retry_connectivity_test'
2862
- ) );
2863
- }
2864
- }
2865
-
2866
- return;
2867
- } else {
2868
- $this->_admin_notices->remove_sticky( array(
2869
- 'failed_connect_api_first',
2870
- 'failed_connect_api',
2871
- ) );
2872
-
2873
- if ( $this->_anonymous_mode ) {
2874
- // Simulate anonymous mode.
2875
- $this->_is_anonymous = true;
2876
- }
2877
- }
2878
- }
2879
-
2880
- // Check if Freemius is on for the current plugin.
2881
- // This MUST be executed after all the plugin variables has been loaded.
2882
- if ( ! $this->is_on() ) {
2883
- return;
2884
- }
2885
- }
2886
-
2887
- if ( $this->has_api_connectivity() ) {
2888
- if ( self::is_cron() ) {
2889
- $this->hook_callback_to_sync_cron();
2890
- } else if ( $this->is_user_in_admin() ) {
2891
- /**
2892
- * Schedule daily data sync cron if:
2893
- *
2894
- * 1. User opted-in (for tracking).
2895
- * 2. If skipped, but later upgraded (opted-in via upgrade).
2896
- *
2897
- * @author Vova Feldman (@svovaf)
2898
- * @since 1.1.7.3
2899
- *
2900
- */
2901
- if ( $this->is_registered() ) {
2902
- if ( ! $this->is_sync_cron_on() && $this->is_tracking_allowed() ) {
2903
- $this->schedule_sync_cron();
2904
- }
2905
- }
2906
-
2907
- /**
2908
- * Check if requested for manual blocking background sync.
2909
- */
2910
- if ( fs_request_has( 'background_sync' ) ) {
2911
- $this->run_manual_sync();
2912
- }
2913
- }
2914
- }
2915
-
2916
- if ( $this->is_registered() ) {
2917
- $this->hook_callback_to_install_sync();
2918
- }
2919
-
2920
- if ( $this->is_addon() ) {
2921
- if ( $this->is_parent_plugin_installed() ) {
2922
- // Link to parent FS.
2923
- $this->_parent = self::get_instance_by_id( $this->_plugin->parent_plugin_id );
2924
-
2925
- // Get parent plugin reference.
2926
- $this->_parent_plugin = $this->_parent->get_plugin();
2927
- }
2928
- }
2929
-
2930
- if ( $this->is_user_in_admin() ) {
2931
- if ( self::is_plugins_page() && $this->is_plugin() ) {
2932
- $this->hook_plugin_action_links();
2933
- }
2934
-
2935
- if ( $this->is_addon() ) {
2936
- if ( ! $this->is_parent_plugin_installed() ) {
2937
- $parent_name = $this->get_option( $plugin_info, 'parent_name', null );
2938
-
2939
- if ( isset( $plugin_info['parent'] ) ) {
2940
- $parent_name = $this->get_option( $plugin_info['parent'], 'name', null );
2941
- }
2942
-
2943
- $this->_admin_notices->add(
2944
- ( ! empty( $parent_name ) ?
2945
- sprintf( $this->get_text( 'addon-x-cannot-run-without-y' ), $this->get_plugin_name(), $parent_name ) :
2946
- sprintf( $this->get_text( 'addon-x-cannot-run-without-parent' ), $this->get_plugin_name() )
2947
- ),
2948
- $this->get_text( 'oops' ) . '...',
2949
- 'error'
2950
- );
2951
-
2952
- return;
2953
- } else {
2954
- if ( $this->_parent->is_registered() && ! $this->is_registered() ) {
2955
- // If parent plugin activated, automatically install add-on for the user.
2956
- $this->_activate_addon_account( $this->_parent );
2957
- } else if ( ! $this->_parent->is_registered() && $this->is_registered() ) {
2958
- // If add-on activated and parent not, automatically install parent for the user.
2959
- $this->activate_parent_account( $this->_parent );
2960
- }
2961
-
2962
- // @todo This should be only executed on activation. It should be migrated to register_activation_hook() together with other activation related logic.
2963
- if ( $this->is_premium() ) {
2964
- // Remove add-on download admin-notice.
2965
- $this->_parent->_admin_notices->remove_sticky( array(
2966
- 'addon_plan_upgraded_' . $this->_slug,
2967
- 'no_addon_license_' . $this->_slug,
2968
- ) );
2969
- }
2970
-
2971
- // $this->deactivate_premium_only_addon_without_license();
2972
- }
2973
- } else {
2974
- if ( $this->has_addons() &&
2975
- 'plugin-information' === fs_request_get( 'tab', false ) &&
2976
- $this->get_id() == fs_request_get( 'parent_plugin_id', false )
2977
- ) {
2978
- require_once WP_FS__DIR_INCLUDES . '/fs-plugin-info-dialog.php';
2979
-
2980
- new FS_Plugin_Info_Dialog( $this );
2981
- }
2982
- }
2983
-
2984
- add_action( 'admin_init', array( &$this, '_admin_init_action' ) );
2985
-
2986
- // if ( $this->is_registered() ||
2987
- // $this->is_anonymous() ||
2988
- // $this->is_pending_activation()
2989
- // ) {
2990
- // $this->_init_admin();
2991
- // }
2992
- }
2993
-
2994
- /**
2995
- * Should be called outside `$this->is_user_in_admin()` scope
2996
- * because the updater has some logic that needs to be executed
2997
- * during AJAX calls.
2998
- *
2999
- * Currently we need to hook to the `http_request_host_is_external` filter.
3000
- * In the future, there might be additional logic added.
3001
- *
3002
- * @author Vova Feldman
3003
- * @since 1.2.1.6
3004
- */
3005
- if ( $this->is_premium() && $this->has_release_on_freemius() ) {
3006
- new FS_Plugin_Updater( $this );
3007
- }
3008
-
3009
- $this->do_action( 'initiated' );
3010
-
3011
- if ( $this->_storage->prev_is_premium !== $this->_plugin->is_premium ) {
3012
- if ( isset( $this->_storage->prev_is_premium ) ) {
3013
- $this->apply_filters(
3014
- 'after_code_type_change',
3015
- // New code type.
3016
- $this->_plugin->is_premium
3017
- );
3018
- } else {
3019
- // Set for code type for the first time.
3020
- $this->_storage->prev_is_premium = $this->_plugin->is_premium;
3021
- }
3022
- }
3023
-
3024
- if ( ! $this->is_addon() ) {
3025
- if ( $this->is_registered() ) {
3026
- // Fix for upgrade from versions < 1.0.9.
3027
- if ( ! isset( $this->_storage->activation_timestamp ) ) {
3028
- $this->_storage->activation_timestamp = WP_FS__SCRIPT_START_TIME;
3029
- }
3030
-
3031
- $this->do_action( 'after_init_plugin_registered' );
3032
- } else if ( $this->is_anonymous() ) {
3033
- $this->do_action( 'after_init_plugin_anonymous' );
3034
- } else if ( $this->is_pending_activation() ) {
3035
- $this->do_action( 'after_init_plugin_pending_activations' );
3036
- }
3037
- } else {
3038
- if ( $this->is_registered() ) {
3039
- $this->do_action( 'after_init_addon_registered' );
3040
- } else if ( $this->is_anonymous() ) {
3041
- $this->do_action( 'after_init_addon_anonymous' );
3042
- } else if ( $this->is_pending_activation() ) {
3043
- $this->do_action( 'after_init_addon_pending_activations' );
3044
- }
3045
- }
3046
- }
3047
-
3048
- /**
3049
- * @author Leo Fajardo (@leorw)
3050
- *
3051
- * @since 1.2.1.5
3052
- */
3053
- function _stop_tracking_callback() {
3054
- $this->_logger->entrance();
3055
-
3056
- $this->check_ajax_referer( 'stop_tracking' );
3057
-
3058
- $result = $this->stop_tracking();
3059
-
3060
- if ( true === $result ) {
3061
- self::shoot_ajax_success();
3062
- }
3063
-
3064
- $this->_logger->api_error( $result );
3065
-
3066
- self::shoot_ajax_failure(
3067
- $this->get_text( 'unexpected-api-error' ) .
3068
- ( $this->is_api_error( $result ) && isset( $result->error ) ?
3069
- $result->error->message :
3070
- var_export( $result, true ) )
3071
- );
3072
- }
3073
-
3074
- /**
3075
- * @author Leo Fajardo (@leorw)
3076
- * @since 1.2.1.5
3077
- */
3078
- function _allow_tracking_callback() {
3079
- $this->_logger->entrance();
3080
-
3081
- $this->check_ajax_referer( 'allow_tracking' );
3082
-
3083
- $result = $this->allow_tracking();
3084
-
3085
- if ( true === $result ) {
3086
- self::shoot_ajax_success();
3087
- }
3088
-
3089
- $this->_logger->api_error( $result );
3090
-
3091
- self::shoot_ajax_failure(
3092
- $this->get_text( 'unexpected-api-error' ) .
3093
- ( $this->is_api_error( $result ) && isset( $result->error ) ?
3094
- $result->error->message :
3095
- var_export( $result, true ) )
3096
- );
3097
- }
3098
-
3099
- /**
3100
- * Opt-out from usage tracking.
3101
- *
3102
- * Note: This will not delete the account information but will stop all tracking.
3103
- *
3104
- * Returns:
3105
- * 1. FALSE - If the user never opted-in.
3106
- * 2. TRUE - If successfully opted-out.
3107
- * 3. object - API result on failure.
3108
- *
3109
- * @author Leo Fajardo (@leorw)
3110
- * @since 1.2.1.5
3111
- *
3112
- * @return bool|object
3113
- */
3114
- function stop_tracking() {
3115
- $this->_logger->entrance();
3116
-
3117
- if ( ! $this->is_registered() ) {
3118
- // User never opted-in.
3119
- return false;
3120
- }
3121
-
3122
- if ( $this->is_tracking_prohibited() ) {
3123
- // Already disconnected.
3124
- return true;
3125
- }
3126
-
3127
- // Send update to FS.
3128
- $result = $this->get_api_site_scope()->call( '/?fields=is_disconnected', 'put', array(
3129
- 'is_disconnected' => true
3130
- ) );
3131
-
3132
- if ( ! $this->is_api_result_entity( $result ) ||
3133
- ! isset( $result->is_disconnected ) ||
3134
- ! $result->is_disconnected
3135
- ) {
3136
- $this->_logger->api_error( $result );
3137
-
3138
- return $result;
3139
- }
3140
-
3141
- $this->_site->is_disconnected = $result->is_disconnected;
3142
- $this->_store_site();
3143
-
3144
- $this->clear_sync_cron();
3145
-
3146
- // Successfully disconnected.
3147
- return true;
3148
- }
3149
-
3150
- /**
3151
- * Opt-in back into usage tracking.
3152
- *
3153
- * Note: This will only work if the user opted-in previously.
3154
- *
3155
- * Returns:
3156
- * 1. FALSE - If the user never opted-in.
3157
- * 2. TRUE - If successfully opted-in back to usage tracking.
3158
- * 3. object - API result on failure.
3159
- *
3160
- * @author Leo Fajardo (@leorw)
3161
- * @since 1.2.1.5
3162
- *
3163
- * @return bool|object
3164
- */
3165
- function allow_tracking() {
3166
- $this->_logger->entrance();
3167
-
3168
- if ( ! $this->is_registered() ) {
3169
- // User never opted-in.
3170
- return false;
3171
- }
3172
-
3173
- if ( $this->is_tracking_allowed() ) {
3174
- // Tracking already allowed.
3175
- return true;
3176
- }
3177
-
3178
- $result = $this->get_api_site_scope()->call( '/?is_disconnected', 'put', array(
3179
- 'is_disconnected' => false
3180
- ) );
3181
-
3182
- if ( ! $this->is_api_result_entity( $result ) ||
3183
- ! isset( $result->is_disconnected ) ||
3184
- $result->is_disconnected
3185
- ) {
3186
- $this->_logger->api_error( $result );
3187
-
3188
- return $result;
3189
- }
3190
-
3191
- $this->_site->is_disconnected = $result->is_disconnected;
3192
- $this->_store_site();
3193
-
3194
- $this->schedule_sync_cron();
3195
-
3196
- // Successfully reconnected.
3197
- return true;
3198
- }
3199
-
3200
- /**
3201
- * If user opted-in and later disabled usage-tracking,
3202
- * re-allow tracking for licensing and updates.
3203
- *
3204
- * @author Leo Fajardo (@leorw)
3205
- *
3206
- * @since 1.2.1.5
3207
- */
3208
- private function reconnect_locally() {
3209
- $this->_logger->entrance();
3210
-
3211
- if ( $this->is_tracking_prohibited() &&
3212
- $this->is_registered()
3213
- ) {
3214
- $this->_site->is_disconnected = false;
3215
- $this->_store_site();
3216
- }
3217
- }
3218
-
3219
- /**
3220
- * Parse plugin's settings (as defined by the plugin dev).
3221
- *
3222
- * @author Vova Feldman (@svovaf)
3223
- * @since 1.1.7.3
3224
- *
3225
- * @param array $plugin_info
3226
- *
3227
- * @throws \Freemius_Exception
3228
- */
3229
- private function parse_settings( &$plugin_info ) {
3230
- $this->_logger->entrance();
3231
-
3232
- $id = $this->get_numeric_option( $plugin_info, 'id', false );
3233
- $public_key = $this->get_option( $plugin_info, 'public_key', false );
3234
- $secret_key = $this->get_option( $plugin_info, 'secret_key', null );
3235
- $parent_id = $this->get_numeric_option( $plugin_info, 'parent_id', null );
3236
- $parent_name = $this->get_option( $plugin_info, 'parent_name', null );
3237
-
3238
- /**
3239
- * @author Vova Feldman (@svovaf)
3240
- * @since 1.1.9 Try to pull secret key from external config.
3241
- */
3242
- if ( is_null( $secret_key ) && defined( "WP_FS__{$this->_slug}_SECRET_KEY" ) ) {
3243
- $secret_key = constant( "WP_FS__{$this->_slug}_SECRET_KEY" );
3244
- }
3245
-
3246
- if ( isset( $plugin_info['parent'] ) ) {
3247
- $parent_id = $this->get_numeric_option( $plugin_info['parent'], 'id', null );
3248
- // $parent_slug = $this->get_option( $plugin_info['parent'], 'slug', null );
3249
- // $parent_public_key = $this->get_option( $plugin_info['parent'], 'public_key', null );
3250
- // $parent_name = $this->get_option( $plugin_info['parent'], 'name', null );
3251
- }
3252
-
3253
- if ( false === $id ) {
3254
- throw new Freemius_Exception( array(
3255
- 'error' => array(
3256
- 'type' => 'ParameterNotSet',
3257
- 'message' => 'Plugin id parameter is not set.',
3258
- 'code' => 'plugin_id_not_set',
3259
- 'http' => 500,
3260
- )
3261
- ) );
3262
- }
3263
- if ( false === $public_key ) {
3264
- throw new Freemius_Exception( array(
3265
- 'error' => array(
3266
- 'type' => 'ParameterNotSet',
3267
- 'message' => 'Plugin public_key parameter is not set.',
3268
- 'code' => 'plugin_public_key_not_set',
3269
- 'http' => 500,
3270
- )
3271
- ) );
3272
- }
3273
-
3274
- $plugin = ( $this->_plugin instanceof FS_Plugin ) ?
3275
- $this->_plugin :
3276
- new FS_Plugin();
3277
-
3278
- $plugin->update( array(
3279
- 'id' => $id,
3280
- 'public_key' => $public_key,
3281
- 'slug' => $this->_slug,
3282
- 'parent_plugin_id' => $parent_id,
3283
- 'version' => $this->get_plugin_version(),
3284
- 'title' => $this->get_plugin_name(),
3285
- 'file' => $this->_plugin_basename,
3286
- 'is_premium' => $this->get_bool_option( $plugin_info, 'is_premium', true ),
3287
- 'is_live' => $this->get_bool_option( $plugin_info, 'is_live', true ),
3288
- 'type' => $this->_module_type,
3289
- // 'secret_key' => $secret_key,
3290
- ) );
3291
-
3292
- if ( $plugin->is_updated() ) {
3293
- // Update plugin details.
3294
- $this->_plugin = FS_Plugin_Manager::instance( $this->_module_id )->store( $plugin );
3295
- }
3296
- // Set the secret key after storing the plugin, we don't want to store the key in the storage.
3297
- $this->_plugin->secret_key = $secret_key;
3298
-
3299
- if ( ! isset( $plugin_info['menu'] ) ) {
3300
- $plugin_info['menu'] = array();
3301
-
3302
- if ( ! empty( $this->_storage->sdk_last_version ) &&
3303
- version_compare( $this->_storage->sdk_last_version, '1.1.2', '<=' )
3304
- ) {
3305
- // Backward compatibility to 1.1.2
3306
- $plugin_info['menu']['slug'] = isset( $plugin_info['menu_slug'] ) ?
3307
- $plugin_info['menu_slug'] :
3308
- $this->_slug;
3309
- }
3310
- }
3311
-
3312
- $this->_menu = FS_Admin_Menu_Manager::instance(
3313
- $this->_module_id,
3314
- $this->_module_type,
3315
- $this->get_unique_affix()
3316
- );
3317
-
3318
- $this->_menu->init( $plugin_info['menu'], $this->is_addon() );
3319
-
3320
- $this->_has_addons = $this->get_bool_option( $plugin_info, 'has_addons', false );
3321
- $this->_has_paid_plans = $this->get_bool_option( $plugin_info, 'has_paid_plans', true );
3322
- $this->_has_premium_version = $this->get_bool_option( $plugin_info, 'has_premium_version', $this->_has_paid_plans );
3323
- $this->_ignore_pending_mode = $this->get_bool_option( $plugin_info, 'ignore_pending_mode', false );
3324
- $this->_is_org_compliant = $this->get_bool_option( $plugin_info, 'is_org_compliant', true );
3325
- $this->_is_premium_only = $this->get_bool_option( $plugin_info, 'is_premium_only', false );
3326
- if ( $this->_is_premium_only ) {
3327
- // If premium only plugin, disable anonymous mode.
3328
- $this->_enable_anonymous = false;
3329
- $this->_anonymous_mode = false;
3330
- } else {
3331
- $this->_enable_anonymous = $this->get_bool_option( $plugin_info, 'enable_anonymous', true );
3332
- $this->_anonymous_mode = $this->get_bool_option( $plugin_info, 'anonymous_mode', false );
3333
- }
3334
- $this->_permissions = $this->get_option( $plugin_info, 'permissions', array() );
3335
-
3336
- if ( ! empty( $plugin_info['trial'] ) ) {
3337
- $this->_trial_days = $this->get_numeric_option(
3338
- $plugin_info['trial'],
3339
- 'days',
3340
- // Default to 0 - trial without days specification.
3341
- 0
3342
- );
3343
-
3344
- $this->_is_trial_require_payment = $this->get_bool_option( $plugin_info['trial'], 'is_require_payment', false );
3345
- }
3346
- }
3347
-
3348
- /**
3349
- * @param string[] $options
3350
- * @param string $key
3351
- * @param mixed $default
3352
- *
3353
- * @return bool
3354
- */
3355
- private function get_option( &$options, $key, $default = false ) {
3356
- return ! empty( $options[ $key ] ) ? $options[ $key ] : $default;
3357
- }
3358
-
3359
- private function get_bool_option( &$options, $key, $default = false ) {
3360
- return isset( $options[ $key ] ) && is_bool( $options[ $key ] ) ? $options[ $key ] : $default;
3361
- }
3362
-
3363
- private function get_numeric_option( &$options, $key, $default = false ) {
3364
- return isset( $options[ $key ] ) && is_numeric( $options[ $key ] ) ? $options[ $key ] : $default;
3365
- }
3366
-
3367
- /**
3368
- * Gate keeper.
3369
- *
3370
- * @author Vova Feldman (@svovaf)
3371
- * @since 1.1.7.3
3372
- *
3373
- * @return bool
3374
- */
3375
- private function should_stop_execution() {
3376
- if ( empty( $this->_storage->was_plugin_loaded ) ) {
3377
- /**
3378
- * Don't execute Freemius until plugin was fully loaded at least once,
3379
- * to give the opportunity for the activation hook to run before pinging
3380
- * the API for connectivity test. This logic is relevant for the
3381
- * identification of new plugin install vs. plugin update.
3382
- *
3383
- * @author Vova Feldman (@svovaf)
3384
- * @since 1.1.9
3385
- */
3386
- return true;
3387
- }
3388
-
3389
- if ( $this->is_activation_mode() ) {
3390
- if ( ! is_admin() ) {
3391
- /**
3392
- * If in activation mode, don't execute Freemius outside of the
3393
- * admin dashboard.
3394
- *
3395
- * @author Vova Feldman (@svovaf)
3396
- * @since 1.1.7.3
3397
- */
3398
- return true;
3399
- }
3400
-
3401
- if ( ! WP_FS__IS_HTTP_REQUEST ) {
3402
- /**
3403
- * If in activation and executed without HTTP context (e.g. CLI, Cronjob),
3404
- * then don't start Freemius.
3405
- *
3406
- * @author Vova Feldman (@svovaf)
3407
- * @since 1.1.6.3
3408
- *
3409
- * @link https://wordpress.org/support/topic/errors-in-the-freemius-class-when-running-in-wordpress-in-cli
3410
- */
3411
- return true;
3412
- }
3413
-
3414
- if ( self::is_cron() ) {
3415
- /**
3416
- * If in activation mode, don't execute Freemius during wp crons
3417
- * (wp crons have HTTP context - called as HTTP request).
3418
- *
3419
- * @author Vova Feldman (@svovaf)
3420
- * @since 1.1.7.3
3421
- */
3422
- return true;
3423
- }
3424
-
3425
- if ( self::is_ajax() &&
3426
- ! $this->_admin_notices->has_sticky( 'failed_connect_api_first' ) &&
3427
- ! $this->_admin_notices->has_sticky( 'failed_connect_api' )
3428
- ) {
3429
- /**
3430
- * During activation, if running in AJAX mode, unless there's a sticky
3431
- * connectivity issue notice, don't run Freemius.
3432
- *
3433
- * @author Vova Feldman (@svovaf)
3434
- * @since 1.1.7.3
3435
- */
3436
- return true;
3437
- }
3438
- }
3439
-
3440
- return false;
3441
- }
3442
-
3443
- /**
3444
- * Triggered after code type has changed.
3445
- *
3446
- * @author Vova Feldman (@svovaf)
3447
- * @since 1.1.9.1
3448
- */
3449
- function _after_code_type_change() {
3450
- $this->_logger->entrance();
3451
-
3452
- if ( $this->is_theme() ) {
3453
- // Expire the cache of the previous tabs since the theme may
3454
- // have setting updates after code type has changed.
3455
- $this->_cache->expire( 'tabs' );
3456
- $this->_cache->expire( 'tabs_stylesheets' );
3457
- }
3458
-
3459
- if ( $this->is_registered() ) {
3460
- if ( ! $this->is_addon() ) {
3461
- add_action(
3462
- is_admin() ? 'admin_init' : 'init',
3463
- array( &$this, '_plugin_code_type_changed' )
3464
- );
3465
- }
3466
-
3467
- if ( $this->is_premium() ) {
3468
- // Purge cached payments after switching to the premium version.
3469
- // @todo This logic doesn't handle purging the cache for serviceware module upgrade.
3470
- $this->get_api_user_scope()->purge_cache( "/plugins/{$this->_module_id}/payments.json?include_addons=true" );
3471
- }
3472
- }
3473
- }
3474
-
3475
- /**
3476
- * Handles plugin's code type change (free <--> premium).
3477
- *
3478
- * @author Vova Feldman (@svovaf)
3479
- * @since 1.0.9
3480
- */
3481
- function _plugin_code_type_changed() {
3482
- $this->_logger->entrance();
3483
-
3484
- if ( $this->is_premium() ) {
3485
- $this->reconnect_locally();
3486
-
3487
- // Activated premium code.
3488
- $this->do_action( 'after_premium_version_activation' );
3489
-
3490
- // Remove all sticky messages related to download of the premium version.
3491
- $this->_admin_notices->remove_sticky( array(
3492
- 'trial_started',
3493
- 'plan_upgraded',
3494
- 'plan_changed',
3495
- 'license_activated',
3496
- ) );
3497
-
3498
- $this->_admin_notices->add_sticky(
3499
- sprintf( $this->get_text( 'premium-activated-message' ), $this->_module_type ),
3500
- 'premium_activated',
3501
- $this->get_text( 'woot' ) . '!'
3502
- );
3503
- } else {
3504
- // Remove sticky message related to premium code activation.
3505
- $this->_admin_notices->remove_sticky( 'premium_activated' );
3506
-
3507
- // Activated free code (after had the premium before).
3508
- $this->do_action( 'after_free_version_reactivation' );
3509
-
3510
- if ( $this->is_paying() && ! $this->is_premium() ) {
3511
- $this->_admin_notices->add_sticky(
3512
- sprintf(
3513
- $this->get_text( 'you-have-x-license' ),
3514
- $this->_site->plan->title
3515
- ) . $this->get_complete_upgrade_instructions(),
3516
- 'plan_upgraded',
3517
- $this->get_text( 'yee-haw' ) . '!'
3518
- );
3519
- }
3520
- }
3521
-
3522
- // Schedule code type changes event.
3523
- $this->schedule_install_sync();
3524
-
3525
- /**
3526
- * Unregister the uninstall hook for the other version of the plugin (with different code type) to avoid
3527
- * triggering a fatal error when uninstalling that plugin. For example, after deactivating the "free" version
3528
- * of a specific plugin, its uninstall hook should be unregistered after the "premium" version has been
3529
- * activated. If we don't do that, a fatal error will occur when we try to uninstall the "free" version since
3530
- * the main file of the "free" version will be loaded first before calling the hooked callback. Since the
3531
- * free and premium versions are almost identical (same class or have same functions), a fatal error like
3532
- * "Cannot redeclare class MyClass" or "Cannot redeclare my_function()" will occur.
3533
- */
3534
- $this->unregister_uninstall_hook();
3535
-
3536
- $this->clear_module_main_file_cache();
3537
-
3538
- // Update is_premium of latest version.
3539
- $this->_storage->prev_is_premium = $this->_plugin->is_premium;
3540
- }
3541
-
3542
- #endregion
3543
-
3544
- #----------------------------------------------------------------------------------
3545
- #region Add-ons
3546
- #----------------------------------------------------------------------------------
3547
-
3548
- /**
3549
- * Check if add-on installed and activated on site.
3550
- *
3551
- * @author Vova Feldman (@svovaf)
3552
- * @since 1.0.6
3553
- *
3554
- * @param string|number $id_or_slug
3555
- * @param bool|null $is_premium Since 1.2.1.7 can check for specified add-on version.
3556
- *
3557
- * @return bool
3558
- */
3559
- function is_addon_activated( $id_or_slug, $is_premium = null ) {
3560
- $this->_logger->entrance();
3561
-
3562
- $addon_id = self::get_module_id( $id_or_slug );
3563
- $is_activated = self::has_instance( $addon_id );
3564
-
3565
- if ( ! $is_activated ) {
3566
- return false;
3567
- }
3568
-
3569
- if ( is_bool( $is_premium ) ) {
3570
- // Check if the specified code version is activate.
3571
- $addon = $this->get_addon_instance( $addon_id );
3572
- $is_activated = ( $is_premium === $addon->is_premium() );
3573
- }
3574
-
3575
- return $is_activated;
3576
- }
3577
-
3578
- /**
3579
- * Check if add-on was connected to install
3580
- *
3581
- * @author Vova Feldman (@svovaf)
3582
- * @since 1.1.7
3583
- *
3584
- * @param string|number $id_or_slug
3585
- *
3586
- * @return bool
3587
- */
3588
- function is_addon_connected( $id_or_slug ) {
3589
- $this->_logger->entrance();
3590
-
3591
- $sites = self::get_all_sites( WP_FS__MODULE_TYPE_PLUGIN );
3592
-
3593
- $addon_id = self::get_module_id( $id_or_slug );
3594
- $addon = $this->get_addon( $addon_id );
3595
- $slug = $addon->slug;
3596
- if ( ! isset( $sites[ $slug ] ) ) {
3597
- return false;
3598
- }
3599
-
3600
- $site = $sites[ $slug ];
3601
-
3602
- $plugin = FS_Plugin_Manager::instance( $addon_id )->get();
3603
-
3604
- if ( $plugin->parent_plugin_id != $this->_plugin->id ) {
3605
- // The given slug do NOT belong to any of the plugin's add-ons.
3606
- return false;
3607
- }
3608
-
3609
- return ( is_object( $site ) &&
3610
- is_numeric( $site->id ) &&
3611
- is_numeric( $site->user_id ) &&
3612
- is_object( $site->plan )
3613
- );
3614
- }
3615
-
3616
- /**
3617
- * Determines if add-on installed.
3618
- *
3619
- * NOTE: This is a heuristic and only works if the folder/file named as the slug.
3620
- *
3621
- * @author Vova Feldman (@svovaf)
3622
- * @since 1.0.6
3623
- *
3624
- * @param string|number $id_or_slug
3625
- *
3626
- * @return bool
3627
- */
3628
- function is_addon_installed( $id_or_slug ) {
3629
- $this->_logger->entrance();
3630
-
3631
- $addon_id = self::get_module_id( $id_or_slug );
3632
-
3633
- return file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $this->get_addon_basename( $addon_id ) ) );
3634
- }
3635
-
3636
- /**
3637
- * Get add-on basename.
3638
- *
3639
- * @author Vova Feldman (@svovaf)
3640
- * @since 1.0.6
3641
- *
3642
- * @param string|number $id_or_slug
3643
- *
3644
- * @return string
3645
- */
3646
- function get_addon_basename( $id_or_slug ) {
3647
- $addon_id = self::get_module_id( $id_or_slug );
3648
-
3649
- if ( $this->is_addon_activated( $addon_id ) ) {
3650
- return self::instance( $addon_id )->get_plugin_basename();
3651
- }
3652
-
3653
- $addon = $this->get_addon( $addon_id );
3654
- $premium_basename = "{$addon->slug}-premium/{$addon->slug}.php";
3655
-
3656
- if ( file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $premium_basename ) ) ) {
3657
- return $premium_basename;
3658
- }
3659
-
3660
- $all_plugins = $this->get_all_plugins();
3661
-
3662
- foreach ( $all_plugins as $basename => &$data ) {
3663
- if ( $addon->slug === $data['slug'] ||
3664
- $addon->slug . '-premium' === $data['slug']
3665
- ) {
3666
- return $basename;
3667
- }
3668
- }
3669
-
3670
- $free_basename = "{$addon->slug}/{$addon->slug}.php";
3671
-
3672
- return $free_basename;
3673
- }
3674
-
3675
- /**
3676
- * Get installed add-ons instances.
3677
- *
3678
- * @author Vova Feldman (@svovaf)
3679
- * @since 1.0.6
3680
- *
3681
- * @return Freemius[]
3682
- */
3683
- function get_installed_addons() {
3684
- $installed_addons = array();
3685
- foreach ( self::$_instances as $instance ) {
3686
- if ( $instance->is_addon() && is_object( $instance->_parent_plugin ) ) {
3687
- if ( $this->_plugin->id == $instance->_parent_plugin->id ) {
3688
- $installed_addons[] = $instance;
3689
- }
3690
- }
3691
- }
3692
-
3693
- return $installed_addons;
3694
- }
3695
-
3696
- /**
3697
- * Check if any add-ons of the plugin are installed.
3698
- *
3699
- * @author Leo Fajardo (@leorw)
3700
- * @since 1.1.1
3701
- *
3702
- * @return bool
3703
- */
3704
- function has_installed_addons() {
3705
- if ( ! $this->has_addons() ) {
3706
- return false;
3707
- }
3708
-
3709
- foreach ( self::$_instances as $instance ) {
3710
- if ( $instance->is_addon() && is_object( $instance->_parent_plugin ) ) {
3711
- if ( $this->_plugin->id == $instance->_parent_plugin->id ) {
3712
- return true;
3713
- }
3714
- }
3715
- }
3716
-
3717
- return false;
3718
- }
3719
-
3720
- /**
3721
- * Tell Freemius that the current plugin is an add-on.
3722
- *
3723
- * @author Vova Feldman (@svovaf)
3724
- * @since 1.0.6
3725
- *
3726
- * @param number $parent_plugin_id The parent plugin ID
3727
- */
3728
- function init_addon( $parent_plugin_id ) {
3729
- $this->_plugin->parent_plugin_id = $parent_plugin_id;
3730
- }
3731
-
3732
- /**
3733
- * @author Vova Feldman (@svovaf)
3734
- * @since 1.0.6
3735
- *
3736
- * @return bool
3737
- */
3738
- function is_addon() {
3739
- return isset( $this->_plugin->parent_plugin_id ) && is_numeric( $this->_plugin->parent_plugin_id );
3740
- }
3741
-
3742
- /**
3743
- * Deactivate add-on if it's premium only and the user does't have a valid license.
3744
- *
3745
- * @param bool $is_after_trial_cancel
3746
- *
3747
- * @return bool If add-on was deactivated.
3748
- */
3749
- private function deactivate_premium_only_addon_without_license( $is_after_trial_cancel = false ) {
3750
- if ( ! $this->has_free_plan() &&
3751
- ! $this->has_features_enabled_license() &&
3752
- ! $this->_has_premium_license()
3753
- ) {
3754
- if ( $this->is_registered() ) {
3755
- // IF wrapper is turned off because activation_timestamp is currently only stored for plugins (not addons).
3756
- // if (empty($this->_storage->activation_timestamp) ||
3757
- // (WP_FS__SCRIPT_START_TIME - $this->_storage->activation_timestamp) > 30
3758
- // ) {
3759
- /**
3760
- * @todo When it's first fail, there's no reason to try and re-sync because the licenses were just synced after initial activation.
3761
- *
3762
- * Retry syncing the user add-on licenses.
3763
- */
3764
- // Sync licenses.
3765
- $this->_sync_licenses();
3766
- // }
3767
-
3768
- // Try to activate premium license.
3769
- $this->_activate_license( true );
3770
- }
3771
-
3772
- if ( ! $this->has_free_plan() &&
3773
- ! $this->has_features_enabled_license() &&
3774
- ! $this->_has_premium_license()
3775
- ) {
3776
- // @todo Check if deactivate plugins also call the deactivation hook.
3777
-
3778
- $this->_parent->_admin_notices->add_sticky(
3779
- sprintf(
3780
- $this->_parent->get_text( $is_after_trial_cancel ?
3781
- 'addon-trial-cancelled-message' :
3782
- 'addon-no-license-message'
3783
- ),
3784
- '<b>' . $this->_plugin->title . '</b>'
3785
- ) . ' ' . sprintf(
3786
- '<a href="%s" aria-label="%s" class="button button-primary" style="margin-left: 10px; vertical-align: middle;">%s &nbsp;&#10140;</a>',
3787
- $this->_parent->addon_url( $this->_slug ),
3788
- esc_attr( sprintf( $this->_parent->get_text( 'more-information-about-x' ), $this->_plugin->title ) ),
3789
- $this->_parent->get_text( 'purchase-license' )
3790
- ),
3791
- 'no_addon_license_' . $this->_slug,
3792
- ( $is_after_trial_cancel ? '' : $this->_parent->get_text( 'oops' ) . '...' ),
3793
- ( $is_after_trial_cancel ? 'success' : 'error' )
3794
- );
3795
-
3796
- deactivate_plugins( array( $this->_plugin_basename ), true );
3797
-
3798
- return true;
3799
- }
3800
- }
3801
-
3802
- return false;
3803
- }
3804
-
3805
- #endregion
3806
-
3807
- #----------------------------------------------------------------------------------
3808
- #region Sandbox
3809
- #----------------------------------------------------------------------------------
3810
-
3811
- /**
3812
- * Set Freemius into sandbox mode for debugging.
3813
- *
3814
- * @author Vova Feldman (@svovaf)
3815
- * @since 1.0.4
3816
- *
3817
- * @param string $secret_key
3818
- */
3819
- function init_sandbox( $secret_key ) {
3820
- $this->_plugin->secret_key = $secret_key;
3821
-
3822
- // Update plugin details.
3823
- FS_Plugin_Manager::instance( $this->_module_id )->update( $this->_plugin, true );
3824
- }
3825
-
3826
- /**
3827
- * Check if running payments in sandbox mode.
3828
- *
3829
- * @author Vova Feldman (@svovaf)
3830
- * @since 1.0.4
3831
- *
3832
- * @return bool
3833
- */
3834
- function is_payments_sandbox() {
3835
- return ( ! $this->is_live() ) || isset( $this->_plugin->secret_key );
3836
- }
3837
-
3838
- #endregion
3839
-
3840
- /**
3841
- * Check if running test vs. live plugin.
3842
- *
3843
- * @author Vova Feldman (@svovaf)
3844
- * @since 1.0.5
3845
- *
3846
- * @return bool
3847
- */
3848
- function is_live() {
3849
- return $this->_plugin->is_live;
3850
- }
3851
-
3852
- /**
3853
- * Check if the user skipped connecting the account with Freemius.
3854
- *
3855
- * @author Vova Feldman (@svovaf)
3856
- * @since 1.0.7
3857
- *
3858
- * @return bool
3859
- */
3860
- function is_anonymous() {
3861
- if ( ! isset( $this->_is_anonymous ) ) {
3862
- if ( ! isset( $this->_storage->is_anonymous ) ) {
3863
- // Not skipped.
3864
- $this->_is_anonymous = false;
3865
- } else if ( is_bool( $this->_storage->is_anonymous ) ) {
3866
- // For back compatibility, since the variable was boolean before.
3867
- $this->_is_anonymous = $this->_storage->is_anonymous;
3868
-
3869
- // Upgrade stored data format to 1.1.3 format.
3870
- $this->set_anonymous_mode( $this->_storage->is_anonymous );
3871
- } else {
3872
- // Version 1.1.3 and later.
3873
- $this->_is_anonymous = $this->_storage->is_anonymous['is'];
3874
- }
3875
- }
3876
-
3877
- return $this->_is_anonymous;
3878
- }
3879
-
3880
- /**
3881
- * Check if user connected his account and install pending email activation.
3882
- *
3883
- * @author Vova Feldman (@svovaf)
3884
- * @since 1.0.7
3885
- *
3886
- * @return bool
3887
- */
3888
- function is_pending_activation() {
3889
- return $this->_storage->get( 'is_pending_activation', false );
3890
- }
3891
-
3892
- /**
3893
- * Check if plugin must be WordPress.org compliant.
3894
- *
3895
- * @since 1.0.7
3896
- *
3897
- * @return bool
3898
- */
3899
- function is_org_repo_compliant() {
3900
- return $this->_is_org_compliant;
3901
- }
3902
-
3903
- #----------------------------------------------------------------------------------
3904
- #region Daily Sync Cron
3905
- #----------------------------------------------------------------------------------
3906
-
3907
- /**
3908
- * @author Vova Feldman (@svovaf)
3909
- * @since 1.1.7.3
3910
- */
3911
- private function run_manual_sync() {
3912
- self::require_pluggable_essentials();
3913
-
3914
- if ( ! $this->is_user_admin() ) {
3915
- return;
3916
- }
3917
-
3918
- // Run manual sync.
3919
- $this->_sync_cron();
3920
-
3921
- // Reschedule next cron to run 24 hours from now (performance optimization).
3922
- $this->clear_sync_cron();
3923
-
3924
- $this->schedule_sync_cron( time() + WP_FS__TIME_24_HOURS_IN_SEC, false );
3925
- }
3926
-
3927
- /**
3928
- * Data sync cron job. Replaces the background sync non blocking HTTP request
3929
- * that doesn't halt page loading.
3930
- *
3931
- * @author Vova Feldman (@svovaf)
3932
- * @since 1.1.7.3
3933
- */
3934
- function _sync_cron() {
3935
- $this->_logger->entrance();
3936
-
3937
- // Store the last time data sync was executed.
3938
- $this->_storage->sync_timestamp = time();
3939
-
3940
- // Check if API is temporary down.
3941
- if ( FS_Api::is_temporary_down() ) {
3942
- return;
3943
- }
3944
-
3945
- // @todo Add logic that identifies API latency, and reschedule the next background sync randomly between 8-16 hours.
3946
-
3947
- if ( $this->is_registered() ) {
3948
- if ( $this->has_paid_plan() ) {
3949
- // Initiate background plan sync.
3950
- $this->_sync_license( true );
3951
-
3952
- if ( $this->is_paying() ) {
3953
- // Check for premium plugin updates.
3954
- $this->check_updates( true );
3955
- }
3956
- } else {
3957
- // Sync install (only if something changed locally).
3958
- $this->sync_install();
3959
- }
3960
- }
3961
-
3962
- $this->do_action( 'after_sync_cron' );
3963
- }
3964
-
3965
- /**
3966
- * Check if sync was executed in the last $period of seconds.
3967
- *
3968
- * @author Vova Feldman (@svovaf)
3969
- * @since 1.1.7.3
3970
- *
3971
- * @param int $period In seconds
3972
- *
3973
- * @return bool
3974
- */
3975
- private function is_sync_executed( $period = WP_FS__TIME_24_HOURS_IN_SEC ) {
3976
- if ( ! isset( $this->_storage->sync_timestamp ) ) {
3977
- return false;
3978
- }
3979
-
3980
- return ( $this->_storage->sync_timestamp > ( WP_FS__SCRIPT_START_TIME - $period ) );
3981
- }
3982
-
3983
- /**
3984
- * @author Vova Feldman (@svovaf)
3985
- * @since 1.1.7.3
3986
- *
3987
- * @return bool
3988
- */
3989
- private function is_sync_cron_on() {
3990
- /**
3991
- * @var object $sync_cron_data
3992
- */
3993
- $sync_cron_data = $this->_storage->get( 'sync_cron', null );
3994
-
3995
- return ( ! is_null( $sync_cron_data ) && true === $sync_cron_data->on );
3996
- }
3997
-
3998
- /**
3999
- * @author Vova Feldman (@svovaf)
4000
- * @since 1.1.7.3
4001
- *
4002
- * @param int $start_at Defaults to now.
4003
- * @param bool $randomize_start If true, schedule first job randomly during the next 12 hours. Otherwise,
4004
- * schedule job to start right away.
4005
- */
4006
- private function schedule_sync_cron( $start_at = WP_FS__SCRIPT_START_TIME, $randomize_start = true ) {
4007
- $this->_logger->entrance();
4008
-
4009
- if ( $randomize_start ) {
4010
- // Schedule first sync with a random 12 hour time range from now.
4011
- $start_at += rand( 0, ( WP_FS__TIME_24_HOURS_IN_SEC / 2 ) );
4012
- }
4013
-
4014
- // Schedule daily WP cron.
4015
- wp_schedule_event(
4016
- $start_at,
4017
- 'daily',
4018
- $this->get_action_tag( 'data_sync' )
4019
- );
4020
-
4021
- $this->_storage->store( 'sync_cron', (object) array(
4022
- 'version' => $this->get_plugin_version(),
4023
- 'sdk_version' => $this->version,
4024
- 'timestamp' => WP_FS__SCRIPT_START_TIME,
4025
- 'on' => true,
4026
- ) );
4027
- }
4028
-
4029
- /**
4030
- * Add the actual sync function to the cron job hook.
4031
- *
4032
- * @author Vova Feldman (@svovaf)
4033
- * @since 1.1.7.3
4034
- */
4035
- private function hook_callback_to_sync_cron() {
4036
- $this->add_action( 'data_sync', array( &$this, '_sync_cron' ) );
4037
- }
4038
-
4039
- /**
4040
- * @author Vova Feldman (@svovaf)
4041
- * @since 1.1.7.3
4042
- */
4043
- private function clear_sync_cron() {
4044
- $this->_logger->entrance();
4045
-
4046
- if ( ! $this->is_sync_cron_on() ) {
4047
- return;
4048
- }
4049
-
4050
- $this->_storage->remove( 'sync_cron' );
4051
-
4052
- wp_clear_scheduled_hook( $this->get_action_tag( 'data_sync' ) );
4053
- }
4054
-
4055
- /**
4056
- * Unix timestamp for next sync cron execution or false if not scheduled.
4057
- *
4058
- * @author Vova Feldman (@svovaf)
4059
- * @since 1.1.7.3
4060
- *
4061
- * @return int|false
4062
- */
4063
- function next_sync_cron() {
4064
- $this->_logger->entrance();
4065
-
4066
- if ( ! $this->is_sync_cron_on() ) {
4067
- return false;
4068
- }
4069
-
4070
- return wp_next_scheduled( $this->get_action_tag( 'data_sync' ) );
4071
- }
4072
-
4073
- /**
4074
- * Unix timestamp for previous sync cron execution or false if never executed.
4075
- *
4076
- * @author Vova Feldman (@svovaf)
4077
- * @since 1.1.7.3
4078
- *
4079
- * @return int|false
4080
- */
4081
- function last_sync_cron() {
4082
- $this->_logger->entrance();
4083
-
4084
- return $this->_storage->get( 'sync_timestamp' );
4085
- }
4086
-
4087
- #endregion Daily Sync Cron ------------------------------------------------------------------
4088
-
4089
- #----------------------------------------------------------------------------------
4090
- #region Async Install Sync
4091
- #----------------------------------------------------------------------------------
4092
-
4093
- /**
4094
- * @author Vova Feldman (@svovaf)
4095
- * @since 1.1.7.3
4096
- *
4097
- * @return bool
4098
- */
4099
- private function is_install_sync_scheduled() {
4100
- /**
4101
- * @var object $cron_data
4102
- */
4103
- $cron_data = $this->_storage->get( 'install_sync_cron', null );
4104
-
4105
- return ( ! is_null( $cron_data ) && true === $cron_data->on );
4106
- }
4107
-
4108
- /**
4109
- * Instead of running blocking install sync event, execute non blocking scheduled wp-cron.
4110
- *
4111
- * @author Vova Feldman (@svovaf)
4112
- * @since 1.1.7.3
4113
- */
4114
- private function schedule_install_sync() {
4115
- $this->_logger->entrance();
4116
-
4117
- $this->clear_install_sync_cron();
4118
-
4119
- // Schedule immediate install sync.
4120
- wp_schedule_single_event(
4121
- WP_FS__SCRIPT_START_TIME,
4122
- $this->get_action_tag( 'install_sync' )
4123
- );
4124
-
4125
- $this->_storage->store( 'install_sync_cron', (object) array(
4126
- 'version' => $this->get_plugin_version(),
4127
- 'sdk_version' => $this->version,
4128
- 'timestamp' => WP_FS__SCRIPT_START_TIME,
4129
- 'on' => true,
4130
- ) );
4131
- }
4132
-
4133
- /**
4134
- * Unix timestamp for previous install sync cron execution or false if never executed.
4135
- *
4136
- * @todo There's some very strange bug that $this->_storage->install_sync_timestamp value is not being
4137
- * updated. But for sure the sync event is working.
4138
- *
4139
- * @author Vova Feldman (@svovaf)
4140
- * @since 1.1.7.3
4141
- *
4142
- * @return int|false
4143
- */
4144
- function last_install_sync() {
4145
- $this->_logger->entrance();
4146
-
4147
- return $this->_storage->get( 'install_sync_timestamp' );
4148
- }
4149
-
4150
- /**
4151
- * Unix timestamp for next install sync cron execution or false if not scheduled.
4152
- *
4153
- * @author Vova Feldman (@svovaf)
4154
- * @since 1.1.7.3
4155
- *
4156
- * @return int|false
4157
- */
4158
- function next_install_sync() {
4159
- $this->_logger->entrance();
4160
-
4161
- if ( ! $this->is_install_sync_scheduled() ) {
4162
- return false;
4163
- }
4164
-
4165
- return wp_next_scheduled( $this->get_action_tag( 'install_sync' ) );
4166
- }
4167
-
4168
- /**
4169
- * Add the actual install sync function to the cron job hook.
4170
- *
4171
- * @author Vova Feldman (@svovaf)
4172
- * @since 1.1.7.3
4173
- */
4174
- private function hook_callback_to_install_sync() {
4175
- $this->add_action( 'install_sync', array( &$this, '_run_sync_install' ) );
4176
- }
4177
-
4178
- /**
4179
- * @author Vova Feldman (@svovaf)
4180
- * @since 1.1.7.3
4181
- */
4182
- private function clear_install_sync_cron() {
4183
- $this->_logger->entrance();
4184
-
4185
- if ( ! $this->is_install_sync_scheduled() ) {
4186
- return;
4187
- }
4188
-
4189
- $this->_storage->remove( 'install_sync_cron' );
4190
-
4191
- wp_clear_scheduled_hook( $this->get_action_tag( 'install_sync' ) );
4192
- }
4193
-
4194
- /**
4195
- * @author Vova Feldman (@svovaf)
4196
- * @since 1.1.7.3
4197
- */
4198
- public function _run_sync_install() {
4199
- $this->_logger->entrance();
4200
-
4201
- // Update last install sync timestamp.
4202
- $this->_storage->install_sync_timestamp = time();
4203
-
4204
- $this->sync_install( array(), true );
4205
- }
4206
-
4207
- #endregion Async Install Sync ------------------------------------------------------------------
4208
-
4209
- /**
4210
- * Show a notice that activation is currently pending.
4211
- *
4212
- * @author Vova Feldman (@svovaf)
4213
- * @since 1.0.7
4214
- *
4215
- * @param bool|string $email
4216
- * @param bool $is_pending_trial Since 1.2.1.5
4217
- */
4218
- function _add_pending_activation_notice( $email = false, $is_pending_trial = false ) {
4219
- if ( ! is_string( $email ) ) {
4220
- $current_user = self::_get_current_wp_user();
4221
- $email = $current_user->user_email;
4222
- }
4223
-
4224
- $this->_admin_notices->add_sticky(
4225
- sprintf(
4226
- $this->get_text( 'pending-activation-message' ),
4227
- '<b>' . $this->get_plugin_name() . '</b>',
4228
- '<b>' . $email . '</b>',
4229
- $this->get_text( $is_pending_trial ? 'start-the-trial' : 'complete-the-install' )
4230
- ),
4231
- 'activation_pending',
4232
- 'Thanks!'
4233
- );
4234
- }
4235
-
4236
- /**
4237
- * Check if currently in plugin activation.
4238
- *
4239
- * @author Vova Feldman (@svovaf)
4240
- * @since 1.1.4
4241
- *
4242
- * @return bool
4243
- */
4244
- function is_plugin_activation() {
4245
- return get_option( 'fs_'
4246
- . ( $this->is_plugin() ? '' : $this->_module_type . '_' )
4247
- . "{$this->_slug}_activated", false );
4248
- }
4249
-
4250
- /**
4251
- *
4252
- * NOTE: admin_menu action executed before admin_init.
4253
- *
4254
- * @author Vova Feldman (@svovaf)
4255
- * @since 1.0.7
4256
- */
4257
- function _admin_init_action() {
4258
- /**
4259
- * Automatically redirect to connect/activation page after plugin activation.
4260
- *
4261
- * @since 1.1.7 Do NOT redirect to opt-in when running in network admin mode.
4262
- */
4263
- if ( $this->is_plugin_activation() ) {
4264
- delete_option( 'fs_'
4265
- . ( $this->is_plugin() ? '' : $this->_module_type . '_' )
4266
- . "{$this->_slug}_activated" );
4267
-
4268
- if ( ! function_exists( 'is_network_admin' ) || ! is_network_admin() ) {
4269
- $this->_redirect_on_activation_hook();
4270
-
4271
- return;
4272
- }
4273
- }
4274
-
4275
- if ( fs_request_is_action( $this->get_unique_affix() . '_skip_activation' ) ) {
4276
- check_admin_referer( $this->get_unique_affix() . '_skip_activation' );
4277
-
4278
- $this->skip_connection();
4279
-
4280
- fs_redirect( $this->get_after_activation_url( 'after_skip_url' ) );
4281
- }
4282
-
4283
- if ( ! $this->is_addon() && ! $this->is_registered() && ! $this->is_anonymous() ) {
4284
- if ( ! $this->is_pending_activation() ) {
4285
- if ( ! $this->_menu->is_main_settings_page() ) {
4286
- /**
4287
- * If a user visits any other admin page before activating the premium-only theme with a valid
4288
- * license, reactivate the previous theme.
4289
- *
4290
- * @author Leo Fajardo (@leorw)
4291
- * @since 1.2.2
4292
- */
4293
- if ( $this->is_theme()
4294
- && $this->is_only_premium()
4295
- && ! $this->has_settings_menu()
4296
- && ! isset( $_REQUEST['fs_action'] )
4297
- && $this->can_activate_previous_theme()
4298
- ) {
4299
- $this->activate_previous_theme();
4300
-
4301
- return;
4302
- }
4303
-
4304
- if ( $this->is_plugin_new_install() || $this->is_only_premium() ) {
4305
- // Show notice for new plugin installations.
4306
- $this->_admin_notices->add(
4307
- sprintf(
4308
- $this->get_text( 'you-are-step-away' ),
4309
- sprintf( '<b><a href="%s">%s</a></b>',
4310
- $this->get_activation_url(),
4311
- sprintf( $this->get_text( 'activate-x-now' ), $this->get_plugin_name() )
4312
- )
4313
- ),
4314
- '',
4315
- 'update-nag'
4316
- );
4317
- } else {
4318
- if ( ! isset( $this->_storage->sticky_optin_added ) ) {
4319
- $this->_storage->sticky_optin_added = true;
4320
-
4321
- // Show notice for new plugin installations.
4322
- $this->_admin_notices->add_sticky(
4323
- sprintf(
4324
- $this->get_text( 'few-plugin-tweaks' ),
4325
- $this->_module_type,
4326
- sprintf( '<b><a href="%s">%s</a></b>',
4327
- $this->get_activation_url(),
4328
- sprintf( $this->get_text( 'optin-x-now' ), $this->get_plugin_name() )
4329
- )
4330
- ),
4331
- 'connect_account',
4332
- '',
4333
- 'update-nag'
4334
- );
4335
- }
4336
-
4337
- if ( $this->has_filter( 'optin_pointer_element' ) ) {
4338
- // Don't show admin nag if plugin update.
4339
- wp_enqueue_script( 'wp-pointer' );
4340
- wp_enqueue_style( 'wp-pointer' );
4341
-
4342
- $this->_enqueue_connect_essentials();
4343
-
4344
- add_action( 'admin_print_footer_scripts', array(
4345
- $this,
4346
- '_add_connect_pointer_script'
4347
- ) );
4348
- }
4349
- }
4350
- }
4351
- }
4352
-
4353
- if ( $this->is_theme() &&
4354
- $this->_menu->is_main_settings_page()
4355
- ) {
4356
- $this->_show_theme_activation_optin_dialog();
4357
- }
4358
- }
4359
-
4360
- $this->_add_upgrade_action_link();
4361
- }
4362
-
4363
- /**
4364
- * Enqueue connect requires scripts and styles.
4365
- *
4366
- * @author Vova Feldman (@svovaf)
4367
- * @since 1.1.4
4368
- */
4369
- function _enqueue_connect_essentials() {
4370
- wp_enqueue_script( 'jquery' );
4371
- wp_enqueue_script( 'json2' );
4372
-
4373
- fs_enqueue_local_script( 'postmessage', 'nojquery.ba-postmessage.min.js' );
4374
- fs_enqueue_local_script( 'fs-postmessage', 'postmessage.js' );
4375
-
4376
- fs_enqueue_local_style( 'fs_connect', '/admin/connect.css' );
4377
- }
4378
-
4379
- /**
4380
- * Add connect / opt-in pointer.
4381
- *
4382
- * @author Vova Feldman (@svovaf)
4383
- * @since 1.1.4
4384
- */
4385
- function _add_connect_pointer_script() {
4386
- $vars = array( 'id' => $this->_module_id );
4387
- $pointer_content = fs_get_template( 'connect.php', $vars );
4388
- ?>
4389
- <script type="text/javascript">// <![CDATA[
4390
- jQuery(document).ready(function ($) {
4391
- if ('undefined' !== typeof(jQuery().pointer)) {
4392
-
4393
- var element = <?php echo $this->apply_filters( 'optin_pointer_element', '$("#non_existing_element");' ) ?>;
4394
-
4395
- if (element.length > 0) {
4396
- var optin = $(element).pointer($.extend(true, {}, {
4397
- content : <?php echo json_encode( $pointer_content ) ?>,
4398
- position : {
4399
- edge : 'left',
4400
- align: 'center'
4401
- },
4402
- buttons : function () {
4403
- // Don't show pointer buttons.
4404
- return '';
4405
- },
4406
- pointerWidth: 482
4407
- }, <?php echo $this->apply_filters( 'optin_pointer_options_json', '{}' ) ?>));
4408
-
4409
- <?php
4410
- echo $this->apply_filters( 'optin_pointer_execute', "
4411
-
4412
- optin.pointer('open');
4413
-
4414
- // Tag the opt-in pointer with custom class.
4415
- $('.wp-pointer #fs_connect')
4416
- .parents('.wp-pointer.wp-pointer-top')
4417
- .addClass('fs-opt-in-pointer');
4418
-
4419
- ", 'element', 'optin' ) ?>
4420
- }
4421
- }
4422
- });
4423
- // ]]></script>
4424
- <?php
4425
- }
4426
-
4427
- /**
4428
- * Return current page's URL.
4429
- *
4430
- * @author Vova Feldman (@svovaf)
4431
- * @since 1.0.7
4432
- *
4433
- * @return string
4434
- */
4435
- function current_page_url() {
4436
- $url = 'http';
4437
-
4438
- if ( isset( $_SERVER["HTTPS"] ) ) {
4439
- if ( $_SERVER["HTTPS"] == "on" ) {
4440
- $url .= "s";
4441
- }
4442
- }
4443
- $url .= "://";
4444
- if ( $_SERVER["SERVER_PORT"] != "80" ) {
4445
- $url .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
4446
- } else {
4447
- $url .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
4448
- }
4449
-
4450
- return esc_url( $url );
4451
- }
4452
-
4453
- /**
4454
- * Check if the current page is the plugin's main admin settings page.
4455
- *
4456
- * @author Vova Feldman (@svovaf)
4457
- * @since 1.0.7
4458
- *
4459
- * @return bool
4460
- */
4461
- function _is_plugin_page() {
4462
- return fs_is_plugin_page( $this->_menu->get_raw_slug() ) ||
4463
- fs_is_plugin_page( $this->_slug );
4464
- }
4465
-
4466
- /* Events
4467
- ------------------------------------------------------------------------------------------------------------------*/
4468
- /**
4469
- * Delete site install from Database.
4470
- *
4471
- * @author Vova Feldman (@svovaf)
4472
- * @since 1.0.1
4473
- *
4474
- * @param bool $store
4475
- */
4476
- function _delete_site( $store = true ) {
4477
- self::_delete_site_by_slug( $this->_slug, $this->_module_type, $store );
4478
- }
4479
-
4480
- /**
4481
- * Delete site install from Database.
4482
- *
4483
- * @author Vova Feldman (@svovaf)
4484
- * @since 1.2.2.7
4485
- *
4486
- * @param string $slug
4487
- * @param string $module_type
4488
- * @param bool $store
4489
- */
4490
- static function _delete_site_by_slug($slug, $module_type, $store = true ) {
4491
- $sites = self::get_all_sites( $module_type );
4492
-
4493
- if ( isset( $sites[ $slug ] ) ) {
4494
- unset( $sites[ $slug ] );
4495
- }
4496
-
4497
- self::set_account_option_by_module( $module_type, 'sites', $sites, $store );
4498
- }
4499
-
4500
- /**
4501
- * Delete plugin's plans information.
4502
- *
4503
- * @param bool $store Flush to Database if true.
4504
- *
4505
- * @author Vova Feldman (@svovaf)
4506
- * @since 1.0.9
4507
- */
4508
- private function _delete_plans( $store = true ) {
4509
- $this->_logger->entrance();
4510
-
4511
- $plans = self::get_all_plans( $this->_module_type );
4512
-
4513
- unset( $plans[ $this->_slug ] );
4514
-
4515
- $this->set_account_option( 'plans', $plans, $store );
4516
- }
4517
-
4518
- /**
4519
- * Delete all plugin licenses.
4520
- *
4521
- * @author Vova Feldman (@svovaf)
4522
- * @since 1.0.9
4523
- *
4524
- * @param bool $store
4525
- * @param string|bool $plugin_slug
4526
- */
4527
- private function _delete_licenses( $store = true, $plugin_slug = false ) {
4528
- $this->_logger->entrance();
4529
-
4530
- $all_licenses = self::get_all_licenses( $this->_module_type );
4531
-
4532
- if ( ! is_string( $plugin_slug ) ) {
4533
- $plugin_slug = $this->_slug;
4534
- }
4535
-
4536
- unset( $all_licenses[ $plugin_slug ] );
4537
-
4538
- $this->set_account_option( 'licenses', $all_licenses, $store );
4539
- }
4540
-
4541
- /**
4542
- * Check if Freemius was added on new plugin installation.
4543
- *
4544
- * @author Vova Feldman (@svovaf)
4545
- * @since 1.1.5
4546
- *
4547
- * @return bool
4548
- */
4549
- function is_plugin_new_install() {
4550
- return isset( $this->_storage->is_plugin_new_install ) &&
4551
- $this->_storage->is_plugin_new_install;
4552
- }
4553
-
4554
- /**
4555
- * Check if it's the first plugin release that is running Freemius.
4556
- *
4557
- * @author Vova Feldman (@svovaf)
4558
- * @since 1.2.1.5
4559
- *
4560
- * @return bool
4561
- */
4562
- function is_first_freemius_powered_version() {
4563
- return empty( $this->_storage->plugin_last_version );
4564
- }
4565
-
4566
- /**
4567
- * @author Leo Fajardo (@leorw)
4568
- * @since 1.2.2
4569
- *
4570
- * @return bool|string
4571
- */
4572
- private function get_previous_theme_slug() {
4573
- return isset( $this->_storage->previous_theme ) ?
4574
- $this->_storage->previous_theme :
4575
- false;
4576
- }
4577
-
4578
- /**
4579
- * @author Leo Fajardo (@leorw)
4580
- * @since 1.2.2
4581
- *
4582
- * @return string
4583
- */
4584
- private function can_activate_previous_theme() {
4585
- $slug = $this->get_previous_theme_slug();
4586
- if ( false !== $slug && current_user_can( 'switch_themes' ) ) {
4587
- $theme_instance = wp_get_theme( $slug );
4588
-
4589
- return $theme_instance->exists();
4590
- }
4591
-
4592
- return false;
4593
- }
4594
-
4595
- /**
4596
- * @author Leo Fajardo (@leorw)
4597
- * @since 1.2.2
4598
- *
4599
- * @return string
4600
- */
4601
- private function activate_previous_theme() {
4602
- switch_theme( $this->get_previous_theme_slug() );
4603
- unset( $this->_storage->previous_theme );
4604
-
4605
- global $pagenow;
4606
- if ( 'themes.php' === $pagenow ) {
4607
- /**
4608
- * Refresh the active theme information.
4609
- *
4610
- * @author Leo Fajardo (@leorw)
4611
- * @since 1.2.2
4612
- */
4613
- fs_redirect( admin_url( $pagenow ) );
4614
- }
4615
- }
4616
-
4617
- /**
4618
- * @author Leo Fajardo (@leorw)
4619
- * @since 1.2.2
4620
- *
4621
- * @return string
4622
- */
4623
- function get_previous_theme_activation_url() {
4624
- if ( ! $this->can_activate_previous_theme() ) {
4625
- return '';
4626
- }
4627
-
4628
- /**
4629
- * Activation URL
4630
- *
4631
- * @author Leo Fajardo (@leorw)
4632
- * @since 1.2.2
4633
- */
4634
- return wp_nonce_url(
4635
- admin_url( 'themes.php?action=activate&stylesheet=' . urlencode( $this->get_previous_theme_slug() ) ),
4636
- 'switch-theme_' . $this->get_previous_theme_slug()
4637
- );
4638
- }
4639
-
4640
- /**
4641
- * Saves the slug of the previous theme if it still exists so that it can be used by the logic in the opt-in
4642
- * form that decides whether to add a close button to the opt-in dialog or not. So after a premium-only theme is
4643
- * activated, the close button will appear and will reactivate the previous theme if clicked. If the previous
4644
- * theme doesn't exist, then there will be no close button.
4645
- *
4646
- * @author Leo Fajardo (@leorw)
4647
- * @since 1.2.2
4648
- *
4649
- * @param string $slug_or_name Old theme's slug or name.
4650
- * @param bool|WP_Theme $old_theme WP_Theme instance of the old theme if it still exists.
4651
- */
4652
- function _activate_theme_event_hook( $slug_or_name, $old_theme = false ) {
4653
- $this->_storage->previous_theme = ( false !== $old_theme ) ?
4654
- $old_theme->get_stylesheet() :
4655
- $slug_or_name;
4656
-
4657
- $this->_activate_plugin_event_hook();
4658
- }
4659
-
4660
- /**
4661
- * Plugin activated hook.
4662
- *
4663
- * @author Vova Feldman (@svovaf)
4664
- * @since 1.0.1
4665
- *
4666
- * @uses FS_Api
4667
- */
4668
- function _activate_plugin_event_hook() {
4669
- $this->_logger->entrance( 'slug = ' . $this->_slug );
4670
-
4671
- if ( ! $this->is_user_admin() ) {
4672
- return;
4673
- }
4674
-
4675
- $this->unregister_uninstall_hook();
4676
-
4677
- // Clear API cache on activation.
4678
- FS_Api::clear_cache();
4679
-
4680
- $is_premium_version_activation = ( current_filter() !== ( 'activate_' . $this->_free_plugin_basename ) );
4681
-
4682
- $this->_logger->info( 'Activating ' . ( $is_premium_version_activation ? 'premium' : 'free' ) . ' plugin version.' );
4683
-
4684
- // 1. If running in the activation of the FREE module, get the basename of the PREMIUM.
4685
- // 2. If running in the activation of the PREMIUM module, get the basename of the FREE.
4686
- $other_version_basename = $is_premium_version_activation ?
4687
- $this->_free_plugin_basename :
4688
- $this->premium_plugin_basename();
4689
-
4690
- /**
4691
- * If the other module version is activate, deactivate it.
4692
- *
4693
- * @author Leo Fajardo (@leorw)
4694
- * @since 1.2.2
4695
- */
4696
- if ( is_plugin_active( $other_version_basename ) ) {
4697
- deactivate_plugins( $other_version_basename );
4698
- }
4699
-
4700
- if ( $this->is_registered() ) {
4701
- if ( $is_premium_version_activation ) {
4702
- $this->reconnect_locally();
4703
- }
4704
-
4705
-
4706
- // Schedule re-activation event and sync.
4707
- // $this->sync_install( array(), true );
4708
- $this->schedule_install_sync();
4709
-
4710
- // If activating the premium module version, add an admin notice to congratulate for an upgrade completion.
4711
- if ( $is_premium_version_activation ) {
4712
- $this->_admin_notices->add(
4713
- sprintf( $this->get_text( 'successful-version-upgrade-message' ), sprintf( '<b>%s</b>', $this->_plugin->title ) ),
4714
- $this->get_text( 'woot' ) . '!'
4715
- );
4716
- }
4717
- } else if ( $this->is_anonymous() ) {
4718
- /**
4719
- * Reset "skipped" click cache on the following:
4720
- * 1. Freemius DEV mode.
4721
- * 2. WordPress DEBUG mode.
4722
- * 3. If a plugin and the user skipped the exact same version before.
4723
- *
4724
- * @since 1.2.2.7 Ulrich Pogson (@grapplerulrich) asked to not reset the SKIPPED flag if the exact same THEME version was activated before unless the developer is running with WP_DEBUG on, or Freemius debug mode on (WP_FS__DEV_MODE).
4725
- *
4726
- * @todo 4. If explicitly asked to retry after every activation.
4727
- */
4728
- if ( WP_FS__DEV_MODE ||
4729
- (
4730
- ( $this->is_plugin() || ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ) &&
4731
- $this->get_plugin_version() == $this->_storage->is_anonymous['version']
4732
- )
4733
- ) {
4734
- $this->reset_anonymous_mode();
4735
- }
4736
- }
4737
-
4738
- if ( ! isset( $this->_storage->is_plugin_new_install ) ) {
4739
- /**
4740
- * If no previous version of plugin's version exist, it means that it's either
4741
- * the first time that the plugin installed on the site, or the plugin was installed
4742
- * before but didn't have Freemius integrated.
4743
- *
4744
- * Since register_activation_hook() do NOT fires on updates since 3.1, and only fires
4745
- * on manual activation via the dashboard, is_plugin_activation() is TRUE
4746
- * only after immediate activation.
4747
- *
4748
- * @since 1.1.4
4749
- * @link https://make.wordpress.org/core/2010/10/27/plugin-activation-hooks-no-longer-fire-for-updates/
4750
- */
4751
- $this->_storage->is_plugin_new_install = empty( $this->_storage->plugin_last_version );
4752
- }
4753
-
4754
- if ( ! $this->_anonymous_mode &&
4755
- $this->has_api_connectivity( WP_FS__DEV_MODE ) &&
4756
- ! $this->_isAutoInstall
4757
- ) {
4758
- // Store hint that the plugin was just activated to enable auto-redirection to settings.
4759
- add_option( 'fs_'
4760
- . ( $this->is_plugin() ? '' : $this->_module_type . '_' )
4761
- . "{$this->_slug}_activated", true );
4762
- }
4763
-
4764
- /**
4765
- * Activation hook is executed after the plugin's main file is loaded, therefore,
4766
- * after the plugin was loaded. The logic is located at activate_plugin()
4767
- * ./wp-admin/includes/plugin.php.
4768
- *
4769
- * @author Vova Feldman (@svovaf)
4770
- * @since 1.1.9
4771
- */
4772
- $this->_storage->was_plugin_loaded = true;
4773
- }
4774
-
4775
- /**
4776
- * Delete account.
4777
- *
4778
- * @author Vova Feldman (@svovaf)
4779
- * @since 1.0.3
4780
- *
4781
- * @param bool $check_user Enforce checking if user have plugins activation privileges.
4782
- */
4783
- function delete_account_event( $check_user = true ) {
4784
- $this->_logger->entrance( 'slug = ' . $this->_slug );
4785
-
4786
- if ( $check_user && ! $this->is_user_admin() ) {
4787
- return;
4788
- }
4789
-
4790
- $this->do_action( 'before_account_delete' );
4791
-
4792
- // Clear all admin notices.
4793
- $this->_admin_notices->clear_all_sticky();
4794
-
4795
- $this->_delete_site( false );
4796
-
4797
- $this->_delete_plans( false );
4798
-
4799
- $this->_delete_licenses( false );
4800
-
4801
- // Delete add-ons related to plugin's account.
4802
- $this->_delete_account_addons( false );
4803
-
4804
- // @todo Delete plans and licenses of add-ons.
4805
-
4806
- self::$_accounts->store();
4807
-
4808
- /**
4809
- * IMPORTANT:
4810
- * Clear crons must be executed before clearing all storage.
4811
- * Otherwise, the cron will not be cleared.
4812
- */
4813
- $this->clear_sync_cron();
4814
- $this->clear_install_sync_cron();
4815
-
4816
- // Clear all storage data.
4817
- $this->_storage->clear_all( true, array(
4818
- 'connectivity_test',
4819
- 'is_on',
4820
- ) );
4821
-
4822
- // Send delete event.
4823
- $this->get_api_site_scope()->call( '/', 'delete' );
4824
-
4825
- $this->do_action( 'after_account_delete' );
4826
- }
4827
-
4828
- /**
4829
- * Plugin deactivation hook.
4830
- *
4831
- * @author Vova Feldman (@svovaf)
4832
- * @since 1.0.1
4833
- */
4834
- function _deactivate_plugin_hook() {
4835
- $this->_logger->entrance( 'slug = ' . $this->_slug );
4836
-
4837
- if ( ! current_user_can( 'activate_plugins' ) ) {
4838
- return;
4839
- }
4840
-
4841
- $this->_admin_notices->clear_all_sticky();
4842
- if ( isset( $this->_storage->sticky_optin_added ) ) {
4843
- unset( $this->_storage->sticky_optin_added );
4844
- }
4845
-
4846
- if ( ! isset( $this->_storage->is_plugin_new_install ) ) {
4847
- // Remember that plugin was already installed.
4848
- $this->_storage->is_plugin_new_install = false;
4849
- }
4850
-
4851
- // Hook to plugin uninstall.
4852
- register_uninstall_hook( $this->_plugin_main_file_path, array( 'Freemius', '_uninstall_plugin_hook' ) );
4853
-
4854
- $this->clear_module_main_file_cache();
4855
- $this->clear_sync_cron();
4856
- $this->clear_install_sync_cron();
4857
-
4858
- if ( $this->is_registered() ) {
4859
- // Send deactivation event.
4860
- $this->sync_install( array(
4861
- 'is_active' => false,
4862
- ) );
4863
- } else {
4864
- if ( ! $this->has_api_connectivity() ) {
4865
- // Reset connectivity test cache.
4866
- unset( $this->_storage->connectivity_test );
4867
- }
4868
- }
4869
-
4870
- // Clear API cache on deactivation.
4871
- FS_Api::clear_cache();
4872
-
4873
- $this->remove_sdk_reference();
4874
- }
4875
-
4876
- /**
4877
- * @author Vova Feldman (@svovaf)
4878
- * @since 1.1.6
4879
- */
4880
- private function remove_sdk_reference() {
4881
- global $fs_active_plugins;
4882
-
4883
- foreach ( $fs_active_plugins->plugins as $sdk_path => &$data ) {
4884
- if ( $this->_plugin_basename == $data->plugin_path ) {
4885
- unset( $fs_active_plugins->plugins[ $sdk_path ] );
4886
- break;
4887
- }
4888
- }
4889
-
4890
- fs_fallback_to_newest_active_sdk();
4891
- }
4892
-
4893
- /**
4894
- * @author Vova Feldman (@svovaf)
4895
- * @since 1.1.3
4896
- *
4897
- * @param bool $is_anonymous
4898
- */
4899
- private function set_anonymous_mode( $is_anonymous = true ) {
4900
- // Store information regarding skip to try and opt-in the user
4901
- // again in the future.
4902
- $this->_storage->is_anonymous = array(
4903
- 'is' => $is_anonymous,
4904
- 'timestamp' => WP_FS__SCRIPT_START_TIME,
4905
- 'version' => $this->get_plugin_version(),
4906
- );
4907
-
4908
- // Update anonymous mode cache.
4909
- $this->_is_anonymous = $is_anonymous;
4910
- }
4911
-
4912
- /**
4913
- * @author Vova Feldman (@svovaf)
4914
- * @since 1.1.3
4915
- */
4916
- private function reset_anonymous_mode() {
4917
- unset( $this->_storage->is_anonymous );
4918
-
4919
- /**
4920
- * Ensure that this field is also "false", otherwise, if the current module's type is "theme" and the module
4921
- * has no menus, the opt-in popup will not be shown immediately (in this case, the user will have to click
4922
- * on the admin notice that contains the opt-in link in order to trigger the opt-in popup).
4923
- *
4924
- * @author Leo Fajardo (@leorw)
4925
- * @since 1.2.2
4926
- */
4927
- unset( $this->_is_anonymous );
4928
- }
4929
-
4930
- /**
4931
- * Clears the anonymous mode and redirects to the opt-in screen.
4932
- *
4933
- * @author Vova Feldman (@svovaf)
4934
- * @since 1.1.7
4935
- */
4936
- function connect_again() {
4937
- if ( ! $this->is_anonymous() ) {
4938
- return;
4939
- }
4940
-
4941
- $this->reset_anonymous_mode();
4942
-
4943
- fs_redirect( $this->get_activation_url() );
4944
- }
4945
-
4946
- /**
4947
- * Skip account connect, and set anonymous mode.
4948
- *
4949
- * @author Vova Feldman (@svovaf)
4950
- * @since 1.1.1
4951
- */
4952
- private function skip_connection() {
4953
- $this->_logger->entrance();
4954
-
4955
- $this->_admin_notices->remove_sticky( 'connect_account' );
4956
-
4957
- $this->set_anonymous_mode();
4958
-
4959
- // Send anonymous skip event.
4960
- // No user identified info nor any tracking will be sent after the user skips the opt-in.
4961
- $this->get_api_plugin_scope()->call( 'skip.json', 'put', array(
4962
- 'uid' => $this->get_anonymous_id(),
4963
- ) );
4964
- }
4965
-
4966
- /**
4967
- * Plugin version update hook.
4968
- *
4969
- * @author Vova Feldman (@svovaf)
4970
- * @since 1.0.4
4971
- */
4972
- private function update_plugin_version_event() {
4973
- $this->_logger->entrance();
4974
-
4975
- if ( ! $this->is_registered() ) {
4976
- return;
4977
- }
4978
-
4979
- $this->schedule_install_sync();
4980
- // $this->sync_install( array(), true );
4981
- }
4982
-
4983
- /**
4984
- * Return a list of modified plugins since the last sync.
4985
- *
4986
- * Note:
4987
- * There's no point to store a plugins counter since even if the number of
4988
- * plugins didn't change, we still need to check if the versions are all the
4989
- * same and the activity state is similar.
4990
- *
4991
- * @author Vova Feldman (@svovaf)
4992
- * @since 1.1.8
4993
- *
4994
- * @return array|false
4995
- */
4996
- private function get_plugins_data_for_api() {
4997
- // Alias.
4998
- $option_name = 'all_plugins';
4999
-
5000
- $all_cached_plugins = self::$_accounts->get_option( $option_name );
5001
-
5002
- if ( ! is_object( $all_cached_plugins ) ) {
5003
- $all_cached_plugins = (object) array(
5004
- 'timestamp' => '',
5005
- 'md5' => '',
5006
- 'plugins' => array(),
5007
- );
5008
- }
5009
-
5010
- $time = time();
5011
-
5012
- if ( ! empty( $all_cached_plugins->timestamp ) &&
5013
- ( $time - $all_cached_plugins->timestamp ) < WP_FS__TIME_5_MIN_IN_SEC
5014
- ) {
5015
- // Don't send plugin updates if last update was in the past 5 min.
5016
- return false;
5017
- }
5018
-
5019
- // Write timestamp to lock the logic.
5020
- $all_cached_plugins->timestamp = $time;
5021
- self::$_accounts->set_option( $option_name, $all_cached_plugins, true );
5022
-
5023
- // Reload options from DB.
5024
- self::$_accounts->load( true );
5025
- $all_cached_plugins = self::$_accounts->get_option( $option_name );
5026
-
5027
- if ( $time != $all_cached_plugins->timestamp ) {
5028
- // If timestamp is different, then another thread captured the lock.
5029
- return false;
5030
- }
5031
-
5032
- // Check if there's a change in plugins.
5033
- $all_plugins = self::get_all_plugins();
5034
-
5035
- // Check if plugins changed.
5036
- ksort( $all_plugins );
5037
-
5038
- $plugins_signature = '';
5039
- foreach ( $all_plugins as $basename => $data ) {
5040
- $plugins_signature .= $data['slug'] . ',' .
5041
- $data['Version'] . ',' .
5042
- ( $data['is_active'] ? '1' : '0' ) . ';';
5043
- }
5044
-
5045
- // Check if plugins status changed (version or active/inactive).
5046
- $plugins_changed = ( $all_cached_plugins->md5 !== md5( $plugins_signature ) );
5047
-
5048
- $plugins_update_data = array();
5049
-
5050
- if ( $plugins_changed ) {
5051
- // Change in plugins, report changes.
5052
-
5053
- // Update existing plugins info.
5054
- foreach ( $all_cached_plugins->plugins as $basename => $data ) {
5055
- if ( ! isset( $all_plugins[ $basename ] ) ) {
5056
- // Plugin uninstalled.
5057
- $uninstalled_plugin_data = $data;
5058
- $uninstalled_plugin_data['is_active'] = false;
5059
- $uninstalled_plugin_data['is_uninstalled'] = true;
5060
- $plugins_update_data[] = $uninstalled_plugin_data;
5061
-
5062
- unset( $all_plugins[ $basename ] );
5063
- unset( $all_cached_plugins->plugins[ $basename ] );
5064
- } else if ( $data['is_active'] !== $all_plugins[ $basename ]['is_active'] ||
5065
- $data['version'] !== $all_plugins[ $basename ]['Version']
5066
- ) {
5067
- // Plugin activated or deactivated, or version changed.
5068
- $all_cached_plugins->plugins[ $basename ]['is_active'] = $all_plugins[ $basename ]['is_active'];
5069
- $all_cached_plugins->plugins[ $basename ]['version'] = $all_plugins[ $basename ]['Version'];
5070
-
5071
- $plugins_update_data[] = $all_cached_plugins->plugins[ $basename ];
5072
- }
5073
- }
5074
-
5075
- // Find new plugins that weren't yet seen before.
5076
- foreach ( $all_plugins as $basename => $data ) {
5077
- if ( ! isset( $all_cached_plugins->plugins[ $basename ] ) ) {
5078
- // New plugin.
5079
- $new_plugin = array(
5080
- 'slug' => $data['slug'],
5081
- 'version' => $data['Version'],
5082
- 'title' => $data['Name'],
5083
- 'is_active' => $data['is_active'],
5084
- 'is_uninstalled' => false,
5085
- );
5086
-
5087
- $plugins_update_data[] = $new_plugin;
5088
- $all_cached_plugins->plugins[ $basename ] = $new_plugin;
5089
- }
5090
- }
5091
-
5092
- $all_cached_plugins->md5 = md5( $plugins_signature );
5093
- $all_cached_plugins->timestamp = $time;
5094
- self::$_accounts->set_option( $option_name, $all_cached_plugins, true );
5095
- }
5096
-
5097
- return $plugins_update_data;
5098
- }
5099
-
5100
- /**
5101
- * Return a list of modified themes since the last sync.
5102
- *
5103
- * Note:
5104
- * There's no point to store a themes counter since even if the number of
5105
- * themes didn't change, we still need to check if the versions are all the
5106
- * same and the activity state is similar.
5107
- *
5108
- * @author Vova Feldman (@svovaf)
5109
- * @since 1.1.8
5110
- *
5111
- * @return array|false
5112
- */
5113
- private function get_themes_data_for_api() {
5114
- // Alias.
5115
- $option_name = 'all_themes';
5116
-
5117
- $all_cached_themes = self::$_accounts->get_option( $option_name );
5118
-
5119
- if ( ! is_object( $all_cached_themes ) ) {
5120
- $all_cached_themes = (object) array(
5121
- 'timestamp' => '',
5122
- 'md5' => '',
5123
- 'themes' => array(),
5124
- );
5125
- }
5126
-
5127
- $time = time();
5128
-
5129
- if ( ! empty( $all_cached_themes->timestamp ) &&
5130
- ( $time - $all_cached_themes->timestamp ) < WP_FS__TIME_5_MIN_IN_SEC
5131
- ) {
5132
- // Don't send theme updates if last update was in the past 5 min.
5133
- return false;
5134
- }
5135
-
5136
- // Write timestamp to lock the logic.
5137
- $all_cached_themes->timestamp = $time;
5138
- self::$_accounts->set_option( $option_name, $all_cached_themes, true );
5139
-
5140
- // Reload options from DB.
5141
- self::$_accounts->load( true );
5142
- $all_cached_themes = self::$_accounts->get_option( $option_name );
5143
-
5144
- if ( $time != $all_cached_themes->timestamp ) {
5145
- // If timestamp is different, then another thread captured the lock.
5146
- return false;
5147
- }
5148
-
5149
- // Get active theme.
5150
- $active_theme = wp_get_theme();
5151
- $active_theme_stylesheet = $active_theme->get_stylesheet();
5152
-
5153
- // Check if there's a change in themes.
5154
- $all_themes = wp_get_themes();
5155
-
5156
- // Check if themes changed.
5157
- ksort( $all_themes );
5158
-
5159
- $themes_signature = '';
5160
- foreach ( $all_themes as $slug => $data ) {
5161
- $is_active = ( $slug === $active_theme_stylesheet );
5162
- $themes_signature .= $slug . ',' .
5163
- $data->version . ',' .
5164
- ( $is_active ? '1' : '0' ) . ';';
5165
- }
5166
-
5167
- // Check if themes status changed (version or active/inactive).
5168
- $themes_changed = ( $all_cached_themes->md5 !== md5( $themes_signature ) );
5169
-
5170
- $themes_update_data = array();
5171
-
5172
- if ( $themes_changed ) {
5173
- // Change in themes, report changes.
5174
-
5175
- // Update existing themes info.
5176
- foreach ( $all_cached_themes->themes as $slug => $data ) {
5177
- $is_active = ( $slug === $active_theme_stylesheet );
5178
-
5179
- if ( ! isset( $all_themes[ $slug ] ) ) {
5180
- // Plugin uninstalled.
5181
- $uninstalled_theme_data = $data;
5182
- $uninstalled_theme_data['is_active'] = false;
5183
- $uninstalled_theme_data['is_uninstalled'] = true;
5184
- $themes_update_data[] = $uninstalled_theme_data;
5185
-
5186
- unset( $all_themes[ $slug ] );
5187
- unset( $all_cached_themes->themes[ $slug ] );
5188
- } else if ( $data['is_active'] !== $is_active ||
5189
- $data['version'] !== $all_themes[ $slug ]->version
5190
- ) {
5191
- // Plugin activated or deactivated, or version changed.
5192
-
5193
- $all_cached_themes->themes[ $slug ]['is_active'] = $is_active;
5194
- $all_cached_themes->themes[ $slug ]['version'] = $all_themes[ $slug ]->version;
5195
-
5196
- $themes_update_data[] = $all_cached_themes->themes[ $slug ];
5197
- }
5198
- }
5199
-
5200
- // Find new themes that weren't yet seen before.
5201
- foreach ( $all_themes as $slug => $data ) {
5202
- if ( ! isset( $all_cached_themes->themes[ $slug ] ) ) {
5203
- $is_active = ( $slug === $active_theme_stylesheet );
5204
-
5205
- // New plugin.
5206
- $new_plugin = array(
5207
- 'slug' => $slug,
5208
- 'version' => $data->version,
5209
- 'title' => $data->name,
5210
- 'is_active' => $is_active,
5211
- 'is_uninstalled' => false,
5212
- );
5213
-
5214
- $themes_update_data[] = $new_plugin;
5215
- $all_cached_themes->themes[ $slug ] = $new_plugin;
5216
- }
5217
- }
5218
-
5219
- $all_cached_themes->md5 = md5( $themes_signature );
5220
- $all_cached_themes->timestamp = time();
5221
- self::$_accounts->set_option( $option_name, $all_cached_themes, true );
5222
- }
5223
-
5224
- return $themes_update_data;
5225
- }
5226
-
5227
- /**
5228
- * Update install details.
5229
- *
5230
- * @author Vova Feldman (@svovaf)
5231
- * @since 1.1.2
5232
- *
5233
- * @param string[] string $override
5234
- * @param bool $include_plugins Since 1.1.8 by default include plugin changes.
5235
- * @param bool $include_themes Since 1.1.8 by default include plugin changes.
5236
- *
5237
- * @return array
5238
- */
5239
- private function get_install_data_for_api(
5240
- array $override,
5241
- $include_plugins = true,
5242
- $include_themes = true
5243
- ) {
5244
- /**
5245
- * @since 1.1.8 Also send plugin updates.
5246
- */
5247
- if ( $include_plugins && ! isset( $override['plugins'] ) ) {
5248
- $plugins = $this->get_plugins_data_for_api();
5249
- if ( ! empty( $plugins ) ) {
5250
- $override['plugins'] = $plugins;
5251
- }
5252
- }
5253
- /**
5254
- * @since 1.1.8 Also send themes updates.
5255
- */
5256
- if ( $include_themes && ! isset( $override['themes'] ) ) {
5257
- $themes = $this->get_themes_data_for_api();
5258
- if ( ! empty( $themes ) ) {
5259
- $override['themes'] = $themes;
5260
- }
5261
- }
5262
-
5263
- return array_merge( array(
5264
- 'version' => $this->get_plugin_version(),
5265
- 'is_premium' => $this->is_premium(),
5266
- 'language' => get_bloginfo( 'language' ),
5267
- 'charset' => get_bloginfo( 'charset' ),
5268
- 'platform_version' => get_bloginfo( 'version' ),
5269
- 'sdk_version' => $this->version,
5270
- 'programming_language_version' => phpversion(),
5271
- 'title' => get_bloginfo( 'name' ),
5272
- 'url' => get_site_url(),
5273
- // Special params.
5274
- 'is_active' => true,
5275
- 'is_disconnected' => $this->is_tracking_prohibited(),
5276
- 'is_uninstalled' => false,
5277
- ), $override );
5278
- }
5279
-
5280
- /**
5281
- * Update install only if changed.
5282
- *
5283
- * @author Vova Feldman (@svovaf)
5284
- * @since 1.0.9
5285
- *
5286
- * @param string[] string $override
5287
- * @param bool $flush
5288
- *
5289
- * @return false|object|string
5290
- */
5291
- private function send_install_update( $override = array(), $flush = false ) {
5292
- $this->_logger->entrance();
5293
-
5294
- $check_properties = $this->get_install_data_for_api( $override );
5295
-
5296
- if ( $flush ) {
5297
- $params = $check_properties;
5298
- } else {
5299
- $params = array();
5300
- $special = array();
5301
- $special_override = false;
5302
-
5303
- foreach ( $check_properties as $p => $v ) {
5304
- if ( property_exists( $this->_site, $p ) ) {
5305
- if ( ( is_bool( $this->_site->{$p} ) || ! empty( $this->_site->{$p} ) ) &&
5306
- $this->_site->{$p} != $v
5307
- ) {
5308
- $this->_site->{$p} = $v;
5309
- $params[ $p ] = $v;
5310
- }
5311
- } else {
5312
- $special[ $p ] = $v;
5313
-
5314
- if ( isset( $override[ $p ] ) ||
5315
- 'plugins' === $p ||
5316
- 'themes' === $p
5317
- ) {
5318
- $special_override = true;
5319
- }
5320
- }
5321
- }
5322
-
5323
- if ( $special_override || 0 < count( $params ) ) {
5324
- // Add special params only if has at least one
5325
- // standard param, or if explicitly requested to
5326
- // override a special param or a param which is not exist
5327
- // in the install object.
5328
- $params = array_merge( $params, $special );
5329
- }
5330
- }
5331
-
5332
- if ( 0 < count( $params ) ) {
5333
- // Update last install sync timestamp.
5334
- $this->_storage->install_sync_timestamp = time();
5335
-
5336
- $params['uid'] = $this->get_anonymous_id();
5337
-
5338
- // Send updated values to FS.
5339
- $site = $this->get_api_site_scope()->call( '/', 'put', $params );
5340
-
5341
- if ( $this->is_api_result_entity( $site ) ) {
5342
- // I successfully sent install update, clear scheduled sync if exist.
5343
- $this->clear_install_sync_cron();
5344
- }
5345
-
5346
- return $site;
5347
- }
5348
-
5349
- return false;
5350
- }
5351
-
5352
- /**
5353
- * Update install only if changed.
5354
- *
5355
- * @author Vova Feldman (@svovaf)
5356
- * @since 1.0.9
5357
- *
5358
- * @param string[] string $override
5359
- * @param bool $flush
5360
- */
5361
- private function sync_install( $override = array(), $flush = false ) {
5362
- $this->_logger->entrance();
5363
-
5364
- $site = $this->send_install_update( $override, $flush );
5365
-
5366
- if ( false === $site ) {
5367
- // No sync required.
5368
- return;
5369
- }
5370
-
5371
- if ( ! $this->is_api_result_entity( $site ) ) {
5372
- // Failed to sync, don't update locally.
5373
- return;
5374
- }
5375
-
5376
- $plan = $this->get_plan();
5377
- $this->_site = new FS_Site( $site );
5378
- $this->_site->plan = $plan;
5379
-
5380
- $this->_store_site( true );
5381
- }
5382
-
5383
- /**
5384
- * Track install's custom event.
5385
- *
5386
- * IMPORTANT:
5387
- * Custom event tracking is currently only supported for specific clients.
5388
- * If you are not one of them, please don't use this method. If you will,
5389
- * the API will simply ignore your request based on the plugin ID.
5390
- *
5391
- * Need custom tracking for your plugin or theme?
5392
- * If you are interested in custom event tracking please contact yo@freemius.com
5393
- * for further details.
5394
- *
5395
- * @author Vova Feldman (@svovaf)
5396
- * @since 1.2.1
5397
- *
5398
- * @param string $name Event name.
5399
- * @param array $properties Associative key/value array with primitive values only
5400
- * @param bool $process_at A valid future date-time in the following format Y-m-d H:i:s.
5401
- * @param bool $once If true, event will be tracked only once. IMPORTANT: Still trigger the API call.
5402
- *
5403
- * @return object|false Event data or FALSE on failure.
5404
- *
5405
- * @throws \Freemius_InvalidArgumentException
5406
- */
5407
- public function track_event( $name, $properties = array(), $process_at = false, $once = false ) {
5408
- $this->_logger->entrance( http_build_query( array( 'name' => $name, 'once' => $once ) ) );
5409
-
5410
- if ( ! $this->is_registered() ) {
5411
- return false;
5412
- }
5413
-
5414
- $event = array( 'type' => $name );
5415
-
5416
- if ( is_numeric( $process_at ) && $process_at > time() ) {
5417
- $event['process_at'] = $process_at;
5418
- }
5419
-
5420
- if ( $once ) {
5421
- $event['once'] = true;
5422
- }
5423
-
5424
- if ( ! empty( $properties ) ) {
5425
- // Verify associative array values are primitive.
5426
- foreach ( $properties as $k => $v ) {
5427
- if ( ! is_scalar( $v ) ) {
5428
- throw new Freemius_InvalidArgumentException( 'The $properties argument must be an associative key/value array with primitive values only.' );
5429
- }
5430
- }
5431
-
5432
- $event['properties'] = $properties;
5433
- }
5434
-
5435
- $result = $this->get_api_site_scope()->call( 'events.json', 'post', $event );
5436
-
5437
- return $this->is_api_error( $result ) ?
5438
- false :
5439
- $result;
5440
- }
5441
-
5442
- /**
5443
- * Track install's custom event only once, but it still triggers the API call.
5444
- *
5445
- * IMPORTANT:
5446
- * Custom event tracking is currently only supported for specific clients.
5447
- * If you are not one of them, please don't use this method. If you will,
5448
- * the API will simply ignore your request based on the plugin ID.
5449
- *
5450
- * Need custom tracking for your plugin or theme?
5451
- * If you are interested in custom event tracking please contact yo@freemius.com
5452
- * for further details.
5453
- *
5454
- * @author Vova Feldman (@svovaf)
5455
- * @since 1.2.1
5456
- *
5457
- * @param string $name Event name.
5458
- * @param array $properties Associative key/value array with primitive values only
5459
- * @param bool $process_at A valid future date-time in the following format Y-m-d H:i:s.
5460
- *
5461
- * @return object|false Event data or FALSE on failure.
5462
- *
5463
- * @throws \Freemius_InvalidArgumentException
5464
- *
5465
- * @user Freemius::track_event()
5466
- */
5467
- public function track_event_once( $name, $properties = array(), $process_at = false ) {
5468
- return $this->track_event( $name, $properties, $process_at, true );
5469
- }
5470
-
5471
- /**
5472
- * Plugin uninstall hook.
5473
- *
5474
- * @author Vova Feldman (@svovaf)
5475
- * @since 1.0.1
5476
- *
5477
- * @param bool $check_user Enforce checking if user have plugins activation privileges.
5478
- */
5479
- function _uninstall_plugin_event( $check_user = true ) {
5480
- $this->_logger->entrance( 'slug = ' . $this->_slug );
5481
-
5482
- if ( $check_user && ! current_user_can( 'activate_plugins' ) ) {
5483
- return;
5484
- }
5485
-
5486
- $params = array();
5487
- $uninstall_reason = null;
5488
- if ( isset( $this->_storage->uninstall_reason ) ) {
5489
- $uninstall_reason = $this->_storage->uninstall_reason;
5490
- $params['reason_id'] = $uninstall_reason->id;
5491
- $params['reason_info'] = $uninstall_reason->info;
5492
- }
5493
-
5494
- if ( ! $this->is_registered() ) {
5495
- // Send anonymous uninstall event only if user submitted a feedback.
5496
- if ( isset( $uninstall_reason ) ) {
5497
- if ( isset( $uninstall_reason->is_anonymous ) && ! $uninstall_reason->is_anonymous ) {
5498
- $this->opt_in( false, false, false, false, true );
5499
- } else {
5500
- $params['uid'] = $this->get_anonymous_id();
5501
- $this->get_api_plugin_scope()->call( 'uninstall.json', 'put', $params );
5502
- }
5503
- }
5504
- } else {
5505
- // Send uninstall event.
5506
- $this->send_install_update( array_merge( $params, array(
5507
- 'is_active' => false,
5508
- 'is_uninstalled' => true,
5509
- ) ) );
5510
- }
5511
-
5512
- // @todo Decide if we want to delete plugin information from db.
5513
- }
5514
-
5515
- /**
5516
- * @author Vova Feldman (@svovaf)
5517
- * @since 1.1.1
5518
- *
5519
- * @return string
5520
- */
5521
- function premium_plugin_basename() {
5522
- return "{$this->_slug}-premium/" . basename( $this->_free_plugin_basename );
5523
- }
5524
-
5525
- /**
5526
- * Uninstall plugin hook. Called only when connected his account with Freemius for active sites tracking.
5527
- *
5528
- * @author Vova Feldman (@svovaf)
5529
- * @since 1.0.2
5530
- */
5531
- public static function _uninstall_plugin_hook() {
5532
- self::_load_required_static();
5533
-
5534
- self::$_static_logger->entrance();
5535
-
5536
- if ( ! current_user_can( 'activate_plugins' ) ) {
5537
- return;
5538
- }
5539
-
5540
- $plugin_file = substr( current_filter(), strlen( 'uninstall_' ) );
5541
-
5542
- self::$_static_logger->info( 'plugin = ' . $plugin_file );
5543
-
5544
- define( 'WP_FS__UNINSTALL_MODE', true );
5545
-
5546
- $fs = self::get_instance_by_file( $plugin_file );
5547
-
5548
- if ( is_object( $fs ) ) {
5549
- self::require_plugin_essentials();
5550
-
5551
- if ( is_plugin_active( $fs->_free_plugin_basename ) ||
5552
- is_plugin_active( $fs->premium_plugin_basename() )
5553
- ) {
5554
- // Deleting Free or Premium plugin version while the other version still installed.
5555
- return;
5556
- }
5557
-
5558
- $fs->_uninstall_plugin_event();
5559
-
5560
- $fs->do_action( 'after_uninstall' );
5561
- }
5562
- }
5563
-
5564
- #----------------------------------------------------------------------------------
5565
- #region Plugin Information
5566
- #----------------------------------------------------------------------------------
5567
-
5568
- /**
5569
- * Load WordPress core plugin.php essential module.
5570
- *
5571
- * @author Vova Feldman (@svovaf)
5572
- * @since 1.1.1
5573
- */
5574
- private static function require_plugin_essentials() {
5575
- if ( ! function_exists( 'get_plugins' ) ) {
5576
- self::$_static_logger->log( 'Including wp-admin/includes/plugin.php...' );
5577
-
5578
- require_once ABSPATH . 'wp-admin/includes/plugin.php';
5579
- }
5580
- }
5581
-
5582
- /**
5583
- * Load WordPress core pluggable.php module.
5584
- *
5585
- * @author Vova Feldman (@svovaf)
5586
- * @since 1.1.2
5587
- */
5588
- private static function require_pluggable_essentials() {
5589
- if ( ! function_exists( 'wp_get_current_user' ) ) {
5590
- require_once ABSPATH . 'wp-includes/pluggable.php';
5591
- }
5592
- }
5593
-
5594
- /**
5595
- * Return plugin data.
5596
- *
5597
- * @author Vova Feldman (@svovaf)
5598
- * @since 1.0.1
5599
- *
5600
- * @return array
5601
- */
5602
- function get_plugin_data() {
5603
- if ( ! isset( $this->_plugin_data ) ) {
5604
- self::require_plugin_essentials();
5605
-
5606
- if ( $this->is_plugin() ) {
5607
- /**
5608
- * @author Vova Feldman (@svovaf)
5609
- * @since 1.2.0 When using get_plugin_data() do NOT translate plugin data.
5610
- *
5611
- * @link https://github.com/Freemius/wordpress-sdk/issues/77
5612
- */
5613
- $plugin_data = get_plugin_data(
5614
- $this->_plugin_main_file_path,
5615
- false,
5616
- false
5617
- );
5618
- } else {
5619
- $theme_data = wp_get_theme();
5620
-
5621
- $plugin_data = array(
5622
- 'Name' => $theme_data->get( 'Name' ),
5623
- 'Version' => $theme_data->get( 'Version' ),
5624
- 'Author' => $theme_data->get( 'Author' ),
5625
- 'Description' => $theme_data->get( 'Description' ),
5626
- 'PluginURI' => $theme_data->get( 'ThemeURI' ),
5627
- );
5628
- }
5629
-
5630
- $this->_plugin_data = $plugin_data;
5631
- }
5632
-
5633
- return $this->_plugin_data;
5634
- }
5635
-
5636
- /**
5637
- * @author Vova Feldman (@svovaf)
5638
- * @since 1.0.1
5639
- * @since 1.2.2.5 If slug not set load slug by module ID.
5640
- *
5641
- * @return string Plugin slug.
5642
- */
5643
- function get_slug() {
5644
- if ( ! isset( $this->_slug ) ) {
5645
- $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
5646
- $this->_slug = $id_slug_type_path_map[ $this->_module_id ]['slug'];
5647
- }
5648
-
5649
- return $this->_slug;
5650
- }
5651
-
5652
- /**
5653
- * @author Vova Feldman (@svovaf)
5654
- * @since 1.2.1.7
5655
- *
5656
- * @return string Plugin slug.
5657
- */
5658
- function get_target_folder_name() {
5659
- return $this->_slug . ( $this->can_use_premium_code() ? '-premium' : '' );
5660
- }
5661
-
5662
- /**
5663
- * @author Vova Feldman (@svovaf)
5664
- * @since 1.0.1
5665
- *
5666
- * @return number Plugin ID.
5667
- */
5668
- function get_id() {
5669
- return $this->_plugin->id;
5670
- }
5671
-
5672
- /**
5673
- * @author Vova Feldman (@svovaf)
5674
- * @since 1.2.1.5
5675
- *
5676
- * @return string Freemius SDK version
5677
- */
5678
- function get_sdk_version() {
5679
- return $this->version;
5680
- }
5681
-
5682
- /**
5683
- * @author Vova Feldman (@svovaf)
5684
- * @since 1.2.1.5
5685
- *
5686
- * @return number Parent plugin ID (if parent exist).
5687
- */
5688
- function get_parent_id() {
5689
- return $this->is_addon() ?
5690
- $this->get_parent_instance()->get_id() :
5691
- $this->_plugin->id;
5692
- }
5693
-
5694
- /**
5695
- * @author Vova Feldman (@svovaf)
5696
- * @since 1.0.1
5697
- *
5698
- * @return string Plugin public key.
5699
- */
5700
- function get_public_key() {
5701
- return $this->_plugin->public_key;
5702
- }
5703
-
5704
- /**
5705
- * Will be available only on sandbox mode.
5706
- *
5707
- * @author Vova Feldman (@svovaf)
5708
- * @since 1.0.4
5709
- *
5710
- * @return mixed Plugin secret key.
5711
- */
5712
- function get_secret_key() {
5713
- return $this->_plugin->secret_key;
5714
- }
5715
-
5716
- /**
5717
- * @author Vova Feldman (@svovaf)
5718
- * @since 1.1.1
5719
- *
5720
- * @return bool
5721
- */
5722
- function has_secret_key() {
5723
- return ! empty( $this->_plugin->secret_key );
5724
- }
5725
-
5726
- /**
5727
- * @author Vova Feldman (@svovaf)
5728
- * @since 1.0.9
5729
- *
5730
- * @return string
5731
- */
5732
- function get_plugin_name() {
5733
- $this->_logger->entrance();
5734
-
5735
- if ( ! isset( $this->_plugin_name ) ) {
5736
- $plugin_data = $this->get_plugin_data();
5737
-
5738
- // Get name.
5739
- $this->_plugin_name = $plugin_data['Name'];
5740
-
5741
- // Check if plugin name contains "(Premium)" suffix and remove it.
5742
- $suffix = ' (premium)';
5743
- $suffix_len = strlen( $suffix );
5744
-
5745
- if ( strlen( $plugin_data['Name'] ) > $suffix_len &&
5746
- $suffix === substr( strtolower( $plugin_data['Name'] ), - $suffix_len )
5747
- ) {
5748
- $this->_plugin_name = substr( $plugin_data['Name'], 0, - $suffix_len );
5749
- }
5750
-
5751
- $this->_logger->departure( 'Name = ' . $this->_plugin_name );
5752
- }
5753
-
5754
- return $this->_plugin_name;
5755
- }
5756
-
5757
- /**
5758
- * @author Vova Feldman (@svovaf)
5759
- * @since 1.0.0
5760
- *
5761
- * @return string
5762
- */
5763
- function get_plugin_version() {
5764
- $this->_logger->entrance();
5765
-
5766
- $plugin_data = $this->get_plugin_data();
5767
-
5768
- $this->_logger->departure( 'Version = ' . $plugin_data['Version'] );
5769
-
5770
- return $this->apply_filters( 'plugin_version', $plugin_data['Version'] );
5771
- }
5772
-
5773
- /**
5774
- * @author Vova Feldman (@svovaf)
5775
- * @since 1.2.1.7
5776
- *
5777
- * @return string
5778
- */
5779
- function get_plugin_title() {
5780
- $this->_logger->entrance();
5781
-
5782
- $title = $this->_plugin->title;
5783
-
5784
- return $this->apply_filters( 'plugin_title', $title );
5785
- }
5786
-
5787
- /**
5788
- * @author Vova Feldman (@svovaf)
5789
- * @since 1.2.2.7
5790
- *
5791
- * @param bool $lowercase
5792
- *
5793
- * @return string
5794
- */
5795
- function get_module_label( $lowercase = false ) {
5796
- $label = $this->is_addon() ?
5797
- $this->get_text( 'addon' ) :
5798
- ( $this->is_plugin() ?
5799
- $this->get_text( 'plugin' ) :
5800
- $this->get_text( 'theme' ) );
5801
-
5802
- if ( $lowercase ) {
5803
- $label = strtolower( $lowercase );
5804
- }
5805
-
5806
- return $label;
5807
- }
5808
-
5809
- /**
5810
- * @author Vova Feldman (@svovaf)
5811
- * @since 1.0.4
5812
- *
5813
- * @return string
5814
- */
5815
- function get_plugin_basename() {
5816
- if ( ! isset( $this->_plugin_basename ) ) {
5817
- if ( $this->is_plugin() ) {
5818
- $this->_plugin_basename = plugin_basename( $this->_plugin_main_file_path );
5819
- } else {
5820
- $this->_plugin_basename = basename( dirname( $this->_plugin_main_file_path ) );
5821
- }
5822
- }
5823
-
5824
- return $this->_plugin_basename;
5825
- }
5826
-
5827
- function get_plugin_folder_name() {
5828
- $this->_logger->entrance();
5829
-
5830
- $plugin_folder = $this->_plugin_basename;
5831
-
5832
- while ( '.' !== dirname( $plugin_folder ) ) {
5833
- $plugin_folder = dirname( $plugin_folder );
5834
- }
5835
-
5836
- $this->_logger->departure( 'Folder Name = ' . $plugin_folder );
5837
-
5838
- return $plugin_folder;
5839
- }
5840
-
5841
- #endregion ------------------------------------------------------------------
5842
-
5843
- /* Account
5844
- ------------------------------------------------------------------------------------------------------------------*/
5845
-
5846
- /**
5847
- * Find plugin's slug by plugin's basename.
5848
- *
5849
- * @author Vova Feldman (@svovaf)
5850
- * @since 1.0.9
5851
- *
5852
- * @param string $plugin_base_name
5853
- *
5854
- * @return false|string
5855
- */
5856
- private static function find_slug_by_basename( $plugin_base_name ) {
5857
- $file_slug_map = self::$_accounts->get_option( 'file_slug_map', array() );
5858
-
5859
- if ( ! array( $file_slug_map ) || ! isset( $file_slug_map[ $plugin_base_name ] ) ) {
5860
- return false;
5861
- }
5862
-
5863
- return $file_slug_map[ $plugin_base_name ];
5864
- }
5865
-
5866
- /**
5867
- * Store the map between the plugin's basename to the slug.
5868
- *
5869
- * @author Vova Feldman (@svovaf)
5870
- * @since 1.0.9
5871
- */
5872
- private function store_file_slug_map() {
5873
- $file_slug_map = self::$_accounts->get_option( 'file_slug_map', array() );
5874
-
5875
- if ( ! array( $file_slug_map ) ) {
5876
- $file_slug_map = array();
5877
- }
5878
-
5879
- if ( ! isset( $file_slug_map[ $this->_plugin_basename ] ) ||
5880
- $file_slug_map[ $this->_plugin_basename ] !== $this->_slug
5881
- ) {
5882
- $file_slug_map[ $this->_plugin_basename ] = $this->_slug;
5883
- self::$_accounts->set_option( 'file_slug_map', $file_slug_map, true );
5884
- }
5885
- }
5886
-
5887
- /**
5888
- * @return FS_User[]
5889
- */
5890
- static function get_all_users() {
5891
- $users = self::$_accounts->get_option( 'users', array() );
5892
-
5893
- if ( ! is_array( $users ) ) {
5894
- $users = array();
5895
- }
5896
-
5897
- return $users;
5898
- }
5899
-
5900
- /**
5901
- * @param string $module_type
5902
- *
5903
- * @return FS_Site[]
5904
- */
5905
- private static function get_all_sites( $module_type = WP_FS__MODULE_TYPE_PLUGIN ) {
5906
- $sites = self::get_account_option( 'sites', $module_type );
5907
-
5908
- if ( ! is_array( $sites ) ) {
5909
- $sites = array();
5910
- }
5911
-
5912
- return $sites;
5913
- }
5914
-
5915
- /**
5916
- * @author Leo Fajardo (@leorw)
5917
- *
5918
- * @since 1.2.2
5919
- *
5920
- * @param string $option_name
5921
- * @param string $module_type
5922
- *
5923
- * @return mixed
5924
- */
5925
- private static function get_account_option( $option_name, $module_type ) {
5926
- if ( WP_FS__MODULE_TYPE_PLUGIN !== $module_type ) {
5927
- $option_name = $module_type . '_' . $option_name;
5928
- }
5929
-
5930
- return self::$_accounts->get_option( $option_name, array() );
5931
- }
5932
-
5933
- /**
5934
- * @author Leo Fajardo (@leorw)
5935
- *
5936
- * @since 1.2.2
5937
- *
5938
- * @param string $option_name
5939
- * @param mixed $option_value
5940
- * @param bool $store
5941
- */
5942
- private function set_account_option( $option_name, $option_value, $store ) {
5943
- self::set_account_option_by_module(
5944
- $this->_module_type,
5945
- $option_name,
5946
- $option_value,
5947
- $store
5948
- );
5949
- }
5950
-
5951
- /**
5952
- * @author Vova Feldman (@svovaf)
5953
- *
5954
- * @since 1.2.2.7
5955
- *
5956
- * @param string $module_type
5957
- * @param string $option_name
5958
- * @param mixed $option_value
5959
- * @param bool $store
5960
- */
5961
- private static function set_account_option_by_module( $module_type, $option_name, $option_value, $store ) {
5962
- if ( WP_FS__MODULE_TYPE_PLUGIN != $module_type ) {
5963
- $option_name = $module_type . '_' . $option_name;
5964
- }
5965
-
5966
- self::$_accounts->set_option( $option_name, $option_value, $store );
5967
- }
5968
-
5969
- /**
5970
- * @author Vova Feldman (@svovaf)
5971
- * @since 1.0.6
5972
- *
5973
- * @param string $module_type
5974
- *
5975
- * @return FS_Plugin_License[]
5976
- */
5977
- private static function get_all_licenses( $module_type = WP_FS__MODULE_TYPE_PLUGIN ) {
5978
- $licenses = self::get_account_option( 'licenses', $module_type );
5979
-
5980
- if ( ! is_array( $licenses ) ) {
5981
- $licenses = array();
5982
- }
5983
-
5984
- return $licenses;
5985
- }
5986
-
5987
- /**
5988
- * @param string|bool $module_type
5989
- *
5990
- * @return FS_Plugin_Plan[]
5991
- */
5992
- private static function get_all_plans( $module_type = false ) {
5993
- $plans = self::get_account_option( 'plans', $module_type );
5994
-
5995
- if ( ! is_array( $plans ) ) {
5996
- $plans = array();
5997
- }
5998
-
5999
- return $plans;
6000
- }
6001
-
6002
- /**
6003
- * @author Vova Feldman (@svovaf)
6004
- * @since 1.0.4
6005
- *
6006
- * @return FS_Plugin_Tag[]
6007
- */
6008
- private static function get_all_updates() {
6009
- $updates = self::$_accounts->get_option( 'updates', array() );
6010
-
6011
- if ( ! is_array( $updates ) ) {
6012
- $updates = array();
6013
- }
6014
-
6015
- return $updates;
6016
- }
6017
-
6018
- /**
6019
- * @author Vova Feldman (@svovaf)
6020
- * @since 1.0.6
6021
- *
6022
- * @return array<number,FS_Plugin[]>|false
6023
- */
6024
- private static function get_all_addons() {
6025
- $addons = self::$_accounts->get_option( 'addons', array() );
6026
-
6027
- if ( ! is_array( $addons ) ) {
6028
- $addons = array();
6029
- }
6030
-
6031
- return $addons;
6032
- }
6033
-
6034
- /**
6035
- * @author Vova Feldman (@svovaf)
6036
- * @since 1.0.6
6037
- *
6038
- * @return FS_Plugin[]|false
6039
- */
6040
- private static function get_all_account_addons() {
6041
- $addons = self::$_accounts->get_option( 'account_addons', array() );
6042
-
6043
- if ( ! is_array( $addons ) ) {
6044
- $addons = array();
6045
- }
6046
-
6047
- return $addons;
6048
- }
6049
-
6050
- /**
6051
- * Check if user has connected his account (opted-in).
6052
- *
6053
- * Note:
6054
- * If the user opted-in and opted-out on a later stage,
6055
- * this will still return true. If you want to check if the
6056
- * user is currently opted-in, use:
6057
- * `$fs->is_registered() && $fs->is_tracking_allowed()`
6058
- *
6059
- * @author Vova Feldman (@svovaf)
6060
- * @since 1.0.1
6061
- * @return bool
6062
- */
6063
- function is_registered() {
6064
- return is_object( $this->_user );
6065
- }
6066
-
6067
- /**
6068
- * Returns TRUE if the user opted-in and didn't disconnect (opt-out).
6069
- *
6070
- * @author Leo Fajardo (@leorw)
6071
- * @since 1.2.1.5
6072
- *
6073
- * @return bool
6074
- */
6075
- function is_tracking_allowed() {
6076
- return ( is_object( $this->_site ) && true !== $this->_site->is_disconnected );
6077
- }
6078
-
6079
- /**
6080
- * @author Vova Feldman (@svovaf)
6081
- * @since 1.0.4
6082
- *
6083
- * @return FS_Plugin
6084
- */
6085
- function get_plugin() {
6086
- return $this->_plugin;
6087
- }
6088
-
6089
- /**
6090
- * @author Vova Feldman (@svovaf)
6091
- * @since 1.0.3
6092
- *
6093
- * @return FS_User
6094
- */
6095
- function get_user() {
6096
- return $this->_user;
6097
- }
6098
-
6099
- /**
6100
- * @author Vova Feldman (@svovaf)
6101
- * @since 1.0.3
6102
- *
6103
- * @return FS_Site
6104
- */
6105
- function get_site() {
6106
- return $this->_site;
6107
- }
6108
-
6109
- /**
6110
- * Get plugin add-ons.
6111
- *
6112
- * @author Vova Feldman (@svovaf)
6113
- * @since 1.0.6
6114
- *
6115
- * @since 1.1.7.3 If not yet loaded, fetch data from the API.
6116
- *
6117
- * @param bool $flush
6118
- *
6119
- * @return FS_Plugin[]|false
6120
- */
6121
- function get_addons( $flush = false ) {
6122
- $this->_logger->entrance();
6123
-
6124
- if ( ! $this->_has_addons ) {
6125
- return false;
6126
- }
6127
-
6128
- $addons = $this->sync_addons( $flush );
6129
-
6130
- return ( ! is_array( $addons ) || empty( $addons ) ) ?
6131
- false :
6132
- $addons;
6133
- }
6134
-
6135
- /**
6136
- * @author Vova Feldman (@svovaf)
6137
- * @since 1.0.6
6138
- *
6139
- * @return FS_Plugin[]|false
6140
- */
6141
- function get_account_addons() {
6142
- $this->_logger->entrance();
6143
-
6144
- $addons = self::get_all_account_addons();
6145
-
6146
- if ( ! is_array( $addons ) ||
6147
- ! isset( $addons[ $this->_plugin->id ] ) ||
6148
- ! is_array( $addons[ $this->_plugin->id ] ) ||
6149
- 0 === count( $addons[ $this->_plugin->id ] )
6150
- ) {
6151
- return false;
6152
- }
6153
-
6154
- return $addons[ $this->_plugin->id ];
6155
- }
6156
-
6157
- /**
6158
- * Check if user has any
6159
- *
6160
- * @author Vova Feldman (@svovaf)
6161
- * @since 1.1.6
6162
- *
6163
- * @return bool
6164
- */
6165
- function has_account_addons() {
6166
- $addons = $this->get_account_addons();
6167
-
6168
- return is_array( $addons ) && ( 0 < count( $addons ) );
6169
- }
6170
-
6171
-
6172
- /**
6173
- * Get add-on by ID (from local data).
6174
- *
6175
- * @author Vova Feldman (@svovaf)
6176
- * @since 1.0.6
6177
- *
6178
- * @param number $id
6179
- *
6180
- * @return FS_Plugin|false
6181
- */
6182
- function get_addon( $id ) {
6183
- $this->_logger->entrance();
6184
-
6185
- $addons = $this->get_addons();
6186
-
6187
- if ( is_array( $addons ) ) {
6188
- foreach ( $addons as $addon ) {
6189
- if ( $id == $addon->id ) {
6190
- return $addon;
6191
- }
6192
- }
6193
- }
6194
-
6195
- return false;
6196
- }
6197
-
6198
- /**
6199
- * Get add-on by slug (from local data).
6200
- *
6201
- * @author Vova Feldman (@svovaf)
6202
- * @since 1.0.6
6203
- *
6204
- * @param string $slug
6205
- *
6206
- * @param bool $flush
6207
- *
6208
- * @return FS_Plugin|false
6209
- */
6210
- function get_addon_by_slug( $slug, $flush = false ) {
6211
- $this->_logger->entrance();
6212
-
6213
- $addons = $this->get_addons( $flush );
6214
-
6215
- if ( is_array( $addons ) ) {
6216
- foreach ( $addons as $addon ) {
6217
- if ( $slug === $addon->slug ) {
6218
- return $addon;
6219
- }
6220
- }
6221
- }
6222
-
6223
- return false;
6224
- }
6225
-
6226
- #----------------------------------------------------------------------------------
6227
- #region Plans & Licensing
6228
- #----------------------------------------------------------------------------------
6229
-
6230
- /**
6231
- * Check if running premium plugin code.
6232
- *
6233
- * @author Vova Feldman (@svovaf)
6234
- * @since 1.0.5
6235
- *
6236
- * @return bool
6237
- */
6238
- function is_premium() {
6239
- return $this->_plugin->is_premium;
6240
- }
6241
-
6242
- /**
6243
- * Get site's plan ID.
6244
- *
6245
- * @author Vova Feldman (@svovaf)
6246
- * @since 1.0.2
6247
- *
6248
- * @return number
6249
- */
6250
- function get_plan_id() {
6251
- return $this->_site->plan->id;
6252
- }
6253
-
6254
- /**
6255
- * Get site's plan title.
6256
- *
6257
- * @author Vova Feldman (@svovaf)
6258
- * @since 1.0.2
6259
- *
6260
- * @return string
6261
- */
6262
- function get_plan_title() {
6263
- return $this->_site->plan->title;
6264
- }
6265
-
6266
- /**
6267
- * @author Vova Feldman (@svovaf)
6268
- * @since 1.0.9
6269
- *
6270
- * @return FS_Plugin_Plan|false
6271
- */
6272
- function get_plan() {
6273
- return is_object( $this->_site->plan ) ?
6274
- $this->_site->plan :
6275
- false;
6276
- }
6277
-
6278
- /**
6279
- * @author Vova Feldman (@svovaf)
6280
- * @since 1.0.3
6281
- *
6282
- * @return bool
6283
- */
6284
- function is_trial() {
6285
- $this->_logger->entrance();
6286
-
6287
- if ( ! $this->is_registered() ) {
6288
- return false;
6289
- }
6290
-
6291
- return $this->_site->is_trial();
6292
- }
6293
-
6294
- /**
6295
- * Check if currently in a trial with payment method (credit card or paypal).
6296
- *
6297
- * @author Vova Feldman (@svovaf)
6298
- * @since 1.1.7
6299
- *
6300
- * @return bool
6301
- */
6302
- function is_paid_trial() {
6303
- $this->_logger->entrance();
6304
-
6305
- if ( ! $this->is_trial() ) {
6306
- return false;
6307
- }
6308
-
6309
- return $this->has_active_valid_license() && ( $this->_site->trial_plan_id == $this->_license->plan_id );
6310
- }
6311
-
6312
- /**
6313
- * Check if trial already utilized.
6314
- *
6315
- * @since 1.0.9
6316
- *
6317
- * @return bool
6318
- */
6319
- function is_trial_utilized() {
6320
- $this->_logger->entrance();
6321
-
6322
- if ( ! $this->is_registered() ) {
6323
- return false;
6324
- }
6325
-
6326
- return $this->_site->is_trial_utilized();
6327
- }
6328
-
6329
- /**
6330
- * Get trial plan information (if in trial).
6331
- *
6332
- * @author Vova Feldman (@svovaf)
6333
- * @since 1.0.9
6334
- *
6335
- * @return bool|FS_Plugin_Plan
6336
- */
6337
- function get_trial_plan() {
6338
- $this->_logger->entrance();
6339
-
6340
- if ( ! $this->is_trial() ) {
6341
- return false;
6342
- }
6343
-
6344
- return $this->_storage->trial_plan;
6345
- }
6346
-
6347
- /**
6348
- * Check if the user has an activate, non-expired license on current plugin's install.
6349
- *
6350
- * @since 1.0.9
6351
- *
6352
- * @return bool
6353
- */
6354
- function is_paying() {
6355
- $this->_logger->entrance();
6356
-
6357
- if ( ! $this->is_registered() ) {
6358
- return false;
6359
- }
6360
-
6361
- if ( ! $this->has_paid_plan() ) {
6362
- return false;
6363
- }
6364
-
6365
- return (
6366
- ! $this->is_trial() &&
6367
- 'free' !== $this->_site->plan->name &&
6368
- $this->has_active_valid_license()
6369
- );
6370
- }
6371
-
6372
- /**
6373
- * @author Vova Feldman (@svovaf)
6374
- * @since 1.0.4
6375
- *
6376
- * @return bool
6377
- */
6378
- function is_free_plan() {
6379
- if ( ! $this->is_registered() ) {
6380
- return true;
6381
- }
6382
-
6383
- if ( ! $this->has_paid_plan() ) {
6384
- return true;
6385
- }
6386
-
6387
- return (
6388
- 'free' === $this->_site->plan->name ||
6389
- ! $this->has_features_enabled_license()
6390
- );
6391
- }
6392
-
6393
- /**
6394
- * @author Vova Feldman (@svovaf)
6395
- * @since 1.0.5
6396
- *
6397
- * @return bool
6398
- */
6399
- function _has_premium_license() {
6400
- $this->_logger->entrance();
6401
-
6402
- $premium_license = $this->_get_available_premium_license();
6403
-
6404
- return ( false !== $premium_license );
6405
- }
6406
-
6407
- /**
6408
- * Check if user has any licenses associated with the plugin (including expired or blocking).
6409
- *
6410
- * @author Vova Feldman (@svovaf)
6411
- * @since 1.1.7.3
6412
- *
6413
- * @return bool
6414
- */
6415
- private function has_any_license() {
6416
- return is_array( $this->_licenses ) && ( 0 < count( $this->_licenses ) );
6417
- }
6418
-
6419
- /**
6420
- * @author Vova Feldman (@svovaf)
6421
- * @since 1.0.5
6422
- *
6423
- * @return FS_Plugin_License|false
6424
- */
6425
- function _get_available_premium_license() {
6426
- $this->_logger->entrance();
6427
-
6428
- if ( ! $this->has_paid_plan() ) {
6429
- return false;
6430
- }
6431
-
6432
- if ( is_array( $this->_licenses ) ) {
6433
- foreach ( $this->_licenses as $license ) {
6434
- if ( ! $license->is_utilized() && $license->is_features_enabled() ) {
6435
- return $license;
6436
- }
6437
- }
6438
- }
6439
-
6440
- return false;
6441
- }
6442
-
6443
- /**
6444
- * Sync local plugin plans with remote server.
6445
- *
6446
- * @author Vova Feldman (@svovaf)
6447
- * @since 1.0.5
6448
- *
6449
- * @return FS_Plugin_Plan[]|object
6450
- */
6451
- function _sync_plans() {
6452
- $plans = $this->_fetch_plugin_plans();
6453
-
6454
- if ( $this->is_array_instanceof( $plans, 'FS_Plugin_Plan' ) ) {
6455
- $this->_plans = $plans;
6456
- $this->_store_plans();
6457
- }
6458
-
6459
- $this->do_action( 'after_plans_sync', $plans );
6460
-
6461
- return $this->_plans;
6462
- }
6463
-
6464
- /**
6465
- * @author Vova Feldman (@svovaf)
6466
- * @since 1.0.5
6467
- *
6468
- * @param number $id
6469
- *
6470
- * @return FS_Plugin_Plan|false
6471
- */
6472
- function _get_plan_by_id( $id ) {
6473
- $this->_logger->entrance();
6474
-
6475
- if ( ! is_array( $this->_plans ) || 0 === count( $this->_plans ) ) {
6476
- $this->_sync_plans();
6477
- }
6478
-
6479
- foreach ( $this->_plans as $plan ) {
6480
- if ( $id == $plan->id ) {
6481
- return $plan;
6482
- }
6483
- }
6484
-
6485
- return false;
6486
- }
6487
-
6488
- /**
6489
- * @author Vova Feldman (@svovaf)
6490
- * @since 1.1.8.1
6491
- *
6492
- * @param string $name
6493
- *
6494
- * @return FS_Plugin_Plan|false
6495
- */
6496
- private function get_plan_by_name( $name ) {
6497
- $this->_logger->entrance();
6498
-
6499
- if ( ! is_array( $this->_plans ) || 0 === count( $this->_plans ) ) {
6500
- $this->_sync_plans();
6501
- }
6502
-
6503
- foreach ( $this->_plans as $plan ) {
6504
- if ( $name == $plan->name ) {
6505
- return $plan;
6506
- }
6507
- }
6508
-
6509
- return false;
6510
- }
6511
-
6512
- /**
6513
- * Sync local plugin plans with remote server.
6514
- *
6515
- * @author Vova Feldman (@svovaf)
6516
- * @since 1.0.6
6517
- *
6518
- * @param number|bool $site_license_id
6519
- *
6520
- * @return FS_Plugin_License[]|object
6521
- */
6522
- function _sync_licenses( $site_license_id = false ) {
6523
- $licenses = $this->_fetch_licenses( false, $site_license_id );
6524
-
6525
- if ( $this->is_array_instanceof( $licenses, 'FS_Plugin_License' ) ) {
6526
- $this->_licenses = $licenses;
6527
- $this->_store_licenses();
6528
- }
6529
-
6530
- // Update current license.
6531
- if ( is_object( $this->_license ) ) {
6532
- $this->_license = $this->_get_license_by_id( $this->_license->id );
6533
- }
6534
-
6535
- return $this->_licenses;
6536
- }
6537
-
6538
- /**
6539
- * @author Vova Feldman (@svovaf)
6540
- * @since 1.0.5
6541
- *
6542
- * @param number $id
6543
- *
6544
- * @return FS_Plugin_License|false
6545
- */
6546
- function _get_license_by_id( $id ) {
6547
- $this->_logger->entrance();
6548
-
6549
- if ( ! is_numeric( $id ) ) {
6550
- return false;
6551
- }
6552
-
6553
- if ( ! $this->has_any_license() ) {
6554
- $this->_sync_licenses();
6555
- }
6556
-
6557
- foreach ( $this->_licenses as $license ) {
6558
- if ( $id == $license->id ) {
6559
- return $license;
6560
- }
6561
- }
6562
-
6563
- return false;
6564
- }
6565
-
6566
- /**
6567
- * Sync site's license with user licenses.
6568
- *
6569
- * @author Vova Feldman (@svovaf)
6570
- * @since 1.0.6
6571
- *
6572
- * @param FS_Plugin_License|null $new_license
6573
- */
6574
- function _update_site_license( $new_license ) {
6575
- $this->_logger->entrance();
6576
-
6577
- $this->_license = $new_license;
6578
-
6579
- if ( ! is_object( $new_license ) ) {
6580
- $this->_site->license_id = null;
6581
- $this->_sync_site_subscription( null );
6582
-
6583
- return;
6584
- }
6585
-
6586
- $this->_site->license_id = $this->_license->id;
6587
-
6588
- if ( ! is_array( $this->_licenses ) ) {
6589
- $this->_licenses = array();
6590
- }
6591
-
6592
- $is_license_found = false;
6593
- for ( $i = 0, $len = count( $this->_licenses ); $i < $len; $i ++ ) {
6594
- if ( $new_license->id == $this->_licenses[ $i ]->id ) {
6595
- $this->_licenses[ $i ] = $new_license;
6596
-
6597
- $is_license_found = true;
6598
- break;
6599
- }
6600
- }
6601
-
6602
- // If new license just append.
6603
- if ( ! $is_license_found ) {
6604
- $this->_licenses[] = $new_license;
6605
- }
6606
-
6607
- $this->_sync_site_subscription( $new_license );
6608
- }
6609
-
6610
- /**
6611
- * Sync site's subscription.
6612
- *
6613
- * @author Vova Feldman (@svovaf)
6614
- * @since 1.0.9
6615
- *
6616
- * @param FS_Plugin_License|null $license
6617
- *
6618
- * @return bool|\FS_Subscription
6619
- */
6620
- private function _sync_site_subscription( $license ) {
6621
- if ( ! is_object( $license ) ) {
6622
- unset( $this->_storage->subscription );
6623
-
6624
- return false;
6625
- }
6626
-
6627
- // Load subscription details if not lifetime.
6628
- $subscription = $license->is_lifetime() ?
6629
- false :
6630
- $this->_fetch_site_license_subscription();
6631
-
6632
- if ( is_object( $subscription ) && ! isset( $subscription->error ) ) {
6633
- $this->_storage->subscription = $subscription;
6634
- } else {
6635
- unset( $this->_storage->subscription );
6636
- }
6637
-
6638
- return $subscription;
6639
- }
6640
-
6641
- /**
6642
- * @author Vova Feldman (@svovaf)
6643
- * @since 1.0.6
6644
- *
6645
- * @return bool|\FS_Plugin_License
6646
- */
6647
- function _get_license() {
6648
- return $this->_license;
6649
- }
6650
-
6651
- /**
6652
- * @return bool|\FS_Subscription
6653
- */
6654
- function _get_subscription() {
6655
- return isset( $this->_storage->subscription ) ?
6656
- $this->_storage->subscription :
6657
- false;
6658
- }
6659
-
6660
- /**
6661
- * @author Vova Feldman (@svovaf)
6662
- * @since 1.0.2
6663
- *
6664
- * @param string $plan Plan name
6665
- * @param bool $exact If true, looks for exact plan. If false, also check "higher" plans.
6666
- *
6667
- * @return bool
6668
- */
6669
- function is_plan( $plan, $exact = false ) {
6670
- $this->_logger->entrance();
6671
-
6672
- if ( ! $this->is_registered() ) {
6673
- return false;
6674
- }
6675
-
6676
- $plan = strtolower( $plan );
6677
-
6678
- if ( $this->_site->plan->name === $plan ) // Exact plan.
6679
- {
6680
- return true;
6681
- } else if ( $exact ) // Required exact, but plans are different.
6682
- {
6683
- return false;
6684
- }
6685
-
6686
- $current_plan_order = - 1;
6687
- $required_plan_order = - 1;
6688
- for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
6689
- if ( $plan === $this->_plans[ $i ]->name ) {
6690
- $required_plan_order = $i;
6691
- } else if ( $this->_site->plan->name === $this->_plans[ $i ]->name ) {
6692
- $current_plan_order = $i;
6693
- }
6694
- }
6695
-
6696
- return ( $current_plan_order > $required_plan_order );
6697
- }
6698
-
6699
- /**
6700
- * Check if module has only one plan.
6701
- *
6702
- * @author Vova Feldman (@svovaf)
6703
- * @since 1.2.1.7
6704
- *
6705
- * @return bool
6706
- */
6707
- function is_single_plan() {
6708
- $this->_logger->entrance();
6709
-
6710
- if ( ! $this->is_registered() ||
6711
- ! is_array( $this->_plans ) ||
6712
- 0 === count( $this->_plans )
6713
- ) {
6714
- return true;
6715
- }
6716
-
6717
- return ( 1 === count( $this->_plans ) );
6718
- }
6719
-
6720
- /**
6721
- * Check if plan based on trial. If not in trial mode, should return false.
6722
- *
6723
- * @since 1.0.9
6724
- *
6725
- * @param string $plan Plan name
6726
- * @param bool $exact If true, looks for exact plan. If false, also check "higher" plans.
6727
- *
6728
- * @return bool
6729
- */
6730
- function is_trial_plan( $plan, $exact = false ) {
6731
- $this->_logger->entrance();
6732
-
6733
- if ( ! $this->is_registered() ) {
6734
- return false;
6735
- }
6736
-
6737
- if ( ! $this->is_trial() ) {
6738
- return false;
6739
- }
6740
-
6741
- if ( ! isset( $this->_storage->trial_plan ) ) {
6742
- // Store trial plan information.
6743
- $this->_enrich_site_trial_plan( true );
6744
- }
6745
-
6746
- if ( $this->_storage->trial_plan->name === $plan ) // Exact plan.
6747
- {
6748
- return true;
6749
- } else if ( $exact ) // Required exact, but plans are different.
6750
- {
6751
- return false;
6752
- }
6753
-
6754
- $current_plan_order = - 1;
6755
- $required_plan_order = - 1;
6756
- for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
6757
- if ( $plan === $this->_plans[ $i ]->name ) {
6758
- $required_plan_order = $i;
6759
- } else if ( $this->_storage->trial_plan->name === $this->_plans[ $i ]->name ) {
6760
- $current_plan_order = $i;
6761
- }
6762
- }
6763
-
6764
- return ( $current_plan_order > $required_plan_order );
6765
- }
6766
-
6767
- /**
6768
- * Check if plugin has any paid plans.
6769
- *
6770
- * @author Vova Feldman (@svovaf)
6771
- * @since 1.0.7
6772
- *
6773
- * @return bool
6774
- */
6775
- function has_paid_plan() {
6776
- return $this->_has_paid_plans ||
6777
- FS_Plan_Manager::instance()->has_paid_plan( $this->_plans );
6778
- }
6779
-
6780
- /**
6781
- * Check if plugin has any plan with a trail.
6782
- *
6783
- * @author Vova Feldman (@svovaf)
6784
- * @since 1.0.9
6785
- *
6786
- * @return bool
6787
- */
6788
- function has_trial_plan() {
6789
- if ( ! $this->is_registered() ) {
6790
- /**
6791
- * @author Vova Feldman(@svovaf)
6792
- * @since 1.2.1.5
6793
- *
6794
- * Allow setting a trial from the SDK without calling the API.
6795
- * But, if the user did opt-in, continue using the real data from the API.
6796
- */
6797
- if ( $this->_trial_days >= 0 ) {
6798
- return true;
6799
- }
6800
-
6801
- return false;
6802
- }
6803
-
6804
- return $this->_storage->get( 'has_trial_plan', false );
6805
- }
6806
-
6807
- /**
6808
- * Check if plugin has any free plan, or is it premium only.
6809
- *
6810
- * Note: If no plans configured, assume plugin is free.
6811
- *
6812
- * @author Vova Feldman (@svovaf)
6813
- * @since 1.0.7
6814
- *
6815
- * @return bool
6816
- */
6817
- function has_free_plan() {
6818
- return ! $this->is_only_premium();
6819
- }
6820
-
6821
- /**
6822
- * Displays a license activation dialog box when the user clicks on the "Activate License"
6823
- * or "Change License" link on the plugins
6824
- * page.
6825
- *
6826
- * @author Leo Fajardo (@leorw)
6827
- * @since 1.1.9
6828
- */
6829
- function _add_license_activation_dialog_box() {
6830
- $vars = array(
6831
- 'id' => $this->_module_id,
6832
- );
6833
-
6834
- fs_require_template( 'forms/license-activation.php', $vars );
6835
- fs_require_template( 'forms/resend-key.php', $vars );
6836
- }
6837
-
6838
- /**
6839
- * Displays the opt-out dialog box when the user clicks on the "Opt Out" link on the "Plugins"
6840
- * page.
6841
- *
6842
- * @author Leo Fajardo (@leorw)
6843
- * @since 1.2.1.5
6844
- */
6845
- function _add_optout_dialog() {
6846
- if ( $this->is_theme() ) {
6847
- $vars = null;
6848
- fs_require_once_template( '/js/jquery.content-change.php', $vars );
6849
- }
6850
-
6851
- $vars = array( 'id' => $this->_module_id );
6852
- fs_require_template( 'forms/optout.php', $vars );
6853
- }
6854
-
6855
- /**
6856
- * Prepare page to include all required UI and logic for the license activation dialog.
6857
- *
6858
- * @author Vova Feldman (@svovaf)
6859
- * @since 1.2.0
6860
- */
6861
- function _add_license_activation() {
6862
- if ( ! $this->is_user_admin() ) {
6863
- // Only admins can activate a license.
6864
- return;
6865
- }
6866
-
6867
- if ( ! $this->has_paid_plan() ) {
6868
- // Module doesn't have any paid plans.
6869
- return;
6870
- }
6871
-
6872
- if ( ! $this->is_premium() ) {
6873
- // Only add license activation logic to the premium version.
6874
- return;
6875
- }
6876
-
6877
- // Add license activation link and AJAX request handler.
6878
- if ( self::is_plugins_page() ) {
6879
- /**
6880
- * @since 1.2.0 Add license action link only on plugins page.
6881
- */
6882
- $this->_add_license_action_link();
6883
- }
6884
-
6885
- // Add license activation AJAX callback.
6886
- $this->add_ajax_action( 'activate_license', array( &$this, '_activate_license_ajax_action' ) );
6887
-
6888
- // Add resend license AJAX callback.
6889
- $this->add_ajax_action( 'resend_license_key', array( &$this, '_resend_license_key_ajax_action' ) );
6890
- }
6891
-
6892
- /**
6893
- * @author Leo Fajardo (@leorw)
6894
- * @since 1.1.9
6895
- */
6896
- function _activate_license_ajax_action() {
6897
- $this->_logger->entrance();
6898
-
6899
- $this->check_ajax_referer( 'activate_license' );
6900
-
6901
- $license_key = trim( fs_request_get( 'license_key' ) );
6902
-
6903
- if ( empty( $license_key ) ) {
6904
- exit;
6905
- }
6906
-
6907
- $plugin_id = fs_request_get( 'module_id', '', 'post' );
6908
- $fs = ( $plugin_id == $this->_module_id ) ?
6909
- $this :
6910
- $this->get_addon_instance( $plugin_id );
6911
-
6912
- $error = false;
6913
- $next_page = false;
6914
-
6915
- if ( $fs->is_registered() ) {
6916
- $api = $fs->get_api_site_scope();
6917
- $install = $api->call( '/', 'put', array(
6918
- 'license_key' => $fs->apply_filters( 'license_key', $license_key )
6919
- ) );
6920
-
6921
- if ( isset( $install->error ) ) {
6922
- $error = $install->error->message;
6923
- } else {
6924
- $parent_fs = $fs->is_addon() ?
6925
- $fs->get_parent_instance() :
6926
- $fs;
6927
-
6928
- $next_page = $parent_fs->_get_sync_license_url( $fs->get_id(), true );
6929
-
6930
- $fs->reconnect_locally();
6931
- }
6932
- } else {
6933
- $next_page = $fs->opt_in( false, false, false, $license_key );
6934
-
6935
- if ( isset( $next_page->error ) ) {
6936
- $error = $next_page->error;
6937
- }
6938
- }
6939
-
6940
- $result = array(
6941
- 'success' => ( false === $error )
6942
- );
6943
-
6944
- if ( false !== $error ) {
6945
- $result['error'] = $error;
6946
- } else {
6947
- $result['next_page'] = $next_page;
6948
- }
6949
-
6950
- echo json_encode( $result );
6951
-
6952
- exit;
6953
- }
6954
-
6955
- /**
6956
- * Billing update AJAX callback.
6957
- *
6958
- * @author Vova Feldman (@svovaf)
6959
- * @since 1.2.1.5
6960
- */
6961
- function _update_billing_ajax_action() {
6962
- $this->_logger->entrance();
6963
-
6964
- $this->check_ajax_referer( 'update_billing' );
6965
-
6966
- if ( ! $this->is_user_admin() ) {
6967
- // Only for admins.
6968
- self::shoot_ajax_failure();
6969
- }
6970
-
6971
- $billing = fs_request_get( 'billing' );
6972
-
6973
- $api = $this->get_api_user_scope();
6974
- $result = $api->call( '/billing.json', 'put', array_merge( $billing, array(
6975
- 'plugin_id' => $this->get_parent_id(),
6976
- ) ) );
6977
-
6978
- if ( ! $this->is_api_result_entity( $result ) ) {
6979
- self::shoot_ajax_failure();
6980
- }
6981
-
6982
- // Purge cached billing.
6983
- $this->get_api_user_scope()->purge_cache( 'billing.json' );
6984
-
6985
- self::shoot_ajax_success();
6986
- }
6987
-
6988
- /**
6989
- * Trial start for anonymous users (AJAX callback).
6990
- *
6991
- * @author Vova Feldman (@svovaf)
6992
- * @since 1.2.1.5
6993
- */
6994
- function _start_trial_ajax_action() {
6995
- $this->_logger->entrance();
6996
-
6997
- $this->check_ajax_referer( 'start_trial' );
6998
-
6999
- if ( ! $this->is_user_admin() ) {
7000
- // Only for admins.
7001
- self::shoot_ajax_failure();
7002
- }
7003
-
7004
- $trial_data = fs_request_get( 'trial' );
7005
-
7006
- $next_page = $this->opt_in(
7007
- false,
7008
- false,
7009
- false,
7010
- false,
7011
- false,
7012
- $trial_data['plan_id']
7013
- );
7014
-
7015
- if ( is_object( $next_page ) && $this->is_api_error( $next_page ) ) {
7016
- self::shoot_ajax_failure(
7017
- isset( $next_page->error ) ?
7018
- $next_page->error->message :
7019
- var_export( $next_page, true )
7020
- );
7021
- }
7022
-
7023
- $this->shoot_ajax_success( array(
7024
- 'next_page' => $next_page,
7025
- ) );
7026
- }
7027
-
7028
- /**
7029
- * @author Leo Fajardo (@leorw)
7030
- * @since 1.2.0
7031
- */
7032
- function _resend_license_key_ajax_action() {
7033
- $this->_logger->entrance();
7034
-
7035
- $this->check_ajax_referer( 'resend_license_key' );
7036
-
7037
- $email_address = sanitize_email( trim( fs_request_get( 'email', '', 'post' ) ) );
7038
-
7039
- if ( empty( $email_address ) ) {
7040
- exit;
7041
- }
7042
-
7043
- $error = false;
7044
-
7045
- $api = $this->get_api_plugin_scope();
7046
- $result = $api->call( '/licenses/resend.json', 'post',
7047
- array(
7048
- 'email' => $email_address,
7049
- 'url' => home_url(),
7050
- )
7051
- );
7052
-
7053
- if ( is_object( $result ) && isset( $result->error ) ) {
7054
- $error = $result->error;
7055
-
7056
- if ( in_array( $error->code, array( 'invalid_email', 'no_user' ) ) ) {
7057
- $error = $this->get_text( 'email-not-found' );
7058
- } else if ( 'no_license' === $error->code ) {
7059
- $error = $this->get_text( 'no-active-licenses' );
7060
- } else {
7061
- $error = $error->message;
7062
- }
7063
- }
7064
-
7065
- $licenses = array(
7066
- 'success' => ( false === $error )
7067
- );
7068
-
7069
- if ( false !== $error ) {
7070
- $licenses['error'] = sprintf( '%s... %s', $this->get_text( 'oops' ), strtolower( $error ) );
7071
- }
7072
-
7073
- echo json_encode( $licenses );
7074
-
7075
- exit;
7076
- }
7077
-
7078
- /**
7079
- * @author Vova Feldman (@svovaf)
7080
- * @since 1.2.1.8
7081
- *
7082
- * @var string
7083
- */
7084
- private static $_pagenow;
7085
-
7086
- /**
7087
- * Get current page or the referer if executing a WP AJAX request.
7088
- *
7089
- * @author Vova Feldman (@svovaf)
7090
- * @since 1.2.1.8
7091
- *
7092
- * @return string
7093
- */
7094
- static function get_current_page() {
7095
- if ( ! isset( self::$_pagenow ) ) {
7096
- global $pagenow;
7097
-
7098
- self::$_pagenow = $pagenow;
7099
-
7100
- if ( self::is_ajax() &&
7101
- 'admin-ajax.php' === $pagenow
7102
- ) {
7103
- $referer = wp_get_raw_referer();
7104
-
7105
- if ( is_string( $referer ) ) {
7106
- $parts = explode( '?', $referer );
7107
-
7108
- self::$_pagenow = basename( $parts[0] );
7109
- }
7110
- }
7111
- }
7112
-
7113
- return self::$_pagenow;
7114
- }
7115
-
7116
- /**
7117
- * Helper method to check if user in the plugins page.
7118
- *
7119
- * @author Vova Feldman (@svovaf)
7120
- * @since 1.2.1.5
7121
- *
7122
- * @return bool
7123
- */
7124
- static function is_plugins_page() {
7125
- return ( 'plugins.php' === self::get_current_page() );
7126
- }
7127
-
7128
- /**
7129
- * Helper method to check if user in the themes page.
7130
- *
7131
- * @author Vova Feldman (@svovaf)
7132
- * @since 1.2.2.6
7133
- *
7134
- * @return bool
7135
- */
7136
- static function is_themes_page() {
7137
- return ( 'themes.php' === self::get_current_page() );
7138
- }
7139
-
7140
- #----------------------------------------------------------------------------------
7141
- #region URL Generators
7142
- #----------------------------------------------------------------------------------
7143
-
7144
- /**
7145
- * Alias to pricing_url().
7146
- *
7147
- * @author Vova Feldman (@svovaf)
7148
- * @since 1.0.2
7149
- *
7150
- * @uses pricing_url()
7151
- *
7152
- * @param string $period Billing cycle
7153
- * @param bool $is_trial
7154
- *
7155
- * @return string
7156
- */
7157
- function get_upgrade_url( $period = WP_FS__PERIOD_ANNUALLY, $is_trial = false ) {
7158
- return $this->pricing_url( $period, $is_trial );
7159
- }
7160
-
7161
- /**
7162
- * @author Vova Feldman (@svovaf)
7163
- * @since 1.0.9
7164
- *
7165
- * @uses get_upgrade_url()
7166
- *
7167
- * @return string
7168
- */
7169
- function get_trial_url() {
7170
- return $this->get_upgrade_url( WP_FS__PERIOD_ANNUALLY, true );
7171
- }
7172
-
7173
- /**
7174
- * Plugin's pricing URL.
7175
- *
7176
- * @author Vova Feldman (@svovaf)
7177
- * @since 1.0.4
7178
- *
7179
- * @param string $billing_cycle Billing cycle
7180
- *
7181
- * @param bool $is_trial
7182
- *
7183
- * @return string
7184
- */
7185
- function pricing_url( $billing_cycle = WP_FS__PERIOD_ANNUALLY, $is_trial = false ) {
7186
- $this->_logger->entrance();
7187
-
7188
- $params = array(
7189
- 'billing_cycle' => $billing_cycle
7190
- );
7191
-
7192
- if ( $is_trial ) {
7193
- $params['trial'] = 'true';
7194
- }
7195
-
7196
- return $this->_get_admin_page_url( 'pricing', $params );
7197
- }
7198
-
7199
- /**
7200
- * Checkout page URL.
7201
- *
7202
- * @author Vova Feldman (@svovaf)
7203
- * @since 1.0.6
7204
- *
7205
- * @param string $billing_cycle Billing cycle
7206
- * @param bool $is_trial
7207
- * @param array $extra (optional) Extra parameters, override other query params.
7208
- *
7209
- * @return string
7210
- */
7211
- function checkout_url(
7212
- $billing_cycle = WP_FS__PERIOD_ANNUALLY,
7213
- $is_trial = false,
7214
- $extra = array()
7215
- ) {
7216
- $this->_logger->entrance();
7217
-
7218
- $params = array(
7219
- 'checkout' => 'true',
7220
- 'billing_cycle' => $billing_cycle,
7221
- );
7222
-
7223
- if ( $is_trial ) {
7224
- $params['trial'] = 'true';
7225
- }
7226
-
7227
- /**
7228
- * Params in extra override other params.
7229
- */
7230
- $params = array_merge( $params, $extra );
7231
-
7232
- return $this->_get_admin_page_url( 'pricing', $params );
7233
- }
7234
-
7235
- /**
7236
- * Add-on checkout URL.
7237
- *
7238
- * @author Vova Feldman (@svovaf)
7239
- * @since 1.1.7
7240
- *
7241
- * @param number $addon_id
7242
- * @param number $pricing_id
7243
- * @param string $billing_cycle
7244
- * @param bool $is_trial
7245
- *
7246
- * @return string
7247
- */
7248
- function addon_checkout_url(
7249
- $addon_id,
7250
- $pricing_id,
7251
- $billing_cycle = WP_FS__PERIOD_ANNUALLY,
7252
- $is_trial = false
7253
- ) {
7254
- return $this->checkout_url( $billing_cycle, $is_trial, array(
7255
- 'plugin_id' => $addon_id,
7256
- 'pricing_id' => $pricing_id,
7257
- ) );
7258
- }
7259
-
7260
- #endregion
7261
-
7262
- #endregion ------------------------------------------------------------------
7263
-
7264
- /**
7265
- * Check if plugin has any add-ons.
7266
- *
7267
- * @author Vova Feldman (@svovaf)
7268
- * @since 1.0.5
7269
- *
7270
- * @since 1.1.7.3 Base logic only on the parameter provided by the developer in the init function.
7271
- *
7272
- * @return bool
7273
- */
7274
- function has_addons() {
7275
- $this->_logger->entrance();
7276
-
7277
- return $this->_has_addons;
7278
- }
7279
-
7280
- /**
7281
- * Check if plugin can work in anonymous mode.
7282
- *
7283
- * @author Vova Feldman (@svovaf)
7284
- * @since 1.0.9
7285
- *
7286
- * @return bool
7287
- *
7288
- * @deprecated Please use is_enable_anonymous() instead
7289
- */
7290
- function enable_anonymous() {
7291
- return $this->_enable_anonymous;
7292
- }
7293
-
7294
- /**
7295
- * Check if plugin can work in anonymous mode.
7296
- *
7297
- * @author Vova Feldman (@svovaf)
7298
- * @since 1.1.9
7299
- *
7300
- * @return bool
7301
- */
7302
- function is_enable_anonymous() {
7303
- return $this->_enable_anonymous;
7304
- }
7305
-
7306
- /**
7307
- * Check if plugin is premium only (no free plans).
7308
- *
7309
- * @author Vova Feldman (@svovaf)
7310
- * @since 1.1.9
7311
- *
7312
- * @return bool
7313
- */
7314
- function is_only_premium() {
7315
- return $this->_is_premium_only;
7316
- }
7317
-
7318
- /**
7319
- * Checks if the plugin's type is "plugin". The other type is "theme".
7320
- *
7321
- * @author Leo Fajardo (@leorw)
7322
- * @since 1.2.2
7323
- *
7324
- * @return bool
7325
- */
7326
- function is_plugin() {
7327
- return ( WP_FS__MODULE_TYPE_PLUGIN === $this->_module_type );
7328
- }
7329
-
7330
- /**
7331
- * @author Leo Fajardo (@leorw)
7332
- * @since 1.2.2
7333
- *
7334
- * @return string
7335
- */
7336
- function get_module_type() {
7337
- if ( ! isset( $this->_module_type ) ) {
7338
- $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
7339
- $this->_module_type = $id_slug_type_path_map[ $this->_module_id ]['type'];
7340
- }
7341
-
7342
- return $this->_module_type;
7343
- }
7344
-
7345
- /**
7346
- * @author Leo Fajardo (@leorw)
7347
- * @since 1.2.2
7348
- *
7349
- * @return string
7350
- */
7351
- function get_plugin_main_file_path() {
7352
- return $this->_plugin_main_file_path;
7353
- }
7354
-
7355
- /**
7356
- * Check if module has a premium code version.
7357
- *
7358
- * Serviceware module might be freemium without any
7359
- * premium code version, where the paid features
7360
- * are all part of the service.
7361
- *
7362
- * @author Vova Feldman (@svovaf)
7363
- * @since 1.2.1.6
7364
- *
7365
- * @return bool
7366
- */
7367
- function has_premium_version() {
7368
- return $this->_has_premium_version;
7369
- }
7370
-
7371
- /**
7372
- * Check if feature supported with current site's plan.
7373
- *
7374
- * @author Vova Feldman (@svovaf)
7375
- * @since 1.0.1
7376
- *
7377
- * @todo IMPLEMENT
7378
- *
7379
- * @param number $feature_id
7380
- *
7381
- * @throws Exception
7382
- */
7383
- function is_feature_supported( $feature_id ) {
7384
- throw new Exception( 'not implemented' );
7385
- }
7386
-
7387
- /**
7388
- * @author Vova Feldman (@svovaf)
7389
- * @since 1.0.1
7390
- *
7391
- * @return bool Is running in SSL/HTTPS
7392
- */
7393
- function is_ssl() {
7394
- return WP_FS__IS_HTTPS;
7395
- }
7396
-
7397
- /**
7398
- * @author Vova Feldman (@svovaf)
7399
- * @since 1.0.9
7400
- *
7401
- * @return bool Is running in AJAX call.
7402
- *
7403
- * @link http://wordpress.stackexchange.com/questions/70676/how-to-check-if-i-am-in-admin-ajax
7404
- */
7405
- static function is_ajax() {
7406
- return ( defined( 'DOING_AJAX' ) && DOING_AJAX );
7407
- }
7408
-
7409
- /**
7410
- * Check if it's an AJAX call targeted for the current module.
7411
- *
7412
- * @author Vova Feldman (@svovaf)
7413
- * @since 1.2.0
7414
- *
7415
- * @param array|string $actions Collection of AJAX actions.
7416
- *
7417
- * @return bool
7418
- */
7419
- function is_ajax_action( $actions ) {
7420
- // Verify it's an ajax call.
7421
- if ( ! self::is_ajax() ) {
7422
- return false;
7423
- }
7424
-
7425
- // Verify the call is relevant for the plugin.
7426
- if ( $this->_module_id != fs_request_get( 'module_id' ) ) {
7427
- return false;
7428
- }
7429
-
7430
- // Verify it's one of the specified actions.
7431
- if ( is_string( $actions ) ) {
7432
- $actions = explode( ',', $actions );
7433
- }
7434
-
7435
- if ( is_array( $actions ) && 0 < count( $actions ) ) {
7436
- $ajax_action = fs_request_get( 'action' );
7437
-
7438
- foreach ( $actions as $action ) {
7439
- if ( $ajax_action === $this->get_action_tag( $action ) ) {
7440
- return true;
7441
- }
7442
- }
7443
- }
7444
-
7445
- return false;
7446
- }
7447
-
7448
- /**
7449
- * Check if it's an AJAX call targeted for current request.
7450
- *
7451
- * @author Vova Feldman (@svovaf)
7452
- * @since 1.2.0
7453
- *
7454
- * @param array|string $actions Collection of AJAX actions.
7455
- * @param number|null $module_id
7456
- *
7457
- * @return bool
7458
- */
7459
- static function is_ajax_action_static( $actions, $module_id = null ) {
7460
- // Verify it's an ajax call.
7461
- if ( ! self::is_ajax() ) {
7462
- return false;
7463
- }
7464
-
7465
-
7466
- if ( ! empty( $module_id ) ) {
7467
- // Verify the call is relevant for the plugin.
7468
- if ( $module_id != fs_request_get( 'module_id' ) ) {
7469
- return false;
7470
- }
7471
- }
7472
-
7473
- // Verify it's one of the specified actions.
7474
- if ( is_string( $actions ) ) {
7475
- $actions = explode( ',', $actions );
7476
- }
7477
-
7478
- if ( is_array( $actions ) && 0 < count( $actions ) ) {
7479
- $ajax_action = fs_request_get( 'action' );
7480
-
7481
- foreach ( $actions as $action ) {
7482
- if ( $ajax_action === self::get_ajax_action_static( $action, $module_id ) ) {
7483
- return true;
7484
- }
7485
- }
7486
- }
7487
-
7488
- return false;
7489
- }
7490
-
7491
- /**
7492
- * @author Vova Feldman (@svovaf)
7493
- * @since 1.1.7
7494
- *
7495
- * @return bool
7496
- */
7497
- static function is_cron() {
7498
- return ( defined( 'DOING_CRON' ) && DOING_CRON );
7499
- }
7500
-
7501
- /**
7502
- * Check if a real user is visiting the admin dashboard.
7503
- *
7504
- * @author Vova Feldman (@svovaf)
7505
- * @since 1.1.7
7506
- *
7507
- * @return bool
7508
- */
7509
- function is_user_in_admin() {
7510
- return is_admin() && ! self::is_ajax() && ! self::is_cron();
7511
- }
7512
-
7513
- /**
7514
- * Check if a real user is in the customizer view.
7515
- *
7516
- * @author Vova Feldman (@svovaf)
7517
- * @since 1.2.2.7
7518
- *
7519
- * @return bool
7520
- */
7521
- static function is_customizer() {
7522
- return is_customize_preview();
7523
- }
7524
-
7525
- /**
7526
- * Check if running in HTTPS and if site's plan matching the specified plan.
7527
- *
7528
- * @param string $plan
7529
- * @param bool $exact
7530
- *
7531
- * @return bool
7532
- */
7533
- function is_ssl_and_plan( $plan, $exact = false ) {
7534
- return ( $this->is_ssl() && $this->is_plan( $plan, $exact ) );
7535
- }
7536
-
7537
- /**
7538
- * Construct plugin's settings page URL.
7539
- *
7540
- * @author Vova Feldman (@svovaf)
7541
- * @since 1.0.4
7542
- *
7543
- * @param string $page
7544
- * @param array $params
7545
- *
7546
- * @return string
7547
- */
7548
- function _get_admin_page_url( $page = '', $params = array() ) {
7549
- if ( 0 < count( $params ) ) {
7550
- foreach ( $params as $k => $v ) {
7551
- $params[ $k ] = urlencode( $v );
7552
- }
7553
- }
7554
-
7555
- $page_param = $this->_menu->get_slug( $page );
7556
-
7557
- if ( empty( $page ) &&
7558
- $this->is_theme() &&
7559
- // Show the opt-in as an overlay for free wp.org themes or themes without any settings page.
7560
- ( $this->is_free_wp_org_theme() || ! $this->has_settings_menu() ) ) {
7561
- $params[ $this->get_unique_affix() . '_show_optin' ] = 'true';
7562
-
7563
- return add_query_arg(
7564
- $params,
7565
- admin_url( 'themes.php' )
7566
- );
7567
- }
7568
-
7569
- if ( ! $this->has_settings_menu() ) {
7570
- if ( ! empty( $page ) ) {
7571
- // Module doesn't have a setting page, but since the request is for
7572
- // a specific Freemius page, use the admin.php path.
7573
- return add_query_arg( array_merge( $params, array(
7574
- 'page' => $page_param,
7575
- ) ), admin_url( 'admin.php' ) );
7576
- } else {
7577
- if ( $this->is_activation_mode() ) {
7578
- /**
7579
- * @author Vova Feldman
7580
- * @since 1.2.1.6
7581
- *
7582
- * If plugin doesn't have a settings page, create one for the opt-in screen.
7583
- */
7584
- return add_query_arg( array_merge( $params, array(
7585
- 'page' => $this->_slug,
7586
- ) ), admin_url( 'admin.php', 'admin' ) );
7587
- } else {
7588
- // Plugin without a settings page.
7589
- return admin_url( 'plugins.php' );
7590
- }
7591
- }
7592
- }
7593
-
7594
- // Module has a submenu settings page.
7595
- if ( ! $this->_menu->is_top_level() ) {
7596
- $parent_slug = $this->_menu->get_parent_slug();
7597
- $menu_file = ( false !== strpos( $parent_slug, '.php' ) ) ?
7598
- $parent_slug :
7599
- 'admin.php';
7600
-
7601
- return add_query_arg( array_merge( $params, array(
7602
- 'page' => $page_param,
7603
- ) ), admin_url( $menu_file, 'admin' ) );
7604
- }
7605
-
7606
- // Module has a top level CPT settings page.
7607
- if ( $this->_menu->is_cpt() ) {
7608
- if ( empty( $page ) && $this->is_activation_mode() ) {
7609
- return add_query_arg( array_merge( $params, array(
7610
- 'page' => $page_param
7611
- ) ), admin_url( 'admin.php', 'admin' ) );
7612
- } else {
7613
- if ( ! empty( $page ) ) {
7614
- $params['page'] = $page_param;
7615
- }
7616
-
7617
- return add_query_arg(
7618
- $params,
7619
- admin_url( $this->_menu->get_raw_slug(), 'admin' )
7620
- );
7621
- }
7622
- }
7623
-
7624
- // Module has a custom top level settings page.
7625
- return add_query_arg( array_merge( $params, array(
7626
- 'page' => $page_param,
7627
- ) ), admin_url( 'admin.php', 'admin' ) );
7628
- }
7629
-
7630
- /**
7631
- * Check if currently in a specified admin page.
7632
- *
7633
- * @author Vova Feldman (@svovaf)
7634
- * @since 1.2.2.7
7635
- *
7636
- * @param string $page
7637
- *
7638
- * @return bool
7639
- */
7640
- function is_admin_page( $page ) {
7641
- return ( $this->_menu->get_slug( $page ) === fs_request_get( 'page', '', 'get' ) );
7642
- }
7643
-
7644
- /**
7645
- * Get module's main admin setting page URL.
7646
- *
7647
- * @author Vova Feldman (@svovaf)
7648
- * @since 1.2.2.7
7649
- *
7650
- * @return string
7651
- */
7652
- function main_menu_url() {
7653
- return $this->_menu->main_menu_url();
7654
- }
7655
-
7656
- /**
7657
- * Check if currently on the theme's setting page or
7658
- * on any of the Freemius added pages (via tabs).
7659
- *
7660
- * @author Vova Feldman (@svovaf)
7661
- * @since 1.2.2.7
7662
- *
7663
- * @return bool
7664
- */
7665
- function is_theme_settings_page() {
7666
- return fs_starts_with(
7667
- fs_request_get( 'page', '', 'get' ),
7668
- $this->_menu->get_slug()
7669
- );
7670
- }
7671
-
7672
- /**
7673
- * Plugin's account page + sync license URL.
7674
- *
7675
- * @author Vova Feldman (@svovaf)
7676
- * @since 1.1.9.1
7677
- *
7678
- * @param bool|number $plugin_id
7679
- * @param bool $add_action_nonce
7680
- * @param array $params
7681
- *
7682
- * @return string
7683
- */
7684
- function _get_sync_license_url( $plugin_id = false, $add_action_nonce = true, $params = array() ) {
7685
- if ( is_numeric( $plugin_id ) ) {
7686
- $params['plugin_id'] = $plugin_id;
7687
- }
7688
-
7689
- return $this->get_account_url(
7690
- $this->get_unique_affix() . '_sync_license',
7691
- $params,
7692
- $add_action_nonce
7693
- );
7694
- }
7695
-
7696
- /**
7697
- * Plugin's account URL.
7698
- *
7699
- * @author Vova Feldman (@svovaf)
7700
- * @since 1.0.4
7701
- *
7702
- * @param bool|string $action
7703
- * @param array $params
7704
- *
7705
- * @param bool $add_action_nonce
7706
- *
7707
- * @return string
7708
- */
7709
- function get_account_url( $action = false, $params = array(), $add_action_nonce = true ) {
7710
- if ( is_string( $action ) ) {
7711
- $params['fs_action'] = $action;
7712
- }
7713
-
7714
- self::require_pluggable_essentials();
7715
-
7716
- return ( $add_action_nonce && is_string( $action ) ) ?
7717
- fs_nonce_url( $this->_get_admin_page_url( 'account', $params ), $action ) :
7718
- $this->_get_admin_page_url( 'account', $params );
7719
- }
7720
-
7721
- /**
7722
- * @author Vova Feldman (@svovaf)
7723
- * @since 1.2.0
7724
- *
7725
- * @param string $tab
7726
- * @param bool $action
7727
- * @param array $params
7728
- * @param bool $add_action_nonce
7729
- *
7730
- * @return string
7731
- *
7732
- * @uses get_account_url()
7733
- */
7734
- function get_account_tab_url( $tab, $action = false, $params = array(), $add_action_nonce = true ) {
7735
- $params['tab'] = $tab;
7736
-
7737
- return $this->get_account_url( $action, $params, $add_action_nonce );
7738
- }
7739
-
7740
- /**
7741
- * Plugin's account URL.
7742
- *
7743
- * @author Vova Feldman (@svovaf)
7744
- * @since 1.0.4
7745
- *
7746
- * @param bool|string $topic
7747
- * @param bool|string $message
7748
- *
7749
- * @return string
7750
- */
7751
- function contact_url( $topic = false, $message = false ) {
7752
- $params = array();
7753
- if ( is_string( $topic ) ) {
7754
- $params['topic'] = $topic;
7755
- }
7756
- if ( is_string( $message ) ) {
7757
- $params['message'] = $message;
7758
- }
7759
-
7760
- if ( $this->is_addon() ) {
7761
- $params['addon_id'] = $this->get_id();
7762
-
7763
- return $this->get_parent_instance()->_get_admin_page_url( 'contact', $params );
7764
- } else {
7765
- return $this->_get_admin_page_url( 'contact', $params );
7766
- }
7767
- }
7768
-
7769
- /**
7770
- * Add-on direct info URL.
7771
- *
7772
- * @author Vova Feldman (@svovaf)
7773
- * @since 1.1.0
7774
- *
7775
- * @param string $slug
7776
- *
7777
- * @return string
7778
- */
7779
- function addon_url( $slug ) {
7780
- return $this->_get_admin_page_url( 'addons', array(
7781
- 'slug' => $slug
7782
- ) );
7783
- }
7784
-
7785
- /* Logger
7786
- ------------------------------------------------------------------------------------------------------------------*/
7787
- /**
7788
- * @param string $id
7789
- * @param bool $prefix_slug
7790
- *
7791
- * @return FS_Logger
7792
- */
7793
- function get_logger( $id = '', $prefix_slug = true ) {
7794
- return FS_Logger::get_logger( ( $prefix_slug ? $this->_slug : '' ) . ( ( ! $prefix_slug || empty( $id ) ) ? '' : '_' ) . $id );
7795
- }
7796
-
7797
- /**
7798
- * @param $id
7799
- * @param bool $load_options
7800
- * @param bool $prefix_slug
7801
- *
7802
- * @return FS_Option_Manager
7803
- */
7804
- function get_options_manager( $id, $load_options = false, $prefix_slug = true ) {
7805
- return FS_Option_Manager::get_manager( ( $prefix_slug ? $this->_slug : '' ) . ( ( ! $prefix_slug || empty( $id ) ) ? '' : '_' ) . $id, $load_options );
7806
- }
7807
-
7808
- /* Security
7809
- ------------------------------------------------------------------------------------------------------------------*/
7810
- private static function _encrypt( $str ) {
7811
- if ( is_null( $str ) ) {
7812
- return null;
7813
- }
7814
-
7815
- /**
7816
- * The encrypt/decrypt functions are used to protect
7817
- * the user from messing up with some of the sensitive
7818
- * data stored for the module as a JSON in the database.
7819
- *
7820
- * I used the same suggested hack by the theme review team.
7821
- * For more details, look at the function `Base64UrlDecode()`
7822
- * in `./sdk/FreemiusBase.php`.
7823
- *
7824
- * @todo Remove this hack once the base64 error is removed from the Theme Check.
7825
- *
7826
- * @author Vova Feldman (@svovaf)
7827
- * @since 1.2.2
7828
- */
7829
- $fn = 'base64' . '_encode';
7830
-
7831
- return $fn( $str );
7832
- }
7833
-
7834
- static function _decrypt( $str ) {
7835
- if ( is_null( $str ) ) {
7836
- return null;
7837
- }
7838
-
7839
- /**
7840
- * The encrypt/decrypt functions are used to protect
7841
- * the user from messing up with some of the sensitive
7842
- * data stored for the module as a JSON in the database.
7843
- *
7844
- * I used the same suggested hack by the theme review team.
7845
- * For more details, look at the function `Base64UrlDecode()`
7846
- * in `./sdk/FreemiusBase.php`.
7847
- *
7848
- * @todo Remove this hack once the base64 error is removed from the Theme Check.
7849
- *
7850
- * @author Vova Feldman (@svovaf)
7851
- * @since 1.2.2
7852
- */
7853
- $fn = 'base64' . '_decode';
7854
-
7855
- return $fn( $str );
7856
- }
7857
-
7858
- /**
7859
- * @author Vova Feldman (@svovaf)
7860
- * @since 1.0.5
7861
- *
7862
- * @param FS_Entity $entity
7863
- *
7864
- * @return FS_Entity Return an encrypted clone entity.
7865
- */
7866
- private static function _encrypt_entity( FS_Entity $entity ) {
7867
- $clone = clone $entity;
7868
- $props = get_object_vars( $entity );
7869
-
7870
- foreach ( $props as $key => $val ) {
7871
- $clone->{$key} = self::_encrypt( $val );
7872
- }
7873
-
7874
- return $clone;
7875
- }
7876
-
7877
- /**
7878
- * @author Vova Feldman (@svovaf)
7879
- * @since 1.0.5
7880
- *
7881
- * @param FS_Entity $entity
7882
- *
7883
- * @return FS_Entity Return an decrypted clone entity.
7884
- */
7885
- private static function decrypt_entity( FS_Entity $entity ) {
7886
- $clone = clone $entity;
7887
- $props = get_object_vars( $entity );
7888
-
7889
- foreach ( $props as $key => $val ) {
7890
- $clone->{$key} = self::_decrypt( $val );
7891
- }
7892
-
7893
- return $clone;
7894
- }
7895
-
7896
- /**
7897
- * Tries to activate account based on POST params.
7898
- *
7899
- * @author Vova Feldman (@svovaf)
7900
- * @since 1.0.2
7901
- */
7902
- function _activate_account() {
7903
- if ( $this->is_registered() ) {
7904
- // Already activated.
7905
- return;
7906
- }
7907
-
7908
- self::_clean_admin_content_section();
7909
-
7910
- if ( fs_request_is_action( 'activate' ) && fs_request_is_post() ) {
7911
- // check_admin_referer( 'activate_' . $this->_plugin->public_key );
7912
-
7913
- // Verify matching plugin details.
7914
- if ( $this->_plugin->id != fs_request_get( 'plugin_id' ) || $this->_slug != fs_request_get( 'plugin_slug' ) ) {
7915
- return;
7916
- }
7917
-
7918
- $user = new FS_User();
7919
- $user->id = fs_request_get( 'user_id' );
7920
- $user->public_key = fs_request_get( 'user_public_key' );
7921
- $user->secret_key = fs_request_get( 'user_secret_key' );
7922
- $user->email = fs_request_get( 'user_email' );
7923
- $user->first = fs_request_get( 'user_first' );
7924
- $user->last = fs_request_get( 'user_last' );
7925
- $user->is_verified = fs_request_get_bool( 'user_is_verified' );
7926
-
7927
- $site = new FS_Site();
7928
- $site->id = fs_request_get( 'install_id' );
7929
- $site->public_key = fs_request_get( 'install_public_key' );
7930
- $site->secret_key = fs_request_get( 'install_secret_key' );
7931
- $site->plan->id = fs_request_get( 'plan_id' );
7932
- $site->plan->title = fs_request_get( 'plan_title' );
7933
- $site->plan->name = fs_request_get( 'plan_name' );
7934
-
7935
- $plans = array();
7936
- $plans_data = json_decode( urldecode( fs_request_get( 'plans' ) ) );
7937
- foreach ( $plans_data as $p ) {
7938
- $plans[] = new FS_Plugin_Plan( $p );
7939
- }
7940
-
7941
- $this->_set_account( $user, $site, $plans );
7942
-
7943
- // Reload the page with the keys.
7944
- fs_redirect( $this->_get_admin_page_url() );
7945
- }
7946
- }
7947
-
7948
- /**
7949
- * @author Vova Feldman (@svovaf)
7950
- * @since 1.0.7
7951
- *
7952
- * @param string $email
7953
- *
7954
- * @return FS_User|bool
7955
- */
7956
- static function _get_user_by_email( $email ) {
7957
- self::$_static_logger->entrance();
7958
-
7959
- $email = trim( strtolower( $email ) );
7960
- $users = self::get_all_users();
7961
- if ( is_array( $users ) ) {
7962
- foreach ( $users as $u ) {
7963
- if ( $email === trim( strtolower( $u->email ) ) ) {
7964
- return $u;
7965
- }
7966
- }
7967
- }
7968
-
7969
- return false;
7970
- }
7971
-
7972
- #----------------------------------------------------------------------------------
7973
- #region Account (Loading, Updates & Activation)
7974
- #----------------------------------------------------------------------------------
7975
-
7976
- /***
7977
- * Load account information (user + site).
7978
- *
7979
- * @author Vova Feldman (@svovaf)
7980
- * @since 1.0.1
7981
- */
7982
- private function _load_account() {
7983
- $this->_logger->entrance();
7984
-
7985
- $this->do_action( 'before_account_load' );
7986
-
7987
- $sites = self::get_all_sites( $this->_module_type );
7988
- $users = self::get_all_users();
7989
- $plans = self::get_all_plans( $this->_module_type );
7990
- $licenses = self::get_all_licenses( $this->_module_type );
7991
-
7992
- if ( $this->_logger->is_on() && is_admin() ) {
7993
- $this->_logger->log( 'sites = ' . var_export( $sites, true ) );
7994
- $this->_logger->log( 'users = ' . var_export( $users, true ) );
7995
- $this->_logger->log( 'plans = ' . var_export( $plans, true ) );
7996
- $this->_logger->log( 'licenses = ' . var_export( $licenses, true ) );
7997
- }
7998
-
7999
- $site = isset( $sites[ $this->_slug ] ) ? $sites[ $this->_slug ] : false;
8000
-
8001
- if ( is_object( $site ) &&
8002
- is_numeric( $site->id ) &&
8003
- is_numeric( $site->user_id ) &&
8004
- is_object( $site->plan )
8005
- ) {
8006
- // Load site.
8007
- $this->_site = clone $site;
8008
- $this->_site->plan = self::decrypt_entity( $this->_site->plan );
8009
-
8010
- // Load relevant user.
8011
- $this->_user = clone $users[ $this->_site->user_id ];
8012
-
8013
- // Load plans.
8014
- $this->_plans = $plans[ $this->_slug ];
8015
- if ( ! is_array( $this->_plans ) || empty( $this->_plans ) ) {
8016
- $this->_sync_plans();
8017
- } else {
8018
- for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
8019
- if ( $this->_plans[ $i ] instanceof FS_Plugin_Plan ) {
8020
- $this->_plans[ $i ] = self::decrypt_entity( $this->_plans[ $i ] );
8021
- } else {
8022
- unset( $this->_plans[ $i ] );
8023
- }
8024
- }
8025
- }
8026
-
8027
- // Load licenses.
8028
- $this->_licenses = array();
8029
- if ( is_array( $licenses ) &&
8030
- isset( $licenses[ $this->_slug ] ) &&
8031
- isset( $licenses[ $this->_slug ][ $this->_user->id ] )
8032
- ) {
8033
- $this->_licenses = $licenses[ $this->_slug ][ $this->_user->id ];
8034
- }
8035
-
8036
- $this->_license = $this->_get_license_by_id( $this->_site->license_id );
8037
-
8038
- if ( $this->_site->version != $this->get_plugin_version() ) {
8039
- // If stored install version is different than current installed plugin version,
8040
- // then update plugin version event.
8041
- $this->update_plugin_version_event();
8042
- }
8043
- }
8044
-
8045
- $this->_register_account_hooks();
8046
- }
8047
-
8048
- /**
8049
- * @author Vova Feldman (@svovaf)
8050
- * @since 1.0.1
8051
- *
8052
- * @param FS_User $user
8053
- * @param FS_Site $site
8054
- * @param bool|array $plans
8055
- */
8056
- private function _set_account( FS_User $user, FS_Site $site, $plans = false ) {
8057
- $site->slug = $this->_slug;
8058
- $site->user_id = $user->id;
8059
-
8060
- $this->_site = $site;
8061
- $this->_user = $user;
8062
- if ( false !== $plans ) {
8063
- $this->_plans = $plans;
8064
- }
8065
-
8066
- $this->send_install_update();
8067
-
8068
- $this->_store_account();
8069
-
8070
- }
8071
-
8072
- /**
8073
- * @author Vova Feldman (@svovaf)
8074
- * @since 1.1.7.4
8075
- *
8076
- * @param array $override_with
8077
- *
8078
- * @return array
8079
- */
8080
- function get_opt_in_params( $override_with = array() ) {
8081
- $this->_logger->entrance();
8082
-
8083
- $current_user = self::_get_current_wp_user();
8084
-
8085
- $activation_action = $this->get_unique_affix() . '_activate_new';
8086
- $return_url = $this->is_anonymous() ?
8087
- // If skipped already, then return to the account page.
8088
- $this->get_account_url( $activation_action, array(), false ) :
8089
- // Return to the module's main page.
8090
- $this->get_after_activation_url( 'after_connect_url', array( 'fs_action' => $activation_action ) );
8091
-
8092
- $params = array(
8093
- 'user_firstname' => $current_user->user_firstname,
8094
- 'user_lastname' => $current_user->user_lastname,
8095
- 'user_nickname' => $current_user->user_nicename,
8096
- 'user_email' => $current_user->user_email,
8097
- 'user_ip' => WP_FS__REMOTE_ADDR,
8098
- 'plugin_slug' => $this->_slug,
8099
- 'plugin_id' => $this->get_id(),
8100
- 'plugin_public_key' => $this->get_public_key(),
8101
- 'plugin_version' => $this->get_plugin_version(),
8102
- 'return_url' => fs_nonce_url( $return_url, $activation_action ),
8103
- 'account_url' => fs_nonce_url( $this->_get_admin_page_url(
8104
- 'account',
8105
- array( 'fs_action' => 'sync_user' )
8106
- ), 'sync_user' ),
8107
- 'site_uid' => $this->get_anonymous_id(),
8108
- 'site_url' => get_site_url(),
8109
- 'site_name' => get_bloginfo( 'name' ),
8110
- 'platform_version' => get_bloginfo( 'version' ),
8111
- 'sdk_version' => $this->version,
8112
- 'programming_language_version' => phpversion(),
8113
- 'language' => get_bloginfo( 'language' ),
8114
- 'charset' => get_bloginfo( 'charset' ),
8115
- 'is_premium' => $this->is_premium(),
8116
- 'is_active' => true,
8117
- 'is_uninstalled' => false,
8118
- );
8119
-
8120
- if ( $this->is_pending_activation() &&
8121
- ! empty( $this->_storage->pending_license_key )
8122
- ) {
8123
- $params['license_key'] = $this->_storage->pending_license_key;
8124
- }
8125
-
8126
- if ( WP_FS__SKIP_EMAIL_ACTIVATION && $this->has_secret_key() ) {
8127
- // Even though rand() is known for its security issues,
8128
- // the timestamp adds another layer of protection.
8129
- // It would be very hard for an attacker to get the secret key form here.
8130
- // Plus, this should never run in production since the secret should never
8131
- // be included in the production version.
8132
- $params['ts'] = WP_FS__SCRIPT_START_TIME;
8133
- $params['salt'] = md5( uniqid( rand() ) );
8134
- $params['secure'] = md5(
8135
- $params['ts'] .
8136
- $params['salt'] .
8137
- $this->get_secret_key()
8138
- );
8139
- }
8140
-
8141
- return array_merge( $params, $override_with );
8142
- }
8143
-
8144
- /**
8145
- * 1. If successful opt-in or pending activation returns the next page that the user should be redirected to.
8146
- * 2. If there was an API error, return the API result.
8147
- *
8148
- * @author Vova Feldman (@svovaf)
8149
- * @since 1.1.7.4
8150
- *
8151
- * @param string|bool $email
8152
- * @param string|bool $first
8153
- * @param string|bool $last
8154
- * @param string|bool $license_key
8155
- * @param bool $is_uninstall If "true", this means that the module is currently being uninstalled.
8156
- * In this case, the user and site info will be sent to the server but no
8157
- * data will be saved to the WP installation's database.
8158
- * @param number|bool $trial_plan_id
8159
- *
8160
- * @return string|object
8161
- * @use WP_Error
8162
- */
8163
- function opt_in(
8164
- $email = false,
8165
- $first = false,
8166
- $last = false,
8167
- $license_key = false,
8168
- $is_uninstall = false,
8169
- $trial_plan_id = false
8170
- ) {
8171
- $this->_logger->entrance();
8172
-
8173
- if ( false === $email ) {
8174
- $current_user = self::_get_current_wp_user();
8175
- $email = $current_user->user_email;
8176
- }
8177
-
8178
- /**
8179
- * @since 1.2.1 If activating with license key, ignore the context-user
8180
- * since the user will be automatically loaded from the license.
8181
- */
8182
- if ( empty( $license_key ) ) {
8183
- // Clean up pending license if opt-ing in again.
8184
- $this->_storage->remove( 'pending_license_key' );
8185
-
8186
- if ( ! $is_uninstall ) {
8187
- $fs_user = Freemius::_get_user_by_email( $email );
8188
- if ( is_object( $fs_user ) && ! $this->is_pending_activation() ) {
8189
- return $this->install_with_current_user( false, $trial_plan_id );
8190
- }
8191
- }
8192
- }
8193
-
8194
- $user_info = array();
8195
- if ( ! empty( $email ) ) {
8196
- $user_info['user_email'] = $email;
8197
- }
8198
- if ( ! empty( $first ) ) {
8199
- $user_info['user_firstname'] = $first;
8200
- }
8201
- if ( ! empty( $last ) ) {
8202
- $user_info['user_lastname'] = $last;
8203
- }
8204
-
8205
- $params = $this->get_opt_in_params( $user_info );
8206
-
8207
- $filtered_license_key = false;
8208
- if ( is_string( $license_key ) ) {
8209
- $filtered_license_key = $this->apply_filters( 'license_key', $license_key );
8210
- $params['license_key'] = $filtered_license_key;
8211
- } else if ( FS_Plugin_Plan::is_valid_id( $trial_plan_id ) ) {
8212
- $params['trial_plan_id'] = $trial_plan_id;
8213
- }
8214
-
8215
- if ( $is_uninstall ) {
8216
- $params['uninstall_params'] = array(
8217
- 'reason_id' => $this->_storage->uninstall_reason->id,
8218
- 'reason_info' => $this->_storage->uninstall_reason->info
8219
- );
8220
- }
8221
-
8222
- $params['format'] = 'json';
8223
-
8224
- $url = WP_FS__ADDRESS . '/action/service/user/install/';
8225
- if ( isset( $_COOKIE['XDEBUG_SESSION'] ) ) {
8226
- $url = add_query_arg( 'XDEBUG_SESSION', 'PHPSTORM', $url );
8227
- }
8228
-
8229
- $response = wp_remote_post( $url, array(
8230
- 'method' => 'POST',
8231
- 'body' => $params,
8232
- 'timeout' => 15,
8233
- ) );
8234
-
8235
- if ( $response instanceof WP_Error ) {
8236
- if ( 'https://' === substr( $url, 0, 8 ) &&
8237
- isset( $response->errors ) &&
8238
- isset( $response->errors['http_request_failed'] )
8239
- ) {
8240
- $http_error = strtolower( $response->errors['http_request_failed'][0] );
8241
-
8242
- if ( false !== strpos( $http_error, 'ssl' ) ) {
8243
- // Failed due to old version of cURL or Open SSL (SSLv3 is not supported by CloudFlare).
8244
- $url = 'http://' . substr( $url, 8 );
8245
-
8246
- $response = wp_remote_post( $url, array(
8247
- 'method' => 'POST',
8248
- 'body' => $params,
8249
- 'timeout' => 15,
8250
- ) );
8251
- }
8252
- }
8253
- }
8254
-
8255
- if ( is_wp_error( $response ) ) {
8256
- /**
8257
- * @var WP_Error $response
8258
- */
8259
- $result = new stdClass();
8260
-
8261
- $error_code = $response->get_error_code();
8262
- $error_type = str_replace( ' ', '', ucwords( str_replace( '_', ' ', $error_code ) ) );
8263
-
8264
- $result->error = (object) array(
8265
- 'type' => $error_type,
8266
- 'message' => $response->get_error_message(),
8267
- 'code' => $error_code,
8268
- 'http' => 402
8269
- );
8270
-
8271
- return $result;
8272
- }
8273
-
8274
- // Module is being uninstalled, don't handle the returned data.
8275
- if ( $is_uninstall ) {
8276
- return true;
8277
- }
8278
-
8279
- $decoded = @json_decode( $response['body'] );
8280
-
8281
- if ( empty( $decoded ) ) {
8282
- return false;
8283
- }
8284
-
8285
- if ( ! $this->is_api_result_object( $decoded ) ) {
8286
- if ( ! empty( $params['license_key'] ) ) {
8287
- // Pass the fully entered license key to the failure handler.
8288
- $params['license_key'] = $license_key;
8289
- }
8290
-
8291
- return $is_uninstall ?
8292
- $decoded :
8293
- $this->apply_filters( 'after_install_failure', $decoded, $params );
8294
- } else if ( isset( $decoded->pending_activation ) && $decoded->pending_activation ) {
8295
- // Pending activation, add message.
8296
- return $this->set_pending_confirmation(
8297
- true,
8298
- false,
8299
- $filtered_license_key,
8300
- ! empty( $params['trial_plan_id'] )
8301
- );
8302
- } else if ( isset( $decoded->install_secret_key ) ) {
8303
- return $this->install_with_new_user(
8304
- $decoded->user_id,
8305
- $decoded->user_public_key,
8306
- $decoded->user_secret_key,
8307
- $decoded->install_id,
8308
- $decoded->install_public_key,
8309
- $decoded->install_secret_key,
8310
- false
8311
- );
8312
- }
8313
-
8314
- return $decoded;
8315
- }
8316
-
8317
- /**
8318
- * Set user and site identities.
8319
- *
8320
- * @author Vova Feldman (@svovaf)
8321
- * @since 1.0.9
8322
- *
8323
- * @param FS_User $user
8324
- * @param FS_Site $site
8325
- * @param bool $redirect
8326
- * @param bool $auto_install Since 1.2.1.7 If `true` and setting up an account with a valid license, will
8327
- * redirect (or return a URL) to the account page with a special parameter to
8328
- * trigger the auto installation processes.
8329
- *
8330
- * @return string If redirect is `false`, returns the next page the user should be redirected to.
8331
- */
8332
- function setup_account(
8333
- FS_User $user,
8334
- FS_Site $site,
8335
- $redirect = true,
8336
- $auto_install = false
8337
- ) {
8338
- $this->_user = $user;
8339
- $this->_site = $site;
8340
-
8341
- $this->_sync_plans();
8342
-
8343
- $this->_enrich_site_plan( false );
8344
-
8345
- $this->_set_account( $user, $site );
8346
-
8347
- if ( $this->is_trial() ) {
8348
- // Store trial plan information.
8349
- $this->_enrich_site_trial_plan( true );
8350
- }
8351
-
8352
- // If Freemius was OFF before, turn it on.
8353
- $this->turn_on();
8354
-
8355
- $this->do_action( 'after_account_connection', $user, $site );
8356
-
8357
- if ( is_numeric( $site->license_id ) ) {
8358
- $this->_license = $this->_get_license_by_id( $site->license_id );
8359
- }
8360
-
8361
- $this->_admin_notices->remove_sticky( 'connect_account' );
8362
-
8363
- if ( $this->is_pending_activation() ) {
8364
- // Remove pending activation sticky notice (if still exist).
8365
- $this->_admin_notices->remove_sticky( 'activation_pending' );
8366
-
8367
- // Remove plugin from pending activation mode.
8368
- unset( $this->_storage->is_pending_activation );
8369
-
8370
- if ( ! $this->is_paying_or_trial() ) {
8371
- $this->_admin_notices->add_sticky(
8372
- sprintf( $this->get_text( 'plugin-x-activation-message' ), '<b>' . $this->get_plugin_name() . '</b>' ),
8373
- 'activation_complete'
8374
- );
8375
- }
8376
- }
8377
-
8378
- if ( $this->is_paying_or_trial() ) {
8379
- if ( ! $this->is_premium() || ! $this->has_premium_version() ) {
8380
- if ( $this->is_paying() ) {
8381
- $this->_admin_notices->add_sticky(
8382
- sprintf(
8383
- $this->get_text( 'activation-with-plan-x-message' ),
8384
- $this->_site->plan->title
8385
- ) . $this->get_complete_upgrade_instructions(),
8386
- 'plan_upgraded',
8387
- $this->get_text( 'yee-haw' ) . '!'
8388
- );
8389
- } else {
8390
- $this->_admin_notices->add_sticky(
8391
- sprintf(
8392
- $this->get_text( 'trial-started-message' ),
8393
- '<i>' . $this->get_plugin_name() . '</i>'
8394
- ) . $this->get_complete_upgrade_instructions( $this->_storage->trial_plan->title ),
8395
- 'trial_started',
8396
- $this->get_text( 'yee-haw' ) . '!'
8397
- );
8398
- }
8399
- }
8400
-
8401
- $this->_admin_notices->remove_sticky( array(
8402
- 'trial_promotion',
8403
- ) );
8404
- }
8405
-
8406
- $plugin_id = fs_request_get( 'plugin_id', false );
8407
-
8408
- // Store activation time ONLY for plugins (not add-ons).
8409
- if ( ! is_numeric( $plugin_id ) || ( $plugin_id == $this->_plugin->id ) ) {
8410
- $this->_storage->activation_timestamp = WP_FS__SCRIPT_START_TIME;
8411
- }
8412
-
8413
- $next_page = '';
8414
-
8415
- $extra = array();
8416
- if ( $auto_install ) {
8417
- $extra['auto_install'] = 'true';
8418
- }
8419
-
8420
- if ( is_numeric( $plugin_id ) ) {
8421
- /**
8422
- * @author Leo Fajardo
8423
- * @since 1.2.1.6
8424
- *
8425
- * Also sync the license after an anonymous user subscribes.
8426
- */
8427
- if ( $this->is_anonymous() || $plugin_id != $this->_plugin->id ) {
8428
- // Add-on was installed - sync license right after install.
8429
- $next_page = $this->_get_sync_license_url( $plugin_id, true, $extra );
8430
- }
8431
- } else {
8432
- /**
8433
- * @author Vova Feldman (@svovaf)
8434
- * @since 1.1.9 If site installed with a valid license, sync license.
8435
- */
8436
- if ( $this->is_paying() ) {
8437
- $this->_sync_plugin_license( true );
8438
- }
8439
-
8440
- // Reload the page with the keys.
8441
- $next_page = $this->is_anonymous() ?
8442
- // If user previously skipped, redirect to account page.
8443
- $this->get_account_url( false, $extra ) :
8444
- $this->get_after_activation_url( 'after_connect_url' );
8445
- }
8446
-
8447
- if ( ! empty( $next_page ) && $redirect ) {
8448
- fs_redirect( $next_page );
8449
- }
8450
-
8451
- return $next_page;
8452
- }
8453
-
8454
- /**
8455
- * Install plugin with new user information after approval.
8456
- *
8457
- * @author Vova Feldman (@svovaf)
8458
- * @since 1.0.7
8459
- */
8460
- function _install_with_new_user() {
8461
- $this->_logger->entrance();
8462
-
8463
- if ( $this->is_registered() ) {
8464
- return;
8465
- }
8466
-
8467
- if ( ( $this->is_plugin() && fs_request_is_action( $this->get_unique_affix() . '_activate_new' ) ) ||
8468
- // @todo This logic should be improved because it's executed on every load of a theme.
8469
- $this->is_theme()
8470
- ) {
8471
- // check_admin_referer( $this->_slug . '_activate_new' );
8472
-
8473
- if ( fs_request_has( 'user_secret_key' ) ) {
8474
- $this->install_with_new_user(
8475
- fs_request_get( 'user_id' ),
8476
- fs_request_get( 'user_public_key' ),
8477
- fs_request_get( 'user_secret_key' ),
8478
- fs_request_get( 'install_id' ),
8479
- fs_request_get( 'install_public_key' ),
8480
- fs_request_get( 'install_secret_key' ),
8481
- true,
8482
- fs_request_get_bool( 'auto_install' )
8483
- );
8484
- } else if ( fs_request_has( 'pending_activation' ) ) {
8485
- $this->set_pending_confirmation( fs_request_get( 'user_email' ), true );
8486
- }
8487
- }
8488
- }
8489
-
8490
- /**
8491
- * Install plugin with new user.
8492
- *
8493
- * @author Vova Feldman (@svovaf)
8494
- * @since 1.1.7.4
8495
- *
8496
- * @param number $user_id
8497
- * @param string $user_public_key
8498
- * @param string $user_secret_key
8499
- * @param number $install_id
8500
- * @param string $install_public_key
8501
- * @param string $install_secret_key
8502
- * @param bool $redirect
8503
- * @param bool $auto_install Since 1.2.1.7 If `true` and setting up an account with a valid license, will
8504
- * redirect (or return a URL) to the account page with a special parameter to
8505
- * trigger the auto installation processes.
8506
- *
8507
- * @return string If redirect is `false`, returns the next page the user should be redirected to.
8508
- */
8509
- private function install_with_new_user(
8510
- $user_id,
8511
- $user_public_key,
8512
- $user_secret_key,
8513
- $install_id,
8514
- $install_public_key,
8515
- $install_secret_key,
8516
- $redirect = true,
8517
- $auto_install = false
8518
- ) {
8519
- $user = new FS_User();
8520
- $user->id = $user_id;
8521
- $user->public_key = $user_public_key;
8522
- $user->secret_key = $user_secret_key;
8523
-
8524
- $this->_user = $user;
8525
- $user_result = $this->get_api_user_scope()->get();
8526
- $user = new FS_User( $user_result );
8527
- $this->_user = $user;
8528
-
8529
- $site = new FS_Site();
8530
- $site->id = $install_id;
8531
- $site->public_key = $install_public_key;
8532
- $site->secret_key = $install_secret_key;
8533
-
8534
- $this->_site = $site;
8535
- $site_result = $this->get_api_site_scope()->get();
8536
- $site = new FS_Site( $site_result );
8537
- $this->_site = $site;
8538
-
8539
- return $this->setup_account(
8540
- $this->_user,
8541
- $this->_site,
8542
- $redirect,
8543
- $auto_install
8544
- );
8545
- }
8546
-
8547
- /**
8548
- * @author Vova Feldman (@svovaf)
8549
- * @since 1.1.7.4
8550
- *
8551
- * @param string|bool $email
8552
- * @param bool $redirect
8553
- * @param string|bool $license_key Since 1.2.1.5
8554
- * @param bool $is_pending_trial Since 1.2.1.5
8555
- *
8556
- * @return string Since 1.2.1.5 if $redirect is `false`, return the pending activation page.
8557
- */
8558
- private function set_pending_confirmation(
8559
- $email = false,
8560
- $redirect = true,
8561
- $license_key = false,
8562
- $is_pending_trial = false
8563
- ) {
8564
- if ( $this->_ignore_pending_mode ) {
8565
- /**
8566
- * If explicitly asked to ignore pending mode, set to anonymous mode
8567
- * if require confirmation before finalizing the opt-in.
8568
- *
8569
- * @author Vova Feldman
8570
- * @since 1.2.1.6
8571
- */
8572
- $this->skip_connection();
8573
- } else {
8574
- // Install must be activated via email since
8575
- // user with the same email already exist.
8576
- $this->_storage->is_pending_activation = true;
8577
- $this->_add_pending_activation_notice( $email, $is_pending_trial );
8578
- }
8579
-
8580
- if ( ! empty( $license_key ) ) {
8581
- $this->_storage->pending_license_key = $license_key;
8582
- }
8583
-
8584
- // Remove the opt-in sticky notice.
8585
- $this->_admin_notices->remove_sticky( array(
8586
- 'connect_account',
8587
- 'trial_promotion',
8588
- ) );
8589
-
8590
- $next_page = $this->get_after_activation_url( 'after_pending_connect_url' );
8591
-
8592
- // Reload the page with with pending activation message.
8593
- if ( $redirect ) {
8594
- fs_redirect( $next_page );
8595
- }
8596
-
8597
- return $next_page;
8598
- }
8599
-
8600
- /**
8601
- * Install plugin with current logged WP user info.
8602
- *
8603
- * @author Vova Feldman (@svovaf)
8604
- * @since 1.0.7
8605
- */
8606
- function _install_with_current_user() {
8607
- $this->_logger->entrance();
8608
-
8609
- if ( $this->is_registered() ) {
8610
- return;
8611
- }
8612
-
8613
- if ( fs_request_is_action( $this->get_unique_affix() . '_activate_existing' ) && fs_request_is_post() ) {
8614
- // check_admin_referer( 'activate_existing_' . $this->_plugin->public_key );
8615
-
8616
- /**
8617
- * @author Vova Feldman (@svovaf)
8618
- * @since 1.1.9 Add license key if given.
8619
- */
8620
- $license_key = fs_request_get( 'license_secret_key' );
8621
-
8622
- $this->install_with_current_user( $license_key );
8623
- }
8624
- }
8625
-
8626
-
8627
- /**
8628
- * @author Vova Feldman (@svovaf)
8629
- * @since 1.1.7.4
8630
- *
8631
- * @param string|bool $license_key
8632
- * @param number|bool $trial_plan_id
8633
- * @param bool $redirect
8634
- *
8635
- * @return string|object If redirect is `false`, returns the next page the user should be redirected to, or the
8636
- * API error object if failed to install.
8637
- */
8638
- private function install_with_current_user(
8639
- $license_key = false,
8640
- $trial_plan_id = false,
8641
- $redirect = true
8642
- ) {
8643
- // Get current logged WP user.
8644
- $current_user = self::_get_current_wp_user();
8645
-
8646
- // Find the relevant FS user by the email.
8647
- $user = self::_get_user_by_email( $current_user->user_email );
8648
-
8649
- // We have to set the user before getting user scope API handler.
8650
- $this->_user = $user;
8651
-
8652
- $extra_install_params = array(
8653
- 'uid' => $this->get_anonymous_id(),
8654
- );
8655
-
8656
- if ( ! empty( $license_key ) ) {
8657
- $filtered_license_key = $this->apply_filters( 'license_key', $license_key );
8658
- $extra_install_params['license_key'] = $filtered_license_key;
8659
- } else if ( FS_Plugin_Plan::is_valid_id( $trial_plan_id ) ) {
8660
- $extra_install_params['trial_plan_id'] = $trial_plan_id;
8661
- }
8662
-
8663
- $args = $this->get_install_data_for_api( $extra_install_params, false, false );
8664
-
8665
- // Install the plugin.
8666
- $install = $this->get_api_user_scope()->call(
8667
- "/plugins/{$this->get_id()}/installs.json",
8668
- 'post',
8669
- $args
8670
- );
8671
-
8672
- if ( ! $this->is_api_result_entity( $install ) ) {
8673
- if ( ! empty( $args['license_key'] ) ) {
8674
- // Pass full the fully entered license key to the failure handler.
8675
- $args['license_key'] = $license_key;
8676
- }
8677
-
8678
- $install = $this->apply_filters( 'after_install_failure', $install, $args );
8679
-
8680
- $this->_admin_notices->add(
8681
- sprintf( $this->get_text( 'could-not-activate-x' ), $this->get_plugin_name() ) . ' ' .
8682
- $this->get_text( 'contact-us-with-error-message' ) . ' ' . '<b>' . $install->error->message . '</b>',
8683
- $this->get_text( 'oops' ) . '...',
8684
- 'error'
8685
- );
8686
-
8687
- if ( $redirect ) {
8688
- fs_redirect( $this->get_activation_url( array( 'error' => $install->error->message ) ) );
8689
- }
8690
-
8691
- return $install;
8692
- }
8693
-
8694
- $site = new FS_Site( $install );
8695
- $this->_site = $site;
8696
-
8697
- return $this->setup_account( $this->_user, $this->_site, $redirect );
8698
- }
8699
-
8700
- /**
8701
- * Tries to activate add-on account based on parent plugin info.
8702
- *
8703
- * @author Vova Feldman (@svovaf)
8704
- * @since 1.0.6
8705
- *
8706
- * @param Freemius $parent_fs
8707
- */
8708
- private function _activate_addon_account( Freemius $parent_fs ) {
8709
- if ( $this->is_registered() ) {
8710
- // Already activated.
8711
- return;
8712
- }
8713
-
8714
- // Activate add-on with parent plugin credentials.
8715
- $addon_install = $parent_fs->get_api_site_scope()->call(
8716
- "/addons/{$this->_plugin->id}/installs.json",
8717
- 'post',
8718
- $this->get_install_data_for_api( array(
8719
- 'uid' => $this->get_anonymous_id(),
8720
- ), false, false )
8721
- );
8722
-
8723
- if ( isset( $addon_install->error ) ) {
8724
- $this->_admin_notices->add(
8725
- sprintf( $this->get_text( 'could-not-activate-x' ), $this->get_plugin_name() ) . ' ' .
8726
- $this->get_text( 'contact-us-with-error-message' ) . ' ' . '<b>' . $addon_install->error->message . '</b>',
8727
- $this->get_text( 'oops' ) . '...',
8728
- 'error'
8729
- );
8730
-
8731
- return;
8732
- }
8733
-
8734
- // First of all, set site info - otherwise we won't
8735
- // be able to invoke API calls.
8736
- $this->_site = new FS_Site( $addon_install );
8737
-
8738
- // Sync add-on plans.
8739
- $this->_sync_plans();
8740
-
8741
- // Get site's current plan.
8742
- $this->_site->plan = $this->_get_plan_by_id( $this->_site->plan->id );
8743
-
8744
- // Get user information based on parent's plugin.
8745
- $user = $parent_fs->get_user();
8746
-
8747
- $this->_set_account( $user, $this->_site );
8748
-
8749
- // Sync licenses.
8750
- $this->_sync_licenses();
8751
-
8752
- // Try to activate premium license.
8753
- $this->_activate_license( true );
8754
- }
8755
-
8756
- /**
8757
- * Tries to activate parent account based on add-on's info.
8758
- *
8759
- * @author Vova Feldman (@svovaf)
8760
- * @since 1.2.2.7
8761
- *
8762
- * @param Freemius $parent_fs
8763
- */
8764
- private function activate_parent_account( Freemius $parent_fs ) {
8765
- if ( ! $this->is_addon() ) {
8766
- // This is not an add-on.
8767
- return;
8768
- }
8769
-
8770
- if ( $parent_fs->is_registered() ) {
8771
- // Already activated.
8772
- return;
8773
- }
8774
-
8775
- // Activate parent with add-on's user credentials.
8776
- $parent_install = $this->get_api_user_scope()->call(
8777
- "/plugins/{$parent_fs->_plugin->id}/installs.json",
8778
- 'post',
8779
- $parent_fs->get_install_data_for_api( array(
8780
- 'uid' => $parent_fs->get_anonymous_id(),
8781
- ), false, false )
8782
- );
8783
-
8784
- if ( isset( $parent_install->error ) ) {
8785
- $this->_admin_notices->add(
8786
- sprintf( $this->get_text( 'could-not-activate-x' ), $this->get_plugin_name() ) . ' ' .
8787
- $this->get_text( 'contact-us-with-error-message' ) . ' ' . '<b>' . $parent_install->error->message . '</b>',
8788
- $this->get_text( 'oops' ) . '...',
8789
- 'error'
8790
- );
8791
-
8792
- return;
8793
- }
8794
-
8795
- // First of all, set site info - otherwise we won't
8796
- // be able to invoke API calls.
8797
- $parent_fs->_site = new FS_Site( $parent_install );
8798
-
8799
- // Sync add-on plans.
8800
- $parent_fs->_sync_plans();
8801
-
8802
- // Get site's current plan.
8803
- $parent_fs->_site->plan = $parent_fs->_get_plan_by_id( $parent_fs->_site->plan->id );
8804
-
8805
- // Get user information based on parent's plugin.
8806
- $user = $this->get_user();
8807
-
8808
- $parent_fs->_set_account( $user, $parent_fs->_site );
8809
- }
8810
-
8811
- #endregion
8812
-
8813
- #----------------------------------------------------------------------------------
8814
- #region Admin Menu Items
8815
- #----------------------------------------------------------------------------------
8816
-
8817
- private $_menu_items = array();
8818
-
8819
- /**
8820
- * @author Vova Feldman (@svovaf)
8821
- * @since 1.2.1.8
8822
- *
8823
- * @return array
8824
- */
8825
- function get_menu_items() {
8826
- return $this->_menu_items;
8827
- }
8828
-
8829
- /**
8830
- * @author Vova Feldman (@svovaf)
8831
- * @since 1.0.7
8832
- *
8833
- * @return string
8834
- */
8835
- function get_menu_slug() {
8836
- return $this->_menu->get_slug();
8837
- }
8838
-
8839
- /**
8840
- * @author Vova Feldman (@svovaf)
8841
- * @since 1.0.9
8842
- */
8843
- function _prepare_admin_menu() {
8844
- // if ( ! $this->is_on() ) {
8845
- // return;
8846
- // }
8847
-
8848
- if ( ! $this->has_api_connectivity() && ! $this->is_enable_anonymous() ) {
8849
- $this->_menu->remove_menu_item();
8850
- } else {
8851
- $this->do_action( 'before_admin_menu_init' );
8852
-
8853
- $this->add_menu_action();
8854
- $this->add_submenu_items();
8855
- }
8856
- }
8857
-
8858
- /**
8859
- * Admin dashboard menu items modifications.
8860
- *
8861
- * NOTE: admin_menu action executed before admin_init.
8862
- *
8863
- * @author Vova Feldman (@svovaf)
8864
- * @since 1.0.7
8865
- *
8866
- */
8867
- private function add_menu_action() {
8868
- if ( $this->is_activation_mode() ) {
8869
- if ( $this->is_plugin() || ( $this->has_settings_menu() && ! $this->is_free_wp_org_theme() ) ) {
8870
- $this->override_plugin_menu_with_activation();
8871
- } else {
8872
- /**
8873
- * Handle theme opt-in when the opt-in form shows as a dialog box in the themes page.
8874
- */
8875
- if ( fs_request_is_action( $this->get_unique_affix() . '_activate_existing' ) ) {
8876
- add_action( 'load-themes.php', array( &$this, '_install_with_current_user' ) );
8877
- } else if ( fs_request_is_action( $this->get_unique_affix() . '_activate_new' ) ||
8878
- fs_request_get_bool( 'pending_activation' )
8879
- ) {
8880
- add_action( 'load-themes.php', array( &$this, '_install_with_new_user' ) );
8881
- }
8882
- }
8883
- } else {
8884
- if ( ! $this->is_registered() ) {
8885
- // If not registered try to install user.
8886
- if ( fs_request_is_action( $this->get_unique_affix() . '_activate_new' ) ) {
8887
- $this->_install_with_new_user();
8888
- }
8889
- } else if (
8890
- fs_request_is_action( 'sync_user' ) &&
8891
- ( ! $this->has_settings_menu() || $this->is_free_wp_org_theme() )
8892
- ) {
8893
- $this->_handle_account_user_sync();
8894
- }
8895
- }
8896
- }
8897
-
8898
- /**
8899
- * @author Vova Feldman (@svovaf)
8900
- * @since 1.0.1
8901
- */
8902
- function _redirect_on_clicked_menu_link() {
8903
- $this->_logger->entrance();
8904
-
8905
- $page = strtolower( isset( $_REQUEST['page'] ) ? $_REQUEST['page'] : '' );
8906
-
8907
- $this->_logger->log( 'page = ' . $page );
8908
-
8909
- foreach ( $this->_menu_items as $priority => $items ) {
8910
- foreach ( $items as $item ) {
8911
- if ( isset( $item['url'] ) ) {
8912
- if ( $page === $this->_menu->get_slug( strtolower( $item['menu_slug'] ) ) ) {
8913
- $this->_logger->log( 'Redirecting to ' . $item['url'] );
8914
-
8915
- fs_redirect( $item['url'] );
8916
- }
8917
- }
8918
- }
8919
- }
8920
- }
8921
-
8922
- /**
8923
- * Remove plugin's all admin menu items & pages, and replace with activation page.
8924
- *
8925
- * @author Vova Feldman (@svovaf)
8926
- * @since 1.0.1
8927
- */
8928
- private function override_plugin_menu_with_activation() {
8929
- $this->_logger->entrance();
8930
-
8931
- $hook = false;
8932
-
8933
- if ( ! $this->_menu->has_menu() ) {
8934
- // Add the opt-in page without a menu item.
8935
- $hook = FS_Admin_Menu_Manager::add_subpage(
8936
- null,
8937
- $this->get_plugin_name(),
8938
- $this->get_plugin_name(),
8939
- 'manage_options',
8940
- $this->_slug,
8941
- array( &$this, '_connect_page_render' )
8942
- );
8943
- } else if ( $this->_menu->is_top_level() ) {
8944
- $hook = $this->_menu->override_menu_item( array( &$this, '_connect_page_render' ) );
8945
-
8946
- if ( false === $hook ) {
8947
- // Create new menu item just for the opt-in.
8948
- $hook = FS_Admin_Menu_Manager::add_page(
8949
- $this->get_plugin_name(),
8950
- $this->get_plugin_name(),
8951
- 'manage_options',
8952
- $this->_menu->get_slug(),
8953
- array( &$this, '_connect_page_render' )
8954
- );
8955
- }
8956
- } else {
8957
- $menus = array( $this->_menu->get_parent_slug() );
8958
-
8959
- if ( $this->_menu->is_override_exact() ) {
8960
- // Make sure the current page is matching the activation page.
8961
- if ( ! $this->is_matching_url( $this->get_activation_url() ) ) {
8962
- return;
8963
- }
8964
- }
8965
-
8966
- foreach ( $menus as $parent_slug ) {
8967
- $hook = $this->_menu->override_submenu_action(
8968
- $parent_slug,
8969
- $this->_menu->get_raw_slug(),
8970
- array( &$this, '_connect_page_render' )
8971
- );
8972
-
8973
- if ( false !== $hook ) {
8974
- // Found plugin's submenu item.
8975
- break;
8976
- }
8977
- }
8978
- }
8979
-
8980
- if ( $this->is_activation_page() ) {
8981
- // Clean admin page from distracting content.
8982
- self::_clean_admin_content_section();
8983
- }
8984
-
8985
- if ( false !== $hook ) {
8986
- if ( fs_request_is_action( $this->get_unique_affix() . '_activate_existing' ) ) {
8987
- add_action( "load-$hook", array( &$this, '_install_with_current_user' ) );
8988
- } else if ( fs_request_is_action( $this->get_unique_affix() . '_activate_new' ) ) {
8989
- add_action( "load-$hook", array( &$this, '_install_with_new_user' ) );
8990
- }
8991
- }
8992
- }
8993
-
8994
- /**
8995
- * @author Leo Fajardo (leorw)
8996
- * @since 1.2.1
8997
- *
8998
- * return string
8999
- */
9000
- function get_top_level_menu_capability() {
9001
- global $menu;
9002
-
9003
- $top_level_menu_slug = $this->get_top_level_menu_slug();
9004
-
9005
- foreach ( $menu as $menu_info ) {
9006
- /**
9007
- * The second element in the menu info array is the capability/role that has access to the menu and the
9008
- * third element is the menu slug.
9009
- */
9010
- if ( $menu_info[2] === $top_level_menu_slug ) {
9011
- return $menu_info[1];
9012
- }
9013
- }
9014
-
9015
- return 'read';
9016
- }
9017
-
9018
- /**
9019
- * @author Vova Feldman (@svovaf)
9020
- * @since 1.0.0
9021
- *
9022
- * @return string
9023
- */
9024
- private function get_top_level_menu_slug() {
9025
- return ( $this->is_addon() ?
9026
- $this->get_parent_instance()->_menu->get_top_level_menu_slug() :
9027
- $this->_menu->get_top_level_menu_slug() );
9028
- }
9029
-
9030
- /**
9031
- * @author Vova Feldman (@svovaf)
9032
- * @since 1.2.2.7
9033
- *
9034
- * @return string
9035
- */
9036
- function get_pricing_cta_label() {
9037
- $label = 'upgrade';
9038
-
9039
- if ( $this->is_in_trial_promotion() &&
9040
- ! $this->is_paying_or_trial()
9041
- ) {
9042
- // If running a trial promotion, modify the pricing to load the trial.
9043
- $label = 'start-trial';
9044
- } else if ( $this->is_paying() ) {
9045
- $label = 'pricing';
9046
- }
9047
-
9048
- return $label;
9049
- }
9050
-
9051
- /**
9052
- * @author Vova Feldman (@svovaf)
9053
- * @since 1.2.2.7
9054
- *
9055
- * @return bool
9056
- */
9057
- function is_pricing_page_visible() {
9058
- return (
9059
- // Has at least one paid plan.
9060
- $this->has_paid_plan() &&
9061
- // Didn't ask to hide the pricing page.
9062
- $this->is_page_visible( 'pricing' ) &&
9063
- // Don't have a valid active license or has more than one plan.
9064
- ( ! $this->is_paying() || ! $this->is_single_plan() )
9065
- );
9066
- }
9067
-
9068
- /**
9069
- * Add default Freemius menu items.
9070
- *
9071
- * @author Vova Feldman (@svovaf)
9072
- * @since 1.0.0
9073
- */
9074
- private function add_submenu_items() {
9075
- $this->_logger->entrance();
9076
-
9077
- if ( ! $this->is_addon() ) {
9078
- /**
9079
- * @since 1.2.2.7 Also add submenu items when running in a free .org theme so the tabs will be visible.
9080
- */
9081
- if ( ! $this->is_activation_mode() || $this->is_free_wp_org_theme() ) {
9082
- if ( $this->is_registered() ) {
9083
- $show_account = (
9084
- $this->is_submenu_item_visible( 'account' ) &&
9085
- /**
9086
- * @since 1.2.2.7 Don't show the Account for free WP.org themes without any paid plans.
9087
- */
9088
- ( ! $this->is_free_wp_org_theme() || $this->has_paid_plan() )
9089
- );
9090
-
9091
- // Add user account page.
9092
- $this->add_submenu_item(
9093
- $this->get_text( 'account' ),
9094
- array( &$this, '_account_page_render' ),
9095
- $this->get_plugin_name() . ' &ndash; ' . $this->get_text( 'account' ),
9096
- 'manage_options',
9097
- 'account',
9098
- array( &$this, '_account_page_load' ),
9099
- WP_FS__DEFAULT_PRIORITY,
9100
- $show_account
9101
- );
9102
- }
9103
-
9104
- // Add contact page.
9105
- $this->add_submenu_item(
9106
- $this->get_text( 'contact-us' ),
9107
- array( &$this, '_contact_page_render' ),
9108
- $this->get_plugin_name() . ' &ndash; ' . $this->get_text( 'contact-us' ),
9109
- 'manage_options',
9110
- 'contact',
9111
- 'Freemius::_clean_admin_content_section',
9112
- WP_FS__DEFAULT_PRIORITY,
9113
- $this->is_submenu_item_visible( 'contact' )
9114
- );
9115
-
9116
- if ( $this->has_addons() ) {
9117
- $this->add_submenu_item(
9118
- $this->get_text( 'add-ons' ),
9119
- array( &$this, '_addons_page_render' ),
9120
- $this->get_plugin_name() . ' &ndash; ' . $this->get_text( 'add-ons' ),
9121
- 'manage_options',
9122
- 'addons',
9123
- array( &$this, '_addons_page_load' ),
9124
- WP_FS__LOWEST_PRIORITY - 1,
9125
- $this->is_submenu_item_visible( 'addons' )
9126
- );
9127
- }
9128
-
9129
- $show_pricing = (
9130
- $this->is_submenu_item_visible( 'pricing' ) &&
9131
- $this->is_pricing_page_visible()
9132
- );
9133
-
9134
- $pricing_cta_slug = $this->get_pricing_cta_label();
9135
- $pricing_class = 'upgrade-mode';
9136
- if ( $show_pricing ) {
9137
- if ( $this->is_in_trial_promotion() &&
9138
- ! $this->is_paying_or_trial()
9139
- ) {
9140
- // If running a trial promotion, modify the pricing to load the trial.
9141
- $pricing_class = 'trial-mode';
9142
- } else if ( $this->is_paying() ) {
9143
- $pricing_class = '';
9144
- }
9145
- }
9146
-
9147
- // Add upgrade/pricing page.
9148
- $this->add_submenu_item(
9149
- $this->get_text( $pricing_cta_slug ) . '&nbsp;&nbsp;' . ( is_rtl() ? '&#x2190;' : '&#x27a4;' ),
9150
- array( &$this, '_pricing_page_render' ),
9151
- $this->get_plugin_name() . ' &ndash; ' . $this->get_text( 'pricing' ),
9152
- 'manage_options',
9153
- 'pricing',
9154
- 'Freemius::_clean_admin_content_section',
9155
- WP_FS__LOWEST_PRIORITY,
9156
- $show_pricing,
9157
- $pricing_class
9158
- );
9159
- }
9160
- }
9161
-
9162
-
9163
- if ( 0 < count( $this->_menu_items ) ) {
9164
- if ( ! $this->_menu->is_top_level() ) {
9165
- fs_enqueue_local_style( 'fs_common', '/admin/common.css' );
9166
-
9167
- // Append submenu items right after the plugin's submenu item.
9168
- $this->order_sub_submenu_items();
9169
- } else {
9170
- // Append submenu items.
9171
- $this->embed_submenu_items();
9172
- }
9173
- }
9174
- }
9175
-
9176
- /**
9177
- * Moved the actual submenu item additions to a separated function,
9178
- * in order to support sub-submenu items when the plugin's settings
9179
- * only have a submenu and not top-level menu item.
9180
- *
9181
- * @author Vova Feldman (@svovaf)
9182
- * @since 1.1.4
9183
- */
9184
- private function embed_submenu_items() {
9185
- $item_template = $this->_menu->is_top_level() ?
9186
- '<span class="fs-submenu-item %s %s %s">%s</span>' :
9187
- '<span class="fs-submenu-item fs-sub %s %s %s">%s</span>';
9188
-
9189
- $top_level_menu_capability = $this->get_top_level_menu_capability();
9190
-
9191
- ksort( $this->_menu_items );
9192
-
9193
- foreach ( $this->_menu_items as $priority => $items ) {
9194
- foreach ( $items as $item ) {
9195
- $capability = ( ! empty( $item['capability'] ) ? $item['capability'] : $top_level_menu_capability );
9196
-
9197
- $menu_item = sprintf(
9198
- $item_template,
9199
- $this->get_unique_affix(),
9200
- $item['menu_slug'],
9201
- ! empty( $item['class'] ) ? $item['class'] : '',
9202
- $item['menu_title']
9203
- );
9204
-
9205
- $menu_slug = $this->_menu->get_slug( $item['menu_slug'] );
9206
-
9207
- if ( ! isset( $item['url'] ) ) {
9208
- $hook = FS_Admin_Menu_Manager::add_subpage(
9209
- $item['show_submenu'] ?
9210
- $this->get_top_level_menu_slug() :
9211
- null,
9212
- $item['page_title'],
9213
- $menu_item,
9214
- $capability,
9215
- $menu_slug,
9216
- $item['render_function']
9217
- );
9218
-
9219
- if ( false !== $item['before_render_function'] ) {
9220
- add_action( "load-$hook", $item['before_render_function'] );
9221
- }
9222
- } else {
9223
- FS_Admin_Menu_Manager::add_subpage(
9224
- $item['show_submenu'] ?
9225
- $this->get_top_level_menu_slug() :
9226
- null,
9227
- $item['page_title'],
9228
- $menu_item,
9229
- $capability,
9230
- $menu_slug,
9231
- array( $this, '' )
9232
- );
9233
- }
9234
- }
9235
- }
9236
- }
9237
-
9238
- /**
9239
- * Re-order the submenu items so all Freemius added new submenu items
9240
- * are added right after the plugin's settings submenu item.
9241
- *
9242
- * @author Vova Feldman (@svovaf)
9243
- * @since 1.1.4
9244
- */
9245
- private function order_sub_submenu_items() {
9246
- global $submenu;
9247
-
9248
- $menu_slug = $this->_menu->get_top_level_menu_slug();
9249
-
9250
- /**
9251
- * Before "admin_menu" fires, WordPress will loop over the default submenus and remove pages for which the user
9252
- * does not have permissions. So in case a plugin does not have top-level menu but does have submenus under any
9253
- * of the default menus, only users that have the right role can access its sub-submenus (Account, Contact Us,
9254
- * Support Forum, etc.) since $submenu[ $menu_slug ] will be empty if the user doesn't have permission.
9255
- *
9256
- * In case a plugin does not have submenus under any of the default menus but does have submenus under the menu
9257
- * of another plugin, only users that have the right role can access its sub-submenus since we will use the
9258
- * capability needed to access the parent menu as the capability for the submenus that we will add.
9259
- */
9260
- if ( empty( $submenu[ $menu_slug ] ) ) {
9261
- return;
9262
- }
9263
-
9264
- $top_level_menu = &$submenu[ $menu_slug ];
9265
-
9266
- $all_submenu_items_after = array();
9267
-
9268
- $found_submenu_item = false;
9269
-
9270
- foreach ( $top_level_menu as $submenu_id => $meta ) {
9271
- if ( $found_submenu_item ) {
9272
- // Remove all submenu items after the plugin's submenu item.
9273
- $all_submenu_items_after[] = $meta;
9274
- unset( $top_level_menu[ $submenu_id ] );
9275
- }
9276
-
9277
- if ( $this->_menu->get_raw_slug() === $meta[2] ) {
9278
- // Found the submenu item, put all below.
9279
- $found_submenu_item = true;
9280
- continue;
9281
- }
9282
- }
9283
-
9284
- // Embed all plugin's new submenu items.
9285
- $this->embed_submenu_items();
9286
-
9287
- // Start with specially high number to make sure it's appended.
9288
- $i = max( 10000, max( array_keys( $top_level_menu ) ) + 1 );
9289
- foreach ( $all_submenu_items_after as $meta ) {
9290
- $top_level_menu[ $i ] = $meta;
9291
- $i ++;
9292
- }
9293
-
9294
- // Sort submenu items.
9295
- ksort( $top_level_menu );
9296
- }
9297
-
9298
- /**
9299
- * Helper method to return the module's support forum URL.
9300
- *
9301
- * @author Vova Feldman (@svovaf)
9302
- * @since 1.2.2.7
9303
- *
9304
- * @return string
9305
- */
9306
- function get_support_forum_url() {
9307
- return $this->apply_filters( 'support_forum_url', "https://wordpress.org/support/{$this->_module_type}/{$this->_slug}" );
9308
- }
9309
-
9310
- /**
9311
- * Displays the Support Forum link when enabled.
9312
- *
9313
- * Can be filtered like so:
9314
- *
9315
- * function _fs_show_support_menu( $is_visible, $menu_id ) {
9316
- * if ( 'support' === $menu_id ) {
9317
- * return _fs->is_registered();
9318
- * }
9319
- * return $is_visible;
9320
- * }
9321
- * _fs()->add_filter('is_submenu_visible', '_fs_show_support_menu', 10, 2);
9322
- *
9323
- */
9324
- function _add_default_submenu_items() {
9325
- if ( ! $this->is_on() ) {
9326
- return;
9327
- }
9328
-
9329
- if ( ! $this->is_activation_mode() ) {
9330
- $this->add_submenu_link_item(
9331
- $this->apply_filters( 'support_forum_submenu', $this->get_text( 'support-forum' ) ),
9332
- $this->get_support_forum_url(),
9333
- 'wp-support-forum',
9334
- null,
9335
- 50,
9336
- $this->is_submenu_item_visible( 'support' )
9337
- );
9338
- }
9339
- }
9340
-
9341
- /**
9342
- * @author Vova Feldman (@svovaf)
9343
- * @since 1.0.1
9344
- *
9345
- * @param string $menu_title
9346
- * @param callable $render_function
9347
- * @param bool|string $page_title
9348
- * @param string $capability
9349
- * @param bool|string $menu_slug
9350
- * @param bool|callable $before_render_function
9351
- * @param int $priority
9352
- * @param bool $show_submenu
9353
- * @param string $class Since 1.2.1.5 can add custom classes to menu items.
9354
- */
9355
- function add_submenu_item(
9356
- $menu_title,
9357
- $render_function,
9358
- $page_title = false,
9359
- $capability = 'manage_options',
9360
- $menu_slug = false,
9361
- $before_render_function = false,
9362
- $priority = WP_FS__DEFAULT_PRIORITY,
9363
- $show_submenu = true,
9364
- $class = ''
9365
- ) {
9366
- $this->_logger->entrance( 'Title = ' . $menu_title );
9367
-
9368
- if ( $this->is_addon() ) {
9369
- $parent_fs = $this->get_parent_instance();
9370
-
9371
- if ( is_object( $parent_fs ) ) {
9372
- $parent_fs->add_submenu_item(
9373
- $menu_title,
9374
- $render_function,
9375
- $page_title,
9376
- $capability,
9377
- $menu_slug,
9378
- $before_render_function,
9379
- $priority,
9380
- $show_submenu,
9381
- $class
9382
- );
9383
-
9384
- return;
9385
- }
9386
- }
9387
-
9388
- if ( ! isset( $this->_menu_items[ $priority ] ) ) {
9389
- $this->_menu_items[ $priority ] = array();
9390
- }
9391
-
9392
- $this->_menu_items[ $priority ][] = array(
9393
- 'page_title' => is_string( $page_title ) ? $page_title : $menu_title,
9394
- 'menu_title' => $menu_title,
9395
- 'capability' => $capability,
9396
- 'menu_slug' => is_string( $menu_slug ) ? $menu_slug : strtolower( $menu_title ),
9397
- 'render_function' => $render_function,
9398
- 'before_render_function' => $before_render_function,
9399
- 'show_submenu' => $show_submenu,
9400
- 'class' => $class,
9401
- );
9402
- }
9403
-
9404
- /**
9405
- * @author Vova Feldman (@svovaf)
9406
- * @since 1.0.1
9407
- *
9408
- * @param string $menu_title
9409
- * @param string $url
9410
- * @param bool $menu_slug
9411
- * @param string $capability
9412
- * @param int $priority
9413
- * @param bool $show_submenu
9414
- */
9415
- function add_submenu_link_item(
9416
- $menu_title,
9417
- $url,
9418
- $menu_slug = false,
9419
- $capability = 'read',
9420
- $priority = WP_FS__DEFAULT_PRIORITY,
9421
- $show_submenu = true
9422
- ) {
9423
- $this->_logger->entrance( 'Title = ' . $menu_title . '; Url = ' . $url );
9424
-
9425
- if ( $this->is_addon() ) {
9426
- $parent_fs = $this->get_parent_instance();
9427
-
9428
- if ( is_object( $parent_fs ) ) {
9429
- $parent_fs->add_submenu_link_item(
9430
- $menu_title,
9431
- $url,
9432
- $menu_slug,
9433
- $capability,
9434
- $priority,
9435
- $show_submenu
9436
- );
9437
-
9438
- return;
9439
- }
9440
- }
9441
-
9442
- if ( ! isset( $this->_menu_items[ $priority ] ) ) {
9443
- $this->_menu_items[ $priority ] = array();
9444
- }
9445
-
9446
- $this->_menu_items[ $priority ][] = array(
9447
- 'menu_title' => $menu_title,
9448
- 'capability' => $capability,
9449
- 'menu_slug' => is_string( $menu_slug ) ? $menu_slug : strtolower( $menu_title ),
9450
- 'url' => $url,
9451
- 'page_title' => $menu_title,
9452
- 'render_function' => 'fs_dummy',
9453
- 'before_render_function' => '',
9454
- 'show_submenu' => $show_submenu,
9455
- );
9456
- }
9457
-
9458
- #endregion ------------------------------------------------------------------
9459
-
9460
-
9461
- #--------------------------------------------------------------------------------
9462
- #region Actions / Hooks / Filters
9463
- #--------------------------------------------------------------------------------
9464
-
9465
- /**
9466
- * @author Vova Feldman (@svovaf)
9467
- * @since 1.1.7
9468
- *
9469
- * @param string $tag
9470
- *
9471
- * @return string
9472
- */
9473
- public function get_action_tag( $tag ) {
9474
- return self::get_action_tag_static( $tag, $this->_slug, $this->is_plugin() );
9475
- }
9476
-
9477
- /**
9478
- * @author Vova Feldman (@svovaf)
9479
- * @since 1.2.1.6
9480
- *
9481
- * @param string $tag
9482
- * @param string $slug
9483
- * @param bool $is_plugin
9484
- *
9485
- * @return string
9486
- */
9487
- static function get_action_tag_static( $tag, $slug = '', $is_plugin = true ) {
9488
- $action = "fs_{$tag}";
9489
-
9490
- if ( ! empty( $slug ) ) {
9491
- $action .= '_' . self::get_module_unique_affix( $slug, $is_plugin );
9492
- }
9493
-
9494
- return $action;
9495
- }
9496
-
9497
- /**
9498
- * Returns a string that can be used to generate a unique action name,
9499
- * option name, HTML element ID, or HTML element class.
9500
- *
9501
- * @author Leo Fajardo (@leorw)
9502
- * @since 1.2.2
9503
- *
9504
- * @return string
9505
- */
9506
- public function get_unique_affix() {
9507
- return self::get_module_unique_affix( $this->_slug, $this->is_plugin() );
9508
- }
9509
-
9510
- /**
9511
- * Returns a string that can be used to generate a unique action name,
9512
- * option name, HTML element ID, or HTML element class.
9513
- *
9514
- * @author Vova Feldman (@svovaf)
9515
- * @since 1.2.2.5
9516
- *
9517
- * @param string $slug
9518
- * @param bool $is_plugin
9519
- *
9520
- * @return string
9521
- */
9522
- static function get_module_unique_affix( $slug, $is_plugin = true ) {
9523
- $affix = $slug;
9524
-
9525
- if ( ! $is_plugin ) {
9526
- $affix .= '-' . WP_FS__MODULE_TYPE_THEME;
9527
- }
9528
-
9529
- return $affix;
9530
- }
9531
-
9532
- /**
9533
- * @author Vova Feldman (@svovaf)
9534
- * @since 1.2.1
9535
- * @since 1.2.2.5 The AJAX action names are based on the module ID, not like the non-AJAX actions that are
9536
- * based on the slug for backward compatibility.
9537
- *
9538
- * @param string $tag
9539
- *
9540
- * @return string
9541
- */
9542
- function get_ajax_action( $tag ) {
9543
- return self::get_ajax_action_static( $tag, $this->_module_id );
9544
- }
9545
-
9546
- /**
9547
- * @author Vova Feldman (@svovaf)
9548
- * @since 1.2.1.7
9549
- *
9550
- * @param string $tag
9551
- *
9552
- * @return string
9553
- */
9554
- function get_ajax_security( $tag ) {
9555
- return wp_create_nonce( $this->get_ajax_action( $tag ) );
9556
- }
9557
-
9558
- /**
9559
- * @author Vova Feldman (@svovaf)
9560
- * @since 1.2.1.7
9561
- *
9562
- * @param string $tag
9563
- */
9564
- function check_ajax_referer( $tag ) {
9565
- check_ajax_referer( $this->get_ajax_action( $tag ), 'security' );
9566
- }
9567
-
9568
- /**
9569
- * @author Vova Feldman (@svovaf)
9570
- * @since 1.2.1.6
9571
- * @since 1.2.2.5 The AJAX action names are based on the module ID, not like the non-AJAX actions that are
9572
- * based on the slug for backward compatibility.
9573
- *
9574
- * @param string $tag
9575
- * @param number|null $module_id
9576
- *
9577
- * @return string
9578
- */
9579
- private static function get_ajax_action_static( $tag, $module_id = null ) {
9580
- $action = "fs_{$tag}";
9581
-
9582
- if ( ! empty( $module_id ) ) {
9583
- $action .= "_{$module_id}";
9584
- }
9585
-
9586
- return $action;
9587
- }
9588
-
9589
- /**
9590
- * Do action, specific for the current context plugin.
9591
- *
9592
- * @author Vova Feldman (@svovaf)
9593
- * @since 1.0.1
9594
- *
9595
- * @param string $tag The name of the action to be executed.
9596
- * @param mixed $arg,... Optional. Additional arguments which are passed on to the
9597
- * functions hooked to the action. Default empty.
9598
- *
9599
- * @uses do_action()
9600
- */
9601
- function do_action( $tag, $arg = '' ) {
9602
- $this->_logger->entrance( $tag );
9603
-
9604
- $args = func_get_args();
9605
-
9606
- call_user_func_array( 'do_action', array_merge(
9607
- array( $this->get_action_tag( $tag ) ),
9608
- array_slice( $args, 1 ) )
9609
- );
9610
- }
9611
-
9612
- /**
9613
- * Add action, specific for the current context plugin.
9614
- *
9615
- * @author Vova Feldman (@svovaf)
9616
- * @since 1.0.1
9617
- *
9618
- * @param string $tag
9619
- * @param callable $function_to_add
9620
- * @param int $priority
9621
- * @param int $accepted_args
9622
- *
9623
- * @uses add_action()
9624
- */
9625
- function add_action(
9626
- $tag,
9627
- $function_to_add,
9628
- $priority = WP_FS__DEFAULT_PRIORITY,
9629
- $accepted_args = 1
9630
- ) {
9631
- $this->_logger->entrance( $tag );
9632
-
9633
- add_action( $this->get_action_tag( $tag ), $function_to_add, $priority, $accepted_args );
9634
- }
9635
-
9636
- /**
9637
- * Add AJAX action, specific for the current context plugin.
9638
- *
9639
- * @author Vova Feldman (@svovaf)
9640
- * @since 1.2.1
9641
- *
9642
- * @param string $tag
9643
- * @param callable $function_to_add
9644
- * @param int $priority
9645
- *
9646
- * @uses add_action()
9647
- *
9648
- * @return bool True if action added, false if no need to add the action since the AJAX call isn't matching.
9649
- */
9650
- function add_ajax_action(
9651
- $tag,
9652
- $function_to_add,
9653
- $priority = WP_FS__DEFAULT_PRIORITY
9654
- ) {
9655
- $this->_logger->entrance( $tag );
9656
-
9657
- return self::add_ajax_action_static(
9658
- $tag,
9659
- $function_to_add,
9660
- $priority,
9661
- $this->_module_id
9662
- );
9663
- }
9664
-
9665
- /**
9666
- * Add AJAX action.
9667
- *
9668
- * @author Vova Feldman (@svovaf)
9669
- * @since 1.2.1.6
9670
- *
9671
- * @param string $tag
9672
- * @param callable $function_to_add
9673
- * @param int $priority
9674
- * @param number|null $module_id
9675
- *
9676
- * @return bool True if action added, false if no need to add the action since the AJAX call isn't matching.
9677
- * @uses add_action()
9678
- *
9679
- */
9680
- static function add_ajax_action_static(
9681
- $tag,
9682
- $function_to_add,
9683
- $priority = WP_FS__DEFAULT_PRIORITY,
9684
- $module_id = null
9685
- ) {
9686
- self::$_static_logger->entrance( $tag );
9687
-
9688
- if ( ! self::is_ajax_action_static( $tag, $module_id ) ) {
9689
- return false;
9690
- }
9691
-
9692
- add_action(
9693
- 'wp_ajax_' . self::get_ajax_action_static( $tag, $module_id ),
9694
- $function_to_add,
9695
- $priority,
9696
- 0
9697
- );
9698
-
9699
- self::$_static_logger->info( "$tag AJAX callback action added." );
9700
-
9701
- return true;
9702
- }
9703
-
9704
- /**
9705
- * Send a JSON response back to an Ajax request.
9706
- *
9707
- * @author Vova Feldman (@svovaf)
9708
- * @since 1.2.1.5
9709
- *
9710
- * @param mixed $response
9711
- */
9712
- static function shoot_ajax_response( $response ) {
9713
- wp_send_json( $response );
9714
- }
9715
-
9716
- /**
9717
- * Send a JSON response back to an Ajax request, indicating success.
9718
- *
9719
- * @author Vova Feldman (@svovaf)
9720
- * @since 1.2.1.5
9721
- *
9722
- * @param mixed $data Data to encode as JSON, then print and exit.
9723
- */
9724
- static function shoot_ajax_success( $data = null ) {
9725
- wp_send_json_success( $data );
9726
- }
9727
-
9728
- /**
9729
- * Send a JSON response back to an Ajax request, indicating failure.
9730
- *
9731
- * @author Vova Feldman (@svovaf)
9732
- * @since 1.2.1.5
9733
- *
9734
- * @param mixed $error Optional error message.
9735
- */
9736
- static function shoot_ajax_failure( $error = '' ) {
9737
- $result = array( 'success' => false );
9738
- if ( ! empty( $error ) ) {
9739
- $result['error'] = $error;
9740
- }
9741
-
9742
- wp_send_json( $result );
9743
- }
9744
-
9745
- /**
9746
- * Apply filter, specific for the current context plugin.
9747
- *
9748
- * @author Vova Feldman (@svovaf)
9749
- * @since 1.0.9
9750
- *
9751
- * @param string $tag The name of the filter hook.
9752
- * @param mixed $value The value on which the filters hooked to `$tag` are applied on.
9753
- *
9754
- * @return mixed The filtered value after all hooked functions are applied to it.
9755
- *
9756
- * @uses apply_filters()
9757
- */
9758
- function apply_filters( $tag, $value ) {
9759
- $this->_logger->entrance( $tag );
9760
-
9761
- $args = func_get_args();
9762
- array_unshift( $args, $this->get_unique_affix() );
9763
-
9764
- return call_user_func_array( 'fs_apply_filter', $args );
9765
- }
9766
-
9767
- /**
9768
- * Add filter, specific for the current context plugin.
9769
- *
9770
- * @author Vova Feldman (@svovaf)
9771
- * @since 1.0.9
9772
- *
9773
- * @param string $tag
9774
- * @param callable $function_to_add
9775
- * @param int $priority
9776
- * @param int $accepted_args
9777
- *
9778
- * @uses add_filter()
9779
- */
9780
- function add_filter( $tag, $function_to_add, $priority = WP_FS__DEFAULT_PRIORITY, $accepted_args = 1 ) {
9781
- $this->_logger->entrance( $tag );
9782
-
9783
- add_filter( $this->get_action_tag( $tag ), $function_to_add, $priority, $accepted_args );
9784
- }
9785
-
9786
- /**
9787
- * Check if has filter.
9788
- *
9789
- * @author Vova Feldman (@svovaf)
9790
- * @since 1.1.4
9791
- *
9792
- * @param string $tag
9793
- * @param callable|bool $function_to_check Optional. The callback to check for. Default false.
9794
- *
9795
- * @return false|int
9796
- *
9797
- * @uses has_filter()
9798
- */
9799
- function has_filter( $tag, $function_to_check = false ) {
9800
- $this->_logger->entrance( $tag );
9801
-
9802
- return has_filter( $this->get_action_tag( $tag ), $function_to_check );
9803
- }
9804
-
9805
- #endregion
9806
-
9807
- /**
9808
- * Override default i18n text phrases.
9809
- *
9810
- * @author Vova Feldman (@svovaf)
9811
- * @since 1.1.6
9812
- *
9813
- * @param string[] string $key_value
9814
- *
9815
- * @uses fs_override_i18n()
9816
- */
9817
- function override_i18n( $key_value ) {
9818
- fs_override_i18n( $key_value, $this->_slug );
9819
- }
9820
-
9821
- /* Account Page
9822
- ------------------------------------------------------------------------------------------------------------------*/
9823
- /**
9824
- * Update site information.
9825
- *
9826
- * @author Vova Feldman (@svovaf)
9827
- * @since 1.0.1
9828
- *
9829
- * @param bool $store Flush to Database if true.
9830
- */
9831
- private function _store_site( $store = true ) {
9832
- $this->_logger->entrance();
9833
-
9834
- if ( empty( $this->_site->id ) ) {
9835
- $this->_logger->error( "Empty install ID, can't store site." );
9836
-
9837
- return;
9838
- }
9839
-
9840
- $encrypted_site = clone $this->_site;
9841
- $encrypted_site->plan = self::_encrypt_entity( $this->_site->plan );
9842
-
9843
- $sites = self::get_all_sites( $this->_module_type );
9844
- $sites[ $this->_slug ] = $encrypted_site;
9845
-
9846
- $this->set_account_option( 'sites', $sites, $store );
9847
- }
9848
-
9849
- /**
9850
- * Update plugin's plans information.
9851
- *
9852
- * @author Vova Feldman (@svovaf)
9853
- * @since 1.0.2
9854
- *
9855
- * @param bool $store Flush to Database if true.
9856
- */
9857
- private function _store_plans( $store = true ) {
9858
- $this->_logger->entrance();
9859
-
9860
- $plans = self::get_all_plans( $this->_module_type );
9861
-
9862
- // Copy plans.
9863
- $encrypted_plans = array();
9864
- for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
9865
- $encrypted_plans[] = self::_encrypt_entity( $this->_plans[ $i ] );
9866
- }
9867
-
9868
- $plans[ $this->_slug ] = $encrypted_plans;
9869
-
9870
- $this->set_account_option( 'plans', $plans, $store );
9871
- }
9872
-
9873
- /**
9874
- * Update user's plugin licenses.
9875
- *
9876
- * @author Vova Feldman (@svovaf)
9877
- * @since 1.0.5
9878
- *
9879
- * @param bool $store
9880
- * @param string|bool $plugin_slug
9881
- * @param FS_Plugin_License[] $licenses
9882
- */
9883
- private function _store_licenses( $store = true, $plugin_slug = false, $licenses = array() ) {
9884
- $this->_logger->entrance();
9885
-
9886
- $all_licenses = self::get_all_licenses( $this->_module_type );
9887
-
9888
- if ( ! is_string( $plugin_slug ) ) {
9889
- $plugin_slug = $this->_slug;
9890
- $licenses = $this->_licenses;
9891
- }
9892
-
9893
- if ( ! isset( $all_licenses[ $plugin_slug ] ) ) {
9894
- $all_licenses[ $plugin_slug ] = array();
9895
- }
9896
-
9897
- $all_licenses[ $plugin_slug ][ $this->_user->id ] = $licenses;
9898
-
9899
- $this->set_account_option( 'licenses', $all_licenses, $store );
9900
- }
9901
-
9902
- /**
9903
- * Update user information.
9904
- *
9905
- * @author Vova Feldman (@svovaf)
9906
- * @since 1.0.1
9907
- *
9908
- * @param bool $store Flush to Database if true.
9909
- */
9910
- private function _store_user( $store = true ) {
9911
- $this->_logger->entrance();
9912
-
9913
- if ( empty( $this->_user->id ) ) {
9914
- $this->_logger->error( "Empty user ID, can't store user." );
9915
-
9916
- return;
9917
- }
9918
-
9919
- $users = self::get_all_users();
9920
- $users[ $this->_user->id ] = $this->_user;
9921
- self::$_accounts->set_option( 'users', $users, $store );
9922
- }
9923
-
9924
- /**
9925
- * Update new updates information.
9926
- *
9927
- * @author Vova Feldman (@svovaf)
9928
- * @since 1.0.4
9929
- *
9930
- * @param FS_Plugin_Tag|null $update
9931
- * @param bool $store Flush to Database if true.
9932
- * @param bool|number $plugin_id
9933
- */
9934
- private function _store_update( $update, $store = true, $plugin_id = false ) {
9935
- $this->_logger->entrance();
9936
-
9937
- if ( $update instanceof FS_Plugin_Tag ) {
9938
- $update->updated = time();
9939
- }
9940
-
9941
- if ( ! is_numeric( $plugin_id ) ) {
9942
- $plugin_id = $this->_plugin->id;
9943
- }
9944
-
9945
- $updates = self::get_all_updates();
9946
- $updates[ $plugin_id ] = $update;
9947
- self::$_accounts->set_option( 'updates', $updates, $store );
9948
- }
9949
-
9950
- /**
9951
- * Update new updates information.
9952
- *
9953
- * @author Vova Feldman (@svovaf)
9954
- * @since 1.0.6
9955
- *
9956
- * @param FS_Plugin[] $plugin_addons
9957
- * @param bool $store Flush to Database if true.
9958
- */
9959
- private function _store_addons( $plugin_addons, $store = true ) {
9960
- $this->_logger->entrance();
9961
-
9962
- $addons = self::get_all_addons();
9963
- $addons[ $this->_plugin->id ] = $plugin_addons;
9964
- self::$_accounts->set_option( 'addons', $addons, $store );
9965
- }
9966
-
9967
- /**
9968
- * Delete plugin's associated add-ons.
9969
- *
9970
- * @author Vova Feldman (@svovaf)
9971
- * @since 1.0.8
9972
- *
9973
- * @param bool $store
9974
- *
9975
- * @return bool
9976
- */
9977
- private function _delete_account_addons( $store = true ) {
9978
- $all_addons = self::get_all_account_addons();
9979
-
9980
- if ( ! isset( $all_addons[ $this->_plugin->id ] ) ) {
9981
- return false;
9982
- }
9983
-
9984
- unset( $all_addons[ $this->_plugin->id ] );
9985
-
9986
- self::$_accounts->set_option( 'account_addons', $all_addons, $store );
9987
-
9988
- return true;
9989
- }
9990
-
9991
- /**
9992
- * Update account add-ons list.
9993
- *
9994
- * @author Vova Feldman (@svovaf)
9995
- * @since 1.0.6
9996
- *
9997
- * @param FS_Plugin[] $addons
9998
- * @param bool $store Flush to Database if true.
9999
- */
10000
- private function _store_account_addons( $addons, $store = true ) {
10001
- $this->_logger->entrance();
10002
-
10003
- $all_addons = self::get_all_account_addons();
10004
- $all_addons[ $this->_plugin->id ] = $addons;
10005
- self::$_accounts->set_option( 'account_addons', $all_addons, $store );
10006
- }
10007
-
10008
- /**
10009
- * Store account params in the Database.
10010
- *
10011
- * @author Vova Feldman (@svovaf)
10012
- * @since 1.0.1
10013
- */
10014
- private function _store_account() {
10015
- $this->_logger->entrance();
10016
-
10017
- $this->_store_site( false );
10018
- $this->_store_user( false );
10019
- $this->_store_plans( false );
10020
- $this->_store_licenses( false );
10021
-
10022
- self::$_accounts->store();
10023
- }
10024
-
10025
- /**
10026
- * Sync user's information.
10027
- *
10028
- * @author Vova Feldman (@svovaf)
10029
- * @since 1.0.3
10030
- * @uses FS_Api
10031
- */
10032
- private function _handle_account_user_sync() {
10033
- $this->_logger->entrance();
10034
-
10035
- $api = $this->get_api_user_scope();
10036
-
10037
- // Get user's information.
10038
- $user = $api->get( '/', true );
10039
-
10040
- if ( isset( $user->id ) ) {
10041
- $this->_user->first = $user->first;
10042
- $this->_user->last = $user->last;
10043
- $this->_user->email = $user->email;
10044
-
10045
- $is_menu_item_account_visible = $this->is_submenu_item_visible( 'account' );
10046
-
10047
- if ( $user->is_verified &&
10048
- ( ! isset( $this->_user->is_verified ) || false === $this->_user->is_verified )
10049
- ) {
10050
- $this->_user->is_verified = true;
10051
-
10052
- $this->do_action( 'account_email_verified', $user->email );
10053
-
10054
- $this->_admin_notices->add(
10055
- $this->get_text( 'email-verified-message' ),
10056
- $this->get_text( 'right-on' ) . '!',
10057
- 'success',
10058
- // Make admin sticky if account menu item is invisible,
10059
- // since the page will be auto redirected to the plugin's
10060
- // main settings page, and the non-sticky message
10061
- // will disappear.
10062
- ! $is_menu_item_account_visible,
10063
- false,
10064
- 'email_verified'
10065
- );
10066
- }
10067
-
10068
- // Flush user details to DB.
10069
- $this->_store_user();
10070
-
10071
- $this->do_action( 'after_account_user_sync', $user );
10072
-
10073
- /**
10074
- * If account menu item is hidden, redirect to plugin's main settings page.
10075
- *
10076
- * @author Vova Feldman (@svovaf)
10077
- * @since 1.1.6
10078
- *
10079
- * @link https://github.com/Freemius/wordpress-sdk/issues/6
10080
- */
10081
- if ( ! $is_menu_item_account_visible ) {
10082
- fs_redirect( $this->_get_admin_page_url() );
10083
- }
10084
- }
10085
- }
10086
-
10087
- /**
10088
- * @param bool $store
10089
- *
10090
- * @return FS_Plugin_Plan|object|false
10091
- */
10092
- private function _enrich_site_plan( $store = true ) {
10093
- // Try to load plan from local cache.
10094
- $plan = $this->_get_plan_by_id( $this->_site->plan->id );
10095
-
10096
- if ( false === $plan ) {
10097
- $plan = $this->_fetch_site_plan();
10098
- }
10099
-
10100
- if ( $plan instanceof FS_Plugin_Plan ) {
10101
- $this->_update_plan( $plan, $store );
10102
- }
10103
-
10104
- return $plan;
10105
- }
10106
-
10107
- /**
10108
- * @author Vova Feldman (@svovaf)
10109
- * @since 1.0.9
10110
- * @uses FS_Api
10111
- *
10112
- * @param bool $store
10113
- *
10114
- * @return FS_Plugin_Plan|object|false
10115
- */
10116
- private function _enrich_site_trial_plan( $store = true ) {
10117
- // Try to load plan from local cache.
10118
- $trial_plan = $this->_get_plan_by_id( $this->_site->trial_plan_id );
10119
-
10120
- if ( false === $trial_plan ) {
10121
- $trial_plan = $this->_fetch_site_plan( $this->_site->trial_plan_id );
10122
- }
10123
-
10124
- if ( $trial_plan instanceof FS_Plugin_Plan ) {
10125
- $this->_storage->store( 'trial_plan', $trial_plan, $store );
10126
- }
10127
-
10128
- return $trial_plan;
10129
- }
10130
-
10131
- /**
10132
- * @author Vova Feldman (@svovaf)
10133
- * @since 1.0.9
10134
- * @uses FS_Api
10135
- *
10136
- * @param number|bool $license_id
10137
- *
10138
- * @return FS_Subscription|object|bool
10139
- */
10140
- private function _fetch_site_license_subscription( $license_id = false ) {
10141
- $this->_logger->entrance();
10142
- $api = $this->get_api_site_scope();
10143
-
10144
- if ( ! is_numeric( $license_id ) ) {
10145
- $license_id = $this->_license->id;
10146
- }
10147
-
10148
- $result = $api->get( "/licenses/{$license_id}/subscriptions.json", true );
10149
-
10150
- return ! isset( $result->error ) ?
10151
- ( ( is_array( $result->subscriptions ) && 0 < count( $result->subscriptions ) ) ?
10152
- new FS_Subscription( $result->subscriptions[0] ) :
10153
- false
10154
- ) :
10155
- $result;
10156
- }
10157
-
10158
- /**
10159
- * @author Vova Feldman (@svovaf)
10160
- * @since 1.0.4
10161
- * @uses FS_Api
10162
- *
10163
- * @param number|bool $plan_id
10164
- *
10165
- * @return FS_Plugin_Plan|object
10166
- */
10167
- private function _fetch_site_plan( $plan_id = false ) {
10168
- $this->_logger->entrance();
10169
- $api = $this->get_api_site_scope();
10170
-
10171
- if ( ! is_numeric( $plan_id ) ) {
10172
- $plan_id = $this->_site->plan->id;
10173
- }
10174
-
10175
- $plan = $api->get( "/plans/{$plan_id}.json", true );
10176
-
10177
- return ! isset( $plan->error ) ? new FS_Plugin_Plan( $plan ) : $plan;
10178
- }
10179
-
10180
- /**
10181
- * @author Vova Feldman (@svovaf)
10182
- * @since 1.0.5
10183
- * @uses FS_Api
10184
- *
10185
- * @return FS_Plugin_Plan[]|object
10186
- */
10187
- private function _fetch_plugin_plans() {
10188
- $this->_logger->entrance();
10189
- $api = $this->get_api_site_scope();
10190
-
10191
- $result = $api->get( '/plans.json', true );
10192
-
10193
- if ( $this->is_api_result_object( $result, 'plans' ) && is_array( $result->plans ) ) {
10194
- for ( $i = 0, $len = count( $result->plans ); $i < $len; $i ++ ) {
10195
- $result->plans[ $i ] = new FS_Plugin_Plan( $result->plans[ $i ] );
10196
- }
10197
-
10198
- $result = $result->plans;
10199
- }
10200
-
10201
- return $result;
10202
- }
10203
-
10204
- /**
10205
- * @author Vova Feldman (@svovaf)
10206
- * @since 1.0.5
10207
- * @uses FS_Api
10208
- *
10209
- * @param number|bool $plugin_id
10210
- * @param number|bool $site_license_id
10211
- *
10212
- * @return FS_Plugin_License[]|object
10213
- */
10214
- private function _fetch_licenses( $plugin_id = false, $site_license_id = false ) {
10215
- $this->_logger->entrance();
10216
-
10217
- $api = $this->get_api_user_scope();
10218
-
10219
- if ( ! is_numeric( $plugin_id ) ) {
10220
- $plugin_id = $this->_plugin->id;
10221
- }
10222
-
10223
- $result = $api->get( "/plugins/{$plugin_id}/licenses.json", true );
10224
-
10225
- $is_site_license_synced = false;
10226
-
10227
- $api_errors = array();
10228
-
10229
- if ( $this->is_api_result_object( $result, 'licenses' ) &&
10230
- is_array( $result->licenses )
10231
- ) {
10232
- for ( $i = 0, $len = count( $result->licenses ); $i < $len; $i ++ ) {
10233
- $result->licenses[ $i ] = new FS_Plugin_License( $result->licenses[ $i ] );
10234
-
10235
- if ( ( ! $is_site_license_synced ) && is_numeric( $site_license_id ) ) {
10236
- $is_site_license_synced = ( $site_license_id == $result->licenses[ $i ]->id );
10237
- }
10238
- }
10239
-
10240
- $result = $result->licenses;
10241
- } else {
10242
- $api_errors[] = $result;
10243
- $result = array();
10244
- }
10245
-
10246
- if ( ! $is_site_license_synced ) {
10247
- $api = $this->get_api_site_scope();
10248
-
10249
- if ( is_numeric( $site_license_id ) ) {
10250
- // Try to retrieve a foreign license that is linked to the install.
10251
- $api_result = $api->call( '/licenses.json' );
10252
-
10253
- if ( $this->is_api_result_object( $api_result, 'licenses' ) &&
10254
- is_array( $api_result->licenses )
10255
- ) {
10256
- $licenses = $api_result->licenses;
10257
-
10258
- if ( ! empty( $licenses ) ) {
10259
- $result[] = new FS_Plugin_License( $licenses[0] );
10260
- }
10261
- } else {
10262
- $api_errors[] = $api_result;
10263
- }
10264
- } else if ( is_object( $this->_license ) ) {
10265
- // Fetch foreign license by ID and license key.
10266
- $license = $api->get( "/licenses/{$this->_license->id}.json?license_key=" .
10267
- urlencode( $this->_license->secret_key ) );
10268
-
10269
- if ( $this->is_api_result_entity( $license ) ) {
10270
- $result[] = new FS_Plugin_License( $license );
10271
- } else {
10272
- $api_errors[] = $license;
10273
- }
10274
- }
10275
- }
10276
-
10277
- if ( is_array( $result ) && 0 < count( $result ) ) {
10278
- // If found at least one license, return license collection even if there are errors.
10279
- return $result;
10280
- }
10281
-
10282
- if ( ! empty( $api_errors ) ) {
10283
- // If found any errors and no licenses, return first error.
10284
- return $api_errors[0];
10285
- }
10286
-
10287
- // Fallback to empty licenses list.
10288
- return $result;
10289
- }
10290
-
10291
- /**
10292
- * @author Vova Feldman (@svovaf)
10293
- * @since 1.2.0
10294
- * @uses FS_Api
10295
- *
10296
- * @param number|bool $plugin_id
10297
- * @param bool $flush
10298
- *
10299
- * @return FS_Payment[]|object
10300
- */
10301
- function _fetch_payments( $plugin_id = false, $flush = false ) {
10302
- $this->_logger->entrance();
10303
-
10304
- $api = $this->get_api_user_scope();
10305
-
10306
- if ( ! is_numeric( $plugin_id ) ) {
10307
- $plugin_id = $this->_plugin->id;
10308
- }
10309
-
10310
- $result = $api->get( "/plugins/{$plugin_id}/payments.json?include_addons=true", $flush );
10311
-
10312
- if ( ! isset( $result->error ) ) {
10313
- for ( $i = 0, $len = count( $result->payments ); $i < $len; $i ++ ) {
10314
- $result->payments[ $i ] = new FS_Payment( $result->payments[ $i ] );
10315
- }
10316
- $result = $result->payments;
10317
- }
10318
-
10319
- return $result;
10320
- }
10321
-
10322
- /**
10323
- * @author Vova Feldman (@svovaf)
10324
- * @since 1.2.1.5
10325
- * @uses FS_Api
10326
- *
10327
- * @param bool $flush
10328
- *
10329
- * @return \FS_Billing|mixed
10330
- */
10331
- function _fetch_billing( $flush = false ) {
10332
- require_once WP_FS__DIR_INCLUDES . '/entities/class-fs-billing.php';
10333
-
10334
- $billing = $this->get_api_user_scope()->get( 'billing.json', $flush );
10335
-
10336
- if ( $this->is_api_result_entity( $billing ) ) {
10337
- $billing = new FS_Billing( $billing );
10338
- }
10339
-
10340
- return $billing;
10341
- }
10342
-
10343
- /**
10344
- * @author Vova Feldman (@svovaf)
10345
- * @since 1.0.4
10346
- *
10347
- * @param FS_Plugin_Plan $plan
10348
- * @param bool $store
10349
- */
10350
- private function _update_plan( $plan, $store = false ) {
10351
- $this->_logger->entrance();
10352
-
10353
- $this->_site->plan = $plan;
10354
- $this->_store_site( $store );
10355
- }
10356
-
10357
- /**
10358
- * @author Vova Feldman (@svovaf)
10359
- * @since 1.0.5
10360
- *
10361
- * @param FS_Plugin_License[] $licenses
10362
- * @param string|bool $plugin_slug
10363
- */
10364
- private function _update_licenses( $licenses, $plugin_slug = false ) {
10365
- $this->_logger->entrance();
10366
-
10367
- if ( is_array( $licenses ) ) {
10368
- for ( $i = 0, $len = count( $licenses ); $i < $len; $i ++ ) {
10369
- $licenses[ $i ]->updated = time();
10370
- }
10371
- }
10372
-
10373
- if ( ! is_string( $plugin_slug ) ) {
10374
- $this->_licenses = $licenses;
10375
- }
10376
-
10377
- $this->_store_licenses( true, $plugin_slug, $licenses );
10378
- }
10379
-
10380
- /**
10381
- * @author Vova Feldman (@svovaf)
10382
- * @since 1.0.4
10383
- *
10384
- * @param bool|number $plugin_id
10385
- * @param bool $flush Since 1.1.7.3
10386
- * @param int $expiration Since 1.2.2.7
10387
- *
10388
- * @return object|false New plugin tag info if exist.
10389
- */
10390
- private function _fetch_newer_version( $plugin_id = false, $flush = true, $expiration = WP_FS__TIME_24_HOURS_IN_SEC ) {
10391
- $latest_tag = $this->_fetch_latest_version( $plugin_id, $flush, $expiration );
10392
-
10393
- if ( ! is_object( $latest_tag ) ) {
10394
- return false;
10395
- }
10396
-
10397
- // Check if version is actually newer.
10398
- $has_new_version =
10399
- // If it's an non-installed add-on then always return latest.
10400
- ( $this->_is_addon_id( $plugin_id ) && ! $this->is_addon_activated( $plugin_id ) ) ||
10401
- // Compare versions.
10402
- version_compare( $this->get_plugin_version(), $latest_tag->version, '<' );
10403
-
10404
- $this->_logger->departure( $has_new_version ? 'Found newer plugin version ' . $latest_tag->version : 'No new version' );
10405
-
10406
- return $has_new_version ? $latest_tag : false;
10407
- }
10408
-
10409
- /**
10410
- * @author Vova Feldman (@svovaf)
10411
- * @since 1.0.5
10412
- *
10413
- * @param bool|number $plugin_id
10414
- * @param bool $flush Since 1.1.7.3
10415
- * @param int $expiration Since 1.2.2.7
10416
- *
10417
- * @return bool|FS_Plugin_Tag
10418
- */
10419
- function get_update( $plugin_id = false, $flush = true, $expiration = WP_FS__TIME_24_HOURS_IN_SEC ) {
10420
- $this->_logger->entrance();
10421
-
10422
- if ( ! is_numeric( $plugin_id ) ) {
10423
- $plugin_id = $this->_plugin->id;
10424
- }
10425
-
10426
- $this->check_updates( true, $plugin_id, $flush, $expiration );
10427
- $updates = $this->get_all_updates();
10428
-
10429
- return isset( $updates[ $plugin_id ] ) && is_object( $updates[ $plugin_id ] ) ? $updates[ $plugin_id ] : false;
10430
- }
10431
-
10432
- /**
10433
- * Check if site assigned with active license.
10434
- *
10435
- * @author Vova Feldman (@svovaf)
10436
- * @since 1.0.6
10437
- *
10438
- * @deprecated Please use has_active_valid_license() instead because license can be cancelled.
10439
- */
10440
- function has_active_license() {
10441
- return (
10442
- is_object( $this->_license ) &&
10443
- is_numeric( $this->_license->id ) &&
10444
- ! $this->_license->is_expired()
10445
- );
10446
- }
10447
-
10448
- /**
10449
- * Check if site assigned with active & valid (not expired) license.
10450
- *
10451
- * @author Vova Feldman (@svovaf)
10452
- * @since 1.2.1
10453
- */
10454
- function has_active_valid_license() {
10455
- return (
10456
- is_object( $this->_license ) &&
10457
- is_numeric( $this->_license->id ) &&
10458
- $this->_license->is_active() &&
10459
- $this->_license->is_valid()
10460
- );
10461
- }
10462
-
10463
- /**
10464
- * Check if site assigned with license with enabled features.
10465
- *
10466
- * @author Vova Feldman (@svovaf)
10467
- * @since 1.0.6
10468
- *
10469
- * @return bool
10470
- */
10471
- function has_features_enabled_license() {
10472
- return (
10473
- is_object( $this->_license ) &&
10474
- is_numeric( $this->_license->id ) &&
10475
- $this->_license->is_features_enabled()
10476
- );
10477
- }
10478
-
10479
- /**
10480
- * Check if user is a trial or have feature enabled license.
10481
- *
10482
- * @author Vova Feldman (@svovaf)
10483
- * @since 1.1.7
10484
- *
10485
- * @return bool
10486
- */
10487
- function can_use_premium_code() {
10488
- return $this->is_trial() || $this->has_features_enabled_license();
10489
- }
10490
-
10491
- /**
10492
- * Checks if the current user can activate plugins or switch themes. Note that this method should only be used
10493
- * after the `init` action is triggered because it is using `current_user_can()` which is only functional after
10494
- * the context user is authenticated.
10495
- *
10496
- * @author Leo Fajardo (@leorw)
10497
- * @since 1.2.2
10498
- *
10499
- * @return bool
10500
- */
10501
- function is_user_admin() {
10502
- return ( $this->is_plugin() && current_user_can( 'activate_plugins' ) )
10503
- || ( $this->is_theme() && current_user_can( 'switch_themes' ) );
10504
- }
10505
-
10506
- /**
10507
- * Sync site's plan.
10508
- *
10509
- * @author Vova Feldman (@svovaf)
10510
- * @since 1.0.3
10511
- *
10512
- * @uses FS_Api
10513
- *
10514
- * @param bool $background Hints the method if it's a background sync. If false, it means that was initiated by
10515
- * the admin.
10516
- */
10517
- private function _sync_license( $background = false ) {
10518
- $this->_logger->entrance();
10519
-
10520
- $plugin_id = fs_request_get( 'plugin_id', $this->get_id() );
10521
-
10522
- $is_addon_sync = ( ! $this->_plugin->is_addon() && $plugin_id != $this->get_id() );
10523
-
10524
- if ( $is_addon_sync ) {
10525
- $this->_sync_addon_license( $plugin_id, $background );
10526
- } else {
10527
- $this->_sync_plugin_license( $background );
10528
- }
10529
-
10530
- $this->do_action( 'after_account_plan_sync', $this->_site->plan->name );
10531
- }
10532
-
10533
- /**
10534
- * Sync plugin's add-on license.
10535
- *
10536
- * @author Vova Feldman (@svovaf)
10537
- * @since 1.0.6
10538
- * @uses FS_Api
10539
- *
10540
- * @param number $addon_id
10541
- * @param bool $background
10542
- */
10543
- private function _sync_addon_license( $addon_id, $background ) {
10544
- $this->_logger->entrance();
10545
-
10546
- if ( $this->is_addon_activated( $addon_id ) ) {
10547
- // If already installed, use add-on sync.
10548
- $fs_addon = self::get_instance_by_id( $addon_id );
10549
- $fs_addon->_sync_license( $background );
10550
-
10551
- return;
10552
- }
10553
-
10554
- // Validate add-on exists.
10555
- $addon = $this->get_addon( $addon_id );
10556
-
10557
- if ( ! is_object( $addon ) ) {
10558
- return;
10559
- }
10560
-
10561
- // Add add-on into account add-ons.
10562
- $account_addons = $this->get_account_addons();
10563
- if ( ! is_array( $account_addons ) ) {
10564
- $account_addons = array();
10565
- }
10566
- $account_addons[] = $addon->id;
10567
- $account_addons = array_unique( $account_addons );
10568
- $this->_store_account_addons( $account_addons );
10569
-
10570
- // Load add-on licenses.
10571
- $licenses = $this->_fetch_licenses( $addon->id );
10572
-
10573
- // Sync add-on licenses.
10574
- if ( $this->is_array_instanceof( $licenses, 'FS_Plugin_License' ) ) {
10575
- $this->_update_licenses( $licenses, $addon->slug );
10576
-
10577
- if ( ! $this->is_addon_installed( $addon->id ) && FS_License_Manager::has_premium_license( $licenses ) ) {
10578
- $plans_result = $this->get_api_site_or_plugin_scope()->get( "/addons/{$addon_id}/plans.json" );
10579
-
10580
- if ( ! isset( $plans_result->error ) ) {
10581
- $plans = array();
10582
- foreach ( $plans_result->plans as $plan ) {
10583
- $plans[] = new FS_Plugin_Plan( $plan );
10584
- }
10585
-
10586
- $this->_admin_notices->add_sticky(
10587
- FS_Plan_Manager::instance()->has_free_plan( $plans ) ?
10588
- sprintf(
10589
- $this->get_text( 'addon-successfully-upgraded-message' ),
10590
- $addon->title
10591
- ) . ' ' . $this->get_latest_download_link(
10592
- $this->get_text( 'download-latest-version' ),
10593
- $addon_id
10594
- )
10595
- :
10596
- sprintf(
10597
- $this->get_text( 'addon-successfully-purchased-message' ),
10598
- $addon->title
10599
- ) . ' ' . $this->get_latest_download_link(
10600
- $this->get_text( 'download-latest-version' ),
10601
- $addon_id
10602
- ),
10603
- 'addon_plan_upgraded_' . $addon->slug,
10604
- $this->get_text( 'yee-haw' ) . '!'
10605
- );
10606
- }
10607
- }
10608
- }
10609
- }
10610
-
10611
- /**
10612
- * Sync site's plugin plan.
10613
- *
10614
- * @author Vova Feldman (@svovaf)
10615
- * @since 1.0.6
10616
- * @uses FS_Api
10617
- *
10618
- * @param bool $background Hints the method if it's a background sync. If false, it means that was initiated by
10619
- * the admin.
10620
- */
10621
- private function _sync_plugin_license( $background = false ) {
10622
- $this->_logger->entrance();
10623
-
10624
- /**
10625
- * Sync site info.
10626
- *
10627
- * @todo This line will execute install sync on a daily basis, even if running the free version (for opted-in users). The reason we want to keep it that way is for cases when the user was a paying customer, then there was a failure in subscription payment, and then after some time the payment was successful. This could be heavily optimized. For example, we can skip the $flush if the current install was never associated with a paid version.
10628
- */
10629
- $site = $this->send_install_update( array(), true );
10630
-
10631
- $plan_change = 'none';
10632
-
10633
- if ( ! $this->is_api_result_entity( $site ) ) {
10634
- // Show API messages only if not background sync or if paying customer.
10635
- if ( ! $background || $this->is_paying() ) {
10636
- // Try to ping API to see if not blocked.
10637
- if ( ! FS_Api::test() ) {
10638
- /**
10639
- * Failed to ping API - blocked!
10640
- *
10641
- * @author Vova Feldman (@svovaf)
10642
- * @since 1.1.6 Only show message related to one of the Freemius powered plugins. Once it will be resolved it will fix the issue for all plugins anyways. There's no point to scare users with multiple error messages.
10643
- */
10644
- $api = $this->get_api_site_scope();
10645
-
10646
- if ( ! self::$_global_admin_notices->has_sticky( 'api_blocked' ) ) {
10647
- self::$_global_admin_notices->add(
10648
- sprintf(
10649
- $this->get_text( 'server-blocking-access' ),
10650
- $this->get_plugin_name(),
10651
- '<a href="' . $api->get_url() . '" target="_blank">' . $api->get_url() . '</a>'
10652
- ) . '<br> ' . $this->get_text( 'server-error-message' ) . var_export( $site->error, true ),
10653
- $this->get_text( 'oops' ) . '...',
10654
- 'error',
10655
- $background,
10656
- false,
10657
- 'api_blocked'
10658
- );
10659
- }
10660
- } else {
10661
- // Authentication params are broken.
10662
- $this->_admin_notices->add(
10663
- $this->get_text( 'wrong-authentication-param-message' ),
10664
- $this->get_text( 'oops' ) . '...',
10665
- 'error'
10666
- );
10667
- }
10668
- }
10669
-
10670
- // No reason to continue with license sync while there are API issues.
10671
- return;
10672
- }
10673
-
10674
- // Remove sticky API connectivity message.
10675
- self::$_global_admin_notices->remove_sticky( 'api_blocked' );
10676
-
10677
- $site = new FS_Site( $site );
10678
-
10679
- // Sync plans.
10680
- $this->_sync_plans();
10681
-
10682
- if ( ! $this->has_paid_plan() ) {
10683
- $this->_site = $site;
10684
- $this->_enrich_site_plan( true );
10685
- $this->_store_site();
10686
- } else {
10687
- /**
10688
- * Sync licenses. Pass the site's license ID so that the foreign licenses will be fetched if the license
10689
- * associated with that ID is not included in the user's licenses collection.
10690
- */
10691
- $this->_sync_licenses( $site->license_id );
10692
-
10693
- // Check if plan / license changed.
10694
- if ( ! FS_Entity::equals( $site->plan, $this->_site->plan ) ||
10695
- // Check if trial started.
10696
- $site->trial_plan_id != $this->_site->trial_plan_id ||
10697
- $site->trial_ends != $this->_site->trial_ends ||
10698
- // Check if license changed.
10699
- $site->license_id != $this->_site->license_id
10700
- ) {
10701
- if ( $site->is_trial() && ( ! $this->_site->is_trial() || $site->trial_ends != $this->_site->trial_ends ) ) {
10702
- // New trial started.
10703
- $this->_site = $site;
10704
- $plan_change = 'trial_started';
10705
-
10706
- // Store trial plan information.
10707
- $this->_enrich_site_trial_plan( true );
10708
-
10709
- // For trial with subscription use-case.
10710
- $new_license = is_null( $site->license_id ) ? null : $this->_get_license_by_id( $site->license_id );
10711
-
10712
- if ( is_object( $new_license ) && $new_license->is_valid() ) {
10713
- $this->_site = $site;
10714
- $this->_update_site_license( $new_license );
10715
- $this->_store_licenses();
10716
- $this->_enrich_site_plan( true );
10717
-
10718
- $this->_sync_site_subscription( $this->_license );
10719
- }
10720
- } else if ( $this->_site->is_trial() && ! $site->is_trial() && ! is_numeric( $site->license_id ) ) {
10721
- // Was in trial, but now trial expired and no license ID.
10722
- // New trial started.
10723
- $this->_site = $site;
10724
- $plan_change = 'trial_expired';
10725
-
10726
- // Clear trial plan information.
10727
- $this->_storage->trial_plan = null;
10728
-
10729
- } else {
10730
- $is_free = $this->is_free_plan();
10731
-
10732
- // Make sure license exist and not expired.
10733
- $new_license = is_null( $site->license_id ) ?
10734
- null :
10735
- $this->_get_license_by_id( $site->license_id );
10736
-
10737
- if ( $is_free && is_null( $new_license ) && $this->has_any_license() && $this->_license->is_cancelled ) {
10738
- // License cancelled.
10739
- $this->_site = $site;
10740
- $this->_update_site_license( $new_license );
10741
- $this->_store_licenses();
10742
- $this->_enrich_site_plan( true );
10743
-
10744
- $plan_change = 'cancelled';
10745
- } else if ( $is_free && ( ( ! is_object( $new_license ) || $new_license->is_expired() ) ) ) {
10746
- // The license is expired, so ignore upgrade method.
10747
- } else {
10748
- // License changed.
10749
- $this->_site = $site;
10750
- $this->_update_site_license( $new_license );
10751
- $this->_store_licenses();
10752
- $this->_enrich_site_plan( true );
10753
-
10754
- $plan_change = $is_free ?
10755
- 'upgraded' :
10756
- ( is_object( $new_license ) ?
10757
- 'changed' :
10758
- 'downgraded' );
10759
- }
10760
- }
10761
-
10762
- // Store updated site info.
10763
- $this->_store_site();
10764
- } else {
10765
- if ( is_object( $this->_license ) && $this->_license->is_expired() ) {
10766
- if ( ! $this->has_features_enabled_license() ) {
10767
- $this->_deactivate_license();
10768
- $plan_change = 'downgraded';
10769
- } else {
10770
- $plan_change = 'expired';
10771
- }
10772
- }
10773
-
10774
- if ( is_numeric( $site->license_id ) && is_object( $this->_license ) ) {
10775
- $this->_sync_site_subscription( $this->_license );
10776
- }
10777
- }
10778
- }
10779
-
10780
- if ( $this->has_paid_plan() ) {
10781
- switch ( $plan_change ) {
10782
- case 'none':
10783
- if ( ! $background && is_admin() ) {
10784
- $plan = $this->is_trial() ?
10785
- $this->_storage->trial_plan :
10786
- $this->_site->plan;
10787
-
10788
- if ( $plan->is_free() ) {
10789
- $this->_admin_notices->add(
10790
- sprintf(
10791
- $this->get_text( 'plan-did-not-change-message' ),
10792
- '<i><b>' . $plan->title . ( $this->is_trial() ? ' ' . $this->get_text( 'trial' ) : '' ) . '</b></i>'
10793
- ) . ' ' . sprintf(
10794
- '<a href="%s">%s</a>',
10795
- $this->contact_url(
10796
- 'bug',
10797
- sprintf( $this->get_text( 'plan-did-not-change-email-message' ),
10798
- strtoupper( $plan->name )
10799
- )
10800
- ),
10801
- $this->get_text( 'contact-us-here' )
10802
- ),
10803
- $this->get_text( 'hmm' ) . '...'
10804
- );
10805
- }
10806
- }
10807
- break;
10808
- case 'upgraded':
10809
- $this->_admin_notices->add_sticky(
10810
- sprintf(
10811
- $this->get_text( 'plan-upgraded-message' ),
10812
- '<i>' . $this->get_plugin_name() . '</i>'
10813
- ) . $this->get_complete_upgrade_instructions(),
10814
- 'plan_upgraded',
10815
- $this->get_text( 'yee-haw' ) . '!'
10816
- );
10817
-
10818
- $this->_admin_notices->remove_sticky( array(
10819
- 'trial_started',
10820
- 'trial_promotion',
10821
- 'trial_expired',
10822
- 'activation_complete',
10823
- ) );
10824
- break;
10825
- case 'changed':
10826
- $this->_admin_notices->add_sticky(
10827
- sprintf(
10828
- $this->get_text( 'plan-changed-to-x-message' ),
10829
- $this->_site->plan->title
10830
- ),
10831
- 'plan_changed'
10832
- );
10833
-
10834
- $this->_admin_notices->remove_sticky( array(
10835
- 'trial_started',
10836
- 'trial_promotion',
10837
- 'trial_expired',
10838
- 'activation_complete',
10839
- ) );
10840
- break;
10841
- case 'downgraded':
10842
- $this->_admin_notices->add_sticky(
10843
- sprintf( $this->get_text( 'license-expired-blocking-message' ), $this->_module_type ),
10844
- 'license_expired',
10845
- $this->get_text( 'hmm' ) . '...'
10846
- );
10847
- $this->_admin_notices->remove_sticky( 'plan_upgraded' );
10848
- break;
10849
- case 'cancelled':
10850
- $this->_admin_notices->add(
10851
- $this->get_text( 'license-cancelled' ) . ' ' .
10852
- sprintf(
10853
- '<a href="%s">%s</a>',
10854
- $this->contact_url( 'bug' ),
10855
- $this->get_text( 'contact-us-here' )
10856
- ),
10857
- $this->get_text( 'hmm' ) . '...',
10858
- 'error'
10859
- );
10860
- $this->_admin_notices->remove_sticky( 'plan_upgraded' );
10861
- break;
10862
- case 'expired':
10863
- $this->_admin_notices->add_sticky(
10864
- sprintf( $this->get_text( 'license-expired-non-blocking-message' ), $this->_site->plan->title ),
10865
- 'license_expired',
10866
- $this->get_text( 'hmm' ) . '...'
10867
- );
10868
- $this->_admin_notices->remove_sticky( 'plan_upgraded' );
10869
- break;
10870
- case 'trial_started':
10871
- $this->_admin_notices->add_sticky(
10872
- sprintf(
10873
- $this->get_text( 'trial-started-message' ),
10874
- '<i>' . $this->get_plugin_name() . '</i>'
10875
- ) . $this->get_complete_upgrade_instructions( $this->_storage->trial_plan->title ),
10876
- 'trial_started',
10877
- $this->get_text( 'yee-haw' ) . '!'
10878
- );
10879
-
10880
- $this->_admin_notices->remove_sticky( array(
10881
- 'trial_promotion',
10882
- ) );
10883
- break;
10884
- case 'trial_expired':
10885
- $this->_admin_notices->add_sticky(
10886
- $this->get_text( 'trial-expired-message' ),
10887
- 'trial_expired',
10888
- $this->get_text( 'hmm' ) . '...'
10889
- );
10890
- $this->_admin_notices->remove_sticky( array(
10891
- 'trial_started',
10892
- 'trial_promotion',
10893
- 'plan_upgraded',
10894
- ) );
10895
- break;
10896
- }
10897
- }
10898
-
10899
- if ( 'none' !== $plan_change ) {
10900
- $this->do_action( 'after_license_change', $plan_change, $this->_site->plan );
10901
- }
10902
- }
10903
-
10904
- /**
10905
- * @author Vova Feldman (@svovaf)
10906
- * @since 1.0.5
10907
- *
10908
- * @param bool $background
10909
- */
10910
- protected function _activate_license( $background = false ) {
10911
- $this->_logger->entrance();
10912
-
10913
- $license_id = fs_request_get( 'license_id' );
10914
-
10915
- if ( FS_Plugin_License::is_valid_id( $license_id ) && $license_id == $this->_site->license_id ) {
10916
- // License is already activated.
10917
- return;
10918
- }
10919
-
10920
- $premium_license = FS_Plugin_License::is_valid_id( $license_id ) ?
10921
- $this->_get_license_by_id( $license_id ) :
10922
- $this->_get_available_premium_license();
10923
-
10924
- if ( ! is_object( $premium_license ) ) {
10925
- return;
10926
- }
10927
-
10928
- /**
10929
- * If the premium license is already associated with the install, just
10930
- * update the license reference (activation is not required).
10931
- *
10932
- * @since 1.1.9
10933
- */
10934
- if ( $premium_license->id == $this->_site->license_id ) {
10935
- // License is already activated.
10936
- $this->_update_site_license( $premium_license );
10937
- $this->_enrich_site_plan( false );
10938
- $this->_store_account();
10939
-
10940
- return;
10941
- }
10942
-
10943
- if ( $this->_site->user_id != $premium_license->user_id ) {
10944
- $api_request_params = array( 'license_key' => $premium_license->secret_key );
10945
- } else {
10946
- $api_request_params = array();
10947
- }
10948
-
10949
- $api = $this->get_api_site_scope();
10950
- $license = $api->call( "/licenses/{$premium_license->id}.json", 'put', $api_request_params );
10951
-
10952
- if ( ! $this->is_api_result_entity( $license ) ) {
10953
- if ( ! $background ) {
10954
- $this->_admin_notices->add( sprintf(
10955
- '%s %s',
10956
- $this->get_text( 'license-activation-failed-message' ),
10957
- ( is_object( $license ) && isset( $license->error ) ?
10958
- $license->error->message :
10959
- sprintf( '%s<br><code>%s</code>',
10960
- $this->get_text( 'server-error-message' ),
10961
- var_export( $license, true )
10962
- )
10963
- )
10964
- ),
10965
- $this->get_text( 'hmm' ) . '...',
10966
- 'error'
10967
- );
10968
- }
10969
-
10970
- return;
10971
- }
10972
-
10973
- $premium_license = new FS_Plugin_License( $license );
10974
-
10975
- // Updated site plan.
10976
- $site = $this->get_api_site_scope()->get( '/', true );
10977
- if ( $this->is_api_result_entity( $site ) ) {
10978
- $this->_site = new FS_Site( $site );
10979
- }
10980
- $this->_update_site_license( $premium_license );
10981
- $this->_enrich_site_plan( false );
10982
-
10983
- $this->_store_account();
10984
-
10985
- if ( ! $background ) {
10986
- $this->_admin_notices->add_sticky(
10987
- $this->get_text( 'license-activated-message' ) .
10988
- $this->get_complete_upgrade_instructions(),
10989
- 'license_activated',
10990
- $this->get_text( 'yee-haw' ) . '!'
10991
- );
10992
- }
10993
-
10994
- $this->_admin_notices->remove_sticky( array(
10995
- 'trial_promotion',
10996
- 'license_expired',
10997
- ) );
10998
- }
10999
-
11000
- /**
11001
- * @author Vova Feldman (@svovaf)
11002
- * @since 1.0.5
11003
- *
11004
- * @param bool $show_notice
11005
- */
11006
- protected function _deactivate_license( $show_notice = true ) {
11007
- $this->_logger->entrance();
11008
-
11009
- if ( ! is_object( $this->_license ) ) {
11010
- $this->_admin_notices->add(
11011
- sprintf( $this->get_text( 'no-active-license-message' ), $this->_site->plan->title ),
11012
- $this->get_text( 'hmm' ) . '...'
11013
- );
11014
-
11015
- return;
11016
- }
11017
-
11018
- $api = $this->get_api_site_scope();
11019
- $license = $api->call( "/licenses/{$this->_site->license_id}.json", 'delete' );
11020
-
11021
- if ( isset( $license->error ) ) {
11022
- $this->_admin_notices->add(
11023
- $this->get_text( 'license-deactivation-failed-message' ) . '<br> ' .
11024
- $this->get_text( 'server-error-message' ) . ' ' . var_export( $license->error, true ),
11025
- $this->get_text( 'hmm' ) . '...',
11026
- 'error'
11027
- );
11028
-
11029
- return;
11030
- }
11031
-
11032
- // Update license cache.
11033
- for ( $i = 0, $len = count( $this->_licenses ); $i < $len; $i ++ ) {
11034
- if ( $license->id == $this->_licenses[ $i ]->id ) {
11035
- $this->_licenses[ $i ] = new FS_Plugin_License( $license );
11036
- }
11037
- }
11038
-
11039
- // Updated site plan to default.
11040
- $this->_sync_plans();
11041
- $this->_site->plan->id = $this->_plans[0]->id;
11042
- // Unlink license from site.
11043
- $this->_update_site_license( null );
11044
- $this->_enrich_site_plan( false );
11045
-
11046
- $this->_store_account();
11047
-
11048
- if ( $show_notice ) {
11049
- $this->_admin_notices->add(
11050
- sprintf( $this->get_text( 'license-deactivation-message' ), $this->_site->plan->title ),
11051
- $this->get_text( 'ok' )
11052
- );
11053
- }
11054
-
11055
- $this->_admin_notices->remove_sticky( array(
11056
- 'plan_upgraded',
11057
- 'license_activated',
11058
- ) );
11059
- }
11060
-
11061
- /**
11062
- * Site plan downgrade.
11063
- *
11064
- * @author Vova Feldman (@svovaf)
11065
- * @since 1.0.4
11066
- *
11067
- * @uses FS_Api
11068
- */
11069
- private function _downgrade_site() {
11070
- $this->_logger->entrance();
11071
-
11072
- $api = $this->get_api_site_scope();
11073
- $site = $api->call( 'downgrade.json', 'put' );
11074
-
11075
- $plan_downgraded = false;
11076
- $plan = false;
11077
- if ( $this->is_api_result_entity( $site ) ) {
11078
- $prev_plan_id = $this->_site->plan->id;
11079
-
11080
- // Update new site plan id.
11081
- $this->_site->plan->id = $site->plan_id;
11082
-
11083
- $plan = $this->_enrich_site_plan();
11084
- $subscription = $this->_sync_site_subscription( $this->_license );
11085
-
11086
- // Plan downgraded if plan was changed or subscription was cancelled.
11087
- $plan_downgraded = ( $plan instanceof FS_Plugin_Plan && $prev_plan_id != $plan->id ) ||
11088
- ( is_object( $subscription ) && ! isset( $subscription->error ) && ! $subscription->is_active() );
11089
- } else {
11090
- // handle different error cases.
11091
-
11092
- }
11093
-
11094
- if ( $plan_downgraded ) {
11095
- // Remove previous sticky message about upgrade (if exist).
11096
- $this->_admin_notices->remove_sticky( 'plan_upgraded' );
11097
-
11098
- $this->_admin_notices->add(
11099
- sprintf( $this->get_text( 'plan-x-downgraded-message' ),
11100
- $plan->title,
11101
- human_time_diff( time(), strtotime( $this->_license->expiration ) )
11102
- )
11103
- );
11104
-
11105
- // Store site updates.
11106
- $this->_store_site();
11107
- } else {
11108
- $this->_admin_notices->add(
11109
- $this->get_text( 'plan-downgraded-failure-message' ),
11110
- $this->get_text( 'oops' ) . '...',
11111
- 'error'
11112
- );
11113
- }
11114
- }
11115
-
11116
- /**
11117
- * @author Vova Feldman (@svovaf)
11118
- * @since 1.1.8.1
11119
- *
11120
- * @param bool|string $plan_name
11121
- *
11122
- * @return bool If trial was successfully started.
11123
- */
11124
- function start_trial( $plan_name = false ) {
11125
- $this->_logger->entrance();
11126
-
11127
- if ( $this->is_trial() ) {
11128
- // Already in trial mode.
11129
- $this->_admin_notices->add(
11130
- sprintf( $this->get_text( 'in-trial-mode' ), $this->_module_type ),
11131
- $this->get_text( 'oops' ) . '...',
11132
- 'error'
11133
- );
11134
-
11135
- return false;
11136
- }
11137
-
11138
- if ( $this->_site->is_trial_utilized() ) {
11139
- // Trial was already utilized.
11140
- $this->_admin_notices->add(
11141
- $this->get_text( 'trial-utilized' ),
11142
- $this->get_text( 'oops' ) . '...',
11143
- 'error'
11144
- );
11145
-
11146
- return false;
11147
- }
11148
-
11149
- if ( false !== $plan_name ) {
11150
- $plan = $this->get_plan_by_name( $plan_name );
11151
-
11152
- if ( false === $plan ) {
11153
- // Plan doesn't exist.
11154
- $this->_admin_notices->add(
11155
- sprintf( $this->get_text( 'trial-plan-x-not-exist' ), $plan_name ),
11156
- $this->get_text( 'oops' ) . '...',
11157
- 'error'
11158
- );
11159
-
11160
- return false;
11161
- }
11162
-
11163
- if ( ! $plan->has_trial() ) {
11164
- // Plan doesn't exist.
11165
- $this->_admin_notices->add(
11166
- sprintf( $this->get_text( 'plan-x-no-trial' ), $plan_name ),
11167
- $this->get_text( 'oops' ) . '...',
11168
- 'error'
11169
- );
11170
-
11171
- return false;
11172
- }
11173
- } else {
11174
- if ( ! $this->has_trial_plan() ) {
11175
- // None of the plans have a trial.
11176
- $this->_admin_notices->add(
11177
- sprintf( $this->get_text( 'no-trials' ), $this->_module_type ),
11178
- $this->get_text( 'oops' ) . '...',
11179
- 'error'
11180
- );
11181
-
11182
- return false;
11183
- }
11184
-
11185
- $plans_with_trial = FS_Plan_Manager::instance()->get_trial_plans( $this->_plans );
11186
-
11187
- $plan = $plans_with_trial[0];
11188
- }
11189
-
11190
- $api = $this->get_api_site_scope();
11191
- $plan = $api->call( "plans/{$plan->id}/trials.json", 'post' );
11192
-
11193
- if ( ! $this->is_api_result_entity( $plan ) ) {
11194
- // Some API error while trying to start the trial.
11195
- $this->_admin_notices->add(
11196
- sprintf( $this->get_text( 'unexpected-api-error' ), $this->_module_type )
11197
- . ' ' . var_export( $plan, true ),
11198
- $this->get_text( 'oops' ) . '...',
11199
- 'error'
11200
- );
11201
-
11202
- return false;
11203
- }
11204
-
11205
- // Sync license.
11206
- $this->_sync_license();
11207
-
11208
- return $this->is_trial();
11209
- }
11210
-
11211
- /**
11212
- * Cancel site trial.
11213
- *
11214
- * @author Vova Feldman (@svovaf)
11215
- * @since 1.0.9
11216
- *
11217
- * @uses FS_Api
11218
- */
11219
- private function _cancel_trial() {
11220
- $this->_logger->entrance();
11221
-
11222
- if ( ! $this->is_trial() ) {
11223
- $this->_admin_notices->add(
11224
- $this->get_text( 'trial-cancel-no-trial-message' ),
11225
- $this->get_text( 'oops' ) . '...',
11226
- 'error'
11227
- );
11228
-
11229
- return;
11230
- }
11231
-
11232
- $api = $this->get_api_site_scope();
11233
- $site = $api->call( 'trials.json', 'delete' );
11234
-
11235
- $trial_cancelled = false;
11236
-
11237
- if ( $this->is_api_result_entity( $site ) ) {
11238
- $prev_trial_ends = $this->_site->trial_ends;
11239
-
11240
- if ( $this->is_paid_trial() ) {
11241
- $this->_license->expiration = $site->trial_ends;
11242
- $this->_license->is_cancelled = true;
11243
- $this->_update_site_license( $this->_license );
11244
- $this->_store_licenses();
11245
-
11246
- // Clear subscription reference.
11247
- $this->_sync_site_subscription( null );
11248
- }
11249
-
11250
- // Update site info.
11251
- $this->_site = new FS_Site( $site );
11252
- $this->_enrich_site_plan();
11253
-
11254
- $trial_cancelled = ( $prev_trial_ends != $site->trial_ends );
11255
- } else {
11256
- // handle different error cases.
11257
-
11258
- }
11259
-
11260
- if ( $trial_cancelled ) {
11261
- // Remove previous sticky messages about upgrade or trial (if exist).
11262
- $this->_admin_notices->remove_sticky( array(
11263
- 'trial_started',
11264
- 'trial_promotion',
11265
- 'plan_upgraded',
11266
- ) );
11267
-
11268
- // Store site updates.
11269
- $this->_store_site();
11270
-
11271
- if ( ! $this->is_addon() ||
11272
- ! $this->deactivate_premium_only_addon_without_license( true )
11273
- ) {
11274
- $this->_admin_notices->add(
11275
- sprintf( $this->get_text( 'trial-cancel-message' ), $this->_storage->trial_plan->title )
11276
- );
11277
- }
11278
-
11279
- // Clear trial plan information.
11280
- unset( $this->_storage->trial_plan );
11281
- } else {
11282
- $this->_admin_notices->add(
11283
- $this->get_text( 'trial-cancel-failure-message' ),
11284
- $this->get_text( 'oops' ) . '...',
11285
- 'error'
11286
- );
11287
- }
11288
- }
11289
-
11290
- /**
11291
- * @author Vova Feldman (@svovaf)
11292
- * @since 1.0.6
11293
- *
11294
- * @param bool|number $plugin_id
11295
- *
11296
- * @return bool
11297
- */
11298
- private function _is_addon_id( $plugin_id ) {
11299
- return is_numeric( $plugin_id ) && ( $this->get_id() != $plugin_id );
11300
- }
11301
-
11302
- /**
11303
- * Check if user eligible to download premium version updates.
11304
- *
11305
- * @author Vova Feldman (@svovaf)
11306
- * @since 1.0.6
11307
- *
11308
- * @return bool
11309
- */
11310
- private function _can_download_premium() {
11311
- return $this->has_active_valid_license() ||
11312
- ( $this->is_trial() && ! $this->get_trial_plan()->is_free() );
11313
- }
11314
-
11315
- /**
11316
- *
11317
- * @author Vova Feldman (@svovaf)
11318
- * @since 1.0.6
11319
- *
11320
- * @param bool|number $addon_id
11321
- * @param string $type "json" or "zip"
11322
- *
11323
- * @return string
11324
- */
11325
- private function _get_latest_version_endpoint( $addon_id = false, $type = 'json' ) {
11326
-
11327
- $is_addon = $this->_is_addon_id( $addon_id );
11328
-
11329
- $is_premium = null;
11330
- if ( ! $is_addon ) {
11331
- $is_premium = $this->_can_download_premium();
11332
- } else if ( $this->is_addon_activated( $addon_id ) ) {
11333
- $is_premium = self::get_instance_by_id( $addon_id )->_can_download_premium();
11334
- }
11335
-
11336
- // If add-on, then append add-on ID.
11337
- $endpoint = ( $is_addon ? "/addons/$addon_id" : '' ) .
11338
- '/updates/latest.' . $type;
11339
-
11340
- // If add-on and not yet activated, try to fetch based on server licensing.
11341
- if ( is_bool( $is_premium ) ) {
11342
- $endpoint = add_query_arg( 'is_premium', json_encode( $is_premium ), $endpoint );
11343
- }
11344
-
11345
- if ( $this->has_secret_key() ) {
11346
- $endpoint = add_query_arg( 'type', 'all', $endpoint );
11347
- }
11348
-
11349
- return $endpoint;
11350
- }
11351
-
11352
- /**
11353
- * @author Vova Feldman (@svovaf)
11354
- * @since 1.0.4
11355
- *
11356
- * @param bool|number $addon_id
11357
- * @param bool $flush Since 1.1.7.3
11358
- * @param int $expiration Since 1.2.2.7
11359
- *
11360
- * @return object|false Plugin latest tag info.
11361
- */
11362
- function _fetch_latest_version( $addon_id = false, $flush = true, $expiration = WP_FS__TIME_24_HOURS_IN_SEC ) {
11363
- $this->_logger->entrance();
11364
-
11365
- /**
11366
- * @since 1.1.7.3 Check for plugin updates from Freemius only if opted-in.
11367
- * @since 1.1.7.4 Also check updates for add-ons.
11368
- */
11369
- if ( ! $this->is_registered() &&
11370
- ! $this->_is_addon_id( $addon_id )
11371
- ) {
11372
- return false;
11373
- }
11374
-
11375
- $tag = $this->get_api_site_or_plugin_scope()->get(
11376
- $this->_get_latest_version_endpoint( $addon_id, 'json' ),
11377
- $flush,
11378
- $expiration
11379
- );
11380
-
11381
- $latest_version = ( is_object( $tag ) && isset( $tag->version ) ) ? $tag->version : 'couldn\'t get';
11382
-
11383
- $this->_logger->departure( 'Latest version ' . $latest_version );
11384
-
11385
- return ( is_object( $tag ) && isset( $tag->version ) ) ? $tag : false;
11386
- }
11387
-
11388
- #----------------------------------------------------------------------------------
11389
- #region Download Plugin
11390
- #----------------------------------------------------------------------------------
11391
-
11392
- /**
11393
- * Download latest plugin version, based on plan.
11394
- *
11395
- * Not like _download_latest(), this will redirect the page
11396
- * to secure download url to prevent dual download (from FS to WP server,
11397
- * and then from WP server to the client / browser).
11398
- *
11399
- * @author Vova Feldman (@svovaf)
11400
- * @since 1.0.9
11401
- *
11402
- * @param bool|number $plugin_id
11403
- *
11404
- * @uses FS_Api
11405
- * @uses wp_redirect()
11406
- */
11407
- private function download_latest_directly( $plugin_id = false ) {
11408
- $this->_logger->entrance();
11409
-
11410
- wp_redirect( $this->get_latest_download_api_url( $plugin_id ) );
11411
- }
11412
-
11413
- /**
11414
- * Get latest plugin FS API download URL.
11415
- *
11416
- * @author Vova Feldman (@svovaf)
11417
- * @since 1.0.9
11418
- *
11419
- * @param bool|number $plugin_id
11420
- *
11421
- * @return string
11422
- */
11423
- private function get_latest_download_api_url( $plugin_id = false ) {
11424
- $this->_logger->entrance();
11425
-
11426
- return $this->get_api_site_scope()->get_signed_url(
11427
- $this->_get_latest_version_endpoint( $plugin_id, 'zip' )
11428
- );
11429
- }
11430
-
11431
- /**
11432
- * Get payment invoice URL.
11433
- *
11434
- * @author Vova Feldman (@svovaf)
11435
- * @since 1.2.0
11436
- *
11437
- * @param bool|number $payment_id
11438
- *
11439
- * @return string
11440
- */
11441
- function _get_invoice_api_url( $payment_id = false ) {
11442
- $this->_logger->entrance();
11443
-
11444
- return $this->get_api_user_scope()->get_signed_url(
11445
- "/payments/{$payment_id}/invoice.pdf"
11446
- );
11447
- }
11448
-
11449
- /**
11450
- * Get latest plugin download link.
11451
- *
11452
- * @author Vova Feldman (@svovaf)
11453
- * @since 1.0.9
11454
- *
11455
- * @param string $label
11456
- * @param bool|number $plugin_id
11457
- *
11458
- * @return string
11459
- */
11460
- private function get_latest_download_link( $label, $plugin_id = false ) {
11461
- return sprintf(
11462
- '<a target="_blank" href="%s">%s</a>',
11463
- $this->_get_latest_download_local_url( $plugin_id ),
11464
- $label
11465
- );
11466
- }
11467
-
11468
- /**
11469
- * Get latest plugin download local URL.
11470
- *
11471
- * @author Vova Feldman (@svovaf)
11472
- * @since 1.0.9
11473
- *
11474
- * @param bool|number $plugin_id
11475
- *
11476
- * @return string
11477
- */
11478
- function _get_latest_download_local_url( $plugin_id = false ) {
11479
- // Add timestamp to protect from caching.
11480
- $params = array( 'ts' => WP_FS__SCRIPT_START_TIME );
11481
-
11482
- if ( ! empty( $plugin_id ) ) {
11483
- $params['plugin_id'] = $plugin_id;
11484
- }
11485
-
11486
- return $this->get_account_url( 'download_latest', $params );
11487
- }
11488
-
11489
- #endregion Download Plugin ------------------------------------------------------------------
11490
-
11491
- /**
11492
- * @author Vova Feldman (@svovaf)
11493
- * @since 1.0.4
11494
- *
11495
- * @uses FS_Api
11496
- *
11497
- * @param bool $background Hints the method if it's a background updates check. If false, it means that
11498
- * was initiated by the admin.
11499
- * @param bool|number $plugin_id
11500
- * @param bool $flush Since 1.1.7.3
11501
- * @param int $expiration Since 1.2.2.7
11502
- */
11503
- private function check_updates(
11504
- $background = false,
11505
- $plugin_id = false,
11506
- $flush = true,
11507
- $expiration = WP_FS__TIME_24_HOURS_IN_SEC
11508
- ) {
11509
- $this->_logger->entrance();
11510
-
11511
- // Check if there's a newer version for download.
11512
- $new_version = $this->_fetch_newer_version( $plugin_id, $flush, $expiration );
11513
-
11514
- $update = null;
11515
- if ( is_object( $new_version ) ) {
11516
- $update = new FS_Plugin_Tag( $new_version );
11517
-
11518
- if ( ! $background ) {
11519
- $this->_admin_notices->add(
11520
- sprintf(
11521
- $this->get_text( 'version-x-released' ) . ' ' . $this->get_text( 'please-download-x' ),
11522
- $update->version,
11523
- sprintf(
11524
- '<a href="%s" target="_blank">%s</a>',
11525
- $this->get_account_url( 'download_latest' ),
11526
- sprintf( $this->get_text( 'latest-x-version' ), $this->_site->plan->title )
11527
- )
11528
- ),
11529
- $this->get_text( 'new' ) . '!'
11530
- );
11531
- }
11532
- } else if ( false === $new_version && ! $background ) {
11533
- $this->_admin_notices->add(
11534
- $this->get_text( 'you-have-latest' ),
11535
- $this->get_text( 'you-are-good' )
11536
- );
11537
- }
11538
-
11539
- $this->_store_update( $update, true, $plugin_id );
11540
- }
11541
-
11542
- /**
11543
- * @author Vova Feldman (@svovaf)
11544
- * @since 1.0.4
11545
- *
11546
- * @param bool $flush Since 1.1.7.3 add 24 hour cache by default.
11547
- *
11548
- * @return FS_Plugin[]
11549
- *
11550
- * @uses FS_Api
11551
- */
11552
- private function sync_addons( $flush = false ) {
11553
- $this->_logger->entrance();
11554
-
11555
- $api = $this->get_api_site_or_plugin_scope();
11556
-
11557
- /**
11558
- * @since 1.2.1
11559
- *
11560
- * If there's a cached version of the add-ons and not asking
11561
- * for a flush, just use the currently stored add-ons.
11562
- */
11563
- if ( ! $flush && $api->is_cached( '/addons.json?enriched=true' ) ) {
11564
- $addons = self::get_all_addons();
11565
-
11566
- return $addons[ $this->_plugin->id ];
11567
- }
11568
-
11569
- $result = $api->get( '/addons.json?enriched=true', $flush );
11570
-
11571
- $addons = array();
11572
- if ( $this->is_api_result_object( $result, 'plugins' ) &&
11573
- is_array( $result->plugins )
11574
- ) {
11575
- for ( $i = 0, $len = count( $result->plugins ); $i < $len; $i ++ ) {
11576
- $addons[ $i ] = new FS_Plugin( $result->plugins[ $i ] );
11577
- }
11578
-
11579
- $this->_store_addons( $addons, true );
11580
- }
11581
-
11582
- return $addons;
11583
- }
11584
-
11585
- /**
11586
- * Handle user email update.
11587
- *
11588
- * @author Vova Feldman (@svovaf)
11589
- * @since 1.0.3
11590
- * @uses FS_Api
11591
- *
11592
- * @param string $new_email
11593
- *
11594
- * @return object
11595
- */
11596
- private function update_email( $new_email ) {
11597
- $this->_logger->entrance();
11598
-
11599
-
11600
- $api = $this->get_api_user_scope();
11601
- $user = $api->call( "?plugin_id={$this->_plugin->id}&fields=id,email,is_verified", 'put', array(
11602
- 'email' => $new_email,
11603
- 'after_email_confirm_url' => $this->_get_admin_page_url(
11604
- 'account',
11605
- array( 'fs_action' => 'sync_user' )
11606
- ),
11607
- ) );
11608
-
11609
- if ( ! isset( $user->error ) ) {
11610
- $this->_user->email = $user->email;
11611
- $this->_user->is_verified = $user->is_verified;
11612
- $this->_store_user();
11613
- } else {
11614
- // handle different error cases.
11615
-
11616
- }
11617
-
11618
- return $user;
11619
- }
11620
-
11621
- #----------------------------------------------------------------------------------
11622
- #region API Error Handling
11623
- #----------------------------------------------------------------------------------
11624
-
11625
- /**
11626
- * @author Vova Feldman (@svovaf)
11627
- * @since 1.1.1
11628
- *
11629
- * @param mixed $result
11630
- *
11631
- * @return bool Is API result contains an error.
11632
- */
11633
- private function is_api_error( $result ) {
11634
- return FS_Api::is_api_error( $result );
11635
- }
11636
-
11637
- /**
11638
- * Checks if given API result is a non-empty and not an error object.
11639
- *
11640
- * @author Vova Feldman (@svovaf)
11641
- * @since 1.2.1.5
11642
- *
11643
- * @param mixed $result
11644
- * @param string|null $required_property Optional property we want to verify that is set.
11645
- *
11646
- * @return bool
11647
- */
11648
- function is_api_result_object( $result, $required_property = null ) {
11649
- return FS_Api::is_api_result_object( $result, $required_property );
11650
- }
11651
-
11652
- /**
11653
- * Checks if given API result is a non-empty entity object with non-empty ID.
11654
- *
11655
- * @author Vova Feldman (@svovaf)
11656
- * @since 1.2.1.5
11657
- *
11658
- * @param mixed $result
11659
- *
11660
- * @return bool
11661
- */
11662
- private function is_api_result_entity( $result ) {
11663
- return FS_Api::is_api_result_entity( $result );
11664
- }
11665
-
11666
- #endregion
11667
-
11668
- /**
11669
- * Make sure a given argument is an array of a specific type.
11670
- *
11671
- * @author Vova Feldman (@svovaf)
11672
- * @since 1.2.1.5
11673
- *
11674
- * @param mixed $array
11675
- * @param string $class
11676
- *
11677
- * @return bool
11678
- */
11679
- private function is_array_instanceof( $array, $class ) {
11680
- return ( is_array( $array ) && ( empty( $array ) || $array[0] instanceof $class ) );
11681
- }
11682
-
11683
- /**
11684
- * Start install ownership change.
11685
- *
11686
- * @author Vova Feldman (@svovaf)
11687
- * @since 1.1.1
11688
- * @uses FS_Api
11689
- *
11690
- * @param string $candidate_email
11691
- *
11692
- * @return bool Is ownership change successfully initiated.
11693
- */
11694
- private function init_change_owner( $candidate_email ) {
11695
- $this->_logger->entrance();
11696
-
11697
- $api = $this->get_api_site_scope();
11698
- $result = $api->call( "/users/{$this->_user->id}.json", 'put', array(
11699
- 'email' => $candidate_email,
11700
- 'after_confirm_url' => $this->_get_admin_page_url(
11701
- 'account',
11702
- array( 'fs_action' => 'change_owner' )
11703
- ),
11704
- ) );
11705
-
11706
- return ! $this->is_api_error( $result );
11707
- }
11708
-
11709
- /**
11710
- * Handle install ownership change.
11711
- *
11712
- * @author Vova Feldman (@svovaf)
11713
- * @since 1.1.1
11714
- * @uses FS_Api
11715
- *
11716
- * @return bool Was ownership change successfully complete.
11717
- */
11718
- private function complete_change_owner() {
11719
- $this->_logger->entrance();
11720
-
11721
- $site_result = $this->get_api_site_scope( true )->get();
11722
- $site = new FS_Site( $site_result );
11723
- $this->_site = $site;
11724
-
11725
- $user = new FS_User();
11726
- $user->id = fs_request_get( 'user_id' );
11727
-
11728
- // Validate install's user and given user.
11729
- if ( $user->id != $this->_site->user_id ) {
11730
- return false;
11731
- }
11732
-
11733
- $user->public_key = fs_request_get( 'user_public_key' );
11734
- $user->secret_key = fs_request_get( 'user_secret_key' );
11735
-
11736
- // Fetch new user information.
11737
- $this->_user = $user;
11738
- $user_result = $this->get_api_user_scope( true )->get();
11739
- $user = new FS_User( $user_result );
11740
- $this->_user = $user;
11741
-
11742
- $this->_set_account( $user, $site );
11743
-
11744
- return true;
11745
- }
11746
-
11747
- /**
11748
- * Handle user name update.
11749
- *
11750
- * @author Vova Feldman (@svovaf)
11751
- * @since 1.0.9
11752
- * @uses FS_Api
11753
- *
11754
- * @return object
11755
- */
11756
- private function update_user_name() {
11757
- $this->_logger->entrance();
11758
- $name = fs_request_get( 'fs_user_name_' . $this->get_unique_affix(), '' );
11759
-
11760
- $api = $this->get_api_user_scope();
11761
- $user = $api->call( "?plugin_id={$this->_plugin->id}&fields=id,first,last", 'put', array(
11762
- 'name' => $name,
11763
- ) );
11764
-
11765
- if ( ! isset( $user->error ) ) {
11766
- $this->_user->first = $user->first;
11767
- $this->_user->last = $user->last;
11768
- $this->_store_user();
11769
- } else {
11770
- // handle different error cases.
11771
-
11772
- }
11773
-
11774
- return $user;
11775
- }
11776
-
11777
- /**
11778
- * Verify user email.
11779
- *
11780
- * @author Vova Feldman (@svovaf)
11781
- * @since 1.0.3
11782
- * @uses FS_Api
11783
- */
11784
- private function verify_email() {
11785
- $this->_handle_account_user_sync();
11786
-
11787
- if ( $this->_user->is_verified() ) {
11788
- return;
11789
- }
11790
-
11791
- $api = $this->get_api_site_scope();
11792
- $result = $api->call( "/users/{$this->_user->id}/verify.json", 'put', array(
11793
- 'after_email_confirm_url' => $this->_get_admin_page_url(
11794
- 'account',
11795
- array( 'fs_action' => 'sync_user' )
11796
- )
11797
- ) );
11798
-
11799
- if ( ! isset( $result->error ) ) {
11800
- $this->_admin_notices->add( sprintf(
11801
- $this->get_text( 'verification-email-sent-message' ),
11802
- sprintf( '<a href="mailto:%1s">%2s</a>', esc_url( $this->_user->email ), $this->_user->email )
11803
- ) );
11804
- } else {
11805
- // handle different error cases.
11806
-
11807
- }
11808
- }
11809
-
11810
- /**
11811
- * @author Vova Feldman (@svovaf)
11812
- * @since 1.1.2
11813
- *
11814
- * @param array $params
11815
- *
11816
- * @return string
11817
- */
11818
- function get_activation_url( $params = array() ) {
11819
- if ( $this->is_addon() && $this->has_free_plan() ) {
11820
- /**
11821
- * @author Vova Feldman (@svovaf)
11822
- * @since 1.2.1.7 Add-on's activation is the parent's module activation.
11823
- */
11824
- return $this->get_parent_instance()->get_activation_url( $params );
11825
- }
11826
-
11827
- return $this->apply_filters( 'connect_url', $this->_get_admin_page_url( '', $params ) );
11828
- }
11829
-
11830
- /**
11831
- * @author Vova Feldman (@svovaf)
11832
- * @since 1.2.1.5
11833
- *
11834
- * @param array $params
11835
- *
11836
- * @return string
11837
- */
11838
- function get_reconnect_url( $params = array() ) {
11839
- $params['fs_action'] = 'reset_anonymous_mode';
11840
- $params['fs_unique_affix'] = $this->get_unique_affix();
11841
-
11842
- return $this->get_activation_url( $params );
11843
- }
11844
-
11845
- /**
11846
- * Get the URL of the page that should be loaded after the user connect
11847
- * or skip in the opt-in screen.
11848
- *
11849
- * @author Vova Feldman (@svovaf)
11850
- * @since 1.1.3
11851
- *
11852
- * @param string $filter Filter name.
11853
- * @param array $params Since 1.2.2.7
11854
- *
11855
- * @return string
11856
- */
11857
- function get_after_activation_url( $filter, $params = array() ) {
11858
- if ( $this->is_free_wp_org_theme() &&
11859
- fs_request_has( 'pending_activation' )
11860
- ) {
11861
- $first_time_path = '';
11862
- } else {
11863
- $first_time_path = $this->_menu->get_first_time_path();
11864
- }
11865
-
11866
- return add_query_arg( $params, $this->apply_filters(
11867
- $filter,
11868
- empty( $first_time_path ) ?
11869
- $this->_get_admin_page_url() :
11870
- $first_time_path
11871
- ) );
11872
- }
11873
-
11874
- /**
11875
- * Handle account page updates / edits / actions.
11876
- *
11877
- * @author Vova Feldman (@svovaf)
11878
- * @since 1.0.2
11879
- *
11880
- */
11881
- private function _handle_account_edits() {
11882
- if ( ! $this->is_user_admin() ) {
11883
- return;
11884
- }
11885
-
11886
- $plugin_id = fs_request_get( 'plugin_id', $this->get_id() );
11887
- $action = fs_get_action();
11888
-
11889
- switch ( $action ) {
11890
- case 'delete_account':
11891
- check_admin_referer( $action );
11892
-
11893
- if ( $plugin_id == $this->get_id() ) {
11894
- $this->delete_account_event();
11895
-
11896
- // Clear user and site.
11897
- $this->_site = null;
11898
- $this->_user = null;
11899
-
11900
- fs_redirect( $this->get_activation_url() );
11901
- } else {
11902
- if ( $this->is_addon_activated( $plugin_id ) ) {
11903
- $fs_addon = self::get_instance_by_id( $plugin_id );
11904
- $fs_addon->delete_account_event();
11905
-
11906
- fs_redirect( $this->_get_admin_page_url( 'account' ) );
11907
- }
11908
- }
11909
-
11910
- return;
11911
-
11912
- case 'downgrade_account':
11913
- check_admin_referer( $action );
11914
-
11915
- if ( $plugin_id == $this->get_id() ) {
11916
- $this->_downgrade_site();
11917
- } else if ( $this->is_addon_activated( $plugin_id ) ) {
11918
- $fs_addon = self::get_instance_by_id( $plugin_id );
11919
- $fs_addon->_downgrade_site();
11920
- }
11921
-
11922
- return;
11923
-
11924
- case 'activate_license':
11925
- check_admin_referer( $action );
11926
-
11927
- if ( $plugin_id == $this->get_id() ) {
11928
- $this->_activate_license();
11929
- } else {
11930
- if ( $this->is_addon_activated( $plugin_id ) ) {
11931
- $fs_addon = self::get_instance_by_id( $plugin_id );
11932
- $fs_addon->_activate_license();
11933
- }
11934
- }
11935
-
11936
- return;
11937
-
11938
- case 'deactivate_license':
11939
- check_admin_referer( $action );
11940
-
11941
- if ( $plugin_id == $this->get_id() ) {
11942
- $this->_deactivate_license();
11943
- } else {
11944
- if ( $this->is_addon_activated( $plugin_id ) ) {
11945
- $fs_addon = self::get_instance_by_id( $plugin_id );
11946
- $fs_addon->_deactivate_license();
11947
- }
11948
- }
11949
-
11950
- return;
11951
-
11952
- case 'check_updates':
11953
- check_admin_referer( $action );
11954
- $this->check_updates();
11955
-
11956
- return;
11957
-
11958
- case 'change_owner':
11959
- $state = fs_request_get( 'state', 'init' );
11960
- switch ( $state ) {
11961
- case 'init':
11962
- $candidate_email = fs_request_get( 'candidate_email', '' );
11963
-
11964
- if ( $this->init_change_owner( $candidate_email ) ) {
11965
- $this->_admin_notices->add( sprintf( $this->get_text( 'change-owner-request-sent-x' ), '<b>' . $this->_user->email . '</b>' ) );
11966
- }
11967
- break;
11968
- case 'owner_confirmed':
11969
- $candidate_email = fs_request_get( 'candidate_email', '' );
11970
-
11971
- $this->_admin_notices->add( sprintf( $this->get_text( 'change-owner-request_owner-confirmed' ), '<b>' . $candidate_email . '</b>' ) );
11972
- break;
11973
- case 'candidate_confirmed':
11974
- if ( $this->complete_change_owner() ) {
11975
- $this->_admin_notices->add_sticky(
11976
- sprintf( $this->get_text( 'change-owner-request_candidate-confirmed' ), '<b>' . $this->_user->email . '</b>' ),
11977
- 'ownership_changed',
11978
- $this->get_text( 'congrats' ) . '!'
11979
- );
11980
- } else {
11981
- // @todo Handle failed ownership change message.
11982
- }
11983
- break;
11984
- }
11985
-
11986
- return;
11987
-
11988
- case 'update_email':
11989
- check_admin_referer( 'update_email' );
11990
-
11991
- $new_email = fs_request_get( 'fs_email_' . $this->get_unique_affix(), '' );
11992
- $result = $this->update_email( $new_email );
11993
-
11994
- if ( isset( $result->error ) ) {
11995
- switch ( $result->error->code ) {
11996
- case 'user_exist':
11997
- $this->_admin_notices->add(
11998
- $this->get_text( 'user-exist-message' ) . ' ' .
11999
- sprintf( $this->get_text( 'user-exist-message_ownership' ), $this->_module_type, '<b>' . $new_email . '</b>' ) .
12000
- sprintf(
12001
- '<a style="margin-left: 10px;" href="%s"><button class="button button-primary">%s &nbsp;&#10140;</button></a>',
12002
- $this->get_account_url( 'change_owner', array(
12003
- 'state' => 'init',
12004
- 'candidate_email' => $new_email
12005
- ) ),
12006
- $this->get_text( 'change-ownership' )
12007
- ),
12008
- $this->get_text( 'oops' ) . '...',
12009
- 'error'
12010
- );
12011
- break;
12012
- }
12013
- } else {
12014
- $this->_admin_notices->add( $this->get_text( 'email-updated-message' ) );
12015
- }
12016
-
12017
- return;
12018
-
12019
- case 'update_user_name':
12020
- check_admin_referer( 'update_user_name' );
12021
-
12022
- $result = $this->update_user_name();
12023
-
12024
- if ( isset( $result->error ) ) {
12025
- $this->_admin_notices->add(
12026
- $this->get_text( 'name-update-failed-message' ),
12027
- $this->get_text( 'oops' ) . '...',
12028
- 'error'
12029
- );
12030
- } else {
12031
- $this->_admin_notices->add( $this->get_text( 'name-updated-message' ) );
12032
- }
12033
-
12034
- return;
12035
-
12036
- #region Actions that might be called from external links (e.g. email)
12037
-
12038
- case 'cancel_trial':
12039
- if ( $plugin_id == $this->get_id() ) {
12040
- $this->_cancel_trial();
12041
- } else {
12042
- if ( $this->is_addon_activated( $plugin_id ) ) {
12043
- $fs_addon = self::get_instance_by_id( $plugin_id );
12044
- $fs_addon->_cancel_trial();
12045
- }
12046
- }
12047
-
12048
- return;
12049
-
12050
- case 'verify_email':
12051
- $this->verify_email();
12052
-
12053
- return;
12054
-
12055
- case 'sync_user':
12056
- $this->_handle_account_user_sync();
12057
-
12058
- return;
12059
-
12060
- case $this->get_unique_affix() . '_sync_license':
12061
- $this->_sync_license();
12062
-
12063
- return;
12064
-
12065
- case 'download_latest':
12066
- $this->download_latest_directly( $plugin_id );
12067
-
12068
- return;
12069
-
12070
- #endregion
12071
- }
12072
-
12073
- if ( WP_FS__IS_POST_REQUEST ) {
12074
- $properties = array( 'site_secret_key', 'site_id', 'site_public_key' );
12075
- foreach ( $properties as $p ) {
12076
- if ( 'update_' . $p === $action ) {
12077
- check_admin_referer( $action );
12078
-
12079
- $this->_logger->log( $action );
12080
-
12081
- $site_property = substr( $p, strlen( 'site_' ) );
12082
- $site_property_value = fs_request_get( 'fs_' . $p . '_' . $this->get_unique_affix(), '' );
12083
- $this->get_site()->{$site_property} = $site_property_value;
12084
-
12085
- // Store account after modification.
12086
- $this->_store_site();
12087
-
12088
- $this->do_action( 'account_property_edit', 'site', $site_property, $site_property_value );
12089
-
12090
- $this->_admin_notices->add( sprintf(
12091
- $this->get_text( 'x-updated' ),
12092
- '<b>' . str_replace( '_', ' ', $p ) . '</b>' ) );
12093
-
12094
- return;
12095
- }
12096
- }
12097
- }
12098
- }
12099
-
12100
- /**
12101
- * Account page resources load.
12102
- *
12103
- * @author Vova Feldman (@svovaf)
12104
- * @since 1.0.6
12105
- */
12106
- function _account_page_load() {
12107
- $this->_logger->entrance();
12108
-
12109
- $this->_logger->info( var_export( $_REQUEST, true ) );
12110
-
12111
- fs_enqueue_local_style( 'fs_account', '/admin/account.css' );
12112
-
12113
- if ( $this->has_addons() ) {
12114
- wp_enqueue_script( 'plugin-install' );
12115
- add_thickbox();
12116
-
12117
- function fs_addons_body_class( $classes ) {
12118
- $classes .= ' plugins-php';
12119
-
12120
- return $classes;
12121
- }
12122
-
12123
- add_filter( 'admin_body_class', 'fs_addons_body_class' );
12124
- }
12125
-
12126
- if ( $this->has_paid_plan() &&
12127
- ! $this->has_any_license() &&
12128
- ! $this->is_sync_executed() &&
12129
- $this->is_tracking_allowed()
12130
- ) {
12131
- /**
12132
- * If no licenses found and no sync job was executed during the last 24 hours,
12133
- * just execute the sync job right away (blocking execution).
12134
- *
12135
- * @since 1.1.7.3
12136
- */
12137
- $this->run_manual_sync();
12138
- }
12139
-
12140
- $this->_handle_account_edits();
12141
-
12142
- $this->do_action( 'account_page_load_before_departure' );
12143
- }
12144
-
12145
- /**
12146
- * Render account page.
12147
- *
12148
- * @author Vova Feldman (@svovaf)
12149
- * @since 1.0.0
12150
- */
12151
- function _account_page_render() {
12152
- $this->_logger->entrance();
12153
-
12154
- $template = 'account.php';
12155
- if ( 'billing' === fs_request_get( 'tab' ) ) {
12156
- $template = 'billing.php';
12157
- }
12158
-
12159
- $vars = array( 'id' => $this->_module_id );
12160
-
12161
- /**
12162
- * Added filter to the template to allow developers wrapping the template
12163
- * in custom HTML (e.g. within a wizard/tabs).
12164
- *
12165
- * @author Vova Feldman (@svovaf)
12166
- * @since 1.2.1.6
12167
- */
12168
- echo $this->apply_filters( "templates/{$template}", fs_get_template( $template, $vars ) );
12169
- }
12170
-
12171
- /**
12172
- * Render account connect page.
12173
- *
12174
- * @author Vova Feldman (@svovaf)
12175
- * @since 1.0.7
12176
- */
12177
- function _connect_page_render() {
12178
- $this->_logger->entrance();
12179
-
12180
- $vars = array( 'id' => $this->_module_id );
12181
-
12182
- /**
12183
- * Added filter to the template to allow developers wrapping the template
12184
- * in custom HTML (e.g. within a wizard/tabs).
12185
- *
12186
- * @author Vova Feldman (@svovaf)
12187
- * @since 1.2.1.6
12188
- */
12189
- echo $this->apply_filters( 'templates/connect.php', fs_get_template( 'connect.php', $vars ) );
12190
- }
12191
-
12192
- /**
12193
- * Load required resources before add-ons page render.
12194
- *
12195
- * @author Vova Feldman (@svovaf)
12196
- * @since 1.0.6
12197
- */
12198
- function _addons_page_load() {
12199
- $this->_logger->entrance();
12200
-
12201
- fs_enqueue_local_style( 'fs_addons', '/admin/add-ons.css' );
12202
-
12203
- wp_enqueue_script( 'plugin-install' );
12204
- add_thickbox();
12205
-
12206
- function fs_addons_body_class( $classes ) {
12207
- $classes .= ' plugins-php';
12208
-
12209
- return $classes;
12210
- }
12211
-
12212
- add_filter( 'admin_body_class', 'fs_addons_body_class' );
12213
-
12214
- if ( ! $this->is_registered() && $this->is_org_repo_compliant() ) {
12215
- $this->_admin_notices->add(
12216
- sprintf( $this->get_text( 'addons-info-external-message' ), '<b>' . $this->get_plugin_name() . '</b>' ),
12217
- $this->get_text( 'heads-up' ),
12218
- 'update-nag'
12219
- );
12220
- }
12221
- }
12222
-
12223
- /**
12224
- * Render add-ons page.
12225
- *
12226
- * @author Vova Feldman (@svovaf)
12227
- * @since 1.0.6
12228
- */
12229
- function _addons_page_render() {
12230
- $this->_logger->entrance();
12231
-
12232
- $vars = array( 'id' => $this->_module_id );
12233
-
12234
- /**
12235
- * Added filter to the template to allow developers wrapping the template
12236
- * in custom HTML (e.g. within a wizard/tabs).
12237
- *
12238
- * @author Vova Feldman (@svovaf)
12239
- * @since 1.2.1.6
12240
- */
12241
- echo $this->apply_filters( 'templates/add-ons.php', fs_get_template( 'add-ons.php', $vars ) );
12242
- }
12243
-
12244
- /* Pricing & Upgrade
12245
- ------------------------------------------------------------------------------------------------------------------*/
12246
- /**
12247
- * Render pricing page.
12248
- *
12249
- * @author Vova Feldman (@svovaf)
12250
- * @since 1.0.0
12251
- */
12252
- function _pricing_page_render() {
12253
- $this->_logger->entrance();
12254
-
12255
- $vars = array( 'id' => $this->_module_id );
12256
-
12257
- if ( 'true' === fs_request_get( 'checkout', false ) ) {
12258
- fs_require_once_template( 'checkout.php', $vars );
12259
- } else {
12260
- fs_require_once_template( 'pricing.php', $vars );
12261
- }
12262
- }
12263
-
12264
- #----------------------------------------------------------------------------------
12265
- #region Contact Us
12266
- #----------------------------------------------------------------------------------
12267
-
12268
- /**
12269
- * Render contact-us page.
12270
- *
12271
- * @author Vova Feldman (@svovaf)
12272
- * @since 1.0.3
12273
- */
12274
- function _contact_page_render() {
12275
- $this->_logger->entrance();
12276
-
12277
- $vars = array( 'id' => $this->_module_id );
12278
- fs_require_once_template( 'contact.php', $vars );
12279
- }
12280
-
12281
- #endregion ------------------------------------------------------------------------
12282
-
12283
- /**
12284
- * Hide all admin notices to prevent distractions.
12285
- *
12286
- * @author Vova Feldman (@svovaf)
12287
- * @since 1.0.3
12288
- *
12289
- * @uses remove_all_actions()
12290
- */
12291
- private static function _hide_admin_notices() {
12292
- remove_all_actions( 'admin_notices' );
12293
- remove_all_actions( 'network_admin_notices' );
12294
- remove_all_actions( 'all_admin_notices' );
12295
- remove_all_actions( 'user_admin_notices' );
12296
- }
12297
-
12298
- static function _clean_admin_content_section_hook() {
12299
- self::_hide_admin_notices();
12300
-
12301
- // Hide footer.
12302
- echo '<style>#wpfooter { display: none !important; }</style>';
12303
- }
12304
-
12305
- /**
12306
- * Attach to admin_head hook to hide all admin notices.
12307
- *
12308
- * @author Vova Feldman (@svovaf)
12309
- * @since 1.0.3
12310
- */
12311
- static function _clean_admin_content_section() {
12312
- add_action( 'admin_head', 'Freemius::_clean_admin_content_section_hook' );
12313
- }
12314
-
12315
- /* CSS & JavaScript
12316
- ------------------------------------------------------------------------------------------------------------------*/
12317
- /* function _enqueue_script($handle, $src) {
12318
- $url = plugins_url( substr( WP_FS__DIR_JS, strlen( $this->_plugin_dir_path ) ) . '/assets/js/' . $src );
12319
-
12320
- $this->_logger->entrance( 'script = ' . $url );
12321
-
12322
- wp_enqueue_script( $handle, $url );
12323
- }*/
12324
-
12325
- /* SDK
12326
- ------------------------------------------------------------------------------------------------------------------*/
12327
- private $_user_api;
12328
-
12329
- /**
12330
- *
12331
- * @author Vova Feldman (@svovaf)
12332
- * @since 1.0.2
12333
- *
12334
- * @param bool $flush
12335
- *
12336
- * @return FS_Api
12337
- */
12338
- function get_api_user_scope( $flush = false ) {
12339
- if ( ! isset( $this->_user_api ) || $flush ) {
12340
- $this->_user_api = FS_Api::instance(
12341
- $this->_module_id,
12342
- 'user',
12343
- $this->_user->id,
12344
- $this->_user->public_key,
12345
- ! $this->is_live(),
12346
- $this->_user->secret_key
12347
- );
12348
- }
12349
-
12350
- return $this->_user_api;
12351
- }
12352
-
12353
- private $_site_api;
12354
-
12355
- /**
12356
- *
12357
- * @author Vova Feldman (@svovaf)
12358
- * @since 1.0.2
12359
- *
12360
- * @param bool $flush
12361
- *
12362
- * @return FS_Api
12363
- */
12364
- function get_api_site_scope( $flush = false ) {
12365
- if ( ! isset( $this->_site_api ) || $flush ) {
12366
- $this->_site_api = FS_Api::instance(
12367
- $this->_module_id,
12368
- 'install',
12369
- $this->_site->id,
12370
- $this->_site->public_key,
12371
- ! $this->is_live(),
12372
- $this->_site->secret_key
12373
- );
12374
- }
12375
-
12376
- return $this->_site_api;
12377
- }
12378
-
12379
- private $_plugin_api;
12380
-
12381
- /**
12382
- * Get plugin public API scope.
12383
- *
12384
- * @author Vova Feldman (@svovaf)
12385
- * @since 1.0.7
12386
- *
12387
- * @return FS_Api
12388
- */
12389
- function get_api_plugin_scope() {
12390
- if ( ! isset( $this->_plugin_api ) ) {
12391
- $this->_plugin_api = FS_Api::instance(
12392
- $this->_module_id,
12393
- 'plugin',
12394
- $this->_plugin->id,
12395
- $this->_plugin->public_key,
12396
- ! $this->is_live()
12397
- );
12398
- }
12399
-
12400
- return $this->_plugin_api;
12401
- }
12402
-
12403
- /**
12404
- * Get site API scope object (fallback to public plugin scope when not registered).
12405
- *
12406
- * @author Vova Feldman (@svovaf)
12407
- * @since 1.0.7
12408
- *
12409
- * @return FS_Api
12410
- */
12411
- function get_api_site_or_plugin_scope() {
12412
- return $this->is_registered() ?
12413
- $this->get_api_site_scope() :
12414
- $this->get_api_plugin_scope();
12415
- }
12416
-
12417
- /**
12418
- * Show trial promotional notice (if any trial exist).
12419
- *
12420
- * @author Vova Feldman (@svovaf)
12421
- * @since 1.0.9
12422
- *
12423
- * @param $plans
12424
- */
12425
- function _check_for_trial_plans( $plans ) {
12426
- $this->_storage->has_trial_plan = FS_Plan_Manager::instance()->has_trial_plan( $plans );
12427
- }
12428
-
12429
- /**
12430
- * During trial promotion the "upgrade" submenu item turns to
12431
- * "start trial" to encourage the trial. Since we want to keep
12432
- * the same menu item handler and there's no robust way to
12433
- * add new arguments to the menu item link's querystring,
12434
- * use JavaScript to find the menu item and update the href of
12435
- * the link.
12436
- *
12437
- * @author Vova Feldman (@svovaf)
12438
- * @since 1.2.1.5
12439
- */
12440
- function _fix_start_trial_menu_item_url() {
12441
- $template_args = array( 'id' => $this->_module_id );
12442
- fs_require_template( 'add-trial-to-pricing.php', $template_args );
12443
- }
12444
-
12445
- /**
12446
- * Check if module is currently in a trial promotion mode.
12447
- *
12448
- * @author Vova Feldman (@svovaf)
12449
- * @since 1.2.2.7
12450
- *
12451
- * @return bool
12452
- */
12453
- function is_in_trial_promotion() {
12454
- return $this->_admin_notices->has_sticky( 'trial_promotion' );
12455
- }
12456
-
12457
- /**
12458
- * Show trial promotional notice (if any trial exist).
12459
- *
12460
- * @author Vova Feldman (@svovaf)
12461
- * @since 1.0.9
12462
- *
12463
- * @return bool If trial notice added.
12464
- */
12465
- function _add_trial_notice() {
12466
- if ( ! $this->is_user_admin() ) {
12467
- return false;
12468
- }
12469
-
12470
- if ( ! $this->is_user_in_admin() ) {
12471
- return false;
12472
- }
12473
-
12474
- // Check if trial message is already shown.
12475
- if ( $this->is_in_trial_promotion() ) {
12476
- add_action( 'admin_footer', array( &$this, '_fix_start_trial_menu_item_url' ) );
12477
-
12478
- $this->_menu->add_counter_to_menu_item( 1, 'fs-trial' );
12479
-
12480
- return false;
12481
- }
12482
-
12483
- if ( $this->is_premium() && ! WP_FS__DEV_MODE ) {
12484
- // Don't show trial if running the premium code, unless running in DEV mode.
12485
- return false;
12486
- }
12487
-
12488
- if ( ! $this->has_trial_plan() ) {
12489
- // No plans with trial.
12490
- return false;
12491
- }
12492
-
12493
- if ( ! $this->apply_filters( 'show_trial', true ) ) {
12494
- // Developer explicitly asked not to show the trial promo.
12495
- return false;
12496
- }
12497
-
12498
- if ( $this->is_registered() ) {
12499
- // Check if trial already utilized.
12500
- if ( $this->_site->is_trial_utilized() ) {
12501
- return false;
12502
- }
12503
-
12504
- if ( $this->is_paying_or_trial() ) {
12505
- // Don't show trial if paying or already in trial.
12506
- return false;
12507
- }
12508
- }
12509
-
12510
- if ( $this->is_activation_mode() || $this->is_pending_activation() ) {
12511
- // If not yet opted-in/skipped, or pending activation, don't show trial.
12512
- return false;
12513
- }
12514
-
12515
- $last_time_trial_promotion_shown = $this->_storage->get( 'trial_promotion_shown', false );
12516
- $was_promotion_shown_before = ( false !== $last_time_trial_promotion_shown );
12517
-
12518
- // Show promotion if never shown before and 24 hours after initial activation with FS.
12519
- if ( ! $was_promotion_shown_before &&
12520
- $this->_storage->install_timestamp > ( time() - WP_FS__TIME_24_HOURS_IN_SEC )
12521
- ) {
12522
- return false;
12523
- }
12524
-
12525
- // OR if promotion was shown before, try showing it every 30 days.
12526
- if ( $was_promotion_shown_before &&
12527
- 30 * WP_FS__TIME_24_HOURS_IN_SEC > time() - $last_time_trial_promotion_shown
12528
- ) {
12529
- return false;
12530
- }
12531
-
12532
- $trial_period = $this->_trial_days;
12533
- $require_payment = $this->_is_trial_require_payment;
12534
- $trial_url = $this->get_trial_url();
12535
- $plans_string = strtolower( $this->get_text( 'awesome' ) );
12536
-
12537
- if ( $this->is_registered() ) {
12538
- // If opted-in, override trial with up to date data from API.
12539
- $trial_plans = FS_Plan_Manager::instance()->get_trial_plans( $this->_plans );
12540
- $trial_plans_count = count( $trial_plans );
12541
-
12542
- if ( 0 === $trial_plans_count ) {
12543
- // If there's no plans with a trial just exit.
12544
- return false;
12545
- }
12546
-
12547
- /**
12548
- * @var FS_Plugin_Plan $paid_plan
12549
- */
12550
- $paid_plan = $trial_plans[0];
12551
- $require_payment = $paid_plan->is_require_subscription;
12552
- $trial_period = $paid_plan->trial_period;
12553
-
12554
- $total_paid_plans = count( $this->_plans ) - ( FS_Plan_Manager::instance()->has_free_plan( $this->_plans ) ? 1 : 0 );
12555
-
12556
- if ( $total_paid_plans !== $trial_plans_count ) {
12557
- // Not all paid plans have a trial - generate a string of those that have it.
12558
- for ( $i = 0; $i < $trial_plans_count; $i ++ ) {
12559
- $plans_string .= sprintf(
12560
- ' <a href="%s">%s</a>',
12561
- $trial_url,
12562
- $trial_plans[ $i ]->title
12563
- );
12564
-
12565
- if ( $i < $trial_plans_count - 2 ) {
12566
- $plans_string .= ', ';
12567
- } else if ( $i == $trial_plans_count - 2 ) {
12568
- $plans_string .= ' and ';
12569
- }
12570
- }
12571
- }
12572
- }
12573
-
12574
- $message = sprintf(
12575
- $this->get_text( 'hey' ) . '! ' . $this->get_text( 'trial-x-promotion-message' ),
12576
- sprintf( '<b>%s</b>', $this->get_plugin_name() ),
12577
- $plans_string,
12578
- $trial_period
12579
- );
12580
-
12581
- // "No Credit-Card Required" or "No Commitment for N Days".
12582
- $cc_string = $require_payment ?
12583
- sprintf( $this->get_text( 'no-commitment-for-x-days' ), $trial_period ) :
12584
- $this->get_text( 'no-cc-required' ) . '!';
12585
-
12586
-
12587
- // Start trial button.
12588
- $button = ' ' . sprintf(
12589
- '<a style="margin-left: 10px; vertical-align: super;" href="%s"><button class="button button-primary">%s &nbsp;&#10140;</button></a>',
12590
- $trial_url,
12591
- $this->get_text( 'start-free-trial' )
12592
- );
12593
-
12594
- $this->_admin_notices->add_sticky(
12595
- $this->apply_filters( 'trial_promotion_message', "{$message} {$cc_string} {$button}" ),
12596
- 'trial_promotion',
12597
- '',
12598
- 'promotion'
12599
- );
12600
-
12601
- $this->_storage->trial_promotion_shown = WP_FS__SCRIPT_START_TIME;
12602
-
12603
- return true;
12604
- }
12605
-
12606
- /**
12607
- * @author Vova Feldman (@svovaf)
12608
- * @since 1.2.1.5
12609
- */
12610
- function _enqueue_common_css() {
12611
- if ( $this->has_paid_plan() && ! $this->is_paying() ) {
12612
- // Add basic CSS for admin-notices and menu-item colors.
12613
- fs_enqueue_local_style( 'fs_common', '/admin/common.css' );
12614
- }
12615
- }
12616
-
12617
- /**
12618
- * @author Leo Fajardo (leorw)
12619
- * @since 1.2.2
12620
- */
12621
- function _show_theme_activation_optin_dialog() {
12622
- fs_enqueue_local_style( 'fs_connect', '/admin/connect.css' );
12623
-
12624
- add_action( 'admin_footer-themes.php', array( &$this, '_add_fs_theme_activation_dialog' ) );
12625
- }
12626
-
12627
- /**
12628
- * @author Leo Fajardo (leorw)
12629
- * @since 1.2.2
12630
- */
12631
- function _add_fs_theme_activation_dialog() {
12632
- $vars = array( 'id' => $this->_module_id );
12633
- fs_require_once_template( 'connect.php', $vars );
12634
- }
12635
-
12636
- /* Action Links
12637
- ------------------------------------------------------------------------------------------------------------------*/
12638
- private $_action_links_hooked = false;
12639
- private $_action_links = array();
12640
-
12641
- /**
12642
- * Hook to plugin action links filter.
12643
- *
12644
- * @author Vova Feldman (@svovaf)
12645
- * @since 1.0.0
12646
- */
12647
- private function hook_plugin_action_links() {
12648
- $this->_logger->entrance();
12649
-
12650
- $this->_action_links_hooked = true;
12651
-
12652
- $this->_logger->log( 'Adding action links hooks.' );
12653
-
12654
- // Add action link to settings page.
12655
- add_filter( 'plugin_action_links_' . $this->_plugin_basename, array(
12656
- &$this,
12657
- '_modify_plugin_action_links_hook'
12658
- ), WP_FS__DEFAULT_PRIORITY, 2 );
12659
- add_filter( 'network_admin_plugin_action_links_' . $this->_plugin_basename, array(
12660
- &$this,
12661
- '_modify_plugin_action_links_hook'
12662
- ), WP_FS__DEFAULT_PRIORITY, 2 );
12663
- }
12664
-
12665
- /**
12666
- * Add plugin action link.
12667
- *
12668
- * @author Vova Feldman (@svovaf)
12669
- * @since 1.0.0
12670
- *
12671
- * @param $label
12672
- * @param $url
12673
- * @param bool $external
12674
- * @param int $priority
12675
- * @param bool $key
12676
- */
12677
- function add_plugin_action_link( $label, $url, $external = false, $priority = WP_FS__DEFAULT_PRIORITY, $key = false ) {
12678
- $this->_logger->entrance();
12679
-
12680
- if ( ! isset( $this->_action_links[ $priority ] ) ) {
12681
- $this->_action_links[ $priority ] = array();
12682
- }
12683
-
12684
- if ( false === $key ) {
12685
- $key = preg_replace( "/[^A-Za-z0-9 ]/", '', strtolower( $label ) );
12686
- }
12687
-
12688
- $this->_action_links[ $priority ][] = array(
12689
- 'label' => $label,
12690
- 'href' => $url,
12691
- 'key' => $key,
12692
- 'external' => $external
12693
- );
12694
- }
12695
-
12696
- /**
12697
- * Adds Upgrade and Add-Ons links to the main Plugins page link actions collection.
12698
- *
12699
- * @author Vova Feldman (@svovaf)
12700
- * @since 1.0.0
12701
- */
12702
- function _add_upgrade_action_link() {
12703
- $this->_logger->entrance();
12704
-
12705
- if ( $this->is_registered() ) {
12706
- if ( ! $this->is_paying() && $this->has_paid_plan() ) {
12707
- $this->add_plugin_action_link(
12708
- $this->get_text( 'upgrade' ),
12709
- $this->get_upgrade_url(),
12710
- false,
12711
- 7,
12712
- 'upgrade'
12713
- );
12714
- }
12715
-
12716
- if ( $this->has_addons() ) {
12717
- $this->add_plugin_action_link(
12718
- $this->get_text( 'add-ons' ),
12719
- $this->_get_admin_page_url( 'addons' ),
12720
- false,
12721
- 9,
12722
- 'addons'
12723
- );
12724
- }
12725
- }
12726
- }
12727
-
12728
- /**
12729
- * Adds "Activate License" or "Change License" link to the main Plugins page link actions collection.
12730
- *
12731
- * @author Leo Fajardo (@leorw)
12732
- * @since 1.1.9
12733
- */
12734
- function _add_license_action_link() {
12735
- $this->_logger->entrance();
12736
-
12737
- if ( $this->is_free_plan() && $this->is_addon() ) {
12738
- return;
12739
- }
12740
-
12741
- if ( ! self::is_ajax() ) {
12742
- // Inject license activation dialog UI and client side code.
12743
- add_action( 'admin_footer', array( &$this, '_add_license_activation_dialog_box' ) );
12744
- }
12745
-
12746
- $link_text = $this->get_text(
12747
- $this->is_free_plan() ?
12748
- 'activate-license' :
12749
- 'change-license'
12750
- );
12751
-
12752
- $this->add_plugin_action_link(
12753
- $link_text,
12754
- '#',
12755
- false,
12756
- 11,
12757
- ( 'activate-license ' . $this->get_unique_affix() )
12758
- );
12759
- }
12760
-
12761
- /**
12762
- * Adds "Opt in" or "Opt out" link to the main "Plugins" page link actions collection.
12763
- *
12764
- * @author Leo Fajardo (@leorw)
12765
- * @since 1.2.1.5
12766
- */
12767
- function _add_tracking_links() {
12768
- if ( ! current_user_can( 'activate_plugins' ) ) {
12769
- return;
12770
- }
12771
-
12772
- $this->_logger->entrance();
12773
-
12774
- if ( fs_request_is_action_secure( $this->get_unique_affix() . '_reconnect' ) ) {
12775
- if ( ! $this->is_registered() && $this->is_anonymous() ) {
12776
- $this->connect_again();
12777
-
12778
- return;
12779
- }
12780
- }
12781
-
12782
- if ( ( $this->is_plugin() && ! self::is_plugins_page() ) ||
12783
- ( $this->is_theme() && ! self::is_themes_page() )
12784
- ) {
12785
- // Only show tracking links on the plugins and themes pages.
12786
- return;
12787
- }
12788
-
12789
- if ( ! $this->is_enable_anonymous() ) {
12790
- // Don't allow to opt-out if anonymous mode is disabled.
12791
- return;
12792
- }
12793
-
12794
- if ( ! $this->is_free_plan() ) {
12795
- // Don't allow to opt-out if running in paid plan.
12796
- return;
12797
- }
12798
-
12799
- if ( $this->add_ajax_action( 'stop_tracking', array( &$this, '_stop_tracking_callback' ) ) ) {
12800
- return;
12801
- }
12802
-
12803
- if ( $this->add_ajax_action( 'allow_tracking', array( &$this, '_allow_tracking_callback' ) ) ) {
12804
- return;
12805
- }
12806
-
12807
- $url = '#';
12808
-
12809
- if ( $this->is_registered() ) {
12810
- if ( $this->is_tracking_allowed() ) {
12811
- $link_text_id = 'opt-out';
12812
- } else {
12813
- $link_text_id = 'opt-in';
12814
- }
12815
-
12816
- add_action( 'admin_footer', array( &$this, '_add_optout_dialog' ) );
12817
- } else {
12818
- $link_text_id = 'opt-in';
12819
-
12820
- $params = ! $this->is_anonymous() ?
12821
- array() :
12822
- array(
12823
- 'nonce' => wp_create_nonce( $this->get_unique_affix() . '_reconnect' ),
12824
- 'fs_action' => ( $this->get_unique_affix() . '_reconnect' ),
12825
- );
12826
-
12827
- $url = $this->get_activation_url( $params );
12828
- }
12829
-
12830
- if ( $this->is_plugin() && self::is_plugins_page() ) {
12831
- $this->add_plugin_action_link(
12832
- $this->get_text( $link_text_id ),
12833
- $url,
12834
- false,
12835
- 13,
12836
- "opt-in-or-opt-out {$this->_slug}"
12837
- );
12838
- }
12839
- }
12840
-
12841
- /**
12842
- * Get the URL of the page that should be loaded right after the plugin activation.
12843
- *
12844
- * @author Vova Feldman (@svovaf)
12845
- * @since 1.1.7.4
12846
- *
12847
- * @return string
12848
- */
12849
- function get_after_plugin_activation_redirect_url() {
12850
- $url = false;
12851
-
12852
- if ( ! $this->is_addon() || ! $this->has_free_plan() ) {
12853
- $first_time_path = $this->_menu->get_first_time_path();
12854
- $url = $this->is_activation_mode() ?
12855
- $this->get_activation_url() :
12856
- ( empty( $first_time_path ) ?
12857
- $this->_get_admin_page_url() :
12858
- $first_time_path );
12859
- } else {
12860
- $plugin_fs = false;
12861
-
12862
- if ( $this->is_parent_plugin_installed() ) {
12863
- $plugin_fs = self::get_parent_instance();
12864
- }
12865
-
12866
- if ( is_object( $plugin_fs ) ) {
12867
- if ( ! $plugin_fs->is_registered() ) {
12868
- // Forward to parent plugin connect when parent not registered.
12869
- $url = $plugin_fs->get_activation_url();
12870
- } else {
12871
- // Forward to account page.
12872
- $url = $plugin_fs->_get_admin_page_url( 'account' );
12873
- }
12874
- }
12875
- }
12876
-
12877
- return $url;
12878
- }
12879
-
12880
- /**
12881
- * Forward page to activation page.
12882
- *
12883
- * @author Vova Feldman (@svovaf)
12884
- * @since 1.0.3
12885
- */
12886
- function _redirect_on_activation_hook() {
12887
- $url = $this->get_after_plugin_activation_redirect_url();
12888
-
12889
- if ( is_string( $url ) ) {
12890
- fs_redirect( $url );
12891
- }
12892
- }
12893
-
12894
- /**
12895
- * Modify plugin's page action links collection.
12896
- *
12897
- * @author Vova Feldman (@svovaf)
12898
- * @since 1.0.0
12899
- *
12900
- * @param array $links
12901
- * @param $file
12902
- *
12903
- * @return array
12904
- */
12905
- function _modify_plugin_action_links_hook( $links, $file ) {
12906
- $this->_logger->entrance();
12907
-
12908
- $passed_deactivate = false;
12909
- $deactivate_link = '';
12910
- $before_deactivate = array();
12911
- $after_deactivate = array();
12912
- foreach ( $links as $key => $link ) {
12913
- if ( 'deactivate' === $key ) {
12914
- $deactivate_link = $link;
12915
- $passed_deactivate = true;
12916
- continue;
12917
- }
12918
-
12919
- if ( ! $passed_deactivate ) {
12920
- $before_deactivate[ $key ] = $link;
12921
- } else {
12922
- $after_deactivate[ $key ] = $link;
12923
- }
12924
- }
12925
-
12926
- ksort( $this->_action_links );
12927
-
12928
- foreach ( $this->_action_links as $new_links ) {
12929
- foreach ( $new_links as $link ) {
12930
- $before_deactivate[ $link['key'] ] = '<a href="' . $link['href'] . '"' . ( $link['external'] ? ' target="_blank"' : '' ) . '>' . $link['label'] . '</a>';
12931
- }
12932
- }
12933
-
12934
- if ( ! empty( $deactivate_link ) ) {
12935
- /**
12936
- * This HTML element is used to identify the correct plugin when attaching an event to its Deactivate link.
12937
- *
12938
- * @since 1.2.1.6 Always show the deactivation feedback form since we added automatic free version deactivation upon premium code activation.
12939
- */
12940
- $deactivate_link .= '<i class="fs-module-id" data-module-id="' . $this->_module_id . '"></i>';
12941
-
12942
- // Append deactivation link.
12943
- $before_deactivate['deactivate'] = $deactivate_link;
12944
- }
12945
-
12946
- return array_merge( $before_deactivate, $after_deactivate );
12947
- }
12948
-
12949
- /**
12950
- * Adds admin message.
12951
- *
12952
- * @author Vova Feldman (@svovaf)
12953
- * @since 1.0.4
12954
- *
12955
- * @param string $message
12956
- * @param string $title
12957
- * @param string $type
12958
- */
12959
- function add_admin_message( $message, $title = '', $type = 'success' ) {
12960
- $this->_admin_notices->add( $message, $title, $type );
12961
- }
12962
-
12963
- /**
12964
- * Adds sticky admin message.
12965
- *
12966
- * @author Vova Feldman (@svovaf)
12967
- * @since 1.1.0
12968
- *
12969
- * @param string $message
12970
- * @param string $id
12971
- * @param string $title
12972
- * @param string $type
12973
- */
12974
- function add_sticky_admin_message( $message, $id, $title = '', $type = 'success' ) {
12975
- $this->_admin_notices->add_sticky( $message, $id, $title, $type );
12976
- }
12977
-
12978
- /**
12979
- * Helper function that returns the final steps for the upgrade completion.
12980
- *
12981
- * If the module is already running the premium code, returns an empty string.
12982
- *
12983
- * @author Vova Feldman (@svovaf)
12984
- * @since 1.2.1
12985
- *
12986
- * @param string $plan_title
12987
- *
12988
- * @return string
12989
- */
12990
- private function get_complete_upgrade_instructions( $plan_title = '' ) {
12991
- if ( ! $this->has_premium_version() || $this->is_premium() ) {
12992
- return '';
12993
- }
12994
-
12995
- if ( empty( $plan_title ) ) {
12996
- $plan_title = $this->_site->plan->title;
12997
- }
12998
-
12999
- // @since 1.2.1.5 The free version is auto deactivated.
13000
- $deactivation_step = version_compare( $this->version, '1.2.1.5', '<' ) ?
13001
- ( '<li>' . $this->get_text( 'deactivate-free-version' ) . '.</li>' ) :
13002
- '';
13003
-
13004
- return sprintf(
13005
- ' %s: <ol><li>%s.</li>%s<li>%s (<a href="%s" target="_blank">%s</a>).</li></ol>',
13006
- $this->get_text( 'follow-steps-to-complete-upgrade' ),
13007
- $this->get_latest_download_link( sprintf(
13008
- $this->get_text( 'download-latest-x-version' ),
13009
- $plan_title
13010
- ) ),
13011
- $deactivation_step,
13012
- $this->get_text( 'upload-and-activate' ),
13013
- '//bit.ly/upload-wp-' . $this->_module_type . 's',
13014
- $this->get_text( 'howto-upload-activate' )
13015
- );
13016
- }
13017
-
13018
- /**
13019
- * @author Vova Feldman (@svovaf)
13020
- * @since 1.2.1.7
13021
- *
13022
- * @param string $key
13023
- *
13024
- * @return string
13025
- */
13026
- function get_text( $key ) {
13027
- return fs_text( $key, $this->_slug );
13028
- }
13029
-
13030
- #----------------------------------------------------------------------------------
13031
- #region Versioning
13032
- #----------------------------------------------------------------------------------
13033
-
13034
- /**
13035
- * Check if Freemius in SDK upgrade mode.
13036
- *
13037
- * @author Vova Feldman (@svovaf)
13038
- * @since 1.0.9
13039
- *
13040
- * @return bool
13041
- */
13042
- function is_sdk_upgrade_mode() {
13043
- return isset( $this->_storage->sdk_upgrade_mode ) ?
13044
- $this->_storage->sdk_upgrade_mode :
13045
- false;
13046
- }
13047
-
13048
- /**
13049
- * Turn SDK upgrade mode off.
13050
- *
13051
- * @author Vova Feldman (@svovaf)
13052
- * @since 1.0.9
13053
- */
13054
- function set_sdk_upgrade_complete() {
13055
- $this->_storage->sdk_upgrade_mode = false;
13056
- }
13057
-
13058
- /**
13059
- * Check if plugin upgrade mode.
13060
- *
13061
- * @author Vova Feldman (@svovaf)
13062
- * @since 1.0.9
13063
- *
13064
- * @return bool
13065
- */
13066
- function is_plugin_upgrade_mode() {
13067
- return isset( $this->_storage->plugin_upgrade_mode ) ?
13068
- $this->_storage->plugin_upgrade_mode :
13069
- false;
13070
- }
13071
-
13072
- /**
13073
- * Turn plugin upgrade mode off.
13074
- *
13075
- * @author Vova Feldman (@svovaf)
13076
- * @since 1.0.9
13077
- */
13078
- function set_plugin_upgrade_complete() {
13079
- $this->_storage->plugin_upgrade_mode = false;
13080
- }
13081
-
13082
- #endregion
13083
-
13084
- #----------------------------------------------------------------------------------
13085
- #region Permissions
13086
- #----------------------------------------------------------------------------------
13087
-
13088
- /**
13089
- * Check if specific permission requested.
13090
- *
13091
- * @author Vova Feldman (@svovaf)
13092
- * @since 1.1.6
13093
- *
13094
- * @param string $permission
13095
- *
13096
- * @return bool
13097
- */
13098
- function is_permission_requested( $permission ) {
13099
- return isset( $this->_permissions[ $permission ] ) && ( true === $this->_permissions[ $permission ] );
13100
- }
13101
-
13102
- #endregion
13103
-
13104
- #----------------------------------------------------------------------------------
13105
- #region Auto Activation
13106
- #----------------------------------------------------------------------------------
13107
-
13108
- /**
13109
- * Hints the SDK if running an auto-installation.
13110
- *
13111
- * @var bool
13112
- */
13113
- private $_isAutoInstall = false;
13114
-
13115
- /**
13116
- * After upgrade callback to install and auto activate a plugin.
13117
- * This code will only be executed on explicit request from the user,
13118
- * following the practice Jetpack are using with their theme installations.
13119
- *
13120
- * @link https://make.wordpress.org/plugins/2017/03/16/clarification-of-guideline-8-executable-code-and-installs/
13121
- *
13122
- * @author Vova Feldman (@svovaf)
13123
- * @since 1.2.1.7
13124
- */
13125
- function _install_premium_version_ajax_action() {
13126
- $this->_logger->entrance();
13127
-
13128
- $this->check_ajax_referer( 'install_premium_version' );
13129
-
13130
- if ( ! $this->is_registered() ) {
13131
- // Not registered.
13132
- self::shoot_ajax_failure( array(
13133
- 'message' => $this->get_text( 'auto-install-error-not-opted-in' ),
13134
- 'code' => 'premium_installed',
13135
- ) );
13136
- }
13137
-
13138
- $plugin_id = fs_request_get( 'module_id', $this->get_id() );
13139
-
13140
- if ( ! FS_Plugin::is_valid_id( $plugin_id ) ) {
13141
- // Invalid ID.
13142
- self::shoot_ajax_failure( array(
13143
- 'message' => $this->get_text( 'auto-install-error-invalid-id' ),
13144
- 'code' => 'invalid_module_id',
13145
- ) );
13146
- }
13147
-
13148
- if ( $plugin_id == $this->get_id() ) {
13149
- if ( $this->is_premium() ) {
13150
- // Already using the premium code version.
13151
- self::shoot_ajax_failure( array(
13152
- 'message' => $this->get_text( 'auto-install-error-premium-activated' ),
13153
- 'code' => 'premium_installed',
13154
- ) );
13155
- }
13156
- if ( ! $this->can_use_premium_code() ) {
13157
- // Don't have access to the premium code.
13158
- self::shoot_ajax_failure( array(
13159
- 'message' => $this->get_text( 'auto-install-error-invalid-license' ),
13160
- 'code' => 'invalid_license',
13161
- ) );
13162
- }
13163
- if ( ! $this->has_release_on_freemius() ) {
13164
- // Plugin is a serviceware, no premium code version.
13165
- self::shoot_ajax_failure( array(
13166
- 'message' => $this->get_text( 'auto-install-error-serviceware' ),
13167
- 'code' => 'premium_version_missing',
13168
- ) );
13169
- }
13170
- } else {
13171
- $addon = $this->get_addon( $plugin_id );
13172
-
13173
- if ( ! is_object( $addon ) ) {
13174
- // Invalid add-on ID.
13175
- self::shoot_ajax_failure( array(
13176
- 'message' => $this->get_text( 'auto-install-error-invalid-id' ),
13177
- 'code' => 'invalid_module_id',
13178
- ) );
13179
- }
13180
-
13181
- if ( $this->is_addon_activated( $plugin_id, true ) ) {
13182
- // Premium add-on version is already activated.
13183
- self::shoot_ajax_failure( array(
13184
- 'message' => $this->get_text( 'auto-install-error-premium-addon-activated' ),
13185
- 'code' => 'premium_installed',
13186
- ) );
13187
- }
13188
- }
13189
-
13190
- $this->_isAutoInstall = true;
13191
-
13192
- // Try to install and activate.
13193
- $updater = new FS_Plugin_Updater( $this );
13194
- $result = $updater->install_and_activate_plugin( $plugin_id );
13195
-
13196
- if ( is_array( $result ) && ! empty( $result['message'] ) ) {
13197
- self::shoot_ajax_failure( array(
13198
- 'message' => $result['message'],
13199
- 'code' => $result['code'],
13200
- ) );
13201
- }
13202
-
13203
- self::shoot_ajax_success( $result );
13204
- }
13205
-
13206
- /**
13207
- * Displays module activation dialog box after a successful upgrade
13208
- * where the user explicitly requested to auto download and install
13209
- * the premium version.
13210
- *
13211
- * @author Vova Feldman (@svovaf)
13212
- * @since 1.2.1.7
13213
- */
13214
- function _add_auto_installation_dialog_box() {
13215
- $this->_logger->entrance();
13216
-
13217
- if ( ! $this->is_registered() ) {
13218
- // Not registered.
13219
- return;
13220
- }
13221
-
13222
- $plugin_id = fs_request_get( 'plugin_id', $this->get_id() );
13223
-
13224
- if ( ! FS_Plugin::is_valid_id( $plugin_id ) ) {
13225
- // Invalid module ID.
13226
- return;
13227
- }
13228
-
13229
- if ( $plugin_id == $this->get_id() ) {
13230
- if ( $this->is_premium() ) {
13231
- // Already using the premium code version.
13232
- return;
13233
- }
13234
- if ( ! $this->can_use_premium_code() ) {
13235
- // Don't have access to the premium code.
13236
- return;
13237
- }
13238
- if ( ! $this->has_release_on_freemius() ) {
13239
- // Plugin is a serviceware, no premium code version.
13240
- return;
13241
- }
13242
- } else {
13243
- $addon = $this->get_addon( $plugin_id );
13244
-
13245
- if ( ! is_object( $addon ) ) {
13246
- // Invalid add-on ID.
13247
- return;
13248
- }
13249
-
13250
- if ( $this->is_addon_activated( $plugin_id, true ) ) {
13251
- // Premium add-on version is already activated.
13252
- return;
13253
- }
13254
- }
13255
-
13256
- $vars = array(
13257
- 'id' => $plugin_id,
13258
- 'slug' => $this->_slug,
13259
- );
13260
-
13261
- fs_require_template( 'auto-installation.php', $vars );
13262
- }
13263
-
13264
- #endregion
13265
-
13266
- #--------------------------------------------------------------------------------
13267
- #region Tabs Integration
13268
- #--------------------------------------------------------------------------------
13269
-
13270
- #region Module's Original Tabs
13271
-
13272
- /**
13273
- * Inject a JavaScript logic to capture the theme tabs HTML.
13274
- *
13275
- * @author Vova Feldman (@svovaf)
13276
- * @since 1.2.2.7
13277
- */
13278
- function _tabs_capture() {
13279
- $this->_logger->entrance();
13280
-
13281
- if ( ! $this->is_theme_settings_page() ||
13282
- ! $this->is_matching_url( $this->main_menu_url() )
13283
- ) {
13284
- return;
13285
- }
13286
-
13287
- $params = array(
13288
- 'id' => $this->_module_id,
13289
- );
13290
-
13291
- fs_require_once_template( 'tabs-capture-js.php', $params );
13292
- }
13293
-
13294
- /**
13295
- * Cache theme's tabs HTML for a week. The cache will also be set as expired
13296
- * after version and type (free/premium) changes, in addition to the week period.
13297
- *
13298
- * @author Vova Feldman (@svovaf)
13299
- * @since 1.2.2.7
13300
- */
13301
- function _store_tabs_ajax_action() {
13302
- $this->_logger->entrance();
13303
-
13304
- $this->check_ajax_referer( 'store_tabs' );
13305
-
13306
- // Init filesystem if not yet initiated.
13307
- WP_Filesystem();
13308
-
13309
- // Get POST body HTML data.
13310
- global $wp_filesystem;
13311
- $tabs_html = $wp_filesystem->get_contents( "php://input" );
13312
-
13313
- if ( is_string( $tabs_html ) ) {
13314
- $tabs_html = trim( $tabs_html );
13315
- }
13316
-
13317
- if ( ! is_string( $tabs_html ) || empty( $tabs_html ) ) {
13318
- self::shoot_ajax_failure();
13319
- }
13320
-
13321
- $this->_cache->set( 'tabs', $tabs_html, 7 * WP_FS__TIME_24_HOURS_IN_SEC );
13322
-
13323
- self::shoot_ajax_success();
13324
- }
13325
-
13326
- /**
13327
- * Cache theme's settings page custom styles. The cache will also be set as expired
13328
- * after version and type (free/premium) changes, in addition to the week period.
13329
- *
13330
- * @author Vova Feldman (@svovaf)
13331
- * @since 1.2.2.7
13332
- */
13333
- function _store_tabs_styles() {
13334
- $this->_logger->entrance();
13335
-
13336
- if ( ! $this->is_theme_settings_page() ||
13337
- ! $this->is_matching_url( $this->main_menu_url() )
13338
- ) {
13339
- return;
13340
- }
13341
-
13342
- $wp_styles = wp_styles();
13343
-
13344
- $theme_styles_url = get_template_directory_uri();
13345
-
13346
- $stylesheets = array();
13347
- foreach ( $wp_styles->queue as $handler ) {
13348
- if ( fs_starts_with( $handler, 'fs_' ) ) {
13349
- // Assume that stylesheets that their handler starts with "fs_" belong to the SDK.
13350
- continue;
13351
- }
13352
-
13353
- /**
13354
- * @var _WP_Dependency $stylesheet
13355
- */
13356
- $stylesheet = $wp_styles->registered[ $handler ];
13357
-
13358
- if ( fs_starts_with( $stylesheet->src, $theme_styles_url ) ) {
13359
- $stylesheets[] = $stylesheet->src;
13360
- }
13361
- }
13362
-
13363
- if ( ! empty( $stylesheets ) ) {
13364
- $this->_cache->set( 'tabs_stylesheets', $stylesheets, 7 * WP_FS__TIME_24_HOURS_IN_SEC );
13365
- }
13366
- }
13367
-
13368
- /**
13369
- * Check if module's original settings page has any tabs.
13370
- *
13371
- * @author Vova Feldman (@svovaf)
13372
- * @since 1.2.2.7
13373
- *
13374
- * @return bool
13375
- */
13376
- private function has_tabs() {
13377
- return $this->_cache->has( 'tabs' );
13378
- }
13379
-
13380
- /**
13381
- * Get module's settings page HTML content, starting
13382
- * from the beginning of the <div class="wrap"> element,
13383
- * until the tabs HTML (including).
13384
- *
13385
- * @author Vova Feldman (@svovaf)
13386
- * @since 1.2.2.7
13387
- *
13388
- * @return string
13389
- */
13390
- private function get_tabs_html() {
13391
- $this->_logger->entrance();
13392
-
13393
- return $this->_cache->get( 'tabs' );
13394
- }
13395
-
13396
- /**
13397
- * Check if page should include tabs.
13398
- *
13399
- * @author Vova Feldman (@svovaf)
13400
- * @since 1.2.2.7
13401
- *
13402
- * @return bool
13403
- */
13404
- private function should_page_include_tabs()
13405
- {
13406
- if ( ! $this->has_settings_menu() ) {
13407
- // Don't add tabs if no settings at all.
13408
- return false;
13409
- }
13410
-
13411
- if ( ! $this->is_theme() ) {
13412
- // Only add tabs to themes for now.
13413
- return false;
13414
- }
13415
-
13416
- if ( ! $this->has_paid_plan() && ! $this->has_addons() ) {
13417
- // Only add tabs to monetizing themes.
13418
- return false;
13419
- }
13420
-
13421
- if ( ! $this->is_theme_settings_page() ) {
13422
- // Only add tabs if browsing one of the theme's setting pages.
13423
- return false;
13424
- }
13425
-
13426
- if ( $this->is_admin_page( 'pricing' ) && fs_request_get_bool( 'checkout' ) ) {
13427
- // Don't add tabs on checkout page, we want to reduce distractions
13428
- // as much as possible.
13429
- return false;
13430
- }
13431
-
13432
- return true;
13433
- }
13434
-
13435
- /**
13436
- * Add the tabs HTML before the setting's page content and
13437
- * enqueue any required stylesheets.
13438
- *
13439
- * @author Vova Feldman (@svovaf)
13440
- * @since 1.2.2.7
13441
- *
13442
- * @return bool If tabs were included.
13443
- */
13444
- function _add_tabs_before_content() {
13445
- $this->_logger->entrance();
13446
-
13447
- if ( ! $this->should_page_include_tabs() ) {
13448
- return false;
13449
- }
13450
-
13451
- /**
13452
- * Enqueue the original stylesheets that are included in the
13453
- * theme settings page. That way, if the theme settings has
13454
- * some custom _styled_ content above the tabs UI, this
13455
- * will make sure that the styling is preserved.
13456
- */
13457
- $stylesheets = $this->_cache->get( 'tabs_stylesheets', array() );
13458
- if ( is_array( $stylesheets ) ) {
13459
- for ( $i = 0, $len = count( $stylesheets ); $i < $len; $i ++ ) {
13460
- wp_enqueue_style( "fs_{$this->_module_id}_tabs_{$i}", $stylesheets[ $i ] );
13461
- }
13462
- }
13463
-
13464
- // Cut closing </div> tag.
13465
- echo substr( trim( $this->get_tabs_html() ), 0, - 6 );
13466
-
13467
- return true;
13468
- }
13469
-
13470
- /**
13471
- * Add the tabs closing HTML after the setting's page content.
13472
- *
13473
- * @author Vova Feldman (@svovaf)
13474
- * @since 1.2.2.7
13475
- *
13476
- * @return bool If tabs closing HTML was included.
13477
- */
13478
- function _add_tabs_after_content() {
13479
- $this->_logger->entrance();
13480
-
13481
- if ( ! $this->should_page_include_tabs() ) {
13482
- return false;
13483
- }
13484
-
13485
- echo '</div>';
13486
-
13487
- return true;
13488
- }
13489
-
13490
- #endregion
13491
-
13492
- /**
13493
- * Add in-page JavaScript to inject the Freemius tabs into
13494
- * the module's setting tabs section.
13495
- *
13496
- * @author Vova Feldman (@svovaf)
13497
- * @since 1.2.2.7
13498
- */
13499
- function _add_freemius_tabs() {
13500
- $this->_logger->entrance();
13501
-
13502
- if ( ! $this->should_page_include_tabs() ) {
13503
- return;
13504
- }
13505
-
13506
- $params = array( 'id' => $this->_module_id );
13507
- fs_require_once_template( 'tabs.php', $params );
13508
- }
13509
-
13510
- #endregion
13511
-
13512
- #--------------------------------------------------------------------------------
13513
- #region Customizer Integration for Themes
13514
- #--------------------------------------------------------------------------------
13515
-
13516
- /**
13517
- * @author Vova Feldman (@svovaf)
13518
- * @since 1.2.2.7
13519
- *
13520
- * @param WP_Customize_Manager $customizer
13521
- */
13522
- function _customizer_register($customizer) {
13523
- $this->_logger->entrance();
13524
-
13525
- if ( $this->is_pricing_page_visible() ) {
13526
- require_once WP_FS__DIR_INCLUDES . '/customizer/class-fs-customizer-upsell-control.php';
13527
-
13528
- $customizer->add_section( 'freemius_upsell', array(
13529
- 'title' => '&#9733; ' . $this->get_text( 'view-paid-features' ),
13530
- 'priority' => 1,
13531
- ) );
13532
- $customizer->add_setting( 'freemius_upsell', array(
13533
- 'sanitize_callback' => 'esc_html',
13534
- ) );
13535
-
13536
- $customizer->add_control( new FS_Customizer_Upsell_Control( $customizer, 'freemius_upsell', array(
13537
- 'fs' => $this,
13538
- 'section' => 'freemius_upsell',
13539
- 'priority' => 100,
13540
- ) ) );
13541
- }
13542
-
13543
- if ( $this->is_page_visible( 'contact' ) || $this->is_page_visible( 'support' ) ) {
13544
- require_once WP_FS__DIR_INCLUDES . '/customizer/class-fs-customizer-support-section.php';
13545
-
13546
- // Main Documentation Link In Customizer Root.
13547
- $customizer->add_section( new FS_Customizer_Support_Section( $customizer, 'freemius_support', array(
13548
- 'fs' => $this,
13549
- 'priority' => 1000,
13550
- ) ) );
13551
- }
13552
- }
13553
-
13554
- #endregion
13555
-
13556
- /**
13557
- * If the theme has a paid version, add some custom
13558
- * styling to the theme's premium version (if exists)
13559
- * to highlight that it's the premium version of the
13560
- * same theme, making it easier for identification
13561
- * after the user upgrades and upload it to the site.
13562
- *
13563
- * @author Vova Feldman (@svovaf)
13564
- * @since 1.2.2.7
13565
- */
13566
- function _style_premium_theme() {
13567
- $this->_logger->entrance();
13568
-
13569
- if ( ! self::is_themes_page() ) {
13570
- // Only include in the themes page.
13571
- return;
13572
- }
13573
-
13574
- if ( ! $this->has_paid_plan() ) {
13575
- // Only include if has any paid plans.
13576
- return;
13577
- }
13578
-
13579
- $params = null;
13580
- fs_require_once_template( '/js/jquery.content-change.php', $params );
13581
-
13582
- $params = array(
13583
- 'slug' => $this->_slug,
13584
- 'id' => $this->_module_id,
13585
- );
13586
-
13587
- fs_require_template( '/js/style-premium-theme.php', $params );
13588
- }
13589
-
13590
- #----------------------------------------------------------------------------------
13591
- #region Marketing
13592
- #----------------------------------------------------------------------------------
13593
-
13594
- /**
13595
- * Check if current user purchased any other plugins before.
13596
- *
13597
- * @author Vova Feldman (@svovaf)
13598
- * @since 1.0.9
13599
- *
13600
- * @return bool
13601
- */
13602
- function has_purchased_before() {
13603
- // TODO: Implement has_purchased_before() method.
13604
- throw new Exception( 'not implemented' );
13605
- }
13606
-
13607
- /**
13608
- * Check if current user classified as an agency.
13609
- *
13610
- * @author Vova Feldman (@svovaf)
13611
- * @since 1.0.9
13612
- *
13613
- * @return bool
13614
- */
13615
- function is_agency() {
13616
- // TODO: Implement is_agency() method.
13617
- throw new Exception( 'not implemented' );
13618
- }
13619
-
13620
- /**
13621
- * Check if current user classified as a developer.
13622
- *
13623
- * @author Vova Feldman (@svovaf)
13624
- * @since 1.0.9
13625
- *
13626
- * @return bool
13627
- */
13628
- function is_developer() {
13629
- // TODO: Implement is_developer() method.
13630
- throw new Exception( 'not implemented' );
13631
- }
13632
-
13633
- /**
13634
- * Check if current user classified as a business.
13635
- *
13636
- * @author Vova Feldman (@svovaf)
13637
- * @since 1.0.9
13638
- *
13639
- * @return bool
13640
- */
13641
- function is_business() {
13642
- // TODO: Implement is_business() method.
13643
- throw new Exception( 'not implemented' );
13644
- }
13645
-
13646
- #endregion
13647
- }
 
 
 
 
1
+ <?php
2
+ /**
3
+ * @package Freemius
4
+ * @copyright Copyright (c) 2015, Freemius, Inc.
5
+ * @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3
6
+ * @since 1.0.3
7
+ */
8
+ if ( ! defined( 'ABSPATH' ) ) {
9
+ exit;
10
+ }
11
+
12
+ // "final class"
13
+ class Freemius extends Freemius_Abstract {
14
+ /**
15
+ * SDK Version
16
+ *
17
+ * @var string
18
+ */
19
+ public $version = WP_FS__SDK_VERSION;
20
+
21
+ #region Plugin Info
22
+
23
+ /**
24
+ * @since 1.0.1
25
+ *
26
+ * @var string
27
+ */
28
+ private $_slug;
29
+
30
+ /**
31
+ * @since 1.0.0
32
+ *
33
+ * @var string
34
+ */
35
+ private $_plugin_basename;
36
+ /**
37
+ * @since 1.0.0
38
+ *
39
+ * @var string
40
+ */
41
+ private $_free_plugin_basename;
42
+ /**
43
+ * @since 1.0.0
44
+ *
45
+ * @var string
46
+ */
47
+ private $_plugin_dir_path;
48
+ /**
49
+ * @since 1.0.0
50
+ *
51
+ * @var string
52
+ */
53
+ private $_plugin_dir_name;
54
+ /**
55
+ * @since 1.0.0
56
+ *
57
+ * @var string
58
+ */
59
+ private $_plugin_main_file_path;
60
+ /**
61
+ * @var string[]
62
+ */
63
+ private $_plugin_data;
64
+ /**
65
+ * @since 1.0.9
66
+ *
67
+ * @var string
68
+ */
69
+ private $_plugin_name;
70
+ /**
71
+ * @since 1.2.2
72
+ *
73
+ * @var string
74
+ */
75
+ private $_module_type;
76
+
77
+ #endregion Plugin Info
78
+
79
+ /**
80
+ * @since 1.0.9
81
+ *
82
+ * @var bool If false, don't turn Freemius on.
83
+ */
84
+ private $_is_on;
85
+
86
+ /**
87
+ * @since 1.1.3
88
+ *
89
+ * @var bool If false, don't turn Freemius on.
90
+ */
91
+ private $_is_anonymous;
92
+
93
+ /**
94
+ * @since 1.0.9
95
+ * @var bool If false, issues with connectivity to Freemius API.
96
+ */
97
+ private $_has_api_connection;
98
+
99
+ /**
100
+ * @since 1.0.9
101
+ * @var bool Hints the SDK if plugin can support anonymous mode (if skip connect is visible).
102
+ */
103
+ private $_enable_anonymous;
104
+
105
+ /**
106
+ * @since 1.1.7.5
107
+ * @var bool Hints the SDK if plugin should run in anonymous mode (only adds feedback form).
108
+ */
109
+ private $_anonymous_mode;
110
+
111
+ /**
112
+ * @since 1.1.9
113
+ * @var bool Hints the SDK if plugin have any free plans.
114
+ */
115
+ private $_is_premium_only;
116
+
117
+ /**
118
+ * @since 1.2.1.6
119
+ * @var bool Hints the SDK if plugin have premium code version at all.
120
+ */
121
+ private $_has_premium_version;
122
+
123
+ /**
124
+ * @since 1.2.1.6
125
+ * @var bool Hints the SDK if plugin should ignore pending mode by simulating a skip.
126
+ */
127
+ private $_ignore_pending_mode;
128
+
129
+ /**
130
+ * @since 1.0.8
131
+ * @var bool Hints the SDK if the plugin has any paid plans.
132
+ */
133
+ private $_has_paid_plans;
134
+
135
+ /**
136
+ * @since 1.2.1.5
137
+ * @var int Hints the SDK if the plugin offers a trial period. If negative, no trial, if zero - has a trial but
138
+ * without a specified period, if positive - the number of trial days.
139
+ */
140
+ private $_trial_days = - 1;
141
+
142
+ /**
143
+ * @since 1.2.1.5
144
+ * @var bool Hints the SDK if the trial requires a payment method or not.
145
+ */
146
+ private $_is_trial_require_payment = false;
147
+
148
+ /**
149
+ * @since 1.0.7
150
+ * @var bool Hints the SDK if the plugin is WordPress.org compliant.
151
+ */
152
+ private $_is_org_compliant;
153
+
154
+ /**
155
+ * @since 1.0.7
156
+ * @var bool Hints the SDK if the plugin is has add-ons.
157
+ */
158
+ private $_has_addons;
159
+
160
+ /**
161
+ * @since 1.1.6
162
+ * @var string[]bool.
163
+ */
164
+ private $_permissions;
165
+
166
+ /**
167
+ * @var FS_Key_Value_Storage
168
+ */
169
+ private $_storage;
170
+
171
+ /**
172
+ * @since 1.2.2.7
173
+ * @var FS_Cache_Manager
174
+ */
175
+ private $_cache;
176
+
177
+ /**
178
+ * @since 1.0.0
179
+ *
180
+ * @var FS_Logger
181
+ */
182
+ private $_logger;
183
+ /**
184
+ * @since 1.0.4
185
+ *
186
+ * @var FS_Plugin
187
+ */
188
+ private $_plugin = false;
189
+ /**
190
+ * @since 1.0.4
191
+ *
192
+ * @var FS_Plugin|false
193
+ */
194
+ private $_parent_plugin = false;
195
+ /**
196
+ * @since 1.1.1
197
+ *
198
+ * @var Freemius
199
+ */
200
+ private $_parent = false;
201
+ /**
202
+ * @since 1.0.1
203
+ *
204
+ * @var FS_User
205
+ */
206
+ private $_user = false;
207
+ /**
208
+ * @since 1.0.1
209
+ *
210
+ * @var FS_Site
211
+ */
212
+ private $_site = false;
213
+ /**
214
+ * @since 1.0.1
215
+ *
216
+ * @var FS_Plugin_License
217
+ */
218
+ private $_license;
219
+ /**
220
+ * @since 1.0.2
221
+ *
222
+ * @var FS_Plugin_Plan[]
223
+ */
224
+ private $_plans = false;
225
+ /**
226
+ * @var FS_Plugin_License[]
227
+ * @since 1.0.5
228
+ */
229
+ private $_licenses = false;
230
+
231
+ /**
232
+ * @since 1.0.1
233
+ *
234
+ * @var FS_Admin_Menu_Manager
235
+ */
236
+ private $_menu;
237
+
238
+ /**
239
+ * @var FS_Admin_Notice_Manager
240
+ */
241
+ private $_admin_notices;
242
+
243
+ /**
244
+ * @since 1.1.6
245
+ *
246
+ * @var FS_Admin_Notice_Manager
247
+ */
248
+ private static $_global_admin_notices;
249
+
250
+ /**
251
+ * @var FS_Logger
252
+ * @since 1.0.0
253
+ */
254
+ private static $_static_logger;
255
+
256
+ /**
257
+ * @var FS_Option_Manager
258
+ * @since 1.0.2
259
+ */
260
+ private static $_accounts;
261
+
262
+ /**
263
+ * @since 1.2.2
264
+ *
265
+ * @var number
266
+ */
267
+ private $_module_id;
268
+
269
+ /**
270
+ * @var Freemius[]
271
+ */
272
+ private static $_instances = array();
273
+
274
+ #region Uninstall Reasons IDs
275
+
276
+ const REASON_NO_LONGER_NEEDED = 1;
277
+ const REASON_FOUND_A_BETTER_PLUGIN = 2;
278
+ const REASON_NEEDED_FOR_A_SHORT_PERIOD = 3;
279
+ const REASON_BROKE_MY_SITE = 4;
280
+ const REASON_SUDDENLY_STOPPED_WORKING = 5;
281
+ const REASON_CANT_PAY_ANYMORE = 6;
282
+ const REASON_OTHER = 7;
283
+ const REASON_DIDNT_WORK = 8;
284
+ const REASON_DONT_LIKE_TO_SHARE_MY_INFORMATION = 9;
285
+ const REASON_COULDNT_MAKE_IT_WORK = 10;
286
+ const REASON_GREAT_BUT_NEED_SPECIFIC_FEATURE = 11;
287
+ const REASON_NOT_WORKING = 12;
288
+ const REASON_NOT_WHAT_I_WAS_LOOKING_FOR = 13;
289
+ const REASON_DIDNT_WORK_AS_EXPECTED = 14;
290
+ const REASON_TEMPORARY_DEACTIVATION = 15;
291
+
292
+ #endregion
293
+
294
+ /* Ctor
295
+ ------------------------------------------------------------------------------------------------------------------*/
296
+
297
+ /**
298
+ * Main singleton instance.
299
+ *
300
+ * @author Vova Feldman (@svovaf)
301
+ * @since 1.0.0
302
+ *
303
+ * @param number $module_id
304
+ * @param string|bool $slug
305
+ * @param bool $is_init Since 1.2.1 Is initiation sequence.
306
+ */
307
+ private function __construct( $module_id, $slug = false, $is_init = false ) {
308
+ if ( $is_init && is_numeric( $module_id ) && is_string( $slug ) ) {
309
+ $this->store_id_slug_type_path_map( $module_id, $slug );
310
+ }
311
+
312
+ $this->_module_id = $module_id;
313
+ $this->_slug = $this->get_slug();
314
+ $this->_module_type = $this->get_module_type();
315
+
316
+ $this->_storage = FS_Key_Value_Storage::instance( $this->_module_type . '_data', $this->_slug );
317
+ $this->_cache = FS_Cache_Manager::get_manager( WP_FS___OPTION_PREFIX . "cache_{$module_id}" );
318
+
319
+ $this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_' . $this->get_unique_affix(), WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
320
+
321
+ $this->_plugin_main_file_path = $this->_find_caller_plugin_file( $is_init );
322
+ $this->_plugin_dir_path = plugin_dir_path( $this->_plugin_main_file_path );
323
+ $this->_plugin_basename = $this->get_plugin_basename();
324
+ $this->_free_plugin_basename = str_replace( '-premium/', '/', $this->_plugin_basename );
325
+
326
+ $base_name_split = explode( '/', $this->_plugin_basename );
327
+ $this->_plugin_dir_name = $base_name_split[0];
328
+
329
+ if ( $this->_logger->is_on() ) {
330
+ $this->_logger->info( 'plugin_main_file_path = ' . $this->_plugin_main_file_path );
331
+ $this->_logger->info( 'plugin_dir_path = ' . $this->_plugin_dir_path );
332
+ $this->_logger->info( 'plugin_basename = ' . $this->_plugin_basename );
333
+ $this->_logger->info( 'free_plugin_basename = ' . $this->_free_plugin_basename );
334
+ $this->_logger->info( 'plugin_dir_name = ' . $this->_plugin_dir_name );
335
+ }
336
+
337
+ // Remember link between file to slug.
338
+ $this->store_file_slug_map();
339
+
340
+ // Store plugin's initial install timestamp.
341
+ if ( ! isset( $this->_storage->install_timestamp ) ) {
342
+ $this->_storage->install_timestamp = WP_FS__SCRIPT_START_TIME;
343
+ }
344
+
345
+ if ( ! is_object( $this->_plugin ) ) {
346
+ $this->_plugin = FS_Plugin_Manager::instance( $this->_module_id )->get();
347
+ }
348
+
349
+ $this->_admin_notices = FS_Admin_Notice_Manager::instance(
350
+ $this->_slug . ( $this->is_theme() ? ':theme' : '' ),
351
+ /**
352
+ * Ensure that the admin notice will always have a title by using the stored plugin title if available and
353
+ * retrieving the title via the "get_plugin_name" method if there is no stored plugin title available.
354
+ *
355
+ * @author Leo Fajardo (@leorw)
356
+ * @since 1.2.2
357
+ */
358
+ ( is_object( $this->_plugin ) ? $this->_plugin->title : $this->get_plugin_name() ),
359
+ $this->get_unique_affix()
360
+ );
361
+
362
+ if ( 'true' === fs_request_get( 'fs_clear_api_cache' ) ||
363
+ 'true' === fs_request_is_action( 'restart_freemius' )
364
+ ) {
365
+ FS_Api::clear_cache();
366
+ $this->_cache->clear();
367
+ }
368
+
369
+ $this->_register_hooks();
370
+
371
+ $this->_load_account();
372
+
373
+ $this->_version_updates_handler();
374
+ }
375
+
376
+ /**
377
+ * Checks whether this module has a settings menu.
378
+ *
379
+ * @author Leo Fajardo (@leorw)
380
+ * @since 1.2.2
381
+ *
382
+ * @return bool
383
+ */
384
+ function has_settings_menu() {
385
+ return $this->_menu->has_menu();
386
+ }
387
+
388
+ /**
389
+ * Check if the context module is free wp.org theme.
390
+ *
391
+ * This method is helpful because:
392
+ * 1. wp.org themes are limited to a single submenu item,
393
+ * and sub-submenu items are most likely not allowed (never verified).
394
+ * 2. wp.org themes are not allowed to redirect the user
395
+ * after the theme activation, therefore, the agreed UX
396
+ * is showing the opt-in as a modal dialog box after
397
+ * activation (approved by @otto42, @emiluzelac, @greenshady, @grapplerulrich).
398
+ *
399
+ * @author Vova Feldman (@svovaf)
400
+ * @since 1.2.2.7
401
+ *
402
+ * @return bool
403
+ */
404
+ function is_free_wp_org_theme() {
405
+ return (
406
+ $this->is_theme() &&
407
+ $this->is_org_repo_compliant() &&
408
+ ! $this->is_premium()
409
+ );
410
+ }
411
+
412
+ /**
413
+ * Checks whether this a submenu item is visible.
414
+ *
415
+ * @author Vova Feldman (@svovaf)
416
+ * @since 1.2.2.6
417
+ * @since 1.2.2.7 Even if the menu item was specified to be hidden, when it is the context page, then show the submenu item so the user will have the right context page.
418
+ *
419
+ * @param string $slug
420
+ *
421
+ * @return bool
422
+ */
423
+ function is_submenu_item_visible( $slug ) {
424
+ if ( $this->is_admin_page( $slug ) ) {
425
+ /**
426
+ * It is the current context page, so show the submenu item
427
+ * so the user will have the right context page, even if it
428
+ * was set to hidden.
429
+ */
430
+ return true;
431
+ }
432
+
433
+ if ( ! $this->has_settings_menu() ) {
434
+ // No menu settings at all.
435
+ return false;
436
+ }
437
+
438
+ if ( $this->is_free_wp_org_theme() ) {
439
+ /**
440
+ * wp.org themes are limited to a single submenu item, and
441
+ * sub-submenu items are most likely not allowed (never verified).
442
+ */
443
+ return false;
444
+ }
445
+
446
+ return $this->_menu->is_submenu_item_visible( $slug );
447
+ }
448
+
449
+ /**
450
+ * Check if a Freemius page should be accessible via the UI.
451
+ *
452
+ * @author Vova Feldman (@svovaf)
453
+ * @since 1.2.2.7
454
+ *
455
+ * @param string $slug
456
+ *
457
+ * @return bool
458
+ */
459
+ function is_page_visible( $slug ) {
460
+ if ( $this->is_admin_page( $slug ) ) {
461
+ return true;
462
+ }
463
+
464
+ return $this->_menu->is_submenu_item_visible( $slug, true, true );
465
+ }
466
+
467
+ /**
468
+ * @author Vova Feldman (@svovaf)
469
+ * @since 1.0.9
470
+ */
471
+ private function _version_updates_handler() {
472
+ if ( ! isset( $this->_storage->sdk_version ) || $this->_storage->sdk_version != $this->version ) {
473
+ // Freemius version upgrade mode.
474
+ $this->_storage->sdk_last_version = $this->_storage->sdk_version;
475
+ $this->_storage->sdk_version = $this->version;
476
+
477
+ if ( empty( $this->_storage->sdk_last_version ) ||
478
+ version_compare( $this->_storage->sdk_last_version, $this->version, '<' )
479
+ ) {
480
+ $this->_storage->sdk_upgrade_mode = true;
481
+ $this->_storage->sdk_downgrade_mode = false;
482
+ } else {
483
+ $this->_storage->sdk_downgrade_mode = true;
484
+ $this->_storage->sdk_upgrade_mode = false;
485
+
486
+ }
487
+
488
+ $this->do_action( 'sdk_version_update', $this->_storage->sdk_last_version, $this->version );
489
+ }
490
+
491
+ $plugin_version = $this->get_plugin_version();
492
+ if ( ! isset( $this->_storage->plugin_version ) || $this->_storage->plugin_version != $plugin_version ) {
493
+ // Plugin version upgrade mode.
494
+ $this->_storage->plugin_last_version = $this->_storage->plugin_version;
495
+ $this->_storage->plugin_version = $plugin_version;
496
+
497
+ if ( empty( $this->_storage->plugin_last_version ) ||
498
+ version_compare( $this->_storage->plugin_last_version, $plugin_version, '<' )
499
+ ) {
500
+ $this->_storage->plugin_upgrade_mode = true;
501
+ $this->_storage->plugin_downgrade_mode = false;
502
+ } else {
503
+ $this->_storage->plugin_downgrade_mode = true;
504
+ $this->_storage->plugin_upgrade_mode = false;
505
+ }
506
+
507
+ if ( ! empty( $this->_storage->plugin_last_version ) ) {
508
+ // Different version of the plugin was installed before, therefore it's an update.
509
+ $this->_storage->is_plugin_new_install = false;
510
+ }
511
+
512
+ $this->do_action( 'plugin_version_update', $this->_storage->plugin_last_version, $plugin_version );
513
+ }
514
+ }
515
+
516
+ /**
517
+ * @author Vova Feldman (@svovaf)
518
+ * @since 1.1.5
519
+ *
520
+ * @param string $sdk_prev_version
521
+ * @param string $sdk_version
522
+ */
523
+ function _data_migration( $sdk_prev_version, $sdk_version ) {
524
+ /**
525
+ * @since 1.1.7.3 Fixed unwanted connectivity test cleanup.
526
+ */
527
+ if ( empty( $sdk_prev_version ) ) {
528
+ return;
529
+ }
530
+
531
+ if ( version_compare( $sdk_prev_version, '1.1.5', '<' ) &&
532
+ version_compare( $sdk_version, '1.1.5', '>=' )
533
+ ) {
534
+ // On version 1.1.5 merged connectivity and is_on data.
535
+ if ( isset( $this->_storage->connectivity_test ) ) {
536
+ if ( ! isset( $this->_storage->is_on ) ) {
537
+ unset( $this->_storage->connectivity_test );
538
+ } else {
539
+ $connectivity_data = $this->_storage->connectivity_test;
540
+ $connectivity_data['is_active'] = $this->_storage->is_on['is_active'];
541
+ $connectivity_data['timestamp'] = $this->_storage->is_on['timestamp'];
542
+
543
+ // Override.
544
+ $this->_storage->connectivity_test = $connectivity_data;
545
+
546
+ // Remove previous structure.
547
+ unset( $this->_storage->is_on );
548
+ }
549
+
550
+ }
551
+ }
552
+ }
553
+
554
+ /**
555
+ * @author Vova Feldman (@svovaf)
556
+ * @since 1.2.2.7
557
+ *
558
+ * @param string $plugin_prev_version
559
+ * @param string $plugin_version
560
+ */
561
+ function _after_version_update( $plugin_prev_version, $plugin_version ) {
562
+ if ( $this->is_theme() ) {
563
+ // Expire the cache of the previous tabs since the theme may
564
+ // have setting updates.
565
+ $this->_cache->expire( 'tabs' );
566
+ $this->_cache->expire( 'tabs_stylesheets' );
567
+ }
568
+ }
569
+
570
+ /**
571
+ * This action is connected to the 'plugins_loaded' hook and helps to determine
572
+ * if this is a new plugin installation or a plugin update.
573
+ *
574
+ * There are 3 different use-cases:
575
+ * 1) New plugin installation right with Freemius:
576
+ * 1.1 _activate_plugin_event_hook() will be executed first
577
+ * 1.2 Since $this->_storage->is_plugin_new_install is not set,
578
+ * and $this->_storage->plugin_last_version is not set,
579
+ * $this->_storage->is_plugin_new_install will be set to TRUE.
580
+ * 1.3 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install will
581
+ * be already set to TRUE.
582
+ *
583
+ * 2) Plugin update, didn't have Freemius before, and now have the SDK:
584
+ * 2.1 _activate_plugin_event_hook() will not be executed, because
585
+ * the activation hook do NOT fires on updates since WP 3.1.
586
+ * 2.2 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install will
587
+ * be empty, therefore, it will be set to FALSE.
588
+ *
589
+ * 3) Plugin update, had Freemius in prev version as well:
590
+ * 3.1 _version_updates_handler() will be executed 1st, since FS was installed
591
+ * before, $this->_storage->plugin_last_version will NOT be empty,
592
+ * therefore, $this->_storage->is_plugin_new_install will be set to FALSE.
593
+ * 3.2 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install is
594
+ * already set, therefore, it will not be modified.
595
+ *
596
+ * Use-case #3 is backward compatible, #3.1 will be executed since 1.0.9.
597
+ *
598
+ * NOTE:
599
+ * The only fallback of this mechanism is if an admin updates a plugin based on use-case #2,
600
+ * and then, the next immediate PageView is the plugin's main settings page, it will not
601
+ * show the opt-in right away. The reason it will happen is because Freemius execution
602
+ * will be turned off till the plugin is fully loaded at least once
603
+ * (till $this->_storage->was_plugin_loaded is TRUE).
604
+ *
605
+ * @author Vova Feldman (@svovaf)
606
+ * @since 1.1.9
607
+ *
608
+ */
609
+ function _plugins_loaded() {
610
+ // Update flag that plugin was loaded with Freemius at least once.
611
+ $this->_storage->was_plugin_loaded = true;
612
+
613
+ /**
614
+ * Bug fix - only set to false when it's a plugin, due to the
615
+ * execution sequence of the theme hooks and our methods, if
616
+ * this will be set for themes, Freemius will always assume
617
+ * it's a theme update.
618
+ *
619
+ * @author Vova Feldman (@svovaf)
620
+ * @since 1.2.2.2
621
+ */
622
+ if ( $this->is_plugin() &&
623
+ ! isset( $this->_storage->is_plugin_new_install )
624
+ ) {
625
+ $this->_storage->is_plugin_new_install = false;
626
+ }
627
+ }
628
+
629
+ /**
630
+ * @author Vova Feldman (@svovaf)
631
+ * @since 1.0.9
632
+ */
633
+ private function _register_hooks() {
634
+ $this->_logger->entrance();
635
+
636
+ if ( is_admin() ) {
637
+ if ( $this->is_plugin() ) {
638
+ $plugin_dir = dirname( $this->_plugin_dir_path ) . '/';
639
+
640
+ /**
641
+ * @since 1.2.2
642
+ *
643
+ * Hook to both free and premium version activations to support
644
+ * auto deactivation on the other version activation.
645
+ */
646
+ register_activation_hook(
647
+ $plugin_dir . $this->_free_plugin_basename,
648
+ array( &$this, '_activate_plugin_event_hook' )
649
+ );
650
+
651
+ register_activation_hook(
652
+ $plugin_dir . $this->premium_plugin_basename(),
653
+ array( &$this, '_activate_plugin_event_hook' )
654
+ );
655
+ } else {
656
+ add_action( 'after_switch_theme', array( &$this, '_activate_theme_event_hook' ), 10, 2 );
657
+
658
+ /**
659
+ * Include the required hooks to capture the theme settings' page tabs
660
+ * and cache them.
661
+ *
662
+ * @author Vova Feldman (@svovaf)
663
+ * @since 1.2.2.7
664
+ */
665
+ if ( ! $this->_cache->has_valid( 'tabs' ) ) {
666
+ add_action( 'admin_footer', array( &$this, '_tabs_capture' ) );
667
+ // Add license activation AJAX callback.
668
+ $this->add_ajax_action( 'store_tabs', array( &$this, '_store_tabs_ajax_action' ) );
669
+
670
+ add_action( 'admin_enqueue_scripts', array( &$this, '_store_tabs_styles' ), 9999999 );
671
+ }
672
+
673
+ add_action(
674
+ 'admin_footer',
675
+ array( &$this, '_add_freemius_tabs' ),
676
+ /**
677
+ * The tabs JS code must be executed after the tabs capture logic (_tabs_capture()).
678
+ * That's why the priority is 11 while the tabs capture logic is added
679
+ * with priority 10.
680
+ *
681
+ * @author Vova Feldman (@svovaf)
682
+ */
683
+ 11
684
+ );
685
+
686
+ add_action( 'admin_footer', array( &$this, '_style_premium_theme' ) );
687
+ }
688
+
689
+ /**
690
+ * Part of the mechanism to identify new plugin install vs. plugin update.
691
+ *
692
+ * @author Vova Feldman (@svovaf)
693
+ * @since 1.1.9
694
+ */
695
+ if ( empty( $this->_storage->was_plugin_loaded ) ) {
696
+ if ( $this->is_plugin() &&
697
+ $this->is_activation_mode( false ) &&
698
+ 0 == did_action( 'plugins_loaded' )
699
+ ) {
700
+ add_action( 'plugins_loaded', array( &$this, '_plugins_loaded' ) );
701
+ } else {
702
+ // If was activated before, then it was already loaded before.
703
+ $this->_plugins_loaded();
704
+ }
705
+ }
706
+
707
+ if ( ! self::is_ajax() ) {
708
+ if ( ! $this->is_addon() ) {
709
+ add_action( 'init', array( &$this, '_add_default_submenu_items' ), WP_FS__LOWEST_PRIORITY );
710
+ }
711
+ }
712
+ }
713
+
714
+ if ( $this->is_plugin() ) {
715
+ register_deactivation_hook( $this->_plugin_main_file_path, array( &$this, '_deactivate_plugin_hook' ) );
716
+ }
717
+
718
+ if ( $this->is_theme() && self::is_customizer() ) {
719
+ // Register customizer upsell.
720
+ add_action( 'customize_register', array( &$this, '_customizer_register' ) );
721
+ }
722
+
723
+ add_action( 'init', array( &$this, '_redirect_on_clicked_menu_link' ), WP_FS__LOWEST_PRIORITY );
724
+
725
+ add_action( 'admin_init', array( &$this, '_add_tracking_links' ) );
726
+ add_action( 'admin_init', array( &$this, '_add_license_activation' ) );
727
+ $this->add_ajax_action( 'update_billing', array( &$this, '_update_billing_ajax_action' ) );
728
+ $this->add_ajax_action( 'start_trial', array( &$this, '_start_trial_ajax_action' ) );
729
+
730
+ $this->add_ajax_action( 'install_premium_version', array(
731
+ &$this,
732
+ '_install_premium_version_ajax_action'
733
+ ) );
734
+
735
+ $this->add_action( 'after_plans_sync', array( &$this, '_check_for_trial_plans' ) );
736
+
737
+ $this->add_action( 'sdk_version_update', array( &$this, '_data_migration' ), WP_FS__DEFAULT_PRIORITY, 2 );
738
+ $this->add_action( 'plugin_version_update', array( &$this, '_after_version_update' ), WP_FS__DEFAULT_PRIORITY, 2 );
739
+ $this->add_filter( 'after_code_type_change', array( &$this, '_after_code_type_change' ) );
740
+
741
+ add_action( 'admin_init', array( &$this, '_add_trial_notice' ) );
742
+ add_action( 'admin_init', array( &$this, '_enqueue_common_css' ) );
743
+
744
+ /**
745
+ * Handle request to reset anonymous mode for `get_reconnect_url()`.
746
+ *
747
+ * @author Vova Feldman (@svovaf)
748
+ * @since 1.2.1.5
749
+ */
750
+ if ( fs_request_is_action( 'reset_anonymous_mode' ) &&
751
+ $this->get_unique_affix() === fs_request_get( 'fs_unique_affix' )
752
+ ) {
753
+ add_action( 'admin_init', array( &$this, 'connect_again' ) );
754
+ }
755
+ }
756
+
757
+ /**
758
+ * Keeping the uninstall hook registered for free or premium plugin version may result to a fatal error that
759
+ * could happen when a user tries to uninstall either version while one of them is still active. Uninstalling a
760
+ * plugin will trigger inclusion of the free or premium version and if one of them is active during the
761
+ * uninstallation, a fatal error may occur in case the plugin's class or functions are already defined.
762
+ *
763
+ * @author Leo Fajardo (leorw)
764
+ *
765
+ * @since 1.2.0
766
+ */
767
+ private function unregister_uninstall_hook() {
768
+ $uninstallable_plugins = (array) get_option( 'uninstall_plugins' );
769
+ unset( $uninstallable_plugins[ $this->_free_plugin_basename ] );
770
+ unset( $uninstallable_plugins[ $this->premium_plugin_basename() ] );
771
+
772
+ update_option( 'uninstall_plugins', $uninstallable_plugins );
773
+ }
774
+
775
+ /**
776
+ * @since 1.2.0 Invalidate module's main file cache, otherwise, FS_Plugin_Updater will not fetch updates.
777
+ */
778
+ private function clear_module_main_file_cache() {
779
+ if ( ! isset( $this->_storage->plugin_main_file ) ||
780
+ empty( $this->_storage->plugin_main_file->path )
781
+ ) {
782
+ return;
783
+ }
784
+
785
+ $plugin_main_file = clone $this->_storage->plugin_main_file;
786
+
787
+ // Store cached path (2nd layer cache).
788
+ $plugin_main_file->prev_path = $plugin_main_file->path;
789
+
790
+ // Clear cached path.
791
+ unset( $plugin_main_file->path );
792
+
793
+ $this->_storage->plugin_main_file = $plugin_main_file;
794
+
795
+ /**
796
+ * Clear global cached path.
797
+ *
798
+ * @author Leo Fajardo (@leorw)
799
+ * @since 1.2.2
800
+ */
801
+ $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map' );
802
+ unset( $id_slug_type_path_map[ $this->_module_id ]['path'] );
803
+ self::$_accounts->set_option( 'id_slug_type_path_map', $id_slug_type_path_map, true );
804
+ }
805
+
806
+ /**
807
+ * @author Vova Feldman (@svovaf)
808
+ * @since 1.0.9
809
+ */
810
+ private function _register_account_hooks() {
811
+ if ( ! is_admin() ) {
812
+ return;
813
+ }
814
+
815
+ /**
816
+ * Always show the deactivation feedback form since we added
817
+ * automatic free version deactivation upon premium code activation.
818
+ *
819
+ * @since 1.2.1.6
820
+ */
821
+ $this->add_ajax_action(
822
+ 'submit_uninstall_reason',
823
+ array( &$this, '_submit_uninstall_reason_action' )
824
+ );
825
+
826
+ if ( ( $this->is_plugin() && self::is_plugins_page() ) ||
827
+ ( $this->is_theme() && self::is_themes_page() )
828
+ ) {
829
+ add_action( 'admin_footer', array( &$this, '_add_deactivation_feedback_dialog_box' ) );
830
+ }
831
+ }
832
+
833
+ /**
834
+ * Leverage backtrace to find caller plugin file path.
835
+ *
836
+ * @author Vova Feldman (@svovaf)
837
+ * @since 1.0.6
838
+ *
839
+ * @param bool $is_init Is initiation sequence.
840
+ *
841
+ * @return string
842
+ */
843
+ private function _find_caller_plugin_file( $is_init = false ) {
844
+ // Try to load the cached value of the file path.
845
+ if ( isset( $this->_storage->plugin_main_file ) ) {
846
+ $plugin_main_file = $this->_storage->plugin_main_file;
847
+ if ( isset( $plugin_main_file->path ) && file_exists( $plugin_main_file->path ) ) {
848
+ return $plugin_main_file->path;
849
+ }
850
+ }
851
+
852
+ /**
853
+ * @since 1.2.1
854
+ *
855
+ * `clear_module_main_file_cache()` is clearing the plugin's cached path on
856
+ * deactivation. Therefore, if any plugin/theme was initiating `Freemius`
857
+ * with that plugin's slug, it was overriding the empty plugin path with a wrong path.
858
+ *
859
+ * So, we've added a special mechanism with a 2nd layer of cache that uses `prev_path`
860
+ * when the class instantiator isn't the module.
861
+ */
862
+ if ( ! $is_init ) {
863
+ // Fetch prev path cache.
864
+ if ( isset( $this->_storage->plugin_main_file ) &&
865
+ isset( $this->_storage->plugin_main_file->prev_path )
866
+ ) {
867
+ if ( file_exists( $this->_storage->plugin_main_file->prev_path ) ) {
868
+ return $this->_storage->plugin_main_file->prev_path;
869
+ }
870
+ }
871
+
872
+ wp_die(
873
+ $this->get_text( 'failed-finding-main-path' ) .
874
+ " Module: {$this->_slug}; SDK: " . WP_FS__SDK_VERSION . ";",
875
+ $this->get_text( 'error' ),
876
+ array( 'back_link' => true )
877
+ );
878
+ }
879
+
880
+
881
+ //Fix for migration issues.
882
+ return TINVWL_PATH . 'ti-woocommerce-wishlist.php';
883
+
884
+ /**
885
+ * @since 1.2.1
886
+ *
887
+ * Only the original instantiator that calls dynamic_init can modify the module's path.
888
+ */
889
+ // Find caller module.
890
+ $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
891
+ $this->_storage->plugin_main_file = (object) array(
892
+ 'path' => $id_slug_type_path_map[ $this->_module_id ]['path'],
893
+ );
894
+
895
+ return $id_slug_type_path_map[ $this->_module_id ]['path'];
896
+ }
897
+
898
+ /**
899
+ * @author Leo Fajardo (@leorw)
900
+ *
901
+ * @param number $module_id
902
+ * @param string $slug
903
+ *
904
+ * @since 1.2.2
905
+ */
906
+ private function store_id_slug_type_path_map( $module_id, $slug ) {
907
+ $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
908
+
909
+ $store_option = false;
910
+
911
+ if ( ! isset( $id_slug_type_path_map[ $module_id ] ) ) {
912
+ $id_slug_type_path_map[ $module_id ] = array(
913
+ 'slug' => $slug
914
+ );
915
+
916
+ $store_option = true;
917
+ }
918
+
919
+ if ( ! isset( $id_slug_type_path_map[ $module_id ]['path'] ) ) {
920
+ $caller_main_file_and_type = $this->get_caller_main_file_and_type();
921
+
922
+ $id_slug_type_path_map[ $module_id ]['type'] = $caller_main_file_and_type->module_type;
923
+ $id_slug_type_path_map[ $module_id ]['path'] = $caller_main_file_and_type->path;
924
+
925
+ $store_option = true;
926
+ }
927
+
928
+ if ( $store_option ) {
929
+ self::$_accounts->set_option( 'id_slug_type_path_map', $id_slug_type_path_map, true );
930
+ }
931
+ }
932
+
933
+ /**
934
+ * Identifies the caller type: plugin or theme.
935
+ *
936
+ * @author Leo Fajardo (@leorw)
937
+ * @since 1.2.2
938
+ *
939
+ * @author Vova Feldman (@svovaf)
940
+ * @since 1.2.2.3 Find the earliest module in the call stack that calls to the SDK. This fix is for cases when
941
+ * add-ons are relying on loading the SDK from the parent module, and also allows themes including the
942
+ * SDK an internal file instead of directly from functions.php.
943
+ * @since 1.2.1.7 Knows how to handle cases when an add-on includes the parent module logic.
944
+ */
945
+ private function get_caller_main_file_and_type() {
946
+ self::require_plugin_essentials();
947
+
948
+ $all_plugins = get_plugins();
949
+ $all_plugins_paths = array();
950
+
951
+ // Get active plugin's main files real full names (might be symlinks).
952
+ foreach ( $all_plugins as $relative_path => &$data ) {
953
+ if ( false === strpos( fs_normalize_path( $relative_path ), '/' ) ) {
954
+ /**
955
+ * Ignore plugins that don't have a folder (e.g. Hello Dolly) since they
956
+ * can't really include the SDK.
957
+ *
958
+ * @author Vova Feldman
959
+ * @since 1.2.1.7
960
+ */
961
+ continue;
962
+ }
963
+
964
+ $all_plugins_paths[] = fs_normalize_path( realpath( WP_PLUGIN_DIR . '/' . $relative_path ) );
965
+ }
966
+
967
+ $caller_file_candidate = false;
968
+ $caller_map = array();
969
+ $module_type = WP_FS__MODULE_TYPE_PLUGIN;
970
+ $themes_dir = fs_normalize_path( get_theme_root() );
971
+
972
+ for ( $i = 1, $bt = debug_backtrace(), $len = count( $bt ); $i < $len; $i ++ ) {
973
+ if ( empty( $bt[ $i ]['file'] ) ) {
974
+ continue;
975
+ }
976
+
977
+ if ( $i > 1 && ! empty( $bt[ $i - 1 ]['file'] ) && $bt[ $i ]['file'] === $bt[ $i - 1 ]['file'] ) {
978
+ // If file same as the prev file in the stack, skip it.
979
+ continue;
980
+ }
981
+
982
+ if ( ! empty( $bt[ $i ]['function'] ) && in_array( $bt[ $i ]['function'], array(
983
+ 'do_action',
984
+ 'apply_filter',
985
+ // The string split is stupid, but otherwise, theme check
986
+ // throws info notices.
987
+ 'requir' . 'e_once',
988
+ 'requir' . 'e',
989
+ 'includ' . 'e_once',
990
+ 'includ' . 'e'
991
+ ) )
992
+ ) {
993
+ // Ignore call stack hooks and files inclusion.
994
+ continue;
995
+ }
996
+
997
+ $caller_file_path = fs_normalize_path( $bt[ $i ]['file'] );
998
+
999
+ if ( 'functions.php' === basename( $caller_file_path ) ) {
1000
+ /**
1001
+ * 1. Assumes that theme's starting execution file is functions.php.
1002
+ * 2. This complex logic fixes symlink issues (e.g. with Vargant).
1003
+ *
1004
+ * @author Vova Feldman (@svovaf)
1005
+ * @since 1.2.2.5
1006
+ */
1007
+
1008
+ if ( $caller_file_path == fs_normalize_path( realpath( trailingslashit( $themes_dir ) . basename( dirname( $caller_file_path ) ) . '/' . basename( $caller_file_path ) ) ) ) {
1009
+ $module_type = WP_FS__MODULE_TYPE_THEME;
1010
+ $caller_file_candidate = $caller_file_path;
1011
+ continue;
1012
+ }
1013
+ }
1014
+
1015
+ $caller_file_hash = md5( $caller_file_path );
1016
+
1017
+ if ( ! isset( $caller_map[ $caller_file_hash ] ) ) {
1018
+ foreach ( $all_plugins_paths as $plugin_path ) {
1019
+ if ( false !== strpos( $caller_file_path, fs_normalize_path( dirname( $plugin_path ) . '/' ) ) ) {
1020
+ $caller_map[ $caller_file_hash ] = fs_normalize_path( $plugin_path );
1021
+ break;
1022
+ }
1023
+ }
1024
+ }
1025
+
1026
+ if ( isset( $caller_map[ $caller_file_hash ] ) ) {
1027
+ $module_type = WP_FS__MODULE_TYPE_PLUGIN;
1028
+ $caller_file_candidate = $caller_map[ $caller_file_hash ];
1029
+ }
1030
+ }
1031
+
1032
+ return (object) array(
1033
+ 'module_type' => $module_type,
1034
+ 'path' => $caller_file_candidate
1035
+ );
1036
+ }
1037
+
1038
+ #----------------------------------------------------------------------------------
1039
+ #region Deactivation Feedback Form
1040
+ #----------------------------------------------------------------------------------
1041
+
1042
+ /**
1043
+ * Displays a confirmation and feedback dialog box when the user clicks on the "Deactivate" link on the plugins
1044
+ * page.
1045
+ *
1046
+ * @author Vova Feldman (@svovaf)
1047
+ * @author Leo Fajardo (@leorw)
1048
+ * @since 1.1.2
1049
+ */
1050
+ function _add_deactivation_feedback_dialog_box() {
1051
+ /* Check the type of user:
1052
+ * 1. Long-term (long-term)
1053
+ * 2. Non-registered and non-anonymous short-term (non-registered-and-non-anonymous-short-term).
1054
+ * 3. Short-term (short-term)
1055
+ */
1056
+ $is_long_term_user = true;
1057
+
1058
+ // Check if the site is at least 2 days old.
1059
+ $time_installed = $this->_storage->install_timestamp;
1060
+
1061
+ // Difference in seconds.
1062
+ $date_diff = time() - $time_installed;
1063
+
1064
+ // Convert seconds to days.
1065
+ $date_diff_days = floor( $date_diff / ( 60 * 60 * 24 ) );
1066
+
1067
+ if ( $date_diff_days < 2 ) {
1068
+ $is_long_term_user = false;
1069
+ }
1070
+
1071
+ $is_long_term_user = $this->apply_filters( 'is_long_term_user', $is_long_term_user );
1072
+
1073
+ if ( $is_long_term_user ) {
1074
+ $user_type = 'long-term';
1075
+ } else {
1076
+ if ( ! $this->is_registered() && ! $this->is_anonymous() ) {
1077
+ $user_type = 'non-registered-and-non-anonymous-short-term';
1078
+ } else {
1079
+ $user_type = 'short-term';
1080
+ }
1081
+ }
1082
+
1083
+ $uninstall_reasons = $this->_get_uninstall_reasons( $user_type );
1084
+
1085
+ // Load the HTML template for the deactivation feedback dialog box.
1086
+ $vars = array(
1087
+ 'reasons' => $uninstall_reasons,
1088
+ 'id' => $this->_module_id
1089
+ );
1090
+
1091
+ /**
1092
+ * @todo Deactivation form core functions should be loaded only once! Otherwise, when there are multiple Freemius powered plugins the same code is loaded multiple times. The only thing that should be loaded differently is the various deactivation reasons object based on the state of the plugin.
1093
+ */
1094
+ fs_require_template( 'forms/deactivation/form.php', $vars );
1095
+ }
1096
+
1097
+ /**
1098
+ * @author Leo Fajardo (leorw)
1099
+ * @since 1.1.2
1100
+ *
1101
+ * @param string $user_type
1102
+ *
1103
+ * @return array The uninstall reasons for the specified user type.
1104
+ */
1105
+ function _get_uninstall_reasons( $user_type = 'long-term' ) {
1106
+ $module_type = $this->_module_type;
1107
+
1108
+ $internal_message_template_var = array(
1109
+ 'id' => $this->_module_id
1110
+ );
1111
+
1112
+ if ( $this->is_registered() && false !== $this->get_plan() && $this->get_plan()->has_technical_support() ) {
1113
+ $contact_support_template = fs_get_template( 'forms/deactivation/contact.php', $internal_message_template_var );
1114
+ } else {
1115
+ $contact_support_template = '';
1116
+ }
1117
+
1118
+ $reason_found_better_plugin = array(
1119
+ 'id' => self::REASON_FOUND_A_BETTER_PLUGIN,
1120
+ 'text' => sprintf( $this->get_text( 'reason-found-a-better-plugin' ), $module_type ),
1121
+ 'input_type' => 'textfield',
1122
+ 'input_placeholder' => sprintf( $this->get_text( 'placeholder-plugin-name' ), $module_type ),
1123
+ );
1124
+
1125
+ $reason_temporary_deactivation = array(
1126
+ 'id' => self::REASON_TEMPORARY_DEACTIVATION,
1127
+ 'text' => sprintf(
1128
+ $this->get_text( 'reason-temporary-x' ),
1129
+ strtolower( $this->is_plugin() ?
1130
+ $this->get_text( 'deactivation' ) :
1131
+ $this->get_text( 'theme-switch' )
1132
+ )
1133
+ ),
1134
+ 'input_type' => '',
1135
+ 'input_placeholder' => ''
1136
+ );
1137
+
1138
+ $reason_other = array(
1139
+ 'id' => self::REASON_OTHER,
1140
+ 'text' => $this->get_text( 'reason-other' ),
1141
+ 'input_type' => 'textfield',
1142
+ 'input_placeholder' => ''
1143
+ );
1144
+
1145
+ $long_term_user_reasons = array(
1146
+ array(
1147
+ 'id' => self::REASON_NO_LONGER_NEEDED,
1148
+ 'text' => sprintf( $this->get_text( 'reason-no-longer-needed' ), $module_type ),
1149
+ 'input_type' => '',
1150
+ 'input_placeholder' => ''
1151
+ ),
1152
+ $reason_found_better_plugin,
1153
+ array(
1154
+ 'id' => self::REASON_NEEDED_FOR_A_SHORT_PERIOD,
1155
+ 'text' => sprintf( $this->get_text( 'reason-needed-for-a-short-period' ), $module_type ),
1156
+ 'input_type' => '',
1157
+ 'input_placeholder' => ''
1158
+ ),
1159
+ array(
1160
+ 'id' => self::REASON_BROKE_MY_SITE,
1161
+ 'text' => sprintf( $this->get_text( 'reason-broke-my-site' ), $module_type ),
1162
+ 'input_type' => '',
1163
+ 'input_placeholder' => '',
1164
+ 'internal_message' => $contact_support_template
1165
+ ),
1166
+ array(
1167
+ 'id' => self::REASON_SUDDENLY_STOPPED_WORKING,
1168
+ 'text' => sprintf( $this->get_text( 'reason-suddenly-stopped-working' ), $module_type ),
1169
+ 'input_type' => '',
1170
+ 'input_placeholder' => '',
1171
+ 'internal_message' => $contact_support_template
1172
+ )
1173
+ );
1174
+
1175
+ if ( $this->is_paying() ) {
1176
+ $long_term_user_reasons[] = array(
1177
+ 'id' => self::REASON_CANT_PAY_ANYMORE,
1178
+ 'text' => $this->get_text( 'reason-cant-pay-anymore' ),
1179
+ 'input_type' => 'textfield',
1180
+ 'input_placeholder' => $this->get_text( 'placeholder-comfortable-price' )
1181
+ );
1182
+ }
1183
+
1184
+ $reason_dont_share_info = array(
1185
+ 'id' => self::REASON_DONT_LIKE_TO_SHARE_MY_INFORMATION,
1186
+ 'text' => $this->get_text( 'reason-dont-like-to-share-my-information' ),
1187
+ 'input_type' => '',
1188
+ 'input_placeholder' => ''
1189
+ );
1190
+
1191
+ /**
1192
+ * If the current user has selected the "don't share data" reason in the deactivation feedback modal, inform the
1193
+ * user by showing additional message that he doesn't have to share data and can just choose to skip the opt-in
1194
+ * (the Skip button is included in the message to show). This message will only be shown if anonymous mode is
1195
+ * enabled and the user's account is currently not in pending activation state (similar to the way the Skip
1196
+ * button in the opt-in form is shown/hidden).
1197
+ */
1198
+ if ( $this->is_enable_anonymous() && ! $this->is_pending_activation() ) {
1199
+ $reason_dont_share_info['internal_message'] = fs_get_template( 'forms/deactivation/retry-skip.php', $internal_message_template_var );
1200
+ }
1201
+
1202
+ $uninstall_reasons = array(
1203
+ 'long-term' => $long_term_user_reasons,
1204
+ 'non-registered-and-non-anonymous-short-term' => array(
1205
+ array(
1206
+ 'id' => self::REASON_DIDNT_WORK,
1207
+ 'text' => sprintf( $this->get_text( 'reason-didnt-work' ), $module_type ),
1208
+ 'input_type' => '',
1209
+ 'input_placeholder' => ''
1210
+ ),
1211
+ $reason_dont_share_info,
1212
+ $reason_found_better_plugin
1213
+ ),
1214
+ 'short-term' => array(
1215
+ array(
1216
+ 'id' => self::REASON_COULDNT_MAKE_IT_WORK,
1217
+ 'text' => $this->get_text( 'reason-couldnt-make-it-work' ),
1218
+ 'input_type' => '',
1219
+ 'input_placeholder' => '',
1220
+ 'internal_message' => $contact_support_template
1221
+ ),
1222
+ $reason_found_better_plugin,
1223
+ array(
1224
+ 'id' => self::REASON_GREAT_BUT_NEED_SPECIFIC_FEATURE,
1225
+ 'text' => sprintf( $this->get_text( 'reason-great-but-need-specific-feature' ), $module_type ),
1226
+ 'input_type' => 'textarea',
1227
+ 'input_placeholder' => $this->get_text( 'placeholder-feature' )
1228
+ ),
1229
+ array(
1230
+ 'id' => self::REASON_NOT_WORKING,
1231
+ 'text' => sprintf( $this->get_text( 'reason-not-working' ), $module_type ),
1232
+ 'input_type' => 'textarea',
1233
+ 'input_placeholder' => $this->get_text( 'placeholder-share-what-didnt-work' )
1234
+ ),
1235
+ array(
1236
+ 'id' => self::REASON_NOT_WHAT_I_WAS_LOOKING_FOR,
1237
+ 'text' => $this->get_text( 'reason-not-what-i-was-looking-for' ),
1238
+ 'input_type' => 'textarea',
1239
+ 'input_placeholder' => $this->get_text( 'placeholder-what-youve-been-looking-for' )
1240
+ ),
1241
+ array(
1242
+ 'id' => self::REASON_DIDNT_WORK_AS_EXPECTED,
1243
+ 'text' => sprintf( $this->get_text( 'reason-didnt-work-as-expected' ), $module_type ),
1244
+ 'input_type' => 'textarea',
1245
+ 'input_placeholder' => $this->get_text( 'placeholder-what-did-you-expect' )
1246
+ )
1247
+ )
1248
+ );
1249
+
1250
+ // Randomize the reasons for the current user type.
1251
+ shuffle( $uninstall_reasons[ $user_type ] );
1252
+
1253
+ // Keep the following reasons as the last items in the list.
1254
+ $uninstall_reasons[ $user_type ][] = $reason_temporary_deactivation;
1255
+ $uninstall_reasons[ $user_type ][] = $reason_other;
1256
+
1257
+ $uninstall_reasons = $this->apply_filters( 'uninstall_reasons', $uninstall_reasons );
1258
+
1259
+ return $uninstall_reasons[ $user_type ];
1260
+ }
1261
+
1262
+ /**
1263
+ * Called after the user has submitted his reason for deactivating the plugin.
1264
+ *
1265
+ * @author Leo Fajardo (@leorw)
1266
+ * @since 1.1.2
1267
+ */
1268
+ function _submit_uninstall_reason_action() {
1269
+ $this->_logger->entrance();
1270
+
1271
+ $this->check_ajax_referer( 'submit_uninstall_reason' );
1272
+
1273
+ $reason_id = fs_request_get( 'reason_id' );
1274
+
1275
+ // Check if the given reason ID is an unsigned integer.
1276
+ if ( ! ctype_digit( $reason_id ) ) {
1277
+ exit;
1278
+ }
1279
+
1280
+ $reason_info = trim( fs_request_get( 'reason_info', '' ) );
1281
+ if ( ! empty( $reason_info ) ) {
1282
+ $reason_info = substr( $reason_info, 0, 128 );
1283
+ }
1284
+
1285
+ $reason = (object) array(
1286
+ 'id' => $reason_id,
1287
+ 'info' => $reason_info,
1288
+ 'is_anonymous' => fs_request_get_bool( 'is_anonymous' )
1289
+ );
1290
+
1291
+ $this->_storage->store( 'uninstall_reason', $reason );
1292
+
1293
+ /**
1294
+ * If the module type is "theme", trigger the uninstall event here (on theme deactivation) since themes do
1295
+ * not support uninstall hook.
1296
+ *
1297
+ * @author Leo Fajardo (@leorw)
1298
+ * @since 1.2.2
1299
+ */
1300
+ if ( $this->is_theme() ) {
1301
+ $this->_uninstall_plugin_event( false );
1302
+ $this->remove_sdk_reference();
1303
+ }
1304
+
1305
+ // Print '1' for successful operation.
1306
+ echo 1;
1307
+ exit;
1308
+ }
1309
+
1310
+ #endregion
1311
+
1312
+ #----------------------------------------------------------------------------------
1313
+ #region Instance
1314
+ #----------------------------------------------------------------------------------
1315
+
1316
+ /**
1317
+ * Main singleton instance.
1318
+ *
1319
+ * @author Vova Feldman (@svovaf)
1320
+ * @since 1.0.0
1321
+ *
1322
+ * @param number $module_id
1323
+ * @param string|bool $slug
1324
+ * @param bool $is_init Is initiation sequence.
1325
+ *
1326
+ * @return Freemius|false
1327
+ */
1328
+ static function instance( $module_id, $slug = false, $is_init = false ) {
1329
+ if ( empty( $module_id ) ) {
1330
+ return false;
1331
+ }
1332
+
1333
+ if ( ! is_numeric( $module_id ) ) {
1334
+ if ( ! $is_init && true === $slug ) {
1335
+ $is_init = true;
1336
+ }
1337
+
1338
+ $slug = $module_id;
1339
+
1340
+ $module = FS_Plugin_Manager::instance( $slug )->get();
1341
+
1342
+ if ( is_object( $module ) ) {
1343
+ $module_id = $module->id;
1344
+ }
1345
+ }
1346
+
1347
+ $key = 'm_' . $module_id;
1348
+
1349
+ if ( ! isset( self::$_instances[ $key ] ) ) {
1350
+ if ( 0 === count( self::$_instances ) ) {
1351
+ self::_load_required_static();
1352
+ }
1353
+
1354
+ self::$_instances[ $key ] = new Freemius( $module_id, $slug, $is_init );
1355
+ }
1356
+
1357
+ return self::$_instances[ $key ];
1358
+ }
1359
+
1360
+ /**
1361
+ * @author Vova Feldman (@svovaf)
1362
+ * @since 1.0.6
1363
+ *
1364
+ * @param number $addon_id
1365
+ *
1366
+ * @return bool
1367
+ */
1368
+ private static function has_instance( $addon_id ) {
1369
+ return isset( self::$_instances[ 'm_' . $addon_id ] );
1370
+ }
1371
+
1372
+ /**
1373
+ * @author Leo Fajardo (@leorw)
1374
+ * @since 1.2.2
1375
+ *
1376
+ * @param string|number $id_or_slug
1377
+ *
1378
+ * @return number|false
1379
+ */
1380
+ private static function get_module_id( $id_or_slug ) {
1381
+ if ( is_numeric( $id_or_slug ) ) {
1382
+ return $id_or_slug;
1383
+ }
1384
+
1385
+ foreach ( self::$_instances as $instance ) {
1386
+ if ( $instance->is_plugin() && ( $id_or_slug === $instance->get_slug() ) ) {
1387
+ return $instance->get_id();
1388
+ }
1389
+ }
1390
+
1391
+ return false;
1392
+ }
1393
+
1394
+ /**
1395
+ * @author Vova Feldman (@svovaf)
1396
+ * @since 1.0.6
1397
+ *
1398
+ * @param number $id
1399
+ *
1400
+ * @return false|Freemius
1401
+ */
1402
+ static function get_instance_by_id( $id ) {
1403
+ return isset ( self::$_instances[ 'm_' . $id ] ) ?
1404
+ self::$_instances[ 'm_' . $id ] :
1405
+ false;
1406
+ }
1407
+
1408
+ /**
1409
+ *
1410
+ * @author Vova Feldman (@svovaf)
1411
+ * @since 1.0.1
1412
+ *
1413
+ * @param $plugin_file
1414
+ *
1415
+ * @return false|Freemius
1416
+ */
1417
+ static function get_instance_by_file( $plugin_file ) {
1418
+ $slug = self::find_slug_by_basename( $plugin_file );
1419
+
1420
+ return ( false !== $slug ) ?
1421
+ self::instance( self::get_module_id( $slug ) ) :
1422
+ false;
1423
+ }
1424
+
1425
+ /**
1426
+ * @author Vova Feldman (@svovaf)
1427
+ * @since 1.0.6
1428
+ *
1429
+ * @return false|Freemius
1430
+ */
1431
+ function get_parent_instance() {
1432
+ return self::get_instance_by_id( $this->_plugin->parent_plugin_id );
1433
+ }
1434
+
1435
+ /**
1436
+ * @author Vova Feldman (@svovaf)
1437
+ * @since 1.0.6
1438
+ *
1439
+ * @param string|number $id_or_slug
1440
+ *
1441
+ * @return false|Freemius
1442
+ */
1443
+ function get_addon_instance( $id_or_slug ) {
1444
+ $addon_id = self::get_module_id( $id_or_slug );
1445
+
1446
+ return self::instance( $addon_id );
1447
+ }
1448
+
1449
+ #endregion ------------------------------------------------------------------
1450
+
1451
+ /**
1452
+ * @author Vova Feldman (@svovaf)
1453
+ * @since 1.0.6
1454
+ *
1455
+ * @return bool
1456
+ */
1457
+ function is_parent_plugin_installed() {
1458
+ $is_active = self::has_instance( $this->_plugin->parent_plugin_id );
1459
+
1460
+ if ( $is_active ) {
1461
+ return true;
1462
+ }
1463
+
1464
+ /**
1465
+ * Parent module might be a theme. If that's the case, the add-on's FS
1466
+ * instance will be loaded prior to the theme's FS instance, therefore,
1467
+ * we need to check if it's active with a "look ahead".
1468
+ *
1469
+ * @author Vova Feldman
1470
+ * @since 1.2.2.3
1471
+ */
1472
+ global $fs_active_plugins;
1473
+ if ( is_object( $fs_active_plugins ) && is_array( $fs_active_plugins->plugins ) ) {
1474
+ $active_theme = wp_get_theme();
1475
+
1476
+ foreach ( $fs_active_plugins->plugins as $sdk => $module ) {
1477
+ if ( WP_FS__MODULE_TYPE_THEME === $module->type ) {
1478
+ if ( $module->plugin_path == $active_theme->get_stylesheet() ) {
1479
+ // Parent module is a theme and it's currently active.
1480
+ return true;
1481
+ }
1482
+ }
1483
+ }
1484
+ }
1485
+
1486
+ return false;
1487
+ }
1488
+
1489
+ /**
1490
+ * Check if add-on parent plugin in activation mode.
1491
+ *
1492
+ * @author Vova Feldman (@svovaf)
1493
+ * @since 1.0.7
1494
+ *
1495
+ * @return bool
1496
+ */
1497
+ function is_parent_in_activation() {
1498
+ $parent_fs = $this->get_parent_instance();
1499
+ if ( ! is_object( $parent_fs ) ) {
1500
+ return false;
1501
+ }
1502
+
1503
+ return ( $parent_fs->is_activation_mode() );
1504
+ }
1505
+
1506
+ /**
1507
+ * Is plugin in activation mode.
1508
+ *
1509
+ * @author Vova Feldman (@svovaf)
1510
+ * @since 1.0.7
1511
+ *
1512
+ * @param bool $and_on
1513
+ *
1514
+ * @return bool
1515
+ */
1516
+ function is_activation_mode( $and_on = true ) {
1517
+ return (
1518
+ ( $this->is_on() || ! $and_on ) &&
1519
+ ( ! $this->is_registered() || ( $this->is_only_premium() && ! $this->has_features_enabled_license() ) ) &&
1520
+ ( ! $this->is_enable_anonymous() ||
1521
+ ( ! $this->is_anonymous() && ! $this->is_pending_activation() ) )
1522
+ );
1523
+ }
1524
+
1525
+ /**
1526
+ * Check if current page is the opt-in/pending-activation page.
1527
+ *
1528
+ * @author Vova Feldman (@svovaf)
1529
+ * @since 1.2.1.7
1530
+ *
1531
+ * @return bool
1532
+ */
1533
+ function is_activation_page() {
1534
+ if ( $this->_menu->is_main_settings_page() ) {
1535
+ return true;
1536
+ }
1537
+
1538
+ if ( ! $this->is_activation_mode() ) {
1539
+ return false;
1540
+ }
1541
+
1542
+ // Check if current page is matching the activation page.
1543
+ return $this->is_matching_url( $this->get_activation_url() );
1544
+ }
1545
+
1546
+ /**
1547
+ * Check if URL path's are matching and that all querystring
1548
+ * arguments of the $sub_url exist in the $url with the same values.
1549
+ *
1550
+ * WARNING:
1551
+ * 1. This method doesn't check if the sub/domain are matching.
1552
+ * 2. Ignore case sensitivity.
1553
+ *
1554
+ * @author Vova Feldman (@svovaf)
1555
+ * @since 1.2.1.7
1556
+ *
1557
+ * @param string $sub_url
1558
+ * @param string $url If argument is not set, check if the sub_url matching the current's page URL.
1559
+ *
1560
+ * @return bool
1561
+ */
1562
+ private function is_matching_url( $sub_url, $url = '' ) {
1563
+ if ( empty( $url ) ) {
1564
+ $url = $_SERVER['REQUEST_URI'];
1565
+ }
1566
+
1567
+ $url = strtolower( $url );
1568
+ $sub_url = strtolower( $sub_url );
1569
+
1570
+ if ( parse_url( $sub_url, PHP_URL_PATH ) !== parse_url( $url, PHP_URL_PATH ) ) {
1571
+ // Different path - DO NOT OVERRIDE PAGE.
1572
+ return false;
1573
+ }
1574
+
1575
+ $url_params = array();
1576
+ parse_str( parse_url( $url, PHP_URL_QUERY ), $url_params );
1577
+
1578
+ $sub_url_params = array();
1579
+ parse_str( parse_url( $sub_url, PHP_URL_QUERY ), $sub_url_params );
1580
+
1581
+ foreach ( $sub_url_params as $key => $val ) {
1582
+ if ( ! isset( $url_params[ $key ] ) || $val != $url_params[ $key ] ) {
1583
+ // Not matching query string - DO NOT OVERRIDE PAGE.
1584
+ return false;
1585
+ }
1586
+ }
1587
+
1588
+ return true;
1589
+ }
1590
+
1591
+ /**
1592
+ * Get collection of all active plugins.
1593
+ *
1594
+ * @author Vova Feldman (@svovaf)
1595
+ * @since 1.0.9
1596
+ *
1597
+ * @return array[string]array
1598
+ */
1599
+ private static function get_active_plugins() {
1600
+ self::require_plugin_essentials();
1601
+
1602
+ $active_plugin = array();
1603
+ $all_plugins = get_plugins();
1604
+ $active_plugins_basenames = get_option( 'active_plugins' );
1605
+
1606
+ foreach ( $active_plugins_basenames as $plugin_basename ) {
1607
+ $active_plugin[ $plugin_basename ] = $all_plugins[ $plugin_basename ];
1608
+ }
1609
+
1610
+ return $active_plugin;
1611
+ }
1612
+
1613
+ /**
1614
+ * Get collection of all plugins.
1615
+ *
1616
+ * @author Vova Feldman (@svovaf)
1617
+ * @since 1.1.8
1618
+ *
1619
+ * @return array Key is the plugin file path and the value is an array of the plugin data.
1620
+ */
1621
+ private static function get_all_plugins() {
1622
+ self::require_plugin_essentials();
1623
+
1624
+ $all_plugins = get_plugins();
1625
+ $active_plugins_basenames = get_option( 'active_plugins' );
1626
+
1627
+ foreach ( $all_plugins as $basename => &$data ) {
1628
+ // By default set to inactive (next foreach update the active plugins).
1629
+ $data['is_active'] = false;
1630
+ // Enrich with plugin slug.
1631
+ $data['slug'] = self::get_plugin_slug( $basename );
1632
+ }
1633
+
1634
+ // Flag active plugins.
1635
+ foreach ( $active_plugins_basenames as $basename ) {
1636
+ if ( isset( $all_plugins[ $basename ] ) ) {
1637
+ $all_plugins[ $basename ]['is_active'] = true;
1638
+ }
1639
+ }
1640
+
1641
+ return $all_plugins;
1642
+ }
1643
+
1644
+
1645
+ /**
1646
+ * Cached result of get_site_transient( 'update_plugins' )
1647
+ *
1648
+ * @author Vova Feldman (@svovaf)
1649
+ * @since 1.1.8
1650
+ *
1651
+ * @var object
1652
+ */
1653
+ private static $_plugins_info;
1654
+
1655
+ /**
1656
+ * Helper function to get specified plugin's slug.
1657
+ *
1658
+ * @author Vova Feldman (@svovaf)
1659
+ * @since 1.1.8
1660
+ *
1661
+ * @param $basename
1662
+ *
1663
+ * @return string
1664
+ */
1665
+ private static function get_plugin_slug( $basename ) {
1666
+ if ( ! isset( self::$_plugins_info ) ) {
1667
+ self::$_plugins_info = get_site_transient( 'update_plugins' );
1668
+ }
1669
+
1670
+ $slug = '';
1671
+
1672
+ if ( is_object( self::$_plugins_info ) ) {
1673
+ if ( isset( self::$_plugins_info->no_update ) &&
1674
+ isset( self::$_plugins_info->no_update[ $basename ] ) &&
1675
+ ! empty( self::$_plugins_info->no_update[ $basename ]->slug )
1676
+ ) {
1677
+ $slug = self::$_plugins_info->no_update[ $basename ]->slug;
1678
+ } else if ( isset( self::$_plugins_info->response ) &&
1679
+ isset( self::$_plugins_info->response[ $basename ] ) &&
1680
+ ! empty( self::$_plugins_info->response[ $basename ]->slug )
1681
+ ) {
1682
+ $slug = self::$_plugins_info->response[ $basename ]->slug;
1683
+ }
1684
+ }
1685
+
1686
+ if ( empty( $slug ) ) {
1687
+ // Try to find slug from FS data.
1688
+ $slug = self::find_slug_by_basename( $basename );
1689
+ }
1690
+
1691
+ if ( empty( $slug ) ) {
1692
+ // Fallback to plugin's folder name.
1693
+ $slug = dirname( $basename );
1694
+ }
1695
+
1696
+ return $slug;
1697
+ }
1698
+
1699
+ private static $_statics_loaded = false;
1700
+
1701
+ /**
1702
+ * Load static resources.
1703
+ *
1704
+ * @author Vova Feldman (@svovaf)
1705
+ * @since 1.0.1
1706
+ */
1707
+ private static function _load_required_static() {
1708
+ if ( self::$_statics_loaded ) {
1709
+ return;
1710
+ }
1711
+
1712
+ self::$_static_logger = FS_Logger::get_logger( WP_FS__SLUG, WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
1713
+
1714
+ self::$_static_logger->entrance();
1715
+
1716
+ self::$_accounts = FS_Option_Manager::get_manager( WP_FS__ACCOUNTS_OPTION_NAME, true );
1717
+
1718
+ self::$_global_admin_notices = FS_Admin_Notice_Manager::instance( 'global' );
1719
+
1720
+ add_action( 'admin_menu', array( 'Freemius', '_add_debug_section' ) );
1721
+
1722
+ add_action( "wp_ajax_fs_toggle_debug_mode", array( 'Freemius', '_toggle_debug_mode' ) );
1723
+
1724
+ self::add_ajax_action_static( 'get_debug_log', array( 'Freemius', '_get_debug_log' ) );
1725
+
1726
+ self::add_ajax_action_static( 'get_db_option', array( 'Freemius', '_get_db_option' ) );
1727
+
1728
+ self::add_ajax_action_static( 'set_db_option', array( 'Freemius', '_set_db_option' ) );
1729
+
1730
+ if ( 0 == did_action( 'plugins_loaded' ) ) {
1731
+ add_action( 'plugins_loaded', array( 'Freemius', '_load_textdomain' ), 1 );
1732
+ }
1733
+
1734
+ self::$_statics_loaded = true;
1735
+ }
1736
+
1737
+ #----------------------------------------------------------------------------------
1738
+ #region Localization
1739
+ #----------------------------------------------------------------------------------
1740
+
1741
+ /**
1742
+ * Load framework's text domain.
1743
+ *
1744
+ * @author Vova Feldman (@svovaf)
1745
+ * @since 1.2.1
1746
+ */
1747
+ static function _load_textdomain() {
1748
+ if ( ! is_admin() ) {
1749
+ return;
1750
+ }
1751
+
1752
+ global $fs_active_plugins;
1753
+
1754
+ // Works both for plugins and themes.
1755
+ load_plugin_textdomain(
1756
+ 'freemius',
1757
+ false,
1758
+ $fs_active_plugins->newest->sdk_path . '/languages/'
1759
+ );
1760
+ }
1761
+
1762
+ #endregion
1763
+
1764
+ #----------------------------------------------------------------------------------
1765
+ #region Debugging
1766
+ #----------------------------------------------------------------------------------
1767
+
1768
+ /**
1769
+ * @author Vova Feldman (@svovaf)
1770
+ * @since 1.0.8
1771
+ */
1772
+ static function _add_debug_section() {
1773
+ if ( ! current_user_can( 'activate_plugins' )
1774
+ && ! current_user_can( 'switch_themes' )
1775
+ ) {
1776
+ return;
1777
+ }
1778
+
1779
+ self::$_static_logger->entrance();
1780
+
1781
+ $title = sprintf( '%s [v.%s]', fs_text( 'freemius-debug' ), WP_FS__SDK_VERSION );
1782
+
1783
+ if ( WP_FS__DEV_MODE ) {
1784
+ // Add top-level debug menu item.
1785
+ $hook = FS_Admin_Menu_Manager::add_page(
1786
+ $title,
1787
+ $title,
1788
+ 'manage_options',
1789
+ 'freemius',
1790
+ array( 'Freemius', '_debug_page_render' )
1791
+ );
1792
+ } else {
1793
+ // Add hidden debug page.
1794
+ $hook = FS_Admin_Menu_Manager::add_subpage(
1795
+ null,
1796
+ $title,
1797
+ $title,
1798
+ 'manage_options',
1799
+ 'freemius',
1800
+ array( 'Freemius', '_debug_page_render' )
1801
+ );
1802
+ }
1803
+
1804
+ if ( ! empty( $hook ) ) {
1805
+ add_action( "load-$hook", array( 'Freemius', '_debug_page_actions' ) );
1806
+ }
1807
+ }
1808
+
1809
+ /**
1810
+ * @author Vova Feldman (@svovaf)
1811
+ * @since 1.1.7.3
1812
+ */
1813
+ static function _toggle_debug_mode() {
1814
+ $is_on = fs_request_get( 'is_on', false, 'post' );
1815
+
1816
+ if ( fs_request_is_post() && in_array( $is_on, array( 0, 1 ) ) ) {
1817
+ update_option( 'fs_debug_mode', $is_on );
1818
+
1819
+ // Turn on/off storage logging.
1820
+ FS_Logger::_set_storage_logging( ( 1 == $is_on ) );
1821
+ }
1822
+
1823
+ exit;
1824
+ }
1825
+
1826
+ /**
1827
+ * @author Vova Feldman (@svovaf)
1828
+ * @since 1.2.1.6
1829
+ */
1830
+ static function _get_debug_log() {
1831
+ $logs = FS_Logger::load_db_logs(
1832
+ fs_request_get( 'filters', false, 'post' ),
1833
+ ! empty( $_POST['limit'] ) && is_numeric( $_POST['limit'] ) ? $_POST['limit'] : 200,
1834
+ ! empty( $_POST['offset'] ) && is_numeric( $_POST['offset'] ) ? $_POST['offset'] : 0
1835
+ );
1836
+
1837
+ self::shoot_ajax_success( $logs );
1838
+ }
1839
+
1840
+ /**
1841
+ * @author Vova Feldman (@svovaf)
1842
+ * @since 1.2.1.7
1843
+ */
1844
+ static function _get_db_option() {
1845
+ $option_name = fs_request_get( 'option_name' );
1846
+
1847
+ $value = get_option( $option_name );
1848
+
1849
+ $result = array(
1850
+ 'name' => $option_name,
1851
+ );
1852
+
1853
+ if ( false !== $value ) {
1854
+ if ( ! is_string( $value ) ) {
1855
+ $value = json_encode( $value );
1856
+ }
1857
+
1858
+ $result['value'] = $value;
1859
+ }
1860
+
1861
+ self::shoot_ajax_success( $result );
1862
+ }
1863
+
1864
+ /**
1865
+ * @author Vova Feldman (@svovaf)
1866
+ * @since 1.2.1.7
1867
+ */
1868
+ static function _set_db_option() {
1869
+ $option_name = fs_request_get( 'option_name' );
1870
+ $option_value = fs_request_get( 'option_value' );
1871
+
1872
+ if ( ! empty( $option_value ) ) {
1873
+ update_option( $option_name, $option_value );
1874
+ }
1875
+
1876
+ self::shoot_ajax_success();
1877
+ }
1878
+
1879
+ /**
1880
+ * @author Vova Feldman (@svovaf)
1881
+ * @since 1.0.8
1882
+ */
1883
+ static function _debug_page_actions() {
1884
+ self::_clean_admin_content_section();
1885
+
1886
+ if ( fs_request_is_action( 'restart_freemius' ) ) {
1887
+ check_admin_referer( 'restart_freemius' );
1888
+
1889
+ // Clear accounts data.
1890
+ self::$_accounts->clear( true );
1891
+
1892
+ // Clear SDK reference cache.
1893
+ delete_option( 'fs_active_plugins' );
1894
+ } else if ( fs_request_is_action( 'simulate_trial' ) ) {
1895
+ check_admin_referer( 'simulate_trial' );
1896
+
1897
+ $fs = freemius( fs_request_get( 'module_id' ) );
1898
+
1899
+ // Update SDK install to at least 24 hours before.
1900
+ $fs->_storage->install_timestamp = ( time() - WP_FS__TIME_24_HOURS_IN_SEC );
1901
+ // Unset the trial shown timestamp.
1902
+ unset( $fs->_storage->trial_promotion_shown );
1903
+ } else if ( fs_request_is_action( 'delete_install' ) ) {
1904
+ check_admin_referer( 'delete_install' );
1905
+
1906
+ self::_delete_site_by_slug(
1907
+ fs_request_get( 'slug' ),
1908
+ fs_request_get( 'module_type' )
1909
+ );
1910
+ } else if ( fs_request_is_action( 'download_logs' ) ) {
1911
+ check_admin_referer( 'download_logs' );
1912
+
1913
+ $download_url = FS_Logger::download_db_logs(
1914
+ fs_request_get( 'filters', false, 'post' )
1915
+ );
1916
+
1917
+ if ( false === $download_url ) {
1918
+ wp_die( 'Oops... there was an error while generating the logs download file. Please try again and if it doesn\'t work contact support@freemius.com.' );
1919
+ }
1920
+
1921
+ fs_redirect( $download_url );
1922
+ }
1923
+ }
1924
+
1925
+ /**
1926
+ * @author Vova Feldman (@svovaf)
1927
+ * @since 1.0.8
1928
+ */
1929
+ static function _debug_page_render() {
1930
+ self::$_static_logger->entrance();
1931
+
1932
+ $vars = array(
1933
+ 'plugin_sites' => self::get_all_sites(),
1934
+ 'theme_sites' => self::get_all_sites( WP_FS__MODULE_TYPE_THEME ),
1935
+ 'users' => self::get_all_users(),
1936
+ 'addons' => self::get_all_addons(),
1937
+ 'account_addons' => self::get_all_account_addons(),
1938
+ 'plugin_licenses' => self::get_all_licenses(),
1939
+ 'theme_licenses' => self::get_all_licenses( WP_FS__MODULE_TYPE_THEME )
1940
+ );
1941
+
1942
+ fs_enqueue_local_style( 'fs_debug', '/admin/debug.css' );
1943
+ fs_require_once_template( 'debug.php', $vars );
1944
+ }
1945
+
1946
+ #endregion
1947
+
1948
+ #----------------------------------------------------------------------------------
1949
+ #region Connectivity Issues
1950
+ #----------------------------------------------------------------------------------
1951
+
1952
+ /**
1953
+ * Check if Freemius should be turned on for the current plugin install.
1954
+ *
1955
+ * Note:
1956
+ * $this->_is_on is updated in has_api_connectivity()
1957
+ *
1958
+ * @author Vova Feldman (@svovaf)
1959
+ * @since 1.0.9
1960
+ *
1961
+ * @return bool
1962
+ */
1963
+ function is_on() {
1964
+ self::$_static_logger->entrance();
1965
+
1966
+ if ( isset( $this->_is_on ) ) {
1967
+ return $this->_is_on;
1968
+ }
1969
+
1970
+ // If already installed or pending then sure it's on :)
1971
+ if ( $this->is_registered() || $this->is_pending_activation() ) {
1972
+ $this->_is_on = true;
1973
+
1974
+ return true;
1975
+ }
1976
+
1977
+ return false;
1978
+ }
1979
+
1980
+ /**
1981
+ * @author Vova Feldman (@svovaf)
1982
+ * @since 1.1.7.3
1983
+ *
1984
+ * @param bool $flush_if_no_connectivity
1985
+ *
1986
+ * @return bool
1987
+ */
1988
+ private function should_run_connectivity_test( $flush_if_no_connectivity = false ) {
1989
+ if ( ! isset( $this->_storage->connectivity_test ) ) {
1990
+ // Connectivity test was never executed, or cache was cleared.
1991
+ return true;
1992
+ }
1993
+
1994
+ if ( WP_FS__PING_API_ON_IP_OR_HOST_CHANGES ) {
1995
+ if ( WP_FS__IS_HTTP_REQUEST ) {
1996
+ if ( $_SERVER['HTTP_HOST'] != $this->_storage->connectivity_test['host'] ) {
1997
+ // Domain changed.
1998
+ return true;
1999
+ }
2000
+
2001
+ if ( WP_FS__REMOTE_ADDR != $this->_storage->connectivity_test['server_ip'] ) {
2002
+ // Server IP changed.
2003
+ return true;
2004
+ }
2005
+ }
2006
+ }
2007
+
2008
+ if ( $this->_storage->connectivity_test['is_connected'] &&
2009
+ $this->_storage->connectivity_test['is_active']
2010
+ ) {
2011
+ // API connected and Freemius is active - no need to run connectivity check.
2012
+ return false;
2013
+ }
2014
+
2015
+ if ( $flush_if_no_connectivity ) {
2016
+ /**
2017
+ * If explicitly asked to flush when no connectivity - do it only
2018
+ * if at least 10 sec passed from the last API connectivity test.
2019
+ */
2020
+ return ( isset( $this->_storage->connectivity_test['timestamp'] ) &&
2021
+ ( WP_FS__SCRIPT_START_TIME - $this->_storage->connectivity_test['timestamp'] ) > 10 );
2022
+ }
2023
+
2024
+ /**
2025
+ * @since 1.1.7 Don't check for connectivity on plugin downgrade.
2026
+ */
2027
+ $version = $this->get_plugin_version();
2028
+ if ( version_compare( $version, $this->_storage->connectivity_test['version'], '>' ) ) {
2029
+ // If it's a plugin version upgrade and Freemius is off or no connectivity, run connectivity test.
2030
+ return true;
2031
+ }
2032
+
2033
+ return false;
2034
+ }
2035
+
2036
+ /**
2037
+ * @author Vova Feldman (@svovaf)
2038
+ * @since 1.1.7.4
2039
+ *
2040
+ * @return object|false
2041
+ */
2042
+ private function ping() {
2043
+ if ( WP_FS__SIMULATE_NO_API_CONNECTIVITY ) {
2044
+ return false;
2045
+ }
2046
+
2047
+ $version = $this->get_plugin_version();
2048
+
2049
+ $is_update = $this->apply_filters( 'is_plugin_update', $this->is_plugin_update() );
2050
+
2051
+ return $this->get_api_plugin_scope()->ping(
2052
+ $this->get_anonymous_id(),
2053
+ array(
2054
+ 'is_update' => json_encode( $is_update ),
2055
+ 'version' => $version,
2056
+ 'sdk' => $this->version,
2057
+ 'is_admin' => json_encode( is_admin() ),
2058
+ 'is_ajax' => json_encode( self::is_ajax() ),
2059
+ 'is_cron' => json_encode( self::is_cron() ),
2060
+ 'is_http' => json_encode( WP_FS__IS_HTTP_REQUEST ),
2061
+ )
2062
+ );
2063
+ }
2064
+
2065
+ /**
2066
+ * Check if there's any connectivity issue to Freemius API.
2067
+ *
2068
+ * @author Vova Feldman (@svovaf)
2069
+ * @since 1.0.9
2070
+ *
2071
+ * @param bool $flush_if_no_connectivity
2072
+ *
2073
+ * @return bool
2074
+ */
2075
+ function has_api_connectivity( $flush_if_no_connectivity = false ) {
2076
+ $this->_logger->entrance();
2077
+
2078
+ if ( isset( $this->_has_api_connection ) && ( $this->_has_api_connection || ! $flush_if_no_connectivity ) ) {
2079
+ return $this->_has_api_connection;
2080
+ }
2081
+
2082
+ if ( WP_FS__SIMULATE_NO_API_CONNECTIVITY &&
2083
+ isset( $this->_storage->connectivity_test ) &&
2084
+ true === $this->_storage->connectivity_test['is_connected']
2085
+ ) {
2086
+ unset( $this->_storage->connectivity_test );
2087
+ }
2088
+
2089
+ if ( ! $this->should_run_connectivity_test( $flush_if_no_connectivity ) ) {
2090
+ $this->_has_api_connection = $this->_storage->connectivity_test['is_connected'];
2091
+ /**
2092
+ * @since 1.1.6 During dev mode, if there's connectivity - turn Freemius on regardless the configuration.
2093
+ *
2094
+ * @since 1.2.1.5 If the user running the premium version then ignore the 'is_active' flag and turn Freemius on to enable license key activation.
2095
+ */
2096
+ $this->_is_on = $this->_storage->connectivity_test['is_active'] ||
2097
+ $this->is_premium() ||
2098
+ ( WP_FS__DEV_MODE && $this->_has_api_connection && ! WP_FS__SIMULATE_FREEMIUS_OFF );
2099
+
2100
+ return $this->_has_api_connection;
2101
+ }
2102
+
2103
+ $pong = $this->ping();
2104
+ $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong );
2105
+
2106
+ if ( ! $is_connected ) {
2107
+ // API failure.
2108
+ $this->_add_connectivity_issue_message( $pong );
2109
+ }
2110
+
2111
+ $this->store_connectivity_info( $pong, $is_connected );
2112
+
2113
+ return $this->_has_api_connection;
2114
+ }
2115
+
2116
+ /**
2117
+ * @author Vova Feldman (@svovaf)
2118
+ * @since 1.1.7.4
2119
+ *
2120
+ * @param object $pong
2121
+ * @param bool $is_connected
2122
+ */
2123
+ private function store_connectivity_info( $pong, $is_connected ) {
2124
+ $this->_logger->entrance();
2125
+
2126
+ $version = $this->get_plugin_version();
2127
+
2128
+ if ( ! $is_connected || WP_FS__SIMULATE_FREEMIUS_OFF ) {
2129
+ $is_active = false;
2130
+ } else {
2131
+ $is_active = ( isset( $pong->is_active ) && true == $pong->is_active );
2132
+ }
2133
+
2134
+ $is_active = $this->apply_filters(
2135
+ 'is_on',
2136
+ $is_active,
2137
+ $this->is_plugin_update(),
2138
+ $version
2139
+ );
2140
+
2141
+ $this->_storage->connectivity_test = array(
2142
+ 'is_connected' => $is_connected,
2143
+ 'host' => $_SERVER['HTTP_HOST'],
2144
+ 'server_ip' => WP_FS__REMOTE_ADDR,
2145
+ 'is_active' => $is_active,
2146
+ 'timestamp' => WP_FS__SCRIPT_START_TIME,
2147
+ // Last version with connectivity attempt.
2148
+ 'version' => $version,
2149
+ );
2150
+
2151
+ $this->_has_api_connection = $is_connected;
2152
+ $this->_is_on = $is_active || ( WP_FS__DEV_MODE && $is_connected && ! WP_FS__SIMULATE_FREEMIUS_OFF );
2153
+ }
2154
+
2155
+ /**
2156
+ * Force turning Freemius on.
2157
+ *
2158
+ * @author Vova Feldman (@svovaf)
2159
+ * @since 1.1.8.1
2160
+ *
2161
+ * @return bool TRUE if successfully turned on.
2162
+ */
2163
+ private function turn_on() {
2164
+ $this->_logger->entrance();
2165
+
2166
+ if ( $this->is_on() || ! isset( $this->_storage->connectivity_test['is_active'] ) ) {
2167
+ return false;
2168
+ }
2169
+
2170
+ $updated_connectivity = $this->_storage->connectivity_test;
2171
+ $updated_connectivity['is_active'] = true;
2172
+ $updated_connectivity['timestamp'] = WP_FS__SCRIPT_START_TIME;
2173
+ $this->_storage->connectivity_test = $updated_connectivity;
2174
+
2175
+ $this->_is_on = true;
2176
+
2177
+ return true;
2178
+ }
2179
+
2180
+ /**
2181
+ * Anonymous and unique site identifier (Hash).
2182
+ *
2183
+ * @author Vova Feldman (@svovaf)
2184
+ * @since 1.1.0
2185
+ *
2186
+ * @return string
2187
+ */
2188
+ function get_anonymous_id() {
2189
+ $unique_id = self::$_accounts->get_option( 'unique_id' );
2190
+
2191
+ if ( empty( $unique_id ) || ! is_string( $unique_id ) ) {
2192
+ $key = get_site_url();
2193
+
2194
+ // If localhost, assign microtime instead of domain.
2195
+ if ( WP_FS__IS_LOCALHOST ||
2196
+ false !== strpos( $key, 'localhost' ) ||
2197
+ false === strpos( $key, '.' )
2198
+ ) {
2199
+ $key = microtime();
2200
+ }
2201
+
2202
+ $unique_id = md5( $key );
2203
+
2204
+ self::$_accounts->set_option( 'unique_id', $unique_id, true );
2205
+ }
2206
+
2207
+ $this->_logger->departure( $unique_id );
2208
+
2209
+ return $unique_id;
2210
+ }
2211
+
2212
+ /**
2213
+ * @author Vova Feldman (@svovaf)
2214
+ * @since 1.1.7.4
2215
+ *
2216
+ * @return \WP_User
2217
+ */
2218
+ static function _get_current_wp_user() {
2219
+ self::require_pluggable_essentials();
2220
+
2221
+ return wp_get_current_user();
2222
+ }
2223
+
2224
+ /**
2225
+ * @author Vova Feldman (@svovaf)
2226
+ * @since 1.2.1.7
2227
+ *
2228
+ * @param string $email
2229
+ *
2230
+ * @return bool
2231
+ */
2232
+ static function is_valid_email( $email ) {
2233
+ if ( false === filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
2234
+ return false;
2235
+ }
2236
+
2237
+ $parts = explode( '@', $email );
2238
+
2239
+ if ( 2 !== count( $parts ) || empty( $parts[1] ) ) {
2240
+ return false;
2241
+ }
2242
+
2243
+ $blacklist = array(
2244
+ 'admin.',
2245
+ 'webmaster.',
2246
+ 'localhost.',
2247
+ 'dev.',
2248
+ 'development.',
2249
+ 'test.',
2250
+ 'stage.',
2251
+ 'staging.',
2252
+ );
2253
+
2254
+ // Make sure domain is not one of the blacklisted.
2255
+ foreach ( $blacklist as $invalid ) {
2256
+ if ( 0 === strpos( $parts[1], $invalid ) ) {
2257
+ return false;
2258
+ }
2259
+ }
2260
+
2261
+ // Get the UTF encoded domain name.
2262
+ $domain = idn_to_ascii( $parts[1] ) . '.';
2263
+
2264
+ return ( checkdnsrr( $domain, 'MX' ) || checkdnsrr( $domain, 'A' ) );
2265
+ }
2266
+
2267
+ /**
2268
+ * Generate API connectivity issue message.
2269
+ *
2270
+ * @author Vova Feldman (@svovaf)
2271
+ * @since 1.0.9
2272
+ *
2273
+ * @param mixed $api_result
2274
+ * @param bool $is_first_failure
2275
+ */
2276
+ function _add_connectivity_issue_message( $api_result, $is_first_failure = true ) {
2277
+ if ( ! $this->is_premium() && $this->_enable_anonymous ) {
2278
+ // Don't add message if it's the free version and can run anonymously.
2279
+ return;
2280
+ }
2281
+
2282
+ if ( ! function_exists( 'wp_nonce_url' ) ) {
2283
+ require_once ABSPATH . 'wp-includes/functions.php';
2284
+ }
2285
+
2286
+ $current_user = self::_get_current_wp_user();
2287
+ // $admin_email = get_option( 'admin_email' );
2288
+ $admin_email = $current_user->user_email;
2289
+
2290
+ $message = false;
2291
+ if ( is_object( $api_result ) &&
2292
+ isset( $api_result->error ) &&
2293
+ isset( $api_result->error->code )
2294
+ ) {
2295
+ switch ( $api_result->error->code ) {
2296
+ case 'curl_missing':
2297
+ $missing_methods = '';
2298
+ if ( is_array( $api_result->missing_methods ) &&
2299
+ ! empty( $api_result->missing_methods )
2300
+ ) {
2301
+ foreach ( $api_result->missing_methods as $m ) {
2302
+ if ( 'curl_version' === $m ) {
2303
+ continue;
2304
+ }
2305
+
2306
+ if ( ! empty( $missing_methods ) ) {
2307
+ $missing_methods .= ', ';
2308
+ }
2309
+
2310
+ $missing_methods .= sprintf( '<code>%s</code>', $m );
2311
+ }
2312
+
2313
+ if ( ! empty( $missing_methods ) ) {
2314
+ $missing_methods = sprintf(
2315
+ '<br><br><b>%s</b> %s',
2316
+ $this->get_text( 'curl-disabled-methods' ),
2317
+ $missing_methods
2318
+ );
2319
+ }
2320
+ }
2321
+
2322
+ $message = sprintf(
2323
+ $this->get_text( 'x-requires-access-to-api' ) . ' ' .
2324
+ $this->get_text( 'curl-missing-message' ) . ' ' .
2325
+ $missing_methods .
2326
+ ' %s',
2327
+ '<b>' . $this->get_plugin_name() . '</b>',
2328
+ sprintf(
2329
+ '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
2330
+ sprintf(
2331
+ '<a class="fs-resolve" data-type="curl" href="#"><b>%s</b></a>%s',
2332
+ $this->get_text( 'curl-missing-no-clue-title' ),
2333
+ ' - ' . sprintf(
2334
+ $this->get_text( 'curl-missing-no-clue-desc' ),
2335
+ '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
2336
+ )
2337
+ ),
2338
+ sprintf(
2339
+ '<b>%s</b> - %s',
2340
+ $this->get_text( 'sysadmin-title' ),
2341
+ sprintf( $this->get_text( 'curl-missing-sysadmin-desc' ), $this->_module_type )
2342
+ ),
2343
+ sprintf(
2344
+ '<a href="%s"><b>%s</b></a>%s',
2345
+ wp_nonce_url( 'plugins.php?action=deactivate&amp;plugin=' . $this->_plugin_basename . '&amp;plugin_status=' . 'all' . '&amp;paged=' . '1' . '&amp;s=' . '', 'deactivate-plugin_' . $this->_plugin_basename ),
2346
+ $this->get_text( 'deactivate-plugin-title' ),
2347
+ ' - ' . $this->get_text( 'deactivate-plugin-desc' )
2348
+ )
2349
+ )
2350
+ );
2351
+ break;
2352
+ case 'cloudflare_ddos_protection':
2353
+ $message = sprintf(
2354
+ $this->get_text( 'x-requires-access-to-api' ) . ' ' .
2355
+ $this->get_text( 'cloudflare-blocks-connection-message' ) . ' ' .
2356
+ $this->get_text( 'happy-to-resolve-issue-asap' ) .
2357
+ ' %s',
2358
+ '<b>' . $this->get_plugin_name() . '</b>',
2359
+ sprintf(
2360
+ '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
2361
+ sprintf(
2362
+ '<a class="fs-resolve" data-type="cloudflare" href="#"><b>%s</b></a>%s',
2363
+ $this->get_text( 'fix-issue-title' ),
2364
+ ' - ' . sprintf(
2365
+ $this->get_text( 'fix-issue-desc' ),
2366
+ '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
2367
+ )
2368
+ ),
2369
+ sprintf(
2370
+ '<a href="%s" target="_blank"><b>%s</b></a>%s',
2371
+ sprintf( 'https://wordpress.org/plugins/%s/download/', $this->_slug ),
2372
+ $this->get_text( 'install-previous-title' ),
2373
+ ' - ' . $this->get_text( 'install-previous-desc' )
2374
+ ),
2375
+ sprintf(
2376
+ '<a href="%s"><b>%s</b></a>%s',
2377
+ wp_nonce_url( 'plugins.php?action=deactivate&amp;plugin=' . $this->_plugin_basename . '&amp;plugin_status=' . 'all' . '&amp;paged=' . '1' . '&amp;s=' . '', 'deactivate-plugin_' . $this->_plugin_basename ),
2378
+ $this->get_text( 'deactivate-plugin-title' ),
2379
+ ' - ' . $this->get_text( 'deactivate-plugin-desc' )
2380
+ )
2381
+ )
2382
+ );
2383
+ break;
2384
+ case 'squid_cache_block':
2385
+ $message = sprintf(
2386
+ $this->get_text( 'x-requires-access-to-api' ) . ' ' .
2387
+ $this->get_text( 'squid-blocks-connection-message' ) .
2388
+ ' %s',
2389
+ '<b>' . $this->get_plugin_name() . '</b>',
2390
+ sprintf(
2391
+ '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
2392
+ sprintf(
2393
+ '<a class="fs-resolve" data-type="squid" href="#"><b>%s</b></a>%s',
2394
+ $this->get_text( 'squid-no-clue-title' ),
2395
+ ' - ' . sprintf(
2396
+ $this->get_text( 'squid-no-clue-desc' ),
2397
+ '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
2398
+ )
2399
+ ),
2400
+ sprintf(
2401
+ '<b>%s</b> - %s',
2402
+ $this->get_text( 'sysadmin-title' ),
2403
+ sprintf(
2404
+ $this->get_text( 'squid-sysadmin-desc' ),
2405
+ // We use a filter since the plugin might require additional API connectivity.
2406
+ '<b>' . implode( ', ', $this->apply_filters( 'api_domains', array( 'api.freemius.com', 'wp.freemius.com' ) ) ) . '</b>',
2407
+ $this->_module_type
2408
+ )
2409
+ ),
2410
+ sprintf(
2411
+ '<a href="%s"><b>%s</b></a>%s',
2412
+ wp_nonce_url( 'plugins.php?action=deactivate&amp;plugin=' . $this->_plugin_basename . '&amp;plugin_status=' . 'all' . '&amp;paged=' . '1' . '&amp;s=' . '', 'deactivate-plugin_' . $this->_plugin_basename ),
2413
+ $this->get_text( 'deactivate-plugin-title' ),
2414
+ ' - ' . $this->get_text( 'deactivate-plugin-desc' )
2415
+ )
2416
+ )
2417
+ );
2418
+ break;
2419
+ // default:
2420
+ // $message = $this->get_text( 'connectivity-test-fails-message' );
2421
+ // break;
2422
+ }
2423
+ }
2424
+
2425
+ $message_id = 'failed_connect_api';
2426
+ $type = 'error';
2427
+
2428
+ if ( false === $message ) {
2429
+ if ( $is_first_failure ) {
2430
+ // First attempt failed.
2431
+ $message = sprintf(
2432
+ $this->get_text( 'x-requires-access-to-api' ) . ' ' .
2433
+ $this->get_text( 'connectivity-test-fails-message' ) . ' ' .
2434
+ $this->get_text( 'connectivity-test-maybe-temporary' ) . '<br><br>' .
2435
+ '%s',
2436
+ '<b>' . $this->get_plugin_name() . '</b>',
2437
+ sprintf(
2438
+ '<div id="fs_firewall_issue_options">%s %s</div>',
2439
+ sprintf(
2440
+ '<a class="button button-primary fs-resolve" data-type="retry_ping" href="#">%s</a>',
2441
+ $this->get_text( 'yes-do-your-thing' )
2442
+ ),
2443
+ sprintf(
2444
+ '<a href="%s" class="button">%s</a>',
2445
+ wp_nonce_url( 'plugins.php?action=deactivate&amp;plugin=' . $this->_plugin_basename . '&amp;plugin_status=' . 'all' . '&amp;paged=' . '1' . '&amp;s=' . '', 'deactivate-plugin_' . $this->_plugin_basename ),
2446
+ $this->get_text( 'no-deactivate' )
2447
+ )
2448
+ )
2449
+ );
2450
+
2451
+ $message_id = 'failed_connect_api_first';
2452
+ $type = 'promotion';
2453
+ } else {
2454
+ // Second connectivity attempt failed.
2455
+ $message = sprintf(
2456
+ $this->get_text( 'x-requires-access-to-api' ) . ' ' .
2457
+ $this->get_text( 'connectivity-test-fails-message' ) . ' ' .
2458
+ $this->get_text( 'happy-to-resolve-issue-asap' ) .
2459
+ ' %s',
2460
+ '<b>' . $this->get_plugin_name() . '</b>',
2461
+ sprintf(
2462
+ '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
2463
+ sprintf(
2464
+ '<a class="fs-resolve" data-type="general" href="#"><b>%s</b></a>%s',
2465
+ $this->get_text( 'fix-issue-title' ),
2466
+ ' - ' . sprintf(
2467
+ $this->get_text( 'fix-issue-desc' ),
2468
+ '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
2469
+ )
2470
+ ),
2471
+ sprintf(
2472
+ '<a href="%s" target="_blank"><b>%s</b></a>%s',
2473
+ sprintf( 'https://wordpress.org/plugins/%s/download/', $this->_slug ),
2474
+ $this->get_text( 'install-previous-title' ),
2475
+ ' - ' . $this->get_text( 'install-previous-desc' )
2476
+ ),
2477
+ sprintf(
2478
+ '<a href="%s"><b>%s</b></a>%s',
2479
+ wp_nonce_url( 'plugins.php?action=deactivate&amp;plugin=' . $this->_plugin_basename . '&amp;plugin_status=' . 'all' . '&amp;paged=' . '1' . '&amp;s=' . '', 'deactivate-plugin_' . $this->_plugin_basename ),
2480
+ $this->get_text( 'deactivate-plugin-title' ),
2481
+ ' - ' . $this->get_text( 'deactivate-plugin-desc' )
2482
+ )
2483
+ )
2484
+ );
2485
+ }
2486
+ }
2487
+
2488
+ $this->_admin_notices->add_sticky(
2489
+ $message,
2490
+ $message_id,
2491
+ $this->get_text( 'oops' ) . '...',
2492
+ $type
2493
+ );
2494
+ }
2495
+
2496
+ /**
2497
+ * Handle user request to resolve connectivity issue.
2498
+ * This method will send an email to Freemius API technical staff for resolution.
2499
+ * The email will contain server's info and installed plugins (might be caching issue).
2500
+ *
2501
+ * @author Vova Feldman (@svovaf)
2502
+ * @since 1.0.9
2503
+ */
2504
+ function _email_about_firewall_issue() {
2505
+ $this->_admin_notices->remove_sticky( 'failed_connect_api' );
2506
+
2507
+ $pong = $this->ping();
2508
+
2509
+ $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong );
2510
+
2511
+ if ( $is_connected ) {
2512
+ $this->store_connectivity_info( $pong, $is_connected );
2513
+
2514
+ echo $this->get_after_plugin_activation_redirect_url();
2515
+ exit;
2516
+ }
2517
+
2518
+ $current_user = self::_get_current_wp_user();
2519
+ $admin_email = $current_user->user_email;
2520
+
2521
+ $error_type = fs_request_get( 'error_type', 'general' );
2522
+
2523
+ switch ( $error_type ) {
2524
+ case 'squid':
2525
+ $title = 'Squid ACL Blocking Issue';
2526
+ break;
2527
+ case 'cloudflare':
2528
+ $title = 'CloudFlare Blocking Issue';
2529
+ break;
2530
+ default:
2531
+ $title = 'API Connectivity Issue';
2532
+ break;
2533
+ }
2534
+
2535
+ $custom_email_sections = array();
2536
+
2537
+ // Add 'API Error' custom email section.
2538
+ $custom_email_sections['api_error'] = array(
2539
+ 'title' => 'API Error',
2540
+ 'rows' => array(
2541
+ 'ping' => array(
2542
+ 'API Error',
2543
+ is_string( $pong ) ? htmlentities( $pong ) : json_encode( $pong )
2544
+ ),
2545
+ )
2546
+ );
2547
+
2548
+ // Send email with technical details to resolve API connectivity issues.
2549
+ $this->send_email(
2550
+ 'api@freemius.com', // recipient
2551
+ $title . ' [' . $this->get_plugin_name() . ']', // subject
2552
+ $custom_email_sections,
2553
+ array( "Reply-To: $admin_email <$admin_email>" ) // headers
2554
+ );
2555
+
2556
+ $this->_admin_notices->add_sticky(
2557
+ sprintf(
2558
+ $this->get_text( 'fix-request-sent-message' ),
2559
+ '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
2560
+ ),
2561
+ 'server_details_sent'
2562
+ );
2563
+
2564
+ // Action was taken, tell that API connectivity troubleshooting should be off now.
2565
+
2566
+ echo "1";
2567
+ exit;
2568
+ }
2569
+
2570
+ /**
2571
+ * Handle connectivity test retry approved by the user.
2572
+ *
2573
+ * @author Vova Feldman (@svovaf)
2574
+ * @since 1.1.7.4
2575
+ */
2576
+ function _retry_connectivity_test() {
2577
+ $this->_admin_notices->remove_sticky( 'failed_connect_api_first' );
2578
+
2579
+ $pong = $this->ping();
2580
+
2581
+ $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong );
2582
+
2583
+ if ( $is_connected ) {
2584
+ $this->store_connectivity_info( $pong, $is_connected );
2585
+
2586
+ echo $this->get_after_plugin_activation_redirect_url();
2587
+ } else {
2588
+ // Add connectivity issue message after 2nd failed attempt.
2589
+ $this->_add_connectivity_issue_message( $pong, false );
2590
+
2591
+ echo "1";
2592
+ }
2593
+
2594
+ exit;
2595
+ }
2596
+
2597
+ static function _add_firewall_issues_javascript() {
2598
+ $params = array();
2599
+ fs_require_once_template( 'firewall-issues-js.php', $params );
2600
+ }
2601
+
2602
+ #endregion
2603
+
2604
+ #----------------------------------------------------------------------------------
2605
+ #region Email
2606
+ #----------------------------------------------------------------------------------
2607
+
2608
+ /**
2609
+ * Generates and sends an HTML email with customizable sections.
2610
+ *
2611
+ * @author Leo Fajardo (@leorw)
2612
+ * @since 1.1.2
2613
+ *
2614
+ * @param string $to_address
2615
+ * @param string $subject
2616
+ * @param array $sections
2617
+ * @param array $headers
2618
+ *
2619
+ * @return bool Whether the email contents were sent successfully.
2620
+ */
2621
+ private function send_email(
2622
+ $to_address,
2623
+ $subject,
2624
+ $sections = array(),
2625
+ $headers = array()
2626
+ ) {
2627
+ $default_sections = $this->get_email_sections();
2628
+
2629
+ // Insert new sections or replace the default email sections.
2630
+ if ( is_array( $sections ) && ! empty( $sections ) ) {
2631
+ foreach ( $sections as $section_id => $custom_section ) {
2632
+ if ( ! isset( $default_sections[ $section_id ] ) ) {
2633
+ // If the section does not exist, add it.
2634
+ $default_sections[ $section_id ] = $custom_section;
2635
+ } else {
2636
+ // If the section already exists, override it.
2637
+ $current_section = $default_sections[ $section_id ];
2638
+
2639
+ // Replace the current section's title if a custom section title exists.
2640
+ if ( isset( $custom_section['title'] ) ) {
2641
+ $current_section['title'] = $custom_section['title'];
2642
+ }
2643
+
2644
+ // Insert new rows under the current section or replace the default rows.
2645
+ if ( isset( $custom_section['rows'] ) && is_array( $custom_section['rows'] ) && ! empty( $custom_section['rows'] ) ) {
2646
+ foreach ( $custom_section['rows'] as $row_id => $row ) {
2647
+ $current_section['rows'][ $row_id ] = $row;
2648
+ }
2649
+ }
2650
+
2651
+ $default_sections[ $section_id ] = $current_section;
2652
+ }
2653
+ }
2654
+ }
2655
+
2656
+ $vars = array( 'sections' => $default_sections );
2657
+ $message = fs_get_template( 'email.php', $vars );
2658
+
2659
+ // Set the type of email to HTML.
2660
+ $headers[] = 'Content-type: text/html; charset=UTF-8';
2661
+
2662
+ $header_string = implode( "\r\n", $headers );
2663
+
2664
+ return wp_mail(
2665
+ $to_address,
2666
+ $subject,
2667
+ $message,
2668
+ $header_string
2669
+ );
2670
+ }
2671
+
2672
+ /**
2673
+ * Generates the data for the sections of the email content.
2674
+ *
2675
+ * @author Leo Fajardo (@leorw)
2676
+ * @since 1.1.2
2677
+ *
2678
+ * @return array
2679
+ */
2680
+ private function get_email_sections() {
2681
+ // Retrieve the current user's information so that we can get the user's email, first name, and last name below.
2682
+ $current_user = self::_get_current_wp_user();
2683
+
2684
+ // Retrieve the cURL version information so that we can get the version number below.
2685
+ $curl_version_information = curl_version();
2686
+
2687
+ $active_plugin = self::get_active_plugins();
2688
+
2689
+ // Generate the list of active plugins separated by new line.
2690
+ $active_plugin_string = '';
2691
+ foreach ( $active_plugin as $plugin ) {
2692
+ $active_plugin_string .= sprintf(
2693
+ '<a href="%s">%s</a> [v%s]<br>',
2694
+ $plugin['PluginURI'],
2695
+ $plugin['Name'],
2696
+ $plugin['Version']
2697
+ );
2698
+ }
2699
+
2700
+ $server_ip = WP_FS__REMOTE_ADDR;
2701
+
2702
+ // Add PHP info for deeper investigation.
2703
+ ob_start();
2704
+ phpinfo();
2705
+ $php_info = ob_get_clean();
2706
+
2707
+ $api_domain = substr( FS_API__ADDRESS, strpos( FS_API__ADDRESS, ':' ) + 3 );
2708
+
2709
+ // Generate the default email sections.
2710
+ $sections = array(
2711
+ 'sdk' => array(
2712
+ 'title' => 'SDK',
2713
+ 'rows' => array(
2714
+ 'fs_version' => array( 'FS Version', $this->version ),
2715
+ 'curl_version' => array( 'cURL Version', $curl_version_information['version'] )
2716
+ )
2717
+ ),
2718
+ 'plugin' => array(
2719
+ 'title' => ucfirst( $this->get_module_type() ),
2720
+ 'rows' => array(
2721
+ 'name' => array( 'Name', $this->get_plugin_name() ),
2722
+ 'version' => array( 'Version', $this->get_plugin_version() )
2723
+ )
2724
+ ),
2725
+ 'api' => array(
2726
+ 'title' => 'API Subdomain',
2727
+ 'rows' => array(
2728
+ 'dns' => array(
2729
+ 'DNS_CNAME',
2730
+ function_exists( 'dns_get_record' ) ?
2731
+ var_export( dns_get_record( $api_domain, DNS_CNAME ), true ) :
2732
+ 'dns_get_record() disabled/blocked'
2733
+ ),
2734
+ 'ip' => array(
2735
+ 'IP',
2736
+ function_exists( 'gethostbyname' ) ?
2737
+ gethostbyname( $api_domain ) :
2738
+ 'gethostbyname() disabled/blocked'
2739
+ ),
2740
+ ),
2741
+ ),
2742
+ 'site' => array(
2743
+ 'title' => 'Site',
2744
+ 'rows' => array(
2745
+ 'unique_id' => array( 'Unique ID', $this->get_anonymous_id() ),
2746
+ 'address' => array( 'Address', site_url() ),
2747
+ 'host' => array(
2748
+ 'HTTP_HOST',
2749
+ ( ! empty( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : '' )
2750
+ ),
2751
+ 'hosting' => array(
2752
+ 'Hosting Company' => fs_request_has( 'hosting_company' ) ?
2753
+ fs_request_get( 'hosting_company' ) :
2754
+ 'Unknown',
2755
+ ),
2756
+ 'server_addr' => array(
2757
+ 'SERVER_ADDR',
2758
+ '<a href="http://www.projecthoneypot.org/ip_' . $server_ip . '">' . $server_ip . '</a>'
2759
+ )
2760
+ )
2761
+ ),
2762
+ 'user' => array(
2763
+ 'title' => 'User',
2764
+ 'rows' => array(
2765
+ 'email' => array( 'Email', $current_user->user_email ),
2766
+ 'first' => array( 'First', $current_user->user_firstname ),
2767
+ 'last' => array( 'Last', $current_user->user_lastname )
2768
+ )
2769
+ ),
2770
+ 'plugins' => array(
2771
+ 'title' => 'Plugins',
2772
+ 'rows' => array(
2773
+ 'active_plugins' => array( 'Active Plugins', $active_plugin_string )
2774
+ )
2775
+ ),
2776
+ 'php_info' => array(
2777
+ 'title' => 'PHP Info',
2778
+ 'rows' => array(
2779
+ 'info' => array( $php_info )
2780
+ ),
2781
+ )
2782
+ );
2783
+
2784
+ // Allow the sections to be modified by other code.
2785
+ $sections = $this->apply_filters( 'email_template_sections', $sections );
2786
+
2787
+ return $sections;
2788
+ }
2789
+
2790
+ #endregion
2791
+
2792
+ #----------------------------------------------------------------------------------
2793
+ #region Initialization
2794
+ #----------------------------------------------------------------------------------
2795
+
2796
+ /**
2797
+ * Init plugin's Freemius instance.
2798
+ *
2799
+ * @author Vova Feldman (@svovaf)
2800
+ * @since 1.0.1
2801
+ *
2802
+ * @param number $id
2803
+ * @param string $public_key
2804
+ * @param bool $is_live
2805
+ * @param bool $is_premium
2806
+ */
2807
+ function init( $id, $public_key, $is_live = true, $is_premium = true ) {
2808
+ $this->_logger->entrance();
2809
+
2810
+ $this->dynamic_init( array(
2811
+ 'id' => $id,
2812
+ 'public_key' => $public_key,
2813
+ 'is_live' => $is_live,
2814
+ 'is_premium' => $is_premium,
2815
+ ) );
2816
+ }
2817
+
2818
+ /**
2819
+ * Dynamic initiator, originally created to support initiation
2820
+ * with parent_id for add-ons.
2821
+ *
2822
+ * @author Vova Feldman (@svovaf)
2823
+ * @since 1.0.6
2824
+ *
2825
+ * @param array $plugin_info
2826
+ *
2827
+ * @throws Freemius_Exception
2828
+ */
2829
+ function dynamic_init( array $plugin_info ) {
2830
+ $this->_logger->entrance();
2831
+
2832
+ $this->parse_settings( $plugin_info );
2833
+
2834
+ if ( ! self::is_ajax() ) {
2835
+ if ( ! $this->is_addon() || $this->is_only_premium() ) {
2836
+ add_action( 'admin_menu', array( &$this, '_prepare_admin_menu' ), WP_FS__LOWEST_PRIORITY );
2837
+ }
2838
+ }
2839
+
2840
+ if ( $this->should_stop_execution() ) {
2841
+ return;
2842
+ }
2843
+
2844
+ if ( ! $this->is_registered() ) {
2845
+ if ( $this->is_anonymous() ) {
2846
+ // If user skipped, no need to test connectivity.
2847
+ $this->_has_api_connection = true;
2848
+ $this->_is_on = true;
2849
+ } else {
2850
+ if ( ! $this->has_api_connectivity() ) {
2851
+ if ( $this->_admin_notices->has_sticky( 'failed_connect_api_first' ) ||
2852
+ $this->_admin_notices->has_sticky( 'failed_connect_api' )
2853
+ ) {
2854
+ if ( ! $this->_enable_anonymous || $this->is_premium() ) {
2855
+ // If anonymous mode is disabled, add firewall admin-notice message.
2856
+ add_action( 'admin_footer', array( 'Freemius', '_add_firewall_issues_javascript' ) );
2857
+
2858
+ $this->add_ajax_action( 'resolve_firewall_issues', array(
2859
+ &$this,
2860
+ '_email_about_firewall_issue'
2861
+ ) );
2862
+
2863
+ $this->add_ajax_action( 'retry_connectivity_test', array(
2864
+ &$this,
2865
+ '_retry_connectivity_test'
2866
+ ) );
2867
+ }
2868
+ }
2869
+
2870
+ return;
2871
+ } else {
2872
+ $this->_admin_notices->remove_sticky( array(
2873
+ 'failed_connect_api_first',
2874
+ 'failed_connect_api',
2875
+ ) );
2876
+
2877
+ if ( $this->_anonymous_mode ) {
2878
+ // Simulate anonymous mode.
2879
+ $this->_is_anonymous = true;
2880
+ }
2881
+ }
2882
+ }
2883
+
2884
+ // Check if Freemius is on for the current plugin.
2885
+ // This MUST be executed after all the plugin variables has been loaded.
2886
+ if ( ! $this->is_on() ) {
2887
+ return;
2888
+ }
2889
+ }
2890
+
2891
+ if ( $this->has_api_connectivity() ) {
2892
+ if ( self::is_cron() ) {
2893
+ $this->hook_callback_to_sync_cron();
2894
+ } else if ( $this->is_user_in_admin() ) {
2895
+ /**
2896
+ * Schedule daily data sync cron if:
2897
+ *
2898
+ * 1. User opted-in (for tracking).
2899
+ * 2. If skipped, but later upgraded (opted-in via upgrade).
2900
+ *
2901
+ * @author Vova Feldman (@svovaf)
2902
+ * @since 1.1.7.3
2903
+ *
2904
+ */
2905
+ if ( $this->is_registered() ) {
2906
+ if ( ! $this->is_sync_cron_on() && $this->is_tracking_allowed() ) {
2907
+ $this->schedule_sync_cron();
2908
+ }
2909
+ }
2910
+
2911
+ /**
2912
+ * Check if requested for manual blocking background sync.
2913
+ */
2914
+ if ( fs_request_has( 'background_sync' ) ) {
2915
+ $this->run_manual_sync();
2916
+ }
2917
+ }
2918
+ }
2919
+
2920
+ if ( $this->is_registered() ) {
2921
+ $this->hook_callback_to_install_sync();
2922
+ }
2923
+
2924
+ if ( $this->is_addon() ) {
2925
+ if ( $this->is_parent_plugin_installed() ) {
2926
+ // Link to parent FS.
2927
+ $this->_parent = self::get_instance_by_id( $this->_plugin->parent_plugin_id );
2928
+
2929
+ // Get parent plugin reference.
2930
+ $this->_parent_plugin = $this->_parent->get_plugin();
2931
+ }
2932
+ }
2933
+
2934
+ if ( $this->is_user_in_admin() ) {
2935
+ if ( self::is_plugins_page() && $this->is_plugin() ) {
2936
+ $this->hook_plugin_action_links();
2937
+ }
2938
+
2939
+ if ( $this->is_addon() ) {
2940
+ if ( ! $this->is_parent_plugin_installed() ) {
2941
+ $parent_name = $this->get_option( $plugin_info, 'parent_name', null );
2942
+
2943
+ if ( isset( $plugin_info['parent'] ) ) {
2944
+ $parent_name = $this->get_option( $plugin_info['parent'], 'name', null );
2945
+ }
2946
+
2947
+ $this->_admin_notices->add(
2948
+ ( ! empty( $parent_name ) ?
2949
+ sprintf( $this->get_text( 'addon-x-cannot-run-without-y' ), $this->get_plugin_name(), $parent_name ) :
2950
+ sprintf( $this->get_text( 'addon-x-cannot-run-without-parent' ), $this->get_plugin_name() )
2951
+ ),
2952
+ $this->get_text( 'oops' ) . '...',
2953
+ 'error'
2954
+ );
2955
+
2956
+ return;
2957
+ } else {
2958
+ if ( $this->_parent->is_registered() && ! $this->is_registered() ) {
2959
+ // If parent plugin activated, automatically install add-on for the user.
2960
+ $this->_activate_addon_account( $this->_parent );
2961
+ } else if ( ! $this->_parent->is_registered() && $this->is_registered() ) {
2962
+ // If add-on activated and parent not, automatically install parent for the user.
2963
+ $this->activate_parent_account( $this->_parent );
2964
+ }
2965
+
2966
+ // @todo This should be only executed on activation. It should be migrated to register_activation_hook() together with other activation related logic.
2967
+ if ( $this->is_premium() ) {
2968
+ // Remove add-on download admin-notice.
2969
+ $this->_parent->_admin_notices->remove_sticky( array(
2970
+ 'addon_plan_upgraded_' . $this->_slug,
2971
+ 'no_addon_license_' . $this->_slug,
2972
+ ) );
2973
+ }
2974
+
2975
+ // $this->deactivate_premium_only_addon_without_license();
2976
+ }
2977
+ } else {
2978
+ if ( $this->has_addons() &&
2979
+ 'plugin-information' === fs_request_get( 'tab', false ) &&
2980
+ $this->get_id() == fs_request_get( 'parent_plugin_id', false )
2981
+ ) {
2982
+ require_once WP_FS__DIR_INCLUDES . '/fs-plugin-info-dialog.php';
2983
+
2984
+ new FS_Plugin_Info_Dialog( $this );
2985
+ }
2986
+ }
2987
+
2988
+ add_action( 'admin_init', array( &$this, '_admin_init_action' ) );
2989
+
2990
+ // if ( $this->is_registered() ||
2991
+ // $this->is_anonymous() ||
2992
+ // $this->is_pending_activation()
2993
+ // ) {
2994
+ // $this->_init_admin();
2995
+ // }
2996
+ }
2997
+
2998
+ /**
2999
+ * Should be called outside `$this->is_user_in_admin()` scope
3000
+ * because the updater has some logic that needs to be executed
3001
+ * during AJAX calls.
3002
+ *
3003
+ * Currently we need to hook to the `http_request_host_is_external` filter.
3004
+ * In the future, there might be additional logic added.
3005
+ *
3006
+ * @author Vova Feldman
3007
+ * @since 1.2.1.6
3008
+ */
3009
+ if ( $this->is_premium() && $this->has_release_on_freemius() ) {
3010
+ new FS_Plugin_Updater( $this );
3011
+ }
3012
+
3013
+ $this->do_action( 'initiated' );
3014
+
3015
+ if ( $this->_storage->prev_is_premium !== $this->_plugin->is_premium ) {
3016
+ if ( isset( $this->_storage->prev_is_premium ) ) {
3017
+ $this->apply_filters(
3018
+ 'after_code_type_change',
3019
+ // New code type.
3020
+ $this->_plugin->is_premium
3021
+ );
3022
+ } else {
3023
+ // Set for code type for the first time.
3024
+ $this->_storage->prev_is_premium = $this->_plugin->is_premium;
3025
+ }
3026
+ }
3027
+
3028
+ if ( ! $this->is_addon() ) {
3029
+ if ( $this->is_registered() ) {
3030
+ // Fix for upgrade from versions < 1.0.9.
3031
+ if ( ! isset( $this->_storage->activation_timestamp ) ) {
3032
+ $this->_storage->activation_timestamp = WP_FS__SCRIPT_START_TIME;
3033
+ }
3034
+
3035
+ $this->do_action( 'after_init_plugin_registered' );
3036
+ } else if ( $this->is_anonymous() ) {
3037
+ $this->do_action( 'after_init_plugin_anonymous' );
3038
+ } else if ( $this->is_pending_activation() ) {
3039
+ $this->do_action( 'after_init_plugin_pending_activations' );
3040
+ }
3041
+ } else {
3042
+ if ( $this->is_registered() ) {
3043
+ $this->do_action( 'after_init_addon_registered' );
3044
+ } else if ( $this->is_anonymous() ) {
3045
+ $this->do_action( 'after_init_addon_anonymous' );
3046
+ } else if ( $this->is_pending_activation() ) {
3047
+ $this->do_action( 'after_init_addon_pending_activations' );
3048
+ }
3049
+ }
3050
+ }
3051
+
3052
+ /**
3053
+ * @author Leo Fajardo (@leorw)
3054
+ *
3055
+ * @since 1.2.1.5
3056
+ */
3057
+ function _stop_tracking_callback() {
3058
+ $this->_logger->entrance();
3059
+
3060
+ $this->check_ajax_referer( 'stop_tracking' );
3061
+
3062
+ $result = $this->stop_tracking();
3063
+
3064
+ if ( true === $result ) {
3065
+ self::shoot_ajax_success();
3066
+ }
3067
+
3068
+ $this->_logger->api_error( $result );
3069
+
3070
+ self::shoot_ajax_failure(
3071
+ $this->get_text( 'unexpected-api-error' ) .
3072
+ ( $this->is_api_error( $result ) && isset( $result->error ) ?
3073
+ $result->error->message :
3074
+ var_export( $result, true ) )
3075
+ );
3076
+ }
3077
+
3078
+ /**
3079
+ * @author Leo Fajardo (@leorw)
3080
+ * @since 1.2.1.5
3081
+ */
3082
+ function _allow_tracking_callback() {
3083
+ $this->_logger->entrance();
3084
+
3085
+ $this->check_ajax_referer( 'allow_tracking' );
3086
+
3087
+ $result = $this->allow_tracking();
3088
+
3089
+ if ( true === $result ) {
3090
+ self::shoot_ajax_success();
3091
+ }
3092
+
3093
+ $this->_logger->api_error( $result );
3094
+
3095
+ self::shoot_ajax_failure(
3096
+ $this->get_text( 'unexpected-api-error' ) .
3097
+ ( $this->is_api_error( $result ) && isset( $result->error ) ?
3098
+ $result->error->message :
3099
+ var_export( $result, true ) )
3100
+ );
3101
+ }
3102
+
3103
+ /**
3104
+ * Opt-out from usage tracking.
3105
+ *
3106
+ * Note: This will not delete the account information but will stop all tracking.
3107
+ *
3108
+ * Returns:
3109
+ * 1. FALSE - If the user never opted-in.
3110
+ * 2. TRUE - If successfully opted-out.
3111
+ * 3. object - API result on failure.
3112
+ *
3113
+ * @author Leo Fajardo (@leorw)
3114
+ * @since 1.2.1.5
3115
+ *
3116
+ * @return bool|object
3117
+ */
3118
+ function stop_tracking() {
3119
+ $this->_logger->entrance();
3120
+
3121
+ if ( ! $this->is_registered() ) {
3122
+ // User never opted-in.
3123
+ return false;
3124
+ }
3125
+
3126
+ if ( $this->is_tracking_prohibited() ) {
3127
+ // Already disconnected.
3128
+ return true;
3129
+ }
3130
+
3131
+ // Send update to FS.
3132
+ $result = $this->get_api_site_scope()->call( '/?fields=is_disconnected', 'put', array(
3133
+ 'is_disconnected' => true
3134
+ ) );
3135
+
3136
+ if ( ! $this->is_api_result_entity( $result ) ||
3137
+ ! isset( $result->is_disconnected ) ||
3138
+ ! $result->is_disconnected
3139
+ ) {
3140
+ $this->_logger->api_error( $result );
3141
+
3142
+ return $result;
3143
+ }
3144
+
3145
+ $this->_site->is_disconnected = $result->is_disconnected;
3146
+ $this->_store_site();
3147
+
3148
+ $this->clear_sync_cron();
3149
+
3150
+ // Successfully disconnected.
3151
+ return true;
3152
+ }
3153
+
3154
+ /**
3155
+ * Opt-in back into usage tracking.
3156
+ *
3157
+ * Note: This will only work if the user opted-in previously.
3158
+ *
3159
+ * Returns:
3160
+ * 1. FALSE - If the user never opted-in.
3161
+ * 2. TRUE - If successfully opted-in back to usage tracking.
3162
+ * 3. object - API result on failure.
3163
+ *
3164
+ * @author Leo Fajardo (@leorw)
3165
+ * @since 1.2.1.5
3166
+ *
3167
+ * @return bool|object
3168
+ */
3169
+ function allow_tracking() {
3170
+ $this->_logger->entrance();
3171
+
3172
+ if ( ! $this->is_registered() ) {
3173
+ // User never opted-in.
3174
+ return false;
3175
+ }
3176
+
3177
+ if ( $this->is_tracking_allowed() ) {
3178
+ // Tracking already allowed.
3179
+ return true;
3180
+ }
3181
+
3182
+ $result = $this->get_api_site_scope()->call( '/?is_disconnected', 'put', array(
3183
+ 'is_disconnected' => false
3184
+ ) );
3185
+
3186
+ if ( ! $this->is_api_result_entity( $result ) ||
3187
+ ! isset( $result->is_disconnected ) ||
3188
+ $result->is_disconnected
3189
+ ) {
3190
+ $this->_logger->api_error( $result );
3191
+
3192
+ return $result;
3193
+ }
3194
+
3195
+ $this->_site->is_disconnected = $result->is_disconnected;
3196
+ $this->_store_site();
3197
+
3198
+ $this->schedule_sync_cron();
3199
+
3200
+ // Successfully reconnected.
3201
+ return true;
3202
+ }
3203
+
3204
+ /**
3205
+ * If user opted-in and later disabled usage-tracking,
3206
+ * re-allow tracking for licensing and updates.
3207
+ *
3208
+ * @author Leo Fajardo (@leorw)
3209
+ *
3210
+ * @since 1.2.1.5
3211
+ */
3212
+ private function reconnect_locally() {
3213
+ $this->_logger->entrance();
3214
+
3215
+ if ( $this->is_tracking_prohibited() &&
3216
+ $this->is_registered()
3217
+ ) {
3218
+ $this->_site->is_disconnected = false;
3219
+ $this->_store_site();
3220
+ }
3221
+ }
3222
+
3223
+ /**
3224
+ * Parse plugin's settings (as defined by the plugin dev).
3225
+ *
3226
+ * @author Vova Feldman (@svovaf)
3227
+ * @since 1.1.7.3
3228
+ *
3229
+ * @param array $plugin_info
3230
+ *
3231
+ * @throws \Freemius_Exception
3232
+ */
3233
+ private function parse_settings( &$plugin_info ) {
3234
+ $this->_logger->entrance();
3235
+
3236
+ $id = $this->get_numeric_option( $plugin_info, 'id', false );
3237
+ $public_key = $this->get_option( $plugin_info, 'public_key', false );
3238
+ $secret_key = $this->get_option( $plugin_info, 'secret_key', null );
3239
+ $parent_id = $this->get_numeric_option( $plugin_info, 'parent_id', null );
3240
+ $parent_name = $this->get_option( $plugin_info, 'parent_name', null );
3241
+
3242
+ /**
3243
+ * @author Vova Feldman (@svovaf)
3244
+ * @since 1.1.9 Try to pull secret key from external config.
3245
+ */
3246
+ if ( is_null( $secret_key ) && defined( "WP_FS__{$this->_slug}_SECRET_KEY" ) ) {
3247
+ $secret_key = constant( "WP_FS__{$this->_slug}_SECRET_KEY" );
3248
+ }
3249
+
3250
+ if ( isset( $plugin_info['parent'] ) ) {
3251
+ $parent_id = $this->get_numeric_option( $plugin_info['parent'], 'id', null );
3252
+ // $parent_slug = $this->get_option( $plugin_info['parent'], 'slug', null );
3253
+ // $parent_public_key = $this->get_option( $plugin_info['parent'], 'public_key', null );
3254
+ // $parent_name = $this->get_option( $plugin_info['parent'], 'name', null );
3255
+ }
3256
+
3257
+ if ( false === $id ) {
3258
+ throw new Freemius_Exception( array(
3259
+ 'error' => array(
3260
+ 'type' => 'ParameterNotSet',
3261
+ 'message' => 'Plugin id parameter is not set.',
3262
+ 'code' => 'plugin_id_not_set',
3263
+ 'http' => 500,
3264
+ )
3265
+ ) );
3266
+ }
3267
+ if ( false === $public_key ) {
3268
+ throw new Freemius_Exception( array(
3269
+ 'error' => array(
3270
+ 'type' => 'ParameterNotSet',
3271
+ 'message' => 'Plugin public_key parameter is not set.',
3272
+ 'code' => 'plugin_public_key_not_set',
3273
+ 'http' => 500,
3274
+ )
3275
+ ) );
3276
+ }
3277
+
3278
+ $plugin = ( $this->_plugin instanceof FS_Plugin ) ?
3279
+ $this->_plugin :
3280
+ new FS_Plugin();
3281
+
3282
+ $plugin->update( array(
3283
+ 'id' => $id,
3284
+ 'public_key' => $public_key,
3285
+ 'slug' => $this->_slug,
3286
+ 'parent_plugin_id' => $parent_id,
3287
+ 'version' => $this->get_plugin_version(),
3288
+ 'title' => $this->get_plugin_name(),
3289
+ 'file' => $this->_plugin_basename,
3290
+ 'is_premium' => $this->get_bool_option( $plugin_info, 'is_premium', true ),
3291
+ 'is_live' => $this->get_bool_option( $plugin_info, 'is_live', true ),
3292
+ 'type' => $this->_module_type,
3293
+ // 'secret_key' => $secret_key,
3294
+ ) );
3295
+
3296
+ if ( $plugin->is_updated() ) {
3297
+ // Update plugin details.
3298
+ $this->_plugin = FS_Plugin_Manager::instance( $this->_module_id )->store( $plugin );
3299
+ }
3300
+ // Set the secret key after storing the plugin, we don't want to store the key in the storage.
3301
+ $this->_plugin->secret_key = $secret_key;
3302
+
3303
+ if ( ! isset( $plugin_info['menu'] ) ) {
3304
+ $plugin_info['menu'] = array();
3305
+
3306
+ if ( ! empty( $this->_storage->sdk_last_version ) &&
3307
+ version_compare( $this->_storage->sdk_last_version, '1.1.2', '<=' )
3308
+ ) {
3309
+ // Backward compatibility to 1.1.2
3310
+ $plugin_info['menu']['slug'] = isset( $plugin_info['menu_slug'] ) ?
3311
+ $plugin_info['menu_slug'] :
3312
+ $this->_slug;
3313
+ }
3314
+ }
3315
+
3316
+ $this->_menu = FS_Admin_Menu_Manager::instance(
3317
+ $this->_module_id,
3318
+ $this->_module_type,
3319
+ $this->get_unique_affix()
3320
+ );
3321
+
3322
+ $this->_menu->init( $plugin_info['menu'], $this->is_addon() );
3323
+
3324
+ $this->_has_addons = $this->get_bool_option( $plugin_info, 'has_addons', false );
3325
+ $this->_has_paid_plans = $this->get_bool_option( $plugin_info, 'has_paid_plans', true );
3326
+ $this->_has_premium_version = $this->get_bool_option( $plugin_info, 'has_premium_version', $this->_has_paid_plans );
3327
+ $this->_ignore_pending_mode = $this->get_bool_option( $plugin_info, 'ignore_pending_mode', false );
3328
+ $this->_is_org_compliant = $this->get_bool_option( $plugin_info, 'is_org_compliant', true );
3329
+ $this->_is_premium_only = $this->get_bool_option( $plugin_info, 'is_premium_only', false );
3330
+ if ( $this->_is_premium_only ) {
3331
+ // If premium only plugin, disable anonymous mode.
3332
+ $this->_enable_anonymous = false;
3333
+ $this->_anonymous_mode = false;
3334
+ } else {
3335
+ $this->_enable_anonymous = $this->get_bool_option( $plugin_info, 'enable_anonymous', true );
3336
+ $this->_anonymous_mode = $this->get_bool_option( $plugin_info, 'anonymous_mode', false );
3337
+ }
3338
+ $this->_permissions = $this->get_option( $plugin_info, 'permissions', array() );
3339
+
3340
+ if ( ! empty( $plugin_info['trial'] ) ) {
3341
+ $this->_trial_days = $this->get_numeric_option(
3342
+ $plugin_info['trial'],
3343
+ 'days',
3344
+ // Default to 0 - trial without days specification.
3345
+ 0
3346
+ );
3347
+
3348
+ $this->_is_trial_require_payment = $this->get_bool_option( $plugin_info['trial'], 'is_require_payment', false );
3349
+ }
3350
+ }
3351
+
3352
+ /**
3353
+ * @param string[] $options
3354
+ * @param string $key
3355
+ * @param mixed $default
3356
+ *
3357
+ * @return bool
3358
+ */
3359
+ private function get_option( &$options, $key, $default = false ) {
3360
+ return ! empty( $options[ $key ] ) ? $options[ $key ] : $default;
3361
+ }
3362
+
3363
+ private function get_bool_option( &$options, $key, $default = false ) {
3364
+ return isset( $options[ $key ] ) && is_bool( $options[ $key ] ) ? $options[ $key ] : $default;
3365
+ }
3366
+
3367
+ private function get_numeric_option( &$options, $key, $default = false ) {
3368
+ return isset( $options[ $key ] ) && is_numeric( $options[ $key ] ) ? $options[ $key ] : $default;
3369
+ }
3370
+
3371
+ /**
3372
+ * Gate keeper.
3373
+ *
3374
+ * @author Vova Feldman (@svovaf)
3375
+ * @since 1.1.7.3
3376
+ *
3377
+ * @return bool
3378
+ */
3379
+ private function should_stop_execution() {
3380
+ if ( empty( $this->_storage->was_plugin_loaded ) ) {
3381
+ /**
3382
+ * Don't execute Freemius until plugin was fully loaded at least once,
3383
+ * to give the opportunity for the activation hook to run before pinging
3384
+ * the API for connectivity test. This logic is relevant for the
3385
+ * identification of new plugin install vs. plugin update.
3386
+ *
3387
+ * @author Vova Feldman (@svovaf)
3388
+ * @since 1.1.9
3389
+ */
3390
+ return true;
3391
+ }
3392
+
3393
+ if ( $this->is_activation_mode() ) {
3394
+ if ( ! is_admin() ) {
3395
+ /**
3396
+ * If in activation mode, don't execute Freemius outside of the
3397
+ * admin dashboard.
3398
+ *
3399
+ * @author Vova Feldman (@svovaf)
3400
+ * @since 1.1.7.3
3401
+ */
3402
+ return true;
3403
+ }
3404
+
3405
+ if ( ! WP_FS__IS_HTTP_REQUEST ) {
3406
+ /**
3407
+ * If in activation and executed without HTTP context (e.g. CLI, Cronjob),
3408
+ * then don't start Freemius.
3409
+ *
3410
+ * @author Vova Feldman (@svovaf)
3411
+ * @since 1.1.6.3
3412
+ *
3413
+ * @link https://wordpress.org/support/topic/errors-in-the-freemius-class-when-running-in-wordpress-in-cli
3414
+ */
3415
+ return true;
3416
+ }
3417
+
3418
+ if ( self::is_cron() ) {
3419
+ /**
3420
+ * If in activation mode, don't execute Freemius during wp crons
3421
+ * (wp crons have HTTP context - called as HTTP request).
3422
+ *
3423
+ * @author Vova Feldman (@svovaf)
3424
+ * @since 1.1.7.3
3425
+ */
3426
+ return true;
3427
+ }
3428
+
3429
+ if ( self::is_ajax() &&
3430
+ ! $this->_admin_notices->has_sticky( 'failed_connect_api_first' ) &&
3431
+ ! $this->_admin_notices->has_sticky( 'failed_connect_api' )
3432
+ ) {
3433
+ /**
3434
+ * During activation, if running in AJAX mode, unless there's a sticky
3435
+ * connectivity issue notice, don't run Freemius.
3436
+ *
3437
+ * @author Vova Feldman (@svovaf)
3438
+ * @since 1.1.7.3
3439
+ */
3440
+ return true;
3441
+ }
3442
+ }
3443
+
3444
+ return false;
3445
+ }
3446
+
3447
+ /**
3448
+ * Triggered after code type has changed.
3449
+ *
3450
+ * @author Vova Feldman (@svovaf)
3451
+ * @since 1.1.9.1
3452
+ */
3453
+ function _after_code_type_change() {
3454
+ $this->_logger->entrance();
3455
+
3456
+ if ( $this->is_theme() ) {
3457
+ // Expire the cache of the previous tabs since the theme may
3458
+ // have setting updates after code type has changed.
3459
+ $this->_cache->expire( 'tabs' );
3460
+ $this->_cache->expire( 'tabs_stylesheets' );
3461
+ }
3462
+
3463
+ if ( $this->is_registered() ) {
3464
+ if ( ! $this->is_addon() ) {
3465
+ add_action(
3466
+ is_admin() ? 'admin_init' : 'init',
3467
+ array( &$this, '_plugin_code_type_changed' )
3468
+ );
3469
+ }
3470
+
3471
+ if ( $this->is_premium() ) {
3472
+ // Purge cached payments after switching to the premium version.
3473
+ // @todo This logic doesn't handle purging the cache for serviceware module upgrade.
3474
+ $this->get_api_user_scope()->purge_cache( "/plugins/{$this->_module_id}/payments.json?include_addons=true" );
3475
+ }
3476
+ }
3477
+ }
3478
+
3479
+ /**
3480
+ * Handles plugin's code type change (free <--> premium).
3481
+ *
3482
+ * @author Vova Feldman (@svovaf)
3483
+ * @since 1.0.9
3484
+ */
3485
+ function _plugin_code_type_changed() {
3486
+ $this->_logger->entrance();
3487
+
3488
+ if ( $this->is_premium() ) {
3489
+ $this->reconnect_locally();
3490
+
3491
+ // Activated premium code.
3492
+ $this->do_action( 'after_premium_version_activation' );
3493
+
3494
+ // Remove all sticky messages related to download of the premium version.
3495
+ $this->_admin_notices->remove_sticky( array(
3496
+ 'trial_started',
3497
+ 'plan_upgraded',
3498
+ 'plan_changed',
3499
+ 'license_activated',
3500
+ ) );
3501
+
3502
+ $this->_admin_notices->add_sticky(
3503
+ sprintf( $this->get_text( 'premium-activated-message' ), $this->_module_type ),
3504
+ 'premium_activated',
3505
+ $this->get_text( 'woot' ) . '!'
3506
+ );
3507
+ } else {
3508
+ // Remove sticky message related to premium code activation.
3509
+ $this->_admin_notices->remove_sticky( 'premium_activated' );
3510
+
3511
+ // Activated free code (after had the premium before).
3512
+ $this->do_action( 'after_free_version_reactivation' );
3513
+
3514
+ if ( $this->is_paying() && ! $this->is_premium() ) {
3515
+ $this->_admin_notices->add_sticky(
3516
+ sprintf(
3517
+ $this->get_text( 'you-have-x-license' ),
3518
+ $this->_site->plan->title
3519
+ ) . $this->get_complete_upgrade_instructions(),
3520
+ 'plan_upgraded',
3521
+ $this->get_text( 'yee-haw' ) . '!'
3522
+ );
3523
+ }
3524
+ }
3525
+
3526
+ // Schedule code type changes event.
3527
+ $this->schedule_install_sync();
3528
+
3529
+ /**
3530
+ * Unregister the uninstall hook for the other version of the plugin (with different code type) to avoid
3531
+ * triggering a fatal error when uninstalling that plugin. For example, after deactivating the "free" version
3532
+ * of a specific plugin, its uninstall hook should be unregistered after the "premium" version has been
3533
+ * activated. If we don't do that, a fatal error will occur when we try to uninstall the "free" version since
3534
+ * the main file of the "free" version will be loaded first before calling the hooked callback. Since the
3535
+ * free and premium versions are almost identical (same class or have same functions), a fatal error like
3536
+ * "Cannot redeclare class MyClass" or "Cannot redeclare my_function()" will occur.
3537
+ */
3538
+ $this->unregister_uninstall_hook();
3539
+
3540
+ $this->clear_module_main_file_cache();
3541
+
3542
+ // Update is_premium of latest version.
3543
+ $this->_storage->prev_is_premium = $this->_plugin->is_premium;
3544
+ }
3545
+
3546
+ #endregion
3547
+
3548
+ #----------------------------------------------------------------------------------
3549
+ #region Add-ons
3550
+ #----------------------------------------------------------------------------------
3551
+
3552
+ /**
3553
+ * Check if add-on installed and activated on site.
3554
+ *
3555
+ * @author Vova Feldman (@svovaf)
3556
+ * @since 1.0.6
3557
+ *
3558
+ * @param string|number $id_or_slug
3559
+ * @param bool|null $is_premium Since 1.2.1.7 can check for specified add-on version.
3560
+ *
3561
+ * @return bool
3562
+ */
3563
+ function is_addon_activated( $id_or_slug, $is_premium = null ) {
3564
+ $this->_logger->entrance();
3565
+
3566
+ $addon_id = self::get_module_id( $id_or_slug );
3567
+ $is_activated = self::has_instance( $addon_id );
3568
+
3569
+ if ( ! $is_activated ) {
3570
+ return false;
3571
+ }
3572
+
3573
+ if ( is_bool( $is_premium ) ) {
3574
+ // Check if the specified code version is activate.
3575
+ $addon = $this->get_addon_instance( $addon_id );
3576
+ $is_activated = ( $is_premium === $addon->is_premium() );
3577
+ }
3578
+
3579
+ return $is_activated;
3580
+ }
3581
+
3582
+ /**
3583
+ * Check if add-on was connected to install
3584
+ *
3585
+ * @author Vova Feldman (@svovaf)
3586
+ * @since 1.1.7
3587
+ *
3588
+ * @param string|number $id_or_slug
3589
+ *
3590
+ * @return bool
3591
+ */
3592
+ function is_addon_connected( $id_or_slug ) {
3593
+ $this->_logger->entrance();
3594
+
3595
+ $sites = self::get_all_sites( WP_FS__MODULE_TYPE_PLUGIN );
3596
+
3597
+ $addon_id = self::get_module_id( $id_or_slug );
3598
+ $addon = $this->get_addon( $addon_id );
3599
+ $slug = $addon->slug;
3600
+ if ( ! isset( $sites[ $slug ] ) ) {
3601
+ return false;
3602
+ }
3603
+
3604
+ $site = $sites[ $slug ];
3605
+
3606
+ $plugin = FS_Plugin_Manager::instance( $addon_id )->get();
3607
+
3608
+ if ( $plugin->parent_plugin_id != $this->_plugin->id ) {
3609
+ // The given slug do NOT belong to any of the plugin's add-ons.
3610
+ return false;
3611
+ }
3612
+
3613
+ return ( is_object( $site ) &&
3614
+ is_numeric( $site->id ) &&
3615
+ is_numeric( $site->user_id ) &&
3616
+ is_object( $site->plan )
3617
+ );
3618
+ }
3619
+
3620
+ /**
3621
+ * Determines if add-on installed.
3622
+ *
3623
+ * NOTE: This is a heuristic and only works if the folder/file named as the slug.
3624
+ *
3625
+ * @author Vova Feldman (@svovaf)
3626
+ * @since 1.0.6
3627
+ *
3628
+ * @param string|number $id_or_slug
3629
+ *
3630
+ * @return bool
3631
+ */
3632
+ function is_addon_installed( $id_or_slug ) {
3633
+ $this->_logger->entrance();
3634
+
3635
+ $addon_id = self::get_module_id( $id_or_slug );
3636
+
3637
+ return file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $this->get_addon_basename( $addon_id ) ) );
3638
+ }
3639
+
3640
+ /**
3641
+ * Get add-on basename.
3642
+ *
3643
+ * @author Vova Feldman (@svovaf)
3644
+ * @since 1.0.6
3645
+ *
3646
+ * @param string|number $id_or_slug
3647
+ *
3648
+ * @return string
3649
+ */
3650
+ function get_addon_basename( $id_or_slug ) {
3651
+ $addon_id = self::get_module_id( $id_or_slug );
3652
+
3653
+ if ( $this->is_addon_activated( $addon_id ) ) {
3654
+ return self::instance( $addon_id )->get_plugin_basename();
3655
+ }
3656
+
3657
+ $addon = $this->get_addon( $addon_id );
3658
+ $premium_basename = "{$addon->slug}-premium/{$addon->slug}.php";
3659
+
3660
+ if ( file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $premium_basename ) ) ) {
3661
+ return $premium_basename;
3662
+ }
3663
+
3664
+ $all_plugins = $this->get_all_plugins();
3665
+
3666
+ foreach ( $all_plugins as $basename => &$data ) {
3667
+ if ( $addon->slug === $data['slug'] ||
3668
+ $addon->slug . '-premium' === $data['slug']
3669
+ ) {
3670
+ return $basename;
3671
+ }
3672
+ }
3673
+
3674
+ $free_basename = "{$addon->slug}/{$addon->slug}.php";
3675
+
3676
+ return $free_basename;
3677
+ }
3678
+
3679
+ /**
3680
+ * Get installed add-ons instances.
3681
+ *
3682
+ * @author Vova Feldman (@svovaf)
3683
+ * @since 1.0.6
3684
+ *
3685
+ * @return Freemius[]
3686
+ */
3687
+ function get_installed_addons() {
3688
+ $installed_addons = array();
3689
+ foreach ( self::$_instances as $instance ) {
3690
+ if ( $instance->is_addon() && is_object( $instance->_parent_plugin ) ) {
3691
+ if ( $this->_plugin->id == $instance->_parent_plugin->id ) {
3692
+ $installed_addons[] = $instance;
3693
+ }
3694
+ }
3695
+ }
3696
+
3697
+ return $installed_addons;
3698
+ }
3699
+
3700
+ /**
3701
+ * Check if any add-ons of the plugin are installed.
3702
+ *
3703
+ * @author Leo Fajardo (@leorw)
3704
+ * @since 1.1.1
3705
+ *
3706
+ * @return bool
3707
+ */
3708
+ function has_installed_addons() {
3709
+ if ( ! $this->has_addons() ) {
3710
+ return false;
3711
+ }
3712
+
3713
+ foreach ( self::$_instances as $instance ) {
3714
+ if ( $instance->is_addon() && is_object( $instance->_parent_plugin ) ) {
3715
+ if ( $this->_plugin->id == $instance->_parent_plugin->id ) {
3716
+ return true;
3717
+ }
3718
+ }
3719
+ }
3720
+
3721
+ return false;
3722
+ }
3723
+
3724
+ /**
3725
+ * Tell Freemius that the current plugin is an add-on.
3726
+ *
3727
+ * @author Vova Feldman (@svovaf)
3728
+ * @since 1.0.6
3729
+ *
3730
+ * @param number $parent_plugin_id The parent plugin ID
3731
+ */
3732
+ function init_addon( $parent_plugin_id ) {
3733
+ $this->_plugin->parent_plugin_id = $parent_plugin_id;
3734
+ }
3735
+
3736
+ /**
3737
+ * @author Vova Feldman (@svovaf)
3738
+ * @since 1.0.6
3739
+ *
3740
+ * @return bool
3741
+ */
3742
+ function is_addon() {
3743
+ return isset( $this->_plugin->parent_plugin_id ) && is_numeric( $this->_plugin->parent_plugin_id );
3744
+ }
3745
+
3746
+ /**
3747
+ * Deactivate add-on if it's premium only and the user does't have a valid license.
3748
+ *
3749
+ * @param bool $is_after_trial_cancel
3750
+ *
3751
+ * @return bool If add-on was deactivated.
3752
+ */
3753
+ private function deactivate_premium_only_addon_without_license( $is_after_trial_cancel = false ) {
3754
+ if ( ! $this->has_free_plan() &&
3755
+ ! $this->has_features_enabled_license() &&
3756
+ ! $this->_has_premium_license()
3757
+ ) {
3758
+ if ( $this->is_registered() ) {
3759
+ // IF wrapper is turned off because activation_timestamp is currently only stored for plugins (not addons).
3760
+ // if (empty($this->_storage->activation_timestamp) ||
3761
+ // (WP_FS__SCRIPT_START_TIME - $this->_storage->activation_timestamp) > 30
3762
+ // ) {
3763
+ /**
3764
+ * @todo When it's first fail, there's no reason to try and re-sync because the licenses were just synced after initial activation.
3765
+ *
3766
+ * Retry syncing the user add-on licenses.
3767
+ */
3768
+ // Sync licenses.
3769
+ $this->_sync_licenses();
3770
+ // }
3771
+
3772
+ // Try to activate premium license.
3773
+ $this->_activate_license( true );
3774
+ }
3775
+
3776
+ if ( ! $this->has_free_plan() &&
3777
+ ! $this->has_features_enabled_license() &&
3778
+ ! $this->_has_premium_license()
3779
+ ) {
3780
+ // @todo Check if deactivate plugins also call the deactivation hook.
3781
+
3782
+ $this->_parent->_admin_notices->add_sticky(
3783
+ sprintf(
3784
+ $this->_parent->get_text( $is_after_trial_cancel ?
3785
+ 'addon-trial-cancelled-message' :
3786
+ 'addon-no-license-message'
3787
+ ),
3788
+ '<b>' . $this->_plugin->title . '</b>'
3789
+ ) . ' ' . sprintf(
3790
+ '<a href="%s" aria-label="%s" class="button button-primary" style="margin-left: 10px; vertical-align: middle;">%s &nbsp;&#10140;</a>',
3791
+ $this->_parent->addon_url( $this->_slug ),
3792
+ esc_attr( sprintf( $this->_parent->get_text( 'more-information-about-x' ), $this->_plugin->title ) ),
3793
+ $this->_parent->get_text( 'purchase-license' )
3794
+ ),
3795
+ 'no_addon_license_' . $this->_slug,
3796
+ ( $is_after_trial_cancel ? '' : $this->_parent->get_text( 'oops' ) . '...' ),
3797
+ ( $is_after_trial_cancel ? 'success' : 'error' )
3798
+ );
3799
+
3800
+ deactivate_plugins( array( $this->_plugin_basename ), true );
3801
+
3802
+ return true;
3803
+ }
3804
+ }
3805
+
3806
+ return false;
3807
+ }
3808
+
3809
+ #endregion
3810
+
3811
+ #----------------------------------------------------------------------------------
3812
+ #region Sandbox
3813
+ #----------------------------------------------------------------------------------
3814
+
3815
+ /**
3816
+ * Set Freemius into sandbox mode for debugging.
3817
+ *
3818
+ * @author Vova Feldman (@svovaf)
3819
+ * @since 1.0.4
3820
+ *
3821
+ * @param string $secret_key
3822
+ */
3823
+ function init_sandbox( $secret_key ) {
3824
+ $this->_plugin->secret_key = $secret_key;
3825
+
3826
+ // Update plugin details.
3827
+ FS_Plugin_Manager::instance( $this->_module_id )->update( $this->_plugin, true );
3828
+ }
3829
+
3830
+ /**
3831
+ * Check if running payments in sandbox mode.
3832
+ *
3833
+ * @author Vova Feldman (@svovaf)
3834
+ * @since 1.0.4
3835
+ *
3836
+ * @return bool
3837
+ */
3838
+ function is_payments_sandbox() {
3839
+ return ( ! $this->is_live() ) || isset( $this->_plugin->secret_key );
3840
+ }
3841
+
3842
+ #endregion
3843
+
3844
+ /**
3845
+ * Check if running test vs. live plugin.
3846
+ *
3847
+ * @author Vova Feldman (@svovaf)
3848
+ * @since 1.0.5
3849
+ *
3850
+ * @return bool
3851
+ */
3852
+ function is_live() {
3853
+ return $this->_plugin->is_live;
3854
+ }
3855
+
3856
+ /**
3857
+ * Check if the user skipped connecting the account with Freemius.
3858
+ *
3859
+ * @author Vova Feldman (@svovaf)
3860
+ * @since 1.0.7
3861
+ *
3862
+ * @return bool
3863
+ */
3864
+ function is_anonymous() {
3865
+ if ( ! isset( $this->_is_anonymous ) ) {
3866
+ if ( ! isset( $this->_storage->is_anonymous ) ) {
3867
+ // Not skipped.
3868
+ $this->_is_anonymous = false;
3869
+ } else if ( is_bool( $this->_storage->is_anonymous ) ) {
3870
+ // For back compatibility, since the variable was boolean before.
3871
+ $this->_is_anonymous = $this->_storage->is_anonymous;
3872
+
3873
+ // Upgrade stored data format to 1.1.3 format.
3874
+ $this->set_anonymous_mode( $this->_storage->is_anonymous );
3875
+ } else {
3876
+ // Version 1.1.3 and later.
3877
+ $this->_is_anonymous = $this->_storage->is_anonymous['is'];
3878
+ }
3879
+ }
3880
+
3881
+ return $this->_is_anonymous;
3882
+ }
3883
+
3884
+ /**
3885
+ * Check if user connected his account and install pending email activation.
3886
+ *
3887
+ * @author Vova Feldman (@svovaf)
3888
+ * @since 1.0.7
3889
+ *
3890
+ * @return bool
3891
+ */
3892
+ function is_pending_activation() {
3893
+ return $this->_storage->get( 'is_pending_activation', false );
3894
+ }
3895
+
3896
+ /**
3897
+ * Check if plugin must be WordPress.org compliant.
3898
+ *
3899
+ * @since 1.0.7
3900
+ *
3901
+ * @return bool
3902
+ */
3903
+ function is_org_repo_compliant() {
3904
+ return $this->_is_org_compliant;
3905
+ }
3906
+
3907
+ #----------------------------------------------------------------------------------
3908
+ #region Daily Sync Cron
3909
+ #----------------------------------------------------------------------------------
3910
+
3911
+ /**
3912
+ * @author Vova Feldman (@svovaf)
3913
+ * @since 1.1.7.3
3914
+ */
3915
+ private function run_manual_sync() {
3916
+ self::require_pluggable_essentials();
3917
+
3918
+ if ( ! $this->is_user_admin() ) {
3919
+ return;
3920
+ }
3921
+
3922
+ // Run manual sync.
3923
+ $this->_sync_cron();
3924
+
3925
+ // Reschedule next cron to run 24 hours from now (performance optimization).
3926
+ $this->clear_sync_cron();
3927
+
3928
+ $this->schedule_sync_cron( time() + WP_FS__TIME_24_HOURS_IN_SEC, false );
3929
+ }
3930
+
3931
+ /**
3932
+ * Data sync cron job. Replaces the background sync non blocking HTTP request
3933
+ * that doesn't halt page loading.
3934
+ *
3935
+ * @author Vova Feldman (@svovaf)
3936
+ * @since 1.1.7.3
3937
+ */
3938
+ function _sync_cron() {
3939
+ $this->_logger->entrance();
3940
+
3941
+ // Store the last time data sync was executed.
3942
+ $this->_storage->sync_timestamp = time();
3943
+
3944
+ // Check if API is temporary down.
3945
+ if ( FS_Api::is_temporary_down() ) {
3946
+ return;
3947
+ }
3948
+
3949
+ // @todo Add logic that identifies API latency, and reschedule the next background sync randomly between 8-16 hours.
3950
+
3951
+ if ( $this->is_registered() ) {
3952
+ if ( $this->has_paid_plan() ) {
3953
+ // Initiate background plan sync.
3954
+ $this->_sync_license( true );
3955
+
3956
+ if ( $this->is_paying() ) {
3957
+ // Check for premium plugin updates.
3958
+ $this->check_updates( true );
3959
+ }
3960
+ } else {
3961
+ // Sync install (only if something changed locally).
3962
+ $this->sync_install();
3963
+ }
3964
+ }
3965
+
3966
+ $this->do_action( 'after_sync_cron' );
3967
+ }
3968
+
3969
+ /**
3970
+ * Check if sync was executed in the last $period of seconds.
3971
+ *
3972
+ * @author Vova Feldman (@svovaf)
3973
+ * @since 1.1.7.3
3974
+ *
3975
+ * @param int $period In seconds
3976
+ *
3977
+ * @return bool
3978
+ */
3979
+ private function is_sync_executed( $period = WP_FS__TIME_24_HOURS_IN_SEC ) {
3980
+ if ( ! isset( $this->_storage->sync_timestamp ) ) {
3981
+ return false;
3982
+ }
3983
+
3984
+ return ( $this->_storage->sync_timestamp > ( WP_FS__SCRIPT_START_TIME - $period ) );
3985
+ }
3986
+
3987
+ /**
3988
+ * @author Vova Feldman (@svovaf)
3989
+ * @since 1.1.7.3
3990
+ *
3991
+ * @return bool
3992
+ */
3993
+ private function is_sync_cron_on() {
3994
+ /**
3995
+ * @var object $sync_cron_data
3996
+ */
3997
+ $sync_cron_data = $this->_storage->get( 'sync_cron', null );
3998
+
3999
+ return ( ! is_null( $sync_cron_data ) && true === $sync_cron_data->on );
4000
+ }
4001
+
4002
+ /**
4003
+ * @author Vova Feldman (@svovaf)
4004
+ * @since 1.1.7.3
4005
+ *
4006
+ * @param int $start_at Defaults to now.
4007
+ * @param bool $randomize_start If true, schedule first job randomly during the next 12 hours. Otherwise,
4008
+ * schedule job to start right away.
4009
+ */
4010
+ private function schedule_sync_cron( $start_at = WP_FS__SCRIPT_START_TIME, $randomize_start = true ) {
4011
+ $this->_logger->entrance();
4012
+
4013
+ if ( $randomize_start ) {
4014
+ // Schedule first sync with a random 12 hour time range from now.
4015
+ $start_at += rand( 0, ( WP_FS__TIME_24_HOURS_IN_SEC / 2 ) );
4016
+ }
4017
+
4018
+ // Schedule daily WP cron.
4019
+ wp_schedule_event(
4020
+ $start_at,
4021
+ 'daily',
4022
+ $this->get_action_tag( 'data_sync' )
4023
+ );
4024
+
4025
+ $this->_storage->store( 'sync_cron', (object) array(
4026
+ 'version' => $this->get_plugin_version(),
4027
+ 'sdk_version' => $this->version,
4028
+ 'timestamp' => WP_FS__SCRIPT_START_TIME,
4029
+ 'on' => true,
4030
+ ) );
4031
+ }
4032
+
4033
+ /**
4034
+ * Add the actual sync function to the cron job hook.
4035
+ *
4036
+ * @author Vova Feldman (@svovaf)
4037
+ * @since 1.1.7.3
4038
+ */
4039
+ private function hook_callback_to_sync_cron() {
4040
+ $this->add_action( 'data_sync', array( &$this, '_sync_cron' ) );
4041
+ }
4042
+
4043
+ /**
4044
+ * @author Vova Feldman (@svovaf)
4045
+ * @since 1.1.7.3
4046
+ */
4047
+ private function clear_sync_cron() {
4048
+ $this->_logger->entrance();
4049
+
4050
+ if ( ! $this->is_sync_cron_on() ) {
4051
+ return;
4052
+ }
4053
+
4054
+ $this->_storage->remove( 'sync_cron' );
4055
+
4056
+ wp_clear_scheduled_hook( $this->get_action_tag( 'data_sync' ) );
4057
+ }
4058
+
4059
+ /**
4060
+ * Unix timestamp for next sync cron execution or false if not scheduled.
4061
+ *
4062
+ * @author Vova Feldman (@svovaf)
4063
+ * @since 1.1.7.3
4064
+ *
4065
+ * @return int|false
4066
+ */
4067
+ function next_sync_cron() {
4068
+ $this->_logger->entrance();
4069
+
4070
+ if ( ! $this->is_sync_cron_on() ) {
4071
+ return false;
4072
+ }
4073
+
4074
+ return wp_next_scheduled( $this->get_action_tag( 'data_sync' ) );
4075
+ }
4076
+
4077
+ /**
4078
+ * Unix timestamp for previous sync cron execution or false if never executed.
4079
+ *
4080
+ * @author Vova Feldman (@svovaf)
4081
+ * @since 1.1.7.3
4082
+ *
4083
+ * @return int|false
4084
+ */
4085
+ function last_sync_cron() {
4086
+ $this->_logger->entrance();
4087
+
4088
+ return $this->_storage->get( 'sync_timestamp' );
4089
+ }
4090
+
4091
+ #endregion Daily Sync Cron ------------------------------------------------------------------
4092
+
4093
+ #----------------------------------------------------------------------------------
4094
+ #region Async Install Sync
4095
+ #----------------------------------------------------------------------------------
4096
+
4097
+ /**
4098
+ * @author Vova Feldman (@svovaf)
4099
+ * @since 1.1.7.3
4100
+ *
4101
+ * @return bool
4102
+ */
4103
+ private function is_install_sync_scheduled() {
4104
+ /**
4105
+ * @var object $cron_data
4106
+ */
4107
+ $cron_data = $this->_storage->get( 'install_sync_cron', null );
4108
+
4109
+ return ( ! is_null( $cron_data ) && true === $cron_data->on );
4110
+ }
4111
+
4112
+ /**
4113
+ * Instead of running blocking install sync event, execute non blocking scheduled wp-cron.
4114
+ *
4115
+ * @author Vova Feldman (@svovaf)
4116
+ * @since 1.1.7.3
4117
+ */
4118
+ private function schedule_install_sync() {
4119
+ $this->_logger->entrance();
4120
+
4121
+ $this->clear_install_sync_cron();
4122
+
4123
+ // Schedule immediate install sync.
4124
+ wp_schedule_single_event(
4125
+ WP_FS__SCRIPT_START_TIME,
4126
+ $this->get_action_tag( 'install_sync' )
4127
+ );
4128
+
4129
+ $this->_storage->store( 'install_sync_cron', (object) array(
4130
+ 'version' => $this->get_plugin_version(),
4131
+ 'sdk_version' => $this->version,
4132
+ 'timestamp' => WP_FS__SCRIPT_START_TIME,
4133
+ 'on' => true,
4134
+ ) );
4135
+ }
4136
+
4137
+ /**
4138
+ * Unix timestamp for previous install sync cron execution or false if never executed.
4139
+ *
4140
+ * @todo There's some very strange bug that $this->_storage->install_sync_timestamp value is not being
4141
+ * updated. But for sure the sync event is working.
4142
+ *
4143
+ * @author Vova Feldman (@svovaf)
4144
+ * @since 1.1.7.3
4145
+ *
4146
+ * @return int|false
4147
+ */
4148
+ function last_install_sync() {
4149
+ $this->_logger->entrance();
4150
+
4151
+ return $this->_storage->get( 'install_sync_timestamp' );
4152
+ }
4153
+
4154
+ /**
4155
+ * Unix timestamp for next install sync cron execution or false if not scheduled.
4156
+ *
4157
+ * @author Vova Feldman (@svovaf)
4158
+ * @since 1.1.7.3
4159
+ *
4160
+ * @return int|false
4161
+ */
4162
+ function next_install_sync() {
4163
+ $this->_logger->entrance();
4164
+
4165
+ if ( ! $this->is_install_sync_scheduled() ) {
4166
+ return false;
4167
+ }
4168
+
4169
+ return wp_next_scheduled( $this->get_action_tag( 'install_sync' ) );
4170
+ }
4171
+
4172
+ /**
4173
+ * Add the actual install sync function to the cron job hook.
4174
+ *
4175
+ * @author Vova Feldman (@svovaf)
4176
+ * @since 1.1.7.3
4177
+ */
4178
+ private function hook_callback_to_install_sync() {
4179
+ $this->add_action( 'install_sync', array( &$this, '_run_sync_install' ) );
4180
+ }
4181
+
4182
+ /**
4183
+ * @author Vova Feldman (@svovaf)
4184
+ * @since 1.1.7.3
4185
+ */
4186
+ private function clear_install_sync_cron() {
4187
+ $this->_logger->entrance();
4188
+
4189
+ if ( ! $this->is_install_sync_scheduled() ) {
4190
+ return;
4191
+ }
4192
+
4193
+ $this->_storage->remove( 'install_sync_cron' );
4194
+
4195
+ wp_clear_scheduled_hook( $this->get_action_tag( 'install_sync' ) );
4196
+ }
4197
+
4198
+ /**
4199
+ * @author Vova Feldman (@svovaf)
4200
+ * @since 1.1.7.3
4201
+ */
4202
+ public function _run_sync_install() {
4203
+ $this->_logger->entrance();
4204
+
4205
+ // Update last install sync timestamp.
4206
+ $this->_storage->install_sync_timestamp = time();
4207
+
4208
+ $this->sync_install( array(), true );
4209
+ }
4210
+
4211
+ #endregion Async Install Sync ------------------------------------------------------------------
4212
+
4213
+ /**
4214
+ * Show a notice that activation is currently pending.
4215
+ *
4216
+ * @author Vova Feldman (@svovaf)
4217
+ * @since 1.0.7
4218
+ *
4219
+ * @param bool|string $email
4220
+ * @param bool $is_pending_trial Since 1.2.1.5
4221
+ */
4222
+ function _add_pending_activation_notice( $email = false, $is_pending_trial = false ) {
4223
+ if ( ! is_string( $email ) ) {
4224
+ $current_user = self::_get_current_wp_user();
4225
+ $email = $current_user->user_email;
4226
+ }
4227
+
4228
+ $this->_admin_notices->add_sticky(
4229
+ sprintf(
4230
+ $this->get_text( 'pending-activation-message' ),
4231
+ '<b>' . $this->get_plugin_name() . '</b>',
4232
+ '<b>' . $email . '</b>',
4233
+ $this->get_text( $is_pending_trial ? 'start-the-trial' : 'complete-the-install' )
4234
+ ),
4235
+ 'activation_pending',
4236
+ 'Thanks!'
4237
+ );
4238
+ }
4239
+
4240
+ /**
4241
+ * Check if currently in plugin activation.
4242
+ *
4243
+ * @author Vova Feldman (@svovaf)
4244
+ * @since 1.1.4
4245
+ *
4246
+ * @return bool
4247
+ */
4248
+ function is_plugin_activation() {
4249
+ return get_option( 'fs_'
4250
+ . ( $this->is_plugin() ? '' : $this->_module_type . '_' )
4251
+ . "{$this->_slug}_activated", false );
4252
+ }
4253
+
4254
+ /**
4255
+ *
4256
+ * NOTE: admin_menu action executed before admin_init.
4257
+ *
4258
+ * @author Vova Feldman (@svovaf)
4259
+ * @since 1.0.7
4260
+ */
4261
+ function _admin_init_action() {
4262
+ /**
4263
+ * Automatically redirect to connect/activation page after plugin activation.
4264
+ *
4265
+ * @since 1.1.7 Do NOT redirect to opt-in when running in network admin mode.
4266
+ */
4267
+ if ( $this->is_plugin_activation() ) {
4268
+ delete_option( 'fs_'
4269
+ . ( $this->is_plugin() ? '' : $this->_module_type . '_' )
4270
+ . "{$this->_slug}_activated" );
4271
+
4272
+ if ( ! function_exists( 'is_network_admin' ) || ! is_network_admin() ) {
4273
+ $this->_redirect_on_activation_hook();
4274
+
4275
+ return;
4276
+ }
4277
+ }
4278
+
4279
+ if ( fs_request_is_action( $this->get_unique_affix() . '_skip_activation' ) ) {
4280
+ check_admin_referer( $this->get_unique_affix() . '_skip_activation' );
4281
+
4282
+ $this->skip_connection();
4283
+
4284
+ fs_redirect( $this->get_after_activation_url( 'after_skip_url' ) );
4285
+ }
4286
+
4287
+ if ( ! $this->is_addon() && ! $this->is_registered() && ! $this->is_anonymous() ) {
4288
+ if ( ! $this->is_pending_activation() ) {
4289
+ if ( ! $this->_menu->is_main_settings_page() ) {
4290
+ /**
4291
+ * If a user visits any other admin page before activating the premium-only theme with a valid
4292
+ * license, reactivate the previous theme.
4293
+ *
4294
+ * @author Leo Fajardo (@leorw)
4295
+ * @since 1.2.2
4296
+ */
4297
+ if ( $this->is_theme()
4298
+ && $this->is_only_premium()
4299
+ && ! $this->has_settings_menu()
4300
+ && ! isset( $_REQUEST['fs_action'] )
4301
+ && $this->can_activate_previous_theme()
4302
+ ) {
4303
+ $this->activate_previous_theme();
4304
+
4305
+ return;
4306
+ }
4307
+
4308
+ if ( $this->is_plugin_new_install() || $this->is_only_premium() ) {
4309
+ // Show notice for new plugin installations.
4310
+ $this->_admin_notices->add(
4311
+ sprintf(
4312
+ $this->get_text( 'you-are-step-away' ),
4313
+ sprintf( '<b><a href="%s">%s</a></b>',
4314
+ $this->get_activation_url(),
4315
+ sprintf( $this->get_text( 'activate-x-now' ), $this->get_plugin_name() )
4316
+ )
4317
+ ),
4318
+ '',
4319
+ 'update-nag'
4320
+ );
4321
+ } else {
4322
+ if ( ! isset( $this->_storage->sticky_optin_added ) ) {
4323
+ $this->_storage->sticky_optin_added = true;
4324
+
4325
+ // Show notice for new plugin installations.
4326
+ $this->_admin_notices->add_sticky(
4327
+ sprintf(
4328
+ $this->get_text( 'few-plugin-tweaks' ),
4329
+ $this->_module_type,
4330
+ sprintf( '<b><a href="%s">%s</a></b>',
4331
+ $this->get_activation_url(),
4332
+ sprintf( $this->get_text( 'optin-x-now' ), $this->get_plugin_name() )
4333
+ )
4334
+ ),
4335
+ 'connect_account',
4336
+ '',
4337
+ 'update-nag'
4338
+ );
4339
+ }
4340
+
4341
+ if ( $this->has_filter( 'optin_pointer_element' ) ) {
4342
+ // Don't show admin nag if plugin update.
4343
+ wp_enqueue_script( 'wp-pointer' );
4344
+ wp_enqueue_style( 'wp-pointer' );
4345
+
4346
+ $this->_enqueue_connect_essentials();
4347
+
4348
+ add_action( 'admin_print_footer_scripts', array(
4349
+ $this,
4350
+ '_add_connect_pointer_script'
4351
+ ) );
4352
+ }
4353
+ }
4354
+ }
4355
+ }
4356
+
4357
+ if ( $this->is_theme() &&
4358
+ $this->_menu->is_main_settings_page()
4359
+ ) {
4360
+ $this->_show_theme_activation_optin_dialog();
4361
+ }
4362
+ }
4363
+
4364
+ $this->_add_upgrade_action_link();
4365
+ }
4366
+
4367
+ /**
4368
+ * Enqueue connect requires scripts and styles.
4369
+ *
4370
+ * @author Vova Feldman (@svovaf)
4371
+ * @since 1.1.4
4372
+ */
4373
+ function _enqueue_connect_essentials() {
4374
+ wp_enqueue_script( 'jquery' );
4375
+ wp_enqueue_script( 'json2' );
4376
+
4377
+ fs_enqueue_local_script( 'postmessage', 'nojquery.ba-postmessage.min.js' );
4378
+ fs_enqueue_local_script( 'fs-postmessage', 'postmessage.js' );
4379
+
4380
+ fs_enqueue_local_style( 'fs_connect', '/admin/connect.css' );
4381
+ }
4382
+
4383
+ /**
4384
+ * Add connect / opt-in pointer.
4385
+ *
4386
+ * @author Vova Feldman (@svovaf)
4387
+ * @since 1.1.4
4388
+ */
4389
+ function _add_connect_pointer_script() {
4390
+ $vars = array( 'id' => $this->_module_id );
4391
+ $pointer_content = fs_get_template( 'connect.php', $vars );
4392
+ ?>
4393
+ <script type="text/javascript">// <![CDATA[
4394
+ jQuery(document).ready(function ($) {
4395
+ if ('undefined' !== typeof(jQuery().pointer)) {
4396
+
4397
+ var element = <?php echo $this->apply_filters( 'optin_pointer_element', '$("#non_existing_element");' ) ?>;
4398
+
4399
+ if (element.length > 0) {
4400
+ var optin = $(element).pointer($.extend(true, {}, {
4401
+ content : <?php echo json_encode( $pointer_content ) ?>,
4402
+ position : {
4403
+ edge : 'left',
4404
+ align: 'center'
4405
+ },
4406
+ buttons : function () {
4407
+ // Don't show pointer buttons.
4408
+ return '';
4409
+ },
4410
+ pointerWidth: 482
4411
+ }, <?php echo $this->apply_filters( 'optin_pointer_options_json', '{}' ) ?>));
4412
+
4413
+ <?php
4414
+ echo $this->apply_filters( 'optin_pointer_execute', "
4415
+
4416
+ optin.pointer('open');
4417
+
4418
+ // Tag the opt-in pointer with custom class.
4419
+ $('.wp-pointer #fs_connect')
4420
+ .parents('.wp-pointer.wp-pointer-top')
4421
+ .addClass('fs-opt-in-pointer');
4422
+
4423
+ ", 'element', 'optin' ) ?>
4424
+ }
4425
+ }
4426
+ });
4427
+ // ]]></script>
4428
+ <?php
4429
+ }
4430
+
4431
+ /**
4432
+ * Return current page's URL.
4433
+ *
4434
+ * @author Vova Feldman (@svovaf)
4435
+ * @since 1.0.7
4436
+ *
4437
+ * @return string
4438
+ */
4439
+ function current_page_url() {
4440
+ $url = 'http';
4441
+
4442
+ if ( isset( $_SERVER["HTTPS"] ) ) {
4443
+ if ( $_SERVER["HTTPS"] == "on" ) {
4444
+ $url .= "s";
4445
+ }
4446
+ }
4447
+ $url .= "://";
4448
+ if ( $_SERVER["SERVER_PORT"] != "80" ) {
4449
+ $url .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
4450
+ } else {
4451
+ $url .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
4452
+ }
4453
+
4454
+ return esc_url( $url );
4455
+ }
4456
+
4457
+ /**
4458
+ * Check if the current page is the plugin's main admin settings page.
4459
+ *
4460
+ * @author Vova Feldman (@svovaf)
4461
+ * @since 1.0.7
4462
+ *
4463
+ * @return bool
4464
+ */
4465
+ function _is_plugin_page() {
4466
+ return fs_is_plugin_page( $this->_menu->get_raw_slug() ) ||
4467
+ fs_is_plugin_page( $this->_slug );
4468
+ }
4469
+
4470
+ /* Events
4471
+ ------------------------------------------------------------------------------------------------------------------*/
4472
+ /**
4473
+ * Delete site install from Database.
4474
+ *
4475
+ * @author Vova Feldman (@svovaf)
4476
+ * @since 1.0.1
4477
+ *
4478
+ * @param bool $store
4479
+ */
4480
+ function _delete_site( $store = true ) {
4481
+ self::_delete_site_by_slug( $this->_slug, $this->_module_type, $store );
4482
+ }
4483
+
4484
+ /**
4485
+ * Delete site install from Database.
4486
+ *
4487
+ * @author Vova Feldman (@svovaf)
4488
+ * @since 1.2.2.7
4489
+ *
4490
+ * @param string $slug
4491
+ * @param string $module_type
4492
+ * @param bool $store
4493
+ */
4494
+ static function _delete_site_by_slug($slug, $module_type, $store = true ) {
4495
+ $sites = self::get_all_sites( $module_type );
4496
+
4497
+ if ( isset( $sites[ $slug ] ) ) {
4498
+ unset( $sites[ $slug ] );
4499
+ }
4500
+
4501
+ self::set_account_option_by_module( $module_type, 'sites', $sites, $store );
4502
+ }
4503
+
4504
+ /**
4505
+ * Delete plugin's plans information.
4506
+ *
4507
+ * @param bool $store Flush to Database if true.
4508
+ *
4509
+ * @author Vova Feldman (@svovaf)
4510
+ * @since 1.0.9
4511
+ */
4512
+ private function _delete_plans( $store = true ) {
4513
+ $this->_logger->entrance();
4514
+
4515
+ $plans = self::get_all_plans( $this->_module_type );
4516
+
4517
+ unset( $plans[ $this->_slug ] );
4518
+
4519
+ $this->set_account_option( 'plans', $plans, $store );
4520
+ }
4521
+
4522
+ /**
4523
+ * Delete all plugin licenses.
4524
+ *
4525
+ * @author Vova Feldman (@svovaf)
4526
+ * @since 1.0.9
4527
+ *
4528
+ * @param bool $store
4529
+ * @param string|bool $plugin_slug
4530
+ */
4531
+ private function _delete_licenses( $store = true, $plugin_slug = false ) {
4532
+ $this->_logger->entrance();
4533
+
4534
+ $all_licenses = self::get_all_licenses( $this->_module_type );
4535
+
4536
+ if ( ! is_string( $plugin_slug ) ) {
4537
+ $plugin_slug = $this->_slug;
4538
+ }
4539
+
4540
+ unset( $all_licenses[ $plugin_slug ] );
4541
+
4542
+ $this->set_account_option( 'licenses', $all_licenses, $store );
4543
+ }
4544
+
4545
+ /**
4546
+ * Check if Freemius was added on new plugin installation.
4547
+ *
4548
+ * @author Vova Feldman (@svovaf)
4549
+ * @since 1.1.5
4550
+ *
4551
+ * @return bool
4552
+ */
4553
+ function is_plugin_new_install() {
4554
+ return isset( $this->_storage->is_plugin_new_install ) &&
4555
+ $this->_storage->is_plugin_new_install;
4556
+ }
4557
+
4558
+ /**
4559
+ * Check if it's the first plugin release that is running Freemius.
4560
+ *
4561
+ * @author Vova Feldman (@svovaf)
4562
+ * @since 1.2.1.5
4563
+ *
4564
+ * @return bool
4565
+ */
4566
+ function is_first_freemius_powered_version() {
4567
+ return empty( $this->_storage->plugin_last_version );
4568
+ }
4569
+
4570
+ /**
4571
+ * @author Leo Fajardo (@leorw)
4572
+ * @since 1.2.2
4573
+ *
4574
+ * @return bool|string
4575
+ */
4576
+ private function get_previous_theme_slug() {
4577
+ return isset( $this->_storage->previous_theme ) ?
4578
+ $this->_storage->previous_theme :
4579
+ false;
4580
+ }
4581
+
4582
+ /**
4583
+ * @author Leo Fajardo (@leorw)
4584
+ * @since 1.2.2
4585
+ *
4586
+ * @return string
4587
+ */
4588
+ private function can_activate_previous_theme() {
4589
+ $slug = $this->get_previous_theme_slug();
4590
+ if ( false !== $slug && current_user_can( 'switch_themes' ) ) {
4591
+ $theme_instance = wp_get_theme( $slug );
4592
+
4593
+ return $theme_instance->exists();
4594
+ }
4595
+
4596
+ return false;
4597
+ }
4598
+
4599
+ /**
4600
+ * @author Leo Fajardo (@leorw)
4601
+ * @since 1.2.2
4602
+ *
4603
+ * @return string
4604
+ */
4605
+ private function activate_previous_theme() {
4606
+ switch_theme( $this->get_previous_theme_slug() );
4607
+ unset( $this->_storage->previous_theme );
4608
+
4609
+ global $pagenow;
4610
+ if ( 'themes.php' === $pagenow ) {
4611
+ /**
4612
+ * Refresh the active theme information.
4613
+ *
4614
+ * @author Leo Fajardo (@leorw)
4615
+ * @since 1.2.2
4616
+ */
4617
+ fs_redirect( admin_url( $pagenow ) );
4618
+ }
4619
+ }
4620
+
4621
+ /**
4622
+ * @author Leo Fajardo (@leorw)
4623
+ * @since 1.2.2
4624
+ *
4625
+ * @return string
4626
+ */
4627
+ function get_previous_theme_activation_url() {
4628
+ if ( ! $this->can_activate_previous_theme() ) {
4629
+ return '';
4630
+ }
4631
+
4632
+ /**
4633
+ * Activation URL
4634
+ *
4635
+ * @author Leo Fajardo (@leorw)
4636
+ * @since 1.2.2
4637
+ */
4638
+ return wp_nonce_url(
4639
+ admin_url( 'themes.php?action=activate&stylesheet=' . urlencode( $this->get_previous_theme_slug() ) ),
4640
+ 'switch-theme_' . $this->get_previous_theme_slug()
4641
+ );
4642
+ }
4643
+
4644
+ /**
4645
+ * Saves the slug of the previous theme if it still exists so that it can be used by the logic in the opt-in
4646
+ * form that decides whether to add a close button to the opt-in dialog or not. So after a premium-only theme is
4647
+ * activated, the close button will appear and will reactivate the previous theme if clicked. If the previous
4648
+ * theme doesn't exist, then there will be no close button.
4649
+ *
4650
+ * @author Leo Fajardo (@leorw)
4651
+ * @since 1.2.2
4652
+ *
4653
+ * @param string $slug_or_name Old theme's slug or name.
4654
+ * @param bool|WP_Theme $old_theme WP_Theme instance of the old theme if it still exists.
4655
+ */
4656
+ function _activate_theme_event_hook( $slug_or_name, $old_theme = false ) {
4657
+ $this->_storage->previous_theme = ( false !== $old_theme ) ?
4658
+ $old_theme->get_stylesheet() :
4659
+ $slug_or_name;
4660
+
4661
+ $this->_activate_plugin_event_hook();
4662
+ }
4663
+
4664
+ /**
4665
+ * Plugin activated hook.
4666
+ *
4667
+ * @author Vova Feldman (@svovaf)
4668
+ * @since 1.0.1
4669
+ *
4670
+ * @uses FS_Api
4671
+ */
4672
+ function _activate_plugin_event_hook() {
4673
+ $this->_logger->entrance( 'slug = ' . $this->_slug );
4674
+
4675
+ if ( ! $this->is_user_admin() ) {
4676
+ return;
4677
+ }
4678
+
4679
+ $this->unregister_uninstall_hook();
4680
+
4681
+ // Clear API cache on activation.
4682
+ FS_Api::clear_cache();
4683
+
4684
+ $is_premium_version_activation = ( current_filter() !== ( 'activate_' . $this->_free_plugin_basename ) );
4685
+
4686
+ $this->_logger->info( 'Activating ' . ( $is_premium_version_activation ? 'premium' : 'free' ) . ' plugin version.' );
4687
+
4688
+ // 1. If running in the activation of the FREE module, get the basename of the PREMIUM.
4689
+ // 2. If running in the activation of the PREMIUM module, get the basename of the FREE.
4690
+ $other_version_basename = $is_premium_version_activation ?
4691
+ $this->_free_plugin_basename :
4692
+ $this->premium_plugin_basename();
4693
+
4694
+ /**
4695
+ * If the other module version is activate, deactivate it.
4696
+ *
4697
+ * @author Leo Fajardo (@leorw)
4698
+ * @since 1.2.2
4699
+ */
4700
+ if ( is_plugin_active( $other_version_basename ) ) {
4701
+ deactivate_plugins( $other_version_basename );
4702
+ }
4703
+
4704
+ if ( $this->is_registered() ) {
4705
+ if ( $is_premium_version_activation ) {
4706
+ $this->reconnect_locally();
4707
+ }
4708
+
4709
+
4710
+ // Schedule re-activation event and sync.
4711
+ // $this->sync_install( array(), true );
4712
+ $this->schedule_install_sync();
4713
+
4714
+ // If activating the premium module version, add an admin notice to congratulate for an upgrade completion.
4715
+ if ( $is_premium_version_activation ) {
4716
+ $this->_admin_notices->add(
4717
+ sprintf( $this->get_text( 'successful-version-upgrade-message' ), sprintf( '<b>%s</b>', $this->_plugin->title ) ),
4718
+ $this->get_text( 'woot' ) . '!'
4719
+ );
4720
+ }
4721
+ } else if ( $this->is_anonymous() ) {
4722
+ /**
4723
+ * Reset "skipped" click cache on the following:
4724
+ * 1. Freemius DEV mode.
4725
+ * 2. WordPress DEBUG mode.
4726
+ * 3. If a plugin and the user skipped the exact same version before.
4727
+ *
4728
+ * @since 1.2.2.7 Ulrich Pogson (@grapplerulrich) asked to not reset the SKIPPED flag if the exact same THEME version was activated before unless the developer is running with WP_DEBUG on, or Freemius debug mode on (WP_FS__DEV_MODE).
4729
+ *
4730
+ * @todo 4. If explicitly asked to retry after every activation.
4731
+ */
4732
+ if ( WP_FS__DEV_MODE ||
4733
+ (
4734
+ ( $this->is_plugin() || ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ) &&
4735
+ $this->get_plugin_version() == $this->_storage->is_anonymous['version']
4736
+ )
4737
+ ) {
4738
+ $this->reset_anonymous_mode();
4739
+ }
4740
+ }
4741
+
4742
+ if ( ! isset( $this->_storage->is_plugin_new_install ) ) {
4743
+ /**
4744
+ * If no previous version of plugin's version exist, it means that it's either
4745
+ * the first time that the plugin installed on the site, or the plugin was installed
4746
+ * before but didn't have Freemius integrated.
4747
+ *
4748
+ * Since register_activation_hook() do NOT fires on updates since 3.1, and only fires
4749
+ * on manual activation via the dashboard, is_plugin_activation() is TRUE
4750
+ * only after immediate activation.
4751
+ *
4752
+ * @since 1.1.4
4753
+ * @link https://make.wordpress.org/core/2010/10/27/plugin-activation-hooks-no-longer-fire-for-updates/
4754
+ */
4755
+ $this->_storage->is_plugin_new_install = empty( $this->_storage->plugin_last_version );
4756
+ }
4757
+
4758
+ if ( ! $this->_anonymous_mode &&
4759
+ $this->has_api_connectivity( WP_FS__DEV_MODE ) &&
4760
+ ! $this->_isAutoInstall
4761
+ ) {
4762
+ // Store hint that the plugin was just activated to enable auto-redirection to settings.
4763
+ add_option( 'fs_'
4764
+ . ( $this->is_plugin() ? '' : $this->_module_type . '_' )
4765
+ . "{$this->_slug}_activated", true );
4766
+ }
4767
+
4768
+ /**
4769
+ * Activation hook is executed after the plugin's main file is loaded, therefore,
4770
+ * after the plugin was loaded. The logic is located at activate_plugin()
4771
+ * ./wp-admin/includes/plugin.php.
4772
+ *
4773
+ * @author Vova Feldman (@svovaf)
4774
+ * @since 1.1.9
4775
+ */
4776
+ $this->_storage->was_plugin_loaded = true;
4777
+ }
4778
+
4779
+ /**
4780
+ * Delete account.
4781
+ *
4782
+ * @author Vova Feldman (@svovaf)
4783
+ * @since 1.0.3
4784
+ *
4785
+ * @param bool $check_user Enforce checking if user have plugins activation privileges.
4786
+ */
4787
+ function delete_account_event( $check_user = true ) {
4788
+ $this->_logger->entrance( 'slug = ' . $this->_slug );
4789
+
4790
+ if ( $check_user && ! $this->is_user_admin() ) {
4791
+ return;
4792
+ }
4793
+
4794
+ $this->do_action( 'before_account_delete' );
4795
+
4796
+ // Clear all admin notices.
4797
+ $this->_admin_notices->clear_all_sticky();
4798
+
4799
+ $this->_delete_site( false );
4800
+
4801
+ $this->_delete_plans( false );
4802
+
4803
+ $this->_delete_licenses( false );
4804
+
4805
+ // Delete add-ons related to plugin's account.
4806
+ $this->_delete_account_addons( false );
4807
+
4808
+ // @todo Delete plans and licenses of add-ons.
4809
+
4810
+ self::$_accounts->store();
4811
+
4812
+ /**
4813
+ * IMPORTANT:
4814
+ * Clear crons must be executed before clearing all storage.
4815
+ * Otherwise, the cron will not be cleared.
4816
+ */
4817
+ $this->clear_sync_cron();
4818
+ $this->clear_install_sync_cron();
4819
+
4820
+ // Clear all storage data.
4821
+ $this->_storage->clear_all( true, array(
4822
+ 'connectivity_test',
4823
+ 'is_on',
4824
+ ) );
4825
+
4826
+ // Send delete event.
4827
+ $this->get_api_site_scope()->call( '/', 'delete' );
4828
+
4829
+ $this->do_action( 'after_account_delete' );
4830
+ }
4831
+
4832
+ /**
4833
+ * Plugin deactivation hook.
4834
+ *
4835
+ * @author Vova Feldman (@svovaf)
4836
+ * @since 1.0.1
4837
+ */
4838
+ function _deactivate_plugin_hook() {
4839
+ $this->_logger->entrance( 'slug = ' . $this->_slug );
4840
+
4841
+ if ( ! current_user_can( 'activate_plugins' ) ) {
4842
+ return;
4843
+ }
4844
+
4845
+ $this->_admin_notices->clear_all_sticky();
4846
+ if ( isset( $this->_storage->sticky_optin_added ) ) {
4847
+ unset( $this->_storage->sticky_optin_added );
4848
+ }
4849
+
4850
+ if ( ! isset( $this->_storage->is_plugin_new_install ) ) {
4851
+ // Remember that plugin was already installed.
4852
+ $this->_storage->is_plugin_new_install = false;
4853
+ }
4854
+
4855
+ // Hook to plugin uninstall.
4856
+ register_uninstall_hook( $this->_plugin_main_file_path, array( 'Freemius', '_uninstall_plugin_hook' ) );
4857
+
4858
+ $this->clear_module_main_file_cache();
4859
+ $this->clear_sync_cron();
4860
+ $this->clear_install_sync_cron();
4861
+
4862
+ if ( $this->is_registered() ) {
4863
+ // Send deactivation event.
4864
+ $this->sync_install( array(
4865
+ 'is_active' => false,
4866
+ ) );
4867
+ } else {
4868
+ if ( ! $this->has_api_connectivity() ) {
4869
+ // Reset connectivity test cache.
4870
+ unset( $this->_storage->connectivity_test );
4871
+ }
4872
+ }
4873
+
4874
+ // Clear API cache on deactivation.
4875
+ FS_Api::clear_cache();
4876
+
4877
+ $this->remove_sdk_reference();
4878
+ }
4879
+
4880
+ /**
4881
+ * @author Vova Feldman (@svovaf)
4882
+ * @since 1.1.6
4883
+ */
4884
+ private function remove_sdk_reference() {
4885
+ global $fs_active_plugins;
4886
+
4887
+ foreach ( $fs_active_plugins->plugins as $sdk_path => &$data ) {
4888
+ if ( $this->_plugin_basename == $data->plugin_path ) {
4889
+ unset( $fs_active_plugins->plugins[ $sdk_path ] );
4890
+ break;
4891
+ }
4892
+ }
4893
+
4894
+ fs_fallback_to_newest_active_sdk();
4895
+ }
4896
+
4897
+ /**
4898
+ * @author Vova Feldman (@svovaf)
4899
+ * @since 1.1.3
4900
+ *
4901
+ * @param bool $is_anonymous
4902
+ */
4903
+ private function set_anonymous_mode( $is_anonymous = true ) {
4904
+ // Store information regarding skip to try and opt-in the user
4905
+ // again in the future.
4906
+ $this->_storage->is_anonymous = array(
4907
+ 'is' => $is_anonymous,
4908
+ 'timestamp' => WP_FS__SCRIPT_START_TIME,
4909
+ 'version' => $this->get_plugin_version(),
4910
+ );
4911
+
4912
+ // Update anonymous mode cache.
4913
+ $this->_is_anonymous = $is_anonymous;
4914
+ }
4915
+
4916
+ /**
4917
+ * @author Vova Feldman (@svovaf)
4918
+ * @since 1.1.3
4919
+ */
4920
+ private function reset_anonymous_mode() {
4921
+ unset( $this->_storage->is_anonymous );
4922
+
4923
+ /**
4924
+ * Ensure that this field is also "false", otherwise, if the current module's type is "theme" and the module
4925
+ * has no menus, the opt-in popup will not be shown immediately (in this case, the user will have to click
4926
+ * on the admin notice that contains the opt-in link in order to trigger the opt-in popup).
4927
+ *
4928
+ * @author Leo Fajardo (@leorw)
4929
+ * @since 1.2.2
4930
+ */
4931
+ unset( $this->_is_anonymous );
4932
+ }
4933
+
4934
+ /**
4935
+ * Clears the anonymous mode and redirects to the opt-in screen.
4936
+ *
4937
+ * @author Vova Feldman (@svovaf)
4938
+ * @since 1.1.7
4939
+ */
4940
+ function connect_again() {
4941
+ if ( ! $this->is_anonymous() ) {
4942
+ return;
4943
+ }
4944
+
4945
+ $this->reset_anonymous_mode();
4946
+
4947
+ fs_redirect( $this->get_activation_url() );
4948
+ }
4949
+
4950
+ /**
4951
+ * Skip account connect, and set anonymous mode.
4952
+ *
4953
+ * @author Vova Feldman (@svovaf)
4954
+ * @since 1.1.1
4955
+ */
4956
+ private function skip_connection() {
4957
+ $this->_logger->entrance();
4958
+
4959
+ $this->_admin_notices->remove_sticky( 'connect_account' );
4960
+
4961
+ $this->set_anonymous_mode();
4962
+
4963
+ // Send anonymous skip event.
4964
+ // No user identified info nor any tracking will be sent after the user skips the opt-in.
4965
+ $this->get_api_plugin_scope()->call( 'skip.json', 'put', array(
4966
+ 'uid' => $this->get_anonymous_id(),
4967
+ ) );
4968
+ }
4969
+
4970
+ /**
4971
+ * Plugin version update hook.
4972
+ *
4973
+ * @author Vova Feldman (@svovaf)
4974
+ * @since 1.0.4
4975
+ */
4976
+ private function update_plugin_version_event() {
4977
+ $this->_logger->entrance();
4978
+
4979
+ if ( ! $this->is_registered() ) {
4980
+ return;
4981
+ }
4982
+
4983
+ $this->schedule_install_sync();
4984
+ // $this->sync_install( array(), true );
4985
+ }
4986
+
4987
+ /**
4988
+ * Return a list of modified plugins since the last sync.
4989
+ *
4990
+ * Note:
4991
+ * There's no point to store a plugins counter since even if the number of
4992
+ * plugins didn't change, we still need to check if the versions are all the
4993
+ * same and the activity state is similar.
4994
+ *
4995
+ * @author Vova Feldman (@svovaf)
4996
+ * @since 1.1.8
4997
+ *
4998
+ * @return array|false
4999
+ */
5000
+ private function get_plugins_data_for_api() {
5001
+ // Alias.
5002
+ $option_name = 'all_plugins';
5003
+
5004
+ $all_cached_plugins = self::$_accounts->get_option( $option_name );
5005
+
5006
+ if ( ! is_object( $all_cached_plugins ) ) {
5007
+ $all_cached_plugins = (object) array(
5008
+ 'timestamp' => '',
5009
+ 'md5' => '',
5010
+ 'plugins' => array(),
5011
+ );
5012
+ }
5013
+
5014
+ $time = time();
5015
+
5016
+ if ( ! empty( $all_cached_plugins->timestamp ) &&
5017
+ ( $time - $all_cached_plugins->timestamp ) < WP_FS__TIME_5_MIN_IN_SEC
5018
+ ) {
5019
+ // Don't send plugin updates if last update was in the past 5 min.
5020
+ return false;
5021
+ }
5022
+
5023
+ // Write timestamp to lock the logic.
5024
+ $all_cached_plugins->timestamp = $time;
5025
+ self::$_accounts->set_option( $option_name, $all_cached_plugins, true );
5026
+
5027
+ // Reload options from DB.
5028
+ self::$_accounts->load( true );
5029
+ $all_cached_plugins = self::$_accounts->get_option( $option_name );
5030
+
5031
+ if ( $time != $all_cached_plugins->timestamp ) {
5032
+ // If timestamp is different, then another thread captured the lock.
5033
+ return false;
5034
+ }
5035
+
5036
+ // Check if there's a change in plugins.
5037
+ $all_plugins = self::get_all_plugins();
5038
+
5039
+ // Check if plugins changed.
5040
+ ksort( $all_plugins );
5041
+
5042
+ $plugins_signature = '';
5043
+ foreach ( $all_plugins as $basename => $data ) {
5044
+ $plugins_signature .= $data['slug'] . ',' .
5045
+ $data['Version'] . ',' .
5046
+ ( $data['is_active'] ? '1' : '0' ) . ';';
5047
+ }
5048
+
5049
+ // Check if plugins status changed (version or active/inactive).
5050
+ $plugins_changed = ( $all_cached_plugins->md5 !== md5( $plugins_signature ) );
5051
+
5052
+ $plugins_update_data = array();
5053
+
5054
+ if ( $plugins_changed ) {
5055
+ // Change in plugins, report changes.
5056
+
5057
+ // Update existing plugins info.
5058
+ foreach ( $all_cached_plugins->plugins as $basename => $data ) {
5059
+ if ( ! isset( $all_plugins[ $basename ] ) ) {
5060
+ // Plugin uninstalled.
5061
+ $uninstalled_plugin_data = $data;
5062
+ $uninstalled_plugin_data['is_active'] = false;
5063
+ $uninstalled_plugin_data['is_uninstalled'] = true;
5064
+ $plugins_update_data[] = $uninstalled_plugin_data;
5065
+
5066
+ unset( $all_plugins[ $basename ] );
5067
+ unset( $all_cached_plugins->plugins[ $basename ] );
5068
+ } else if ( $data['is_active'] !== $all_plugins[ $basename ]['is_active'] ||
5069
+ $data['version'] !== $all_plugins[ $basename ]['Version']
5070
+ ) {
5071
+ // Plugin activated or deactivated, or version changed.
5072
+ $all_cached_plugins->plugins[ $basename ]['is_active'] = $all_plugins[ $basename ]['is_active'];
5073
+ $all_cached_plugins->plugins[ $basename ]['version'] = $all_plugins[ $basename ]['Version'];
5074
+
5075
+ $plugins_update_data[] = $all_cached_plugins->plugins[ $basename ];
5076
+ }
5077
+ }
5078
+
5079
+ // Find new plugins that weren't yet seen before.
5080
+ foreach ( $all_plugins as $basename => $data ) {
5081
+ if ( ! isset( $all_cached_plugins->plugins[ $basename ] ) ) {
5082
+ // New plugin.
5083
+ $new_plugin = array(
5084
+ 'slug' => $data['slug'],
5085
+ 'version' => $data['Version'],
5086
+ 'title' => $data['Name'],
5087
+ 'is_active' => $data['is_active'],
5088
+ 'is_uninstalled' => false,
5089
+ );
5090
+
5091
+ $plugins_update_data[] = $new_plugin;
5092
+ $all_cached_plugins->plugins[ $basename ] = $new_plugin;
5093
+ }
5094
+ }
5095
+
5096
+ $all_cached_plugins->md5 = md5( $plugins_signature );
5097
+ $all_cached_plugins->timestamp = $time;
5098
+ self::$_accounts->set_option( $option_name, $all_cached_plugins, true );
5099
+ }
5100
+
5101
+ return $plugins_update_data;
5102
+ }
5103
+
5104
+ /**
5105
+ * Return a list of modified themes since the last sync.
5106
+ *
5107
+ * Note:
5108
+ * There's no point to store a themes counter since even if the number of
5109
+ * themes didn't change, we still need to check if the versions are all the
5110
+ * same and the activity state is similar.
5111
+ *
5112
+ * @author Vova Feldman (@svovaf)
5113
+ * @since 1.1.8
5114
+ *
5115
+ * @return array|false
5116
+ */
5117
+ private function get_themes_data_for_api() {
5118
+ // Alias.
5119
+ $option_name = 'all_themes';
5120
+
5121
+ $all_cached_themes = self::$_accounts->get_option( $option_name );
5122
+
5123
+ if ( ! is_object( $all_cached_themes ) ) {
5124
+ $all_cached_themes = (object) array(
5125
+ 'timestamp' => '',
5126
+ 'md5' => '',
5127
+ 'themes' => array(),
5128
+ );
5129
+ }
5130
+
5131
+ $time = time();
5132
+
5133
+ if ( ! empty( $all_cached_themes->timestamp ) &&
5134
+ ( $time - $all_cached_themes->timestamp ) < WP_FS__TIME_5_MIN_IN_SEC
5135
+ ) {
5136
+ // Don't send theme updates if last update was in the past 5 min.
5137
+ return false;
5138
+ }
5139
+
5140
+ // Write timestamp to lock the logic.
5141
+ $all_cached_themes->timestamp = $time;
5142
+ self::$_accounts->set_option( $option_name, $all_cached_themes, true );
5143
+
5144
+ // Reload options from DB.
5145
+ self::$_accounts->load( true );
5146
+ $all_cached_themes = self::$_accounts->get_option( $option_name );
5147
+
5148
+ if ( $time != $all_cached_themes->timestamp ) {
5149
+ // If timestamp is different, then another thread captured the lock.
5150
+ return false;
5151
+ }
5152
+
5153
+ // Get active theme.
5154
+ $active_theme = wp_get_theme();
5155
+ $active_theme_stylesheet = $active_theme->get_stylesheet();
5156
+
5157
+ // Check if there's a change in themes.
5158
+ $all_themes = wp_get_themes();
5159
+
5160
+ // Check if themes changed.
5161
+ ksort( $all_themes );
5162
+
5163
+ $themes_signature = '';
5164
+ foreach ( $all_themes as $slug => $data ) {
5165
+ $is_active = ( $slug === $active_theme_stylesheet );
5166
+ $themes_signature .= $slug . ',' .
5167
+ $data->version . ',' .
5168
+ ( $is_active ? '1' : '0' ) . ';';
5169
+ }
5170
+
5171
+ // Check if themes status changed (version or active/inactive).
5172
+ $themes_changed = ( $all_cached_themes->md5 !== md5( $themes_signature ) );
5173
+
5174
+ $themes_update_data = array();
5175
+
5176
+ if ( $themes_changed ) {
5177
+ // Change in themes, report changes.
5178
+
5179
+ // Update existing themes info.
5180
+ foreach ( $all_cached_themes->themes as $slug => $data ) {
5181
+ $is_active = ( $slug === $active_theme_stylesheet );
5182
+
5183
+ if ( ! isset( $all_themes[ $slug ] ) ) {
5184
+ // Plugin uninstalled.
5185
+ $uninstalled_theme_data = $data;
5186
+ $uninstalled_theme_data['is_active'] = false;
5187
+ $uninstalled_theme_data['is_uninstalled'] = true;
5188
+ $themes_update_data[] = $uninstalled_theme_data;
5189
+
5190
+ unset( $all_themes[ $slug ] );
5191
+ unset( $all_cached_themes->themes[ $slug ] );
5192
+ } else if ( $data['is_active'] !== $is_active ||
5193
+ $data['version'] !== $all_themes[ $slug ]->version
5194
+ ) {
5195
+ // Plugin activated or deactivated, or version changed.
5196
+
5197
+ $all_cached_themes->themes[ $slug ]['is_active'] = $is_active;
5198
+ $all_cached_themes->themes[ $slug ]['version'] = $all_themes[ $slug ]->version;
5199
+
5200
+ $themes_update_data[] = $all_cached_themes->themes[ $slug ];
5201
+ }
5202
+ }
5203
+
5204
+ // Find new themes that weren't yet seen before.
5205
+ foreach ( $all_themes as $slug => $data ) {
5206
+ if ( ! isset( $all_cached_themes->themes[ $slug ] ) ) {
5207
+ $is_active = ( $slug === $active_theme_stylesheet );
5208
+
5209
+ // New plugin.
5210
+ $new_plugin = array(
5211
+ 'slug' => $slug,
5212
+ 'version' => $data->version,
5213
+ 'title' => $data->name,
5214
+ 'is_active' => $is_active,
5215
+ 'is_uninstalled' => false,
5216
+ );
5217
+
5218
+ $themes_update_data[] = $new_plugin;
5219
+ $all_cached_themes->themes[ $slug ] = $new_plugin;
5220
+ }
5221
+ }
5222
+
5223
+ $all_cached_themes->md5 = md5( $themes_signature );
5224
+ $all_cached_themes->timestamp = time();
5225
+ self::$_accounts->set_option( $option_name, $all_cached_themes, true );
5226
+ }
5227
+
5228
+ return $themes_update_data;
5229
+ }
5230
+
5231
+ /**
5232
+ * Update install details.
5233
+ *
5234
+ * @author Vova Feldman (@svovaf)
5235
+ * @since 1.1.2
5236
+ *
5237
+ * @param string[] string $override
5238
+ * @param bool $include_plugins Since 1.1.8 by default include plugin changes.
5239
+ * @param bool $include_themes Since 1.1.8 by default include plugin changes.
5240
+ *
5241
+ * @return array
5242
+ */
5243
+ private function get_install_data_for_api(
5244
+ array $override,
5245
+ $include_plugins = true,
5246
+ $include_themes = true
5247
+ ) {
5248
+ /**
5249
+ * @since 1.1.8 Also send plugin updates.
5250
+ */
5251
+ if ( $include_plugins && ! isset( $override['plugins'] ) ) {
5252
+ $plugins = $this->get_plugins_data_for_api();
5253
+ if ( ! empty( $plugins ) ) {
5254
+ $override['plugins'] = $plugins;
5255
+ }
5256
+ }
5257
+ /**
5258
+ * @since 1.1.8 Also send themes updates.
5259
+ */
5260
+ if ( $include_themes && ! isset( $override['themes'] ) ) {
5261
+ $themes = $this->get_themes_data_for_api();
5262
+ if ( ! empty( $themes ) ) {
5263
+ $override['themes'] = $themes;
5264
+ }
5265
+ }
5266
+
5267
+ return array_merge( array(
5268
+ 'version' => $this->get_plugin_version(),
5269
+ 'is_premium' => $this->is_premium(),
5270
+ 'language' => get_bloginfo( 'language' ),
5271
+ 'charset' => get_bloginfo( 'charset' ),
5272
+ 'platform_version' => get_bloginfo( 'version' ),
5273
+ 'sdk_version' => $this->version,
5274
+ 'programming_language_version' => phpversion(),
5275
+ 'title' => get_bloginfo( 'name' ),
5276
+ 'url' => get_site_url(),
5277
+ // Special params.
5278
+ 'is_active' => true,
5279
+ 'is_disconnected' => $this->is_tracking_prohibited(),
5280
+ 'is_uninstalled' => false,
5281
+ ), $override );
5282
+ }
5283
+
5284
+ /**
5285
+ * Update install only if changed.
5286
+ *
5287
+ * @author Vova Feldman (@svovaf)
5288
+ * @since 1.0.9
5289
+ *
5290
+ * @param string[] string $override
5291
+ * @param bool $flush
5292
+ *
5293
+ * @return false|object|string
5294
+ */
5295
+ private function send_install_update( $override = array(), $flush = false ) {
5296
+ $this->_logger->entrance();
5297
+
5298
+ $check_properties = $this->get_install_data_for_api( $override );
5299
+
5300
+ if ( $flush ) {
5301
+ $params = $check_properties;
5302
+ } else {
5303
+ $params = array();
5304
+ $special = array();
5305
+ $special_override = false;
5306
+
5307
+ foreach ( $check_properties as $p => $v ) {
5308
+ if ( property_exists( $this->_site, $p ) ) {
5309
+ if ( ( is_bool( $this->_site->{$p} ) || ! empty( $this->_site->{$p} ) ) &&
5310
+ $this->_site->{$p} != $v
5311
+ ) {
5312
+ $this->_site->{$p} = $v;
5313
+ $params[ $p ] = $v;
5314
+ }
5315
+ } else {
5316
+ $special[ $p ] = $v;
5317
+
5318
+ if ( isset( $override[ $p ] ) ||
5319
+ 'plugins' === $p ||
5320
+ 'themes' === $p
5321
+ ) {
5322
+ $special_override = true;
5323
+ }
5324
+ }
5325
+ }
5326
+
5327
+ if ( $special_override || 0 < count( $params ) ) {
5328
+ // Add special params only if has at least one
5329
+ // standard param, or if explicitly requested to
5330
+ // override a special param or a param which is not exist
5331
+ // in the install object.
5332
+ $params = array_merge( $params, $special );
5333
+ }
5334
+ }
5335
+
5336
+ if ( 0 < count( $params ) ) {
5337
+ // Update last install sync timestamp.
5338
+ $this->_storage->install_sync_timestamp = time();
5339
+
5340
+ $params['uid'] = $this->get_anonymous_id();
5341
+
5342
+ // Send updated values to FS.
5343
+ $site = $this->get_api_site_scope()->call( '/', 'put', $params );
5344
+
5345
+ if ( $this->is_api_result_entity( $site ) ) {
5346
+ // I successfully sent install update, clear scheduled sync if exist.
5347
+ $this->clear_install_sync_cron();
5348
+ }
5349
+
5350
+ return $site;
5351
+ }
5352
+
5353
+ return false;
5354
+ }
5355
+
5356
+ /**
5357
+ * Update install only if changed.
5358
+ *
5359
+ * @author Vova Feldman (@svovaf)
5360
+ * @since 1.0.9
5361
+ *
5362
+ * @param string[] string $override
5363
+ * @param bool $flush
5364
+ */
5365
+ private function sync_install( $override = array(), $flush = false ) {
5366
+ $this->_logger->entrance();
5367
+
5368
+ $site = $this->send_install_update( $override, $flush );
5369
+
5370
+ if ( false === $site ) {
5371
+ // No sync required.
5372
+ return;
5373
+ }
5374
+
5375
+ if ( ! $this->is_api_result_entity( $site ) ) {
5376
+ // Failed to sync, don't update locally.
5377
+ return;
5378
+ }
5379
+
5380
+ $plan = $this->get_plan();
5381
+ $this->_site = new FS_Site( $site );
5382
+ $this->_site->plan = $plan;
5383
+
5384
+ $this->_store_site( true );
5385
+ }
5386
+
5387
+ /**
5388
+ * Track install's custom event.
5389
+ *
5390
+ * IMPORTANT:
5391
+ * Custom event tracking is currently only supported for specific clients.
5392
+ * If you are not one of them, please don't use this method. If you will,
5393
+ * the API will simply ignore your request based on the plugin ID.
5394
+ *
5395
+ * Need custom tracking for your plugin or theme?
5396
+ * If you are interested in custom event tracking please contact yo@freemius.com
5397
+ * for further details.
5398
+ *
5399
+ * @author Vova Feldman (@svovaf)
5400
+ * @since 1.2.1
5401
+ *
5402
+ * @param string $name Event name.
5403
+ * @param array $properties Associative key/value array with primitive values only
5404
+ * @param bool $process_at A valid future date-time in the following format Y-m-d H:i:s.
5405
+ * @param bool $once If true, event will be tracked only once. IMPORTANT: Still trigger the API call.
5406
+ *
5407
+ * @return object|false Event data or FALSE on failure.
5408
+ *
5409
+ * @throws \Freemius_InvalidArgumentException
5410
+ */
5411
+ public function track_event( $name, $properties = array(), $process_at = false, $once = false ) {
5412
+ $this->_logger->entrance( http_build_query( array( 'name' => $name, 'once' => $once ) ) );
5413
+
5414
+ if ( ! $this->is_registered() ) {
5415
+ return false;
5416
+ }
5417
+
5418
+ $event = array( 'type' => $name );
5419
+
5420
+ if ( is_numeric( $process_at ) && $process_at > time() ) {
5421
+ $event['process_at'] = $process_at;
5422
+ }
5423
+
5424
+ if ( $once ) {
5425
+ $event['once'] = true;
5426
+ }
5427
+
5428
+ if ( ! empty( $properties ) ) {
5429
+ // Verify associative array values are primitive.
5430
+ foreach ( $properties as $k => $v ) {
5431
+ if ( ! is_scalar( $v ) ) {
5432
+ throw new Freemius_InvalidArgumentException( 'The $properties argument must be an associative key/value array with primitive values only.' );
5433
+ }
5434
+ }
5435
+
5436
+ $event['properties'] = $properties;
5437
+ }
5438
+
5439
+ $result = $this->get_api_site_scope()->call( 'events.json', 'post', $event );
5440
+
5441
+ return $this->is_api_error( $result ) ?
5442
+ false :
5443
+ $result;
5444
+ }
5445
+
5446
+ /**
5447
+ * Track install's custom event only once, but it still triggers the API call.
5448
+ *
5449
+ * IMPORTANT:
5450
+ * Custom event tracking is currently only supported for specific clients.
5451
+ * If you are not one of them, please don't use this method. If you will,
5452
+ * the API will simply ignore your request based on the plugin ID.
5453
+ *
5454
+ * Need custom tracking for your plugin or theme?
5455
+ * If you are interested in custom event tracking please contact yo@freemius.com
5456
+ * for further details.
5457
+ *
5458
+ * @author Vova Feldman (@svovaf)
5459
+ * @since 1.2.1
5460
+ *
5461
+ * @param string $name Event name.
5462
+ * @param array $properties Associative key/value array with primitive values only
5463
+ * @param bool $process_at A valid future date-time in the following format Y-m-d H:i:s.
5464
+ *
5465
+ * @return object|false Event data or FALSE on failure.
5466
+ *
5467
+ * @throws \Freemius_InvalidArgumentException
5468
+ *
5469
+ * @user Freemius::track_event()
5470
+ */
5471
+ public function track_event_once( $name, $properties = array(), $process_at = false ) {
5472
+ return $this->track_event( $name, $properties, $process_at, true );
5473
+ }
5474
+
5475
+ /**
5476
+ * Plugin uninstall hook.
5477
+ *
5478
+ * @author Vova Feldman (@svovaf)
5479
+ * @since 1.0.1
5480
+ *
5481
+ * @param bool $check_user Enforce checking if user have plugins activation privileges.
5482
+ */
5483
+ function _uninstall_plugin_event( $check_user = true ) {
5484
+ $this->_logger->entrance( 'slug = ' . $this->_slug );
5485
+
5486
+ if ( $check_user && ! current_user_can( 'activate_plugins' ) ) {
5487
+ return;
5488
+ }
5489
+
5490
+ $params = array();
5491
+ $uninstall_reason = null;
5492
+ if ( isset( $this->_storage->uninstall_reason ) ) {
5493
+ $uninstall_reason = $this->_storage->uninstall_reason;
5494
+ $params['reason_id'] = $uninstall_reason->id;
5495
+ $params['reason_info'] = $uninstall_reason->info;
5496
+ }
5497
+
5498
+ if ( ! $this->is_registered() ) {
5499
+ // Send anonymous uninstall event only if user submitted a feedback.
5500
+ if ( isset( $uninstall_reason ) ) {
5501
+ if ( isset( $uninstall_reason->is_anonymous ) && ! $uninstall_reason->is_anonymous ) {
5502
+ $this->opt_in( false, false, false, false, true );
5503
+ } else {
5504
+ $params['uid'] = $this->get_anonymous_id();
5505
+ $this->get_api_plugin_scope()->call( 'uninstall.json', 'put', $params );
5506
+ }
5507
+ }
5508
+ } else {
5509
+ // Send uninstall event.
5510
+ $this->send_install_update( array_merge( $params, array(
5511
+ 'is_active' => false,
5512
+ 'is_uninstalled' => true,
5513
+ ) ) );
5514
+ }
5515
+
5516
+ // @todo Decide if we want to delete plugin information from db.
5517
+ }
5518
+
5519
+ /**
5520
+ * @author Vova Feldman (@svovaf)
5521
+ * @since 1.1.1
5522
+ *
5523
+ * @return string
5524
+ */
5525
+ function premium_plugin_basename() {
5526
+ return "{$this->_slug}-premium/" . basename( $this->_free_plugin_basename );
5527
+ }
5528
+
5529
+ /**
5530
+ * Uninstall plugin hook. Called only when connected his account with Freemius for active sites tracking.
5531
+ *
5532
+ * @author Vova Feldman (@svovaf)
5533
+ * @since 1.0.2
5534
+ */
5535
+ public static function _uninstall_plugin_hook() {
5536
+ self::_load_required_static();
5537
+
5538
+ self::$_static_logger->entrance();
5539
+
5540
+ if ( ! current_user_can( 'activate_plugins' ) ) {
5541
+ return;
5542
+ }
5543
+
5544
+ $plugin_file = substr( current_filter(), strlen( 'uninstall_' ) );
5545
+
5546
+ self::$_static_logger->info( 'plugin = ' . $plugin_file );
5547
+
5548
+ define( 'WP_FS__UNINSTALL_MODE', true );
5549
+
5550
+ $fs = self::get_instance_by_file( $plugin_file );
5551
+
5552
+ if ( is_object( $fs ) ) {
5553
+ self::require_plugin_essentials();
5554
+
5555
+ if ( is_plugin_active( $fs->_free_plugin_basename ) ||
5556
+ is_plugin_active( $fs->premium_plugin_basename() )
5557
+ ) {
5558
+ // Deleting Free or Premium plugin version while the other version still installed.
5559
+ return;
5560
+ }
5561
+
5562
+ $fs->_uninstall_plugin_event();
5563
+
5564
+ $fs->do_action( 'after_uninstall' );
5565
+ }
5566
+ }
5567
+
5568
+ #----------------------------------------------------------------------------------
5569
+ #region Plugin Information
5570
+ #----------------------------------------------------------------------------------
5571
+
5572
+ /**
5573
+ * Load WordPress core plugin.php essential module.
5574
+ *
5575
+ * @author Vova Feldman (@svovaf)
5576
+ * @since 1.1.1
5577
+ */
5578
+ private static function require_plugin_essentials() {
5579
+ if ( ! function_exists( 'get_plugins' ) ) {
5580
+ self::$_static_logger->log( 'Including wp-admin/includes/plugin.php...' );
5581
+
5582
+ require_once ABSPATH . 'wp-admin/includes/plugin.php';
5583
+ }
5584
+ }
5585
+
5586
+ /**
5587
+ * Load WordPress core pluggable.php module.
5588
+ *
5589
+ * @author Vova Feldman (@svovaf)
5590
+ * @since 1.1.2
5591
+ */
5592
+ private static function require_pluggable_essentials() {
5593
+ if ( ! function_exists( 'wp_get_current_user' ) ) {
5594
+ require_once ABSPATH . 'wp-includes/pluggable.php';
5595
+ }
5596
+ }
5597
+
5598
+ /**
5599
+ * Return plugin data.
5600
+ *
5601
+ * @author Vova Feldman (@svovaf)
5602
+ * @since 1.0.1
5603
+ *
5604
+ * @return array
5605
+ */
5606
+ function get_plugin_data() {
5607
+ if ( ! isset( $this->_plugin_data ) ) {
5608
+ self::require_plugin_essentials();
5609
+
5610
+ if ( $this->is_plugin() ) {
5611
+ /**
5612
+ * @author Vova Feldman (@svovaf)
5613
+ * @since 1.2.0 When using get_plugin_data() do NOT translate plugin data.
5614
+ *
5615
+ * @link https://github.com/Freemius/wordpress-sdk/issues/77
5616
+ */
5617
+ $plugin_data = get_plugin_data(
5618
+ $this->_plugin_main_file_path,
5619
+ false,
5620
+ false
5621
+ );
5622
+ } else {
5623
+ $theme_data = wp_get_theme();
5624
+
5625
+ $plugin_data = array(
5626
+ 'Name' => $theme_data->get( 'Name' ),
5627
+ 'Version' => $theme_data->get( 'Version' ),
5628
+ 'Author' => $theme_data->get( 'Author' ),
5629
+ 'Description' => $theme_data->get( 'Description' ),
5630
+ 'PluginURI' => $theme_data->get( 'ThemeURI' ),
5631
+ );
5632
+ }
5633
+
5634
+ $this->_plugin_data = $plugin_data;
5635
+ }
5636
+
5637
+ return $this->_plugin_data;
5638
+ }
5639
+
5640
+ /**
5641
+ * @author Vova Feldman (@svovaf)
5642
+ * @since 1.0.1
5643
+ * @since 1.2.2.5 If slug not set load slug by module ID.
5644
+ *
5645
+ * @return string Plugin slug.
5646
+ */
5647
+ function get_slug() {
5648
+ if ( ! isset( $this->_slug ) ) {
5649
+ $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
5650
+ $this->_slug = $id_slug_type_path_map[ $this->_module_id ]['slug'];
5651
+ }
5652
+
5653
+ return $this->_slug;
5654
+ }
5655
+
5656
+ /**
5657
+ * @author Vova Feldman (@svovaf)
5658
+ * @since 1.2.1.7
5659
+ *
5660
+ * @return string Plugin slug.
5661
+ */
5662
+ function get_target_folder_name() {
5663
+ return $this->_slug . ( $this->can_use_premium_code() ? '-premium' : '' );
5664
+ }
5665
+
5666
+ /**
5667
+ * @author Vova Feldman (@svovaf)
5668
+ * @since 1.0.1
5669
+ *
5670
+ * @return number Plugin ID.
5671
+ */
5672
+ function get_id() {
5673
+ return $this->_plugin->id;
5674
+ }
5675
+
5676
+ /**
5677
+ * @author Vova Feldman (@svovaf)
5678
+ * @since 1.2.1.5
5679
+ *
5680
+ * @return string Freemius SDK version
5681
+ */
5682
+ function get_sdk_version() {
5683
+ return $this->version;
5684
+ }
5685
+
5686
+ /**
5687
+ * @author Vova Feldman (@svovaf)
5688
+ * @since 1.2.1.5
5689
+ *
5690
+ * @return number Parent plugin ID (if parent exist).
5691
+ */
5692
+ function get_parent_id() {
5693
+ return $this->is_addon() ?
5694
+ $this->get_parent_instance()->get_id() :
5695
+ $this->_plugin->id;
5696
+ }
5697
+
5698
+ /**
5699
+ * @author Vova Feldman (@svovaf)
5700
+ * @since 1.0.1
5701
+ *
5702
+ * @return string Plugin public key.
5703
+ */
5704
+ function get_public_key() {
5705
+ return $this->_plugin->public_key;
5706
+ }
5707
+
5708
+ /**
5709
+ * Will be available only on sandbox mode.
5710
+ *
5711
+ * @author Vova Feldman (@svovaf)
5712
+ * @since 1.0.4
5713
+ *
5714
+ * @return mixed Plugin secret key.
5715
+ */
5716
+ function get_secret_key() {
5717
+ return $this->_plugin->secret_key;
5718
+ }
5719
+
5720
+ /**
5721
+ * @author Vova Feldman (@svovaf)
5722
+ * @since 1.1.1
5723
+ *
5724
+ * @return bool
5725
+ */
5726
+ function has_secret_key() {
5727
+ return ! empty( $this->_plugin->secret_key );
5728
+ }
5729
+
5730
+ /**
5731
+ * @author Vova Feldman (@svovaf)
5732
+ * @since 1.0.9
5733
+ *
5734
+ * @return string
5735
+ */
5736
+ function get_plugin_name() {
5737
+ $this->_logger->entrance();
5738
+
5739
+ if ( ! isset( $this->_plugin_name ) ) {
5740
+ $plugin_data = $this->get_plugin_data();
5741
+
5742
+ // Get name.
5743
+ $this->_plugin_name = $plugin_data['Name'];
5744
+
5745
+ // Check if plugin name contains "(Premium)" suffix and remove it.
5746
+ $suffix = ' (premium)';
5747
+ $suffix_len = strlen( $suffix );
5748
+
5749
+ if ( strlen( $plugin_data['Name'] ) > $suffix_len &&
5750
+ $suffix === substr( strtolower( $plugin_data['Name'] ), - $suffix_len )
5751
+ ) {
5752
+ $this->_plugin_name = substr( $plugin_data['Name'], 0, - $suffix_len );
5753
+ }
5754
+
5755
+ $this->_logger->departure( 'Name = ' . $this->_plugin_name );
5756
+ }
5757
+
5758
+ return $this->_plugin_name;
5759
+ }
5760
+
5761
+ /**
5762
+ * @author Vova Feldman (@svovaf)
5763
+ * @since 1.0.0
5764
+ *
5765
+ * @return string
5766
+ */
5767
+ function get_plugin_version() {
5768
+ $this->_logger->entrance();
5769
+
5770
+ $plugin_data = $this->get_plugin_data();
5771
+
5772
+ $this->_logger->departure( 'Version = ' . $plugin_data['Version'] );
5773
+
5774
+ return $this->apply_filters( 'plugin_version', $plugin_data['Version'] );
5775
+ }
5776
+
5777
+ /**
5778
+ * @author Vova Feldman (@svovaf)
5779
+ * @since 1.2.1.7
5780
+ *
5781
+ * @return string
5782
+ */
5783
+ function get_plugin_title() {
5784
+ $this->_logger->entrance();
5785
+
5786
+ $title = $this->_plugin->title;
5787
+
5788
+ return $this->apply_filters( 'plugin_title', $title );
5789
+ }
5790
+
5791
+ /**
5792
+ * @author Vova Feldman (@svovaf)
5793
+ * @since 1.2.2.7
5794
+ *
5795
+ * @param bool $lowercase
5796
+ *
5797
+ * @return string
5798
+ */
5799
+ function get_module_label( $lowercase = false ) {
5800
+ $label = $this->is_addon() ?
5801
+ $this->get_text( 'addon' ) :
5802
+ ( $this->is_plugin() ?
5803
+ $this->get_text( 'plugin' ) :
5804
+ $this->get_text( 'theme' ) );
5805
+
5806
+ if ( $lowercase ) {
5807
+ $label = strtolower( $lowercase );
5808
+ }
5809
+
5810
+ return $label;
5811
+ }
5812
+
5813
+ /**
5814
+ * @author Vova Feldman (@svovaf)
5815
+ * @since 1.0.4
5816
+ *
5817
+ * @return string
5818
+ */
5819
+ function get_plugin_basename() {
5820
+ if ( ! isset( $this->_plugin_basename ) ) {
5821
+ if ( $this->is_plugin() ) {
5822
+ $this->_plugin_basename = plugin_basename( $this->_plugin_main_file_path );
5823
+ } else {
5824
+ $this->_plugin_basename = basename( dirname( $this->_plugin_main_file_path ) );
5825
+ }
5826
+ }
5827
+
5828
+ return $this->_plugin_basename;
5829
+ }
5830
+
5831
+ function get_plugin_folder_name() {
5832
+ $this->_logger->entrance();
5833
+
5834
+ $plugin_folder = $this->_plugin_basename;
5835
+
5836
+ while ( '.' !== dirname( $plugin_folder ) ) {
5837
+ $plugin_folder = dirname( $plugin_folder );
5838
+ }
5839
+
5840
+ $this->_logger->departure( 'Folder Name = ' . $plugin_folder );
5841
+
5842
+ return $plugin_folder;
5843
+ }
5844
+
5845
+ #endregion ------------------------------------------------------------------
5846
+
5847
+ /* Account
5848
+ ------------------------------------------------------------------------------------------------------------------*/
5849
+
5850
+ /**
5851
+ * Find plugin's slug by plugin's basename.
5852
+ *
5853
+ * @author Vova Feldman (@svovaf)
5854
+ * @since 1.0.9
5855
+ *
5856
+ * @param string $plugin_base_name
5857
+ *
5858
+ * @return false|string
5859
+ */
5860
+ private static function find_slug_by_basename( $plugin_base_name ) {
5861
+ $file_slug_map = self::$_accounts->get_option( 'file_slug_map', array() );
5862
+
5863
+ if ( ! array( $file_slug_map ) || ! isset( $file_slug_map[ $plugin_base_name ] ) ) {
5864
+ return false;
5865
+ }
5866
+
5867
+ return $file_slug_map[ $plugin_base_name ];
5868
+ }
5869
+
5870
+ /**
5871
+ * Store the map between the plugin's basename to the slug.
5872
+ *
5873
+ * @author Vova Feldman (@svovaf)
5874
+ * @since 1.0.9
5875
+ */
5876
+ private function store_file_slug_map() {
5877
+ $file_slug_map = self::$_accounts->get_option( 'file_slug_map', array() );
5878
+
5879
+ if ( ! array( $file_slug_map ) ) {
5880
+ $file_slug_map = array();
5881
+ }
5882
+
5883
+ if ( ! isset( $file_slug_map[ $this->_plugin_basename ] ) ||
5884
+ $file_slug_map[ $this->_plugin_basename ] !== $this->_slug
5885
+ ) {
5886
+ $file_slug_map[ $this->_plugin_basename ] = $this->_slug;
5887
+ self::$_accounts->set_option( 'file_slug_map', $file_slug_map, true );
5888
+ }
5889
+ }
5890
+
5891
+ /**
5892
+ * @return FS_User[]
5893
+ */
5894
+ static function get_all_users() {
5895
+ $users = self::$_accounts->get_option( 'users', array() );
5896
+
5897
+ if ( ! is_array( $users ) ) {
5898
+ $users = array();
5899
+ }
5900
+
5901
+ return $users;
5902
+ }
5903
+
5904
+ /**
5905
+ * @param string $module_type
5906
+ *
5907
+ * @return FS_Site[]
5908
+ */
5909
+ private static function get_all_sites( $module_type = WP_FS__MODULE_TYPE_PLUGIN ) {
5910
+ $sites = self::get_account_option( 'sites', $module_type );
5911
+
5912
+ if ( ! is_array( $sites ) ) {
5913
+ $sites = array();
5914
+ }
5915
+
5916
+ return $sites;
5917
+ }
5918
+
5919
+ /**
5920
+ * @author Leo Fajardo (@leorw)
5921
+ *
5922
+ * @since 1.2.2
5923
+ *
5924
+ * @param string $option_name
5925
+ * @param string $module_type
5926
+ *
5927
+ * @return mixed
5928
+ */
5929
+ private static function get_account_option( $option_name, $module_type ) {
5930
+ if ( WP_FS__MODULE_TYPE_PLUGIN !== $module_type ) {
5931
+ $option_name = $module_type . '_' . $option_name;
5932
+ }
5933
+
5934
+ return self::$_accounts->get_option( $option_name, array() );
5935
+ }
5936
+
5937
+ /**
5938
+ * @author Leo Fajardo (@leorw)
5939
+ *
5940
+ * @since 1.2.2
5941
+ *
5942
+ * @param string $option_name
5943
+ * @param mixed $option_value
5944
+ * @param bool $store
5945
+ */
5946
+ private function set_account_option( $option_name, $option_value, $store ) {
5947
+ self::set_account_option_by_module(
5948
+ $this->_module_type,
5949
+ $option_name,
5950
+ $option_value,
5951
+ $store
5952
+ );
5953
+ }
5954
+
5955
+ /**
5956
+ * @author Vova Feldman (@svovaf)
5957
+ *
5958
+ * @since 1.2.2.7
5959
+ *
5960
+ * @param string $module_type
5961
+ * @param string $option_name
5962
+ * @param mixed $option_value
5963
+ * @param bool $store
5964
+ */
5965
+ private static function set_account_option_by_module( $module_type, $option_name, $option_value, $store ) {
5966
+ if ( WP_FS__MODULE_TYPE_PLUGIN != $module_type ) {
5967
+ $option_name = $module_type . '_' . $option_name;
5968
+ }
5969
+
5970
+ self::$_accounts->set_option( $option_name, $option_value, $store );
5971
+ }
5972
+
5973
+ /**
5974
+ * @author Vova Feldman (@svovaf)
5975
+ * @since 1.0.6
5976
+ *
5977
+ * @param string $module_type
5978
+ *
5979
+ * @return FS_Plugin_License[]
5980
+ */
5981
+ private static function get_all_licenses( $module_type = WP_FS__MODULE_TYPE_PLUGIN ) {
5982
+ $licenses = self::get_account_option( 'licenses', $module_type );
5983
+
5984
+ if ( ! is_array( $licenses ) ) {
5985
+ $licenses = array();
5986
+ }
5987
+
5988
+ return $licenses;
5989
+ }
5990
+
5991
+ /**
5992
+ * @param string|bool $module_type
5993
+ *
5994
+ * @return FS_Plugin_Plan[]
5995
+ */
5996
+ private static function get_all_plans( $module_type = false ) {
5997
+ $plans = self::get_account_option( 'plans', $module_type );
5998
+
5999
+ if ( ! is_array( $plans ) ) {
6000
+ $plans = array();
6001
+ }
6002
+
6003
+ return $plans;
6004
+ }
6005
+
6006
+ /**
6007
+ * @author Vova Feldman (@svovaf)
6008
+ * @since 1.0.4
6009
+ *
6010
+ * @return FS_Plugin_Tag[]
6011
+ */
6012
+ private static function get_all_updates() {
6013
+ $updates = self::$_accounts->get_option( 'updates', array() );
6014
+
6015
+ if ( ! is_array( $updates ) ) {
6016
+ $updates = array();
6017
+ }
6018
+
6019
+ return $updates;
6020
+ }
6021
+
6022
+ /**
6023
+ * @author Vova Feldman (@svovaf)
6024
+ * @since 1.0.6
6025
+ *
6026
+ * @return array<number,FS_Plugin[]>|false
6027
+ */
6028
+ private static function get_all_addons() {
6029
+ $addons = self::$_accounts->get_option( 'addons', array() );
6030
+
6031
+ if ( ! is_array( $addons ) ) {
6032
+ $addons = array();
6033
+ }
6034
+
6035
+ return $addons;
6036
+ }
6037
+
6038
+ /**
6039
+ * @author Vova Feldman (@svovaf)
6040
+ * @since 1.0.6
6041
+ *
6042
+ * @return FS_Plugin[]|false
6043
+ */
6044
+ private static function get_all_account_addons() {
6045
+ $addons = self::$_accounts->get_option( 'account_addons', array() );
6046
+
6047
+ if ( ! is_array( $addons ) ) {
6048
+ $addons = array();
6049
+ }
6050
+
6051
+ return $addons;
6052
+ }
6053
+
6054
+ /**
6055
+ * Check if user has connected his account (opted-in).
6056
+ *
6057
+ * Note:
6058
+ * If the user opted-in and opted-out on a later stage,
6059
+ * this will still return true. If you want to check if the
6060
+ * user is currently opted-in, use:
6061
+ * `$fs->is_registered() && $fs->is_tracking_allowed()`
6062
+ *
6063
+ * @author Vova Feldman (@svovaf)
6064
+ * @since 1.0.1
6065
+ * @return bool
6066
+ */
6067
+ function is_registered() {
6068
+ return is_object( $this->_user );
6069
+ }
6070
+
6071
+ /**
6072
+ * Returns TRUE if the user opted-in and didn't disconnect (opt-out).
6073
+ *
6074
+ * @author Leo Fajardo (@leorw)
6075
+ * @since 1.2.1.5
6076
+ *
6077
+ * @return bool
6078
+ */
6079
+ function is_tracking_allowed() {
6080
+ return ( is_object( $this->_site ) && true !== $this->_site->is_disconnected );
6081
+ }
6082
+
6083
+ /**
6084
+ * @author Vova Feldman (@svovaf)
6085
+ * @since 1.0.4
6086
+ *
6087
+ * @return FS_Plugin
6088
+ */
6089
+ function get_plugin() {
6090
+ return $this->_plugin;
6091
+ }
6092
+
6093
+ /**
6094
+ * @author Vova Feldman (@svovaf)
6095
+ * @since 1.0.3
6096
+ *
6097
+ * @return FS_User
6098
+ */
6099
+ function get_user() {
6100
+ return $this->_user;
6101
+ }
6102
+
6103
+ /**
6104
+ * @author Vova Feldman (@svovaf)
6105
+ * @since 1.0.3
6106
+ *
6107
+ * @return FS_Site
6108
+ */
6109
+ function get_site() {
6110
+ return $this->_site;
6111
+ }
6112
+
6113
+ /**
6114
+ * Get plugin add-ons.
6115
+ *
6116
+ * @author Vova Feldman (@svovaf)
6117
+ * @since 1.0.6
6118
+ *
6119
+ * @since 1.1.7.3 If not yet loaded, fetch data from the API.
6120
+ *
6121
+ * @param bool $flush
6122
+ *
6123
+ * @return FS_Plugin[]|false
6124
+ */
6125
+ function get_addons( $flush = false ) {
6126
+ $this->_logger->entrance();
6127
+
6128
+ if ( ! $this->_has_addons ) {
6129
+ return false;
6130
+ }
6131
+
6132
+ $addons = $this->sync_addons( $flush );
6133
+
6134
+ return ( ! is_array( $addons ) || empty( $addons ) ) ?
6135
+ false :
6136
+ $addons;
6137
+ }
6138
+
6139
+ /**
6140
+ * @author Vova Feldman (@svovaf)
6141
+ * @since 1.0.6
6142
+ *
6143
+ * @return FS_Plugin[]|false
6144
+ */
6145
+ function get_account_addons() {
6146
+ $this->_logger->entrance();
6147
+
6148
+ $addons = self::get_all_account_addons();
6149
+
6150
+ if ( ! is_array( $addons ) ||
6151
+ ! isset( $addons[ $this->_plugin->id ] ) ||
6152
+ ! is_array( $addons[ $this->_plugin->id ] ) ||
6153
+ 0 === count( $addons[ $this->_plugin->id ] )
6154
+ ) {
6155
+ return false;
6156
+ }
6157
+
6158
+ return $addons[ $this->_plugin->id ];
6159
+ }
6160
+
6161
+ /**
6162
+ * Check if user has any
6163
+ *
6164
+ * @author Vova Feldman (@svovaf)
6165
+ * @since 1.1.6
6166
+ *
6167
+ * @return bool
6168
+ */
6169
+ function has_account_addons() {
6170
+ $addons = $this->get_account_addons();
6171
+
6172
+ return is_array( $addons ) && ( 0 < count( $addons ) );
6173
+ }
6174
+
6175
+
6176
+ /**
6177
+ * Get add-on by ID (from local data).
6178
+ *
6179
+ * @author Vova Feldman (@svovaf)
6180
+ * @since 1.0.6
6181
+ *
6182
+ * @param number $id
6183
+ *
6184
+ * @return FS_Plugin|false
6185
+ */
6186
+ function get_addon( $id ) {
6187
+ $this->_logger->entrance();
6188
+
6189
+ $addons = $this->get_addons();
6190
+
6191
+ if ( is_array( $addons ) ) {
6192
+ foreach ( $addons as $addon ) {
6193
+ if ( $id == $addon->id ) {
6194
+ return $addon;
6195
+ }
6196
+ }
6197
+ }
6198
+
6199
+ return false;
6200
+ }
6201
+
6202
+ /**
6203
+ * Get add-on by slug (from local data).
6204
+ *
6205
+ * @author Vova Feldman (@svovaf)
6206
+ * @since 1.0.6
6207
+ *
6208
+ * @param string $slug
6209
+ *
6210
+ * @param bool $flush
6211
+ *
6212
+ * @return FS_Plugin|false
6213
+ */
6214
+ function get_addon_by_slug( $slug, $flush = false ) {
6215
+ $this->_logger->entrance();
6216
+
6217
+ $addons = $this->get_addons( $flush );
6218
+
6219
+ if ( is_array( $addons ) ) {
6220
+ foreach ( $addons as $addon ) {
6221
+ if ( $slug === $addon->slug ) {
6222
+ return $addon;
6223
+ }
6224
+ }
6225
+ }
6226
+
6227
+ return false;
6228
+ }
6229
+
6230
+ #----------------------------------------------------------------------------------
6231
+ #region Plans & Licensing
6232
+ #----------------------------------------------------------------------------------
6233
+
6234
+ /**
6235
+ * Check if running premium plugin code.
6236
+ *
6237
+ * @author Vova Feldman (@svovaf)
6238
+ * @since 1.0.5
6239
+ *
6240
+ * @return bool
6241
+ */
6242
+ function is_premium() {
6243
+ return $this->_plugin->is_premium;
6244
+ }
6245
+
6246
+ /**
6247
+ * Get site's plan ID.
6248
+ *
6249
+ * @author Vova Feldman (@svovaf)
6250
+ * @since 1.0.2
6251
+ *
6252
+ * @return number
6253
+ */
6254
+ function get_plan_id() {
6255
+ return $this->_site->plan->id;
6256
+ }
6257
+
6258
+ /**
6259
+ * Get site's plan title.
6260
+ *
6261
+ * @author Vova Feldman (@svovaf)
6262
+ * @since 1.0.2
6263
+ *
6264
+ * @return string
6265
+ */
6266
+ function get_plan_title() {
6267
+ return $this->_site->plan->title;
6268
+ }
6269
+
6270
+ /**
6271
+ * @author Vova Feldman (@svovaf)
6272
+ * @since 1.0.9
6273
+ *
6274
+ * @return FS_Plugin_Plan|false
6275
+ */
6276
+ function get_plan() {
6277
+ return is_object( $this->_site->plan ) ?
6278
+ $this->_site->plan :
6279
+ false;
6280
+ }
6281
+
6282
+ /**
6283
+ * @author Vova Feldman (@svovaf)
6284
+ * @since 1.0.3
6285
+ *
6286
+ * @return bool
6287
+ */
6288
+ function is_trial() {
6289
+ $this->_logger->entrance();
6290
+
6291
+ if ( ! $this->is_registered() ) {
6292
+ return false;
6293
+ }
6294
+
6295
+ return $this->_site->is_trial();
6296
+ }
6297
+
6298
+ /**
6299
+ * Check if currently in a trial with payment method (credit card or paypal).
6300
+ *
6301
+ * @author Vova Feldman (@svovaf)
6302
+ * @since 1.1.7
6303
+ *
6304
+ * @return bool
6305
+ */
6306
+ function is_paid_trial() {
6307
+ $this->_logger->entrance();
6308
+
6309
+ if ( ! $this->is_trial() ) {
6310
+ return false;
6311
+ }
6312
+
6313
+ return $this->has_active_valid_license() && ( $this->_site->trial_plan_id == $this->_license->plan_id );
6314
+ }
6315
+
6316
+ /**
6317
+ * Check if trial already utilized.
6318
+ *
6319
+ * @since 1.0.9
6320
+ *
6321
+ * @return bool
6322
+ */
6323
+ function is_trial_utilized() {
6324
+ $this->_logger->entrance();
6325
+
6326
+ if ( ! $this->is_registered() ) {
6327
+ return false;
6328
+ }
6329
+
6330
+ return $this->_site->is_trial_utilized();
6331
+ }
6332
+
6333
+ /**
6334
+ * Get trial plan information (if in trial).
6335
+ *
6336
+ * @author Vova Feldman (@svovaf)
6337
+ * @since 1.0.9
6338
+ *
6339
+ * @return bool|FS_Plugin_Plan
6340
+ */
6341
+ function get_trial_plan() {
6342
+ $this->_logger->entrance();
6343
+
6344
+ if ( ! $this->is_trial() ) {
6345
+ return false;
6346
+ }
6347
+
6348
+ return $this->_storage->trial_plan;
6349
+ }
6350
+
6351
+ /**
6352
+ * Check if the user has an activate, non-expired license on current plugin's install.
6353
+ *
6354
+ * @since 1.0.9
6355
+ *
6356
+ * @return bool
6357
+ */
6358
+ function is_paying() {
6359
+ $this->_logger->entrance();
6360
+
6361
+ if ( ! $this->is_registered() ) {
6362
+ return false;
6363
+ }
6364
+
6365
+ if ( ! $this->has_paid_plan() ) {
6366
+ return false;
6367
+ }
6368
+
6369
+ return (
6370
+ ! $this->is_trial() &&
6371
+ 'free' !== $this->_site->plan->name &&
6372
+ $this->has_active_valid_license()
6373
+ );
6374
+ }
6375
+
6376
+ /**
6377
+ * @author Vova Feldman (@svovaf)
6378
+ * @since 1.0.4
6379
+ *
6380
+ * @return bool
6381
+ */
6382
+ function is_free_plan() {
6383
+ if ( ! $this->is_registered() ) {
6384
+ return true;
6385
+ }
6386
+
6387
+ if ( ! $this->has_paid_plan() ) {
6388
+ return true;
6389
+ }
6390
+
6391
+ return (
6392
+ 'free' === $this->_site->plan->name ||
6393
+ ! $this->has_features_enabled_license()
6394
+ );
6395
+ }
6396
+
6397
+ /**
6398
+ * @author Vova Feldman (@svovaf)
6399
+ * @since 1.0.5
6400
+ *
6401
+ * @return bool
6402
+ */
6403
+ function _has_premium_license() {
6404
+ $this->_logger->entrance();
6405
+
6406
+ $premium_license = $this->_get_available_premium_license();
6407
+
6408
+ return ( false !== $premium_license );
6409
+ }
6410
+
6411
+ /**
6412
+ * Check if user has any licenses associated with the plugin (including expired or blocking).
6413
+ *
6414
+ * @author Vova Feldman (@svovaf)
6415
+ * @since 1.1.7.3
6416
+ *
6417
+ * @return bool
6418
+ */
6419
+ private function has_any_license() {
6420
+ return is_array( $this->_licenses ) && ( 0 < count( $this->_licenses ) );
6421
+ }
6422
+
6423
+ /**
6424
+ * @author Vova Feldman (@svovaf)
6425
+ * @since 1.0.5
6426
+ *
6427
+ * @return FS_Plugin_License|false
6428
+ */
6429
+ function _get_available_premium_license() {
6430
+ $this->_logger->entrance();
6431
+
6432
+ if ( ! $this->has_paid_plan() ) {
6433
+ return false;
6434
+ }
6435
+
6436
+ if ( is_array( $this->_licenses ) ) {
6437
+ foreach ( $this->_licenses as $license ) {
6438
+ if ( ! $license->is_utilized() && $license->is_features_enabled() ) {
6439
+ return $license;
6440
+ }
6441
+ }
6442
+ }
6443
+
6444
+ return false;
6445
+ }
6446
+
6447
+ /**
6448
+ * Sync local plugin plans with remote server.
6449
+ *
6450
+ * @author Vova Feldman (@svovaf)
6451
+ * @since 1.0.5
6452
+ *
6453
+ * @return FS_Plugin_Plan[]|object
6454
+ */
6455
+ function _sync_plans() {
6456
+ $plans = $this->_fetch_plugin_plans();
6457
+
6458
+ if ( $this->is_array_instanceof( $plans, 'FS_Plugin_Plan' ) ) {
6459
+ $this->_plans = $plans;
6460
+ $this->_store_plans();
6461
+ }
6462
+
6463
+ $this->do_action( 'after_plans_sync', $plans );
6464
+
6465
+ return $this->_plans;
6466
+ }
6467
+
6468
+ /**
6469
+ * @author Vova Feldman (@svovaf)
6470
+ * @since 1.0.5
6471
+ *
6472
+ * @param number $id
6473
+ *
6474
+ * @return FS_Plugin_Plan|false
6475
+ */
6476
+ function _get_plan_by_id( $id ) {
6477
+ $this->_logger->entrance();
6478
+
6479
+ if ( ! is_array( $this->_plans ) || 0 === count( $this->_plans ) ) {
6480
+ $this->_sync_plans();
6481
+ }
6482
+
6483
+ foreach ( $this->_plans as $plan ) {
6484
+ if ( $id == $plan->id ) {
6485
+ return $plan;
6486
+ }
6487
+ }
6488
+
6489
+ return false;
6490
+ }
6491
+
6492
+ /**
6493
+ * @author Vova Feldman (@svovaf)
6494
+ * @since 1.1.8.1
6495
+ *
6496
+ * @param string $name
6497
+ *
6498
+ * @return FS_Plugin_Plan|false
6499
+ */
6500
+ private function get_plan_by_name( $name ) {
6501
+ $this->_logger->entrance();
6502
+
6503
+ if ( ! is_array( $this->_plans ) || 0 === count( $this->_plans ) ) {
6504
+ $this->_sync_plans();
6505
+ }
6506
+
6507
+ foreach ( $this->_plans as $plan ) {
6508
+ if ( $name == $plan->name ) {
6509
+ return $plan;
6510
+ }
6511
+ }
6512
+
6513
+ return false;
6514
+ }
6515
+
6516
+ /**
6517
+ * Sync local plugin plans with remote server.
6518
+ *
6519
+ * @author Vova Feldman (@svovaf)
6520
+ * @since 1.0.6
6521
+ *
6522
+ * @param number|bool $site_license_id
6523
+ *
6524
+ * @return FS_Plugin_License[]|object
6525
+ */
6526
+ function _sync_licenses( $site_license_id = false ) {
6527
+ $licenses = $this->_fetch_licenses( false, $site_license_id );
6528
+
6529
+ if ( $this->is_array_instanceof( $licenses, 'FS_Plugin_License' ) ) {
6530
+ $this->_licenses = $licenses;
6531
+ $this->_store_licenses();
6532
+ }
6533
+
6534
+ // Update current license.
6535
+ if ( is_object( $this->_license ) ) {
6536
+ $this->_license = $this->_get_license_by_id( $this->_license->id );
6537
+ }
6538
+
6539
+ return $this->_licenses;
6540
+ }
6541
+
6542
+ /**
6543
+ * @author Vova Feldman (@svovaf)
6544
+ * @since 1.0.5
6545
+ *
6546
+ * @param number $id
6547
+ *
6548
+ * @return FS_Plugin_License|false
6549
+ */
6550
+ function _get_license_by_id( $id ) {
6551
+ $this->_logger->entrance();
6552
+
6553
+ if ( ! is_numeric( $id ) ) {
6554
+ return false;
6555
+ }
6556
+
6557
+ if ( ! $this->has_any_license() ) {
6558
+ $this->_sync_licenses();
6559
+ }
6560
+
6561
+ foreach ( $this->_licenses as $license ) {
6562
+ if ( $id == $license->id ) {
6563
+ return $license;
6564
+ }
6565
+ }
6566
+
6567
+ return false;
6568
+ }
6569
+
6570
+ /**
6571
+ * Sync site's license with user licenses.
6572
+ *
6573
+ * @author Vova Feldman (@svovaf)
6574
+ * @since 1.0.6
6575
+ *
6576
+ * @param FS_Plugin_License|null $new_license
6577
+ */
6578
+ function _update_site_license( $new_license ) {
6579
+ $this->_logger->entrance();
6580
+
6581
+ $this->_license = $new_license;
6582
+
6583
+ if ( ! is_object( $new_license ) ) {
6584
+ $this->_site->license_id = null;
6585
+ $this->_sync_site_subscription( null );
6586
+
6587
+ return;
6588
+ }
6589
+
6590
+ $this->_site->license_id = $this->_license->id;
6591
+
6592
+ if ( ! is_array( $this->_licenses ) ) {
6593
+ $this->_licenses = array();
6594
+ }
6595
+
6596
+ $is_license_found = false;
6597
+ for ( $i = 0, $len = count( $this->_licenses ); $i < $len; $i ++ ) {
6598
+ if ( $new_license->id == $this->_licenses[ $i ]->id ) {
6599
+ $this->_licenses[ $i ] = $new_license;
6600
+
6601
+ $is_license_found = true;
6602
+ break;
6603
+ }
6604
+ }
6605
+
6606
+ // If new license just append.
6607
+ if ( ! $is_license_found ) {
6608
+ $this->_licenses[] = $new_license;
6609
+ }
6610
+
6611
+ $this->_sync_site_subscription( $new_license );
6612
+ }
6613
+
6614
+ /**
6615
+ * Sync site's subscription.
6616
+ *
6617
+ * @author Vova Feldman (@svovaf)
6618
+ * @since 1.0.9
6619
+ *
6620
+ * @param FS_Plugin_License|null $license
6621
+ *
6622
+ * @return bool|\FS_Subscription
6623
+ */
6624
+ private function _sync_site_subscription( $license ) {
6625
+ if ( ! is_object( $license ) ) {
6626
+ unset( $this->_storage->subscription );
6627
+
6628
+ return false;
6629
+ }
6630
+
6631
+ // Load subscription details if not lifetime.
6632
+ $subscription = $license->is_lifetime() ?
6633
+ false :
6634
+ $this->_fetch_site_license_subscription();
6635
+
6636
+ if ( is_object( $subscription ) && ! isset( $subscription->error ) ) {
6637
+ $this->_storage->subscription = $subscription;
6638
+ } else {
6639
+ unset( $this->_storage->subscription );
6640
+ }
6641
+
6642
+ return $subscription;
6643
+ }
6644
+
6645
+ /**
6646
+ * @author Vova Feldman (@svovaf)
6647
+ * @since 1.0.6
6648
+ *
6649
+ * @return bool|\FS_Plugin_License
6650
+ */
6651
+ function _get_license() {
6652
+ return $this->_license;
6653
+ }
6654
+
6655
+ /**
6656
+ * @return bool|\FS_Subscription
6657
+ */
6658
+ function _get_subscription() {
6659
+ return isset( $this->_storage->subscription ) ?
6660
+ $this->_storage->subscription :
6661
+ false;
6662
+ }
6663
+
6664
+ /**
6665
+ * @author Vova Feldman (@svovaf)
6666
+ * @since 1.0.2
6667
+ *
6668
+ * @param string $plan Plan name
6669
+ * @param bool $exact If true, looks for exact plan. If false, also check "higher" plans.
6670
+ *
6671
+ * @return bool
6672
+ */
6673
+ function is_plan( $plan, $exact = false ) {
6674
+ $this->_logger->entrance();
6675
+
6676
+ if ( ! $this->is_registered() ) {
6677
+ return false;
6678
+ }
6679
+
6680
+ $plan = strtolower( $plan );
6681
+
6682
+ if ( $this->_site->plan->name === $plan ) // Exact plan.
6683
+ {
6684
+ return true;
6685
+ } else if ( $exact ) // Required exact, but plans are different.
6686
+ {
6687
+ return false;
6688
+ }
6689
+
6690
+ $current_plan_order = - 1;
6691
+ $required_plan_order = - 1;
6692
+ for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
6693
+ if ( $plan === $this->_plans[ $i ]->name ) {
6694
+ $required_plan_order = $i;
6695
+ } else if ( $this->_site->plan->name === $this->_plans[ $i ]->name ) {
6696
+ $current_plan_order = $i;
6697
+ }
6698
+ }
6699
+
6700
+ return ( $current_plan_order > $required_plan_order );
6701
+ }
6702
+
6703
+ /**
6704
+ * Check if module has only one plan.
6705
+ *
6706
+ * @author Vova Feldman (@svovaf)
6707
+ * @since 1.2.1.7
6708
+ *
6709
+ * @return bool
6710
+ */
6711
+ function is_single_plan() {
6712
+ $this->_logger->entrance();
6713
+
6714
+ if ( ! $this->is_registered() ||
6715
+ ! is_array( $this->_plans ) ||
6716
+ 0 === count( $this->_plans )
6717
+ ) {
6718
+ return true;
6719
+ }
6720
+
6721
+ return ( 1 === count( $this->_plans ) );
6722
+ }
6723
+
6724
+ /**
6725
+ * Check if plan based on trial. If not in trial mode, should return false.
6726
+ *
6727
+ * @since 1.0.9
6728
+ *
6729
+ * @param string $plan Plan name
6730
+ * @param bool $exact If true, looks for exact plan. If false, also check "higher" plans.
6731
+ *
6732
+ * @return bool
6733
+ */
6734
+ function is_trial_plan( $plan, $exact = false ) {
6735
+ $this->_logger->entrance();
6736
+
6737
+ if ( ! $this->is_registered() ) {
6738
+ return false;
6739
+ }
6740
+
6741
+ if ( ! $this->is_trial() ) {
6742
+ return false;
6743
+ }
6744
+
6745
+ if ( ! isset( $this->_storage->trial_plan ) ) {
6746
+ // Store trial plan information.
6747
+ $this->_enrich_site_trial_plan( true );
6748
+ }
6749
+
6750
+ if ( $this->_storage->trial_plan->name === $plan ) // Exact plan.
6751
+ {
6752
+ return true;
6753
+ } else if ( $exact ) // Required exact, but plans are different.
6754
+ {
6755
+ return false;
6756
+ }
6757
+
6758
+ $current_plan_order = - 1;
6759
+ $required_plan_order = - 1;
6760
+ for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
6761
+ if ( $plan === $this->_plans[ $i ]->name ) {
6762
+ $required_plan_order = $i;
6763
+ } else if ( $this->_storage->trial_plan->name === $this->_plans[ $i ]->name ) {
6764
+ $current_plan_order = $i;
6765
+ }
6766
+ }
6767
+
6768
+ return ( $current_plan_order > $required_plan_order );
6769
+ }
6770
+
6771
+ /**
6772
+ * Check if plugin has any paid plans.
6773
+ *
6774
+ * @author Vova Feldman (@svovaf)
6775
+ * @since 1.0.7
6776
+ *
6777
+ * @return bool
6778
+ */
6779
+ function has_paid_plan() {
6780
+ return $this->_has_paid_plans ||
6781
+ FS_Plan_Manager::instance()->has_paid_plan( $this->_plans );
6782
+ }
6783
+
6784
+ /**
6785
+ * Check if plugin has any plan with a trail.
6786
+ *
6787
+ * @author Vova Feldman (@svovaf)
6788
+ * @since 1.0.9
6789
+ *
6790
+ * @return bool
6791
+ */
6792
+ function has_trial_plan() {
6793
+ if ( ! $this->is_registered() ) {
6794
+ /**
6795
+ * @author Vova Feldman(@svovaf)
6796
+ * @since 1.2.1.5
6797
+ *
6798
+ * Allow setting a trial from the SDK without calling the API.
6799
+ * But, if the user did opt-in, continue using the real data from the API.
6800
+ */
6801
+ if ( $this->_trial_days >= 0 ) {
6802
+ return true;
6803
+ }
6804
+
6805
+ return false;
6806
+ }
6807
+
6808
+ return $this->_storage->get( 'has_trial_plan', false );
6809
+ }
6810
+
6811
+ /**
6812
+ * Check if plugin has any free plan, or is it premium only.
6813
+ *
6814
+ * Note: If no plans configured, assume plugin is free.
6815
+ *
6816
+ * @author Vova Feldman (@svovaf)
6817
+ * @since 1.0.7
6818
+ *
6819
+ * @return bool
6820
+ */
6821
+ function has_free_plan() {
6822
+ return ! $this->is_only_premium();
6823
+ }
6824
+
6825
+ /**
6826
+ * Displays a license activation dialog box when the user clicks on the "Activate License"
6827
+ * or "Change License" link on the plugins
6828
+ * page.
6829
+ *
6830
+ * @author Leo Fajardo (@leorw)
6831
+ * @since 1.1.9
6832
+ */
6833
+ function _add_license_activation_dialog_box() {
6834
+ $vars = array(
6835
+ 'id' => $this->_module_id,
6836
+ );
6837
+
6838
+ fs_require_template( 'forms/license-activation.php', $vars );
6839
+ fs_require_template( 'forms/resend-key.php', $vars );
6840
+ }
6841
+
6842
+ /**
6843
+ * Displays the opt-out dialog box when the user clicks on the "Opt Out" link on the "Plugins"
6844
+ * page.
6845
+ *
6846
+ * @author Leo Fajardo (@leorw)
6847
+ * @since 1.2.1.5
6848
+ */
6849
+ function _add_optout_dialog() {
6850
+ if ( $this->is_theme() ) {
6851
+ $vars = null;
6852
+ fs_require_once_template( '/js/jquery.content-change.php', $vars );
6853
+ }
6854
+
6855
+ $vars = array( 'id' => $this->_module_id );
6856
+ fs_require_template( 'forms/optout.php', $vars );
6857
+ }
6858
+
6859
+ /**
6860
+ * Prepare page to include all required UI and logic for the license activation dialog.
6861
+ *
6862
+ * @author Vova Feldman (@svovaf)
6863
+ * @since 1.2.0
6864
+ */
6865
+ function _add_license_activation() {
6866
+ if ( ! $this->is_user_admin() ) {
6867
+ // Only admins can activate a license.
6868
+ return;
6869
+ }
6870
+
6871
+ if ( ! $this->has_paid_plan() ) {
6872
+ // Module doesn't have any paid plans.
6873
+ return;
6874
+ }
6875
+
6876
+ if ( ! $this->is_premium() ) {
6877
+ // Only add license activation logic to the premium version.
6878
+ return;
6879
+ }
6880
+
6881
+ // Add license activation link and AJAX request handler.
6882
+ if ( self::is_plugins_page() ) {
6883
+ /**
6884
+ * @since 1.2.0 Add license action link only on plugins page.
6885
+ */
6886
+ $this->_add_license_action_link();
6887
+ }
6888
+
6889
+ // Add license activation AJAX callback.
6890
+ $this->add_ajax_action( 'activate_license', array( &$this, '_activate_license_ajax_action' ) );
6891
+
6892
+ // Add resend license AJAX callback.
6893
+ $this->add_ajax_action( 'resend_license_key', array( &$this, '_resend_license_key_ajax_action' ) );
6894
+ }
6895
+
6896
+ /**
6897
+ * @author Leo Fajardo (@leorw)
6898
+ * @since 1.1.9
6899
+ */
6900
+ function _activate_license_ajax_action() {
6901
+ $this->_logger->entrance();
6902
+
6903
+ $this->check_ajax_referer( 'activate_license' );
6904
+
6905
+ $license_key = trim( fs_request_get( 'license_key' ) );
6906
+
6907
+ if ( empty( $license_key ) ) {
6908
+ exit;
6909
+ }
6910
+
6911
+ $plugin_id = fs_request_get( 'module_id', '', 'post' );
6912
+ $fs = ( $plugin_id == $this->_module_id ) ?
6913
+ $this :
6914
+ $this->get_addon_instance( $plugin_id );
6915
+
6916
+ $error = false;
6917
+ $next_page = false;
6918
+
6919
+ if ( $fs->is_registered() ) {
6920
+ $api = $fs->get_api_site_scope();
6921
+ $install = $api->call( '/', 'put', array(
6922
+ 'license_key' => $fs->apply_filters( 'license_key', $license_key )
6923
+ ) );
6924
+
6925
+ if ( isset( $install->error ) ) {
6926
+ $error = $install->error->message;
6927
+ } else {
6928
+ $parent_fs = $fs->is_addon() ?
6929
+ $fs->get_parent_instance() :
6930
+ $fs;
6931
+
6932
+ $next_page = $parent_fs->_get_sync_license_url( $fs->get_id(), true );
6933
+
6934
+ $fs->reconnect_locally();
6935
+ }
6936
+ } else {
6937
+ $next_page = $fs->opt_in( false, false, false, $license_key );
6938
+
6939
+ if ( isset( $next_page->error ) ) {
6940
+ $error = $next_page->error;
6941
+ }
6942
+ }
6943
+
6944
+ $result = array(
6945
+ 'success' => ( false === $error )
6946
+ );
6947
+
6948
+ if ( false !== $error ) {
6949
+ $result['error'] = $error;
6950
+ } else {
6951
+ $result['next_page'] = $next_page;
6952
+ }
6953
+
6954
+ echo json_encode( $result );
6955
+
6956
+ exit;
6957
+ }
6958
+
6959
+ /**
6960
+ * Billing update AJAX callback.
6961
+ *
6962
+ * @author Vova Feldman (@svovaf)
6963
+ * @since 1.2.1.5
6964
+ */
6965
+ function _update_billing_ajax_action() {
6966
+ $this->_logger->entrance();
6967
+
6968
+ $this->check_ajax_referer( 'update_billing' );
6969
+
6970
+ if ( ! $this->is_user_admin() ) {
6971
+ // Only for admins.
6972
+ self::shoot_ajax_failure();
6973
+ }
6974
+
6975
+ $billing = fs_request_get( 'billing' );
6976
+
6977
+ $api = $this->get_api_user_scope();
6978
+ $result = $api->call( '/billing.json', 'put', array_merge( $billing, array(
6979
+ 'plugin_id' => $this->get_parent_id(),
6980
+ ) ) );
6981
+
6982
+ if ( ! $this->is_api_result_entity( $result ) ) {
6983
+ self::shoot_ajax_failure();
6984
+ }
6985
+
6986
+ // Purge cached billing.
6987
+ $this->get_api_user_scope()->purge_cache( 'billing.json' );
6988
+
6989
+ self::shoot_ajax_success();
6990
+ }
6991
+
6992
+ /**
6993
+ * Trial start for anonymous users (AJAX callback).
6994
+ *
6995
+ * @author Vova Feldman (@svovaf)
6996
+ * @since 1.2.1.5
6997
+ */
6998
+ function _start_trial_ajax_action() {
6999
+ $this->_logger->entrance();
7000
+
7001
+ $this->check_ajax_referer( 'start_trial' );
7002
+
7003
+ if ( ! $this->is_user_admin() ) {
7004
+ // Only for admins.
7005
+ self::shoot_ajax_failure();
7006
+ }
7007
+
7008
+ $trial_data = fs_request_get( 'trial' );
7009
+
7010
+ $next_page = $this->opt_in(
7011
+ false,
7012
+ false,
7013
+ false,
7014
+ false,
7015
+ false,
7016
+ $trial_data['plan_id']
7017
+ );
7018
+
7019
+ if ( is_object( $next_page ) && $this->is_api_error( $next_page ) ) {
7020
+ self::shoot_ajax_failure(
7021
+ isset( $next_page->error ) ?
7022
+ $next_page->error->message :
7023
+ var_export( $next_page, true )
7024
+ );
7025
+ }
7026
+
7027
+ $this->shoot_ajax_success( array(
7028
+ 'next_page' => $next_page,
7029
+ ) );
7030
+ }
7031
+
7032
+ /**
7033
+ * @author Leo Fajardo (@leorw)
7034
+ * @since 1.2.0
7035
+ */
7036
+ function _resend_license_key_ajax_action() {
7037
+ $this->_logger->entrance();
7038
+
7039
+ $this->check_ajax_referer( 'resend_license_key' );
7040
+
7041
+ $email_address = sanitize_email( trim( fs_request_get( 'email', '', 'post' ) ) );
7042
+
7043
+ if ( empty( $email_address ) ) {
7044
+ exit;
7045
+ }
7046
+
7047
+ $error = false;
7048
+
7049
+ $api = $this->get_api_plugin_scope();
7050
+ $result = $api->call( '/licenses/resend.json', 'post',
7051
+ array(
7052
+ 'email' => $email_address,
7053
+ 'url' => home_url(),
7054
+ )
7055
+ );
7056
+
7057
+ if ( is_object( $result ) && isset( $result->error ) ) {
7058
+ $error = $result->error;
7059
+
7060
+ if ( in_array( $error->code, array( 'invalid_email', 'no_user' ) ) ) {
7061
+ $error = $this->get_text( 'email-not-found' );
7062
+ } else if ( 'no_license' === $error->code ) {
7063
+ $error = $this->get_text( 'no-active-licenses' );
7064
+ } else {
7065
+ $error = $error->message;
7066
+ }
7067
+ }
7068
+
7069
+ $licenses = array(
7070
+ 'success' => ( false === $error )
7071
+ );
7072
+
7073
+ if ( false !== $error ) {
7074
+ $licenses['error'] = sprintf( '%s... %s', $this->get_text( 'oops' ), strtolower( $error ) );
7075
+ }
7076
+
7077
+ echo json_encode( $licenses );
7078
+
7079
+ exit;
7080
+ }
7081
+
7082
+ /**
7083
+ * @author Vova Feldman (@svovaf)
7084
+ * @since 1.2.1.8
7085
+ *
7086
+ * @var string
7087
+ */
7088
+ private static $_pagenow;
7089
+
7090
+ /**
7091
+ * Get current page or the referer if executing a WP AJAX request.
7092
+ *
7093
+ * @author Vova Feldman (@svovaf)
7094
+ * @since 1.2.1.8
7095
+ *
7096
+ * @return string
7097
+ */
7098
+ static function get_current_page() {
7099
+ if ( ! isset( self::$_pagenow ) ) {
7100
+ global $pagenow;
7101
+
7102
+ self::$_pagenow = $pagenow;
7103
+
7104
+ if ( self::is_ajax() &&
7105
+ 'admin-ajax.php' === $pagenow
7106
+ ) {
7107
+ $referer = wp_get_raw_referer();
7108
+
7109
+ if ( is_string( $referer ) ) {
7110
+ $parts = explode( '?', $referer );
7111
+
7112
+ self::$_pagenow = basename( $parts[0] );
7113
+ }
7114
+ }
7115
+ }
7116
+
7117
+ return self::$_pagenow;
7118
+ }
7119
+
7120
+ /**
7121
+ * Helper method to check if user in the plugins page.
7122
+ *
7123
+ * @author Vova Feldman (@svovaf)
7124
+ * @since 1.2.1.5
7125
+ *
7126
+ * @return bool
7127
+ */
7128
+ static function is_plugins_page() {
7129
+ return ( 'plugins.php' === self::get_current_page() );
7130
+ }
7131
+
7132
+ /**
7133
+ * Helper method to check if user in the themes page.
7134
+ *
7135
+ * @author Vova Feldman (@svovaf)
7136
+ * @since 1.2.2.6
7137
+ *
7138
+ * @return bool
7139
+ */
7140
+ static function is_themes_page() {
7141
+ return ( 'themes.php' === self::get_current_page() );
7142
+ }
7143
+
7144
+ #----------------------------------------------------------------------------------
7145
+ #region URL Generators
7146
+ #----------------------------------------------------------------------------------
7147
+
7148
+ /**
7149
+ * Alias to pricing_url().
7150
+ *
7151
+ * @author Vova Feldman (@svovaf)
7152
+ * @since 1.0.2
7153
+ *
7154
+ * @uses pricing_url()
7155
+ *
7156
+ * @param string $period Billing cycle
7157
+ * @param bool $is_trial
7158
+ *
7159
+ * @return string
7160
+ */
7161
+ function get_upgrade_url( $period = WP_FS__PERIOD_ANNUALLY, $is_trial = false ) {
7162
+ return $this->pricing_url( $period, $is_trial );
7163
+ }
7164
+
7165
+ /**
7166
+ * @author Vova Feldman (@svovaf)
7167
+ * @since 1.0.9
7168
+ *
7169
+ * @uses get_upgrade_url()
7170
+ *
7171
+ * @return string
7172
+ */
7173
+ function get_trial_url() {
7174
+ return $this->get_upgrade_url( WP_FS__PERIOD_ANNUALLY, true );
7175
+ }
7176
+
7177
+ /**
7178
+ * Plugin's pricing URL.
7179
+ *
7180
+ * @author Vova Feldman (@svovaf)
7181
+ * @since 1.0.4
7182
+ *
7183
+ * @param string $billing_cycle Billing cycle
7184
+ *
7185
+ * @param bool $is_trial
7186
+ *
7187
+ * @return string
7188
+ */
7189
+ function pricing_url( $billing_cycle = WP_FS__PERIOD_ANNUALLY, $is_trial = false ) {
7190
+ $this->_logger->entrance();
7191
+
7192
+ $params = array(
7193
+ 'billing_cycle' => $billing_cycle
7194
+ );
7195
+
7196
+ if ( $is_trial ) {
7197
+ $params['trial'] = 'true';
7198
+ }
7199
+
7200
+ return $this->_get_admin_page_url( 'pricing', $params );
7201
+ }
7202
+
7203
+ /**
7204
+ * Checkout page URL.
7205
+ *
7206
+ * @author Vova Feldman (@svovaf)
7207
+ * @since 1.0.6
7208
+ *
7209
+ * @param string $billing_cycle Billing cycle
7210
+ * @param bool $is_trial
7211
+ * @param array $extra (optional) Extra parameters, override other query params.
7212
+ *
7213
+ * @return string
7214
+ */
7215
+ function checkout_url(
7216
+ $billing_cycle = WP_FS__PERIOD_ANNUALLY,
7217
+ $is_trial = false,
7218
+ $extra = array()
7219
+ ) {
7220
+ $this->_logger->entrance();
7221
+
7222
+ $params = array(
7223
+ 'checkout' => 'true',
7224
+ 'billing_cycle' => $billing_cycle,
7225
+ );
7226
+
7227
+ if ( $is_trial ) {
7228
+ $params['trial'] = 'true';
7229
+ }
7230
+
7231
+ /**
7232
+ * Params in extra override other params.
7233
+ */
7234
+ $params = array_merge( $params, $extra );
7235
+
7236
+ return $this->_get_admin_page_url( 'pricing', $params );
7237
+ }
7238
+
7239
+ /**
7240
+ * Add-on checkout URL.
7241
+ *
7242
+ * @author Vova Feldman (@svovaf)
7243
+ * @since 1.1.7
7244
+ *
7245
+ * @param number $addon_id
7246
+ * @param number $pricing_id
7247
+ * @param string $billing_cycle
7248
+ * @param bool $is_trial
7249
+ *
7250
+ * @return string
7251
+ */
7252
+ function addon_checkout_url(
7253
+ $addon_id,
7254
+ $pricing_id,
7255
+ $billing_cycle = WP_FS__PERIOD_ANNUALLY,
7256
+ $is_trial = false
7257
+ ) {
7258
+ return $this->checkout_url( $billing_cycle, $is_trial, array(
7259
+ 'plugin_id' => $addon_id,
7260
+ 'pricing_id' => $pricing_id,
7261
+ ) );
7262
+ }
7263
+
7264
+ #endregion
7265
+
7266
+ #endregion ------------------------------------------------------------------
7267
+
7268
+ /**
7269
+ * Check if plugin has any add-ons.
7270
+ *
7271
+ * @author Vova Feldman (@svovaf)
7272
+ * @since 1.0.5
7273
+ *
7274
+ * @since 1.1.7.3 Base logic only on the parameter provided by the developer in the init function.
7275
+ *
7276
+ * @return bool
7277
+ */
7278
+ function has_addons() {
7279
+ $this->_logger->entrance();
7280
+
7281
+ return $this->_has_addons;
7282
+ }
7283
+
7284
+ /**
7285
+ * Check if plugin can work in anonymous mode.
7286
+ *
7287
+ * @author Vova Feldman (@svovaf)
7288
+ * @since 1.0.9
7289
+ *
7290
+ * @return bool
7291
+ *
7292
+ * @deprecated Please use is_enable_anonymous() instead
7293
+ */
7294
+ function enable_anonymous() {
7295
+ return $this->_enable_anonymous;
7296
+ }
7297
+
7298
+ /**
7299
+ * Check if plugin can work in anonymous mode.
7300
+ *
7301
+ * @author Vova Feldman (@svovaf)
7302
+ * @since 1.1.9
7303
+ *
7304
+ * @return bool
7305
+ */
7306
+ function is_enable_anonymous() {
7307
+ return $this->_enable_anonymous;
7308
+ }
7309
+
7310
+ /**
7311
+ * Check if plugin is premium only (no free plans).
7312
+ *
7313
+ * @author Vova Feldman (@svovaf)
7314
+ * @since 1.1.9
7315
+ *
7316
+ * @return bool
7317
+ */
7318
+ function is_only_premium() {
7319
+ return $this->_is_premium_only;
7320
+ }
7321
+
7322
+ /**
7323
+ * Checks if the plugin's type is "plugin". The other type is "theme".
7324
+ *
7325
+ * @author Leo Fajardo (@leorw)
7326
+ * @since 1.2.2
7327
+ *
7328
+ * @return bool
7329
+ */
7330
+ function is_plugin() {
7331
+ return ( WP_FS__MODULE_TYPE_PLUGIN === $this->_module_type );
7332
+ }
7333
+
7334
+ /**
7335
+ * @author Leo Fajardo (@leorw)
7336
+ * @since 1.2.2
7337
+ *
7338
+ * @return string
7339
+ */
7340
+ function get_module_type() {
7341
+ if ( ! isset( $this->_module_type ) ) {
7342
+ $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
7343
+ $this->_module_type = $id_slug_type_path_map[ $this->_module_id ]['type'];
7344
+ }
7345
+
7346
+ return $this->_module_type;
7347
+ }
7348
+
7349
+ /**
7350
+ * @author Leo Fajardo (@leorw)
7351
+ * @since 1.2.2
7352
+ *
7353
+ * @return string
7354
+ */
7355
+ function get_plugin_main_file_path() {
7356
+ return $this->_plugin_main_file_path;
7357
+ }
7358
+
7359
+ /**
7360
+ * Check if module has a premium code version.
7361
+ *
7362
+ * Serviceware module might be freemium without any
7363
+ * premium code version, where the paid features
7364
+ * are all part of the service.
7365
+ *
7366
+ * @author Vova Feldman (@svovaf)
7367
+ * @since 1.2.1.6
7368
+ *
7369
+ * @return bool
7370
+ */
7371
+ function has_premium_version() {
7372
+ return $this->_has_premium_version;
7373
+ }
7374
+
7375
+ /**
7376
+ * Check if feature supported with current site's plan.
7377
+ *
7378
+ * @author Vova Feldman (@svovaf)
7379
+ * @since 1.0.1
7380
+ *
7381
+ * @todo IMPLEMENT
7382
+ *
7383
+ * @param number $feature_id
7384
+ *
7385
+ * @throws Exception
7386
+ */
7387
+ function is_feature_supported( $feature_id ) {
7388
+ throw new Exception( 'not implemented' );
7389
+ }
7390
+
7391
+ /**
7392
+ * @author Vova Feldman (@svovaf)
7393
+ * @since 1.0.1
7394
+ *
7395
+ * @return bool Is running in SSL/HTTPS
7396
+ */
7397
+ function is_ssl() {
7398
+ return WP_FS__IS_HTTPS;
7399
+ }
7400
+
7401
+ /**
7402
+ * @author Vova Feldman (@svovaf)
7403
+ * @since 1.0.9
7404
+ *
7405
+ * @return bool Is running in AJAX call.
7406
+ *
7407
+ * @link http://wordpress.stackexchange.com/questions/70676/how-to-check-if-i-am-in-admin-ajax
7408
+ */
7409
+ static function is_ajax() {
7410
+ return ( defined( 'DOING_AJAX' ) && DOING_AJAX );
7411
+ }
7412
+
7413
+ /**
7414
+ * Check if it's an AJAX call targeted for the current module.
7415
+ *
7416
+ * @author Vova Feldman (@svovaf)
7417
+ * @since 1.2.0
7418
+ *
7419
+ * @param array|string $actions Collection of AJAX actions.
7420
+ *
7421
+ * @return bool
7422
+ */
7423
+ function is_ajax_action( $actions ) {
7424
+ // Verify it's an ajax call.
7425
+ if ( ! self::is_ajax() ) {
7426
+ return false;
7427
+ }
7428
+
7429
+ // Verify the call is relevant for the plugin.
7430
+ if ( $this->_module_id != fs_request_get( 'module_id' ) ) {
7431
+ return false;
7432
+ }
7433
+
7434
+ // Verify it's one of the specified actions.
7435
+ if ( is_string( $actions ) ) {
7436
+ $actions = explode( ',', $actions );
7437
+ }
7438
+
7439
+ if ( is_array( $actions ) && 0 < count( $actions ) ) {
7440
+ $ajax_action = fs_request_get( 'action' );
7441
+
7442
+ foreach ( $actions as $action ) {
7443
+ if ( $ajax_action === $this->get_action_tag( $action ) ) {
7444
+ return true;
7445
+ }
7446
+ }
7447
+ }
7448
+
7449
+ return false;
7450
+ }
7451
+
7452
+ /**
7453
+ * Check if it's an AJAX call targeted for current request.
7454
+ *
7455
+ * @author Vova Feldman (@svovaf)
7456
+ * @since 1.2.0
7457
+ *
7458
+ * @param array|string $actions Collection of AJAX actions.
7459
+ * @param number|null $module_id
7460
+ *
7461
+ * @return bool
7462
+ */
7463
+ static function is_ajax_action_static( $actions, $module_id = null ) {
7464
+ // Verify it's an ajax call.
7465
+ if ( ! self::is_ajax() ) {
7466
+ return false;
7467
+ }
7468
+
7469
+
7470
+ if ( ! empty( $module_id ) ) {
7471
+ // Verify the call is relevant for the plugin.
7472
+ if ( $module_id != fs_request_get( 'module_id' ) ) {
7473
+ return false;
7474
+ }
7475
+ }
7476
+
7477
+ // Verify it's one of the specified actions.
7478
+ if ( is_string( $actions ) ) {
7479
+ $actions = explode( ',', $actions );
7480
+ }
7481
+
7482
+ if ( is_array( $actions ) && 0 < count( $actions ) ) {
7483
+ $ajax_action = fs_request_get( 'action' );
7484
+
7485
+ foreach ( $actions as $action ) {
7486
+ if ( $ajax_action === self::get_ajax_action_static( $action, $module_id ) ) {
7487
+ return true;
7488
+ }
7489
+ }
7490
+ }
7491
+
7492
+ return false;
7493
+ }
7494
+
7495
+ /**
7496
+ * @author Vova Feldman (@svovaf)
7497
+ * @since 1.1.7
7498
+ *
7499
+ * @return bool
7500
+ */
7501
+ static function is_cron() {
7502
+ return ( defined( 'DOING_CRON' ) && DOING_CRON );
7503
+ }
7504
+
7505
+ /**
7506
+ * Check if a real user is visiting the admin dashboard.
7507
+ *
7508
+ * @author Vova Feldman (@svovaf)
7509
+ * @since 1.1.7
7510
+ *
7511
+ * @return bool
7512
+ */
7513
+ function is_user_in_admin() {
7514
+ return is_admin() && ! self::is_ajax() && ! self::is_cron();
7515
+ }
7516
+
7517
+ /**
7518
+ * Check if a real user is in the customizer view.
7519
+ *
7520
+ * @author Vova Feldman (@svovaf)
7521
+ * @since 1.2.2.7
7522
+ *
7523
+ * @return bool
7524
+ */
7525
+ static function is_customizer() {
7526
+ return is_customize_preview();
7527
+ }
7528
+
7529
+ /**
7530
+ * Check if running in HTTPS and if site's plan matching the specified plan.
7531
+ *
7532
+ * @param string $plan
7533
+ * @param bool $exact
7534
+ *
7535
+ * @return bool
7536
+ */
7537
+ function is_ssl_and_plan( $plan, $exact = false ) {
7538
+ return ( $this->is_ssl() && $this->is_plan( $plan, $exact ) );
7539
+ }
7540
+
7541
+ /**
7542
+ * Construct plugin's settings page URL.
7543
+ *
7544
+ * @author Vova Feldman (@svovaf)
7545
+ * @since 1.0.4
7546
+ *
7547
+ * @param string $page
7548
+ * @param array $params
7549
+ *
7550
+ * @return string
7551
+ */
7552
+ function _get_admin_page_url( $page = '', $params = array() ) {
7553
+ if ( 0 < count( $params ) ) {
7554
+ foreach ( $params as $k => $v ) {
7555
+ $params[ $k ] = urlencode( $v );
7556
+ }
7557
+ }
7558
+
7559
+ $page_param = $this->_menu->get_slug( $page );
7560
+
7561
+ if ( empty( $page ) &&
7562
+ $this->is_theme() &&
7563
+ // Show the opt-in as an overlay for free wp.org themes or themes without any settings page.
7564
+ ( $this->is_free_wp_org_theme() || ! $this->has_settings_menu() ) ) {
7565
+ $params[ $this->get_unique_affix() . '_show_optin' ] = 'true';
7566
+
7567
+ return add_query_arg(
7568
+ $params,
7569
+ admin_url( 'themes.php' )
7570
+ );
7571
+ }
7572
+
7573
+ if ( ! $this->has_settings_menu() ) {
7574
+ if ( ! empty( $page ) ) {
7575
+ // Module doesn't have a setting page, but since the request is for
7576
+ // a specific Freemius page, use the admin.php path.
7577
+ return add_query_arg( array_merge( $params, array(
7578
+ 'page' => $page_param,
7579
+ ) ), admin_url( 'admin.php' ) );
7580
+ } else {
7581
+ if ( $this->is_activation_mode() ) {
7582
+ /**
7583
+ * @author Vova Feldman
7584
+ * @since 1.2.1.6
7585
+ *
7586
+ * If plugin doesn't have a settings page, create one for the opt-in screen.
7587
+ */
7588
+ return add_query_arg( array_merge( $params, array(
7589
+ 'page' => $this->_slug,
7590
+ ) ), admin_url( 'admin.php', 'admin' ) );
7591
+ } else {
7592
+ // Plugin without a settings page.
7593
+ return admin_url( 'plugins.php' );
7594
+ }
7595
+ }
7596
+ }
7597
+
7598
+ // Module has a submenu settings page.
7599
+ if ( ! $this->_menu->is_top_level() ) {
7600
+ $parent_slug = $this->_menu->get_parent_slug();
7601
+ $menu_file = ( false !== strpos( $parent_slug, '.php' ) ) ?
7602
+ $parent_slug :
7603
+ 'admin.php';
7604
+
7605
+ return add_query_arg( array_merge( $params, array(
7606
+ 'page' => $page_param,
7607
+ ) ), admin_url( $menu_file, 'admin' ) );
7608
+ }
7609
+
7610
+ // Module has a top level CPT settings page.
7611
+ if ( $this->_menu->is_cpt() ) {
7612
+ if ( empty( $page ) && $this->is_activation_mode() ) {
7613
+ return add_query_arg( array_merge( $params, array(
7614
+ 'page' => $page_param
7615
+ ) ), admin_url( 'admin.php', 'admin' ) );
7616
+ } else {
7617
+ if ( ! empty( $page ) ) {
7618
+ $params['page'] = $page_param;
7619
+ }
7620
+
7621
+ return add_query_arg(
7622
+ $params,
7623
+ admin_url( $this->_menu->get_raw_slug(), 'admin' )
7624
+ );
7625
+ }
7626
+ }
7627
+
7628
+ // Module has a custom top level settings page.
7629
+ return add_query_arg( array_merge( $params, array(
7630
+ 'page' => $page_param,
7631
+ ) ), admin_url( 'admin.php', 'admin' ) );
7632
+ }
7633
+
7634
+ /**
7635
+ * Check if currently in a specified admin page.
7636
+ *
7637
+ * @author Vova Feldman (@svovaf)
7638
+ * @since 1.2.2.7
7639
+ *
7640
+ * @param string $page
7641
+ *
7642
+ * @return bool
7643
+ */
7644
+ function is_admin_page( $page ) {
7645
+ return ( $this->_menu->get_slug( $page ) === fs_request_get( 'page', '', 'get' ) );
7646
+ }
7647
+
7648
+ /**
7649
+ * Get module's main admin setting page URL.
7650
+ *
7651
+ * @author Vova Feldman (@svovaf)
7652
+ * @since 1.2.2.7
7653
+ *
7654
+ * @return string
7655
+ */
7656
+ function main_menu_url() {
7657
+ return $this->_menu->main_menu_url();
7658
+ }
7659
+
7660
+ /**
7661
+ * Check if currently on the theme's setting page or
7662
+ * on any of the Freemius added pages (via tabs).
7663
+ *
7664
+ * @author Vova Feldman (@svovaf)
7665
+ * @since 1.2.2.7
7666
+ *
7667
+ * @return bool
7668
+ */
7669
+ function is_theme_settings_page() {
7670
+ return fs_starts_with(
7671
+ fs_request_get( 'page', '', 'get' ),
7672
+ $this->_menu->get_slug()
7673
+ );
7674
+ }
7675
+
7676
+ /**
7677
+ * Plugin's account page + sync license URL.
7678
+ *
7679
+ * @author Vova Feldman (@svovaf)
7680
+ * @since 1.1.9.1
7681
+ *
7682
+ * @param bool|number $plugin_id
7683
+ * @param bool $add_action_nonce
7684
+ * @param array $params
7685
+ *
7686
+ * @return string
7687
+ */
7688
+ function _get_sync_license_url( $plugin_id = false, $add_action_nonce = true, $params = array() ) {
7689
+ if ( is_numeric( $plugin_id ) ) {
7690
+ $params['plugin_id'] = $plugin_id;
7691
+ }
7692
+
7693
+ return $this->get_account_url(
7694
+ $this->get_unique_affix() . '_sync_license',
7695
+ $params,
7696
+ $add_action_nonce
7697
+ );
7698
+ }
7699
+
7700
+ /**
7701
+ * Plugin's account URL.
7702
+ *
7703
+ * @author Vova Feldman (@svovaf)
7704
+ * @since 1.0.4
7705
+ *
7706
+ * @param bool|string $action
7707
+ * @param array $params
7708
+ *
7709
+ * @param bool $add_action_nonce
7710
+ *
7711
+ * @return string
7712
+ */
7713
+ function get_account_url( $action = false, $params = array(), $add_action_nonce = true ) {
7714
+ if ( is_string( $action ) ) {
7715
+ $params['fs_action'] = $action;
7716
+ }
7717
+
7718
+ self::require_pluggable_essentials();
7719
+
7720
+ return ( $add_action_nonce && is_string( $action ) ) ?
7721
+ fs_nonce_url( $this->_get_admin_page_url( 'account', $params ), $action ) :
7722
+ $this->_get_admin_page_url( 'account', $params );
7723
+ }
7724
+
7725
+ /**
7726
+ * @author Vova Feldman (@svovaf)
7727
+ * @since 1.2.0
7728
+ *
7729
+ * @param string $tab
7730
+ * @param bool $action
7731
+ * @param array $params
7732
+ * @param bool $add_action_nonce
7733
+ *
7734
+ * @return string
7735
+ *
7736
+ * @uses get_account_url()
7737
+ */
7738
+ function get_account_tab_url( $tab, $action = false, $params = array(), $add_action_nonce = true ) {
7739
+ $params['tab'] = $tab;
7740
+
7741
+ return $this->get_account_url( $action, $params, $add_action_nonce );
7742
+ }
7743
+
7744
+ /**
7745
+ * Plugin's account URL.
7746
+ *
7747
+ * @author Vova Feldman (@svovaf)
7748
+ * @since 1.0.4
7749
+ *
7750
+ * @param bool|string $topic
7751
+ * @param bool|string $message
7752
+ *
7753
+ * @return string
7754
+ */
7755
+ function contact_url( $topic = false, $message = false ) {
7756
+ $params = array();
7757
+ if ( is_string( $topic ) ) {
7758
+ $params['topic'] = $topic;
7759
+ }
7760
+ if ( is_string( $message ) ) {
7761
+ $params['message'] = $message;
7762
+ }
7763
+
7764
+ if ( $this->is_addon() ) {
7765
+ $params['addon_id'] = $this->get_id();
7766
+
7767
+ return $this->get_parent_instance()->_get_admin_page_url( 'contact', $params );
7768
+ } else {
7769
+ return $this->_get_admin_page_url( 'contact', $params );
7770
+ }
7771
+ }
7772
+
7773
+ /**
7774
+ * Add-on direct info URL.
7775
+ *
7776
+ * @author Vova Feldman (@svovaf)
7777
+ * @since 1.1.0
7778
+ *
7779
+ * @param string $slug
7780
+ *
7781
+ * @return string
7782
+ */
7783
+ function addon_url( $slug ) {
7784
+ return $this->_get_admin_page_url( 'addons', array(
7785
+ 'slug' => $slug
7786
+ ) );
7787
+ }
7788
+
7789
+ /* Logger
7790
+ ------------------------------------------------------------------------------------------------------------------*/
7791
+ /**
7792
+ * @param string $id
7793
+ * @param bool $prefix_slug
7794
+ *
7795
+ * @return FS_Logger
7796
+ */
7797
+ function get_logger( $id = '', $prefix_slug = true ) {
7798
+ return FS_Logger::get_logger( ( $prefix_slug ? $this->_slug : '' ) . ( ( ! $prefix_slug || empty( $id ) ) ? '' : '_' ) . $id );
7799
+ }
7800
+
7801
+ /**
7802
+ * @param $id
7803
+ * @param bool $load_options
7804
+ * @param bool $prefix_slug
7805
+ *
7806
+ * @return FS_Option_Manager
7807
+ */
7808
+ function get_options_manager( $id, $load_options = false, $prefix_slug = true ) {
7809
+ return FS_Option_Manager::get_manager( ( $prefix_slug ? $this->_slug : '' ) . ( ( ! $prefix_slug || empty( $id ) ) ? '' : '_' ) . $id, $load_options );
7810
+ }
7811
+
7812
+ /* Security
7813
+ ------------------------------------------------------------------------------------------------------------------*/
7814
+ private static function _encrypt( $str ) {
7815
+ if ( is_null( $str ) ) {
7816
+ return null;
7817
+ }
7818
+
7819
+ /**
7820
+ * The encrypt/decrypt functions are used to protect
7821
+ * the user from messing up with some of the sensitive
7822
+ * data stored for the module as a JSON in the database.
7823
+ *
7824
+ * I used the same suggested hack by the theme review team.
7825
+ * For more details, look at the function `Base64UrlDecode()`
7826
+ * in `./sdk/FreemiusBase.php`.
7827
+ *
7828
+ * @todo Remove this hack once the base64 error is removed from the Theme Check.
7829
+ *
7830
+ * @author Vova Feldman (@svovaf)
7831
+ * @since 1.2.2
7832
+ */
7833
+ $fn = 'base64' . '_encode';
7834
+
7835
+ return $fn( $str );
7836
+ }
7837
+
7838
+ static function _decrypt( $str ) {
7839
+ if ( is_null( $str ) ) {
7840
+ return null;
7841
+ }
7842
+
7843
+ /**
7844
+ * The encrypt/decrypt functions are used to protect
7845
+ * the user from messing up with some of the sensitive
7846
+ * data stored for the module as a JSON in the database.
7847
+ *
7848
+ * I used the same suggested hack by the theme review team.
7849
+ * For more details, look at the function `Base64UrlDecode()`
7850
+ * in `./sdk/FreemiusBase.php`.
7851
+ *
7852
+ * @todo Remove this hack once the base64 error is removed from the Theme Check.
7853
+ *
7854
+ * @author Vova Feldman (@svovaf)
7855
+ * @since 1.2.2
7856
+ */
7857
+ $fn = 'base64' . '_decode';
7858
+
7859
+ return $fn( $str );
7860
+ }
7861
+
7862
+ /**
7863
+ * @author Vova Feldman (@svovaf)
7864
+ * @since 1.0.5
7865
+ *
7866
+ * @param FS_Entity $entity
7867
+ *
7868
+ * @return FS_Entity Return an encrypted clone entity.
7869
+ */
7870
+ private static function _encrypt_entity( FS_Entity $entity ) {
7871
+ $clone = clone $entity;
7872
+ $props = get_object_vars( $entity );
7873
+
7874
+ foreach ( $props as $key => $val ) {
7875
+ $clone->{$key} = self::_encrypt( $val );
7876
+ }
7877
+
7878
+ return $clone;
7879
+ }
7880
+
7881
+ /**
7882
+ * @author Vova Feldman (@svovaf)
7883
+ * @since 1.0.5
7884
+ *
7885
+ * @param FS_Entity $entity
7886
+ *
7887
+ * @return FS_Entity Return an decrypted clone entity.
7888
+ */
7889
+ private static function decrypt_entity( FS_Entity $entity ) {
7890
+ $clone = clone $entity;
7891
+ $props = get_object_vars( $entity );
7892
+
7893
+ foreach ( $props as $key => $val ) {
7894
+ $clone->{$key} = self::_decrypt( $val );
7895
+ }
7896
+
7897
+ return $clone;
7898
+ }
7899
+
7900
+ /**
7901
+ * Tries to activate account based on POST params.
7902
+ *
7903
+ * @author Vova Feldman (@svovaf)
7904
+ * @since 1.0.2
7905
+ */
7906
+ function _activate_account() {
7907
+ if ( $this->is_registered() ) {
7908
+ // Already activated.
7909
+ return;
7910
+ }
7911
+
7912
+ self::_clean_admin_content_section();
7913
+
7914
+ if ( fs_request_is_action( 'activate' ) && fs_request_is_post() ) {
7915
+ // check_admin_referer( 'activate_' . $this->_plugin->public_key );
7916
+
7917
+ // Verify matching plugin details.
7918
+ if ( $this->_plugin->id != fs_request_get( 'plugin_id' ) || $this->_slug != fs_request_get( 'plugin_slug' ) ) {
7919
+ return;
7920
+ }
7921
+
7922
+ $user = new FS_User();
7923
+ $user->id = fs_request_get( 'user_id' );
7924
+ $user->public_key = fs_request_get( 'user_public_key' );
7925
+ $user->secret_key = fs_request_get( 'user_secret_key' );
7926
+ $user->email = fs_request_get( 'user_email' );
7927
+ $user->first = fs_request_get( 'user_first' );
7928
+ $user->last = fs_request_get( 'user_last' );
7929
+ $user->is_verified = fs_request_get_bool( 'user_is_verified' );
7930
+
7931
+ $site = new FS_Site();
7932
+ $site->id = fs_request_get( 'install_id' );
7933
+ $site->public_key = fs_request_get( 'install_public_key' );
7934
+ $site->secret_key = fs_request_get( 'install_secret_key' );
7935
+ $site->plan->id = fs_request_get( 'plan_id' );
7936
+ $site->plan->title = fs_request_get( 'plan_title' );
7937
+ $site->plan->name = fs_request_get( 'plan_name' );
7938
+
7939
+ $plans = array();
7940
+ $plans_data = json_decode( urldecode( fs_request_get( 'plans' ) ) );
7941
+ foreach ( $plans_data as $p ) {
7942
+ $plans[] = new FS_Plugin_Plan( $p );
7943
+ }
7944
+
7945
+ $this->_set_account( $user, $site, $plans );
7946
+
7947
+ // Reload the page with the keys.
7948
+ fs_redirect( $this->_get_admin_page_url() );
7949
+ }
7950
+ }
7951
+
7952
+ /**
7953
+ * @author Vova Feldman (@svovaf)
7954
+ * @since 1.0.7
7955
+ *
7956
+ * @param string $email
7957
+ *
7958
+ * @return FS_User|bool
7959
+ */
7960
+ static function _get_user_by_email( $email ) {
7961
+ self::$_static_logger->entrance();
7962
+
7963
+ $email = trim( strtolower( $email ) );
7964
+ $users = self::get_all_users();
7965
+ if ( is_array( $users ) ) {
7966
+ foreach ( $users as $u ) {
7967
+ if ( $email === trim( strtolower( $u->email ) ) ) {
7968
+ return $u;
7969
+ }
7970
+ }
7971
+ }
7972
+
7973
+ return false;
7974
+ }
7975
+
7976
+ #----------------------------------------------------------------------------------
7977
+ #region Account (Loading, Updates & Activation)
7978
+ #----------------------------------------------------------------------------------
7979
+
7980
+ /***
7981
+ * Load account information (user + site).
7982
+ *
7983
+ * @author Vova Feldman (@svovaf)
7984
+ * @since 1.0.1
7985
+ */
7986
+ private function _load_account() {
7987
+ $this->_logger->entrance();
7988
+
7989
+ $this->do_action( 'before_account_load' );
7990
+
7991
+ $sites = self::get_all_sites( $this->_module_type );
7992
+ $users = self::get_all_users();
7993
+ $plans = self::get_all_plans( $this->_module_type );
7994
+ $licenses = self::get_all_licenses( $this->_module_type );
7995
+
7996
+ if ( $this->_logger->is_on() && is_admin() ) {
7997
+ $this->_logger->log( 'sites = ' . var_export( $sites, true ) );
7998
+ $this->_logger->log( 'users = ' . var_export( $users, true ) );
7999
+ $this->_logger->log( 'plans = ' . var_export( $plans, true ) );
8000
+ $this->_logger->log( 'licenses = ' . var_export( $licenses, true ) );
8001
+ }
8002
+
8003
+ $site = isset( $sites[ $this->_slug ] ) ? $sites[ $this->_slug ] : false;
8004
+
8005
+ if ( is_object( $site ) &&
8006
+ is_numeric( $site->id ) &&
8007
+ is_numeric( $site->user_id ) &&
8008
+ is_object( $site->plan )
8009
+ ) {
8010
+ // Load site.
8011
+ $this->_site = clone $site;
8012
+ $this->_site->plan = self::decrypt_entity( $this->_site->plan );
8013
+
8014
+ // Load relevant user.
8015
+ $this->_user = clone $users[ $this->_site->user_id ];
8016
+
8017
+ // Load plans.
8018
+ $this->_plans = $plans[ $this->_slug ];
8019
+ if ( ! is_array( $this->_plans ) || empty( $this->_plans ) ) {
8020
+ $this->_sync_plans();
8021
+ } else {
8022
+ for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
8023
+ if ( $this->_plans[ $i ] instanceof FS_Plugin_Plan ) {
8024
+ $this->_plans[ $i ] = self::decrypt_entity( $this->_plans[ $i ] );
8025
+ } else {
8026
+ unset( $this->_plans[ $i ] );
8027
+ }
8028
+ }
8029
+ }
8030
+
8031
+ // Load licenses.
8032
+ $this->_licenses = array();
8033
+ if ( is_array( $licenses ) &&
8034
+ isset( $licenses[ $this->_slug ] ) &&
8035
+ isset( $licenses[ $this->_slug ][ $this->_user->id ] )
8036
+ ) {
8037
+ $this->_licenses = $licenses[ $this->_slug ][ $this->_user->id ];
8038
+ }
8039
+
8040
+ $this->_license = $this->_get_license_by_id( $this->_site->license_id );
8041
+
8042
+ if ( $this->_site->version != $this->get_plugin_version() ) {
8043
+ // If stored install version is different than current installed plugin version,
8044
+ // then update plugin version event.
8045
+ $this->update_plugin_version_event();
8046
+ }
8047
+ }
8048
+
8049
+ $this->_register_account_hooks();
8050
+ }
8051
+
8052
+ /**
8053
+ * @author Vova Feldman (@svovaf)
8054
+ * @since 1.0.1
8055
+ *
8056
+ * @param FS_User $user
8057
+ * @param FS_Site $site
8058
+ * @param bool|array $plans
8059
+ */
8060
+ private function _set_account( FS_User $user, FS_Site $site, $plans = false ) {
8061
+ $site->slug = $this->_slug;
8062
+ $site->user_id = $user->id;
8063
+
8064
+ $this->_site = $site;
8065
+ $this->_user = $user;
8066
+ if ( false !== $plans ) {
8067
+ $this->_plans = $plans;
8068
+ }
8069
+
8070
+ $this->send_install_update();
8071
+
8072
+ $this->_store_account();
8073
+
8074
+ }
8075
+
8076
+ /**
8077
+ * @author Vova Feldman (@svovaf)
8078
+ * @since 1.1.7.4
8079
+ *
8080
+ * @param array $override_with
8081
+ *
8082
+ * @return array
8083
+ */
8084
+ function get_opt_in_params( $override_with = array() ) {
8085
+ $this->_logger->entrance();
8086
+
8087
+ $current_user = self::_get_current_wp_user();
8088
+
8089
+ $activation_action = $this->get_unique_affix() . '_activate_new';
8090
+ $return_url = $this->is_anonymous() ?
8091
+ // If skipped already, then return to the account page.
8092
+ $this->get_account_url( $activation_action, array(), false ) :
8093
+ // Return to the module's main page.
8094
+ $this->get_after_activation_url( 'after_connect_url', array( 'fs_action' => $activation_action ) );
8095
+
8096
+ $params = array(
8097
+ 'user_firstname' => $current_user->user_firstname,
8098
+ 'user_lastname' => $current_user->user_lastname,
8099
+ 'user_nickname' => $current_user->user_nicename,
8100
+ 'user_email' => $current_user->user_email,
8101
+ 'user_ip' => WP_FS__REMOTE_ADDR,
8102
+ 'plugin_slug' => $this->_slug,
8103
+ 'plugin_id' => $this->get_id(),
8104
+ 'plugin_public_key' => $this->get_public_key(),
8105
+ 'plugin_version' => $this->get_plugin_version(),
8106
+ 'return_url' => fs_nonce_url( $return_url, $activation_action ),
8107
+ 'account_url' => fs_nonce_url( $this->_get_admin_page_url(
8108
+ 'account',
8109
+ array( 'fs_action' => 'sync_user' )
8110
+ ), 'sync_user' ),
8111
+ 'site_uid' => $this->get_anonymous_id(),
8112
+ 'site_url' => get_site_url(),
8113
+ 'site_name' => get_bloginfo( 'name' ),
8114
+ 'platform_version' => get_bloginfo( 'version' ),
8115
+ 'sdk_version' => $this->version,
8116
+ 'programming_language_version' => phpversion(),
8117
+ 'language' => get_bloginfo( 'language' ),
8118
+ 'charset' => get_bloginfo( 'charset' ),
8119
+ 'is_premium' => $this->is_premium(),
8120
+ 'is_active' => true,
8121
+ 'is_uninstalled' => false,
8122
+ );
8123
+
8124
+ if ( $this->is_pending_activation() &&
8125
+ ! empty( $this->_storage->pending_license_key )
8126
+ ) {
8127
+ $params['license_key'] = $this->_storage->pending_license_key;
8128
+ }
8129
+
8130
+ if ( WP_FS__SKIP_EMAIL_ACTIVATION && $this->has_secret_key() ) {
8131
+ // Even though rand() is known for its security issues,
8132
+ // the timestamp adds another layer of protection.
8133
+ // It would be very hard for an attacker to get the secret key form here.
8134
+ // Plus, this should never run in production since the secret should never
8135
+ // be included in the production version.
8136
+ $params['ts'] = WP_FS__SCRIPT_START_TIME;
8137
+ $params['salt'] = md5( uniqid( rand() ) );
8138
+ $params['secure'] = md5(
8139
+ $params['ts'] .
8140
+ $params['salt'] .
8141
+ $this->get_secret_key()
8142
+ );
8143
+ }
8144
+
8145
+ return array_merge( $params, $override_with );
8146
+ }
8147
+
8148
+ /**
8149
+ * 1. If successful opt-in or pending activation returns the next page that the user should be redirected to.
8150
+ * 2. If there was an API error, return the API result.
8151
+ *
8152
+ * @author Vova Feldman (@svovaf)
8153
+ * @since 1.1.7.4
8154
+ *
8155
+ * @param string|bool $email
8156
+ * @param string|bool $first
8157
+ * @param string|bool $last
8158
+ * @param string|bool $license_key
8159
+ * @param bool $is_uninstall If "true", this means that the module is currently being uninstalled.
8160
+ * In this case, the user and site info will be sent to the server but no
8161
+ * data will be saved to the WP installation's database.
8162
+ * @param number|bool $trial_plan_id
8163
+ *
8164
+ * @return string|object
8165
+ * @use WP_Error
8166
+ */
8167
+ function opt_in(
8168
+ $email = false,
8169
+ $first = false,
8170
+ $last = false,
8171
+ $license_key = false,
8172
+ $is_uninstall = false,
8173
+ $trial_plan_id = false
8174
+ ) {
8175
+ $this->_logger->entrance();
8176
+
8177
+ if ( false === $email ) {
8178
+ $current_user = self::_get_current_wp_user();
8179
+ $email = $current_user->user_email;
8180
+ }
8181
+
8182
+ /**
8183
+ * @since 1.2.1 If activating with license key, ignore the context-user
8184
+ * since the user will be automatically loaded from the license.
8185
+ */
8186
+ if ( empty( $license_key ) ) {
8187
+ // Clean up pending license if opt-ing in again.
8188
+ $this->_storage->remove( 'pending_license_key' );
8189
+
8190
+ if ( ! $is_uninstall ) {
8191
+ $fs_user = Freemius::_get_user_by_email( $email );
8192
+ if ( is_object( $fs_user ) && ! $this->is_pending_activation() ) {
8193
+ return $this->install_with_current_user( false, $trial_plan_id );
8194
+ }
8195
+ }
8196
+ }
8197
+
8198
+ $user_info = array();
8199
+ if ( ! empty( $email ) ) {
8200
+ $user_info['user_email'] = $email;
8201
+ }
8202
+ if ( ! empty( $first ) ) {
8203
+ $user_info['user_firstname'] = $first;
8204
+ }
8205
+ if ( ! empty( $last ) ) {
8206
+ $user_info['user_lastname'] = $last;
8207
+ }
8208
+
8209
+ $params = $this->get_opt_in_params( $user_info );
8210
+
8211
+ $filtered_license_key = false;
8212
+ if ( is_string( $license_key ) ) {
8213
+ $filtered_license_key = $this->apply_filters( 'license_key', $license_key );
8214
+ $params['license_key'] = $filtered_license_key;
8215
+ } else if ( FS_Plugin_Plan::is_valid_id( $trial_plan_id ) ) {
8216
+ $params['trial_plan_id'] = $trial_plan_id;
8217
+ }
8218
+
8219
+ if ( $is_uninstall ) {
8220
+ $params['uninstall_params'] = array(
8221
+ 'reason_id' => $this->_storage->uninstall_reason->id,
8222
+ 'reason_info' => $this->_storage->uninstall_reason->info
8223
+ );
8224
+ }
8225
+
8226
+ $params['format'] = 'json';
8227
+
8228
+ $url = WP_FS__ADDRESS . '/action/service/user/install/';
8229
+ if ( isset( $_COOKIE['XDEBUG_SESSION'] ) ) {
8230
+ $url = add_query_arg( 'XDEBUG_SESSION', 'PHPSTORM', $url );
8231
+ }
8232
+
8233
+ $response = wp_remote_post( $url, array(
8234
+ 'method' => 'POST',
8235
+ 'body' => $params,
8236
+ 'timeout' => 15,
8237
+ ) );
8238
+
8239
+ if ( $response instanceof WP_Error ) {
8240
+ if ( 'https://' === substr( $url, 0, 8 ) &&
8241
+ isset( $response->errors ) &&
8242
+ isset( $response->errors['http_request_failed'] )
8243
+ ) {
8244
+ $http_error = strtolower( $response->errors['http_request_failed'][0] );
8245
+
8246
+ if ( false !== strpos( $http_error, 'ssl' ) ) {
8247
+ // Failed due to old version of cURL or Open SSL (SSLv3 is not supported by CloudFlare).
8248
+ $url = 'http://' . substr( $url, 8 );
8249
+
8250
+ $response = wp_remote_post( $url, array(
8251
+ 'method' => 'POST',
8252
+ 'body' => $params,
8253
+ 'timeout' => 15,
8254
+ ) );
8255
+ }
8256
+ }
8257
+ }
8258
+
8259
+ if ( is_wp_error( $response ) ) {
8260
+ /**
8261
+ * @var WP_Error $response
8262
+ */
8263
+ $result = new stdClass();
8264
+
8265
+ $error_code = $response->get_error_code();
8266
+ $error_type = str_replace( ' ', '', ucwords( str_replace( '_', ' ', $error_code ) ) );
8267
+
8268
+ $result->error = (object) array(
8269
+ 'type' => $error_type,
8270
+ 'message' => $response->get_error_message(),
8271
+ 'code' => $error_code,
8272
+ 'http' => 402
8273
+ );
8274
+
8275
+ return $result;
8276
+ }
8277
+
8278
+ // Module is being uninstalled, don't handle the returned data.
8279
+ if ( $is_uninstall ) {
8280
+ return true;
8281
+ }
8282
+
8283
+ $decoded = @json_decode( $response['body'] );
8284
+
8285
+ if ( empty( $decoded ) ) {
8286
+ return false;
8287
+ }
8288
+
8289
+ if ( ! $this->is_api_result_object( $decoded ) ) {
8290
+ if ( ! empty( $params['license_key'] ) ) {
8291
+ // Pass the fully entered license key to the failure handler.
8292
+ $params['license_key'] = $license_key;
8293
+ }
8294
+
8295
+ return $is_uninstall ?
8296
+ $decoded :
8297
+ $this->apply_filters( 'after_install_failure', $decoded, $params );
8298
+ } else if ( isset( $decoded->pending_activation ) && $decoded->pending_activation ) {
8299
+ // Pending activation, add message.
8300
+ return $this->set_pending_confirmation(
8301
+ true,
8302
+ false,
8303
+ $filtered_license_key,
8304
+ ! empty( $params['trial_plan_id'] )
8305
+ );
8306
+ } else if ( isset( $decoded->install_secret_key ) ) {
8307
+ return $this->install_with_new_user(
8308
+ $decoded->user_id,
8309
+ $decoded->user_public_key,
8310
+ $decoded->user_secret_key,
8311
+ $decoded->install_id,
8312
+ $decoded->install_public_key,
8313
+ $decoded->install_secret_key,
8314
+ false
8315
+ );
8316
+ }
8317
+
8318
+ return $decoded;
8319
+ }
8320
+
8321
+ /**
8322
+ * Set user and site identities.
8323
+ *
8324
+ * @author Vova Feldman (@svovaf)
8325
+ * @since 1.0.9
8326
+ *
8327
+ * @param FS_User $user
8328
+ * @param FS_Site $site
8329
+ * @param bool $redirect
8330
+ * @param bool $auto_install Since 1.2.1.7 If `true` and setting up an account with a valid license, will
8331
+ * redirect (or return a URL) to the account page with a special parameter to
8332
+ * trigger the auto installation processes.
8333
+ *
8334
+ * @return string If redirect is `false`, returns the next page the user should be redirected to.
8335
+ */
8336
+ function setup_account(
8337
+ FS_User $user,
8338
+ FS_Site $site,
8339
+ $redirect = true,
8340
+ $auto_install = false
8341
+ ) {
8342
+ $this->_user = $user;
8343
+ $this->_site = $site;
8344
+
8345
+ $this->_sync_plans();
8346
+
8347
+ $this->_enrich_site_plan( false );
8348
+
8349
+ $this->_set_account( $user, $site );
8350
+
8351
+ if ( $this->is_trial() ) {
8352
+ // Store trial plan information.
8353
+ $this->_enrich_site_trial_plan( true );
8354
+ }
8355
+
8356
+ // If Freemius was OFF before, turn it on.
8357
+ $this->turn_on();
8358
+
8359
+ $this->do_action( 'after_account_connection', $user, $site );
8360
+
8361
+ if ( is_numeric( $site->license_id ) ) {
8362
+ $this->_license = $this->_get_license_by_id( $site->license_id );
8363
+ }
8364
+
8365
+ $this->_admin_notices->remove_sticky( 'connect_account' );
8366
+
8367
+ if ( $this->is_pending_activation() ) {
8368
+ // Remove pending activation sticky notice (if still exist).
8369
+ $this->_admin_notices->remove_sticky( 'activation_pending' );
8370
+
8371
+ // Remove plugin from pending activation mode.
8372
+ unset( $this->_storage->is_pending_activation );
8373
+
8374
+ if ( ! $this->is_paying_or_trial() ) {
8375
+ $this->_admin_notices->add_sticky(
8376
+ sprintf( $this->get_text( 'plugin-x-activation-message' ), '<b>' . $this->get_plugin_name() . '</b>' ),
8377
+ 'activation_complete'
8378
+ );
8379
+ }
8380
+ }
8381
+
8382
+ if ( $this->is_paying_or_trial() ) {
8383
+ if ( ! $this->is_premium() || ! $this->has_premium_version() ) {
8384
+ if ( $this->is_paying() ) {
8385
+ $this->_admin_notices->add_sticky(
8386
+ sprintf(
8387
+ $this->get_text( 'activation-with-plan-x-message' ),
8388
+ $this->_site->plan->title
8389
+ ) . $this->get_complete_upgrade_instructions(),
8390
+ 'plan_upgraded',
8391
+ $this->get_text( 'yee-haw' ) . '!'
8392
+ );
8393
+ } else {
8394
+ $this->_admin_notices->add_sticky(
8395
+ sprintf(
8396
+ $this->get_text( 'trial-started-message' ),
8397
+ '<i>' . $this->get_plugin_name() . '</i>'
8398
+ ) . $this->get_complete_upgrade_instructions( $this->_storage->trial_plan->title ),
8399
+ 'trial_started',
8400
+ $this->get_text( 'yee-haw' ) . '!'
8401
+ );
8402
+ }
8403
+ }
8404
+
8405
+ $this->_admin_notices->remove_sticky( array(
8406
+ 'trial_promotion',
8407
+ ) );
8408
+ }
8409
+
8410
+ $plugin_id = fs_request_get( 'plugin_id', false );
8411
+
8412
+ // Store activation time ONLY for plugins (not add-ons).
8413
+ if ( ! is_numeric( $plugin_id ) || ( $plugin_id == $this->_plugin->id ) ) {
8414
+ $this->_storage->activation_timestamp = WP_FS__SCRIPT_START_TIME;
8415
+ }
8416
+
8417
+ $next_page = '';
8418
+
8419
+ $extra = array();
8420
+ if ( $auto_install ) {
8421
+ $extra['auto_install'] = 'true';
8422
+ }
8423
+
8424
+ if ( is_numeric( $plugin_id ) ) {
8425
+ /**
8426
+ * @author Leo Fajardo
8427
+ * @since 1.2.1.6
8428
+ *
8429
+ * Also sync the license after an anonymous user subscribes.
8430
+ */
8431
+ if ( $this->is_anonymous() || $plugin_id != $this->_plugin->id ) {
8432
+ // Add-on was installed - sync license right after install.
8433
+ $next_page = $this->_get_sync_license_url( $plugin_id, true, $extra );
8434
+ }
8435
+ } else {
8436
+ /**
8437
+ * @author Vova Feldman (@svovaf)
8438
+ * @since 1.1.9 If site installed with a valid license, sync license.
8439
+ */
8440
+ if ( $this->is_paying() ) {
8441
+ $this->_sync_plugin_license( true );
8442
+ }
8443
+
8444
+ // Reload the page with the keys.
8445
+ $next_page = $this->is_anonymous() ?
8446
+ // If user previously skipped, redirect to account page.
8447
+ $this->get_account_url( false, $extra ) :
8448
+ $this->get_after_activation_url( 'after_connect_url' );
8449
+ }
8450
+
8451
+ if ( ! empty( $next_page ) && $redirect ) {
8452
+ fs_redirect( $next_page );
8453
+ }
8454
+
8455
+ return $next_page;
8456
+ }
8457
+
8458
+ /**
8459
+ * Install plugin with new user information after approval.
8460
+ *
8461
+ * @author Vova Feldman (@svovaf)
8462
+ * @since 1.0.7
8463
+ */
8464
+ function _install_with_new_user() {
8465
+ $this->_logger->entrance();
8466
+
8467
+ if ( $this->is_registered() ) {
8468
+ return;
8469
+ }
8470
+
8471
+ if ( ( $this->is_plugin() && fs_request_is_action( $this->get_unique_affix() . '_activate_new' ) ) ||
8472
+ // @todo This logic should be improved because it's executed on every load of a theme.
8473
+ $this->is_theme()
8474
+ ) {
8475
+ // check_admin_referer( $this->_slug . '_activate_new' );
8476
+
8477
+ if ( fs_request_has( 'user_secret_key' ) ) {
8478
+ $this->install_with_new_user(
8479
+ fs_request_get( 'user_id' ),
8480
+ fs_request_get( 'user_public_key' ),
8481
+ fs_request_get( 'user_secret_key' ),
8482
+ fs_request_get( 'install_id' ),
8483
+ fs_request_get( 'install_public_key' ),
8484
+ fs_request_get( 'install_secret_key' ),
8485
+ true,
8486
+ fs_request_get_bool( 'auto_install' )
8487
+ );
8488
+ } else if ( fs_request_has( 'pending_activation' ) ) {
8489
+ $this->set_pending_confirmation( fs_request_get( 'user_email' ), true );
8490
+ }
8491
+ }
8492
+ }
8493
+
8494
+ /**
8495
+ * Install plugin with new user.
8496
+ *
8497
+ * @author Vova Feldman (@svovaf)
8498
+ * @since 1.1.7.4
8499
+ *
8500
+ * @param number $user_id
8501
+ * @param string $user_public_key
8502
+ * @param string $user_secret_key
8503
+ * @param number $install_id
8504
+ * @param string $install_public_key
8505
+ * @param string $install_secret_key
8506
+ * @param bool $redirect
8507
+ * @param bool $auto_install Since 1.2.1.7 If `true` and setting up an account with a valid license, will
8508
+ * redirect (or return a URL) to the account page with a special parameter to
8509
+ * trigger the auto installation processes.
8510
+ *
8511
+ * @return string If redirect is `false`, returns the next page the user should be redirected to.
8512
+ */
8513
+ private function install_with_new_user(
8514
+ $user_id,
8515
+ $user_public_key,
8516
+ $user_secret_key,
8517
+ $install_id,
8518
+ $install_public_key,
8519
+ $install_secret_key,
8520
+ $redirect = true,
8521
+ $auto_install = false
8522
+ ) {
8523
+ $user = new FS_User();
8524
+ $user->id = $user_id;
8525
+ $user->public_key = $user_public_key;
8526
+ $user->secret_key = $user_secret_key;
8527
+
8528
+ $this->_user = $user;
8529
+ $user_result = $this->get_api_user_scope()->get();
8530
+ $user = new FS_User( $user_result );
8531
+ $this->_user = $user;
8532
+
8533
+ $site = new FS_Site();
8534
+ $site->id = $install_id;
8535
+ $site->public_key = $install_public_key;
8536
+ $site->secret_key = $install_secret_key;
8537
+
8538
+ $this->_site = $site;
8539
+ $site_result = $this->get_api_site_scope()->get();
8540
+ $site = new FS_Site( $site_result );
8541
+ $this->_site = $site;
8542
+
8543
+ return $this->setup_account(
8544
+ $this->_user,
8545
+ $this->_site,
8546
+ $redirect,
8547
+ $auto_install
8548
+ );
8549
+ }
8550
+
8551
+ /**
8552
+ * @author Vova Feldman (@svovaf)
8553
+ * @since 1.1.7.4
8554
+ *
8555
+ * @param string|bool $email
8556
+ * @param bool $redirect
8557
+ * @param string|bool $license_key Since 1.2.1.5
8558
+ * @param bool $is_pending_trial Since 1.2.1.5
8559
+ *
8560
+ * @return string Since 1.2.1.5 if $redirect is `false`, return the pending activation page.
8561
+ */
8562
+ private function set_pending_confirmation(
8563
+ $email = false,
8564
+ $redirect = true,
8565
+ $license_key = false,
8566
+ $is_pending_trial = false
8567
+ ) {
8568
+ if ( $this->_ignore_pending_mode ) {
8569
+ /**
8570
+ * If explicitly asked to ignore pending mode, set to anonymous mode
8571
+ * if require confirmation before finalizing the opt-in.
8572
+ *
8573
+ * @author Vova Feldman
8574
+ * @since 1.2.1.6
8575
+ */
8576
+ $this->skip_connection();
8577
+ } else {
8578
+ // Install must be activated via email since
8579
+ // user with the same email already exist.
8580
+ $this->_storage->is_pending_activation = true;
8581
+ $this->_add_pending_activation_notice( $email, $is_pending_trial );
8582
+ }
8583
+
8584
+ if ( ! empty( $license_key ) ) {
8585
+ $this->_storage->pending_license_key = $license_key;
8586
+ }
8587
+
8588
+ // Remove the opt-in sticky notice.
8589
+ $this->_admin_notices->remove_sticky( array(
8590
+ 'connect_account',
8591
+ 'trial_promotion',
8592
+ ) );
8593
+
8594
+ $next_page = $this->get_after_activation_url( 'after_pending_connect_url' );
8595
+
8596
+ // Reload the page with with pending activation message.
8597
+ if ( $redirect ) {
8598
+ fs_redirect( $next_page );
8599
+ }
8600
+
8601
+ return $next_page;
8602
+ }
8603
+
8604
+ /**
8605
+ * Install plugin with current logged WP user info.
8606
+ *
8607
+ * @author Vova Feldman (@svovaf)
8608
+ * @since 1.0.7
8609
+ */
8610
+ function _install_with_current_user() {
8611
+ $this->_logger->entrance();
8612
+
8613
+ if ( $this->is_registered() ) {
8614
+ return;
8615
+ }
8616
+
8617
+ if ( fs_request_is_action( $this->get_unique_affix() . '_activate_existing' ) && fs_request_is_post() ) {
8618
+ // check_admin_referer( 'activate_existing_' . $this->_plugin->public_key );
8619
+
8620
+ /**
8621
+ * @author Vova Feldman (@svovaf)
8622
+ * @since 1.1.9 Add license key if given.
8623
+ */
8624
+ $license_key = fs_request_get( 'license_secret_key' );
8625
+
8626
+ $this->install_with_current_user( $license_key );
8627
+ }
8628
+ }
8629
+
8630
+
8631
+ /**
8632
+ * @author Vova Feldman (@svovaf)
8633
+ * @since 1.1.7.4
8634
+ *
8635
+ * @param string|bool $license_key
8636
+ * @param number|bool $trial_plan_id
8637
+ * @param bool $redirect
8638
+ *
8639
+ * @return string|object If redirect is `false`, returns the next page the user should be redirected to, or the
8640
+ * API error object if failed to install.
8641
+ */
8642
+ private function install_with_current_user(
8643
+ $license_key = false,
8644
+ $trial_plan_id = false,
8645
+ $redirect = true
8646
+ ) {
8647
+ // Get current logged WP user.
8648
+ $current_user = self::_get_current_wp_user();
8649
+
8650
+ // Find the relevant FS user by the email.
8651
+ $user = self::_get_user_by_email( $current_user->user_email );
8652
+
8653
+ // We have to set the user before getting user scope API handler.
8654
+ $this->_user = $user;
8655
+
8656
+ $extra_install_params = array(
8657
+ 'uid' => $this->get_anonymous_id(),
8658
+ );
8659
+
8660
+ if ( ! empty( $license_key ) ) {
8661
+ $filtered_license_key = $this->apply_filters( 'license_key', $license_key );
8662
+ $extra_install_params['license_key'] = $filtered_license_key;
8663
+ } else if ( FS_Plugin_Plan::is_valid_id( $trial_plan_id ) ) {
8664
+ $extra_install_params['trial_plan_id'] = $trial_plan_id;
8665
+ }
8666
+
8667
+ $args = $this->get_install_data_for_api( $extra_install_params, false, false );
8668
+
8669
+ // Install the plugin.
8670
+ $install = $this->get_api_user_scope()->call(
8671
+ "/plugins/{$this->get_id()}/installs.json",
8672
+ 'post',
8673
+ $args
8674
+ );
8675
+
8676
+ if ( ! $this->is_api_result_entity( $install ) ) {
8677
+ if ( ! empty( $args['license_key'] ) ) {
8678
+ // Pass full the fully entered license key to the failure handler.
8679
+ $args['license_key'] = $license_key;
8680
+ }
8681
+
8682
+ $install = $this->apply_filters( 'after_install_failure', $install, $args );
8683
+
8684
+ $this->_admin_notices->add(
8685
+ sprintf( $this->get_text( 'could-not-activate-x' ), $this->get_plugin_name() ) . ' ' .
8686
+ $this->get_text( 'contact-us-with-error-message' ) . ' ' . '<b>' . $install->error->message . '</b>',
8687
+ $this->get_text( 'oops' ) . '...',
8688
+ 'error'
8689
+ );
8690
+
8691
+ if ( $redirect ) {
8692
+ fs_redirect( $this->get_activation_url( array( 'error' => $install->error->message ) ) );
8693
+ }
8694
+
8695
+ return $install;
8696
+ }
8697
+
8698
+ $site = new FS_Site( $install );
8699
+ $this->_site = $site;
8700
+
8701
+ return $this->setup_account( $this->_user, $this->_site, $redirect );
8702
+ }
8703
+
8704
+ /**
8705
+ * Tries to activate add-on account based on parent plugin info.
8706
+ *
8707
+ * @author Vova Feldman (@svovaf)
8708
+ * @since 1.0.6
8709
+ *
8710
+ * @param Freemius $parent_fs
8711
+ */
8712
+ private function _activate_addon_account( Freemius $parent_fs ) {
8713
+ if ( $this->is_registered() ) {
8714
+ // Already activated.
8715
+ return;
8716
+ }
8717
+
8718
+ // Activate add-on with parent plugin credentials.
8719
+ $addon_install = $parent_fs->get_api_site_scope()->call(
8720
+ "/addons/{$this->_plugin->id}/installs.json",
8721
+ 'post',
8722
+ $this->get_install_data_for_api( array(
8723
+ 'uid' => $this->get_anonymous_id(),
8724
+ ), false, false )
8725
+ );
8726
+
8727
+ if ( isset( $addon_install->error ) ) {
8728
+ $this->_admin_notices->add(
8729
+ sprintf( $this->get_text( 'could-not-activate-x' ), $this->get_plugin_name() ) . ' ' .
8730
+ $this->get_text( 'contact-us-with-error-message' ) . ' ' . '<b>' . $addon_install->error->message . '</b>',
8731
+ $this->get_text( 'oops' ) . '...',
8732
+ 'error'
8733
+ );
8734
+
8735
+ return;
8736
+ }
8737
+
8738
+ // First of all, set site info - otherwise we won't
8739
+ // be able to invoke API calls.
8740
+ $this->_site = new FS_Site( $addon_install );
8741
+
8742
+ // Sync add-on plans.
8743
+ $this->_sync_plans();
8744
+
8745
+ // Get site's current plan.
8746
+ $this->_site->plan = $this->_get_plan_by_id( $this->_site->plan->id );
8747
+
8748
+ // Get user information based on parent's plugin.
8749
+ $user = $parent_fs->get_user();
8750
+
8751
+ $this->_set_account( $user, $this->_site );
8752
+
8753
+ // Sync licenses.
8754
+ $this->_sync_licenses();
8755
+
8756
+ // Try to activate premium license.
8757
+ $this->_activate_license( true );
8758
+ }
8759
+
8760
+ /**
8761
+ * Tries to activate parent account based on add-on's info.
8762
+ *
8763
+ * @author Vova Feldman (@svovaf)
8764
+ * @since 1.2.2.7
8765
+ *
8766
+ * @param Freemius $parent_fs
8767
+ */
8768
+ private function activate_parent_account( Freemius $parent_fs ) {
8769
+ if ( ! $this->is_addon() ) {
8770
+ // This is not an add-on.
8771
+ return;
8772
+ }
8773
+
8774
+ if ( $parent_fs->is_registered() ) {
8775
+ // Already activated.
8776
+ return;
8777
+ }
8778
+
8779
+ // Activate parent with add-on's user credentials.
8780
+ $parent_install = $this->get_api_user_scope()->call(
8781
+ "/plugins/{$parent_fs->_plugin->id}/installs.json",
8782
+ 'post',
8783
+ $parent_fs->get_install_data_for_api( array(
8784
+ 'uid' => $parent_fs->get_anonymous_id(),
8785
+ ), false, false )
8786
+ );
8787
+
8788
+ if ( isset( $parent_install->error ) ) {
8789
+ $this->_admin_notices->add(
8790
+ sprintf( $this->get_text( 'could-not-activate-x' ), $this->get_plugin_name() ) . ' ' .
8791
+ $this->get_text( 'contact-us-with-error-message' ) . ' ' . '<b>' . $parent_install->error->message . '</b>',
8792
+ $this->get_text( 'oops' ) . '...',
8793
+ 'error'
8794
+ );
8795
+
8796
+ return;
8797
+ }
8798
+
8799
+ // First of all, set site info - otherwise we won't
8800
+ // be able to invoke API calls.
8801
+ $parent_fs->_site = new FS_Site( $parent_install );
8802
+
8803
+ // Sync add-on plans.
8804
+ $parent_fs->_sync_plans();
8805
+
8806
+ // Get site's current plan.
8807
+ $parent_fs->_site->plan = $parent_fs->_get_plan_by_id( $parent_fs->_site->plan->id );
8808
+
8809
+ // Get user information based on parent's plugin.
8810
+ $user = $this->get_user();
8811
+
8812
+ $parent_fs->_set_account( $user, $parent_fs->_site );
8813
+ }
8814
+
8815
+ #endregion
8816
+
8817
+ #----------------------------------------------------------------------------------
8818
+ #region Admin Menu Items
8819
+ #----------------------------------------------------------------------------------
8820
+
8821
+ private $_menu_items = array();
8822
+
8823
+ /**
8824
+ * @author Vova Feldman (@svovaf)
8825
+ * @since 1.2.1.8
8826
+ *
8827
+ * @return array
8828
+ */
8829
+ function get_menu_items() {
8830
+ return $this->_menu_items;
8831
+ }
8832
+
8833
+ /**
8834
+ * @author Vova Feldman (@svovaf)
8835
+ * @since 1.0.7
8836
+ *
8837
+ * @return string
8838
+ */
8839
+ function get_menu_slug() {
8840
+ return $this->_menu->get_slug();
8841
+ }
8842
+
8843
+ /**
8844
+ * @author Vova Feldman (@svovaf)
8845
+ * @since 1.0.9
8846
+ */
8847
+ function _prepare_admin_menu() {
8848
+ // if ( ! $this->is_on() ) {
8849
+ // return;
8850
+ // }
8851
+
8852
+ if ( ! $this->has_api_connectivity() && ! $this->is_enable_anonymous() ) {
8853
+ $this->_menu->remove_menu_item();
8854
+ } else {
8855
+ $this->do_action( 'before_admin_menu_init' );
8856
+
8857
+ $this->add_menu_action();
8858
+ $this->add_submenu_items();
8859
+ }
8860
+ }
8861
+
8862
+ /**
8863
+ * Admin dashboard menu items modifications.
8864
+ *
8865
+ * NOTE: admin_menu action executed before admin_init.
8866
+ *
8867
+ * @author Vova Feldman (@svovaf)
8868
+ * @since 1.0.7
8869
+ *
8870
+ */
8871
+ private function add_menu_action() {
8872
+ if ( $this->is_activation_mode() ) {
8873
+ if ( $this->is_plugin() || ( $this->has_settings_menu() && ! $this->is_free_wp_org_theme() ) ) {
8874
+ $this->override_plugin_menu_with_activation();
8875
+ } else {
8876
+ /**
8877
+ * Handle theme opt-in when the opt-in form shows as a dialog box in the themes page.
8878
+ */
8879
+ if ( fs_request_is_action( $this->get_unique_affix() . '_activate_existing' ) ) {
8880
+ add_action( 'load-themes.php', array( &$this, '_install_with_current_user' ) );
8881
+ } else if ( fs_request_is_action( $this->get_unique_affix() . '_activate_new' ) ||
8882
+ fs_request_get_bool( 'pending_activation' )
8883
+ ) {
8884
+ add_action( 'load-themes.php', array( &$this, '_install_with_new_user' ) );
8885
+ }
8886
+ }
8887
+ } else {
8888
+ if ( ! $this->is_registered() ) {
8889
+ // If not registered try to install user.
8890
+ if ( fs_request_is_action( $this->get_unique_affix() . '_activate_new' ) ) {
8891
+ $this->_install_with_new_user();
8892
+ }
8893
+ } else if (
8894
+ fs_request_is_action( 'sync_user' ) &&
8895
+ ( ! $this->has_settings_menu() || $this->is_free_wp_org_theme() )
8896
+ ) {
8897
+ $this->_handle_account_user_sync();
8898
+ }
8899
+ }
8900
+ }
8901
+
8902
+ /**
8903
+ * @author Vova Feldman (@svovaf)
8904
+ * @since 1.0.1
8905
+ */
8906
+ function _redirect_on_clicked_menu_link() {
8907
+ $this->_logger->entrance();
8908
+
8909
+ $page = strtolower( isset( $_REQUEST['page'] ) ? $_REQUEST['page'] : '' );
8910
+
8911
+ $this->_logger->log( 'page = ' . $page );
8912
+
8913
+ foreach ( $this->_menu_items as $priority => $items ) {
8914
+ foreach ( $items as $item ) {
8915
+ if ( isset( $item['url'] ) ) {
8916
+ if ( $page === $this->_menu->get_slug( strtolower( $item['menu_slug'] ) ) ) {
8917
+ $this->_logger->log( 'Redirecting to ' . $item['url'] );
8918
+
8919
+ fs_redirect( $item['url'] );
8920
+ }
8921
+ }
8922
+ }
8923
+ }
8924
+ }
8925
+
8926
+ /**
8927
+ * Remove plugin's all admin menu items & pages, and replace with activation page.
8928
+ *
8929
+ * @author Vova Feldman (@svovaf)
8930
+ * @since 1.0.1
8931
+ */
8932
+ private function override_plugin_menu_with_activation() {
8933
+ $this->_logger->entrance();
8934
+
8935
+ $hook = false;
8936
+
8937
+ if ( ! $this->_menu->has_menu() ) {
8938
+ // Add the opt-in page without a menu item.
8939
+ $hook = FS_Admin_Menu_Manager::add_subpage(
8940
+ null,
8941
+ $this->get_plugin_name(),
8942
+ $this->get_plugin_name(),
8943
+ 'manage_options',
8944
+ $this->_slug,
8945
+ array( &$this, '_connect_page_render' )
8946
+ );
8947
+ } else if ( $this->_menu->is_top_level() ) {
8948
+ $hook = $this->_menu->override_menu_item( array( &$this, '_connect_page_render' ) );
8949
+
8950
+ if ( false === $hook ) {
8951
+ // Create new menu item just for the opt-in.
8952
+ $hook = FS_Admin_Menu_Manager::add_page(
8953
+ $this->get_plugin_name(),
8954
+ $this->get_plugin_name(),
8955
+ 'manage_options',
8956
+ $this->_menu->get_slug(),
8957
+ array( &$this, '_connect_page_render' )
8958
+ );
8959
+ }
8960
+ } else {
8961
+ $menus = array( $this->_menu->get_parent_slug() );
8962
+
8963
+ if ( $this->_menu->is_override_exact() ) {
8964
+ // Make sure the current page is matching the activation page.
8965
+ if ( ! $this->is_matching_url( $this->get_activation_url() ) ) {
8966
+ return;
8967
+ }
8968
+ }
8969
+
8970
+ foreach ( $menus as $parent_slug ) {
8971
+ $hook = $this->_menu->override_submenu_action(
8972
+ $parent_slug,
8973
+ $this->_menu->get_raw_slug(),
8974
+ array( &$this, '_connect_page_render' )
8975
+ );
8976
+
8977
+ if ( false !== $hook ) {
8978
+ // Found plugin's submenu item.
8979
+ break;
8980
+ }
8981
+ }
8982
+ }
8983
+
8984
+ if ( $this->is_activation_page() ) {
8985
+ // Clean admin page from distracting content.
8986
+ self::_clean_admin_content_section();
8987
+ }
8988
+
8989
+ if ( false !== $hook ) {
8990
+ if ( fs_request_is_action( $this->get_unique_affix() . '_activate_existing' ) ) {
8991
+ add_action( "load-$hook", array( &$this, '_install_with_current_user' ) );
8992
+ } else if ( fs_request_is_action( $this->get_unique_affix() . '_activate_new' ) ) {
8993
+ add_action( "load-$hook", array( &$this, '_install_with_new_user' ) );
8994
+ }
8995
+ }
8996
+ }
8997
+
8998
+ /**
8999
+ * @author Leo Fajardo (leorw)
9000
+ * @since 1.2.1
9001
+ *
9002
+ * return string
9003
+ */
9004
+ function get_top_level_menu_capability() {
9005
+ global $menu;
9006
+
9007
+ $top_level_menu_slug = $this->get_top_level_menu_slug();
9008
+
9009
+ foreach ( $menu as $menu_info ) {
9010
+ /**
9011
+ * The second element in the menu info array is the capability/role that has access to the menu and the
9012
+ * third element is the menu slug.
9013
+ */
9014
+ if ( $menu_info[2] === $top_level_menu_slug ) {
9015
+ return $menu_info[1];
9016
+ }
9017
+ }
9018
+
9019
+ return 'read';
9020
+ }
9021
+
9022
+ /**
9023
+ * @author Vova Feldman (@svovaf)
9024
+ * @since 1.0.0
9025
+ *
9026
+ * @return string
9027
+ */
9028
+ private function get_top_level_menu_slug() {
9029
+ return ( $this->is_addon() ?
9030
+ $this->get_parent_instance()->_menu->get_top_level_menu_slug() :
9031
+ $this->_menu->get_top_level_menu_slug() );
9032
+ }
9033
+
9034
+ /**
9035
+ * @author Vova Feldman (@svovaf)
9036
+ * @since 1.2.2.7
9037
+ *
9038
+ * @return string
9039
+ */
9040
+ function get_pricing_cta_label() {
9041
+ $label = 'upgrade';
9042
+
9043
+ if ( $this->is_in_trial_promotion() &&
9044
+ ! $this->is_paying_or_trial()
9045
+ ) {
9046
+ // If running a trial promotion, modify the pricing to load the trial.
9047
+ $label = 'start-trial';
9048
+ } else if ( $this->is_paying() ) {
9049
+ $label = 'pricing';
9050
+ }
9051
+
9052
+ return $label;
9053
+ }
9054
+
9055
+ /**
9056
+ * @author Vova Feldman (@svovaf)
9057
+ * @since 1.2.2.7
9058
+ *
9059
+ * @return bool
9060
+ */
9061
+ function is_pricing_page_visible() {
9062
+ return (
9063
+ // Has at least one paid plan.
9064
+ $this->has_paid_plan() &&
9065
+ // Didn't ask to hide the pricing page.
9066
+ $this->is_page_visible( 'pricing' ) &&
9067
+ // Don't have a valid active license or has more than one plan.
9068
+ ( ! $this->is_paying() || ! $this->is_single_plan() )
9069
+ );
9070
+ }
9071
+
9072
+ /**
9073
+ * Add default Freemius menu items.
9074
+ *
9075
+ * @author Vova Feldman (@svovaf)
9076
+ * @since 1.0.0
9077
+ */
9078
+ private function add_submenu_items() {
9079
+ $this->_logger->entrance();
9080
+
9081
+ if ( ! $this->is_addon() ) {
9082
+ /**
9083
+ * @since 1.2.2.7 Also add submenu items when running in a free .org theme so the tabs will be visible.
9084
+ */
9085
+ if ( ! $this->is_activation_mode() || $this->is_free_wp_org_theme() ) {
9086
+ if ( $this->is_registered() ) {
9087
+ $show_account = (
9088
+ $this->is_submenu_item_visible( 'account' ) &&
9089
+ /**
9090
+ * @since 1.2.2.7 Don't show the Account for free WP.org themes without any paid plans.
9091
+ */
9092
+ ( ! $this->is_free_wp_org_theme() || $this->has_paid_plan() )
9093
+ );
9094
+
9095
+ // Add user account page.
9096
+ $this->add_submenu_item(
9097
+ $this->get_text( 'account' ),
9098
+ array( &$this, '_account_page_render' ),
9099
+ $this->get_plugin_name() . ' &ndash; ' . $this->get_text( 'account' ),
9100
+ 'manage_options',
9101
+ 'account',
9102
+ array( &$this, '_account_page_load' ),
9103
+ WP_FS__DEFAULT_PRIORITY,
9104
+ $show_account
9105
+ );
9106
+ }
9107
+
9108
+ // Add contact page.
9109
+ $this->add_submenu_item(
9110
+ $this->get_text( 'contact-us' ),
9111
+ array( &$this, '_contact_page_render' ),
9112
+ $this->get_plugin_name() . ' &ndash; ' . $this->get_text( 'contact-us' ),
9113
+ 'manage_options',
9114
+ 'contact',
9115
+ 'Freemius::_clean_admin_content_section',
9116
+ WP_FS__DEFAULT_PRIORITY,
9117
+ $this->is_submenu_item_visible( 'contact' )
9118
+ );
9119
+
9120
+ if ( $this->has_addons() ) {
9121
+ $this->add_submenu_item(
9122
+ $this->get_text( 'add-ons' ),
9123
+ array( &$this, '_addons_page_render' ),
9124
+ $this->get_plugin_name() . ' &ndash; ' . $this->get_text( 'add-ons' ),
9125
+ 'manage_options',
9126
+ 'addons',
9127
+ array( &$this, '_addons_page_load' ),
9128
+ WP_FS__LOWEST_PRIORITY - 1,
9129
+ $this->is_submenu_item_visible( 'addons' )
9130
+ );
9131
+ }
9132
+
9133
+ $show_pricing = (
9134
+ $this->is_submenu_item_visible( 'pricing' ) &&
9135
+ $this->is_pricing_page_visible()
9136
+ );
9137
+
9138
+ $pricing_cta_slug = $this->get_pricing_cta_label();
9139
+ $pricing_class = 'upgrade-mode';
9140
+ if ( $show_pricing ) {
9141
+ if ( $this->is_in_trial_promotion() &&
9142
+ ! $this->is_paying_or_trial()
9143
+ ) {
9144
+ // If running a trial promotion, modify the pricing to load the trial.
9145
+ $pricing_class = 'trial-mode';
9146
+ } else if ( $this->is_paying() ) {
9147
+ $pricing_class = '';
9148
+ }
9149
+ }
9150
+
9151
+ // Add upgrade/pricing page.
9152
+ $this->add_submenu_item(
9153
+ $this->get_text( $pricing_cta_slug ) . '&nbsp;&nbsp;' . ( is_rtl() ? '&#x2190;' : '&#x27a4;' ),
9154
+ array( &$this, '_pricing_page_render' ),
9155
+ $this->get_plugin_name() . ' &ndash; ' . $this->get_text( 'pricing' ),
9156
+ 'manage_options',
9157
+ 'pricing',
9158
+ 'Freemius::_clean_admin_content_section',
9159
+ WP_FS__LOWEST_PRIORITY,
9160
+ $show_pricing,
9161
+ $pricing_class
9162
+ );
9163
+ }
9164
+ }
9165
+
9166
+
9167
+ if ( 0 < count( $this->_menu_items ) ) {
9168
+ if ( ! $this->_menu->is_top_level() ) {
9169
+ fs_enqueue_local_style( 'fs_common', '/admin/common.css' );
9170
+
9171
+ // Append submenu items right after the plugin's submenu item.
9172
+ $this->order_sub_submenu_items();
9173
+ } else {
9174
+ // Append submenu items.
9175
+ $this->embed_submenu_items();
9176
+ }
9177
+ }
9178
+ }
9179
+
9180
+ /**
9181
+ * Moved the actual submenu item additions to a separated function,
9182
+ * in order to support sub-submenu items when the plugin's settings
9183
+ * only have a submenu and not top-level menu item.
9184
+ *
9185
+ * @author Vova Feldman (@svovaf)
9186
+ * @since 1.1.4
9187
+ */
9188
+ private function embed_submenu_items() {
9189
+ $item_template = $this->_menu->is_top_level() ?
9190
+ '<span class="fs-submenu-item %s %s %s">%s</span>' :
9191
+ '<span class="fs-submenu-item fs-sub %s %s %s">%s</span>';
9192
+
9193
+ $top_level_menu_capability = $this->get_top_level_menu_capability();
9194
+
9195
+ ksort( $this->_menu_items );
9196
+
9197
+ foreach ( $this->_menu_items as $priority => $items ) {
9198
+ foreach ( $items as $item ) {
9199
+ $capability = ( ! empty( $item['capability'] ) ? $item['capability'] : $top_level_menu_capability );
9200
+
9201
+ $menu_item = sprintf(
9202
+ $item_template,
9203
+ $this->get_unique_affix(),
9204
+ $item['menu_slug'],
9205
+ ! empty( $item['class'] ) ? $item['class'] : '',
9206
+ $item['menu_title']
9207
+ );
9208
+
9209
+ $menu_slug = $this->_menu->get_slug( $item['menu_slug'] );
9210
+
9211
+ if ( ! isset( $item['url'] ) ) {
9212
+ $hook = FS_Admin_Menu_Manager::add_subpage(
9213
+ $item['show_submenu'] ?
9214
+ $this->get_top_level_menu_slug() :
9215
+ null,
9216
+ $item['page_title'],
9217
+ $menu_item,
9218
+ $capability,
9219
+ $menu_slug,
9220
+ $item['render_function']
9221
+ );
9222
+
9223
+ if ( false !== $item['before_render_function'] ) {
9224
+ add_action( "load-$hook", $item['before_render_function'] );
9225
+ }
9226
+ } else {
9227
+ FS_Admin_Menu_Manager::add_subpage(
9228
+ $item['show_submenu'] ?
9229
+ $this->get_top_level_menu_slug() :
9230
+ null,
9231
+ $item['page_title'],
9232
+ $menu_item,
9233
+ $capability,
9234
+ $menu_slug,
9235
+ array( $this, '' )
9236
+ );
9237
+ }
9238
+ }
9239
+ }
9240
+ }
9241
+
9242
+ /**
9243
+ * Re-order the submenu items so all Freemius added new submenu items
9244
+ * are added right after the plugin's settings submenu item.
9245
+ *
9246
+ * @author Vova Feldman (@svovaf)
9247
+ * @since 1.1.4
9248
+ */
9249
+ private function order_sub_submenu_items() {
9250
+ global $submenu;
9251
+
9252
+ $menu_slug = $this->_menu->get_top_level_menu_slug();
9253
+
9254
+ /**
9255
+ * Before "admin_menu" fires, WordPress will loop over the default submenus and remove pages for which the user
9256
+ * does not have permissions. So in case a plugin does not have top-level menu but does have submenus under any
9257
+ * of the default menus, only users that have the right role can access its sub-submenus (Account, Contact Us,
9258
+ * Support Forum, etc.) since $submenu[ $menu_slug ] will be empty if the user doesn't have permission.
9259
+ *
9260
+ * In case a plugin does not have submenus under any of the default menus but does have submenus under the menu
9261
+ * of another plugin, only users that have the right role can access its sub-submenus since we will use the
9262
+ * capability needed to access the parent menu as the capability for the submenus that we will add.
9263
+ */
9264
+ if ( empty( $submenu[ $menu_slug ] ) ) {
9265
+ return;
9266
+ }
9267
+
9268
+ $top_level_menu = &$submenu[ $menu_slug ];
9269
+
9270
+ $all_submenu_items_after = array();
9271
+
9272
+ $found_submenu_item = false;
9273
+
9274
+ foreach ( $top_level_menu as $submenu_id => $meta ) {
9275
+ if ( $found_submenu_item ) {
9276
+ // Remove all submenu items after the plugin's submenu item.
9277
+ $all_submenu_items_after[] = $meta;
9278
+ unset( $top_level_menu[ $submenu_id ] );
9279
+ }
9280
+
9281
+ if ( $this->_menu->get_raw_slug() === $meta[2] ) {
9282
+ // Found the submenu item, put all below.
9283
+ $found_submenu_item = true;
9284
+ continue;
9285
+ }
9286
+ }
9287
+
9288
+ // Embed all plugin's new submenu items.
9289
+ $this->embed_submenu_items();
9290
+
9291
+ // Start with specially high number to make sure it's appended.
9292
+ $i = max( 10000, max( array_keys( $top_level_menu ) ) + 1 );
9293
+ foreach ( $all_submenu_items_after as $meta ) {
9294
+ $top_level_menu[ $i ] = $meta;
9295
+ $i ++;
9296
+ }
9297
+
9298
+ // Sort submenu items.
9299
+ ksort( $top_level_menu );
9300
+ }
9301
+
9302
+ /**
9303
+ * Helper method to return the module's support forum URL.
9304
+ *
9305
+ * @author Vova Feldman (@svovaf)
9306
+ * @since 1.2.2.7
9307
+ *
9308
+ * @return string
9309
+ */
9310
+ function get_support_forum_url() {
9311
+ return $this->apply_filters( 'support_forum_url', "https://wordpress.org/support/{$this->_module_type}/{$this->_slug}" );
9312
+ }
9313
+
9314
+ /**
9315
+ * Displays the Support Forum link when enabled.
9316
+ *
9317
+ * Can be filtered like so:
9318
+ *
9319
+ * function _fs_show_support_menu( $is_visible, $menu_id ) {
9320
+ * if ( 'support' === $menu_id ) {
9321
+ * return _fs->is_registered();
9322
+ * }
9323
+ * return $is_visible;
9324
+ * }
9325
+ * _fs()->add_filter('is_submenu_visible', '_fs_show_support_menu', 10, 2);
9326
+ *
9327
+ */
9328
+ function _add_default_submenu_items() {
9329
+ if ( ! $this->is_on() ) {
9330
+ return;
9331
+ }
9332
+
9333
+ if ( ! $this->is_activation_mode() ) {
9334
+ $this->add_submenu_link_item(
9335
+ $this->apply_filters( 'support_forum_submenu', $this->get_text( 'support-forum' ) ),
9336
+ $this->get_support_forum_url(),
9337
+ 'wp-support-forum',
9338
+ null,
9339
+ 50,
9340
+ $this->is_submenu_item_visible( 'support' )
9341
+ );
9342
+ }
9343
+ }
9344
+
9345
+ /**
9346
+ * @author Vova Feldman (@svovaf)
9347
+ * @since 1.0.1
9348
+ *
9349
+ * @param string $menu_title
9350
+ * @param callable $render_function
9351
+ * @param bool|string $page_title
9352
+ * @param string $capability
9353
+ * @param bool|string $menu_slug
9354
+ * @param bool|callable $before_render_function
9355
+ * @param int $priority
9356
+ * @param bool $show_submenu
9357
+ * @param string $class Since 1.2.1.5 can add custom classes to menu items.
9358
+ */
9359
+ function add_submenu_item(
9360
+ $menu_title,
9361
+ $render_function,
9362
+ $page_title = false,
9363
+ $capability = 'manage_options',
9364
+ $menu_slug = false,
9365
+ $before_render_function = false,
9366
+ $priority = WP_FS__DEFAULT_PRIORITY,
9367
+ $show_submenu = true,
9368
+ $class = ''
9369
+ ) {
9370
+ $this->_logger->entrance( 'Title = ' . $menu_title );
9371
+
9372
+ if ( $this->is_addon() ) {
9373
+ $parent_fs = $this->get_parent_instance();
9374
+
9375
+ if ( is_object( $parent_fs ) ) {
9376
+ $parent_fs->add_submenu_item(
9377
+ $menu_title,
9378
+ $render_function,
9379
+ $page_title,
9380
+ $capability,
9381
+ $menu_slug,
9382
+ $before_render_function,
9383
+ $priority,
9384
+ $show_submenu,
9385
+ $class
9386
+ );
9387
+
9388
+ return;
9389
+ }
9390
+ }
9391
+
9392
+ if ( ! isset( $this->_menu_items[ $priority ] ) ) {
9393
+ $this->_menu_items[ $priority ] = array();
9394
+ }
9395
+
9396
+ $this->_menu_items[ $priority ][] = array(
9397
+ 'page_title' => is_string( $page_title ) ? $page_title : $menu_title,
9398
+ 'menu_title' => $menu_title,
9399
+ 'capability' => $capability,
9400
+ 'menu_slug' => is_string( $menu_slug ) ? $menu_slug : strtolower( $menu_title ),
9401
+ 'render_function' => $render_function,
9402
+ 'before_render_function' => $before_render_function,
9403
+ 'show_submenu' => $show_submenu,
9404
+ 'class' => $class,
9405
+ );
9406
+ }
9407
+
9408
+ /**
9409
+ * @author Vova Feldman (@svovaf)
9410
+ * @since 1.0.1
9411
+ *
9412
+ * @param string $menu_title
9413
+ * @param string $url
9414
+ * @param bool $menu_slug
9415
+ * @param string $capability
9416
+ * @param int $priority
9417
+ * @param bool $show_submenu
9418
+ */
9419
+ function add_submenu_link_item(
9420
+ $menu_title,
9421
+ $url,
9422
+ $menu_slug = false,
9423
+ $capability = 'read',
9424
+ $priority = WP_FS__DEFAULT_PRIORITY,
9425
+ $show_submenu = true
9426
+ ) {
9427
+ $this->_logger->entrance( 'Title = ' . $menu_title . '; Url = ' . $url );
9428
+
9429
+ if ( $this->is_addon() ) {
9430
+ $parent_fs = $this->get_parent_instance();
9431
+
9432
+ if ( is_object( $parent_fs ) ) {
9433
+ $parent_fs->add_submenu_link_item(
9434
+ $menu_title,
9435
+ $url,
9436
+ $menu_slug,
9437
+ $capability,
9438
+ $priority,
9439
+ $show_submenu
9440
+ );
9441
+
9442
+ return;
9443
+ }
9444
+ }
9445
+
9446
+ if ( ! isset( $this->_menu_items[ $priority ] ) ) {
9447
+ $this->_menu_items[ $priority ] = array();
9448
+ }
9449
+
9450
+ $this->_menu_items[ $priority ][] = array(
9451
+ 'menu_title' => $menu_title,
9452
+ 'capability' => $capability,
9453
+ 'menu_slug' => is_string( $menu_slug ) ? $menu_slug : strtolower( $menu_title ),
9454
+ 'url' => $url,
9455
+ 'page_title' => $menu_title,
9456
+ 'render_function' => 'fs_dummy',
9457
+ 'before_render_function' => '',
9458
+ 'show_submenu' => $show_submenu,
9459
+ );
9460
+ }
9461
+
9462
+ #endregion ------------------------------------------------------------------
9463
+
9464
+
9465
+ #--------------------------------------------------------------------------------
9466
+ #region Actions / Hooks / Filters
9467
+ #--------------------------------------------------------------------------------
9468
+
9469
+ /**
9470
+ * @author Vova Feldman (@svovaf)
9471
+ * @since 1.1.7
9472
+ *
9473
+ * @param string $tag
9474
+ *
9475
+ * @return string
9476
+ */
9477
+ public function get_action_tag( $tag ) {
9478
+ return self::get_action_tag_static( $tag, $this->_slug, $this->is_plugin() );
9479
+ }
9480
+
9481
+ /**
9482
+ * @author Vova Feldman (@svovaf)
9483
+ * @since 1.2.1.6
9484
+ *
9485
+ * @param string $tag
9486
+ * @param string $slug
9487
+ * @param bool $is_plugin
9488
+ *
9489
+ * @return string
9490
+ */
9491
+ static function get_action_tag_static( $tag, $slug = '', $is_plugin = true ) {
9492
+ $action = "fs_{$tag}";
9493
+
9494
+ if ( ! empty( $slug ) ) {
9495
+ $action .= '_' . self::get_module_unique_affix( $slug, $is_plugin );
9496
+ }
9497
+
9498
+ return $action;
9499
+ }
9500
+
9501
+ /**
9502
+ * Returns a string that can be used to generate a unique action name,
9503
+ * option name, HTML element ID, or HTML element class.
9504
+ *
9505
+ * @author Leo Fajardo (@leorw)
9506
+ * @since 1.2.2
9507
+ *
9508
+ * @return string
9509
+ */
9510
+ public function get_unique_affix() {
9511
+ return self::get_module_unique_affix( $this->_slug, $this->is_plugin() );
9512
+ }
9513
+
9514
+ /**
9515
+ * Returns a string that can be used to generate a unique action name,
9516
+ * option name, HTML element ID, or HTML element class.
9517
+ *
9518
+ * @author Vova Feldman (@svovaf)
9519
+ * @since 1.2.2.5
9520
+ *
9521
+ * @param string $slug
9522
+ * @param bool $is_plugin
9523
+ *
9524
+ * @return string
9525
+ */
9526
+ static function get_module_unique_affix( $slug, $is_plugin = true ) {
9527
+ $affix = $slug;
9528
+
9529
+ if ( ! $is_plugin ) {
9530
+ $affix .= '-' . WP_FS__MODULE_TYPE_THEME;
9531
+ }
9532
+
9533
+ return $affix;
9534
+ }
9535
+
9536
+ /**
9537
+ * @author Vova Feldman (@svovaf)
9538
+ * @since 1.2.1
9539
+ * @since 1.2.2.5 The AJAX action names are based on the module ID, not like the non-AJAX actions that are
9540
+ * based on the slug for backward compatibility.
9541
+ *
9542
+ * @param string $tag
9543
+ *
9544
+ * @return string
9545
+ */
9546
+ function get_ajax_action( $tag ) {
9547
+ return self::get_ajax_action_static( $tag, $this->_module_id );
9548
+ }
9549
+
9550
+ /**
9551
+ * @author Vova Feldman (@svovaf)
9552
+ * @since 1.2.1.7
9553
+ *
9554
+ * @param string $tag
9555
+ *
9556
+ * @return string
9557
+ */
9558
+ function get_ajax_security( $tag ) {
9559
+ return wp_create_nonce( $this->get_ajax_action( $tag ) );
9560
+ }
9561
+
9562
+ /**
9563
+ * @author Vova Feldman (@svovaf)
9564
+ * @since 1.2.1.7
9565
+ *
9566
+ * @param string $tag
9567
+ */
9568
+ function check_ajax_referer( $tag ) {
9569
+ check_ajax_referer( $this->get_ajax_action( $tag ), 'security' );
9570
+ }
9571
+
9572
+ /**
9573
+ * @author Vova Feldman (@svovaf)
9574
+ * @since 1.2.1.6
9575
+ * @since 1.2.2.5 The AJAX action names are based on the module ID, not like the non-AJAX actions that are
9576
+ * based on the slug for backward compatibility.
9577
+ *
9578
+ * @param string $tag
9579
+ * @param number|null $module_id
9580
+ *
9581
+ * @return string
9582
+ */
9583
+ private static function get_ajax_action_static( $tag, $module_id = null ) {
9584
+ $action = "fs_{$tag}";
9585
+
9586
+ if ( ! empty( $module_id ) ) {
9587
+ $action .= "_{$module_id}";
9588
+ }
9589
+
9590
+ return $action;
9591
+ }
9592
+
9593
+ /**
9594
+ * Do action, specific for the current context plugin.
9595
+ *
9596
+ * @author Vova Feldman (@svovaf)
9597
+ * @since 1.0.1
9598
+ *
9599
+ * @param string $tag The name of the action to be executed.
9600
+ * @param mixed $arg,... Optional. Additional arguments which are passed on to the
9601
+ * functions hooked to the action. Default empty.
9602
+ *
9603
+ * @uses do_action()
9604
+ */
9605
+ function do_action( $tag, $arg = '' ) {
9606
+ $this->_logger->entrance( $tag );
9607
+
9608
+ $args = func_get_args();
9609
+
9610
+ call_user_func_array( 'do_action', array_merge(
9611
+ array( $this->get_action_tag( $tag ) ),
9612
+ array_slice( $args, 1 ) )
9613
+ );
9614
+ }
9615
+
9616
+ /**
9617
+ * Add action, specific for the current context plugin.
9618
+ *
9619
+ * @author Vova Feldman (@svovaf)
9620
+ * @since 1.0.1
9621
+ *
9622
+ * @param string $tag
9623
+ * @param callable $function_to_add
9624
+ * @param int $priority
9625
+ * @param int $accepted_args
9626
+ *
9627
+ * @uses add_action()
9628
+ */
9629
+ function add_action(
9630
+ $tag,
9631
+ $function_to_add,
9632
+ $priority = WP_FS__DEFAULT_PRIORITY,
9633
+ $accepted_args = 1
9634
+ ) {
9635
+ $this->_logger->entrance( $tag );
9636
+
9637
+ add_action( $this->get_action_tag( $tag ), $function_to_add, $priority, $accepted_args );
9638
+ }
9639
+
9640
+ /**
9641
+ * Add AJAX action, specific for the current context plugin.
9642
+ *
9643
+ * @author Vova Feldman (@svovaf)
9644
+ * @since 1.2.1
9645
+ *
9646
+ * @param string $tag
9647
+ * @param callable $function_to_add
9648
+ * @param int $priority
9649
+ *
9650
+ * @uses add_action()
9651
+ *
9652
+ * @return bool True if action added, false if no need to add the action since the AJAX call isn't matching.
9653
+ */
9654
+ function add_ajax_action(
9655
+ $tag,
9656
+ $function_to_add,
9657
+ $priority = WP_FS__DEFAULT_PRIORITY
9658
+ ) {
9659
+ $this->_logger->entrance( $tag );
9660
+
9661
+ return self::add_ajax_action_static(
9662
+ $tag,
9663
+ $function_to_add,
9664
+ $priority,
9665
+ $this->_module_id
9666
+ );
9667
+ }
9668
+
9669
+ /**
9670
+ * Add AJAX action.
9671
+ *
9672
+ * @author Vova Feldman (@svovaf)
9673
+ * @since 1.2.1.6
9674
+ *
9675
+ * @param string $tag
9676
+ * @param callable $function_to_add
9677
+ * @param int $priority
9678
+ * @param number|null $module_id
9679
+ *
9680
+ * @return bool True if action added, false if no need to add the action since the AJAX call isn't matching.
9681
+ * @uses add_action()
9682
+ *
9683
+ */
9684
+ static function add_ajax_action_static(
9685
+ $tag,
9686
+ $function_to_add,
9687
+ $priority = WP_FS__DEFAULT_PRIORITY,
9688
+ $module_id = null
9689
+ ) {
9690
+ self::$_static_logger->entrance( $tag );
9691
+
9692
+ if ( ! self::is_ajax_action_static( $tag, $module_id ) ) {
9693
+ return false;
9694
+ }
9695
+
9696
+ add_action(
9697
+ 'wp_ajax_' . self::get_ajax_action_static( $tag, $module_id ),
9698
+ $function_to_add,
9699
+ $priority,
9700
+ 0
9701
+ );
9702
+
9703
+ self::$_static_logger->info( "$tag AJAX callback action added." );
9704
+
9705
+ return true;
9706
+ }
9707
+
9708
+ /**
9709
+ * Send a JSON response back to an Ajax request.
9710
+ *
9711
+ * @author Vova Feldman (@svovaf)
9712
+ * @since 1.2.1.5
9713
+ *
9714
+ * @param mixed $response
9715
+ */
9716
+ static function shoot_ajax_response( $response ) {
9717
+ wp_send_json( $response );
9718
+ }
9719
+
9720
+ /**
9721
+ * Send a JSON response back to an Ajax request, indicating success.
9722
+ *
9723
+ * @author Vova Feldman (@svovaf)
9724
+ * @since 1.2.1.5
9725
+ *
9726
+ * @param mixed $data Data to encode as JSON, then print and exit.
9727
+ */
9728
+ static function shoot_ajax_success( $data = null ) {
9729
+ wp_send_json_success( $data );
9730
+ }
9731
+
9732
+ /**
9733
+ * Send a JSON response back to an Ajax request, indicating failure.
9734
+ *
9735
+ * @author Vova Feldman (@svovaf)
9736
+ * @since 1.2.1.5
9737
+ *
9738
+ * @param mixed $error Optional error message.
9739
+ */
9740
+ static function shoot_ajax_failure( $error = '' ) {
9741
+ $result = array( 'success' => false );
9742
+ if ( ! empty( $error ) ) {
9743
+ $result['error'] = $error;
9744
+ }
9745
+
9746
+ wp_send_json( $result );
9747
+ }
9748
+
9749
+ /**
9750
+ * Apply filter, specific for the current context plugin.
9751
+ *
9752
+ * @author Vova Feldman (@svovaf)
9753
+ * @since 1.0.9
9754
+ *
9755
+ * @param string $tag The name of the filter hook.
9756
+ * @param mixed $value The value on which the filters hooked to `$tag` are applied on.
9757
+ *
9758
+ * @return mixed The filtered value after all hooked functions are applied to it.
9759
+ *
9760
+ * @uses apply_filters()
9761
+ */
9762
+ function apply_filters( $tag, $value ) {
9763
+ $this->_logger->entrance( $tag );
9764
+
9765
+ $args = func_get_args();
9766
+ array_unshift( $args, $this->get_unique_affix() );
9767
+
9768
+ return call_user_func_array( 'fs_apply_filter', $args );
9769
+ }
9770
+
9771
+ /**
9772
+ * Add filter, specific for the current context plugin.
9773
+ *
9774
+ * @author Vova Feldman (@svovaf)
9775
+ * @since 1.0.9
9776
+ *
9777
+ * @param string $tag
9778
+ * @param callable $function_to_add
9779
+ * @param int $priority
9780
+ * @param int $accepted_args
9781
+ *
9782
+ * @uses add_filter()
9783
+ */
9784
+ function add_filter( $tag, $function_to_add, $priority = WP_FS__DEFAULT_PRIORITY, $accepted_args = 1 ) {
9785
+ $this->_logger->entrance( $tag );
9786
+
9787
+ add_filter( $this->get_action_tag( $tag ), $function_to_add, $priority, $accepted_args );
9788
+ }
9789
+
9790
+ /**
9791
+ * Check if has filter.
9792
+ *
9793
+ * @author Vova Feldman (@svovaf)
9794
+ * @since 1.1.4
9795
+ *
9796
+ * @param string $tag
9797
+ * @param callable|bool $function_to_check Optional. The callback to check for. Default false.
9798
+ *
9799
+ * @return false|int
9800
+ *
9801
+ * @uses has_filter()
9802
+ */
9803
+ function has_filter( $tag, $function_to_check = false ) {
9804
+ $this->_logger->entrance( $tag );
9805
+
9806
+ return has_filter( $this->get_action_tag( $tag ), $function_to_check );
9807
+ }
9808
+
9809
+ #endregion
9810
+
9811
+ /**
9812
+ * Override default i18n text phrases.
9813
+ *
9814
+ * @author Vova Feldman (@svovaf)
9815
+ * @since 1.1.6
9816
+ *
9817
+ * @param string[] string $key_value
9818
+ *
9819
+ * @uses fs_override_i18n()
9820
+ */
9821
+ function override_i18n( $key_value ) {
9822
+ fs_override_i18n( $key_value, $this->_slug );
9823
+ }
9824
+
9825
+ /* Account Page
9826
+ ------------------------------------------------------------------------------------------------------------------*/
9827
+ /**
9828
+ * Update site information.
9829
+ *
9830
+ * @author Vova Feldman (@svovaf)
9831
+ * @since 1.0.1
9832
+ *
9833
+ * @param bool $store Flush to Database if true.
9834
+ */
9835
+ private function _store_site( $store = true ) {
9836
+ $this->_logger->entrance();
9837
+
9838
+ if ( empty( $this->_site->id ) ) {
9839
+ $this->_logger->error( "Empty install ID, can't store site." );
9840
+
9841
+ return;
9842
+ }
9843
+
9844
+ $encrypted_site = clone $this->_site;
9845
+ $encrypted_site->plan = self::_encrypt_entity( $this->_site->plan );
9846
+
9847
+ $sites = self::get_all_sites( $this->_module_type );
9848
+ $sites[ $this->_slug ] = $encrypted_site;
9849
+
9850
+ $this->set_account_option( 'sites', $sites, $store );
9851
+ }
9852
+
9853
+ /**
9854
+ * Update plugin's plans information.
9855
+ *
9856
+ * @author Vova Feldman (@svovaf)
9857
+ * @since 1.0.2
9858
+ *
9859
+ * @param bool $store Flush to Database if true.
9860
+ */
9861
+ private function _store_plans( $store = true ) {
9862
+ $this->_logger->entrance();
9863
+
9864
+ $plans = self::get_all_plans( $this->_module_type );
9865
+
9866
+ // Copy plans.
9867
+ $encrypted_plans = array();
9868
+ for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
9869
+ $encrypted_plans[] = self::_encrypt_entity( $this->_plans[ $i ] );
9870
+ }
9871
+
9872
+ $plans[ $this->_slug ] = $encrypted_plans;
9873
+
9874
+ $this->set_account_option( 'plans', $plans, $store );
9875
+ }
9876
+
9877
+ /**
9878
+ * Update user's plugin licenses.
9879
+ *
9880
+ * @author Vova Feldman (@svovaf)
9881
+ * @since 1.0.5
9882
+ *
9883
+ * @param bool $store
9884
+ * @param string|bool $plugin_slug
9885
+ * @param FS_Plugin_License[] $licenses
9886
+ */
9887
+ private function _store_licenses( $store = true, $plugin_slug = false, $licenses = array() ) {
9888
+ $this->_logger->entrance();
9889
+
9890
+ $all_licenses = self::get_all_licenses( $this->_module_type );
9891
+
9892
+ if ( ! is_string( $plugin_slug ) ) {
9893
+ $plugin_slug = $this->_slug;
9894
+ $licenses = $this->_licenses;
9895
+ }
9896
+
9897
+ if ( ! isset( $all_licenses[ $plugin_slug ] ) ) {
9898
+ $all_licenses[ $plugin_slug ] = array();
9899
+ }
9900
+
9901
+ $all_licenses[ $plugin_slug ][ $this->_user->id ] = $licenses;
9902
+
9903
+ $this->set_account_option( 'licenses', $all_licenses, $store );
9904
+ }
9905
+
9906
+ /**
9907
+ * Update user information.
9908
+ *
9909
+ * @author Vova Feldman (@svovaf)
9910
+ * @since 1.0.1
9911
+ *
9912
+ * @param bool $store Flush to Database if true.
9913
+ */
9914
+ private function _store_user( $store = true ) {
9915
+ $this->_logger->entrance();
9916
+
9917
+ if ( empty( $this->_user->id ) ) {
9918
+ $this->_logger->error( "Empty user ID, can't store user." );
9919
+
9920
+ return;
9921
+ }
9922
+
9923
+ $users = self::get_all_users();
9924
+ $users[ $this->_user->id ] = $this->_user;
9925
+ self::$_accounts->set_option( 'users', $users, $store );
9926
+ }
9927
+
9928
+ /**
9929
+ * Update new updates information.
9930
+ *
9931
+ * @author Vova Feldman (@svovaf)
9932
+ * @since 1.0.4
9933
+ *
9934
+ * @param FS_Plugin_Tag|null $update
9935
+ * @param bool $store Flush to Database if true.
9936
+ * @param bool|number $plugin_id
9937
+ */
9938
+ private function _store_update( $update, $store = true, $plugin_id = false ) {
9939
+ $this->_logger->entrance();
9940
+
9941
+ if ( $update instanceof FS_Plugin_Tag ) {
9942
+ $update->updated = time();
9943
+ }
9944
+
9945
+ if ( ! is_numeric( $plugin_id ) ) {
9946
+ $plugin_id = $this->_plugin->id;
9947
+ }
9948
+
9949
+ $updates = self::get_all_updates();
9950
+ $updates[ $plugin_id ] = $update;
9951
+ self::$_accounts->set_option( 'updates', $updates, $store );
9952
+ }
9953
+
9954
+ /**
9955
+ * Update new updates information.
9956
+ *
9957
+ * @author Vova Feldman (@svovaf)
9958
+ * @since 1.0.6
9959
+ *
9960
+ * @param FS_Plugin[] $plugin_addons
9961
+ * @param bool $store Flush to Database if true.
9962
+ */
9963
+ private function _store_addons( $plugin_addons, $store = true ) {
9964
+ $this->_logger->entrance();
9965
+
9966
+ $addons = self::get_all_addons();
9967
+ $addons[ $this->_plugin->id ] = $plugin_addons;
9968
+ self::$_accounts->set_option( 'addons', $addons, $store );
9969
+ }
9970
+
9971
+ /**
9972
+ * Delete plugin's associated add-ons.
9973
+ *
9974
+ * @author Vova Feldman (@svovaf)
9975
+ * @since 1.0.8
9976
+ *
9977
+ * @param bool $store
9978
+ *
9979
+ * @return bool
9980
+ */
9981
+ private function _delete_account_addons( $store = true ) {
9982
+ $all_addons = self::get_all_account_addons();
9983
+
9984
+ if ( ! isset( $all_addons[ $this->_plugin->id ] ) ) {
9985
+ return false;
9986
+ }
9987
+
9988
+ unset( $all_addons[ $this->_plugin->id ] );
9989
+
9990
+ self::$_accounts->set_option( 'account_addons', $all_addons, $store );
9991
+
9992
+ return true;
9993
+ }
9994
+
9995
+ /**
9996
+ * Update account add-ons list.
9997
+ *
9998
+ * @author Vova Feldman (@svovaf)
9999
+ * @since 1.0.6
10000
+ *
10001
+ * @param FS_Plugin[] $addons
10002
+ * @param bool $store Flush to Database if true.
10003
+ */
10004
+ private function _store_account_addons( $addons, $store = true ) {
10005
+ $this->_logger->entrance();
10006
+
10007
+ $all_addons = self::get_all_account_addons();
10008
+ $all_addons[ $this->_plugin->id ] = $addons;
10009
+ self::$_accounts->set_option( 'account_addons', $all_addons, $store );
10010
+ }
10011
+
10012
+ /**
10013
+ * Store account params in the Database.
10014
+ *
10015
+ * @author Vova Feldman (@svovaf)
10016
+ * @since 1.0.1
10017
+ */
10018
+ private function _store_account() {
10019
+ $this->_logger->entrance();
10020
+
10021
+ $this->_store_site( false );
10022
+ $this->_store_user( false );
10023
+ $this->_store_plans( false );
10024
+ $this->_store_licenses( false );
10025
+
10026
+ self::$_accounts->store();
10027
+ }
10028
+
10029
+ /**
10030
+ * Sync user's information.
10031
+ *
10032
+ * @author Vova Feldman (@svovaf)
10033
+ * @since 1.0.3
10034
+ * @uses FS_Api
10035
+ */
10036
+ private function _handle_account_user_sync() {
10037
+ $this->_logger->entrance();
10038
+
10039
+ $api = $this->get_api_user_scope();
10040
+
10041
+ // Get user's information.
10042
+ $user = $api->get( '/', true );
10043
+
10044
+ if ( isset( $user->id ) ) {
10045
+ $this->_user->first = $user->first;
10046
+ $this->_user->last = $user->last;
10047
+ $this->_user->email = $user->email;
10048
+
10049
+ $is_menu_item_account_visible = $this->is_submenu_item_visible( 'account' );
10050
+
10051
+ if ( $user->is_verified &&
10052
+ ( ! isset( $this->_user->is_verified ) || false === $this->_user->is_verified )
10053
+ ) {
10054
+ $this->_user->is_verified = true;
10055
+
10056
+ $this->do_action( 'account_email_verified', $user->email );
10057
+
10058
+ $this->_admin_notices->add(
10059
+ $this->get_text( 'email-verified-message' ),
10060
+ $this->get_text( 'right-on' ) . '!',
10061
+ 'success',
10062
+ // Make admin sticky if account menu item is invisible,
10063
+ // since the page will be auto redirected to the plugin's
10064
+ // main settings page, and the non-sticky message
10065
+ // will disappear.
10066
+ ! $is_menu_item_account_visible,
10067
+ false,
10068
+ 'email_verified'
10069
+ );
10070
+ }
10071
+
10072
+ // Flush user details to DB.
10073
+ $this->_store_user();
10074
+
10075
+ $this->do_action( 'after_account_user_sync', $user );
10076
+
10077
+ /**
10078
+ * If account menu item is hidden, redirect to plugin's main settings page.
10079
+ *
10080
+ * @author Vova Feldman (@svovaf)
10081
+ * @since 1.1.6
10082
+ *
10083
+ * @link https://github.com/Freemius/wordpress-sdk/issues/6
10084
+ */
10085
+ if ( ! $is_menu_item_account_visible ) {
10086
+ fs_redirect( $this->_get_admin_page_url() );
10087
+ }
10088
+ }
10089
+ }
10090
+
10091
+ /**
10092
+ * @param bool $store
10093
+ *
10094
+ * @return FS_Plugin_Plan|object|false
10095
+ */
10096
+ private function _enrich_site_plan( $store = true ) {
10097
+ // Try to load plan from local cache.
10098
+ $plan = $this->_get_plan_by_id( $this->_site->plan->id );
10099
+
10100
+ if ( false === $plan ) {
10101
+ $plan = $this->_fetch_site_plan();
10102
+ }
10103
+
10104
+ if ( $plan instanceof FS_Plugin_Plan ) {
10105
+ $this->_update_plan( $plan, $store );
10106
+ }
10107
+
10108
+ return $plan;
10109
+ }
10110
+
10111
+ /**
10112
+ * @author Vova Feldman (@svovaf)
10113
+ * @since 1.0.9
10114
+ * @uses FS_Api
10115
+ *
10116
+ * @param bool $store
10117
+ *
10118
+ * @return FS_Plugin_Plan|object|false
10119
+ */
10120
+ private function _enrich_site_trial_plan( $store = true ) {
10121
+ // Try to load plan from local cache.
10122
+ $trial_plan = $this->_get_plan_by_id( $this->_site->trial_plan_id );
10123
+
10124
+ if ( false === $trial_plan ) {
10125
+ $trial_plan = $this->_fetch_site_plan( $this->_site->trial_plan_id );
10126
+ }
10127
+
10128
+ if ( $trial_plan instanceof FS_Plugin_Plan ) {
10129
+ $this->_storage->store( 'trial_plan', $trial_plan, $store );
10130
+ }
10131
+
10132
+ return $trial_plan;
10133
+ }
10134
+
10135
+ /**
10136
+ * @author Vova Feldman (@svovaf)
10137
+ * @since 1.0.9
10138
+ * @uses FS_Api
10139
+ *
10140
+ * @param number|bool $license_id
10141
+ *
10142
+ * @return FS_Subscription|object|bool
10143
+ */
10144
+ private function _fetch_site_license_subscription( $license_id = false ) {
10145
+ $this->_logger->entrance();
10146
+ $api = $this->get_api_site_scope();
10147
+
10148
+ if ( ! is_numeric( $license_id ) ) {
10149
+ $license_id = $this->_license->id;
10150
+ }
10151
+
10152
+ $result = $api->get( "/licenses/{$license_id}/subscriptions.json", true );
10153
+
10154
+ return ! isset( $result->error ) ?
10155
+ ( ( is_array( $result->subscriptions ) && 0 < count( $result->subscriptions ) ) ?
10156
+ new FS_Subscription( $result->subscriptions[0] ) :
10157
+ false
10158
+ ) :
10159
+ $result;
10160
+ }
10161
+
10162
+ /**
10163
+ * @author Vova Feldman (@svovaf)
10164
+ * @since 1.0.4
10165
+ * @uses FS_Api
10166
+ *
10167
+ * @param number|bool $plan_id
10168
+ *
10169
+ * @return FS_Plugin_Plan|object
10170
+ */
10171
+ private function _fetch_site_plan( $plan_id = false ) {
10172
+ $this->_logger->entrance();
10173
+ $api = $this->get_api_site_scope();
10174
+
10175
+ if ( ! is_numeric( $plan_id ) ) {
10176
+ $plan_id = $this->_site->plan->id;
10177
+ }
10178
+
10179
+ $plan = $api->get( "/plans/{$plan_id}.json", true );
10180
+
10181
+ return ! isset( $plan->error ) ? new FS_Plugin_Plan( $plan ) : $plan;
10182
+ }
10183
+
10184
+ /**
10185
+ * @author Vova Feldman (@svovaf)
10186
+ * @since 1.0.5
10187
+ * @uses FS_Api
10188
+ *
10189
+ * @return FS_Plugin_Plan[]|object
10190
+ */
10191
+ private function _fetch_plugin_plans() {
10192
+ $this->_logger->entrance();
10193
+ $api = $this->get_api_site_scope();
10194
+
10195
+ $result = $api->get( '/plans.json', true );
10196
+
10197
+ if ( $this->is_api_result_object( $result, 'plans' ) && is_array( $result->plans ) ) {
10198
+ for ( $i = 0, $len = count( $result->plans ); $i < $len; $i ++ ) {
10199
+ $result->plans[ $i ] = new FS_Plugin_Plan( $result->plans[ $i ] );
10200
+ }
10201
+
10202
+ $result = $result->plans;
10203
+ }
10204
+
10205
+ return $result;
10206
+ }
10207
+
10208
+ /**
10209
+ * @author Vova Feldman (@svovaf)
10210
+ * @since 1.0.5
10211
+ * @uses FS_Api
10212
+ *
10213
+ * @param number|bool $plugin_id
10214
+ * @param number|bool $site_license_id
10215
+ *
10216
+ * @return FS_Plugin_License[]|object
10217
+ */
10218
+ private function _fetch_licenses( $plugin_id = false, $site_license_id = false ) {
10219
+ $this->_logger->entrance();
10220
+
10221
+ $api = $this->get_api_user_scope();
10222
+
10223
+ if ( ! is_numeric( $plugin_id ) ) {
10224
+ $plugin_id = $this->_plugin->id;
10225
+ }
10226
+
10227
+ $result = $api->get( "/plugins/{$plugin_id}/licenses.json", true );
10228
+
10229
+ $is_site_license_synced = false;
10230
+
10231
+ $api_errors = array();
10232
+
10233
+ if ( $this->is_api_result_object( $result, 'licenses' ) &&
10234
+ is_array( $result->licenses )
10235
+ ) {
10236
+ for ( $i = 0, $len = count( $result->licenses ); $i < $len; $i ++ ) {
10237
+ $result->licenses[ $i ] = new FS_Plugin_License( $result->licenses[ $i ] );
10238
+
10239
+ if ( ( ! $is_site_license_synced ) && is_numeric( $site_license_id ) ) {
10240
+ $is_site_license_synced = ( $site_license_id == $result->licenses[ $i ]->id );
10241
+ }
10242
+ }
10243
+
10244
+ $result = $result->licenses;
10245
+ } else {
10246
+ $api_errors[] = $result;
10247
+ $result = array();
10248
+ }
10249
+
10250
+ if ( ! $is_site_license_synced ) {
10251
+ $api = $this->get_api_site_scope();
10252
+
10253
+ if ( is_numeric( $site_license_id ) ) {
10254
+ // Try to retrieve a foreign license that is linked to the install.
10255
+ $api_result = $api->call( '/licenses.json' );
10256
+
10257
+ if ( $this->is_api_result_object( $api_result, 'licenses' ) &&
10258
+ is_array( $api_result->licenses )
10259
+ ) {
10260
+ $licenses = $api_result->licenses;
10261
+
10262
+ if ( ! empty( $licenses ) ) {
10263
+ $result[] = new FS_Plugin_License( $licenses[0] );
10264
+ }
10265
+ } else {
10266
+ $api_errors[] = $api_result;
10267
+ }
10268
+ } else if ( is_object( $this->_license ) ) {
10269
+ // Fetch foreign license by ID and license key.
10270
+ $license = $api->get( "/licenses/{$this->_license->id}.json?license_key=" .
10271
+ urlencode( $this->_license->secret_key ) );
10272
+
10273
+ if ( $this->is_api_result_entity( $license ) ) {
10274
+ $result[] = new FS_Plugin_License( $license );
10275
+ } else {
10276
+ $api_errors[] = $license;
10277
+ }
10278
+ }
10279
+ }
10280
+
10281
+ if ( is_array( $result ) && 0 < count( $result ) ) {
10282
+ // If found at least one license, return license collection even if there are errors.
10283
+ return $result;
10284
+ }
10285
+
10286
+ if ( ! empty( $api_errors ) ) {
10287
+ // If found any errors and no licenses, return first error.
10288
+ return $api_errors[0];
10289
+ }
10290
+
10291
+ // Fallback to empty licenses list.
10292
+ return $result;
10293
+ }
10294
+
10295
+ /**
10296
+ * @author Vova Feldman (@svovaf)
10297
+ * @since 1.2.0
10298
+ * @uses FS_Api
10299
+ *
10300
+ * @param number|bool $plugin_id
10301
+ * @param bool $flush
10302
+ *
10303
+ * @return FS_Payment[]|object
10304
+ */
10305
+ function _fetch_payments( $plugin_id = false, $flush = false ) {
10306
+ $this->_logger->entrance();
10307
+
10308
+ $api = $this->get_api_user_scope();
10309
+
10310
+ if ( ! is_numeric( $plugin_id ) ) {
10311
+ $plugin_id = $this->_plugin->id;
10312
+ }
10313
+
10314
+ $result = $api->get( "/plugins/{$plugin_id}/payments.json?include_addons=true", $flush );
10315
+
10316
+ if ( ! isset( $result->error ) ) {
10317
+ for ( $i = 0, $len = count( $result->payments ); $i < $len; $i ++ ) {
10318
+ $result->payments[ $i ] = new FS_Payment( $result->payments[ $i ] );
10319
+ }
10320
+ $result = $result->payments;
10321
+ }
10322
+
10323
+ return $result;
10324
+ }
10325
+
10326
+ /**
10327
+ * @author Vova Feldman (@svovaf)
10328
+ * @since 1.2.1.5
10329
+ * @uses FS_Api
10330
+ *
10331
+ * @param bool $flush
10332
+ *
10333
+ * @return \FS_Billing|mixed
10334
+ */
10335
+ function _fetch_billing( $flush = false ) {
10336
+ require_once WP_FS__DIR_INCLUDES . '/entities/class-fs-billing.php';
10337
+
10338
+ $billing = $this->get_api_user_scope()->get( 'billing.json', $flush );
10339
+
10340
+ if ( $this->is_api_result_entity( $billing ) ) {
10341
+ $billing = new FS_Billing( $billing );
10342
+ }
10343
+
10344
+ return $billing;
10345
+ }
10346
+
10347
+ /**
10348
+ * @author Vova Feldman (@svovaf)
10349
+ * @since 1.0.4
10350
+ *
10351
+ * @param FS_Plugin_Plan $plan
10352
+ * @param bool $store
10353
+ */
10354
+ private function _update_plan( $plan, $store = false ) {
10355
+ $this->_logger->entrance();
10356
+
10357
+ $this->_site->plan = $plan;
10358
+ $this->_store_site( $store );
10359
+ }
10360
+
10361
+ /**
10362
+ * @author Vova Feldman (@svovaf)
10363
+ * @since 1.0.5
10364
+ *
10365
+ * @param FS_Plugin_License[] $licenses
10366
+ * @param string|bool $plugin_slug
10367
+ */
10368
+ private function _update_licenses( $licenses, $plugin_slug = false ) {
10369
+ $this->_logger->entrance();
10370
+
10371
+ if ( is_array( $licenses ) ) {
10372
+ for ( $i = 0, $len = count( $licenses ); $i < $len; $i ++ ) {
10373
+ $licenses[ $i ]->updated = time();
10374
+ }
10375
+ }
10376
+
10377
+ if ( ! is_string( $plugin_slug ) ) {
10378
+ $this->_licenses = $licenses;
10379
+ }
10380
+
10381
+ $this->_store_licenses( true, $plugin_slug, $licenses );
10382
+ }
10383
+
10384
+ /**
10385
+ * @author Vova Feldman (@svovaf)
10386
+ * @since 1.0.4
10387
+ *
10388
+ * @param bool|number $plugin_id
10389
+ * @param bool $flush Since 1.1.7.3
10390
+ * @param int $expiration Since 1.2.2.7
10391
+ *
10392
+ * @return object|false New plugin tag info if exist.
10393
+ */
10394
+ private function _fetch_newer_version( $plugin_id = false, $flush = true, $expiration = WP_FS__TIME_24_HOURS_IN_SEC ) {
10395
+ $latest_tag = $this->_fetch_latest_version( $plugin_id, $flush, $expiration );
10396
+
10397
+ if ( ! is_object( $latest_tag ) ) {
10398
+ return false;
10399
+ }
10400
+
10401
+ // Check if version is actually newer.
10402
+ $has_new_version =
10403
+ // If it's an non-installed add-on then always return latest.
10404
+ ( $this->_is_addon_id( $plugin_id ) && ! $this->is_addon_activated( $plugin_id ) ) ||
10405
+ // Compare versions.
10406
+ version_compare( $this->get_plugin_version(), $latest_tag->version, '<' );
10407
+
10408
+ $this->_logger->departure( $has_new_version ? 'Found newer plugin version ' . $latest_tag->version : 'No new version' );
10409
+
10410
+ return $has_new_version ? $latest_tag : false;
10411
+ }
10412
+
10413
+ /**
10414
+ * @author Vova Feldman (@svovaf)
10415
+ * @since 1.0.5
10416
+ *
10417
+ * @param bool|number $plugin_id
10418
+ * @param bool $flush Since 1.1.7.3
10419
+ * @param int $expiration Since 1.2.2.7
10420
+ *
10421
+ * @return bool|FS_Plugin_Tag
10422
+ */
10423
+ function get_update( $plugin_id = false, $flush = true, $expiration = WP_FS__TIME_24_HOURS_IN_SEC ) {
10424
+ $this->_logger->entrance();
10425
+
10426
+ if ( ! is_numeric( $plugin_id ) ) {
10427
+ $plugin_id = $this->_plugin->id;
10428
+ }
10429
+
10430
+ $this->check_updates( true, $plugin_id, $flush, $expiration );
10431
+ $updates = $this->get_all_updates();
10432
+
10433
+ return isset( $updates[ $plugin_id ] ) && is_object( $updates[ $plugin_id ] ) ? $updates[ $plugin_id ] : false;
10434
+ }
10435
+
10436
+ /**
10437
+ * Check if site assigned with active license.
10438
+ *
10439
+ * @author Vova Feldman (@svovaf)
10440
+ * @since 1.0.6
10441
+ *
10442
+ * @deprecated Please use has_active_valid_license() instead because license can be cancelled.
10443
+ */
10444
+ function has_active_license() {
10445
+ return (
10446
+ is_object( $this->_license ) &&
10447
+ is_numeric( $this->_license->id ) &&
10448
+ ! $this->_license->is_expired()
10449
+ );
10450
+ }
10451
+
10452
+ /**
10453
+ * Check if site assigned with active & valid (not expired) license.
10454
+ *
10455
+ * @author Vova Feldman (@svovaf)
10456
+ * @since 1.2.1
10457
+ */
10458
+ function has_active_valid_license() {
10459
+ return (
10460
+ is_object( $this->_license ) &&
10461
+ is_numeric( $this->_license->id ) &&
10462
+ $this->_license->is_active() &&
10463
+ $this->_license->is_valid()
10464
+ );
10465
+ }
10466
+
10467
+ /**
10468
+ * Check if site assigned with license with enabled features.
10469
+ *
10470
+ * @author Vova Feldman (@svovaf)
10471
+ * @since 1.0.6
10472
+ *
10473
+ * @return bool
10474
+ */
10475
+ function has_features_enabled_license() {
10476
+ return (
10477
+ is_object( $this->_license ) &&
10478
+ is_numeric( $this->_license->id ) &&
10479
+ $this->_license->is_features_enabled()
10480
+ );
10481
+ }
10482
+
10483
+ /**
10484
+ * Check if user is a trial or have feature enabled license.
10485
+ *
10486
+ * @author Vova Feldman (@svovaf)
10487
+ * @since 1.1.7
10488
+ *
10489
+ * @return bool
10490
+ */
10491
+ function can_use_premium_code() {
10492
+ return $this->is_trial() || $this->has_features_enabled_license();
10493
+ }
10494
+
10495
+ /**
10496
+ * Checks if the current user can activate plugins or switch themes. Note that this method should only be used
10497
+ * after the `init` action is triggered because it is using `current_user_can()` which is only functional after
10498
+ * the context user is authenticated.
10499
+ *
10500
+ * @author Leo Fajardo (@leorw)
10501
+ * @since 1.2.2
10502
+ *
10503
+ * @return bool
10504
+ */
10505
+ function is_user_admin() {
10506
+ return ( $this->is_plugin() && current_user_can( 'activate_plugins' ) )
10507
+ || ( $this->is_theme() && current_user_can( 'switch_themes' ) );
10508
+ }
10509
+
10510
+ /**
10511
+ * Sync site's plan.
10512
+ *
10513
+ * @author Vova Feldman (@svovaf)
10514
+ * @since 1.0.3
10515
+ *
10516
+ * @uses FS_Api
10517
+ *
10518
+ * @param bool $background Hints the method if it's a background sync. If false, it means that was initiated by
10519
+ * the admin.
10520
+ */
10521
+ private function _sync_license( $background = false ) {
10522
+ $this->_logger->entrance();
10523
+
10524
+ $plugin_id = fs_request_get( 'plugin_id', $this->get_id() );
10525
+
10526
+ $is_addon_sync = ( ! $this->_plugin->is_addon() && $plugin_id != $this->get_id() );
10527
+
10528
+ if ( $is_addon_sync ) {
10529
+ $this->_sync_addon_license( $plugin_id, $background );
10530
+ } else {
10531
+ $this->_sync_plugin_license( $background );
10532
+ }
10533
+
10534
+ $this->do_action( 'after_account_plan_sync', $this->_site->plan->name );
10535
+ }
10536
+
10537
+ /**
10538
+ * Sync plugin's add-on license.
10539
+ *
10540
+ * @author Vova Feldman (@svovaf)
10541
+ * @since 1.0.6
10542
+ * @uses FS_Api
10543
+ *
10544
+ * @param number $addon_id
10545
+ * @param bool $background
10546
+ */
10547
+ private function _sync_addon_license( $addon_id, $background ) {
10548
+ $this->_logger->entrance();
10549
+
10550
+ if ( $this->is_addon_activated( $addon_id ) ) {
10551
+ // If already installed, use add-on sync.
10552
+ $fs_addon = self::get_instance_by_id( $addon_id );
10553
+ $fs_addon->_sync_license( $background );
10554
+
10555
+ return;
10556
+ }
10557
+
10558
+ // Validate add-on exists.
10559
+ $addon = $this->get_addon( $addon_id );
10560
+
10561
+ if ( ! is_object( $addon ) ) {
10562
+ return;
10563
+ }
10564
+
10565
+ // Add add-on into account add-ons.
10566
+ $account_addons = $this->get_account_addons();
10567
+ if ( ! is_array( $account_addons ) ) {
10568
+ $account_addons = array();
10569
+ }
10570
+ $account_addons[] = $addon->id;
10571
+ $account_addons = array_unique( $account_addons );
10572
+ $this->_store_account_addons( $account_addons );
10573
+
10574
+ // Load add-on licenses.
10575
+ $licenses = $this->_fetch_licenses( $addon->id );
10576
+
10577
+ // Sync add-on licenses.
10578
+ if ( $this->is_array_instanceof( $licenses, 'FS_Plugin_License' ) ) {
10579
+ $this->_update_licenses( $licenses, $addon->slug );
10580
+
10581
+ if ( ! $this->is_addon_installed( $addon->id ) && FS_License_Manager::has_premium_license( $licenses ) ) {
10582
+ $plans_result = $this->get_api_site_or_plugin_scope()->get( "/addons/{$addon_id}/plans.json" );
10583
+
10584
+ if ( ! isset( $plans_result->error ) ) {
10585
+ $plans = array();
10586
+ foreach ( $plans_result->plans as $plan ) {
10587
+ $plans[] = new FS_Plugin_Plan( $plan );
10588
+ }
10589
+
10590
+ $this->_admin_notices->add_sticky(
10591
+ FS_Plan_Manager::instance()->has_free_plan( $plans ) ?
10592
+ sprintf(
10593
+ $this->get_text( 'addon-successfully-upgraded-message' ),
10594
+ $addon->title
10595
+ ) . ' ' . $this->get_latest_download_link(
10596
+ $this->get_text( 'download-latest-version' ),
10597
+ $addon_id
10598
+ )
10599
+ :
10600
+ sprintf(
10601
+ $this->get_text( 'addon-successfully-purchased-message' ),
10602
+ $addon->title
10603
+ ) . ' ' . $this->get_latest_download_link(
10604
+ $this->get_text( 'download-latest-version' ),
10605
+ $addon_id
10606
+ ),
10607
+ 'addon_plan_upgraded_' . $addon->slug,
10608
+ $this->get_text( 'yee-haw' ) . '!'
10609
+ );
10610
+ }
10611
+ }
10612
+ }
10613
+ }
10614
+
10615
+ /**
10616
+ * Sync site's plugin plan.
10617
+ *
10618
+ * @author Vova Feldman (@svovaf)
10619
+ * @since 1.0.6
10620
+ * @uses FS_Api
10621
+ *
10622
+ * @param bool $background Hints the method if it's a background sync. If false, it means that was initiated by
10623
+ * the admin.
10624
+ */
10625
+ private function _sync_plugin_license( $background = false ) {
10626
+ $this->_logger->entrance();
10627
+
10628
+ /**
10629
+ * Sync site info.
10630
+ *
10631
+ * @todo This line will execute install sync on a daily basis, even if running the free version (for opted-in users). The reason we want to keep it that way is for cases when the user was a paying customer, then there was a failure in subscription payment, and then after some time the payment was successful. This could be heavily optimized. For example, we can skip the $flush if the current install was never associated with a paid version.
10632
+ */
10633
+ $site = $this->send_install_update( array(), true );
10634
+
10635
+ $plan_change = 'none';
10636
+
10637
+ if ( ! $this->is_api_result_entity( $site ) ) {
10638
+ // Show API messages only if not background sync or if paying customer.
10639
+ if ( ! $background || $this->is_paying() ) {
10640
+ // Try to ping API to see if not blocked.
10641
+ if ( ! FS_Api::test() ) {
10642
+ /**
10643
+ * Failed to ping API - blocked!
10644
+ *
10645
+ * @author Vova Feldman (@svovaf)
10646
+ * @since 1.1.6 Only show message related to one of the Freemius powered plugins. Once it will be resolved it will fix the issue for all plugins anyways. There's no point to scare users with multiple error messages.
10647
+ */
10648
+ $api = $this->get_api_site_scope();
10649
+
10650
+ if ( ! self::$_global_admin_notices->has_sticky( 'api_blocked' ) ) {
10651
+ self::$_global_admin_notices->add(
10652
+ sprintf(
10653
+ $this->get_text( 'server-blocking-access' ),
10654
+ $this->get_plugin_name(),
10655
+ '<a href="' . $api->get_url() . '" target="_blank">' . $api->get_url() . '</a>'
10656
+ ) . '<br> ' . $this->get_text( 'server-error-message' ) . var_export( $site->error, true ),
10657
+ $this->get_text( 'oops' ) . '...',
10658
+ 'error',
10659
+ $background,
10660
+ false,
10661
+ 'api_blocked'
10662
+ );
10663
+ }
10664
+ } else {
10665
+ // Authentication params are broken.
10666
+ $this->_admin_notices->add(
10667
+ $this->get_text( 'wrong-authentication-param-message' ),
10668
+ $this->get_text( 'oops' ) . '...',
10669
+ 'error'
10670
+ );
10671
+ }
10672
+ }
10673
+
10674
+ // No reason to continue with license sync while there are API issues.
10675
+ return;
10676
+ }
10677
+
10678
+ // Remove sticky API connectivity message.
10679
+ self::$_global_admin_notices->remove_sticky( 'api_blocked' );
10680
+
10681
+ $site = new FS_Site( $site );
10682
+
10683
+ // Sync plans.
10684
+ $this->_sync_plans();
10685
+
10686
+ if ( ! $this->has_paid_plan() ) {
10687
+ $this->_site = $site;
10688
+ $this->_enrich_site_plan( true );
10689
+ $this->_store_site();
10690
+ } else {
10691
+ /**
10692
+ * Sync licenses. Pass the site's license ID so that the foreign licenses will be fetched if the license
10693
+ * associated with that ID is not included in the user's licenses collection.
10694
+ */
10695
+ $this->_sync_licenses( $site->license_id );
10696
+
10697
+ // Check if plan / license changed.
10698
+ if ( ! FS_Entity::equals( $site->plan, $this->_site->plan ) ||
10699
+ // Check if trial started.
10700
+ $site->trial_plan_id != $this->_site->trial_plan_id ||
10701
+ $site->trial_ends != $this->_site->trial_ends ||
10702
+ // Check if license changed.
10703
+ $site->license_id != $this->_site->license_id
10704
+ ) {
10705
+ if ( $site->is_trial() && ( ! $this->_site->is_trial() || $site->trial_ends != $this->_site->trial_ends ) ) {
10706
+ // New trial started.
10707
+ $this->_site = $site;
10708
+ $plan_change = 'trial_started';
10709
+
10710
+ // Store trial plan information.
10711
+ $this->_enrich_site_trial_plan( true );
10712
+
10713
+ // For trial with subscription use-case.
10714
+ $new_license = is_null( $site->license_id ) ? null : $this->_get_license_by_id( $site->license_id );
10715
+
10716
+ if ( is_object( $new_license ) && $new_license->is_valid() ) {
10717
+ $this->_site = $site;
10718
+ $this->_update_site_license( $new_license );
10719
+ $this->_store_licenses();
10720
+ $this->_enrich_site_plan( true );
10721
+
10722
+ $this->_sync_site_subscription( $this->_license );
10723
+ }
10724
+ } else if ( $this->_site->is_trial() && ! $site->is_trial() && ! is_numeric( $site->license_id ) ) {
10725
+ // Was in trial, but now trial expired and no license ID.
10726
+ // New trial started.
10727
+ $this->_site = $site;
10728
+ $plan_change = 'trial_expired';
10729
+
10730
+ // Clear trial plan information.
10731
+ $this->_storage->trial_plan = null;
10732
+
10733
+ } else {
10734
+ $is_free = $this->is_free_plan();
10735
+
10736
+ // Make sure license exist and not expired.
10737
+ $new_license = is_null( $site->license_id ) ?
10738
+ null :
10739
+ $this->_get_license_by_id( $site->license_id );
10740
+
10741
+ if ( $is_free && is_null( $new_license ) && $this->has_any_license() && $this->_license->is_cancelled ) {
10742
+ // License cancelled.
10743
+ $this->_site = $site;
10744
+ $this->_update_site_license( $new_license );
10745
+ $this->_store_licenses();
10746
+ $this->_enrich_site_plan( true );
10747
+
10748
+ $plan_change = 'cancelled';
10749
+ } else if ( $is_free && ( ( ! is_object( $new_license ) || $new_license->is_expired() ) ) ) {
10750
+ // The license is expired, so ignore upgrade method.
10751
+ } else {
10752
+ // License changed.
10753
+ $this->_site = $site;
10754
+ $this->_update_site_license( $new_license );
10755
+ $this->_store_licenses();
10756
+ $this->_enrich_site_plan( true );
10757
+
10758
+ $plan_change = $is_free ?
10759
+ 'upgraded' :
10760
+ ( is_object( $new_license ) ?
10761
+ 'changed' :
10762
+ 'downgraded' );
10763
+ }
10764
+ }
10765
+
10766
+ // Store updated site info.
10767
+ $this->_store_site();
10768
+ } else {
10769
+ if ( is_object( $this->_license ) && $this->_license->is_expired() ) {
10770
+ if ( ! $this->has_features_enabled_license() ) {
10771
+ $this->_deactivate_license();
10772
+ $plan_change = 'downgraded';
10773
+ } else {
10774
+ $plan_change = 'expired';
10775
+ }
10776
+ }
10777
+
10778
+ if ( is_numeric( $site->license_id ) && is_object( $this->_license ) ) {
10779
+ $this->_sync_site_subscription( $this->_license );
10780
+ }
10781
+ }
10782
+ }
10783
+
10784
+ if ( $this->has_paid_plan() ) {
10785
+ switch ( $plan_change ) {
10786
+ case 'none':
10787
+ if ( ! $background && is_admin() ) {
10788
+ $plan = $this->is_trial() ?
10789
+ $this->_storage->trial_plan :
10790
+ $this->_site->plan;
10791
+
10792
+ if ( $plan->is_free() ) {
10793
+ $this->_admin_notices->add(
10794
+ sprintf(
10795
+ $this->get_text( 'plan-did-not-change-message' ),
10796
+ '<i><b>' . $plan->title . ( $this->is_trial() ? ' ' . $this->get_text( 'trial' ) : '' ) . '</b></i>'
10797
+ ) . ' ' . sprintf(
10798
+ '<a href="%s">%s</a>',
10799
+ $this->contact_url(
10800
+ 'bug',
10801
+ sprintf( $this->get_text( 'plan-did-not-change-email-message' ),
10802
+ strtoupper( $plan->name )
10803
+ )
10804
+ ),
10805
+ $this->get_text( 'contact-us-here' )
10806
+ ),
10807
+ $this->get_text( 'hmm' ) . '...'
10808
+ );
10809
+ }
10810
+ }
10811
+ break;
10812
+ case 'upgraded':
10813
+ $this->_admin_notices->add_sticky(
10814
+ sprintf(
10815
+ $this->get_text( 'plan-upgraded-message' ),
10816
+ '<i>' . $this->get_plugin_name() . '</i>'
10817
+ ) . $this->get_complete_upgrade_instructions(),
10818
+ 'plan_upgraded',
10819
+ $this->get_text( 'yee-haw' ) . '!'
10820
+ );
10821
+
10822
+ $this->_admin_notices->remove_sticky( array(
10823
+ 'trial_started',
10824
+ 'trial_promotion',
10825
+ 'trial_expired',
10826
+ 'activation_complete',
10827
+ ) );
10828
+ break;
10829
+ case 'changed':
10830
+ $this->_admin_notices->add_sticky(
10831
+ sprintf(
10832
+ $this->get_text( 'plan-changed-to-x-message' ),
10833
+ $this->_site->plan->title
10834
+ ),
10835
+ 'plan_changed'
10836
+ );
10837
+
10838
+ $this->_admin_notices->remove_sticky( array(
10839
+ 'trial_started',
10840
+ 'trial_promotion',
10841
+ 'trial_expired',
10842
+ 'activation_complete',
10843
+ ) );
10844
+ break;
10845
+ case 'downgraded':
10846
+ $this->_admin_notices->add_sticky(
10847
+ sprintf( $this->get_text( 'license-expired-blocking-message' ), $this->_module_type ),
10848
+ 'license_expired',
10849
+ $this->get_text( 'hmm' ) . '...'
10850
+ );
10851
+ $this->_admin_notices->remove_sticky( 'plan_upgraded' );
10852
+ break;
10853
+ case 'cancelled':
10854
+ $this->_admin_notices->add(
10855
+ $this->get_text( 'license-cancelled' ) . ' ' .
10856
+ sprintf(
10857
+ '<a href="%s">%s</a>',
10858
+ $this->contact_url( 'bug' ),
10859
+ $this->get_text( 'contact-us-here' )
10860
+ ),
10861
+ $this->get_text( 'hmm' ) . '...',
10862
+ 'error'
10863
+ );
10864
+ $this->_admin_notices->remove_sticky( 'plan_upgraded' );
10865
+ break;
10866
+ case 'expired':
10867
+ $this->_admin_notices->add_sticky(
10868
+ sprintf( $this->get_text( 'license-expired-non-blocking-message' ), $this->_site->plan->title ),
10869
+ 'license_expired',
10870
+ $this->get_text( 'hmm' ) . '...'
10871
+ );
10872
+ $this->_admin_notices->remove_sticky( 'plan_upgraded' );
10873
+ break;
10874
+ case 'trial_started':
10875
+ $this->_admin_notices->add_sticky(
10876
+ sprintf(
10877
+ $this->get_text( 'trial-started-message' ),
10878
+ '<i>' . $this->get_plugin_name() . '</i>'
10879
+ ) . $this->get_complete_upgrade_instructions( $this->_storage->trial_plan->title ),
10880
+ 'trial_started',
10881
+ $this->get_text( 'yee-haw' ) . '!'
10882
+ );
10883
+
10884
+ $this->_admin_notices->remove_sticky( array(
10885
+ 'trial_promotion',
10886
+ ) );
10887
+ break;
10888
+ case 'trial_expired':
10889
+ $this->_admin_notices->add_sticky(
10890
+ $this->get_text( 'trial-expired-message' ),
10891
+ 'trial_expired',
10892
+ $this->get_text( 'hmm' ) . '...'
10893
+ );
10894
+ $this->_admin_notices->remove_sticky( array(
10895
+ 'trial_started',
10896
+ 'trial_promotion',
10897
+ 'plan_upgraded',
10898
+ ) );
10899
+ break;
10900
+ }
10901
+ }
10902
+
10903
+ if ( 'none' !== $plan_change ) {
10904
+ $this->do_action( 'after_license_change', $plan_change, $this->_site->plan );
10905
+ }
10906
+ }
10907
+
10908
+ /**
10909
+ * @author Vova Feldman (@svovaf)
10910
+ * @since 1.0.5
10911
+ *
10912
+ * @param bool $background
10913
+ */
10914
+ protected function _activate_license( $background = false ) {
10915
+ $this->_logger->entrance();
10916
+
10917
+ $license_id = fs_request_get( 'license_id' );
10918
+
10919
+ if ( FS_Plugin_License::is_valid_id( $license_id ) && $license_id == $this->_site->license_id ) {
10920
+ // License is already activated.
10921
+ return;
10922
+ }
10923
+
10924
+ $premium_license = FS_Plugin_License::is_valid_id( $license_id ) ?
10925
+ $this->_get_license_by_id( $license_id ) :
10926
+ $this->_get_available_premium_license();
10927
+
10928
+ if ( ! is_object( $premium_license ) ) {
10929
+ return;
10930
+ }
10931
+
10932
+ /**
10933
+ * If the premium license is already associated with the install, just
10934
+ * update the license reference (activation is not required).
10935
+ *
10936
+ * @since 1.1.9
10937
+ */
10938
+ if ( $premium_license->id == $this->_site->license_id ) {
10939
+ // License is already activated.
10940
+ $this->_update_site_license( $premium_license );
10941
+ $this->_enrich_site_plan( false );
10942
+ $this->_store_account();
10943
+
10944
+ return;
10945
+ }
10946
+
10947
+ if ( $this->_site->user_id != $premium_license->user_id ) {
10948
+ $api_request_params = array( 'license_key' => $premium_license->secret_key );
10949
+ } else {
10950
+ $api_request_params = array();
10951
+ }
10952
+
10953
+ $api = $this->get_api_site_scope();
10954
+ $license = $api->call( "/licenses/{$premium_license->id}.json", 'put', $api_request_params );
10955
+
10956
+ if ( ! $this->is_api_result_entity( $license ) ) {
10957
+ if ( ! $background ) {
10958
+ $this->_admin_notices->add( sprintf(
10959
+ '%s %s',
10960
+ $this->get_text( 'license-activation-failed-message' ),
10961
+ ( is_object( $license ) && isset( $license->error ) ?
10962
+ $license->error->message :
10963
+ sprintf( '%s<br><code>%s</code>',
10964
+ $this->get_text( 'server-error-message' ),
10965
+ var_export( $license, true )
10966
+ )
10967
+ )
10968
+ ),
10969
+ $this->get_text( 'hmm' ) . '...',
10970
+ 'error'
10971
+ );
10972
+ }
10973
+
10974
+ return;
10975
+ }
10976
+
10977
+ $premium_license = new FS_Plugin_License( $license );
10978
+
10979
+ // Updated site plan.
10980
+ $site = $this->get_api_site_scope()->get( '/', true );
10981
+ if ( $this->is_api_result_entity( $site ) ) {
10982
+ $this->_site = new FS_Site( $site );
10983
+ }
10984
+ $this->_update_site_license( $premium_license );
10985
+ $this->_enrich_site_plan( false );
10986
+
10987
+ $this->_store_account();
10988
+
10989
+ if ( ! $background ) {
10990
+ $this->_admin_notices->add_sticky(
10991
+ $this->get_text( 'license-activated-message' ) .
10992
+ $this->get_complete_upgrade_instructions(),
10993
+ 'license_activated',
10994
+ $this->get_text( 'yee-haw' ) . '!'
10995
+ );
10996
+ }
10997
+
10998
+ $this->_admin_notices->remove_sticky( array(
10999
+ 'trial_promotion',
11000
+ 'license_expired',
11001
+ ) );
11002
+ }
11003
+
11004
+ /**
11005
+ * @author Vova Feldman (@svovaf)
11006
+ * @since 1.0.5
11007
+ *
11008
+ * @param bool $show_notice
11009
+ */
11010
+ protected function _deactivate_license( $show_notice = true ) {
11011
+ $this->_logger->entrance();
11012
+
11013
+ if ( ! is_object( $this->_license ) ) {
11014
+ $this->_admin_notices->add(
11015
+ sprintf( $this->get_text( 'no-active-license-message' ), $this->_site->plan->title ),
11016
+ $this->get_text( 'hmm' ) . '...'
11017
+ );
11018
+
11019
+ return;
11020
+ }
11021
+
11022
+ $api = $this->get_api_site_scope();
11023
+ $license = $api->call( "/licenses/{$this->_site->license_id}.json", 'delete' );
11024
+
11025
+ if ( isset( $license->error ) ) {
11026
+ $this->_admin_notices->add(
11027
+ $this->get_text( 'license-deactivation-failed-message' ) . '<br> ' .
11028
+ $this->get_text( 'server-error-message' ) . ' ' . var_export( $license->error, true ),
11029
+ $this->get_text( 'hmm' ) . '...',
11030
+ 'error'
11031
+ );
11032
+
11033
+ return;
11034
+ }
11035
+
11036
+ // Update license cache.
11037
+ for ( $i = 0, $len = count( $this->_licenses ); $i < $len; $i ++ ) {
11038
+ if ( $license->id == $this->_licenses[ $i ]->id ) {
11039
+ $this->_licenses[ $i ] = new FS_Plugin_License( $license );
11040
+ }
11041
+ }
11042
+
11043
+ // Updated site plan to default.
11044
+ $this->_sync_plans();
11045
+ $this->_site->plan->id = $this->_plans[0]->id;
11046
+ // Unlink license from site.
11047
+ $this->_update_site_license( null );
11048
+ $this->_enrich_site_plan( false );
11049
+
11050
+ $this->_store_account();
11051
+
11052
+ if ( $show_notice ) {
11053
+ $this->_admin_notices->add(
11054
+ sprintf( $this->get_text( 'license-deactivation-message' ), $this->_site->plan->title ),
11055
+ $this->get_text( 'ok' )
11056
+ );
11057
+ }
11058
+
11059
+ $this->_admin_notices->remove_sticky( array(
11060
+ 'plan_upgraded',
11061
+ 'license_activated',
11062
+ ) );
11063
+ }
11064
+
11065
+ /**
11066
+ * Site plan downgrade.
11067
+ *
11068
+ * @author Vova Feldman (@svovaf)
11069
+ * @since 1.0.4
11070
+ *
11071
+ * @uses FS_Api
11072
+ */
11073
+ private function _downgrade_site() {
11074
+ $this->_logger->entrance();
11075
+
11076
+ $api = $this->get_api_site_scope();
11077
+ $site = $api->call( 'downgrade.json', 'put' );
11078
+
11079
+ $plan_downgraded = false;
11080
+ $plan = false;
11081
+ if ( $this->is_api_result_entity( $site ) ) {
11082
+ $prev_plan_id = $this->_site->plan->id;
11083
+
11084
+ // Update new site plan id.
11085
+ $this->_site->plan->id = $site->plan_id;
11086
+
11087
+ $plan = $this->_enrich_site_plan();
11088
+ $subscription = $this->_sync_site_subscription( $this->_license );
11089
+
11090
+ // Plan downgraded if plan was changed or subscription was cancelled.
11091
+ $plan_downgraded = ( $plan instanceof FS_Plugin_Plan && $prev_plan_id != $plan->id ) ||
11092
+ ( is_object( $subscription ) && ! isset( $subscription->error ) && ! $subscription->is_active() );
11093
+ } else {
11094
+ // handle different error cases.
11095
+
11096
+ }
11097
+
11098
+ if ( $plan_downgraded ) {
11099
+ // Remove previous sticky message about upgrade (if exist).
11100
+ $this->_admin_notices->remove_sticky( 'plan_upgraded' );
11101
+
11102
+ $this->_admin_notices->add(
11103
+ sprintf( $this->get_text( 'plan-x-downgraded-message' ),
11104
+ $plan->title,
11105
+ human_time_diff( time(), strtotime( $this->_license->expiration ) )
11106
+ )
11107
+ );
11108
+
11109
+ // Store site updates.
11110
+ $this->_store_site();
11111
+ } else {
11112
+ $this->_admin_notices->add(
11113
+ $this->get_text( 'plan-downgraded-failure-message' ),
11114
+ $this->get_text( 'oops' ) . '...',
11115
+ 'error'
11116
+ );
11117
+ }
11118
+ }
11119
+
11120
+ /**
11121
+ * @author Vova Feldman (@svovaf)
11122
+ * @since 1.1.8.1
11123
+ *
11124
+ * @param bool|string $plan_name
11125
+ *
11126
+ * @return bool If trial was successfully started.
11127
+ */
11128
+ function start_trial( $plan_name = false ) {
11129
+ $this->_logger->entrance();
11130
+
11131
+ if ( $this->is_trial() ) {
11132
+ // Already in trial mode.
11133
+ $this->_admin_notices->add(
11134
+ sprintf( $this->get_text( 'in-trial-mode' ), $this->_module_type ),
11135
+ $this->get_text( 'oops' ) . '...',
11136
+ 'error'
11137
+ );
11138
+
11139
+ return false;
11140
+ }
11141
+
11142
+ if ( $this->_site->is_trial_utilized() ) {
11143
+ // Trial was already utilized.
11144
+ $this->_admin_notices->add(
11145
+ $this->get_text( 'trial-utilized' ),
11146
+ $this->get_text( 'oops' ) . '...',
11147
+ 'error'
11148
+ );
11149
+
11150
+ return false;
11151
+ }
11152
+
11153
+ if ( false !== $plan_name ) {
11154
+ $plan = $this->get_plan_by_name( $plan_name );
11155
+
11156
+ if ( false === $plan ) {
11157
+ // Plan doesn't exist.
11158
+ $this->_admin_notices->add(
11159
+ sprintf( $this->get_text( 'trial-plan-x-not-exist' ), $plan_name ),
11160
+ $this->get_text( 'oops' ) . '...',
11161
+ 'error'
11162
+ );
11163
+
11164
+ return false;
11165
+ }
11166
+
11167
+ if ( ! $plan->has_trial() ) {
11168
+ // Plan doesn't exist.
11169
+ $this->_admin_notices->add(
11170
+ sprintf( $this->get_text( 'plan-x-no-trial' ), $plan_name ),
11171
+ $this->get_text( 'oops' ) . '...',
11172
+ 'error'
11173
+ );
11174
+
11175
+ return false;
11176
+ }
11177
+ } else {
11178
+ if ( ! $this->has_trial_plan() ) {
11179
+ // None of the plans have a trial.
11180
+ $this->_admin_notices->add(
11181
+ sprintf( $this->get_text( 'no-trials' ), $this->_module_type ),
11182
+ $this->get_text( 'oops' ) . '...',
11183
+ 'error'
11184
+ );
11185
+
11186
+ return false;
11187
+ }
11188
+
11189
+ $plans_with_trial = FS_Plan_Manager::instance()->get_trial_plans( $this->_plans );
11190
+
11191
+ $plan = $plans_with_trial[0];
11192
+ }
11193
+
11194
+ $api = $this->get_api_site_scope();
11195
+ $plan = $api->call( "plans/{$plan->id}/trials.json", 'post' );
11196
+
11197
+ if ( ! $this->is_api_result_entity( $plan ) ) {
11198
+ // Some API error while trying to start the trial.
11199
+ $this->_admin_notices->add(
11200
+ sprintf( $this->get_text( 'unexpected-api-error' ), $this->_module_type )
11201
+ . ' ' . var_export( $plan, true ),
11202
+ $this->get_text( 'oops' ) . '...',
11203
+ 'error'
11204
+ );
11205
+
11206
+ return false;
11207
+ }
11208
+
11209
+ // Sync license.
11210
+ $this->_sync_license();
11211
+
11212
+ return $this->is_trial();
11213
+ }
11214
+
11215
+ /**
11216
+ * Cancel site trial.
11217
+ *
11218
+ * @author Vova Feldman (@svovaf)
11219
+ * @since 1.0.9
11220
+ *
11221
+ * @uses FS_Api
11222
+ */
11223
+ private function _cancel_trial() {
11224
+ $this->_logger->entrance();
11225
+
11226
+ if ( ! $this->is_trial() ) {
11227
+ $this->_admin_notices->add(
11228
+ $this->get_text( 'trial-cancel-no-trial-message' ),
11229
+ $this->get_text( 'oops' ) . '...',
11230
+ 'error'
11231
+ );
11232
+
11233
+ return;
11234
+ }
11235
+
11236
+ $api = $this->get_api_site_scope();
11237
+ $site = $api->call( 'trials.json', 'delete' );
11238
+
11239
+ $trial_cancelled = false;
11240
+
11241
+ if ( $this->is_api_result_entity( $site ) ) {
11242
+ $prev_trial_ends = $this->_site->trial_ends;
11243
+
11244
+ if ( $this->is_paid_trial() ) {
11245
+ $this->_license->expiration = $site->trial_ends;
11246
+ $this->_license->is_cancelled = true;
11247
+ $this->_update_site_license( $this->_license );
11248
+ $this->_store_licenses();
11249
+
11250
+ // Clear subscription reference.
11251
+ $this->_sync_site_subscription( null );
11252
+ }
11253
+
11254
+ // Update site info.
11255
+ $this->_site = new FS_Site( $site );
11256
+ $this->_enrich_site_plan();
11257
+
11258
+ $trial_cancelled = ( $prev_trial_ends != $site->trial_ends );
11259
+ } else {
11260
+ // handle different error cases.
11261
+
11262
+ }
11263
+
11264
+ if ( $trial_cancelled ) {
11265
+ // Remove previous sticky messages about upgrade or trial (if exist).
11266
+ $this->_admin_notices->remove_sticky( array(
11267
+ 'trial_started',
11268
+ 'trial_promotion',
11269
+ 'plan_upgraded',
11270
+ ) );
11271
+
11272
+ // Store site updates.
11273
+ $this->_store_site();
11274
+
11275
+ if ( ! $this->is_addon() ||
11276
+ ! $this->deactivate_premium_only_addon_without_license( true )
11277
+ ) {
11278
+ $this->_admin_notices->add(
11279
+ sprintf( $this->get_text( 'trial-cancel-message' ), $this->_storage->trial_plan->title )
11280
+ );
11281
+ }
11282
+
11283
+ // Clear trial plan information.
11284
+ unset( $this->_storage->trial_plan );
11285
+ } else {
11286
+ $this->_admin_notices->add(
11287
+ $this->get_text( 'trial-cancel-failure-message' ),
11288
+ $this->get_text( 'oops' ) . '...',
11289
+ 'error'
11290
+ );
11291
+ }
11292
+ }
11293
+
11294
+ /**
11295
+ * @author Vova Feldman (@svovaf)
11296
+ * @since 1.0.6
11297
+ *
11298
+ * @param bool|number $plugin_id
11299
+ *
11300
+ * @return bool
11301
+ */
11302
+ private function _is_addon_id( $plugin_id ) {
11303
+ return is_numeric( $plugin_id ) && ( $this->get_id() != $plugin_id );
11304
+ }
11305
+
11306
+ /**
11307
+ * Check if user eligible to download premium version updates.
11308
+ *
11309
+ * @author Vova Feldman (@svovaf)
11310
+ * @since 1.0.6
11311
+ *
11312
+ * @return bool
11313
+ */
11314
+ private function _can_download_premium() {
11315
+ return $this->has_active_valid_license() ||
11316
+ ( $this->is_trial() && ! $this->get_trial_plan()->is_free() );
11317
+ }
11318
+
11319
+ /**
11320
+ *
11321
+ * @author Vova Feldman (@svovaf)
11322
+ * @since 1.0.6
11323
+ *
11324
+ * @param bool|number $addon_id
11325
+ * @param string $type "json" or "zip"
11326
+ *
11327
+ * @return string
11328
+ */
11329
+ private function _get_latest_version_endpoint( $addon_id = false, $type = 'json' ) {
11330
+
11331
+ $is_addon = $this->_is_addon_id( $addon_id );
11332
+
11333
+ $is_premium = null;
11334
+ if ( ! $is_addon ) {
11335
+ $is_premium = $this->_can_download_premium();
11336
+ } else if ( $this->is_addon_activated( $addon_id ) ) {
11337
+ $is_premium = self::get_instance_by_id( $addon_id )->_can_download_premium();
11338
+ }
11339
+
11340
+ // If add-on, then append add-on ID.
11341
+ $endpoint = ( $is_addon ? "/addons/$addon_id" : '' ) .
11342
+ '/updates/latest.' . $type;
11343
+
11344
+ // If add-on and not yet activated, try to fetch based on server licensing.
11345
+ if ( is_bool( $is_premium ) ) {
11346
+ $endpoint = add_query_arg( 'is_premium', json_encode( $is_premium ), $endpoint );
11347
+ }
11348
+
11349
+ if ( $this->has_secret_key() ) {
11350
+ $endpoint = add_query_arg( 'type', 'all', $endpoint );
11351
+ }
11352
+
11353
+ return $endpoint;
11354
+ }
11355
+
11356
+ /**
11357
+ * @author Vova Feldman (@svovaf)
11358
+ * @since 1.0.4
11359
+ *
11360
+ * @param bool|number $addon_id
11361
+ * @param bool $flush Since 1.1.7.3
11362
+ * @param int $expiration Since 1.2.2.7
11363
+ *
11364
+ * @return object|false Plugin latest tag info.
11365
+ */
11366
+ function _fetch_latest_version( $addon_id = false, $flush = true, $expiration = WP_FS__TIME_24_HOURS_IN_SEC ) {
11367
+ $this->_logger->entrance();
11368
+
11369
+ /**
11370
+ * @since 1.1.7.3 Check for plugin updates from Freemius only if opted-in.
11371
+ * @since 1.1.7.4 Also check updates for add-ons.
11372
+ */
11373
+ if ( ! $this->is_registered() &&
11374
+ ! $this->_is_addon_id( $addon_id )
11375
+ ) {
11376
+ return false;
11377
+ }
11378
+
11379
+ $tag = $this->get_api_site_or_plugin_scope()->get(
11380
+ $this->_get_latest_version_endpoint( $addon_id, 'json' ),
11381
+ $flush,
11382
+ $expiration
11383
+ );
11384
+
11385
+ $latest_version = ( is_object( $tag ) && isset( $tag->version ) ) ? $tag->version : 'couldn\'t get';
11386
+
11387
+ $this->_logger->departure( 'Latest version ' . $latest_version );
11388
+
11389
+ return ( is_object( $tag ) && isset( $tag->version ) ) ? $tag : false;
11390
+ }
11391
+
11392
+ #----------------------------------------------------------------------------------
11393
+ #region Download Plugin
11394
+ #----------------------------------------------------------------------------------
11395
+
11396
+ /**
11397
+ * Download latest plugin version, based on plan.
11398
+ *
11399
+ * Not like _download_latest(), this will redirect the page
11400
+ * to secure download url to prevent dual download (from FS to WP server,
11401
+ * and then from WP server to the client / browser).
11402
+ *
11403
+ * @author Vova Feldman (@svovaf)
11404
+ * @since 1.0.9
11405
+ *
11406
+ * @param bool|number $plugin_id
11407
+ *
11408
+ * @uses FS_Api
11409
+ * @uses wp_redirect()
11410
+ */
11411
+ private function download_latest_directly( $plugin_id = false ) {
11412
+ $this->_logger->entrance();
11413
+
11414
+ wp_redirect( $this->get_latest_download_api_url( $plugin_id ) );
11415
+ }
11416
+
11417
+ /**
11418
+ * Get latest plugin FS API download URL.
11419
+ *
11420
+ * @author Vova Feldman (@svovaf)
11421
+ * @since 1.0.9
11422
+ *
11423
+ * @param bool|number $plugin_id
11424
+ *
11425
+ * @return string
11426
+ */
11427
+ private function get_latest_download_api_url( $plugin_id = false ) {
11428
+ $this->_logger->entrance();
11429
+
11430
+ return $this->get_api_site_scope()->get_signed_url(
11431
+ $this->_get_latest_version_endpoint( $plugin_id, 'zip' )
11432
+ );
11433
+ }
11434
+
11435
+ /**
11436
+ * Get payment invoice URL.
11437
+ *
11438
+ * @author Vova Feldman (@svovaf)
11439
+ * @since 1.2.0
11440
+ *
11441
+ * @param bool|number $payment_id
11442
+ *
11443
+ * @return string
11444
+ */
11445
+ function _get_invoice_api_url( $payment_id = false ) {
11446
+ $this->_logger->entrance();
11447
+
11448
+ return $this->get_api_user_scope()->get_signed_url(
11449
+ "/payments/{$payment_id}/invoice.pdf"
11450
+ );
11451
+ }
11452
+
11453
+ /**
11454
+ * Get latest plugin download link.
11455
+ *
11456
+ * @author Vova Feldman (@svovaf)
11457
+ * @since 1.0.9
11458
+ *
11459
+ * @param string $label
11460
+ * @param bool|number $plugin_id
11461
+ *
11462
+ * @return string
11463
+ */
11464
+ private function get_latest_download_link( $label, $plugin_id = false ) {
11465
+ return sprintf(
11466
+ '<a target="_blank" href="%s">%s</a>',
11467
+ $this->_get_latest_download_local_url( $plugin_id ),
11468
+ $label
11469
+ );
11470
+ }
11471
+
11472
+ /**
11473
+ * Get latest plugin download local URL.
11474
+ *
11475
+ * @author Vova Feldman (@svovaf)
11476
+ * @since 1.0.9
11477
+ *
11478
+ * @param bool|number $plugin_id
11479
+ *
11480
+ * @return string
11481
+ */
11482
+ function _get_latest_download_local_url( $plugin_id = false ) {
11483
+ // Add timestamp to protect from caching.
11484
+ $params = array( 'ts' => WP_FS__SCRIPT_START_TIME );
11485
+
11486
+ if ( ! empty( $plugin_id ) ) {
11487
+ $params['plugin_id'] = $plugin_id;
11488
+ }
11489
+
11490
+ return $this->get_account_url( 'download_latest', $params );
11491
+ }
11492
+
11493
+ #endregion Download Plugin ------------------------------------------------------------------
11494
+
11495
+ /**
11496
+ * @author Vova Feldman (@svovaf)
11497
+ * @since 1.0.4
11498
+ *
11499
+ * @uses FS_Api
11500
+ *
11501
+ * @param bool $background Hints the method if it's a background updates check. If false, it means that
11502
+ * was initiated by the admin.
11503
+ * @param bool|number $plugin_id
11504
+ * @param bool $flush Since 1.1.7.3
11505
+ * @param int $expiration Since 1.2.2.7
11506
+ */
11507
+ private function check_updates(
11508
+ $background = false,
11509
+ $plugin_id = false,
11510
+ $flush = true,
11511
+ $expiration = WP_FS__TIME_24_HOURS_IN_SEC
11512
+ ) {
11513
+ $this->_logger->entrance();
11514
+
11515
+ // Check if there's a newer version for download.
11516
+ $new_version = $this->_fetch_newer_version( $plugin_id, $flush, $expiration );
11517
+
11518
+ $update = null;
11519
+ if ( is_object( $new_version ) ) {
11520
+ $update = new FS_Plugin_Tag( $new_version );
11521
+
11522
+ if ( ! $background ) {
11523
+ $this->_admin_notices->add(
11524
+ sprintf(
11525
+ $this->get_text( 'version-x-released' ) . ' ' . $this->get_text( 'please-download-x' ),
11526
+ $update->version,
11527
+ sprintf(
11528
+ '<a href="%s" target="_blank">%s</a>',
11529
+ $this->get_account_url( 'download_latest' ),
11530
+ sprintf( $this->get_text( 'latest-x-version' ), $this->_site->plan->title )
11531
+ )
11532
+ ),
11533
+ $this->get_text( 'new' ) . '!'
11534
+ );
11535
+ }
11536
+ } else if ( false === $new_version && ! $background ) {
11537
+ $this->_admin_notices->add(
11538
+ $this->get_text( 'you-have-latest' ),
11539
+ $this->get_text( 'you-are-good' )
11540
+ );
11541
+ }
11542
+
11543
+ $this->_store_update( $update, true, $plugin_id );
11544
+ }
11545
+
11546
+ /**
11547
+ * @author Vova Feldman (@svovaf)
11548
+ * @since 1.0.4
11549
+ *
11550
+ * @param bool $flush Since 1.1.7.3 add 24 hour cache by default.
11551
+ *
11552
+ * @return FS_Plugin[]
11553
+ *
11554
+ * @uses FS_Api
11555
+ */
11556
+ private function sync_addons( $flush = false ) {
11557
+ $this->_logger->entrance();
11558
+
11559
+ $api = $this->get_api_site_or_plugin_scope();
11560
+
11561
+ /**
11562
+ * @since 1.2.1
11563
+ *
11564
+ * If there's a cached version of the add-ons and not asking
11565
+ * for a flush, just use the currently stored add-ons.
11566
+ */
11567
+ if ( ! $flush && $api->is_cached( '/addons.json?enriched=true' ) ) {
11568
+ $addons = self::get_all_addons();
11569
+
11570
+ return $addons[ $this->_plugin->id ];
11571
+ }
11572
+
11573
+ $result = $api->get( '/addons.json?enriched=true', $flush );
11574
+
11575
+ $addons = array();
11576
+ if ( $this->is_api_result_object( $result, 'plugins' ) &&
11577
+ is_array( $result->plugins )
11578
+ ) {
11579
+ for ( $i = 0, $len = count( $result->plugins ); $i < $len; $i ++ ) {
11580
+ $addons[ $i ] = new FS_Plugin( $result->plugins[ $i ] );
11581
+ }
11582
+
11583
+ $this->_store_addons( $addons, true );
11584
+ }
11585
+
11586
+ return $addons;
11587
+ }
11588
+
11589
+ /**
11590
+ * Handle user email update.
11591
+ *
11592
+ * @author Vova Feldman (@svovaf)
11593
+ * @since 1.0.3
11594
+ * @uses FS_Api
11595
+ *
11596
+ * @param string $new_email
11597
+ *
11598
+ * @return object
11599
+ */
11600
+ private function update_email( $new_email ) {
11601
+ $this->_logger->entrance();
11602
+
11603
+
11604
+ $api = $this->get_api_user_scope();
11605
+ $user = $api->call( "?plugin_id={$this->_plugin->id}&fields=id,email,is_verified", 'put', array(
11606
+ 'email' => $new_email,
11607
+ 'after_email_confirm_url' => $this->_get_admin_page_url(
11608
+ 'account',
11609
+ array( 'fs_action' => 'sync_user' )
11610
+ ),
11611
+ ) );
11612
+
11613
+ if ( ! isset( $user->error ) ) {
11614
+ $this->_user->email = $user->email;
11615
+ $this->_user->is_verified = $user->is_verified;
11616
+ $this->_store_user();
11617
+ } else {
11618
+ // handle different error cases.
11619
+
11620
+ }
11621
+
11622
+ return $user;
11623
+ }
11624
+
11625
+ #----------------------------------------------------------------------------------
11626
+ #region API Error Handling
11627
+ #----------------------------------------------------------------------------------
11628
+
11629
+ /**
11630
+ * @author Vova Feldman (@svovaf)
11631
+ * @since 1.1.1
11632
+ *
11633
+ * @param mixed $result
11634
+ *
11635
+ * @return bool Is API result contains an error.
11636
+ */
11637
+ private function is_api_error( $result ) {
11638
+ return FS_Api::is_api_error( $result );
11639
+ }
11640
+
11641
+ /**
11642
+ * Checks if given API result is a non-empty and not an error object.
11643
+ *
11644
+ * @author Vova Feldman (@svovaf)
11645
+ * @since 1.2.1.5
11646
+ *
11647
+ * @param mixed $result
11648
+ * @param string|null $required_property Optional property we want to verify that is set.
11649
+ *
11650
+ * @return bool
11651
+ */
11652
+ function is_api_result_object( $result, $required_property = null ) {
11653
+ return FS_Api::is_api_result_object( $result, $required_property );
11654
+ }
11655
+
11656
+ /**
11657
+ * Checks if given API result is a non-empty entity object with non-empty ID.
11658
+ *
11659
+ * @author Vova Feldman (@svovaf)
11660
+ * @since 1.2.1.5
11661
+ *
11662
+ * @param mixed $result
11663
+ *
11664
+ * @return bool
11665
+ */
11666
+ private function is_api_result_entity( $result ) {
11667
+ return FS_Api::is_api_result_entity( $result );
11668
+ }
11669
+
11670
+ #endregion
11671
+
11672
+ /**
11673
+ * Make sure a given argument is an array of a specific type.
11674
+ *
11675
+ * @author Vova Feldman (@svovaf)
11676
+ * @since 1.2.1.5
11677
+ *
11678
+ * @param mixed $array
11679
+ * @param string $class
11680
+ *
11681
+ * @return bool
11682
+ */
11683
+ private function is_array_instanceof( $array, $class ) {
11684
+ return ( is_array( $array ) && ( empty( $array ) || $array[0] instanceof $class ) );
11685
+ }
11686
+
11687
+ /**
11688
+ * Start install ownership change.
11689
+ *
11690
+ * @author Vova Feldman (@svovaf)
11691
+ * @since 1.1.1
11692
+ * @uses FS_Api
11693
+ *
11694
+ * @param string $candidate_email
11695
+ *
11696
+ * @return bool Is ownership change successfully initiated.
11697
+ */
11698
+ private function init_change_owner( $candidate_email ) {
11699
+ $this->_logger->entrance();
11700
+
11701
+ $api = $this->get_api_site_scope();
11702
+ $result = $api->call( "/users/{$this->_user->id}.json", 'put', array(
11703
+ 'email' => $candidate_email,
11704
+ 'after_confirm_url' => $this->_get_admin_page_url(
11705
+ 'account',
11706
+ array( 'fs_action' => 'change_owner' )
11707
+ ),
11708
+ ) );
11709
+
11710
+ return ! $this->is_api_error( $result );
11711
+ }
11712
+
11713
+ /**
11714
+ * Handle install ownership change.
11715
+ *
11716
+ * @author Vova Feldman (@svovaf)
11717
+ * @since 1.1.1
11718
+ * @uses FS_Api
11719
+ *
11720
+ * @return bool Was ownership change successfully complete.
11721
+ */
11722
+ private function complete_change_owner() {
11723
+ $this->_logger->entrance();
11724
+
11725
+ $site_result = $this->get_api_site_scope( true )->get();
11726
+ $site = new FS_Site( $site_result );
11727
+ $this->_site = $site;
11728
+
11729
+ $user = new FS_User();
11730
+ $user->id = fs_request_get( 'user_id' );
11731
+
11732
+ // Validate install's user and given user.
11733
+ if ( $user->id != $this->_site->user_id ) {
11734
+ return false;
11735
+ }
11736
+
11737
+ $user->public_key = fs_request_get( 'user_public_key' );
11738
+ $user->secret_key = fs_request_get( 'user_secret_key' );
11739
+
11740
+ // Fetch new user information.
11741
+ $this->_user = $user;
11742
+ $user_result = $this->get_api_user_scope( true )->get();
11743
+ $user = new FS_User( $user_result );
11744
+ $this->_user = $user;
11745
+
11746
+ $this->_set_account( $user, $site );
11747
+
11748
+ return true;
11749
+ }
11750
+
11751
+ /**
11752
+ * Handle user name update.
11753
+ *
11754
+ * @author Vova Feldman (@svovaf)
11755
+ * @since 1.0.9
11756
+ * @uses FS_Api
11757
+ *
11758
+ * @return object
11759
+ */
11760
+ private function update_user_name() {
11761
+ $this->_logger->entrance();
11762
+ $name = fs_request_get( 'fs_user_name_' . $this->get_unique_affix(), '' );
11763
+
11764
+ $api = $this->get_api_user_scope();
11765
+ $user = $api->call( "?plugin_id={$this->_plugin->id}&fields=id,first,last", 'put', array(
11766
+ 'name' => $name,
11767
+ ) );
11768
+
11769
+ if ( ! isset( $user->error ) ) {
11770
+ $this->_user->first = $user->first;
11771
+ $this->_user->last = $user->last;
11772
+ $this->_store_user();
11773
+ } else {
11774
+ // handle different error cases.
11775
+
11776
+ }
11777
+
11778
+ return $user;
11779
+ }
11780
+
11781
+ /**
11782
+ * Verify user email.
11783
+ *
11784
+ * @author Vova Feldman (@svovaf)
11785
+ * @since 1.0.3
11786
+ * @uses FS_Api
11787
+ */
11788
+ private function verify_email() {
11789
+ $this->_handle_account_user_sync();
11790
+
11791
+ if ( $this->_user->is_verified() ) {
11792
+ return;
11793
+ }
11794
+
11795
+ $api = $this->get_api_site_scope();
11796
+ $result = $api->call( "/users/{$this->_user->id}/verify.json", 'put', array(
11797
+ 'after_email_confirm_url' => $this->_get_admin_page_url(
11798
+ 'account',
11799
+ array( 'fs_action' => 'sync_user' )
11800
+ )
11801
+ ) );
11802
+
11803
+ if ( ! isset( $result->error ) ) {
11804
+ $this->_admin_notices->add( sprintf(
11805
+ $this->get_text( 'verification-email-sent-message' ),
11806
+ sprintf( '<a href="mailto:%1s">%2s</a>', esc_url( $this->_user->email ), $this->_user->email )
11807
+ ) );
11808
+ } else {
11809
+ // handle different error cases.
11810
+
11811
+ }
11812
+ }
11813
+
11814
+ /**
11815
+ * @author Vova Feldman (@svovaf)
11816
+ * @since 1.1.2
11817
+ *
11818
+ * @param array $params
11819
+ *
11820
+ * @return string
11821
+ */
11822
+ function get_activation_url( $params = array() ) {
11823
+ if ( $this->is_addon() && $this->has_free_plan() ) {
11824
+ /**
11825
+ * @author Vova Feldman (@svovaf)
11826
+ * @since 1.2.1.7 Add-on's activation is the parent's module activation.
11827
+ */
11828
+ return $this->get_parent_instance()->get_activation_url( $params );
11829
+ }
11830
+
11831
+ return $this->apply_filters( 'connect_url', $this->_get_admin_page_url( '', $params ) );
11832
+ }
11833
+
11834
+ /**
11835
+ * @author Vova Feldman (@svovaf)
11836
+ * @since 1.2.1.5
11837
+ *
11838
+ * @param array $params
11839
+ *
11840
+ * @return string
11841
+ */
11842
+ function get_reconnect_url( $params = array() ) {
11843
+ $params['fs_action'] = 'reset_anonymous_mode';
11844
+ $params['fs_unique_affix'] = $this->get_unique_affix();
11845
+
11846
+ return $this->get_activation_url( $params );
11847
+ }
11848
+
11849
+ /**
11850
+ * Get the URL of the page that should be loaded after the user connect
11851
+ * or skip in the opt-in screen.
11852
+ *
11853
+ * @author Vova Feldman (@svovaf)
11854
+ * @since 1.1.3
11855
+ *
11856
+ * @param string $filter Filter name.
11857
+ * @param array $params Since 1.2.2.7
11858
+ *
11859
+ * @return string
11860
+ */
11861
+ function get_after_activation_url( $filter, $params = array() ) {
11862
+ if ( $this->is_free_wp_org_theme() &&
11863
+ fs_request_has( 'pending_activation' )
11864
+ ) {
11865
+ $first_time_path = '';
11866
+ } else {
11867
+ $first_time_path = $this->_menu->get_first_time_path();
11868
+ }
11869
+
11870
+ return add_query_arg( $params, $this->apply_filters(
11871
+ $filter,
11872
+ empty( $first_time_path ) ?
11873
+ $this->_get_admin_page_url() :
11874
+ $first_time_path
11875
+ ) );
11876
+ }
11877
+
11878
+ /**
11879
+ * Handle account page updates / edits / actions.
11880
+ *
11881
+ * @author Vova Feldman (@svovaf)
11882
+ * @since 1.0.2
11883
+ *
11884
+ */
11885
+ private function _handle_account_edits() {
11886
+ if ( ! $this->is_user_admin() ) {
11887
+ return;
11888
+ }
11889
+
11890
+ $plugin_id = fs_request_get( 'plugin_id', $this->get_id() );
11891
+ $action = fs_get_action();
11892
+
11893
+ switch ( $action ) {
11894
+ case 'delete_account':
11895
+ check_admin_referer( $action );
11896
+
11897
+ if ( $plugin_id == $this->get_id() ) {
11898
+ $this->delete_account_event();
11899
+
11900
+ // Clear user and site.
11901
+ $this->_site = null;
11902
+ $this->_user = null;
11903
+
11904
+ fs_redirect( $this->get_activation_url() );
11905
+ } else {
11906
+ if ( $this->is_addon_activated( $plugin_id ) ) {
11907
+ $fs_addon = self::get_instance_by_id( $plugin_id );
11908
+ $fs_addon->delete_account_event();
11909
+
11910
+ fs_redirect( $this->_get_admin_page_url( 'account' ) );
11911
+ }
11912
+ }
11913
+
11914
+ return;
11915
+
11916
+ case 'downgrade_account':
11917
+ check_admin_referer( $action );
11918
+
11919
+ if ( $plugin_id == $this->get_id() ) {
11920
+ $this->_downgrade_site();
11921
+ } else if ( $this->is_addon_activated( $plugin_id ) ) {
11922
+ $fs_addon = self::get_instance_by_id( $plugin_id );
11923
+ $fs_addon->_downgrade_site();
11924
+ }
11925
+
11926
+ return;
11927
+
11928
+ case 'activate_license':
11929
+ check_admin_referer( $action );
11930
+
11931
+ if ( $plugin_id == $this->get_id() ) {
11932
+ $this->_activate_license();
11933
+ } else {
11934
+ if ( $this->is_addon_activated( $plugin_id ) ) {
11935
+ $fs_addon = self::get_instance_by_id( $plugin_id );
11936
+ $fs_addon->_activate_license();
11937
+ }
11938
+ }
11939
+
11940
+ return;
11941
+
11942
+ case 'deactivate_license':
11943
+ check_admin_referer( $action );
11944
+
11945
+ if ( $plugin_id == $this->get_id() ) {
11946
+ $this->_deactivate_license();
11947
+ } else {
11948
+ if ( $this->is_addon_activated( $plugin_id ) ) {
11949
+ $fs_addon = self::get_instance_by_id( $plugin_id );
11950
+ $fs_addon->_deactivate_license();
11951
+ }
11952
+ }
11953
+
11954
+ return;
11955
+
11956
+ case 'check_updates':
11957
+ check_admin_referer( $action );
11958
+ $this->check_updates();
11959
+
11960
+ return;
11961
+
11962
+ case 'change_owner':
11963
+ $state = fs_request_get( 'state', 'init' );
11964
+ switch ( $state ) {
11965
+ case 'init':
11966
+ $candidate_email = fs_request_get( 'candidate_email', '' );
11967
+
11968
+ if ( $this->init_change_owner( $candidate_email ) ) {
11969
+ $this->_admin_notices->add( sprintf( $this->get_text( 'change-owner-request-sent-x' ), '<b>' . $this->_user->email . '</b>' ) );
11970
+ }
11971
+ break;
11972
+ case 'owner_confirmed':
11973
+ $candidate_email = fs_request_get( 'candidate_email', '' );
11974
+
11975
+ $this->_admin_notices->add( sprintf( $this->get_text( 'change-owner-request_owner-confirmed' ), '<b>' . $candidate_email . '</b>' ) );
11976
+ break;
11977
+ case 'candidate_confirmed':
11978
+ if ( $this->complete_change_owner() ) {
11979
+ $this->_admin_notices->add_sticky(
11980
+ sprintf( $this->get_text( 'change-owner-request_candidate-confirmed' ), '<b>' . $this->_user->email . '</b>' ),
11981
+ 'ownership_changed',
11982
+ $this->get_text( 'congrats' ) . '!'
11983
+ );
11984
+ } else {
11985
+ // @todo Handle failed ownership change message.
11986
+ }
11987
+ break;
11988
+ }
11989
+
11990
+ return;
11991
+
11992
+ case 'update_email':
11993
+ check_admin_referer( 'update_email' );
11994
+
11995
+ $new_email = fs_request_get( 'fs_email_' . $this->get_unique_affix(), '' );
11996
+ $result = $this->update_email( $new_email );
11997
+
11998
+ if ( isset( $result->error ) ) {
11999
+ switch ( $result->error->code ) {
12000
+ case 'user_exist':
12001
+ $this->_admin_notices->add(
12002
+ $this->get_text( 'user-exist-message' ) . ' ' .
12003
+ sprintf( $this->get_text( 'user-exist-message_ownership' ), $this->_module_type, '<b>' . $new_email . '</b>' ) .
12004
+ sprintf(
12005
+ '<a style="margin-left: 10px;" href="%s"><button class="button button-primary">%s &nbsp;&#10140;</button></a>',
12006
+ $this->get_account_url( 'change_owner', array(
12007
+ 'state' => 'init',
12008
+ 'candidate_email' => $new_email
12009
+ ) ),
12010
+ $this->get_text( 'change-ownership' )
12011
+ ),
12012
+ $this->get_text( 'oops' ) . '...',
12013
+ 'error'
12014
+ );
12015
+ break;
12016
+ }
12017
+ } else {
12018
+ $this->_admin_notices->add( $this->get_text( 'email-updated-message' ) );
12019
+ }
12020
+
12021
+ return;
12022
+
12023
+ case 'update_user_name':
12024
+ check_admin_referer( 'update_user_name' );
12025
+
12026
+ $result = $this->update_user_name();
12027
+
12028
+ if ( isset( $result->error ) ) {
12029
+ $this->_admin_notices->add(
12030
+ $this->get_text( 'name-update-failed-message' ),
12031
+ $this->get_text( 'oops' ) . '...',
12032
+ 'error'
12033
+ );
12034
+ } else {
12035
+ $this->_admin_notices->add( $this->get_text( 'name-updated-message' ) );
12036
+ }
12037
+
12038
+ return;
12039
+
12040
+ #region Actions that might be called from external links (e.g. email)
12041
+
12042
+ case 'cancel_trial':
12043
+ if ( $plugin_id == $this->get_id() ) {
12044
+ $this->_cancel_trial();
12045
+ } else {
12046
+ if ( $this->is_addon_activated( $plugin_id ) ) {
12047
+ $fs_addon = self::get_instance_by_id( $plugin_id );
12048
+ $fs_addon->_cancel_trial();
12049
+ }
12050
+ }
12051
+
12052
+ return;
12053
+
12054
+ case 'verify_email':
12055
+ $this->verify_email();
12056
+
12057
+ return;
12058
+
12059
+ case 'sync_user':
12060
+ $this->_handle_account_user_sync();
12061
+
12062
+ return;
12063
+
12064
+ case $this->get_unique_affix() . '_sync_license':
12065
+ $this->_sync_license();
12066
+
12067
+ return;
12068
+
12069
+ case 'download_latest':
12070
+ $this->download_latest_directly( $plugin_id );
12071
+
12072
+ return;
12073
+
12074
+ #endregion
12075
+ }
12076
+
12077
+ if ( WP_FS__IS_POST_REQUEST ) {
12078
+ $properties = array( 'site_secret_key', 'site_id', 'site_public_key' );
12079
+ foreach ( $properties as $p ) {
12080
+ if ( 'update_' . $p === $action ) {
12081
+ check_admin_referer( $action );
12082
+
12083
+ $this->_logger->log( $action );
12084
+
12085
+ $site_property = substr( $p, strlen( 'site_' ) );
12086
+ $site_property_value = fs_request_get( 'fs_' . $p . '_' . $this->get_unique_affix(), '' );
12087
+ $this->get_site()->{$site_property} = $site_property_value;
12088
+
12089
+ // Store account after modification.
12090
+ $this->_store_site();
12091
+
12092
+ $this->do_action( 'account_property_edit', 'site', $site_property, $site_property_value );
12093
+
12094
+ $this->_admin_notices->add( sprintf(
12095
+ $this->get_text( 'x-updated' ),
12096
+ '<b>' . str_replace( '_', ' ', $p ) . '</b>' ) );
12097
+
12098
+ return;
12099
+ }
12100
+ }
12101
+ }
12102
+ }
12103
+
12104
+ /**
12105
+ * Account page resources load.
12106
+ *
12107
+ * @author Vova Feldman (@svovaf)
12108
+ * @since 1.0.6
12109
+ */
12110
+ function _account_page_load() {
12111
+ $this->_logger->entrance();
12112
+
12113
+ $this->_logger->info( var_export( $_REQUEST, true ) );
12114
+
12115
+ fs_enqueue_local_style( 'fs_account', '/admin/account.css' );
12116
+
12117
+ if ( $this->has_addons() ) {
12118
+ wp_enqueue_script( 'plugin-install' );
12119
+ add_thickbox();
12120
+
12121
+ function fs_addons_body_class( $classes ) {
12122
+ $classes .= ' plugins-php';
12123
+
12124
+ return $classes;
12125
+ }
12126
+
12127
+ add_filter( 'admin_body_class', 'fs_addons_body_class' );
12128
+ }
12129
+
12130
+ if ( $this->has_paid_plan() &&
12131
+ ! $this->has_any_license() &&
12132
+ ! $this->is_sync_executed() &&
12133
+ $this->is_tracking_allowed()
12134
+ ) {
12135
+ /**
12136
+ * If no licenses found and no sync job was executed during the last 24 hours,
12137
+ * just execute the sync job right away (blocking execution).
12138
+ *
12139
+ * @since 1.1.7.3
12140
+ */
12141
+ $this->run_manual_sync();
12142
+ }
12143
+
12144
+ $this->_handle_account_edits();
12145
+
12146
+ $this->do_action( 'account_page_load_before_departure' );
12147
+ }
12148
+
12149
+ /**
12150
+ * Render account page.
12151
+ *
12152
+ * @author Vova Feldman (@svovaf)
12153
+ * @since 1.0.0
12154
+ */
12155
+ function _account_page_render() {
12156
+ $this->_logger->entrance();
12157
+
12158
+ $template = 'account.php';
12159
+ if ( 'billing' === fs_request_get( 'tab' ) ) {
12160
+ $template = 'billing.php';
12161
+ }
12162
+
12163
+ $vars = array( 'id' => $this->_module_id );
12164
+
12165
+ /**
12166
+ * Added filter to the template to allow developers wrapping the template
12167
+ * in custom HTML (e.g. within a wizard/tabs).
12168
+ *
12169
+ * @author Vova Feldman (@svovaf)
12170
+ * @since 1.2.1.6
12171
+ */
12172
+ echo $this->apply_filters( "templates/{$template}", fs_get_template( $template, $vars ) );
12173
+ }
12174
+
12175
+ /**
12176
+ * Render account connect page.
12177
+ *
12178
+ * @author Vova Feldman (@svovaf)
12179
+ * @since 1.0.7
12180
+ */
12181
+ function _connect_page_render() {
12182
+ $this->_logger->entrance();
12183
+
12184
+ $vars = array( 'id' => $this->_module_id );
12185
+
12186
+ /**
12187
+ * Added filter to the template to allow developers wrapping the template
12188
+ * in custom HTML (e.g. within a wizard/tabs).
12189
+ *
12190
+ * @author Vova Feldman (@svovaf)
12191
+ * @since 1.2.1.6
12192
+ */
12193
+ echo $this->apply_filters( 'templates/connect.php', fs_get_template( 'connect.php', $vars ) );
12194
+ }
12195
+
12196
+ /**
12197
+ * Load required resources before add-ons page render.
12198
+ *
12199
+ * @author Vova Feldman (@svovaf)
12200
+ * @since 1.0.6
12201
+ */
12202
+ function _addons_page_load() {
12203
+ $this->_logger->entrance();
12204
+
12205
+ fs_enqueue_local_style( 'fs_addons', '/admin/add-ons.css' );
12206
+
12207
+ wp_enqueue_script( 'plugin-install' );
12208
+ add_thickbox();
12209
+
12210
+ function fs_addons_body_class( $classes ) {
12211
+ $classes .= ' plugins-php';
12212
+
12213
+ return $classes;
12214
+ }
12215
+
12216
+ add_filter( 'admin_body_class', 'fs_addons_body_class' );
12217
+
12218
+ if ( ! $this->is_registered() && $this->is_org_repo_compliant() ) {
12219
+ $this->_admin_notices->add(
12220
+ sprintf( $this->get_text( 'addons-info-external-message' ), '<b>' . $this->get_plugin_name() . '</b>' ),
12221
+ $this->get_text( 'heads-up' ),
12222
+ 'update-nag'
12223
+ );
12224
+ }
12225
+ }
12226
+
12227
+ /**
12228
+ * Render add-ons page.
12229
+ *
12230
+ * @author Vova Feldman (@svovaf)
12231
+ * @since 1.0.6
12232
+ */
12233
+ function _addons_page_render() {
12234
+ $this->_logger->entrance();
12235
+
12236
+ $vars = array( 'id' => $this->_module_id );
12237
+
12238
+ /**
12239
+ * Added filter to the template to allow developers wrapping the template
12240
+ * in custom HTML (e.g. within a wizard/tabs).
12241
+ *
12242
+ * @author Vova Feldman (@svovaf)
12243
+ * @since 1.2.1.6
12244
+ */
12245
+ echo $this->apply_filters( 'templates/add-ons.php', fs_get_template( 'add-ons.php', $vars ) );
12246
+ }
12247
+
12248
+ /* Pricing & Upgrade
12249
+ ------------------------------------------------------------------------------------------------------------------*/
12250
+ /**
12251
+ * Render pricing page.
12252
+ *
12253
+ * @author Vova Feldman (@svovaf)
12254
+ * @since 1.0.0
12255
+ */
12256
+ function _pricing_page_render() {
12257
+ $this->_logger->entrance();
12258
+
12259
+ $vars = array( 'id' => $this->_module_id );
12260
+
12261
+ if ( 'true' === fs_request_get( 'checkout', false ) ) {
12262
+ fs_require_once_template( 'checkout.php', $vars );
12263
+ } else {
12264
+ fs_require_once_template( 'pricing.php', $vars );
12265
+ }
12266
+ }
12267
+
12268
+ #----------------------------------------------------------------------------------
12269
+ #region Contact Us
12270
+ #----------------------------------------------------------------------------------
12271
+
12272
+ /**
12273
+ * Render contact-us page.
12274
+ *
12275
+ * @author Vova Feldman (@svovaf)
12276
+ * @since 1.0.3
12277
+ */
12278
+ function _contact_page_render() {
12279
+ $this->_logger->entrance();
12280
+
12281
+ $vars = array( 'id' => $this->_module_id );
12282
+ fs_require_once_template( 'contact.php', $vars );
12283
+ }
12284
+
12285
+ #endregion ------------------------------------------------------------------------
12286
+
12287
+ /**
12288
+ * Hide all admin notices to prevent distractions.
12289
+ *
12290
+ * @author Vova Feldman (@svovaf)
12291
+ * @since 1.0.3
12292
+ *
12293
+ * @uses remove_all_actions()
12294
+ */
12295
+ private static function _hide_admin_notices() {
12296
+ remove_all_actions( 'admin_notices' );
12297
+ remove_all_actions( 'network_admin_notices' );
12298
+ remove_all_actions( 'all_admin_notices' );
12299
+ remove_all_actions( 'user_admin_notices' );
12300
+ }
12301
+
12302
+ static function _clean_admin_content_section_hook() {
12303
+ self::_hide_admin_notices();
12304
+
12305
+ // Hide footer.
12306
+ echo '<style>#wpfooter { display: none !important; }</style>';
12307
+ }
12308
+
12309
+ /**
12310
+ * Attach to admin_head hook to hide all admin notices.
12311
+ *
12312
+ * @author Vova Feldman (@svovaf)
12313
+ * @since 1.0.3
12314
+ */
12315
+ static function _clean_admin_content_section() {
12316
+ add_action( 'admin_head', 'Freemius::_clean_admin_content_section_hook' );
12317
+ }
12318
+
12319
+ /* CSS & JavaScript
12320
+ ------------------------------------------------------------------------------------------------------------------*/
12321
+ /* function _enqueue_script($handle, $src) {
12322
+ $url = plugins_url( substr( WP_FS__DIR_JS, strlen( $this->_plugin_dir_path ) ) . '/assets/js/' . $src );
12323
+
12324
+ $this->_logger->entrance( 'script = ' . $url );
12325
+
12326
+ wp_enqueue_script( $handle, $url );
12327
+ }*/
12328
+
12329
+ /* SDK
12330
+ ------------------------------------------------------------------------------------------------------------------*/
12331
+ private $_user_api;
12332
+
12333
+ /**
12334
+ *
12335
+ * @author Vova Feldman (@svovaf)
12336
+ * @since 1.0.2
12337
+ *
12338
+ * @param bool $flush
12339
+ *
12340
+ * @return FS_Api
12341
+ */
12342
+ function get_api_user_scope( $flush = false ) {
12343
+ if ( ! isset( $this->_user_api ) || $flush ) {
12344
+ $this->_user_api = FS_Api::instance(
12345
+ $this->_module_id,
12346
+ 'user',
12347
+ $this->_user->id,
12348
+ $this->_user->public_key,
12349
+ ! $this->is_live(),
12350
+ $this->_user->secret_key
12351
+ );
12352
+ }
12353
+
12354
+ return $this->_user_api;
12355
+ }
12356
+
12357
+ private $_site_api;
12358
+
12359
+ /**
12360
+ *
12361
+ * @author Vova Feldman (@svovaf)
12362
+ * @since 1.0.2
12363
+ *
12364
+ * @param bool $flush
12365
+ *
12366
+ * @return FS_Api
12367
+ */
12368
+ function get_api_site_scope( $flush = false ) {
12369
+ if ( ! isset( $this->_site_api ) || $flush ) {
12370
+ $this->_site_api = FS_Api::instance(
12371
+ $this->_module_id,
12372
+ 'install',
12373
+ $this->_site->id,
12374
+ $this->_site->public_key,
12375
+ ! $this->is_live(),
12376
+ $this->_site->secret_key
12377
+ );
12378
+ }
12379
+
12380
+ return $this->_site_api;
12381
+ }
12382
+
12383
+ private $_plugin_api;
12384
+
12385
+ /**
12386
+ * Get plugin public API scope.
12387
+ *
12388
+ * @author Vova Feldman (@svovaf)
12389
+ * @since 1.0.7
12390
+ *
12391
+ * @return FS_Api
12392
+ */
12393
+ function get_api_plugin_scope() {
12394
+ if ( ! isset( $this->_plugin_api ) ) {
12395
+ $this->_plugin_api = FS_Api::instance(
12396
+ $this->_module_id,
12397
+ 'plugin',
12398
+ $this->_plugin->id,
12399
+ $this->_plugin->public_key,
12400
+ ! $this->is_live()
12401
+ );
12402
+ }
12403
+
12404
+ return $this->_plugin_api;
12405
+ }
12406
+
12407
+ /**
12408
+ * Get site API scope object (fallback to public plugin scope when not registered).
12409
+ *
12410
+ * @author Vova Feldman (@svovaf)
12411
+ * @since 1.0.7
12412
+ *
12413
+ * @return FS_Api
12414
+ */
12415
+ function get_api_site_or_plugin_scope() {
12416
+ return $this->is_registered() ?
12417
+ $this->get_api_site_scope() :
12418
+ $this->get_api_plugin_scope();
12419
+ }
12420
+
12421
+ /**
12422
+ * Show trial promotional notice (if any trial exist).
12423
+ *
12424
+ * @author Vova Feldman (@svovaf)
12425
+ * @since 1.0.9
12426
+ *
12427
+ * @param $plans
12428
+ */
12429
+ function _check_for_trial_plans( $plans ) {
12430
+ $this->_storage->has_trial_plan = FS_Plan_Manager::instance()->has_trial_plan( $plans );
12431
+ }
12432
+
12433
+ /**
12434
+ * During trial promotion the "upgrade" submenu item turns to
12435
+ * "start trial" to encourage the trial. Since we want to keep
12436
+ * the same menu item handler and there's no robust way to
12437
+ * add new arguments to the menu item link's querystring,
12438
+ * use JavaScript to find the menu item and update the href of
12439
+ * the link.
12440
+ *
12441
+ * @author Vova Feldman (@svovaf)
12442
+ * @since 1.2.1.5
12443
+ */
12444
+ function _fix_start_trial_menu_item_url() {
12445
+ $template_args = array( 'id' => $this->_module_id );
12446
+ fs_require_template( 'add-trial-to-pricing.php', $template_args );
12447
+ }
12448
+
12449
+ /**
12450
+ * Check if module is currently in a trial promotion mode.
12451
+ *
12452
+ * @author Vova Feldman (@svovaf)
12453
+ * @since 1.2.2.7
12454
+ *
12455
+ * @return bool
12456
+ */
12457
+ function is_in_trial_promotion() {
12458
+ return $this->_admin_notices->has_sticky( 'trial_promotion' );
12459
+ }
12460
+
12461
+ /**
12462
+ * Show trial promotional notice (if any trial exist).
12463
+ *
12464
+ * @author Vova Feldman (@svovaf)
12465
+ * @since 1.0.9
12466
+ *
12467
+ * @return bool If trial notice added.
12468
+ */
12469
+ function _add_trial_notice() {
12470
+ if ( ! $this->is_user_admin() ) {
12471
+ return false;
12472
+ }
12473
+
12474
+ if ( ! $this->is_user_in_admin() ) {
12475
+ return false;
12476
+ }
12477
+
12478
+ // Check if trial message is already shown.
12479
+ if ( $this->is_in_trial_promotion() ) {
12480
+ add_action( 'admin_footer', array( &$this, '_fix_start_trial_menu_item_url' ) );
12481
+
12482
+ $this->_menu->add_counter_to_menu_item( 1, 'fs-trial' );
12483
+
12484
+ return false;
12485
+ }
12486
+
12487
+ if ( $this->is_premium() && ! WP_FS__DEV_MODE ) {
12488
+ // Don't show trial if running the premium code, unless running in DEV mode.
12489
+ return false;
12490
+ }
12491
+
12492
+ if ( ! $this->has_trial_plan() ) {
12493
+ // No plans with trial.
12494
+ return false;
12495
+ }
12496
+
12497
+ if ( ! $this->apply_filters( 'show_trial', true ) ) {
12498
+ // Developer explicitly asked not to show the trial promo.
12499
+ return false;
12500
+ }
12501
+
12502
+ if ( $this->is_registered() ) {
12503
+ // Check if trial already utilized.
12504
+ if ( $this->_site->is_trial_utilized() ) {
12505
+ return false;
12506
+ }
12507
+
12508
+ if ( $this->is_paying_or_trial() ) {
12509
+ // Don't show trial if paying or already in trial.
12510
+ return false;
12511
+ }
12512
+ }
12513
+
12514
+ if ( $this->is_activation_mode() || $this->is_pending_activation() ) {
12515
+ // If not yet opted-in/skipped, or pending activation, don't show trial.
12516
+ return false;
12517
+ }
12518
+
12519
+ $last_time_trial_promotion_shown = $this->_storage->get( 'trial_promotion_shown', false );
12520
+ $was_promotion_shown_before = ( false !== $last_time_trial_promotion_shown );
12521
+
12522
+ // Show promotion if never shown before and 24 hours after initial activation with FS.
12523
+ if ( ! $was_promotion_shown_before &&
12524
+ $this->_storage->install_timestamp > ( time() - WP_FS__TIME_24_HOURS_IN_SEC )
12525
+ ) {
12526
+ return false;
12527
+ }
12528
+
12529
+ // OR if promotion was shown before, try showing it every 30 days.
12530
+ if ( $was_promotion_shown_before &&
12531
+ 30 * WP_FS__TIME_24_HOURS_IN_SEC > time() - $last_time_trial_promotion_shown
12532
+ ) {
12533
+ return false;
12534
+ }
12535
+
12536
+ $trial_period = $this->_trial_days;
12537
+ $require_payment = $this->_is_trial_require_payment;
12538
+ $trial_url = $this->get_trial_url();
12539
+ $plans_string = strtolower( $this->get_text( 'awesome' ) );
12540
+
12541
+ if ( $this->is_registered() ) {
12542
+ // If opted-in, override trial with up to date data from API.
12543
+ $trial_plans = FS_Plan_Manager::instance()->get_trial_plans( $this->_plans );
12544
+ $trial_plans_count = count( $trial_plans );
12545
+
12546
+ if ( 0 === $trial_plans_count ) {
12547
+ // If there's no plans with a trial just exit.
12548
+ return false;
12549
+ }
12550
+
12551
+ /**
12552
+ * @var FS_Plugin_Plan $paid_plan
12553
+ */
12554
+ $paid_plan = $trial_plans[0];
12555
+ $require_payment = $paid_plan->is_require_subscription;
12556
+ $trial_period = $paid_plan->trial_period;
12557
+
12558
+ $total_paid_plans = count( $this->_plans ) - ( FS_Plan_Manager::instance()->has_free_plan( $this->_plans ) ? 1 : 0 );
12559
+
12560
+ if ( $total_paid_plans !== $trial_plans_count ) {
12561
+ // Not all paid plans have a trial - generate a string of those that have it.
12562
+ for ( $i = 0; $i < $trial_plans_count; $i ++ ) {
12563
+ $plans_string .= sprintf(
12564
+ ' <a href="%s">%s</a>',
12565
+ $trial_url,
12566
+ $trial_plans[ $i ]->title
12567
+ );
12568
+
12569
+ if ( $i < $trial_plans_count - 2 ) {
12570
+ $plans_string .= ', ';
12571
+ } else if ( $i == $trial_plans_count - 2 ) {
12572
+ $plans_string .= ' and ';
12573
+ }
12574
+ }
12575
+ }
12576
+ }
12577
+
12578
+ $message = sprintf(
12579
+ $this->get_text( 'hey' ) . '! ' . $this->get_text( 'trial-x-promotion-message' ),
12580
+ sprintf( '<b>%s</b>', $this->get_plugin_name() ),
12581
+ $plans_string,
12582
+ $trial_period
12583
+ );
12584
+
12585
+ // "No Credit-Card Required" or "No Commitment for N Days".
12586
+ $cc_string = $require_payment ?
12587
+ sprintf( $this->get_text( 'no-commitment-for-x-days' ), $trial_period ) :
12588
+ $this->get_text( 'no-cc-required' ) . '!';
12589
+
12590
+
12591
+ // Start trial button.
12592
+ $button = ' ' . sprintf(
12593
+ '<a style="margin-left: 10px; vertical-align: super;" href="%s"><button class="button button-primary">%s &nbsp;&#10140;</button></a>',
12594
+ $trial_url,
12595
+ $this->get_text( 'start-free-trial' )
12596
+ );
12597
+
12598
+ $this->_admin_notices->add_sticky(
12599
+ $this->apply_filters( 'trial_promotion_message', "{$message} {$cc_string} {$button}" ),
12600
+ 'trial_promotion',
12601
+ '',
12602
+ 'promotion'
12603
+ );
12604
+
12605
+ $this->_storage->trial_promotion_shown = WP_FS__SCRIPT_START_TIME;
12606
+
12607
+ return true;
12608
+ }
12609
+
12610
+ /**
12611
+ * @author Vova Feldman (@svovaf)
12612
+ * @since 1.2.1.5
12613
+ */
12614
+ function _enqueue_common_css() {
12615
+ if ( $this->has_paid_plan() && ! $this->is_paying() ) {
12616
+ // Add basic CSS for admin-notices and menu-item colors.
12617
+ fs_enqueue_local_style( 'fs_common', '/admin/common.css' );
12618
+ }
12619
+ }
12620
+
12621
+ /**
12622
+ * @author Leo Fajardo (leorw)
12623
+ * @since 1.2.2
12624
+ */
12625
+ function _show_theme_activation_optin_dialog() {
12626
+ fs_enqueue_local_style( 'fs_connect', '/admin/connect.css' );
12627
+
12628
+ add_action( 'admin_footer-themes.php', array( &$this, '_add_fs_theme_activation_dialog' ) );
12629
+ }
12630
+
12631
+ /**
12632
+ * @author Leo Fajardo (leorw)
12633
+ * @since 1.2.2
12634
+ */
12635
+ function _add_fs_theme_activation_dialog() {
12636
+ $vars = array( 'id' => $this->_module_id );
12637
+ fs_require_once_template( 'connect.php', $vars );
12638
+ }
12639
+
12640
+ /* Action Links
12641
+ ------------------------------------------------------------------------------------------------------------------*/
12642
+ private $_action_links_hooked = false;
12643
+ private $_action_links = array();
12644
+
12645
+ /**
12646
+ * Hook to plugin action links filter.
12647
+ *
12648
+ * @author Vova Feldman (@svovaf)
12649
+ * @since 1.0.0
12650
+ */
12651
+ private function hook_plugin_action_links() {
12652
+ $this->_logger->entrance();
12653
+
12654
+ $this->_action_links_hooked = true;
12655
+
12656
+ $this->_logger->log( 'Adding action links hooks.' );
12657
+
12658
+ // Add action link to settings page.
12659
+ add_filter( 'plugin_action_links_' . $this->_plugin_basename, array(
12660
+ &$this,
12661
+ '_modify_plugin_action_links_hook'
12662
+ ), WP_FS__DEFAULT_PRIORITY, 2 );
12663
+ add_filter( 'network_admin_plugin_action_links_' . $this->_plugin_basename, array(
12664
+ &$this,
12665
+ '_modify_plugin_action_links_hook'
12666
+ ), WP_FS__DEFAULT_PRIORITY, 2 );
12667
+ }
12668
+
12669
+ /**
12670
+ * Add plugin action link.
12671
+ *
12672
+ * @author Vova Feldman (@svovaf)
12673
+ * @since 1.0.0
12674
+ *
12675
+ * @param $label
12676
+ * @param $url
12677
+ * @param bool $external
12678
+ * @param int $priority
12679
+ * @param bool $key
12680
+ */
12681
+ function add_plugin_action_link( $label, $url, $external = false, $priority = WP_FS__DEFAULT_PRIORITY, $key = false ) {
12682
+ $this->_logger->entrance();
12683
+
12684
+ if ( ! isset( $this->_action_links[ $priority ] ) ) {
12685
+ $this->_action_links[ $priority ] = array();
12686
+ }
12687
+
12688
+ if ( false === $key ) {
12689
+ $key = preg_replace( "/[^A-Za-z0-9 ]/", '', strtolower( $label ) );
12690
+ }
12691
+
12692
+ $this->_action_links[ $priority ][] = array(
12693
+ 'label' => $label,
12694
+ 'href' => $url,
12695
+ 'key' => $key,
12696
+ 'external' => $external
12697
+ );
12698
+ }
12699
+
12700
+ /**
12701
+ * Adds Upgrade and Add-Ons links to the main Plugins page link actions collection.
12702
+ *
12703
+ * @author Vova Feldman (@svovaf)
12704
+ * @since 1.0.0
12705
+ */
12706
+ function _add_upgrade_action_link() {
12707
+ $this->_logger->entrance();
12708
+
12709
+ if ( $this->is_registered() ) {
12710
+ if ( ! $this->is_paying() && $this->has_paid_plan() ) {
12711
+ $this->add_plugin_action_link(
12712
+ $this->get_text( 'upgrade' ),
12713
+ $this->get_upgrade_url(),
12714
+ false,
12715
+ 7,
12716
+ 'upgrade'
12717
+ );
12718
+ }
12719
+
12720
+ if ( $this->has_addons() ) {
12721
+ $this->add_plugin_action_link(
12722
+ $this->get_text( 'add-ons' ),
12723
+ $this->_get_admin_page_url( 'addons' ),
12724
+ false,
12725
+ 9,
12726
+ 'addons'
12727
+ );
12728
+ }
12729
+ }
12730
+ }
12731
+
12732
+ /**
12733
+ * Adds "Activate License" or "Change License" link to the main Plugins page link actions collection.
12734
+ *
12735
+ * @author Leo Fajardo (@leorw)
12736
+ * @since 1.1.9
12737
+ */
12738
+ function _add_license_action_link() {
12739
+ $this->_logger->entrance();
12740
+
12741
+ if ( $this->is_free_plan() && $this->is_addon() ) {
12742
+ return;
12743
+ }
12744
+
12745
+ if ( ! self::is_ajax() ) {
12746
+ // Inject license activation dialog UI and client side code.
12747
+ add_action( 'admin_footer', array( &$this, '_add_license_activation_dialog_box' ) );
12748
+ }
12749
+
12750
+ $link_text = $this->get_text(
12751
+ $this->is_free_plan() ?
12752
+ 'activate-license' :
12753
+ 'change-license'
12754
+ );
12755
+
12756
+ $this->add_plugin_action_link(
12757
+ $link_text,
12758
+ '#',
12759
+ false,
12760
+ 11,
12761
+ ( 'activate-license ' . $this->get_unique_affix() )
12762
+ );
12763
+ }
12764
+
12765
+ /**
12766
+ * Adds "Opt in" or "Opt out" link to the main "Plugins" page link actions collection.
12767
+ *
12768
+ * @author Leo Fajardo (@leorw)
12769
+ * @since 1.2.1.5
12770
+ */
12771
+ function _add_tracking_links() {
12772
+ if ( ! current_user_can( 'activate_plugins' ) ) {
12773
+ return;
12774
+ }
12775
+
12776
+ $this->_logger->entrance();
12777
+
12778
+ if ( fs_request_is_action_secure( $this->get_unique_affix() . '_reconnect' ) ) {
12779
+ if ( ! $this->is_registered() && $this->is_anonymous() ) {
12780
+ $this->connect_again();
12781
+
12782
+ return;
12783
+ }
12784
+ }
12785
+
12786
+ if ( ( $this->is_plugin() && ! self::is_plugins_page() ) ||
12787
+ ( $this->is_theme() && ! self::is_themes_page() )
12788
+ ) {
12789
+ // Only show tracking links on the plugins and themes pages.
12790
+ return;
12791
+ }
12792
+
12793
+ if ( ! $this->is_enable_anonymous() ) {
12794
+ // Don't allow to opt-out if anonymous mode is disabled.
12795
+ return;
12796
+ }
12797
+
12798
+ if ( ! $this->is_free_plan() ) {
12799
+ // Don't allow to opt-out if running in paid plan.
12800
+ return;
12801
+ }
12802
+
12803
+ if ( $this->add_ajax_action( 'stop_tracking', array( &$this, '_stop_tracking_callback' ) ) ) {
12804
+ return;
12805
+ }
12806
+
12807
+ if ( $this->add_ajax_action( 'allow_tracking', array( &$this, '_allow_tracking_callback' ) ) ) {
12808
+ return;
12809
+ }
12810
+
12811
+ $url = '#';
12812
+
12813
+ if ( $this->is_registered() ) {
12814
+ if ( $this->is_tracking_allowed() ) {
12815
+ $link_text_id = 'opt-out';
12816
+ } else {
12817
+ $link_text_id = 'opt-in';
12818
+ }
12819
+
12820
+ add_action( 'admin_footer', array( &$this, '_add_optout_dialog' ) );
12821
+ } else {
12822
+ $link_text_id = 'opt-in';
12823
+
12824
+ $params = ! $this->is_anonymous() ?
12825
+ array() :
12826
+ array(
12827
+ 'nonce' => wp_create_nonce( $this->get_unique_affix() . '_reconnect' ),
12828
+ 'fs_action' => ( $this->get_unique_affix() . '_reconnect' ),
12829
+ );
12830
+
12831
+ $url = $this->get_activation_url( $params );
12832
+ }
12833
+
12834
+ if ( $this->is_plugin() && self::is_plugins_page() ) {
12835
+ $this->add_plugin_action_link(
12836
+ $this->get_text( $link_text_id ),
12837
+ $url,
12838
+ false,
12839
+ 13,
12840
+ "opt-in-or-opt-out {$this->_slug}"
12841
+ );
12842
+ }
12843
+ }
12844
+
12845
+ /**
12846
+ * Get the URL of the page that should be loaded right after the plugin activation.
12847
+ *
12848
+ * @author Vova Feldman (@svovaf)
12849
+ * @since 1.1.7.4
12850
+ *
12851
+ * @return string
12852
+ */
12853
+ function get_after_plugin_activation_redirect_url() {
12854
+ $url = false;
12855
+
12856
+ if ( ! $this->is_addon() || ! $this->has_free_plan() ) {
12857
+ $first_time_path = $this->_menu->get_first_time_path();
12858
+ $url = $this->is_activation_mode() ?
12859
+ $this->get_activation_url() :
12860
+ ( empty( $first_time_path ) ?
12861
+ $this->_get_admin_page_url() :
12862
+ $first_time_path );
12863
+ } else {
12864
+ $plugin_fs = false;
12865
+
12866
+ if ( $this->is_parent_plugin_installed() ) {
12867
+ $plugin_fs = self::get_parent_instance();
12868
+ }
12869
+
12870
+ if ( is_object( $plugin_fs ) ) {
12871
+ if ( ! $plugin_fs->is_registered() ) {
12872
+ // Forward to parent plugin connect when parent not registered.
12873
+ $url = $plugin_fs->get_activation_url();
12874
+ } else {
12875
+ // Forward to account page.
12876
+ $url = $plugin_fs->_get_admin_page_url( 'account' );
12877
+ }
12878
+ }
12879
+ }
12880
+
12881
+ return $url;
12882
+ }
12883
+
12884
+ /**
12885
+ * Forward page to activation page.
12886
+ *
12887
+ * @author Vova Feldman (@svovaf)
12888
+ * @since 1.0.3
12889
+ */
12890
+ function _redirect_on_activation_hook() {
12891
+ $url = $this->get_after_plugin_activation_redirect_url();
12892
+
12893
+ if ( is_string( $url ) ) {
12894
+ fs_redirect( $url );
12895
+ }
12896
+ }
12897
+
12898
+ /**
12899
+ * Modify plugin's page action links collection.
12900
+ *
12901
+ * @author Vova Feldman (@svovaf)
12902
+ * @since 1.0.0
12903
+ *
12904
+ * @param array $links
12905
+ * @param $file
12906
+ *
12907
+ * @return array
12908
+ */
12909
+ function _modify_plugin_action_links_hook( $links, $file ) {
12910
+ $this->_logger->entrance();
12911
+
12912
+ $passed_deactivate = false;
12913
+ $deactivate_link = '';
12914
+ $before_deactivate = array();
12915
+ $after_deactivate = array();
12916
+ foreach ( $links as $key => $link ) {
12917
+ if ( 'deactivate' === $key ) {
12918
+ $deactivate_link = $link;
12919
+ $passed_deactivate = true;
12920
+ continue;
12921
+ }
12922
+
12923
+ if ( ! $passed_deactivate ) {
12924
+ $before_deactivate[ $key ] = $link;
12925
+ } else {
12926
+ $after_deactivate[ $key ] = $link;
12927
+ }
12928
+ }
12929
+
12930
+ ksort( $this->_action_links );
12931
+
12932
+ foreach ( $this->_action_links as $new_links ) {
12933
+ foreach ( $new_links as $link ) {
12934
+ $before_deactivate[ $link['key'] ] = '<a href="' . $link['href'] . '"' . ( $link['external'] ? ' target="_blank"' : '' ) . '>' . $link['label'] . '</a>';
12935
+ }
12936
+ }
12937
+
12938
+ if ( ! empty( $deactivate_link ) ) {
12939
+ /**
12940
+ * This HTML element is used to identify the correct plugin when attaching an event to its Deactivate link.
12941
+ *
12942
+ * @since 1.2.1.6 Always show the deactivation feedback form since we added automatic free version deactivation upon premium code activation.
12943
+ */
12944
+ $deactivate_link .= '<i class="fs-module-id" data-module-id="' . $this->_module_id . '"></i>';
12945
+
12946
+ // Append deactivation link.
12947
+ $before_deactivate['deactivate'] = $deactivate_link;
12948
+ }
12949
+
12950
+ return array_merge( $before_deactivate, $after_deactivate );
12951
+ }
12952
+
12953
+ /**
12954
+ * Adds admin message.
12955
+ *
12956
+ * @author Vova Feldman (@svovaf)
12957
+ * @since 1.0.4
12958
+ *
12959
+ * @param string $message
12960
+ * @param string $title
12961
+ * @param string $type
12962
+ */
12963
+ function add_admin_message( $message, $title = '', $type = 'success' ) {
12964
+ $this->_admin_notices->add( $message, $title, $type );
12965
+ }
12966
+
12967
+ /**
12968
+ * Adds sticky admin message.
12969
+ *
12970
+ * @author Vova Feldman (@svovaf)
12971
+ * @since 1.1.0
12972
+ *
12973
+ * @param string $message
12974
+ * @param string $id
12975
+ * @param string $title
12976
+ * @param string $type
12977
+ */
12978
+ function add_sticky_admin_message( $message, $id, $title = '', $type = 'success' ) {
12979
+ $this->_admin_notices->add_sticky( $message, $id, $title, $type );
12980
+ }
12981
+
12982
+ /**
12983
+ * Helper function that returns the final steps for the upgrade completion.
12984
+ *
12985
+ * If the module is already running the premium code, returns an empty string.
12986
+ *
12987
+ * @author Vova Feldman (@svovaf)
12988
+ * @since 1.2.1
12989
+ *
12990
+ * @param string $plan_title
12991
+ *
12992
+ * @return string
12993
+ */
12994
+ private function get_complete_upgrade_instructions( $plan_title = '' ) {
12995
+ if ( ! $this->has_premium_version() || $this->is_premium() ) {
12996
+ return '';
12997
+ }
12998
+
12999
+ if ( empty( $plan_title ) ) {
13000
+ $plan_title = $this->_site->plan->title;
13001
+ }
13002
+
13003
+ // @since 1.2.1.5 The free version is auto deactivated.
13004
+ $deactivation_step = version_compare( $this->version, '1.2.1.5', '<' ) ?
13005
+ ( '<li>' . $this->get_text( 'deactivate-free-version' ) . '.</li>' ) :
13006
+ '';
13007
+
13008
+ return sprintf(
13009
+ ' %s: <ol><li>%s.</li>%s<li>%s (<a href="%s" target="_blank">%s</a>).</li></ol>',
13010
+ $this->get_text( 'follow-steps-to-complete-upgrade' ),
13011
+ $this->get_latest_download_link( sprintf(
13012
+ $this->get_text( 'download-latest-x-version' ),
13013
+ $plan_title
13014
+ ) ),
13015
+ $deactivation_step,
13016
+ $this->get_text( 'upload-and-activate' ),
13017
+ '//bit.ly/upload-wp-' . $this->_module_type . 's',
13018
+ $this->get_text( 'howto-upload-activate' )
13019
+ );
13020
+ }
13021
+
13022
+ /**
13023
+ * @author Vova Feldman (@svovaf)
13024
+ * @since 1.2.1.7
13025
+ *
13026
+ * @param string $key
13027
+ *
13028
+ * @return string
13029
+ */
13030
+ function get_text( $key ) {
13031
+ return fs_text( $key, $this->_slug );
13032
+ }
13033
+
13034
+ #----------------------------------------------------------------------------------
13035
+ #region Versioning
13036
+ #----------------------------------------------------------------------------------
13037
+
13038
+ /**
13039
+ * Check if Freemius in SDK upgrade mode.
13040
+ *
13041
+ * @author Vova Feldman (@svovaf)
13042
+ * @since 1.0.9
13043
+ *
13044
+ * @return bool
13045
+ */
13046
+ function is_sdk_upgrade_mode() {
13047
+ return isset( $this->_storage->sdk_upgrade_mode ) ?
13048
+ $this->_storage->sdk_upgrade_mode :
13049
+ false;
13050
+ }
13051
+
13052
+ /**
13053
+ * Turn SDK upgrade mode off.
13054
+ *
13055
+ * @author Vova Feldman (@svovaf)
13056
+ * @since 1.0.9
13057
+ */
13058
+ function set_sdk_upgrade_complete() {
13059
+ $this->_storage->sdk_upgrade_mode = false;
13060
+ }
13061
+
13062
+ /**
13063
+ * Check if plugin upgrade mode.
13064
+ *
13065
+ * @author Vova Feldman (@svovaf)
13066
+ * @since 1.0.9
13067
+ *
13068
+ * @return bool
13069
+ */
13070
+ function is_plugin_upgrade_mode() {
13071
+ return isset( $this->_storage->plugin_upgrade_mode ) ?
13072
+ $this->_storage->plugin_upgrade_mode :
13073
+ false;
13074
+ }
13075
+
13076
+ /**
13077
+ * Turn plugin upgrade mode off.
13078
+ *
13079
+ * @author Vova Feldman (@svovaf)
13080
+ * @since 1.0.9
13081
+ */
13082
+ function set_plugin_upgrade_complete() {
13083
+ $this->_storage->plugin_upgrade_mode = false;
13084
+ }
13085
+
13086
+ #endregion
13087
+
13088
+ #----------------------------------------------------------------------------------
13089
+ #region Permissions
13090
+ #----------------------------------------------------------------------------------
13091
+
13092
+ /**
13093
+ * Check if specific permission requested.
13094
+ *
13095
+ * @author Vova Feldman (@svovaf)
13096
+ * @since 1.1.6
13097
+ *
13098
+ * @param string $permission
13099
+ *
13100
+ * @return bool
13101
+ */
13102
+ function is_permission_requested( $permission ) {
13103
+ return isset( $this->_permissions[ $permission ] ) && ( true === $this->_permissions[ $permission ] );
13104
+ }
13105
+
13106
+ #endregion
13107
+
13108
+ #----------------------------------------------------------------------------------
13109
+ #region Auto Activation
13110
+ #----------------------------------------------------------------------------------
13111
+
13112
+ /**
13113
+ * Hints the SDK if running an auto-installation.
13114
+ *
13115
+ * @var bool
13116
+ */
13117
+ private $_isAutoInstall = false;
13118
+
13119
+ /**
13120
+ * After upgrade callback to install and auto activate a plugin.
13121
+ * This code will only be executed on explicit request from the user,
13122
+ * following the practice Jetpack are using with their theme installations.
13123
+ *
13124
+ * @link https://make.wordpress.org/plugins/2017/03/16/clarification-of-guideline-8-executable-code-and-installs/
13125
+ *
13126
+ * @author Vova Feldman (@svovaf)
13127
+ * @since 1.2.1.7
13128
+ */
13129
+ function _install_premium_version_ajax_action() {
13130
+ $this->_logger->entrance();
13131
+
13132
+ $this->check_ajax_referer( 'install_premium_version' );
13133
+
13134
+ if ( ! $this->is_registered() ) {
13135
+ // Not registered.
13136
+ self::shoot_ajax_failure( array(
13137
+ 'message' => $this->get_text( 'auto-install-error-not-opted-in' ),
13138
+ 'code' => 'premium_installed',
13139
+ ) );
13140
+ }
13141
+
13142
+ $plugin_id = fs_request_get( 'module_id', $this->get_id() );
13143
+
13144
+ if ( ! FS_Plugin::is_valid_id( $plugin_id ) ) {
13145
+ // Invalid ID.
13146
+ self::shoot_ajax_failure( array(
13147
+ 'message' => $this->get_text( 'auto-install-error-invalid-id' ),
13148
+ 'code' => 'invalid_module_id',
13149
+ ) );
13150
+ }
13151
+
13152
+ if ( $plugin_id == $this->get_id() ) {
13153
+ if ( $this->is_premium() ) {
13154
+ // Already using the premium code version.
13155
+ self::shoot_ajax_failure( array(
13156
+ 'message' => $this->get_text( 'auto-install-error-premium-activated' ),
13157
+ 'code' => 'premium_installed',
13158
+ ) );
13159
+ }
13160
+ if ( ! $this->can_use_premium_code() ) {
13161
+ // Don't have access to the premium code.
13162
+ self::shoot_ajax_failure( array(
13163
+ 'message' => $this->get_text( 'auto-install-error-invalid-license' ),
13164
+ 'code' => 'invalid_license',
13165
+ ) );
13166
+ }
13167
+ if ( ! $this->has_release_on_freemius() ) {
13168
+ // Plugin is a serviceware, no premium code version.
13169
+ self::shoot_ajax_failure( array(
13170
+ 'message' => $this->get_text( 'auto-install-error-serviceware' ),
13171
+ 'code' => 'premium_version_missing',
13172
+ ) );
13173
+ }
13174
+ } else {
13175
+ $addon = $this->get_addon( $plugin_id );
13176
+
13177
+ if ( ! is_object( $addon ) ) {
13178
+ // Invalid add-on ID.
13179
+ self::shoot_ajax_failure( array(
13180
+ 'message' => $this->get_text( 'auto-install-error-invalid-id' ),
13181
+ 'code' => 'invalid_module_id',
13182
+ ) );
13183
+ }
13184
+
13185
+ if ( $this->is_addon_activated( $plugin_id, true ) ) {
13186
+ // Premium add-on version is already activated.
13187
+ self::shoot_ajax_failure( array(
13188
+ 'message' => $this->get_text( 'auto-install-error-premium-addon-activated' ),
13189
+ 'code' => 'premium_installed',
13190
+ ) );
13191
+ }
13192
+ }
13193
+
13194
+ $this->_isAutoInstall = true;
13195
+
13196
+ // Try to install and activate.
13197
+ $updater = new FS_Plugin_Updater( $this );
13198
+ $result = $updater->install_and_activate_plugin( $plugin_id );
13199
+
13200
+ if ( is_array( $result ) && ! empty( $result['message'] ) ) {
13201
+ self::shoot_ajax_failure( array(
13202
+ 'message' => $result['message'],
13203
+ 'code' => $result['code'],
13204
+ ) );
13205
+ }
13206
+
13207
+ self::shoot_ajax_success( $result );
13208
+ }
13209
+
13210
+ /**
13211
+ * Displays module activation dialog box after a successful upgrade
13212
+ * where the user explicitly requested to auto download and install
13213
+ * the premium version.
13214
+ *
13215
+ * @author Vova Feldman (@svovaf)
13216
+ * @since 1.2.1.7
13217
+ */
13218
+ function _add_auto_installation_dialog_box() {
13219
+ $this->_logger->entrance();
13220
+
13221
+ if ( ! $this->is_registered() ) {
13222
+ // Not registered.
13223
+ return;
13224
+ }
13225
+
13226
+ $plugin_id = fs_request_get( 'plugin_id', $this->get_id() );
13227
+
13228
+ if ( ! FS_Plugin::is_valid_id( $plugin_id ) ) {
13229
+ // Invalid module ID.
13230
+ return;
13231
+ }
13232
+
13233
+ if ( $plugin_id == $this->get_id() ) {
13234
+ if ( $this->is_premium() ) {
13235
+ // Already using the premium code version.
13236
+ return;
13237
+ }
13238
+ if ( ! $this->can_use_premium_code() ) {
13239
+ // Don't have access to the premium code.
13240
+ return;
13241
+ }
13242
+ if ( ! $this->has_release_on_freemius() ) {
13243
+ // Plugin is a serviceware, no premium code version.
13244
+ return;
13245
+ }
13246
+ } else {
13247
+ $addon = $this->get_addon( $plugin_id );
13248
+
13249
+ if ( ! is_object( $addon ) ) {
13250
+ // Invalid add-on ID.
13251
+ return;
13252
+ }
13253
+
13254
+ if ( $this->is_addon_activated( $plugin_id, true ) ) {
13255
+ // Premium add-on version is already activated.
13256
+ return;
13257
+ }
13258
+ }
13259
+
13260
+ $vars = array(
13261
+ 'id' => $plugin_id,
13262
+ 'slug' => $this->_slug,
13263
+ );
13264
+
13265
+ fs_require_template( 'auto-installation.php', $vars );
13266
+ }
13267
+
13268
+ #endregion
13269
+
13270
+ #--------------------------------------------------------------------------------
13271
+ #region Tabs Integration
13272
+ #--------------------------------------------------------------------------------
13273
+
13274
+ #region Module's Original Tabs
13275
+
13276
+ /**
13277
+ * Inject a JavaScript logic to capture the theme tabs HTML.
13278
+ *
13279
+ * @author Vova Feldman (@svovaf)
13280
+ * @since 1.2.2.7
13281
+ */
13282
+ function _tabs_capture() {
13283
+ $this->_logger->entrance();
13284
+
13285
+ if ( ! $this->is_theme_settings_page() ||
13286
+ ! $this->is_matching_url( $this->main_menu_url() )
13287
+ ) {
13288
+ return;
13289
+ }
13290
+
13291
+ $params = array(
13292
+ 'id' => $this->_module_id,
13293
+ );
13294
+
13295
+ fs_require_once_template( 'tabs-capture-js.php', $params );
13296
+ }
13297
+
13298
+ /**
13299
+ * Cache theme's tabs HTML for a week. The cache will also be set as expired
13300
+ * after version and type (free/premium) changes, in addition to the week period.
13301
+ *
13302
+ * @author Vova Feldman (@svovaf)
13303
+ * @since 1.2.2.7
13304
+ */
13305
+ function _store_tabs_ajax_action() {
13306
+ $this->_logger->entrance();
13307
+
13308
+ $this->check_ajax_referer( 'store_tabs' );
13309
+
13310
+ // Init filesystem if not yet initiated.
13311
+ WP_Filesystem();
13312
+
13313
+ // Get POST body HTML data.
13314
+ global $wp_filesystem;
13315
+ $tabs_html = $wp_filesystem->get_contents( "php://input" );
13316
+
13317
+ if ( is_string( $tabs_html ) ) {
13318
+ $tabs_html = trim( $tabs_html );
13319
+ }
13320
+
13321
+ if ( ! is_string( $tabs_html ) || empty( $tabs_html ) ) {
13322
+ self::shoot_ajax_failure();
13323
+ }
13324
+
13325
+ $this->_cache->set( 'tabs', $tabs_html, 7 * WP_FS__TIME_24_HOURS_IN_SEC );
13326
+
13327
+ self::shoot_ajax_success();
13328
+ }
13329
+
13330
+ /**
13331
+ * Cache theme's settings page custom styles. The cache will also be set as expired
13332
+ * after version and type (free/premium) changes, in addition to the week period.
13333
+ *
13334
+ * @author Vova Feldman (@svovaf)
13335
+ * @since 1.2.2.7
13336
+ */
13337
+ function _store_tabs_styles() {
13338
+ $this->_logger->entrance();
13339
+
13340
+ if ( ! $this->is_theme_settings_page() ||
13341
+ ! $this->is_matching_url( $this->main_menu_url() )
13342
+ ) {
13343
+ return;
13344
+ }
13345
+
13346
+ $wp_styles = wp_styles();
13347
+
13348
+ $theme_styles_url = get_template_directory_uri();
13349
+
13350
+ $stylesheets = array();
13351
+ foreach ( $wp_styles->queue as $handler ) {
13352
+ if ( fs_starts_with( $handler, 'fs_' ) ) {
13353
+ // Assume that stylesheets that their handler starts with "fs_" belong to the SDK.
13354
+ continue;
13355
+ }
13356
+
13357
+ /**
13358
+ * @var _WP_Dependency $stylesheet
13359
+ */
13360
+ $stylesheet = $wp_styles->registered[ $handler ];
13361
+
13362
+ if ( fs_starts_with( $stylesheet->src, $theme_styles_url ) ) {
13363
+ $stylesheets[] = $stylesheet->src;
13364
+ }
13365
+ }
13366
+
13367
+ if ( ! empty( $stylesheets ) ) {
13368
+ $this->_cache->set( 'tabs_stylesheets', $stylesheets, 7 * WP_FS__TIME_24_HOURS_IN_SEC );
13369
+ }
13370
+ }
13371
+
13372
+ /**
13373
+ * Check if module's original settings page has any tabs.
13374
+ *
13375
+ * @author Vova Feldman (@svovaf)
13376
+ * @since 1.2.2.7
13377
+ *
13378
+ * @return bool
13379
+ */
13380
+ private function has_tabs() {
13381
+ return $this->_cache->has( 'tabs' );
13382
+ }
13383
+
13384
+ /**
13385
+ * Get module's settings page HTML content, starting
13386
+ * from the beginning of the <div class="wrap"> element,
13387
+ * until the tabs HTML (including).
13388
+ *
13389
+ * @author Vova Feldman (@svovaf)
13390
+ * @since 1.2.2.7
13391
+ *
13392
+ * @return string
13393
+ */
13394
+ private function get_tabs_html() {
13395
+ $this->_logger->entrance();
13396
+
13397
+ return $this->_cache->get( 'tabs' );
13398
+ }
13399
+
13400
+ /**
13401
+ * Check if page should include tabs.
13402
+ *
13403
+ * @author Vova Feldman (@svovaf)
13404
+ * @since 1.2.2.7
13405
+ *
13406
+ * @return bool
13407
+ */
13408
+ private function should_page_include_tabs()
13409
+ {
13410
+ if ( ! $this->has_settings_menu() ) {
13411
+ // Don't add tabs if no settings at all.
13412
+ return false;
13413
+ }
13414
+
13415
+ if ( ! $this->is_theme() ) {
13416
+ // Only add tabs to themes for now.
13417
+ return false;
13418
+ }
13419
+
13420
+ if ( ! $this->has_paid_plan() && ! $this->has_addons() ) {
13421
+ // Only add tabs to monetizing themes.
13422
+ return false;
13423
+ }
13424
+
13425
+ if ( ! $this->is_theme_settings_page() ) {
13426
+ // Only add tabs if browsing one of the theme's setting pages.
13427
+ return false;
13428
+ }
13429
+
13430
+ if ( $this->is_admin_page( 'pricing' ) && fs_request_get_bool( 'checkout' ) ) {
13431
+ // Don't add tabs on checkout page, we want to reduce distractions
13432
+ // as much as possible.
13433
+ return false;
13434
+ }
13435
+
13436
+ return true;
13437
+ }
13438
+
13439
+ /**
13440
+ * Add the tabs HTML before the setting's page content and
13441
+ * enqueue any required stylesheets.
13442
+ *
13443
+ * @author Vova Feldman (@svovaf)
13444
+ * @since 1.2.2.7
13445
+ *
13446
+ * @return bool If tabs were included.
13447
+ */
13448
+ function _add_tabs_before_content() {
13449
+ $this->_logger->entrance();
13450
+
13451
+ if ( ! $this->should_page_include_tabs() ) {
13452
+ return false;
13453
+ }
13454
+
13455
+ /**
13456
+ * Enqueue the original stylesheets that are included in the
13457
+ * theme settings page. That way, if the theme settings has
13458
+ * some custom _styled_ content above the tabs UI, this
13459
+ * will make sure that the styling is preserved.
13460
+ */
13461
+ $stylesheets = $this->_cache->get( 'tabs_stylesheets', array() );
13462
+ if ( is_array( $stylesheets ) ) {
13463
+ for ( $i = 0, $len = count( $stylesheets ); $i < $len; $i ++ ) {
13464
+ wp_enqueue_style( "fs_{$this->_module_id}_tabs_{$i}", $stylesheets[ $i ] );
13465
+ }
13466
+ }
13467
+
13468
+ // Cut closing </div> tag.
13469
+ echo substr( trim( $this->get_tabs_html() ), 0, - 6 );
13470
+
13471
+ return true;
13472
+ }
13473
+
13474
+ /**
13475
+ * Add the tabs closing HTML after the setting's page content.
13476
+ *
13477
+ * @author Vova Feldman (@svovaf)
13478
+ * @since 1.2.2.7
13479
+ *
13480
+ * @return bool If tabs closing HTML was included.
13481
+ */
13482
+ function _add_tabs_after_content() {
13483
+ $this->_logger->entrance();
13484
+
13485
+ if ( ! $this->should_page_include_tabs() ) {
13486
+ return false;
13487
+ }
13488
+
13489
+ echo '</div>';
13490
+
13491
+ return true;
13492
+ }
13493
+
13494
+ #endregion
13495
+
13496
+ /**
13497
+ * Add in-page JavaScript to inject the Freemius tabs into
13498
+ * the module's setting tabs section.
13499
+ *
13500
+ * @author Vova Feldman (@svovaf)
13501
+ * @since 1.2.2.7
13502
+ */
13503
+ function _add_freemius_tabs() {
13504
+ $this->_logger->entrance();
13505
+
13506
+ if ( ! $this->should_page_include_tabs() ) {
13507
+ return;
13508
+ }
13509
+
13510
+ $params = array( 'id' => $this->_module_id );
13511
+ fs_require_once_template( 'tabs.php', $params );
13512
+ }
13513
+
13514
+ #endregion
13515
+
13516
+ #--------------------------------------------------------------------------------
13517
+ #region Customizer Integration for Themes
13518
+ #--------------------------------------------------------------------------------
13519
+
13520
+ /**
13521
+ * @author Vova Feldman (@svovaf)
13522
+ * @since 1.2.2.7
13523
+ *
13524
+ * @param WP_Customize_Manager $customizer
13525
+ */
13526
+ function _customizer_register($customizer) {
13527
+ $this->_logger->entrance();
13528
+
13529
+ if ( $this->is_pricing_page_visible() ) {
13530
+ require_once WP_FS__DIR_INCLUDES . '/customizer/class-fs-customizer-upsell-control.php';
13531
+
13532
+ $customizer->add_section( 'freemius_upsell', array(
13533
+ 'title' => '&#9733; ' . $this->get_text( 'view-paid-features' ),
13534
+ 'priority' => 1,
13535
+ ) );
13536
+ $customizer->add_setting( 'freemius_upsell', array(
13537
+ 'sanitize_callback' => 'esc_html',
13538
+ ) );
13539
+
13540
+ $customizer->add_control( new FS_Customizer_Upsell_Control( $customizer, 'freemius_upsell', array(
13541
+ 'fs' => $this,
13542
+ 'section' => 'freemius_upsell',
13543
+ 'priority' => 100,
13544
+ ) ) );
13545
+ }
13546
+
13547
+ if ( $this->is_page_visible( 'contact' ) || $this->is_page_visible( 'support' ) ) {
13548
+ require_once WP_FS__DIR_INCLUDES . '/customizer/class-fs-customizer-support-section.php';
13549
+
13550
+ // Main Documentation Link In Customizer Root.
13551
+ $customizer->add_section( new FS_Customizer_Support_Section( $customizer, 'freemius_support', array(
13552
+ 'fs' => $this,
13553
+ 'priority' => 1000,
13554
+ ) ) );
13555
+ }
13556
+ }
13557
+
13558
+ #endregion
13559
+
13560
+ /**
13561
+ * If the theme has a paid version, add some custom
13562
+ * styling to the theme's premium version (if exists)
13563
+ * to highlight that it's the premium version of the
13564
+ * same theme, making it easier for identification
13565
+ * after the user upgrades and upload it to the site.
13566
+ *
13567
+ * @author Vova Feldman (@svovaf)
13568
+ * @since 1.2.2.7
13569
+ */
13570
+ function _style_premium_theme() {
13571
+ $this->_logger->entrance();
13572
+
13573
+ if ( ! self::is_themes_page() ) {
13574
+ // Only include in the themes page.
13575
+ return;
13576
+ }
13577
+
13578
+ if ( ! $this->has_paid_plan() ) {
13579
+ // Only include if has any paid plans.
13580
+ return;
13581
+ }
13582
+
13583
+ $params = null;
13584
+ fs_require_once_template( '/js/jquery.content-change.php', $params );
13585
+
13586
+ $params = array(
13587
+ 'slug' => $this->_slug,
13588
+ 'id' => $this->_module_id,
13589
+ );
13590
+
13591
+ fs_require_template( '/js/style-premium-theme.php', $params );
13592
+ }
13593
+
13594
+ #----------------------------------------------------------------------------------
13595
+ #region Marketing
13596
+ #----------------------------------------------------------------------------------
13597
+
13598
+ /**
13599
+ * Check if current user purchased any other plugins before.
13600
+ *
13601
+ * @author Vova Feldman (@svovaf)
13602
+ * @since 1.0.9
13603
+ *
13604
+ * @return bool
13605
+ */
13606
+ function has_purchased_before() {
13607
+ // TODO: Implement has_purchased_before() method.
13608
+ throw new Exception( 'not implemented' );
13609
+ }
13610
+
13611
+ /**
13612
+ * Check if current user classified as an agency.
13613
+ *
13614
+ * @author Vova Feldman (@svovaf)
13615
+ * @since 1.0.9
13616
+ *
13617
+ * @return bool
13618
+ */
13619
+ function is_agency() {
13620
+ // TODO: Implement is_agency() method.
13621
+ throw new Exception( 'not implemented' );
13622
+ }
13623
+
13624
+ /**
13625
+ * Check if current user classified as a developer.
13626
+ *
13627
+ * @author Vova Feldman (@svovaf)
13628
+ * @since 1.0.9
13629
+ *
13630
+ * @return bool
13631
+ */
13632
+ function is_developer() {
13633
+ // TODO: Implement is_developer() method.
13634
+ throw new Exception( 'not implemented' );
13635
+ }
13636
+
13637
+ /**
13638
+ * Check if current user classified as a business.
13639
+ *
13640
+ * @author Vova Feldman (@svovaf)
13641
+ * @since 1.0.9
13642
+ *
13643
+ * @return bool
13644
+ */
13645
+ function is_business() {
13646
+ // TODO: Implement is_business() method.
13647
+ throw new Exception( 'not implemented' );
13648
+ }
13649
+
13650
+ #endregion
13651
+ }
includes/activator.class.php CHANGED
@@ -133,6 +133,12 @@ class TInvWL_Activator {
133
  $table['upgrade'][ self::pre_database( $method ) ] = $_upgrade;
134
  }
135
  if ( array_key_exists( $name, $tables1 ) ) {
 
 
 
 
 
 
136
  $table['upgrade'] = tinv_array_merge( $tables1[ $name ]['upgrade'], $table['upgrade'] );
137
  }
138
  if ( empty( $table['field'] ) ) {
133
  $table['upgrade'][ self::pre_database( $method ) ] = $_upgrade;
134
  }
135
  if ( array_key_exists( $name, $tables1 ) ) {
136
+ if ( ! array_key_exists( 'upgrade', $tables1[ $name ] ) ) {
137
+ $tables1[ $name ]['upgrade'] = array();
138
+ }
139
+ if ( ! array_key_exists( 'upgrade', $table ) ) {
140
+ $table['upgrade'] = array();
141
+ }
142
  $table['upgrade'] = tinv_array_merge( $tables1[ $name ]['upgrade'], $table['upgrade'] );
143
  }
144
  if ( empty( $table['field'] ) ) {
includes/update.helper.php CHANGED
@@ -121,11 +121,23 @@ class TInvWL_Update {
121
  * Fix name field
122
  */
123
  function up_p_1_5_4() {
124
- tinv_update_option( 'product_table', 'add_to_cart', tinv_get_option( 'product_table', 'add_to_card' ) );
125
- tinv_update_option( 'product_table', 'text_add_to_cart', tinv_get_option( 'product_table', 'text_add_to_card' ) );
126
- tinv_update_option( 'table', 'add_select_to_cart', tinv_get_option( 'table', 'add_select_to_card' ) );
127
- tinv_update_option( 'table', 'text_add_select_to_cart', tinv_get_option( 'table', 'text_add_select_to_card' ) );
128
- tinv_update_option( 'table', 'add_all_to_cart', tinv_get_option( 'table', 'add_all_to_card' ) );
129
- tinv_update_option( 'table', 'text_add_all_to_cart', tinv_get_option( 'table', 'text_add_all_to_card' ) );
 
 
 
 
 
 
 
 
 
 
 
 
130
  }
131
  }
121
  * Fix name field
122
  */
123
  function up_p_1_5_4() {
124
+ if ( $value = tinv_get_option( 'product_table', 'add_to_card' ) ) {
125
+ tinv_update_option( 'product_table', 'add_to_cart', $value );
126
+ }
127
+ if ( $value = tinv_get_option( 'product_table', 'text_add_to_card' ) ) {
128
+ tinv_update_option( 'product_table', 'text_add_to_cart', $value );
129
+ }
130
+ if ( $value = tinv_get_option( 'table', 'add_select_to_card' ) ) {
131
+ tinv_update_option( 'table', 'add_select_to_cart', $value );
132
+ }
133
+ if ( $value = tinv_get_option( 'table', 'text_add_select_to_card' ) ) {
134
+ tinv_update_option( 'table', 'text_add_select_to_cart', $value );
135
+ }
136
+ if ( $value = tinv_get_option( 'table', 'add_all_to_card' ) ) {
137
+ tinv_update_option( 'table', 'add_all_to_cart', $value );
138
+ }
139
+ if ( $value = tinv_get_option( 'table', 'text_add_all_to_card' ) ) {
140
+ tinv_update_option( 'table', 'text_add_all_to_cart', $value );
141
+ }
142
  }
143
  }
languages/ti-woocommerce-wishlist.pot CHANGED
@@ -3,7 +3,7 @@ msgid ""
3
  msgstr ""
4
  "Project-Id-Version: WooCommerce Wishlist Plugin\n"
5
  "Report-Msgid-Bugs-To: \n"
6
- "POT-Creation-Date: 2017-11-08 00:31+0700\n"
7
  "POT-Revision-Date: Sun Apr 10 2016 12:21:06 GMT+0200 (Västeuropa, "
8
  "sommartid)\n"
9
  "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
@@ -731,7 +731,7 @@ msgstr ""
731
  msgid "Accent Buttons Text Hover Color"
732
  msgstr ""
733
 
734
- #: admin/settings/upgrade.class.php:34 admin/tinvwl.class.php:198
735
  msgid "Upgrade to Pro"
736
  msgstr ""
737
 
@@ -753,48 +753,48 @@ msgstr ""
753
  msgid "Share your love with us."
754
  msgstr ""
755
 
756
- #: admin/tinvwl.class.php:95
757
  msgid ""
758
  "<strong>Welcome to WooCommerce Wishlist Plugin<strong> – You‘re almost ready "
759
  "to start :)"
760
  msgstr ""
761
 
762
- #: admin/tinvwl.class.php:97 admin/tinvwl.class.php:113
763
  msgid "Run the Setup Wizard"
764
  msgstr ""
765
 
766
- #: admin/tinvwl.class.php:99
767
  msgid "Skip Setup"
768
  msgstr ""
769
 
770
- #: admin/tinvwl.class.php:107 includes/notice.helper.php:111
771
  msgid "Dismiss"
772
  msgstr ""
773
 
774
- #: admin/tinvwl.class.php:108
775
  msgid "Link to Wishlists does not work!"
776
  msgstr ""
777
 
778
- #: admin/tinvwl.class.php:110
779
  msgid "Please apply the Wishlist page"
780
  msgstr ""
781
 
782
- #: admin/tinvwl.class.php:111
783
  msgid " or "
784
  msgstr ""
785
 
786
- #: admin/tinvwl.class.php:171
787
  msgid "Are you sure you want to reset the settings?"
788
  msgstr ""
789
 
790
- #: admin/tinvwl.class.php:236
791
  #, php-format
792
  msgid ""
793
  "<code>%1$s</code> version <strong style=\"color:red\">%2$s</strong> is out "
794
  "of date. The core version is <strong style=\"color:red\">%3$s</strong>"
795
  msgstr ""
796
 
797
- #: admin/tinvwl.class.php:262
798
  #, php-format
799
  msgid ""
800
  "<strong>Your theme (%1$s) contains outdated copies of some WooCommerce "
@@ -805,7 +805,7 @@ msgid ""
805
  "the theme."
806
  msgstr ""
807
 
808
- #: admin/tinvwl.class.php:384
809
  #, php-format
810
  msgid ""
811
  "The \"Add to Wishlist\" button may work improperly in a product type \"%1$s"
@@ -1111,46 +1111,46 @@ msgid "Stock Status"
1111
  msgstr ""
1112
 
1113
  #: templates/ti-wishlist-user.php:110 templates/ti-wishlist.php:114
1114
- #: tinv-wishlists-function-integration.php:793
1115
- #: tinv-wishlists-function-integration.php:942
1116
- #: tinv-wishlists-function-integration.php:1089
1117
- #: tinv-wishlists-function-integration.php:1302
1118
  msgid "In stock"
1119
  msgstr ""
1120
 
1121
- #: tinv-wishlists-function-integration.php:224
1122
  msgid "Select options"
1123
  msgstr ""
1124
 
1125
- #: tinv-wishlists-function-integration.php:518
1126
  msgid "Note"
1127
  msgstr ""
1128
 
1129
- #: tinv-wishlists-function-integration.php:521
1130
  msgid "To"
1131
  msgstr ""
1132
 
1133
- #: tinv-wishlists-function-integration.php:524
1134
  msgid "To Email"
1135
  msgstr ""
1136
 
1137
- #: tinv-wishlists-function-integration.php:527
1138
  msgid "Address"
1139
  msgstr ""
1140
 
1141
- #: tinv-wishlists-function-integration.php:530
1142
  msgid "Reload existing Gift Card"
1143
  msgstr ""
1144
 
1145
- #: tinv-wishlists-function-integration.php:1241
1146
  msgid "Out of stock"
1147
  msgstr ""
1148
 
1149
- #: tinv-wishlists-function-integration.php:1432
1150
  msgid "Photos imported - "
1151
  msgstr ""
1152
 
1153
- #: tinv-wishlists-function-integration.php:1539
1154
  #, php-format
1155
  msgid ""
1156
  "Please help us improve %2$s! If you opt-in, some data about your usage of "
@@ -1158,18 +1158,18 @@ msgid ""
1158
  "work just fine."
1159
  msgstr ""
1160
 
1161
- #: tinv-wishlists-function-integration.php:1553
1162
  #, php-format
1163
  msgid ""
1164
  "Allow %6$s to collect some usage data with %5$s to make the plugin even more "
1165
  "awesome. If you skip this, that's okay! %2$s will still work just fine."
1166
  msgstr ""
1167
 
1168
- #: tinv-wishlists-function-integration.php:1553
1169
  msgid "WooCommerce Wishlist Plugin"
1170
  msgstr ""
1171
 
1172
- #: tinv-wishlists-function-integration.php:1553
1173
  msgid "TemplateInvaders"
1174
  msgstr ""
1175
 
3
  msgstr ""
4
  "Project-Id-Version: WooCommerce Wishlist Plugin\n"
5
  "Report-Msgid-Bugs-To: \n"
6
+ "POT-Creation-Date: 2017-11-21 15:34+0700\n"
7
  "POT-Revision-Date: Sun Apr 10 2016 12:21:06 GMT+0200 (Västeuropa, "
8
  "sommartid)\n"
9
  "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
731
  msgid "Accent Buttons Text Hover Color"
732
  msgstr ""
733
 
734
+ #: admin/settings/upgrade.class.php:34 admin/tinvwl.class.php:215
735
  msgid "Upgrade to Pro"
736
  msgstr ""
737
 
753
  msgid "Share your love with us."
754
  msgstr ""
755
 
756
+ #: admin/tinvwl.class.php:96
757
  msgid ""
758
  "<strong>Welcome to WooCommerce Wishlist Plugin<strong> – You‘re almost ready "
759
  "to start :)"
760
  msgstr ""
761
 
762
+ #: admin/tinvwl.class.php:98 admin/tinvwl.class.php:114
763
  msgid "Run the Setup Wizard"
764
  msgstr ""
765
 
766
+ #: admin/tinvwl.class.php:100
767
  msgid "Skip Setup"
768
  msgstr ""
769
 
770
+ #: admin/tinvwl.class.php:108 includes/notice.helper.php:111
771
  msgid "Dismiss"
772
  msgstr ""
773
 
774
+ #: admin/tinvwl.class.php:109
775
  msgid "Link to Wishlists does not work!"
776
  msgstr ""
777
 
778
+ #: admin/tinvwl.class.php:111
779
  msgid "Please apply the Wishlist page"
780
  msgstr ""
781
 
782
+ #: admin/tinvwl.class.php:112
783
  msgid " or "
784
  msgstr ""
785
 
786
+ #: admin/tinvwl.class.php:175
787
  msgid "Are you sure you want to reset the settings?"
788
  msgstr ""
789
 
790
+ #: admin/tinvwl.class.php:254
791
  #, php-format
792
  msgid ""
793
  "<code>%1$s</code> version <strong style=\"color:red\">%2$s</strong> is out "
794
  "of date. The core version is <strong style=\"color:red\">%3$s</strong>"
795
  msgstr ""
796
 
797
+ #: admin/tinvwl.class.php:280
798
  #, php-format
799
  msgid ""
800
  "<strong>Your theme (%1$s) contains outdated copies of some WooCommerce "
805
  "the theme."
806
  msgstr ""
807
 
808
+ #: admin/tinvwl.class.php:405
809
  #, php-format
810
  msgid ""
811
  "The \"Add to Wishlist\" button may work improperly in a product type \"%1$s"
1111
  msgstr ""
1112
 
1113
  #: templates/ti-wishlist-user.php:110 templates/ti-wishlist.php:114
1114
+ #: tinv-wishlists-function-integration.php:864
1115
+ #: tinv-wishlists-function-integration.php:1013
1116
+ #: tinv-wishlists-function-integration.php:1160
1117
+ #: tinv-wishlists-function-integration.php:1373
1118
  msgid "In stock"
1119
  msgstr ""
1120
 
1121
+ #: tinv-wishlists-function-integration.php:295
1122
  msgid "Select options"
1123
  msgstr ""
1124
 
1125
+ #: tinv-wishlists-function-integration.php:589
1126
  msgid "Note"
1127
  msgstr ""
1128
 
1129
+ #: tinv-wishlists-function-integration.php:592
1130
  msgid "To"
1131
  msgstr ""
1132
 
1133
+ #: tinv-wishlists-function-integration.php:595
1134
  msgid "To Email"
1135
  msgstr ""
1136
 
1137
+ #: tinv-wishlists-function-integration.php:598
1138
  msgid "Address"
1139
  msgstr ""
1140
 
1141
+ #: tinv-wishlists-function-integration.php:601
1142
  msgid "Reload existing Gift Card"
1143
  msgstr ""
1144
 
1145
+ #: tinv-wishlists-function-integration.php:1312
1146
  msgid "Out of stock"
1147
  msgstr ""
1148
 
1149
+ #: tinv-wishlists-function-integration.php:1503
1150
  msgid "Photos imported - "
1151
  msgstr ""
1152
 
1153
+ #: tinv-wishlists-function-integration.php:1610
1154
  #, php-format
1155
  msgid ""
1156
  "Please help us improve %2$s! If you opt-in, some data about your usage of "
1158
  "work just fine."
1159
  msgstr ""
1160
 
1161
+ #: tinv-wishlists-function-integration.php:1624
1162
  #, php-format
1163
  msgid ""
1164
  "Allow %6$s to collect some usage data with %5$s to make the plugin even more "
1165
  "awesome. If you skip this, that's okay! %2$s will still work just fine."
1166
  msgstr ""
1167
 
1168
+ #: tinv-wishlists-function-integration.php:1624
1169
  msgid "WooCommerce Wishlist Plugin"
1170
  msgstr ""
1171
 
1172
+ #: tinv-wishlists-function-integration.php:1624
1173
  msgid "TemplateInvaders"
1174
  msgstr ""
1175
 
public/addtowishlist.class.php CHANGED
@@ -512,7 +512,7 @@ class TInvWL_Public_AddToWishlist {
512
  if ( ! empty( $icon ) ) {
513
  $icon_upload = tinv_get_option( 'add_to_wishlist' . ( $this->is_loop ? '_catalog' : '' ), 'icon_upload' );
514
  if ( 'custom' === $icon && ! empty( $icon_upload ) ) {
515
- $text = sprintf( '<img src="%s" /> %s', esc_url( $icon_upload ), $text );
516
  }
517
  $icon = 'tinvwl-icon-' . $icon;
518
  if ( 'custom' !== $icon ) {
@@ -541,7 +541,7 @@ class TInvWL_Public_AddToWishlist {
541
  if ( 'button' == tinv_get_option( 'add_to_wishlist' . ( $this->is_loop ? '_catalog' : '' ), 'type' ) ) { // WPCS: loose comparison ok.
542
  $icon .= ' button';
543
  }
544
- $content .= sprintf( '<a class="tinvwl_add_to_wishlist_button %s" tinv-wl-list="%s" tinv-wl-product="%s" tinv-wl-productvariation="%s" tinv-wl-producttype="%s" tinv-wl-action="%s">%s</a>', $icon, htmlspecialchars( wp_json_encode( $this->wishlist ) ), ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $this->product->id : ( $this->product->is_type( 'variation' ) ? $this->product->get_parent_id() : $this->product->get_id() ) ), ( ( $this->is_loop && 'variable' === ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $this->product->product_type : $this->product->get_type() ) ) ? $this->variation_id : ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $this->product->variation_id : ( $this->product->is_type( 'variation' ) ? $this->product->get_id() : 0 ) ) ), ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $this->product->product_type : $this->product->get_type() ), $action, $text );
545
  $content .= apply_filters( 'tinvwl_wishlist_button_after', '' );
546
 
547
  if ( ! empty( $text ) ) {
512
  if ( ! empty( $icon ) ) {
513
  $icon_upload = tinv_get_option( 'add_to_wishlist' . ( $this->is_loop ? '_catalog' : '' ), 'icon_upload' );
514
  if ( 'custom' === $icon && ! empty( $icon_upload ) ) {
515
+ $text = sprintf( '<img src="%s" alt="%s" /> %s', esc_url( $icon_upload ), esc_attr( tinv_get_option( 'add_to_wishlist' . ($this->is_loop ? '_catalog' : ''), 'text' ) ), $text );
516
  }
517
  $icon = 'tinvwl-icon-' . $icon;
518
  if ( 'custom' !== $icon ) {
541
  if ( 'button' == tinv_get_option( 'add_to_wishlist' . ( $this->is_loop ? '_catalog' : '' ), 'type' ) ) { // WPCS: loose comparison ok.
542
  $icon .= ' button';
543
  }
544
+ $content .= sprintf( '<a class="tinvwl_add_to_wishlist_button %s" data-tinv-wl-list="%s" data-tinv-wl-product="%s" data-tinv-wl-productvariation="%s" data-tinv-wl-producttype="%s" data-tinv-wl-action="%s">%s</a>', $icon, htmlspecialchars( wp_json_encode( $this->wishlist ) ), ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $this->product->id : ( $this->product->is_type( 'variation' ) ? $this->product->get_parent_id() : $this->product->get_id() ) ), ( ( $this->is_loop && 'variable' === ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $this->product->product_type : $this->product->get_type() ) ) ? $this->variation_id : ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $this->product->variation_id : ( $this->product->is_type( 'variation' ) ? $this->product->get_id() : 0 ) ) ), ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $this->product->product_type : $this->product->get_type() ), $action, $text );
545
  $content .= apply_filters( 'tinvwl_wishlist_button_after', '' );
546
 
547
  if ( ! empty( $text ) ) {
public/tinvwl.class.php CHANGED
@@ -432,7 +432,7 @@ class TInvWL_Public_TInvWL {
432
  */
433
  function enqueue_scripts() {
434
  $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
435
- wp_register_script( $this->_n, TINVWL_URL . 'asset/js/public' . $suffix . '.js', array( 'jquery' ), $this->_v, true );
436
  wp_localize_script( $this->_n, 'tinvwl_add_to_wishlist', array(
437
  'text_create' => __( 'Create New', 'ti-woocommerce-wishlist' ),
438
  'text_already_in' => tinv_get_option( 'general', 'text_already_in' ),
432
  */
433
  function enqueue_scripts() {
434
  $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
435
+ wp_register_script( $this->_n, TINVWL_URL . 'asset/js/public' . $suffix . '.js', array( 'jquery', 'wc-cart-fragments' ), $this->_v, true );
436
  wp_localize_script( $this->_n, 'tinvwl_add_to_wishlist', array(
437
  'text_create' => __( 'Create New', 'ti-woocommerce-wishlist' ),
438
  'text_already_in' => tinv_get_option( 'general', 'text_already_in' ),
public/topwishlist.class.php CHANGED
@@ -57,6 +57,7 @@ class TInvWL_Public_TopWishlist {
57
  */
58
  function define_hooks() {
59
  add_filter( 'tinvwl_addtowishlist_return_ajax', array( __CLASS__, 'update_widget' ) );
 
60
  }
61
 
62
  /**
@@ -71,7 +72,7 @@ class TInvWL_Public_TopWishlist {
71
  'icon_style' => ( $atts['show_icon'] && tinv_get_option( 'topline', 'icon' ) ) ? esc_attr( 'top_wishlist-' . tinv_get_option( 'topline', 'icon_style' ) ) : '',
72
  'icon_upload' => tinv_get_option( 'topline', 'icon_upload' ),
73
  'text' => $atts['show_text'] ? $atts['text'] : '',
74
- 'counter' => $atts['show_counter'] ? self::counter() : 0,
75
  'show_counter' => $atts['show_counter'],
76
  );
77
  tinv_wishlist_template( 'ti-wishlist-product-counter.php', $data );
@@ -85,6 +86,18 @@ class TInvWL_Public_TopWishlist {
85
  */
86
  public static function update_widget( $data ) {
87
  $data['top_wishlist_counter'] = self::counter();
 
 
 
 
 
 
 
 
 
 
 
 
88
  return $data;
89
  }
90
 
57
  */
58
  function define_hooks() {
59
  add_filter( 'tinvwl_addtowishlist_return_ajax', array( __CLASS__, 'update_widget' ) );
60
+ add_filter( 'woocommerce_add_to_cart_fragments', array( __CLASS__, 'update_fragments' ) );
61
  }
62
 
63
  /**
72
  'icon_style' => ( $atts['show_icon'] && tinv_get_option( 'topline', 'icon' ) ) ? esc_attr( 'top_wishlist-' . tinv_get_option( 'topline', 'icon_style' ) ) : '',
73
  'icon_upload' => tinv_get_option( 'topline', 'icon_upload' ),
74
  'text' => $atts['show_text'] ? $atts['text'] : '',
75
+ 'counter' => $atts['show_counter'],
76
  'show_counter' => $atts['show_counter'],
77
  );
78
  tinv_wishlist_template( 'ti-wishlist-product-counter.php', $data );
86
  */
87
  public static function update_widget( $data ) {
88
  $data['top_wishlist_counter'] = self::counter();
89
+
90
+ return $data;
91
+ }
92
+
93
+ /**
94
+ * Load fragments for wishlist product counter
95
+ *
96
+ * @param array $data Woocommerce Fragments for updateing data.
97
+ */
98
+ public static function update_fragments( $data = array() ) {
99
+ $data['span.wishlist_products_counter_number'] = sprintf( '<span class="wishlist_products_counter_number">%d</span>', self::counter() );
100
+
101
  return $data;
102
  }
103
 
readme.txt CHANGED
@@ -3,8 +3,8 @@ Contributors: templateinvaders
3
  Donate link: https://templateinvaders.com/?utm_source=wordpressorg&utm_content=donate
4
  Tags: wishlist, woocommerce, products, e-commerce, shop, ecommerce wishlist, woocommerce wishlist, woocommerce , shop wishlist, wishlist for Woocommerce
5
  Requires at least: 4.5
6
- Tested up to: 4.8
7
- Stable tag: 1.5.8
8
  License: GPLv3
9
  License URI: https://www.gnu.org/licenses/gpl-3.0.html
10
 
@@ -129,6 +129,16 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
129
 
130
  == Changelog ==
131
 
 
 
 
 
 
 
 
 
 
 
132
  = 1.5.8 =
133
  *Release Date - 07 November 2017*
134
 
3
  Donate link: https://templateinvaders.com/?utm_source=wordpressorg&utm_content=donate
4
  Tags: wishlist, woocommerce, products, e-commerce, shop, ecommerce wishlist, woocommerce wishlist, woocommerce , shop wishlist, wishlist for Woocommerce
5
  Requires at least: 4.5
6
+ Tested up to: 4.9
7
+ Stable tag: 1.5.9
8
  License: GPLv3
9
  License URI: https://www.gnu.org/licenses/gpl-3.0.html
10
 
129
 
130
  == Changelog ==
131
 
132
+ = 1.5.9 =
133
+ *Release Date - 21 November 2017*
134
+
135
+ * Fixed Wishlist Products Counter issue when the wrong number of products was displaying if cache is enabled
136
+ * Fixed an issue with W3 Total Cache plugin
137
+ * Fixed an issue with wrong file path after website migration
138
+ * Fixed an issue with extra scheduled cleanup events
139
+ * Fixed an issue with reset some settings after plugin update
140
+ * Added support for WP Fastest Cache plugin
141
+
142
  = 1.5.8 =
143
  *Release Date - 07 November 2017*
144
 
templates/ti-wishlist-product-counter.php CHANGED
@@ -17,6 +17,6 @@ if ( $icon_class && 'custom' === $icon && ! empty( $icon_upload ) ) {
17
  <a href="<?php echo esc_url( tinv_url_wishlist_default() ); ?>" class="wishlist_products_counter<?php echo ' ' . $icon_class . ' ' . $icon_style . ( empty( $text ) ? ' no-txt' : '' ) . ( 0 < $counter ? ' wishlist-counter-with-products' : '' ); // WPCS: xss ok. ?>">
18
  <span class="wishlist_products_counter_text"><?php echo $text; // WPCS: xss ok. ?></span>
19
  <?php if ( $show_counter ) : ?>
20
- <span class="wishlist_products_counter_number"><?php echo $counter; // WPCS: xss ok. ?></span>
21
  <?php endif; ?>
22
  </a>
17
  <a href="<?php echo esc_url( tinv_url_wishlist_default() ); ?>" class="wishlist_products_counter<?php echo ' ' . $icon_class . ' ' . $icon_style . ( empty( $text ) ? ' no-txt' : '' ) . ( 0 < $counter ? ' wishlist-counter-with-products' : '' ); // WPCS: xss ok. ?>">
18
  <span class="wishlist_products_counter_text"><?php echo $text; // WPCS: xss ok. ?></span>
19
  <?php if ( $show_counter ) : ?>
20
+ <span class="wishlist_products_counter_number"></span>
21
  <?php endif; ?>
22
  </a>
templates/ti-wishlist-user.php CHANGED
@@ -132,7 +132,7 @@ if ( ! defined( 'ABSPATH' ) ) {
132
  </tbody>
133
  <tfoot>
134
  <tr>
135
- <td colspan="100%">
136
  <?php do_action( 'tinvwl_after_wishlist_table', $wishlist ); ?>
137
  <?php wp_nonce_field( 'tinvwl_wishlist_user', 'wishlist_nonce' ); ?>
138
  </td>
132
  </tbody>
133
  <tfoot>
134
  <tr>
135
+ <td colspan="100">
136
  <?php do_action( 'tinvwl_after_wishlist_table', $wishlist ); ?>
137
  <?php wp_nonce_field( 'tinvwl_wishlist_user', 'wishlist_nonce' ); ?>
138
  </td>
templates/ti-wishlist.php CHANGED
@@ -136,7 +136,7 @@ if ( ! defined( 'ABSPATH' ) ) {
136
  </tbody>
137
  <tfoot>
138
  <tr>
139
- <td colspan="100%">
140
  <?php do_action( 'tinvwl_after_wishlist_table', $wishlist ); ?>
141
  <?php wp_nonce_field( 'tinvwl_wishlist_owner', 'wishlist_nonce' ); ?>
142
  </td>
136
  </tbody>
137
  <tfoot>
138
  <tr>
139
+ <td colspan="100">
140
  <?php do_action( 'tinvwl_after_wishlist_table', $wishlist ); ?>
141
  <?php wp_nonce_field( 'tinvwl_wishlist_owner', 'wishlist_nonce' ); ?>
142
  </td>
ti-woocommerce-wishlist.php CHANGED
@@ -4,9 +4,9 @@
4
  * Plugin Name: WooCommerce Wishlist Plugin
5
  * Plugin URI: https://wordpress.org/plugins/ti-woocommerce-wishlist/
6
  * Description: Wishlist functionality for your WooCommerce store.
7
- * Version: 1.5.8
8
  * Requires at least: 4.5
9
- * Tested up to: 4.8
10
  * WC requires at least: 2.6
11
  * WC tested up to: 3.2
12
  * Author: TemplateInvaders
@@ -41,7 +41,7 @@ if ( ! defined( 'TINVWL_DOMAIN' ) ) {
41
  }
42
 
43
  if ( ! defined( 'TINVWL_FVERSION' ) ) {
44
- define( 'TINVWL_FVERSION', '1.5.8' );
45
  }
46
 
47
  if ( ! defined( 'TINVWL_LOAD_FREE' ) ) {
@@ -83,6 +83,7 @@ if ( ! function_exists( 'uninstall_tinv_wishlist' ) ) {
83
  if ( ! defined( 'TINVWL_LOAD_PREMIUM' ) ) {
84
  TInvWL_Activator::uninstall();
85
  flush_rewrite_rules();
 
86
  }
87
  }
88
  }
4
  * Plugin Name: WooCommerce Wishlist Plugin
5
  * Plugin URI: https://wordpress.org/plugins/ti-woocommerce-wishlist/
6
  * Description: Wishlist functionality for your WooCommerce store.
7
+ * Version: 1.5.9
8
  * Requires at least: 4.5
9
+ * Tested up to: 4.9
10
  * WC requires at least: 2.6
11
  * WC tested up to: 3.2
12
  * Author: TemplateInvaders
41
  }
42
 
43
  if ( ! defined( 'TINVWL_FVERSION' ) ) {
44
+ define( 'TINVWL_FVERSION', '1.5.9' );
45
  }
46
 
47
  if ( ! defined( 'TINVWL_LOAD_FREE' ) ) {
83
  if ( ! defined( 'TINVWL_LOAD_PREMIUM' ) ) {
84
  TInvWL_Activator::uninstall();
85
  flush_rewrite_rules();
86
+ wp_clear_scheduled_hook( 'tinvwl_remove_without_author_wishlist' );
87
  }
88
  }
89
  }
tinv-wishlists-function-integration.php CHANGED
@@ -146,8 +146,8 @@ if ( ! function_exists( 'tinvwl_w3total_reject_uri' ) ) {
146
  $pages = array_unique( $pages );
147
  $pages = array_filter( $pages );
148
 
 
149
  if ( ! empty( $pages ) ) {
150
- $config = w3_instance( 'W3_Config' );
151
  $sections = array( 'dbcache.reject.uri', 'pgcache.reject.uri' );
152
  foreach ( $sections as $section ) {
153
  $settings = array_map( 'trim', $config->get_array( $section ) );
@@ -176,6 +176,77 @@ if ( ! function_exists( 'tinvwl_w3total_reject_uri' ) ) {
176
  add_action( 'admin_init', 'tinvwl_w3total_reject_uri' );
177
  } // End if().
178
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
  if ( function_exists( 'tinvwl_comet_cache_reject' ) ) {
180
 
181
  /**
146
  $pages = array_unique( $pages );
147
  $pages = array_filter( $pages );
148
 
149
+ $config = w3_instance( 'W3_Config' );
150
  if ( ! empty( $pages ) ) {
 
151
  $sections = array( 'dbcache.reject.uri', 'pgcache.reject.uri' );
152
  foreach ( $sections as $section ) {
153
  $settings = array_map( 'trim', $config->get_array( $section ) );
176
  add_action( 'admin_init', 'tinvwl_w3total_reject_uri' );
177
  } // End if().
178
 
179
+ if ( ! function_exists( 'tinvwl_wp_fastest_cache_reject' ) ) {
180
+
181
+ /**
182
+ * Disable cache for WP Fastest Cache
183
+ */
184
+ function tinvwl_wp_fastest_cache_reject() {
185
+ if ( defined( 'WPFC_WP_PLUGIN_DIR' ) ) {
186
+ if ( $rules_json = get_option( 'WpFastestCacheExclude' ) ) {
187
+ if ( 'null' !== $rules_json ) {
188
+ $ids = array(
189
+ tinv_get_option( 'page', 'wishlist' ),
190
+ tinv_get_option( 'page', 'manage' ),
191
+ );
192
+ $pages = $ids;
193
+ $languages = apply_filters( 'wpml_active_languages', array(), array(
194
+ 'skip_missing' => 0,
195
+ 'orderby' => 'code',
196
+ ) );
197
+ if ( ! empty( $languages ) ) {
198
+ foreach ( $ids as $id ) {
199
+ foreach ( $languages as $l ) {
200
+ $pages[] = apply_filters( 'wpml_object_id', $id, 'page', true, $l['language_code'] );
201
+ }
202
+ }
203
+ $pages = array_unique( $pages );
204
+ }
205
+ $pages = array_filter( $pages );
206
+ if ( ! empty( $pages ) ) {
207
+ foreach ( $pages as $i => $page ) {
208
+ $pages[ $i ] = preg_replace( "/^\//", '', str_replace( get_site_url(), '', get_permalink( $page ) ) ); // @codingStandardsIgnoreLine Squiz.Strings.DoubleQuoteUsage.NotRequired
209
+ }
210
+ }
211
+ $pages = array_unique( $pages );
212
+ $pages = array_filter( $pages );
213
+
214
+ $rules_std = json_decode( $rules_json, true );
215
+ $ex_pages = array();
216
+ foreach ( $rules_std as $key => $value ) {
217
+ $value['type'] = isset( $value['type'] ) ? $value['type'] : 'page';
218
+ if ( 'page' === $value['type'] ) {
219
+ $ex_pages[] = $value['content'];
220
+ }
221
+ }
222
+ $ex_pages = array_unique( $ex_pages );
223
+ $ex_pages = array_filter( $ex_pages );
224
+ $changed = false;
225
+
226
+ foreach ( $pages as $page ) {
227
+ $page = preg_replace( '/\/$/', '', $page );
228
+
229
+ if ( ! in_array( $page, $ex_pages ) ) {
230
+ $changed = true;
231
+ $rules_std[] = array(
232
+ 'prefix' => 'startwith',
233
+ 'content' => $page,
234
+ 'type' => 'page',
235
+ );
236
+ }
237
+ }
238
+ if ( $changed ) {
239
+ $data = json_encode( $rules_std );
240
+ update_option( 'WpFastestCacheExclude', $data );
241
+ }
242
+ } // End if().
243
+ } // End if().
244
+ } // End if().
245
+ }
246
+
247
+ add_action( 'admin_init', 'tinvwl_wp_fastest_cache_reject' );
248
+ } // End if().
249
+
250
  if ( function_exists( 'tinvwl_comet_cache_reject' ) ) {
251
 
252
  /**