Better Notifications for WordPress - Version 1.6.10

Version Description

  • 31st January 2018 =
  • New: Faster customisation! You can now add the subject and message body from a default WordPress email to notifications. Just choose a default WordPress email and click the 'Insert Default Content' button next to the WYSIWYG editor when adding or editing a notification.
  • New: If you want to allow other user roles to create or edit notifications, you can now grant them the 'bnfw' capability using a role editor.
  • Added: A new notice will display at the top of all Notification screens explaining how you can improve email deliverability. You can dismiss this permanently at any time.
  • Added: Inline help tips are now available when creating or editing a notification. Handy if you want to remember how something works but don't want to sift through the documentation.
  • Added: Merged two pull request that came in via the GitHub clone of this plugin - (#1 and #2).
  • Added: For faster support, there is now a Priority Support menu item in the BNFW Notifications menu. Priority Support is available to customers with an active subscription to an add-on.
  • Improved: All date related shortcodes now output in the date and time format set in Settings > General in the WordPress Admin.
  • Improved: The list of available notifications has been re-ordered to make more sense.
  • Improved: A warning will now show if the total user count in the Send To box is more than 200 users.
  • Improved: Usage tracking will now only load on single sites and not on sites that are part of a Multisite network.
  • Updated: Usage tracking SDK updated to the latest version.
  • Fixed: Warnings were showing when posting a new comment on the front-end.
Download this release

Release Info

Developer voltronik
Plugin Icon 128x128 Better Notifications for WordPress
Version 1.6.10
Comparing to
See all releases

Code changes from version 1.6.9 to 1.6.10

Files changed (55) hide show
  1. README.txt +19 -13
  2. assets/css/bnfw.css +138 -0
  3. assets/js/bnfw.js +120 -1
  4. bnfw.php +23 -4
  5. freemius/LICENSE.txt +620 -286
  6. freemius/assets/css/admin/account.css +1 -1
  7. freemius/assets/css/admin/affiliation.css +1 -0
  8. freemius/assets/css/admin/common.css +2 -2
  9. freemius/assets/css/admin/connect.css +1 -1
  10. freemius/assets/css/admin/index.php +3 -0
  11. freemius/assets/css/common.css +0 -1
  12. freemius/assets/css/customizer.css +1 -0
  13. freemius/assets/css/index.php +3 -0
  14. freemius/assets/img/index.php +3 -0
  15. freemius/assets/img/theme-icon.png +0 -0
  16. freemius/assets/js/index.php +3 -0
  17. freemius/assets/scss/_mixins.scss +0 -13
  18. freemius/assets/scss/admin/_themes.scss +21 -0
  19. freemius/assets/scss/admin/account.scss +6 -0
  20. freemius/assets/scss/admin/affiliation.scss +97 -0
  21. freemius/assets/scss/admin/common.scss +217 -172
  22. freemius/assets/scss/admin/connect.scss +49 -0
  23. freemius/assets/scss/admin/index.php +3 -0
  24. freemius/assets/scss/customizer.scss +125 -0
  25. freemius/assets/scss/index.php +3 -0
  26. freemius/composer.json +10 -0
  27. freemius/config.php +6 -2
  28. freemius/gulpfile.js +167 -0
  29. freemius/includes/class-freemius-abstract.php +2 -2
  30. freemius/includes/class-freemius.php +14288 -12263
  31. freemius/includes/class-fs-api.php +557 -557
  32. freemius/includes/class-fs-logger.php +1 -1
  33. freemius/includes/class-fs-plugin-updater.php +711 -682
  34. freemius/includes/class-fs-security.php +1 -1
  35. freemius/includes/customizer/class-fs-customizer-support-section.php +102 -0
  36. freemius/includes/customizer/class-fs-customizer-upsell-control.php +157 -0
  37. freemius/includes/customizer/index.php +3 -0
  38. freemius/includes/debug/class-fs-debug-bar-panel.php +6 -6
  39. freemius/includes/debug/debug-bar-start.php +3 -3
  40. freemius/includes/entities/class-fs-affiliate-terms.php +128 -0
  41. freemius/includes/entities/class-fs-affiliate.php +84 -0
  42. freemius/includes/entities/class-fs-billing.php +1 -1
  43. freemius/includes/entities/class-fs-entity.php +4 -4
  44. freemius/includes/entities/class-fs-payment.php +1 -1
  45. freemius/includes/entities/class-fs-plugin-info.php +1 -1
  46. freemius/includes/entities/class-fs-plugin-license.php +1 -1
  47. freemius/includes/entities/class-fs-plugin-plan.php +1 -1
  48. freemius/includes/entities/class-fs-plugin-tag.php +1 -1
  49. freemius/includes/entities/class-fs-plugin.php +24 -1
  50. freemius/includes/entities/class-fs-pricing.php +1 -1
  51. freemius/includes/entities/class-fs-scope-entity.php +1 -1
  52. freemius/includes/entities/class-fs-site.php +1 -1
  53. freemius/includes/entities/class-fs-subscription.php +1 -1
  54. freemius/includes/entities/class-fs-user.php +1 -1
  55. freemius/includes/fs-core-functions.php +1086 -629
README.txt CHANGED
@@ -3,9 +3,9 @@ Contributors: voltronik
3
  Donate link: https://betternotificationsforwp.com/donate/
4
  Tags: notification, email, push, sms, alert, HTML, customize, bulk, trigger, CC, BCC
5
  Requires at least: 3.5
6
- Tested up to: 4.9.1
7
  Requires PHP: 5.6
8
- Stable tag: 1.6.9
9
  License: GPLv2 or later
10
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
11
 
@@ -152,19 +152,11 @@ Yes and no - some notifications work, others don't, and some are missing entirel
152
 
153
  It's not designed to send out newsletters. There is no send-this-out-on-this-date style functionality included. There are many other great plugins available that you could use for that instead. Additionally, users cannot currently unsubscribe automatically from notifications so you'll have to work out how you manage unsubscribers manually.
154
 
155
- = How do I set-up WordPress to work with this plugin correctly? =
156
-
157
- This will very much depend on what notifications you're using the plugin for. Out-of-the-box, this plugin works very well but there are a few tweaks that will ensure 100% compatibility. All the below points refer to this plugin:
158
-
159
- * If you want to use the new comment notifications in BNFW, you need to un-tick 'Email me whenever anyone posts a comment' and 'A comment is held for moderation' under Settings > Discussion. It's OK if you don't do this but you might receive WordPress's own email notifications along with the ones you've configure using BNFW. It also goes without saying that you need to enable comments for your posts if you want the email notifications to come through.
160
-
161
- * If you want to use the 'Comment Reply' transactional email, you need to ensure that comments are only set-up to be 2-levels deep. You can do this by going to Settings > Discussion and changing the option 'Enable threaded (nested) comments 'X' levels deep' to '2'. Please also ensure this option is ticked.
162
-
163
  = Notifications aren't coming through! =
164
 
165
  First of all, follow [this article](https://betternotificationsforwp.com/how-can-i-receive-the-best-support/) about how to find out what might be causing the problem. Additionally, please take a look at [this help document](https://betternotificationsforwp.com/documentation/getting-started/how-to-improve-email-delivery/) to see how you might improve email delivery when using Better Notifications for WordPress.
166
 
167
- Many hosts place a limit on the number of emails that can be sent out within an hour so this may also cause some delay in emails arriving. MailPoet has a fairly extensive list of hosts and their corresponding email rate limits that's worth checking out [here](https://support.mailpoet.com/knowledgebase/lists-of-hosts-and-their-sending-limits/). Alternatively, please check with your host directly to find out what your limit is.
168
 
169
  If you're still having problems, please drop me a line in the [Free Support Forums](https://wordpress.org/support/plugin/bnfw) and I'll do my best to help.
170
 
@@ -206,12 +198,26 @@ An older version might work but this is untested. A lot of the newer features re
206
 
207
  == Changelog ==
208
 
209
- = 1.6.9 - 7th December 2017 =
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
  * Merry Christmas & Happy New Year!
211
  * New: Support for new [Profile Builder Add-on](https://betternotificationsforwp.com/downloads/profile-builder/).
212
  * Fixed: An issue where the YouTube video wasn't showing properly on the BNFW WP.org plugin page.
213
 
214
- = 1.6.8 - 23rd November 2017 =
215
  * New: All shortcodes that are correctly registered within WordPress will now be stripped from the `[post_excerpt]` and `[post_content]` shortcodes so as to not cause layout or styling issues within your notifications.
216
  * Added: A minimum PHP version of 5.6 is now required to run BNFW.
217
  * Fixed: Compatibility with the [Reminders](https://betternotificationsforwp.com/downloads/update-reminder/) and [Conditional Notifications](https://betternotificationsforwp.com/downloads/conditional-notifications/) add-ons.
3
  Donate link: https://betternotificationsforwp.com/donate/
4
  Tags: notification, email, push, sms, alert, HTML, customize, bulk, trigger, CC, BCC
5
  Requires at least: 3.5
6
+ Tested up to: 4.9.2
7
  Requires PHP: 5.6
8
+ Stable tag: 1.6.10
9
  License: GPLv2 or later
10
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
11
 
152
 
153
  It's not designed to send out newsletters. There is no send-this-out-on-this-date style functionality included. There are many other great plugins available that you could use for that instead. Additionally, users cannot currently unsubscribe automatically from notifications so you'll have to work out how you manage unsubscribers manually.
154
 
 
 
 
 
 
 
 
 
155
  = Notifications aren't coming through! =
156
 
157
  First of all, follow [this article](https://betternotificationsforwp.com/how-can-i-receive-the-best-support/) about how to find out what might be causing the problem. Additionally, please take a look at [this help document](https://betternotificationsforwp.com/documentation/getting-started/how-to-improve-email-delivery/) to see how you might improve email delivery when using Better Notifications for WordPress.
158
 
159
+ Many hosts place a limit on the number of emails that can be sent out within an hour so this may also cause some delay in emails arriving. [This article](https://support.mailpoet.com/knowledgebase/lists-of-hosts-and-their-sending-limits/) has a fairly extensive list of hosts and their corresponding email rate limits that's worth checking out. Alternatively, please check with your host directly to find out what your limit is.
160
 
161
  If you're still having problems, please drop me a line in the [Free Support Forums](https://wordpress.org/support/plugin/bnfw) and I'll do my best to help.
162
 
198
 
199
  == Changelog ==
200
 
201
+ = 1.6.10 - 31st January 2018 =
202
+ * New: Faster customisation! You can now add the subject and message body from a default WordPress email to notifications. Just choose a default WordPress email and click the 'Insert Default Content' button next to the WYSIWYG editor when adding or editing a notification.
203
+ * New: If you want to allow other user roles to create or edit notifications, you can now grant them the 'bnfw' capability using a role editor.
204
+ * Added: A new notice will display at the top of all Notification screens explaining how you can improve email deliverability. You can dismiss this permanently at any time.
205
+ * Added: Inline help tips are now available when creating or editing a notification. Handy if you want to remember how something works but don't want to sift through the documentation.
206
+ * Added: Merged two pull request that came in via the GitHub clone of this plugin - ([#1](https://github.com/jackmcconnell/bnfw/pull/1) and [#2](https://github.com/jackmcconnell/bnfw/pull/2)).
207
+ * Added: For faster support, there is now a Priority Support menu item in the BNFW Notifications menu. Priority Support is available to customers with an active subscription to an add-on.
208
+ * Improved: All date related shortcodes now output in the date and time format set in Settings > General in the WordPress Admin.
209
+ * Improved: The list of available notifications has been re-ordered to make more sense.
210
+ * Improved: A warning will now show if the total user count in the Send To box is more than 200 users.
211
+ * Improved: Usage tracking will now only load on single sites and not on sites that are part of a Multisite network.
212
+ * Updated: Usage tracking SDK updated to the latest version.
213
+ * Fixed: Warnings were showing when posting a new comment on the front-end.
214
+
215
+ = 1.6.9 - 7th December 2017 =
216
  * Merry Christmas & Happy New Year!
217
  * New: Support for new [Profile Builder Add-on](https://betternotificationsforwp.com/downloads/profile-builder/).
218
  * Fixed: An issue where the YouTube video wasn't showing properly on the BNFW WP.org plugin page.
219
 
220
+ = 1.6.8 - 23rd November 2017 =
221
  * New: All shortcodes that are correctly registered within WordPress will now be stripped from the `[post_excerpt]` and `[post_content]` shortcodes so as to not cause layout or styling issues within your notifications.
222
  * Added: A minimum PHP version of 5.6 is now required to run BNFW.
223
  * Fixed: Compatibility with the [Reminders](https://betternotificationsforwp.com/downloads/update-reminder/) and [Conditional Notifications](https://betternotificationsforwp.com/downloads/conditional-notifications/) add-ons.
assets/css/bnfw.css CHANGED
@@ -16,6 +16,15 @@
16
  padding: 15px;
17
  }
18
 
 
 
 
 
 
 
 
 
 
19
  .manage-column.column-disabled {
20
  width: 8% !important;
21
  }
@@ -38,3 +47,132 @@
38
  ul.select2-selection__rendered {
39
  line-height: 1.3;
40
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  padding: 15px;
17
  }
18
 
19
+ #users-count-msg div {
20
+ background: #F79C2D;
21
+ padding: 15px;
22
+ }
23
+
24
+ #users-count-msg div p {
25
+ margin-top: 0;
26
+ }
27
+
28
  .manage-column.column-disabled {
29
  width: 8% !important;
30
  }
47
  ul.select2-selection__rendered {
48
  line-height: 1.3;
49
  }
50
+
51
+
52
+ /* Help Tip Positioning */
53
+ .bnfw-help-tip-container,
54
+ body.post-type-bnfw_notification #poststuff th,
55
+ body.post-type-bnfw_notification #poststuff td,
56
+ .bnfw-form th,
57
+ .bnfw-form td {
58
+ position: relative;
59
+ }
60
+
61
+ #bnfw-post-notification th[scope="row"],
62
+ .bnfw-form th[scope="row"] {
63
+ padding-right: 24px;
64
+ }
65
+
66
+ /* Inline Help Tips */
67
+ .bnfw-help-tip {
68
+ position: absolute;
69
+ top: 22px;
70
+ right: 8px;
71
+ background-color: #73daeb;
72
+ border-radius: 50%;
73
+ width: 15px;
74
+ height: 15px;
75
+ font-size: 12px;
76
+ line-height: 16px;
77
+ text-align: center;
78
+ cursor: default;
79
+ }
80
+
81
+ .bnfw-help-tip-container .bnfw-help-tip {
82
+ top: 8px;
83
+ }
84
+
85
+ /* Left Align Elements on Right of Screen */
86
+ #bnfw_submitdiv .bnfw-help-tip p {
87
+ left: auto;
88
+ right: -8px;
89
+ }
90
+
91
+ #bnfw_submitdiv .bnfw-help-tip p:before {
92
+ left: auto;
93
+ right: 10px;
94
+ top: -12px;
95
+ }
96
+
97
+
98
+ .bnfw-help-tip:hover:before {
99
+ color: #444;
100
+ }
101
+
102
+ .bnfw-help-tip:before {
103
+ content: '?';
104
+ font-weight: bold;
105
+ color: #fff;
106
+ }
107
+
108
+ .bnfw-help-tip:hover p {
109
+ display: block;
110
+ transform-origin: 100% 0%;
111
+ -webkit-animation: fadeIn 0.3s ease-in-out;
112
+ animation: fadeIn 0.3s ease-in-out;
113
+ }
114
+
115
+ .bnfw-help-tip p {
116
+ display: none;
117
+ position: absolute;
118
+ left: -8px;
119
+ z-index: 9991;
120
+ width: 300px;
121
+ box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.2);
122
+ border-radius: 3px;
123
+ background-color: #1E2021;
124
+ padding: 20px;
125
+ font-size: 13px;
126
+ line-height: 1.4;
127
+ color: #FFF;
128
+ text-align: left;
129
+ }
130
+
131
+ .bnfw-help-tip p:before {
132
+ position: absolute;
133
+ content: '';
134
+ width: 0;
135
+ height: 0;
136
+ border: 6px solid transparent;
137
+ border-bottom-color: #1E2021;
138
+ left: 10px;
139
+ top: -12px;
140
+ }
141
+
142
+ .bnfw-help-tip p:after {
143
+ content: '';
144
+ position: absolute;
145
+ top: -40px;
146
+ left: 0;
147
+ width: 100%;
148
+ height: 40px;
149
+ }
150
+
151
+ .bnfw-help-tip p a {
152
+ color: #73daeb;
153
+ }
154
+
155
+ .bnfw-help-tip p a:hover {
156
+ color: #a0e6f1;
157
+ }
158
+
159
+ .bnfw-help-tip + .wp-ui-text-highlight {
160
+ color: #1fbfd9;
161
+ }
162
+
163
+ @-webkit-keyframes fadeIn {
164
+ 0% {
165
+ opacity: 0;
166
+ transform: scale(0.6);
167
+ }
168
+
169
+ 100% {
170
+ opacity:1 00%;
171
+ transform: scale(1);
172
+ }
173
+ }
174
+
175
+ @keyframes fadeIn {
176
+ 0% { opacity: 0; }
177
+ 100% { opacity: 100%; }
178
+ }
assets/js/bnfw.js CHANGED
@@ -82,6 +82,21 @@ jQuery(document).ready(function($) {
82
  }
83
 
84
  init();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  $('#notification').on('change', function() {
86
  var $this = $(this),
87
  notification = $this.val();
@@ -127,7 +142,7 @@ jQuery(document).ready(function($) {
127
  // Validate before saving notification
128
  $( '#publish' ).click(function() {
129
  if ( $('#users').is(':visible') ) {
130
- if ( null === $('#users-select').val() ) {
131
  $('#bnfw_error').remove();
132
  $('.wrap h1').after('<div class="error" id="bnfw_error"><p>' + BNFW.empty_user + '</p></div>');
133
  return false;
@@ -203,4 +218,108 @@ jQuery(document).ready(function($) {
203
 
204
  $(this).attr( 'href', 'https://betternotificationsforwp.com/documentation/notifications/shortcodes/?notification=' + notification_slug + '&utm_source=WP%20Admin%20Notification%20Editor%20-%20"Shortcode%20Help"&utm_medium=referral' );
205
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
206
  });
82
  }
83
 
84
  init();
85
+
86
+ /**
87
+ * Show a warning message if a notification is configured for more than 200 emails.
88
+ */
89
+ $( '#users-select' ).on( 'change', function () {
90
+ var emailCount = $( '#users-select' ).find( ':selected' ).length,
91
+ $msg = $( '#users-count-msg' );
92
+
93
+ if ( emailCount > 200 ) {
94
+ $msg.show();
95
+ } else {
96
+ $msg.hide();
97
+ }
98
+ } );
99
+
100
  $('#notification').on('change', function() {
101
  var $this = $(this),
102
  notification = $this.val();
142
  // Validate before saving notification
143
  $( '#publish' ).click(function() {
144
  if ( $('#users').is(':visible') ) {
145
+ if ( null === $(BNFW.validation_element).val() ) {
146
  $('#bnfw_error').remove();
147
  $('.wrap h1').after('<div class="error" id="bnfw_error"><p>' + BNFW.empty_user + '</p></div>');
148
  return false;
218
 
219
  $(this).attr( 'href', 'https://betternotificationsforwp.com/documentation/notifications/shortcodes/?notification=' + notification_slug + '&utm_source=WP%20Admin%20Notification%20Editor%20-%20"Shortcode%20Help"&utm_medium=referral' );
220
  });
221
+
222
+ /**
223
+ * Insert Default Message for notification.
224
+ */
225
+ $( '#insert-default-msg' ).on( 'click', function() {
226
+ var notification = $( '#notification' ).val(),
227
+ subject = '',
228
+ body = '';
229
+
230
+ switch ( notification ) {
231
+ case 'new-comment':
232
+ case 'new-trackback':
233
+ case 'new-pingback':
234
+ case 'reply-comment':
235
+ subject = '[[global_site_title]] Comment: "[post_title]"';
236
+ body = 'New comment on your post "[post_title]"<br>' +
237
+ 'Author: [comment_author] (IP address: [comment_author_IP]) <br>' +
238
+ 'Email: [comment_author_email] <br>' +
239
+ 'URL: [comment_author_url] <br>' +
240
+ 'Comment: <br> ' +
241
+ '[comment_content] <br>' +
242
+ '<br>' +
243
+ 'You can see all comments on this post here: <br>' +
244
+ '[permalink]#comments';
245
+
246
+ break;
247
+
248
+ case 'admin-user':
249
+ subject = '[[global_site_title]] New User Registration';
250
+ body = 'New user registration on your site [global_site_title]: <br>' +
251
+ 'Username: [user_login] <br>' +
252
+ 'E-mail: [user_email]';
253
+
254
+ break;
255
+
256
+ case 'admin-password-changed':
257
+ subject = '[[global_site_title]] Password Changed';
258
+ body = 'Password changed for user: [user_login]: <br>';
259
+
260
+ break;
261
+
262
+ case 'user-password':
263
+ subject = '[[global_site_title]] Password Reset';
264
+ body = 'Someone has requested a password reset for the following account: <br>' +
265
+ 'Site Name: [global_site_title] <br>' +
266
+ 'Username: [user_login] <br>' +
267
+ 'If this was a mistake, just ignore this email and nothing will happen. <br>' +
268
+ 'To reset your password, visit the following address: [password_reset_link]';
269
+
270
+ break;
271
+
272
+ case 'password-changed':
273
+ subject = '[[global_site_title]] Notice of Password Change';
274
+ body = 'Hi [user_nicename], <br>' +
275
+ '<br>' +
276
+ 'This notice confirms that your password was changed on [global_site_title].' +
277
+ '<br>' +
278
+ 'If you did not change your password, please contact the Site Administrator at [admin_email] <br>' +
279
+ '<br>' +
280
+ 'This email has been sent to [global_user_email]' +
281
+ '<br>' +
282
+ 'Regards, <br>' +
283
+ 'All at [global_site_title] <br>' +
284
+ '[global_site_url]';
285
+ break;
286
+
287
+ case 'email-changed':
288
+ subject = '[[global_site_title]] Notice of Email Change';
289
+ body = 'Hi [user_nicename], <br>' +
290
+ '<br>' +
291
+ 'This notice confirms that your email address was changed to [user_email].' +
292
+ '<br>' +
293
+ 'If you did not change your email, please contact the Site Administrator at [admin_email] <br>' +
294
+ '<br>' +
295
+ 'This email has been sent to [global_user_email]' +
296
+ '<br>' +
297
+ 'Regards, <br>' +
298
+ 'All at [global_site_title] <br>' +
299
+ '[global_site_url]';
300
+ break;
301
+
302
+ case 'new-user':
303
+ subject = '[[global_site_title]] Your username and password info';
304
+ body = 'Username: [user_login] <br>' +
305
+ 'To set your password, visit the following address: [password_url]';
306
+
307
+ break;
308
+ default:
309
+ alert( "This is a new notification that is not available in WordPress by default and has been added by Better Notifications for WordPress. As such, it doesn't have any default content." );
310
+ break;
311
+ }
312
+
313
+ if ( subject !== '' ) {
314
+ $( '#subject' ).val( subject );
315
+ }
316
+
317
+ if ( body !== '' ) {
318
+ if ( tinyMCE && tinyMCE.editors && tinyMCE.editors['notification_message'] ) {
319
+ tinyMCE.editors['notification_message'].selection.setContent( '<div>' + body + '</div>' );
320
+ }
321
+ }
322
+
323
+ return false;
324
+ } );
325
  });
bnfw.php CHANGED
@@ -3,7 +3,7 @@
3
  * Plugin Name: Better Notifications for WordPress
4
  * Plugin URI: https://wordpress.org/plugins/bnfw/
5
  * Description: Supercharge your WordPress notifications using a WYSIWYG editor and shortcodes. Default and new notifications available. Add more power with Add-ons.
6
- * Version: 1.6.9
7
  * Author: Made with Fuel
8
  * Author URI: https://betternotificationsforwp.com/
9
  * Author Email: hello@betternotificationsforwp.com
@@ -14,7 +14,7 @@
14
  */
15
 
16
  /**
17
- * Copyright © 2017 Made with Fuel Ltd. (hello@betternotificationsforwp.com)
18
  * This program is free software; you can redistribute it and/or modify
19
  * it under the terms of the GNU General Public License, version 2, as
20
  * published by the Free Software Foundation.
@@ -27,8 +27,6 @@
27
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28
  */
29
 
30
- require_once 'includes/freemius.php';
31
-
32
  class BNFW {
33
 
34
  /**
@@ -93,6 +91,9 @@ class BNFW {
93
  if ( ! class_exists( 'EDD_SL_Plugin_Updater' ) ) {
94
  require_once 'includes/libraries/EDD_SL_Plugin_Updater.php';
95
  }
 
 
 
96
  require_once 'includes/license/class-bnfw-license.php';
97
  require_once 'includes/license/class-bnfw-license-setting.php';
98
 
@@ -124,6 +125,9 @@ class BNFW {
124
 
125
  register_activation_hook( __FILE__, array( $this, 'activate' ) );
126
 
 
 
 
127
  add_action( 'draft_to_private' , array( $this, 'private_post' ) );
128
  add_action( 'future_to_private' , array( $this, 'private_post' ) );
129
  add_action( 'pending_to_private' , array( $this, 'private_post' ) );
@@ -169,6 +173,17 @@ class BNFW {
169
  add_action( 'shutdown' , array( $this, 'on_shutdown' ) );
170
  }
171
 
 
 
 
 
 
 
 
 
 
 
 
172
  /**
173
  * Setup hooks for custom post types.
174
  *
@@ -751,6 +766,10 @@ class BNFW {
751
  }
752
  }
753
 
 
 
 
 
754
  /* ------------------------------------------------------------------------ *
755
  * Fire up the plugin
756
  * ------------------------------------------------------------------------ */
3
  * Plugin Name: Better Notifications for WordPress
4
  * Plugin URI: https://wordpress.org/plugins/bnfw/
5
  * Description: Supercharge your WordPress notifications using a WYSIWYG editor and shortcodes. Default and new notifications available. Add more power with Add-ons.
6
+ * Version: 1.6.10
7
  * Author: Made with Fuel
8
  * Author URI: https://betternotificationsforwp.com/
9
  * Author Email: hello@betternotificationsforwp.com
14
  */
15
 
16
  /**
17
+ * Copyright © 2018 Made with Fuel Ltd. (hello@betternotificationsforwp.com)
18
  * This program is free software; you can redistribute it and/or modify
19
  * it under the terms of the GNU General Public License, version 2, as
20
  * published by the Free Software Foundation.
27
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28
  */
29
 
 
 
30
  class BNFW {
31
 
32
  /**
91
  if ( ! class_exists( 'EDD_SL_Plugin_Updater' ) ) {
92
  require_once 'includes/libraries/EDD_SL_Plugin_Updater.php';
93
  }
94
+
95
+ require_once 'vendor/persist-admin-notices-dismissal/persist-admin-notices-dismissal.php';
96
+
97
  require_once 'includes/license/class-bnfw-license.php';
98
  require_once 'includes/license/class-bnfw-license-setting.php';
99
 
125
 
126
  register_activation_hook( __FILE__, array( $this, 'activate' ) );
127
 
128
+ add_action( 'admin_init', array( 'PAnD', 'init' ) );
129
+ add_action( 'admin_init', array( $this, 'add_capability_to_admin' ) );
130
+
131
  add_action( 'draft_to_private' , array( $this, 'private_post' ) );
132
  add_action( 'future_to_private' , array( $this, 'private_post' ) );
133
  add_action( 'pending_to_private' , array( $this, 'private_post' ) );
173
  add_action( 'shutdown' , array( $this, 'on_shutdown' ) );
174
  }
175
 
176
+ /**
177
+ * Add 'bnfw' capability to admin.
178
+ */
179
+ public function add_capability_to_admin() {
180
+ $admins = get_role( 'administrator' );
181
+
182
+ if ( ! $admins->has_cap( 'bnfw' ) ) {
183
+ $admins->add_cap( 'bnfw' );
184
+ }
185
+ }
186
+
187
  /**
188
  * Setup hooks for custom post types.
189
  *
766
  }
767
  }
768
 
769
+ if ( ! is_multisite() ) {
770
+ require_once 'includes/freemius.php';
771
+ }
772
+
773
  /* ------------------------------------------------------------------------ *
774
  * Fire up the plugin
775
  * ------------------------------------------------------------------------ */
freemius/LICENSE.txt CHANGED
@@ -1,281 +1,622 @@
1
- GNU GENERAL PUBLIC LICENSE
2
- Version 2, June 1991
3
 
4
- Copyright (C) 1989, 1991 Free Software Foundation, Inc., <http://fsf.org/>
5
- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6
  Everyone is permitted to copy and distribute verbatim copies
7
  of this license document, but changing it is not allowed.
8
 
9
  Preamble
10
 
11
- The licenses for most software are designed to take away your
12
- freedom to share and change it. By contrast, the GNU General Public
13
- License is intended to guarantee your freedom to share and change free
14
- software--to make sure the software is free for all its users. This
15
- General Public License applies to most of the Free Software
16
- Foundation's software and to any other program whose authors commit to
17
- using it. (Some other Free Software Foundation software is covered by
18
- the GNU Lesser General Public License instead.) You can apply it to
 
 
19
  your programs, too.
20
 
21
  When we speak of free software, we are referring to freedom, not
22
  price. Our General Public Licenses are designed to make sure that you
23
  have the freedom to distribute copies of free software (and charge for
24
- this service if you wish), that you receive source code or can get it
25
- if you want it, that you can change the software or use pieces of it
26
- in new free programs; and that you know you can do these things.
27
 
28
- To protect your rights, we need to make restrictions that forbid
29
- anyone to deny you these rights or to ask you to surrender the rights.
30
- These restrictions translate to certain responsibilities for you if you
31
- distribute copies of the software, or if you modify it.
32
 
33
  For example, if you distribute copies of such a program, whether
34
- gratis or for a fee, you must give the recipients all the rights that
35
- you have. You must make sure that they, too, receive or can get the
36
- source code. And you must show them these terms so they know their
37
- rights.
38
-
39
- We protect your rights with two steps: (1) copyright the software, and
40
- (2) offer you this license which gives you legal permission to copy,
41
- distribute and/or modify the software.
42
-
43
- Also, for each author's protection and ours, we want to make certain
44
- that everyone understands that there is no warranty for this free
45
- software. If the software is modified by someone else and passed on, we
46
- want its recipients to know that what they have is not the original, so
47
- that any problems introduced by others will not reflect on the original
48
- authors' reputations.
49
-
50
- Finally, any free program is threatened constantly by software
51
- patents. We wish to avoid the danger that redistributors of a free
52
- program will individually obtain patent licenses, in effect making the
53
- program proprietary. To prevent this, we have made it clear that any
54
- patent must be licensed for everyone's free use or not licensed at all.
 
 
 
 
 
 
 
 
 
 
 
55
 
56
  The precise terms and conditions for copying, distribution and
57
  modification follow.
58
 
59
- GNU GENERAL PUBLIC LICENSE
60
- TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61
-
62
- 0. This License applies to any program or other work which contains
63
- a notice placed by the copyright holder saying it may be distributed
64
- under the terms of this General Public License. The "Program", below,
65
- refers to any such program or work, and a "work based on the Program"
66
- means either the Program or any derivative work under copyright law:
67
- that is to say, a work containing the Program or a portion of it,
68
- either verbatim or with modifications and/or translated into another
69
- language. (Hereinafter, translation is included without limitation in
70
- the term "modification".) Each licensee is addressed as "you".
71
-
72
- Activities other than copying, distribution and modification are not
73
- covered by this License; they are outside its scope. The act of
74
- running the Program is not restricted, and the output from the Program
75
- is covered only if its contents constitute a work based on the
76
- Program (independent of having been made by running the Program).
77
- Whether that is true depends on what the Program does.
78
-
79
- 1. You may copy and distribute verbatim copies of the Program's
80
- source code as you receive it, in any medium, provided that you
81
- conspicuously and appropriately publish on each copy an appropriate
82
- copyright notice and disclaimer of warranty; keep intact all the
83
- notices that refer to this License and to the absence of any warranty;
84
- and give any other recipients of the Program a copy of this License
85
- along with the Program.
86
-
87
- You may charge a fee for the physical act of transferring a copy, and
88
- you may at your option offer warranty protection in exchange for a fee.
89
-
90
- 2. You may modify your copy or copies of the Program or any portion
91
- of it, thus forming a work based on the Program, and copy and
92
- distribute such modifications or work under the terms of Section 1
93
- above, provided that you also meet all of these conditions:
94
-
95
- a) You must cause the modified files to carry prominent notices
96
- stating that you changed the files and the date of any change.
97
-
98
- b) You must cause any work that you distribute or publish, that in
99
- whole or in part contains or is derived from the Program or any
100
- part thereof, to be licensed as a whole at no charge to all third
101
- parties under the terms of this License.
102
-
103
- c) If the modified program normally reads commands interactively
104
- when run, you must cause it, when started running for such
105
- interactive use in the most ordinary way, to print or display an
106
- announcement including an appropriate copyright notice and a
107
- notice that there is no warranty (or else, saying that you provide
108
- a warranty) and that users may redistribute the program under
109
- these conditions, and telling the user how to view a copy of this
110
- License. (Exception: if the Program itself is interactive but
111
- does not normally print such an announcement, your work based on
112
- the Program is not required to print an announcement.)
113
-
114
- These requirements apply to the modified work as a whole. If
115
- identifiable sections of that work are not derived from the Program,
116
- and can be reasonably considered independent and separate works in
117
- themselves, then this License, and its terms, do not apply to those
118
- sections when you distribute them as separate works. But when you
119
- distribute the same sections as part of a whole which is a work based
120
- on the Program, the distribution of the whole must be on the terms of
121
- this License, whose permissions for other licensees extend to the
122
- entire whole, and thus to each and every part regardless of who wrote it.
123
-
124
- Thus, it is not the intent of this section to claim rights or contest
125
- your rights to work written entirely by you; rather, the intent is to
126
- exercise the right to control the distribution of derivative or
127
- collective works based on the Program.
128
-
129
- In addition, mere aggregation of another work not based on the Program
130
- with the Program (or with a work based on the Program) on a volume of
131
- a storage or distribution medium does not bring the other work under
132
- the scope of this License.
133
-
134
- 3. You may copy and distribute the Program (or a work based on it,
135
- under Section 2) in object code or executable form under the terms of
136
- Sections 1 and 2 above provided that you also do one of the following:
137
-
138
- a) Accompany it with the complete corresponding machine-readable
139
- source code, which must be distributed under the terms of Sections
140
- 1 and 2 above on a medium customarily used for software interchange; or,
141
-
142
- b) Accompany it with a written offer, valid for at least three
143
- years, to give any third party, for a charge no more than your
144
- cost of physically performing source distribution, a complete
145
- machine-readable copy of the corresponding source code, to be
146
- distributed under the terms of Sections 1 and 2 above on a medium
147
- customarily used for software interchange; or,
148
-
149
- c) Accompany it with the information you received as to the offer
150
- to distribute corresponding source code. (This alternative is
151
- allowed only for noncommercial distribution and only if you
152
- received the program in object code or executable form with such
153
- an offer, in accord with Subsection b above.)
154
-
155
- The source code for a work means the preferred form of the work for
156
- making modifications to it. For an executable work, complete source
157
- code means all the source code for all modules it contains, plus any
158
- associated interface definition files, plus the scripts used to
159
- control compilation and installation of the executable. However, as a
160
- special exception, the source code distributed need not include
161
- anything that is normally distributed (in either source or binary
162
- form) with the major components (compiler, kernel, and so on) of the
163
- operating system on which the executable runs, unless that component
164
- itself accompanies the executable.
165
-
166
- If distribution of executable or object code is made by offering
167
- access to copy from a designated place, then offering equivalent
168
- access to copy the source code from the same place counts as
169
- distribution of the source code, even though third parties are not
170
- compelled to copy the source along with the object code.
171
-
172
- 4. You may not copy, modify, sublicense, or distribute the Program
173
- except as expressly provided under this License. Any attempt
174
- otherwise to copy, modify, sublicense or distribute the Program is
175
- void, and will automatically terminate your rights under this License.
176
- However, parties who have received copies, or rights, from you under
177
- this License will not have their licenses terminated so long as such
178
- parties remain in full compliance.
179
-
180
- 5. You are not required to accept this License, since you have not
181
- signed it. However, nothing else grants you permission to modify or
182
- distribute the Program or its derivative works. These actions are
183
- prohibited by law if you do not accept this License. Therefore, by
184
- modifying or distributing the Program (or any work based on the
185
- Program), you indicate your acceptance of this License to do so, and
186
- all its terms and conditions for copying, distributing or modifying
187
- the Program or works based on it.
188
-
189
- 6. Each time you redistribute the Program (or any work based on the
190
- Program), the recipient automatically receives a license from the
191
- original licensor to copy, distribute or modify the Program subject to
192
- these terms and conditions. You may not impose any further
193
- restrictions on the recipients' exercise of the rights granted herein.
194
- You are not responsible for enforcing compliance by third parties to
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
195
  this License.
196
 
197
- 7. If, as a consequence of a court judgment or allegation of patent
198
- infringement or for any other reason (not limited to patent issues),
199
- conditions are imposed on you (whether by court order, agreement or
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
200
  otherwise) that contradict the conditions of this License, they do not
201
- excuse you from the conditions of this License. If you cannot
202
- distribute so as to satisfy simultaneously your obligations under this
203
- License and any other pertinent obligations, then as a consequence you
204
- may not distribute the Program at all. For example, if a patent
205
- license would not permit royalty-free redistribution of the Program by
206
- all those who receive copies directly or indirectly through you, then
207
- the only way you could satisfy both it and this License would be to
208
- refrain entirely from distribution of the Program.
209
-
210
- If any portion of this section is held invalid or unenforceable under
211
- any particular circumstance, the balance of the section is intended to
212
- apply and the section as a whole is intended to apply in other
213
- circumstances.
214
-
215
- It is not the purpose of this section to induce you to infringe any
216
- patents or other property right claims or to contest validity of any
217
- such claims; this section has the sole purpose of protecting the
218
- integrity of the free software distribution system, which is
219
- implemented by public license practices. Many people have made
220
- generous contributions to the wide range of software distributed
221
- through that system in reliance on consistent application of that
222
- system; it is up to the author/donor to decide if he or she is willing
223
- to distribute software through any other system and a licensee cannot
224
- impose that choice.
225
-
226
- This section is intended to make thoroughly clear what is believed to
227
- be a consequence of the rest of this License.
228
-
229
- 8. If the distribution and/or use of the Program is restricted in
230
- certain countries either by patents or by copyrighted interfaces, the
231
- original copyright holder who places the Program under this License
232
- may add an explicit geographical distribution limitation excluding
233
- those countries, so that distribution is permitted only in or among
234
- countries not thus excluded. In such case, this License incorporates
235
- the limitation as if written in the body of this License.
236
-
237
- 9. The Free Software Foundation may publish revised and/or new versions
238
- of the General Public License from time to time. Such new versions will
239
  be similar in spirit to the present version, but may differ in detail to
240
  address new problems or concerns.
241
 
242
- Each version is given a distinguishing version number. If the Program
243
- specifies a version number of this License which applies to it and "any
244
- later version", you have the option of following the terms and conditions
245
- either of that version or of any later version published by the Free
246
- Software Foundation. If the Program does not specify a version number of
247
- this License, you may choose any version ever published by the Free Software
248
- Foundation.
249
-
250
- 10. If you wish to incorporate parts of the Program into other free
251
- programs whose distribution conditions are different, write to the author
252
- to ask for permission. For software which is copyrighted by the Free
253
- Software Foundation, write to the Free Software Foundation; we sometimes
254
- make exceptions for this. Our decision will be guided by the two goals
255
- of preserving the free status of all derivatives of our free software and
256
- of promoting the sharing and reuse of software generally.
257
-
258
- NO WARRANTY
259
-
260
- 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261
- FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
262
- OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263
- PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264
- OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265
- MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
266
- TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
267
- PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268
- REPAIR OR CORRECTION.
269
-
270
- 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271
- WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272
- REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273
- INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274
- OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275
- TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276
- YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277
- PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278
- POSSIBILITY OF SUCH DAMAGES.
 
 
 
 
 
 
 
 
 
 
 
 
 
279
 
280
  END OF TERMS AND CONDITIONS
281
 
@@ -287,15 +628,15 @@ free software which everyone can redistribute and change under these terms.
287
 
288
  To do so, attach the following notices to the program. It is safest
289
  to attach them to the start of each source file to most effectively
290
- convey the exclusion of warranty; and each file should have at least
291
  the "copyright" line and a pointer to where the full notice is found.
292
 
293
- {description}
294
- Copyright (C) {year} {fullname}
295
 
296
- This program is free software; you can redistribute it and/or modify
297
  it under the terms of the GNU General Public License as published by
298
- the Free Software Foundation; either version 2 of the License, or
299
  (at your option) any later version.
300
 
301
  This program is distributed in the hope that it will be useful,
@@ -303,38 +644,31 @@ the "copyright" line and a pointer to where the full notice is found.
303
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
304
  GNU General Public License for more details.
305
 
306
- You should have received a copy of the GNU General Public License along
307
- with this program; if not, write to the Free Software Foundation, Inc.,
308
- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
309
 
310
  Also add information on how to contact you by electronic and paper mail.
311
 
312
- If the program is interactive, make it output a short notice like this
313
- when it starts in an interactive mode:
314
 
315
- Gnomovision version 69, Copyright (C) year name of author
316
- Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
317
  This is free software, and you are welcome to redistribute it
318
  under certain conditions; type `show c' for details.
319
 
320
  The hypothetical commands `show w' and `show c' should show the appropriate
321
- parts of the General Public License. Of course, the commands you use may
322
- be called something other than `show w' and `show c'; they could even be
323
- mouse-clicks or menu items--whatever suits your program.
324
-
325
- You should also get your employer (if you work as a programmer) or your
326
- school, if any, to sign a "copyright disclaimer" for the program, if
327
- necessary. Here is a sample; alter the names:
328
-
329
- Yoyodyne, Inc., hereby disclaims all copyright interest in the program
330
- `Gnomovision' (which makes passes at compilers) written by James Hacker.
331
-
332
- {signature of Ty Coon}, 1 April 1989
333
- Ty Coon, President of Vice
334
-
335
- This General Public License does not permit incorporating your program into
336
- proprietary programs. If your program is a subroutine library, you may
337
- consider it more useful to permit linking proprietary applications with the
338
- library. If this is what you want to do, use the GNU Lesser General
339
- Public License instead of this License.
340
-
1
+ GNU GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
 
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
 
5
  Everyone is permitted to copy and distribute verbatim copies
6
  of this license document, but changing it is not allowed.
7
 
8
  Preamble
9
 
10
+ The GNU General Public License is a free, copyleft license for
11
+ software and other kinds of works.
12
+
13
+ The licenses for most software and other practical works are designed
14
+ to take away your freedom to share and change the works. By contrast,
15
+ the GNU General Public License is intended to guarantee your freedom to
16
+ share and change all versions of a program--to make sure it remains free
17
+ software for all its users. We, the Free Software Foundation, use the
18
+ GNU General Public License for most of our software; it applies also to
19
+ any other work released this way by its authors. You can apply it to
20
  your programs, too.
21
 
22
  When we speak of free software, we are referring to freedom, not
23
  price. Our General Public Licenses are designed to make sure that you
24
  have the freedom to distribute copies of free software (and charge for
25
+ them if you wish), that you receive source code or can get it if you
26
+ want it, that you can change the software or use pieces of it in new
27
+ free programs, and that you know you can do these things.
28
 
29
+ To protect your rights, we need to prevent others from denying you
30
+ these rights or asking you to surrender the rights. Therefore, you have
31
+ certain responsibilities if you distribute copies of the software, or if
32
+ you modify it: responsibilities to respect the freedom of others.
33
 
34
  For example, if you distribute copies of such a program, whether
35
+ gratis or for a fee, you must pass on to the recipients the same
36
+ freedoms that you received. You must make sure that they, too, receive
37
+ or can get the source code. And you must show them these terms so they
38
+ know their rights.
39
+
40
+ Developers that use the GNU GPL protect your rights with two steps:
41
+ (1) assert copyright on the software, and (2) offer you this License
42
+ giving you legal permission to copy, distribute and/or modify it.
43
+
44
+ For the developers' and authors' protection, the GPL clearly explains
45
+ that there is no warranty for this free software. For both users' and
46
+ authors' sake, the GPL requires that modified versions be marked as
47
+ changed, so that their problems will not be attributed erroneously to
48
+ authors of previous versions.
49
+
50
+ Some devices are designed to deny users access to install or run
51
+ modified versions of the software inside them, although the manufacturer
52
+ can do so. This is fundamentally incompatible with the aim of
53
+ protecting users' freedom to change the software. The systematic
54
+ pattern of such abuse occurs in the area of products for individuals to
55
+ use, which is precisely where it is most unacceptable. Therefore, we
56
+ have designed this version of the GPL to prohibit the practice for those
57
+ products. If such problems arise substantially in other domains, we
58
+ stand ready to extend this provision to those domains in future versions
59
+ of the GPL, as needed to protect the freedom of users.
60
+
61
+ Finally, every program is threatened constantly by software patents.
62
+ States should not allow patents to restrict development and use of
63
+ software on general-purpose computers, but in those that do, we wish to
64
+ avoid the special danger that patents applied to a free program could
65
+ make it effectively proprietary. To prevent this, the GPL assures that
66
+ patents cannot be used to render the program non-free.
67
 
68
  The precise terms and conditions for copying, distribution and
69
  modification follow.
70
 
71
+ TERMS AND CONDITIONS
72
+
73
+ 0. Definitions.
74
+
75
+ "This License" refers to version 3 of the GNU General Public License.
76
+
77
+ "Copyright" also means copyright-like laws that apply to other kinds of
78
+ works, such as semiconductor masks.
79
+
80
+ "The Program" refers to any copyrightable work licensed under this
81
+ License. Each licensee is addressed as "you". "Licensees" and
82
+ "recipients" may be individuals or organizations.
83
+
84
+ To "modify" a work means to copy from or adapt all or part of the work
85
+ in a fashion requiring copyright permission, other than the making of an
86
+ exact copy. The resulting work is called a "modified version" of the
87
+ earlier work or a work "based on" the earlier work.
88
+
89
+ A "covered work" means either the unmodified Program or a work based
90
+ on the Program.
91
+
92
+ To "propagate" a work means to do anything with it that, without
93
+ permission, would make you directly or secondarily liable for
94
+ infringement under applicable copyright law, except executing it on a
95
+ computer or modifying a private copy. Propagation includes copying,
96
+ distribution (with or without modification), making available to the
97
+ public, and in some countries other activities as well.
98
+
99
+ To "convey" a work means any kind of propagation that enables other
100
+ parties to make or receive copies. Mere interaction with a user through
101
+ a computer network, with no transfer of a copy, is not conveying.
102
+
103
+ An interactive user interface displays "Appropriate Legal Notices"
104
+ to the extent that it includes a convenient and prominently visible
105
+ feature that (1) displays an appropriate copyright notice, and (2)
106
+ tells the user that there is no warranty for the work (except to the
107
+ extent that warranties are provided), that licensees may convey the
108
+ work under this License, and how to view a copy of this License. If
109
+ the interface presents a list of user commands or options, such as a
110
+ menu, a prominent item in the list meets this criterion.
111
+
112
+ 1. Source Code.
113
+
114
+ The "source code" for a work means the preferred form of the work
115
+ for making modifications to it. "Object code" means any non-source
116
+ form of a work.
117
+
118
+ A "Standard Interface" means an interface that either is an official
119
+ standard defined by a recognized standards body, or, in the case of
120
+ interfaces specified for a particular programming language, one that
121
+ is widely used among developers working in that language.
122
+
123
+ The "System Libraries" of an executable work include anything, other
124
+ than the work as a whole, that (a) is included in the normal form of
125
+ packaging a Major Component, but which is not part of that Major
126
+ Component, and (b) serves only to enable use of the work with that
127
+ Major Component, or to implement a Standard Interface for which an
128
+ implementation is available to the public in source code form. A
129
+ "Major Component", in this context, means a major essential component
130
+ (kernel, window system, and so on) of the specific operating system
131
+ (if any) on which the executable work runs, or a compiler used to
132
+ produce the work, or an object code interpreter used to run it.
133
+
134
+ The "Corresponding Source" for a work in object code form means all
135
+ the source code needed to generate, install, and (for an executable
136
+ work) run the object code and to modify the work, including scripts to
137
+ control those activities. However, it does not include the work's
138
+ System Libraries, or general-purpose tools or generally available free
139
+ programs which are used unmodified in performing those activities but
140
+ which are not part of the work. For example, Corresponding Source
141
+ includes interface definition files associated with source files for
142
+ the work, and the source code for shared libraries and dynamically
143
+ linked subprograms that the work is specifically designed to require,
144
+ such as by intimate data communication or control flow between those
145
+ subprograms and other parts of the work.
146
+
147
+ The Corresponding Source need not include anything that users
148
+ can regenerate automatically from other parts of the Corresponding
149
+ Source.
150
+
151
+ The Corresponding Source for a work in source code form is that
152
+ same work.
153
+
154
+ 2. Basic Permissions.
155
+
156
+ All rights granted under this License are granted for the term of
157
+ copyright on the Program, and are irrevocable provided the stated
158
+ conditions are met. This License explicitly affirms your unlimited
159
+ permission to run the unmodified Program. The output from running a
160
+ covered work is covered by this License only if the output, given its
161
+ content, constitutes a covered work. This License acknowledges your
162
+ rights of fair use or other equivalent, as provided by copyright law.
163
+
164
+ You may make, run and propagate covered works that you do not
165
+ convey, without conditions so long as your license otherwise remains
166
+ in force. You may convey covered works to others for the sole purpose
167
+ of having them make modifications exclusively for you, or provide you
168
+ with facilities for running those works, provided that you comply with
169
+ the terms of this License in conveying all material for which you do
170
+ not control copyright. Those thus making or running the covered works
171
+ for you must do so exclusively on your behalf, under your direction
172
+ and control, on terms that prohibit them from making any copies of
173
+ your copyrighted material outside their relationship with you.
174
+
175
+ Conveying under any other circumstances is permitted solely under
176
+ the conditions stated below. Sublicensing is not allowed; section 10
177
+ makes it unnecessary.
178
+
179
+ 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180
+
181
+ No covered work shall be deemed part of an effective technological
182
+ measure under any applicable law fulfilling obligations under article
183
+ 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184
+ similar laws prohibiting or restricting circumvention of such
185
+ measures.
186
+
187
+ When you convey a covered work, you waive any legal power to forbid
188
+ circumvention of technological measures to the extent such circumvention
189
+ is effected by exercising rights under this License with respect to
190
+ the covered work, and you disclaim any intention to limit operation or
191
+ modification of the work as a means of enforcing, against the work's
192
+ users, your or third parties' legal rights to forbid circumvention of
193
+ technological measures.
194
+
195
+ 4. Conveying Verbatim Copies.
196
+
197
+ You may convey verbatim copies of the Program's source code as you
198
+ receive it, in any medium, provided that you conspicuously and
199
+ appropriately publish on each copy an appropriate copyright notice;
200
+ keep intact all notices stating that this License and any
201
+ non-permissive terms added in accord with section 7 apply to the code;
202
+ keep intact all notices of the absence of any warranty; and give all
203
+ recipients a copy of this License along with the Program.
204
+
205
+ You may charge any price or no price for each copy that you convey,
206
+ and you may offer support or warranty protection for a fee.
207
+
208
+ 5. Conveying Modified Source Versions.
209
+
210
+ You may convey a work based on the Program, or the modifications to
211
+ produce it from the Program, in the form of source code under the
212
+ terms of section 4, provided that you also meet all of these conditions:
213
+
214
+ a) The work must carry prominent notices stating that you modified
215
+ it, and giving a relevant date.
216
+
217
+ b) The work must carry prominent notices stating that it is
218
+ released under this License and any conditions added under section
219
+ 7. This requirement modifies the requirement in section 4 to
220
+ "keep intact all notices".
221
+
222
+ c) You must license the entire work, as a whole, under this
223
+ License to anyone who comes into possession of a copy. This
224
+ License will therefore apply, along with any applicable section 7
225
+ additional terms, to the whole of the work, and all its parts,
226
+ regardless of how they are packaged. This License gives no
227
+ permission to license the work in any other way, but it does not
228
+ invalidate such permission if you have separately received it.
229
+
230
+ d) If the work has interactive user interfaces, each must display
231
+ Appropriate Legal Notices; however, if the Program has interactive
232
+ interfaces that do not display Appropriate Legal Notices, your
233
+ work need not make them do so.
234
+
235
+ A compilation of a covered work with other separate and independent
236
+ works, which are not by their nature extensions of the covered work,
237
+ and which are not combined with it such as to form a larger program,
238
+ in or on a volume of a storage or distribution medium, is called an
239
+ "aggregate" if the compilation and its resulting copyright are not
240
+ used to limit the access or legal rights of the compilation's users
241
+ beyond what the individual works permit. Inclusion of a covered work
242
+ in an aggregate does not cause this License to apply to the other
243
+ parts of the aggregate.
244
+
245
+ 6. Conveying Non-Source Forms.
246
+
247
+ You may convey a covered work in object code form under the terms
248
+ of sections 4 and 5, provided that you also convey the
249
+ machine-readable Corresponding Source under the terms of this License,
250
+ in one of these ways:
251
+
252
+ a) Convey the object code in, or embodied in, a physical product
253
+ (including a physical distribution medium), accompanied by the
254
+ Corresponding Source fixed on a durable physical medium
255
+ customarily used for software interchange.
256
+
257
+ b) Convey the object code in, or embodied in, a physical product
258
+ (including a physical distribution medium), accompanied by a
259
+ written offer, valid for at least three years and valid for as
260
+ long as you offer spare parts or customer support for that product
261
+ model, to give anyone who possesses the object code either (1) a
262
+ copy of the Corresponding Source for all the software in the
263
+ product that is covered by this License, on a durable physical
264
+ medium customarily used for software interchange, for a price no
265
+ more than your reasonable cost of physically performing this
266
+ conveying of source, or (2) access to copy the
267
+ Corresponding Source from a network server at no charge.
268
+
269
+ c) Convey individual copies of the object code with a copy of the
270
+ written offer to provide the Corresponding Source. This
271
+ alternative is allowed only occasionally and noncommercially, and
272
+ only if you received the object code with such an offer, in accord
273
+ with subsection 6b.
274
+
275
+ d) Convey the object code by offering access from a designated
276
+ place (gratis or for a charge), and offer equivalent access to the
277
+ Corresponding Source in the same way through the same place at no
278
+ further charge. You need not require recipients to copy the
279
+ Corresponding Source along with the object code. If the place to
280
+ copy the object code is a network server, the Corresponding Source
281
+ may be on a different server (operated by you or a third party)
282
+ that supports equivalent copying facilities, provided you maintain
283
+ clear directions next to the object code saying where to find the
284
+ Corresponding Source. Regardless of what server hosts the
285
+ Corresponding Source, you remain obligated to ensure that it is
286
+ available for as long as needed to satisfy these requirements.
287
+
288
+ e) Convey the object code using peer-to-peer transmission, provided
289
+ you inform other peers where the object code and Corresponding
290
+ Source of the work are being offered to the general public at no
291
+ charge under subsection 6d.
292
+
293
+ A separable portion of the object code, whose source code is excluded
294
+ from the Corresponding Source as a System Library, need not be
295
+ included in conveying the object code work.
296
+
297
+ A "User Product" is either (1) a "consumer product", which means any
298
+ tangible personal property which is normally used for personal, family,
299
+ or household purposes, or (2) anything designed or sold for incorporation
300
+ into a dwelling. In determining whether a product is a consumer product,
301
+ doubtful cases shall be resolved in favor of coverage. For a particular
302
+ product received by a particular user, "normally used" refers to a
303
+ typical or common use of that class of product, regardless of the status
304
+ of the particular user or of the way in which the particular user
305
+ actually uses, or expects or is expected to use, the product. A product
306
+ is a consumer product regardless of whether the product has substantial
307
+ commercial, industrial or non-consumer uses, unless such uses represent
308
+ the only significant mode of use of the product.
309
+
310
+ "Installation Information" for a User Product means any methods,
311
+ procedures, authorization keys, or other information required to install
312
+ and execute modified versions of a covered work in that User Product from
313
+ a modified version of its Corresponding Source. The information must
314
+ suffice to ensure that the continued functioning of the modified object
315
+ code is in no case prevented or interfered with solely because
316
+ modification has been made.
317
+
318
+ If you convey an object code work under this section in, or with, or
319
+ specifically for use in, a User Product, and the conveying occurs as
320
+ part of a transaction in which the right of possession and use of the
321
+ User Product is transferred to the recipient in perpetuity or for a
322
+ fixed term (regardless of how the transaction is characterized), the
323
+ Corresponding Source conveyed under this section must be accompanied
324
+ by the Installation Information. But this requirement does not apply
325
+ if neither you nor any third party retains the ability to install
326
+ modified object code on the User Product (for example, the work has
327
+ been installed in ROM).
328
+
329
+ The requirement to provide Installation Information does not include a
330
+ requirement to continue to provide support service, warranty, or updates
331
+ for a work that has been modified or installed by the recipient, or for
332
+ the User Product in which it has been modified or installed. Access to a
333
+ network may be denied when the modification itself materially and
334
+ adversely affects the operation of the network or violates the rules and
335
+ protocols for communication across the network.
336
+
337
+ Corresponding Source conveyed, and Installation Information provided,
338
+ in accord with this section must be in a format that is publicly
339
+ documented (and with an implementation available to the public in
340
+ source code form), and must require no special password or key for
341
+ unpacking, reading or copying.
342
+
343
+ 7. Additional Terms.
344
+
345
+ "Additional permissions" are terms that supplement the terms of this
346
+ License by making exceptions from one or more of its conditions.
347
+ Additional permissions that are applicable to the entire Program shall
348
+ be treated as though they were included in this License, to the extent
349
+ that they are valid under applicable law. If additional permissions
350
+ apply only to part of the Program, that part may be used separately
351
+ under those permissions, but the entire Program remains governed by
352
+ this License without regard to the additional permissions.
353
+
354
+ When you convey a copy of a covered work, you may at your option
355
+ remove any additional permissions from that copy, or from any part of
356
+ it. (Additional permissions may be written to require their own
357
+ removal in certain cases when you modify the work.) You may place
358
+ additional permissions on material, added by you to a covered work,
359
+ for which you have or can give appropriate copyright permission.
360
+
361
+ Notwithstanding any other provision of this License, for material you
362
+ add to a covered work, you may (if authorized by the copyright holders of
363
+ that material) supplement the terms of this License with terms:
364
+
365
+ a) Disclaiming warranty or limiting liability differently from the
366
+ terms of sections 15 and 16 of this License; or
367
+
368
+ b) Requiring preservation of specified reasonable legal notices or
369
+ author attributions in that material or in the Appropriate Legal
370
+ Notices displayed by works containing it; or
371
+
372
+ c) Prohibiting misrepresentation of the origin of that material, or
373
+ requiring that modified versions of such material be marked in
374
+ reasonable ways as different from the original version; or
375
+
376
+ d) Limiting the use for publicity purposes of names of licensors or
377
+ authors of the material; or
378
+
379
+ e) Declining to grant rights under trademark law for use of some
380
+ trade names, trademarks, or service marks; or
381
+
382
+ f) Requiring indemnification of licensors and authors of that
383
+ material by anyone who conveys the material (or modified versions of
384
+ it) with contractual assumptions of liability to the recipient, for
385
+ any liability that these contractual assumptions directly impose on
386
+ those licensors and authors.
387
+
388
+ All other non-permissive additional terms are considered "further
389
+ restrictions" within the meaning of section 10. If the Program as you
390
+ received it, or any part of it, contains a notice stating that it is
391
+ governed by this License along with a term that is a further
392
+ restriction, you may remove that term. If a license document contains
393
+ a further restriction but permits relicensing or conveying under this
394
+ License, you may add to a covered work material governed by the terms
395
+ of that license document, provided that the further restriction does
396
+ not survive such relicensing or conveying.
397
+
398
+ If you add terms to a covered work in accord with this section, you
399
+ must place, in the relevant source files, a statement of the
400
+ additional terms that apply to those files, or a notice indicating
401
+ where to find the applicable terms.
402
+
403
+ Additional terms, permissive or non-permissive, may be stated in the
404
+ form of a separately written license, or stated as exceptions;
405
+ the above requirements apply either way.
406
+
407
+ 8. Termination.
408
+
409
+ You may not propagate or modify a covered work except as expressly
410
+ provided under this License. Any attempt otherwise to propagate or
411
+ modify it is void, and will automatically terminate your rights under
412
+ this License (including any patent licenses granted under the third
413
+ paragraph of section 11).
414
+
415
+ However, if you cease all violation of this License, then your
416
+ license from a particular copyright holder is reinstated (a)
417
+ provisionally, unless and until the copyright holder explicitly and
418
+ finally terminates your license, and (b) permanently, if the copyright
419
+ holder fails to notify you of the violation by some reasonable means
420
+ prior to 60 days after the cessation.
421
+
422
+ Moreover, your license from a particular copyright holder is
423
+ reinstated permanently if the copyright holder notifies you of the
424
+ violation by some reasonable means, this is the first time you have
425
+ received notice of violation of this License (for any work) from that
426
+ copyright holder, and you cure the violation prior to 30 days after
427
+ your receipt of the notice.
428
+
429
+ Termination of your rights under this section does not terminate the
430
+ licenses of parties who have received copies or rights from you under
431
+ this License. If your rights have been terminated and not permanently
432
+ reinstated, you do not qualify to receive new licenses for the same
433
+ material under section 10.
434
+
435
+ 9. Acceptance Not Required for Having Copies.
436
+
437
+ You are not required to accept this License in order to receive or
438
+ run a copy of the Program. Ancillary propagation of a covered work
439
+ occurring solely as a consequence of using peer-to-peer transmission
440
+ to receive a copy likewise does not require acceptance. However,
441
+ nothing other than this License grants you permission to propagate or
442
+ modify any covered work. These actions infringe copyright if you do
443
+ not accept this License. Therefore, by modifying or propagating a
444
+ covered work, you indicate your acceptance of this License to do so.
445
+
446
+ 10. Automatic Licensing of Downstream Recipients.
447
+
448
+ Each time you convey a covered work, the recipient automatically
449
+ receives a license from the original licensors, to run, modify and
450
+ propagate that work, subject to this License. You are not responsible
451
+ for enforcing compliance by third parties with this License.
452
+
453
+ An "entity transaction" is a transaction transferring control of an
454
+ organization, or substantially all assets of one, or subdividing an
455
+ organization, or merging organizations. If propagation of a covered
456
+ work results from an entity transaction, each party to that
457
+ transaction who receives a copy of the work also receives whatever
458
+ licenses to the work the party's predecessor in interest had or could
459
+ give under the previous paragraph, plus a right to possession of the
460
+ Corresponding Source of the work from the predecessor in interest, if
461
+ the predecessor has it or can get it with reasonable efforts.
462
+
463
+ You may not impose any further restrictions on the exercise of the
464
+ rights granted or affirmed under this License. For example, you may
465
+ not impose a license fee, royalty, or other charge for exercise of
466
+ rights granted under this License, and you may not initiate litigation
467
+ (including a cross-claim or counterclaim in a lawsuit) alleging that
468
+ any patent claim is infringed by making, using, selling, offering for
469
+ sale, or importing the Program or any portion of it.
470
+
471
+ 11. Patents.
472
+
473
+ A "contributor" is a copyright holder who authorizes use under this
474
+ License of the Program or a work on which the Program is based. The
475
+ work thus licensed is called the contributor's "contributor version".
476
+
477
+ A contributor's "essential patent claims" are all patent claims
478
+ owned or controlled by the contributor, whether already acquired or
479
+ hereafter acquired, that would be infringed by some manner, permitted
480
+ by this License, of making, using, or selling its contributor version,
481
+ but do not include claims that would be infringed only as a
482
+ consequence of further modification of the contributor version. For
483
+ purposes of this definition, "control" includes the right to grant
484
+ patent sublicenses in a manner consistent with the requirements of
485
  this License.
486
 
487
+ Each contributor grants you a non-exclusive, worldwide, royalty-free
488
+ patent license under the contributor's essential patent claims, to
489
+ make, use, sell, offer for sale, import and otherwise run, modify and
490
+ propagate the contents of its contributor version.
491
+
492
+ In the following three paragraphs, a "patent license" is any express
493
+ agreement or commitment, however denominated, not to enforce a patent
494
+ (such as an express permission to practice a patent or covenant not to
495
+ sue for patent infringement). To "grant" such a patent license to a
496
+ party means to make such an agreement or commitment not to enforce a
497
+ patent against the party.
498
+
499
+ If you convey a covered work, knowingly relying on a patent license,
500
+ and the Corresponding Source of the work is not available for anyone
501
+ to copy, free of charge and under the terms of this License, through a
502
+ publicly available network server or other readily accessible means,
503
+ then you must either (1) cause the Corresponding Source to be so
504
+ available, or (2) arrange to deprive yourself of the benefit of the
505
+ patent license for this particular work, or (3) arrange, in a manner
506
+ consistent with the requirements of this License, to extend the patent
507
+ license to downstream recipients. "Knowingly relying" means you have
508
+ actual knowledge that, but for the patent license, your conveying the
509
+ covered work in a country, or your recipient's use of the covered work
510
+ in a country, would infringe one or more identifiable patents in that
511
+ country that you have reason to believe are valid.
512
+
513
+ If, pursuant to or in connection with a single transaction or
514
+ arrangement, you convey, or propagate by procuring conveyance of, a
515
+ covered work, and grant a patent license to some of the parties
516
+ receiving the covered work authorizing them to use, propagate, modify
517
+ or convey a specific copy of the covered work, then the patent license
518
+ you grant is automatically extended to all recipients of the covered
519
+ work and works based on it.
520
+
521
+ A patent license is "discriminatory" if it does not include within
522
+ the scope of its coverage, prohibits the exercise of, or is
523
+ conditioned on the non-exercise of one or more of the rights that are
524
+ specifically granted under this License. You may not convey a covered
525
+ work if you are a party to an arrangement with a third party that is
526
+ in the business of distributing software, under which you make payment
527
+ to the third party based on the extent of your activity of conveying
528
+ the work, and under which the third party grants, to any of the
529
+ parties who would receive the covered work from you, a discriminatory
530
+ patent license (a) in connection with copies of the covered work
531
+ conveyed by you (or copies made from those copies), or (b) primarily
532
+ for and in connection with specific products or compilations that
533
+ contain the covered work, unless you entered into that arrangement,
534
+ or that patent license was granted, prior to 28 March 2007.
535
+
536
+ Nothing in this License shall be construed as excluding or limiting
537
+ any implied license or other defenses to infringement that may
538
+ otherwise be available to you under applicable patent law.
539
+
540
+ 12. No Surrender of Others' Freedom.
541
+
542
+ If conditions are imposed on you (whether by court order, agreement or
543
  otherwise) that contradict the conditions of this License, they do not
544
+ excuse you from the conditions of this License. If you cannot convey a
545
+ covered work so as to satisfy simultaneously your obligations under this
546
+ License and any other pertinent obligations, then as a consequence you may
547
+ not convey it at all. For example, if you agree to terms that obligate you
548
+ to collect a royalty for further conveying from those to whom you convey
549
+ the Program, the only way you could satisfy both those terms and this
550
+ License would be to refrain entirely from conveying the Program.
551
+
552
+ 13. Use with the GNU Affero General Public License.
553
+
554
+ Notwithstanding any other provision of this License, you have
555
+ permission to link or combine any covered work with a work licensed
556
+ under version 3 of the GNU Affero General Public License into a single
557
+ combined work, and to convey the resulting work. The terms of this
558
+ License will continue to apply to the part which is the covered work,
559
+ but the special requirements of the GNU Affero General Public License,
560
+ section 13, concerning interaction through a network will apply to the
561
+ combination as such.
562
+
563
+ 14. Revised Versions of this License.
564
+
565
+ The Free Software Foundation may publish revised and/or new versions of
566
+ the GNU General Public License from time to time. Such new versions will
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
567
  be similar in spirit to the present version, but may differ in detail to
568
  address new problems or concerns.
569
 
570
+ Each version is given a distinguishing version number. If the
571
+ Program specifies that a certain numbered version of the GNU General
572
+ Public License "or any later version" applies to it, you have the
573
+ option of following the terms and conditions either of that numbered
574
+ version or of any later version published by the Free Software
575
+ Foundation. If the Program does not specify a version number of the
576
+ GNU General Public License, you may choose any version ever published
577
+ by the Free Software Foundation.
578
+
579
+ If the Program specifies that a proxy can decide which future
580
+ versions of the GNU General Public License can be used, that proxy's
581
+ public statement of acceptance of a version permanently authorizes you
582
+ to choose that version for the Program.
583
+
584
+ Later license versions may give you additional or different
585
+ permissions. However, no additional obligations are imposed on any
586
+ author or copyright holder as a result of your choosing to follow a
587
+ later version.
588
+
589
+ 15. Disclaimer of Warranty.
590
+
591
+ THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592
+ APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593
+ HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594
+ OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595
+ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596
+ PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597
+ IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598
+ ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599
+
600
+ 16. Limitation of Liability.
601
+
602
+ IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603
+ WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604
+ THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605
+ GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606
+ USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607
+ DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608
+ PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609
+ EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610
+ SUCH DAMAGES.
611
+
612
+ 17. Interpretation of Sections 15 and 16.
613
+
614
+ If the disclaimer of warranty and limitation of liability provided
615
+ above cannot be given local legal effect according to their terms,
616
+ reviewing courts shall apply local law that most closely approximates
617
+ an absolute waiver of all civil liability in connection with the
618
+ Program, unless a warranty or assumption of liability accompanies a
619
+ copy of the Program in return for a fee.
620
 
621
  END OF TERMS AND CONDITIONS
622
 
628
 
629
  To do so, attach the following notices to the program. It is safest
630
  to attach them to the start of each source file to most effectively
631
+ state the exclusion of warranty; and each file should have at least
632
  the "copyright" line and a pointer to where the full notice is found.
633
 
634
+ {one line to give the program's name and a brief idea of what it does.}
635
+ Copyright (C) {year} {name of author}
636
 
637
+ This program is free software: you can redistribute it and/or modify
638
  it under the terms of the GNU General Public License as published by
639
+ the Free Software Foundation, either version 3 of the License, or
640
  (at your option) any later version.
641
 
642
  This program is distributed in the hope that it will be useful,
644
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645
  GNU General Public License for more details.
646
 
647
+ You should have received a copy of the GNU General Public License
648
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
 
649
 
650
  Also add information on how to contact you by electronic and paper mail.
651
 
652
+ If the program does terminal interaction, make it output a short
653
+ notice like this when it starts in an interactive mode:
654
 
655
+ {project} Copyright (C) {year} {fullname}
656
+ This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657
  This is free software, and you are welcome to redistribute it
658
  under certain conditions; type `show c' for details.
659
 
660
  The hypothetical commands `show w' and `show c' should show the appropriate
661
+ parts of the General Public License. Of course, your program's commands
662
+ might be different; for a GUI interface, you would use an "about box".
663
+
664
+ You should also get your employer (if you work as a programmer) or school,
665
+ if any, to sign a "copyright disclaimer" for the program, if necessary.
666
+ For more information on this, and how to apply and follow the GNU GPL, see
667
+ <http://www.gnu.org/licenses/>.
668
+
669
+ The GNU General Public License does not permit incorporating your program
670
+ into proprietary programs. If your program is a subroutine library, you
671
+ may consider it more useful to permit linking proprietary applications with
672
+ the library. If this is what you want to do, use the GNU Lesser General
673
+ Public License instead of this License. But first, please read
674
+ <http://www.gnu.org/philosophy/why-not-lgpl.html>.
 
 
 
 
 
 
freemius/assets/css/admin/account.css CHANGED
@@ -1 +1 @@
1
- #fs_account .postbox,#fs_account .widefat{max-width:700px}#fs_account h3{font-size:1.3em;padding:12px 15px;margin:0 0 12px 0;line-height:1.4;border-bottom:1px solid #F1F1F1}#fs_account i.dashicons{font-size:1.2em;height:1.2em;width:1.2em}#fs_account .button i.dashicons{vertical-align:middle}#fs_account .fs-header-actions{position:absolute;top:17px;right:15px;font-size:0.9em}#fs_account .fs-header-actions ul{margin:0}#fs_account .fs-header-actions li{float:left}#fs_account .fs-header-actions li form{display:inline-block}#fs_account .fs-header-actions li a{text-decoration:none}#fs_account_details .button-group{float:right}.rtl #fs_account .fs-header-actions{left:15px;right:auto}.fs-key-value-table{width:100%}.fs-key-value-table form{display:inline-block}.fs-key-value-table tr td:first-child{text-align:right}.fs-key-value-table tr td:first-child nobr{font-weight:bold}.fs-key-value-table tr td:first-child form{display:block}.fs-key-value-table tr td.fs-right{text-align:right}.fs-key-value-table tr.fs-odd{background:#ebebeb}.fs-key-value-table td,.fs-key-value-table th{padding:10px}.fs-key-value-table code{line-height:28px}.fs-key-value-table var,.fs-key-value-table code,.fs-key-value-table input[type="text"]{color:#0073AA;font-size:16px;background:none}.fs-key-value-table input[type="text"]{width:100%;font-weight:bold}label.fs-tag{background:#ffba00;color:#fff;display:inline-block;border-radius:3px;padding:5px;font-size:11px;line-height:11px;vertical-align:baseline}label.fs-tag.fs-warn{background:#ffba00}label.fs-tag.fs-success{background:#46b450}label.fs-tag.fs-error{background:#dc3232}#fs_addons h3{border:none;margin-bottom:0;padding:4px 5px}#fs_addons td{vertical-align:middle}#fs_addons thead{white-space:nowrap}#fs_addons td:first-child,#fs_addons th:first-child{text-align:left;font-weight:bold}#fs_addons td:last-child,#fs_addons th:last-child{text-align:right}#fs_addons th{font-weight:bold}#fs_billing_address{width:100%}#fs_billing_address tr td{width:50%;padding:5px}#fs_billing_address tr:first-of-type td{padding-top:0}#fs_billing_address span{font-weight:bold}#fs_billing_address input,#fs_billing_address select{display:block;width:100%;margin-top:5px}#fs_billing_address input::-moz-placeholder,#fs_billing_address select::-moz-placeholder{color:transparent;opacity:1}#fs_billing_address input:-ms-input-placeholder,#fs_billing_address select:-ms-input-placeholder{color:transparent}#fs_billing_address input::-webkit-input-placeholder,#fs_billing_address select::-webkit-input-placeholder{color:transparent}#fs_billing_address input.fs-read-mode,#fs_billing_address select.fs-read-mode{border-color:transparent;color:#777;border-bottom:1px dashed #ccc;padding-left:0;background:none}#fs_billing_address.fs-read-mode td span{display:none}#fs_billing_address.fs-read-mode input,#fs_billing_address.fs-read-mode select{border-color:transparent;color:#777;border-bottom:1px dashed #ccc;padding-left:0;background:none}#fs_billing_address.fs-read-mode input::-moz-placeholder,#fs_billing_address.fs-read-mode select::-moz-placeholder{color:#ccc;opacity:1}#fs_billing_address.fs-read-mode input:-ms-input-placeholder,#fs_billing_address.fs-read-mode select:-ms-input-placeholder{color:#ccc}#fs_billing_address.fs-read-mode input::-webkit-input-placeholder,#fs_billing_address.fs-read-mode select::-webkit-input-placeholder{color:#ccc}#fs_billing_address button{display:block;width:100%}
1
+ #fs_account .postbox,#fs_account .widefat{max-width:700px}#fs_account h3{font-size:1.3em;padding:12px 15px;margin:0 0 12px 0;line-height:1.4;border-bottom:1px solid #F1F1F1}#fs_account h3 .dashicons{width:26px;height:26px;font-size:1.3em}#fs_account i.dashicons{font-size:1.2em;height:1.2em;width:1.2em}#fs_account .button i.dashicons{vertical-align:middle}#fs_account .fs-header-actions{position:absolute;top:17px;right:15px;font-size:0.9em}#fs_account .fs-header-actions ul{margin:0}#fs_account .fs-header-actions li{float:left}#fs_account .fs-header-actions li form{display:inline-block}#fs_account .fs-header-actions li a{text-decoration:none}#fs_account_details .button-group{float:right}.rtl #fs_account .fs-header-actions{left:15px;right:auto}.fs-key-value-table{width:100%}.fs-key-value-table form{display:inline-block}.fs-key-value-table tr td:first-child{text-align:right}.fs-key-value-table tr td:first-child nobr{font-weight:bold}.fs-key-value-table tr td:first-child form{display:block}.fs-key-value-table tr td.fs-right{text-align:right}.fs-key-value-table tr.fs-odd{background:#ebebeb}.fs-key-value-table td,.fs-key-value-table th{padding:10px}.fs-key-value-table code{line-height:28px}.fs-key-value-table var,.fs-key-value-table code,.fs-key-value-table input[type="text"]{color:#0073AA;font-size:16px;background:none}.fs-key-value-table input[type="text"]{width:100%;font-weight:bold}label.fs-tag{background:#ffba00;color:#fff;display:inline-block;border-radius:3px;padding:5px;font-size:11px;line-height:11px;vertical-align:baseline}label.fs-tag.fs-warn{background:#ffba00}label.fs-tag.fs-success{background:#46b450}label.fs-tag.fs-error{background:#dc3232}#fs_addons h3{border:none;margin-bottom:0;padding:4px 5px}#fs_addons td{vertical-align:middle}#fs_addons thead{white-space:nowrap}#fs_addons td:first-child,#fs_addons th:first-child{text-align:left;font-weight:bold}#fs_addons td:last-child,#fs_addons th:last-child{text-align:right}#fs_addons th{font-weight:bold}#fs_billing_address{width:100%}#fs_billing_address tr td{width:50%;padding:5px}#fs_billing_address tr:first-of-type td{padding-top:0}#fs_billing_address span{font-weight:bold}#fs_billing_address input,#fs_billing_address select{display:block;width:100%;margin-top:5px}#fs_billing_address input::-moz-placeholder,#fs_billing_address select::-moz-placeholder{color:transparent;opacity:1}#fs_billing_address input:-ms-input-placeholder,#fs_billing_address select:-ms-input-placeholder{color:transparent}#fs_billing_address input::-webkit-input-placeholder,#fs_billing_address select::-webkit-input-placeholder{color:transparent}#fs_billing_address input.fs-read-mode,#fs_billing_address select.fs-read-mode{border-color:transparent;color:#777;border-bottom:1px dashed #ccc;padding-left:0;background:none}#fs_billing_address.fs-read-mode td span{display:none}#fs_billing_address.fs-read-mode input,#fs_billing_address.fs-read-mode select{border-color:transparent;color:#777;border-bottom:1px dashed #ccc;padding-left:0;background:none}#fs_billing_address.fs-read-mode input::-moz-placeholder,#fs_billing_address.fs-read-mode select::-moz-placeholder{color:#ccc;opacity:1}#fs_billing_address.fs-read-mode input:-ms-input-placeholder,#fs_billing_address.fs-read-mode select:-ms-input-placeholder{color:#ccc}#fs_billing_address.fs-read-mode input::-webkit-input-placeholder,#fs_billing_address.fs-read-mode select::-webkit-input-placeholder{color:#ccc}#fs_billing_address button{display:block;width:100%}
freemius/assets/css/admin/affiliation.css ADDED
@@ -0,0 +1 @@
 
1
+ @charset "UTF-8";#fs_affiliation_content_wrapper #messages{margin-top:25px}#fs_affiliation_content_wrapper h3{font-size:24px;padding:0;margin-left:0}#fs_affiliation_content_wrapper ul li{box-sizing:border-box;list-style-type:none}#fs_affiliation_content_wrapper ul li:before{content:'✓';margin-right:10px;font-weight:bold}#fs_affiliation_content_wrapper p:not(.description),#fs_affiliation_content_wrapper li,#fs_affiliation_content_wrapper label{font-size:16px !important;line-height:26px !important}#fs_affiliation_content_wrapper .button{margin-top:20px;margin-bottom:7px;line-height:35px;height:40px;font-size:16px}#fs_affiliation_content_wrapper .button#cancel_button{margin-right:5px}#fs_affiliation_content_wrapper form .input-container{margin-bottom:15px}#fs_affiliation_content_wrapper form .input-container .input-label{font-weight:bold;display:block;width:100%}#fs_affiliation_content_wrapper form .input-container.input-container-text label,#fs_affiliation_content_wrapper form .input-container.input-container-text input,#fs_affiliation_content_wrapper form .input-container.input-container-text textarea{display:block}#fs_affiliation_content_wrapper form .input-container #add_domain,#fs_affiliation_content_wrapper form .input-container .remove-domain{text-decoration:none;display:inline-block;margin-top:3px}#fs_affiliation_content_wrapper form .input-container #add_domain:focus,#fs_affiliation_content_wrapper form .input-container .remove-domain:focus{box-shadow:none}#fs_affiliation_content_wrapper form .input-container #add_domain.disabled,#fs_affiliation_content_wrapper form .input-container .remove-domain.disabled{color:#aaa;cursor:default}#fs_affiliation_content_wrapper form #extra_domains_container .description{margin-top:0;position:relative;top:-4px}#fs_affiliation_content_wrapper form #extra_domains_container .extra-domain-input-container{margin-bottom:15px}#fs_affiliation_content_wrapper form #extra_domains_container .extra-domain-input-container .domain{display:inline-block;margin-right:5px}#fs_affiliation_content_wrapper form #extra_domains_container .extra-domain-input-container .domain:last-of-type{margin-bottom:0}
freemius/assets/css/admin/common.css CHANGED
@@ -1,2 +1,2 @@
1
- #iframe{line-height:0;font-size:0}.fs-full-size-wrapper{margin:0 0 -65px -20px}@media (max-width: 782px){.fs-full-size-wrapper{margin:0 0 -65px -10px}}
2
- .fs-notice{position:relative}.fs-notice.fs-has-title{margin-bottom:30px !important}.fs-notice.success{color:green}.fs-notice.promotion{border-color:#00a0d2 !important;background-color:#f2fcff !important}.fs-notice .fs-notice-body{margin:.5em 0;padding:2px}.fs-notice .fs-close{cursor:pointer;color:#aaa;float:right}.fs-notice .fs-close:hover{color:#666}.fs-notice .fs-close>*{margin-top:7px;display:inline-block}.fs-notice label.fs-plugin-title{background:rgba(0,0,0,0.3);color:#fff;padding:2px 10px;position:absolute;top:100%;bottom:auto;right:auto;-moz-border-radius:0 0 3px 3px;-webkit-border-radius:0 0 3px 3px;border-radius:0 0 3px 3px;left:10px;font-size:12px;font-weight:bold;cursor:auto}.rtl .fs-notice .fs-close{float:left}.fs-secure-notice{position:fixed;top:32px;left:160px;right:0;background:#ebfdeb;padding:10px 20px;color:green;z-index:9999;box-shadow:0px 2px 2px rgba(6,113,6,0.3);opacity:0.95;filter:alpha(opacity=95)}.fs-secure-notice:hover{opacity:1;filter:alpha(opacity=100)}@media screen and (max-width: 960px){.fs-secure-notice{left:36px}}@media screen and (max-width: 500px){#fs_promo_tab{display:none}}@media screen and (max-width: 782px){.fs-secure-notice{left:0;top:46px;text-align:center}}span.fs-submenu-item.fs-sub:before{content:'\21B3';padding:0 5px}.rtl span.fs-submenu-item.fs-sub:before{content:'\21B2'}.fs-submenu-item.pricing.upgrade-mode{color:greenyellow}.fs-submenu-item.pricing.trial-mode{color:#83e2ff}#adminmenu .update-plugins.fs-trial{background-color:#00b9eb}
1
+ .theme-browser .theme .fs-premium-theme-badge{position:absolute;top:10px;right:0;background:#71ae00;color:#fff;text-transform:uppercase;padding:5px 10px;-moz-border-radius:3px 0 0 3px;-webkit-border-radius:3px 0 0 3px;border-radius:3px 0 0 3px;font-weight:bold;border-right:0;-moz-box-shadow:0 2px 1px -1px rgba(0,0,0,0.3);-webkit-box-shadow:0 2px 1px -1px rgba(0,0,0,0.3);box-shadow:0 2px 1px -1px rgba(0,0,0,0.3);font-size:1.1em}#iframe{line-height:0;font-size:0}.fs-full-size-wrapper{margin:40px 0 -65px -20px}@media (max-width: 600px){.fs-full-size-wrapper{margin:0 0 -65px -10px}}
2
+ .fs-notice{position:relative}.fs-notice.fs-has-title{margin-bottom:30px !important}.fs-notice.success{color:green}.fs-notice.promotion{border-color:#00a0d2 !important;background-color:#f2fcff !important}.fs-notice .fs-notice-body{margin:.5em 0;padding:2px}.fs-notice .fs-close{cursor:pointer;color:#aaa;float:right}.fs-notice .fs-close:hover{color:#666}.fs-notice .fs-close>*{margin-top:7px;display:inline-block}.fs-notice label.fs-plugin-title{background:rgba(0,0,0,0.3);color:#fff;padding:2px 10px;position:absolute;top:100%;bottom:auto;right:auto;-moz-border-radius:0 0 3px 3px;-webkit-border-radius:0 0 3px 3px;border-radius:0 0 3px 3px;left:10px;font-size:12px;font-weight:bold;cursor:auto}div.fs-notice.updated,div.fs-notice.success,div.fs-notice.promotion{display:block !important}.rtl .fs-notice .fs-close{float:left}.fs-secure-notice{position:fixed;top:32px;left:160px;right:0;background:#ebfdeb;padding:10px 20px;color:green;z-index:9999;-moz-box-shadow:0 2px 2px rgba(6,113,6,0.3);-webkit-box-shadow:0 2px 2px rgba(6,113,6,0.3);box-shadow:0 2px 2px rgba(6,113,6,0.3);opacity:0.95;filter:alpha(opacity=95)}.fs-secure-notice:hover{opacity:1;filter:alpha(opacity=100)}.fs-secure-notice a.fs-security-proof{color:green;text-decoration:none}@media screen and (max-width: 960px){.fs-secure-notice{left:36px}}@media screen and (max-width: 600px){.fs-secure-notice{display:none}}@media screen and (max-width: 500px){#fs_promo_tab{display:none}}@media screen and (max-width: 782px){.fs-secure-notice{left:0;top:46px;text-align:center}}span.fs-submenu-item.fs-sub:before{content:'\21B3';padding:0 5px}.rtl span.fs-submenu-item.fs-sub:before{content:'\21B2'}.fs-submenu-item.pricing.upgrade-mode{color:greenyellow}.fs-submenu-item.pricing.trial-mode{color:#83e2ff}#adminmenu .update-plugins.fs-trial{background-color:#00b9eb}.fs-ajax-spinner{border:0;width:20px;height:20px;margin-right:5px;vertical-align:sub;display:inline-block;background:url("../../../../../../../wp-admin/images/wpspin_light-2x.gif");background-size:contain}.wrap.fs-section h2{text-align:left}
freemius/assets/css/admin/connect.css CHANGED
@@ -1 +1 @@
1
- #fs_connect{width:480px;-moz-box-shadow:0px 1px 2px rgba(0,0,0,0.3);-webkit-box-shadow:0px 1px 2px rgba(0,0,0,0.3);box-shadow:0px 1px 2px rgba(0,0,0,0.3);margin:20px 0}@media screen and (max-width: 479px){#fs_connect{-moz-box-shadow:none;-webkit-box-shadow:none;box-shadow:none;width:auto;margin:0 0 0 -10px}}#fs_connect .fs-content{background:#fff;padding:15px 20px}#fs_connect .fs-content .fs-error{background:snow;color:#d3135a;border:1px solid #d3135a;-moz-box-shadow:0 1px 1px 0 rgba(0,0,0,0.1);-webkit-box-shadow:0 1px 1px 0 rgba(0,0,0,0.1);box-shadow:0 1px 1px 0 rgba(0,0,0,0.1);text-align:center;padding:5px;margin-bottom:10px}#fs_connect .fs-content p{margin:0;padding:0;font-size:1.2em}#fs_connect .fs-license-key-container{position:relative;width:280px;margin:10px auto 0 auto}#fs_connect .fs-license-key-container input{width:100%}#fs_connect .fs-license-key-container .dashicons{position:absolute;top:5px;right:5px}#fs_connect .fs-actions{padding:10px 20px;background:#C0C7CA}#fs_connect .fs-actions .button{padding:0 10px 1px;line-height:35px;height:37px;font-size:16px;margin-bottom:0}#fs_connect .fs-actions .button .dashicons{font-size:37px;margin-left:-8px;margin-right:12px}#fs_connect .fs-actions .button.button-primary{padding-right:15px;padding-left:15px}#fs_connect .fs-actions .button.button-primary:after{content:' \279C'}#fs_connect .fs-actions .button.button-primary.fs-loading:after{content:''}#fs_connect .fs-actions .button.button-secondary{float:right}#fs_connect.fs-anonymous-disabled .fs-actions .button.button-primary{width:100%}#fs_connect .fs-permissions{padding:10px 20px;background:#FEFEFE;-moz-transition:background 0.5s ease;-o-transition:background 0.5s ease;-ms-transition:background 0.5s ease;-webkit-transition:background 0.5s ease;transition:background 0.5s ease}#fs_connect .fs-permissions .fs-license-sync-disclaimer{text-align:center;margin-top:0}#fs_connect .fs-permissions .fs-trigger{font-size:0.9em;text-decoration:none;text-align:center;display:block}#fs_connect .fs-permissions ul{height:0;overflow:hidden;margin:0}#fs_connect .fs-permissions ul li{margin-bottom:12px}#fs_connect .fs-permissions ul li:last-child{margin-bottom:0}#fs_connect .fs-permissions ul li i.dashicons{float:left;font-size:40px;width:40px;height:40px}#fs_connect .fs-permissions ul li div{margin-left:55px}#fs_connect .fs-permissions ul li div span{font-weight:bold;text-transform:uppercase;color:#23282d}#fs_connect .fs-permissions ul li div p{margin:2px 0 0 0}#fs_connect .fs-permissions.fs-open{background:#fff}#fs_connect .fs-permissions.fs-open ul{height:auto;margin:20px 20px 10px 20px}@media screen and (max-width: 479px){#fs_connect .fs-permissions{background:#fff}#fs_connect .fs-permissions .fs-trigger{display:none}#fs_connect .fs-permissions ul{height:auto;margin:20px}}#fs_connect .fs-freemium-licensing{padding:8px;background:#777;color:#fff}#fs_connect .fs-freemium-licensing p{text-align:center;display:block;margin:0;padding:0}#fs_connect .fs-freemium-licensing a{color:#C2EEFF;text-decoration:underline}#fs_connect .fs-visual{padding:12px;line-height:0;background:#fafafa;height:80px;position:relative}#fs_connect .fs-visual .fs-site-icon{position:absolute;left:20px;top:10px}#fs_connect .fs-visual .fs-connect-logo{position:absolute;right:20px;top:10px}#fs_connect .fs-visual .fs-plugin-icon{position:absolute;top:10px;left:50%;margin-left:-40px}#fs_connect .fs-visual .fs-plugin-icon,#fs_connect .fs-visual .fs-site-icon,#fs_connect .fs-visual img,#fs_connect .fs-visual object{width:80px;height:80px}#fs_connect .fs-visual .dashicons-wordpress{font-size:64px;background:#01749a;color:#fff;width:64px;height:64px;padding:8px}#fs_connect .fs-visual .dashicons-plus{position:absolute;top:50%;font-size:30px;margin-top:-10px;color:#bbb}#fs_connect .fs-visual .dashicons-plus.fs-first{left:28%}#fs_connect .fs-visual .dashicons-plus.fs-second{left:65%}#fs_connect .fs-visual .fs-plugin-icon,#fs_connect .fs-visual .fs-connect-logo,#fs_connect .fs-visual .fs-site-icon{border:1px solid #ccc;padding:1px;background:#fff}#fs_connect .fs-terms{text-align:center;font-size:0.85em;padding:5px;background:rgba(0,0,0,0.05)}#fs_connect .fs-terms,#fs_connect .fs-terms a{color:#999}#fs_connect .fs-terms a{text-decoration:none}.rtl #fs_connect .fs-actions{padding:10px 20px;background:#C0C7CA}.rtl #fs_connect .fs-actions .button .dashicons{font-size:37px;margin-left:-8px;margin-right:12px}.rtl #fs_connect .fs-actions .button.button-primary:after{content:' \000bb'}.rtl #fs_connect .fs-actions .button.button-primary.fs-loading:after{content:''}.rtl #fs_connect .fs-actions .button.button-secondary{float:left}.rtl #fs_connect .fs-permissions ul li div{margin-right:55px;margin-left:0}.rtl #fs_connect .fs-permissions ul li i.dashicons{float:right}.rtl #fs_connect .fs-visual .fs-site-icon{right:20px;left:auto}.rtl #fs_connect .fs-visual .fs-connect-logo{right:auto;left:20px}.wp-pointer-content #fs_connect{margin:0;-moz-box-shadow:none;-webkit-box-shadow:none;box-shadow:none}.fs-opt-in-pointer .wp-pointer-content{padding:0}.fs-opt-in-pointer.wp-pointer-top .wp-pointer-arrow{border-bottom-color:#dfdfdf}.fs-opt-in-pointer.wp-pointer-top .wp-pointer-arrow-inner{border-bottom-color:#fafafa}.fs-opt-in-pointer.wp-pointer-bottom .wp-pointer-arrow{border-top-color:#dfdfdf}.fs-opt-in-pointer.wp-pointer-bottom .wp-pointer-arrow-inner{border-top-color:#fafafa}.fs-opt-in-pointer.wp-pointer-left .wp-pointer-arrow{border-right-color:#dfdfdf}.fs-opt-in-pointer.wp-pointer-left .wp-pointer-arrow-inner{border-right-color:#fafafa}.fs-opt-in-pointer.wp-pointer-right .wp-pointer-arrow{border-left-color:#dfdfdf}.fs-opt-in-pointer.wp-pointer-right .wp-pointer-arrow-inner{border-left-color:#fafafa}
1
+ #fs_connect{width:480px;-moz-box-shadow:0px 1px 2px rgba(0,0,0,0.3);-webkit-box-shadow:0px 1px 2px rgba(0,0,0,0.3);box-shadow:0px 1px 2px rgba(0,0,0,0.3);margin:20px 0}@media screen and (max-width: 479px){#fs_connect{-moz-box-shadow:none;-webkit-box-shadow:none;box-shadow:none;width:auto;margin:0 0 0 -10px}}#fs_connect .fs-content{background:#fff;padding:15px 20px}#fs_connect .fs-content .fs-error{background:snow;color:#d3135a;border:1px solid #d3135a;-moz-box-shadow:0 1px 1px 0 rgba(0,0,0,0.1);-webkit-box-shadow:0 1px 1px 0 rgba(0,0,0,0.1);box-shadow:0 1px 1px 0 rgba(0,0,0,0.1);text-align:center;padding:5px;margin-bottom:10px}#fs_connect .fs-content p{margin:0;padding:0;font-size:1.2em}#fs_connect .fs-license-key-container{position:relative;width:280px;margin:10px auto 0 auto}#fs_connect .fs-license-key-container input{width:100%}#fs_connect .fs-license-key-container .dashicons{position:absolute;top:5px;right:5px}#fs_connect .fs-actions{padding:10px 20px;background:#C0C7CA}#fs_connect .fs-actions .button{padding:0 10px 1px;line-height:35px;height:37px;font-size:16px;margin-bottom:0}#fs_connect .fs-actions .button .dashicons{font-size:37px;margin-left:-8px;margin-right:12px}#fs_connect .fs-actions .button.button-primary{padding-right:15px;padding-left:15px}#fs_connect .fs-actions .button.button-primary:after{content:' \279C'}#fs_connect .fs-actions .button.button-primary.fs-loading:after{content:''}#fs_connect .fs-actions .button.button-secondary{float:right}#fs_connect.fs-anonymous-disabled .fs-actions .button.button-primary{width:100%}#fs_connect .fs-permissions{padding:10px 20px;background:#FEFEFE;-moz-transition:background 0.5s ease;-o-transition:background 0.5s ease;-ms-transition:background 0.5s ease;-webkit-transition:background 0.5s ease;transition:background 0.5s ease}#fs_connect .fs-permissions .fs-license-sync-disclaimer{text-align:center;margin-top:0}#fs_connect .fs-permissions .fs-trigger{font-size:0.9em;text-decoration:none;text-align:center;display:block}#fs_connect .fs-permissions ul{height:0;overflow:hidden;margin:0}#fs_connect .fs-permissions ul li{margin-bottom:12px}#fs_connect .fs-permissions ul li:last-child{margin-bottom:0}#fs_connect .fs-permissions ul li i.dashicons{float:left;font-size:40px;width:40px;height:40px}#fs_connect .fs-permissions ul li div{margin-left:55px}#fs_connect .fs-permissions ul li div span{font-weight:bold;text-transform:uppercase;color:#23282d}#fs_connect .fs-permissions ul li div p{margin:2px 0 0 0}#fs_connect .fs-permissions.fs-open{background:#fff}#fs_connect .fs-permissions.fs-open ul{height:auto;margin:20px 20px 10px 20px}@media screen and (max-width: 479px){#fs_connect .fs-permissions{background:#fff}#fs_connect .fs-permissions .fs-trigger{display:none}#fs_connect .fs-permissions ul{height:auto;margin:20px}}#fs_connect .fs-freemium-licensing{padding:8px;background:#777;color:#fff}#fs_connect .fs-freemium-licensing p{text-align:center;display:block;margin:0;padding:0}#fs_connect .fs-freemium-licensing a{color:#C2EEFF;text-decoration:underline}#fs_connect .fs-visual{padding:12px;line-height:0;background:#fafafa;height:80px;position:relative}#fs_connect .fs-visual .fs-site-icon{position:absolute;left:20px;top:10px}#fs_connect .fs-visual .fs-connect-logo{position:absolute;right:20px;top:10px}#fs_connect .fs-visual .fs-plugin-icon{position:absolute;top:10px;left:50%;margin-left:-40px}#fs_connect .fs-visual .fs-plugin-icon,#fs_connect .fs-visual .fs-site-icon,#fs_connect .fs-visual img,#fs_connect .fs-visual object{width:80px;height:80px}#fs_connect .fs-visual .dashicons-wordpress{font-size:64px;background:#01749a;color:#fff;width:64px;height:64px;padding:8px}#fs_connect .fs-visual .dashicons-plus{position:absolute;top:50%;font-size:30px;margin-top:-10px;color:#bbb}#fs_connect .fs-visual .dashicons-plus.fs-first{left:28%}#fs_connect .fs-visual .dashicons-plus.fs-second{left:65%}#fs_connect .fs-visual .fs-plugin-icon,#fs_connect .fs-visual .fs-connect-logo,#fs_connect .fs-visual .fs-site-icon{border:1px solid #ccc;padding:1px;background:#fff}#fs_connect .fs-terms{text-align:center;font-size:0.85em;padding:5px;background:rgba(0,0,0,0.05)}#fs_connect .fs-terms,#fs_connect .fs-terms a{color:#999}#fs_connect .fs-terms a{text-decoration:none}.rtl #fs_connect .fs-actions{padding:10px 20px;background:#C0C7CA}.rtl #fs_connect .fs-actions .button .dashicons{font-size:37px;margin-left:-8px;margin-right:12px}.rtl #fs_connect .fs-actions .button.button-primary:after{content:' \000bb'}.rtl #fs_connect .fs-actions .button.button-primary.fs-loading:after{content:''}.rtl #fs_connect .fs-actions .button.button-secondary{float:left}.rtl #fs_connect .fs-permissions ul li div{margin-right:55px;margin-left:0}.rtl #fs_connect .fs-permissions ul li i.dashicons{float:right}.rtl #fs_connect .fs-visual .fs-site-icon{right:20px;left:auto}.rtl #fs_connect .fs-visual .fs-connect-logo{right:auto;left:20px}#fs_theme_connect_wrapper{position:fixed;top:0;height:100%;width:100%;z-index:99990;background:rgba(0,0,0,0.75);text-align:center;overflow-y:auto}#fs_theme_connect_wrapper:before{content:"";display:inline-block;vertical-align:middle;height:100%}#fs_theme_connect_wrapper>button.close{color:white;cursor:pointer;height:40px;width:40px;position:absolute;right:0;border:0;background-color:transparent;top:32px}#fs_theme_connect_wrapper #fs_connect{top:0;text-align:left;display:inline-block;vertical-align:middle;margin-top:52px;margin-bottom:20px}#fs_theme_connect_wrapper #fs_connect .fs-terms{background:rgba(140,140,140,0.64)}#fs_theme_connect_wrapper #fs_connect .fs-terms,#fs_theme_connect_wrapper #fs_connect .fs-terms a{color:#c5c5c5}.wp-pointer-content #fs_connect{margin:0;-moz-box-shadow:none;-webkit-box-shadow:none;box-shadow:none}.fs-opt-in-pointer .wp-pointer-content{padding:0}.fs-opt-in-pointer.wp-pointer-top .wp-pointer-arrow{border-bottom-color:#dfdfdf}.fs-opt-in-pointer.wp-pointer-top .wp-pointer-arrow-inner{border-bottom-color:#fafafa}.fs-opt-in-pointer.wp-pointer-bottom .wp-pointer-arrow{border-top-color:#dfdfdf}.fs-opt-in-pointer.wp-pointer-bottom .wp-pointer-arrow-inner{border-top-color:#fafafa}.fs-opt-in-pointer.wp-pointer-left .wp-pointer-arrow{border-right-color:#dfdfdf}.fs-opt-in-pointer.wp-pointer-left .wp-pointer-arrow-inner{border-right-color:#fafafa}.fs-opt-in-pointer.wp-pointer-right .wp-pointer-arrow{border-left-color:#dfdfdf}.fs-opt-in-pointer.wp-pointer-right .wp-pointer-arrow-inner{border-left-color:#fafafa}
freemius/assets/css/admin/index.php ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ <?php
2
+ // Silence is golden.
3
+ // Hide file structure from users on unprotected servers.
freemius/assets/css/common.css DELETED
@@ -1 +0,0 @@
1
- .fs-notice.success{color:green;font-weight:700}
 
freemius/assets/css/customizer.css ADDED
@@ -0,0 +1 @@
 
1
+ #fs_customizer_upsell .fs-customizer-plan{padding:10px 20px 20px 20px;border-radius:3px;background:#fff}#fs_customizer_upsell .fs-customizer-plan h2{position:relative;margin:0;line-height:2em;text-transform:uppercase}#fs_customizer_upsell .fs-customizer-plan h2 .button-link{top:-2px}#fs_customizer_upsell .fs-feature{position:relative}#fs_customizer_upsell .dashicons-yes{color:#0085ba;font-size:2em;vertical-align:bottom;margin-left:-7px;margin-right:10px}.rtl #fs_customizer_upsell .dashicons-yes{margin-left:10px;margin-right:-7px}#fs_customizer_upsell .dashicons-editor-help{color:#bbb;cursor:help}#fs_customizer_upsell .dashicons-editor-help .fs-feature-desc{opacity:0;visibility:hidden;-moz-transition:opacity 0.3s ease-in-out;-o-transition:opacity 0.3s ease-in-out;-ms-transition:opacity 0.3s ease-in-out;-webkit-transition:opacity 0.3s ease-in-out;transition:opacity 0.3s ease-in-out;position:absolute;background:#000;color:#fff;font-family:'arial', serif;font-size:12px;padding:10px;z-index:999999;bottom:100%;margin-bottom:5px;left:0;right:0;-moz-border-radius:5px;-webkit-border-radius:5px;border-radius:5px;-moz-box-shadow:1px 1px 1px rgba(0,0,0,0.2);-webkit-box-shadow:1px 1px 1px rgba(0,0,0,0.2);box-shadow:1px 1px 1px rgba(0,0,0,0.2);line-height:1.3em;font-weight:bold;text-align:left}.rtl #fs_customizer_upsell .dashicons-editor-help .fs-feature-desc{text-align:right}#fs_customizer_upsell .dashicons-editor-help .fs-feature-desc::after{content:' ';display:block;width:0;height:0;border-style:solid;border-width:5px 5px 0 5px;border-color:#000 transparent transparent transparent;position:absolute;top:100%;left:21px}.rtl #fs_customizer_upsell .dashicons-editor-help .fs-feature-desc::after{right:21px;left:auto}#fs_customizer_upsell .dashicons-editor-help:hover .fs-feature-desc{visibility:visible;opacity:1}#fs_customizer_upsell .button-primary{display:block;text-align:center;margin-top:10px}#fs_customizer_support{display:block !important}#fs_customizer_support .button{float:right}#fs_customizer_support .button-group{width:100%;display:block;margin-top:10px}#fs_customizer_support .button-group .button{float:none;width:50%;text-align:center}
freemius/assets/css/index.php ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ <?php
2
+ // Silence is golden.
3
+ // Hide file structure from users on unprotected servers.
freemius/assets/img/index.php ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ <?php
2
+ // Silence is golden.
3
+ // Hide file structure from users on unprotected servers.
freemius/assets/img/theme-icon.png ADDED
Binary file
freemius/assets/js/index.php ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ <?php
2
+ // Silence is golden.
3
+ // Hide file structure from users on unprotected servers.
freemius/assets/scss/_mixins.scss CHANGED
@@ -132,19 +132,6 @@ $useIEFilters: 0;
132
  }
133
  }
134
 
135
- /*@mixin rgba-bg ($hex, $alpha, $ie: $useIEFilters) {
136
- @if $ie == 1 {
137
- background-color: none;
138
- $hexopac: ie-hex-str(rgba($hex, $alpha));
139
- filter: progid:DXImageTransform.Microsoft.gradient(startColorStr='#{$hexopac}',EndColorStr='#{$hexopac}}');
140
- -ms-filter: quote(progid:DXImageTransform.Microsoft.gradient(startColorStr='#{$hexopac}',EndColorStr='#{$hexopac}'));
141
- }
142
- @else {
143
- background-color: $hex;
144
- background-color: rgba($hex, $alpha);
145
- }
146
- }*/
147
-
148
  @mixin perspective($perspective) {
149
  -moz-perspective: $perspective;
150
  -ms-perspective: $perspective;
132
  }
133
  }
134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
  @mixin perspective($perspective) {
136
  -moz-perspective: $perspective;
137
  -ms-perspective: $perspective;
freemius/assets/scss/admin/_themes.scss ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .theme-browser
2
+ {
3
+ .theme
4
+ {
5
+ .fs-premium-theme-badge
6
+ {
7
+ position: absolute;
8
+ top: 10px;
9
+ right: 0;
10
+ background: $fs-logo-green-color;
11
+ color: #fff;
12
+ text-transform: uppercase;
13
+ padding: 5px 10px;
14
+ @include border-radius(3px 0 0 3px);
15
+ font-weight: bold;
16
+ border-right: 0;
17
+ @include box-shadow(0 2px 1px -1px rgba(0, 0, 0, .3));
18
+ font-size: 1.1em;
19
+ }
20
+ }
21
+ }
freemius/assets/scss/admin/account.scss CHANGED
@@ -15,6 +15,12 @@
15
  margin: 0 0 12px 0;
16
  line-height: 1.4;
17
  border-bottom: 1px solid #F1F1F1;
 
 
 
 
 
 
18
  }
19
 
20
  i.dashicons
15
  margin: 0 0 12px 0;
16
  line-height: 1.4;
17
  border-bottom: 1px solid #F1F1F1;
18
+
19
+ .dashicons {
20
+ width: 26px;
21
+ height: 26px;
22
+ font-size: 1.3em;
23
+ }
24
  }
25
 
26
  i.dashicons
freemius/assets/scss/admin/affiliation.scss ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ @import "../start";
2
+
3
+ #fs_affiliation_content_wrapper {
4
+ #messages {
5
+ margin-top: 25px;
6
+ }
7
+
8
+ h3 {
9
+ font-size: 24px;
10
+ padding: 0;
11
+ margin-left: 0;
12
+ }
13
+
14
+ ul {
15
+ li {
16
+ @include box-sizing(border-box);
17
+ list-style-type: none;
18
+
19
+ &:before {
20
+ content: '✓';
21
+ margin-right: 10px;
22
+ font-weight: bold;
23
+ }
24
+ }
25
+ }
26
+
27
+ p:not(.description), li, label {
28
+ font-size: 16px !important;
29
+ line-height: 26px !important;
30
+ }
31
+
32
+ .button {
33
+ margin-top: 20px;
34
+ margin-bottom: 7px;
35
+ line-height: 35px;
36
+ height: 40px;
37
+ font-size: 16px;
38
+
39
+ &#cancel_button {
40
+ margin-right: 5px;
41
+ }
42
+ }
43
+
44
+ form {
45
+ .input-container {
46
+ .input-label {
47
+ font-weight: bold;
48
+ display: block;
49
+ width: 100%;
50
+ }
51
+
52
+ &.input-container-text {
53
+ label, input, textarea {
54
+ display: block;
55
+ }
56
+ }
57
+
58
+ margin-bottom: 15px;
59
+
60
+ #add_domain, .remove-domain {
61
+ text-decoration: none;
62
+ display: inline-block;
63
+ margin-top: 3px;
64
+
65
+ &:focus {
66
+ box-shadow: none;
67
+ }
68
+
69
+ &.disabled {
70
+ color: #aaa;
71
+ cursor: default;
72
+ }
73
+ }
74
+ }
75
+
76
+ #extra_domains_container {
77
+ .description {
78
+ margin-top: 0;
79
+ position: relative;
80
+ top: -4px;
81
+ }
82
+
83
+ .extra-domain-input-container {
84
+ margin-bottom: 15px;
85
+
86
+ .domain {
87
+ display: inline-block;
88
+ margin-right: 5px;
89
+
90
+ &:last-of-type {
91
+ margin-bottom: 0;
92
+ }
93
+ }
94
+ }
95
+ }
96
+ }
97
+ }
freemius/assets/scss/admin/common.scss CHANGED
@@ -1,173 +1,218 @@
1
- @import "../start";
2
-
3
- #iframe {
4
- line-height: 0;
5
- font-size: 0;
6
- }
7
-
8
- .fs-full-size-wrapper {
9
- margin: 0 0 -65px -20px;
10
-
11
- @media (max-width: $admin_mobile_max_width) {
12
- margin: 0 0 -65px -10px;
13
- }
14
- }
15
-
16
- .fs-notice
17
- {
18
- position: relative;
19
-
20
- &.fs-has-title
21
- {
22
- margin-bottom: 30px !important;
23
- }
24
-
25
- &.success
26
- {
27
- color: green;
28
- // font-weight: normal;
29
- }
30
-
31
- &.promotion
32
- {
33
- border-color: $fs-notice-promotion-border-color !important;
34
- background-color: $fs-notice-promotion-bkg !important;
35
- }
36
-
37
- .fs-notice-body
38
- {
39
- margin: .5em 0;
40
- padding: 2px;
41
- }
42
-
43
- .fs-close
44
- {
45
- // position: absolute;
46
- // top: 2px;
47
- // bottom: 2px;
48
- // right: 2px;
49
- // min-width: 100px;
50
- // text-align: center;
51
- // padding-right: 2px;
52
- cursor: pointer;
53
- color: #aaa;
54
- float: right;
55
-
56
- &:hover
57
- {
58
- color: #666;
59
- // background: #A9A9A9;
60
- }
61
-
62
- > *
63
- {
64
- margin-top: 7px;
65
- display: inline-block;
66
- }
67
- }
68
-
69
- label.fs-plugin-title
70
- {
71
- background: rgba(0, 0, 0, 0.3);
72
- color: #fff;
73
- padding: 2px 10px;
74
- position: absolute;
75
- top: 100%;
76
- bottom: auto;
77
- right: auto;
78
- @include border-radius(0 0 3px 3px);
79
- left: 10px;
80
- font-size: 12px;
81
- font-weight: bold;
82
- cursor: auto;
83
- }
84
- }
85
-
86
- .rtl .fs-notice
87
- {
88
- .fs-close
89
- {
90
- // left: 2px;
91
- // right: auto;
92
- // padding-right: 0;
93
- // padding-left: 2px;
94
- float: left;
95
- }
96
- }
97
-
98
- .fs-secure-notice
99
- {
100
- position: fixed;
101
- top: 32px;
102
- left: 160px;
103
- right: 0;
104
- background: rgb(235, 253, 235);
105
- padding: 10px 20px;
106
- color: green;
107
- z-index: 9999;
108
- box-shadow: 0px 2px 2px rgba(6, 113, 6, 0.3);
109
- @include opacity(0.95);
110
-
111
- &:hover
112
- {
113
- @include opacity(1);
114
- }
115
- }
116
-
117
- @media screen and (max-width: 960px) {
118
- .fs-secure-notice
119
- {
120
- left: 36px;
121
- }
122
- }
123
-
124
- @media screen and (max-width:500px) {
125
- #fs_promo_tab {
126
- display: none;
127
- }
128
- }
129
-
130
- @media screen and (max-width: 782px) {
131
- .fs-secure-notice
132
- {
133
- left: 0;
134
- top: 46px;
135
- text-align: center;
136
- }
137
- }
138
-
139
- span.fs-submenu-item.fs-sub:before
140
- {
141
- // Add small arrow.
142
- content: '\21B3';
143
- padding: 0 5px;
144
- }
145
-
146
- .rtl
147
- {
148
- span.fs-submenu-item.fs-sub:before
149
- {
150
- // Add small RTL arrow.
151
- content: '\21B2';
152
- }
153
- }
154
-
155
- .fs-submenu-item
156
- {
157
- &.pricing
158
- {
159
- &.upgrade-mode
160
- {
161
- color: greenyellow;
162
- }
163
-
164
- &.trial-mode
165
- {
166
- color: #83e2ff;
167
- }
168
- }
169
- }
170
-
171
- #adminmenu .update-plugins.fs-trial {
172
- background-color: #00b9eb;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
  }
1
+ @import "../start";
2
+ @import "themes";
3
+
4
+ #iframe
5
+ {
6
+ line-height: 0;
7
+ font-size: 0;
8
+ }
9
+
10
+ .fs-full-size-wrapper
11
+ {
12
+ margin: 40px 0 -65px -20px;
13
+
14
+ @media (max-width: 600px) {
15
+ margin: 0 0 -65px -10px;
16
+ }
17
+ }
18
+
19
+ .fs-notice
20
+ {
21
+ position: relative;
22
+
23
+ &.fs-has-title
24
+ {
25
+ margin-bottom: 30px !important;
26
+ }
27
+
28
+ &.success
29
+ {
30
+ color: green;
31
+ // font-weight: normal;
32
+ }
33
+
34
+ &.promotion
35
+ {
36
+ border-color: $fs-notice-promotion-border-color !important;
37
+ background-color: $fs-notice-promotion-bkg !important;
38
+ }
39
+
40
+ .fs-notice-body
41
+ {
42
+ margin: .5em 0;
43
+ padding: 2px;
44
+ }
45
+
46
+ .fs-close
47
+ {
48
+ // position: absolute;
49
+ // top: 2px;
50
+ // bottom: 2px;
51
+ // right: 2px;
52
+ // min-width: 100px;
53
+ // text-align: center;
54
+ // padding-right: 2px;
55
+ cursor: pointer;
56
+ color: #aaa;
57
+ float: right;
58
+
59
+ &:hover
60
+ {
61
+ color: #666;
62
+ // background: #A9A9A9;
63
+ }
64
+
65
+ > *
66
+ {
67
+ margin-top: 7px;
68
+ display: inline-block;
69
+ }
70
+ }
71
+
72
+ label.fs-plugin-title
73
+ {
74
+ background: rgba(0, 0, 0, 0.3);
75
+ color: #fff;
76
+ padding: 2px 10px;
77
+ position: absolute;
78
+ top: 100%;
79
+ bottom: auto;
80
+ right: auto;
81
+ @include border-radius(0 0 3px 3px);
82
+ left: 10px;
83
+ font-size: 12px;
84
+ font-weight: bold;
85
+ cursor: auto;
86
+ }
87
+ }
88
+
89
+ div.fs-notice
90
+ {
91
+ &.updated,
92
+ &.success,
93
+ &.promotion
94
+ {
95
+ display: block !important;
96
+ }
97
+ }
98
+
99
+ .rtl .fs-notice
100
+ {
101
+ .fs-close
102
+ {
103
+ // left: 2px;
104
+ // right: auto;
105
+ // padding-right: 0;
106
+ // padding-left: 2px;
107
+ float: left;
108
+ }
109
+ }
110
+
111
+ .fs-secure-notice
112
+ {
113
+ position: fixed;
114
+ top: 32px;
115
+ left: 160px;
116
+ right: 0;
117
+ background: rgb(235, 253, 235);
118
+ padding: 10px 20px;
119
+ color: green;
120
+ z-index: 9999;
121
+ @include box-shadow(0 2px 2px rgba(6, 113, 6, 0.3));
122
+ @include opacity(0.95);
123
+
124
+ &:hover
125
+ {
126
+ @include opacity(1);
127
+ }
128
+
129
+ a.fs-security-proof
130
+ {
131
+ color: green;
132
+ text-decoration: none;
133
+ }
134
+ }
135
+
136
+ @media screen and (max-width: 960px) {
137
+ .fs-secure-notice
138
+ {
139
+ left: 36px;
140
+ }
141
+ }
142
+
143
+ @media screen and (max-width: 600px) {
144
+ .fs-secure-notice
145
+ {
146
+ display: none;
147
+ }
148
+ }
149
+
150
+ @media screen and (max-width: 500px) {
151
+ #fs_promo_tab
152
+ {
153
+ display: none;
154
+ }
155
+ }
156
+
157
+ @media screen and (max-width: 782px) {
158
+ .fs-secure-notice
159
+ {
160
+ left: 0;
161
+ top: 46px;
162
+ text-align: center;
163
+ }
164
+ }
165
+
166
+ span.fs-submenu-item.fs-sub:before
167
+ {
168
+ // Add small arrow.
169
+ content: '\21B3';
170
+ padding: 0 5px;
171
+ }
172
+
173
+ .rtl
174
+ {
175
+ span.fs-submenu-item.fs-sub:before
176
+ {
177
+ // Add small RTL arrow.
178
+ content: '\21B2';
179
+ }
180
+ }
181
+
182
+ .fs-submenu-item
183
+ {
184
+ &.pricing
185
+ {
186
+ &.upgrade-mode
187
+ {
188
+ color: greenyellow;
189
+ }
190
+
191
+ &.trial-mode
192
+ {
193
+ color: #83e2ff;
194
+ }
195
+ }
196
+ }
197
+
198
+ #adminmenu .update-plugins.fs-trial
199
+ {
200
+ background-color: #00b9eb;
201
+ }
202
+ .fs-ajax-spinner
203
+ {
204
+ border: 0;
205
+ width: 20px;
206
+ height: 20px;
207
+ margin-right: 5px;
208
+ vertical-align: sub;
209
+ display: inline-block;
210
+ background: url('../../../../../../../wp-admin/images/wpspin_light-2x.gif');
211
+ background-size: contain;
212
+ }
213
+
214
+ .wrap.fs-section {
215
+ h2 {
216
+ text-align: left;
217
+ }
218
  }
freemius/assets/scss/admin/connect.scss CHANGED
@@ -405,6 +405,55 @@ $form_width: 480px;
405
  }
406
  }
407
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
408
  .wp-pointer-content
409
  {
410
  #fs_connect
405
  }
406
  }
407
 
408
+ #fs_theme_connect_wrapper {
409
+ position: fixed;
410
+ top: 0;
411
+ height: 100%;
412
+ width: 100%;
413
+ z-index: 99990;
414
+ background: rgba(0, 0, 0, 0.75);
415
+ text-align: center;
416
+ overflow-y: auto;
417
+
418
+ &:before {
419
+ content: "";
420
+ display: inline-block;
421
+ vertical-align: middle;
422
+ height: 100%;
423
+ }
424
+
425
+ > button.close {
426
+ color: white;
427
+ cursor: pointer;
428
+ height: 40px;
429
+ width: 40px;
430
+ position: absolute;
431
+ right: 0;
432
+ border: 0;
433
+ background-color: transparent;
434
+ top: 32px;
435
+ }
436
+
437
+ #fs_connect {
438
+ top: 0;
439
+ text-align: left;
440
+ display: inline-block;
441
+ vertical-align: middle;
442
+ margin-top: 52px;
443
+ margin-bottom: 20px;
444
+
445
+ .fs-terms
446
+ {
447
+ background: rgba(140, 140, 140, 0.64);
448
+
449
+ &, a
450
+ {
451
+ color: #c5c5c5;
452
+ }
453
+ }
454
+ }
455
+ }
456
+
457
  .wp-pointer-content
458
  {
459
  #fs_connect
freemius/assets/scss/admin/index.php ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ <?php
2
+ // Silence is golden.
3
+ // Hide file structure from users on unprotected servers.
freemius/assets/scss/customizer.scss ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ @import "start";
2
+
3
+ #fs_customizer_upsell {
4
+ .fs-customizer-plan {
5
+ padding: 10px 20px 20px 20px;
6
+ border-radius: 3px;
7
+ background: #fff;
8
+
9
+ h2 {
10
+ position: relative;
11
+ margin: 0;
12
+ line-height: 2em;
13
+ text-transform: uppercase;
14
+
15
+ .button-link {
16
+ top: -2px;
17
+ }
18
+ }
19
+ }
20
+
21
+ .fs-feature {
22
+ position: relative;
23
+ }
24
+
25
+ .dashicons-yes {
26
+ color: #0085ba;
27
+ font-size: 2em;
28
+ vertical-align: bottom;
29
+ margin-left: -7px;
30
+ margin-right: 10px;
31
+
32
+ .rtl & {
33
+ margin-left: 10px;
34
+ margin-right: -7px;
35
+ }
36
+ }
37
+
38
+ .dashicons-editor-help
39
+ {
40
+ color: #bbb;
41
+ cursor: help;
42
+
43
+ $tooltip-color: #000;
44
+
45
+ .fs-feature-desc {
46
+ opacity: 0;
47
+ visibility: hidden;
48
+ @include transition(opacity 0.3s ease-in-out);
49
+
50
+ position: absolute;
51
+ background: $tooltip-color;
52
+ color: #fff;
53
+ font-family: 'arial', serif;
54
+ font-size: 12px;
55
+ padding: 10px;
56
+ z-index: 999999;
57
+ bottom: 100%;
58
+ margin-bottom: 5px;
59
+ left: 0;
60
+ right: 0;
61
+ @include border-radius(5px);
62
+ @include box-shadow(1px 1px 1px rgba(0,0,0,0.2));
63
+ line-height: 1.3em;
64
+ font-weight: bold;
65
+ text-align: left;
66
+
67
+ .rtl &
68
+ {
69
+ text-align: right;
70
+ }
71
+
72
+ &::after {
73
+ content: ' ';
74
+ display: block;
75
+ width: 0;
76
+ height: 0;
77
+ border-style: solid;
78
+ border-width: 5px 5px 0 5px;
79
+ border-color: $tooltip-color transparent transparent transparent;
80
+ position: absolute;
81
+ top: 100%;
82
+ left: 21px;
83
+
84
+ .rtl & {
85
+ right: 21px;
86
+ left: auto;
87
+ }
88
+ }
89
+ }
90
+
91
+ &:hover {
92
+ .fs-feature-desc {
93
+ visibility: visible;
94
+ opacity: 1;
95
+ }
96
+ }
97
+ }
98
+
99
+ .button-primary {
100
+ display: block;
101
+ text-align: center;
102
+ margin-top: 10px;
103
+ }
104
+ }
105
+
106
+ #fs_customizer_support
107
+ {
108
+ display: block !important;
109
+
110
+ .button {
111
+ float: right;
112
+ }
113
+
114
+ .button-group {
115
+ width: 100%;
116
+ display: block;
117
+ margin-top: 10px;
118
+
119
+ .button {
120
+ float: none;
121
+ width: 50%;
122
+ text-align: center;
123
+ }
124
+ }
125
+ }
freemius/assets/scss/index.php ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ <?php
2
+ // Silence is golden.
3
+ // Hide file structure from users on unprotected servers.
freemius/composer.json ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "freemius/wordpress-sdk",
3
+ "description": "Freemius WordPress SDK",
4
+ "keywords": ["freemius", "wordpress", "plugin", "sdk"],
5
+ "homepage": "https://freemius.com",
6
+ "license": "GPL-3.0",
7
+ "require": {
8
+ "php": ">=5.2"
9
+ }
10
+ }
freemius/config.php CHANGED
@@ -2,7 +2,7 @@
2
  /**
3
  * @package Freemius
4
  * @copyright Copyright (c) 2015, Freemius, Inc.
5
- * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
6
  * @since 1.0.4
7
  */
8
 
@@ -126,6 +126,7 @@
126
  /**
127
  * Domain / URL / Address
128
  */
 
129
  define( 'WP_FS__DOMAIN_PRODUCTION', 'wp.freemius.com' );
130
  define( 'WP_FS__ADDRESS_PRODUCTION', 'https://' . WP_FS__DOMAIN_PRODUCTION );
131
 
@@ -230,7 +231,7 @@
230
  }
231
 
232
  if ( ! defined( 'FS_CHECKOUT__ADDRESS_LOCALHOST' ) ) {
233
- define( 'FS_CHECKOUT__ADDRESS_LOCALHOST', 'http://checkout.freemius:8080' );
234
  }
235
 
236
  if ( ! defined( 'FS_CHECKOUT__ADDRESS' ) ) {
@@ -284,6 +285,9 @@
284
  if ( ! defined( 'WP_FS__TIME_24_HOURS_IN_SEC' ) ) {
285
  define( 'WP_FS__TIME_24_HOURS_IN_SEC', 86400 );
286
  }
 
 
 
287
 
288
  #--------------------------------------------------------------------------------
289
  #region Debugging
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.4
7
  */
8
 
126
  /**
127
  * Domain / URL / Address
128
  */
129
+ define( 'WP_FS__ROOT_DOMAIN_PRODUCTION', 'freemius.com' );
130
  define( 'WP_FS__DOMAIN_PRODUCTION', 'wp.freemius.com' );
131
  define( 'WP_FS__ADDRESS_PRODUCTION', 'https://' . WP_FS__DOMAIN_PRODUCTION );
132
 
231
  }
232
 
233
  if ( ! defined( 'FS_CHECKOUT__ADDRESS_LOCALHOST' ) ) {
234
+ define( 'FS_CHECKOUT__ADDRESS_LOCALHOST', 'http://checkout.freemius-local.com:8080' );
235
  }
236
 
237
  if ( ! defined( 'FS_CHECKOUT__ADDRESS' ) ) {
285
  if ( ! defined( 'WP_FS__TIME_24_HOURS_IN_SEC' ) ) {
286
  define( 'WP_FS__TIME_24_HOURS_IN_SEC', 86400 );
287
  }
288
+ if ( ! defined( 'WP_FS__TIME_WEEK_IN_SEC' ) ) {
289
+ define( 'WP_FS__TIME_WEEK_IN_SEC', 7 * WP_FS__TIME_24_HOURS_IN_SEC );
290
+ }
291
 
292
  #--------------------------------------------------------------------------------
293
  #region Debugging
freemius/gulpfile.js ADDED
@@ -0,0 +1,167 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ var gulp = require('gulp');
2
+ var path = require('path');
3
+ var filesystem = require('fs');
4
+ var wpPot = require('gulp-wp-pot');
5
+ var gettext = require('gulp-gettext');
6
+ var sort = require('gulp-sort');
7
+ var pofill = require('gulp-pofill');
8
+ var rename = require('gulp-rename');
9
+ var clean = require('gulp-clean');
10
+
11
+ var languagesFolder = './languages/';
12
+
13
+ var options = require('./transifex-config.json');
14
+
15
+ function getFolders(dir) {
16
+ return filesystem.readdirSync(dir)
17
+ .filter(function (file) {
18
+ return filesystem.statSync(path.join(dir, file)).isDirectory();
19
+ });
20
+ }
21
+
22
+ var transifex = require('gulp-transifex').createClient(options);
23
+
24
+ // Create POT out of i18n.php.
25
+ gulp.task('prepare-source', function () {
26
+ gulp.src('**/*.php')
27
+ .pipe(sort())
28
+ .pipe(wpPot({
29
+ destFile : 'freemius.pot',
30
+ package : 'freemius',
31
+ bugReport : 'https://github.com/Freemius/wordpress-sdk/issues',
32
+ lastTranslator : 'Vova Feldman <vova@freemius.com>',
33
+ team : 'Freemius Team <admin@freemius.com>',
34
+ /*gettextMethods: {
35
+ instances: ['this', '_fs'],
36
+ methods: [
37
+ 'get_text_inline'
38
+ ]
39
+ },*/
40
+ gettextFunctions: [
41
+ {name: 'get_text_inline'},
42
+
43
+ {name: 'fs_text_inline'},
44
+ {name: 'fs_echo_inline'},
45
+ {name: 'fs_esc_js_inline'},
46
+ {name: 'fs_esc_attr_inline'},
47
+ {name: 'fs_esc_attr_echo_inline'},
48
+ {name: 'fs_esc_html_inline'},
49
+ {name: 'fs_esc_html_echo_inline'},
50
+
51
+ {name: 'get_text_x_inline', context: 2},
52
+ {name: 'fs_text_x_inline', context: 2},
53
+ {name: 'fs_echo_x_inline', context: 2},
54
+ {name: 'fs_esc_attr_x_inline', context: 2},
55
+ {name: 'fs_esc_js_x_inline', context: 2},
56
+ {name: 'fs_esc_js_echo_x_inline', context: 2},
57
+ {name: 'fs_esc_html_x_inline', context: 2},
58
+ {name: 'fs_esc_html_echo_x_inline', context: 2}
59
+ /*,
60
+
61
+
62
+ {name: '_fs_text'},
63
+ {name: '_fs_x', context: 2},
64
+ {name: '_fs_echo'},
65
+ {name: '_fs_esc_attr'},
66
+ {name: '_fs_esc_attr_echo'},
67
+ {name: '_fs_esc_html'},
68
+ {name: '_fs_esc_html_echo'},
69
+ {name: '_fs_ex', context: 2},
70
+ {name: '_fs_esc_attr_x', context: 2},
71
+ {name: '_fs_esc_html_x', context: 2},
72
+
73
+ {name: '_fs_n', plural: 2},
74
+ {name: '_fs_n_noop', plural: 2},
75
+ {name: '_fs_nx', plural: 2, context: 4},
76
+ {name: '_fs_nx_noop', plural: 2, context: 3}*/
77
+ ]
78
+ }))
79
+ .pipe(gulp.dest(languagesFolder + 'freemius.pot'));
80
+
81
+ // Create English PO out of the POT.
82
+ return gulp.src(languagesFolder + 'freemius.pot')
83
+ .pipe(pofill({
84
+ items: function (item) {
85
+ // If msgstr is empty, use identity translation
86
+ if (!item.msgstr.length) {
87
+ item.msgstr = [''];
88
+ }
89
+ if (!item.msgstr[0]) {
90
+ item.msgstr[0] = item.msgid;
91
+ }
92
+ return item;
93
+ }
94
+ }))
95
+ .pipe(rename('freemius-en.po'))
96
+ .pipe(gulp.dest(languagesFolder));
97
+ });
98
+
99
+ // Push updated po resource to transifex.
100
+ gulp.task('update-transifex', ['prepare-source'], function () {
101
+ return gulp.src(languagesFolder + 'freemius-en.po')
102
+ .pipe(transifex.pushResource());
103
+ });
104
+
105
+ // Download latest *.po translations.
106
+ gulp.task('download-translations', ['update-transifex'], function () {
107
+ return gulp.src(languagesFolder + 'freemius-en.po')
108
+ .pipe(transifex.pullResource());
109
+ });
110
+
111
+ // Move translations to languages root.
112
+ gulp.task('prepare-translations', ['download-translations'], function () {
113
+ var folders = getFolders(languagesFolder);
114
+
115
+ return folders.map(function (folder) {
116
+ return gulp.src(path.join(languagesFolder, folder, 'freemius-en.po'))
117
+ .pipe(rename('freemius-' + folder + '.po'))
118
+ .pipe(gulp.dest(languagesFolder));
119
+ });
120
+ });
121
+
122
+ // Feel up empty translations with English.
123
+ gulp.task('translations-feelup', ['prepare-translations'], function () {
124
+ return gulp.src(languagesFolder + '*.po')
125
+ .pipe(pofill({
126
+ items: function (item) {
127
+ // If msgstr is empty, use identity translation
128
+ if (0 == item.msgstr.length) {
129
+ item.msgstr = [''];
130
+ }
131
+ if (0 == item.msgstr[0].length) {
132
+ // item.msgid[0] = item.msgid;
133
+ item.msgstr[0] = item.msgid;
134
+ }
135
+ return item;
136
+ }
137
+ }))
138
+ .pipe(gulp.dest(languagesFolder));
139
+ });
140
+
141
+ // Cleanup temporary translation folders.
142
+ gulp.task('cleanup', ['prepare-translations'], function () {
143
+ var folders = getFolders(languagesFolder);
144
+
145
+ return folders.map(function (folder) {
146
+ return gulp.src(path.join(languagesFolder, folder), {read: false})
147
+ .pipe(clean());
148
+ });
149
+ });
150
+
151
+ // Compile *.po to *.mo binaries for usage.
152
+ gulp.task('compile-translations', ['translations-feelup'], function () {
153
+ // Compile POs to MOs.
154
+ return gulp.src(languagesFolder + '*.po')
155
+ .pipe(gettext())
156
+ .pipe(gulp.dest(languagesFolder))
157
+ });
158
+
159
+ gulp.task('default', [], function () {
160
+ gulp.run('prepare-source');
161
+ gulp.run('update-transifex');
162
+ gulp.run('download-translations');
163
+ gulp.run('prepare-translations');
164
+ gulp.run('translations-feelup');
165
+ gulp.run('cleanup');
166
+ gulp.run('compile-translations');
167
+ });
freemius/includes/class-freemius-abstract.php CHANGED
@@ -2,7 +2,7 @@
2
  /**
3
  * @package Freemius
4
  * @copyright Copyright (c) 2015, Freemius, Inc.
5
- * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
6
  * @since 1.0.7
7
  */
8
 
@@ -594,4 +594,4 @@
594
  abstract function is_business();
595
 
596
  #endregion
597
- }
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.7
7
  */
8
 
594
  abstract function is_business();
595
 
596
  #endregion
597
+ }
freemius/includes/class-freemius.php CHANGED
@@ -1,12298 +1,14323 @@
1
- <?php
2
- /**
3
- * @package Freemius
4
- * @copyright Copyright (c) 2015, Freemius, Inc.
5
- * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
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
- #endregion Plugin Info
72
-
73
- /**
74
- * @since 1.0.9
75
- *
76
- * @var bool If false, don't turn Freemius on.
77
- */
78
- private $_is_on;
79
-
80
- /**
81
- * @since 1.1.3
82
- *
83
- * @var bool If false, don't turn Freemius on.
84
- */
85
- private $_is_anonymous;
86
-
87
- /**
88
- * @since 1.0.9
89
- * @var bool If false, issues with connectivity to Freemius API.
90
- */
91
- private $_has_api_connection;
92
-
93
- /**
94
- * @since 1.0.9
95
- * @var bool Hints the SDK if plugin can support anonymous mode (if skip connect is visible).
96
- */
97
- private $_enable_anonymous;
98
-
99
- /**
100
- * @since 1.1.7.5
101
- * @var bool Hints the SDK if plugin should run in anonymous mode (only adds feedback form).
102
- */
103
- private $_anonymous_mode;
104
-
105
- /**
106
- * @since 1.1.9
107
- * @var bool Hints the SDK if plugin have any free plans.
108
- */
109
- private $_is_premium_only;
110
-
111
- /**
112
- * @since 1.2.1.6
113
- * @var bool Hints the SDK if plugin have premium code version at all.
114
- */
115
- private $_has_premium_version;
116
-
117
- /**
118
- * @since 1.2.1.6
119
- * @var bool Hints the SDK if plugin should ignore pending mode by simulating a skip.
120
- */
121
- private $_ignore_pending_mode;
122
-
123
- /**
124
- * @since 1.0.8
125
- * @var bool Hints the SDK if the plugin has any paid plans.
126
- */
127
- private $_has_paid_plans;
128
-
129
- /**
130
- * @since 1.2.1.5
131
- * @var int Hints the SDK if the plugin offers a trial period. If negative, no trial, if zero - has a trial but
132
- * without a specified period, if positive - the number of trial days.
133
- */
134
- private $_trial_days = - 1;
135
-
136
- /**
137
- * @since 1.2.1.5
138
- * @var bool Hints the SDK if the trial requires a payment method or not.
139
- */
140
- private $_is_trial_require_payment = false;
141
-
142
- /**
143
- * @since 1.0.7
144
- * @var bool Hints the SDK if the plugin is WordPress.org compliant.
145
- */
146
- private $_is_org_compliant;
147
-
148
- /**
149
- * @since 1.0.7
150
- * @var bool Hints the SDK if the plugin is has add-ons.
151
- */
152
- private $_has_addons;
153
-
154
- /**
155
- * @since 1.1.6
156
- * @var string[]bool.
157
- */
158
- private $_permissions;
159
-
160
- /**
161
- * @var FS_Key_Value_Storage
162
- */
163
- private $_storage;
164
-
165
- /**
166
- * @since 1.0.0
167
- *
168
- * @var FS_Logger
169
- */
170
- private $_logger;
171
- /**
172
- * @since 1.0.4
173
- *
174
- * @var FS_Plugin
175
- */
176
- private $_plugin = false;
177
- /**
178
- * @since 1.0.4
179
- *
180
- * @var FS_Plugin|false
181
- */
182
- private $_parent_plugin = false;
183
- /**
184
- * @since 1.1.1
185
- *
186
- * @var Freemius
187
- */
188
- private $_parent = false;
189
- /**
190
- * @since 1.0.1
191
- *
192
- * @var FS_User
193
- */
194
- private $_user = false;
195
- /**
196
- * @since 1.0.1
197
- *
198
- * @var FS_Site
199
- */
200
- private $_site = false;
201
- /**
202
- * @since 1.0.1
203
- *
204
- * @var FS_Plugin_License
205
- */
206
- private $_license;
207
- /**
208
- * @since 1.0.2
209
- *
210
- * @var FS_Plugin_Plan[]
211
- */
212
- private $_plans = false;
213
- /**
214
- * @var FS_Plugin_License[]
215
- * @since 1.0.5
216
- */
217
- private $_licenses = false;
218
-
219
- /**
220
- * @since 1.0.1
221
- *
222
- * @var FS_Admin_Menu_Manager
223
- */
224
- private $_menu;
225
-
226
- /**
227
- * @var FS_Admin_Notice_Manager
228
- */
229
- private $_admin_notices;
230
-
231
- /**
232
- * @since 1.1.6
233
- *
234
- * @var FS_Admin_Notice_Manager
235
- */
236
- private static $_global_admin_notices;
237
-
238
- /**
239
- * @var FS_Logger
240
- * @since 1.0.0
241
- */
242
- private static $_static_logger;
243
-
244
- /**
245
- * @var FS_Option_Manager
246
- * @since 1.0.2
247
- */
248
- private static $_accounts;
249
-
250
- /**
251
- * @var Freemius[]
252
- */
253
- private static $_instances = array();
254
-
255
- #region Uninstall Reasons IDs
256
-
257
- const REASON_NO_LONGER_NEEDED = 1;
258
- const REASON_FOUND_A_BETTER_PLUGIN = 2;
259
- const REASON_NEEDED_FOR_A_SHORT_PERIOD = 3;
260
- const REASON_BROKE_MY_SITE = 4;
261
- const REASON_SUDDENLY_STOPPED_WORKING = 5;
262
- const REASON_CANT_PAY_ANYMORE = 6;
263
- const REASON_OTHER = 7;
264
- const REASON_DIDNT_WORK = 8;
265
- const REASON_DONT_LIKE_TO_SHARE_MY_INFORMATION = 9;
266
- const REASON_COULDNT_MAKE_IT_WORK = 10;
267
- const REASON_GREAT_BUT_NEED_SPECIFIC_FEATURE = 11;
268
- const REASON_NOT_WORKING = 12;
269
- const REASON_NOT_WHAT_I_WAS_LOOKING_FOR = 13;
270
- const REASON_DIDNT_WORK_AS_EXPECTED = 14;
271
- const REASON_TEMPORARY_DEACTIVATION = 15;
272
-
273
- #endregion
274
-
275
- /* Ctor
276
- ------------------------------------------------------------------------------------------------------------------*/
277
-
278
- /**
279
- * Main singleton instance.
280
- *
281
- * @author Vova Feldman (@svovaf)
282
- * @since 1.0.0
283
- *
284
- * @param string $slug
285
- * @param bool $is_init Since 1.2.1 Is initiation sequence.
286
- */
287
- private function __construct( $slug, $is_init = false ) {
288
- $this->_slug = $slug;
289
-
290
- $this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_' . $slug, WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
291
-
292
- $this->_storage = FS_Key_Value_Storage::instance( 'plugin_data', $this->_slug );
293
-
294
- $this->_plugin_main_file_path = $this->_find_caller_plugin_file( $is_init );
295
- $this->_plugin_dir_path = plugin_dir_path( $this->_plugin_main_file_path );
296
- $this->_plugin_basename = plugin_basename( $this->_plugin_main_file_path );
297
- $this->_free_plugin_basename = str_replace( '-premium/', '/', $this->_plugin_basename );
298
-
299
- $base_name_split = explode( '/', $this->_plugin_basename );
300
- $this->_plugin_dir_name = $base_name_split[0];
301
-
302
- if ( $this->_logger->is_on() ) {
303
- $this->_logger->info( 'plugin_main_file_path = ' . $this->_plugin_main_file_path );
304
- $this->_logger->info( 'plugin_dir_path = ' . $this->_plugin_dir_path );
305
- $this->_logger->info( 'plugin_basename = ' . $this->_plugin_basename );
306
- $this->_logger->info( 'free_plugin_basename = ' . $this->_free_plugin_basename );
307
- $this->_logger->info( 'plugin_dir_name = ' . $this->_plugin_dir_name );
308
- }
309
-
310
- // Remember link between file to slug.
311
- $this->store_file_slug_map();
312
-
313
- // Store plugin's initial install timestamp.
314
- if ( ! isset( $this->_storage->install_timestamp ) ) {
315
- $this->_storage->install_timestamp = WP_FS__SCRIPT_START_TIME;
316
- }
317
-
318
- $this->_plugin = FS_Plugin_Manager::instance( $this->_slug )->get();
319
-
320
- $this->_admin_notices = FS_Admin_Notice_Manager::instance(
321
- $slug,
322
- is_object( $this->_plugin ) ? $this->_plugin->title : ''
323
- );
324
-
325
- if ( 'true' === fs_request_get( 'fs_clear_api_cache' ) ||
326
- 'true' === fs_request_is_action( 'restart_freemius' )
327
- ) {
328
- FS_Api::clear_cache();
329
- }
330
-
331
- $this->_register_hooks();
332
-
333
- $this->_load_account();
334
-
335
- $this->_version_updates_handler();
336
- }
337
-
338
- /**
339
- * Checks whether this plugin or theme has settings menu.
340
- *
341
- * @author Leo Fajardo (@leorw)
342
- * @since 1.2.2
343
- *
344
- * @return bool
345
- */
346
- function has_settings_menu() {
347
- /**
348
- * At the moment the wp.org require to show the opt-in in
349
- * the themes page. Therefore, if the theme is .org compliant,
350
- * treat it as if it doesn't have a menu item.
351
- */
352
- if ( $this->is_theme() && $this->is_org_repo_compliant() ) {
353
- return false;
354
- }
355
-
356
- return $this->_menu->has_menu();
357
- }
358
-
359
- /**
360
- * Checks whether this a submenu item is visible.
361
- *
362
- * @author Vova Feldman (@svovaf)
363
- * @since 1.2.2.6
364
- *
365
- * @param string $slug
366
- *
367
- * @return bool
368
- */
369
- function is_submenu_item_visible( $slug ) {
370
- if ( ! $this->has_settings_menu() ) {
371
- return false;
372
- }
373
-
374
- return $this->_menu->is_submenu_item_visible( $slug );
375
- }
376
-
377
- /**
378
- * @author Vova Feldman (@svovaf)
379
- * @since 1.0.9
380
- */
381
- private function _version_updates_handler() {
382
- if ( ! isset( $this->_storage->sdk_version ) || $this->_storage->sdk_version != $this->version ) {
383
- // Freemius version upgrade mode.
384
- $this->_storage->sdk_last_version = $this->_storage->sdk_version;
385
- $this->_storage->sdk_version = $this->version;
386
-
387
- if ( empty( $this->_storage->sdk_last_version ) ||
388
- version_compare( $this->_storage->sdk_last_version, $this->version, '<' )
389
- ) {
390
- $this->_storage->sdk_upgrade_mode = true;
391
- $this->_storage->sdk_downgrade_mode = false;
392
- } else {
393
- $this->_storage->sdk_downgrade_mode = true;
394
- $this->_storage->sdk_upgrade_mode = false;
395
-
396
- }
397
-
398
- $this->do_action( 'sdk_version_update', $this->_storage->sdk_last_version, $this->version );
399
- }
400
-
401
- $plugin_version = $this->get_plugin_version();
402
- if ( ! isset( $this->_storage->plugin_version ) || $this->_storage->plugin_version != $plugin_version ) {
403
- // Plugin version upgrade mode.
404
- $this->_storage->plugin_last_version = $this->_storage->plugin_version;
405
- $this->_storage->plugin_version = $plugin_version;
406
-
407
- if ( empty( $this->_storage->plugin_last_version ) ||
408
- version_compare( $this->_storage->plugin_last_version, $plugin_version, '<' )
409
- ) {
410
- $this->_storage->plugin_upgrade_mode = true;
411
- $this->_storage->plugin_downgrade_mode = false;
412
- } else {
413
- $this->_storage->plugin_downgrade_mode = true;
414
- $this->_storage->plugin_upgrade_mode = false;
415
- }
416
-
417
- if ( ! empty( $this->_storage->plugin_last_version ) ) {
418
- // Different version of the plugin was installed before, therefore it's an update.
419
- $this->_storage->is_plugin_new_install = false;
420
- }
421
-
422
- $this->do_action( 'plugin_version_update', $this->_storage->plugin_last_version, $plugin_version );
423
- }
424
- }
425
-
426
- /**
427
- * @author Vova Feldman (@svovaf)
428
- * @since 1.1.5
429
- *
430
- * @param string $sdk_prev_version
431
- * @param string $sdk_version
432
- */
433
- function _data_migration( $sdk_prev_version, $sdk_version ) {
434
- /**
435
- * @since 1.1.7.3 Fixed unwanted connectivity test cleanup.
436
- */
437
- if ( empty( $sdk_prev_version ) ) {
438
- return;
439
- }
440
-
441
- if ( version_compare( $sdk_prev_version, '1.1.5', '<' ) &&
442
- version_compare( $sdk_version, '1.1.5', '>=' )
443
- ) {
444
- // On version 1.1.5 merged connectivity and is_on data.
445
- if ( isset( $this->_storage->connectivity_test ) ) {
446
- if ( ! isset( $this->_storage->is_on ) ) {
447
- unset( $this->_storage->connectivity_test );
448
- } else {
449
- $connectivity_data = $this->_storage->connectivity_test;
450
- $connectivity_data['is_active'] = $this->_storage->is_on['is_active'];
451
- $connectivity_data['timestamp'] = $this->_storage->is_on['timestamp'];
452
-
453
- // Override.
454
- $this->_storage->connectivity_test = $connectivity_data;
455
-
456
- // Remove previous structure.
457
- unset( $this->_storage->is_on );
458
- }
459
-
460
- }
461
- }
462
- }
463
-
464
- /**
465
- * This action is connected to the 'plugins_loaded' hook and helps to determine
466
- * if this is a new plugin installation or a plugin update.
467
- *
468
- * There are 3 different use-cases:
469
- * 1) New plugin installation right with Freemius:
470
- * 1.1 _activate_plugin_event_hook() will be executed first
471
- * 1.2 Since $this->_storage->is_plugin_new_install is not set,
472
- * and $this->_storage->plugin_last_version is not set,
473
- * $this->_storage->is_plugin_new_install will be set to TRUE.
474
- * 1.3 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install will
475
- * be already set to TRUE.
476
- *
477
- * 2) Plugin update, didn't have Freemius before, and now have the SDK:
478
- * 2.1 _activate_plugin_event_hook() will not be executed, because
479
- * the activation hook do NOT fires on updates since WP 3.1.
480
- * 2.2 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install will
481
- * be empty, therefore, it will be set to FALSE.
482
- *
483
- * 3) Plugin update, had Freemius in prev version as well:
484
- * 3.1 _version_updates_handler() will be executed 1st, since FS was installed
485
- * before, $this->_storage->plugin_last_version will NOT be empty,
486
- * therefore, $this->_storage->is_plugin_new_install will be set to FALSE.
487
- * 3.2 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install is
488
- * already set, therefore, it will not be modified.
489
- *
490
- * Use-case #3 is backward compatible, #3.1 will be executed since 1.0.9.
491
- *
492
- * NOTE:
493
- * The only fallback of this mechanism is if an admin updates a plugin based on use-case #2,
494
- * and then, the next immediate PageView is the plugin's main settings page, it will not
495
- * show the opt-in right away. The reason it will happen is because Freemius execution
496
- * will be turned off till the plugin is fully loaded at least once
497
- * (till $this->_storage->was_plugin_loaded is TRUE).
498
- *
499
- * @author Vova Feldman (@svovaf)
500
- * @since 1.1.9
501
- *
502
- */
503
- function _plugins_loaded() {
504
- // Update flag that plugin was loaded with Freemius at least once.
505
- $this->_storage->was_plugin_loaded = true;
506
-
507
- if ( ! isset( $this->_storage->is_plugin_new_install ) ) {
508
- $this->_storage->is_plugin_new_install = false;
509
- }
510
- }
511
-
512
- /**
513
- * @author Vova Feldman (@svovaf)
514
- * @since 1.0.9
515
- */
516
- private function _register_hooks() {
517
- $this->_logger->entrance();
518
-
519
- if ( is_admin() ) {
520
- $plugin_dir = dirname( $this->_plugin_dir_path ) . '/';
521
-
522
- /**
523
- * @since 1.2.2
524
- *
525
- * Hook to both free and premium version activations to support
526
- * auto deactivation on the other version activation.
527
- */
528
- register_activation_hook(
529
- $plugin_dir . $this->_free_plugin_basename,
530
- array( &$this, '_activate_plugin_event_hook' )
531
- );
532
- register_activation_hook(
533
- $plugin_dir . $this->premium_plugin_basename(),
534
- array( &$this, '_activate_plugin_event_hook' )
535
- );
536
-
537
- /**
538
- * Part of the mechanism to identify new plugin install vs. plugin update.
539
- *
540
- * @author Vova Feldman (@svovaf)
541
- * @since 1.1.9
542
- */
543
- if ( empty( $this->_storage->was_plugin_loaded ) ) {
544
- if ( $this->is_activation_mode( false ) ) {
545
- add_action( 'plugins_loaded', array( &$this, '_plugins_loaded' ) );
546
- } else {
547
- // If was activated before, then it was already loaded before.
548
- $this->_plugins_loaded();
549
- }
550
- }
551
-
552
- if ( ! self::is_ajax() ) {
553
- if ( ! $this->is_addon() ) {
554
- add_action( 'init', array( &$this, '_add_default_submenu_items' ), WP_FS__LOWEST_PRIORITY );
555
- add_action( 'admin_menu', array( &$this, '_prepare_admin_menu' ), WP_FS__LOWEST_PRIORITY );
556
- }
557
- }
558
- }
559
-
560
- register_deactivation_hook( $this->_plugin_main_file_path, array( &$this, '_deactivate_plugin_hook' ) );
561
-
562
- add_action( 'init', array( &$this, '_redirect_on_clicked_menu_link' ), WP_FS__LOWEST_PRIORITY );
563
-
564
- add_action( 'admin_init', array( &$this, '_add_tracking_links' ) );
565
- add_action( 'admin_init', array( &$this, '_add_license_activation' ) );
566
- $this->add_ajax_action( 'update_billing', array( &$this, '_update_billing_ajax_action' ) );
567
- $this->add_ajax_action( 'start_trial', array( &$this, '_start_trial_ajax_action' ) );
568
-
569
- $this->add_ajax_action( 'install_premium_version', array(
570
- &$this,
571
- '_install_premium_version_ajax_action'
572
- ) );
573
-
574
- $this->add_action( 'after_plans_sync', array( &$this, '_check_for_trial_plans' ) );
575
-
576
- $this->add_action( 'sdk_version_update', array( &$this, '_data_migration' ), WP_FS__DEFAULT_PRIORITY, 2 );
577
-
578
- add_action( 'admin_init', array( &$this, '_add_trial_notice' ) );
579
- add_action( 'admin_init', array( &$this, '_enqueue_common_css' ) );
580
-
581
- /**
582
- * Handle request to reset anonymous mode for `get_reconnect_url()`.
583
- *
584
- * @author Vova Feldman (@svovaf)
585
- * @since 1.2.1.5
586
- */
587
- if ( fs_request_is_action( 'reset_anonymous_mode' ) &&
588
- $this->_slug === fs_request_get( 'fs_slug' )
589
- ) {
590
- add_action( 'admin_init', array( &$this, 'connect_again' ) );
591
- }
592
- }
593
-
594
- /**
595
- * Keeping the uninstall hook registered for free or premium plugin version may result to a fatal error that
596
- * could happen when a user tries to uninstall either version while one of them is still active. Uninstalling a
597
- * plugin will trigger inclusion of the free or premium version and if one of them is active during the
598
- * uninstallation, a fatal error may occur in case the plugin's class or functions are already defined.
599
- *
600
- * @author Leo Fajardo (leorw)
601
- *
602
- * @since 1.2.0
603
- */
604
- private function unregister_uninstall_hook() {
605
- $uninstallable_plugins = (array) get_option( 'uninstall_plugins' );
606
- unset( $uninstallable_plugins[ $this->_free_plugin_basename ] );
607
- unset( $uninstallable_plugins[ $this->premium_plugin_basename() ] );
608
-
609
- update_option( 'uninstall_plugins', $uninstallable_plugins );
610
- }
611
-
612
- /**
613
- * @since 1.2.0 Invalidate module's main file cache, otherwise, FS_Plugin_Updater will not fetch updates.
614
- */
615
- private function clear_module_main_file_cache() {
616
- if ( ! isset( $this->_storage->plugin_main_file ) ||
617
- empty( $this->_storage->plugin_main_file->path )
618
- ) {
619
- return;
620
- }
621
-
622
- $plugin_main_file = clone $this->_storage->plugin_main_file;
623
-
624
- // Store cached path (2nd layer cache).
625
- $plugin_main_file->prev_path = $plugin_main_file->path;
626
-
627
- // Clear cached path.
628
- unset( $plugin_main_file->path );
629
-
630
- $this->_storage->plugin_main_file = $plugin_main_file;
631
- }
632
-
633
- /**
634
- * @author Vova Feldman (@svovaf)
635
- * @since 1.0.9
636
- */
637
- private function _register_account_hooks() {
638
- if ( ! is_admin() ) {
639
- return;
640
- }
641
-
642
- /**
643
- * Always show the deactivation feedback form since we added
644
- * automatic free version deactivation upon premium code activation.
645
- *
646
- * @since 1.2.1.6
647
- */
648
- $this->add_ajax_action(
649
- 'submit_uninstall_reason',
650
- array( &$this, '_submit_uninstall_reason_action' )
651
- );
652
-
653
- if ( $this->is_plugins_page() ) {
654
- add_action( 'admin_footer', array( &$this, '_add_deactivation_feedback_dialog_box' ) );
655
- }
656
-
657
- if ( ! $this->is_addon() ) {
658
- if ( $this->is_registered() ) {
659
- $this->add_filter( 'after_code_type_change', array( &$this, '_after_code_type_change' ) );
660
- }
661
- }
662
- }
663
-
664
- /**
665
- * Leverage backtrace to find caller plugin file path.
666
- *
667
- * @author Vova Feldman (@svovaf)
668
- * @since 1.0.6
669
- *
670
- * @param bool $is_init Is initiation sequence.
671
- *
672
- * @return string
673
- *
674
- * @uses fs_find_caller_plugin_file
675
- */
676
- private function _find_caller_plugin_file( $is_init = false ) {
677
- // Try to load the cached value of the file path.
678
- if ( isset( $this->_storage->plugin_main_file ) ) {
679
- $plugin_main_file = $this->_storage->plugin_main_file;
680
- if ( isset( $plugin_main_file->path ) && file_exists( $plugin_main_file->path ) ) {
681
- return $plugin_main_file->path;
682
- }
683
- }
684
-
685
- /**
686
- * @since 1.2.1
687
- *
688
- * `clear_module_main_file_cache()` is clearing the plugin's cached path on
689
- * deactivation. Therefore, if any plugin/theme was initiating `Freemius`
690
- * with that plugin's slug, it was overriding the empty plugin path with a wrong path.
691
- *
692
- * So, we've added a special mechanism with a 2nd layer of cache that uses `prev_path`
693
- * when the class instantiator isn't the module.
694
- */
695
- if ( ! $is_init ) {
696
- // Fetch prev path cache.
697
- if ( isset( $this->_storage->plugin_main_file ) &&
698
- isset( $this->_storage->plugin_main_file->prev_path )
699
- ) {
700
- if ( file_exists( $this->_storage->plugin_main_file->prev_path ) ) {
701
- return $this->_storage->plugin_main_file->prev_path;
702
- }
703
- }
704
-
705
- wp_die(
706
- $this->get_text( 'failed-finding-main-path' ) .
707
- " Module: {$this->_slug}; SDK: " . WP_FS__SDK_VERSION . ";",
708
- $this->get_text( 'error' ),
709
- array( 'back_link' => true )
710
- );
711
- }
712
-
713
- /**
714
- * @since 1.2.1
715
- *
716
- * Only the original instantiator that calls dynamic_init can modify the module's path.
717
- */
718
- // Find caller module.
719
- $file_and_type = $this->get_caller_main_file_and_type();
720
- $plugin_file = $file_and_type->path;
721
-
722
- $this->_storage->plugin_main_file = (object) array(
723
- 'path' => fs_normalize_path( $plugin_file ),
724
- );
725
-
726
- return $plugin_file;
727
- }
728
-
729
- /**
730
- * Identifies the caller path.
731
- *
732
- * @todo (Vova) When merging this branch with the theme's one, use the theme's one instead of this one.
733
- *
734
- * @author Leo Fajardo (@leorw)
735
- * @since 1.2.2
736
- *
737
- * @author Vova Feldman (@svovaf)
738
- * @since 1.2.2.3 Find the earliest module in the call stack that calls to the SDK. This fix is for cases
739
- * when add-ons are relying on loading the SDK from the parent module, and also allows themes
740
- * including the SDK an internal file instead of directly from functions.php.
741
- * @since 1.2.1.7 Knows how to handle cases when an add-on includes the parent module logic.
742
- */
743
- private function get_caller_main_file_and_type() {
744
- self::require_plugin_essentials();
745
-
746
- $all_plugins = get_plugins();
747
- $all_plugins_paths = array();
748
-
749
- // Get active plugin's main files real full names (might be symlinks).
750
- foreach ( $all_plugins as $relative_path => &$data ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
751
  if ( false === strpos( fs_normalize_path( $relative_path ), '/' ) ) {
752
  /**
753
- * Ignore plugins that don't have a folder (e.g. Hello Dolly) since they
754
- * can't really include the SDK.
755
- *
756
- * @author Vova Feldman
757
- * @since 1.2.1.7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
758
  */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
759
  continue;
760
  }
761
 
762
- $all_plugins_paths[] = fs_normalize_path( realpath( WP_PLUGIN_DIR . '/' . $relative_path ) );
763
- }
764
-
765
- $caller_file_candidate = false;
766
- $caller_map = array();
767
- $module_type = WP_FS__MODULE_TYPE_PLUGIN;
768
-
769
- for ( $i = 1, $bt = debug_backtrace(), $len = count( $bt ); $i < $len; $i ++ ) {
770
- if ( empty( $bt[ $i ]['file'] ) ) {
771
- continue;
772
- }
773
-
774
- if ( $i > 1 && ! empty( $bt[ $i - 1 ]['file'] ) && $bt[ $i ]['file'] === $bt[ $i - 1 ]['file'] ) {
775
- // If file same as the prev file in the stack, skip it.
776
- continue;
777
- }
778
-
779
- if ( ! empty( $bt[ $i ]['function'] ) && in_array( $bt[ $i ]['function'], array(
780
- 'do_action',
781
- 'apply_filter',
782
- // The string split is stupid, but otherwise, theme check
783
- // throws info notices.
784
- 'requir' . 'e_once',
785
- 'requir' . 'e',
786
- 'includ' . 'e_once',
787
- 'includ' . 'e'
788
- ) )
789
- ) {
790
- // Ignore call stack hooks and files inclusion.
791
- continue;
792
- }
793
-
794
- $caller_file_path = fs_normalize_path( $bt[ $i ]['file'] );
795
- $caller_file_hash = md5( $caller_file_path );
796
-
797
- if ( ! isset( $caller_map[ $caller_file_hash ] ) ) {
798
- foreach ( $all_plugins_paths as $plugin_path ) {
799
- if ( false !== strpos( $caller_file_path, fs_normalize_path( dirname( $plugin_path ) . '/' ) ) ) {
800
- $caller_map[ $caller_file_hash ] = fs_normalize_path( $plugin_path );
801
- break;
802
- }
803
- }
804
- }
805
-
806
- if ( isset( $caller_map[ $caller_file_hash ] ) ) {
807
- $module_type = WP_FS__MODULE_TYPE_PLUGIN;
808
- $caller_file_candidate = $caller_map[ $caller_file_hash ];
809
- }
810
- }
811
-
812
- if ( empty( $caller_file_candidate ) ) {
813
- // Throw an error to the developer in case of some edge case dev environment.
814
- wp_die( $this->get_text( 'failed-finding-main-path' ), $this->get_text( 'error' ), array( 'back_link' => true ) );
815
- }
816
-
817
- return (object) array(
818
- 'module_type' => $module_type,
819
- 'path' => $caller_file_candidate
820
- );
821
- }
822
-
823
- #----------------------------------------------------------------------------------
824
- #region Deactivation Feedback Form
825
- #----------------------------------------------------------------------------------
826
-
827
- /**
828
- * Displays a confirmation and feedback dialog box when the user clicks on the "Deactivate" link on the plugins
829
- * page.
830
- *
831
- * @author Vova Feldman (@svovaf)
832
- * @author Leo Fajardo (@leorw)
833
- * @since 1.1.2
834
- */
835
- function _add_deactivation_feedback_dialog_box() {
836
- /* Check the type of user:
837
- * 1. Long-term (long-term)
838
- * 2. Non-registered and non-anonymous short-term (non-registered-and-non-anonymous-short-term).
839
- * 3. Short-term (short-term)
840
- */
841
- $is_long_term_user = true;
842
-
843
- // Check if the site is at least 2 days old.
844
- $time_installed = $this->_storage->install_timestamp;
845
-
846
- // Difference in seconds.
847
- $date_diff = time() - $time_installed;
848
-
849
- // Convert seconds to days.
850
- $date_diff_days = floor( $date_diff / ( 60 * 60 * 24 ) );
851
-
852
- if ( $date_diff_days < 2 ) {
853
- $is_long_term_user = false;
854
- }
855
-
856
- $is_long_term_user = $this->apply_filters( 'is_long_term_user', $is_long_term_user );
857
-
858
- if ( $is_long_term_user ) {
859
- $user_type = 'long-term';
860
- } else {
861
- if ( ! $this->is_registered() && ! $this->is_anonymous() ) {
862
- $user_type = 'non-registered-and-non-anonymous-short-term';
863
- } else {
864
- $user_type = 'short-term';
865
- }
866
- }
867
-
868
- $uninstall_reasons = $this->_get_uninstall_reasons( $user_type );
869
-
870
- // Load the HTML template for the deactivation feedback dialog box.
871
- $vars = array(
872
- 'reasons' => $uninstall_reasons,
873
- 'slug' => $this->_slug
874
- );
875
-
876
- /**
877
- * @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.
878
- */
879
- fs_require_template( 'forms/deactivation/form.php', $vars );
880
- }
881
-
882
- /**
883
- * @author Leo Fajardo (leorw)
884
- * @since 1.1.2
885
- *
886
- * @param string $user_type
887
- *
888
- * @return array The uninstall reasons for the specified user type.
889
- */
890
- function _get_uninstall_reasons( $user_type = 'long-term' ) {
891
- $internal_message_template_var = array(
892
- 'slug' => $this->_slug
893
- );
894
-
895
- if ( $this->is_registered() && false !== $this->get_plan() && $this->get_plan()->has_technical_support() ) {
896
- $contact_support_template = fs_get_template( 'forms/deactivation/contact.php', $internal_message_template_var );
897
- } else {
898
- $contact_support_template = '';
899
- }
900
-
901
- $reason_found_better_plugin = array(
902
- 'id' => self::REASON_FOUND_A_BETTER_PLUGIN,
903
- 'text' => $this->get_text( 'reason-found-a-better-plugin' ),
904
- 'input_type' => 'textfield',
905
- 'input_placeholder' => $this->get_text( 'placeholder-plugin-name' )
906
- );
907
-
908
- $reason_temporary_deactivation = array(
909
- 'id' => self::REASON_TEMPORARY_DEACTIVATION,
910
- 'text' => $this->get_text( 'reason-temporary-deactivation' ),
911
- 'input_type' => '',
912
- 'input_placeholder' => ''
913
- );
914
-
915
- $reason_other = array(
916
- 'id' => self::REASON_OTHER,
917
- 'text' => $this->get_text( 'reason-other' ),
918
- 'input_type' => 'textfield',
919
- 'input_placeholder' => ''
920
- );
921
-
922
- $long_term_user_reasons = array(
923
- array(
924
- 'id' => self::REASON_NO_LONGER_NEEDED,
925
- 'text' => $this->get_text( 'reason-no-longer-needed' ),
926
- 'input_type' => '',
927
- 'input_placeholder' => ''
928
- ),
929
- $reason_found_better_plugin,
930
- array(
931
- 'id' => self::REASON_NEEDED_FOR_A_SHORT_PERIOD,
932
- 'text' => $this->get_text( 'reason-needed-for-a-short-period' ),
933
- 'input_type' => '',
934
- 'input_placeholder' => ''
935
- ),
936
- array(
937
- 'id' => self::REASON_BROKE_MY_SITE,
938
- 'text' => $this->get_text( 'reason-broke-my-site' ),
939
- 'input_type' => '',
940
- 'input_placeholder' => '',
941
- 'internal_message' => $contact_support_template
942
- ),
943
- array(
944
- 'id' => self::REASON_SUDDENLY_STOPPED_WORKING,
945
- 'text' => $this->get_text( 'reason-suddenly-stopped-working' ),
946
- 'input_type' => '',
947
- 'input_placeholder' => '',
948
- 'internal_message' => $contact_support_template
949
- )
950
- );
951
-
952
- if ( $this->is_paying() ) {
953
- $long_term_user_reasons[] = array(
954
- 'id' => self::REASON_CANT_PAY_ANYMORE,
955
- 'text' => $this->get_text( 'reason-cant-pay-anymore' ),
956
- 'input_type' => 'textfield',
957
- 'input_placeholder' => $this->get_text( 'placeholder-comfortable-price' )
958
- );
959
- }
960
-
961
- $reason_dont_share_info = array(
962
- 'id' => self::REASON_DONT_LIKE_TO_SHARE_MY_INFORMATION,
963
- 'text' => $this->get_text( 'reason-dont-like-to-share-my-information' ),
964
- 'input_type' => '',
965
- 'input_placeholder' => ''
966
- );
967
-
968
- /**
969
- * If the current user has selected the "don't share data" reason in the deactivation feedback modal, inform the
970
- * user by showing additional message that he doesn't have to share data and can just choose to skip the opt-in
971
- * (the Skip button is included in the message to show). This message will only be shown if anonymous mode is
972
- * enabled and the user's account is currently not in pending activation state (similar to the way the Skip
973
- * button in the opt-in form is shown/hidden).
974
- */
975
- if ( $this->is_enable_anonymous() && ! $this->is_pending_activation() ) {
976
- $reason_dont_share_info['internal_message'] = fs_get_template( 'forms/deactivation/retry-skip.php', $internal_message_template_var );
977
- }
978
-
979
- $uninstall_reasons = array(
980
- 'long-term' => $long_term_user_reasons,
981
- 'non-registered-and-non-anonymous-short-term' => array(
982
- array(
983
- 'id' => self::REASON_DIDNT_WORK,
984
- 'text' => $this->get_text( 'reason-didnt-work' ),
985
- 'input_type' => '',
986
- 'input_placeholder' => ''
987
- ),
988
- $reason_dont_share_info,
989
- $reason_found_better_plugin
990
- ),
991
- 'short-term' => array(
992
- array(
993
- 'id' => self::REASON_COULDNT_MAKE_IT_WORK,
994
- 'text' => $this->get_text( 'reason-couldnt-make-it-work' ),
995
- 'input_type' => '',
996
- 'input_placeholder' => '',
997
- 'internal_message' => $contact_support_template
998
- ),
999
- $reason_found_better_plugin,
1000
- array(
1001
- 'id' => self::REASON_GREAT_BUT_NEED_SPECIFIC_FEATURE,
1002
- 'text' => $this->get_text( 'reason-great-but-need-specific-feature' ),
1003
- 'input_type' => 'textarea',
1004
- 'input_placeholder' => $this->get_text( 'placeholder-feature' )
1005
- ),
1006
- array(
1007
- 'id' => self::REASON_NOT_WORKING,
1008
- 'text' => $this->get_text( 'reason-not-working' ),
1009
- 'input_type' => 'textarea',
1010
- 'input_placeholder' => $this->get_text( 'placeholder-share-what-didnt-work' )
1011
- ),
1012
- array(
1013
- 'id' => self::REASON_NOT_WHAT_I_WAS_LOOKING_FOR,
1014
- 'text' => $this->get_text( 'reason-not-what-i-was-looking-for' ),
1015
- 'input_type' => 'textarea',
1016
- 'input_placeholder' => $this->get_text( 'placeholder-what-youve-been-looking-for' )
1017
- ),
1018
- array(
1019
- 'id' => self::REASON_DIDNT_WORK_AS_EXPECTED,
1020
- 'text' => $this->get_text( 'reason-didnt-work-as-expected' ),
1021
- 'input_type' => 'textarea',
1022
- 'input_placeholder' => $this->get_text( 'placeholder-what-did-you-expect' )
1023
- )
1024
- )
1025
- );
1026
-
1027
- // Randomize the reasons for the current user type.
1028
- shuffle( $uninstall_reasons[ $user_type ] );
1029
-
1030
- // Keep the following reasons as the last items in the list.
1031
- $uninstall_reasons[ $user_type ][] = $reason_temporary_deactivation;
1032
- $uninstall_reasons[ $user_type ][] = $reason_other;
1033
-
1034
- $uninstall_reasons = $this->apply_filters( 'uninstall_reasons', $uninstall_reasons );
1035
-
1036
- return $uninstall_reasons[ $user_type ];
1037
- }
1038
-
1039
- /**
1040
- * Called after the user has submitted his reason for deactivating the plugin.
1041
- *
1042
- * @author Leo Fajardo (@leorw)
1043
- * @since 1.1.2
1044
- */
1045
- function _submit_uninstall_reason_action() {
1046
- $this->_logger->entrance();
1047
-
1048
- $this->check_ajax_referer( 'submit_uninstall_reason' );
1049
-
1050
- $reason_id = fs_request_get( 'reason_id' );
1051
-
1052
- // Check if the given reason ID is an unsigned integer.
1053
- if ( ! ctype_digit( $reason_id ) ) {
1054
- exit;
1055
- }
1056
-
1057
- $reason_info = trim( fs_request_get( 'reason_info', '' ) );
1058
- if ( ! empty( $reason_info ) ) {
1059
- $reason_info = substr( $reason_info, 0, 128 );
1060
- }
1061
-
1062
- $reason = (object) array(
1063
- 'id' => $reason_id,
1064
- 'info' => $reason_info,
1065
- 'is_anonymous' => fs_request_get_bool( 'is_anonymous' )
1066
- );
1067
-
1068
- $this->_storage->store( 'uninstall_reason', $reason );
1069
-
1070
- // Print '1' for successful operation.
1071
- echo 1;
1072
- exit;
1073
- }
1074
-
1075
- #endregion
1076
-
1077
- #----------------------------------------------------------------------------------
1078
- #region Instance
1079
- #----------------------------------------------------------------------------------
1080
-
1081
- /**
1082
- * Main singleton instance.
1083
- *
1084
- * @author Vova Feldman (@svovaf)
1085
- * @since 1.0.0
1086
- *
1087
- * @param string $slug
1088
- * @param bool $is_init Is initiation sequence.
1089
- *
1090
- * @return Freemius|false
1091
- */
1092
- static function instance( $slug, $is_init = false ) {
1093
- if ( empty( $slug ) ) {
1094
- return false;
1095
- }
1096
-
1097
- $slug = strtolower( $slug );
1098
-
1099
- if ( ! isset( self::$_instances[ $slug ] ) ) {
1100
- if ( 0 === count( self::$_instances ) ) {
1101
- self::_load_required_static();
1102
- }
1103
-
1104
- self::$_instances[ $slug ] = new Freemius( $slug, $is_init );
1105
- }
1106
-
1107
- return self::$_instances[ $slug ];
1108
- }
1109
-
1110
- /**
1111
- * @author Vova Feldman (@svovaf)
1112
- * @since 1.0.6
1113
- *
1114
- * @param string|number $slug_or_id
1115
- *
1116
- * @return bool
1117
- */
1118
- private static function has_instance( $slug_or_id ) {
1119
- return ! is_numeric( $slug_or_id ) ?
1120
- isset( self::$_instances[ strtolower( $slug_or_id ) ] ) :
1121
- ( false !== self::get_instance_by_id( $slug_or_id ) );
1122
- }
1123
-
1124
- /**
1125
- * @author Vova Feldman (@svovaf)
1126
- * @since 1.0.6
1127
- *
1128
- * @param number $id
1129
- *
1130
- * @return false|Freemius
1131
- */
1132
- static function get_instance_by_id( $id ) {
1133
- foreach ( self::$_instances as $slug => $instance ) {
1134
- if ( $id == $instance->get_id() ) {
1135
- return $instance;
1136
- }
1137
- }
1138
-
1139
- return false;
1140
- }
1141
-
1142
- /**
1143
- *
1144
- * @author Vova Feldman (@svovaf)
1145
- * @since 1.0.1
1146
- *
1147
- * @param $plugin_file
1148
- *
1149
- * @return false|Freemius
1150
- */
1151
- static function get_instance_by_file( $plugin_file ) {
1152
- $slug = self::find_slug_by_basename( $plugin_file );
1153
-
1154
- return ( false !== $slug ) ?
1155
- self::instance( $slug ) :
1156
- false;
1157
- }
1158
-
1159
- /**
1160
- * @author Vova Feldman (@svovaf)
1161
- * @since 1.0.6
1162
- *
1163
- * @return false|Freemius
1164
- */
1165
- function get_parent_instance() {
1166
- return self::get_instance_by_id( $this->_plugin->parent_plugin_id );
1167
- }
1168
-
1169
- /**
1170
- * @author Vova Feldman (@svovaf)
1171
- * @since 1.0.6
1172
- *
1173
- * @param string|number $slug_or_id
1174
- *
1175
- * @return bool|Freemius
1176
- */
1177
- function get_addon_instance( $slug_or_id ) {
1178
- return ! is_numeric( $slug_or_id ) ?
1179
- self::instance( strtolower( $slug_or_id ) ) :
1180
- self::get_instance_by_id( $slug_or_id );
1181
- }
1182
-
1183
- #endregion ------------------------------------------------------------------
1184
-
1185
- /**
1186
- * @author Vova Feldman (@svovaf)
1187
- * @since 1.0.6
1188
- *
1189
- * @return bool
1190
- */
1191
- function is_parent_plugin_installed() {
1192
- return self::has_instance( $this->_plugin->parent_plugin_id );
1193
- }
1194
-
1195
- /**
1196
- * Check if add-on parent plugin in activation mode.
1197
- *
1198
- * @author Vova Feldman (@svovaf)
1199
- * @since 1.0.7
1200
- *
1201
- * @return bool
1202
- */
1203
- function is_parent_in_activation() {
1204
- $parent_fs = $this->get_parent_instance();
1205
- if ( ! is_object( $parent_fs ) ) {
1206
- return false;
1207
- }
1208
-
1209
- return ( $parent_fs->is_activation_mode() );
1210
- }
1211
-
1212
- /**
1213
- * Is plugin in activation mode.
1214
- *
1215
- * @author Vova Feldman (@svovaf)
1216
- * @since 1.0.7
1217
- *
1218
- * @param bool $and_on
1219
- *
1220
- * @return bool
1221
- */
1222
- function is_activation_mode( $and_on = true ) {
1223
- return (
1224
- ( $this->is_on() || ! $and_on ) &&
1225
- ! $this->is_registered() &&
1226
- ( ! $this->is_enable_anonymous() ||
1227
- ( ! $this->is_anonymous() && ! $this->is_pending_activation() ) )
1228
- );
1229
- }
1230
-
1231
- /**
1232
- * Check if current page is the opt-in/pending-activation page.
1233
- *
1234
- * @author Vova Feldman (@svovaf)
1235
- * @since 1.2.1.7
1236
- *
1237
- * @return bool
1238
- */
1239
- function is_activation_page() {
1240
- if ( $this->_menu->is_main_settings_page() ) {
1241
- return true;
1242
- }
1243
-
1244
- if ( ! $this->is_activation_mode() ) {
1245
- return false;
1246
- }
1247
-
1248
- // Check if current page is matching the activation page.
1249
- return $this->is_matching_url( $_SERVER['REQUEST_URI'], $this->get_activation_url() );
1250
- }
1251
-
1252
- /**
1253
- * Check if URL path's are matching and that all querystring
1254
- * arguments of the $sub_url exist in the $url with the same values.
1255
- *
1256
- * WARNING:
1257
- * 1. This method doesn't check if the sub/domain are matching.
1258
- * 2. Ignore case sensitivity.
1259
- *
1260
- * @author Vova Feldman (@svovaf)
1261
- * @since 1.2.1.7
1262
- *
1263
- * @param string $url
1264
- * @param string $sub_url
1265
- *
1266
- * @return bool
1267
- */
1268
- private function is_matching_url( $url, $sub_url ) {
1269
- $url = strtolower( $url );
1270
- $sub_url = strtolower( $sub_url );
1271
-
1272
- if ( parse_url( $sub_url, PHP_URL_PATH ) !== parse_url( $url, PHP_URL_PATH ) ) {
1273
- // Different path - DO NOT OVERRIDE PAGE.
1274
- return false;
1275
- }
1276
-
1277
- $url_params = array();
1278
- parse_str( parse_url( $url, PHP_URL_QUERY ), $url_params );
1279
-
1280
- $sub_url_params = array();
1281
- parse_str( parse_url( $sub_url, PHP_URL_QUERY ), $sub_url_params );
1282
-
1283
- foreach ( $sub_url_params as $key => $val ) {
1284
- if ( ! isset( $url_params[ $key ] ) || $val != $url_params[ $key ] ) {
1285
- // Not matching query string - DO NOT OVERRIDE PAGE.
1286
- return false;
1287
- }
1288
- }
1289
-
1290
- return true;
1291
- }
1292
-
1293
- /**
1294
- * Get collection of all active plugins.
1295
- *
1296
- * @author Vova Feldman (@svovaf)
1297
- * @since 1.0.9
1298
- *
1299
- * @return array[string]array
1300
- */
1301
- private static function get_active_plugins() {
1302
- self::require_plugin_essentials();
1303
-
1304
- $active_plugin = array();
1305
- $all_plugins = get_plugins();
1306
- $active_plugins_basenames = get_option( 'active_plugins' );
1307
-
1308
- foreach ( $active_plugins_basenames as $plugin_basename ) {
1309
- $active_plugin[ $plugin_basename ] = $all_plugins[ $plugin_basename ];
1310
- }
1311
-
1312
- return $active_plugin;
1313
- }
1314
-
1315
- /**
1316
- * Get collection of all plugins.
1317
- *
1318
- * @author Vova Feldman (@svovaf)
1319
- * @since 1.1.8
1320
- *
1321
- * @return array Key is the plugin file path and the value is an array of the plugin data.
1322
- */
1323
- private static function get_all_plugins() {
1324
- self::require_plugin_essentials();
1325
-
1326
- $all_plugins = get_plugins();
1327
- $active_plugins_basenames = get_option( 'active_plugins' );
1328
-
1329
- foreach ( $all_plugins as $basename => &$data ) {
1330
- // By default set to inactive (next foreach update the active plugins).
1331
- $data['is_active'] = false;
1332
- // Enrich with plugin slug.
1333
- $data['slug'] = self::get_plugin_slug( $basename );
1334
- }
1335
-
1336
- // Flag active plugins.
1337
- foreach ( $active_plugins_basenames as $basename ) {
1338
- if ( isset( $all_plugins[ $basename ] ) ) {
1339
- $all_plugins[ $basename ]['is_active'] = true;
1340
- }
1341
- }
1342
-
1343
- return $all_plugins;
1344
- }
1345
-
1346
-
1347
- /**
1348
- * Cached result of get_site_transient( 'update_plugins' )
1349
- *
1350
- * @author Vova Feldman (@svovaf)
1351
- * @since 1.1.8
1352
- *
1353
- * @var object
1354
- */
1355
- private static $_plugins_info;
1356
-
1357
- /**
1358
- * Helper function to get specified plugin's slug.
1359
- *
1360
- * @author Vova Feldman (@svovaf)
1361
- * @since 1.1.8
1362
- *
1363
- * @param $basename
1364
- *
1365
- * @return string
1366
- */
1367
- private static function get_plugin_slug( $basename ) {
1368
- if ( ! isset( self::$_plugins_info ) ) {
1369
- self::$_plugins_info = get_site_transient( 'update_plugins' );
1370
- }
1371
-
1372
- $slug = '';
1373
-
1374
- if ( is_object( self::$_plugins_info ) ) {
1375
- if ( isset( self::$_plugins_info->no_update ) &&
1376
- isset( self::$_plugins_info->no_update[ $basename ] ) &&
1377
- ! empty( self::$_plugins_info->no_update[ $basename ]->slug )
1378
- ) {
1379
- $slug = self::$_plugins_info->no_update[ $basename ]->slug;
1380
- } else if ( isset( self::$_plugins_info->response ) &&
1381
- isset( self::$_plugins_info->response[ $basename ] ) &&
1382
- ! empty( self::$_plugins_info->response[ $basename ]->slug )
1383
- ) {
1384
- $slug = self::$_plugins_info->response[ $basename ]->slug;
1385
- }
1386
- }
1387
-
1388
- if ( empty( $slug ) ) {
1389
- // Try to find slug from FS data.
1390
- $slug = self::find_slug_by_basename( $basename );
1391
- }
1392
-
1393
- if ( empty( $slug ) ) {
1394
- // Fallback to plugin's folder name.
1395
- $slug = dirname( $basename );
1396
- }
1397
-
1398
- return $slug;
1399
- }
1400
-
1401
- private static $_statics_loaded = false;
1402
-
1403
- /**
1404
- * Load static resources.
1405
- *
1406
- * @author Vova Feldman (@svovaf)
1407
- * @since 1.0.1
1408
- */
1409
- private static function _load_required_static() {
1410
- if ( self::$_statics_loaded ) {
1411
- return;
1412
- }
1413
-
1414
- self::$_static_logger = FS_Logger::get_logger( WP_FS__SLUG, WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
1415
-
1416
- self::$_static_logger->entrance();
1417
-
1418
- self::$_accounts = FS_Option_Manager::get_manager( WP_FS__ACCOUNTS_OPTION_NAME, true );
1419
-
1420
- self::$_global_admin_notices = FS_Admin_Notice_Manager::instance( 'global' );
1421
-
1422
- add_action( 'admin_menu', array( 'Freemius', '_add_debug_section' ) );
1423
-
1424
- add_action( "wp_ajax_fs_toggle_debug_mode", array( 'Freemius', '_toggle_debug_mode' ) );
1425
-
1426
- self::add_ajax_action_static( 'get_debug_log', array( 'Freemius', '_get_debug_log' ) );
1427
-
1428
- self::add_ajax_action_static( 'get_db_option', array( 'Freemius', '_get_db_option' ) );
1429
-
1430
- self::add_ajax_action_static( 'set_db_option', array( 'Freemius', '_set_db_option' ) );
1431
-
1432
- add_action( 'plugins_loaded', array( 'Freemius', '_load_textdomain' ), 1 );
1433
-
1434
- self::$_statics_loaded = true;
1435
- }
1436
-
1437
- #----------------------------------------------------------------------------------
1438
- #region Localization
1439
- #----------------------------------------------------------------------------------
1440
-
1441
- /**
1442
- * Load framework's text domain.
1443
- *
1444
- * @author Vova Feldman (@svovaf)
1445
- * @since 1.2.1
1446
- */
1447
- static function _load_textdomain() {
1448
- if ( ! is_admin() ) {
1449
- return;
1450
- }
1451
-
1452
- global $fs_active_plugins;
1453
-
1454
- // Works both for plugins and themes.
1455
- load_plugin_textdomain(
1456
- 'freemius',
1457
- false,
1458
- $fs_active_plugins->newest->sdk_path . '/languages/'
1459
- );
1460
- }
1461
-
1462
- #endregion
1463
-
1464
- #----------------------------------------------------------------------------------
1465
- #region Debugging
1466
- #----------------------------------------------------------------------------------
1467
-
1468
- /**
1469
- * @author Vova Feldman (@svovaf)
1470
- * @since 1.0.8
1471
- */
1472
- static function _add_debug_section() {
1473
- if ( ! current_user_can( 'activate_plugins' ) ) {
1474
- return;
1475
- }
1476
-
1477
- self::$_static_logger->entrance();
1478
-
1479
- $title = sprintf( '%s [v.%s]', fs_text( 'freemius-debug' ), WP_FS__SDK_VERSION );
1480
-
1481
- $hook = null;
1482
-
1483
- if ( WP_FS__DEV_MODE ) {
1484
- // Add top-level debug menu item.
1485
- $hook = FS_Admin_Menu_Manager::add_page(
1486
- $title,
1487
- $title,
1488
- 'manage_options',
1489
- 'freemius',
1490
- array( 'Freemius', '_debug_page_render' )
1491
- );
1492
- } else {
1493
- if ( 'freemius' === fs_request_get( 'page' ) ) {
1494
- // Add hidden debug page.
1495
- $hook = FS_Admin_Menu_Manager::add_subpage(
1496
- null,
1497
- $title,
1498
- $title,
1499
- 'manage_options',
1500
- 'freemius',
1501
- array( 'Freemius', '_debug_page_render' )
1502
- );
1503
- }
1504
- }
1505
-
1506
- if ( ! empty( $hook ) ) {
1507
- add_action( "load-$hook", array( 'Freemius', '_debug_page_actions' ) );
1508
- }
1509
- }
1510
-
1511
- /**
1512
- * @author Vova Feldman (@svovaf)
1513
- * @since 1.1.7.3
1514
- */
1515
- static function _toggle_debug_mode() {
1516
- $is_on = fs_request_get( 'is_on', false, 'post' );
1517
-
1518
- if ( fs_request_is_post() && in_array( $is_on, array( 0, 1 ) ) ) {
1519
- update_option( 'fs_debug_mode', $is_on );
1520
-
1521
- // Turn on/off storage logging.
1522
- FS_Logger::_set_storage_logging( ( 1 == $is_on ) );
1523
- }
1524
-
1525
- exit;
1526
- }
1527
-
1528
- /**
1529
- * @author Vova Feldman (@svovaf)
1530
- * @since 1.2.1.6
1531
- */
1532
- static function _get_debug_log() {
1533
- $logs = FS_Logger::load_db_logs(
1534
- fs_request_get( 'filters', false, 'post' ),
1535
- ! empty( $_POST['limit'] ) && is_numeric( $_POST['limit'] ) ? $_POST['limit'] : 200,
1536
- ! empty( $_POST['offset'] ) && is_numeric( $_POST['offset'] ) ? $_POST['offset'] : 0
1537
- );
1538
-
1539
- self::shoot_ajax_success( $logs );
1540
- }
1541
-
1542
- /**
1543
- * @author Vova Feldman (@svovaf)
1544
- * @since 1.2.1.7
1545
- */
1546
- static function _get_db_option() {
1547
- $option_name = fs_request_get( 'option_name' );
1548
-
1549
- $value = get_option( $option_name );
1550
-
1551
- $result = array(
1552
- 'name' => $option_name,
1553
- );
1554
-
1555
- if ( false !== $value ) {
1556
- if ( ! is_string( $value ) ) {
1557
- $value = json_encode( $value );
1558
- }
1559
-
1560
- $result['value'] = $value;
1561
- }
1562
-
1563
- self::shoot_ajax_success( $result );
1564
- }
1565
-
1566
- /**
1567
- * @author Vova Feldman (@svovaf)
1568
- * @since 1.2.1.7
1569
- */
1570
- static function _set_db_option() {
1571
- $option_name = fs_request_get( 'option_name' );
1572
- $option_value = fs_request_get( 'option_value' );
1573
-
1574
- if ( ! empty( $option_value ) ) {
1575
- update_option( $option_name, $option_value );
1576
- }
1577
-
1578
- self::shoot_ajax_success();
1579
- }
1580
-
1581
-
1582
- /**
1583
- * @author Vova Feldman (@svovaf)
1584
- * @since 1.0.8
1585
- */
1586
- static function _debug_page_actions() {
1587
- self::_clean_admin_content_section();
1588
-
1589
- if ( fs_request_is_action( 'restart_freemius' ) ) {
1590
- check_admin_referer( 'restart_freemius' );
1591
-
1592
- // Clear accounts data.
1593
- self::$_accounts->clear( true );
1594
-
1595
- // Clear SDK reference cache.
1596
- delete_option( 'fs_active_plugins' );
1597
- } else if ( fs_request_is_action( 'simulate_trial' ) ) {
1598
- check_admin_referer( 'simulate_trial' );
1599
-
1600
- $slug = fs_request_get( 'slug' );
1601
-
1602
- $fs = freemius( $slug );
1603
-
1604
- // Update SDK install to at least 24 hours before.
1605
- $fs->_storage->install_timestamp = ( time() - WP_FS__TIME_24_HOURS_IN_SEC );
1606
- // Unset the trial shown timestamp.
1607
- unset( $fs->_storage->trial_promotion_shown );
1608
- } else if ( fs_request_is_action( 'download_logs' ) ) {
1609
- check_admin_referer( 'download_logs' );
1610
-
1611
- $download_url = FS_Logger::download_db_logs(
1612
- fs_request_get( 'filters', false, 'post' )
1613
- );
1614
-
1615
- if ( false === $download_url ) {
1616
- 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.' );
1617
- }
1618
-
1619
- fs_redirect( $download_url );
1620
- }
1621
- }
1622
-
1623
- /**
1624
- * @author Vova Feldman (@svovaf)
1625
- * @since 1.0.8
1626
- */
1627
- static function _debug_page_render() {
1628
- self::$_static_logger->entrance();
1629
-
1630
- $vars = array(
1631
- 'sites' => self::get_all_sites(),
1632
- 'users' => self::get_all_users(),
1633
- 'addons' => self::get_all_addons(),
1634
- 'account_addons' => self::get_all_account_addons(),
1635
- 'licenses' => self::get_all_licenses(),
1636
- );
1637
-
1638
- fs_enqueue_local_style( 'fs_debug', '/admin/debug.css' );
1639
- fs_require_once_template( 'debug.php', $vars );
1640
- }
1641
-
1642
- #endregion
1643
-
1644
- #----------------------------------------------------------------------------------
1645
- #region Connectivity Issues
1646
- #----------------------------------------------------------------------------------
1647
-
1648
- /**
1649
- * Check if Freemius should be turned on for the current plugin install.
1650
- *
1651
- * Note:
1652
- * $this->_is_on is updated in has_api_connectivity()
1653
- *
1654
- * @author Vova Feldman (@svovaf)
1655
- * @since 1.0.9
1656
- *
1657
- * @return bool
1658
- */
1659
- function is_on() {
1660
- self::$_static_logger->entrance();
1661
-
1662
- if ( isset( $this->_is_on ) ) {
1663
- return $this->_is_on;
1664
- }
1665
-
1666
- // If already installed or pending then sure it's on :)
1667
- if ( $this->is_registered() || $this->is_pending_activation() ) {
1668
- $this->_is_on = true;
1669
-
1670
- return true;
1671
- }
1672
-
1673
- return false;
1674
- }
1675
-
1676
- /**
1677
- * @author Vova Feldman (@svovaf)
1678
- * @since 1.1.7.3
1679
- *
1680
- * @param bool $flush_if_no_connectivity
1681
- *
1682
- * @return bool
1683
- */
1684
- private function should_run_connectivity_test( $flush_if_no_connectivity = false ) {
1685
- if ( ! isset( $this->_storage->connectivity_test ) ) {
1686
- // Connectivity test was never executed, or cache was cleared.
1687
- return true;
1688
- }
1689
-
1690
- if ( WP_FS__PING_API_ON_IP_OR_HOST_CHANGES ) {
1691
- if ( WP_FS__IS_HTTP_REQUEST ) {
1692
- if ( $_SERVER['HTTP_HOST'] != $this->_storage->connectivity_test['host'] ) {
1693
- // Domain changed.
1694
- return true;
1695
- }
1696
-
1697
- if ( WP_FS__REMOTE_ADDR != $this->_storage->connectivity_test['server_ip'] ) {
1698
- // Server IP changed.
1699
- return true;
1700
- }
1701
- }
1702
- }
1703
-
1704
- if ( $this->_storage->connectivity_test['is_connected'] &&
1705
- $this->_storage->connectivity_test['is_active']
1706
- ) {
1707
- // API connected and Freemius is active - no need to run connectivity check.
1708
- return false;
1709
- }
1710
-
1711
- if ( $flush_if_no_connectivity ) {
1712
- /**
1713
- * If explicitly asked to flush when no connectivity - do it only
1714
- * if at least 10 sec passed from the last API connectivity test.
1715
- */
1716
- return ( isset( $this->_storage->connectivity_test['timestamp'] ) &&
1717
- ( WP_FS__SCRIPT_START_TIME - $this->_storage->connectivity_test['timestamp'] ) > 10 );
1718
- }
1719
-
1720
- /**
1721
- * @since 1.1.7 Don't check for connectivity on plugin downgrade.
1722
- */
1723
- $version = $this->get_plugin_version();
1724
- if ( version_compare( $version, $this->_storage->connectivity_test['version'], '>' ) ) {
1725
- // If it's a plugin version upgrade and Freemius is off or no connectivity, run connectivity test.
1726
- return true;
1727
- }
1728
-
1729
- return false;
1730
- }
1731
-
1732
- /**
1733
- * @author Vova Feldman (@svovaf)
1734
- * @since 1.1.7.4
1735
- *
1736
- * @return object|false
1737
- */
1738
- private function ping() {
1739
- if ( WP_FS__SIMULATE_NO_API_CONNECTIVITY ) {
1740
- return false;
1741
- }
1742
-
1743
- $version = $this->get_plugin_version();
1744
-
1745
- $is_update = $this->apply_filters( 'is_plugin_update', $this->is_plugin_update() );
1746
-
1747
- return $this->get_api_plugin_scope()->ping(
1748
- $this->get_anonymous_id(),
1749
- array(
1750
- 'is_update' => json_encode( $is_update ),
1751
- 'version' => $version,
1752
- 'sdk' => $this->version,
1753
- 'is_admin' => json_encode( is_admin() ),
1754
- 'is_ajax' => json_encode( self::is_ajax() ),
1755
- 'is_cron' => json_encode( $this->is_cron() ),
1756
- 'is_http' => json_encode( WP_FS__IS_HTTP_REQUEST ),
1757
- )
1758
- );
1759
- }
1760
-
1761
- /**
1762
- * Check if there's any connectivity issue to Freemius API.
1763
- *
1764
- * @author Vova Feldman (@svovaf)
1765
- * @since 1.0.9
1766
- *
1767
- * @param bool $flush_if_no_connectivity
1768
- *
1769
- * @return bool
1770
- */
1771
- function has_api_connectivity( $flush_if_no_connectivity = false ) {
1772
- $this->_logger->entrance();
1773
-
1774
- if ( isset( $this->_has_api_connection ) && ( $this->_has_api_connection || ! $flush_if_no_connectivity ) ) {
1775
- return $this->_has_api_connection;
1776
- }
1777
-
1778
- if ( WP_FS__SIMULATE_NO_API_CONNECTIVITY &&
1779
- isset( $this->_storage->connectivity_test ) &&
1780
- true === $this->_storage->connectivity_test['is_connected']
1781
- ) {
1782
- unset( $this->_storage->connectivity_test );
1783
- }
1784
-
1785
- if ( ! $this->should_run_connectivity_test( $flush_if_no_connectivity ) ) {
1786
- $this->_has_api_connection = $this->_storage->connectivity_test['is_connected'];
1787
- /**
1788
- * @since 1.1.6 During dev mode, if there's connectivity - turn Freemius on regardless the configuration.
1789
- *
1790
- * @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.
1791
- */
1792
- $this->_is_on = $this->_storage->connectivity_test['is_active'] ||
1793
- $this->is_premium() ||
1794
- ( WP_FS__DEV_MODE && $this->_has_api_connection && ! WP_FS__SIMULATE_FREEMIUS_OFF );
1795
-
1796
- return $this->_has_api_connection;
1797
- }
1798
-
1799
- $pong = $this->ping();
1800
- $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong );
1801
-
1802
- if ( ! $is_connected ) {
1803
- // API failure.
1804
- $this->_add_connectivity_issue_message( $pong );
1805
- }
1806
-
1807
- $this->store_connectivity_info( $pong, $is_connected );
1808
-
1809
- return $this->_has_api_connection;
1810
- }
1811
-
1812
- /**
1813
- * @author Vova Feldman (@svovaf)
1814
- * @since 1.1.7.4
1815
- *
1816
- * @param object $pong
1817
- * @param bool $is_connected
1818
- */
1819
- private function store_connectivity_info( $pong, $is_connected ) {
1820
- $this->_logger->entrance();
1821
-
1822
- $version = $this->get_plugin_version();
1823
-
1824
- if ( ! $is_connected || WP_FS__SIMULATE_FREEMIUS_OFF ) {
1825
- $is_active = false;
1826
- } else {
1827
- $is_active = ( isset( $pong->is_active ) && true == $pong->is_active );
1828
- }
1829
-
1830
- $is_active = $this->apply_filters(
1831
- 'is_on',
1832
- $is_active,
1833
- $this->is_plugin_update(),
1834
- $version
1835
- );
1836
-
1837
- $this->_storage->connectivity_test = array(
1838
- 'is_connected' => $is_connected,
1839
- 'host' => $_SERVER['HTTP_HOST'],
1840
- 'server_ip' => WP_FS__REMOTE_ADDR,
1841
- 'is_active' => $is_active,
1842
- 'timestamp' => WP_FS__SCRIPT_START_TIME,
1843
- // Last version with connectivity attempt.
1844
- 'version' => $version,
1845
- );
1846
-
1847
- $this->_has_api_connection = $is_connected;
1848
- $this->_is_on = $is_active || ( WP_FS__DEV_MODE && $is_connected && ! WP_FS__SIMULATE_FREEMIUS_OFF );
1849
- }
1850
-
1851
- /**
1852
- * Force turning Freemius on.
1853
- *
1854
- * @author Vova Feldman (@svovaf)
1855
- * @since 1.1.8.1
1856
- *
1857
- * @return bool TRUE if successfully turned on.
1858
- */
1859
- private function turn_on() {
1860
- $this->_logger->entrance();
1861
-
1862
- if ( $this->is_on() || ! isset( $this->_storage->connectivity_test['is_active'] ) ) {
1863
- return false;
1864
- }
1865
-
1866
- $updated_connectivity = $this->_storage->connectivity_test;
1867
- $updated_connectivity['is_active'] = true;
1868
- $updated_connectivity['timestamp'] = WP_FS__SCRIPT_START_TIME;
1869
- $this->_storage->connectivity_test = $updated_connectivity;
1870
-
1871
- $this->_is_on = true;
1872
-
1873
- return true;
1874
- }
1875
-
1876
- /**
1877
- * Anonymous and unique site identifier (Hash).
1878
- *
1879
- * @author Vova Feldman (@svovaf)
1880
- * @since 1.1.0
1881
- *
1882
- * @return string
1883
- */
1884
- function get_anonymous_id() {
1885
- $unique_id = self::$_accounts->get_option( 'unique_id' );
1886
-
1887
- if ( empty( $unique_id ) || ! is_string( $unique_id ) ) {
1888
- $key = get_site_url();
1889
-
1890
- // If localhost, assign microtime instead of domain.
1891
- if ( WP_FS__IS_LOCALHOST ||
1892
- false !== strpos( $key, 'localhost' ) ||
1893
- false === strpos( $key, '.' )
1894
- ) {
1895
- $key = microtime();
1896
- }
1897
-
1898
- $unique_id = md5( $key );
1899
-
1900
- self::$_accounts->set_option( 'unique_id', $unique_id, true );
1901
- }
1902
-
1903
- $this->_logger->departure( $unique_id );
1904
-
1905
- return $unique_id;
1906
- }
1907
-
1908
- /**
1909
- * @author Vova Feldman (@svovaf)
1910
- * @since 1.1.7.4
1911
- *
1912
- * @return \WP_User
1913
- */
1914
- static function _get_current_wp_user() {
1915
- self::require_pluggable_essentials();
1916
-
1917
- return wp_get_current_user();
1918
- }
1919
-
1920
- /**
1921
- * Generate API connectivity issue message.
1922
- *
1923
- * @author Vova Feldman (@svovaf)
1924
- * @since 1.0.9
1925
- *
1926
- * @param mixed $api_result
1927
- * @param bool $is_first_failure
1928
- */
1929
- function _add_connectivity_issue_message( $api_result, $is_first_failure = true ) {
1930
- if ( ! $this->is_premium() && $this->_enable_anonymous ) {
1931
- // Don't add message if it's the free version and can run anonymously.
1932
- return;
1933
- }
1934
-
1935
- if ( ! function_exists( 'wp_nonce_url' ) ) {
1936
- require_once ABSPATH . 'wp-includes/functions.php';
1937
- }
1938
-
1939
- $current_user = self::_get_current_wp_user();
1940
- // $admin_email = get_option( 'admin_email' );
1941
- $admin_email = $current_user->user_email;
1942
-
1943
- $message = false;
1944
- if ( is_object( $api_result ) &&
1945
- isset( $api_result->error ) &&
1946
- isset( $api_result->error->code )
1947
- ) {
1948
- switch ( $api_result->error->code ) {
1949
- case 'curl_missing':
1950
- $missing_methods = '';
1951
- if ( is_array( $api_result->missing_methods ) &&
1952
- ! empty( $api_result->missing_methods )
1953
- ) {
1954
- foreach ( $api_result->missing_methods as $m ) {
1955
- if ( 'curl_version' === $m ) {
1956
- continue;
1957
- }
1958
-
1959
- if ( ! empty( $missing_methods ) ) {
1960
- $missing_methods .= ', ';
1961
- }
1962
-
1963
- $missing_methods .= sprintf( '<code>%s</code>', $m );
1964
- }
1965
-
1966
- if ( ! empty( $missing_methods ) ) {
1967
- $missing_methods = sprintf(
1968
- '<br><br><b>%s</b> %s',
1969
- $this->get_text( 'curl-disabled-methods' ),
1970
- $missing_methods
1971
- );
1972
- }
1973
- }
1974
-
1975
- $message = sprintf(
1976
- $this->get_text( 'x-requires-access-to-api' ) . ' ' .
1977
- $this->get_text( 'curl-missing-message' ) . ' ' .
1978
- $missing_methods .
1979
- ' %s',
1980
- '<b>' . $this->get_plugin_name() . '</b>',
1981
- sprintf(
1982
- '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
1983
- sprintf(
1984
- '<a class="fs-resolve" data-type="curl" href="#"><b>%s</b></a>%s',
1985
- $this->get_text( 'curl-missing-no-clue-title' ),
1986
- ' - ' . sprintf(
1987
- $this->get_text( 'curl-missing-no-clue-desc' ),
1988
- '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
1989
- )
1990
- ),
1991
- sprintf(
1992
- '<b>%s</b> - %s',
1993
- $this->get_text( 'sysadmin-title' ),
1994
- $this->get_text( 'curl-missing-sysadmin-desc' )
1995
- ),
1996
- sprintf(
1997
- '<a href="%s"><b>%s</b></a>%s',
1998
- 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 ),
1999
- $this->get_text( 'deactivate-plugin-title' ),
2000
- ' - ' . $this->get_text( 'deactivate-plugin-desc' )
2001
- )
2002
- )
2003
- );
2004
- break;
2005
- case 'cloudflare_ddos_protection':
2006
- $message = sprintf(
2007
- $this->get_text( 'x-requires-access-to-api' ) . ' ' .
2008
- $this->get_text( 'cloudflare-blocks-connection-message' ) . ' ' .
2009
- $this->get_text( 'happy-to-resolve-issue-asap' ) .
2010
- ' %s',
2011
- '<b>' . $this->get_plugin_name() . '</b>',
2012
- sprintf(
2013
- '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
2014
- sprintf(
2015
- '<a class="fs-resolve" data-type="cloudflare" href="#"><b>%s</b></a>%s',
2016
- $this->get_text( 'fix-issue-title' ),
2017
- ' - ' . sprintf(
2018
- $this->get_text( 'fix-issue-desc' ),
2019
- '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
2020
- )
2021
- ),
2022
- sprintf(
2023
- '<a href="%s" target="_blank"><b>%s</b></a>%s',
2024
- sprintf( 'https://wordpress.org/plugins/%s/download/', $this->_slug ),
2025
- $this->get_text( 'install-previous-title' ),
2026
- ' - ' . $this->get_text( 'install-previous-desc' )
2027
- ),
2028
- sprintf(
2029
- '<a href="%s"><b>%s</b></a>%s',
2030
- 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 ),
2031
- $this->get_text( 'deactivate-plugin-title' ),
2032
- ' - ' . $this->get_text( 'deactivate-plugin-desc' )
2033
- )
2034
- )
2035
- );
2036
- break;
2037
- case 'squid_cache_block':
2038
- $message = sprintf(
2039
- $this->get_text( 'x-requires-access-to-api' ) . ' ' .
2040
- $this->get_text( 'squid-blocks-connection-message' ) .
2041
- ' %s',
2042
- '<b>' . $this->get_plugin_name() . '</b>',
2043
- sprintf(
2044
- '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
2045
- sprintf(
2046
- '<a class="fs-resolve" data-type="squid" href="#"><b>%s</b></a>%s',
2047
- $this->get_text( 'squid-no-clue-title' ),
2048
- ' - ' . sprintf(
2049
- $this->get_text( 'squid-no-clue-desc' ),
2050
- '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
2051
- )
2052
- ),
2053
- sprintf(
2054
- '<b>%s</b> - %s',
2055
- $this->get_text( 'sysadmin-title' ),
2056
- sprintf(
2057
- $this->get_text( 'squid-sysadmin-desc' ),
2058
- // We use a filter since the plugin might require additional API connectivity.
2059
- '<b>' . implode( ', ', $this->apply_filters( 'api_domains', array( 'api.freemius.com' ) ) ) . '</b>' )
2060
- ),
2061
- sprintf(
2062
- '<a href="%s"><b>%s</b></a>%s',
2063
- 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 ),
2064
- $this->get_text( 'deactivate-plugin-title' ),
2065
- ' - ' . $this->get_text( 'deactivate-plugin-desc' )
2066
- )
2067
- )
2068
- );
2069
- break;
2070
- // default:
2071
- // $message = $this->get_text( 'connectivity-test-fails-message' );
2072
- // break;
2073
- }
2074
- }
2075
-
2076
- $message_id = 'failed_connect_api';
2077
- $type = 'error';
2078
-
2079
- if ( false === $message ) {
2080
- if ( $is_first_failure ) {
2081
- // First attempt failed.
2082
- $message = sprintf(
2083
- $this->get_text( 'x-requires-access-to-api' ) . ' ' .
2084
- $this->get_text( 'connectivity-test-fails-message' ) . ' ' .
2085
- $this->get_text( 'connectivity-test-maybe-temporary' ) . '<br><br>' .
2086
- '%s',
2087
- '<b>' . $this->get_plugin_name() . '</b>',
2088
- sprintf(
2089
- '<div id="fs_firewall_issue_options">%s %s</div>',
2090
- sprintf(
2091
- '<a class="button button-primary fs-resolve" data-type="retry_ping" href="#">%s</a>',
2092
- $this->get_text( 'yes-do-your-thing' )
2093
- ),
2094
- sprintf(
2095
- '<a href="%s" class="button">%s</a>',
2096
- 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 ),
2097
- $this->get_text( 'no-deactivate' )
2098
- )
2099
- )
2100
- );
2101
-
2102
- $message_id = 'failed_connect_api_first';
2103
- $type = 'promotion';
2104
- } else {
2105
- // Second connectivity attempt failed.
2106
- $message = sprintf(
2107
- $this->get_text( 'x-requires-access-to-api' ) . ' ' .
2108
- $this->get_text( 'connectivity-test-fails-message' ) . ' ' .
2109
- $this->get_text( 'happy-to-resolve-issue-asap' ) .
2110
- ' %s',
2111
- '<b>' . $this->get_plugin_name() . '</b>',
2112
- sprintf(
2113
- '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
2114
- sprintf(
2115
- '<a class="fs-resolve" data-type="general" href="#"><b>%s</b></a>%s',
2116
- $this->get_text( 'fix-issue-title' ),
2117
- ' - ' . sprintf(
2118
- $this->get_text( 'fix-issue-desc' ),
2119
- '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
2120
- )
2121
- ),
2122
- sprintf(
2123
- '<a href="%s" target="_blank"><b>%s</b></a>%s',
2124
- sprintf( 'https://wordpress.org/plugins/%s/download/', $this->_slug ),
2125
- $this->get_text( 'install-previous-title' ),
2126
- ' - ' . $this->get_text( 'install-previous-desc' )
2127
- ),
2128
- sprintf(
2129
- '<a href="%s"><b>%s</b></a>%s',
2130
- 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 ),
2131
- $this->get_text( 'deactivate-plugin-title' ),
2132
- ' - ' . $this->get_text( 'deactivate-plugin-desc' )
2133
- )
2134
- )
2135
- );
2136
- }
2137
- }
2138
-
2139
- $this->_admin_notices->add_sticky(
2140
- $message,
2141
- $message_id,
2142
- $this->get_text( 'oops' ) . '...',
2143
- $type
2144
- );
2145
- }
2146
-
2147
- /**
2148
- * Handle user request to resolve connectivity issue.
2149
- * This method will send an email to Freemius API technical staff for resolution.
2150
- * The email will contain server's info and installed plugins (might be caching issue).
2151
- *
2152
- * @author Vova Feldman (@svovaf)
2153
- * @since 1.0.9
2154
- */
2155
- function _email_about_firewall_issue() {
2156
- $this->_admin_notices->remove_sticky( 'failed_connect_api' );
2157
-
2158
- $pong = $this->ping();
2159
-
2160
- $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong );
2161
-
2162
- if ( $is_connected ) {
2163
- $this->store_connectivity_info( $pong, $is_connected );
2164
-
2165
- echo $this->get_after_plugin_activation_redirect_url();
2166
- exit;
2167
- }
2168
-
2169
- $current_user = self::_get_current_wp_user();
2170
- $admin_email = $current_user->user_email;
2171
-
2172
- $error_type = fs_request_get( 'error_type', 'general' );
2173
-
2174
- switch ( $error_type ) {
2175
- case 'squid':
2176
- $title = 'Squid ACL Blocking Issue';
2177
- break;
2178
- case 'cloudflare':
2179
- $title = 'CloudFlare Blocking Issue';
2180
- break;
2181
- default:
2182
- $title = 'API Connectivity Issue';
2183
- break;
2184
- }
2185
-
2186
- $custom_email_sections = array();
2187
-
2188
- // Add 'API Error' custom email section.
2189
- $custom_email_sections['api_error'] = array(
2190
- 'title' => 'API Error',
2191
- 'rows' => array(
2192
- 'ping' => array(
2193
- 'API Error',
2194
- is_string( $pong ) ? htmlentities( $pong ) : json_encode( $pong )
2195
- ),
2196
- )
2197
- );
2198
-
2199
- // Send email with technical details to resolve API connectivity issues.
2200
- $this->send_email(
2201
- 'api@freemius.com', // recipient
2202
- $title . ' [' . $this->get_plugin_name() . ']', // subject
2203
- $custom_email_sections,
2204
- array( "Reply-To: $admin_email <$admin_email>" ) // headers
2205
- );
2206
-
2207
- $this->_admin_notices->add_sticky(
2208
- sprintf(
2209
- $this->get_text( 'fix-request-sent-message' ),
2210
- '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
2211
- ),
2212
- 'server_details_sent'
2213
- );
2214
-
2215
- // Action was taken, tell that API connectivity troubleshooting should be off now.
2216
-
2217
- echo "1";
2218
- exit;
2219
- }
2220
-
2221
- /**
2222
- * Handle connectivity test retry approved by the user.
2223
- *
2224
- * @author Vova Feldman (@svovaf)
2225
- * @since 1.1.7.4
2226
- */
2227
- function _retry_connectivity_test() {
2228
- $this->_admin_notices->remove_sticky( 'failed_connect_api_first' );
2229
-
2230
- $pong = $this->ping();
2231
-
2232
- $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong );
2233
-
2234
- if ( $is_connected ) {
2235
- $this->store_connectivity_info( $pong, $is_connected );
2236
-
2237
- echo $this->get_after_plugin_activation_redirect_url();
2238
- } else {
2239
- // Add connectivity issue message after 2nd failed attempt.
2240
- $this->_add_connectivity_issue_message( $pong, false );
2241
-
2242
- echo "1";
2243
- }
2244
-
2245
- exit;
2246
- }
2247
-
2248
- static function _add_firewall_issues_javascript() {
2249
- $params = array();
2250
- fs_require_once_template( 'firewall-issues-js.php', $params );
2251
- }
2252
-
2253
- #endregion
2254
-
2255
- #----------------------------------------------------------------------------------
2256
- #region Email
2257
- #----------------------------------------------------------------------------------
2258
-
2259
- /**
2260
- * Generates and sends an HTML email with customizable sections.
2261
- *
2262
- * @author Leo Fajardo (@leorw)
2263
- * @since 1.1.2
2264
- *
2265
- * @param string $to_address
2266
- * @param string $subject
2267
- * @param array $sections
2268
- * @param array $headers
2269
- *
2270
- * @return bool Whether the email contents were sent successfully.
2271
- */
2272
- private function send_email(
2273
- $to_address,
2274
- $subject,
2275
- $sections = array(),
2276
- $headers = array()
2277
- ) {
2278
- $default_sections = $this->get_email_sections();
2279
-
2280
- // Insert new sections or replace the default email sections.
2281
- if ( is_array( $sections ) && ! empty( $sections ) ) {
2282
- foreach ( $sections as $section_id => $custom_section ) {
2283
- if ( ! isset( $default_sections[ $section_id ] ) ) {
2284
- // If the section does not exist, add it.
2285
- $default_sections[ $section_id ] = $custom_section;
2286
- } else {
2287
- // If the section already exists, override it.
2288
- $current_section = $default_sections[ $section_id ];
2289
-
2290
- // Replace the current section's title if a custom section title exists.
2291
- if ( isset( $custom_section['title'] ) ) {
2292
- $current_section['title'] = $custom_section['title'];
2293
- }
2294
-
2295
- // Insert new rows under the current section or replace the default rows.
2296
- if ( isset( $custom_section['rows'] ) && is_array( $custom_section['rows'] ) && ! empty( $custom_section['rows'] ) ) {
2297
- foreach ( $custom_section['rows'] as $row_id => $row ) {
2298
- $current_section['rows'][ $row_id ] = $row;
2299
- }
2300
- }
2301
-
2302
- $default_sections[ $section_id ] = $current_section;
2303
- }
2304
- }
2305
- }
2306
-
2307
- $vars = array( 'sections' => $default_sections );
2308
- $message = fs_get_template( 'email.php', $vars );
2309
-
2310
- // Set the type of email to HTML.
2311
- $headers[] = 'Content-type: text/html; charset=UTF-8';
2312
-
2313
- $header_string = implode( "\r\n", $headers );
2314
-
2315
- return wp_mail(
2316
- $to_address,
2317
- $subject,
2318
- $message,
2319
- $header_string
2320
- );
2321
- }
2322
-
2323
- /**
2324
- * Generates the data for the sections of the email content.
2325
- *
2326
- * @author Leo Fajardo (@leorw)
2327
- * @since 1.1.2
2328
- *
2329
- * @return array
2330
- */
2331
- private function get_email_sections() {
2332
- // Retrieve the current user's information so that we can get the user's email, first name, and last name below.
2333
- $current_user = self::_get_current_wp_user();
2334
-
2335
- // Retrieve the cURL version information so that we can get the version number below.
2336
- $curl_version_information = curl_version();
2337
-
2338
- $active_plugin = self::get_active_plugins();
2339
-
2340
- // Generate the list of active plugins separated by new line.
2341
- $active_plugin_string = '';
2342
- foreach ( $active_plugin as $plugin ) {
2343
- $active_plugin_string .= sprintf(
2344
- '<a href="%s">%s</a> [v%s]<br>',
2345
- $plugin['PluginURI'],
2346
- $plugin['Name'],
2347
- $plugin['Version']
2348
- );
2349
- }
2350
-
2351
- $server_ip = WP_FS__REMOTE_ADDR;
2352
-
2353
- // Add PHP info for deeper investigation.
2354
- ob_start();
2355
- phpinfo();
2356
- $php_info = ob_get_clean();
2357
-
2358
- $api_domain = substr( FS_API__ADDRESS, strpos( FS_API__ADDRESS, ':' ) + 3 );
2359
-
2360
- // Generate the default email sections.
2361
- $sections = array(
2362
- 'sdk' => array(
2363
- 'title' => 'SDK',
2364
- 'rows' => array(
2365
- 'fs_version' => array( 'FS Version', $this->version ),
2366
- 'curl_version' => array( 'cURL Version', $curl_version_information['version'] )
2367
- )
2368
- ),
2369
- 'plugin' => array(
2370
- 'title' => 'Plugin',
2371
- 'rows' => array(
2372
- 'name' => array( 'Name', $this->get_plugin_name() ),
2373
- 'version' => array( 'Version', $this->get_plugin_version() )
2374
- )
2375
- ),
2376
- 'api' => array(
2377
- 'title' => 'API Subdomain',
2378
- 'rows' => array(
2379
- 'dns' => array( 'DNS_CNAME', var_export( @dns_get_record( $api_domain, DNS_CNAME ), true ) ),
2380
- 'ip' => array( 'IP', @gethostbyname( $api_domain ) ),
2381
- ),
2382
- ),
2383
- 'site' => array(
2384
- 'title' => 'Site',
2385
- 'rows' => array(
2386
- 'unique_id' => array( 'Unique ID', $this->get_anonymous_id() ),
2387
- 'address' => array( 'Address', site_url() ),
2388
- 'host' => array(
2389
- 'HTTP_HOST',
2390
- ( ! empty( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : '' )
2391
- ),
2392
- 'hosting' => array(
2393
- 'Hosting Company' => fs_request_has( 'hosting_company' ) ?
2394
- fs_request_get( 'hosting_company' ) :
2395
- 'Unknown',
2396
- ),
2397
- 'server_addr' => array(
2398
- 'SERVER_ADDR',
2399
- '<a href="http://www.projecthoneypot.org/ip_' . $server_ip . '">' . $server_ip . '</a>'
2400
- )
2401
- )
2402
- ),
2403
- 'user' => array(
2404
- 'title' => 'User',
2405
- 'rows' => array(
2406
- 'email' => array( 'Email', $current_user->user_email ),
2407
- 'first' => array( 'First', $current_user->user_firstname ),
2408
- 'last' => array( 'Last', $current_user->user_lastname )
2409
- )
2410
- ),
2411
- 'plugins' => array(
2412
- 'title' => 'Plugins',
2413
- 'rows' => array(
2414
- 'active_plugins' => array( 'Active Plugins', $active_plugin_string )
2415
- )
2416
- ),
2417
- 'php_info' => array(
2418
- 'title' => 'PHP Info',
2419
- 'rows' => array(
2420
- 'info' => array( $php_info )
2421
- ),
2422
- )
2423
- );
2424
-
2425
- // Allow the sections to be modified by other code.
2426
- $sections = $this->apply_filters( 'email_template_sections', $sections );
2427
-
2428
- return $sections;
2429
- }
2430
-
2431
- #endregion
2432
-
2433
- #----------------------------------------------------------------------------------
2434
- #region Initialization
2435
- #----------------------------------------------------------------------------------
2436
-
2437
- /**
2438
- * Init plugin's Freemius instance.
2439
- *
2440
- * @author Vova Feldman (@svovaf)
2441
- * @since 1.0.1
2442
- *
2443
- * @param number $id
2444
- * @param string $public_key
2445
- * @param bool $is_live
2446
- * @param bool $is_premium
2447
- */
2448
- function init( $id, $public_key, $is_live = true, $is_premium = true ) {
2449
- $this->_logger->entrance();
2450
-
2451
- $this->dynamic_init( array(
2452
- 'id' => $id,
2453
- 'public_key' => $public_key,
2454
- 'is_live' => $is_live,
2455
- 'is_premium' => $is_premium,
2456
- ) );
2457
- }
2458
-
2459
- /**
2460
- * Dynamic initiator, originally created to support initiation
2461
- * with parent_id for add-ons.
2462
- *
2463
- * @author Vova Feldman (@svovaf)
2464
- * @since 1.0.6
2465
- *
2466
- * @param array $plugin_info
2467
- *
2468
- * @throws Freemius_Exception
2469
- */
2470
- function dynamic_init( array $plugin_info ) {
2471
- $this->_logger->entrance();
2472
-
2473
- $this->parse_settings( $plugin_info );
2474
-
2475
- if ( $this->should_stop_execution() ) {
2476
- return;
2477
- }
2478
-
2479
- if ( ! $this->is_registered() ) {
2480
- if ( $this->is_anonymous() ) {
2481
- // If user skipped, no need to test connectivity.
2482
- $this->_has_api_connection = true;
2483
- $this->_is_on = true;
2484
- } else {
2485
- if ( ! $this->has_api_connectivity() ) {
2486
- if ( $this->_admin_notices->has_sticky( 'failed_connect_api_first' ) ||
2487
- $this->_admin_notices->has_sticky( 'failed_connect_api' )
2488
- ) {
2489
- if ( ! $this->_enable_anonymous || $this->is_premium() ) {
2490
- // If anonymous mode is disabled, add firewall admin-notice message.
2491
- add_action( 'admin_footer', array( 'Freemius', '_add_firewall_issues_javascript' ) );
2492
-
2493
- $this->add_ajax_action( 'resolve_firewall_issues', array(
2494
- &$this,
2495
- '_email_about_firewall_issue'
2496
- ) );
2497
-
2498
- $this->add_ajax_action( 'retry_connectivity_test', array(
2499
- &$this,
2500
- '_retry_connectivity_test'
2501
- ) );
2502
- }
2503
- }
2504
-
2505
- return;
2506
- } else {
2507
- $this->_admin_notices->remove_sticky( array(
2508
- 'failed_connect_api_first',
2509
- 'failed_connect_api',
2510
- ) );
2511
-
2512
- if ( $this->_anonymous_mode ) {
2513
- // Simulate anonymous mode.
2514
- $this->_is_anonymous = true;
2515
- }
2516
- }
2517
- }
2518
-
2519
- // Check if Freemius is on for the current plugin.
2520
- // This MUST be executed after all the plugin variables has been loaded.
2521
- if ( ! $this->is_on() ) {
2522
- return;
2523
- }
2524
- }
2525
-
2526
- if ( $this->has_api_connectivity() ) {
2527
- if ( $this->is_cron() ) {
2528
- $this->hook_callback_to_sync_cron();
2529
- } else if ( $this->is_user_in_admin() ) {
2530
- /**
2531
- * Schedule daily data sync cron if:
2532
- *
2533
- * 1. User opted-in (for tracking).
2534
- * 2. If skipped, but later upgraded (opted-in via upgrade).
2535
- *
2536
- * @author Vova Feldman (@svovaf)
2537
- * @since 1.1.7.3
2538
- *
2539
- */
2540
- if ( $this->is_registered() ) {
2541
- if ( ! $this->is_sync_cron_on() && $this->is_tracking_allowed() ) {
2542
- $this->schedule_sync_cron();
2543
- }
2544
- }
2545
-
2546
- /**
2547
- * Check if requested for manual blocking background sync.
2548
- */
2549
- if ( fs_request_has( 'background_sync' ) ) {
2550
- $this->run_manual_sync();
2551
- }
2552
- }
2553
- }
2554
-
2555
- if ( $this->is_registered() ) {
2556
- $this->hook_callback_to_install_sync();
2557
- }
2558
-
2559
- if ( $this->is_addon() ) {
2560
- if ( $this->is_parent_plugin_installed() ) {
2561
- // Link to parent FS.
2562
- $this->_parent = self::get_instance_by_id( $this->_plugin->parent_plugin_id );
2563
-
2564
- // Get parent plugin reference.
2565
- $this->_parent_plugin = $this->_parent->get_plugin();
2566
- }
2567
- }
2568
-
2569
- if ( $this->is_user_in_admin() ) {
2570
- if ( $this->is_plugins_page() ) {
2571
- $this->hook_plugin_action_links();
2572
- }
2573
-
2574
- if ( $this->is_addon() ) {
2575
- if ( ! $this->is_parent_plugin_installed() ) {
2576
- $parent_name = $this->get_option( $plugin_info, 'parent_name', null );
2577
-
2578
- if ( isset( $plugin_info['parent'] ) ) {
2579
- $parent_name = $this->get_option( $plugin_info['parent'], 'name', null );
2580
- }
2581
-
2582
- $this->_admin_notices->add(
2583
- ( ! empty( $parent_name ) ?
2584
- sprintf( $this->get_text( 'addon-x-cannot-run-without-y' ), $this->get_plugin_name(), $parent_name ) :
2585
- sprintf( $this->get_text( 'addon-x-cannot-run-without-parent' ), $this->get_plugin_name() )
2586
- ),
2587
- $this->get_text( 'oops' ) . '...',
2588
- 'error'
2589
- );
2590
-
2591
- return;
2592
- } else {
2593
- if ( $this->_parent->is_registered() && ! $this->is_registered() ) {
2594
- // If parent plugin activated, automatically install add-on for the user.
2595
- $this->_activate_addon_account( $this->_parent );
2596
- }
2597
-
2598
- // @todo This should be only executed on activation. It should be migrated to register_activation_hook() together with other activation related logic.
2599
- if ( $this->is_premium() ) {
2600
- // Remove add-on download admin-notice.
2601
- $this->_parent->_admin_notices->remove_sticky( array(
2602
- 'addon_plan_upgraded_' . $this->_slug,
2603
- 'no_addon_license_' . $this->_slug,
2604
- ) );
2605
- }
2606
-
2607
- $this->deactivate_premium_only_addon_without_license();
2608
- }
2609
- } else {
2610
- if ( $this->has_addons() &&
2611
- 'plugin-information' === fs_request_get( 'tab', false ) &&
2612
- $this->get_id() == fs_request_get( 'parent_plugin_id', false )
2613
- ) {
2614
- require_once WP_FS__DIR_INCLUDES . '/fs-plugin-info-dialog.php';
2615
-
2616
- new FS_Plugin_Info_Dialog( $this );
2617
- }
2618
- }
2619
-
2620
- add_action( 'admin_init', array( &$this, '_admin_init_action' ) );
2621
-
2622
- // if ( $this->is_registered() ||
2623
- // $this->is_anonymous() ||
2624
- // $this->is_pending_activation()
2625
- // ) {
2626
- // $this->_init_admin();
2627
- // }
2628
- }
2629
-
2630
- /**
2631
- * Should be called outside `$this->is_user_in_admin()` scope
2632
- * because the updater has some logic that needs to be executed
2633
- * during AJAX calls.
2634
- *
2635
- * Currently we need to hook to the `http_request_host_is_external` filter.
2636
- * In the future, there might be additional logic added.
2637
- *
2638
- * @author Vova Feldman
2639
- * @since 1.2.1.6
2640
- */
2641
- if ( $this->is_premium() && $this->has_release_on_freemius() ) {
2642
- new FS_Plugin_Updater( $this );
2643
- }
2644
-
2645
- $this->do_action( 'initiated' );
2646
-
2647
- if ( $this->_storage->prev_is_premium !== $this->_plugin->is_premium ) {
2648
- if ( isset( $this->_storage->prev_is_premium ) ) {
2649
- $this->apply_filters(
2650
- 'after_code_type_change',
2651
- // New code type.
2652
- $this->_plugin->is_premium
2653
- );
2654
- } else {
2655
- // Set for code type for the first time.
2656
- $this->_storage->prev_is_premium = $this->_plugin->is_premium;
2657
- }
2658
- }
2659
-
2660
- if ( ! $this->is_addon() ) {
2661
- if ( $this->is_registered() ) {
2662
- // Fix for upgrade from versions < 1.0.9.
2663
- if ( ! isset( $this->_storage->activation_timestamp ) ) {
2664
- $this->_storage->activation_timestamp = WP_FS__SCRIPT_START_TIME;
2665
- }
2666
-
2667
- $this->do_action( 'after_init_plugin_registered' );
2668
- } else if ( $this->is_anonymous() ) {
2669
- $this->do_action( 'after_init_plugin_anonymous' );
2670
- } else if ( $this->is_pending_activation() ) {
2671
- $this->do_action( 'after_init_plugin_pending_activations' );
2672
- }
2673
- } else {
2674
- if ( $this->is_registered() ) {
2675
- $this->do_action( 'after_init_addon_registered' );
2676
- } else if ( $this->is_anonymous() ) {
2677
- $this->do_action( 'after_init_addon_anonymous' );
2678
- } else if ( $this->is_pending_activation() ) {
2679
- $this->do_action( 'after_init_addon_pending_activations' );
2680
- }
2681
- }
2682
- }
2683
-
2684
- /**
2685
- * @author Leo Fajardo (@leorw)
2686
- *
2687
- * @since 1.2.1.5
2688
- */
2689
- function _stop_tracking_callback() {
2690
- $this->_logger->entrance();
2691
-
2692
- $this->check_ajax_referer( 'stop_tracking' );
2693
-
2694
- $result = $this->stop_tracking();
2695
-
2696
- if ( true === $result ) {
2697
- self::shoot_ajax_success();
2698
- }
2699
-
2700
- $this->_logger->api_error( $result );
2701
-
2702
- self::shoot_ajax_failure(
2703
- $this->get_text( 'unexpected-api-error' ) .
2704
- ( $this->is_api_error( $result ) && isset( $result->error ) ?
2705
- $result->error->message :
2706
- var_export( $result, true ) )
2707
- );
2708
- }
2709
-
2710
- /**
2711
- * @author Leo Fajardo (@leorw)
2712
- * @since 1.2.1.5
2713
- */
2714
- function _allow_tracking_callback() {
2715
- $this->_logger->entrance();
2716
-
2717
- $this->check_ajax_referer( 'allow_tracking' );
2718
-
2719
- $result = $this->allow_tracking();
2720
-
2721
- if ( true === $result ) {
2722
- self::shoot_ajax_success();
2723
- }
2724
-
2725
- $this->_logger->api_error( $result );
2726
-
2727
- self::shoot_ajax_failure(
2728
- $this->get_text( 'unexpected-api-error' ) .
2729
- ( $this->is_api_error( $result ) && isset( $result->error ) ?
2730
- $result->error->message :
2731
- var_export( $result, true ) )
2732
- );
2733
- }
2734
-
2735
- /**
2736
- * Opt-out from usage tracking.
2737
- *
2738
- * Note: This will not delete the account information but will stop all tracking.
2739
- *
2740
- * Returns:
2741
- * 1. FALSE - If the user never opted-in.
2742
- * 2. TRUE - If successfully opted-out.
2743
- * 3. object - API result on failure.
2744
- *
2745
- * @author Leo Fajardo (@leorw)
2746
- * @since 1.2.1.5
2747
- *
2748
- * @return bool|object
2749
- */
2750
- function stop_tracking() {
2751
- $this->_logger->entrance();
2752
-
2753
- if ( ! $this->is_registered() ) {
2754
- // User never opted-in.
2755
- return false;
2756
- }
2757
-
2758
- if ( $this->is_tracking_prohibited() ) {
2759
- // Already disconnected.
2760
- return true;
2761
- }
2762
-
2763
- // Send update to FS.
2764
- $result = $this->get_api_site_scope()->call( '/?fields=is_disconnected', 'put', array(
2765
- 'is_disconnected' => true
2766
- ) );
2767
-
2768
- if ( ! $this->is_api_result_entity( $result ) ||
2769
- ! isset( $result->is_disconnected ) ||
2770
- ! $result->is_disconnected
2771
- ) {
2772
- $this->_logger->api_error( $result );
2773
-
2774
- return $result;
2775
- }
2776
-
2777
- $this->_site->is_disconnected = $result->is_disconnected;
2778
- $this->_store_site();
2779
-
2780
- $this->clear_sync_cron();
2781
-
2782
- // Successfully disconnected.
2783
- return true;
2784
- }
2785
-
2786
- /**
2787
- * Opt-in back into usage tracking.
2788
- *
2789
- * Note: This will only work if the user opted-in previously.
2790
- *
2791
- * Returns:
2792
- * 1. FALSE - If the user never opted-in.
2793
- * 2. TRUE - If successfully opted-in back to usage tracking.
2794
- * 3. object - API result on failure.
2795
- *
2796
- * @author Leo Fajardo (@leorw)
2797
- * @since 1.2.1.5
2798
- *
2799
- * @return bool|object
2800
- */
2801
- function allow_tracking() {
2802
- $this->_logger->entrance();
2803
-
2804
- if ( ! $this->is_registered() ) {
2805
- // User never opted-in.
2806
- return false;
2807
- }
2808
-
2809
- if ( $this->is_tracking_allowed() ) {
2810
- // Tracking already allowed.
2811
- return true;
2812
- }
2813
-
2814
- $result = $this->get_api_site_scope()->call( '/?is_disconnected', 'put', array(
2815
- 'is_disconnected' => false
2816
- ) );
2817
-
2818
- if ( ! $this->is_api_result_entity( $result ) ||
2819
- ! isset( $result->is_disconnected ) ||
2820
- $result->is_disconnected
2821
- ) {
2822
- $this->_logger->api_error( $result );
2823
-
2824
- return $result;
2825
- }
2826
-
2827
- $this->_site->is_disconnected = $result->is_disconnected;
2828
- $this->_store_site();
2829
-
2830
- $this->schedule_sync_cron();
2831
-
2832
- // Successfully reconnected.
2833
- return true;
2834
- }
2835
-
2836
- /**
2837
- * If user opted-in and later disabled usage-tracking,
2838
- * re-allow tracking for licensing and updates.
2839
- *
2840
- * @author Leo Fajardo (@leorw)
2841
- *
2842
- * @since 1.2.1.5
2843
- */
2844
- private function reconnect_locally() {
2845
- $this->_logger->entrance();
2846
-
2847
- if ( $this->is_tracking_prohibited() &&
2848
- $this->is_registered()
2849
- ) {
2850
- $this->_site->is_disconnected = false;
2851
- $this->_store_site();
2852
- }
2853
- }
2854
-
2855
- /**
2856
- * Parse plugin's settings (as defined by the plugin dev).
2857
- *
2858
- * @author Vova Feldman (@svovaf)
2859
- * @since 1.1.7.3
2860
- *
2861
- * @param array $plugin_info
2862
- *
2863
- * @throws \Freemius_Exception
2864
- */
2865
- private function parse_settings( &$plugin_info ) {
2866
- $this->_logger->entrance();
2867
-
2868
- $id = $this->get_numeric_option( $plugin_info, 'id', false );
2869
- $public_key = $this->get_option( $plugin_info, 'public_key', false );
2870
- $secret_key = $this->get_option( $plugin_info, 'secret_key', null );
2871
- $parent_id = $this->get_numeric_option( $plugin_info, 'parent_id', null );
2872
- $parent_name = $this->get_option( $plugin_info, 'parent_name', null );
2873
-
2874
- /**
2875
- * @author Vova Feldman (@svovaf)
2876
- * @since 1.1.9 Try to pull secret key from external config.
2877
- */
2878
- if ( is_null( $secret_key ) && defined( "WP_FS__{$this->_slug}_SECRET_KEY" ) ) {
2879
- $secret_key = constant( "WP_FS__{$this->_slug}_SECRET_KEY" );
2880
- }
2881
-
2882
- if ( isset( $plugin_info['parent'] ) ) {
2883
- $parent_id = $this->get_numeric_option( $plugin_info['parent'], 'id', null );
2884
- // $parent_slug = $this->get_option( $plugin_info['parent'], 'slug', null );
2885
- // $parent_public_key = $this->get_option( $plugin_info['parent'], 'public_key', null );
2886
- // $parent_name = $this->get_option( $plugin_info['parent'], 'name', null );
2887
- }
2888
-
2889
- if ( false === $id ) {
2890
- throw new Freemius_Exception( 'Plugin id parameter is not set.' );
2891
- }
2892
- if ( false === $public_key ) {
2893
- throw new Freemius_Exception( 'Plugin public_key parameter is not set.' );
2894
- }
2895
-
2896
- $plugin = ( $this->_plugin instanceof FS_Plugin ) ?
2897
- $this->_plugin :
2898
- new FS_Plugin();
2899
-
2900
- $plugin->update( array(
2901
- 'id' => $id,
2902
- 'public_key' => $public_key,
2903
- 'slug' => $this->_slug,
2904
- 'parent_plugin_id' => $parent_id,
2905
- 'version' => $this->get_plugin_version(),
2906
- 'title' => $this->get_plugin_name(),
2907
- 'file' => $this->_plugin_basename,
2908
- 'is_premium' => $this->get_bool_option( $plugin_info, 'is_premium', true ),
2909
- 'is_live' => $this->get_bool_option( $plugin_info, 'is_live', true ),
2910
- // 'secret_key' => $secret_key,
2911
- ) );
2912
-
2913
- if ( $plugin->is_updated() ) {
2914
- // Update plugin details.
2915
- $this->_plugin = FS_Plugin_Manager::instance( $this->_slug )->store( $plugin );
2916
- }
2917
- // Set the secret key after storing the plugin, we don't want to store the key in the storage.
2918
- $this->_plugin->secret_key = $secret_key;
2919
-
2920
- if ( ! isset( $plugin_info['menu'] ) ) {
2921
- $plugin_info['menu'] = array();
2922
-
2923
- if ( ! empty( $this->_storage->sdk_last_version ) &&
2924
- version_compare( $this->_storage->sdk_last_version, '1.1.2', '<=' )
2925
- ) {
2926
- // Backward compatibility to 1.1.2
2927
- $plugin_info['menu']['slug'] = isset( $plugin_info['menu_slug'] ) ?
2928
- $plugin_info['menu_slug'] :
2929
- $this->_slug;
2930
- }
2931
- }
2932
-
2933
- $this->_menu = FS_Admin_Menu_Manager::instance( $this->_slug );
2934
- $this->_menu->init( $plugin_info['menu'], $this->is_addon() );
2935
-
2936
- $this->_has_addons = $this->get_bool_option( $plugin_info, 'has_addons', false );
2937
- $this->_has_paid_plans = $this->get_bool_option( $plugin_info, 'has_paid_plans', true );
2938
- $this->_has_premium_version = $this->get_bool_option( $plugin_info, 'has_premium_version', $this->_has_paid_plans );
2939
- $this->_ignore_pending_mode = $this->get_bool_option( $plugin_info, 'ignore_pending_mode', false );
2940
- $this->_is_org_compliant = $this->get_bool_option( $plugin_info, 'is_org_compliant', true );
2941
- $this->_is_premium_only = $this->get_bool_option( $plugin_info, 'is_premium_only', false );
2942
- if ( $this->_is_premium_only ) {
2943
- // If premium only plugin, disable anonymous mode.
2944
- $this->_enable_anonymous = false;
2945
- $this->_anonymous_mode = false;
2946
- } else {
2947
- $this->_enable_anonymous = $this->get_bool_option( $plugin_info, 'enable_anonymous', true );
2948
- $this->_anonymous_mode = $this->get_bool_option( $plugin_info, 'anonymous_mode', false );
2949
- }
2950
- $this->_permissions = $this->get_option( $plugin_info, 'permissions', array() );
2951
-
2952
- if ( ! empty( $plugin_info['trial'] ) ) {
2953
- $this->_trial_days = $this->get_numeric_option(
2954
- $plugin_info['trial'],
2955
- 'days',
2956
- // Default to 0 - trial without days specification.
2957
- 0
2958
- );
2959
-
2960
- $this->_is_trial_require_payment = $this->get_bool_option( $plugin_info['trial'], 'is_require_payment', false );
2961
- }
2962
- }
2963
-
2964
- /**
2965
- * @param string[] $options
2966
- * @param string $key
2967
- * @param mixed $default
2968
- *
2969
- * @return bool
2970
- */
2971
- private function get_option( &$options, $key, $default = false ) {
2972
- return ! empty( $options[ $key ] ) ? $options[ $key ] : $default;
2973
- }
2974
-
2975
- private function get_bool_option( &$options, $key, $default = false ) {
2976
- return isset( $options[ $key ] ) && is_bool( $options[ $key ] ) ? $options[ $key ] : $default;
2977
- }
2978
-
2979
- private function get_numeric_option( &$options, $key, $default = false ) {
2980
- return isset( $options[ $key ] ) && is_numeric( $options[ $key ] ) ? $options[ $key ] : $default;
2981
- }
2982
-
2983
- /**
2984
- * Gate keeper.
2985
- *
2986
- * @author Vova Feldman (@svovaf)
2987
- * @since 1.1.7.3
2988
- *
2989
- * @return bool
2990
- */
2991
- private function should_stop_execution() {
2992
- if ( empty( $this->_storage->was_plugin_loaded ) ) {
2993
- /**
2994
- * Don't execute Freemius until plugin was fully loaded at least once,
2995
- * to give the opportunity for the activation hook to run before pinging
2996
- * the API for connectivity test. This logic is relevant for the
2997
- * identification of new plugin install vs. plugin update.
2998
- *
2999
- * @author Vova Feldman (@svovaf)
3000
- * @since 1.1.9
3001
- */
3002
- return true;
3003
- }
3004
-
3005
- if ( $this->is_activation_mode() ) {
3006
- if ( ! is_admin() ) {
3007
- /**
3008
- * If in activation mode, don't execute Freemius outside of the
3009
- * admin dashboard.
3010
- *
3011
- * @author Vova Feldman (@svovaf)
3012
- * @since 1.1.7.3
3013
- */
3014
- return true;
3015
- }
3016
-
3017
- if ( ! WP_FS__IS_HTTP_REQUEST ) {
3018
- /**
3019
- * If in activation and executed without HTTP context (e.g. CLI, Cronjob),
3020
- * then don't start Freemius.
3021
- *
3022
- * @author Vova Feldman (@svovaf)
3023
- * @since 1.1.6.3
3024
- *
3025
- * @link https://wordpress.org/support/topic/errors-in-the-freemius-class-when-running-in-wordpress-in-cli
3026
- */
3027
- return true;
3028
- }
3029
-
3030
- if ( $this->is_cron() ) {
3031
- /**
3032
- * If in activation mode, don't execute Freemius during wp crons
3033
- * (wp crons have HTTP context - called as HTTP request).
3034
- *
3035
- * @author Vova Feldman (@svovaf)
3036
- * @since 1.1.7.3
3037
- */
3038
- return true;
3039
- }
3040
-
3041
- if ( self::is_ajax() &&
3042
- ! $this->_admin_notices->has_sticky( 'failed_connect_api_first' ) &&
3043
- ! $this->_admin_notices->has_sticky( 'failed_connect_api' )
3044
- ) {
3045
- /**
3046
- * During activation, if running in AJAX mode, unless there's a sticky
3047
- * connectivity issue notice, don't run Freemius.
3048
- *
3049
- * @author Vova Feldman (@svovaf)
3050
- * @since 1.1.7.3
3051
- */
3052
- return true;
3053
- }
3054
- }
3055
-
3056
- return false;
3057
- }
3058
-
3059
- /**
3060
- * Triggered after code type has changed.
3061
- *
3062
- * @author Vova Feldman (@svovaf)
3063
- * @since 1.1.9.1
3064
- */
3065
- function _after_code_type_change() {
3066
- $this->_logger->entrance();
3067
-
3068
- add_action( is_admin() ? 'admin_init' : 'init', array(
3069
- &$this,
3070
- '_plugin_code_type_changed'
3071
- ) );
3072
- }
3073
-
3074
- /**
3075
- * Handles plugin's code type change (free <--> premium).
3076
- *
3077
- * @author Vova Feldman (@svovaf)
3078
- * @since 1.0.9
3079
- */
3080
- function _plugin_code_type_changed() {
3081
- $this->_logger->entrance();
3082
-
3083
- if ( $this->is_premium() ) {
3084
- $this->reconnect_locally();
3085
-
3086
- // Activated premium code.
3087
- $this->do_action( 'after_premium_version_activation' );
3088
-
3089
- // Remove all sticky messages related to download of the premium version.
3090
- $this->_admin_notices->remove_sticky( array(
3091
- 'trial_started',
3092
- 'plan_upgraded',
3093
- 'plan_changed',
3094
- 'license_activated',
3095
- ) );
3096
-
3097
- $this->_admin_notices->add_sticky(
3098
- $this->get_text( 'premium-activated-message' ),
3099
- 'premium_activated',
3100
- $this->get_text( 'woot' ) . '!'
3101
- );
3102
- } else {
3103
- // Remove sticky message related to premium code activation.
3104
- $this->_admin_notices->remove_sticky( 'premium_activated' );
3105
-
3106
- // Activated free code (after had the premium before).
3107
- $this->do_action( 'after_free_version_reactivation' );
3108
-
3109
- if ( $this->is_paying() && ! $this->is_premium() ) {
3110
- $this->_admin_notices->add_sticky(
3111
- sprintf(
3112
- $this->get_text( 'you-have-x-license' ),
3113
- $this->_site->plan->title
3114
- ) . $this->get_complete_upgrade_instructions(),
3115
- 'plan_upgraded',
3116
- $this->get_text( 'yee-haw' ) . '!'
3117
- );
3118
- }
3119
- }
3120
-
3121
- // Schedule code type changes event.
3122
- $this->schedule_install_sync();
3123
-
3124
- /**
3125
- * Unregister the uninstall hook for the other version of the plugin (with different code type) to avoid
3126
- * triggering a fatal error when uninstalling that plugin. For example, after deactivating the "free" version
3127
- * of a specific plugin, its uninstall hook should be unregistered after the "premium" version has been
3128
- * activated. If we don't do that, a fatal error will occur when we try to uninstall the "free" version since
3129
- * the main file of the "free" version will be loaded first before calling the hooked callback. Since the
3130
- * free and premium versions are almost identical (same class or have same functions), a fatal error like
3131
- * "Cannot redeclare class MyClass" or "Cannot redeclare my_function()" will occur.
3132
- */
3133
- $this->unregister_uninstall_hook();
3134
-
3135
- $this->clear_module_main_file_cache();
3136
-
3137
- // Update is_premium of latest version.
3138
- $this->_storage->prev_is_premium = $this->_plugin->is_premium;
3139
- }
3140
-
3141
- #endregion
3142
-
3143
- #----------------------------------------------------------------------------------
3144
- #region Add-ons
3145
- #----------------------------------------------------------------------------------
3146
-
3147
- /**
3148
- * Check if add-on installed and activated on site.
3149
- *
3150
- * @author Vova Feldman (@svovaf)
3151
- * @since 1.0.6
3152
- *
3153
- * @param string|number $slug_or_id
3154
- * @param bool|null $is_premium Since 1.2.1.7 can check for specified add-on version.
3155
- *
3156
- * @return bool
3157
- */
3158
- function is_addon_activated( $slug_or_id, $is_premium = null ) {
3159
- $this->_logger->entrance();
3160
-
3161
- $is_activated = self::has_instance( $slug_or_id );
3162
-
3163
- if ( ! $is_activated ) {
3164
- return false;
3165
- }
3166
-
3167
- if ( is_bool( $is_premium ) ) {
3168
- // Check if the specified code version is activate.
3169
- $addon = $this->get_addon_instance( $slug_or_id );
3170
- $is_activated = ( $is_premium === $addon->is_premium() );
3171
- }
3172
-
3173
- return $is_activated;
3174
- }
3175
-
3176
- /**
3177
- * Check if add-on was connected to install
3178
- *
3179
- * @author Vova Feldman (@svovaf)
3180
- * @since 1.1.7
3181
- *
3182
- * @param string $slug
3183
- *
3184
- * @return bool
3185
- */
3186
- function is_addon_connected( $slug ) {
3187
- $this->_logger->entrance();
3188
-
3189
- $sites = self::get_all_sites();
3190
-
3191
- if ( ! isset( $sites[ $slug ] ) ) {
3192
- return false;
3193
- }
3194
-
3195
- $site = $sites[ $slug ];
3196
-
3197
- $plugin = FS_Plugin_Manager::instance( $slug )->get();
3198
-
3199
- if ( $plugin->parent_plugin_id != $this->_plugin->id ) {
3200
- // The given slug do NOT belong to any of the plugin's add-ons.
3201
- return false;
3202
- }
3203
-
3204
- return ( is_object( $site ) &&
3205
- is_numeric( $site->id ) &&
3206
- is_numeric( $site->user_id ) &&
3207
- is_object( $site->plan )
3208
- );
3209
- }
3210
-
3211
- /**
3212
- * Determines if add-on installed.
3213
- *
3214
- * NOTE: This is a heuristic and only works if the folder/file named as the slug.
3215
- *
3216
- * @author Vova Feldman (@svovaf)
3217
- * @since 1.0.6
3218
- *
3219
- * @param string $slug
3220
- *
3221
- * @return bool
3222
- */
3223
- function is_addon_installed( $slug ) {
3224
- $this->_logger->entrance();
3225
-
3226
- return file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $this->get_addon_basename( $slug ) ) );
3227
- }
3228
-
3229
- /**
3230
- * Get add-on basename.
3231
- *
3232
- * @author Vova Feldman (@svovaf)
3233
- * @since 1.0.6
3234
- *
3235
- * @param string $slug
3236
- *
3237
- * @return string
3238
- */
3239
- function get_addon_basename( $slug ) {
3240
- if ( $this->is_addon_activated( $slug ) ) {
3241
- self::instance( $slug )->get_plugin_basename();
3242
- }
3243
-
3244
- $premium_basename = "{$slug}-premium/{$slug}.php";
3245
-
3246
- if ( file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $premium_basename ) ) ) {
3247
- return $premium_basename;
3248
- }
3249
-
3250
- $free_basename = "{$slug}/{$slug}.php";
3251
-
3252
- return $free_basename;
3253
- }
3254
-
3255
- /**
3256
- * Get installed add-ons instances.
3257
- *
3258
- * @author Vova Feldman (@svovaf)
3259
- * @since 1.0.6
3260
- *
3261
- * @return Freemius[]
3262
- */
3263
- function get_installed_addons() {
3264
- $installed_addons = array();
3265
- foreach ( self::$_instances as $slug => $instance ) {
3266
- if ( $instance->is_addon() && is_object( $instance->_parent_plugin ) ) {
3267
- if ( $this->_plugin->id == $instance->_parent_plugin->id ) {
3268
- $installed_addons[] = $instance;
3269
- }
3270
- }
3271
- }
3272
-
3273
- return $installed_addons;
3274
- }
3275
-
3276
- /**
3277
- * Check if any add-ons of the plugin are installed.
3278
- *
3279
- * @author Leo Fajardo (@leorw)
3280
- * @since 1.1.1
3281
- *
3282
- * @return bool
3283
- */
3284
- function has_installed_addons() {
3285
- if ( ! $this->has_addons() ) {
3286
- return false;
3287
- }
3288
-
3289
- foreach ( self::$_instances as $slug => $instance ) {
3290
- if ( $instance->is_addon() && is_object( $instance->_parent_plugin ) ) {
3291
- if ( $this->_plugin->id == $instance->_parent_plugin->id ) {
3292
- return true;
3293
- }
3294
- }
3295
- }
3296
-
3297
- return false;
3298
- }
3299
-
3300
- /**
3301
- * Tell Freemius that the current plugin is an add-on.
3302
- *
3303
- * @author Vova Feldman (@svovaf)
3304
- * @since 1.0.6
3305
- *
3306
- * @param number $parent_plugin_id The parent plugin ID
3307
- */
3308
- function init_addon( $parent_plugin_id ) {
3309
- $this->_plugin->parent_plugin_id = $parent_plugin_id;
3310
- }
3311
-
3312
- /**
3313
- * @author Vova Feldman (@svovaf)
3314
- * @since 1.0.6
3315
- *
3316
- * @return bool
3317
- */
3318
- function is_addon() {
3319
- return isset( $this->_plugin->parent_plugin_id ) && is_numeric( $this->_plugin->parent_plugin_id );
3320
- }
3321
-
3322
- /**
3323
- * Deactivate add-on if it's premium only and the user does't have a valid license.
3324
- *
3325
- * @param bool $is_after_trial_cancel
3326
- *
3327
- * @return bool If add-on was deactivated.
3328
- */
3329
- private function deactivate_premium_only_addon_without_license( $is_after_trial_cancel = false ) {
3330
- if ( ! $this->has_free_plan() &&
3331
- ! $this->has_features_enabled_license() &&
3332
- ! $this->_has_premium_license()
3333
- ) {
3334
- // IF wrapper is turned off because activation_timestamp is currently only stored for plugins (not addons).
3335
- // if (empty($this->_storage->activation_timestamp) ||
3336
- // (WP_FS__SCRIPT_START_TIME - $this->_storage->activation_timestamp) > 30
3337
- // ) {
3338
- /**
3339
- * @todo When it's first fail, there's no reason to try and re-sync because the licenses were just synced after initial activation.
3340
- *
3341
- * Retry syncing the user add-on licenses.
3342
- */
3343
- // Sync licenses.
3344
- $this->_sync_licenses();
3345
- // }
3346
-
3347
- // Try to activate premium license.
3348
- $this->_activate_license( true );
3349
-
3350
- if ( ! $this->has_free_plan() &&
3351
- ! $this->has_features_enabled_license() &&
3352
- ! $this->_has_premium_license()
3353
- ) {
3354
- // @todo Check if deactivate plugins also call the deactivation hook.
3355
-
3356
- deactivate_plugins( array( $this->_plugin_basename ), true );
3357
-
3358
- $this->_parent->_admin_notices->add_sticky(
3359
- sprintf(
3360
- $this->_parent->get_text( $is_after_trial_cancel ?
3361
- 'addon-trial-cancelled-message' :
3362
- 'addon-no-license-message'
3363
- ),
3364
- '<b>' . $this->_plugin->title . '</b>'
3365
- ) . ' ' . sprintf(
3366
- '<a href="%s" aria-label="%s" class="button button-primary" style="margin-left: 10px; vertical-align: middle;">%s &nbsp;&#10140;</a>',
3367
- $this->_parent->addon_url( $this->_slug ),
3368
- esc_attr( sprintf( $this->_parent->get_text( 'more-information-about-x' ), $this->_plugin->title ) ),
3369
- $this->_parent->get_text( 'purchase-license' )
3370
- ),
3371
- 'no_addon_license_' . $this->_slug,
3372
- ( $is_after_trial_cancel ? '' : $this->_parent->get_text( 'oops' ) . '...' ),
3373
- ( $is_after_trial_cancel ? 'success' : 'error' )
3374
- );
3375
-
3376
- return true;
3377
- }
3378
- }
3379
-
3380
- return false;
3381
- }
3382
-
3383
- #endregion
3384
-
3385
- #----------------------------------------------------------------------------------
3386
- #region Sandbox
3387
- #----------------------------------------------------------------------------------
3388
-
3389
- /**
3390
- * Set Freemius into sandbox mode for debugging.
3391
- *
3392
- * @author Vova Feldman (@svovaf)
3393
- * @since 1.0.4
3394
- *
3395
- * @param string $secret_key
3396
- */
3397
- function init_sandbox( $secret_key ) {
3398
- $this->_plugin->secret_key = $secret_key;
3399
-
3400
- // Update plugin details.
3401
- FS_Plugin_Manager::instance( $this->_slug )->update( $this->_plugin, true );
3402
- }
3403
-
3404
- /**
3405
- * Check if running payments in sandbox mode.
3406
- *
3407
- * @author Vova Feldman (@svovaf)
3408
- * @since 1.0.4
3409
- *
3410
- * @return bool
3411
- */
3412
- function is_payments_sandbox() {
3413
- return ( ! $this->is_live() ) || isset( $this->_plugin->secret_key );
3414
- }
3415
-
3416
- #endregion
3417
-
3418
- /**
3419
- * Check if running test vs. live plugin.
3420
- *
3421
- * @author Vova Feldman (@svovaf)
3422
- * @since 1.0.5
3423
- *
3424
- * @return bool
3425
- */
3426
- function is_live() {
3427
- return $this->_plugin->is_live;
3428
- }
3429
-
3430
- /**
3431
- * Check if the user skipped connecting the account with Freemius.
3432
- *
3433
- * @author Vova Feldman (@svovaf)
3434
- * @since 1.0.7
3435
- *
3436
- * @return bool
3437
- */
3438
- function is_anonymous() {
3439
- if ( ! isset( $this->_is_anonymous ) ) {
3440
- if ( ! isset( $this->_storage->is_anonymous ) ) {
3441
- // Not skipped.
3442
- $this->_is_anonymous = false;
3443
- } else if ( is_bool( $this->_storage->is_anonymous ) ) {
3444
- // For back compatibility, since the variable was boolean before.
3445
- $this->_is_anonymous = $this->_storage->is_anonymous;
3446
-
3447
- // Upgrade stored data format to 1.1.3 format.
3448
- $this->set_anonymous_mode( $this->_storage->is_anonymous );
3449
- } else {
3450
- // Version 1.1.3 and later.
3451
- $this->_is_anonymous = $this->_storage->is_anonymous['is'];
3452
- }
3453
- }
3454
-
3455
- return $this->_is_anonymous;
3456
- }
3457
-
3458
- /**
3459
- * Check if user connected his account and install pending email activation.
3460
- *
3461
- * @author Vova Feldman (@svovaf)
3462
- * @since 1.0.7
3463
- *
3464
- * @return bool
3465
- */
3466
- function is_pending_activation() {
3467
- return $this->_storage->get( 'is_pending_activation', false );
3468
- }
3469
-
3470
- /**
3471
- * Check if plugin must be WordPress.org compliant.
3472
- *
3473
- * @since 1.0.7
3474
- *
3475
- * @return bool
3476
- */
3477
- function is_org_repo_compliant() {
3478
- return $this->_is_org_compliant;
3479
- }
3480
-
3481
- #----------------------------------------------------------------------------------
3482
- #region Daily Sync Cron
3483
- #----------------------------------------------------------------------------------
3484
-
3485
- /**
3486
- * @author Vova Feldman (@svovaf)
3487
- * @since 1.1.7.3
3488
- */
3489
- private function run_manual_sync() {
3490
- $this->require_pluggable_essentials();
3491
-
3492
- if ( ! $this->is_user_admin() ) {
3493
- return;
3494
- }
3495
-
3496
- // Run manual sync.
3497
- $this->_sync_cron();
3498
-
3499
- // Reschedule next cron to run 24 hours from now (performance optimization).
3500
- $this->clear_sync_cron();
3501
-
3502
- $this->schedule_sync_cron( time() + WP_FS__TIME_24_HOURS_IN_SEC, false );
3503
- }
3504
-
3505
- /**
3506
- * Data sync cron job. Replaces the background sync non blocking HTTP request
3507
- * that doesn't halt page loading.
3508
- *
3509
- * @author Vova Feldman (@svovaf)
3510
- * @since 1.1.7.3
3511
- */
3512
- function _sync_cron() {
3513
- $this->_logger->entrance();
3514
-
3515
- // Store the last time data sync was executed.
3516
- $this->_storage->sync_timestamp = time();
3517
-
3518
- // Check if API is temporary down.
3519
- if ( FS_Api::is_temporary_down() ) {
3520
- return;
3521
- }
3522
-
3523
- // @todo Add logic that identifies API latency, and reschedule the next background sync randomly between 8-16 hours.
3524
-
3525
- if ( $this->is_registered() ) {
3526
- if ( $this->has_paid_plan() ) {
3527
- // Initiate background plan sync.
3528
- $this->_sync_license( true );
3529
-
3530
- if ( $this->is_paying() ) {
3531
- // Check for premium plugin updates.
3532
- $this->check_updates( true );
3533
- }
3534
- } else {
3535
- // Sync install (only if something changed locally).
3536
- $this->sync_install();
3537
- }
3538
- }
3539
-
3540
- $this->do_action( 'after_sync_cron' );
3541
- }
3542
-
3543
- /**
3544
- * Check if sync was executed in the last $period of seconds.
3545
- *
3546
- * @author Vova Feldman (@svovaf)
3547
- * @since 1.1.7.3
3548
- *
3549
- * @param int $period In seconds
3550
- *
3551
- * @return bool
3552
- */
3553
- private function is_sync_executed( $period = WP_FS__TIME_24_HOURS_IN_SEC ) {
3554
- if ( ! isset( $this->_storage->sync_timestamp ) ) {
3555
- return false;
3556
- }
3557
-
3558
- return ( $this->_storage->sync_timestamp > ( WP_FS__SCRIPT_START_TIME - $period ) );
3559
- }
3560
-
3561
- /**
3562
- * @author Vova Feldman (@svovaf)
3563
- * @since 1.1.7.3
3564
- *
3565
- * @return bool
3566
- */
3567
- private function is_sync_cron_on() {
3568
- /**
3569
- * @var object $sync_cron_data
3570
- */
3571
- $sync_cron_data = $this->_storage->get( 'sync_cron', null );
3572
-
3573
- return ( ! is_null( $sync_cron_data ) && true === $sync_cron_data->on );
3574
- }
3575
-
3576
- /**
3577
- * @author Vova Feldman (@svovaf)
3578
- * @since 1.1.7.3
3579
- *
3580
- * @param int $start_at Defaults to now.
3581
- * @param bool $randomize_start If true, schedule first job randomly during the next 12 hours. Otherwise,
3582
- * schedule job to start right away.
3583
- */
3584
- private function schedule_sync_cron( $start_at = WP_FS__SCRIPT_START_TIME, $randomize_start = true ) {
3585
- $this->_logger->entrance();
3586
-
3587
- if ( $randomize_start ) {
3588
- // Schedule first sync with a random 12 hour time range from now.
3589
- $start_at += rand( 0, ( WP_FS__TIME_24_HOURS_IN_SEC / 2 ) );
3590
- }
3591
-
3592
- // Schedule daily WP cron.
3593
- wp_schedule_event(
3594
- $start_at,
3595
- 'daily',
3596
- $this->get_action_tag( 'data_sync' )
3597
- );
3598
-
3599
- $this->_storage->store( 'sync_cron', (object) array(
3600
- 'version' => $this->get_plugin_version(),
3601
- 'sdk_version' => $this->version,
3602
- 'timestamp' => WP_FS__SCRIPT_START_TIME,
3603
- 'on' => true,
3604
- ) );
3605
- }
3606
-
3607
- /**
3608
- * Add the actual sync function to the cron job hook.
3609
- *
3610
- * @author Vova Feldman (@svovaf)
3611
- * @since 1.1.7.3
3612
- */
3613
- private function hook_callback_to_sync_cron() {
3614
- $this->add_action( 'data_sync', array( &$this, '_sync_cron' ) );
3615
- }
3616
-
3617
- /**
3618
- * @author Vova Feldman (@svovaf)
3619
- * @since 1.1.7.3
3620
- */
3621
- private function clear_sync_cron() {
3622
- $this->_logger->entrance();
3623
-
3624
- if ( ! $this->is_sync_cron_on() ) {
3625
- return;
3626
- }
3627
-
3628
- $this->_storage->remove( 'sync_cron' );
3629
-
3630
- wp_clear_scheduled_hook( $this->get_action_tag( 'data_sync' ) );
3631
- }
3632
-
3633
- /**
3634
- * Unix timestamp for next sync cron execution or false if not scheduled.
3635
- *
3636
- * @author Vova Feldman (@svovaf)
3637
- * @since 1.1.7.3
3638
- *
3639
- * @return int|false
3640
- */
3641
- function next_sync_cron() {
3642
- $this->_logger->entrance();
3643
-
3644
- if ( ! $this->is_sync_cron_on() ) {
3645
- return false;
3646
- }
3647
-
3648
- return wp_next_scheduled( $this->get_action_tag( 'data_sync' ) );
3649
- }
3650
-
3651
- /**
3652
- * Unix timestamp for previous sync cron execution or false if never executed.
3653
- *
3654
- * @author Vova Feldman (@svovaf)
3655
- * @since 1.1.7.3
3656
- *
3657
- * @return int|false
3658
- */
3659
- function last_sync_cron() {
3660
- $this->_logger->entrance();
3661
-
3662
- return $this->_storage->get( 'sync_timestamp' );
3663
- }
3664
-
3665
- #endregion Daily Sync Cron ------------------------------------------------------------------
3666
-
3667
- #----------------------------------------------------------------------------------
3668
- #region Async Install Sync
3669
- #----------------------------------------------------------------------------------
3670
-
3671
- /**
3672
- * @author Vova Feldman (@svovaf)
3673
- * @since 1.1.7.3
3674
- *
3675
- * @return bool
3676
- */
3677
- private function is_install_sync_scheduled() {
3678
- /**
3679
- * @var object $cron_data
3680
- */
3681
- $cron_data = $this->_storage->get( 'install_sync_cron', null );
3682
-
3683
- return ( ! is_null( $cron_data ) && true === $cron_data->on );
3684
- }
3685
-
3686
- /**
3687
- * Instead of running blocking install sync event, execute non blocking scheduled wp-cron.
3688
- *
3689
- * @author Vova Feldman (@svovaf)
3690
- * @since 1.1.7.3
3691
- */
3692
- private function schedule_install_sync() {
3693
- $this->_logger->entrance();
3694
-
3695
- $this->clear_install_sync_cron();
3696
-
3697
- // Schedule immediate install sync.
3698
- wp_schedule_single_event(
3699
- WP_FS__SCRIPT_START_TIME,
3700
- $this->get_action_tag( 'install_sync' )
3701
- );
3702
-
3703
- $this->_storage->store( 'install_sync_cron', (object) array(
3704
- 'version' => $this->get_plugin_version(),
3705
- 'sdk_version' => $this->version,
3706
- 'timestamp' => WP_FS__SCRIPT_START_TIME,
3707
- 'on' => true,
3708
- ) );
3709
- }
3710
-
3711
- /**
3712
- * Unix timestamp for previous install sync cron execution or false if never executed.
3713
- *
3714
- * @todo There's some very strange bug that $this->_storage->install_sync_timestamp value is not being
3715
- * updated. But for sure the sync event is working.
3716
- *
3717
- * @author Vova Feldman (@svovaf)
3718
- * @since 1.1.7.3
3719
- *
3720
- * @return int|false
3721
- */
3722
- function last_install_sync() {
3723
- $this->_logger->entrance();
3724
-
3725
- return $this->_storage->get( 'install_sync_timestamp' );
3726
- }
3727
-
3728
- /**
3729
- * Unix timestamp for next install sync cron execution or false if not scheduled.
3730
- *
3731
- * @author Vova Feldman (@svovaf)
3732
- * @since 1.1.7.3
3733
- *
3734
- * @return int|false
3735
- */
3736
- function next_install_sync() {
3737
- $this->_logger->entrance();
3738
-
3739
- if ( ! $this->is_install_sync_scheduled() ) {
3740
- return false;
3741
- }
3742
-
3743
- return wp_next_scheduled( $this->get_action_tag( 'install_sync' ) );
3744
- }
3745
-
3746
- /**
3747
- * Add the actual install sync function to the cron job hook.
3748
- *
3749
- * @author Vova Feldman (@svovaf)
3750
- * @since 1.1.7.3
3751
- */
3752
- private function hook_callback_to_install_sync() {
3753
- $this->add_action( 'install_sync', array( &$this, '_run_sync_install' ) );
3754
- }
3755
-
3756
- /**
3757
- * @author Vova Feldman (@svovaf)
3758
- * @since 1.1.7.3
3759
- */
3760
- private function clear_install_sync_cron() {
3761
- $this->_logger->entrance();
3762
-
3763
- if ( ! $this->is_install_sync_scheduled() ) {
3764
- return;
3765
- }
3766
-
3767
- $this->_storage->remove( 'install_sync_cron' );
3768
-
3769
- wp_clear_scheduled_hook( $this->get_action_tag( 'install_sync' ) );
3770
- }
3771
-
3772
- /**
3773
- * @author Vova Feldman (@svovaf)
3774
- * @since 1.1.7.3
3775
- */
3776
- public function _run_sync_install() {
3777
- $this->_logger->entrance();
3778
-
3779
- // Update last install sync timestamp.
3780
- $this->_storage->install_sync_timestamp = time();
3781
-
3782
- $this->sync_install( array(), true );
3783
- }
3784
-
3785
- #endregion Async Install Sync ------------------------------------------------------------------
3786
-
3787
- /**
3788
- * Show a notice that activation is currently pending.
3789
- *
3790
- * @author Vova Feldman (@svovaf)
3791
- * @since 1.0.7
3792
- *
3793
- * @param bool|string $email
3794
- * @param bool $is_pending_trial Since 1.2.1.5
3795
- */
3796
- function _add_pending_activation_notice( $email = false, $is_pending_trial = false ) {
3797
- if ( ! is_string( $email ) ) {
3798
- $current_user = self::_get_current_wp_user();
3799
- $email = $current_user->user_email;
3800
- }
3801
-
3802
- $this->_admin_notices->add_sticky(
3803
- sprintf(
3804
- $this->get_text( 'pending-activation-message' ),
3805
- '<b>' . $this->get_plugin_name() . '</b>',
3806
- '<b>' . $email . '</b>',
3807
- $this->get_text( $is_pending_trial ? 'start-the-trial' : 'complete-the-install' )
3808
- ),
3809
- 'activation_pending',
3810
- 'Thanks!'
3811
- );
3812
- }
3813
-
3814
- /**
3815
- * Check if currently in plugin activation.
3816
- *
3817
- * @author Vova Feldman (@svovaf)
3818
- * @since 1.1.4
3819
- *
3820
- * @return bool
3821
- */
3822
- function is_plugin_activation() {
3823
- return get_option( "fs_{$this->_slug}_activated", false );
3824
- }
3825
-
3826
- /**
3827
- *
3828
- * NOTE: admin_menu action executed before admin_init.
3829
- *
3830
- * @author Vova Feldman (@svovaf)
3831
- * @since 1.0.7
3832
- */
3833
- function _admin_init_action() {
3834
- /**
3835
- * Automatically redirect to connect/activation page after plugin activation.
3836
- *
3837
- * @since 1.1.7 Do NOT redirect to opt-in when running in network admin mode.
3838
- */
3839
- if ( $this->is_plugin_activation() ) {
3840
- delete_option( "fs_{$this->_slug}_activated" );
3841
-
3842
- if ( ! function_exists( 'is_network_admin' ) || ! is_network_admin() ) {
3843
- $this->_redirect_on_activation_hook();
3844
-
3845
- return;
3846
- }
3847
- }
3848
-
3849
- if ( fs_request_is_action( $this->_slug . '_skip_activation' ) ) {
3850
- check_admin_referer( $this->_slug . '_skip_activation' );
3851
-
3852
- $this->skip_connection();
3853
-
3854
- fs_redirect( $this->get_after_activation_url( 'after_skip_url' ) );
3855
- }
3856
-
3857
- if ( ! $this->is_addon() && ! $this->is_registered() && ! $this->is_anonymous() ) {
3858
- if ( ! $this->is_pending_activation() ) {
3859
- if ( ! $this->_menu->is_main_settings_page() ) {
3860
- if ( $this->is_plugin_new_install() || $this->is_only_premium() ) {
3861
- // Show notice for new plugin installations.
3862
- $this->_admin_notices->add(
3863
- sprintf(
3864
- $this->get_text( 'you-are-step-away' ),
3865
- sprintf( '<b><a href="%s">%s</a></b>',
3866
- $this->get_activation_url(),
3867
- sprintf( $this->get_text( 'activate-x-now' ), $this->get_plugin_name() )
3868
- )
3869
- ),
3870
- '',
3871
- 'update-nag'
3872
- );
3873
- } else {
3874
- if ( ! isset( $this->_storage->sticky_optin_added ) ) {
3875
- $this->_storage->sticky_optin_added = true;
3876
-
3877
- // Show notice for new plugin installations.
3878
- $this->_admin_notices->add_sticky(
3879
- sprintf(
3880
- $this->get_text( 'few-plugin-tweaks' ),
3881
- sprintf( '<b><a href="%s">%s</a></b>',
3882
- $this->get_activation_url(),
3883
- sprintf( $this->get_text( 'optin-x-now' ), $this->get_plugin_name() )
3884
- )
3885
- ),
3886
- 'connect_account',
3887
- '',
3888
- 'update-nag'
3889
- );
3890
- }
3891
-
3892
- if ( $this->has_filter( 'optin_pointer_element' ) ) {
3893
- // Don't show admin nag if plugin update.
3894
- wp_enqueue_script( 'wp-pointer' );
3895
- wp_enqueue_style( 'wp-pointer' );
3896
-
3897
- $this->_enqueue_connect_essentials();
3898
-
3899
- add_action( 'admin_print_footer_scripts', array(
3900
- $this,
3901
- '_add_connect_pointer_script'
3902
- ) );
3903
- }
3904
-
3905
- }
3906
- }
3907
- }
3908
- }
3909
-
3910
- $this->_add_upgrade_action_link();
3911
- }
3912
-
3913
- /**
3914
- * Enqueue connect requires scripts and styles.
3915
- *
3916
- * @author Vova Feldman (@svovaf)
3917
- * @since 1.1.4
3918
- */
3919
- function _enqueue_connect_essentials() {
3920
- wp_enqueue_script( 'jquery' );
3921
- wp_enqueue_script( 'json2' );
3922
-
3923
- fs_enqueue_local_script( 'postmessage', 'nojquery.ba-postmessage.min.js' );
3924
- fs_enqueue_local_script( 'fs-postmessage', 'postmessage.js' );
3925
-
3926
- fs_enqueue_local_style( 'fs_connect', '/admin/connect.css' );
3927
- }
3928
-
3929
- /**
3930
- * Add connect / opt-in pointer.
3931
- *
3932
- * @author Vova Feldman (@svovaf)
3933
- * @since 1.1.4
3934
- */
3935
- function _add_connect_pointer_script() {
3936
- $vars = array( 'slug' => $this->_slug );
3937
- $pointer_content = fs_get_template( 'connect.php', $vars );
3938
- ?>
3939
- <script type="text/javascript">// <![CDATA[
3940
- jQuery(document).ready(function ($) {
3941
- if ('undefined' !== typeof(jQuery().pointer)) {
3942
-
3943
- var element = <?php echo $this->apply_filters( 'optin_pointer_element', '$("#non_existing_element");' ) ?>;
3944
-
3945
- if (element.length > 0) {
3946
- var optin = $(element).pointer($.extend(true, {}, {
3947
- content : <?php echo json_encode( $pointer_content ) ?>,
3948
- position : {
3949
- edge : 'left',
3950
- align: 'center'
3951
- },
3952
- buttons : function () {
3953
- // Don't show pointer buttons.
3954
- return '';
3955
- },
3956
- pointerWidth: 482
3957
- }, <?php echo $this->apply_filters( 'optin_pointer_options_json', '{}' ) ?>));
3958
-
3959
- <?php
3960
- echo $this->apply_filters( 'optin_pointer_execute', "
3961
-
3962
- optin.pointer('open');
3963
-
3964
- // Tag the opt-in pointer with custom class.
3965
- $('.wp-pointer #fs_connect')
3966
- .parents('.wp-pointer.wp-pointer-top')
3967
- .addClass('fs-opt-in-pointer');
3968
-
3969
- ", 'element', 'optin' ) ?>
3970
- }
3971
- }
3972
- });
3973
- // ]]></script>
3974
- <?php
3975
- }
3976
-
3977
- /**
3978
- * Return current page's URL.
3979
- *
3980
- * @author Vova Feldman (@svovaf)
3981
- * @since 1.0.7
3982
- *
3983
- * @return string
3984
- */
3985
- function current_page_url() {
3986
- $url = 'http';
3987
-
3988
- if ( isset( $_SERVER["HTTPS"] ) ) {
3989
- if ( $_SERVER["HTTPS"] == "on" ) {
3990
- $url .= "s";
3991
- }
3992
- }
3993
- $url .= "://";
3994
- if ( $_SERVER["SERVER_PORT"] != "80" ) {
3995
- $url .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
3996
- } else {
3997
- $url .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
3998
- }
3999
-
4000
- return esc_url( $url );
4001
- }
4002
-
4003
- /**
4004
- * Check if the current page is the plugin's main admin settings page.
4005
- *
4006
- * @author Vova Feldman (@svovaf)
4007
- * @since 1.0.7
4008
- *
4009
- * @return bool
4010
- */
4011
- function _is_plugin_page() {
4012
- return fs_is_plugin_page( $this->_menu->get_raw_slug() ) ||
4013
- fs_is_plugin_page( $this->_slug );
4014
- }
4015
-
4016
- /* Events
4017
- ------------------------------------------------------------------------------------------------------------------*/
4018
- /**
4019
- * Delete site install from Database.
4020
- *
4021
- * @author Vova Feldman (@svovaf)
4022
- * @since 1.0.1
4023
- *
4024
- * @param bool $store
4025
- */
4026
- function _delete_site( $store = true ) {
4027
- $sites = self::get_all_sites();
4028
-
4029
- if ( isset( $sites[ $this->_slug ] ) ) {
4030
- unset( $sites[ $this->_slug ] );
4031
- }
4032
-
4033
- self::$_accounts->set_option( 'sites', $sites, $store );
4034
- }
4035
-
4036
- /**
4037
- * Delete plugin's plans information.
4038
- *
4039
- * @param bool $store Flush to Database if true.
4040
- *
4041
- * @author Vova Feldman (@svovaf)
4042
- * @since 1.0.9
4043
- */
4044
- private function _delete_plans( $store = true ) {
4045
- $this->_logger->entrance();
4046
-
4047
- $plans = self::get_all_plans();
4048
-
4049
- unset( $plans[ $this->_slug ] );
4050
-
4051
- self::$_accounts->set_option( 'plans', $plans, $store );
4052
- }
4053
-
4054
- /**
4055
- * Delete all plugin licenses.
4056
- *
4057
- * @author Vova Feldman (@svovaf)
4058
- * @since 1.0.9
4059
- *
4060
- * @param bool $store
4061
- * @param string|bool $plugin_slug
4062
- */
4063
- private function _delete_licenses( $store = true, $plugin_slug = false ) {
4064
- $this->_logger->entrance();
4065
-
4066
- $all_licenses = self::get_all_licenses();
4067
-
4068
- if ( ! is_string( $plugin_slug ) ) {
4069
- $plugin_slug = $this->_slug;
4070
- }
4071
-
4072
- unset( $all_licenses[ $plugin_slug ] );
4073
-
4074
- self::$_accounts->set_option( 'licenses', $all_licenses, $store );
4075
- }
4076
-
4077
- /**
4078
- * Check if Freemius was added on new plugin installation.
4079
- *
4080
- * @author Vova Feldman (@svovaf)
4081
- * @since 1.1.5
4082
- *
4083
- * @return bool
4084
- */
4085
- function is_plugin_new_install() {
4086
- return isset( $this->_storage->is_plugin_new_install ) &&
4087
- $this->_storage->is_plugin_new_install;
4088
- }
4089
-
4090
- /**
4091
- * Check if it's the first plugin release that is running Freemius.
4092
- *
4093
- * @author Vova Feldman (@svovaf)
4094
- * @since 1.2.1.5
4095
- *
4096
- * @return bool
4097
- */
4098
- function is_first_freemius_powered_version() {
4099
- return empty( $this->_storage->plugin_last_version );
4100
- }
4101
-
4102
- /**
4103
- * Plugin activated hook.
4104
- *
4105
- * @author Vova Feldman (@svovaf)
4106
- * @since 1.0.1
4107
- *
4108
- * @uses FS_Api
4109
- */
4110
- function _activate_plugin_event_hook() {
4111
- $this->_logger->entrance( 'slug = ' . $this->_slug );
4112
-
4113
- if ( ! $this->is_user_admin() ) {
4114
- return;
4115
- }
4116
-
4117
- $this->unregister_uninstall_hook();
4118
-
4119
- // Clear API cache on activation.
4120
- FS_Api::clear_cache();
4121
-
4122
- $is_premium_version_activation = ( current_filter() !== ( 'activate_' . $this->_free_plugin_basename ) );
4123
-
4124
- $this->_logger->info( 'Activating ' . ( $is_premium_version_activation ? 'premium' : 'free' ) . ' plugin version.' );
4125
-
4126
- // 1. If running in the activation of the FREE module, get the basename of the PREMIUM.
4127
- // 2. If running in the activation of the PREMIUM module, get the basename of the FREE.
4128
- $other_version_basename = $is_premium_version_activation ?
4129
- $this->_free_plugin_basename :
4130
- $this->premium_plugin_basename();
4131
-
4132
- /**
4133
- * If the other module version is activate, deactivate it.
4134
- *
4135
- * @author Leo Fajardo (@leorw)
4136
- * @since 1.2.2
4137
- */
4138
- if ( is_plugin_active( $other_version_basename ) ) {
4139
- deactivate_plugins( $other_version_basename );
4140
- }
4141
-
4142
- if ( $this->is_registered() ) {
4143
- if ( $is_premium_version_activation ) {
4144
- $this->reconnect_locally();
4145
  }
 
4146
 
4147
- // Schedule re-activation event and sync.
4148
- // $this->sync_install( array(), true );
4149
- $this->schedule_install_sync();
 
4150
 
4151
- // If activating the premium module version, add an admin notice to congratulate for an upgrade completion.
4152
- if ( $is_premium_version_activation ) {
4153
- $this->_admin_notices->add(
4154
- sprintf( $this->get_text( 'successful-version-upgrade-message' ), sprintf( '<b>%s</b>', $this->_plugin->title ) ),
4155
- $this->get_text( 'woot' ) . '!'
4156
- );
4157
- }
4158
- } else if ( $this->is_anonymous() ) {
4159
- /**
4160
- * Reset "skipped" click cache on the following:
4161
- * 1. Development mode.
4162
- * 2. If the user skipped the exact same version before.
4163
- *
4164
- * @todo 3. If explicitly asked to retry after every activation.
4165
- */
4166
- if ( WP_FS__DEV_MODE ||
4167
- $this->get_plugin_version() == $this->_storage->is_anonymous['version']
4168
- ) {
4169
- $this->reset_anonymous_mode();
4170
- }
4171
- }
4172
-
4173
- if ( ! isset( $this->_storage->is_plugin_new_install ) ) {
4174
- /**
4175
- * If no previous version of plugin's version exist, it means that it's either
4176
- * the first time that the plugin installed on the site, or the plugin was installed
4177
- * before but didn't have Freemius integrated.
4178
- *
4179
- * Since register_activation_hook() do NOT fires on updates since 3.1, and only fires
4180
- * on manual activation via the dashboard, is_plugin_activation() is TRUE
4181
- * only after immediate activation.
4182
- *
4183
- * @since 1.1.4
4184
- * @link https://make.wordpress.org/core/2010/10/27/plugin-activation-hooks-no-longer-fire-for-updates/
4185
- */
4186
- $this->_storage->is_plugin_new_install = empty( $this->_storage->plugin_last_version );
4187
- }
4188
-
4189
- if ( ! $this->_anonymous_mode &&
4190
- $this->has_api_connectivity( WP_FS__DEV_MODE ) &&
4191
- ! $this->_isAutoInstall
4192
- ) {
4193
- // Store hint that the plugin was just activated to enable auto-redirection to settings.
4194
- add_option( "fs_{$this->_slug}_activated", true );
4195
- }
4196
-
4197
- /**
4198
- * Activation hook is executed after the plugin's main file is loaded, therefore,
4199
- * after the plugin was loaded. The logic is located at activate_plugin()
4200
- * ./wp-admin/includes/plugin.php.
4201
- *
4202
- * @author Vova Feldman (@svovaf)
4203
- * @since 1.1.9
4204
- */
4205
- $this->_storage->was_plugin_loaded = true;
4206
- }
4207
-
4208
- /**
4209
- * Delete account.
4210
- *
4211
- * @author Vova Feldman (@svovaf)
4212
- * @since 1.0.3
4213
- *
4214
- * @param bool $check_user Enforce checking if user have plugins activation privileges.
4215
- */
4216
- function delete_account_event( $check_user = true ) {
4217
- $this->_logger->entrance( 'slug = ' . $this->_slug );
4218
-
4219
- if ( $check_user && ! $this->is_user_admin() ) {
4220
- return;
4221
- }
4222
-
4223
- $this->do_action( 'before_account_delete' );
4224
-
4225
- // Clear all admin notices.
4226
- $this->_admin_notices->clear_all_sticky();
4227
-
4228
- $this->_delete_site( false );
4229
-
4230
- $this->_delete_plans( false );
4231
-
4232
- $this->_delete_licenses( false );
4233
-
4234
- // Delete add-ons related to plugin's account.
4235
- $this->_delete_account_addons( false );
4236
-
4237
- // @todo Delete plans and licenses of add-ons.
4238
-
4239
- self::$_accounts->store();
4240
-
4241
- /**
4242
- * IMPORTANT:
4243
- * Clear crons must be executed before clearing all storage.
4244
- * Otherwise, the cron will not be cleared.
4245
- */
4246
- $this->clear_sync_cron();
4247
- $this->clear_install_sync_cron();
4248
-
4249
- // Clear all storage data.
4250
- $this->_storage->clear_all( true, array(
4251
- 'connectivity_test',
4252
- 'is_on',
4253
- ) );
4254
-
4255
- // Send delete event.
4256
- $this->get_api_site_scope()->call( '/', 'delete' );
4257
-
4258
- $this->do_action( 'after_account_delete' );
4259
- }
4260
-
4261
- /**
4262
- * Plugin deactivation hook.
4263
- *
4264
- * @author Vova Feldman (@svovaf)
4265
- * @since 1.0.1
4266
- */
4267
- function _deactivate_plugin_hook() {
4268
- $this->_logger->entrance( 'slug = ' . $this->_slug );
4269
-
4270
- if ( ! current_user_can( 'activate_plugins' ) ) {
4271
- return;
4272
- }
4273
-
4274
- $this->_admin_notices->clear_all_sticky();
4275
- if ( isset( $this->_storage->sticky_optin_added ) ) {
4276
- unset( $this->_storage->sticky_optin_added );
4277
- }
4278
-
4279
- if ( ! isset( $this->_storage->is_plugin_new_install ) ) {
4280
- // Remember that plugin was already installed.
4281
- $this->_storage->is_plugin_new_install = false;
4282
- }
4283
-
4284
- // Hook to plugin uninstall.
4285
- register_uninstall_hook( $this->_plugin_main_file_path, array( 'Freemius', '_uninstall_plugin_hook' ) );
4286
-
4287
- $this->clear_module_main_file_cache();
4288
- $this->clear_sync_cron();
4289
- $this->clear_install_sync_cron();
4290
-
4291
- if ( $this->is_registered() ) {
4292
- // Send deactivation event.
4293
- $this->sync_install( array(
4294
- 'is_active' => false,
4295
- ) );
4296
- } else {
4297
- if ( ! $this->has_api_connectivity() ) {
4298
- // Reset connectivity test cache.
4299
- unset( $this->_storage->connectivity_test );
4300
- }
4301
- }
4302
-
4303
- // Clear API cache on deactivation.
4304
- FS_Api::clear_cache();
4305
-
4306
- $this->remove_sdk_reference();
4307
- }
4308
-
4309
- /**
4310
- * @author Vova Feldman (@svovaf)
4311
- * @since 1.1.6
4312
- */
4313
- private function remove_sdk_reference() {
4314
- global $fs_active_plugins;
4315
-
4316
- foreach ( $fs_active_plugins->plugins as $sdk_path => &$data ) {
4317
- if ( $this->_plugin_basename == $data->plugin_path ) {
4318
- unset( $fs_active_plugins->plugins[ $sdk_path ] );
4319
- break;
4320
- }
4321
- }
4322
-
4323
- fs_fallback_to_newest_active_sdk();
4324
- }
4325
-
4326
- /**
4327
- * @author Vova Feldman (@svovaf)
4328
- * @since 1.1.3
4329
- *
4330
- * @param bool $is_anonymous
4331
- */
4332
- private function set_anonymous_mode( $is_anonymous = true ) {
4333
- // Store information regarding skip to try and opt-in the user
4334
- // again in the future.
4335
- $this->_storage->is_anonymous = array(
4336
- 'is' => $is_anonymous,
4337
- 'timestamp' => WP_FS__SCRIPT_START_TIME,
4338
- 'version' => $this->get_plugin_version(),
4339
- );
4340
-
4341
- // Update anonymous mode cache.
4342
- $this->_is_anonymous = $is_anonymous;
4343
- }
4344
-
4345
- /**
4346
- * @author Vova Feldman (@svovaf)
4347
- * @since 1.1.3
4348
- */
4349
- private function reset_anonymous_mode() {
4350
- unset( $this->_storage->is_anonymous );
4351
- unset( $this->_is_anonymous );
4352
- }
4353
-
4354
- /**
4355
- * Clears the anonymous mode and redirects to the opt-in screen.
4356
- *
4357
- * @author Vova Feldman (@svovaf)
4358
- * @since 1.1.7
4359
- */
4360
- function connect_again() {
4361
- if ( ! $this->is_anonymous() ) {
4362
- return;
4363
- }
4364
-
4365
- $this->reset_anonymous_mode();
4366
-
4367
- fs_redirect( $this->get_activation_url() );
4368
- }
4369
-
4370
- /**
4371
- * Skip account connect, and set anonymous mode.
4372
- *
4373
- * @author Vova Feldman (@svovaf)
4374
- * @since 1.1.1
4375
- */
4376
- private function skip_connection() {
4377
- $this->_logger->entrance();
4378
-
4379
- $this->_admin_notices->remove_sticky( 'connect_account' );
4380
-
4381
- $this->set_anonymous_mode();
4382
-
4383
- // Send anonymous skip event.
4384
- // No user identified info nor any tracking will be sent after the user skips the opt-in.
4385
- $this->get_api_plugin_scope()->call( 'skip.json', 'put', array(
4386
- 'uid' => $this->get_anonymous_id(),
4387
- ) );
4388
- }
4389
-
4390
- /**
4391
- * Plugin version update hook.
4392
- *
4393
- * @author Vova Feldman (@svovaf)
4394
- * @since 1.0.4
4395
- */
4396
- private function update_plugin_version_event() {
4397
- $this->_logger->entrance();
4398
-
4399
- if ( ! $this->is_registered() ) {
4400
- return;
4401
- }
4402
-
4403
- $this->schedule_install_sync();
4404
- // $this->sync_install( array(), true );
4405
- }
4406
-
4407
- /**
4408
- * Return a list of modified plugins since the last sync.
4409
- *
4410
- * Note:
4411
- * There's no point to store a plugins counter since even if the number of
4412
- * plugins didn't change, we still need to check if the versions are all the
4413
- * same and the activity state is similar.
4414
- *
4415
- * @author Vova Feldman (@svovaf)
4416
- * @since 1.1.8
4417
- *
4418
- * @return array|false
4419
- */
4420
- private function get_plugins_data_for_api() {
4421
- // Alias.
4422
- $option_name = 'all_plugins';
4423
-
4424
- $all_cached_plugins = self::$_accounts->get_option( $option_name );
4425
-
4426
- if ( ! is_object( $all_cached_plugins ) ) {
4427
- $all_cached_plugins = (object) array(
4428
- 'timestamp' => '',
4429
- 'md5' => '',
4430
- 'plugins' => array(),
4431
- );
4432
- }
4433
-
4434
- $time = time();
4435
-
4436
- if ( ! empty( $all_cached_plugins->timestamp ) &&
4437
- ( $time - $all_cached_plugins->timestamp ) < WP_FS__TIME_5_MIN_IN_SEC
4438
- ) {
4439
- // Don't send plugin updates if last update was in the past 5 min.
4440
- return false;
4441
- }
4442
-
4443
- // Write timestamp to lock the logic.
4444
- $all_cached_plugins->timestamp = $time;
4445
- self::$_accounts->set_option( $option_name, $all_cached_plugins, true );
4446
-
4447
- // Reload options from DB.
4448
- self::$_accounts->load( true );
4449
- $all_cached_plugins = self::$_accounts->get_option( $option_name );
4450
-
4451
- if ( $time != $all_cached_plugins->timestamp ) {
4452
- // If timestamp is different, then another thread captured the lock.
4453
- return false;
4454
- }
4455
-
4456
- // Check if there's a change in plugins.
4457
- $all_plugins = self::get_all_plugins();
4458
-
4459
- // Check if plugins changed.
4460
- ksort( $all_plugins );
4461
-
4462
- $plugins_signature = '';
4463
- foreach ( $all_plugins as $basename => $data ) {
4464
- $plugins_signature .= $data['slug'] . ',' .
4465
- $data['Version'] . ',' .
4466
- ( $data['is_active'] ? '1' : '0' ) . ';';
4467
- }
4468
-
4469
- // Check if plugins status changed (version or active/inactive).
4470
- $plugins_changed = ( $all_cached_plugins->md5 !== md5( $plugins_signature ) );
4471
-
4472
- $plugins_update_data = array();
4473
-
4474
- if ( $plugins_changed ) {
4475
- // Change in plugins, report changes.
4476
-
4477
- // Update existing plugins info.
4478
- foreach ( $all_cached_plugins->plugins as $basename => $data ) {
4479
- if ( ! isset( $all_plugins[ $basename ] ) ) {
4480
- // Plugin uninstalled.
4481
- $uninstalled_plugin_data = $data;
4482
- $uninstalled_plugin_data['is_active'] = false;
4483
- $uninstalled_plugin_data['is_uninstalled'] = true;
4484
- $plugins_update_data[] = $uninstalled_plugin_data;
4485
-
4486
- unset( $all_plugins[ $basename ] );
4487
- unset( $all_cached_plugins->plugins[ $basename ] );
4488
- } else if ( $data['is_active'] !== $all_plugins[ $basename ]['is_active'] ||
4489
- $data['version'] !== $all_plugins[ $basename ]['Version']
4490
- ) {
4491
- // Plugin activated or deactivated, or version changed.
4492
- $all_cached_plugins->plugins[ $basename ]['is_active'] = $all_plugins[ $basename ]['is_active'];
4493
- $all_cached_plugins->plugins[ $basename ]['version'] = $all_plugins[ $basename ]['Version'];
4494
-
4495
- $plugins_update_data[] = $all_cached_plugins->plugins[ $basename ];
4496
- }
4497
- }
4498
-
4499
- // Find new plugins that weren't yet seen before.
4500
- foreach ( $all_plugins as $basename => $data ) {
4501
- if ( ! isset( $all_cached_plugins->plugins[ $basename ] ) ) {
4502
- // New plugin.
4503
- $new_plugin = array(
4504
- 'slug' => $data['slug'],
4505
- 'version' => $data['Version'],
4506
- 'title' => $data['Name'],
4507
- 'is_active' => $data['is_active'],
4508
- 'is_uninstalled' => false,
4509
- );
4510
-
4511
- $plugins_update_data[] = $new_plugin;
4512
- $all_cached_plugins->plugins[ $basename ] = $new_plugin;
4513
- }
4514
- }
4515
-
4516
- $all_cached_plugins->md5 = md5( $plugins_signature );
4517
- $all_cached_plugins->timestamp = $time;
4518
- self::$_accounts->set_option( $option_name, $all_cached_plugins, true );
4519
- }
4520
-
4521
- return $plugins_update_data;
4522
- }
4523
-
4524
- /**
4525
- * Return a list of modified themes since the last sync.
4526
- *
4527
- * Note:
4528
- * There's no point to store a themes counter since even if the number of
4529
- * themes didn't change, we still need to check if the versions are all the
4530
- * same and the activity state is similar.
4531
- *
4532
- * @author Vova Feldman (@svovaf)
4533
- * @since 1.1.8
4534
- *
4535
- * @return array|false
4536
- */
4537
- private function get_themes_data_for_api() {
4538
- // Alias.
4539
- $option_name = 'all_themes';
4540
-
4541
- $all_cached_themes = self::$_accounts->get_option( $option_name );
4542
-
4543
- if ( ! is_object( $all_cached_themes ) ) {
4544
- $all_cached_themes = (object) array(
4545
- 'timestamp' => '',
4546
- 'md5' => '',
4547
- 'themes' => array(),
4548
- );
4549
- }
4550
-
4551
- $time = time();
4552
-
4553
- if ( ! empty( $all_cached_themes->timestamp ) &&
4554
- ( $time - $all_cached_themes->timestamp ) < WP_FS__TIME_5_MIN_IN_SEC
4555
- ) {
4556
- // Don't send theme updates if last update was in the past 5 min.
4557
- return false;
4558
- }
4559
-
4560
- // Write timestamp to lock the logic.
4561
- $all_cached_themes->timestamp = $time;
4562
- self::$_accounts->set_option( $option_name, $all_cached_themes, true );
4563
-
4564
- // Reload options from DB.
4565
- self::$_accounts->load( true );
4566
- $all_cached_themes = self::$_accounts->get_option( $option_name );
4567
-
4568
- if ( $time != $all_cached_themes->timestamp ) {
4569
- // If timestamp is different, then another thread captured the lock.
4570
- return false;
4571
- }
4572
-
4573
- // Get active theme.
4574
- $active_theme = wp_get_theme();
4575
- $active_theme_stylesheet = $active_theme->get_stylesheet();
4576
-
4577
- // Check if there's a change in themes.
4578
- $all_themes = wp_get_themes();
4579
-
4580
- // Check if themes changed.
4581
- ksort( $all_themes );
4582
-
4583
- $themes_signature = '';
4584
- foreach ( $all_themes as $slug => $data ) {
4585
- $is_active = ( $slug === $active_theme_stylesheet );
4586
- $themes_signature .= $slug . ',' .
4587
- $data->version . ',' .
4588
- ( $is_active ? '1' : '0' ) . ';';
4589
- }
4590
-
4591
- // Check if themes status changed (version or active/inactive).
4592
- $themes_changed = ( $all_cached_themes->md5 !== md5( $themes_signature ) );
4593
-
4594
- $themes_update_data = array();
4595
-
4596
- if ( $themes_changed ) {
4597
- // Change in themes, report changes.
4598
-
4599
- // Update existing themes info.
4600
- foreach ( $all_cached_themes->themes as $slug => $data ) {
4601
- $is_active = ( $slug === $active_theme_stylesheet );
4602
-
4603
- if ( ! isset( $all_themes[ $slug ] ) ) {
4604
- // Plugin uninstalled.
4605
- $uninstalled_theme_data = $data;
4606
- $uninstalled_theme_data['is_active'] = false;
4607
- $uninstalled_theme_data['is_uninstalled'] = true;
4608
- $themes_update_data[] = $uninstalled_theme_data;
4609
-
4610
- unset( $all_themes[ $slug ] );
4611
- unset( $all_cached_themes->themes[ $slug ] );
4612
- } else if ( $data['is_active'] !== $is_active ||
4613
- $data['version'] !== $all_themes[ $slug ]->version
4614
- ) {
4615
- // Plugin activated or deactivated, or version changed.
4616
-
4617
- $all_cached_themes->themes[ $slug ]['is_active'] = $is_active;
4618
- $all_cached_themes->themes[ $slug ]['version'] = $all_themes[ $slug ]->version;
4619
-
4620
- $themes_update_data[] = $all_cached_themes->themes[ $slug ];
4621
- }
4622
- }
4623
-
4624
- // Find new themes that weren't yet seen before.
4625
- foreach ( $all_themes as $slug => $data ) {
4626
- if ( ! isset( $all_cached_themes->themes[ $slug ] ) ) {
4627
- $is_active = ( $slug === $active_theme_stylesheet );
4628
-
4629
- // New plugin.
4630
- $new_plugin = array(
4631
- 'slug' => $slug,
4632
- 'version' => $data->version,
4633
- 'title' => $data->name,
4634
- 'is_active' => $is_active,
4635
- 'is_uninstalled' => false,
4636
- );
4637
-
4638
- $themes_update_data[] = $new_plugin;
4639
- $all_cached_themes->themes[ $slug ] = $new_plugin;
4640
- }
4641
- }
4642
-
4643
- $all_cached_themes->md5 = md5( $themes_signature );
4644
- $all_cached_themes->timestamp = time();
4645
- self::$_accounts->set_option( $option_name, $all_cached_themes, true );
4646
- }
4647
-
4648
- return $themes_update_data;
4649
- }
4650
-
4651
- /**
4652
- * Update install details.
4653
- *
4654
- * @author Vova Feldman (@svovaf)
4655
- * @since 1.1.2
4656
- *
4657
- * @param string[] string $override
4658
- * @param bool $include_plugins Since 1.1.8 by default include plugin changes.
4659
- * @param bool $include_themes Since 1.1.8 by default include plugin changes.
4660
- *
4661
- * @return array
4662
- */
4663
- private function get_install_data_for_api(
4664
- array $override,
4665
- $include_plugins = true,
4666
- $include_themes = true
4667
- ) {
4668
- /**
4669
- * @since 1.1.8 Also send plugin updates.
4670
- */
4671
- if ( $include_plugins && ! isset( $override['plugins'] ) ) {
4672
- $plugins = $this->get_plugins_data_for_api();
4673
- if ( ! empty( $plugins ) ) {
4674
- $override['plugins'] = $plugins;
4675
- }
4676
- }
4677
- /**
4678
- * @since 1.1.8 Also send themes updates.
4679
- */
4680
- if ( $include_themes && ! isset( $override['themes'] ) ) {
4681
- $themes = $this->get_themes_data_for_api();
4682
- if ( ! empty( $themes ) ) {
4683
- $override['themes'] = $themes;
4684
- }
4685
- }
4686
-
4687
- return array_merge( array(
4688
- 'version' => $this->get_plugin_version(),
4689
- 'is_premium' => $this->is_premium(),
4690
- 'language' => get_bloginfo( 'language' ),
4691
- 'charset' => get_bloginfo( 'charset' ),
4692
- 'platform_version' => get_bloginfo( 'version' ),
4693
- 'sdk_version' => $this->version,
4694
- 'programming_language_version' => phpversion(),
4695
- 'title' => get_bloginfo( 'name' ),
4696
- 'url' => get_site_url(),
4697
- // Special params.
4698
- 'is_active' => true,
4699
- 'is_disconnected' => $this->is_tracking_prohibited(),
4700
- 'is_uninstalled' => false,
4701
- ), $override );
4702
- }
4703
-
4704
- /**
4705
- * Update install only if changed.
4706
- *
4707
- * @author Vova Feldman (@svovaf)
4708
- * @since 1.0.9
4709
- *
4710
- * @param string[] string $override
4711
- * @param bool $flush
4712
- *
4713
- * @return false|object|string
4714
- */
4715
- private function send_install_update( $override = array(), $flush = false ) {
4716
- $this->_logger->entrance();
4717
-
4718
- $check_properties = $this->get_install_data_for_api( $override );
4719
-
4720
- if ( $flush ) {
4721
- $params = $check_properties;
4722
- } else {
4723
- $params = array();
4724
- $special = array();
4725
- $special_override = false;
4726
-
4727
- foreach ( $check_properties as $p => $v ) {
4728
- if ( property_exists( $this->_site, $p ) ) {
4729
- if ( ( is_bool( $this->_site->{$p} ) || ! empty( $this->_site->{$p} ) ) &&
4730
- $this->_site->{$p} != $v
4731
- ) {
4732
- $this->_site->{$p} = $v;
4733
- $params[ $p ] = $v;
4734
- }
4735
- } else {
4736
- $special[ $p ] = $v;
4737
-
4738
- if ( isset( $override[ $p ] ) ||
4739
- 'plugins' === $p ||
4740
- 'themes' === $p
4741
- ) {
4742
- $special_override = true;
4743
- }
4744
- }
4745
- }
4746
-
4747
- if ( $special_override || 0 < count( $params ) ) {
4748
- // Add special params only if has at least one
4749
- // standard param, or if explicitly requested to
4750
- // override a special param or a param which is not exist
4751
- // in the install object.
4752
- $params = array_merge( $params, $special );
4753
- }
4754
- }
4755
-
4756
- if ( 0 < count( $params ) ) {
4757
- // Update last install sync timestamp.
4758
- $this->_storage->install_sync_timestamp = time();
4759
-
4760
- $params['uid'] = $this->get_anonymous_id();
4761
-
4762
- // Send updated values to FS.
4763
- $site = $this->get_api_site_scope()->call( '/', 'put', $params );
4764
-
4765
- if ( $this->is_api_result_entity( $site ) ) {
4766
- // I successfully sent install update, clear scheduled sync if exist.
4767
- $this->clear_install_sync_cron();
4768
- }
4769
-
4770
- return $site;
4771
- }
4772
-
4773
- return false;
4774
- }
4775
-
4776
- /**
4777
- * Update install only if changed.
4778
- *
4779
- * @author Vova Feldman (@svovaf)
4780
- * @since 1.0.9
4781
- *
4782
- * @param string[] string $override
4783
- * @param bool $flush
4784
- */
4785
- private function sync_install( $override = array(), $flush = false ) {
4786
- $this->_logger->entrance();
4787
-
4788
- $site = $this->send_install_update( $override, $flush );
4789
-
4790
- if ( false === $site ) {
4791
- // No sync required.
4792
- return;
4793
- }
4794
-
4795
- if ( ! $this->is_api_result_entity( $site ) ) {
4796
- // Failed to sync, don't update locally.
4797
- return;
4798
- }
4799
-
4800
- $plan = $this->get_plan();
4801
- $this->_site = new FS_Site( $site );
4802
- $this->_site->plan = $plan;
4803
-
4804
- $this->_store_site( true );
4805
- }
4806
-
4807
- /**
4808
- * Track install's custom event.
4809
- *
4810
- * IMPORTANT:
4811
- * Custom event tracking is currently only supported for specific clients.
4812
- * If you are not one of them, please don't use this method. If you will,
4813
- * the API will simply ignore your request based on the plugin ID.
4814
- *
4815
- * Need custom tracking for your plugin or theme?
4816
- * If you are interested in custom event tracking please contact yo@freemius.com
4817
- * for further details.
4818
- *
4819
- * @author Vova Feldman (@svovaf)
4820
- * @since 1.2.1
4821
- *
4822
- * @param string $name Event name.
4823
- * @param array $properties Associative key/value array with primitive values only
4824
- * @param bool $process_at A valid future date-time in the following format Y-m-d H:i:s.
4825
- * @param bool $once If true, event will be tracked only once. IMPORTANT: Still trigger the API call.
4826
- *
4827
- * @return object|false Event data or FALSE on failure.
4828
- *
4829
- * @throws \Freemius_InvalidArgumentException
4830
- */
4831
- public function track_event( $name, $properties = array(), $process_at = false, $once = false ) {
4832
- $this->_logger->entrance( http_build_query( array( 'name' => $name, 'once' => $once ) ) );
4833
-
4834
- if ( ! $this->is_registered() ) {
4835
- return false;
4836
- }
4837
-
4838
- $event = array( 'type' => $name );
4839
-
4840
- if ( is_numeric( $process_at ) && $process_at > time() ) {
4841
- $event['process_at'] = $process_at;
4842
- }
4843
-
4844
- if ( $once ) {
4845
- $event['once'] = true;
4846
- }
4847
-
4848
- if ( ! empty( $properties ) ) {
4849
- // Verify associative array values are primitive.
4850
- foreach ( $properties as $k => $v ) {
4851
- if ( ! is_scalar( $v ) ) {
4852
- throw new Freemius_InvalidArgumentException( 'The $properties argument must be an associative key/value array with primitive values only.' );
4853
- }
4854
- }
4855
-
4856
- $event['properties'] = $properties;
4857
- }
4858
-
4859
- $result = $this->get_api_site_scope()->call( 'events.json', 'post', $event );
4860
-
4861
- return $this->is_api_error( $result ) ?
4862
- false :
4863
- $result;
4864
- }
4865
-
4866
- /**
4867
- * Track install's custom event only once, but it still triggers the API call.
4868
- *
4869
- * IMPORTANT:
4870
- * Custom event tracking is currently only supported for specific clients.
4871
- * If you are not one of them, please don't use this method. If you will,
4872
- * the API will simply ignore your request based on the plugin ID.
4873
- *
4874
- * Need custom tracking for your plugin or theme?
4875
- * If you are interested in custom event tracking please contact yo@freemius.com
4876
- * for further details.
4877
- *
4878
- * @author Vova Feldman (@svovaf)
4879
- * @since 1.2.1
4880
- *
4881
- * @param string $name Event name.
4882
- * @param array $properties Associative key/value array with primitive values only
4883
- * @param bool $process_at A valid future date-time in the following format Y-m-d H:i:s.
4884
- *
4885
- * @return object|false Event data or FALSE on failure.
4886
- *
4887
- * @throws \Freemius_InvalidArgumentException
4888
- *
4889
- * @user Freemius::track_event()
4890
- */
4891
- public function track_event_once( $name, $properties = array(), $process_at = false ) {
4892
- return $this->track_event( $name, $properties, $process_at, true );
4893
- }
4894
-
4895
- /**
4896
- * Plugin uninstall hook.
4897
- *
4898
- * @author Vova Feldman (@svovaf)
4899
- * @since 1.0.1
4900
- *
4901
- * @param bool $check_user Enforce checking if user have plugins activation privileges.
4902
- */
4903
- function _uninstall_plugin_event( $check_user = true ) {
4904
- $this->_logger->entrance( 'slug = ' . $this->_slug );
4905
-
4906
- if ( $check_user && ! current_user_can( 'activate_plugins' ) ) {
4907
- return;
4908
- }
4909
-
4910
- $params = array();
4911
- $uninstall_reason = null;
4912
- if ( isset( $this->_storage->uninstall_reason ) ) {
4913
- $uninstall_reason = $this->_storage->uninstall_reason;
4914
- $params['reason_id'] = $uninstall_reason->id;
4915
- $params['reason_info'] = $uninstall_reason->info;
4916
- }
4917
-
4918
- if ( ! $this->is_registered() ) {
4919
- // Send anonymous uninstall event only if user submitted a feedback.
4920
- if ( isset( $uninstall_reason ) ) {
4921
- if ( isset( $uninstall_reason->is_anonymous ) && ! $uninstall_reason->is_anonymous ) {
4922
- $this->opt_in( false, false, false, false, true );
4923
- } else {
4924
- $params['uid'] = $this->get_anonymous_id();
4925
- $this->get_api_plugin_scope()->call( 'uninstall.json', 'put', $params );
4926
- }
4927
- }
4928
- } else {
4929
- // Send uninstall event.
4930
- $this->send_install_update( array_merge( $params, array(
4931
- 'is_active' => false,
4932
- 'is_uninstalled' => true,
4933
- ) ) );
4934
- }
4935
-
4936
- // @todo Decide if we want to delete plugin information from db.
4937
- }
4938
-
4939
- /**
4940
- * @author Vova Feldman (@svovaf)
4941
- * @since 1.1.1
4942
- *
4943
- * @return string
4944
- */
4945
- function premium_plugin_basename() {
4946
- return "{$this->_slug}-premium/" . basename( $this->_free_plugin_basename );
4947
- }
4948
-
4949
- /**
4950
- * Uninstall plugin hook. Called only when connected his account with Freemius for active sites tracking.
4951
- *
4952
- * @author Vova Feldman (@svovaf)
4953
- * @since 1.0.2
4954
- */
4955
- public static function _uninstall_plugin_hook() {
4956
- self::_load_required_static();
4957
-
4958
- self::$_static_logger->entrance();
4959
-
4960
- if ( ! current_user_can( 'activate_plugins' ) ) {
4961
- return;
4962
- }
4963
-
4964
- $plugin_file = substr( current_filter(), strlen( 'uninstall_' ) );
4965
-
4966
- self::$_static_logger->info( 'plugin = ' . $plugin_file );
4967
-
4968
- define( 'WP_FS__UNINSTALL_MODE', true );
4969
-
4970
- $fs = self::get_instance_by_file( $plugin_file );
4971
-
4972
- if ( is_object( $fs ) ) {
4973
- self::require_plugin_essentials();
4974
-
4975
- if ( is_plugin_active( $fs->_free_plugin_basename ) ||
4976
- is_plugin_active( $fs->premium_plugin_basename() )
4977
- ) {
4978
- // Deleting Free or Premium plugin version while the other version still installed.
4979
- return;
4980
- }
4981
-
4982
- $fs->_uninstall_plugin_event();
4983
-
4984
- $fs->do_action( 'after_uninstall' );
4985
- }
4986
- }
4987
-
4988
- #----------------------------------------------------------------------------------
4989
- #region Plugin Information
4990
- #----------------------------------------------------------------------------------
4991
-
4992
- /**
4993
- * Load WordPress core plugin.php essential module.
4994
- *
4995
- * @author Vova Feldman (@svovaf)
4996
- * @since 1.1.1
4997
- */
4998
- private static function require_plugin_essentials() {
4999
- if ( ! function_exists( 'get_plugins' ) ) {
5000
- self::$_static_logger->log( 'Including wp-admin/includes/plugin.php...' );
5001
-
5002
- require_once ABSPATH . 'wp-admin/includes/plugin.php';
5003
- }
5004
- }
5005
-
5006
- /**
5007
- * Load WordPress core pluggable.php module.
5008
- *
5009
- * @author Vova Feldman (@svovaf)
5010
- * @since 1.1.2
5011
- */
5012
- private static function require_pluggable_essentials() {
5013
- if ( ! function_exists( 'wp_get_current_user' ) ) {
5014
- require_once ABSPATH . 'wp-includes/pluggable.php';
5015
- }
5016
- }
5017
-
5018
- /**
5019
- * Return plugin data.
5020
- *
5021
- * @author Vova Feldman (@svovaf)
5022
- * @since 1.0.1
5023
- *
5024
- * @return array
5025
- */
5026
- function get_plugin_data() {
5027
- if ( ! isset( $this->_plugin_data ) ) {
5028
- self::require_plugin_essentials();
5029
-
5030
- /**
5031
- * @author Vova Feldman (@svovaf)
5032
- * @since 1.2.0 When using get_plugin_data() do NOT translate plugin data.
5033
- *
5034
- * @link https://github.com/Freemius/wordpress-sdk/issues/77
5035
- */
5036
- $this->_plugin_data = get_plugin_data(
5037
- $this->_plugin_main_file_path,
5038
- false,
5039
- false
5040
- );
5041
- }
5042
-
5043
- return $this->_plugin_data;
5044
- }
5045
-
5046
- /**
5047
- * @author Vova Feldman (@svovaf)
5048
- * @since 1.0.1
5049
- *
5050
- * @return string Plugin slug.
5051
- */
5052
- function get_slug() {
5053
- return $this->_slug;
5054
- }
5055
-
5056
- /**
5057
- * @author Vova Feldman (@svovaf)
5058
- * @since 1.2.1.7
5059
- *
5060
- * @return string Plugin slug.
5061
- */
5062
- function get_target_folder_name() {
5063
- return $this->_slug . ( $this->can_use_premium_code() ? '-premium' : '' );
5064
- }
5065
-
5066
- /**
5067
- * @author Vova Feldman (@svovaf)
5068
- * @since 1.0.1
5069
- *
5070
- * @return number Plugin ID.
5071
- */
5072
- function get_id() {
5073
- return $this->_plugin->id;
5074
- }
5075
-
5076
- /**
5077
- * @author Vova Feldman (@svovaf)
5078
- * @since 1.2.1.5
5079
- *
5080
- * @return string Freemius SDK version
5081
- */
5082
- function get_sdk_version() {
5083
- return $this->version;
5084
- }
5085
-
5086
- /**
5087
- * @author Vova Feldman (@svovaf)
5088
- * @since 1.2.1.5
5089
- *
5090
- * @return number Parent plugin ID (if parent exist).
5091
- */
5092
- function get_parent_id() {
5093
- return $this->is_addon() ?
5094
- $this->get_parent_instance()->get_id() :
5095
- $this->_plugin->id;
5096
- }
5097
-
5098
- /**
5099
- * @author Vova Feldman (@svovaf)
5100
- * @since 1.0.1
5101
- *
5102
- * @return string Plugin public key.
5103
- */
5104
- function get_public_key() {
5105
- return $this->_plugin->public_key;
5106
- }
5107
-
5108
- /**
5109
- * Will be available only on sandbox mode.
5110
- *
5111
- * @author Vova Feldman (@svovaf)
5112
- * @since 1.0.4
5113
- *
5114
- * @return mixed Plugin secret key.
5115
- */
5116
- function get_secret_key() {
5117
- return $this->_plugin->secret_key;
5118
- }
5119
-
5120
- /**
5121
- * @author Vova Feldman (@svovaf)
5122
- * @since 1.1.1
5123
- *
5124
- * @return bool
5125
- */
5126
- function has_secret_key() {
5127
- return ! empty( $this->_plugin->secret_key );
5128
- }
5129
-
5130
- /**
5131
- * @author Vova Feldman (@svovaf)
5132
- * @since 1.0.9
5133
- *
5134
- * @return string
5135
- */
5136
- function get_plugin_name() {
5137
- $this->_logger->entrance();
5138
-
5139
- if ( ! isset( $this->_plugin_name ) ) {
5140
- $plugin_data = $this->get_plugin_data();
5141
-
5142
- // Get name.
5143
- $this->_plugin_name = $plugin_data['Name'];
5144
-
5145
- // Check if plugin name contains [Premium] suffix and remove it.
5146
- $suffix = '[premium]';
5147
- $suffix_len = strlen( $suffix );
5148
-
5149
- if ( strlen( $plugin_data['Name'] ) > $suffix_len &&
5150
- $suffix === substr( strtolower( $plugin_data['Name'] ), - $suffix_len )
5151
- ) {
5152
- $this->_plugin_name = substr( $plugin_data['Name'], 0, - $suffix_len );
5153
- }
5154
-
5155
- $this->_logger->departure( 'Name = ' . $this->_plugin_name );
5156
- }
5157
-
5158
- return $this->_plugin_name;
5159
- }
5160
-
5161
- /**
5162
- * @author Vova Feldman (@svovaf)
5163
- * @since 1.0.0
5164
- *
5165
- * @return string
5166
- */
5167
- function get_plugin_version() {
5168
- $this->_logger->entrance();
5169
-
5170
- $plugin_data = $this->get_plugin_data();
5171
-
5172
- $this->_logger->departure( 'Version = ' . $plugin_data['Version'] );
5173
-
5174
- return $this->apply_filters( 'plugin_version', $plugin_data['Version'] );
5175
- }
5176
-
5177
- /**
5178
- * @author Vova Feldman (@svovaf)
5179
- * @since 1.2.1.7
5180
- *
5181
- * @return string
5182
- */
5183
- function get_plugin_title() {
5184
- $this->_logger->entrance();
5185
-
5186
- $title = $this->_plugin->title;
5187
-
5188
- return $this->apply_filters( 'plugin_title', $title );
5189
- }
5190
-
5191
- /**
5192
- * @author Vova Feldman (@svovaf)
5193
- * @since 1.0.4
5194
- *
5195
- * @return string
5196
- */
5197
- function get_plugin_basename() {
5198
- return $this->_plugin_basename;
5199
- }
5200
-
5201
- function get_plugin_folder_name() {
5202
- $this->_logger->entrance();
5203
-
5204
- $plugin_folder = $this->_plugin_basename;
5205
-
5206
- while ( '.' !== dirname( $plugin_folder ) ) {
5207
- $plugin_folder = dirname( $plugin_folder );
5208
- }
5209
-
5210
- $this->_logger->departure( 'Folder Name = ' . $plugin_folder );
5211
-
5212
- return $plugin_folder;
5213
- }
5214
-
5215
- #endregion ------------------------------------------------------------------
5216
-
5217
- /* Account
5218
- ------------------------------------------------------------------------------------------------------------------*/
5219
-
5220
- /**
5221
- * Find plugin's slug by plugin's basename.
5222
- *
5223
- * @author Vova Feldman (@svovaf)
5224
- * @since 1.0.9
5225
- *
5226
- * @param string $plugin_base_name
5227
- *
5228
- * @return false|string
5229
- */
5230
- private static function find_slug_by_basename( $plugin_base_name ) {
5231
- $file_slug_map = self::$_accounts->get_option( 'file_slug_map', array() );
5232
-
5233
- if ( ! array( $file_slug_map ) || ! isset( $file_slug_map[ $plugin_base_name ] ) ) {
5234
- return false;
5235
- }
5236
-
5237
- return $file_slug_map[ $plugin_base_name ];
5238
- }
5239
-
5240
- /**
5241
- * Store the map between the plugin's basename to the slug.
5242
- *
5243
- * @author Vova Feldman (@svovaf)
5244
- * @since 1.0.9
5245
- */
5246
- private function store_file_slug_map() {
5247
- $file_slug_map = self::$_accounts->get_option( 'file_slug_map', array() );
5248
-
5249
- if ( ! array( $file_slug_map ) ) {
5250
- $file_slug_map = array();
5251
- }
5252
-
5253
- if ( ! isset( $file_slug_map[ $this->_plugin_basename ] ) ||
5254
- $file_slug_map[ $this->_plugin_basename ] !== $this->_slug
5255
- ) {
5256
- $file_slug_map[ $this->_plugin_basename ] = $this->_slug;
5257
- self::$_accounts->set_option( 'file_slug_map', $file_slug_map, true );
5258
- }
5259
- }
5260
-
5261
- /**
5262
- * @return FS_User[]
5263
- */
5264
- static function get_all_users() {
5265
- $users = self::$_accounts->get_option( 'users', array() );
5266
-
5267
- if ( ! is_array( $users ) ) {
5268
- $users = array();
5269
- }
5270
-
5271
- return $users;
5272
- }
5273
-
5274
- /**
5275
- * @return FS_Site[]
5276
- */
5277
- private static function get_all_sites() {
5278
- $sites = self::$_accounts->get_option( 'sites', array() );
5279
-
5280
- if ( ! is_array( $sites ) ) {
5281
- $sites = array();
5282
- }
5283
-
5284
- return $sites;
5285
- }
5286
-
5287
- /**
5288
- * @author Vova Feldman (@svovaf)
5289
- * @since 1.0.6
5290
- *
5291
- * @return FS_Plugin_License[]
5292
- */
5293
- private static function get_all_licenses() {
5294
- $licenses = self::$_accounts->get_option( 'licenses', array() );
5295
-
5296
- if ( ! is_array( $licenses ) ) {
5297
- $licenses = array();
5298
- }
5299
-
5300
- return $licenses;
5301
- }
5302
-
5303
- /**
5304
- * @return FS_Plugin_Plan[]
5305
- */
5306
- private static function get_all_plans() {
5307
- $plans = self::$_accounts->get_option( 'plans', array() );
5308
-
5309
- if ( ! is_array( $plans ) ) {
5310
- $plans = array();
5311
- }
5312
-
5313
- return $plans;
5314
- }
5315
-
5316
- /**
5317
- * @author Vova Feldman (@svovaf)
5318
- * @since 1.0.4
5319
- *
5320
- * @return FS_Plugin_Tag[]
5321
- */
5322
- private static function get_all_updates() {
5323
- $updates = self::$_accounts->get_option( 'updates', array() );
5324
-
5325
- if ( ! is_array( $updates ) ) {
5326
- $updates = array();
5327
- }
5328
-
5329
- return $updates;
5330
- }
5331
-
5332
- /**
5333
- * @author Vova Feldman (@svovaf)
5334
- * @since 1.0.6
5335
- *
5336
- * @return array<number,FS_Plugin[]>|false
5337
- */
5338
- private static function get_all_addons() {
5339
- $addons = self::$_accounts->get_option( 'addons', array() );
5340
-
5341
- if ( ! is_array( $addons ) ) {
5342
- $addons = array();
5343
- }
5344
-
5345
- return $addons;
5346
- }
5347
-
5348
- /**
5349
- * @author Vova Feldman (@svovaf)
5350
- * @since 1.0.6
5351
- *
5352
- * @return FS_Plugin[]|false
5353
- */
5354
- private static function get_all_account_addons() {
5355
- $addons = self::$_accounts->get_option( 'account_addons', array() );
5356
-
5357
- if ( ! is_array( $addons ) ) {
5358
- $addons = array();
5359
- }
5360
-
5361
- return $addons;
5362
- }
5363
-
5364
- /**
5365
- * Check if user has connected his account (opted-in).
5366
- *
5367
- * Note:
5368
- * If the user opted-in and opted-out on a later stage,
5369
- * this will still return true. If you want to check if the
5370
- * user is currently opted-in, use:
5371
- * `$fs->is_registered() && $fs->is_tracking_allowed()`
5372
- *
5373
- * @author Vova Feldman (@svovaf)
5374
- * @since 1.0.1
5375
- * @return bool
5376
- */
5377
- function is_registered() {
5378
- return is_object( $this->_user );
5379
- }
5380
-
5381
- /**
5382
- * Returns TRUE if the user opted-in and didn't disconnect (opt-out).
5383
- *
5384
- * @author Leo Fajardo (@leorw)
5385
- * @since 1.2.1.5
5386
- *
5387
- * @return bool
5388
- */
5389
- function is_tracking_allowed() {
5390
- return ( is_object( $this->_site ) && true !== $this->_site->is_disconnected );
5391
- }
5392
-
5393
- /**
5394
- * @author Vova Feldman (@svovaf)
5395
- * @since 1.0.4
5396
- *
5397
- * @return FS_Plugin
5398
- */
5399
- function get_plugin() {
5400
- return $this->_plugin;
5401
- }
5402
-
5403
- /**
5404
- * @author Vova Feldman (@svovaf)
5405
- * @since 1.0.3
5406
- *
5407
- * @return FS_User
5408
- */
5409
- function get_user() {
5410
- return $this->_user;
5411
- }
5412
-
5413
- /**
5414
- * @author Vova Feldman (@svovaf)
5415
- * @since 1.0.3
5416
- *
5417
- * @return FS_Site
5418
- */
5419
- function get_site() {
5420
- return $this->_site;
5421
- }
5422
-
5423
- /**
5424
- * Get plugin add-ons.
5425
- *
5426
- * @author Vova Feldman (@svovaf)
5427
- * @since 1.0.6
5428
- *
5429
- * @since 1.1.7.3 If not yet loaded, fetch data from the API.
5430
- *
5431
- * @param bool $flush
5432
- *
5433
- * @return FS_Plugin[]|false
5434
- */
5435
- function get_addons( $flush = false ) {
5436
- $this->_logger->entrance();
5437
-
5438
- if ( ! $this->_has_addons ) {
5439
- return false;
5440
- }
5441
-
5442
- $addons = $this->sync_addons( $flush );
5443
-
5444
- return ( ! is_array( $addons ) || empty( $addons ) ) ?
5445
- false :
5446
- $addons;
5447
- }
5448
-
5449
- /**
5450
- * @author Vova Feldman (@svovaf)
5451
- * @since 1.0.6
5452
- *
5453
- * @return FS_Plugin[]|false
5454
- */
5455
- function get_account_addons() {
5456
- $this->_logger->entrance();
5457
-
5458
- $addons = self::get_all_account_addons();
5459
-
5460
- if ( ! is_array( $addons ) ||
5461
- ! isset( $addons[ $this->_plugin->id ] ) ||
5462
- ! is_array( $addons[ $this->_plugin->id ] ) ||
5463
- 0 === count( $addons[ $this->_plugin->id ] )
5464
- ) {
5465
- return false;
5466
- }
5467
-
5468
- return $addons[ $this->_plugin->id ];
5469
- }
5470
-
5471
- /**
5472
- * Check if user has any
5473
- *
5474
- * @author Vova Feldman (@svovaf)
5475
- * @since 1.1.6
5476
- *
5477
- * @return bool
5478
- */
5479
- function has_account_addons() {
5480
- $addons = $this->get_account_addons();
5481
-
5482
- return is_array( $addons ) && ( 0 < count( $addons ) );
5483
- }
5484
-
5485
-
5486
- /**
5487
- * Get add-on by ID (from local data).
5488
- *
5489
- * @author Vova Feldman (@svovaf)
5490
- * @since 1.0.6
5491
- *
5492
- * @param number $id
5493
- *
5494
- * @return FS_Plugin|false
5495
- */
5496
- function get_addon( $id ) {
5497
- $this->_logger->entrance();
5498
-
5499
- $addons = $this->get_addons();
5500
-
5501
- if ( is_array( $addons ) ) {
5502
- foreach ( $addons as $addon ) {
5503
- if ( $id == $addon->id ) {
5504
- return $addon;
5505
- }
5506
- }
5507
- }
5508
-
5509
- return false;
5510
- }
5511
-
5512
- /**
5513
- * Get add-on by slug (from local data).
5514
- *
5515
- * @author Vova Feldman (@svovaf)
5516
- * @since 1.0.6
5517
- *
5518
- * @param string $slug
5519
- *
5520
- * @param bool $flush
5521
- *
5522
- * @return FS_Plugin|false
5523
- */
5524
- function get_addon_by_slug( $slug, $flush = false ) {
5525
- $this->_logger->entrance();
5526
-
5527
- $addons = $this->get_addons( $flush );
5528
-
5529
- if ( is_array( $addons ) ) {
5530
- foreach ( $addons as $addon ) {
5531
- if ( $slug === $addon->slug ) {
5532
- return $addon;
5533
- }
5534
- }
5535
- }
5536
-
5537
- return false;
5538
- }
5539
-
5540
- #----------------------------------------------------------------------------------
5541
- #region Plans & Licensing
5542
- #----------------------------------------------------------------------------------
5543
-
5544
- /**
5545
- * Check if running premium plugin code.
5546
- *
5547
- * @author Vova Feldman (@svovaf)
5548
- * @since 1.0.5
5549
- *
5550
- * @return bool
5551
- */
5552
- function is_premium() {
5553
- return $this->_plugin->is_premium;
5554
- }
5555
-
5556
- /**
5557
- * Get site's plan ID.
5558
- *
5559
- * @author Vova Feldman (@svovaf)
5560
- * @since 1.0.2
5561
- *
5562
- * @return number
5563
- */
5564
- function get_plan_id() {
5565
- return $this->_site->plan->id;
5566
- }
5567
-
5568
- /**
5569
- * Get site's plan title.
5570
- *
5571
- * @author Vova Feldman (@svovaf)
5572
- * @since 1.0.2
5573
- *
5574
- * @return string
5575
- */
5576
- function get_plan_title() {
5577
- return $this->_site->plan->title;
5578
- }
5579
-
5580
- /**
5581
- * @author Vova Feldman (@svovaf)
5582
- * @since 1.0.9
5583
- *
5584
- * @return FS_Plugin_Plan|false
5585
- */
5586
- function get_plan() {
5587
- return is_object( $this->_site->plan ) ?
5588
- $this->_site->plan :
5589
- false;
5590
- }
5591
-
5592
- /**
5593
- * @author Vova Feldman (@svovaf)
5594
- * @since 1.0.3
5595
- *
5596
- * @return bool
5597
- */
5598
- function is_trial() {
5599
- $this->_logger->entrance();
5600
-
5601
- if ( ! $this->is_registered() ) {
5602
- return false;
5603
- }
5604
-
5605
- return $this->_site->is_trial();
5606
- }
5607
-
5608
- /**
5609
- * Check if currently in a trial with payment method (credit card or paypal).
5610
- *
5611
- * @author Vova Feldman (@svovaf)
5612
- * @since 1.1.7
5613
- *
5614
- * @return bool
5615
- */
5616
- function is_paid_trial() {
5617
- $this->_logger->entrance();
5618
-
5619
- if ( ! $this->is_trial() ) {
5620
- return false;
5621
- }
5622
-
5623
- return $this->has_active_valid_license() && ( $this->_site->trial_plan_id == $this->_license->plan_id );
5624
- }
5625
-
5626
- /**
5627
- * Check if trial already utilized.
5628
- *
5629
- * @since 1.0.9
5630
- *
5631
- * @return bool
5632
- */
5633
- function is_trial_utilized() {
5634
- $this->_logger->entrance();
5635
-
5636
- if ( ! $this->is_registered() ) {
5637
- return false;
5638
- }
5639
-
5640
- return $this->_site->is_trial_utilized();
5641
- }
5642
-
5643
- /**
5644
- * Get trial plan information (if in trial).
5645
- *
5646
- * @author Vova Feldman (@svovaf)
5647
- * @since 1.0.9
5648
- *
5649
- * @return bool|FS_Plugin_Plan
5650
- */
5651
- function get_trial_plan() {
5652
- $this->_logger->entrance();
5653
-
5654
- if ( ! $this->is_trial() ) {
5655
- return false;
5656
- }
5657
-
5658
- return $this->_storage->trial_plan;
5659
- }
5660
-
5661
- /**
5662
- * Check if the user has an activate, non-expired license on current plugin's install.
5663
- *
5664
- * @since 1.0.9
5665
- *
5666
- * @return bool
5667
- */
5668
- function is_paying() {
5669
- $this->_logger->entrance();
5670
-
5671
- if ( ! $this->is_registered() ) {
5672
- return false;
5673
- }
5674
-
5675
- if ( ! $this->has_paid_plan() ) {
5676
- return false;
5677
- }
5678
-
5679
- return (
5680
- ! $this->is_trial() &&
5681
- 'free' !== $this->_site->plan->name &&
5682
- $this->has_active_valid_license()
5683
- );
5684
- }
5685
-
5686
- /**
5687
- * @author Vova Feldman (@svovaf)
5688
- * @since 1.0.4
5689
- *
5690
- * @return bool
5691
- */
5692
- function is_free_plan() {
5693
- if ( ! $this->is_registered() ) {
5694
- return true;
5695
- }
5696
-
5697
- if ( ! $this->has_paid_plan() ) {
5698
- return true;
5699
- }
5700
-
5701
- return (
5702
- 'free' === $this->_site->plan->name ||
5703
- ! $this->has_features_enabled_license()
5704
- );
5705
- }
5706
-
5707
- /**
5708
- * @author Vova Feldman (@svovaf)
5709
- * @since 1.0.5
5710
- *
5711
- * @return bool
5712
- */
5713
- function _has_premium_license() {
5714
- $this->_logger->entrance();
5715
-
5716
- $premium_license = $this->_get_available_premium_license();
5717
-
5718
- return ( false !== $premium_license );
5719
- }
5720
-
5721
- /**
5722
- * Check if user has any licenses associated with the plugin (including expired or blocking).
5723
- *
5724
- * @author Vova Feldman (@svovaf)
5725
- * @since 1.1.7.3
5726
- *
5727
- * @return bool
5728
- */
5729
- private function has_any_license() {
5730
- return is_array( $this->_licenses ) && ( 0 < count( $this->_licenses ) );
5731
- }
5732
-
5733
- /**
5734
- * @author Vova Feldman (@svovaf)
5735
- * @since 1.0.5
5736
- *
5737
- * @return FS_Plugin_License|false
5738
- */
5739
- function _get_available_premium_license() {
5740
- $this->_logger->entrance();
5741
-
5742
- if ( ! $this->has_paid_plan() ) {
5743
- return false;
5744
- }
5745
-
5746
- if ( is_array( $this->_licenses ) ) {
5747
- foreach ( $this->_licenses as $license ) {
5748
- if ( ! $license->is_utilized() && $license->is_features_enabled() ) {
5749
- return $license;
5750
- }
5751
- }
5752
- }
5753
-
5754
- return false;
5755
- }
5756
-
5757
- /**
5758
- * Sync local plugin plans with remote server.
5759
- *
5760
- * @author Vova Feldman (@svovaf)
5761
- * @since 1.0.5
5762
- *
5763
- * @return FS_Plugin_Plan[]|object
5764
- */
5765
- function _sync_plans() {
5766
- $plans = $this->_fetch_plugin_plans();
5767
-
5768
- if ( $this->is_array_instanceof( $plans, 'FS_Plugin_Plan' ) ) {
5769
- $this->_plans = $plans;
5770
- $this->_store_plans();
5771
- }
5772
-
5773
- $this->do_action( 'after_plans_sync', $plans );
5774
-
5775
- return $this->_plans;
5776
- }
5777
-
5778
- /**
5779
- * @author Vova Feldman (@svovaf)
5780
- * @since 1.0.5
5781
- *
5782
- * @param number $id
5783
- *
5784
- * @return FS_Plugin_Plan|false
5785
- */
5786
- function _get_plan_by_id( $id ) {
5787
- $this->_logger->entrance();
5788
-
5789
- if ( ! is_array( $this->_plans ) || 0 === count( $this->_plans ) ) {
5790
- $this->_sync_plans();
5791
- }
5792
-
5793
- foreach ( $this->_plans as $plan ) {
5794
- if ( $id == $plan->id ) {
5795
- return $plan;
5796
- }
5797
- }
5798
-
5799
- return false;
5800
- }
5801
-
5802
- /**
5803
- * @author Vova Feldman (@svovaf)
5804
- * @since 1.1.8.1
5805
- *
5806
- * @param string $name
5807
- *
5808
- * @return FS_Plugin_Plan|false
5809
- */
5810
- private function get_plan_by_name( $name ) {
5811
- $this->_logger->entrance();
5812
-
5813
- if ( ! is_array( $this->_plans ) || 0 === count( $this->_plans ) ) {
5814
- $this->_sync_plans();
5815
- }
5816
-
5817
- foreach ( $this->_plans as $plan ) {
5818
- if ( $name == $plan->name ) {
5819
- return $plan;
5820
- }
5821
- }
5822
-
5823
- return false;
5824
- }
5825
-
5826
- /**
5827
- * Sync local plugin plans with remote server.
5828
- *
5829
- * @author Vova Feldman (@svovaf)
5830
- * @since 1.0.6
5831
- *
5832
- * @param number|bool $site_license_id
5833
- *
5834
- * @return FS_Plugin_License[]|object
5835
- */
5836
- function _sync_licenses( $site_license_id = false ) {
5837
- $licenses = $this->_fetch_licenses( false, $site_license_id );
5838
-
5839
- if ( $this->is_array_instanceof( $licenses, 'FS_Plugin_License' ) ) {
5840
- $this->_licenses = $licenses;
5841
- $this->_store_licenses();
5842
- }
5843
-
5844
- // Update current license.
5845
- if ( is_object( $this->_license ) ) {
5846
- $this->_license = $this->_get_license_by_id( $this->_license->id );
5847
- }
5848
-
5849
- return $this->_licenses;
5850
- }
5851
-
5852
- /**
5853
- * @author Vova Feldman (@svovaf)
5854
- * @since 1.0.5
5855
- *
5856
- * @param number $id
5857
- *
5858
- * @return FS_Plugin_License|false
5859
- */
5860
- function _get_license_by_id( $id ) {
5861
- $this->_logger->entrance();
5862
-
5863
- if ( ! is_numeric( $id ) ) {
5864
- return false;
5865
- }
5866
-
5867
- if ( ! $this->has_any_license() ) {
5868
- $this->_sync_licenses();
5869
- }
5870
-
5871
- foreach ( $this->_licenses as $license ) {
5872
- if ( $id == $license->id ) {
5873
- return $license;
5874
- }
5875
- }
5876
-
5877
- return false;
5878
- }
5879
-
5880
- /**
5881
- * Sync site's license with user licenses.
5882
- *
5883
- * @author Vova Feldman (@svovaf)
5884
- * @since 1.0.6
5885
- *
5886
- * @param FS_Plugin_License|null $new_license
5887
- */
5888
- function _update_site_license( $new_license ) {
5889
- $this->_logger->entrance();
5890
-
5891
- $this->_license = $new_license;
5892
-
5893
- if ( ! is_object( $new_license ) ) {
5894
- $this->_site->license_id = null;
5895
- $this->_sync_site_subscription( null );
5896
-
5897
- return;
5898
- }
5899
-
5900
- $this->_site->license_id = $this->_license->id;
5901
-
5902
- if ( ! is_array( $this->_licenses ) ) {
5903
- $this->_licenses = array();
5904
- }
5905
-
5906
- $is_license_found = false;
5907
- for ( $i = 0, $len = count( $this->_licenses ); $i < $len; $i ++ ) {
5908
- if ( $new_license->id == $this->_licenses[ $i ]->id ) {
5909
- $this->_licenses[ $i ] = $new_license;
5910
-
5911
- $is_license_found = true;
5912
- break;
5913
- }
5914
- }
5915
-
5916
- // If new license just append.
5917
- if ( ! $is_license_found ) {
5918
- $this->_licenses[] = $new_license;
5919
- }
5920
-
5921
- $this->_sync_site_subscription( $new_license );
5922
- }
5923
-
5924
- /**
5925
- * Sync site's subscription.
5926
- *
5927
- * @author Vova Feldman (@svovaf)
5928
- * @since 1.0.9
5929
- *
5930
- * @param FS_Plugin_License|null $license
5931
- *
5932
- * @return bool|\FS_Subscription
5933
- */
5934
- private function _sync_site_subscription( $license ) {
5935
- if ( ! is_object( $license ) ) {
5936
- unset( $this->_storage->subscription );
5937
-
5938
- return false;
5939
- }
5940
-
5941
- // Load subscription details if not lifetime.
5942
- $subscription = $license->is_lifetime() ?
5943
- false :
5944
- $this->_fetch_site_license_subscription();
5945
-
5946
- if ( is_object( $subscription ) && ! isset( $subscription->error ) ) {
5947
- $this->_storage->subscription = $subscription;
5948
- } else {
5949
- unset( $this->_storage->subscription );
5950
- }
5951
-
5952
- return $subscription;
5953
- }
5954
-
5955
- /**
5956
- * @author Vova Feldman (@svovaf)
5957
- * @since 1.0.6
5958
- *
5959
- * @return bool|\FS_Plugin_License
5960
- */
5961
- function _get_license() {
5962
- return $this->_license;
5963
- }
5964
-
5965
- /**
5966
- * @return bool|\FS_Subscription
5967
- */
5968
- function _get_subscription() {
5969
- return isset( $this->_storage->subscription ) ?
5970
- $this->_storage->subscription :
5971
- false;
5972
- }
5973
-
5974
- /**
5975
- * @author Vova Feldman (@svovaf)
5976
- * @since 1.0.2
5977
- *
5978
- * @param string $plan Plan name
5979
- * @param bool $exact If true, looks for exact plan. If false, also check "higher" plans.
5980
- *
5981
- * @return bool
5982
- */
5983
- function is_plan( $plan, $exact = false ) {
5984
- $this->_logger->entrance();
5985
-
5986
- if ( ! $this->is_registered() ) {
5987
- return false;
5988
- }
5989
-
5990
- $plan = strtolower( $plan );
5991
-
5992
- if ( $this->_site->plan->name === $plan ) // Exact plan.
5993
- {
5994
- return true;
5995
- } else if ( $exact ) // Required exact, but plans are different.
5996
- {
5997
- return false;
5998
- }
5999
-
6000
- $current_plan_order = - 1;
6001
- $required_plan_order = - 1;
6002
- for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
6003
- if ( $plan === $this->_plans[ $i ]->name ) {
6004
- $required_plan_order = $i;
6005
- } else if ( $this->_site->plan->name === $this->_plans[ $i ]->name ) {
6006
- $current_plan_order = $i;
6007
- }
6008
- }
6009
-
6010
- return ( $current_plan_order > $required_plan_order );
6011
- }
6012
-
6013
- /**
6014
- * Check if module has only one plan.
6015
- *
6016
- * @author Vova Feldman (@svovaf)
6017
- * @since 1.2.1.7
6018
- *
6019
- * @return bool
6020
- */
6021
- function is_single_plan() {
6022
- $this->_logger->entrance();
6023
-
6024
- if ( ! $this->is_registered() ||
6025
- ! is_array( $this->_plans ) ||
6026
- 0 === count( $this->_plans )
6027
- ) {
6028
- return true;
6029
- }
6030
-
6031
- return ( 1 === count( $this->_plans ) );
6032
- }
6033
-
6034
- /**
6035
- * Check if plan based on trial. If not in trial mode, should return false.
6036
- *
6037
- * @since 1.0.9
6038
- *
6039
- * @param string $plan Plan name
6040
- * @param bool $exact If true, looks for exact plan. If false, also check "higher" plans.
6041
- *
6042
- * @return bool
6043
- */
6044
- function is_trial_plan( $plan, $exact = false ) {
6045
- $this->_logger->entrance();
6046
-
6047
- if ( ! $this->is_registered() ) {
6048
- return false;
6049
- }
6050
-
6051
- if ( ! $this->is_trial() ) {
6052
- return false;
6053
- }
6054
-
6055
- if ( ! isset( $this->_storage->trial_plan ) ) {
6056
- // Store trial plan information.
6057
- $this->_enrich_site_trial_plan( true );
6058
- }
6059
-
6060
- if ( $this->_storage->trial_plan->name === $plan ) // Exact plan.
6061
- {
6062
- return true;
6063
- } else if ( $exact ) // Required exact, but plans are different.
6064
- {
6065
- return false;
6066
- }
6067
-
6068
- $current_plan_order = - 1;
6069
- $required_plan_order = - 1;
6070
- for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
6071
- if ( $plan === $this->_plans[ $i ]->name ) {
6072
- $required_plan_order = $i;
6073
- } else if ( $this->_storage->trial_plan->name === $this->_plans[ $i ]->name ) {
6074
- $current_plan_order = $i;
6075
- }
6076
- }
6077
-
6078
- return ( $current_plan_order > $required_plan_order );
6079
- }
6080
-
6081
- /**
6082
- * Check if plugin has any paid plans.
6083
- *
6084
- * @author Vova Feldman (@svovaf)
6085
- * @since 1.0.7
6086
- *
6087
- * @return bool
6088
- */
6089
- function has_paid_plan() {
6090
- return $this->_has_paid_plans ||
6091
- FS_Plan_Manager::instance()->has_paid_plan( $this->_plans );
6092
- }
6093
-
6094
- /**
6095
- * Check if plugin has any plan with a trail.
6096
- *
6097
- * @author Vova Feldman (@svovaf)
6098
- * @since 1.0.9
6099
- *
6100
- * @return bool
6101
- */
6102
- function has_trial_plan() {
6103
- if ( ! $this->is_registered() ) {
6104
- /**
6105
- * @author Vova Feldman(@svovaf)
6106
- * @since 1.2.1.5
6107
- *
6108
- * Allow setting a trial from the SDK without calling the API.
6109
- * But, if the user did opt-in, continue using the real data from the API.
6110
- */
6111
- if ( $this->_trial_days >= 0 ) {
6112
- return true;
6113
- }
6114
-
6115
- return false;
6116
- }
6117
-
6118
- return $this->_storage->get( 'has_trial_plan', false );
6119
- }
6120
-
6121
- /**
6122
- * Check if plugin has any free plan, or is it premium only.
6123
- *
6124
- * Note: If no plans configured, assume plugin is free.
6125
- *
6126
- * @author Vova Feldman (@svovaf)
6127
- * @since 1.0.7
6128
- *
6129
- * @return bool
6130
- */
6131
- function has_free_plan() {
6132
- return ! $this->is_only_premium();
6133
- }
6134
-
6135
- /**
6136
- * Displays a license activation dialog box when the user clicks on the "Activate License"
6137
- * or "Change License" link on the plugins
6138
- * page.
6139
- *
6140
- * @author Leo Fajardo (@leorw)
6141
- * @since 1.1.9
6142
- */
6143
- function _add_license_activation_dialog_box() {
6144
- $vars = array(
6145
- 'slug' => $this->_slug,
6146
- );
6147
-
6148
- fs_require_template( 'forms/license-activation.php', $vars );
6149
- fs_require_template( 'forms/resend-key.php', $vars );
6150
- }
6151
-
6152
- /**
6153
- * Displays the opt-out dialog box when the user clicks on the "Opt Out" link on the "Plugins"
6154
- * page.
6155
- *
6156
- * @author Leo Fajardo (@leorw)
6157
- * @since 1.2.1.5
6158
- */
6159
- function _add_optout_dialog() {
6160
- $vars = array(
6161
- 'slug' => $this->_slug,
6162
- );
6163
-
6164
- fs_require_template( 'forms/optout.php', $vars );
6165
- }
6166
-
6167
- /**
6168
- * Prepare page to include all required UI and logic for the license activation dialog.
6169
- *
6170
- * @author Vova Feldman (@svovaf)
6171
- * @since 1.2.0
6172
- */
6173
- function _add_license_activation() {
6174
- if ( ! $this->is_user_admin() ) {
6175
- // Only admins can activate a license.
6176
- return;
6177
- }
6178
-
6179
- if ( ! $this->has_paid_plan() ) {
6180
- // Module doesn't have any paid plans.
6181
- return;
6182
- }
6183
-
6184
- if ( ! $this->is_premium() ) {
6185
- // Only add license activation logic to the premium version.
6186
- return;
6187
- }
6188
-
6189
- // Add license activation link and AJAX request handler.
6190
- if ( $this->is_plugins_page() ) {
6191
- /**
6192
- * @since 1.2.0 Add license action link only on plugins page.
6193
- */
6194
- $this->_add_license_action_link();
6195
- }
6196
-
6197
- // Add license activation AJAX callback.
6198
- $this->add_ajax_action( 'activate_license', array( &$this, '_activate_license_ajax_action' ) );
6199
-
6200
- // Add resend license AJAX callback.
6201
- $this->add_ajax_action( 'resend_license_key', array( &$this, '_resend_license_key_ajax_action' ) );
6202
- }
6203
-
6204
- /**
6205
- * @author Leo Fajardo (@leorw)
6206
- * @since 1.1.9
6207
- */
6208
- function _activate_license_ajax_action() {
6209
- $this->_logger->entrance();
6210
-
6211
- $this->check_ajax_referer( 'activate_license' );
6212
-
6213
- $license_key = trim( fs_request_get( 'license_key' ) );
6214
-
6215
- if ( empty( $license_key ) ) {
6216
- exit;
6217
- }
6218
-
6219
- $slug = fs_request_get( 'slug', '', 'post' );
6220
- $fs = ( $slug === $this->_slug ) ?
6221
- $this :
6222
- $this->get_addon_instance( $slug );
6223
-
6224
- $error = false;
6225
- $next_page = false;
6226
-
6227
- if ( $fs->is_registered() ) {
6228
- $api = $fs->get_api_site_scope();
6229
- $install = $api->call( '/', 'put', array(
6230
- 'license_key' => $fs->apply_filters( 'license_key', $license_key )
6231
- ) );
6232
-
6233
- if ( isset( $install->error ) ) {
6234
- $error = $install->error->message;
6235
- } else {
6236
- $parent_fs = $fs->is_addon() ?
6237
- $fs->get_parent_instance() :
6238
- $fs;
6239
-
6240
- $next_page = $parent_fs->_get_sync_license_url( $fs->get_id(), true );
6241
-
6242
- $fs->reconnect_locally();
6243
- }
6244
- } else {
6245
- $next_page = $fs->opt_in( false, false, false, $license_key );
6246
-
6247
- if ( isset( $next_page->error ) ) {
6248
- $error = $next_page->error;
6249
- }
6250
- }
6251
-
6252
- $result = array(
6253
- 'success' => ( false === $error )
6254
- );
6255
-
6256
- if ( false !== $error ) {
6257
- $result['error'] = $error;
6258
- } else {
6259
- $result['next_page'] = $next_page;
6260
- }
6261
-
6262
- echo json_encode( $result );
6263
-
6264
- exit;
6265
- }
6266
-
6267
- /**
6268
- * Billing update AJAX callback.
6269
- *
6270
- * @author Vova Feldman (@svovaf)
6271
- * @since 1.2.1.5
6272
- */
6273
- function _update_billing_ajax_action() {
6274
- $this->_logger->entrance();
6275
-
6276
- $this->check_ajax_referer( 'update_billing' );
6277
-
6278
- if ( ! $this->is_user_admin() ) {
6279
- // Only for admins.
6280
- self::shoot_ajax_failure();
6281
- }
6282
-
6283
- $billing = fs_request_get( 'billing' );
6284
-
6285
- $api = $this->get_api_user_scope();
6286
- $result = $api->call( '/billing.json', 'put', array_merge( $billing, array(
6287
- 'plugin_id' => $this->get_parent_id(),
6288
- ) ) );
6289
-
6290
- if ( ! $this->is_api_result_entity( $result ) ) {
6291
- self::shoot_ajax_failure();
6292
- }
6293
-
6294
- // Purge cached billing.
6295
- $this->get_api_user_scope()->purge_cache( 'billing.json' );
6296
-
6297
- self::shoot_ajax_success();
6298
- }
6299
-
6300
- /**
6301
- * Trial start for anonymous users (AJAX callback).
6302
- *
6303
- * @author Vova Feldman (@svovaf)
6304
- * @since 1.2.1.5
6305
- */
6306
- function _start_trial_ajax_action() {
6307
- $this->_logger->entrance();
6308
-
6309
- $this->check_ajax_referer( 'start_trial' );
6310
-
6311
- if ( ! $this->is_user_admin() ) {
6312
- // Only for admins.
6313
- self::shoot_ajax_failure();
6314
- }
6315
-
6316
- $trial_data = fs_request_get( 'trial' );
6317
-
6318
- $next_page = $this->opt_in(
6319
- false,
6320
- false,
6321
- false,
6322
- false,
6323
- false,
6324
- $trial_data['plan_id']
6325
- );
6326
-
6327
- if ( is_object( $next_page ) && $this->is_api_error( $next_page ) ) {
6328
- self::shoot_ajax_failure(
6329
- isset( $next_page->error ) ?
6330
- $next_page->error->message :
6331
- var_export( $next_page, true )
6332
- );
6333
- }
6334
-
6335
- self::shoot_ajax_success( array(
6336
- 'next_page' => $next_page,
6337
- ) );
6338
- }
6339
-
6340
- /**
6341
- * @author Leo Fajardo (@leorw)
6342
- * @since 1.2.0
6343
- */
6344
- function _resend_license_key_ajax_action() {
6345
- $this->_logger->entrance();
6346
-
6347
- $this->check_ajax_referer( 'resend_license_key' );
6348
-
6349
- $email_address = sanitize_email( trim( fs_request_get( 'email', '', 'post' ) ) );
6350
-
6351
- if ( empty( $email_address ) ) {
6352
- exit;
6353
- }
6354
-
6355
- $error = false;
6356
-
6357
- $api = $this->get_api_plugin_scope();
6358
- $result = $api->call( '/licenses/resend.json', 'post',
6359
- array(
6360
- 'email' => $email_address,
6361
- 'url' => home_url(),
6362
- )
6363
- );
6364
-
6365
- if ( is_object( $result ) && isset( $result->error ) ) {
6366
- $error = $result->error;
6367
-
6368
- if ( in_array( $error->code, array( 'invalid_email', 'no_user' ) ) ) {
6369
- $error = $this->get_text( 'email-not-found' );
6370
- } else if ( 'no_license' === $error->code ) {
6371
- $error = $this->get_text( 'no-active-licenses' );
6372
- } else {
6373
- $error = $error->message;
6374
- }
6375
- }
6376
-
6377
- $licenses = array(
6378
- 'success' => ( false === $error )
6379
- );
6380
-
6381
- if ( false !== $error ) {
6382
- $licenses['error'] = sprintf( '%s... %s', $this->get_text( 'oops' ), strtolower( $error ) );
6383
- }
6384
-
6385
- echo json_encode( $licenses );
6386
-
6387
- exit;
6388
- }
6389
-
6390
- /**
6391
  * @author Vova Feldman (@svovaf)
6392
- * @since 1.2.1.8
6393
  *
6394
- * @var string
6395
  */
6396
- private static $_pagenow;
 
 
6397
 
6398
  /**
6399
- * Get current page or the referer if executing a WP AJAX request.
 
 
6400
  *
6401
  * @author Vova Feldman (@svovaf)
6402
- * @since 1.2.1.8
6403
  *
6404
  * @return string
6405
  */
6406
- static function get_current_page() {
6407
- if ( ! isset( self::$_pagenow ) ) {
6408
- global $pagenow;
6409
 
6410
- self::$_pagenow = $pagenow;
 
6411
 
6412
- if ( self::is_ajax() &&
6413
- 'admin-ajax.php' === $pagenow
6414
- ) {
6415
- $referer = wp_get_raw_referer();
 
 
 
 
 
 
 
 
 
 
6416
 
6417
- if ( is_string( $referer ) ) {
6418
- $parts = explode( '?', $referer );
 
 
6419
 
6420
- self::$_pagenow = basename( $parts[0] );
6421
- }
6422
- }
6423
  }
6424
 
6425
- return self::$_pagenow;
 
 
 
 
 
 
 
 
 
 
 
6426
  }
6427
 
6428
  /**
6429
- * Helper method to check if user in the plugins page.
6430
- *
6431
- * @author Vova Feldman (@svovaf)
6432
- * @since 1.2.1.5
6433
- *
6434
- * @return bool
6435
- */
6436
- function is_plugins_page() {
6437
- return ( 'plugins.php' === self::get_current_page() );
6438
- }
6439
-
6440
- /**
6441
- * Helper method to check if user in the themes page.
6442
- *
6443
- * @author Vova Feldman (@svovaf)
6444
- * @since 1.2.2.6
6445
- *
6446
- * @return bool
6447
- */
6448
- function is_themes_page() {
6449
- return ( 'themes.php' === self::get_current_page() );
6450
- }
6451
-
6452
- #----------------------------------------------------------------------------------
6453
- #region URL Generators
6454
- #----------------------------------------------------------------------------------
6455
-
6456
- /**
6457
- * Alias to pricing_url().
6458
- *
6459
- * @author Vova Feldman (@svovaf)
6460
- * @since 1.0.2
6461
- *
6462
- * @uses pricing_url()
6463
- *
6464
- * @param string $period Billing cycle
6465
- * @param bool $is_trial
6466
- *
6467
- * @return string
6468
- */
6469
- function get_upgrade_url( $period = WP_FS__PERIOD_ANNUALLY, $is_trial = false ) {
6470
- return $this->pricing_url( $period, $is_trial );
6471
- }
6472
-
6473
- /**
6474
- * @author Vova Feldman (@svovaf)
6475
- * @since 1.0.9
6476
- *
6477
- * @uses get_upgrade_url()
6478
- *
6479
- * @return string
6480
- */
6481
- function get_trial_url() {
6482
- return $this->get_upgrade_url( WP_FS__PERIOD_ANNUALLY, true );
6483
- }
6484
-
6485
- /**
6486
- * Plugin's pricing URL.
6487
- *
6488
- * @author Vova Feldman (@svovaf)
6489
- * @since 1.0.4
6490
- *
6491
- * @param string $billing_cycle Billing cycle
6492
- *
6493
- * @param bool $is_trial
6494
- *
6495
- * @return string
6496
- */
6497
- function pricing_url( $billing_cycle = WP_FS__PERIOD_ANNUALLY, $is_trial = false ) {
6498
- $this->_logger->entrance();
6499
-
6500
- $params = array(
6501
- 'billing_cycle' => $billing_cycle
6502
- );
6503
-
6504
- if ( $is_trial ) {
6505
- $params['trial'] = 'true';
6506
- }
6507
-
6508
- return $this->_get_admin_page_url( 'pricing', $params );
6509
- }
6510
-
6511
- /**
6512
- * Checkout page URL.
6513
- *
6514
- * @author Vova Feldman (@svovaf)
6515
- * @since 1.0.6
6516
- *
6517
- * @param string $billing_cycle Billing cycle
6518
- * @param bool $is_trial
6519
- * @param array $extra (optional) Extra parameters, override other query params.
6520
- *
6521
- * @return string
6522
- */
6523
- function checkout_url(
6524
- $billing_cycle = WP_FS__PERIOD_ANNUALLY,
6525
- $is_trial = false,
6526
- $extra = array()
6527
- ) {
6528
- $this->_logger->entrance();
6529
-
6530
- $params = array(
6531
- 'checkout' => 'true',
6532
- 'billing_cycle' => $billing_cycle,
6533
- );
6534
-
6535
- if ( $is_trial ) {
6536
- $params['trial'] = 'true';
6537
- }
6538
-
6539
- /**
6540
- * Params in extra override other params.
6541
- */
6542
- $params = array_merge( $params, $extra );
6543
-
6544
- return $this->_get_admin_page_url( 'pricing', $params );
6545
- }
6546
-
6547
- /**
6548
- * Add-on checkout URL.
6549
- *
6550
- * @author Vova Feldman (@svovaf)
6551
- * @since 1.1.7
6552
- *
6553
- * @param number $addon_id
6554
- * @param number $pricing_id
6555
- * @param string $billing_cycle
6556
- * @param bool $is_trial
6557
- *
6558
- * @return string
6559
- */
6560
- function addon_checkout_url(
6561
- $addon_id,
6562
- $pricing_id,
6563
- $billing_cycle = WP_FS__PERIOD_ANNUALLY,
6564
- $is_trial = false
6565
- ) {
6566
- return $this->checkout_url( $billing_cycle, $is_trial, array(
6567
- 'plugin_id' => $addon_id,
6568
- 'pricing_id' => $pricing_id,
6569
- ) );
6570
- }
6571
-
6572
- #endregion
6573
-
6574
- #endregion ------------------------------------------------------------------
6575
-
6576
- /**
6577
- * Check if plugin has any add-ons.
6578
- *
6579
- * @author Vova Feldman (@svovaf)
6580
- * @since 1.0.5
6581
- *
6582
- * @since 1.1.7.3 Base logic only on the parameter provided by the developer in the init function.
6583
- *
6584
- * @return bool
6585
- */
6586
- function has_addons() {
6587
- $this->_logger->entrance();
6588
-
6589
- return $this->_has_addons;
6590
- }
6591
-
6592
- /**
6593
- * Check if plugin can work in anonymous mode.
6594
- *
6595
- * @author Vova Feldman (@svovaf)
6596
- * @since 1.0.9
6597
- *
6598
- * @return bool
6599
- *
6600
- * @deprecated Please use is_enable_anonymous() instead
6601
- */
6602
- function enable_anonymous() {
6603
- return $this->_enable_anonymous;
6604
- }
6605
-
6606
- /**
6607
- * Check if plugin can work in anonymous mode.
6608
- *
6609
- * @author Vova Feldman (@svovaf)
6610
- * @since 1.1.9
6611
- *
6612
- * @return bool
6613
- */
6614
- function is_enable_anonymous() {
6615
- return $this->_enable_anonymous;
6616
- }
6617
-
6618
- /**
6619
- * Check if plugin is premium only (no free plans).
6620
- *
6621
- * @author Vova Feldman (@svovaf)
6622
- * @since 1.1.9
6623
- *
6624
- * @return bool
6625
- */
6626
- function is_only_premium() {
6627
- return $this->_is_premium_only;
6628
- }
6629
-
6630
- /**
6631
- * Checks if the plugin's type is "plugin". The other type is "theme".
6632
- *
6633
- * @author Leo Fajardo (@leorw)
6634
- * @since 1.2.2
6635
- *
6636
- * @return bool
6637
- */
6638
- function is_plugin() {
6639
- return true;
6640
- }
6641
-
6642
- /**
6643
- * Check if module has a premium code version.
6644
- *
6645
- * Serviceware module might be freemium without any
6646
- * premium code version, where the paid features
6647
- * are all part of the service.
6648
- *
6649
- * @author Vova Feldman (@svovaf)
6650
- * @since 1.2.1.6
6651
- *
6652
- * @return bool
6653
- */
6654
- function has_premium_version() {
6655
- return $this->_has_premium_version;
6656
- }
6657
-
6658
- /**
6659
- * Check if feature supported with current site's plan.
6660
- *
6661
- * @author Vova Feldman (@svovaf)
6662
- * @since 1.0.1
6663
- *
6664
- * @todo IMPLEMENT
6665
- *
6666
- * @param number $feature_id
6667
- *
6668
- * @throws Exception
6669
- */
6670
- function is_feature_supported( $feature_id ) {
6671
- throw new Exception( 'not implemented' );
6672
- }
6673
-
6674
- /**
6675
- * @author Vova Feldman (@svovaf)
6676
- * @since 1.0.1
6677
- *
6678
- * @return bool Is running in SSL/HTTPS
6679
- */
6680
- function is_ssl() {
6681
- return WP_FS__IS_HTTPS;
6682
- }
6683
-
6684
- /**
6685
- * @author Vova Feldman (@svovaf)
6686
- * @since 1.0.9
6687
- *
6688
- * @return bool Is running in AJAX call.
6689
- *
6690
- * @link http://wordpress.stackexchange.com/questions/70676/how-to-check-if-i-am-in-admin-ajax
6691
- */
6692
- static function is_ajax() {
6693
- return ( defined( 'DOING_AJAX' ) && DOING_AJAX );
6694
- }
6695
-
6696
- /**
6697
- * Check if it's an AJAX call targeted for the current module.
6698
- *
6699
- * @author Vova Feldman (@svovaf)
6700
- * @since 1.2.0
6701
- *
6702
- * @param array|string $actions Collection of AJAX actions.
6703
- *
6704
- * @return bool
6705
- */
6706
- function is_ajax_action( $actions ) {
6707
- // Verify it's an ajax call.
6708
- if ( ! self::is_ajax() ) {
6709
- return false;
6710
- }
6711
-
6712
- // Verify the call is relevant for the plugin.
6713
- if ( $this->_slug !== fs_request_get( 'slug' ) ) {
6714
- return false;
6715
- }
6716
-
6717
- // Verify it's one of the specified actions.
6718
- if ( is_string( $actions ) ) {
6719
- $actions = explode( ',', $actions );
6720
- }
6721
-
6722
- if ( is_array( $actions ) && 0 < count( $actions ) ) {
6723
- $ajax_action = fs_request_get( 'action' );
6724
-
6725
- foreach ( $actions as $action ) {
6726
- if ( $ajax_action === $this->get_action_tag( $action ) ) {
6727
- return true;
6728
- }
6729
- }
6730
- }
6731
-
6732
- return false;
6733
- }
6734
-
6735
- /**
6736
- * Check if it's an AJAX call targeted for current request.
6737
- *
6738
- * @author Vova Feldman (@svovaf)
6739
- * @since 1.2.0
6740
- *
6741
- * @param array|string $actions Collection of AJAX actions.
6742
- * @param string $slug
6743
- *
6744
- * @return bool
6745
- */
6746
- static function is_ajax_action_static( $actions, $slug = '' ) {
6747
- // Verify it's an ajax call.
6748
- if ( ! self::is_ajax() ) {
6749
- return false;
6750
- }
6751
-
6752
- if ( ! empty( $slug ) ) {
6753
- // Verify the call is relevant for the plugin.
6754
- if ( $slug !== fs_request_get( 'slug' ) ) {
6755
- return false;
6756
- }
6757
- }
6758
-
6759
- // Verify it's one of the specified actions.
6760
- if ( is_string( $actions ) ) {
6761
- $actions = explode( ',', $actions );
6762
- }
6763
-
6764
- if ( is_array( $actions ) && 0 < count( $actions ) ) {
6765
- $ajax_action = fs_request_get( 'action' );
6766
-
6767
- foreach ( $actions as $action ) {
6768
- if ( $ajax_action === self::get_ajax_action_static( $action, $slug ) ) {
6769
- return true;
6770
- }
6771
- }
6772
- }
6773
-
6774
- return false;
6775
- }
6776
-
6777
- /**
6778
- * @author Vova Feldman (@svovaf)
6779
- * @since 1.1.7
6780
- *
6781
- * @return bool
6782
- */
6783
- function is_cron() {
6784
- return ( defined( 'DOING_CRON' ) && DOING_CRON );
6785
- }
6786
-
6787
- /**
6788
- * Check if a real user is visiting the admin dashboard.
6789
- *
6790
- * @author Vova Feldman (@svovaf)
6791
- * @since 1.1.7
6792
- *
6793
- * @return bool
6794
- */
6795
- function is_user_in_admin() {
6796
- return is_admin() && ! self::is_ajax() && ! $this->is_cron();
6797
- }
6798
-
6799
- /**
6800
- * Check if running in HTTPS and if site's plan matching the specified plan.
6801
- *
6802
- * @param string $plan
6803
- * @param bool $exact
6804
- *
6805
- * @return bool
6806
- */
6807
- function is_ssl_and_plan( $plan, $exact = false ) {
6808
- return ( $this->is_ssl() && $this->is_plan( $plan, $exact ) );
6809
- }
6810
-
6811
- /**
6812
- * Construct plugin's settings page URL.
6813
- *
6814
- * @author Vova Feldman (@svovaf)
6815
- * @since 1.0.4
6816
- *
6817
- * @param string $page
6818
- * @param array $params
6819
- *
6820
- * @return string
6821
- */
6822
- function _get_admin_page_url( $page = '', $params = array() ) {
6823
- if ( 0 < count( $params ) ) {
6824
- foreach ( $params as $k => $v ) {
6825
- $params[ $k ] = urlencode( $v );
6826
- }
6827
- }
6828
-
6829
- $page_param = $this->_menu->get_slug( $page );
6830
-
6831
- if ( ! $this->has_settings_menu() ) {
6832
- if ( ! empty( $page ) ) {
6833
- // Module doesn't have a setting page, but since the request is for
6834
- // a specific Freemius page, use the admin.php path.
6835
- return add_query_arg( array_merge( $params, array(
6836
- 'page' => $page_param,
6837
- ) ), admin_url( 'admin.php', 'admin' ) );
6838
- } else {
6839
- if ( $this->is_activation_mode() ) {
6840
- /**
6841
- * @author Vova Feldman
6842
- * @since 1.2.1.6
6843
- *
6844
- * If plugin doesn't have a settings page, create one for the opt-in screen.
6845
- */
6846
- return add_query_arg( array_merge( $params, array(
6847
- 'page' => $this->_slug,
6848
- ) ), admin_url( 'admin.php', 'admin' ) );
6849
- } else {
6850
- // Plugin without a settings page.
6851
- return admin_url( 'plugins.php' );
6852
- }
6853
- }
6854
- }
6855
-
6856
- // Module has a submenu settings page.
6857
- if ( ! $this->_menu->is_top_level() ) {
6858
- $parent_slug = $this->_menu->get_parent_slug();
6859
- $menu_file = ( false !== strpos( $parent_slug, '.php' ) ) ?
6860
- $parent_slug :
6861
- 'admin.php';
6862
-
6863
- return add_query_arg( array_merge( $params, array(
6864
- 'page' => $page_param,
6865
- ) ), admin_url( $menu_file, 'admin' ) );
6866
- }
6867
-
6868
- // Module has a top level CPT settings page.
6869
- if ( $this->_menu->is_cpt() ) {
6870
- if ( empty( $page ) && $this->is_activation_mode() ) {
6871
- return add_query_arg( array_merge( $params, array(
6872
- 'page' => $page_param
6873
- ) ), admin_url( 'admin.php', 'admin' ) );
6874
- } else {
6875
- if ( ! empty( $page ) ) {
6876
- $params['page'] = $page_param;
6877
- }
6878
-
6879
- return add_query_arg(
6880
- $params,
6881
- admin_url( $this->_menu->get_raw_slug(), 'admin' )
6882
- );
6883
- }
6884
- }
6885
-
6886
- // Module has a custom top level settings page.
6887
- return add_query_arg( array_merge( $params, array(
6888
- 'page' => $page_param,
6889
- ) ), admin_url( 'admin.php', 'admin' ) );
6890
- }
6891
-
6892
- /**
6893
- * Plugin's account page + sync license URL.
6894
- *
6895
- * @author Vova Feldman (@svovaf)
6896
- * @since 1.1.9.1
6897
- *
6898
- * @param bool|number $plugin_id
6899
- * @param bool $add_action_nonce
6900
- * @param array $params
6901
- *
6902
- * @return string
6903
- */
6904
- function _get_sync_license_url( $plugin_id = false, $add_action_nonce = true, $params = array() ) {
6905
- if ( is_numeric( $plugin_id ) ) {
6906
- $params['plugin_id'] = $plugin_id;
6907
- }
6908
-
6909
- return $this->get_account_url(
6910
- $this->_slug . '_sync_license',
6911
- $params,
6912
- $add_action_nonce
6913
- );
6914
- }
6915
-
6916
- /**
6917
- * Plugin's account URL.
6918
- *
6919
- * @author Vova Feldman (@svovaf)
6920
- * @since 1.0.4
6921
- *
6922
- * @param bool|string $action
6923
- * @param array $params
6924
- *
6925
- * @param bool $add_action_nonce
6926
- *
6927
- * @return string
6928
- */
6929
- function get_account_url( $action = false, $params = array(), $add_action_nonce = true ) {
6930
- if ( is_string( $action ) ) {
6931
- $params['fs_action'] = $action;
6932
- }
6933
-
6934
- self::require_pluggable_essentials();
6935
-
6936
- return ( $add_action_nonce && is_string( $action ) ) ?
6937
- fs_nonce_url( $this->_get_admin_page_url( 'account', $params ), $action ) :
6938
- $this->_get_admin_page_url( 'account', $params );
6939
- }
6940
-
6941
- /**
6942
- * @author Vova Feldman (@svovaf)
6943
- * @since 1.2.0
6944
- *
6945
- * @param string $tab
6946
- * @param bool $action
6947
- * @param array $params
6948
- * @param bool $add_action_nonce
6949
- *
6950
- * @return string
6951
- *
6952
- * @uses get_account_url()
6953
- */
6954
- function get_account_tab_url( $tab, $action = false, $params = array(), $add_action_nonce = true ) {
6955
- $params['tab'] = $tab;
6956
-
6957
- return $this->get_account_url( $action, $params, $add_action_nonce );
6958
- }
6959
-
6960
- /**
6961
- * Plugin's account URL.
6962
- *
6963
- * @author Vova Feldman (@svovaf)
6964
- * @since 1.0.4
6965
- *
6966
- * @param bool|string $topic
6967
- * @param bool|string $message
6968
- *
6969
- * @return string
6970
- */
6971
- function contact_url( $topic = false, $message = false ) {
6972
- $params = array();
6973
- if ( is_string( $topic ) ) {
6974
- $params['topic'] = $topic;
6975
- }
6976
- if ( is_string( $message ) ) {
6977
- $params['message'] = $message;
6978
- }
6979
-
6980
- if ( $this->is_addon() ) {
6981
- $params['addon_id'] = $this->get_id();
6982
-
6983
- return $this->get_parent_instance()->_get_admin_page_url( 'contact', $params );
6984
- } else {
6985
- return $this->_get_admin_page_url( 'contact', $params );
6986
- }
6987
- }
6988
-
6989
- /**
6990
- * Add-on direct info URL.
6991
- *
6992
- * @author Vova Feldman (@svovaf)
6993
- * @since 1.1.0
6994
- *
6995
- * @param string $slug
6996
- *
6997
- * @return string
6998
- */
6999
- function addon_url( $slug ) {
7000
- return $this->_get_admin_page_url( 'addons', array(
7001
- 'slug' => $slug
7002
- ) );
7003
- }
7004
-
7005
- /* Logger
7006
- ------------------------------------------------------------------------------------------------------------------*/
7007
- /**
7008
- * @param string $id
7009
- * @param bool $prefix_slug
7010
- *
7011
- * @return FS_Logger
7012
- */
7013
- function get_logger( $id = '', $prefix_slug = true ) {
7014
- return FS_Logger::get_logger( ( $prefix_slug ? $this->_slug : '' ) . ( ( ! $prefix_slug || empty( $id ) ) ? '' : '_' ) . $id );
7015
- }
7016
-
7017
- /**
7018
- * @param $id
7019
- * @param bool $load_options
7020
- * @param bool $prefix_slug
7021
- *
7022
- * @return FS_Option_Manager
7023
- */
7024
- function get_options_manager( $id, $load_options = false, $prefix_slug = true ) {
7025
- return FS_Option_Manager::get_manager( ( $prefix_slug ? $this->_slug : '' ) . ( ( ! $prefix_slug || empty( $id ) ) ? '' : '_' ) . $id, $load_options );
7026
- }
7027
-
7028
- /* Security
7029
- ------------------------------------------------------------------------------------------------------------------*/
7030
- private static function _encrypt( $str ) {
7031
- if ( is_null( $str ) ) {
7032
- return null;
7033
- }
7034
-
7035
- /**
7036
- * The encrypt/decrypt functions are used to protect
7037
- * the user from messing up with some of the sensitive
7038
- * data stored for the module as a JSON in the database.
7039
- *
7040
- * I used the same suggested hack by the theme review team.
7041
- * For more details, look at the function `Base64UrlDecode()`
7042
- * in `./sdk/FreemiusBase.php`.
7043
- *
7044
- * @todo Remove this hack once the base64 error is removed from the Theme Check.
7045
- *
7046
- * @author Vova Feldman (@svovaf)
7047
- * @since 1.2.2
7048
- */
7049
- $fn = 'base64' . '_encode';
7050
-
7051
- return $fn( $str );
7052
- }
7053
-
7054
- static function _decrypt( $str ) {
7055
- if ( is_null( $str ) ) {
7056
- return null;
7057
- }
7058
-
7059
- /**
7060
- * The encrypt/decrypt functions are used to protect
7061
- * the user from messing up with some of the sensitive
7062
- * data stored for the module as a JSON in the database.
7063
- *
7064
- * I used the same suggested hack by the theme review team.
7065
- * For more details, look at the function `Base64UrlDecode()`
7066
- * in `./sdk/FreemiusBase.php`.
7067
- *
7068
- * @todo Remove this hack once the base64 error is removed from the Theme Check.
7069
- *
7070
- * @author Vova Feldman (@svovaf)
7071
- * @since 1.2.2
7072
- */
7073
- $fn = 'base64' . '_decode';
7074
-
7075
- return $fn( $str );
7076
- }
7077
-
7078
- /**
7079
- * @author Vova Feldman (@svovaf)
7080
- * @since 1.0.5
7081
- *
7082
- * @param FS_Entity $entity
7083
- *
7084
- * @return FS_Entity Return an encrypted clone entity.
7085
- */
7086
- private static function _encrypt_entity( FS_Entity $entity ) {
7087
- $clone = clone $entity;
7088
- $props = get_object_vars( $entity );
7089
-
7090
- foreach ( $props as $key => $val ) {
7091
- $clone->{$key} = self::_encrypt( $val );
7092
- }
7093
-
7094
- return $clone;
7095
- }
7096
-
7097
- /**
7098
- * @author Vova Feldman (@svovaf)
7099
- * @since 1.0.5
7100
- *
7101
- * @param FS_Entity $entity
7102
- *
7103
- * @return FS_Entity Return an decrypted clone entity.
7104
- */
7105
- private static function _decrypt_entity( FS_Entity $entity ) {
7106
- $clone = clone $entity;
7107
- $props = get_object_vars( $entity );
7108
-
7109
- foreach ( $props as $key => $val ) {
7110
- $clone->{$key} = self::_decrypt( $val );
7111
- }
7112
-
7113
- return $clone;
7114
- }
7115
-
7116
- /**
7117
- * Tries to activate account based on POST params.
7118
- *
7119
- * @author Vova Feldman (@svovaf)
7120
- * @since 1.0.2
7121
- */
7122
- function _activate_account() {
7123
- if ( $this->is_registered() ) {
7124
- // Already activated.
7125
- return;
7126
- }
7127
-
7128
- self::_clean_admin_content_section();
7129
-
7130
- if ( fs_request_is_action( 'activate' ) && fs_request_is_post() ) {
7131
- // check_admin_referer( 'activate_' . $this->_plugin->public_key );
7132
-
7133
- // Verify matching plugin details.
7134
- if ( $this->_plugin->id != fs_request_get( 'plugin_id' ) || $this->_slug != fs_request_get( 'plugin_slug' ) ) {
7135
- return;
7136
- }
7137
-
7138
- $user = new FS_User();
7139
- $user->id = fs_request_get( 'user_id' );
7140
- $user->public_key = fs_request_get( 'user_public_key' );
7141
- $user->secret_key = fs_request_get( 'user_secret_key' );
7142
- $user->email = fs_request_get( 'user_email' );
7143
- $user->first = fs_request_get( 'user_first' );
7144
- $user->last = fs_request_get( 'user_last' );
7145
- $user->is_verified = fs_request_get_bool( 'user_is_verified' );
7146
-
7147
- $site = new FS_Site();
7148
- $site->id = fs_request_get( 'install_id' );
7149
- $site->public_key = fs_request_get( 'install_public_key' );
7150
- $site->secret_key = fs_request_get( 'install_secret_key' );
7151
- $site->plan->id = fs_request_get( 'plan_id' );
7152
- $site->plan->title = fs_request_get( 'plan_title' );
7153
- $site->plan->name = fs_request_get( 'plan_name' );
7154
-
7155
- $plans = array();
7156
- $plans_data = json_decode( urldecode( fs_request_get( 'plans' ) ) );
7157
- foreach ( $plans_data as $p ) {
7158
- $plans[] = new FS_Plugin_Plan( $p );
7159
- }
7160
-
7161
- $this->_set_account( $user, $site, $plans );
7162
-
7163
- // Reload the page with the keys.
7164
- fs_redirect( $this->_get_admin_page_url() );
7165
- }
7166
- }
7167
-
7168
- /**
7169
- * @author Vova Feldman (@svovaf)
7170
- * @since 1.0.7
7171
- *
7172
- * @param string $email
7173
- *
7174
- * @return FS_User|bool
7175
- */
7176
- static function _get_user_by_email( $email ) {
7177
- self::$_static_logger->entrance();
7178
-
7179
- $email = trim( strtolower( $email ) );
7180
- $users = self::get_all_users();
7181
- if ( is_array( $users ) ) {
7182
- foreach ( $users as $u ) {
7183
- if ( $email === trim( strtolower( $u->email ) ) ) {
7184
- return $u;
7185
- }
7186
- }
7187
- }
7188
-
7189
- return false;
7190
- }
7191
-
7192
- #----------------------------------------------------------------------------------
7193
- #region Account (Loading, Updates & Activation)
7194
- #----------------------------------------------------------------------------------
7195
-
7196
- /***
7197
- * Load account information (user + site).
7198
- *
7199
- * @author Vova Feldman (@svovaf)
7200
- * @since 1.0.1
7201
- */
7202
- private function _load_account() {
7203
- $this->_logger->entrance();
7204
-
7205
- $this->do_action( 'before_account_load' );
7206
-
7207
- $sites = self::get_all_sites();
7208
- $users = self::get_all_users();
7209
- $plans = self::get_all_plans();
7210
- $licenses = self::get_all_licenses();
7211
-
7212
- if ( $this->_logger->is_on() && is_admin() ) {
7213
- $this->_logger->log( 'sites = ' . var_export( $sites, true ) );
7214
- $this->_logger->log( 'users = ' . var_export( $users, true ) );
7215
- $this->_logger->log( 'plans = ' . var_export( $plans, true ) );
7216
- $this->_logger->log( 'licenses = ' . var_export( $licenses, true ) );
7217
- }
7218
-
7219
- $site = isset( $sites[ $this->_slug ] ) ? $sites[ $this->_slug ] : false;
7220
-
7221
- if ( is_object( $site ) &&
7222
- is_numeric( $site->id ) &&
7223
- is_numeric( $site->user_id ) &&
7224
- is_object( $site->plan )
7225
- ) {
7226
- // Load site.
7227
- $this->_site = clone $site;
7228
- $this->_site->plan = self::_decrypt_entity( $this->_site->plan );
7229
-
7230
- // Load relevant user.
7231
- $this->_user = clone $users[ $this->_site->user_id ];
7232
-
7233
- // Load plans.
7234
- $this->_plans = $plans[ $this->_slug ];
7235
- if ( ! is_array( $this->_plans ) || empty( $this->_plans ) ) {
7236
- $this->_sync_plans();
7237
- } else {
7238
- for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
7239
- if ( $this->_plans[ $i ] instanceof FS_Plugin_Plan ) {
7240
- $this->_plans[ $i ] = self::_decrypt_entity( $this->_plans[ $i ] );
7241
- } else {
7242
- unset( $this->_plans[ $i ] );
7243
- }
7244
- }
7245
- }
7246
-
7247
- // Load licenses.
7248
- $this->_licenses = array();
7249
- if ( is_array( $licenses ) &&
7250
- isset( $licenses[ $this->_slug ] ) &&
7251
- isset( $licenses[ $this->_slug ][ $this->_user->id ] )
7252
- ) {
7253
- $this->_licenses = $licenses[ $this->_slug ][ $this->_user->id ];
7254
- }
7255
-
7256
- $this->_license = $this->_get_license_by_id( $this->_site->license_id );
7257
-
7258
- if ( $this->_site->version != $this->get_plugin_version() ) {
7259
- // If stored install version is different than current installed plugin version,
7260
- // then update plugin version event.
7261
- $this->update_plugin_version_event();
7262
- }
7263
- }
7264
-
7265
- $this->_register_account_hooks();
7266
- }
7267
-
7268
- /**
7269
- * @author Vova Feldman (@svovaf)
7270
- * @since 1.0.1
7271
- *
7272
- * @param FS_User $user
7273
- * @param FS_Site $site
7274
- * @param bool|array $plans
7275
- */
7276
- private function _set_account( FS_User $user, FS_Site $site, $plans = false ) {
7277
- $site->slug = $this->_slug;
7278
- $site->user_id = $user->id;
7279
-
7280
- $this->_site = $site;
7281
- $this->_user = $user;
7282
- if ( false !== $plans ) {
7283
- $this->_plans = $plans;
7284
- }
7285
-
7286
- $this->send_install_update();
7287
-
7288
- $this->_store_account();
7289
-
7290
- }
7291
-
7292
- /**
7293
- * @author Vova Feldman (@svovaf)
7294
- * @since 1.1.7.4
7295
- *
7296
- * @param array $override_with
7297
- *
7298
- * @return array
7299
- */
7300
- function get_opt_in_params( $override_with = array() ) {
7301
- $this->_logger->entrance();
7302
-
7303
- $current_user = self::_get_current_wp_user();
7304
-
7305
- $activation_action = $this->_slug . '_activate_new';
7306
- $return_url = $this->is_anonymous() ?
7307
- // If skipped already, then return to the account page.
7308
- $this->get_account_url( $activation_action, array(), false ) :
7309
- // Return to the module's main page.
7310
- $this->_get_admin_page_url(
7311
- '',
7312
- array( 'fs_action' => $activation_action )
7313
- );
7314
-
7315
- $params = array(
7316
- 'user_firstname' => $current_user->user_firstname,
7317
- 'user_lastname' => $current_user->user_lastname,
7318
- 'user_nickname' => $current_user->user_nicename,
7319
- 'user_email' => $current_user->user_email,
7320
- 'user_ip' => WP_FS__REMOTE_ADDR,
7321
- 'plugin_slug' => $this->_slug,
7322
- 'plugin_id' => $this->get_id(),
7323
- 'plugin_public_key' => $this->get_public_key(),
7324
- 'plugin_version' => $this->get_plugin_version(),
7325
- 'return_url' => fs_nonce_url( $return_url, $activation_action ),
7326
- 'account_url' => fs_nonce_url( $this->_get_admin_page_url(
7327
- 'account',
7328
- array( 'fs_action' => 'sync_user' )
7329
- ), 'sync_user' ),
7330
- 'site_uid' => $this->get_anonymous_id(),
7331
- 'site_url' => get_site_url(),
7332
- 'site_name' => get_bloginfo( 'name' ),
7333
- 'platform_version' => get_bloginfo( 'version' ),
7334
- 'sdk_version' => $this->version,
7335
- 'programming_language_version' => phpversion(),
7336
- 'language' => get_bloginfo( 'language' ),
7337
- 'charset' => get_bloginfo( 'charset' ),
7338
- 'is_premium' => $this->is_premium(),
7339
- 'is_active' => true,
7340
- 'is_uninstalled' => false,
7341
- );
7342
-
7343
- if ( $this->is_pending_activation() &&
7344
- ! empty( $this->_storage->pending_license_key )
7345
- ) {
7346
- $params['license_key'] = $this->_storage->pending_license_key;
7347
- }
7348
-
7349
- if ( WP_FS__SKIP_EMAIL_ACTIVATION && $this->has_secret_key() ) {
7350
- // Even though rand() is known for its security issues,
7351
- // the timestamp adds another layer of protection.
7352
- // It would be very hard for an attacker to get the secret key form here.
7353
- // Plus, this should never run in production since the secret should never
7354
- // be included in the production version.
7355
- $params['ts'] = WP_FS__SCRIPT_START_TIME;
7356
- $params['salt'] = md5( uniqid( rand() ) );
7357
- $params['secure'] = md5(
7358
- $params['ts'] .
7359
- $params['salt'] .
7360
- $this->get_secret_key()
7361
- );
7362
- }
7363
-
7364
- return array_merge( $params, $override_with );
7365
- }
7366
-
7367
- /**
7368
- * 1. If successful opt-in or pending activation returns the next page that the user should be redirected to.
7369
- * 2. If there was an API error, return the API result.
7370
- *
7371
- * @author Vova Feldman (@svovaf)
7372
- * @since 1.1.7.4
7373
- *
7374
- * @param string|bool $email
7375
- * @param string|bool $first
7376
- * @param string|bool $last
7377
- * @param string|bool $license_key
7378
- * @param bool $is_uninstall If "true", this means that the module is currently being uninstalled.
7379
- * In this case, the user and site info will be sent to the server but no
7380
- * data will be saved to the WP installation's database.
7381
- * @param number|bool $trial_plan_id
7382
- *
7383
- * @return string|object
7384
- * @use WP_Error
7385
- */
7386
- function opt_in(
7387
- $email = false,
7388
- $first = false,
7389
- $last = false,
7390
- $license_key = false,
7391
- $is_uninstall = false,
7392
- $trial_plan_id = false
7393
- ) {
7394
- $this->_logger->entrance();
7395
-
7396
- if ( false === $email ) {
7397
- $current_user = self::_get_current_wp_user();
7398
- $email = $current_user->user_email;
7399
- }
7400
-
7401
- /**
7402
- * @since 1.2.1 If activating with license key, ignore the context-user
7403
- * since the user will be automatically loaded from the license.
7404
- */
7405
- if ( empty( $license_key ) ) {
7406
- // Clean up pending license if opt-ing in again.
7407
- $this->_storage->remove( 'pending_license_key' );
7408
-
7409
- if ( ! $is_uninstall ) {
7410
- $fs_user = Freemius::_get_user_by_email( $email );
7411
- if ( is_object( $fs_user ) && ! $this->is_pending_activation() ) {
7412
- return $this->install_with_current_user( false, $trial_plan_id );
7413
- }
7414
- }
7415
- }
7416
-
7417
- $user_info = array();
7418
- if ( ! empty( $email ) ) {
7419
- $user_info['user_email'] = $email;
7420
- }
7421
- if ( ! empty( $first ) ) {
7422
- $user_info['user_firstname'] = $first;
7423
- }
7424
- if ( ! empty( $last ) ) {
7425
- $user_info['user_lastname'] = $last;
7426
- }
7427
-
7428
- $params = $this->get_opt_in_params( $user_info );
7429
-
7430
- $filtered_license_key = false;
7431
- if ( is_string( $license_key ) ) {
7432
- $filtered_license_key = $this->apply_filters( 'license_key', $license_key );
7433
- $params['license_key'] = $filtered_license_key;
7434
- } else if ( FS_Plugin_Plan::is_valid_id( $trial_plan_id ) ) {
7435
- $params['trial_plan_id'] = $trial_plan_id;
7436
- }
7437
-
7438
- if ( $is_uninstall ) {
7439
- $params['uninstall_params'] = array(
7440
- 'reason_id' => $this->_storage->uninstall_reason->id,
7441
- 'reason_info' => $this->_storage->uninstall_reason->info
7442
- );
7443
- }
7444
-
7445
- $params['format'] = 'json';
7446
-
7447
- $url = WP_FS__ADDRESS . '/action/service/user/install/';
7448
- if ( isset( $_COOKIE['XDEBUG_SESSION'] ) ) {
7449
- $url = add_query_arg( 'XDEBUG_SESSION', 'PHPSTORM', $url );
7450
- }
7451
-
7452
- $response = wp_remote_post( $url, array(
7453
- 'method' => 'POST',
7454
- 'body' => $params,
7455
- 'timeout' => 15,
7456
- ) );
7457
-
7458
- if ( $response instanceof WP_Error ) {
7459
- if ( 'https://' === substr( $url, 0, 8 ) &&
7460
- isset( $response->errors ) &&
7461
- isset( $response->errors['http_request_failed'] )
7462
- ) {
7463
- $http_error = strtolower( $response->errors['http_request_failed'][0] );
7464
-
7465
- if ( false !== strpos( $http_error, 'ssl' ) ) {
7466
- // Failed due to old version of cURL or Open SSL (SSLv3 is not supported by CloudFlare).
7467
- $url = 'http://' . substr( $url, 8 );
7468
-
7469
- $response = wp_remote_post( $url, array(
7470
- 'method' => 'POST',
7471
- 'body' => $params,
7472
- 'timeout' => 15,
7473
- ) );
7474
- }
7475
- }
7476
- }
7477
-
7478
- if ( is_wp_error( $response ) ) {
7479
- /**
7480
- * @var WP_Error $response
7481
- */
7482
- $result = new stdClass();
7483
-
7484
- $error_code = $response->get_error_code();
7485
- $error_type = str_replace( ' ', '', ucwords( str_replace( '_', ' ', $error_code ) ) );
7486
-
7487
- $result->error = (object) array(
7488
- 'type' => $error_type,
7489
- 'message' => $response->get_error_message(),
7490
- 'code' => $error_code,
7491
- 'http' => 402
7492
- );
7493
-
7494
- return $result;
7495
- }
7496
-
7497
- // Module is being uninstalled, don't handle the returned data.
7498
- if ( $is_uninstall ) {
7499
- return true;
7500
- }
7501
-
7502
- $decoded = @json_decode( $response['body'] );
7503
-
7504
- if ( empty( $decoded ) ) {
7505
- return false;
7506
- }
7507
-
7508
- if ( ! $this->is_api_result_object( $decoded ) ) {
7509
- if ( ! empty( $params['license_key'] ) ) {
7510
- // Pass the fully entered license key to the failure handler.
7511
- $params['license_key'] = $license_key;
7512
- }
7513
-
7514
- return $is_uninstall ?
7515
- $decoded :
7516
- $this->apply_filters( 'after_install_failure', $decoded, $params );
7517
- } else if ( isset( $decoded->pending_activation ) && $decoded->pending_activation ) {
7518
- // Pending activation, add message.
7519
- return $this->set_pending_confirmation(
7520
- true,
7521
- false,
7522
- $filtered_license_key,
7523
- ! empty( $params['trial_plan_id'] )
7524
- );
7525
- } else if ( isset( $decoded->install_secret_key ) ) {
7526
- return $this->install_with_new_user(
7527
- $decoded->user_id,
7528
- $decoded->user_public_key,
7529
- $decoded->user_secret_key,
7530
- $decoded->install_id,
7531
- $decoded->install_public_key,
7532
- $decoded->install_secret_key,
7533
- false
7534
- );
7535
- }
7536
-
7537
- return $decoded;
7538
- }
7539
-
7540
- /**
7541
- * Set user and site identities.
7542
- *
7543
- * @author Vova Feldman (@svovaf)
7544
- * @since 1.0.9
7545
- *
7546
- * @param FS_User $user
7547
- * @param FS_Site $site
7548
- * @param bool $redirect
7549
- * @param bool $auto_install Since 1.2.1.7 If `true` and setting up an account with a valid license, will
7550
- * redirect (or return a URL) to the account page with a special parameter to
7551
- * trigger the auto installation processes.
7552
- *
7553
- * @return string If redirect is `false`, returns the next page the user should be redirected to.
7554
- */
7555
- function setup_account(
7556
- FS_User $user,
7557
- FS_Site $site,
7558
- $redirect = true,
7559
- $auto_install = false
7560
- ) {
7561
- $this->_user = $user;
7562
- $this->_site = $site;
7563
-
7564
- $this->_sync_plans();
7565
-
7566
- $this->_enrich_site_plan( false );
7567
-
7568
- $this->_set_account( $user, $site );
7569
-
7570
- if ( $this->is_trial() ) {
7571
- // Store trial plan information.
7572
- $this->_enrich_site_trial_plan( true );
7573
- }
7574
-
7575
- // If Freemius was OFF before, turn it on.
7576
- $this->turn_on();
7577
-
7578
- $this->do_action( 'after_account_connection', $user, $site );
7579
-
7580
- if ( is_numeric( $site->license_id ) ) {
7581
- $this->_license = $this->_get_license_by_id( $site->license_id );
7582
- }
7583
-
7584
- $this->_admin_notices->remove_sticky( 'connect_account' );
7585
-
7586
- if ( $this->is_pending_activation() ) {
7587
- // Remove pending activation sticky notice (if still exist).
7588
- $this->_admin_notices->remove_sticky( 'activation_pending' );
7589
-
7590
- // Remove plugin from pending activation mode.
7591
- unset( $this->_storage->is_pending_activation );
7592
-
7593
- if ( ! $this->is_paying_or_trial() ) {
7594
- $this->_admin_notices->add_sticky(
7595
- sprintf( $this->get_text( 'plugin-x-activation-message' ), '<b>' . $this->get_plugin_name() . '</b>' ),
7596
- 'activation_complete'
7597
- );
7598
- }
7599
- }
7600
-
7601
- if ( $this->is_paying_or_trial() ) {
7602
- if ( ! $this->is_premium() || ! $this->has_premium_version() ) {
7603
- if ( $this->is_paying() ) {
7604
- $this->_admin_notices->add_sticky(
7605
- sprintf(
7606
- $this->get_text( 'activation-with-plan-x-message' ),
7607
- $this->_site->plan->title
7608
- ) . $this->get_complete_upgrade_instructions(),
7609
- 'plan_upgraded',
7610
- $this->get_text( 'yee-haw' ) . '!'
7611
- );
7612
- } else {
7613
- $this->_admin_notices->add_sticky(
7614
- sprintf(
7615
- $this->get_text( 'trial-started-message' ),
7616
- '<i>' . $this->get_plugin_name() . '</i>'
7617
- ) . $this->get_complete_upgrade_instructions( $this->_storage->trial_plan->title ),
7618
- 'trial_started',
7619
- $this->get_text( 'yee-haw' ) . '!'
7620
- );
7621
- }
7622
- }
7623
-
7624
- $this->_admin_notices->remove_sticky( array(
7625
- 'trial_promotion',
7626
- ) );
7627
- }
7628
-
7629
- $plugin_id = fs_request_get( 'plugin_id', false );
7630
-
7631
- // Store activation time ONLY for plugins (not add-ons).
7632
- if ( ! is_numeric( $plugin_id ) || ( $plugin_id == $this->_plugin->id ) ) {
7633
- $this->_storage->activation_timestamp = WP_FS__SCRIPT_START_TIME;
7634
- }
7635
-
7636
- $next_page = '';
7637
-
7638
- $extra = array();
7639
- if ( $auto_install ) {
7640
- $extra['auto_install'] = 'true';
7641
- }
7642
-
7643
- if ( is_numeric( $plugin_id ) ) {
7644
- /**
7645
- * @author Leo Fajardo
7646
- * @since 1.2.1.6
7647
- *
7648
- * Also sync the license after an anonymous user subscribes.
7649
- */
7650
- if ( $this->is_anonymous() || $plugin_id != $this->_plugin->id ) {
7651
- // Add-on was installed - sync license right after install.
7652
- $next_page = $this->_get_sync_license_url( $plugin_id, true, $extra );
7653
- }
7654
- } else {
7655
- /**
7656
- * @author Vova Feldman (@svovaf)
7657
- * @since 1.1.9 If site installed with a valid license, sync license.
7658
- */
7659
- if ( $this->is_paying() ) {
7660
- $this->_sync_plugin_license( true );
7661
- }
7662
-
7663
- // Reload the page with the keys.
7664
- $next_page = $this->is_anonymous() ?
7665
- // If user previously skipped, redirect to account page.
7666
- $this->get_account_url( false, $extra ) :
7667
- $this->get_after_activation_url( 'after_connect_url' );
7668
- }
7669
-
7670
- if ( ! empty( $next_page ) && $redirect ) {
7671
- fs_redirect( $next_page );
7672
- }
7673
-
7674
- return $next_page;
7675
- }
7676
-
7677
- /**
7678
- * Install plugin with new user information after approval.
7679
- *
7680
- * @author Vova Feldman (@svovaf)
7681
- * @since 1.0.7
7682
- */
7683
- function _install_with_new_user() {
7684
- $this->_logger->entrance();
7685
-
7686
- if ( $this->is_registered() ) {
7687
- return;
7688
- }
7689
-
7690
- if ( fs_request_is_action( $this->_slug . '_activate_new' ) ) {
7691
- // check_admin_referer( $this->_slug . '_activate_new' );
7692
-
7693
- if ( fs_request_has( 'user_secret_key' ) ) {
7694
- $this->install_with_new_user(
7695
- fs_request_get( 'user_id' ),
7696
- fs_request_get( 'user_public_key' ),
7697
- fs_request_get( 'user_secret_key' ),
7698
- fs_request_get( 'install_id' ),
7699
- fs_request_get( 'install_public_key' ),
7700
- fs_request_get( 'install_secret_key' ),
7701
- true,
7702
- fs_request_get_bool( 'auto_install' )
7703
- );
7704
- } else if ( fs_request_has( 'pending_activation' ) ) {
7705
- $this->set_pending_confirmation( fs_request_get( 'user_email' ), true );
7706
- }
7707
- }
7708
- }
7709
-
7710
- /**
7711
- * Install plugin with new user.
7712
- *
7713
- * @author Vova Feldman (@svovaf)
7714
- * @since 1.1.7.4
7715
- *
7716
- * @param number $user_id
7717
- * @param string $user_public_key
7718
- * @param string $user_secret_key
7719
- * @param number $install_id
7720
- * @param string $install_public_key
7721
- * @param string $install_secret_key
7722
- * @param bool $redirect
7723
- * @param bool $auto_install Since 1.2.1.7 If `true` and setting up an account with a valid license, will
7724
- * redirect (or return a URL) to the account page with a special parameter to
7725
- * trigger the auto installation processes.
7726
- *
7727
- * @return string If redirect is `false`, returns the next page the user should be redirected to.
7728
- */
7729
- private function install_with_new_user(
7730
- $user_id,
7731
- $user_public_key,
7732
- $user_secret_key,
7733
- $install_id,
7734
- $install_public_key,
7735
- $install_secret_key,
7736
- $redirect = true,
7737
- $auto_install = false
7738
- ) {
7739
- $user = new FS_User();
7740
- $user->id = $user_id;
7741
- $user->public_key = $user_public_key;
7742
- $user->secret_key = $user_secret_key;
7743
-
7744
- $this->_user = $user;
7745
- $user_result = $this->get_api_user_scope()->get();
7746
- $user = new FS_User( $user_result );
7747
- $this->_user = $user;
7748
-
7749
- $site = new FS_Site();
7750
- $site->id = $install_id;
7751
- $site->public_key = $install_public_key;
7752
- $site->secret_key = $install_secret_key;
7753
-
7754
- $this->_site = $site;
7755
- $site_result = $this->get_api_site_scope()->get();
7756
- $site = new FS_Site( $site_result );
7757
- $this->_site = $site;
7758
-
7759
- return $this->setup_account(
7760
- $this->_user,
7761
- $this->_site,
7762
- $redirect,
7763
- $auto_install
7764
- );
7765
- }
7766
-
7767
- /**
7768
- * @author Vova Feldman (@svovaf)
7769
- * @since 1.1.7.4
7770
- *
7771
- * @param string|bool $email
7772
- * @param bool $redirect
7773
- * @param string|bool $license_key Since 1.2.1.5
7774
- * @param bool $is_pending_trial Since 1.2.1.5
7775
- *
7776
- * @return string Since 1.2.1.5 if $redirect is `false`, return the pending activation page.
7777
- */
7778
- private function set_pending_confirmation(
7779
- $email = false,
7780
- $redirect = true,
7781
- $license_key = false,
7782
- $is_pending_trial = false
7783
- ) {
7784
- if ( $this->_ignore_pending_mode ) {
7785
- /**
7786
- * If explicitly asked to ignore pending mode, set to anonymous mode
7787
- * if require confirmation before finalizing the opt-in.
7788
- *
7789
- * @author Vova Feldman
7790
- * @since 1.2.1.6
7791
- */
7792
- $this->skip_connection();
7793
- } else {
7794
- // Install must be activated via email since
7795
- // user with the same email already exist.
7796
- $this->_storage->is_pending_activation = true;
7797
- $this->_add_pending_activation_notice( $email, $is_pending_trial );
7798
- }
7799
-
7800
- if ( ! empty( $license_key ) ) {
7801
- $this->_storage->pending_license_key = $license_key;
7802
- }
7803
-
7804
- // Remove the opt-in sticky notice.
7805
- $this->_admin_notices->remove_sticky( array(
7806
- 'connect_account',
7807
- 'trial_promotion',
7808
- ) );
7809
-
7810
- $next_page = $this->get_after_activation_url( 'after_pending_connect_url' );
7811
-
7812
- // Reload the page with with pending activation message.
7813
- if ( $redirect ) {
7814
- fs_redirect( $next_page );
7815
- }
7816
-
7817
- return $next_page;
7818
- }
7819
-
7820
- /**
7821
- * Install plugin with current logged WP user info.
7822
- *
7823
- * @author Vova Feldman (@svovaf)
7824
- * @since 1.0.7
7825
- */
7826
- function _install_with_current_user() {
7827
- $this->_logger->entrance();
7828
-
7829
- if ( $this->is_registered() ) {
7830
- return;
7831
- }
7832
-
7833
- if ( fs_request_is_action( $this->_slug . '_activate_existing' ) && fs_request_is_post() ) {
7834
- // check_admin_referer( 'activate_existing_' . $this->_plugin->public_key );
7835
-
7836
- /**
7837
- * @author Vova Feldman (@svovaf)
7838
- * @since 1.1.9 Add license key if given.
7839
- */
7840
- $license_key = fs_request_get( 'license_secret_key' );
7841
-
7842
- $this->install_with_current_user( $license_key );
7843
- }
7844
- }
7845
-
7846
-
7847
- /**
7848
- * @author Vova Feldman (@svovaf)
7849
- * @since 1.1.7.4
7850
- *
7851
- * @param string|bool $license_key
7852
- * @param number|bool $trial_plan_id
7853
- * @param bool $redirect
7854
- *
7855
- * @return string|object If redirect is `false`, returns the next page the user should be redirected to, or the
7856
- * API error object if failed to install.
7857
- */
7858
- private function install_with_current_user(
7859
- $license_key = false,
7860
- $trial_plan_id = false,
7861
- $redirect = true
7862
- ) {
7863
- // Get current logged WP user.
7864
- $current_user = self::_get_current_wp_user();
7865
-
7866
- // Find the relevant FS user by the email.
7867
- $user = self::_get_user_by_email( $current_user->user_email );
7868
-
7869
- // We have to set the user before getting user scope API handler.
7870
- $this->_user = $user;
7871
-
7872
- $extra_install_params = array(
7873
- 'uid' => $this->get_anonymous_id(),
7874
- );
7875
-
7876
- if ( ! empty( $license_key ) ) {
7877
- $filtered_license_key = $this->apply_filters( 'license_key', $license_key );
7878
- $extra_install_params['license_key'] = $filtered_license_key;
7879
- } else if ( FS_Plugin_Plan::is_valid_id( $trial_plan_id ) ) {
7880
- $extra_install_params['trial_plan_id'] = $trial_plan_id;
7881
- }
7882
-
7883
- $args = $this->get_install_data_for_api( $extra_install_params, false, false );
7884
-
7885
- // Install the plugin.
7886
- $install = $this->get_api_user_scope()->call(
7887
- "/plugins/{$this->get_id()}/installs.json",
7888
- 'post',
7889
- $args
7890
- );
7891
-
7892
- if ( ! $this->is_api_result_entity( $install ) ) {
7893
- if ( ! empty( $args['license_key'] ) ) {
7894
- // Pass full the fully entered license key to the failure handler.
7895
- $args['license_key'] = $license_key;
7896
- }
7897
-
7898
- $install = $this->apply_filters( 'after_install_failure', $install, $args );
7899
-
7900
- $this->_admin_notices->add(
7901
- sprintf( $this->get_text( 'could-not-activate-x' ), $this->get_plugin_name() ) . ' ' .
7902
- $this->get_text( 'contact-us-with-error-message' ) . ' ' . '<b>' . $install->error->message . '</b>',
7903
- $this->get_text( 'oops' ) . '...',
7904
- 'error'
7905
- );
7906
-
7907
- if ( $redirect ) {
7908
- fs_redirect( $this->get_activation_url( array( 'error' => $install->error->message ) ) );
7909
- }
7910
-
7911
- return $install;
7912
- }
7913
-
7914
- $site = new FS_Site( $install );
7915
- $this->_site = $site;
7916
-
7917
- return $this->setup_account( $this->_user, $this->_site, $redirect );
7918
- }
7919
-
7920
- /**
7921
- * Tries to activate add-on account based on parent plugin info.
7922
- *
7923
- * @author Vova Feldman (@svovaf)
7924
- * @since 1.0.6
7925
- *
7926
- * @param Freemius $parent_fs
7927
- */
7928
- private function _activate_addon_account( Freemius $parent_fs ) {
7929
- if ( $this->is_registered() ) {
7930
- // Already activated.
7931
- return;
7932
- }
7933
-
7934
- // Activate add-on with parent plugin credentials.
7935
- $addon_install = $parent_fs->get_api_site_scope()->call(
7936
- "/addons/{$this->_plugin->id}/installs.json",
7937
- 'post',
7938
- $this->get_install_data_for_api( array(
7939
- 'uid' => $this->get_anonymous_id(),
7940
- ), false, false )
7941
- );
7942
-
7943
- if ( isset( $addon_install->error ) ) {
7944
- $this->_admin_notices->add(
7945
- sprintf( $this->get_text( 'could-not-activate-x' ), $this->get_plugin_name() ) . ' ' .
7946
- $this->get_text( 'contact-us-with-error-message' ) . ' ' . '<b>' . $addon_install->error->message . '</b>',
7947
- $this->get_text( 'oops' ) . '...',
7948
- 'error'
7949
- );
7950
-
7951
- return;
7952
- }
7953
-
7954
- // First of all, set site info - otherwise we won't
7955
- // be able to invoke API calls.
7956
- $this->_site = new FS_Site( $addon_install );
7957
-
7958
- // Sync add-on plans.
7959
- $this->_sync_plans();
7960
-
7961
- // Get site's current plan.
7962
- $this->_site->plan = $this->_get_plan_by_id( $this->_site->plan->id );
7963
-
7964
- // Get user information based on parent's plugin.
7965
- $user = $parent_fs->get_user();
7966
-
7967
- $this->_set_account( $user, $this->_site );
7968
-
7969
- // Sync licenses.
7970
- $this->_sync_licenses();
7971
-
7972
- // Try to activate premium license.
7973
- $this->_activate_license( true );
7974
- }
7975
-
7976
- #endregion
7977
-
7978
- #----------------------------------------------------------------------------------
7979
- #region Admin Menu Items
7980
- #----------------------------------------------------------------------------------
7981
-
7982
- private $_menu_items = array();
7983
-
7984
- /**
7985
- * @author Vova Feldman (@svovaf)
7986
- * @since 1.0.7
7987
- *
7988
- * @return string
7989
- */
7990
- function get_menu_slug() {
7991
- return $this->_menu->get_slug();
7992
- }
7993
-
7994
- /**
7995
- * @author Vova Feldman (@svovaf)
7996
- * @since 1.0.9
7997
- */
7998
- function _prepare_admin_menu() {
7999
- // if ( ! $this->is_on() ) {
8000
- // return;
8001
- // }
8002
-
8003
- if ( ! $this->has_api_connectivity() && ! $this->is_enable_anonymous() ) {
8004
- $this->_menu->remove_menu_item();
8005
- } else {
8006
- $this->do_action( 'before_admin_menu_init' );
8007
-
8008
- $this->add_menu_action();
8009
- $this->add_submenu_items();
8010
- }
8011
- }
8012
-
8013
- /**
8014
- * Admin dashboard menu items modifications.
8015
- *
8016
- * NOTE: admin_menu action executed before admin_init.
8017
- *
8018
- * @author Vova Feldman (@svovaf)
8019
- * @since 1.0.7
8020
- *
8021
- */
8022
- private function add_menu_action() {
8023
- if ( $this->is_activation_mode() ) {
8024
- $this->override_plugin_menu_with_activation();
8025
- } else {
8026
- // If not registered try to install user.
8027
- if ( ! $this->is_registered() &&
8028
- fs_request_is_action( $this->_slug . '_activate_new' )
8029
- ) {
8030
- $this->_install_with_new_user();
8031
- } else if ( fs_request_is_action( 'sync_user' ) && ! $this->has_settings_menu() ) {
8032
- $this->_handle_account_user_sync();
8033
- }
8034
- }
8035
- }
8036
-
8037
- /**
8038
- * @author Vova Feldman (@svovaf)
8039
- * @since 1.0.1
8040
- */
8041
- function _redirect_on_clicked_menu_link() {
8042
- $this->_logger->entrance();
8043
-
8044
- $page = strtolower( isset( $_REQUEST['page'] ) ? $_REQUEST['page'] : '' );
8045
-
8046
- $this->_logger->log( 'page = ' . $page );
8047
-
8048
- foreach ( $this->_menu_items as $priority => $items ) {
8049
- foreach ( $items as $item ) {
8050
- if ( isset( $item['url'] ) ) {
8051
- if ( $page === $this->_menu->get_slug( strtolower( $item['menu_slug'] ) ) ) {
8052
- $this->_logger->log( 'Redirecting to ' . $item['url'] );
8053
-
8054
- fs_redirect( $item['url'] );
8055
- }
8056
- }
8057
- }
8058
- }
8059
- }
8060
-
8061
- /**
8062
- * Remove plugin's all admin menu items & pages, and replace with activation page.
8063
- *
8064
- * @author Vova Feldman (@svovaf)
8065
- * @since 1.0.1
8066
- */
8067
- private function override_plugin_menu_with_activation() {
8068
- $this->_logger->entrance();
8069
-
8070
- $hook = false;
8071
-
8072
- if ( ! $this->_menu->has_menu() ) {
8073
- // Add the opt-in page without a menu item.
8074
- $hook = FS_Admin_Menu_Manager::add_subpage(
8075
- null,
8076
- $this->get_plugin_name(),
8077
- $this->get_plugin_name(),
8078
- 'manage_options',
8079
- $this->_slug,
8080
- array( &$this, '_connect_page_render' )
8081
- );
8082
- } else if ( $this->_menu->is_top_level() ) {
8083
- $hook = $this->_menu->override_menu_item( array( &$this, '_connect_page_render' ) );
8084
-
8085
- if ( false === $hook ) {
8086
- // Create new menu item just for the opt-in.
8087
- $hook = FS_Admin_Menu_Manager::add_page(
8088
- $this->get_plugin_name(),
8089
- $this->get_plugin_name(),
8090
- 'manage_options',
8091
- $this->_menu->get_slug(),
8092
- array( &$this, '_connect_page_render' )
8093
- );
8094
- }
8095
- } else {
8096
- $menus = array( $this->_menu->get_parent_slug() );
8097
-
8098
- if ( $this->_menu->is_override_exact() ) {
8099
- if ( ! $this->is_matching_url( $_SERVER['REQUEST_URI'], $this->get_activation_url() ) ) {
8100
- return;
8101
- }
8102
- }
8103
-
8104
- foreach ( $menus as $parent_slug ) {
8105
- $hook = $this->_menu->override_submenu_action(
8106
- $parent_slug,
8107
- $this->_menu->get_raw_slug(),
8108
- array( &$this, '_connect_page_render' )
8109
- );
8110
-
8111
- if ( false !== $hook ) {
8112
- // Found plugin's submenu item.
8113
- break;
8114
- }
8115
- }
8116
- }
8117
-
8118
- if ( $this->is_activation_page() ) {
8119
- // Clean admin page from distracting content.
8120
- self::_clean_admin_content_section();
8121
- }
8122
-
8123
- if ( false !== $hook ) {
8124
- if ( fs_request_is_action( $this->_slug . '_activate_existing' ) ) {
8125
- add_action( "load-$hook", array( &$this, '_install_with_current_user' ) );
8126
- } else if ( fs_request_is_action( $this->_slug . '_activate_new' ) ) {
8127
- add_action( "load-$hook", array( &$this, '_install_with_new_user' ) );
8128
- }
8129
- }
8130
- }
8131
-
8132
- /**
8133
- * @author Leo Fajardo (leorw)
8134
- * @since 1.2.1
8135
- *
8136
- * return string
8137
- */
8138
- function get_top_level_menu_capability() {
8139
- global $menu;
8140
-
8141
- $top_level_menu_slug = $this->get_top_level_menu_slug();
8142
-
8143
- foreach ( $menu as $menu_info ) {
8144
- /**
8145
- * The second element in the menu info array is the capability/role that has access to the menu and the
8146
- * third element is the menu slug.
8147
- */
8148
- if ( $menu_info[2] === $top_level_menu_slug ) {
8149
- return $menu_info[1];
8150
- }
8151
- }
8152
-
8153
- return 'read';
8154
- }
8155
-
8156
- /**
8157
- * @author Vova Feldman (@svovaf)
8158
- * @since 1.0.0
8159
- *
8160
- * @return string
8161
- */
8162
- private function get_top_level_menu_slug() {
8163
- return ( $this->is_addon() ?
8164
- $this->get_parent_instance()->_menu->get_top_level_menu_slug() :
8165
- $this->_menu->get_top_level_menu_slug() );
8166
- }
8167
-
8168
- /**
8169
- * Add default Freemius menu items.
8170
- *
8171
- * @author Vova Feldman (@svovaf)
8172
- * @since 1.0.0
8173
- */
8174
- private function add_submenu_items() {
8175
- $this->_logger->entrance();
8176
-
8177
- if ( ! $this->is_addon() ) {
8178
- if ( ! $this->is_activation_mode() ) {
8179
- if ( $this->is_registered() ) {
8180
- // Add user account page.
8181
- $this->add_submenu_item(
8182
- $this->get_text( 'account' ),
8183
- array( &$this, '_account_page_render' ),
8184
- $this->get_plugin_name() . ' &ndash; ' . $this->get_text( 'account' ),
8185
- 'manage_options',
8186
- 'account',
8187
- array( &$this, '_account_page_load' ),
8188
- WP_FS__DEFAULT_PRIORITY,
8189
- $this->is_submenu_item_visible( 'account' )
8190
- );
8191
- }
8192
-
8193
- // Add contact page.
8194
- $this->add_submenu_item(
8195
- $this->get_text( 'contact-us' ),
8196
- array( &$this, '_contact_page_render' ),
8197
- $this->get_plugin_name() . ' &ndash; ' . $this->get_text( 'contact-us' ),
8198
- 'manage_options',
8199
- 'contact',
8200
- 'Freemius::_clean_admin_content_section',
8201
- WP_FS__DEFAULT_PRIORITY,
8202
- $this->is_submenu_item_visible( 'contact' )
8203
- );
8204
-
8205
- if ( $this->has_addons() ) {
8206
- $this->add_submenu_item(
8207
- $this->get_text( 'add-ons' ),
8208
- array( &$this, '_addons_page_render' ),
8209
- $this->get_plugin_name() . ' &ndash; ' . $this->get_text( 'add-ons' ),
8210
- 'manage_options',
8211
- 'addons',
8212
- array( &$this, '_addons_page_load' ),
8213
- WP_FS__LOWEST_PRIORITY - 1,
8214
- $this->is_submenu_item_visible( 'addons' )
8215
- );
8216
- }
8217
-
8218
- $show_pricing = (
8219
- // Has at least one paid plan.
8220
- $this->has_paid_plan() &&
8221
- // Didn't ask to hide the pricing page.
8222
- $this->is_submenu_item_visible( 'pricing' ) &&
8223
- // Don't have a valid active license or has more than one plan.
8224
- ( ! $this->is_paying() || ! $this->is_single_plan() )
8225
- );
8226
- // If user don't have paid plans, add pricing page
8227
- // to support add-ons checkout but don't add the submenu item.
8228
- // || (isset( $_GET['page'] ) && $this->_menu->get_slug( 'pricing' ) == $_GET['page']);
8229
-
8230
- $pricing_cta_slug = 'upgrade';
8231
- $pricing_class = 'upgrade-mode';
8232
- if ( $show_pricing ) {
8233
- if ( $this->_admin_notices->has_sticky( 'trial_promotion' ) &&
8234
- ! $this->is_paying_or_trial()
8235
- ) {
8236
- // If running a trial promotion, modify the pricing to load the trial.
8237
- $pricing_cta_slug = 'start-trial';
8238
- $pricing_class = 'trial-mode';
8239
- } else if ( $this->is_paying() ) {
8240
- $pricing_cta_slug = 'pricing';
8241
- $pricing_class = '';
8242
- }
8243
- }
8244
-
8245
- // Add upgrade/pricing page.
8246
- $this->add_submenu_item(
8247
- $this->get_text( $pricing_cta_slug ) . '&nbsp;&nbsp;' . ( is_rtl() ? '&#x2190;' : '&#x27a4;' ),
8248
- array( &$this, '_pricing_page_render' ),
8249
- $this->get_plugin_name() . ' &ndash; ' . $this->get_text( 'pricing' ),
8250
- 'manage_options',
8251
- 'pricing',
8252
- 'Freemius::_clean_admin_content_section',
8253
- WP_FS__LOWEST_PRIORITY,
8254
- $show_pricing,
8255
- $pricing_class
8256
- );
8257
- }
8258
- }
8259
-
8260
-
8261
- if ( 0 < count( $this->_menu_items ) ) {
8262
- if ( ! $this->_menu->is_top_level() ) {
8263
- fs_enqueue_local_style( 'fs_common', '/admin/common.css' );
8264
-
8265
- // Append submenu items right after the plugin's submenu item.
8266
- $this->order_sub_submenu_items();
8267
- } else {
8268
- // Append submenu items.
8269
- $this->embed_submenu_items();
8270
- }
8271
- }
8272
- }
8273
-
8274
- /**
8275
- * Moved the actual submenu item additions to a separated function,
8276
- * in order to support sub-submenu items when the plugin's settings
8277
- * only have a submenu and not top-level menu item.
8278
- *
8279
- * @author Vova Feldman (@svovaf)
8280
- * @since 1.1.4
8281
- */
8282
- private function embed_submenu_items() {
8283
- $item_template = $this->_menu->is_top_level() ?
8284
- '<span class="fs-submenu-item %s %s %s">%s</span>' :
8285
- '<span class="fs-submenu-item fs-sub %s %s %s">%s</span>';
8286
-
8287
- $top_level_menu_capability = $this->get_top_level_menu_capability();
8288
-
8289
- ksort( $this->_menu_items );
8290
-
8291
- foreach ( $this->_menu_items as $priority => $items ) {
8292
- foreach ( $items as $item ) {
8293
- $capability = ( ! empty( $item['capability'] ) ? $item['capability'] : $top_level_menu_capability );
8294
-
8295
- $menu_item = sprintf(
8296
- $item_template,
8297
- $this->_slug,
8298
- $item['menu_slug'],
8299
- ! empty( $item['class'] ) ? $item['class'] : '',
8300
- $item['menu_title']
8301
- );
8302
-
8303
- $menu_slug = $this->_menu->get_slug( $item['menu_slug'] );
8304
-
8305
- if ( ! isset( $item['url'] ) ) {
8306
- $hook = FS_Admin_Menu_Manager::add_subpage(
8307
- $item['show_submenu'] ?
8308
- $this->get_top_level_menu_slug() :
8309
- null,
8310
- $item['page_title'],
8311
- $menu_item,
8312
- $capability,
8313
- $menu_slug,
8314
- $item['render_function']
8315
- );
8316
-
8317
- if ( false !== $item['before_render_function'] ) {
8318
- add_action( "load-$hook", $item['before_render_function'] );
8319
- }
8320
- } else {
8321
- FS_Admin_Menu_Manager::add_subpage(
8322
- $this->get_top_level_menu_slug(),
8323
- $item['page_title'],
8324
- $menu_item,
8325
- $capability,
8326
- $menu_slug,
8327
- array( $this, '' )
8328
- );
8329
- }
8330
- }
8331
- }
8332
- }
8333
-
8334
- /**
8335
- * Re-order the submenu items so all Freemius added new submenu items
8336
- * are added right after the plugin's settings submenu item.
8337
- *
8338
- * @author Vova Feldman (@svovaf)
8339
- * @since 1.1.4
8340
- */
8341
- private function order_sub_submenu_items() {
8342
- global $submenu;
8343
-
8344
- $menu_slug = $this->_menu->get_top_level_menu_slug();
8345
-
8346
- /**
8347
- * Before "admin_menu" fires, WordPress will loop over the default submenus and remove pages for which the user
8348
- * does not have permissions. So in case a plugin does not have top-level menu but does have submenus under any
8349
- * of the default menus, only users that have the right role can access its sub-submenus (Account, Contact Us,
8350
- * Support Forum, etc.) since $submenu[ $menu_slug ] will be empty if the user doesn't have permission.
8351
- *
8352
- * In case a plugin does not have submenus under any of the default menus but does have submenus under the menu
8353
- * of another plugin, only users that have the right role can access its sub-submenus since we will use the
8354
- * capability needed to access the parent menu as the capability for the submenus that we will add.
8355
- */
8356
- if ( empty( $submenu[ $menu_slug ] ) ) {
8357
- return;
8358
- }
8359
-
8360
- $top_level_menu = &$submenu[ $menu_slug ];
8361
-
8362
- $all_submenu_items_after = array();
8363
-
8364
- $found_submenu_item = false;
8365
-
8366
- foreach ( $top_level_menu as $submenu_id => $meta ) {
8367
- if ( $found_submenu_item ) {
8368
- // Remove all submenu items after the plugin's submenu item.
8369
- $all_submenu_items_after[] = $meta;
8370
- unset( $top_level_menu[ $submenu_id ] );
8371
- }
8372
-
8373
- if ( $this->_menu->get_raw_slug() === $meta[2] ) {
8374
- // Found the submenu item, put all below.
8375
- $found_submenu_item = true;
8376
- continue;
8377
- }
8378
- }
8379
-
8380
- // Embed all plugin's new submenu items.
8381
- $this->embed_submenu_items();
8382
-
8383
- // Start with specially high number to make sure it's appended.
8384
- $i = max( 10000, max( array_keys( $top_level_menu ) ) + 1 );
8385
- foreach ( $all_submenu_items_after as $meta ) {
8386
- $top_level_menu[ $i ] = $meta;
8387
- $i ++;
8388
- }
8389
-
8390
- // Sort submenu items.
8391
- ksort( $top_level_menu );
8392
- }
8393
-
8394
- /**
8395
- * Displays the Support Forum link when enabled.
8396
- *
8397
- * Can be filtered like so:
8398
- *
8399
- * function _fs_show_support_menu( $is_visible, $menu_id ) {
8400
- * if ( 'support' === $menu_id ) {
8401
- * return _fs->is_registered();
8402
- * }
8403
- * return $is_visible;
8404
- * }
8405
- * _fs()->add_filter('is_submenu_visible', '_fs_show_support_menu', 10, 2);
8406
- *
8407
- */
8408
- function _add_default_submenu_items() {
8409
- if ( ! $this->is_on() ) {
8410
- return;
8411
- }
8412
-
8413
- if ( ! $this->is_activation_mode() ) {
8414
- if ( $this->is_submenu_item_visible( 'support' ) ) {
8415
- $this->add_submenu_link_item(
8416
- $this->apply_filters( 'support_forum_submenu', $this->get_text( 'support-forum' ) ),
8417
- $this->apply_filters( 'support_forum_url', 'https://wordpress.org/support/plugin/' . $this->_slug ),
8418
- 'wp-support-forum',
8419
- null,
8420
- 50
8421
- );
8422
- }
8423
- }
8424
- }
8425
-
8426
- /**
8427
- * @author Vova Feldman (@svovaf)
8428
- * @since 1.0.1
8429
- *
8430
- * @param string $menu_title
8431
- * @param callable $render_function
8432
- * @param bool|string $page_title
8433
- * @param string $capability
8434
- * @param bool|string $menu_slug
8435
- * @param bool|callable $before_render_function
8436
- * @param int $priority
8437
- * @param bool $show_submenu
8438
- * @param string $class Since 1.2.1.5 can add custom classes to menu items.
8439
- */
8440
- function add_submenu_item(
8441
- $menu_title,
8442
- $render_function,
8443
- $page_title = false,
8444
- $capability = 'manage_options',
8445
- $menu_slug = false,
8446
- $before_render_function = false,
8447
- $priority = WP_FS__DEFAULT_PRIORITY,
8448
- $show_submenu = true,
8449
- $class = ''
8450
- ) {
8451
- $this->_logger->entrance( 'Title = ' . $menu_title );
8452
-
8453
- if ( $this->is_addon() ) {
8454
- $parent_fs = $this->get_parent_instance();
8455
-
8456
- if ( is_object( $parent_fs ) ) {
8457
- $parent_fs->add_submenu_item(
8458
- $menu_title,
8459
- $render_function,
8460
- $page_title,
8461
- $capability,
8462
- $menu_slug,
8463
- $before_render_function,
8464
- $priority,
8465
- $show_submenu,
8466
- $class
8467
- );
8468
-
8469
- return;
8470
- }
8471
- }
8472
-
8473
- if ( ! isset( $this->_menu_items[ $priority ] ) ) {
8474
- $this->_menu_items[ $priority ] = array();
8475
- }
8476
-
8477
- $this->_menu_items[ $priority ][] = array(
8478
- 'page_title' => is_string( $page_title ) ? $page_title : $menu_title,
8479
- 'menu_title' => $menu_title,
8480
- 'capability' => $capability,
8481
- 'menu_slug' => is_string( $menu_slug ) ? $menu_slug : strtolower( $menu_title ),
8482
- 'render_function' => $render_function,
8483
- 'before_render_function' => $before_render_function,
8484
- 'show_submenu' => $show_submenu,
8485
- 'class' => $class,
8486
- );
8487
- }
8488
-
8489
- /**
8490
- * @author Vova Feldman (@svovaf)
8491
- * @since 1.0.1
8492
- *
8493
- * @param string $menu_title
8494
- * @param string $url
8495
- * @param bool $menu_slug
8496
- * @param string $capability
8497
- * @param int $priority
8498
- *
8499
- */
8500
- function add_submenu_link_item(
8501
- $menu_title,
8502
- $url,
8503
- $menu_slug = false,
8504
- $capability = 'read',
8505
- $priority = WP_FS__DEFAULT_PRIORITY
8506
- ) {
8507
- $this->_logger->entrance( 'Title = ' . $menu_title . '; Url = ' . $url );
8508
-
8509
- if ( $this->is_addon() ) {
8510
- $parent_fs = $this->get_parent_instance();
8511
-
8512
- if ( is_object( $parent_fs ) ) {
8513
- $parent_fs->add_submenu_link_item(
8514
- $menu_title,
8515
- $url,
8516
- $menu_slug,
8517
- $capability,
8518
- $priority
8519
- );
8520
-
8521
- return;
8522
- }
8523
- }
8524
-
8525
- if ( ! isset( $this->_menu_items[ $priority ] ) ) {
8526
- $this->_menu_items[ $priority ] = array();
8527
- }
8528
-
8529
- $this->_menu_items[ $priority ][] = array(
8530
- 'menu_title' => $menu_title,
8531
- 'capability' => $capability,
8532
- 'menu_slug' => is_string( $menu_slug ) ? $menu_slug : strtolower( $menu_title ),
8533
- 'url' => $url,
8534
- 'page_title' => $menu_title,
8535
- 'render_function' => 'fs_dummy',
8536
- 'before_render_function' => '',
8537
- );
8538
- }
8539
-
8540
- #endregion ------------------------------------------------------------------
8541
-
8542
- #--------------------------------------------------------------------------------
8543
- #region Actions / Hooks / Filters
8544
- #--------------------------------------------------------------------------------
8545
-
8546
- /**
8547
- * @author Vova Feldman (@svovaf)
8548
- * @since 1.1.7
8549
- *
8550
- * @param string $tag
8551
- *
8552
- * @return string
8553
- */
8554
- public function get_action_tag( $tag ) {
8555
- return self::get_action_tag_static( $tag, $this->_slug );
8556
- }
8557
-
8558
- /**
8559
- * @author Vova Feldman (@svovaf)
8560
- * @since 1.2.1.6
8561
- *
8562
- * @param string $tag
8563
- * @param string $slug
8564
- *
8565
- * @return string
8566
- */
8567
- static function get_action_tag_static( $tag, $slug = '' ) {
8568
- return "fs_{$tag}" . ( empty( $slug ) ? '' : "_{$slug}" );
8569
- }
8570
-
8571
- /**
8572
- * @author Vova Feldman (@svovaf)
8573
- * @since 1.2.1
8574
- *
8575
- * @param string $tag
8576
- *
8577
- * @return string
8578
- */
8579
- function get_ajax_action( $tag ) {
8580
- return self::get_ajax_action_static( $tag, $this->_slug );
8581
- }
8582
-
8583
- /**
8584
- * @author Vova Feldman (@svovaf)
8585
- * @since 1.2.1.7
8586
- *
8587
- * @param string $tag
8588
- *
8589
- * @return string
8590
- */
8591
- function get_ajax_security( $tag ) {
8592
- return wp_create_nonce( $this->get_ajax_action( $tag ) );
8593
- }
8594
-
8595
- /**
8596
- * @author Vova Feldman (@svovaf)
8597
- * @since 1.2.1.7
8598
- *
8599
- * @param string $tag
8600
- */
8601
- function check_ajax_referer( $tag ) {
8602
- check_ajax_referer( $this->get_ajax_action( $tag ), 'security' );
8603
- }
8604
-
8605
- /**
8606
- * @author Vova Feldman (@svovaf)
8607
- * @since 1.2.1.6
8608
- *
8609
- * @param string $tag
8610
- * @param string $slug
8611
- *
8612
- * @return string
8613
- */
8614
- private static function get_ajax_action_static( $tag, $slug = '' ) {
8615
- $action = "fs_{$tag}";
8616
-
8617
- if ( ! empty( $slug ) ) {
8618
- $action .= "_{$slug}";
8619
- }
8620
-
8621
- return $action;
8622
- }
8623
-
8624
- /**
8625
- * Do action, specific for the current context plugin.
8626
- *
8627
- * @author Vova Feldman (@svovaf)
8628
- * @since 1.0.1
8629
- *
8630
- * @param string $tag The name of the action to be executed.
8631
- * @param mixed $arg,... Optional. Additional arguments which are passed on to the
8632
- * functions hooked to the action. Default empty.
8633
- *
8634
- * @uses do_action()
8635
- */
8636
- function do_action( $tag, $arg = '' ) {
8637
- $this->_logger->entrance( $tag );
8638
-
8639
- $args = func_get_args();
8640
-
8641
- call_user_func_array( 'do_action', array_merge(
8642
- array( $this->get_action_tag( $tag ) ),
8643
- array_slice( $args, 1 ) )
8644
- );
8645
- }
8646
-
8647
- /**
8648
- * Add action, specific for the current context plugin.
8649
- *
8650
- * @author Vova Feldman (@svovaf)
8651
- * @since 1.0.1
8652
- *
8653
- * @param string $tag
8654
- * @param callable $function_to_add
8655
- * @param int $priority
8656
- * @param int $accepted_args
8657
- *
8658
- * @uses add_action()
8659
- */
8660
- function add_action(
8661
- $tag,
8662
- $function_to_add,
8663
- $priority = WP_FS__DEFAULT_PRIORITY,
8664
- $accepted_args = 1
8665
- ) {
8666
- $this->_logger->entrance( $tag );
8667
-
8668
- add_action( $this->get_action_tag( $tag ), $function_to_add, $priority, $accepted_args );
8669
- }
8670
-
8671
- /**
8672
- * Add AJAX action, specific for the current context plugin.
8673
- *
8674
- * @author Vova Feldman (@svovaf)
8675
- * @since 1.2.1
8676
- *
8677
- * @param string $tag
8678
- * @param callable $function_to_add
8679
- * @param int $priority
8680
- *
8681
- * @uses add_action()
8682
- *
8683
- * @return bool True if action added, false if no need to add the action since the AJAX call isn't matching.
8684
- */
8685
- function add_ajax_action(
8686
- $tag,
8687
- $function_to_add,
8688
- $priority = WP_FS__DEFAULT_PRIORITY
8689
- ) {
8690
- $this->_logger->entrance( $tag );
8691
-
8692
- return self::add_ajax_action_static(
8693
- $tag,
8694
- $function_to_add,
8695
- $priority,
8696
- $this->_slug
8697
- );
8698
- }
8699
-
8700
- /**
8701
- * Add AJAX action.
8702
- *
8703
- * @author Vova Feldman (@svovaf)
8704
- * @since 1.2.1.6
8705
- *
8706
- * @param string $tag
8707
- * @param callable $function_to_add
8708
- * @param int $priority
8709
- * @param string $slug
8710
- *
8711
- * @return bool True if action added, false if no need to add the action since the AJAX call isn't matching.
8712
- * @uses add_action()
8713
- *
8714
- */
8715
- static function add_ajax_action_static(
8716
- $tag,
8717
- $function_to_add,
8718
- $priority = WP_FS__DEFAULT_PRIORITY,
8719
- $slug = ''
8720
- ) {
8721
- self::$_static_logger->entrance( $tag );
8722
-
8723
- if ( ! self::is_ajax_action_static( $tag, $slug ) ) {
8724
- return false;
8725
- }
8726
-
8727
- add_action(
8728
- 'wp_ajax_' . self::get_ajax_action_static( $tag, $slug ),
8729
- $function_to_add,
8730
- $priority,
8731
- 0
8732
- );
8733
-
8734
- self::$_static_logger->info( "$tag AJAX callback action added." );
8735
-
8736
- return true;
8737
- }
8738
-
8739
- /**
8740
- * Send a JSON response back to an Ajax request.
8741
- *
8742
- * @author Vova Feldman (@svovaf)
8743
- * @since 1.2.1.5
8744
- *
8745
- * @param mixed $response
8746
- */
8747
- static function shoot_ajax_response( $response ) {
8748
- wp_send_json( $response );
8749
- }
8750
-
8751
- /**
8752
- * Send a JSON response back to an Ajax request, indicating success.
8753
- *
8754
- * @author Vova Feldman (@svovaf)
8755
- * @since 1.2.1.5
8756
- *
8757
- * @param mixed $data Data to encode as JSON, then print and exit.
8758
- */
8759
- static function shoot_ajax_success( $data = null ) {
8760
- wp_send_json_success( $data );
8761
- }
8762
-
8763
- /**
8764
- * Send a JSON response back to an Ajax request, indicating failure.
8765
- *
8766
- * @author Vova Feldman (@svovaf)
8767
- * @since 1.2.1.5
8768
- *
8769
- * @param mixed $error Optional error message.
8770
- */
8771
- static function shoot_ajax_failure( $error = '' ) {
8772
- $result = array( 'success' => false );
8773
- if ( ! empty( $error ) ) {
8774
- $result['error'] = $error;
8775
- }
8776
-
8777
- wp_send_json( $result );
8778
- }
8779
-
8780
- /**
8781
- * Apply filter, specific for the current context plugin.
8782
- *
8783
- * @author Vova Feldman (@svovaf)
8784
- * @since 1.0.9
8785
- *
8786
- * @param string $tag The name of the filter hook.
8787
- * @param mixed $value The value on which the filters hooked to `$tag` are applied on.
8788
- *
8789
- * @return mixed The filtered value after all hooked functions are applied to it.
8790
- *
8791
- * @uses apply_filters()
8792
- */
8793
- function apply_filters( $tag, $value ) {
8794
- $this->_logger->entrance( $tag );
8795
-
8796
- $args = func_get_args();
8797
- array_unshift( $args, $this->_slug );
8798
-
8799
- return call_user_func_array( 'fs_apply_filter', $args );
8800
- }
8801
-
8802
- /**
8803
- * Add filter, specific for the current context plugin.
8804
- *
8805
- * @author Vova Feldman (@svovaf)
8806
- * @since 1.0.9
8807
- *
8808
- * @param string $tag
8809
- * @param callable $function_to_add
8810
- * @param int $priority
8811
- * @param int $accepted_args
8812
- *
8813
- * @uses add_filter()
8814
- */
8815
- function add_filter( $tag, $function_to_add, $priority = WP_FS__DEFAULT_PRIORITY, $accepted_args = 1 ) {
8816
- $this->_logger->entrance( $tag );
8817
-
8818
- add_filter( $this->get_action_tag( $tag ), $function_to_add, $priority, $accepted_args );
8819
- }
8820
-
8821
- /**
8822
- * Check if has filter.
8823
- *
8824
- * @author Vova Feldman (@svovaf)
8825
- * @since 1.1.4
8826
- *
8827
- * @param string $tag
8828
- * @param callable|bool $function_to_check Optional. The callback to check for. Default false.
8829
- *
8830
- * @return false|int
8831
- *
8832
- * @uses has_filter()
8833
- */
8834
- function has_filter( $tag, $function_to_check = false ) {
8835
- $this->_logger->entrance( $tag );
8836
-
8837
- return has_filter( $this->get_action_tag( $tag ), $function_to_check );
8838
- }
8839
-
8840
- #endregion
8841
-
8842
- /**
8843
- * Override default i18n text phrases.
8844
- *
8845
- * @author Vova Feldman (@svovaf)
8846
- * @since 1.1.6
8847
- *
8848
- * @param string[] string $key_value
8849
- *
8850
- * @uses fs_override_i18n()
8851
- */
8852
- function override_i18n( $key_value ) {
8853
- fs_override_i18n( $key_value, $this->_slug );
8854
- }
8855
-
8856
- /* Account Page
8857
- ------------------------------------------------------------------------------------------------------------------*/
8858
- /**
8859
- * Update site information.
8860
- *
8861
- * @author Vova Feldman (@svovaf)
8862
- * @since 1.0.1
8863
- *
8864
- * @param bool $store Flush to Database if true.
8865
- */
8866
- private function _store_site( $store = true ) {
8867
- $this->_logger->entrance();
8868
-
8869
- if ( empty( $this->_site->id ) ) {
8870
- $this->_logger->error( "Empty install ID, can't store site." );
8871
-
8872
- return;
8873
- }
8874
-
8875
- $encrypted_site = clone $this->_site;
8876
- $encrypted_site->plan = self::_encrypt_entity( $this->_site->plan );
8877
-
8878
- $sites = self::get_all_sites();
8879
- $sites[ $this->_slug ] = $encrypted_site;
8880
- self::$_accounts->set_option( 'sites', $sites, $store );
8881
- }
8882
-
8883
- /**
8884
- * Update plugin's plans information.
8885
- *
8886
- * @author Vova Feldman (@svovaf)
8887
- * @since 1.0.2
8888
- *
8889
- * @param bool $store Flush to Database if true.
8890
- */
8891
- private function _store_plans( $store = true ) {
8892
- $this->_logger->entrance();
8893
-
8894
- $plans = self::get_all_plans();
8895
-
8896
- // Copy plans.
8897
- $encrypted_plans = array();
8898
- for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
8899
- $encrypted_plans[] = self::_encrypt_entity( $this->_plans[ $i ] );
8900
- }
8901
-
8902
- $plans[ $this->_slug ] = $encrypted_plans;
8903
- self::$_accounts->set_option( 'plans', $plans, $store );
8904
- }
8905
-
8906
- /**
8907
- * Update user's plugin licenses.
8908
- *
8909
- * @author Vova Feldman (@svovaf)
8910
- * @since 1.0.5
8911
- *
8912
- * @param bool $store
8913
- * @param string|bool $plugin_slug
8914
- * @param FS_Plugin_License[] $licenses
8915
- */
8916
- private function _store_licenses( $store = true, $plugin_slug = false, $licenses = array() ) {
8917
- $this->_logger->entrance();
8918
-
8919
- $all_licenses = self::get_all_licenses();
8920
-
8921
- if ( ! is_string( $plugin_slug ) ) {
8922
- $plugin_slug = $this->_slug;
8923
- $licenses = $this->_licenses;
8924
- }
8925
-
8926
- if ( ! isset( $all_licenses[ $plugin_slug ] ) ) {
8927
- $all_licenses[ $plugin_slug ] = array();
8928
- }
8929
-
8930
- $all_licenses[ $plugin_slug ][ $this->_user->id ] = $licenses;
8931
-
8932
- self::$_accounts->set_option( 'licenses', $all_licenses, $store );
8933
- }
8934
-
8935
- /**
8936
- * Update user information.
8937
- *
8938
- * @author Vova Feldman (@svovaf)
8939
- * @since 1.0.1
8940
- *
8941
- * @param bool $store Flush to Database if true.
8942
- */
8943
- private function _store_user( $store = true ) {
8944
- $this->_logger->entrance();
8945
-
8946
- if ( empty( $this->_user->id ) ) {
8947
- $this->_logger->error( "Empty user ID, can't store user." );
8948
-
8949
- return;
8950
- }
8951
-
8952
- $users = self::get_all_users();
8953
- $users[ $this->_user->id ] = $this->_user;
8954
- self::$_accounts->set_option( 'users', $users, $store );
8955
- }
8956
-
8957
- /**
8958
- * Update new updates information.
8959
- *
8960
- * @author Vova Feldman (@svovaf)
8961
- * @since 1.0.4
8962
- *
8963
- * @param FS_Plugin_Tag|null $update
8964
- * @param bool $store Flush to Database if true.
8965
- * @param bool|number $plugin_id
8966
- */
8967
- private function _store_update( $update, $store = true, $plugin_id = false ) {
8968
- $this->_logger->entrance();
8969
-
8970
- if ( $update instanceof FS_Plugin_Tag ) {
8971
- $update->updated = time();
8972
- }
8973
-
8974
- if ( ! is_numeric( $plugin_id ) ) {
8975
- $plugin_id = $this->_plugin->id;
8976
- }
8977
-
8978
- $updates = self::get_all_updates();
8979
- $updates[ $plugin_id ] = $update;
8980
- self::$_accounts->set_option( 'updates', $updates, $store );
8981
- }
8982
-
8983
- /**
8984
- * Update new updates information.
8985
- *
8986
- * @author Vova Feldman (@svovaf)
8987
- * @since 1.0.6
8988
- *
8989
- * @param FS_Plugin[] $plugin_addons
8990
- * @param bool $store Flush to Database if true.
8991
- */
8992
- private function _store_addons( $plugin_addons, $store = true ) {
8993
- $this->_logger->entrance();
8994
-
8995
- $addons = self::get_all_addons();
8996
- $addons[ $this->_plugin->id ] = $plugin_addons;
8997
- self::$_accounts->set_option( 'addons', $addons, $store );
8998
- }
8999
-
9000
- /**
9001
- * Delete plugin's associated add-ons.
9002
- *
9003
- * @author Vova Feldman (@svovaf)
9004
- * @since 1.0.8
9005
- *
9006
- * @param bool $store
9007
- *
9008
- * @return bool
9009
- */
9010
- private function _delete_account_addons( $store = true ) {
9011
- $all_addons = self::get_all_account_addons();
9012
-
9013
- if ( ! isset( $all_addons[ $this->_plugin->id ] ) ) {
9014
- return false;
9015
- }
9016
-
9017
- unset( $all_addons[ $this->_plugin->id ] );
9018
-
9019
- self::$_accounts->set_option( 'account_addons', $all_addons, $store );
9020
-
9021
- return true;
9022
- }
9023
-
9024
- /**
9025
- * Update account add-ons list.
9026
- *
9027
- * @author Vova Feldman (@svovaf)
9028
- * @since 1.0.6
9029
- *
9030
- * @param FS_Plugin[] $addons
9031
- * @param bool $store Flush to Database if true.
9032
- */
9033
- private function _store_account_addons( $addons, $store = true ) {
9034
- $this->_logger->entrance();
9035
-
9036
- $all_addons = self::get_all_account_addons();
9037
- $all_addons[ $this->_plugin->id ] = $addons;
9038
- self::$_accounts->set_option( 'account_addons', $all_addons, $store );
9039
- }
9040
-
9041
- /**
9042
- * Store account params in the Database.
9043
- *
9044
- * @author Vova Feldman (@svovaf)
9045
- * @since 1.0.1
9046
- */
9047
- private function _store_account() {
9048
- $this->_logger->entrance();
9049
-
9050
- $this->_store_site( false );
9051
- $this->_store_user( false );
9052
- $this->_store_plans( false );
9053
- $this->_store_licenses( false );
9054
-
9055
- self::$_accounts->store();
9056
- }
9057
-
9058
- /**
9059
- * Sync user's information.
9060
- *
9061
- * @author Vova Feldman (@svovaf)
9062
- * @since 1.0.3
9063
- * @uses FS_Api
9064
- */
9065
- private function _handle_account_user_sync() {
9066
- $this->_logger->entrance();
9067
-
9068
- $api = $this->get_api_user_scope();
9069
-
9070
- // Get user's information.
9071
- $user = $api->get( '/', true );
9072
-
9073
- if ( isset( $user->id ) ) {
9074
- $this->_user->first = $user->first;
9075
- $this->_user->last = $user->last;
9076
- $this->_user->email = $user->email;
9077
-
9078
- $is_menu_item_account_visible = $this->is_submenu_item_visible( 'account' );
9079
-
9080
- if ( $user->is_verified &&
9081
- ( ! isset( $this->_user->is_verified ) || false === $this->_user->is_verified )
9082
- ) {
9083
- $this->_user->is_verified = true;
9084
-
9085
- $this->do_action( 'account_email_verified', $user->email );
9086
-
9087
- $this->_admin_notices->add(
9088
- $this->get_text( 'email-verified-message' ),
9089
- $this->get_text( 'right-on' ) . '!',
9090
- 'success',
9091
- // Make admin sticky if account menu item is invisible,
9092
- // since the page will be auto redirected to the plugin's
9093
- // main settings page, and the non-sticky message
9094
- // will disappear.
9095
- ! $is_menu_item_account_visible,
9096
- false,
9097
- 'email_verified'
9098
- );
9099
- }
9100
-
9101
- // Flush user details to DB.
9102
- $this->_store_user();
9103
-
9104
- $this->do_action( 'after_account_user_sync', $user );
9105
-
9106
- /**
9107
- * If account menu item is hidden, redirect to plugin's main settings page.
9108
- *
9109
- * @author Vova Feldman (@svovaf)
9110
- * @since 1.1.6
9111
- *
9112
- * @link https://github.com/Freemius/wordpress-sdk/issues/6
9113
- */
9114
- if ( ! $is_menu_item_account_visible ) {
9115
- fs_redirect( $this->_get_admin_page_url() );
9116
- }
9117
- }
9118
- }
9119
-
9120
- /**
9121
- * @param bool $store
9122
- *
9123
- * @return FS_Plugin_Plan|object|false
9124
- */
9125
- private function _enrich_site_plan( $store = true ) {
9126
- // Try to load plan from local cache.
9127
- $plan = $this->_get_plan_by_id( $this->_site->plan->id );
9128
-
9129
- if ( false === $plan ) {
9130
- $plan = $this->_fetch_site_plan();
9131
- }
9132
-
9133
- if ( $plan instanceof FS_Plugin_Plan ) {
9134
- $this->_update_plan( $plan, $store );
9135
- }
9136
-
9137
- return $plan;
9138
- }
9139
-
9140
- /**
9141
- * @author Vova Feldman (@svovaf)
9142
- * @since 1.0.9
9143
- * @uses FS_Api
9144
- *
9145
- * @param bool $store
9146
- *
9147
- * @return FS_Plugin_Plan|object|false
9148
- */
9149
- private function _enrich_site_trial_plan( $store = true ) {
9150
- // Try to load plan from local cache.
9151
- $trial_plan = $this->_get_plan_by_id( $this->_site->trial_plan_id );
9152
-
9153
- if ( false === $trial_plan ) {
9154
- $trial_plan = $this->_fetch_site_plan( $this->_site->trial_plan_id );
9155
- }
9156
-
9157
- if ( $trial_plan instanceof FS_Plugin_Plan ) {
9158
- $this->_storage->store( 'trial_plan', $trial_plan, $store );
9159
- }
9160
-
9161
- return $trial_plan;
9162
- }
9163
-
9164
- /**
9165
- * @author Vova Feldman (@svovaf)
9166
- * @since 1.0.9
9167
- * @uses FS_Api
9168
- *
9169
- * @param number|bool $license_id
9170
- *
9171
- * @return FS_Subscription|object|bool
9172
- */
9173
- private function _fetch_site_license_subscription( $license_id = false ) {
9174
- $this->_logger->entrance();
9175
- $api = $this->get_api_site_scope();
9176
-
9177
- if ( ! is_numeric( $license_id ) ) {
9178
- $license_id = $this->_license->id;
9179
- }
9180
-
9181
- $result = $api->get( "/licenses/{$license_id}/subscriptions.json", true );
9182
-
9183
- return ! isset( $result->error ) ?
9184
- ( ( is_array( $result->subscriptions ) && 0 < count( $result->subscriptions ) ) ?
9185
- new FS_Subscription( $result->subscriptions[0] ) :
9186
- false
9187
- ) :
9188
- $result;
9189
- }
9190
-
9191
- /**
9192
- * @author Vova Feldman (@svovaf)
9193
- * @since 1.0.4
9194
- * @uses FS_Api
9195
- *
9196
- * @param number|bool $plan_id
9197
- *
9198
- * @return FS_Plugin_Plan|object
9199
- */
9200
- private function _fetch_site_plan( $plan_id = false ) {
9201
- $this->_logger->entrance();
9202
- $api = $this->get_api_site_scope();
9203
-
9204
- if ( ! is_numeric( $plan_id ) ) {
9205
- $plan_id = $this->_site->plan->id;
9206
- }
9207
-
9208
- $plan = $api->get( "/plans/{$plan_id}.json", true );
9209
-
9210
- return ! isset( $plan->error ) ? new FS_Plugin_Plan( $plan ) : $plan;
9211
- }
9212
-
9213
- /**
9214
- * @author Vova Feldman (@svovaf)
9215
- * @since 1.0.5
9216
- * @uses FS_Api
9217
- *
9218
- * @return FS_Plugin_Plan[]|object
9219
- */
9220
- private function _fetch_plugin_plans() {
9221
- $this->_logger->entrance();
9222
- $api = $this->get_api_site_scope();
9223
-
9224
- $result = $api->get( '/plans.json', true );
9225
-
9226
- if ( $this->is_api_result_object( $result, 'plans' ) && is_array( $result->plans ) ) {
9227
- for ( $i = 0, $len = count( $result->plans ); $i < $len; $i ++ ) {
9228
- $result->plans[ $i ] = new FS_Plugin_Plan( $result->plans[ $i ] );
9229
- }
9230
-
9231
- $result = $result->plans;
9232
- }
9233
-
9234
- return $result;
9235
- }
9236
-
9237
- /**
9238
- * @author Vova Feldman (@svovaf)
9239
- * @since 1.0.5
9240
- * @uses FS_Api
9241
- *
9242
- * @param number|bool $plugin_id
9243
- * @param number|bool $site_license_id
9244
- *
9245
- * @return FS_Plugin_License[]|object
9246
- */
9247
- private function _fetch_licenses( $plugin_id = false, $site_license_id = false ) {
9248
- $this->_logger->entrance();
9249
-
9250
- $api = $this->get_api_user_scope();
9251
-
9252
- if ( ! is_numeric( $plugin_id ) ) {
9253
- $plugin_id = $this->_plugin->id;
9254
- }
9255
-
9256
- $result = $api->get( "/plugins/{$plugin_id}/licenses.json", true );
9257
-
9258
- $is_site_license_synced = false;
9259
-
9260
- $api_errors = array();
9261
-
9262
- if ( $this->is_api_result_object( $result, 'licenses' ) &&
9263
- is_array( $result->licenses )
9264
- ) {
9265
- for ( $i = 0, $len = count( $result->licenses ); $i < $len; $i ++ ) {
9266
- $result->licenses[ $i ] = new FS_Plugin_License( $result->licenses[ $i ] );
9267
-
9268
- if ( ( ! $is_site_license_synced ) && is_numeric( $site_license_id ) ) {
9269
- $is_site_license_synced = ( $site_license_id == $result->licenses[ $i ]->id );
9270
- }
9271
- }
9272
-
9273
- $result = $result->licenses;
9274
- } else {
9275
- $api_errors[] = $result;
9276
- $result = array();
9277
- }
9278
-
9279
- if ( ! $is_site_license_synced ) {
9280
- $api = $this->get_api_site_scope();
9281
-
9282
- if ( is_numeric( $site_license_id ) ) {
9283
- // Try to retrieve a foreign license that is linked to the install.
9284
- $api_result = $api->call( '/licenses.json' );
9285
-
9286
- if ( $this->is_api_result_object( $api_result, 'licenses' ) &&
9287
- is_array( $api_result->licenses )
9288
- ) {
9289
- $licenses = $api_result->licenses;
9290
-
9291
- if ( ! empty( $licenses ) ) {
9292
- $result[] = new FS_Plugin_License( $licenses[0] );
9293
- }
9294
- } else {
9295
- $api_errors[] = $api_result;
9296
- }
9297
- } else if ( is_object( $this->_license ) ) {
9298
- // Fetch foreign license by ID and license key.
9299
- $license = $api->get( "/licenses/{$this->_license->id}.json?license_key=" .
9300
- urlencode( $this->_license->secret_key ) );
9301
-
9302
- if ( $this->is_api_result_entity( $license ) ) {
9303
- $result[] = new FS_Plugin_License( $license );
9304
- } else {
9305
- $api_errors[] = $license;
9306
- }
9307
- }
9308
- }
9309
-
9310
- if ( is_array( $result ) && 0 < count( $result ) ) {
9311
- // If found at least one license, return license collection even if there are errors.
9312
- return $result;
9313
- }
9314
-
9315
- if ( ! empty( $api_errors ) ) {
9316
- // If found any errors and no licenses, return first error.
9317
- return $api_errors[0];
9318
- }
9319
-
9320
- // Fallback to empty licenses list.
9321
- return $result;
9322
- }
9323
-
9324
- /**
9325
- * @author Vova Feldman (@svovaf)
9326
- * @since 1.2.0
9327
- * @uses FS_Api
9328
- *
9329
- * @param number|bool $plugin_id
9330
- *
9331
- * @return FS_Payment[]|object
9332
- */
9333
- function _fetch_payments( $plugin_id = false ) {
9334
- $this->_logger->entrance();
9335
-
9336
- $api = $this->get_api_user_scope();
9337
-
9338
- if ( ! is_numeric( $plugin_id ) ) {
9339
- $plugin_id = $this->_plugin->id;
9340
- }
9341
-
9342
- $result = $api->get( "/plugins/{$plugin_id}/payments.json?include_addons=true", true );
9343
-
9344
- if ( ! isset( $result->error ) ) {
9345
- for ( $i = 0, $len = count( $result->payments ); $i < $len; $i ++ ) {
9346
- $result->payments[ $i ] = new FS_Payment( $result->payments[ $i ] );
9347
- }
9348
- $result = $result->payments;
9349
- }
9350
-
9351
- return $result;
9352
- }
9353
-
9354
- /**
9355
- * @author Vova Feldman (@svovaf)
9356
- * @since 1.2.1.5
9357
- * @uses FS_Api
9358
- *
9359
- * @return \FS_Billing|mixed
9360
- */
9361
- function _fetch_billing() {
9362
- require_once WP_FS__DIR_INCLUDES . '/entities/class-fs-billing.php';
9363
-
9364
- $billing = $this->get_api_user_scope()->call( 'billing.json' );
9365
-
9366
- if ( $this->is_api_result_entity( $billing ) ) {
9367
- $billing = new FS_Billing( $billing );
9368
- }
9369
-
9370
- return $billing;
9371
- }
9372
-
9373
- /**
9374
- * @author Vova Feldman (@svovaf)
9375
- * @since 1.0.4
9376
- *
9377
- * @param FS_Plugin_Plan $plan
9378
- * @param bool $store
9379
- */
9380
- private function _update_plan( $plan, $store = false ) {
9381
- $this->_logger->entrance();
9382
-
9383
- $this->_site->plan = $plan;
9384
- $this->_store_site( $store );
9385
- }
9386
-
9387
- /**
9388
- * @author Vova Feldman (@svovaf)
9389
- * @since 1.0.5
9390
- *
9391
- * @param FS_Plugin_License[] $licenses
9392
- * @param string|bool $plugin_slug
9393
- */
9394
- private function _update_licenses( $licenses, $plugin_slug = false ) {
9395
- $this->_logger->entrance();
9396
-
9397
- if ( is_array( $licenses ) ) {
9398
- for ( $i = 0, $len = count( $licenses ); $i < $len; $i ++ ) {
9399
- $licenses[ $i ]->updated = time();
9400
- }
9401
- }
9402
-
9403
- if ( ! is_string( $plugin_slug ) ) {
9404
- $this->_licenses = $licenses;
9405
- }
9406
-
9407
- $this->_store_licenses( true, $plugin_slug, $licenses );
9408
- }
9409
-
9410
- /**
9411
- * @author Vova Feldman (@svovaf)
9412
- * @since 1.0.4
9413
- *
9414
- * @param bool|number $plugin_id
9415
- * @param bool $flush Since 1.1.7.3
9416
- *
9417
- * @return object|false New plugin tag info if exist.
9418
- */
9419
- private function _fetch_newer_version( $plugin_id = false, $flush = true ) {
9420
- $latest_tag = $this->_fetch_latest_version( $plugin_id, $flush );
9421
-
9422
- if ( ! is_object( $latest_tag ) ) {
9423
- return false;
9424
- }
9425
-
9426
- // Check if version is actually newer.
9427
- $has_new_version =
9428
- // If it's an non-installed add-on then always return latest.
9429
- ( $this->_is_addon_id( $plugin_id ) && ! $this->is_addon_activated( $plugin_id ) ) ||
9430
- // Compare versions.
9431
- version_compare( $this->get_plugin_version(), $latest_tag->version, '<' );
9432
-
9433
- $this->_logger->departure( $has_new_version ? 'Found newer plugin version ' . $latest_tag->version : 'No new version' );
9434
-
9435
- return $has_new_version ? $latest_tag : false;
9436
- }
9437
-
9438
- /**
9439
- * @author Vova Feldman (@svovaf)
9440
- * @since 1.0.5
9441
- *
9442
- * @param bool|number $plugin_id
9443
- * @param bool $flush Since 1.1.7.3
9444
- *
9445
- * @return bool|FS_Plugin_Tag
9446
- */
9447
- function get_update( $plugin_id = false, $flush = true ) {
9448
- $this->_logger->entrance();
9449
-
9450
- if ( ! is_numeric( $plugin_id ) ) {
9451
- $plugin_id = $this->_plugin->id;
9452
- }
9453
-
9454
- $this->check_updates( true, $plugin_id, $flush );
9455
- $updates = $this->get_all_updates();
9456
-
9457
- return isset( $updates[ $plugin_id ] ) && is_object( $updates[ $plugin_id ] ) ? $updates[ $plugin_id ] : false;
9458
- }
9459
-
9460
- /**
9461
- * Check if site assigned with active license.
9462
- *
9463
- * @author Vova Feldman (@svovaf)
9464
- * @since 1.0.6
9465
- *
9466
- * @deprecated Please use has_active_valid_license() instead because license can be cancelled.
9467
- */
9468
- function has_active_license() {
9469
- return (
9470
- is_object( $this->_license ) &&
9471
- is_numeric( $this->_license->id ) &&
9472
- ! $this->_license->is_expired()
9473
- );
9474
- }
9475
-
9476
- /**
9477
- * Check if site assigned with active & valid (not expired) license.
9478
- *
9479
- * @author Vova Feldman (@svovaf)
9480
- * @since 1.2.1
9481
- */
9482
- function has_active_valid_license() {
9483
- return (
9484
- is_object( $this->_license ) &&
9485
- is_numeric( $this->_license->id ) &&
9486
- $this->_license->is_active() &&
9487
- $this->_license->is_valid()
9488
- );
9489
- }
9490
-
9491
- /**
9492
- * Check if site assigned with license with enabled features.
9493
- *
9494
- * @author Vova Feldman (@svovaf)
9495
- * @since 1.0.6
9496
- *
9497
- * @return bool
9498
- */
9499
- function has_features_enabled_license() {
9500
- return (
9501
- is_object( $this->_license ) &&
9502
- is_numeric( $this->_license->id ) &&
9503
- $this->_license->is_features_enabled()
9504
- );
9505
- }
9506
-
9507
- /**
9508
- * Check if user is a trial or have feature enabled license.
9509
- *
9510
- * @author Vova Feldman (@svovaf)
9511
- * @since 1.1.7
9512
- *
9513
- * @return bool
9514
- */
9515
- function can_use_premium_code() {
9516
- return $this->is_trial() || $this->has_features_enabled_license();
9517
- }
9518
-
9519
- /**
9520
- * Checks if the current user can activate plugins or switch themes. Note that this method should only be used
9521
- * after the `init` action is triggered because it is using `current_user_can()` which is only functional after
9522
- * the context user is authenticated.
9523
- *
9524
- * @author Leo Fajardo (@leorw)
9525
- * @since 1.2.2
9526
- *
9527
- * @return bool
9528
- */
9529
- function is_user_admin() {
9530
- return current_user_can( 'activate_plugins' );
9531
- }
9532
-
9533
- /**
9534
- * Sync site's plan.
9535
- *
9536
- * @author Vova Feldman (@svovaf)
9537
- * @since 1.0.3
9538
- *
9539
- * @uses FS_Api
9540
- *
9541
- * @param bool $background Hints the method if it's a background sync. If false, it means that was initiated by
9542
- * the admin.
9543
- */
9544
- private function _sync_license( $background = false ) {
9545
- $this->_logger->entrance();
9546
-
9547
- $plugin_id = fs_request_get( 'plugin_id', $this->get_id() );
9548
-
9549
- $is_addon_sync = ( ! $this->_plugin->is_addon() && $plugin_id != $this->get_id() );
9550
-
9551
- if ( $is_addon_sync ) {
9552
- $this->_sync_addon_license( $plugin_id, $background );
9553
- } else {
9554
- $this->_sync_plugin_license( $background );
9555
- }
9556
-
9557
- $this->do_action( 'after_account_plan_sync', $this->_site->plan->name );
9558
- }
9559
-
9560
- /**
9561
- * Sync plugin's add-on license.
9562
- *
9563
- * @author Vova Feldman (@svovaf)
9564
- * @since 1.0.6
9565
- * @uses FS_Api
9566
- *
9567
- * @param number $addon_id
9568
- * @param bool $background
9569
- */
9570
- private function _sync_addon_license( $addon_id, $background ) {
9571
- $this->_logger->entrance();
9572
-
9573
- if ( $this->is_addon_activated( $addon_id ) ) {
9574
- // If already installed, use add-on sync.
9575
- $fs_addon = self::get_instance_by_id( $addon_id );
9576
- $fs_addon->_sync_license( $background );
9577
-
9578
- return;
9579
- }
9580
-
9581
- // Validate add-on exists.
9582
- $addon = $this->get_addon( $addon_id );
9583
-
9584
- if ( ! is_object( $addon ) ) {
9585
- return;
9586
- }
9587
-
9588
- // Add add-on into account add-ons.
9589
- $account_addons = $this->get_account_addons();
9590
- if ( ! is_array( $account_addons ) ) {
9591
- $account_addons = array();
9592
- }
9593
- $account_addons[] = $addon->id;
9594
- $account_addons = array_unique( $account_addons );
9595
- $this->_store_account_addons( $account_addons );
9596
-
9597
- // Load add-on licenses.
9598
- $licenses = $this->_fetch_licenses( $addon->id );
9599
-
9600
- // Sync add-on licenses.
9601
- if ( $this->is_array_instanceof( $licenses, 'FS_Plugin_License' ) ) {
9602
- $this->_update_licenses( $licenses, $addon->slug );
9603
-
9604
- if ( ! $this->is_addon_installed( $addon->slug ) && FS_License_Manager::has_premium_license( $licenses ) ) {
9605
- $plans_result = $this->get_api_site_or_plugin_scope()->get( "/addons/{$addon_id}/plans.json" );
9606
-
9607
- if ( ! isset( $plans_result->error ) ) {
9608
- $plans = array();
9609
- foreach ( $plans_result->plans as $plan ) {
9610
- $plans[] = new FS_Plugin_Plan( $plan );
9611
- }
9612
-
9613
- $this->_admin_notices->add_sticky(
9614
- FS_Plan_Manager::instance()->has_free_plan( $plans ) ?
9615
- sprintf(
9616
- $this->get_text( 'addon-successfully-upgraded-message' ),
9617
- $addon->title
9618
- ) . ' ' . $this->get_latest_download_link(
9619
- $this->get_text( 'download-latest-version' ),
9620
- $addon_id
9621
- )
9622
- :
9623
- sprintf(
9624
- $this->get_text( 'addon-successfully-purchased-message' ),
9625
- $addon->title
9626
- ) . ' ' . $this->get_latest_download_link(
9627
- $this->get_text( 'download-latest-version' ),
9628
- $addon_id
9629
- ),
9630
- 'addon_plan_upgraded_' . $addon->slug,
9631
- $this->get_text( 'yee-haw' ) . '!'
9632
- );
9633
- }
9634
- }
9635
- }
9636
- }
9637
-
9638
- /**
9639
- * Sync site's plugin plan.
9640
- *
9641
- * @author Vova Feldman (@svovaf)
9642
- * @since 1.0.6
9643
- * @uses FS_Api
9644
- *
9645
- * @param bool $background Hints the method if it's a background sync. If false, it means that was initiated by
9646
- * the admin.
9647
- */
9648
- private function _sync_plugin_license( $background = false ) {
9649
- $this->_logger->entrance();
9650
-
9651
- /**
9652
- * Sync site info.
9653
- *
9654
- * @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.
9655
- */
9656
- $site = $this->send_install_update( array(), true );
9657
-
9658
- $plan_change = 'none';
9659
-
9660
- if ( ! $this->is_api_result_entity( $site ) ) {
9661
- // Show API messages only if not background sync or if paying customer.
9662
- if ( ! $background || $this->is_paying() ) {
9663
- // Try to ping API to see if not blocked.
9664
- if ( ! FS_Api::test() ) {
9665
- /**
9666
- * Failed to ping API - blocked!
9667
- *
9668
- * @author Vova Feldman (@svovaf)
9669
- * @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.
9670
- */
9671
- $api = $this->get_api_site_scope();
9672
-
9673
- if ( ! self::$_global_admin_notices->has_sticky( 'api_blocked' ) ) {
9674
- self::$_global_admin_notices->add(
9675
- sprintf(
9676
- $this->get_text( 'server-blocking-access' ),
9677
- $this->get_plugin_name(),
9678
- '<a href="' . $api->get_url() . '" target="_blank">' . $api->get_url() . '</a>'
9679
- ) . '<br> ' . $this->get_text( 'server-error-message' ) . var_export( $site->error, true ),
9680
- $this->get_text( 'oops' ) . '...',
9681
- 'error',
9682
- $background,
9683
- false,
9684
- 'api_blocked'
9685
- );
9686
- }
9687
- } else {
9688
- // Authentication params are broken.
9689
- $this->_admin_notices->add(
9690
- $this->get_text( 'wrong-authentication-param-message' ),
9691
- $this->get_text( 'oops' ) . '...',
9692
- 'error'
9693
- );
9694
- }
9695
- }
9696
-
9697
- // No reason to continue with license sync while there are API issues.
9698
- return;
9699
- }
9700
-
9701
- // Remove sticky API connectivity message.
9702
- self::$_global_admin_notices->remove_sticky( 'api_blocked' );
9703
-
9704
- $site = new FS_Site( $site );
9705
-
9706
- // Sync plans.
9707
- $this->_sync_plans();
9708
-
9709
- if ( ! $this->has_paid_plan() ) {
9710
- $this->_site = $site;
9711
- $this->_enrich_site_plan( true );
9712
- $this->_store_site();
9713
- } else {
9714
- /**
9715
- * Sync licenses. Pass the site's license ID so that the foreign licenses will be fetched if the license
9716
- * associated with that ID is not included in the user's licenses collection.
9717
- */
9718
- $this->_sync_licenses( $site->license_id );
9719
-
9720
- // Check if plan / license changed.
9721
- if ( ! FS_Entity::equals( $site->plan, $this->_site->plan ) ||
9722
- // Check if trial started.
9723
- $site->trial_plan_id != $this->_site->trial_plan_id ||
9724
- $site->trial_ends != $this->_site->trial_ends ||
9725
- // Check if license changed.
9726
- $site->license_id != $this->_site->license_id
9727
- ) {
9728
- if ( $site->is_trial() && ( ! $this->_site->is_trial() || $site->trial_ends != $this->_site->trial_ends ) ) {
9729
- // New trial started.
9730
- $this->_site = $site;
9731
- $plan_change = 'trial_started';
9732
-
9733
- // Store trial plan information.
9734
- $this->_enrich_site_trial_plan( true );
9735
-
9736
- // For trial with subscription use-case.
9737
- $new_license = is_null( $site->license_id ) ? null : $this->_get_license_by_id( $site->license_id );
9738
-
9739
- if ( is_object( $new_license ) && $new_license->is_valid() ) {
9740
- $this->_site = $site;
9741
- $this->_update_site_license( $new_license );
9742
- $this->_store_licenses();
9743
- $this->_enrich_site_plan( true );
9744
-
9745
- $this->_sync_site_subscription( $this->_license );
9746
- }
9747
- } else if ( $this->_site->is_trial() && ! $site->is_trial() && ! is_numeric( $site->license_id ) ) {
9748
- // Was in trial, but now trial expired and no license ID.
9749
- // New trial started.
9750
- $this->_site = $site;
9751
- $plan_change = 'trial_expired';
9752
-
9753
- // Clear trial plan information.
9754
- $this->_storage->trial_plan = null;
9755
-
9756
- } else {
9757
- $is_free = $this->is_free_plan();
9758
-
9759
- // Make sure license exist and not expired.
9760
- $new_license = is_null( $site->license_id ) ?
9761
- null :
9762
- $this->_get_license_by_id( $site->license_id );
9763
-
9764
- if ( $is_free && is_null( $new_license ) && $this->has_any_license() && $this->_license->is_cancelled ) {
9765
- // License cancelled.
9766
- $this->_site = $site;
9767
- $this->_update_site_license( $new_license );
9768
- $this->_store_licenses();
9769
- $this->_enrich_site_plan( true );
9770
-
9771
- $plan_change = 'cancelled';
9772
- } else if ( $is_free && ( ( ! is_object( $new_license ) || $new_license->is_expired() ) ) ) {
9773
- // The license is expired, so ignore upgrade method.
9774
- } else {
9775
- // License changed.
9776
- $this->_site = $site;
9777
- $this->_update_site_license( $new_license );
9778
- $this->_store_licenses();
9779
- $this->_enrich_site_plan( true );
9780
-
9781
- $plan_change = $is_free ?
9782
- 'upgraded' :
9783
- ( is_object( $new_license ) ?
9784
- 'changed' :
9785
- 'downgraded' );
9786
- }
9787
- }
9788
-
9789
- // Store updated site info.
9790
- $this->_store_site();
9791
- } else {
9792
- if ( is_object( $this->_license ) && $this->_license->is_expired() ) {
9793
- if ( ! $this->has_features_enabled_license() ) {
9794
- $this->_deactivate_license();
9795
- $plan_change = 'downgraded';
9796
- } else {
9797
- $plan_change = 'expired';
9798
- }
9799
- }
9800
-
9801
- if ( is_numeric( $site->license_id ) && is_object( $this->_license ) ) {
9802
- $this->_sync_site_subscription( $this->_license );
9803
- }
9804
- }
9805
- }
9806
-
9807
- if ( $this->has_paid_plan() ) {
9808
- switch ( $plan_change ) {
9809
- case 'none':
9810
- if ( ! $background && is_admin() ) {
9811
- $plan = $this->is_trial() ?
9812
- $this->_storage->trial_plan :
9813
- $this->_site->plan;
9814
-
9815
- if ( $plan->is_free() ) {
9816
- $this->_admin_notices->add(
9817
- sprintf(
9818
- $this->get_text( 'plan-did-not-change-message' ),
9819
- '<i><b>' . $plan->title . ( $this->is_trial() ? ' ' . $this->get_text( 'trial' ) : '' ) . '</b></i>'
9820
- ) . ' ' . sprintf(
9821
- '<a href="%s">%s</a>',
9822
- $this->contact_url(
9823
- 'bug',
9824
- sprintf( $this->get_text( 'plan-did-not-change-email-message' ),
9825
- strtoupper( $plan->name )
9826
- )
9827
- ),
9828
- $this->get_text( 'contact-us-here' )
9829
- ),
9830
- $this->get_text( 'hmm' ) . '...'
9831
- );
9832
- }
9833
- }
9834
- break;
9835
- case 'upgraded':
9836
- $this->_admin_notices->add_sticky(
9837
- sprintf(
9838
- $this->get_text( 'plan-upgraded-message' ),
9839
- '<i>' . $this->get_plugin_name() . '</i>'
9840
- ) . $this->get_complete_upgrade_instructions(),
9841
- 'plan_upgraded',
9842
- $this->get_text( 'yee-haw' ) . '!'
9843
- );
9844
-
9845
- $this->_admin_notices->remove_sticky( array(
9846
- 'trial_started',
9847
- 'trial_promotion',
9848
- 'trial_expired',
9849
- 'activation_complete',
9850
- ) );
9851
- break;
9852
- case 'changed':
9853
- $this->_admin_notices->add_sticky(
9854
- sprintf(
9855
- $this->get_text( 'plan-changed-to-x-message' ),
9856
- $this->_site->plan->title
9857
- ),
9858
- 'plan_changed'
9859
- );
9860
-
9861
- $this->_admin_notices->remove_sticky( array(
9862
- 'trial_started',
9863
- 'trial_promotion',
9864
- 'trial_expired',
9865
- 'activation_complete',
9866
- ) );
9867
- break;
9868
- case 'downgraded':
9869
- $this->_admin_notices->add_sticky(
9870
- sprintf( $this->get_text( 'license-expired-blocking-message' ) ),
9871
- 'license_expired',
9872
- $this->get_text( 'hmm' ) . '...'
9873
- );
9874
- $this->_admin_notices->remove_sticky( 'plan_upgraded' );
9875
- break;
9876
- case 'cancelled':
9877
- $this->_admin_notices->add(
9878
- $this->get_text( 'license-cancelled' ) . ' ' .
9879
- sprintf(
9880
- '<a href="%s">%s</a>',
9881
- $this->contact_url( 'bug' ),
9882
- $this->get_text( 'contact-us-here' )
9883
- ),
9884
- $this->get_text( 'hmm' ) . '...',
9885
- 'error'
9886
- );
9887
- $this->_admin_notices->remove_sticky( 'plan_upgraded' );
9888
- break;
9889
- case 'expired':
9890
- $this->_admin_notices->add_sticky(
9891
- sprintf( $this->get_text( 'license-expired-non-blocking-message' ), $this->_site->plan->title ),
9892
- 'license_expired',
9893
- $this->get_text( 'hmm' ) . '...'
9894
- );
9895
- $this->_admin_notices->remove_sticky( 'plan_upgraded' );
9896
- break;
9897
- case 'trial_started':
9898
- $this->_admin_notices->add_sticky(
9899
- sprintf(
9900
- $this->get_text( 'trial-started-message' ),
9901
- '<i>' . $this->get_plugin_name() . '</i>'
9902
- ) . $this->get_complete_upgrade_instructions( $this->_storage->trial_plan->title ),
9903
- 'trial_started',
9904
- $this->get_text( 'yee-haw' ) . '!'
9905
- );
9906
-
9907
- $this->_admin_notices->remove_sticky( array(
9908
- 'trial_promotion',
9909
- ) );
9910
- break;
9911
- case 'trial_expired':
9912
- $this->_admin_notices->add_sticky(
9913
- $this->get_text( 'trial-expired-message' ),
9914
- 'trial_expired',
9915
- $this->get_text( 'hmm' ) . '...'
9916
- );
9917
- $this->_admin_notices->remove_sticky( array(
9918
- 'trial_started',
9919
- 'trial_promotion',
9920
- 'plan_upgraded',
9921
- ) );
9922
- break;
9923
- }
9924
- }
9925
-
9926
- if ( 'none' !== $plan_change ) {
9927
- $this->do_action( 'after_license_change', $plan_change, $this->_site->plan );
9928
- }
9929
- }
9930
-
9931
- /**
9932
- * @author Vova Feldman (@svovaf)
9933
- * @since 1.0.5
9934
- *
9935
- * @param bool $background
9936
- */
9937
- protected function _activate_license( $background = false ) {
9938
- $this->_logger->entrance();
9939
-
9940
- $license_id = fs_request_get( 'license_id' );
9941
-
9942
- if ( FS_Plugin_License::is_valid_id( $license_id ) && $license_id == $this->_site->license_id ) {
9943
- // License is already activated.
9944
- return;
9945
- }
9946
-
9947
- $premium_license = FS_Plugin_License::is_valid_id( $license_id ) ?
9948
- $this->_get_license_by_id( $license_id ) :
9949
- $this->_get_available_premium_license();
9950
-
9951
- if ( ! is_object( $premium_license ) ) {
9952
- return;
9953
- }
9954
-
9955
- /**
9956
- * If the premium license is already associated with the install, just
9957
- * update the license reference (activation is not required).
9958
- *
9959
- * @since 1.1.9
9960
- */
9961
- if ( $premium_license->id == $this->_site->license_id ) {
9962
- // License is already activated.
9963
- $this->_update_site_license( $premium_license );
9964
- $this->_enrich_site_plan( false );
9965
- $this->_store_account();
9966
-
9967
- return;
9968
- }
9969
-
9970
- if ( $this->_site->user_id != $premium_license->user_id ) {
9971
- $api_request_params = array( 'license_key' => $premium_license->secret_key );
9972
- } else {
9973
- $api_request_params = array();
9974
- }
9975
-
9976
- $api = $this->get_api_site_scope();
9977
- $license = $api->call( "/licenses/{$premium_license->id}.json", 'put', $api_request_params );
9978
-
9979
- if ( ! $this->is_api_result_entity( $license ) ) {
9980
- if ( ! $background ) {
9981
- $this->_admin_notices->add( sprintf(
9982
- '%s %s',
9983
- $this->get_text( 'license-activation-failed-message' ),
9984
- ( is_object( $license ) && isset( $license->error ) ?
9985
- $license->error->message :
9986
- sprintf( '%s<br><code>%s</code>',
9987
- $this->get_text( 'server-error-message' ),
9988
- var_export( $license, true )
9989
- )
9990
- )
9991
- ),
9992
- $this->get_text( 'hmm' ) . '...',
9993
- 'error'
9994
- );
9995
- }
9996
-
9997
- return;
9998
- }
9999
-
10000
- $premium_license = new FS_Plugin_License( $license );
10001
-
10002
- // Updated site plan.
10003
- $site = $this->get_api_site_scope()->get( '/', true );
10004
- if ( $this->is_api_result_entity( $site ) ) {
10005
- $this->_site = new FS_Site( $site );
10006
- }
10007
- $this->_update_site_license( $premium_license );
10008
- $this->_enrich_site_plan( false );
10009
-
10010
- $this->_store_account();
10011
-
10012
- if ( ! $background ) {
10013
- $this->_admin_notices->add_sticky(
10014
- $this->get_text( 'license-activated-message' ) .
10015
- $this->get_complete_upgrade_instructions(),
10016
- 'license_activated',
10017
- $this->get_text( 'yee-haw' ) . '!'
10018
- );
10019
- }
10020
-
10021
- $this->_admin_notices->remove_sticky( array(
10022
- 'trial_promotion',
10023
- 'license_expired',
10024
- ) );
10025
- }
10026
-
10027
- /**
10028
- * @author Vova Feldman (@svovaf)
10029
- * @since 1.0.5
10030
- *
10031
- * @param bool $show_notice
10032
- */
10033
- protected function _deactivate_license( $show_notice = true ) {
10034
- $this->_logger->entrance();
10035
-
10036
- if ( ! is_object( $this->_license ) ) {
10037
- $this->_admin_notices->add(
10038
- sprintf( $this->get_text( 'no-active-license-message' ), $this->_site->plan->title ),
10039
- $this->get_text( 'hmm' ) . '...'
10040
- );
10041
-
10042
- return;
10043
- }
10044
-
10045
- $api = $this->get_api_site_scope();
10046
- $license = $api->call( "/licenses/{$this->_site->license_id}.json", 'delete' );
10047
-
10048
- if ( isset( $license->error ) ) {
10049
- $this->_admin_notices->add(
10050
- $this->get_text( 'license-deactivation-failed-message' ) . '<br> ' .
10051
- $this->get_text( 'server-error-message' ) . ' ' . var_export( $license->error, true ),
10052
- $this->get_text( 'hmm' ) . '...',
10053
- 'error'
10054
- );
10055
-
10056
- return;
10057
- }
10058
-
10059
- // Update license cache.
10060
- for ( $i = 0, $len = count( $this->_licenses ); $i < $len; $i ++ ) {
10061
- if ( $license->id == $this->_licenses[ $i ]->id ) {
10062
- $this->_licenses[ $i ] = new FS_Plugin_License( $license );
10063
- }
10064
- }
10065
-
10066
- // Updated site plan to default.
10067
- $this->_sync_plans();
10068
- $this->_site->plan->id = $this->_plans[0]->id;
10069
- // Unlink license from site.
10070
- $this->_update_site_license( null );
10071
- $this->_enrich_site_plan( false );
10072
-
10073
- $this->_store_account();
10074
-
10075
- if ( $show_notice ) {
10076
- $this->_admin_notices->add(
10077
- sprintf( $this->get_text( 'license-deactivation-message' ), $this->_site->plan->title ),
10078
- $this->get_text( 'ok' )
10079
- );
10080
- }
10081
-
10082
- $this->_admin_notices->remove_sticky( array(
10083
- 'plan_upgraded',
10084
- 'license_activated',
10085
- ) );
10086
- }
10087
-
10088
- /**
10089
- * Site plan downgrade.
10090
- *
10091
- * @author Vova Feldman (@svovaf)
10092
- * @since 1.0.4
10093
- *
10094
- * @uses FS_Api
10095
- */
10096
- private function _downgrade_site() {
10097
- $this->_logger->entrance();
10098
-
10099
- $api = $this->get_api_site_scope();
10100
- $site = $api->call( 'downgrade.json', 'put' );
10101
-
10102
- $plan_downgraded = false;
10103
- $plan = false;
10104
- if ( $this->is_api_result_entity( $site ) ) {
10105
- $prev_plan_id = $this->_site->plan->id;
10106
-
10107
- // Update new site plan id.
10108
- $this->_site->plan->id = $site->plan_id;
10109
-
10110
- $plan = $this->_enrich_site_plan();
10111
- $subscription = $this->_sync_site_subscription( $this->_license );
10112
-
10113
- // Plan downgraded if plan was changed or subscription was cancelled.
10114
- $plan_downgraded = ( $plan instanceof FS_Plugin_Plan && $prev_plan_id != $plan->id ) ||
10115
- ( is_object( $subscription ) && ! isset( $subscription->error ) && ! $subscription->is_active() );
10116
- } else {
10117
- // handle different error cases.
10118
-
10119
- }
10120
-
10121
- if ( $plan_downgraded ) {
10122
- // Remove previous sticky message about upgrade (if exist).
10123
- $this->_admin_notices->remove_sticky( 'plan_upgraded' );
10124
-
10125
- $this->_admin_notices->add(
10126
- sprintf( $this->get_text( 'plan-x-downgraded-message' ),
10127
- $plan->title,
10128
- human_time_diff( time(), strtotime( $this->_license->expiration ) )
10129
- )
10130
- );
10131
-
10132
- // Store site updates.
10133
- $this->_store_site();
10134
- } else {
10135
- $this->_admin_notices->add(
10136
- $this->get_text( 'plan-downgraded-failure-message' ),
10137
- $this->get_text( 'oops' ) . '...',
10138
- 'error'
10139
- );
10140
- }
10141
- }
10142
-
10143
- /**
10144
- * @author Vova Feldman (@svovaf)
10145
- * @since 1.1.8.1
10146
- *
10147
- * @param bool|string $plan_name
10148
- *
10149
- * @return bool If trial was successfully started.
10150
- */
10151
- function start_trial( $plan_name = false ) {
10152
- $this->_logger->entrance();
10153
-
10154
- if ( $this->is_trial() ) {
10155
- // Already in trial mode.
10156
- $this->_admin_notices->add(
10157
- $this->get_text( 'in-trial-mode' ),
10158
- $this->get_text( 'oops' ) . '...',
10159
- 'error'
10160
- );
10161
-
10162
- return false;
10163
- }
10164
-
10165
- if ( $this->_site->is_trial_utilized() ) {
10166
- // Trial was already utilized.
10167
- $this->_admin_notices->add(
10168
- $this->get_text( 'trial-utilized' ),
10169
- $this->get_text( 'oops' ) . '...',
10170
- 'error'
10171
- );
10172
-
10173
- return false;
10174
- }
10175
-
10176
- if ( false !== $plan_name ) {
10177
- $plan = $this->get_plan_by_name( $plan_name );
10178
-
10179
- if ( false === $plan ) {
10180
- // Plan doesn't exist.
10181
- $this->_admin_notices->add(
10182
- sprintf( $this->get_text( 'trial-plan-x-not-exist' ), $plan_name ),
10183
- $this->get_text( 'oops' ) . '...',
10184
- 'error'
10185
- );
10186
-
10187
- return false;
10188
- }
10189
-
10190
- if ( ! $plan->has_trial() ) {
10191
- // Plan doesn't exist.
10192
- $this->_admin_notices->add(
10193
- sprintf( $this->get_text( 'plan-x-no-trial' ), $plan_name ),
10194
- $this->get_text( 'oops' ) . '...',
10195
- 'error'
10196
- );
10197
-
10198
- return false;
10199
- }
10200
- } else {
10201
- if ( ! $this->has_trial_plan() ) {
10202
- // None of the plans have a trial.
10203
- $this->_admin_notices->add(
10204
- $this->get_text( 'no-trials' ),
10205
- $this->get_text( 'oops' ) . '...',
10206
- 'error'
10207
- );
10208
-
10209
- return false;
10210
- }
10211
-
10212
- $plans_with_trial = FS_Plan_Manager::instance()->get_trial_plans( $this->_plans );
10213
-
10214
- $plan = $plans_with_trial[0];
10215
- }
10216
-
10217
- $api = $this->get_api_site_scope();
10218
- $plan = $api->call( "plans/{$plan->id}/trials.json", 'post' );
10219
-
10220
- if ( ! $this->is_api_result_entity( $plan ) ) {
10221
- // Some API error while trying to start the trial.
10222
- $this->_admin_notices->add(
10223
- $this->get_text( 'unexpected-api-error' ) . ' ' . var_export( $plan, true ),
10224
- $this->get_text( 'oops' ) . '...',
10225
- 'error'
10226
- );
10227
-
10228
- return false;
10229
- }
10230
-
10231
- // Sync license.
10232
- $this->_sync_license();
10233
-
10234
- return $this->is_trial();
10235
- }
10236
-
10237
- /**
10238
- * Cancel site trial.
10239
- *
10240
- * @author Vova Feldman (@svovaf)
10241
- * @since 1.0.9
10242
- *
10243
- * @uses FS_Api
10244
- */
10245
- private function _cancel_trial() {
10246
- $this->_logger->entrance();
10247
-
10248
- if ( ! $this->is_trial() ) {
10249
- $this->_admin_notices->add(
10250
- $this->get_text( 'trial-cancel-no-trial-message' ),
10251
- $this->get_text( 'oops' ) . '...',
10252
- 'error'
10253
- );
10254
-
10255
- return;
10256
- }
10257
-
10258
- $api = $this->get_api_site_scope();
10259
- $site = $api->call( 'trials.json', 'delete' );
10260
-
10261
- $trial_cancelled = false;
10262
-
10263
- if ( $this->is_api_result_entity( $site ) ) {
10264
- $prev_trial_ends = $this->_site->trial_ends;
10265
-
10266
- if ( $this->is_paid_trial() ) {
10267
- $this->_license->expiration = $site->trial_ends;
10268
- $this->_license->is_cancelled = true;
10269
- $this->_update_site_license( $this->_license );
10270
- $this->_store_licenses();
10271
-
10272
- // Clear subscription reference.
10273
- $this->_sync_site_subscription( null );
10274
- }
10275
-
10276
- // Update site info.
10277
- $this->_site = new FS_Site( $site );
10278
- $this->_enrich_site_plan();
10279
-
10280
- $trial_cancelled = ( $prev_trial_ends != $site->trial_ends );
10281
- } else {
10282
- // handle different error cases.
10283
-
10284
- }
10285
-
10286
- if ( $trial_cancelled ) {
10287
- // Remove previous sticky messages about upgrade or trial (if exist).
10288
- $this->_admin_notices->remove_sticky( array(
10289
- 'trial_started',
10290
- 'trial_promotion',
10291
- 'plan_upgraded',
10292
- ) );
10293
-
10294
- // Store site updates.
10295
- $this->_store_site();
10296
-
10297
- if ( ! $this->is_addon() ||
10298
- ! $this->deactivate_premium_only_addon_without_license( true )
10299
- ) {
10300
- $this->_admin_notices->add(
10301
- sprintf( $this->get_text( 'trial-cancel-message' ), $this->_storage->trial_plan->title )
10302
- );
10303
- }
10304
-
10305
- // Clear trial plan information.
10306
- unset( $this->_storage->trial_plan );
10307
- } else {
10308
- $this->_admin_notices->add(
10309
- $this->get_text( 'trial-cancel-failure-message' ),
10310
- $this->get_text( 'oops' ) . '...',
10311
- 'error'
10312
- );
10313
- }
10314
- }
10315
-
10316
- /**
10317
- * @author Vova Feldman (@svovaf)
10318
- * @since 1.0.6
10319
- *
10320
- * @param bool|number $plugin_id
10321
- *
10322
- * @return bool
10323
- */
10324
- private function _is_addon_id( $plugin_id ) {
10325
- return is_numeric( $plugin_id ) && ( $this->get_id() != $plugin_id );
10326
- }
10327
-
10328
- /**
10329
- * Check if user eligible to download premium version updates.
10330
- *
10331
- * @author Vova Feldman (@svovaf)
10332
- * @since 1.0.6
10333
- *
10334
- * @return bool
10335
- */
10336
- private function _can_download_premium() {
10337
- return $this->has_active_valid_license() ||
10338
- ( $this->is_trial() && ! $this->get_trial_plan()->is_free() );
10339
- }
10340
-
10341
- /**
10342
- *
10343
- * @author Vova Feldman (@svovaf)
10344
- * @since 1.0.6
10345
- *
10346
- * @param bool|number $addon_id
10347
- * @param string $type "json" or "zip"
10348
- *
10349
- * @return string
10350
- */
10351
- private function _get_latest_version_endpoint( $addon_id = false, $type = 'json' ) {
10352
-
10353
- $is_addon = $this->_is_addon_id( $addon_id );
10354
-
10355
- $is_premium = null;
10356
- if ( ! $is_addon ) {
10357
- $is_premium = $this->_can_download_premium();
10358
- } else if ( $this->is_addon_activated( $addon_id ) ) {
10359
- $is_premium = self::get_instance_by_id( $addon_id )->_can_download_premium();
10360
- }
10361
-
10362
- // If add-on, then append add-on ID.
10363
- $endpoint = ( $is_addon ? "/addons/$addon_id" : '' ) .
10364
- '/updates/latest.' . $type;
10365
-
10366
- // If add-on and not yet activated, try to fetch based on server licensing.
10367
- if ( is_bool( $is_premium ) ) {
10368
- $endpoint = add_query_arg( 'is_premium', json_encode( $is_premium ), $endpoint );
10369
- }
10370
-
10371
- if ( $this->has_secret_key() ) {
10372
- $endpoint = add_query_arg( 'type', 'all', $endpoint );
10373
- }
10374
-
10375
- return $endpoint;
10376
- }
10377
-
10378
- /**
10379
- * @author Vova Feldman (@svovaf)
10380
- * @since 1.0.4
10381
- *
10382
- * @param bool|number $addon_id
10383
- * @param bool $flush Since 1.1.7.3
10384
- *
10385
- * @return object|false Plugin latest tag info.
10386
- */
10387
- function _fetch_latest_version( $addon_id = false, $flush = true ) {
10388
- $this->_logger->entrance();
10389
-
10390
- /**
10391
- * @since 1.1.7.3 Check for plugin updates from Freemius only if opted-in.
10392
- * @since 1.1.7.4 Also check updates for add-ons.
10393
- */
10394
- if ( ! $this->is_registered() &&
10395
- ! $this->_is_addon_id( $addon_id )
10396
- ) {
10397
- return false;
10398
- }
10399
-
10400
- $tag = $this->get_api_site_or_plugin_scope()->get(
10401
- $this->_get_latest_version_endpoint( $addon_id, 'json' ),
10402
- $flush
10403
- );
10404
-
10405
- $latest_version = ( is_object( $tag ) && isset( $tag->version ) ) ? $tag->version : 'couldn\'t get';
10406
-
10407
- $this->_logger->departure( 'Latest version ' . $latest_version );
10408
-
10409
- return ( is_object( $tag ) && isset( $tag->version ) ) ? $tag : false;
10410
- }
10411
-
10412
- #----------------------------------------------------------------------------------
10413
- #region Download Plugin
10414
- #----------------------------------------------------------------------------------
10415
-
10416
- /**
10417
- * Download latest plugin version, based on plan.
10418
- *
10419
- * Not like _download_latest(), this will redirect the page
10420
- * to secure download url to prevent dual download (from FS to WP server,
10421
- * and then from WP server to the client / browser).
10422
- *
10423
- * @author Vova Feldman (@svovaf)
10424
- * @since 1.0.9
10425
- *
10426
- * @param bool|number $plugin_id
10427
- *
10428
- * @uses FS_Api
10429
- * @uses wp_redirect()
10430
- */
10431
- private function download_latest_directly( $plugin_id = false ) {
10432
- $this->_logger->entrance();
10433
-
10434
- wp_redirect( $this->get_latest_download_api_url( $plugin_id ) );
10435
- }
10436
-
10437
- /**
10438
- * Get latest plugin FS API download URL.
10439
- *
10440
- * @author Vova Feldman (@svovaf)
10441
- * @since 1.0.9
10442
- *
10443
- * @param bool|number $plugin_id
10444
- *
10445
- * @return string
10446
- */
10447
- private function get_latest_download_api_url( $plugin_id = false ) {
10448
- $this->_logger->entrance();
10449
-
10450
- return $this->get_api_site_scope()->get_signed_url(
10451
- $this->_get_latest_version_endpoint( $plugin_id, 'zip' )
10452
- );
10453
- }
10454
-
10455
- /**
10456
- * Get payment invoice URL.
10457
- *
10458
- * @author Vova Feldman (@svovaf)
10459
- * @since 1.2.0
10460
- *
10461
- * @param bool|number $payment_id
10462
- *
10463
- * @return string
10464
- */
10465
- function _get_invoice_api_url( $payment_id = false ) {
10466
- $this->_logger->entrance();
10467
-
10468
- return $this->get_api_user_scope()->get_signed_url(
10469
- "/payments/{$payment_id}/invoice.pdf"
10470
- );
10471
- }
10472
-
10473
- /**
10474
- * Get latest plugin download link.
10475
- *
10476
- * @author Vova Feldman (@svovaf)
10477
- * @since 1.0.9
10478
- *
10479
- * @param string $label
10480
- * @param bool|number $plugin_id
10481
- *
10482
- * @return string
10483
- */
10484
- private function get_latest_download_link( $label, $plugin_id = false ) {
10485
- return sprintf(
10486
- '<a target="_blank" href="%s">%s</a>',
10487
- $this->_get_latest_download_local_url( $plugin_id ),
10488
- $label
10489
- );
10490
- }
10491
-
10492
- /**
10493
- * Get latest plugin download local URL.
10494
- *
10495
- * @author Vova Feldman (@svovaf)
10496
- * @since 1.0.9
10497
- *
10498
- * @param bool|number $plugin_id
10499
- *
10500
- * @return string
10501
- */
10502
- function _get_latest_download_local_url( $plugin_id = false ) {
10503
- // Add timestamp to protect from caching.
10504
- $params = array( 'ts' => WP_FS__SCRIPT_START_TIME );
10505
-
10506
- if ( ! empty( $plugin_id ) ) {
10507
- $params['plugin_id'] = $plugin_id;
10508
- }
10509
-
10510
- return $this->get_account_url( 'download_latest', $params );
10511
- }
10512
-
10513
- #endregion Download Plugin ------------------------------------------------------------------
10514
-
10515
- /**
10516
- * @author Vova Feldman (@svovaf)
10517
- * @since 1.0.4
10518
- *
10519
- * @uses FS_Api
10520
- *
10521
- * @param bool $background Hints the method if it's a background updates check. If false, it means that
10522
- * was initiated by the admin.
10523
- * @param bool|number $plugin_id
10524
- * @param bool $flush Since 1.1.7.3
10525
- */
10526
- private function check_updates( $background = false, $plugin_id = false, $flush = true ) {
10527
- $this->_logger->entrance();
10528
-
10529
- // Check if there's a newer version for download.
10530
- $new_version = $this->_fetch_newer_version( $plugin_id, $flush );
10531
-
10532
- $update = null;
10533
- if ( is_object( $new_version ) ) {
10534
- $update = new FS_Plugin_Tag( $new_version );
10535
-
10536
- if ( ! $background ) {
10537
- $this->_admin_notices->add(
10538
- sprintf(
10539
- $this->get_text( 'version-x-released' ) . ' ' . $this->get_text( 'please-download-x' ),
10540
- $update->version,
10541
- sprintf(
10542
- '<a href="%s" target="_blank">%s</a>',
10543
- $this->get_account_url( 'download_latest' ),
10544
- sprintf( $this->get_text( 'latest-x-version' ), $this->_site->plan->title )
10545
- )
10546
- ),
10547
- $this->get_text( 'new' ) . '!'
10548
- );
10549
- }
10550
- } else if ( false === $new_version && ! $background ) {
10551
- $this->_admin_notices->add(
10552
- $this->get_text( 'you-have-latest' ),
10553
- $this->get_text( 'you-are-good' )
10554
- );
10555
- }
10556
-
10557
- $this->_store_update( $update, true, $plugin_id );
10558
- }
10559
-
10560
- /**
10561
- * @author Vova Feldman (@svovaf)
10562
- * @since 1.0.4
10563
- *
10564
- * @param bool $flush Since 1.1.7.3 add 24 hour cache by default.
10565
- *
10566
- * @return FS_Plugin[]
10567
- *
10568
- * @uses FS_Api
10569
- */
10570
- private function sync_addons( $flush = false ) {
10571
- $this->_logger->entrance();
10572
-
10573
- $api = $this->get_api_site_or_plugin_scope();
10574
-
10575
- /**
10576
- * @since 1.2.1
10577
- *
10578
- * If there's a cached version of the add-ons and not asking
10579
- * for a flush, just use the currently stored add-ons.
10580
- */
10581
- if ( ! $flush && $api->is_cached( '/addons.json?enriched=true' ) ) {
10582
- $addons = self::get_all_addons();
10583
-
10584
- return $addons[ $this->_plugin->id ];
10585
- }
10586
-
10587
- $result = $api->get( '/addons.json?enriched=true', $flush );
10588
-
10589
- $addons = array();
10590
- if ( $this->is_api_result_object( $result, 'plugins' ) &&
10591
- is_array( $result->plugins )
10592
- ) {
10593
- for ( $i = 0, $len = count( $result->plugins ); $i < $len; $i ++ ) {
10594
- $addons[ $i ] = new FS_Plugin( $result->plugins[ $i ] );
10595
- }
10596
-
10597
- $this->_store_addons( $addons, true );
10598
- }
10599
-
10600
- return $addons;
10601
- }
10602
-
10603
- /**
10604
- * Handle user email update.
10605
- *
10606
- * @author Vova Feldman (@svovaf)
10607
- * @since 1.0.3
10608
- * @uses FS_Api
10609
- *
10610
- * @param string $new_email
10611
- *
10612
- * @return object
10613
- */
10614
- private function update_email( $new_email ) {
10615
- $this->_logger->entrance();
10616
-
10617
-
10618
- $api = $this->get_api_user_scope();
10619
- $user = $api->call( "?plugin_id={$this->_plugin->id}&fields=id,email,is_verified", 'put', array(
10620
- 'email' => $new_email,
10621
- 'after_email_confirm_url' => $this->_get_admin_page_url(
10622
- 'account',
10623
- array( 'fs_action' => 'sync_user' )
10624
- ),
10625
- ) );
10626
-
10627
- if ( ! isset( $user->error ) ) {
10628
- $this->_user->email = $user->email;
10629
- $this->_user->is_verified = $user->is_verified;
10630
- $this->_store_user();
10631
- } else {
10632
- // handle different error cases.
10633
-
10634
- }
10635
-
10636
- return $user;
10637
- }
10638
-
10639
- #----------------------------------------------------------------------------------
10640
- #region API Error Handling
10641
- #----------------------------------------------------------------------------------
10642
-
10643
- /**
10644
- * @author Vova Feldman (@svovaf)
10645
- * @since 1.1.1
10646
- *
10647
- * @param mixed $result
10648
- *
10649
- * @return bool Is API result contains an error.
10650
- */
10651
- private function is_api_error( $result ) {
10652
- return FS_Api::is_api_error( $result );
10653
- }
10654
-
10655
- /**
10656
- * Checks if given API result is a non-empty and not an error object.
10657
- *
10658
- * @author Vova Feldman (@svovaf)
10659
- * @since 1.2.1.5
10660
- *
10661
- * @param mixed $result
10662
- * @param string|null $required_property Optional property we want to verify that is set.
10663
- *
10664
- * @return bool
10665
- */
10666
- function is_api_result_object( $result, $required_property = null ) {
10667
- return FS_Api::is_api_result_object( $result, $required_property );
10668
- }
10669
-
10670
- /**
10671
- * Checks if given API result is a non-empty entity object with non-empty ID.
10672
- *
10673
- * @author Vova Feldman (@svovaf)
10674
- * @since 1.2.1.5
10675
- *
10676
- * @param mixed $result
10677
- *
10678
- * @return bool
10679
- */
10680
- private function is_api_result_entity( $result ) {
10681
- return FS_Api::is_api_result_entity( $result );
10682
- }
10683
-
10684
- #endregion
10685
-
10686
- /**
10687
- * Make sure a given argument is an array of a specific type.
10688
- *
10689
- * @author Vova Feldman (@svovaf)
10690
- * @since 1.2.1.5
10691
- *
10692
- * @param mixed $array
10693
- * @param string $class
10694
- *
10695
- * @return bool
10696
- */
10697
- private function is_array_instanceof( $array, $class ) {
10698
- return ( is_array( $array ) && ( empty( $array ) || $array[0] instanceof $class ) );
10699
- }
10700
-
10701
- /**
10702
- * Start install ownership change.
10703
- *
10704
- * @author Vova Feldman (@svovaf)
10705
- * @since 1.1.1
10706
- * @uses FS_Api
10707
- *
10708
- * @param string $candidate_email
10709
- *
10710
- * @return bool Is ownership change successfully initiated.
10711
- */
10712
- private function init_change_owner( $candidate_email ) {
10713
- $this->_logger->entrance();
10714
-
10715
- $api = $this->get_api_site_scope();
10716
- $result = $api->call( "/users/{$this->_user->id}.json", 'put', array(
10717
- 'email' => $candidate_email,
10718
- 'after_confirm_url' => $this->_get_admin_page_url(
10719
- 'account',
10720
- array( 'fs_action' => 'change_owner' )
10721
- ),
10722
- ) );
10723
-
10724
- return ! $this->is_api_error( $result );
10725
- }
10726
-
10727
- /**
10728
- * Handle install ownership change.
10729
- *
10730
- * @author Vova Feldman (@svovaf)
10731
- * @since 1.1.1
10732
- * @uses FS_Api
10733
- *
10734
- * @return bool Was ownership change successfully complete.
10735
- */
10736
- private function complete_change_owner() {
10737
- $this->_logger->entrance();
10738
-
10739
- $site_result = $this->get_api_site_scope( true )->get();
10740
- $site = new FS_Site( $site_result );
10741
- $this->_site = $site;
10742
-
10743
- $user = new FS_User();
10744
- $user->id = fs_request_get( 'user_id' );
10745
-
10746
- // Validate install's user and given user.
10747
- if ( $user->id != $this->_site->user_id ) {
10748
- return false;
10749
- }
10750
-
10751
- $user->public_key = fs_request_get( 'user_public_key' );
10752
- $user->secret_key = fs_request_get( 'user_secret_key' );
10753
-
10754
- // Fetch new user information.
10755
- $this->_user = $user;
10756
- $user_result = $this->get_api_user_scope( true )->get();
10757
- $user = new FS_User( $user_result );
10758
- $this->_user = $user;
10759
-
10760
- $this->_set_account( $user, $site );
10761
-
10762
- return true;
10763
- }
10764
-
10765
- /**
10766
- * Handle user name update.
10767
- *
10768
- * @author Vova Feldman (@svovaf)
10769
- * @since 1.0.9
10770
- * @uses FS_Api
10771
- *
10772
- * @return object
10773
- */
10774
- private function update_user_name() {
10775
- $this->_logger->entrance();
10776
- $name = fs_request_get( 'fs_user_name_' . $this->_slug, '' );
10777
-
10778
- $api = $this->get_api_user_scope();
10779
- $user = $api->call( "?plugin_id={$this->_plugin->id}&fields=id,first,last", 'put', array(
10780
- 'name' => $name,
10781
- ) );
10782
-
10783
- if ( ! isset( $user->error ) ) {
10784
- $this->_user->first = $user->first;
10785
- $this->_user->last = $user->last;
10786
- $this->_store_user();
10787
- } else {
10788
- // handle different error cases.
10789
-
10790
- }
10791
-
10792
- return $user;
10793
- }
10794
-
10795
- /**
10796
- * Verify user email.
10797
- *
10798
- * @author Vova Feldman (@svovaf)
10799
- * @since 1.0.3
10800
- * @uses FS_Api
10801
- */
10802
- private function verify_email() {
10803
- $this->_handle_account_user_sync();
10804
-
10805
- if ( $this->_user->is_verified() ) {
10806
- return;
10807
- }
10808
-
10809
- $api = $this->get_api_site_scope();
10810
- $result = $api->call( "/users/{$this->_user->id}/verify.json", 'put', array(
10811
- 'after_email_confirm_url' => $this->_get_admin_page_url(
10812
- 'account',
10813
- array( 'fs_action' => 'sync_user' )
10814
- )
10815
- ) );
10816
-
10817
- if ( ! isset( $result->error ) ) {
10818
- $this->_admin_notices->add( sprintf(
10819
- $this->get_text( 'verification-email-sent-message' ),
10820
- sprintf( '<a href="mailto:%1s">%2s</a>', esc_url( $this->_user->email ), $this->_user->email )
10821
- ) );
10822
- } else {
10823
- // handle different error cases.
10824
-
10825
- }
10826
- }
10827
-
10828
- /**
10829
- * @author Vova Feldman (@svovaf)
10830
- * @since 1.1.2
10831
- *
10832
- * @param array $params
10833
- *
10834
- * @return string
10835
- */
10836
- private function get_activation_url( $params = array() ) {
10837
- if ( $this->is_addon() ) {
10838
- /**
10839
- * @author Vova Feldman (@svovaf)
10840
- * @since 1.2.1.7 Add-on's activation is the parent's module activation.
10841
- */
10842
- return $this->get_parent_instance()->get_activation_url( $params );
10843
- }
10844
-
10845
- return $this->apply_filters( 'connect_url', $this->_get_admin_page_url( '', $params ) );
10846
- }
10847
-
10848
- /**
10849
- * @author Vova Feldman (@svovaf)
10850
- * @since 1.2.1.5
10851
- *
10852
- * @param array $params
10853
- *
10854
- * @return string
10855
- */
10856
- function get_reconnect_url( $params = array() ) {
10857
- $params['fs_action'] = 'reset_anonymous_mode';
10858
- $params['fs_slug'] = $this->_slug;
10859
-
10860
- return $this->apply_filters( 'connect_url', $this->_get_admin_page_url( '', $params ) );
10861
- }
10862
-
10863
- /**
10864
- * Get the URL of the page that should be loaded after the user connect or skip in the opt-in screen.
10865
- *
10866
- * @author Vova Feldman (@svovaf)
10867
- * @since 1.1.3
10868
- *
10869
- * @param string $filter Filter name.
10870
- *
10871
- * @return string
10872
- */
10873
- function get_after_activation_url( $filter ) {
10874
- $first_time_path = $this->_menu->get_first_time_path();
10875
-
10876
- return $this->apply_filters(
10877
- $filter,
10878
- empty( $first_time_path ) ?
10879
- $this->_get_admin_page_url() :
10880
- $first_time_path
10881
- );
10882
- }
10883
-
10884
- /**
10885
- * Handle account page updates / edits / actions.
10886
- *
10887
- * @author Vova Feldman (@svovaf)
10888
- * @since 1.0.2
10889
- *
10890
- */
10891
- private function _handle_account_edits() {
10892
- if ( ! $this->is_user_admin() ) {
10893
- return;
10894
- }
10895
-
10896
- $plugin_id = fs_request_get( 'plugin_id', $this->get_id() );
10897
- $action = fs_get_action();
10898
-
10899
- switch ( $action ) {
10900
- case 'delete_account':
10901
- check_admin_referer( $action );
10902
-
10903
- if ( $plugin_id == $this->get_id() ) {
10904
- $this->delete_account_event();
10905
-
10906
- // Clear user and site.
10907
- $this->_site = null;
10908
- $this->_user = null;
10909
-
10910
- fs_redirect( $this->get_activation_url() );
10911
- } else {
10912
- if ( $this->is_addon_activated( $plugin_id ) ) {
10913
- $fs_addon = self::get_instance_by_id( $plugin_id );
10914
- $fs_addon->delete_account_event();
10915
-
10916
- fs_redirect( $this->_get_admin_page_url( 'account' ) );
10917
- }
10918
- }
10919
-
10920
- return;
10921
-
10922
- case 'downgrade_account':
10923
- check_admin_referer( $action );
10924
-
10925
- if ( $plugin_id == $this->get_id() ) {
10926
- $this->_downgrade_site();
10927
- } else if ( $this->is_addon_activated( $plugin_id ) ) {
10928
- $fs_addon = self::get_instance_by_id( $plugin_id );
10929
- $fs_addon->_downgrade_site();
10930
- }
10931
-
10932
- return;
10933
-
10934
- case 'activate_license':
10935
- check_admin_referer( $action );
10936
-
10937
- if ( $plugin_id == $this->get_id() ) {
10938
- $this->_activate_license();
10939
- } else {
10940
- if ( $this->is_addon_activated( $plugin_id ) ) {
10941
- $fs_addon = self::get_instance_by_id( $plugin_id );
10942
- $fs_addon->_activate_license();
10943
- }
10944
- }
10945
-
10946
- return;
10947
-
10948
- case 'deactivate_license':
10949
- check_admin_referer( $action );
10950
-
10951
- if ( $plugin_id == $this->get_id() ) {
10952
- $this->_deactivate_license();
10953
- } else {
10954
- if ( $this->is_addon_activated( $plugin_id ) ) {
10955
- $fs_addon = self::get_instance_by_id( $plugin_id );
10956
- $fs_addon->_deactivate_license();
10957
- }
10958
- }
10959
-
10960
- return;
10961
-
10962
- case 'check_updates':
10963
- check_admin_referer( $action );
10964
- $this->check_updates();
10965
-
10966
- return;
10967
-
10968
- case 'change_owner':
10969
- $state = fs_request_get( 'state', 'init' );
10970
- switch ( $state ) {
10971
- case 'init':
10972
- $candidate_email = fs_request_get( 'candidate_email', '' );
10973
-
10974
- if ( $this->init_change_owner( $candidate_email ) ) {
10975
- $this->_admin_notices->add( sprintf( $this->get_text( 'change-owner-request-sent-x' ), '<b>' . $this->_user->email . '</b>' ) );
10976
- }
10977
- break;
10978
- case 'owner_confirmed':
10979
- $candidate_email = fs_request_get( 'candidate_email', '' );
10980
-
10981
- $this->_admin_notices->add( sprintf( $this->get_text( 'change-owner-request_owner-confirmed' ), '<b>' . $candidate_email . '</b>' ) );
10982
- break;
10983
- case 'candidate_confirmed':
10984
- if ( $this->complete_change_owner() ) {
10985
- $this->_admin_notices->add_sticky(
10986
- sprintf( $this->get_text( 'change-owner-request_candidate-confirmed' ), '<b>' . $this->_user->email . '</b>' ),
10987
- 'ownership_changed',
10988
- $this->get_text( 'congrats' ) . '!'
10989
- );
10990
- } else {
10991
- // @todo Handle failed ownership change message.
10992
- }
10993
- break;
10994
- }
10995
-
10996
- return;
10997
-
10998
- case 'update_email':
10999
- check_admin_referer( 'update_email' );
11000
-
11001
- $new_email = fs_request_get( 'fs_email_' . $this->_slug, '' );
11002
- $result = $this->update_email( $new_email );
11003
-
11004
- if ( isset( $result->error ) ) {
11005
- switch ( $result->error->code ) {
11006
- case 'user_exist':
11007
- $this->_admin_notices->add(
11008
- $this->get_text( 'user-exist-message' ) . ' ' .
11009
- sprintf( $this->get_text( 'user-exist-message_ownership' ), '<b>' . $new_email . '</b>' ) .
11010
- sprintf(
11011
- '<a style="margin-left: 10px;" href="%s"><button class="button button-primary">%s &nbsp;&#10140;</button></a>',
11012
- $this->get_account_url( 'change_owner', array(
11013
- 'state' => 'init',
11014
- 'candidate_email' => $new_email
11015
- ) ),
11016
- $this->get_text( 'change-ownership' )
11017
- ),
11018
- $this->get_text( 'oops' ) . '...',
11019
- 'error'
11020
- );
11021
- break;
11022
- }
11023
- } else {
11024
- $this->_admin_notices->add( $this->get_text( 'email-updated-message' ) );
11025
- }
11026
-
11027
- return;
11028
-
11029
- case 'update_user_name':
11030
- check_admin_referer( 'update_user_name' );
11031
-
11032
- $result = $this->update_user_name();
11033
-
11034
- if ( isset( $result->error ) ) {
11035
- $this->_admin_notices->add(
11036
- $this->get_text( 'name-update-failed-message' ),
11037
- $this->get_text( 'oops' ) . '...',
11038
- 'error'
11039
- );
11040
- } else {
11041
- $this->_admin_notices->add( $this->get_text( 'name-updated-message' ) );
11042
- }
11043
-
11044
- return;
11045
-
11046
- #region Actions that might be called from external links (e.g. email)
11047
-
11048
- case 'cancel_trial':
11049
- if ( $plugin_id == $this->get_id() ) {
11050
- $this->_cancel_trial();
11051
- } else {
11052
- if ( $this->is_addon_activated( $plugin_id ) ) {
11053
- $fs_addon = self::get_instance_by_id( $plugin_id );
11054
- $fs_addon->_cancel_trial();
11055
- }
11056
- }
11057
-
11058
- return;
11059
-
11060
- case 'verify_email':
11061
- $this->verify_email();
11062
-
11063
- return;
11064
-
11065
- case 'sync_user':
11066
- $this->_handle_account_user_sync();
11067
-
11068
- return;
11069
-
11070
- case $this->_slug . '_sync_license':
11071
- $this->_sync_license();
11072
-
11073
- return;
11074
-
11075
- case 'download_latest':
11076
- $this->download_latest_directly( $plugin_id );
11077
-
11078
- return;
11079
-
11080
- #endregion
11081
- }
11082
-
11083
- if ( WP_FS__IS_POST_REQUEST ) {
11084
- $properties = array( 'site_secret_key', 'site_id', 'site_public_key' );
11085
- foreach ( $properties as $p ) {
11086
- if ( 'update_' . $p === $action ) {
11087
- check_admin_referer( $action );
11088
-
11089
- $this->_logger->log( $action );
11090
-
11091
- $site_property = substr( $p, strlen( 'site_' ) );
11092
- $site_property_value = fs_request_get( 'fs_' . $p . '_' . $this->_slug, '' );
11093
- $this->get_site()->{$site_property} = $site_property_value;
11094
-
11095
- // Store account after modification.
11096
- $this->_store_site();
11097
-
11098
- $this->do_action( 'account_property_edit', 'site', $site_property, $site_property_value );
11099
-
11100
- $this->_admin_notices->add( sprintf(
11101
- $this->get_text( 'x-updated' ),
11102
- '<b>' . str_replace( '_', ' ', $p ) . '</b>' ) );
11103
-
11104
- return;
11105
- }
11106
- }
11107
- }
11108
- }
11109
-
11110
- /**
11111
- * Account page resources load.
11112
- *
11113
- * @author Vova Feldman (@svovaf)
11114
- * @since 1.0.6
11115
- */
11116
- function _account_page_load() {
11117
- $this->_logger->entrance();
11118
-
11119
- $this->_logger->info( var_export( $_REQUEST, true ) );
11120
-
11121
- fs_enqueue_local_style( 'fs_account', '/admin/account.css' );
11122
-
11123
- if ( $this->has_addons() ) {
11124
- wp_enqueue_script( 'plugin-install' );
11125
- add_thickbox();
11126
-
11127
- function fs_addons_body_class( $classes ) {
11128
- $classes .= ' plugins-php';
11129
-
11130
- return $classes;
11131
- }
11132
-
11133
- add_filter( 'admin_body_class', 'fs_addons_body_class' );
11134
- }
11135
-
11136
- if ( $this->has_paid_plan() &&
11137
- ! $this->has_any_license() &&
11138
- ! $this->is_sync_executed() &&
11139
- $this->is_tracking_allowed()
11140
- ) {
11141
- /**
11142
- * If no licenses found and no sync job was executed during the last 24 hours,
11143
- * just execute the sync job right away (blocking execution).
11144
- *
11145
- * @since 1.1.7.3
11146
- */
11147
- $this->run_manual_sync();
11148
- }
11149
-
11150
- $this->_handle_account_edits();
11151
-
11152
- $this->do_action( 'account_page_load_before_departure' );
11153
- }
11154
-
11155
- /**
11156
- * Render account page.
11157
- *
11158
- * @author Vova Feldman (@svovaf)
11159
- * @since 1.0.0
11160
- */
11161
- function _account_page_render() {
11162
- $this->_logger->entrance();
11163
-
11164
- $template = 'account.php';
11165
- if ( 'billing' === fs_request_get( 'tab' ) ) {
11166
- $template = 'billing.php';
11167
- }
11168
-
11169
- $vars = array( 'slug' => $this->_slug );
11170
-
11171
- /**
11172
- * Added filter to the template to allow developers wrapping the template
11173
- * in custom HTML (e.g. within a wizard/tabs).
11174
- *
11175
- * @author Vova Feldman (@svovaf)
11176
- * @since 1.2.1.6
11177
- */
11178
- echo $this->apply_filters( "templates/{$template}", fs_get_template( $template, $vars ) );
11179
- }
11180
-
11181
- /**
11182
- * Render account connect page.
11183
- *
11184
- * @author Vova Feldman (@svovaf)
11185
- * @since 1.0.7
11186
- */
11187
- function _connect_page_render() {
11188
- $this->_logger->entrance();
11189
-
11190
- $vars = array( 'slug' => $this->_slug );
11191
-
11192
- /**
11193
- * Added filter to the template to allow developers wrapping the template
11194
- * in custom HTML (e.g. within a wizard/tabs).
11195
- *
11196
- * @author Vova Feldman (@svovaf)
11197
- * @since 1.2.1.6
11198
- */
11199
- echo $this->apply_filters( 'templates/connect.php', fs_get_template( 'connect.php', $vars ) );
11200
- }
11201
-
11202
- /**
11203
- * Load required resources before add-ons page render.
11204
- *
11205
- * @author Vova Feldman (@svovaf)
11206
- * @since 1.0.6
11207
- */
11208
- function _addons_page_load() {
11209
- $this->_logger->entrance();
11210
-
11211
- fs_enqueue_local_style( 'fs_addons', '/admin/add-ons.css' );
11212
-
11213
- wp_enqueue_script( 'plugin-install' );
11214
- add_thickbox();
11215
-
11216
- function fs_addons_body_class( $classes ) {
11217
- $classes .= ' plugins-php';
11218
-
11219
- return $classes;
11220
- }
11221
-
11222
- add_filter( 'admin_body_class', 'fs_addons_body_class' );
11223
-
11224
- if ( ! $this->is_registered() && $this->is_org_repo_compliant() ) {
11225
- $this->_admin_notices->add(
11226
- sprintf( $this->get_text( 'addons-info-external-message' ), '<b>' . $this->get_plugin_name() . '</b>' ),
11227
- $this->get_text( 'heads-up' ),
11228
- 'update-nag'
11229
- );
11230
- }
11231
- }
11232
-
11233
- /**
11234
- * Render add-ons page.
11235
- *
11236
- * @author Vova Feldman (@svovaf)
11237
- * @since 1.0.6
11238
- */
11239
- function _addons_page_render() {
11240
- $this->_logger->entrance();
11241
-
11242
- $vars = array( 'slug' => $this->_slug );
11243
-
11244
- /**
11245
- * Added filter to the template to allow developers wrapping the template
11246
- * in custom HTML (e.g. within a wizard/tabs).
11247
- *
11248
- * @author Vova Feldman (@svovaf)
11249
- * @since 1.2.1.6
11250
- */
11251
- echo $this->apply_filters( 'templates/add-ons.php', fs_get_template( 'add-ons.php', $vars ) );
11252
- }
11253
-
11254
- /* Pricing & Upgrade
11255
- ------------------------------------------------------------------------------------------------------------------*/
11256
- /**
11257
- * Render pricing page.
11258
- *
11259
- * @author Vova Feldman (@svovaf)
11260
- * @since 1.0.0
11261
- */
11262
- function _pricing_page_render() {
11263
- $this->_logger->entrance();
11264
-
11265
- $vars = array( 'slug' => $this->_slug );
11266
-
11267
- if ( 'true' === fs_request_get( 'checkout', false ) ) {
11268
- fs_require_once_template( 'checkout.php', $vars );
11269
- } else {
11270
- fs_require_once_template( 'pricing.php', $vars );
11271
- }
11272
- }
11273
-
11274
- #----------------------------------------------------------------------------------
11275
- #region Contact Us
11276
- #----------------------------------------------------------------------------------
11277
-
11278
- /**
11279
- * Render contact-us page.
11280
- *
11281
- * @author Vova Feldman (@svovaf)
11282
- * @since 1.0.3
11283
- */
11284
- function _contact_page_render() {
11285
- $this->_logger->entrance();
11286
-
11287
- $vars = array( 'slug' => $this->_slug );
11288
- fs_require_once_template( 'contact.php', $vars );
11289
- }
11290
-
11291
- #endregion ------------------------------------------------------------------------
11292
-
11293
- /**
11294
- * Hide all admin notices to prevent distractions.
11295
- *
11296
- * @author Vova Feldman (@svovaf)
11297
- * @since 1.0.3
11298
- *
11299
- * @uses remove_all_actions()
11300
- */
11301
- private static function _hide_admin_notices() {
11302
- remove_all_actions( 'admin_notices' );
11303
- remove_all_actions( 'network_admin_notices' );
11304
- remove_all_actions( 'all_admin_notices' );
11305
- remove_all_actions( 'user_admin_notices' );
11306
- }
11307
-
11308
- static function _clean_admin_content_section_hook() {
11309
- self::_hide_admin_notices();
11310
-
11311
- // Hide footer.
11312
- echo '<style>#wpfooter { display: none !important; }</style>';
11313
- }
11314
-
11315
- /**
11316
- * Attach to admin_head hook to hide all admin notices.
11317
- *
11318
- * @author Vova Feldman (@svovaf)
11319
- * @since 1.0.3
11320
- */
11321
- static function _clean_admin_content_section() {
11322
- add_action( 'admin_head', 'Freemius::_clean_admin_content_section_hook' );
11323
- }
11324
-
11325
- /* CSS & JavaScript
11326
- ------------------------------------------------------------------------------------------------------------------*/
11327
- /* function _enqueue_script($handle, $src) {
11328
- $url = plugins_url( substr( WP_FS__DIR_JS, strlen( $this->_plugin_dir_path ) ) . '/assets/js/' . $src );
11329
-
11330
- $this->_logger->entrance( 'script = ' . $url );
11331
-
11332
- wp_enqueue_script( $handle, $url );
11333
- }*/
11334
-
11335
- /* SDK
11336
- ------------------------------------------------------------------------------------------------------------------*/
11337
- private $_user_api;
11338
-
11339
- /**
11340
- *
11341
- * @author Vova Feldman (@svovaf)
11342
- * @since 1.0.2
11343
- *
11344
- * @param bool $flush
11345
- *
11346
- * @return FS_Api
11347
- */
11348
- function get_api_user_scope( $flush = false ) {
11349
- if ( ! isset( $this->_user_api ) || $flush ) {
11350
- $this->_user_api = FS_Api::instance(
11351
- $this->_slug,
11352
- 'user',
11353
- $this->_user->id,
11354
- $this->_user->public_key,
11355
- ! $this->is_live(),
11356
- $this->_user->secret_key
11357
- );
11358
- }
11359
-
11360
- return $this->_user_api;
11361
- }
11362
-
11363
- private $_site_api;
11364
-
11365
- /**
11366
- *
11367
- * @author Vova Feldman (@svovaf)
11368
- * @since 1.0.2
11369
- *
11370
- * @param bool $flush
11371
- *
11372
- * @return FS_Api
11373
- */
11374
- function get_api_site_scope( $flush = false ) {
11375
- if ( ! isset( $this->_site_api ) || $flush ) {
11376
- $this->_site_api = FS_Api::instance(
11377
- $this->_slug,
11378
- 'install',
11379
- $this->_site->id,
11380
- $this->_site->public_key,
11381
- ! $this->is_live(),
11382
- $this->_site->secret_key
11383
- );
11384
- }
11385
-
11386
- return $this->_site_api;
11387
- }
11388
-
11389
- private $_plugin_api;
11390
-
11391
- /**
11392
- * Get plugin public API scope.
11393
- *
11394
- * @author Vova Feldman (@svovaf)
11395
- * @since 1.0.7
11396
- *
11397
- * @return FS_Api
11398
- */
11399
- function get_api_plugin_scope() {
11400
- if ( ! isset( $this->_plugin_api ) ) {
11401
- $this->_plugin_api = FS_Api::instance(
11402
- $this->_slug,
11403
- 'plugin',
11404
- $this->_plugin->id,
11405
- $this->_plugin->public_key,
11406
- ! $this->is_live()
11407
- );
11408
- }
11409
-
11410
- return $this->_plugin_api;
11411
- }
11412
-
11413
- /**
11414
- * Get site API scope object (fallback to public plugin scope when not registered).
11415
- *
11416
- * @author Vova Feldman (@svovaf)
11417
- * @since 1.0.7
11418
- *
11419
- * @return FS_Api
11420
- */
11421
- function get_api_site_or_plugin_scope() {
11422
- return $this->is_registered() ?
11423
- $this->get_api_site_scope() :
11424
- $this->get_api_plugin_scope();
11425
- }
11426
-
11427
- /**
11428
- * Show trial promotional notice (if any trial exist).
11429
- *
11430
- * @author Vova Feldman (@svovaf)
11431
- * @since 1.0.9
11432
- *
11433
- * @param $plans
11434
- */
11435
- function _check_for_trial_plans( $plans ) {
11436
- $this->_storage->has_trial_plan = FS_Plan_Manager::instance()->has_trial_plan( $plans );
11437
- }
11438
-
11439
- /**
11440
- * During trial promotion the "upgrade" submenu item turns to
11441
- * "start trial" to encourage the trial. Since we want to keep
11442
- * the same menu item handler and there's no robust way to
11443
- * add new arguments to the menu item link's querystring,
11444
- * use JavaScript to find the menu item and update the href of
11445
- * the link.
11446
- *
11447
- * @author Vova Feldman (@svovaf)
11448
- * @since 1.2.1.5
11449
- */
11450
- function _fix_start_trial_menu_item_url() {
11451
- $template_args = array( 'slug' => $this->_slug );
11452
- fs_require_template( 'add-trial-to-pricing.php', $template_args );
11453
- }
11454
-
11455
- /**
11456
- * Show trial promotional notice (if any trial exist).
11457
- *
11458
- * @author Vova Feldman (@svovaf)
11459
- * @since 1.0.9
11460
- *
11461
- * @return bool If trial notice added.
11462
- */
11463
- function _add_trial_notice() {
11464
- if ( ! $this->is_user_admin() ) {
11465
- return false;
11466
- }
11467
-
11468
- if ( ! $this->is_user_in_admin() ) {
11469
- return false;
11470
- }
11471
-
11472
- // Check if trial message is already shown.
11473
- if ( $this->_admin_notices->has_sticky( 'trial_promotion' ) ) {
11474
- add_action( 'admin_footer', array( &$this, '_fix_start_trial_menu_item_url' ) );
11475
-
11476
- $this->_menu->add_counter_to_menu_item( 1, 'fs-trial' );
11477
-
11478
- return false;
11479
- }
11480
-
11481
- if ( $this->is_premium() && ! WP_FS__DEV_MODE ) {
11482
- // Don't show trial if running the premium code, unless running in DEV mode.
11483
- return false;
11484
- }
11485
-
11486
- if ( ! $this->has_trial_plan() ) {
11487
- // No plans with trial.
11488
- return false;
11489
- }
11490
-
11491
- if ( ! $this->apply_filters( 'show_trial', true ) ) {
11492
- // Developer explicitly asked not to show the trial promo.
11493
- return false;
11494
- }
11495
-
11496
- if ( $this->is_registered() ) {
11497
- // Check if trial already utilized.
11498
- if ( $this->_site->is_trial_utilized() ) {
11499
- return false;
11500
- }
11501
-
11502
- if ( $this->is_paying_or_trial() ) {
11503
- // Don't show trial if paying or already in trial.
11504
- return false;
11505
- }
11506
- }
11507
-
11508
- if ( $this->is_activation_mode() || $this->is_pending_activation() ) {
11509
- // If not yet opted-in/skipped, or pending activation, don't show trial.
11510
- return false;
11511
- }
11512
-
11513
- $last_time_trial_promotion_shown = $this->_storage->get( 'trial_promotion_shown', false );
11514
- $was_promotion_shown_before = ( false !== $last_time_trial_promotion_shown );
11515
-
11516
- // Show promotion if never shown before and 24 hours after initial activation with FS.
11517
- if ( ! $was_promotion_shown_before &&
11518
- $this->_storage->install_timestamp > ( time() - WP_FS__TIME_24_HOURS_IN_SEC )
11519
- ) {
11520
- return false;
11521
- }
11522
-
11523
- // OR if promotion was shown before, try showing it every 30 days.
11524
- if ( $was_promotion_shown_before &&
11525
- 30 * WP_FS__TIME_24_HOURS_IN_SEC > time() - $last_time_trial_promotion_shown
11526
- ) {
11527
- return false;
11528
- }
11529
-
11530
- $trial_period = $this->_trial_days;
11531
- $require_payment = $this->_is_trial_require_payment;
11532
- $trial_url = $this->get_trial_url();
11533
- $plans_string = strtolower( $this->get_text( 'awesome' ) );
11534
-
11535
- if ( $this->is_registered() ) {
11536
- // If opted-in, override trial with up to date data from API.
11537
- $trial_plans = FS_Plan_Manager::instance()->get_trial_plans( $this->_plans );
11538
- $trial_plans_count = count( $trial_plans );
11539
-
11540
- if ( 0 === $trial_plans_count ) {
11541
- // If there's no plans with a trial just exit.
11542
- return false;
11543
- }
11544
-
11545
- /**
11546
- * @var FS_Plugin_Plan $paid_plan
11547
- */
11548
- $paid_plan = $trial_plans[0];
11549
- $require_payment = $paid_plan->is_require_subscription;
11550
- $trial_period = $paid_plan->trial_period;
11551
-
11552
- $total_paid_plans = count( $this->_plans ) - ( FS_Plan_Manager::instance()->has_free_plan( $this->_plans ) ? 1 : 0 );
11553
-
11554
- if ( $total_paid_plans !== $trial_plans_count ) {
11555
- // Not all paid plans have a trial - generate a string of those that have it.
11556
- for ( $i = 0; $i < $trial_plans_count; $i ++ ) {
11557
- $plans_string .= sprintf(
11558
- '<a href="%s">%s</a>',
11559
- $trial_url,
11560
- $trial_plans[ $i ]->title
11561
- );
11562
-
11563
- if ( $i < $trial_plans_count - 2 ) {
11564
- $plans_string .= ', ';
11565
- } else if ( $i == $trial_plans_count - 2 ) {
11566
- $plans_string .= ' and ';
11567
- }
11568
- }
11569
- }
11570
- }
11571
-
11572
- $message = sprintf(
11573
- $this->get_text( 'hey' ) . '! ' . $this->get_text( 'trial-x-promotion-message' ),
11574
- sprintf( '<b>%s</b>', $this->get_plugin_name() ),
11575
- $plans_string,
11576
- $trial_period
11577
- );
11578
-
11579
- // "No Credit-Card Required" or "No Commitment for N Days".
11580
- $cc_string = $require_payment ?
11581
- sprintf( $this->get_text( 'no-commitment-for-x-days' ), $trial_period ) :
11582
- $this->get_text( 'no-cc-required' ) . '!';
11583
-
11584
-
11585
- // Start trial button.
11586
- $button = ' ' . sprintf(
11587
- '<a style="margin-left: 10px; vertical-align: super;" href="%s"><button class="button button-primary">%s &nbsp;&#10140;</button></a>',
11588
- $trial_url,
11589
- $this->get_text( 'start-free-trial' )
11590
- );
11591
-
11592
- $this->_admin_notices->add_sticky(
11593
- $this->apply_filters( 'trial_promotion_message', "{$message} {$cc_string} {$button}" ),
11594
- 'trial_promotion',
11595
- '',
11596
- 'promotion'
11597
- );
11598
-
11599
- $this->_storage->trial_promotion_shown = WP_FS__SCRIPT_START_TIME;
11600
-
11601
- return true;
11602
- }
11603
-
11604
- /**
11605
- * @author Vova Feldman (@svovaf)
11606
- * @since 1.2.1.5
11607
- */
11608
- function _enqueue_common_css() {
11609
- if ( $this->has_paid_plan() && ! $this->is_paying() ) {
11610
- // Add basic CSS for admin-notices and menu-item colors.
11611
- fs_enqueue_local_style( 'fs_common', '/admin/common.css' );
11612
- }
11613
- }
11614
-
11615
- /* Action Links
11616
- ------------------------------------------------------------------------------------------------------------------*/
11617
- private $_action_links_hooked = false;
11618
- private $_action_links = array();
11619
-
11620
- /**
11621
- * Hook to plugin action links filter.
11622
- *
11623
- * @author Vova Feldman (@svovaf)
11624
- * @since 1.0.0
11625
- */
11626
- private function hook_plugin_action_links() {
11627
- $this->_logger->entrance();
11628
-
11629
- $this->_action_links_hooked = true;
11630
-
11631
- $this->_logger->log( 'Adding action links hooks.' );
11632
-
11633
- // Add action link to settings page.
11634
- add_filter( 'plugin_action_links_' . $this->_plugin_basename, array(
11635
- &$this,
11636
- '_modify_plugin_action_links_hook'
11637
- ), WP_FS__DEFAULT_PRIORITY, 2 );
11638
- add_filter( 'network_admin_plugin_action_links_' . $this->_plugin_basename, array(
11639
- &$this,
11640
- '_modify_plugin_action_links_hook'
11641
- ), WP_FS__DEFAULT_PRIORITY, 2 );
11642
- }
11643
-
11644
- /**
11645
- * Add plugin action link.
11646
- *
11647
- * @author Vova Feldman (@svovaf)
11648
- * @since 1.0.0
11649
- *
11650
- * @param $label
11651
- * @param $url
11652
- * @param bool $external
11653
- * @param int $priority
11654
- * @param bool $key
11655
- */
11656
- function add_plugin_action_link( $label, $url, $external = false, $priority = WP_FS__DEFAULT_PRIORITY, $key = false ) {
11657
- $this->_logger->entrance();
11658
-
11659
- if ( ! isset( $this->_action_links[ $priority ] ) ) {
11660
- $this->_action_links[ $priority ] = array();
11661
- }
11662
-
11663
- if ( false === $key ) {
11664
- $key = preg_replace( "/[^A-Za-z0-9 ]/", '', strtolower( $label ) );
11665
- }
11666
-
11667
- $this->_action_links[ $priority ][] = array(
11668
- 'label' => $label,
11669
- 'href' => $url,
11670
- 'key' => $key,
11671
- 'external' => $external
11672
- );
11673
- }
11674
-
11675
- /**
11676
- * Adds Upgrade and Add-Ons links to the main Plugins page link actions collection.
11677
- *
11678
- * @author Vova Feldman (@svovaf)
11679
- * @since 1.0.0
11680
- */
11681
- function _add_upgrade_action_link() {
11682
- $this->_logger->entrance();
11683
-
11684
- if ( $this->is_registered() ) {
11685
- if ( ! $this->is_paying() && $this->has_paid_plan() ) {
11686
- $this->add_plugin_action_link(
11687
- $this->get_text( 'upgrade' ),
11688
- $this->get_upgrade_url(),
11689
- false,
11690
- 7,
11691
- 'upgrade'
11692
- );
11693
- }
11694
-
11695
- if ( $this->has_addons() ) {
11696
- $this->add_plugin_action_link(
11697
- $this->get_text( 'add-ons' ),
11698
- $this->_get_admin_page_url( 'addons' ),
11699
- false,
11700
- 9,
11701
- 'addons'
11702
- );
11703
- }
11704
- }
11705
- }
11706
-
11707
- /**
11708
- * Adds "Activate License" or "Change License" link to the main Plugins page link actions collection.
11709
- *
11710
- * @author Leo Fajardo (@leorw)
11711
- * @since 1.1.9
11712
- */
11713
- function _add_license_action_link() {
11714
- $this->_logger->entrance();
11715
-
11716
- if ( $this->is_free_plan() && $this->is_addon() ) {
11717
- return;
11718
- }
11719
-
11720
- if ( ! self::is_ajax() ) {
11721
- // Inject license activation dialog UI and client side code.
11722
- add_action( 'admin_footer', array( &$this, '_add_license_activation_dialog_box' ) );
11723
- }
11724
-
11725
- $link_text = $this->get_text(
11726
- $this->is_free_plan() ?
11727
- 'activate-license' :
11728
- 'change-license'
11729
- );
11730
-
11731
- $this->add_plugin_action_link(
11732
- $link_text,
11733
- '#',
11734
- false,
11735
- 11,
11736
- ( 'activate-license ' . $this->_slug )
11737
- );
11738
- }
11739
-
11740
- /**
11741
- * Adds "Opt in" or "Opt out" link to the main "Plugins" page link actions collection.
11742
- *
11743
- * @author Leo Fajardo (@leorw)
11744
- * @since 1.2.1.5
11745
- */
11746
- function _add_tracking_links() {
11747
- if ( ! current_user_can( 'activate_plugins' ) ) {
11748
- return;
11749
- }
11750
-
11751
- $this->_logger->entrance();
11752
-
11753
- if ( fs_request_is_action_secure( $this->_slug . '_reconnect' ) ) {
11754
- if ( ! $this->is_registered() && $this->is_anonymous() ) {
11755
- $this->connect_again();
11756
 
11757
- return;
 
 
 
 
 
 
 
 
 
 
 
 
 
11758
  }
11759
  }
11760
 
11761
- if ( ! $this->is_plugins_page() ) {
11762
- // Only show tracking links on the plugin's page.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11763
  return;
11764
  }
11765
 
11766
- if ( ! $this->is_enable_anonymous() ) {
11767
- // Don't allow to opt-out if anonymous mode is disabled.
11768
- return;
11769
- }
11770
-
11771
- if ( ! $this->is_free_plan() ) {
11772
- // Don't allow to opt-out if running in paid plan.
11773
- return;
11774
- }
11775
-
11776
- if ( $this->add_ajax_action( 'stop_tracking', array( &$this, '_stop_tracking_callback' ) ) ) {
11777
- return;
11778
- }
11779
-
11780
- if ( $this->add_ajax_action( 'allow_tracking', array( &$this, '_allow_tracking_callback' ) ) ) {
11781
- return;
11782
- }
11783
-
11784
- $url = '#';
11785
-
11786
- if ( $this->is_registered() ) {
11787
- if ( $this->is_tracking_allowed() ) {
11788
- $link_text_id = 'opt-out';
11789
- } else {
11790
- $link_text_id = 'opt-in';
11791
- }
11792
-
11793
- add_action( 'admin_footer', array( &$this, '_add_optout_dialog' ) );
11794
- } else {
11795
- $link_text_id = 'opt-in';
11796
-
11797
- $params = ! $this->is_anonymous() ?
11798
- array() :
11799
- array(
11800
- 'nonce' => wp_create_nonce( $this->_slug . '_reconnect' ),
11801
- 'fs_action' => ( $this->_slug . '_reconnect' ),
11802
- );
11803
-
11804
- $url = $this->get_activation_url( $params );
11805
- }
11806
-
11807
- $this->add_plugin_action_link(
11808
- $this->get_text( $link_text_id ),
11809
- $url,
11810
- false,
11811
- 13,
11812
- "opt-in-or-opt-out {$this->_slug}"
11813
- );
11814
- }
11815
-
11816
- /**
11817
- * Get the URL of the page that should be loaded right after the plugin activation.
11818
- *
11819
- * @author Vova Feldman (@svovaf)
11820
- * @since 1.1.7.4
11821
- *
11822
- * @return string
11823
- */
11824
- function get_after_plugin_activation_redirect_url() {
11825
- $url = false;
11826
- $plugin_fs = false;
11827
-
11828
- if ( ! $this->is_addon() ) {
11829
- $first_time_path = $this->_menu->get_first_time_path();
11830
- $plugin_fs = $this;
11831
- $url = $plugin_fs->is_activation_mode() ?
11832
- $plugin_fs->get_activation_url() :
11833
- ( empty( $first_time_path ) ?
11834
- $this->_get_admin_page_url() :
11835
- $first_time_path );
11836
- } else {
11837
- if ( $this->is_parent_plugin_installed() ) {
11838
- $plugin_fs = self::get_parent_instance();
11839
- }
11840
-
11841
- if ( is_object( $plugin_fs ) ) {
11842
- if ( ! $plugin_fs->is_registered() ) {
11843
- // Forward to parent plugin connect when parent not registered.
11844
- $url = $plugin_fs->get_activation_url();
11845
- } else {
11846
- // Forward to account page.
11847
- $url = $plugin_fs->_get_admin_page_url( 'account' );
11848
- }
11849
- }
11850
- }
11851
-
11852
- return $url;
11853
- }
11854
-
11855
- /**
11856
- * Forward page to activation page.
11857
- *
11858
- * @author Vova Feldman (@svovaf)
11859
- * @since 1.0.3
11860
- */
11861
- function _redirect_on_activation_hook() {
11862
- $url = $this->get_after_plugin_activation_redirect_url();
11863
-
11864
- if ( is_string( $url ) ) {
11865
- fs_redirect( $url );
11866
- }
11867
- }
11868
-
11869
- /**
11870
- * Modify plugin's page action links collection.
11871
- *
11872
- * @author Vova Feldman (@svovaf)
11873
- * @since 1.0.0
11874
- *
11875
- * @param array $links
11876
- * @param $file
11877
- *
11878
- * @return array
11879
- */
11880
- function _modify_plugin_action_links_hook( $links, $file ) {
11881
- $this->_logger->entrance();
11882
-
11883
- $passed_deactivate = false;
11884
- $deactivate_link = '';
11885
- $before_deactivate = array();
11886
- $after_deactivate = array();
11887
- foreach ( $links as $key => $link ) {
11888
- if ( 'deactivate' === $key ) {
11889
- $deactivate_link = $link;
11890
- $passed_deactivate = true;
11891
- continue;
11892
- }
11893
-
11894
- if ( ! $passed_deactivate ) {
11895
- $before_deactivate[ $key ] = $link;
11896
- } else {
11897
- $after_deactivate[ $key ] = $link;
11898
- }
11899
- }
11900
-
11901
- ksort( $this->_action_links );
11902
-
11903
- foreach ( $this->_action_links as $new_links ) {
11904
- foreach ( $new_links as $link ) {
11905
- $before_deactivate[ $link['key'] ] = '<a href="' . $link['href'] . '"' . ( $link['external'] ? ' target="_blank"' : '' ) . '>' . $link['label'] . '</a>';
11906
- }
11907
- }
11908
-
11909
- if ( ! empty( $deactivate_link ) ) {
11910
- /**
11911
- * This HTML element is used to identify the correct plugin when attaching an event to its Deactivate link.
11912
- *
11913
- * @since 1.2.1.6 Always show the deactivation feedback form since we added automatic free version deactivation upon premium code activation.
11914
- */
11915
- $deactivate_link .= '<i class="fs-slug" data-slug="' . $this->_slug . '"></i>';
11916
-
11917
- // Append deactivation link.
11918
- $before_deactivate['deactivate'] = $deactivate_link;
11919
- }
11920
-
11921
- return array_merge( $before_deactivate, $after_deactivate );
11922
- }
11923
-
11924
- /**
11925
- * Adds admin message.
11926
- *
11927
- * @author Vova Feldman (@svovaf)
11928
- * @since 1.0.4
11929
- *
11930
- * @param string $message
11931
- * @param string $title
11932
- * @param string $type
11933
- */
11934
- function add_admin_message( $message, $title = '', $type = 'success' ) {
11935
- $this->_admin_notices->add( $message, $title, $type );
11936
- }
11937
-
11938
- /**
11939
- * Adds sticky admin message.
11940
- *
11941
- * @author Vova Feldman (@svovaf)
11942
- * @since 1.1.0
11943
- *
11944
- * @param string $message
11945
- * @param string $id
11946
- * @param string $title
11947
- * @param string $type
11948
- */
11949
- function add_sticky_admin_message( $message, $id, $title = '', $type = 'success' ) {
11950
- $this->_admin_notices->add_sticky( $message, $id, $title, $type );
11951
- }
11952
-
11953
- /**
11954
- * Helper function that returns the final steps for the upgrade completion.
11955
- *
11956
- * If the module is already running the premium code, returns an empty string.
11957
- *
11958
- * @author Vova Feldman (@svovaf)
11959
- * @since 1.2.1
11960
- *
11961
- * @param string $plan_title
11962
- *
11963
- * @return string
11964
- */
11965
- private function get_complete_upgrade_instructions( $plan_title = '' ) {
11966
- if ( ! $this->has_premium_version() || $this->is_premium() ) {
11967
- return '';
11968
- }
11969
-
11970
- if ( empty( $plan_title ) ) {
11971
- $plan_title = $this->_site->plan->title;
11972
- }
11973
-
11974
- // @since 1.2.1.5 The free version is auto deactivated.
11975
- $deactivation_step = version_compare( $this->version, '1.2.1.5', '<' ) ?
11976
- ( '<li>' . $this->get_text( 'deactivate-free-version' ) . '.</li>' ) :
11977
- '';
11978
-
11979
- return sprintf(
11980
- ' %s: <ol><li>%s.</li>%s<li>%s (<a href="%s" target="_blank">%s</a>).</li></ol>',
11981
- $this->get_text( 'follow-steps-to-complete-upgrade' ),
11982
- $this->get_latest_download_link( sprintf(
11983
- $this->get_text( 'download-latest-x-version' ),
11984
- $plan_title
11985
- ) ),
11986
- $deactivation_step,
11987
- $this->get_text( 'upload-and-activate' ),
11988
- '//bit.ly/upload-wp-plugin',
11989
- $this->get_text( 'howto-upload-activate' )
11990
- );
11991
- }
11992
-
11993
- /**
11994
- * @author Vova Feldman (@svovaf)
11995
- * @since 1.2.1.7
11996
- *
11997
- * @param string $key
11998
- *
11999
- * @return string
12000
- */
12001
- function get_text( $key ) {
12002
- return fs_text( $key, $this->_slug );
12003
- }
12004
-
12005
- #----------------------------------------------------------------------------------
12006
- #region Versioning
12007
- #----------------------------------------------------------------------------------
12008
-
12009
- /**
12010
- * Check if Freemius in SDK upgrade mode.
12011
- *
12012
- * @author Vova Feldman (@svovaf)
12013
- * @since 1.0.9
12014
- *
12015
- * @return bool
12016
- */
12017
- function is_sdk_upgrade_mode() {
12018
- return isset( $this->_storage->sdk_upgrade_mode ) ?
12019
- $this->_storage->sdk_upgrade_mode :
12020
- false;
12021
- }
12022
-
12023
- /**
12024
- * Turn SDK upgrade mode off.
12025
- *
12026
- * @author Vova Feldman (@svovaf)
12027
- * @since 1.0.9
12028
- */
12029
- function set_sdk_upgrade_complete() {
12030
- $this->_storage->sdk_upgrade_mode = false;
12031
- }
12032
-
12033
- /**
12034
- * Check if plugin upgrade mode.
12035
- *
12036
- * @author Vova Feldman (@svovaf)
12037
- * @since 1.0.9
12038
- *
12039
- * @return bool
12040
- */
12041
- function is_plugin_upgrade_mode() {
12042
- return isset( $this->_storage->plugin_upgrade_mode ) ?
12043
- $this->_storage->plugin_upgrade_mode :
12044
- false;
12045
- }
12046
-
12047
- /**
12048
- * Turn plugin upgrade mode off.
12049
- *
12050
- * @author Vova Feldman (@svovaf)
12051
- * @since 1.0.9
12052
- */
12053
- function set_plugin_upgrade_complete() {
12054
- $this->_storage->plugin_upgrade_mode = false;
12055
- }
12056
-
12057
- #endregion
12058
-
12059
- #----------------------------------------------------------------------------------
12060
- #region Permissions
12061
- #----------------------------------------------------------------------------------
12062
-
12063
- /**
12064
- * Check if specific permission requested.
12065
- *
12066
- * @author Vova Feldman (@svovaf)
12067
- * @since 1.1.6
12068
- *
12069
- * @param string $permission
12070
- *
12071
- * @return bool
12072
- */
12073
- function is_permission_requested( $permission ) {
12074
- return isset( $this->_permissions[ $permission ] ) && ( true === $this->_permissions[ $permission ] );
12075
- }
12076
-
12077
- #endregion
12078
-
12079
- #----------------------------------------------------------------------------------
12080
- #region Auto Activation
12081
- #----------------------------------------------------------------------------------
12082
-
12083
- /**
12084
- * Hints the SDK if running an auto-installation.
12085
- *
12086
- * @var bool
12087
- */
12088
- private $_isAutoInstall = false;
12089
-
12090
- /**
12091
- * After upgrade callback to install and auto activate a plugin.
12092
- * This code will only be executed on explicit request from the user,
12093
- * following the practice Jetpack are using with their theme installations.
12094
- *
12095
- * @link https://make.wordpress.org/plugins/2017/03/16/clarification-of-guideline-8-executable-code-and-installs/
12096
- *
12097
- * @author Vova Feldman (@svovaf)
12098
- * @since 1.2.1.7
12099
- */
12100
- function _install_premium_version_ajax_action() {
12101
- $this->_logger->entrance();
12102
-
12103
- $this->check_ajax_referer( 'install_premium_version' );
12104
-
12105
- if ( ! $this->is_registered() ) {
12106
- // Not registered.
12107
- self::shoot_ajax_failure( array(
12108
- 'message' => $this->get_text( 'auto-install-error-not-opted-in' ),
12109
- 'code' => 'premium_installed',
12110
- ) );
12111
- }
12112
-
12113
- $plugin_id = fs_request_get( 'module_id', $this->get_id() );
12114
-
12115
- if ( ! FS_Plugin::is_valid_id( $plugin_id ) ) {
12116
- // Invalid ID.
12117
- self::shoot_ajax_failure( array(
12118
- 'message' => $this->get_text( 'auto-install-error-invalid-id' ),
12119
- 'code' => 'invalid_module_id',
12120
- ) );
12121
- }
12122
-
12123
- if ( $plugin_id == $this->get_id() ) {
12124
- if ( $this->is_premium() ) {
12125
- // Already using the premium code version.
12126
- self::shoot_ajax_failure( array(
12127
- 'message' => $this->get_text( 'auto-install-error-premium-activated' ),
12128
- 'code' => 'premium_installed',
12129
- ) );
12130
- }
12131
- if ( ! $this->can_use_premium_code() ) {
12132
- // Don't have access to the premium code.
12133
- self::shoot_ajax_failure( array(
12134
- 'message' => $this->get_text( 'auto-install-error-invalid-license' ),
12135
- 'code' => 'invalid_license',
12136
- ) );
12137
- }
12138
- if ( ! $this->has_release_on_freemius() ) {
12139
- // Plugin is a serviceware, no premium code version.
12140
- self::shoot_ajax_failure( array(
12141
- 'message' => $this->get_text( 'auto-install-error-serviceware' ),
12142
- 'code' => 'premium_version_missing',
12143
- ) );
12144
- }
12145
- } else {
12146
- $addon = $this->get_addon( $plugin_id );
12147
-
12148
- if ( ! is_object( $addon ) ) {
12149
- // Invalid add-on ID.
12150
- self::shoot_ajax_failure( array(
12151
- 'message' => $this->get_text( 'auto-install-error-invalid-id' ),
12152
- 'code' => 'invalid_module_id',
12153
- ) );
12154
- }
12155
-
12156
- if ( $this->is_addon_activated( $plugin_id, true ) ) {
12157
- // Premium add-on version is already activated.
12158
- self::shoot_ajax_failure( array(
12159
- 'message' => $this->get_text( 'auto-install-error-premium-addon-activated' ),
12160
- 'code' => 'premium_installed',
12161
- ) );
12162
- }
12163
- }
12164
-
12165
- $this->_isAutoInstall = true;
12166
-
12167
- // Try to install and activate.
12168
- $updater = new FS_Plugin_Updater( $this );
12169
- $result = $updater->install_and_activate_plugin( $plugin_id );
12170
-
12171
- if ( is_array( $result ) && ! empty( $result['message'] ) ) {
12172
- self::shoot_ajax_failure( array(
12173
- 'message' => $result['message'],
12174
- 'code' => $result['code'],
12175
- ) );
12176
- }
12177
-
12178
- self::shoot_ajax_success( $result );
12179
- }
12180
-
12181
- /**
12182
- * Displays module activation dialog box after a successful upgrade
12183
- * where the user explicitly requested to auto download and install
12184
- * the premium version.
12185
- *
12186
- * @author Vova Feldman (@svovaf)
12187
- * @since 1.2.1.7
12188
- */
12189
- function _add_auto_installation_dialog_box() {
12190
- $this->_logger->entrance();
12191
-
12192
- if ( ! $this->is_registered() ) {
12193
- // Not registered.
12194
- return;
12195
- }
12196
-
12197
- $plugin_id = fs_request_get( 'plugin_id', $this->get_id() );
12198
-
12199
- if ( ! FS_Plugin::is_valid_id( $plugin_id ) ) {
12200
- // Invalid module ID.
12201
- return;
12202
- }
12203
-
12204
- if ( $plugin_id == $this->get_id() ) {
12205
- if ( $this->is_premium() ) {
12206
- // Already using the premium code version.
12207
- return;
12208
- }
12209
- if ( ! $this->can_use_premium_code() ) {
12210
- // Don't have access to the premium code.
12211
- return;
12212
- }
12213
- if ( ! $this->has_release_on_freemius() ) {
12214
- // Plugin is a serviceware, no premium code version.
12215
- return;
12216
- }
12217
- } else {
12218
- $addon = $this->get_addon( $plugin_id );
12219
-
12220
- if ( ! is_object( $addon ) ) {
12221
- // Invalid add-on ID.
12222
- return;
12223
- }
12224
-
12225
- if ( $this->is_addon_activated( $plugin_id, true ) ) {
12226
- // Premium add-on version is already activated.
12227
- return;
12228
- }
12229
- }
12230
-
12231
- $vars = array(
12232
- 'id' => $plugin_id,
12233
- 'slug' => $this->_slug,
12234
- );
12235
-
12236
- fs_require_template( 'auto-installation.php', $vars );
12237
- }
12238
-
12239
- #endregion
12240
-
12241
- #----------------------------------------------------------------------------------
12242
- #region Marketing
12243
- #----------------------------------------------------------------------------------
12244
-
12245
- /**
12246
- * Check if current user purchased any other plugins before.
12247
- *
12248
- * @author Vova Feldman (@svovaf)
12249
- * @since 1.0.9
12250
- *
12251
- * @return bool
12252
- */
12253
- function has_purchased_before() {
12254
- // TODO: Implement has_purchased_before() method.
12255
- throw new Exception( 'not implemented' );
12256
- }
12257
-
12258
- /**
12259
- * Check if current user classified as an agency.
12260
- *
12261
- * @author Vova Feldman (@svovaf)
12262
- * @since 1.0.9
12263
- *
12264
- * @return bool
12265
- */
12266
- function is_agency() {
12267
- // TODO: Implement is_agency() method.
12268
- throw new Exception( 'not implemented' );
12269
- }
12270
-
12271
- /**
12272
- * Check if current user classified as a developer.
12273
- *
12274
- * @author Vova Feldman (@svovaf)
12275
- * @since 1.0.9
12276
- *
12277
- * @return bool
12278
- */
12279
- function is_developer() {
12280
- // TODO: Implement is_developer() method.
12281
- throw new Exception( 'not implemented' );
12282
- }
12283
-
12284
- /**
12285
- * Check if current user classified as a business.
12286
- *
12287
- * @author Vova Feldman (@svovaf)
12288
- * @since 1.0.9
12289
- *
12290
- * @return bool
12291
- */
12292
- function is_business() {
12293
- // TODO: Implement is_business() method.
12294
- throw new Exception( 'not implemented' );
12295
- }
12296
-
12297
- #endregion
12298
- }
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
+ /**
275
+ * @author Leo Fajardo (@leorw)
276
+ *
277
+ * @since 1.2.3
278
+ *
279
+ * @var FS_Affiliate
280
+ */
281
+ private $affiliate = null;
282
+
283
+ /**
284
+ * @author Leo Fajardo (@leorw)
285
+ *
286
+ * @since 1.2.3
287
+ *
288
+ * @var FS_AffiliateTerms
289
+ */
290
+ private $plugin_affiliate_terms = null;
291
+
292
+ /**
293
+ * @author Leo Fajardo (@leorw)
294
+ *
295
+ * @since 1.2.3
296
+ *
297
+ * @var FS_AffiliateTerms
298
+ */
299
+ private $custom_affiliate_terms = null;
300
+
301
+ #region Uninstall Reasons IDs
302
+
303
+ const REASON_NO_LONGER_NEEDED = 1;
304
+ const REASON_FOUND_A_BETTER_PLUGIN = 2;
305
+ const REASON_NEEDED_FOR_A_SHORT_PERIOD = 3;
306
+ const REASON_BROKE_MY_SITE = 4;
307
+ const REASON_SUDDENLY_STOPPED_WORKING = 5;
308
+ const REASON_CANT_PAY_ANYMORE = 6;
309
+ const REASON_OTHER = 7;
310
+ const REASON_DIDNT_WORK = 8;
311
+ const REASON_DONT_LIKE_TO_SHARE_MY_INFORMATION = 9;
312
+ const REASON_COULDNT_MAKE_IT_WORK = 10;
313
+ const REASON_GREAT_BUT_NEED_SPECIFIC_FEATURE = 11;
314
+ const REASON_NOT_WORKING = 12;
315
+ const REASON_NOT_WHAT_I_WAS_LOOKING_FOR = 13;
316
+ const REASON_DIDNT_WORK_AS_EXPECTED = 14;
317
+ const REASON_TEMPORARY_DEACTIVATION = 15;
318
+
319
+ #endregion
320
+
321
+ /* Ctor
322
+ ------------------------------------------------------------------------------------------------------------------*/
323
+
324
+ /**
325
+ * Main singleton instance.
326
+ *
327
+ * @author Vova Feldman (@svovaf)
328
+ * @since 1.0.0
329
+ *
330
+ * @param number $module_id
331
+ * @param string|bool $slug
332
+ * @param bool $is_init Since 1.2.1 Is initiation sequence.
333
+ */
334
+ private function __construct( $module_id, $slug = false, $is_init = false ) {
335
+ if ( $is_init && is_numeric( $module_id ) && is_string( $slug ) ) {
336
+ $this->store_id_slug_type_path_map( $module_id, $slug );
337
+ }
338
+
339
+ $this->_module_id = $module_id;
340
+ $this->_slug = $this->get_slug();
341
+ $this->_module_type = $this->get_module_type();
342
+
343
+ $this->_storage = FS_Key_Value_Storage::instance( $this->_module_type . '_data', $this->_slug );
344
+ $this->_cache = FS_Cache_Manager::get_manager( WP_FS___OPTION_PREFIX . "cache_{$module_id}" );
345
+
346
+ $this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_' . $this->get_unique_affix(), WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
347
+
348
+ $this->_plugin_main_file_path = $this->_find_caller_plugin_file( $is_init );
349
+ $this->_plugin_dir_path = plugin_dir_path( $this->_plugin_main_file_path );
350
+ $this->_plugin_basename = $this->get_plugin_basename();
351
+ $this->_free_plugin_basename = str_replace( '-premium/', '/', $this->_plugin_basename );
352
+
353
+ $base_name_split = explode( '/', $this->_plugin_basename );
354
+ $this->_plugin_dir_name = $base_name_split[0];
355
+
356
+ if ( $this->_logger->is_on() ) {
357
+ $this->_logger->info( 'plugin_main_file_path = ' . $this->_plugin_main_file_path );
358
+ $this->_logger->info( 'plugin_dir_path = ' . $this->_plugin_dir_path );
359
+ $this->_logger->info( 'plugin_basename = ' . $this->_plugin_basename );
360
+ $this->_logger->info( 'free_plugin_basename = ' . $this->_free_plugin_basename );
361
+ $this->_logger->info( 'plugin_dir_name = ' . $this->_plugin_dir_name );
362
+ }
363
+
364
+ // Remember link between file to slug.
365
+ $this->store_file_slug_map();
366
+
367
+ // Store plugin's initial install timestamp.
368
+ if ( ! isset( $this->_storage->install_timestamp ) ) {
369
+ $this->_storage->install_timestamp = WP_FS__SCRIPT_START_TIME;
370
+ }
371
+
372
+ if ( ! is_object( $this->_plugin ) ) {
373
+ $this->_plugin = FS_Plugin_Manager::instance( $this->_module_id )->get();
374
+ }
375
+
376
+ $this->_admin_notices = FS_Admin_Notice_Manager::instance(
377
+ $this->_slug . ( $this->is_theme() ? ':theme' : '' ),
378
+ /**
379
+ * Ensure that the admin notice will always have a title by using the stored plugin title if available and
380
+ * retrieving the title via the "get_plugin_name" method if there is no stored plugin title available.
381
+ *
382
+ * @author Leo Fajardo (@leorw)
383
+ * @since 1.2.2
384
+ */
385
+ ( is_object( $this->_plugin ) ? $this->_plugin->title : $this->get_plugin_name() ),
386
+ $this->get_unique_affix()
387
+ );
388
+
389
+ if ( 'true' === fs_request_get( 'fs_clear_api_cache' ) ||
390
+ 'true' === fs_request_is_action( 'restart_freemius' )
391
+ ) {
392
+ FS_Api::clear_cache();
393
+ $this->_cache->clear();
394
+ }
395
+
396
+ $this->_register_hooks();
397
+
398
+ $this->_load_account();
399
+
400
+ $this->_version_updates_handler();
401
+ }
402
+
403
+ /**
404
+ * Checks whether this module has a settings menu.
405
+ *
406
+ * @author Leo Fajardo (@leorw)
407
+ * @since 1.2.2
408
+ *
409
+ * @return bool
410
+ */
411
+ function has_settings_menu() {
412
+ return $this->_menu->has_menu();
413
+ }
414
+
415
+ /**
416
+ * Check if the context module is free wp.org theme.
417
+ *
418
+ * This method is helpful because:
419
+ * 1. wp.org themes are limited to a single submenu item,
420
+ * and sub-submenu items are most likely not allowed (never verified).
421
+ * 2. wp.org themes are not allowed to redirect the user
422
+ * after the theme activation, therefore, the agreed UX
423
+ * is showing the opt-in as a modal dialog box after
424
+ * activation (approved by @otto42, @emiluzelac, @greenshady, @grapplerulrich).
425
+ *
426
+ * @author Vova Feldman (@svovaf)
427
+ * @since 1.2.2.7
428
+ *
429
+ * @return bool
430
+ */
431
+ function is_free_wp_org_theme() {
432
+ return (
433
+ $this->is_theme() &&
434
+ $this->is_org_repo_compliant() &&
435
+ ! $this->is_premium()
436
+ );
437
+ }
438
+
439
+ /**
440
+ * Checks whether this a submenu item is visible.
441
+ *
442
+ * @author Vova Feldman (@svovaf)
443
+ * @since 1.2.2.6
444
+ * @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.
445
+ *
446
+ * @param string $slug
447
+ *
448
+ * @return bool
449
+ */
450
+ function is_submenu_item_visible( $slug ) {
451
+ if ( $this->is_admin_page( $slug ) ) {
452
+ /**
453
+ * It is the current context page, so show the submenu item
454
+ * so the user will have the right context page, even if it
455
+ * was set to hidden.
456
+ */
457
+ return true;
458
+ }
459
+
460
+ if ( ! $this->has_settings_menu() ) {
461
+ // No menu settings at all.
462
+ return false;
463
+ }
464
+
465
+ if ( $this->is_free_wp_org_theme() ) {
466
+ /**
467
+ * wp.org themes are limited to a single submenu item, and
468
+ * sub-submenu items are most likely not allowed (never verified).
469
+ */
470
+ return false;
471
+ }
472
+
473
+ return $this->_menu->is_submenu_item_visible( $slug );
474
+ }
475
+
476
+ /**
477
+ * Check if a Freemius page should be accessible via the UI.
478
+ *
479
+ * @author Vova Feldman (@svovaf)
480
+ * @since 1.2.2.7
481
+ *
482
+ * @param string $slug
483
+ *
484
+ * @return bool
485
+ */
486
+ function is_page_visible( $slug ) {
487
+ if ( $this->is_admin_page( $slug ) ) {
488
+ return true;
489
+ }
490
+
491
+ return $this->_menu->is_submenu_item_visible( $slug, true, true );
492
+ }
493
+
494
+ /**
495
+ * @author Vova Feldman (@svovaf)
496
+ * @since 1.0.9
497
+ */
498
+ private function _version_updates_handler() {
499
+ if ( ! isset( $this->_storage->sdk_version ) || $this->_storage->sdk_version != $this->version ) {
500
+ // Freemius version upgrade mode.
501
+ $this->_storage->sdk_last_version = $this->_storage->sdk_version;
502
+ $this->_storage->sdk_version = $this->version;
503
+
504
+ if ( empty( $this->_storage->sdk_last_version ) ||
505
+ version_compare( $this->_storage->sdk_last_version, $this->version, '<' )
506
+ ) {
507
+ $this->_storage->sdk_upgrade_mode = true;
508
+ $this->_storage->sdk_downgrade_mode = false;
509
+ } else {
510
+ $this->_storage->sdk_downgrade_mode = true;
511
+ $this->_storage->sdk_upgrade_mode = false;
512
+
513
+ }
514
+
515
+ $this->do_action( 'sdk_version_update', $this->_storage->sdk_last_version, $this->version );
516
+ }
517
+
518
+ $plugin_version = $this->get_plugin_version();
519
+ if ( ! isset( $this->_storage->plugin_version ) || $this->_storage->plugin_version != $plugin_version ) {
520
+ // Plugin version upgrade mode.
521
+ $this->_storage->plugin_last_version = $this->_storage->plugin_version;
522
+ $this->_storage->plugin_version = $plugin_version;
523
+
524
+ if ( empty( $this->_storage->plugin_last_version ) ||
525
+ version_compare( $this->_storage->plugin_last_version, $plugin_version, '<' )
526
+ ) {
527
+ $this->_storage->plugin_upgrade_mode = true;
528
+ $this->_storage->plugin_downgrade_mode = false;
529
+ } else {
530
+ $this->_storage->plugin_downgrade_mode = true;
531
+ $this->_storage->plugin_upgrade_mode = false;
532
+ }
533
+
534
+ if ( ! empty( $this->_storage->plugin_last_version ) ) {
535
+ // Different version of the plugin was installed before, therefore it's an update.
536
+ $this->_storage->is_plugin_new_install = false;
537
+ }
538
+
539
+ $this->do_action( 'plugin_version_update', $this->_storage->plugin_last_version, $plugin_version );
540
+ }
541
+ }
542
+
543
+ /**
544
+ * @author Vova Feldman (@svovaf)
545
+ * @since 1.1.5
546
+ *
547
+ * @param string $sdk_prev_version
548
+ * @param string $sdk_version
549
+ */
550
+ function _data_migration( $sdk_prev_version, $sdk_version ) {
551
+ /**
552
+ * @since 1.1.7.3 Fixed unwanted connectivity test cleanup.
553
+ */
554
+ if ( empty( $sdk_prev_version ) ) {
555
+ return;
556
+ }
557
+
558
+ if ( version_compare( $sdk_prev_version, '1.2.3', '<' ) &&
559
+ version_compare( $sdk_version, '1.2.3', '>=' )
560
+ ) {
561
+ /**
562
+ * Starting from version 1.2.3, paths are stored as relative paths and not absolute paths; so when
563
+ * upgrading to 1.2.3, make paths relative.
564
+ *
565
+ * @author Leo Fajardo (@leorw)
566
+ */
567
+ $this->make_paths_relative();
568
+ }
569
+
570
+ if ( version_compare( $sdk_prev_version, '1.1.5', '<' ) &&
571
+ version_compare( $sdk_version, '1.1.5', '>=' )
572
+ ) {
573
+ // On version 1.1.5 merged connectivity and is_on data.
574
+ if ( isset( $this->_storage->connectivity_test ) ) {
575
+ if ( ! isset( $this->_storage->is_on ) ) {
576
+ unset( $this->_storage->connectivity_test );
577
+ } else {
578
+ $connectivity_data = $this->_storage->connectivity_test;
579
+ $connectivity_data['is_active'] = $this->_storage->is_on['is_active'];
580
+ $connectivity_data['timestamp'] = $this->_storage->is_on['timestamp'];
581
+
582
+ // Override.
583
+ $this->_storage->connectivity_test = $connectivity_data;
584
+
585
+ // Remove previous structure.
586
+ unset( $this->_storage->is_on );
587
+ }
588
+
589
+ }
590
+ }
591
+ }
592
+
593
+ /**
594
+ * Makes paths relative.
595
+ *
596
+ * @author Leo Fajardo
597
+ * @since 1.2.3
598
+ */
599
+ private function make_paths_relative() {
600
+ $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
601
+
602
+ if ( isset( $id_slug_type_path_map[ $this->_module_id ]['path'] ) ) {
603
+ $id_slug_type_path_map[ $this->_module_id ]['path'] = $this->get_relative_path( $id_slug_type_path_map[ $this->_module_id ]['path'] );
604
+
605
+ self::$_accounts->set_option( 'id_slug_type_path_map', $id_slug_type_path_map, true );
606
+ }
607
+
608
+ if ( isset( $this->_storage->plugin_main_file ) ) {
609
+ $plugin_main_file = $this->_storage->plugin_main_file;
610
+
611
+ if ( isset( $plugin_main_file->path ) ) {
612
+ $this->_storage->plugin_main_file->path = $this->get_relative_path( $this->_storage->plugin_main_file->path );
613
+ } else if ( isset( $plugin_main_file->prev_path ) ) {
614
+ $this->_storage->plugin_main_file->prev_path = $this->get_relative_path( $this->_storage->plugin_main_file->prev_path );
615
+ }
616
+ }
617
+
618
+ // Remove invalid path that is still associated with the current slug if there's any.
619
+ $file_slug_map = self::$_accounts->get_option( 'file_slug_map', array() );
620
+ foreach ( $file_slug_map as $plugin_basename => $slug ) {
621
+ if ( $slug === $this->_slug &&
622
+ $plugin_basename !== $this->_plugin_basename &&
623
+ ! file_exists( $this->get_absolute_path( $plugin_basename ) )
624
+ ) {
625
+ unset( $file_slug_map[ $plugin_basename ] );
626
+ self::$_accounts->set_option( 'file_slug_map', $file_slug_map, true );
627
+
628
+ break;
629
+ }
630
+ }
631
+ }
632
+
633
+ /**
634
+ * @author Vova Feldman (@svovaf)
635
+ * @since 1.2.2.7
636
+ *
637
+ * @param string $plugin_prev_version
638
+ * @param string $plugin_version
639
+ */
640
+ function _after_version_update( $plugin_prev_version, $plugin_version ) {
641
+ if ( $this->is_theme() ) {
642
+ // Expire the cache of the previous tabs since the theme may
643
+ // have setting updates.
644
+ $this->_cache->expire( 'tabs' );
645
+ $this->_cache->expire( 'tabs_stylesheets' );
646
+ }
647
+ }
648
+
649
+ /**
650
+ * This action is connected to the 'plugins_loaded' hook and helps to determine
651
+ * if this is a new plugin installation or a plugin update.
652
+ *
653
+ * There are 3 different use-cases:
654
+ * 1) New plugin installation right with Freemius:
655
+ * 1.1 _activate_plugin_event_hook() will be executed first
656
+ * 1.2 Since $this->_storage->is_plugin_new_install is not set,
657
+ * and $this->_storage->plugin_last_version is not set,
658
+ * $this->_storage->is_plugin_new_install will be set to TRUE.
659
+ * 1.3 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install will
660
+ * be already set to TRUE.
661
+ *
662
+ * 2) Plugin update, didn't have Freemius before, and now have the SDK:
663
+ * 2.1 _activate_plugin_event_hook() will not be executed, because
664
+ * the activation hook do NOT fires on updates since WP 3.1.
665
+ * 2.2 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install will
666
+ * be empty, therefore, it will be set to FALSE.
667
+ *
668
+ * 3) Plugin update, had Freemius in prev version as well:
669
+ * 3.1 _version_updates_handler() will be executed 1st, since FS was installed
670
+ * before, $this->_storage->plugin_last_version will NOT be empty,
671
+ * therefore, $this->_storage->is_plugin_new_install will be set to FALSE.
672
+ * 3.2 When _plugins_loaded() will be executed, $this->_storage->is_plugin_new_install is
673
+ * already set, therefore, it will not be modified.
674
+ *
675
+ * Use-case #3 is backward compatible, #3.1 will be executed since 1.0.9.
676
+ *
677
+ * NOTE:
678
+ * The only fallback of this mechanism is if an admin updates a plugin based on use-case #2,
679
+ * and then, the next immediate PageView is the plugin's main settings page, it will not
680
+ * show the opt-in right away. The reason it will happen is because Freemius execution
681
+ * will be turned off till the plugin is fully loaded at least once
682
+ * (till $this->_storage->was_plugin_loaded is TRUE).
683
+ *
684
+ * @author Vova Feldman (@svovaf)
685
+ * @since 1.1.9
686
+ *
687
+ */
688
+ function _plugins_loaded() {
689
+ // Update flag that plugin was loaded with Freemius at least once.
690
+ $this->_storage->was_plugin_loaded = true;
691
+
692
+ /**
693
+ * Bug fix - only set to false when it's a plugin, due to the
694
+ * execution sequence of the theme hooks and our methods, if
695
+ * this will be set for themes, Freemius will always assume
696
+ * it's a theme update.
697
+ *
698
+ * @author Vova Feldman (@svovaf)
699
+ * @since 1.2.2.2
700
+ */
701
+ if ( $this->is_plugin() &&
702
+ ! isset( $this->_storage->is_plugin_new_install )
703
+ ) {
704
+ $this->_storage->is_plugin_new_install = false;
705
+ }
706
+ }
707
+
708
+ /**
709
+ * @author Vova Feldman (@svovaf)
710
+ * @since 1.0.9
711
+ */
712
+ private function _register_hooks() {
713
+ $this->_logger->entrance();
714
+
715
+ if ( is_admin() ) {
716
+ if ( $this->is_plugin() ) {
717
+ $plugin_dir = dirname( $this->_plugin_dir_path ) . '/';
718
+
719
+ /**
720
+ * @since 1.2.2
721
+ *
722
+ * Hook to both free and premium version activations to support
723
+ * auto deactivation on the other version activation.
724
+ */
725
+ register_activation_hook(
726
+ $plugin_dir . $this->_free_plugin_basename,
727
+ array( &$this, '_activate_plugin_event_hook' )
728
+ );
729
+
730
+ register_activation_hook(
731
+ $plugin_dir . $this->premium_plugin_basename(),
732
+ array( &$this, '_activate_plugin_event_hook' )
733
+ );
734
+ } else {
735
+ add_action( 'after_switch_theme', array( &$this, '_activate_theme_event_hook' ), 10, 2 );
736
+
737
+ /**
738
+ * Include the required hooks to capture the theme settings' page tabs
739
+ * and cache them.
740
+ *
741
+ * @author Vova Feldman (@svovaf)
742
+ * @since 1.2.2.7
743
+ */
744
+ if ( ! $this->_cache->has_valid( 'tabs' ) ) {
745
+ add_action( 'admin_footer', array( &$this, '_tabs_capture' ) );
746
+ // Add license activation AJAX callback.
747
+ $this->add_ajax_action( 'store_tabs', array( &$this, '_store_tabs_ajax_action' ) );
748
+
749
+ add_action( 'admin_enqueue_scripts', array( &$this, '_store_tabs_styles' ), 9999999 );
750
+ }
751
+
752
+ add_action(
753
+ 'admin_footer',
754
+ array( &$this, '_add_freemius_tabs' ),
755
+ /**
756
+ * The tabs JS code must be executed after the tabs capture logic (_tabs_capture()).
757
+ * That's why the priority is 11 while the tabs capture logic is added
758
+ * with priority 10.
759
+ *
760
+ * @author Vova Feldman (@svovaf)
761
+ */
762
+ 11
763
+ );
764
+
765
+ add_action( 'admin_footer', array( &$this, '_style_premium_theme' ) );
766
+ }
767
+
768
+ /**
769
+ * Part of the mechanism to identify new plugin install vs. plugin update.
770
+ *
771
+ * @author Vova Feldman (@svovaf)
772
+ * @since 1.1.9
773
+ */
774
+ if ( empty( $this->_storage->was_plugin_loaded ) ) {
775
+ if ( $this->is_plugin() &&
776
+ $this->is_activation_mode( false ) &&
777
+ 0 == did_action( 'plugins_loaded' )
778
+ ) {
779
+ add_action( 'plugins_loaded', array( &$this, '_plugins_loaded' ) );
780
+ } else {
781
+ // If was activated before, then it was already loaded before.
782
+ $this->_plugins_loaded();
783
+ }
784
+ }
785
+
786
+ if ( ! self::is_ajax() ) {
787
+ if ( ! $this->is_addon() ) {
788
+ add_action( 'init', array( &$this, '_add_default_submenu_items' ), WP_FS__LOWEST_PRIORITY );
789
+ }
790
+ }
791
+ }
792
+
793
+ if ( $this->is_plugin() ) {
794
+ register_deactivation_hook( $this->_plugin_main_file_path, array( &$this, '_deactivate_plugin_hook' ) );
795
+ }
796
+
797
+ if ( $this->is_theme() && self::is_customizer() ) {
798
+ // Register customizer upsell.
799
+ add_action( 'customize_register', array( &$this, '_customizer_register' ) );
800
+ }
801
+
802
+ add_action( 'init', array( &$this, '_redirect_on_clicked_menu_link' ), WP_FS__LOWEST_PRIORITY );
803
+
804
+ add_action( 'admin_init', array( &$this, '_add_tracking_links' ) );
805
+ add_action( 'admin_init', array( &$this, '_add_license_activation' ) );
806
+ $this->add_ajax_action( 'update_billing', array( &$this, '_update_billing_ajax_action' ) );
807
+ $this->add_ajax_action( 'start_trial', array( &$this, '_start_trial_ajax_action' ) );
808
+
809
+ $this->add_ajax_action( 'install_premium_version', array(
810
+ &$this,
811
+ '_install_premium_version_ajax_action'
812
+ ) );
813
+
814
+ $this->add_ajax_action( 'submit_affiliate_application', array( &$this, '_submit_affiliate_application' ) );
815
+
816
+ $this->add_action( 'after_plans_sync', array( &$this, '_check_for_trial_plans' ) );
817
+
818
+ $this->add_action( 'sdk_version_update', array( &$this, '_data_migration' ), WP_FS__DEFAULT_PRIORITY, 2 );
819
+ $this->add_action( 'plugin_version_update', array( &$this, '_after_version_update' ), WP_FS__DEFAULT_PRIORITY, 2 );
820
+ $this->add_filter( 'after_code_type_change', array( &$this, '_after_code_type_change' ) );
821
+
822
+ add_action( 'admin_init', array( &$this, '_add_trial_notice' ) );
823
+ add_action( 'admin_init', array( &$this, '_add_affiliate_program_notice' ) );
824
+ add_action( 'admin_init', array( &$this, '_enqueue_common_css' ) );
825
+
826
+ /**
827
+ * Handle request to reset anonymous mode for `get_reconnect_url()`.
828
+ *
829
+ * @author Vova Feldman (@svovaf)
830
+ * @since 1.2.1.5
831
+ */
832
+ if ( fs_request_is_action( 'reset_anonymous_mode' ) &&
833
+ $this->get_unique_affix() === fs_request_get( 'fs_unique_affix' )
834
+ ) {
835
+ add_action( 'admin_init', array( &$this, 'connect_again' ) );
836
+ }
837
+ }
838
+
839
+ /**
840
+ * Keeping the uninstall hook registered for free or premium plugin version may result to a fatal error that
841
+ * could happen when a user tries to uninstall either version while one of them is still active. Uninstalling a
842
+ * plugin will trigger inclusion of the free or premium version and if one of them is active during the
843
+ * uninstallation, a fatal error may occur in case the plugin's class or functions are already defined.
844
+ *
845
+ * @author Leo Fajardo (leorw)
846
+ *
847
+ * @since 1.2.0
848
+ */
849
+ private function unregister_uninstall_hook() {
850
+ $uninstallable_plugins = (array) get_option( 'uninstall_plugins' );
851
+ unset( $uninstallable_plugins[ $this->_free_plugin_basename ] );
852
+ unset( $uninstallable_plugins[ $this->premium_plugin_basename() ] );
853
+
854
+ update_option( 'uninstall_plugins', $uninstallable_plugins );
855
+ }
856
+
857
+ /**
858
+ * @since 1.2.0 Invalidate module's main file cache, otherwise, FS_Plugin_Updater will not fetch updates.
859
+ */
860
+ private function clear_module_main_file_cache() {
861
+ if ( ! isset( $this->_storage->plugin_main_file ) ||
862
+ empty( $this->_storage->plugin_main_file->path )
863
+ ) {
864
+ return;
865
+ }
866
+
867
+ $plugin_main_file = clone $this->_storage->plugin_main_file;
868
+
869
+ // Store cached path (2nd layer cache).
870
+ $plugin_main_file->prev_path = $plugin_main_file->path;
871
+
872
+ // Clear cached path.
873
+ unset( $plugin_main_file->path );
874
+
875
+ $this->_storage->plugin_main_file = $plugin_main_file;
876
+
877
+ /**
878
+ * Clear global cached path.
879
+ *
880
+ * @author Leo Fajardo (@leorw)
881
+ * @since 1.2.2
882
+ */
883
+ $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map' );
884
+ unset( $id_slug_type_path_map[ $this->_module_id ]['path'] );
885
+ self::$_accounts->set_option( 'id_slug_type_path_map', $id_slug_type_path_map, true );
886
+ }
887
+
888
+ /**
889
+ * @author Vova Feldman (@svovaf)
890
+ * @since 1.0.9
891
+ */
892
+ private function _register_account_hooks() {
893
+ if ( ! is_admin() ) {
894
+ return;
895
+ }
896
+
897
+ /**
898
+ * Always show the deactivation feedback form since we added
899
+ * automatic free version deactivation upon premium code activation.
900
+ *
901
+ * @since 1.2.1.6
902
+ */
903
+ $this->add_ajax_action(
904
+ 'submit_uninstall_reason',
905
+ array( &$this, '_submit_uninstall_reason_action' )
906
+ );
907
+
908
+ if ( ( $this->is_plugin() && self::is_plugins_page() ) ||
909
+ ( $this->is_theme() && self::is_themes_page() )
910
+ ) {
911
+ add_action( 'admin_footer', array( &$this, '_add_deactivation_feedback_dialog_box' ) );
912
+ }
913
+ }
914
+
915
+ /**
916
+ * Leverage backtrace to find caller plugin file path.
917
+ *
918
+ * @author Vova Feldman (@svovaf)
919
+ * @since 1.0.6
920
+ *
921
+ * @param bool $is_init Is initiation sequence.
922
+ *
923
+ * @return string
924
+ */
925
+ private function _find_caller_plugin_file( $is_init = false ) {
926
+ // Try to load the cached value of the file path.
927
+ if ( isset( $this->_storage->plugin_main_file ) ) {
928
+ $plugin_main_file = $this->_storage->plugin_main_file;
929
+ if ( isset( $plugin_main_file->path ) ) {
930
+ $absolute_path = $this->get_absolute_path( $plugin_main_file->path );
931
+ if ( file_exists( $absolute_path ) ) {
932
+ return $absolute_path;
933
+ }
934
+ }
935
+ }
936
+
937
+ /**
938
+ * @since 1.2.1
939
+ *
940
+ * `clear_module_main_file_cache()` is clearing the plugin's cached path on
941
+ * deactivation. Therefore, if any plugin/theme was initiating `Freemius`
942
+ * with that plugin's slug, it was overriding the empty plugin path with a wrong path.
943
+ *
944
+ * So, we've added a special mechanism with a 2nd layer of cache that uses `prev_path`
945
+ * when the class instantiator isn't the module.
946
+ */
947
+ if ( ! $is_init ) {
948
+ // Fetch prev path cache.
949
+ if ( isset( $this->_storage->plugin_main_file ) &&
950
+ isset( $this->_storage->plugin_main_file->prev_path )
951
+ ) {
952
+ $absolute_path = $this->get_absolute_path( $this->_storage->plugin_main_file->prev_path );
953
+ if ( file_exists( $absolute_path ) ) {
954
+ return $absolute_path;
955
+ }
956
+ }
957
+
958
+ wp_die(
959
+ $this->get_text_inline( 'Freemius SDK couldn\'t find the plugin\'s main file. Please contact sdk@freemius.com with the current error.', 'failed-finding-main-path' ) .
960
+ " Module: {$this->_slug}; SDK: " . WP_FS__SDK_VERSION . ";",
961
+ $this->get_text_inline( 'Error', 'error' ),
962
+ array( 'back_link' => true )
963
+ );
964
+ }
965
+
966
+ /**
967
+ * @since 1.2.1
968
+ *
969
+ * Only the original instantiator that calls dynamic_init can modify the module's path.
970
+ */
971
+ // Find caller module.
972
+ $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
973
+ $this->_storage->plugin_main_file = (object) array(
974
+ 'path' => $id_slug_type_path_map[ $this->_module_id ]['path'],
975
+ );
976
+
977
+ return $this->get_absolute_path( $id_slug_type_path_map[ $this->_module_id ]['path'] );
978
+ }
979
+
980
+ /**
981
+ * @author Leo Fajardo (@leorw)
982
+ * @since 1.2.3
983
+ *
984
+ * @param string $path
985
+ *
986
+ * @return string
987
+ */
988
+ private function get_relative_path( $path ) {
989
+ $module_root_dir = $this->get_module_root_dir_path();
990
+ if ( 0 === strpos( $path, $module_root_dir ) ) {
991
+ $path = substr( $path, strlen( $module_root_dir ) );
992
+ }
993
+
994
+ return $path;
995
+ }
996
+
997
+ /**
998
+ * @author Leo Fajardo (@leorw)
999
+ * @since 1.2.3
1000
+ *
1001
+ * @param string $path
1002
+ * @param string|bool $module_type
1003
+ *
1004
+ * @return string
1005
+ */
1006
+ private function get_absolute_path( $path, $module_type = false ) {
1007
+ $module_root_dir = $this->get_module_root_dir_path( $module_type );
1008
+ if ( 0 !== strpos( $path, $module_root_dir ) ) {
1009
+ $path = fs_normalize_path( $module_root_dir . $path );
1010
+ }
1011
+
1012
+ return $path;
1013
+ }
1014
+
1015
+ /**
1016
+ * @author Leo Fajardo (@leorw)
1017
+ * @since 1.2.3
1018
+ *
1019
+ * @param string|bool $module_type
1020
+ *
1021
+ * @return string
1022
+ */
1023
+ private function get_module_root_dir_path( $module_type = false ) {
1024
+ $is_plugin = empty( $module_type ) ?
1025
+ $this->is_plugin() :
1026
+ ( WP_FS__MODULE_TYPE_PLUGIN === $module_type );
1027
+
1028
+ return fs_normalize_path( trailingslashit( $is_plugin ?
1029
+ WP_PLUGIN_DIR :
1030
+ get_theme_root() ) );
1031
+ }
1032
+
1033
+ /**
1034
+ * @author Leo Fajardo (@leorw)
1035
+ *
1036
+ * @param number $module_id
1037
+ * @param string $slug
1038
+ *
1039
+ * @since 1.2.2
1040
+ */
1041
+ private function store_id_slug_type_path_map( $module_id, $slug ) {
1042
+ $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
1043
+
1044
+ $store_option = false;
1045
+
1046
+ if ( ! isset( $id_slug_type_path_map[ $module_id ] ) ) {
1047
+ $id_slug_type_path_map[ $module_id ] = array(
1048
+ 'slug' => $slug
1049
+ );
1050
+
1051
+ $store_option = true;
1052
+ }
1053
+
1054
+ if ( ! isset( $id_slug_type_path_map[ $module_id ]['path'] ) ||
1055
+ /**
1056
+ * This verification is for cases when suddenly the same module
1057
+ * is installed but with a different folder name.
1058
+ *
1059
+ * @author Vova Feldman (@svovaf)
1060
+ * @since 1.2.3
1061
+ */
1062
+ ! file_exists( $this->get_absolute_path(
1063
+ $id_slug_type_path_map[ $module_id ]['path'],
1064
+ $id_slug_type_path_map[ $module_id ]['type']
1065
+ ) )
1066
+ ) {
1067
+ $caller_main_file_and_type = $this->get_caller_main_file_and_type();
1068
+
1069
+ $id_slug_type_path_map[ $module_id ]['type'] = $caller_main_file_and_type->module_type;
1070
+ $id_slug_type_path_map[ $module_id ]['path'] = $caller_main_file_and_type->path;
1071
+
1072
+ $store_option = true;
1073
+ }
1074
+
1075
+ if ( $store_option ) {
1076
+ self::$_accounts->set_option( 'id_slug_type_path_map', $id_slug_type_path_map, true );
1077
+ }
1078
+ }
1079
+
1080
+ /**
1081
+ * Identifies the caller type: plugin or theme.
1082
+ *
1083
+ * @author Leo Fajardo (@leorw)
1084
+ * @since 1.2.2
1085
+ *
1086
+ * @author Vova Feldman (@svovaf)
1087
+ * @since 1.2.2.3 Find the earliest module in the call stack that calls to the SDK. This fix is for cases when
1088
+ * add-ons are relying on loading the SDK from the parent module, and also allows themes including the
1089
+ * SDK an internal file instead of directly from functions.php.
1090
+ * @since 1.2.1.7 Knows how to handle cases when an add-on includes the parent module logic.
1091
+ */
1092
+ private function get_caller_main_file_and_type() {
1093
+ self::require_plugin_essentials();
1094
+
1095
+ $all_plugins = get_plugins();
1096
+ $all_plugins_paths = array();
1097
+
1098
+ // Get active plugin's main files real full names (might be symlinks).
1099
+ foreach ( $all_plugins as $relative_path => &$data ) {
1100
  if ( false === strpos( fs_normalize_path( $relative_path ), '/' ) ) {
1101
  /**
1102
+ * Ignore plugins that don't have a folder (e.g. Hello Dolly) since they
1103
+ * can't really include the SDK.
1104
+ *
1105
+ * @author Vova Feldman
1106
+ * @since 1.2.1.7
1107
+ */
1108
+ continue;
1109
+ }
1110
+
1111
+ $all_plugins_paths[] = fs_normalize_path( realpath( WP_PLUGIN_DIR . '/' . $relative_path ) );
1112
+ }
1113
+
1114
+ $caller_file_candidate = false;
1115
+ $caller_map = array();
1116
+ $module_type = WP_FS__MODULE_TYPE_PLUGIN;
1117
+ $themes_dir = fs_normalize_path( get_theme_root() );
1118
+
1119
+ for ( $i = 1, $bt = debug_backtrace(), $len = count( $bt ); $i < $len; $i ++ ) {
1120
+ if ( empty( $bt[ $i ]['file'] ) ) {
1121
+ continue;
1122
+ }
1123
+
1124
+ if ( $i > 1 && ! empty( $bt[ $i - 1 ]['file'] ) && $bt[ $i ]['file'] === $bt[ $i - 1 ]['file'] ) {
1125
+ // If file same as the prev file in the stack, skip it.
1126
+ continue;
1127
+ }
1128
+
1129
+ if ( ! empty( $bt[ $i ]['function'] ) && in_array( $bt[ $i ]['function'], array(
1130
+ 'do_action',
1131
+ 'apply_filter',
1132
+ // The string split is stupid, but otherwise, theme check
1133
+ // throws info notices.
1134
+ 'requir' . 'e_once',
1135
+ 'requir' . 'e',
1136
+ 'includ' . 'e_once',
1137
+ 'includ' . 'e'
1138
+ ) )
1139
+ ) {
1140
+ // Ignore call stack hooks and files inclusion.
1141
+ continue;
1142
+ }
1143
+
1144
+ $caller_file_path = fs_normalize_path( $bt[ $i ]['file'] );
1145
+
1146
+ if ( 'functions.php' === basename( $caller_file_path ) ) {
1147
+ /**
1148
+ * 1. Assumes that theme's starting execution file is functions.php.
1149
+ * 2. This complex logic fixes symlink issues (e.g. with Vargant).
1150
+ *
1151
+ * @author Vova Feldman (@svovaf)
1152
+ * @since 1.2.2.5
1153
+ */
1154
+
1155
+ if ( $caller_file_path == fs_normalize_path( realpath( trailingslashit( $themes_dir ) . basename( dirname( $caller_file_path ) ) . '/' . basename( $caller_file_path ) ) ) ) {
1156
+ $module_type = WP_FS__MODULE_TYPE_THEME;
1157
+
1158
+ /**
1159
+ * Relative path of the theme, e.g.:
1160
+ * `my-theme/functions.php`
1161
+ *
1162
+ * @author Leo Fajardo (@leorw)
1163
+ */
1164
+ $caller_file_candidate = basename( dirname( $caller_file_path ) ) .
1165
+ '/' .
1166
+ basename( $caller_file_path );
1167
+
1168
+ continue;
1169
+ }
1170
+ }
1171
+
1172
+ $caller_file_hash = md5( $caller_file_path );
1173
+
1174
+ if ( ! isset( $caller_map[ $caller_file_hash ] ) ) {
1175
+ foreach ( $all_plugins_paths as $plugin_path ) {
1176
+ if ( false !== strpos( $caller_file_path, fs_normalize_path( dirname( $plugin_path ) . '/' ) ) ) {
1177
+ $caller_map[ $caller_file_hash ] = fs_normalize_path( $plugin_path );
1178
+ break;
1179
+ }
1180
+ }
1181
+ }
1182
+
1183
+ if ( isset( $caller_map[ $caller_file_hash ] ) ) {
1184
+ $module_type = WP_FS__MODULE_TYPE_PLUGIN;
1185
+ $caller_file_candidate = plugin_basename( $caller_map[ $caller_file_hash ] );
1186
+ }
1187
+ }
1188
+
1189
+ return (object) array(
1190
+ 'module_type' => $module_type,
1191
+ 'path' => $caller_file_candidate
1192
+ );
1193
+ }
1194
+
1195
+ #----------------------------------------------------------------------------------
1196
+ #region Deactivation Feedback Form
1197
+ #----------------------------------------------------------------------------------
1198
+
1199
+ /**
1200
+ * Displays a confirmation and feedback dialog box when the user clicks on the "Deactivate" link on the plugins
1201
+ * page.
1202
+ *
1203
+ * @author Vova Feldman (@svovaf)
1204
+ * @author Leo Fajardo (@leorw)
1205
+ * @since 1.1.2
1206
+ */
1207
+ function _add_deactivation_feedback_dialog_box() {
1208
+ /* Check the type of user:
1209
+ * 1. Long-term (long-term)
1210
+ * 2. Non-registered and non-anonymous short-term (non-registered-and-non-anonymous-short-term).
1211
+ * 3. Short-term (short-term)
1212
+ */
1213
+ $is_long_term_user = true;
1214
+
1215
+ // Check if the site is at least 2 days old.
1216
+ $time_installed = $this->_storage->install_timestamp;
1217
+
1218
+ // Difference in seconds.
1219
+ $date_diff = time() - $time_installed;
1220
+
1221
+ // Convert seconds to days.
1222
+ $date_diff_days = floor( $date_diff / ( 60 * 60 * 24 ) );
1223
+
1224
+ if ( $date_diff_days < 2 ) {
1225
+ $is_long_term_user = false;
1226
+ }
1227
+
1228
+ $is_long_term_user = $this->apply_filters( 'is_long_term_user', $is_long_term_user );
1229
+
1230
+ if ( $is_long_term_user ) {
1231
+ $user_type = 'long-term';
1232
+ } else {
1233
+ if ( ! $this->is_registered() && ! $this->is_anonymous() ) {
1234
+ $user_type = 'non-registered-and-non-anonymous-short-term';
1235
+ } else {
1236
+ $user_type = 'short-term';
1237
+ }
1238
+ }
1239
+
1240
+ $uninstall_reasons = $this->_get_uninstall_reasons( $user_type );
1241
+
1242
+ // Load the HTML template for the deactivation feedback dialog box.
1243
+ $vars = array(
1244
+ 'reasons' => $uninstall_reasons,
1245
+ 'id' => $this->_module_id
1246
+ );
1247
+
1248
+ /**
1249
+ * @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.
1250
+ */
1251
+ fs_require_template( 'forms/deactivation/form.php', $vars );
1252
+ }
1253
+
1254
+ /**
1255
+ * @author Leo Fajardo (leorw)
1256
+ * @since 1.1.2
1257
+ *
1258
+ * @param string $user_type
1259
+ *
1260
+ * @return array The uninstall reasons for the specified user type.
1261
+ */
1262
+ function _get_uninstall_reasons( $user_type = 'long-term' ) {
1263
+ $module_type = $this->_module_type;
1264
+
1265
+ $internal_message_template_var = array(
1266
+ 'id' => $this->_module_id
1267
+ );
1268
+
1269
+ if ( $this->is_registered() && false !== $this->get_plan() && $this->get_plan()->has_technical_support() ) {
1270
+ $contact_support_template = fs_get_template( 'forms/deactivation/contact.php', $internal_message_template_var );
1271
+ } else {
1272
+ $contact_support_template = '';
1273
+ }
1274
+
1275
+ $reason_found_better_plugin = array(
1276
+ 'id' => self::REASON_FOUND_A_BETTER_PLUGIN,
1277
+ 'text' => sprintf( $this->get_text_inline( 'I found a better %s', 'reason-found-a-better-plugin' ), $module_type ),
1278
+ 'input_type' => 'textfield',
1279
+ 'input_placeholder' => sprintf( $this->get_text_inline( "What's the %s's name?", 'placeholder-plugin-name' ), $module_type ),
1280
+ );
1281
+
1282
+ $reason_temporary_deactivation = array(
1283
+ 'id' => self::REASON_TEMPORARY_DEACTIVATION,
1284
+ 'text' => sprintf(
1285
+ $this->get_text_inline( "It's a temporary %s. I'm just debugging an issue.", 'reason-temporary-x' ),
1286
+ strtolower( $this->is_plugin() ?
1287
+ $this->get_text_inline( 'Deactivation', 'deactivation' ) :
1288
+ $this->get_text_inline( 'Theme Switch', 'theme-switch' )
1289
+ )
1290
+ ),
1291
+ 'input_type' => '',
1292
+ 'input_placeholder' => ''
1293
+ );
1294
+
1295
+ $reason_other = array(
1296
+ 'id' => self::REASON_OTHER,
1297
+ 'text' => $this->get_text_inline( 'Other', 'reason-other' ),
1298
+ 'input_type' => 'textfield',
1299
+ 'input_placeholder' => ''
1300
+ );
1301
+
1302
+ $long_term_user_reasons = array(
1303
+ array(
1304
+ 'id' => self::REASON_NO_LONGER_NEEDED,
1305
+ 'text' => sprintf( $this->get_text_inline( 'I no longer need the %s', 'reason-no-longer-needed' ), $module_type ),
1306
+ 'input_type' => '',
1307
+ 'input_placeholder' => ''
1308
+ ),
1309
+ $reason_found_better_plugin,
1310
+ array(
1311
+ 'id' => self::REASON_NEEDED_FOR_A_SHORT_PERIOD,
1312
+ 'text' => sprintf( $this->get_text_inline( 'I only needed the %s for a short period', 'reason-needed-for-a-short-period' ), $module_type ),
1313
+ 'input_type' => '',
1314
+ 'input_placeholder' => ''
1315
+ ),
1316
+ array(
1317
+ 'id' => self::REASON_BROKE_MY_SITE,
1318
+ 'text' => sprintf( $this->get_text_inline( 'The %s broke my site', 'reason-broke-my-site' ), $module_type ),
1319
+ 'input_type' => '',
1320
+ 'input_placeholder' => '',
1321
+ 'internal_message' => $contact_support_template
1322
+ ),
1323
+ array(
1324
+ 'id' => self::REASON_SUDDENLY_STOPPED_WORKING,
1325
+ 'text' => sprintf( $this->get_text_inline( 'The %s suddenly stopped working', 'reason-suddenly-stopped-working' ), $module_type ),
1326
+ 'input_type' => '',
1327
+ 'input_placeholder' => '',
1328
+ 'internal_message' => $contact_support_template
1329
+ )
1330
+ );
1331
+
1332
+ if ( $this->is_paying() ) {
1333
+ $long_term_user_reasons[] = array(
1334
+ 'id' => self::REASON_CANT_PAY_ANYMORE,
1335
+ 'text' => $this->get_text_inline( "I can't pay for it anymore", 'reason-cant-pay-anymore' ),
1336
+ 'input_type' => 'textfield',
1337
+ 'input_placeholder' => $this->get_text_inline( 'What price would you feel comfortable paying?', 'placeholder-comfortable-price' )
1338
+ );
1339
+ }
1340
+
1341
+ $reason_dont_share_info = array(
1342
+ 'id' => self::REASON_DONT_LIKE_TO_SHARE_MY_INFORMATION,
1343
+ 'text' => $this->get_text_inline( "I don't like to share my information with you", 'reason-dont-like-to-share-my-information' ),
1344
+ 'input_type' => '',
1345
+ 'input_placeholder' => ''
1346
+ );
1347
+
1348
+ /**
1349
+ * If the current user has selected the "don't share data" reason in the deactivation feedback modal, inform the
1350
+ * user by showing additional message that he doesn't have to share data and can just choose to skip the opt-in
1351
+ * (the Skip button is included in the message to show). This message will only be shown if anonymous mode is
1352
+ * enabled and the user's account is currently not in pending activation state (similar to the way the Skip
1353
+ * button in the opt-in form is shown/hidden).
1354
+ */
1355
+ if ( $this->is_enable_anonymous() && ! $this->is_pending_activation() ) {
1356
+ $reason_dont_share_info['internal_message'] = fs_get_template( 'forms/deactivation/retry-skip.php', $internal_message_template_var );
1357
+ }
1358
+
1359
+ $uninstall_reasons = array(
1360
+ 'long-term' => $long_term_user_reasons,
1361
+ 'non-registered-and-non-anonymous-short-term' => array(
1362
+ array(
1363
+ 'id' => self::REASON_DIDNT_WORK,
1364
+ 'text' => sprintf( $this->get_text_inline( "The %s didn't work", 'reason-didnt-work' ), $module_type ),
1365
+ 'input_type' => '',
1366
+ 'input_placeholder' => ''
1367
+ ),
1368
+ $reason_dont_share_info,
1369
+ $reason_found_better_plugin
1370
+ ),
1371
+ 'short-term' => array(
1372
+ array(
1373
+ 'id' => self::REASON_COULDNT_MAKE_IT_WORK,
1374
+ 'text' => $this->get_text_inline( "I couldn't understand how to make it work", 'reason-couldnt-make-it-work' ),
1375
+ 'input_type' => '',
1376
+ 'input_placeholder' => '',
1377
+ 'internal_message' => $contact_support_template
1378
+ ),
1379
+ $reason_found_better_plugin,
1380
+ array(
1381
+ 'id' => self::REASON_GREAT_BUT_NEED_SPECIFIC_FEATURE,
1382
+ 'text' => sprintf( $this->get_text_inline( "The %s is great, but I need specific feature that you don't support", 'reason-great-but-need-specific-feature' ), $module_type ),
1383
+ 'input_type' => 'textarea',
1384
+ 'input_placeholder' => $this->get_text_inline( 'What feature?', 'placeholder-feature' )
1385
+ ),
1386
+ array(
1387
+ 'id' => self::REASON_NOT_WORKING,
1388
+ 'text' => sprintf( $this->get_text_inline( 'The %s is not working', 'reason-not-working' ), $module_type ),
1389
+ 'input_type' => 'textarea',
1390
+ 'input_placeholder' => $this->get_text_inline( "Kindly share what didn't work so we can fix it for future users...", 'placeholder-share-what-didnt-work' )
1391
+ ),
1392
+ array(
1393
+ 'id' => self::REASON_NOT_WHAT_I_WAS_LOOKING_FOR,
1394
+ 'text' => $this->get_text_inline( "It's not what I was looking for", 'reason-not-what-i-was-looking-for' ),
1395
+ 'input_type' => 'textarea',
1396
+ 'input_placeholder' => $this->get_text_inline( "What you've been looking for?", 'placeholder-what-youve-been-looking-for' )
1397
+ ),
1398
+ array(
1399
+ 'id' => self::REASON_DIDNT_WORK_AS_EXPECTED,
1400
+ 'text' => sprintf( $this->get_text_inline( "The %s didn't work as expected", 'reason-didnt-work-as-expected' ), $module_type ),
1401
+ 'input_type' => 'textarea',
1402
+ 'input_placeholder' => $this->get_text_inline( 'What did you expect?', 'placeholder-what-did-you-expect' )
1403
+ )
1404
+ )
1405
+ );
1406
+
1407
+ // Randomize the reasons for the current user type.
1408
+ shuffle( $uninstall_reasons[ $user_type ] );
1409
+
1410
+ // Keep the following reasons as the last items in the list.
1411
+ $uninstall_reasons[ $user_type ][] = $reason_temporary_deactivation;
1412
+ $uninstall_reasons[ $user_type ][] = $reason_other;
1413
+
1414
+ $uninstall_reasons = $this->apply_filters( 'uninstall_reasons', $uninstall_reasons );
1415
+
1416
+ return $uninstall_reasons[ $user_type ];
1417
+ }
1418
+
1419
+ /**
1420
+ * Called after the user has submitted his reason for deactivating the plugin.
1421
+ *
1422
+ * @author Leo Fajardo (@leorw)
1423
+ * @since 1.1.2
1424
+ */
1425
+ function _submit_uninstall_reason_action() {
1426
+ $this->_logger->entrance();
1427
+
1428
+ $this->check_ajax_referer( 'submit_uninstall_reason' );
1429
+
1430
+ $reason_id = fs_request_get( 'reason_id' );
1431
+
1432
+ // Check if the given reason ID is an unsigned integer.
1433
+ if ( ! ctype_digit( $reason_id ) ) {
1434
+ exit;
1435
+ }
1436
+
1437
+ $reason_info = trim( fs_request_get( 'reason_info', '' ) );
1438
+ if ( ! empty( $reason_info ) ) {
1439
+ $reason_info = substr( $reason_info, 0, 128 );
1440
+ }
1441
+
1442
+ $reason = (object) array(
1443
+ 'id' => $reason_id,
1444
+ 'info' => $reason_info,
1445
+ 'is_anonymous' => fs_request_get_bool( 'is_anonymous' )
1446
+ );
1447
+
1448
+ $this->_storage->store( 'uninstall_reason', $reason );
1449
+
1450
+ /**
1451
+ * If the module type is "theme", trigger the uninstall event here (on theme deactivation) since themes do
1452
+ * not support uninstall hook.
1453
+ *
1454
+ * @author Leo Fajardo (@leorw)
1455
+ * @since 1.2.2
1456
+ */
1457
+ if ( $this->is_theme() ) {
1458
+ $this->_uninstall_plugin_event( false );
1459
+ $this->remove_sdk_reference();
1460
+ }
1461
+
1462
+ // Print '1' for successful operation.
1463
+ echo 1;
1464
+ exit;
1465
+ }
1466
+
1467
+ #endregion
1468
+
1469
+ #----------------------------------------------------------------------------------
1470
+ #region Instance
1471
+ #----------------------------------------------------------------------------------
1472
+
1473
+ /**
1474
+ * Main singleton instance.
1475
+ *
1476
+ * @author Vova Feldman (@svovaf)
1477
+ * @since 1.0.0
1478
+ *
1479
+ * @param number $module_id
1480
+ * @param string|bool $slug
1481
+ * @param bool $is_init Is initiation sequence.
1482
+ *
1483
+ * @return Freemius|false
1484
+ */
1485
+ static function instance( $module_id, $slug = false, $is_init = false ) {
1486
+ if ( empty( $module_id ) ) {
1487
+ return false;
1488
+ }
1489
+
1490
+ if ( ! is_numeric( $module_id ) ) {
1491
+ if ( ! $is_init && true === $slug ) {
1492
+ $is_init = true;
1493
+ }
1494
+
1495
+ $slug = $module_id;
1496
+
1497
+ $module = FS_Plugin_Manager::instance( $slug )->get();
1498
+
1499
+ if ( is_object( $module ) ) {
1500
+ $module_id = $module->id;
1501
+ }
1502
+ }
1503
+
1504
+ $key = 'm_' . $module_id;
1505
+
1506
+ if ( ! isset( self::$_instances[ $key ] ) ) {
1507
+ if ( 0 === count( self::$_instances ) ) {
1508
+ self::_load_required_static();
1509
+ }
1510
+
1511
+ self::$_instances[ $key ] = new Freemius( $module_id, $slug, $is_init );
1512
+ }
1513
+
1514
+ return self::$_instances[ $key ];
1515
+ }
1516
+
1517
+ /**
1518
+ * @author Vova Feldman (@svovaf)
1519
+ * @since 1.0.6
1520
+ *
1521
+ * @param number $addon_id
1522
+ *
1523
+ * @return bool
1524
+ */
1525
+ private static function has_instance( $addon_id ) {
1526
+ return isset( self::$_instances[ 'm_' . $addon_id ] );
1527
+ }
1528
+
1529
+ /**
1530
+ * @author Leo Fajardo (@leorw)
1531
+ * @since 1.2.2
1532
+ *
1533
+ * @param string|number $id_or_slug
1534
+ *
1535
+ * @return number|false
1536
+ */
1537
+ private static function get_module_id( $id_or_slug ) {
1538
+ if ( is_numeric( $id_or_slug ) ) {
1539
+ return $id_or_slug;
1540
+ }
1541
+
1542
+ foreach ( self::$_instances as $instance ) {
1543
+ if ( $instance->is_plugin() && ( $id_or_slug === $instance->get_slug() ) ) {
1544
+ return $instance->get_id();
1545
+ }
1546
+ }
1547
+
1548
+ return false;
1549
+ }
1550
+
1551
+ /**
1552
+ * @author Vova Feldman (@svovaf)
1553
+ * @since 1.0.6
1554
+ *
1555
+ * @param number $id
1556
+ *
1557
+ * @return false|Freemius
1558
+ */
1559
+ static function get_instance_by_id( $id ) {
1560
+ return isset ( self::$_instances[ 'm_' . $id ] ) ?
1561
+ self::$_instances[ 'm_' . $id ] :
1562
+ false;
1563
+ }
1564
+
1565
+ /**
1566
+ *
1567
+ * @author Vova Feldman (@svovaf)
1568
+ * @since 1.0.1
1569
+ *
1570
+ * @param $plugin_file
1571
+ *
1572
+ * @return false|Freemius
1573
+ */
1574
+ static function get_instance_by_file( $plugin_file ) {
1575
+ $slug = self::find_slug_by_basename( $plugin_file );
1576
+
1577
+ return ( false !== $slug ) ?
1578
+ self::instance( self::get_module_id( $slug ) ) :
1579
+ false;
1580
+ }
1581
+
1582
+ /**
1583
+ * @author Vova Feldman (@svovaf)
1584
+ * @since 1.0.6
1585
+ *
1586
+ * @return false|Freemius
1587
+ */
1588
+ function get_parent_instance() {
1589
+ return self::get_instance_by_id( $this->_plugin->parent_plugin_id );
1590
+ }
1591
+
1592
+ /**
1593
+ * @author Vova Feldman (@svovaf)
1594
+ * @since 1.0.6
1595
+ *
1596
+ * @param string|number $id_or_slug
1597
+ *
1598
+ * @return false|Freemius
1599
+ */
1600
+ function get_addon_instance( $id_or_slug ) {
1601
+ $addon_id = self::get_module_id( $id_or_slug );
1602
+
1603
+ return self::instance( $addon_id );
1604
+ }
1605
+
1606
+ #endregion ------------------------------------------------------------------
1607
+
1608
+ /**
1609
+ * @author Vova Feldman (@svovaf)
1610
+ * @since 1.0.6
1611
+ *
1612
+ * @return bool
1613
+ */
1614
+ function is_parent_plugin_installed() {
1615
+ $is_active = self::has_instance( $this->_plugin->parent_plugin_id );
1616
+
1617
+ if ( $is_active ) {
1618
+ return true;
1619
+ }
1620
+
1621
+ /**
1622
+ * Parent module might be a theme. If that's the case, the add-on's FS
1623
+ * instance will be loaded prior to the theme's FS instance, therefore,
1624
+ * we need to check if it's active with a "look ahead".
1625
+ *
1626
+ * @author Vova Feldman
1627
+ * @since 1.2.2.3
1628
+ */
1629
+ global $fs_active_plugins;
1630
+ if ( is_object( $fs_active_plugins ) && is_array( $fs_active_plugins->plugins ) ) {
1631
+ $active_theme = wp_get_theme();
1632
+
1633
+ foreach ( $fs_active_plugins->plugins as $sdk => $module ) {
1634
+ if ( WP_FS__MODULE_TYPE_THEME === $module->type ) {
1635
+ if ( $module->plugin_path == $active_theme->get_stylesheet() ) {
1636
+ // Parent module is a theme and it's currently active.
1637
+ return true;
1638
+ }
1639
+ }
1640
+ }
1641
+ }
1642
+
1643
+ return false;
1644
+ }
1645
+
1646
+ /**
1647
+ * Check if add-on parent plugin in activation mode.
1648
+ *
1649
+ * @author Vova Feldman (@svovaf)
1650
+ * @since 1.0.7
1651
+ *
1652
+ * @return bool
1653
+ */
1654
+ function is_parent_in_activation() {
1655
+ $parent_fs = $this->get_parent_instance();
1656
+ if ( ! is_object( $parent_fs ) ) {
1657
+ return false;
1658
+ }
1659
+
1660
+ return ( $parent_fs->is_activation_mode() );
1661
+ }
1662
+
1663
+ /**
1664
+ * Is plugin in activation mode.
1665
+ *
1666
+ * @author Vova Feldman (@svovaf)
1667
+ * @since 1.0.7
1668
+ *
1669
+ * @param bool $and_on
1670
+ *
1671
+ * @return bool
1672
+ */
1673
+ function is_activation_mode( $and_on = true ) {
1674
+ return (
1675
+ ( $this->is_on() || ! $and_on ) &&
1676
+ ( ! $this->is_registered() || ( $this->is_only_premium() && ! $this->has_features_enabled_license() ) ) &&
1677
+ ( ! $this->is_enable_anonymous() ||
1678
+ ( ! $this->is_anonymous() && ! $this->is_pending_activation() ) )
1679
+ );
1680
+ }
1681
+
1682
+ /**
1683
+ * Check if current page is the opt-in/pending-activation page.
1684
+ *
1685
+ * @author Vova Feldman (@svovaf)
1686
+ * @since 1.2.1.7
1687
+ *
1688
+ * @return bool
1689
+ */
1690
+ function is_activation_page() {
1691
+ if ( $this->_menu->is_main_settings_page() ) {
1692
+ return true;
1693
+ }
1694
+
1695
+ if ( ! $this->is_activation_mode() ) {
1696
+ return false;
1697
+ }
1698
+
1699
+ // Check if current page is matching the activation page.
1700
+ return $this->is_matching_url( $this->get_activation_url() );
1701
+ }
1702
+
1703
+ /**
1704
+ * Check if URL path's are matching and that all querystring
1705
+ * arguments of the $sub_url exist in the $url with the same values.
1706
+ *
1707
+ * WARNING:
1708
+ * 1. This method doesn't check if the sub/domain are matching.
1709
+ * 2. Ignore case sensitivity.
1710
+ *
1711
+ * @author Vova Feldman (@svovaf)
1712
+ * @since 1.2.1.7
1713
+ *
1714
+ * @param string $sub_url
1715
+ * @param string $url If argument is not set, check if the sub_url matching the current's page URL.
1716
+ *
1717
+ * @return bool
1718
+ */
1719
+ private function is_matching_url( $sub_url, $url = '' ) {
1720
+ if ( empty( $url ) ) {
1721
+ $url = $_SERVER['REQUEST_URI'];
1722
+ }
1723
+
1724
+ $url = strtolower( $url );
1725
+ $sub_url = strtolower( $sub_url );
1726
+
1727
+ if ( parse_url( $sub_url, PHP_URL_PATH ) !== parse_url( $url, PHP_URL_PATH ) ) {
1728
+ // Different path - DO NOT OVERRIDE PAGE.
1729
+ return false;
1730
+ }
1731
+
1732
+ $url_params = array();
1733
+ parse_str( parse_url( $url, PHP_URL_QUERY ), $url_params );
1734
+
1735
+ $sub_url_params = array();
1736
+ parse_str( parse_url( $sub_url, PHP_URL_QUERY ), $sub_url_params );
1737
+
1738
+ foreach ( $sub_url_params as $key => $val ) {
1739
+ if ( ! isset( $url_params[ $key ] ) || $val != $url_params[ $key ] ) {
1740
+ // Not matching query string - DO NOT OVERRIDE PAGE.
1741
+ return false;
1742
+ }
1743
+ }
1744
+
1745
+ return true;
1746
+ }
1747
+
1748
+ /**
1749
+ * Get collection of all active plugins.
1750
+ *
1751
+ * @author Vova Feldman (@svovaf)
1752
+ * @since 1.0.9
1753
+ *
1754
+ * @return array[string]array
1755
+ */
1756
+ private static function get_active_plugins() {
1757
+ self::require_plugin_essentials();
1758
+
1759
+ $active_plugin = array();
1760
+ $all_plugins = get_plugins();
1761
+ $active_plugins_basenames = get_option( 'active_plugins' );
1762
+
1763
+ foreach ( $active_plugins_basenames as $plugin_basename ) {
1764
+ $active_plugin[ $plugin_basename ] = $all_plugins[ $plugin_basename ];
1765
+ }
1766
+
1767
+ return $active_plugin;
1768
+ }
1769
+
1770
+ /**
1771
+ * Get collection of all plugins.
1772
+ *
1773
+ * @author Vova Feldman (@svovaf)
1774
+ * @since 1.1.8
1775
+ *
1776
+ * @return array Key is the plugin file path and the value is an array of the plugin data.
1777
+ */
1778
+ private static function get_all_plugins() {
1779
+ self::require_plugin_essentials();
1780
+
1781
+ $all_plugins = get_plugins();
1782
+ $active_plugins_basenames = get_option( 'active_plugins' );
1783
+
1784
+ foreach ( $all_plugins as $basename => &$data ) {
1785
+ // By default set to inactive (next foreach update the active plugins).
1786
+ $data['is_active'] = false;
1787
+ // Enrich with plugin slug.
1788
+ $data['slug'] = self::get_plugin_slug( $basename );
1789
+ }
1790
+
1791
+ // Flag active plugins.
1792
+ foreach ( $active_plugins_basenames as $basename ) {
1793
+ if ( isset( $all_plugins[ $basename ] ) ) {
1794
+ $all_plugins[ $basename ]['is_active'] = true;
1795
+ }
1796
+ }
1797
+
1798
+ return $all_plugins;
1799
+ }
1800
+
1801
+
1802
+ /**
1803
+ * Cached result of get_site_transient( 'update_plugins' )
1804
+ *
1805
+ * @author Vova Feldman (@svovaf)
1806
+ * @since 1.1.8
1807
+ *
1808
+ * @var object
1809
+ */
1810
+ private static $_plugins_info;
1811
+
1812
+ /**
1813
+ * Helper function to get specified plugin's slug.
1814
+ *
1815
+ * @author Vova Feldman (@svovaf)
1816
+ * @since 1.1.8
1817
+ *
1818
+ * @param $basename
1819
+ *
1820
+ * @return string
1821
+ */
1822
+ private static function get_plugin_slug( $basename ) {
1823
+ if ( ! isset( self::$_plugins_info ) ) {
1824
+ self::$_plugins_info = get_site_transient( 'update_plugins' );
1825
+ }
1826
+
1827
+ $slug = '';
1828
+
1829
+ if ( is_object( self::$_plugins_info ) ) {
1830
+ if ( isset( self::$_plugins_info->no_update ) &&
1831
+ isset( self::$_plugins_info->no_update[ $basename ] ) &&
1832
+ ! empty( self::$_plugins_info->no_update[ $basename ]->slug )
1833
+ ) {
1834
+ $slug = self::$_plugins_info->no_update[ $basename ]->slug;
1835
+ } else if ( isset( self::$_plugins_info->response ) &&
1836
+ isset( self::$_plugins_info->response[ $basename ] ) &&
1837
+ ! empty( self::$_plugins_info->response[ $basename ]->slug )
1838
+ ) {
1839
+ $slug = self::$_plugins_info->response[ $basename ]->slug;
1840
+ }
1841
+ }
1842
+
1843
+ if ( empty( $slug ) ) {
1844
+ // Try to find slug from FS data.
1845
+ $slug = self::find_slug_by_basename( $basename );
1846
+ }
1847
+
1848
+ if ( empty( $slug ) ) {
1849
+ // Fallback to plugin's folder name.
1850
+ $slug = dirname( $basename );
1851
+ }
1852
+
1853
+ return $slug;
1854
+ }
1855
+
1856
+ private static $_statics_loaded = false;
1857
+
1858
+ /**
1859
+ * Load static resources.
1860
+ *
1861
+ * @author Vova Feldman (@svovaf)
1862
+ * @since 1.0.1
1863
+ */
1864
+ private static function _load_required_static() {
1865
+ if ( self::$_statics_loaded ) {
1866
+ return;
1867
+ }
1868
+
1869
+ self::$_static_logger = FS_Logger::get_logger( WP_FS__SLUG, WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
1870
+
1871
+ self::$_static_logger->entrance();
1872
+
1873
+ self::$_accounts = FS_Option_Manager::get_manager( WP_FS__ACCOUNTS_OPTION_NAME, true );
1874
+
1875
+ self::$_global_admin_notices = FS_Admin_Notice_Manager::instance( 'global' );
1876
+
1877
+ add_action( 'admin_menu', array( 'Freemius', '_add_debug_section' ) );
1878
+
1879
+ add_action( "wp_ajax_fs_toggle_debug_mode", array( 'Freemius', '_toggle_debug_mode' ) );
1880
+
1881
+ self::add_ajax_action_static( 'get_debug_log', array( 'Freemius', '_get_debug_log' ) );
1882
+
1883
+ self::add_ajax_action_static( 'get_db_option', array( 'Freemius', '_get_db_option' ) );
1884
+
1885
+ self::add_ajax_action_static( 'set_db_option', array( 'Freemius', '_set_db_option' ) );
1886
+
1887
+ if ( 0 == did_action( 'plugins_loaded' ) ) {
1888
+ add_action( 'plugins_loaded', array( 'Freemius', '_load_textdomain' ), 1 );
1889
+ }
1890
+
1891
+ self::$_statics_loaded = true;
1892
+ }
1893
+
1894
+ #----------------------------------------------------------------------------------
1895
+ #region Localization
1896
+ #----------------------------------------------------------------------------------
1897
+
1898
+ /**
1899
+ * Load framework's text domain.
1900
+ *
1901
+ * @author Vova Feldman (@svovaf)
1902
+ * @since 1.2.1
1903
+ */
1904
+ static function _load_textdomain() {
1905
+ if ( ! is_admin() ) {
1906
+ return;
1907
+ }
1908
+
1909
+ global $fs_active_plugins;
1910
+
1911
+ // Works both for plugins and themes.
1912
+ load_plugin_textdomain(
1913
+ 'freemius',
1914
+ false,
1915
+ $fs_active_plugins->newest->sdk_path . '/languages/'
1916
+ );
1917
+ }
1918
+
1919
+ #endregion
1920
+
1921
+ #----------------------------------------------------------------------------------
1922
+ #region Debugging
1923
+ #----------------------------------------------------------------------------------
1924
+
1925
+ /**
1926
+ * @author Vova Feldman (@svovaf)
1927
+ * @since 1.0.8
1928
+ */
1929
+ static function _add_debug_section() {
1930
+ if ( ! current_user_can( 'activate_plugins' )
1931
+ && ! current_user_can( 'switch_themes' )
1932
+ ) {
1933
+ return;
1934
+ }
1935
+
1936
+ self::$_static_logger->entrance();
1937
+
1938
+ $title = sprintf( '%s [v.%s]', fs_text_inline( 'Freemius Debug' ), WP_FS__SDK_VERSION );
1939
+
1940
+ if ( WP_FS__DEV_MODE ) {
1941
+ // Add top-level debug menu item.
1942
+ $hook = FS_Admin_Menu_Manager::add_page(
1943
+ $title,
1944
+ $title,
1945
+ 'manage_options',
1946
+ 'freemius',
1947
+ array( 'Freemius', '_debug_page_render' )
1948
+ );
1949
+ } else {
1950
+ // Add hidden debug page.
1951
+ $hook = FS_Admin_Menu_Manager::add_subpage(
1952
+ null,
1953
+ $title,
1954
+ $title,
1955
+ 'manage_options',
1956
+ 'freemius',
1957
+ array( 'Freemius', '_debug_page_render' )
1958
+ );
1959
+ }
1960
+
1961
+ if ( ! empty( $hook ) ) {
1962
+ add_action( "load-$hook", array( 'Freemius', '_debug_page_actions' ) );
1963
+ }
1964
+ }
1965
+
1966
+ /**
1967
+ * @author Vova Feldman (@svovaf)
1968
+ * @since 1.1.7.3
1969
+ */
1970
+ static function _toggle_debug_mode() {
1971
+ $is_on = fs_request_get( 'is_on', false, 'post' );
1972
+
1973
+ if ( fs_request_is_post() && in_array( $is_on, array( 0, 1 ) ) ) {
1974
+ update_option( 'fs_debug_mode', $is_on );
1975
+
1976
+ // Turn on/off storage logging.
1977
+ FS_Logger::_set_storage_logging( ( 1 == $is_on ) );
1978
+ }
1979
+
1980
+ exit;
1981
+ }
1982
+
1983
+ /**
1984
+ * @author Vova Feldman (@svovaf)
1985
+ * @since 1.2.1.6
1986
+ */
1987
+ static function _get_debug_log() {
1988
+ $logs = FS_Logger::load_db_logs(
1989
+ fs_request_get( 'filters', false, 'post' ),
1990
+ ! empty( $_POST['limit'] ) && is_numeric( $_POST['limit'] ) ? $_POST['limit'] : 200,
1991
+ ! empty( $_POST['offset'] ) && is_numeric( $_POST['offset'] ) ? $_POST['offset'] : 0
1992
+ );
1993
+
1994
+ self::shoot_ajax_success( $logs );
1995
+ }
1996
+
1997
+ /**
1998
+ * @author Vova Feldman (@svovaf)
1999
+ * @since 1.2.1.7
2000
+ */
2001
+ static function _get_db_option() {
2002
+ $option_name = fs_request_get( 'option_name' );
2003
+
2004
+ $value = get_option( $option_name );
2005
+
2006
+ $result = array(
2007
+ 'name' => $option_name,
2008
+ );
2009
+
2010
+ if ( false !== $value ) {
2011
+ if ( ! is_string( $value ) ) {
2012
+ $value = json_encode( $value );
2013
+ }
2014
+
2015
+ $result['value'] = $value;
2016
+ }
2017
+
2018
+ self::shoot_ajax_success( $result );
2019
+ }
2020
+
2021
+ /**
2022
+ * @author Vova Feldman (@svovaf)
2023
+ * @since 1.2.1.7
2024
+ */
2025
+ static function _set_db_option() {
2026
+ $option_name = fs_request_get( 'option_name' );
2027
+ $option_value = fs_request_get( 'option_value' );
2028
+
2029
+ if ( ! empty( $option_value ) ) {
2030
+ update_option( $option_name, $option_value );
2031
+ }
2032
+
2033
+ self::shoot_ajax_success();
2034
+ }
2035
+
2036
+ /**
2037
+ * @author Vova Feldman (@svovaf)
2038
+ * @since 1.0.8
2039
+ */
2040
+ static function _debug_page_actions() {
2041
+ self::_clean_admin_content_section();
2042
+
2043
+ if ( fs_request_is_action( 'restart_freemius' ) ) {
2044
+ check_admin_referer( 'restart_freemius' );
2045
+
2046
+ // Clear accounts data.
2047
+ self::$_accounts->clear( true );
2048
+
2049
+ // Clear SDK reference cache.
2050
+ delete_option( 'fs_active_plugins' );
2051
+ } else if ( fs_request_is_action( 'simulate_trial' ) ) {
2052
+ check_admin_referer( 'simulate_trial' );
2053
+
2054
+ $fs = freemius( fs_request_get( 'module_id' ) );
2055
+
2056
+ // Update SDK install to at least 24 hours before.
2057
+ $fs->_storage->install_timestamp = ( time() - WP_FS__TIME_24_HOURS_IN_SEC );
2058
+ // Unset the trial shown timestamp.
2059
+ unset( $fs->_storage->trial_promotion_shown );
2060
+ } else if ( fs_request_is_action( 'delete_install' ) ) {
2061
+ check_admin_referer( 'delete_install' );
2062
+
2063
+ self::_delete_site_by_slug(
2064
+ fs_request_get( 'slug' ),
2065
+ fs_request_get( 'module_type' )
2066
+ );
2067
+ } else if ( fs_request_is_action( 'download_logs' ) ) {
2068
+ check_admin_referer( 'download_logs' );
2069
+
2070
+ $download_url = FS_Logger::download_db_logs(
2071
+ fs_request_get( 'filters', false, 'post' )
2072
+ );
2073
+
2074
+ if ( false === $download_url ) {
2075
+ 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.' );
2076
+ }
2077
+
2078
+ fs_redirect( $download_url );
2079
+ }
2080
+ }
2081
+
2082
+ /**
2083
+ * @author Vova Feldman (@svovaf)
2084
+ * @since 1.0.8
2085
+ */
2086
+ static function _debug_page_render() {
2087
+ self::$_static_logger->entrance();
2088
+
2089
+ $vars = array(
2090
+ 'plugin_sites' => self::get_all_sites(),
2091
+ 'theme_sites' => self::get_all_sites( WP_FS__MODULE_TYPE_THEME ),
2092
+ 'users' => self::get_all_users(),
2093
+ 'addons' => self::get_all_addons(),
2094
+ 'account_addons' => self::get_all_account_addons(),
2095
+ 'plugin_licenses' => self::get_all_licenses(),
2096
+ 'theme_licenses' => self::get_all_licenses( WP_FS__MODULE_TYPE_THEME )
2097
+ );
2098
+
2099
+ fs_enqueue_local_style( 'fs_debug', '/admin/debug.css' );
2100
+ fs_require_once_template( 'debug.php', $vars );
2101
+ }
2102
+
2103
+ #endregion
2104
+
2105
+ #----------------------------------------------------------------------------------
2106
+ #region Connectivity Issues
2107
+ #----------------------------------------------------------------------------------
2108
+
2109
+ /**
2110
+ * Check if Freemius should be turned on for the current plugin install.
2111
+ *
2112
+ * Note:
2113
+ * $this->_is_on is updated in has_api_connectivity()
2114
+ *
2115
+ * @author Vova Feldman (@svovaf)
2116
+ * @since 1.0.9
2117
+ *
2118
+ * @return bool
2119
+ */
2120
+ function is_on() {
2121
+ self::$_static_logger->entrance();
2122
+
2123
+ if ( isset( $this->_is_on ) ) {
2124
+ return $this->_is_on;
2125
+ }
2126
+
2127
+ // If already installed or pending then sure it's on :)
2128
+ if ( $this->is_registered() || $this->is_pending_activation() ) {
2129
+ $this->_is_on = true;
2130
+
2131
+ return true;
2132
+ }
2133
+
2134
+ return false;
2135
+ }
2136
+
2137
+ /**
2138
+ * @author Vova Feldman (@svovaf)
2139
+ * @since 1.1.7.3
2140
+ *
2141
+ * @param bool $flush_if_no_connectivity
2142
+ *
2143
+ * @return bool
2144
+ */
2145
+ private function should_run_connectivity_test( $flush_if_no_connectivity = false ) {
2146
+ if ( ! isset( $this->_storage->connectivity_test ) ) {
2147
+ // Connectivity test was never executed, or cache was cleared.
2148
+ return true;
2149
+ }
2150
+
2151
+ if ( WP_FS__PING_API_ON_IP_OR_HOST_CHANGES ) {
2152
+ if ( WP_FS__IS_HTTP_REQUEST ) {
2153
+ if ( $_SERVER['HTTP_HOST'] != $this->_storage->connectivity_test['host'] ) {
2154
+ // Domain changed.
2155
+ return true;
2156
+ }
2157
+
2158
+ if ( WP_FS__REMOTE_ADDR != $this->_storage->connectivity_test['server_ip'] ) {
2159
+ // Server IP changed.
2160
+ return true;
2161
+ }
2162
+ }
2163
+ }
2164
+
2165
+ if ( $this->_storage->connectivity_test['is_connected'] &&
2166
+ $this->_storage->connectivity_test['is_active']
2167
+ ) {
2168
+ // API connected and Freemius is active - no need to run connectivity check.
2169
+ return false;
2170
+ }
2171
+
2172
+ if ( $flush_if_no_connectivity ) {
2173
+ /**
2174
+ * If explicitly asked to flush when no connectivity - do it only
2175
+ * if at least 10 sec passed from the last API connectivity test.
2176
+ */
2177
+ return ( isset( $this->_storage->connectivity_test['timestamp'] ) &&
2178
+ ( WP_FS__SCRIPT_START_TIME - $this->_storage->connectivity_test['timestamp'] ) > 10 );
2179
+ }
2180
+
2181
+ /**
2182
+ * @since 1.1.7 Don't check for connectivity on plugin downgrade.
2183
+ */
2184
+ $version = $this->get_plugin_version();
2185
+ if ( version_compare( $version, $this->_storage->connectivity_test['version'], '>' ) ) {
2186
+ // If it's a plugin version upgrade and Freemius is off or no connectivity, run connectivity test.
2187
+ return true;
2188
+ }
2189
+
2190
+ return false;
2191
+ }
2192
+
2193
+ /**
2194
+ * @author Vova Feldman (@svovaf)
2195
+ * @since 1.1.7.4
2196
+ *
2197
+ * @return object|false
2198
+ */
2199
+ private function ping() {
2200
+ if ( WP_FS__SIMULATE_NO_API_CONNECTIVITY ) {
2201
+ return false;
2202
+ }
2203
+
2204
+ $version = $this->get_plugin_version();
2205
+
2206
+ $is_update = $this->apply_filters( 'is_plugin_update', $this->is_plugin_update() );
2207
+
2208
+ return $this->get_api_plugin_scope()->ping(
2209
+ $this->get_anonymous_id(),
2210
+ array(
2211
+ 'is_update' => json_encode( $is_update ),
2212
+ 'version' => $version,
2213
+ 'sdk' => $this->version,
2214
+ 'is_admin' => json_encode( is_admin() ),
2215
+ 'is_ajax' => json_encode( self::is_ajax() ),
2216
+ 'is_cron' => json_encode( self::is_cron() ),
2217
+ 'is_http' => json_encode( WP_FS__IS_HTTP_REQUEST ),
2218
+ )
2219
+ );
2220
+ }
2221
+
2222
+ /**
2223
+ * Check if there's any connectivity issue to Freemius API.
2224
+ *
2225
+ * @author Vova Feldman (@svovaf)
2226
+ * @since 1.0.9
2227
+ *
2228
+ * @param bool $flush_if_no_connectivity
2229
+ *
2230
+ * @return bool
2231
+ */
2232
+ function has_api_connectivity( $flush_if_no_connectivity = false ) {
2233
+ $this->_logger->entrance();
2234
+
2235
+ if ( isset( $this->_has_api_connection ) && ( $this->_has_api_connection || ! $flush_if_no_connectivity ) ) {
2236
+ return $this->_has_api_connection;
2237
+ }
2238
+
2239
+ if ( WP_FS__SIMULATE_NO_API_CONNECTIVITY &&
2240
+ isset( $this->_storage->connectivity_test ) &&
2241
+ true === $this->_storage->connectivity_test['is_connected']
2242
+ ) {
2243
+ unset( $this->_storage->connectivity_test );
2244
+ }
2245
+
2246
+ if ( ! $this->should_run_connectivity_test( $flush_if_no_connectivity ) ) {
2247
+ $this->_has_api_connection = $this->_storage->connectivity_test['is_connected'];
2248
+ /**
2249
+ * @since 1.1.6 During dev mode, if there's connectivity - turn Freemius on regardless the configuration.
2250
+ *
2251
+ * @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.
2252
+ */
2253
+ $this->_is_on = $this->_storage->connectivity_test['is_active'] ||
2254
+ $this->is_premium() ||
2255
+ ( WP_FS__DEV_MODE && $this->_has_api_connection && ! WP_FS__SIMULATE_FREEMIUS_OFF );
2256
+
2257
+ return $this->_has_api_connection;
2258
+ }
2259
+
2260
+ $pong = $this->ping();
2261
+ $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong );
2262
+
2263
+ if ( ! $is_connected ) {
2264
+ // API failure.
2265
+ $this->_add_connectivity_issue_message( $pong );
2266
+ }
2267
+
2268
+ $this->store_connectivity_info( $pong, $is_connected );
2269
+
2270
+ return $this->_has_api_connection;
2271
+ }
2272
+
2273
+ /**
2274
+ * @author Vova Feldman (@svovaf)
2275
+ * @since 1.1.7.4
2276
+ *
2277
+ * @param object $pong
2278
+ * @param bool $is_connected
2279
+ */
2280
+ private function store_connectivity_info( $pong, $is_connected ) {
2281
+ $this->_logger->entrance();
2282
+
2283
+ $version = $this->get_plugin_version();
2284
+
2285
+ if ( ! $is_connected || WP_FS__SIMULATE_FREEMIUS_OFF ) {
2286
+ $is_active = false;
2287
+ } else {
2288
+ $is_active = ( isset( $pong->is_active ) && true == $pong->is_active );
2289
+ }
2290
+
2291
+ $is_active = $this->apply_filters(
2292
+ 'is_on',
2293
+ $is_active,
2294
+ $this->is_plugin_update(),
2295
+ $version
2296
+ );
2297
+
2298
+ $this->_storage->connectivity_test = array(
2299
+ 'is_connected' => $is_connected,
2300
+ 'host' => $_SERVER['HTTP_HOST'],
2301
+ 'server_ip' => WP_FS__REMOTE_ADDR,
2302
+ 'is_active' => $is_active,
2303
+ 'timestamp' => WP_FS__SCRIPT_START_TIME,
2304
+ // Last version with connectivity attempt.
2305
+ 'version' => $version,
2306
+ );
2307
+
2308
+ $this->_has_api_connection = $is_connected;
2309
+ $this->_is_on = $is_active || ( WP_FS__DEV_MODE && $is_connected && ! WP_FS__SIMULATE_FREEMIUS_OFF );
2310
+ }
2311
+
2312
+ /**
2313
+ * Force turning Freemius on.
2314
+ *
2315
+ * @author Vova Feldman (@svovaf)
2316
+ * @since 1.1.8.1
2317
+ *
2318
+ * @return bool TRUE if successfully turned on.
2319
+ */
2320
+ private function turn_on() {
2321
+ $this->_logger->entrance();
2322
+
2323
+ if ( $this->is_on() || ! isset( $this->_storage->connectivity_test['is_active'] ) ) {
2324
+ return false;
2325
+ }
2326
+
2327
+ $updated_connectivity = $this->_storage->connectivity_test;
2328
+ $updated_connectivity['is_active'] = true;
2329
+ $updated_connectivity['timestamp'] = WP_FS__SCRIPT_START_TIME;
2330
+ $this->_storage->connectivity_test = $updated_connectivity;
2331
+
2332
+ $this->_is_on = true;
2333
+
2334
+ return true;
2335
+ }
2336
+
2337
+ /**
2338
+ * Anonymous and unique site identifier (Hash).
2339
+ *
2340
+ * @author Vova Feldman (@svovaf)
2341
+ * @since 1.1.0
2342
+ *
2343
+ * @return string
2344
+ */
2345
+ function get_anonymous_id() {
2346
+ $unique_id = self::$_accounts->get_option( 'unique_id' );
2347
+
2348
+ if ( empty( $unique_id ) || ! is_string( $unique_id ) ) {
2349
+ $key = get_site_url();
2350
+
2351
+ // If localhost, assign microtime instead of domain.
2352
+ if ( WP_FS__IS_LOCALHOST ||
2353
+ false !== strpos( $key, 'localhost' ) ||
2354
+ false === strpos( $key, '.' )
2355
+ ) {
2356
+ $key = microtime();
2357
+ }
2358
+
2359
+ /**
2360
+ * Base the unique identifier on the WP secure authentication key. Which
2361
+ * turns the key into a secret anonymous identifier.
2362
+ *
2363
+ * @author Vova Feldman (@svovaf)
2364
+ * @since 1.2.3
2365
+ */
2366
+ $unique_id = md5( $key . SECURE_AUTH_KEY );
2367
+
2368
+ self::$_accounts->set_option( 'unique_id', $unique_id, true );
2369
+ }
2370
+
2371
+ $this->_logger->departure( $unique_id );
2372
+
2373
+ return $unique_id;
2374
+ }
2375
+
2376
+ /**
2377
+ * @author Vova Feldman (@svovaf)
2378
+ * @since 1.1.7.4
2379
+ *
2380
+ * @return \WP_User
2381
+ */
2382
+ static function _get_current_wp_user() {
2383
+ self::require_pluggable_essentials();
2384
+
2385
+ return wp_get_current_user();
2386
+ }
2387
+
2388
+ /**
2389
+ * @author Vova Feldman (@svovaf)
2390
+ * @since 1.2.1.7
2391
+ *
2392
+ * @param string $email
2393
+ *
2394
+ * @return bool
2395
+ */
2396
+ static function is_valid_email( $email ) {
2397
+ if ( false === filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
2398
+ return false;
2399
+ }
2400
+
2401
+ $parts = explode( '@', $email );
2402
+
2403
+ if ( 2 !== count( $parts ) || empty( $parts[1] ) ) {
2404
+ return false;
2405
+ }
2406
+
2407
+ $blacklist = array(
2408
+ 'admin.',
2409
+ 'webmaster.',
2410
+ 'localhost.',
2411
+ 'dev.',
2412
+ 'development.',
2413
+ 'test.',
2414
+ 'stage.',
2415
+ 'staging.',
2416
+ );
2417
+
2418
+ // Make sure domain is not one of the blacklisted.
2419
+ foreach ( $blacklist as $invalid ) {
2420
+ if ( 0 === strpos( $parts[1], $invalid ) ) {
2421
+ return false;
2422
+ }
2423
+ }
2424
+
2425
+ // Get the UTF encoded domain name.
2426
+ $domain = idn_to_ascii( $parts[1] ) . '.';
2427
+
2428
+ return ( checkdnsrr( $domain, 'MX' ) || checkdnsrr( $domain, 'A' ) );
2429
+ }
2430
+
2431
+ /**
2432
+ * Generate API connectivity issue message.
2433
+ *
2434
+ * @author Vova Feldman (@svovaf)
2435
+ * @since 1.0.9
2436
+ *
2437
+ * @param mixed $api_result
2438
+ * @param bool $is_first_failure
2439
+ */
2440
+ function _add_connectivity_issue_message( $api_result, $is_first_failure = true ) {
2441
+ if ( ! $this->is_premium() && $this->_enable_anonymous ) {
2442
+ // Don't add message if it's the free version and can run anonymously.
2443
+ return;
2444
+ }
2445
+
2446
+ if ( ! function_exists( 'wp_nonce_url' ) ) {
2447
+ require_once ABSPATH . 'wp-includes/functions.php';
2448
+ }
2449
+
2450
+ $current_user = self::_get_current_wp_user();
2451
+ // $admin_email = get_option( 'admin_email' );
2452
+ $admin_email = $current_user->user_email;
2453
+
2454
+ // Aliases.
2455
+ $deactivate_plugin_title = $this->esc_html_inline( 'That\'s exhausting, please deactivate', 'deactivate-plugin-title' );
2456
+ $deactivate_plugin_desc = $this->esc_html_inline( 'We feel your frustration and sincerely apologize for the inconvenience. Hope to see you again in the future.', 'deactivate-plugin-desc' );
2457
+ $install_previous_title = $this->esc_html_inline( 'Let\'s try your previous version', 'install-previous-title' );
2458
+ $install_previous_desc = $this->esc_html_inline( 'Uninstall this version and install the previous one.', 'install-previous-desc' );
2459
+ $fix_issue_title = $this->esc_html_inline( 'Yes - I\'m giving you a chance to fix it', 'fix-issue-title' );
2460
+ $fix_issue_desc = $this->esc_html_inline( 'We will do our best to whitelist your server and resolve this issue ASAP. You will get a follow-up email to %s once we have an update.', 'fix-issue-desc' );
2461
+ /* translators: %s: product title (e.g. "Awesome Plugin" requires an access to...) */
2462
+ $x_requires_access_to_api = $this->esc_html_inline( '%s requires an access to our API.', 'x-requires-access-to-api' );
2463
+ $sysadmin_title = $this->esc_html_inline( 'I\'m a system administrator', 'sysadmin-title' );
2464
+ $happy_to_resolve_issue_asap = $this->esc_html_inline( 'We are sure it\'s an issue on our side and more than happy to resolve it for you ASAP if you give us a chance.', 'happy-to-resolve-issue-asap' );
2465
+
2466
+ $message = false;
2467
+ if ( is_object( $api_result ) &&
2468
+ isset( $api_result->error ) &&
2469
+ isset( $api_result->error->code )
2470
+ ) {
2471
+ switch ( $api_result->error->code ) {
2472
+ case 'curl_missing':
2473
+ $missing_methods = '';
2474
+ if ( is_array( $api_result->missing_methods ) &&
2475
+ ! empty( $api_result->missing_methods )
2476
+ ) {
2477
+ foreach ( $api_result->missing_methods as $m ) {
2478
+ if ( 'curl_version' === $m ) {
2479
+ continue;
2480
+ }
2481
+
2482
+ if ( ! empty( $missing_methods ) ) {
2483
+ $missing_methods .= ', ';
2484
+ }
2485
+
2486
+ $missing_methods .= sprintf( '<code>%s</code>', $m );
2487
+ }
2488
+
2489
+ if ( ! empty( $missing_methods ) ) {
2490
+ $missing_methods = sprintf(
2491
+ '<br><br><b>%s</b> %s',
2492
+ $this->esc_html_inline( 'Disabled method(s):', 'curl-disabled-methods' ),
2493
+ $missing_methods
2494
+ );
2495
+ }
2496
+ }
2497
+
2498
+ $message = sprintf(
2499
+ $x_requires_access_to_api . ' ' .
2500
+ $this->esc_html_inline( 'We use PHP cURL library for the API calls, which is a very common library and usually installed and activated out of the box. Unfortunately, cURL is not activated (or disabled) on your server.', 'curl-missing-message' ) . ' ' .
2501
+ $missing_methods .
2502
+ ' %s',
2503
+ '<b>' . $this->get_plugin_name() . '</b>',
2504
+ sprintf(
2505
+ '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
2506
+ sprintf(
2507
+ '<a class="fs-resolve" data-type="curl" href="#"><b>%s</b></a>%s',
2508
+ $this->get_text_inline( 'I don\'t know what is cURL or how to install it, help me!', 'curl-missing-no-clue-title' ),
2509
+ ' - ' . sprintf(
2510
+ $this->get_text_inline( 'We\'ll make sure to contact your hosting company and resolve the issue. You will get a follow-up email to %s once we have an update.', 'curl-missing-no-clue-desc' ),
2511
+ '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
2512
+ )
2513
+ ),
2514
+ sprintf(
2515
+ '<b>%s</b> - %s',
2516
+ $sysadmin_title,
2517
+ esc_html( sprintf( $this->get_text_inline( 'Great, please install cURL and enable it in your php.ini file. In addition, search for the \'disable_functions\' directive in your php.ini file and remove any disabled methods starting with \'curl_\'. To make sure it was successfully activated, use \'phpinfo()\'. Once activated, deactivate the %s and reactivate it back again.', 'curl-missing-sysadmin-desc' ), $this->get_module_label( true ) ) )
2518
+ ),
2519
+ sprintf(
2520
+ '<a href="%s"><b>%s</b></a> - %s',
2521
+ 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 ),
2522
+ $deactivate_plugin_title,
2523
+ $deactivate_plugin_desc
2524
+ )
2525
+ )
2526
+ );
2527
+ break;
2528
+ case 'cloudflare_ddos_protection':
2529
+ $message = sprintf(
2530
+ $x_requires_access_to_api . ' ' .
2531
+ $this->esc_html_inline( 'From unknown reason, CloudFlare, the firewall we use, blocks the connection.', 'cloudflare-blocks-connection-message' ) . ' ' .
2532
+ $happy_to_resolve_issue_asap .
2533
+ ' %s',
2534
+ '<b>' . $this->get_plugin_name() . '</b>',
2535
+ sprintf(
2536
+ '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
2537
+ sprintf(
2538
+ '<a class="fs-resolve" data-type="cloudflare" href="#"><b>%s</b></a>%s',
2539
+ $fix_issue_title,
2540
+ ' - ' . sprintf(
2541
+ $fix_issue_desc,
2542
+ '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
2543
+ )
2544
+ ),
2545
+ sprintf(
2546
+ '<a href="%s" target="_blank"><b>%s</b></a> - %s',
2547
+ sprintf( 'https://wordpress.org/plugins/%s/download/', $this->_slug ),
2548
+ $install_previous_title,
2549
+ $install_previous_desc
2550
+ ),
2551
+ sprintf(
2552
+ '<a href="%s"><b>%s</b></a> - %s',
2553
+ 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 ),
2554
+ $deactivate_plugin_title,
2555
+ $deactivate_plugin_desc
2556
+ )
2557
+ )
2558
+ );
2559
+ break;
2560
+ case 'squid_cache_block':
2561
+ $message = sprintf(
2562
+ $x_requires_access_to_api . ' ' .
2563
+ $this->esc_html_inline( 'It looks like your server is using Squid ACL (access control lists), which blocks the connection.', 'squid-blocks-connection-message' ) .
2564
+ ' %s',
2565
+ '<b>' . $this->get_plugin_name() . '</b>',
2566
+ sprintf(
2567
+ '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
2568
+ sprintf(
2569
+ '<a class="fs-resolve" data-type="squid" href="#"><b>%s</b></a> - %s',
2570
+ $this->esc_html_inline( 'I don\'t know what is Squid or ACL, help me!', 'squid-no-clue-title' ),
2571
+ sprintf(
2572
+ $this->esc_html_inline( 'We\'ll make sure to contact your hosting company and resolve the issue. You will get a follow-up email to %s once we have an update.', 'squid-no-clue-desc' ),
2573
+ '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
2574
+ )
2575
+ ),
2576
+ sprintf(
2577
+ '<b>%s</b> - %s',
2578
+ $sysadmin_title,
2579
+ sprintf(
2580
+ $this->esc_html_inline( 'Great, please whitelist the following domains: %s. Once you are done, deactivate the %s and activate it again.', 'squid-sysadmin-desc' ),
2581
+ // We use a filter since the plugin might require additional API connectivity.
2582
+ '<b>' . implode( ', ', $this->apply_filters( 'api_domains', array( 'api.freemius.com', 'wp.freemius.com' ) ) ) . '</b>',
2583
+ $this->_module_type
2584
+ )
2585
+ ),
2586
+ sprintf(
2587
+ '<a href="%s"><b>%s</b></a> - %s',
2588
+ 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 ),
2589
+ $deactivate_plugin_title,
2590
+ $deactivate_plugin_desc
2591
+ )
2592
+ )
2593
+ );
2594
+ break;
2595
+ // default:
2596
+ // $message = $this->get_text_inline( 'connectivity-test-fails-message' );
2597
+ // break;
2598
+ }
2599
+ }
2600
+
2601
+ $message_id = 'failed_connect_api';
2602
+ $type = 'error';
2603
+
2604
+ $connectivity_test_fails_message = $this->esc_html_inline( 'From unknown reason, the API connectivity test failed.', 'connectivity-test-fails-message' );
2605
+
2606
+ if ( false === $message ) {
2607
+ if ( $is_first_failure ) {
2608
+ // First attempt failed.
2609
+ $message = sprintf(
2610
+ $x_requires_access_to_api . ' ' .
2611
+ $connectivity_test_fails_message . ' ' .
2612
+ $this->esc_html_inline( 'It\'s probably a temporary issue on our end. Just to be sure, with your permission, would it be o.k to run another connectivity test?', 'connectivity-test-maybe-temporary' ) . '<br><br>' .
2613
+ '%s',
2614
+ '<b>' . $this->get_plugin_name() . '</b>',
2615
+ sprintf(
2616
+ '<div id="fs_firewall_issue_options">%s %s</div>',
2617
+ sprintf(
2618
+ '<a class="button button-primary fs-resolve" data-type="retry_ping" href="#">%s</a>',
2619
+ $this->get_text_inline( 'Yes - do your thing', 'yes-do-your-thing' )
2620
+ ),
2621
+ sprintf(
2622
+ '<a href="%s" class="button">%s</a>',
2623
+ 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 ),
2624
+ $this->get_text_inline( 'No - just deactivate', 'no-deactivate' )
2625
+ )
2626
+ )
2627
+ );
2628
+
2629
+ $message_id = 'failed_connect_api_first';
2630
+ $type = 'promotion';
2631
+ } else {
2632
+ // Second connectivity attempt failed.
2633
+ $message = sprintf(
2634
+ $x_requires_access_to_api . ' ' .
2635
+ $connectivity_test_fails_message . ' ' .
2636
+ $happy_to_resolve_issue_asap .
2637
+ ' %s',
2638
+ '<b>' . $this->get_plugin_name() . '</b>',
2639
+ sprintf(
2640
+ '<ol id="fs_firewall_issue_options"><li>%s</li><li>%s</li><li>%s</li></ol>',
2641
+ sprintf(
2642
+ '<a class="fs-resolve" data-type="general" href="#"><b>%s</b></a>%s',
2643
+ $fix_issue_title,
2644
+ ' - ' . sprintf(
2645
+ $fix_issue_desc,
2646
+ '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
2647
+ )
2648
+ ),
2649
+ sprintf(
2650
+ '<a href="%s" target="_blank"><b>%s</b></a> - %s',
2651
+ sprintf( 'https://wordpress.org/plugins/%s/download/', $this->_slug ),
2652
+ $install_previous_title,
2653
+ $install_previous_desc
2654
+ ),
2655
+ sprintf(
2656
+ '<a href="%s"><b>%s</b></a> - %s',
2657
+ 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 ),
2658
+ $deactivate_plugin_title,
2659
+ $deactivate_plugin_desc
2660
+ )
2661
+ )
2662
+ );
2663
+ }
2664
+ }
2665
+
2666
+ $this->_admin_notices->add_sticky(
2667
+ $message,
2668
+ $message_id,
2669
+ $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
2670
+ $type
2671
+ );
2672
+ }
2673
+
2674
+ /**
2675
+ * Handle user request to resolve connectivity issue.
2676
+ * This method will send an email to Freemius API technical staff for resolution.
2677
+ * The email will contain server's info and installed plugins (might be caching issue).
2678
+ *
2679
+ * @author Vova Feldman (@svovaf)
2680
+ * @since 1.0.9
2681
+ */
2682
+ function _email_about_firewall_issue() {
2683
+ $this->_admin_notices->remove_sticky( 'failed_connect_api' );
2684
+
2685
+ $pong = $this->ping();
2686
+
2687
+ $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong );
2688
+
2689
+ if ( $is_connected ) {
2690
+ $this->store_connectivity_info( $pong, $is_connected );
2691
+
2692
+ echo $this->get_after_plugin_activation_redirect_url();
2693
+ exit;
2694
+ }
2695
+
2696
+ $current_user = self::_get_current_wp_user();
2697
+ $admin_email = $current_user->user_email;
2698
+
2699
+ $error_type = fs_request_get( 'error_type', 'general' );
2700
+
2701
+ switch ( $error_type ) {
2702
+ case 'squid':
2703
+ $title = 'Squid ACL Blocking Issue';
2704
+ break;
2705
+ case 'cloudflare':
2706
+ $title = 'CloudFlare Blocking Issue';
2707
+ break;
2708
+ default:
2709
+ $title = 'API Connectivity Issue';
2710
+ break;
2711
+ }
2712
+
2713
+ $custom_email_sections = array();
2714
+
2715
+ // Add 'API Error' custom email section.
2716
+ $custom_email_sections['api_error'] = array(
2717
+ 'title' => 'API Error',
2718
+ 'rows' => array(
2719
+ 'ping' => array(
2720
+ 'API Error',
2721
+ is_string( $pong ) ? htmlentities( $pong ) : json_encode( $pong )
2722
+ ),
2723
+ )
2724
+ );
2725
+
2726
+ // Send email with technical details to resolve API connectivity issues.
2727
+ $this->send_email(
2728
+ 'api@freemius.com', // recipient
2729
+ $title . ' [' . $this->get_plugin_name() . ']', // subject
2730
+ $custom_email_sections,
2731
+ array( "Reply-To: $admin_email <$admin_email>" ) // headers
2732
+ );
2733
+
2734
+ $this->_admin_notices->add_sticky(
2735
+ sprintf(
2736
+ $this->get_text_inline( 'Thank for giving us the chance to fix it! A message was just sent to our technical staff. We will get back to you as soon as we have an update to %s. Appreciate your patience.', 'fix-request-sent-message' ),
2737
+ '<a href="mailto:' . $admin_email . '">' . $admin_email . '</a>'
2738
+ ),
2739
+ 'server_details_sent'
2740
+ );
2741
+
2742
+ // Action was taken, tell that API connectivity troubleshooting should be off now.
2743
+
2744
+ echo "1";
2745
+ exit;
2746
+ }
2747
+
2748
+ /**
2749
+ * Handle connectivity test retry approved by the user.
2750
+ *
2751
+ * @author Vova Feldman (@svovaf)
2752
+ * @since 1.1.7.4
2753
+ */
2754
+ function _retry_connectivity_test() {
2755
+ $this->_admin_notices->remove_sticky( 'failed_connect_api_first' );
2756
+
2757
+ $pong = $this->ping();
2758
+
2759
+ $is_connected = $this->get_api_plugin_scope()->is_valid_ping( $pong );
2760
+
2761
+ if ( $is_connected ) {
2762
+ $this->store_connectivity_info( $pong, $is_connected );
2763
+
2764
+ echo $this->get_after_plugin_activation_redirect_url();
2765
+ } else {
2766
+ // Add connectivity issue message after 2nd failed attempt.
2767
+ $this->_add_connectivity_issue_message( $pong, false );
2768
+
2769
+ echo "1";
2770
+ }
2771
+
2772
+ exit;
2773
+ }
2774
+
2775
+ static function _add_firewall_issues_javascript() {
2776
+ $params = array();
2777
+ fs_require_once_template( 'firewall-issues-js.php', $params );
2778
+ }
2779
+
2780
+ #endregion
2781
+
2782
+ #----------------------------------------------------------------------------------
2783
+ #region Email
2784
+ #----------------------------------------------------------------------------------
2785
+
2786
+ /**
2787
+ * Generates and sends an HTML email with customizable sections.
2788
+ *
2789
+ * @author Leo Fajardo (@leorw)
2790
+ * @since 1.1.2
2791
+ *
2792
+ * @param string $to_address
2793
+ * @param string $subject
2794
+ * @param array $sections
2795
+ * @param array $headers
2796
+ *
2797
+ * @return bool Whether the email contents were sent successfully.
2798
+ */
2799
+ private function send_email(
2800
+ $to_address,
2801
+ $subject,
2802
+ $sections = array(),
2803
+ $headers = array()
2804
+ ) {
2805
+ $default_sections = $this->get_email_sections();
2806
+
2807
+ // Insert new sections or replace the default email sections.
2808
+ if ( is_array( $sections ) && ! empty( $sections ) ) {
2809
+ foreach ( $sections as $section_id => $custom_section ) {
2810
+ if ( ! isset( $default_sections[ $section_id ] ) ) {
2811
+ // If the section does not exist, add it.
2812
+ $default_sections[ $section_id ] = $custom_section;
2813
+ } else {
2814
+ // If the section already exists, override it.
2815
+ $current_section = $default_sections[ $section_id ];
2816
+
2817
+ // Replace the current section's title if a custom section title exists.
2818
+ if ( isset( $custom_section['title'] ) ) {
2819
+ $current_section['title'] = $custom_section['title'];
2820
+ }
2821
+
2822
+ // Insert new rows under the current section or replace the default rows.
2823
+ if ( isset( $custom_section['rows'] ) && is_array( $custom_section['rows'] ) && ! empty( $custom_section['rows'] ) ) {
2824
+ foreach ( $custom_section['rows'] as $row_id => $row ) {
2825
+ $current_section['rows'][ $row_id ] = $row;
2826
+ }
2827
+ }
2828
+
2829
+ $default_sections[ $section_id ] = $current_section;
2830
+ }
2831
+ }
2832
+ }
2833
+
2834
+ $vars = array( 'sections' => $default_sections );
2835
+ $message = fs_get_template( 'email.php', $vars );
2836
+
2837
+ // Set the type of email to HTML.
2838
+ $headers[] = 'Content-type: text/html; charset=UTF-8';
2839
+
2840
+ $header_string = implode( "\r\n", $headers );
2841
+
2842
+ return wp_mail(
2843
+ $to_address,
2844
+ $subject,
2845
+ $message,
2846
+ $header_string
2847
+ );
2848
+ }
2849
+
2850
+ /**
2851
+ * Generates the data for the sections of the email content.
2852
+ *
2853
+ * @author Leo Fajardo (@leorw)
2854
+ * @since 1.1.2
2855
+ *
2856
+ * @return array
2857
+ */
2858
+ private function get_email_sections() {
2859
+ // Retrieve the current user's information so that we can get the user's email, first name, and last name below.
2860
+ $current_user = self::_get_current_wp_user();
2861
+
2862
+ // Retrieve the cURL version information so that we can get the version number below.
2863
+ $curl_version_information = curl_version();
2864
+
2865
+ $active_plugin = self::get_active_plugins();
2866
+
2867
+ // Generate the list of active plugins separated by new line.
2868
+ $active_plugin_string = '';
2869
+ foreach ( $active_plugin as $plugin ) {
2870
+ $active_plugin_string .= sprintf(
2871
+ '<a href="%s">%s</a> [v%s]<br>',
2872
+ $plugin['PluginURI'],
2873
+ $plugin['Name'],
2874
+ $plugin['Version']
2875
+ );
2876
+ }
2877
+
2878
+ $server_ip = WP_FS__REMOTE_ADDR;
2879
+
2880
+ // Add PHP info for deeper investigation.
2881
+ ob_start();
2882
+ phpinfo();
2883
+ $php_info = ob_get_clean();
2884
+
2885
+ $api_domain = substr( FS_API__ADDRESS, strpos( FS_API__ADDRESS, ':' ) + 3 );
2886
+
2887
+ // Generate the default email sections.
2888
+ $sections = array(
2889
+ 'sdk' => array(
2890
+ 'title' => 'SDK',
2891
+ 'rows' => array(
2892
+ 'fs_version' => array( 'FS Version', $this->version ),
2893
+ 'curl_version' => array( 'cURL Version', $curl_version_information['version'] )
2894
+ )
2895
+ ),
2896
+ 'plugin' => array(
2897
+ 'title' => ucfirst( $this->get_module_type() ),
2898
+ 'rows' => array(
2899
+ 'name' => array( 'Name', $this->get_plugin_name() ),
2900
+ 'version' => array( 'Version', $this->get_plugin_version() )
2901
+ )
2902
+ ),
2903
+ 'api' => array(
2904
+ 'title' => 'API Subdomain',
2905
+ 'rows' => array(
2906
+ 'dns' => array(
2907
+ 'DNS_CNAME',
2908
+ function_exists( 'dns_get_record' ) ?
2909
+ var_export( dns_get_record( $api_domain, DNS_CNAME ), true ) :
2910
+ 'dns_get_record() disabled/blocked'
2911
+ ),
2912
+ 'ip' => array(
2913
+ 'IP',
2914
+ function_exists( 'gethostbyname' ) ?
2915
+ gethostbyname( $api_domain ) :
2916
+ 'gethostbyname() disabled/blocked'
2917
+ ),
2918
+ ),
2919
+ ),
2920
+ 'site' => array(
2921
+ 'title' => 'Site',
2922
+ 'rows' => array(
2923
+ 'unique_id' => array( 'Unique ID', $this->get_anonymous_id() ),
2924
+ 'address' => array( 'Address', site_url() ),
2925
+ 'host' => array(
2926
+ 'HTTP_HOST',
2927
+ ( ! empty( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : '' )
2928
+ ),
2929
+ 'hosting' => array(
2930
+ 'Hosting Company' => fs_request_has( 'hosting_company' ) ?
2931
+ fs_request_get( 'hosting_company' ) :
2932
+ 'Unknown',
2933
+ ),
2934
+ 'server_addr' => array(
2935
+ 'SERVER_ADDR',
2936
+ '<a href="http://www.projecthoneypot.org/ip_' . $server_ip . '">' . $server_ip . '</a>'
2937
+ )
2938
+ )
2939
+ ),
2940
+ 'user' => array(
2941
+ 'title' => 'User',
2942
+ 'rows' => array(
2943
+ 'email' => array( 'Email', $current_user->user_email ),
2944
+ 'first' => array( 'First', $current_user->user_firstname ),
2945
+ 'last' => array( 'Last', $current_user->user_lastname )
2946
+ )
2947
+ ),
2948
+ 'plugins' => array(
2949
+ 'title' => 'Plugins',
2950
+ 'rows' => array(
2951
+ 'active_plugins' => array( 'Active Plugins', $active_plugin_string )
2952
+ )
2953
+ ),
2954
+ 'php_info' => array(
2955
+ 'title' => 'PHP Info',
2956
+ 'rows' => array(
2957
+ 'info' => array( $php_info )
2958
+ ),
2959
+ )
2960
+ );
2961
+
2962
+ // Allow the sections to be modified by other code.
2963
+ $sections = $this->apply_filters( 'email_template_sections', $sections );
2964
+
2965
+ return $sections;
2966
+ }
2967
+
2968
+ #endregion
2969
+
2970
+ #----------------------------------------------------------------------------------
2971
+ #region Initialization
2972
+ #----------------------------------------------------------------------------------
2973
+
2974
+ /**
2975
+ * Init plugin's Freemius instance.
2976
+ *
2977
+ * @author Vova Feldman (@svovaf)
2978
+ * @since 1.0.1
2979
+ *
2980
+ * @param number $id
2981
+ * @param string $public_key
2982
+ * @param bool $is_live
2983
+ * @param bool $is_premium
2984
+ */
2985
+ function init( $id, $public_key, $is_live = true, $is_premium = true ) {
2986
+ $this->_logger->entrance();
2987
+
2988
+ $this->dynamic_init( array(
2989
+ 'id' => $id,
2990
+ 'public_key' => $public_key,
2991
+ 'is_live' => $is_live,
2992
+ 'is_premium' => $is_premium,
2993
+ ) );
2994
+ }
2995
+
2996
+ /**
2997
+ * Dynamic initiator, originally created to support initiation
2998
+ * with parent_id for add-ons.
2999
+ *
3000
+ * @author Vova Feldman (@svovaf)
3001
+ * @since 1.0.6
3002
+ *
3003
+ * @param array $plugin_info
3004
+ *
3005
+ * @throws Freemius_Exception
3006
+ */
3007
+ function dynamic_init( array $plugin_info ) {
3008
+ $this->_logger->entrance();
3009
+
3010
+ $this->parse_settings( $plugin_info );
3011
+
3012
+ if ( ! self::is_ajax() ) {
3013
+ if ( ! $this->is_addon() || $this->is_only_premium() ) {
3014
+ add_action( 'admin_menu', array( &$this, '_prepare_admin_menu' ), WP_FS__LOWEST_PRIORITY );
3015
+ }
3016
+ }
3017
+
3018
+ if ( $this->should_stop_execution() ) {
3019
+ return;
3020
+ }
3021
+
3022
+ if ( ! $this->is_registered() ) {
3023
+ if ( $this->is_anonymous() ) {
3024
+ // If user skipped, no need to test connectivity.
3025
+ $this->_has_api_connection = true;
3026
+ $this->_is_on = true;
3027
+ } else {
3028
+ if ( ! $this->has_api_connectivity() ) {
3029
+ if ( $this->_admin_notices->has_sticky( 'failed_connect_api_first' ) ||
3030
+ $this->_admin_notices->has_sticky( 'failed_connect_api' )
3031
+ ) {
3032
+ if ( ! $this->_enable_anonymous || $this->is_premium() ) {
3033
+ // If anonymous mode is disabled, add firewall admin-notice message.
3034
+ add_action( 'admin_footer', array( 'Freemius', '_add_firewall_issues_javascript' ) );
3035
+
3036
+ $this->add_ajax_action( 'resolve_firewall_issues', array(
3037
+ &$this,
3038
+ '_email_about_firewall_issue'
3039
+ ) );
3040
+
3041
+ $this->add_ajax_action( 'retry_connectivity_test', array(
3042
+ &$this,
3043
+ '_retry_connectivity_test'
3044
+ ) );
3045
+ }
3046
+ }
3047
+
3048
+ return;
3049
+ } else {
3050
+ $this->_admin_notices->remove_sticky( array(
3051
+ 'failed_connect_api_first',
3052
+ 'failed_connect_api',
3053
+ ) );
3054
+
3055
+ if ( $this->_anonymous_mode ) {
3056
+ // Simulate anonymous mode.
3057
+ $this->_is_anonymous = true;
3058
+ }
3059
+ }
3060
+ }
3061
+
3062
+ // Check if Freemius is on for the current plugin.
3063
+ // This MUST be executed after all the plugin variables has been loaded.
3064
+ if ( ! $this->is_on() ) {
3065
+ return;
3066
+ }
3067
+ }
3068
+
3069
+ if ( $this->has_api_connectivity() ) {
3070
+ if ( self::is_cron() ) {
3071
+ $this->hook_callback_to_sync_cron();
3072
+ } else if ( $this->is_user_in_admin() ) {
3073
+ /**
3074
+ * Schedule daily data sync cron if:
3075
+ *
3076
+ * 1. User opted-in (for tracking).
3077
+ * 2. If skipped, but later upgraded (opted-in via upgrade).
3078
+ *
3079
+ * @author Vova Feldman (@svovaf)
3080
+ * @since 1.1.7.3
3081
+ *
3082
+ */
3083
+ if ( $this->is_registered() ) {
3084
+ if ( ! $this->is_sync_cron_on() && $this->is_tracking_allowed() ) {
3085
+ $this->schedule_sync_cron();
3086
+ }
3087
+ }
3088
+
3089
+ /**
3090
+ * Check if requested for manual blocking background sync.
3091
+ */
3092
+ if ( fs_request_has( 'background_sync' ) ) {
3093
+ $this->run_manual_sync();
3094
+ }
3095
+ }
3096
+ }
3097
+
3098
+ if ( $this->is_registered() ) {
3099
+ $this->hook_callback_to_install_sync();
3100
+ }
3101
+
3102
+ if ( $this->is_addon() ) {
3103
+ if ( $this->is_parent_plugin_installed() ) {
3104
+ // Link to parent FS.
3105
+ $this->_parent = self::get_instance_by_id( $this->_plugin->parent_plugin_id );
3106
+
3107
+ // Get parent plugin reference.
3108
+ $this->_parent_plugin = $this->_parent->get_plugin();
3109
+ }
3110
+ }
3111
+
3112
+ if ( $this->is_user_in_admin() ) {
3113
+ if ( self::is_plugins_page() && $this->is_plugin() ) {
3114
+ $this->hook_plugin_action_links();
3115
+ }
3116
+
3117
+ if ( $this->is_addon() ) {
3118
+ if ( ! $this->is_parent_plugin_installed() ) {
3119
+ $parent_name = $this->get_option( $plugin_info, 'parent_name', null );
3120
+
3121
+ if ( isset( $plugin_info['parent'] ) ) {
3122
+ $parent_name = $this->get_option( $plugin_info['parent'], 'name', null );
3123
+ }
3124
+
3125
+ $this->_admin_notices->add(
3126
+ ( ! empty( $parent_name ) ?
3127
+ sprintf( $this->get_text_x_inline( '%s cannot run without %s.', 'addonX cannot run without pluginY', 'addon-x-cannot-run-without-y' ), $this->get_plugin_name(), $parent_name ) :
3128
+ sprintf( $this->get_text_x_inline( '%s cannot run without the plugin.', 'addonX cannot run...', 'addon-x-cannot-run-without-parent' ), $this->get_plugin_name() )
3129
+ ),
3130
+ $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
3131
+ 'error'
3132
+ );
3133
+
3134
+ return;
3135
+ } else {
3136
+ if ( $this->_parent->is_registered() && ! $this->is_registered() ) {
3137
+ // If parent plugin activated, automatically install add-on for the user.
3138
+ $this->_activate_addon_account( $this->_parent );
3139
+ } else if ( ! $this->_parent->is_registered() && $this->is_registered() ) {
3140
+ // If add-on activated and parent not, automatically install parent for the user.
3141
+ $this->activate_parent_account( $this->_parent );
3142
+ }
3143
+
3144
+ // @todo This should be only executed on activation. It should be migrated to register_activation_hook() together with other activation related logic.
3145
+ if ( $this->is_premium() ) {
3146
+ // Remove add-on download admin-notice.
3147
+ $this->_parent->_admin_notices->remove_sticky( array(
3148
+ 'addon_plan_upgraded_' . $this->_slug,
3149
+ 'no_addon_license_' . $this->_slug,
3150
+ ) );
3151
+ }
3152
+
3153
+ // $this->deactivate_premium_only_addon_without_license();
3154
+ }
3155
+ } else {
3156
+ if ( $this->has_addons() &&
3157
+ 'plugin-information' === fs_request_get( 'tab', false ) &&
3158
+ $this->get_id() == fs_request_get( 'parent_plugin_id', false )
3159
+ ) {
3160
+ require_once WP_FS__DIR_INCLUDES . '/fs-plugin-info-dialog.php';
3161
+
3162
+ new FS_Plugin_Info_Dialog( $this );
3163
+ }
3164
+ }
3165
+
3166
+ add_action( 'admin_init', array( &$this, '_admin_init_action' ) );
3167
+
3168
+ // if ( $this->is_registered() ||
3169
+ // $this->is_anonymous() ||
3170
+ // $this->is_pending_activation()
3171
+ // ) {
3172
+ // $this->_init_admin();
3173
+ // }
3174
+ }
3175
+
3176
+ /**
3177
+ * Should be called outside `$this->is_user_in_admin()` scope
3178
+ * because the updater has some logic that needs to be executed
3179
+ * during AJAX calls.
3180
+ *
3181
+ * Currently we need to hook to the `http_request_host_is_external` filter.
3182
+ * In the future, there might be additional logic added.
3183
+ *
3184
+ * @author Vova Feldman
3185
+ * @since 1.2.1.6
3186
+ */
3187
+ if ( $this->is_premium() && $this->has_release_on_freemius() ) {
3188
+ new FS_Plugin_Updater( $this );
3189
+ }
3190
+
3191
+ $this->do_action( 'initiated' );
3192
+
3193
+ if ( $this->_storage->prev_is_premium !== $this->_plugin->is_premium ) {
3194
+ if ( isset( $this->_storage->prev_is_premium ) ) {
3195
+ $this->apply_filters(
3196
+ 'after_code_type_change',
3197
+ // New code type.
3198
+ $this->_plugin->is_premium
3199
+ );
3200
+ } else {
3201
+ // Set for code type for the first time.
3202
+ $this->_storage->prev_is_premium = $this->_plugin->is_premium;
3203
+ }
3204
+ }
3205
+
3206
+ if ( ! $this->is_addon() ) {
3207
+ if ( $this->is_registered() ) {
3208
+ // Fix for upgrade from versions < 1.0.9.
3209
+ if ( ! isset( $this->_storage->activation_timestamp ) ) {
3210
+ $this->_storage->activation_timestamp = WP_FS__SCRIPT_START_TIME;
3211
+ }
3212
+
3213
+ $this->do_action( 'after_init_plugin_registered' );
3214
+ } else if ( $this->is_anonymous() ) {
3215
+ $this->do_action( 'after_init_plugin_anonymous' );
3216
+ } else if ( $this->is_pending_activation() ) {
3217
+ $this->do_action( 'after_init_plugin_pending_activations' );
3218
+ }
3219
+ } else {
3220
+ if ( $this->is_registered() ) {
3221
+ $this->do_action( 'after_init_addon_registered' );
3222
+ } else if ( $this->is_anonymous() ) {
3223
+ $this->do_action( 'after_init_addon_anonymous' );
3224
+ } else if ( $this->is_pending_activation() ) {
3225
+ $this->do_action( 'after_init_addon_pending_activations' );
3226
+ }
3227
+ }
3228
+ }
3229
+
3230
+ /**
3231
+ * @author Leo Fajardo (@leorw)
3232
+ *
3233
+ * @since 1.2.1.5
3234
+ */
3235
+ function _stop_tracking_callback() {
3236
+ $this->_logger->entrance();
3237
+
3238
+ $this->check_ajax_referer( 'stop_tracking' );
3239
+
3240
+ $result = $this->stop_tracking();
3241
+
3242
+ if ( true === $result ) {
3243
+ self::shoot_ajax_success();
3244
+ }
3245
+
3246
+ $this->_logger->api_error( $result );
3247
+
3248
+ self::shoot_ajax_failure(
3249
+ sprintf( $this->get_text_inline( 'Unexpected API error. Please contact the %s\'s author with the following error.', 'unexpected-api-error' ), $this->_module_type ) .
3250
+ ( $this->is_api_error( $result ) && isset( $result->error ) ?
3251
+ $result->error->message :
3252
+ var_export( $result, true ) )
3253
+ );
3254
+ }
3255
+
3256
+ /**
3257
+ * @author Leo Fajardo (@leorw)
3258
+ * @since 1.2.1.5
3259
+ */
3260
+ function _allow_tracking_callback() {
3261
+ $this->_logger->entrance();
3262
+
3263
+ $this->check_ajax_referer( 'allow_tracking' );
3264
+
3265
+ $result = $this->allow_tracking();
3266
+
3267
+ if ( true === $result ) {
3268
+ self::shoot_ajax_success();
3269
+ }
3270
+
3271
+ $this->_logger->api_error( $result );
3272
+
3273
+ self::shoot_ajax_failure(
3274
+ sprintf( $this->get_text_inline( 'Unexpected API error. Please contact the %s\'s author with the following error.', 'unexpected-api-error' ), $this->_module_type ) .
3275
+ ( $this->is_api_error( $result ) && isset( $result->error ) ?
3276
+ $result->error->message :
3277
+ var_export( $result, true ) )
3278
+ );
3279
+ }
3280
+
3281
+ /**
3282
+ * Opt-out from usage tracking.
3283
+ *
3284
+ * Note: This will not delete the account information but will stop all tracking.
3285
+ *
3286
+ * Returns:
3287
+ * 1. FALSE - If the user never opted-in.
3288
+ * 2. TRUE - If successfully opted-out.
3289
+ * 3. object - API result on failure.
3290
+ *
3291
+ * @author Leo Fajardo (@leorw)
3292
+ * @since 1.2.1.5
3293
+ *
3294
+ * @return bool|object
3295
+ */
3296
+ function stop_tracking() {
3297
+ $this->_logger->entrance();
3298
+
3299
+ if ( ! $this->is_registered() ) {
3300
+ // User never opted-in.
3301
+ return false;
3302
+ }
3303
+
3304
+ if ( $this->is_tracking_prohibited() ) {
3305
+ // Already disconnected.
3306
+ return true;
3307
+ }
3308
+
3309
+ // Send update to FS.
3310
+ $result = $this->get_api_site_scope()->call( '/?fields=is_disconnected', 'put', array(
3311
+ 'is_disconnected' => true
3312
+ ) );
3313
+
3314
+ if ( ! $this->is_api_result_entity( $result ) ||
3315
+ ! isset( $result->is_disconnected ) ||
3316
+ ! $result->is_disconnected
3317
+ ) {
3318
+ $this->_logger->api_error( $result );
3319
+
3320
+ return $result;
3321
+ }
3322
+
3323
+ $this->_site->is_disconnected = $result->is_disconnected;
3324
+ $this->_store_site();
3325
+
3326
+ $this->clear_sync_cron();
3327
+
3328
+ // Successfully disconnected.
3329
+ return true;
3330
+ }
3331
+
3332
+ /**
3333
+ * Opt-in back into usage tracking.
3334
+ *
3335
+ * Note: This will only work if the user opted-in previously.
3336
+ *
3337
+ * Returns:
3338
+ * 1. FALSE - If the user never opted-in.
3339
+ * 2. TRUE - If successfully opted-in back to usage tracking.
3340
+ * 3. object - API result on failure.
3341
+ *
3342
+ * @author Leo Fajardo (@leorw)
3343
+ * @since 1.2.1.5
3344
+ *
3345
+ * @return bool|object
3346
+ */
3347
+ function allow_tracking() {
3348
+ $this->_logger->entrance();
3349
+
3350
+ if ( ! $this->is_registered() ) {
3351
+ // User never opted-in.
3352
+ return false;
3353
+ }
3354
+
3355
+ if ( $this->is_tracking_allowed() ) {
3356
+ // Tracking already allowed.
3357
+ return true;
3358
+ }
3359
+
3360
+ $result = $this->get_api_site_scope()->call( '/?is_disconnected', 'put', array(
3361
+ 'is_disconnected' => false
3362
+ ) );
3363
+
3364
+ if ( ! $this->is_api_result_entity( $result ) ||
3365
+ ! isset( $result->is_disconnected ) ||
3366
+ $result->is_disconnected
3367
+ ) {
3368
+ $this->_logger->api_error( $result );
3369
+
3370
+ return $result;
3371
+ }
3372
+
3373
+ $this->_site->is_disconnected = $result->is_disconnected;
3374
+ $this->_store_site();
3375
+
3376
+ $this->schedule_sync_cron();
3377
+
3378
+ // Successfully reconnected.
3379
+ return true;
3380
+ }
3381
+
3382
+ /**
3383
+ * If user opted-in and later disabled usage-tracking,
3384
+ * re-allow tracking for licensing and updates.
3385
+ *
3386
+ * @author Leo Fajardo (@leorw)
3387
+ *
3388
+ * @since 1.2.1.5
3389
+ */
3390
+ private function reconnect_locally() {
3391
+ $this->_logger->entrance();
3392
+
3393
+ if ( $this->is_tracking_prohibited() &&
3394
+ $this->is_registered()
3395
+ ) {
3396
+ $this->_site->is_disconnected = false;
3397
+ $this->_store_site();
3398
+ }
3399
+ }
3400
+
3401
+ /**
3402
+ * Parse plugin's settings (as defined by the plugin dev).
3403
+ *
3404
+ * @author Vova Feldman (@svovaf)
3405
+ * @since 1.1.7.3
3406
+ *
3407
+ * @param array $plugin_info
3408
+ *
3409
+ * @throws \Freemius_Exception
3410
+ */
3411
+ private function parse_settings( &$plugin_info ) {
3412
+ $this->_logger->entrance();
3413
+
3414
+ $id = $this->get_numeric_option( $plugin_info, 'id', false );
3415
+ $public_key = $this->get_option( $plugin_info, 'public_key', false );
3416
+ $secret_key = $this->get_option( $plugin_info, 'secret_key', null );
3417
+ $parent_id = $this->get_numeric_option( $plugin_info, 'parent_id', null );
3418
+ $parent_name = $this->get_option( $plugin_info, 'parent_name', null );
3419
+
3420
+ /**
3421
+ * @author Vova Feldman (@svovaf)
3422
+ * @since 1.1.9 Try to pull secret key from external config.
3423
+ */
3424
+ if ( is_null( $secret_key ) && defined( "WP_FS__{$this->_slug}_SECRET_KEY" ) ) {
3425
+ $secret_key = constant( "WP_FS__{$this->_slug}_SECRET_KEY" );
3426
+ }
3427
+
3428
+ if ( isset( $plugin_info['parent'] ) ) {
3429
+ $parent_id = $this->get_numeric_option( $plugin_info['parent'], 'id', null );
3430
+ // $parent_slug = $this->get_option( $plugin_info['parent'], 'slug', null );
3431
+ // $parent_public_key = $this->get_option( $plugin_info['parent'], 'public_key', null );
3432
+ // $parent_name = $this->get_option( $plugin_info['parent'], 'name', null );
3433
+ }
3434
+
3435
+ if ( false === $id ) {
3436
+ throw new Freemius_Exception( array(
3437
+ 'error' => array(
3438
+ 'type' => 'ParameterNotSet',
3439
+ 'message' => 'Plugin id parameter is not set.',
3440
+ 'code' => 'plugin_id_not_set',
3441
+ 'http' => 500,
3442
+ )
3443
+ ) );
3444
+ }
3445
+ if ( false === $public_key ) {
3446
+ throw new Freemius_Exception( array(
3447
+ 'error' => array(
3448
+ 'type' => 'ParameterNotSet',
3449
+ 'message' => 'Plugin public_key parameter is not set.',
3450
+ 'code' => 'plugin_public_key_not_set',
3451
+ 'http' => 500,
3452
+ )
3453
+ ) );
3454
+ }
3455
+
3456
+ $plugin = ( $this->_plugin instanceof FS_Plugin ) ?
3457
+ $this->_plugin :
3458
+ new FS_Plugin();
3459
+
3460
+ $plugin->update( array(
3461
+ 'id' => $id,
3462
+ 'type' => $this->get_option( $plugin_info, 'type', $this->_module_type),
3463
+ 'public_key' => $public_key,
3464
+ 'slug' => $this->_slug,
3465
+ 'parent_plugin_id' => $parent_id,
3466
+ 'version' => $this->get_plugin_version(),
3467
+ 'title' => $this->get_plugin_name(),
3468
+ 'file' => $this->_plugin_basename,
3469
+ 'is_premium' => $this->get_bool_option( $plugin_info, 'is_premium', true ),
3470
+ 'is_live' => $this->get_bool_option( $plugin_info, 'is_live', true ),
3471
+ 'affiliate_moderation' => $this->get_option( $plugin_info, 'has_affiliation' ),
3472
+ ) );
3473
+
3474
+ if ( $plugin->is_updated() ) {
3475
+ // Update plugin details.
3476
+ $this->_plugin = FS_Plugin_Manager::instance( $this->_module_id )->store( $plugin );
3477
+ }
3478
+ // Set the secret key after storing the plugin, we don't want to store the key in the storage.
3479
+ $this->_plugin->secret_key = $secret_key;
3480
+
3481
+ if ( ! isset( $plugin_info['menu'] ) ) {
3482
+ $plugin_info['menu'] = array();
3483
+
3484
+ if ( ! empty( $this->_storage->sdk_last_version ) &&
3485
+ version_compare( $this->_storage->sdk_last_version, '1.1.2', '<=' )
3486
+ ) {
3487
+ // Backward compatibility to 1.1.2
3488
+ $plugin_info['menu']['slug'] = isset( $plugin_info['menu_slug'] ) ?
3489
+ $plugin_info['menu_slug'] :
3490
+ $this->_slug;
3491
+ }
3492
+ }
3493
+
3494
+ $this->_menu = FS_Admin_Menu_Manager::instance(
3495
+ $this->_module_id,
3496
+ $this->_module_type,
3497
+ $this->get_unique_affix()
3498
+ );
3499
+
3500
+ $this->_menu->init( $plugin_info['menu'], $this->is_addon() );
3501
+
3502
+ $this->_has_addons = $this->get_bool_option( $plugin_info, 'has_addons', false );
3503
+ $this->_has_paid_plans = $this->get_bool_option( $plugin_info, 'has_paid_plans', true );
3504
+ $this->_has_premium_version = $this->get_bool_option( $plugin_info, 'has_premium_version', $this->_has_paid_plans );
3505
+ $this->_ignore_pending_mode = $this->get_bool_option( $plugin_info, 'ignore_pending_mode', false );
3506
+ $this->_is_org_compliant = $this->get_bool_option( $plugin_info, 'is_org_compliant', true );
3507
+ $this->_is_premium_only = $this->get_bool_option( $plugin_info, 'is_premium_only', false );
3508
+ if ( $this->_is_premium_only ) {
3509
+ // If premium only plugin, disable anonymous mode.
3510
+ $this->_enable_anonymous = false;
3511
+ $this->_anonymous_mode = false;
3512
+ } else {
3513
+ $this->_enable_anonymous = $this->get_bool_option( $plugin_info, 'enable_anonymous', true );
3514
+ $this->_anonymous_mode = $this->get_bool_option( $plugin_info, 'anonymous_mode', false );
3515
+ }
3516
+ $this->_permissions = $this->get_option( $plugin_info, 'permissions', array() );
3517
+
3518
+ if ( ! empty( $plugin_info['trial'] ) ) {
3519
+ $this->_trial_days = $this->get_numeric_option(
3520
+ $plugin_info['trial'],
3521
+ 'days',
3522
+ // Default to 0 - trial without days specification.
3523
+ 0
3524
+ );
3525
+
3526
+ $this->_is_trial_require_payment = $this->get_bool_option( $plugin_info['trial'], 'is_require_payment', false );
3527
+ }
3528
+ }
3529
+
3530
+ /**
3531
+ * @param string[] $options
3532
+ * @param string $key
3533
+ * @param mixed $default
3534
+ *
3535
+ * @return bool
3536
+ */
3537
+ private function get_option( &$options, $key, $default = false ) {
3538
+ return ! empty( $options[ $key ] ) ? $options[ $key ] : $default;
3539
+ }
3540
+
3541
+ private function get_bool_option( &$options, $key, $default = false ) {
3542
+ return isset( $options[ $key ] ) && is_bool( $options[ $key ] ) ? $options[ $key ] : $default;
3543
+ }
3544
+
3545
+ private function get_numeric_option( &$options, $key, $default = false ) {
3546
+ return isset( $options[ $key ] ) && is_numeric( $options[ $key ] ) ? $options[ $key ] : $default;
3547
+ }
3548
+
3549
+ /**
3550
+ * Gate keeper.
3551
+ *
3552
+ * @author Vova Feldman (@svovaf)
3553
+ * @since 1.1.7.3
3554
+ *
3555
+ * @return bool
3556
+ */
3557
+ private function should_stop_execution() {
3558
+ if ( empty( $this->_storage->was_plugin_loaded ) ) {
3559
+ /**
3560
+ * Don't execute Freemius until plugin was fully loaded at least once,
3561
+ * to give the opportunity for the activation hook to run before pinging
3562
+ * the API for connectivity test. This logic is relevant for the
3563
+ * identification of new plugin install vs. plugin update.
3564
+ *
3565
+ * @author Vova Feldman (@svovaf)
3566
+ * @since 1.1.9
3567
+ */
3568
+ return true;
3569
+ }
3570
+
3571
+ if ( $this->is_activation_mode() ) {
3572
+ if ( ! is_admin() ) {
3573
+ /**
3574
+ * If in activation mode, don't execute Freemius outside of the
3575
+ * admin dashboard.
3576
+ *
3577
+ * @author Vova Feldman (@svovaf)
3578
+ * @since 1.1.7.3
3579
+ */
3580
+ return true;
3581
+ }
3582
+
3583
+ if ( ! WP_FS__IS_HTTP_REQUEST ) {
3584
+ /**
3585
+ * If in activation and executed without HTTP context (e.g. CLI, Cronjob),
3586
+ * then don't start Freemius.
3587
+ *
3588
+ * @author Vova Feldman (@svovaf)
3589
+ * @since 1.1.6.3
3590
+ *
3591
+ * @link https://wordpress.org/support/topic/errors-in-the-freemius-class-when-running-in-wordpress-in-cli
3592
+ */
3593
+ return true;
3594
+ }
3595
+
3596
+ if ( self::is_cron() ) {
3597
+ /**
3598
+ * If in activation mode, don't execute Freemius during wp crons
3599
+ * (wp crons have HTTP context - called as HTTP request).
3600
+ *
3601
+ * @author Vova Feldman (@svovaf)
3602
+ * @since 1.1.7.3
3603
+ */
3604
+ return true;
3605
+ }
3606
+
3607
+ if ( self::is_ajax() &&
3608
+ ! $this->_admin_notices->has_sticky( 'failed_connect_api_first' ) &&
3609
+ ! $this->_admin_notices->has_sticky( 'failed_connect_api' )
3610
+ ) {
3611
+ /**
3612
+ * During activation, if running in AJAX mode, unless there's a sticky
3613
+ * connectivity issue notice, don't run Freemius.
3614
+ *
3615
+ * @author Vova Feldman (@svovaf)
3616
+ * @since 1.1.7.3
3617
+ */
3618
+ return true;
3619
+ }
3620
+ }
3621
+
3622
+ return false;
3623
+ }
3624
+
3625
+ /**
3626
+ * Triggered after code type has changed.
3627
+ *
3628
+ * @author Vova Feldman (@svovaf)
3629
+ * @since 1.1.9.1
3630
+ */
3631
+ function _after_code_type_change() {
3632
+ $this->_logger->entrance();
3633
+
3634
+ if ( $this->is_theme() ) {
3635
+ // Expire the cache of the previous tabs since the theme may
3636
+ // have setting updates after code type has changed.
3637
+ $this->_cache->expire( 'tabs' );
3638
+ $this->_cache->expire( 'tabs_stylesheets' );
3639
+ }
3640
+
3641
+ if ( $this->is_registered() ) {
3642
+ if ( ! $this->is_addon() ) {
3643
+ add_action(
3644
+ is_admin() ? 'admin_init' : 'init',
3645
+ array( &$this, '_plugin_code_type_changed' )
3646
+ );
3647
+ }
3648
+
3649
+ if ( $this->is_premium() ) {
3650
+ // Purge cached payments after switching to the premium version.
3651
+ // @todo This logic doesn't handle purging the cache for serviceware module upgrade.
3652
+ $this->get_api_user_scope()->purge_cache( "/plugins/{$this->_module_id}/payments.json?include_addons=true" );
3653
+ }
3654
+ }
3655
+ }
3656
+
3657
+ /**
3658
+ * Handles plugin's code type change (free <--> premium).
3659
+ *
3660
+ * @author Vova Feldman (@svovaf)
3661
+ * @since 1.0.9
3662
+ */
3663
+ function _plugin_code_type_changed() {
3664
+ $this->_logger->entrance();
3665
+
3666
+ if ( $this->is_premium() ) {
3667
+ $this->reconnect_locally();
3668
+
3669
+ // Activated premium code.
3670
+ $this->do_action( 'after_premium_version_activation' );
3671
+
3672
+ // Remove all sticky messages related to download of the premium version.
3673
+ $this->_admin_notices->remove_sticky( array(
3674
+ 'trial_started',
3675
+ 'plan_upgraded',
3676
+ 'plan_changed',
3677
+ 'license_activated',
3678
+ ) );
3679
+
3680
+ if ( ! $this->is_only_premium() ) {
3681
+ $this->_admin_notices->add_sticky(
3682
+ sprintf( $this->get_text_inline( 'Premium %s version was successfully activated.', 'premium-activated-message' ), $this->_module_type ),
3683
+ 'premium_activated',
3684
+ $this->get_text_x_inline( 'W00t',
3685
+ 'Used to express elation, enthusiasm, or triumph (especially in electronic communication).', 'woot' ) . '!'
3686
+ );
3687
+ }
3688
+ } else {
3689
+ // Remove sticky message related to premium code activation.
3690
+ $this->_admin_notices->remove_sticky( 'premium_activated' );
3691
+
3692
+ // Activated free code (after had the premium before).
3693
+ $this->do_action( 'after_free_version_reactivation' );
3694
+
3695
+ if ( $this->is_paying() && ! $this->is_premium() ) {
3696
+ $this->_admin_notices->add_sticky(
3697
+ sprintf(
3698
+ /* translators: %s: License type (e.g. you have a professional license) */
3699
+ $this->get_text_inline( 'You have a %s license.', 'you-have-x-license' ),
3700
+ $this->_site->plan->title
3701
+ ) . $this->get_complete_upgrade_instructions(),
3702
+ 'plan_upgraded',
3703
+ $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
3704
+ );
3705
+ }
3706
+ }
3707
+
3708
+ // Schedule code type changes event.
3709
+ $this->schedule_install_sync();
3710
+
3711
+ /**
3712
+ * Unregister the uninstall hook for the other version of the plugin (with different code type) to avoid
3713
+ * triggering a fatal error when uninstalling that plugin. For example, after deactivating the "free" version
3714
+ * of a specific plugin, its uninstall hook should be unregistered after the "premium" version has been
3715
+ * activated. If we don't do that, a fatal error will occur when we try to uninstall the "free" version since
3716
+ * the main file of the "free" version will be loaded first before calling the hooked callback. Since the
3717
+ * free and premium versions are almost identical (same class or have same functions), a fatal error like
3718
+ * "Cannot redeclare class MyClass" or "Cannot redeclare my_function()" will occur.
3719
+ */
3720
+ $this->unregister_uninstall_hook();
3721
+
3722
+ $this->clear_module_main_file_cache();
3723
+
3724
+ // Update is_premium of latest version.
3725
+ $this->_storage->prev_is_premium = $this->_plugin->is_premium;
3726
+ }
3727
+
3728
+ #endregion
3729
+
3730
+ #----------------------------------------------------------------------------------
3731
+ #region Add-ons
3732
+ #----------------------------------------------------------------------------------
3733
+
3734
+ /**
3735
+ * Check if add-on installed and activated on site.
3736
+ *
3737
+ * @author Vova Feldman (@svovaf)
3738
+ * @since 1.0.6
3739
+ *
3740
+ * @param string|number $id_or_slug
3741
+ * @param bool|null $is_premium Since 1.2.1.7 can check for specified add-on version.
3742
+ *
3743
+ * @return bool
3744
+ */
3745
+ function is_addon_activated( $id_or_slug, $is_premium = null ) {
3746
+ $this->_logger->entrance();
3747
+
3748
+ $addon_id = self::get_module_id( $id_or_slug );
3749
+ $is_activated = self::has_instance( $addon_id );
3750
+
3751
+ if ( ! $is_activated ) {
3752
+ return false;
3753
+ }
3754
+
3755
+ if ( is_bool( $is_premium ) ) {
3756
+ // Check if the specified code version is activate.
3757
+ $addon = $this->get_addon_instance( $addon_id );
3758
+ $is_activated = ( $is_premium === $addon->is_premium() );
3759
+ }
3760
+
3761
+ return $is_activated;
3762
+ }
3763
+
3764
+ /**
3765
+ * Check if add-on was connected to install
3766
+ *
3767
+ * @author Vova Feldman (@svovaf)
3768
+ * @since 1.1.7
3769
+ *
3770
+ * @param string|number $id_or_slug
3771
+ *
3772
+ * @return bool
3773
+ */
3774
+ function is_addon_connected( $id_or_slug ) {
3775
+ $this->_logger->entrance();
3776
+
3777
+ $sites = self::get_all_sites( WP_FS__MODULE_TYPE_PLUGIN );
3778
+
3779
+ $addon_id = self::get_module_id( $id_or_slug );
3780
+ $addon = $this->get_addon( $addon_id );
3781
+ $slug = $addon->slug;
3782
+ if ( ! isset( $sites[ $slug ] ) ) {
3783
+ return false;
3784
+ }
3785
+
3786
+ $site = $sites[ $slug ];
3787
+
3788
+ $plugin = FS_Plugin_Manager::instance( $addon_id )->get();
3789
+
3790
+ if ( $plugin->parent_plugin_id != $this->_plugin->id ) {
3791
+ // The given slug do NOT belong to any of the plugin's add-ons.
3792
+ return false;
3793
+ }
3794
+
3795
+ return ( is_object( $site ) &&
3796
+ is_numeric( $site->id ) &&
3797
+ is_numeric( $site->user_id ) &&
3798
+ is_object( $site->plan )
3799
+ );
3800
+ }
3801
+
3802
+ /**
3803
+ * Determines if add-on installed.
3804
+ *
3805
+ * NOTE: This is a heuristic and only works if the folder/file named as the slug.
3806
+ *
3807
+ * @author Vova Feldman (@svovaf)
3808
+ * @since 1.0.6
3809
+ *
3810
+ * @param string|number $id_or_slug
3811
+ *
3812
+ * @return bool
3813
+ */
3814
+ function is_addon_installed( $id_or_slug ) {
3815
+ $this->_logger->entrance();
3816
+
3817
+ $addon_id = self::get_module_id( $id_or_slug );
3818
+
3819
+ return file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $this->get_addon_basename( $addon_id ) ) );
3820
+ }
3821
+
3822
+ /**
3823
+ * Get add-on basename.
3824
+ *
3825
+ * @author Vova Feldman (@svovaf)
3826
+ * @since 1.0.6
3827
+ *
3828
+ * @param string|number $id_or_slug
3829
+ *
3830
+ * @return string
3831
+ */
3832
+ function get_addon_basename( $id_or_slug ) {
3833
+ $addon_id = self::get_module_id( $id_or_slug );
3834
+
3835
+ if ( $this->is_addon_activated( $addon_id ) ) {
3836
+ return self::instance( $addon_id )->get_plugin_basename();
3837
+ }
3838
+
3839
+ $addon = $this->get_addon( $addon_id );
3840
+ $premium_basename = "{$addon->slug}-premium/{$addon->slug}.php";
3841
+
3842
+ if ( file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $premium_basename ) ) ) {
3843
+ return $premium_basename;
3844
+ }
3845
+
3846
+ $all_plugins = $this->get_all_plugins();
3847
+
3848
+ foreach ( $all_plugins as $basename => &$data ) {
3849
+ if ( $addon->slug === $data['slug'] ||
3850
+ $addon->slug . '-premium' === $data['slug']
3851
+ ) {
3852
+ return $basename;
3853
+ }
3854
+ }
3855
+
3856
+ $free_basename = "{$addon->slug}/{$addon->slug}.php";
3857
+
3858
+ return $free_basename;
3859
+ }
3860
+
3861
+ /**
3862
+ * Get installed add-ons instances.
3863
+ *
3864
+ * @author Vova Feldman (@svovaf)
3865
+ * @since 1.0.6
3866
+ *
3867
+ * @return Freemius[]
3868
+ */
3869
+ function get_installed_addons() {
3870
+ $installed_addons = array();
3871
+ foreach ( self::$_instances as $instance ) {
3872
+ if ( $instance->is_addon() && is_object( $instance->_parent_plugin ) ) {
3873
+ if ( $this->_plugin->id == $instance->_parent_plugin->id ) {
3874
+ $installed_addons[] = $instance;
3875
+ }
3876
+ }
3877
+ }
3878
+
3879
+ return $installed_addons;
3880
+ }
3881
+
3882
+ /**
3883
+ * Check if any add-ons of the plugin are installed.
3884
+ *
3885
+ * @author Leo Fajardo (@leorw)
3886
+ * @since 1.1.1
3887
+ *
3888
+ * @return bool
3889
+ */
3890
+ function has_installed_addons() {
3891
+ if ( ! $this->has_addons() ) {
3892
+ return false;
3893
+ }
3894
+
3895
+ foreach ( self::$_instances as $instance ) {
3896
+ if ( $instance->is_addon() && is_object( $instance->_parent_plugin ) ) {
3897
+ if ( $this->_plugin->id == $instance->_parent_plugin->id ) {
3898
+ return true;
3899
+ }
3900
+ }
3901
+ }
3902
+
3903
+ return false;
3904
+ }
3905
+
3906
+ /**
3907
+ * Tell Freemius that the current plugin is an add-on.
3908
+ *
3909
+ * @author Vova Feldman (@svovaf)
3910
+ * @since 1.0.6
3911
+ *
3912
+ * @param number $parent_plugin_id The parent plugin ID
3913
+ */
3914
+ function init_addon( $parent_plugin_id ) {
3915
+ $this->_plugin->parent_plugin_id = $parent_plugin_id;
3916
+ }
3917
+
3918
+ /**
3919
+ * @author Vova Feldman (@svovaf)
3920
+ * @since 1.0.6
3921
+ *
3922
+ * @return bool
3923
+ */
3924
+ function is_addon() {
3925
+ return isset( $this->_plugin->parent_plugin_id ) && is_numeric( $this->_plugin->parent_plugin_id );
3926
+ }
3927
+
3928
+ /**
3929
+ * Deactivate add-on if it's premium only and the user does't have a valid license.
3930
+ *
3931
+ * @param bool $is_after_trial_cancel
3932
+ *
3933
+ * @return bool If add-on was deactivated.
3934
+ */
3935
+ private function deactivate_premium_only_addon_without_license( $is_after_trial_cancel = false ) {
3936
+ if ( ! $this->has_free_plan() &&
3937
+ ! $this->has_features_enabled_license() &&
3938
+ ! $this->_has_premium_license()
3939
+ ) {
3940
+ if ( $this->is_registered() ) {
3941
+ // IF wrapper is turned off because activation_timestamp is currently only stored for plugins (not addons).
3942
+ // if (empty($this->_storage->activation_timestamp) ||
3943
+ // (WP_FS__SCRIPT_START_TIME - $this->_storage->activation_timestamp) > 30
3944
+ // ) {
3945
+ /**
3946
+ * @todo When it's first fail, there's no reason to try and re-sync because the licenses were just synced after initial activation.
3947
+ *
3948
+ * Retry syncing the user add-on licenses.
3949
+ */
3950
+ // Sync licenses.
3951
+ $this->_sync_licenses();
3952
+ // }
3953
+
3954
+ // Try to activate premium license.
3955
+ $this->_activate_license( true );
3956
+ }
3957
+
3958
+ if ( ! $this->has_free_plan() &&
3959
+ ! $this->has_features_enabled_license() &&
3960
+ ! $this->_has_premium_license()
3961
+ ) {
3962
+ // @todo Check if deactivate plugins also call the deactivation hook.
3963
+
3964
+ $this->_parent->_admin_notices->add_sticky(
3965
+ sprintf(
3966
+ ($is_after_trial_cancel ?
3967
+ $this->_parent->get_text_inline(
3968
+ '%s free trial was successfully cancelled. Since the add-on is premium only it was automatically deactivated. If you like to use it in the future, you\'ll have to purchase a license.',
3969
+ 'addon-trial-cancelled-message'
3970
+ ) :
3971
+ $this->_parent->get_text_inline(
3972
+ '%s is a premium only add-on. You have to purchase a license first before activating the plugin.',
3973
+ 'addon-no-license-message'
3974
+ )
3975
+ ),
3976
+ '<b>' . $this->_plugin->title . '</b>'
3977
+ ) . ' ' . sprintf(
3978
+ '<a href="%s" aria-label="%s" class="button button-primary" style="margin-left: 10px; vertical-align: middle;">%s &nbsp;&#10140;</a>',
3979
+ $this->_parent->addon_url( $this->_slug ),
3980
+ esc_attr( sprintf( $this->_parent->get_text_inline( 'More information about %s', 'more-information-about-x' ), $this->_plugin->title ) ),
3981
+ $this->_parent->get_text_inline( 'Purchase License', 'purchase-license' )
3982
+ ),
3983
+ 'no_addon_license_' . $this->_slug,
3984
+ ( $is_after_trial_cancel ? '' : $this->_parent->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...' ),
3985
+ ( $is_after_trial_cancel ? 'success' : 'error' )
3986
+ );
3987
+
3988
+ deactivate_plugins( array( $this->_plugin_basename ), true );
3989
+
3990
+ return true;
3991
+ }
3992
+ }
3993
+
3994
+ return false;
3995
+ }
3996
+
3997
+ #endregion
3998
+
3999
+ #----------------------------------------------------------------------------------
4000
+ #region Sandbox
4001
+ #----------------------------------------------------------------------------------
4002
+
4003
+ /**
4004
+ * Set Freemius into sandbox mode for debugging.
4005
+ *
4006
+ * @author Vova Feldman (@svovaf)
4007
+ * @since 1.0.4
4008
+ *
4009
+ * @param string $secret_key
4010
+ */
4011
+ function init_sandbox( $secret_key ) {
4012
+ $this->_plugin->secret_key = $secret_key;
4013
+
4014
+ // Update plugin details.
4015
+ FS_Plugin_Manager::instance( $this->_module_id )->update( $this->_plugin, true );
4016
+ }
4017
+
4018
+ /**
4019
+ * Check if running payments in sandbox mode.
4020
+ *
4021
+ * @author Vova Feldman (@svovaf)
4022
+ * @since 1.0.4
4023
+ *
4024
+ * @return bool
4025
+ */
4026
+ function is_payments_sandbox() {
4027
+ return ( ! $this->is_live() ) || isset( $this->_plugin->secret_key );
4028
+ }
4029
+
4030
+ #endregion
4031
+
4032
+ /**
4033
+ * Check if running test vs. live plugin.
4034
+ *
4035
+ * @author Vova Feldman (@svovaf)
4036
+ * @since 1.0.5
4037
+ *
4038
+ * @return bool
4039
+ */
4040
+ function is_live() {
4041
+ return $this->_plugin->is_live;
4042
+ }
4043
+
4044
+ /**
4045
+ * Check if the user skipped connecting the account with Freemius.
4046
+ *
4047
+ * @author Vova Feldman (@svovaf)
4048
+ * @since 1.0.7
4049
+ *
4050
+ * @return bool
4051
+ */
4052
+ function is_anonymous() {
4053
+ if ( ! isset( $this->_is_anonymous ) ) {
4054
+ if ( ! isset( $this->_storage->is_anonymous ) ) {
4055
+ // Not skipped.
4056
+ $this->_is_anonymous = false;
4057
+ } else if ( is_bool( $this->_storage->is_anonymous ) ) {
4058
+ // For back compatibility, since the variable was boolean before.
4059
+ $this->_is_anonymous = $this->_storage->is_anonymous;
4060
+
4061
+ // Upgrade stored data format to 1.1.3 format.
4062
+ $this->set_anonymous_mode( $this->_storage->is_anonymous );
4063
+ } else {
4064
+ // Version 1.1.3 and later.
4065
+ $this->_is_anonymous = $this->_storage->is_anonymous['is'];
4066
+ }
4067
+ }
4068
+
4069
+ return $this->_is_anonymous;
4070
+ }
4071
+
4072
+ /**
4073
+ * Check if user connected his account and install pending email activation.
4074
+ *
4075
+ * @author Vova Feldman (@svovaf)
4076
+ * @since 1.0.7
4077
+ *
4078
+ * @return bool
4079
+ */
4080
+ function is_pending_activation() {
4081
+ return $this->_storage->get( 'is_pending_activation', false );
4082
+ }
4083
+
4084
+ /**
4085
+ * Check if plugin must be WordPress.org compliant.
4086
+ *
4087
+ * @since 1.0.7
4088
+ *
4089
+ * @return bool
4090
+ */
4091
+ function is_org_repo_compliant() {
4092
+ return $this->_is_org_compliant;
4093
+ }
4094
+
4095
+ #----------------------------------------------------------------------------------
4096
+ #region Daily Sync Cron
4097
+ #----------------------------------------------------------------------------------
4098
+
4099
+ /**
4100
+ * @author Vova Feldman (@svovaf)
4101
+ * @since 1.1.7.3
4102
+ */
4103
+ private function run_manual_sync() {
4104
+ self::require_pluggable_essentials();
4105
+
4106
+ if ( ! $this->is_user_admin() ) {
4107
+ return;
4108
+ }
4109
+
4110
+ // Run manual sync.
4111
+ $this->_sync_cron();
4112
+
4113
+ // Reschedule next cron to run 24 hours from now (performance optimization).
4114
+ $this->clear_sync_cron();
4115
+
4116
+ $this->schedule_sync_cron( time() + WP_FS__TIME_24_HOURS_IN_SEC, false );
4117
+ }
4118
+
4119
+ /**
4120
+ * Data sync cron job. Replaces the background sync non blocking HTTP request
4121
+ * that doesn't halt page loading.
4122
+ *
4123
+ * @author Vova Feldman (@svovaf)
4124
+ * @since 1.1.7.3
4125
+ */
4126
+ function _sync_cron() {
4127
+ $this->_logger->entrance();
4128
+
4129
+ // Store the last time data sync was executed.
4130
+ $this->_storage->sync_timestamp = time();
4131
+
4132
+ // Check if API is temporary down.
4133
+ if ( FS_Api::is_temporary_down() ) {
4134
+ return;
4135
+ }
4136
+
4137
+ // @todo Add logic that identifies API latency, and reschedule the next background sync randomly between 8-16 hours.
4138
+
4139
+ if ( $this->is_registered() ) {
4140
+ if ( $this->has_paid_plan() ) {
4141
+ // Initiate background plan sync.
4142
+ $this->_sync_license( true );
4143
+
4144
+ if ( $this->is_paying() ) {
4145
+ // Check for premium plugin updates.
4146
+ $this->check_updates( true );
4147
+ }
4148
+ } else {
4149
+ // Sync install (only if something changed locally).
4150
+ $this->sync_install();
4151
+ }
4152
+ }
4153
+
4154
+ $this->do_action( 'after_sync_cron' );
4155
+ }
4156
+
4157
+ /**
4158
+ * Check if sync was executed in the last $period of seconds.
4159
+ *
4160
+ * @author Vova Feldman (@svovaf)
4161
+ * @since 1.1.7.3
4162
+ *
4163
+ * @param int $period In seconds
4164
+ *
4165
+ * @return bool
4166
+ */
4167
+ private function is_sync_executed( $period = WP_FS__TIME_24_HOURS_IN_SEC ) {
4168
+ if ( ! isset( $this->_storage->sync_timestamp ) ) {
4169
+ return false;
4170
+ }
4171
+
4172
+ return ( $this->_storage->sync_timestamp > ( WP_FS__SCRIPT_START_TIME - $period ) );
4173
+ }
4174
+
4175
+ /**
4176
+ * @author Vova Feldman (@svovaf)
4177
+ * @since 1.1.7.3
4178
+ *
4179
+ * @return bool
4180
+ */
4181
+ private function is_sync_cron_on() {
4182
+ /**
4183
+ * @var object $sync_cron_data
4184
+ */
4185
+ $sync_cron_data = $this->_storage->get( 'sync_cron', null );
4186
+
4187
+ return ( ! is_null( $sync_cron_data ) && true === $sync_cron_data->on );
4188
+ }
4189
+
4190
+ /**
4191
+ * @author Vova Feldman (@svovaf)
4192
+ * @since 1.1.7.3
4193
+ *
4194
+ * @param int $start_at Defaults to now.
4195
+ * @param bool $randomize_start If true, schedule first job randomly during the next 12 hours. Otherwise,
4196
+ * schedule job to start right away.
4197
+ */
4198
+ private function schedule_sync_cron( $start_at = WP_FS__SCRIPT_START_TIME, $randomize_start = true ) {
4199
+ $this->_logger->entrance();
4200
+
4201
+ if ( $randomize_start ) {
4202
+ // Schedule first sync with a random 12 hour time range from now.
4203
+ $start_at += rand( 0, ( WP_FS__TIME_24_HOURS_IN_SEC / 2 ) );
4204
+ }
4205
+
4206
+ // Schedule daily WP cron.
4207
+ wp_schedule_event(
4208
+ $start_at,
4209
+ 'daily',
4210
+ $this->get_action_tag( 'data_sync' )
4211
+ );
4212
+
4213
+ $this->_storage->store( 'sync_cron', (object) array(
4214
+ 'version' => $this->get_plugin_version(),
4215
+ 'sdk_version' => $this->version,
4216
+ 'timestamp' => WP_FS__SCRIPT_START_TIME,
4217
+ 'on' => true,
4218
+ ) );
4219
+ }
4220
+
4221
+ /**
4222
+ * Add the actual sync function to the cron job hook.
4223
+ *
4224
+ * @author Vova Feldman (@svovaf)
4225
+ * @since 1.1.7.3
4226
+ */
4227
+ private function hook_callback_to_sync_cron() {
4228
+ $this->add_action( 'data_sync', array( &$this, '_sync_cron' ) );
4229
+ }
4230
+
4231
+ /**
4232
+ * @author Vova Feldman (@svovaf)
4233
+ * @since 1.1.7.3
4234
+ */
4235
+ private function clear_sync_cron() {
4236
+ $this->_logger->entrance();
4237
+
4238
+ if ( ! $this->is_sync_cron_on() ) {
4239
+ return;
4240
+ }
4241
+
4242
+ $this->_storage->remove( 'sync_cron' );
4243
+
4244
+ wp_clear_scheduled_hook( $this->get_action_tag( 'data_sync' ) );
4245
+ }
4246
+
4247
+ /**
4248
+ * Unix timestamp for next sync cron execution or false if not scheduled.
4249
+ *
4250
+ * @author Vova Feldman (@svovaf)
4251
+ * @since 1.1.7.3
4252
+ *
4253
+ * @return int|false
4254
+ */
4255
+ function next_sync_cron() {
4256
+ $this->_logger->entrance();
4257
+
4258
+ if ( ! $this->is_sync_cron_on() ) {
4259
+ return false;
4260
+ }
4261
+
4262
+ return wp_next_scheduled( $this->get_action_tag( 'data_sync' ) );
4263
+ }
4264
+
4265
+ /**
4266
+ * Unix timestamp for previous sync cron execution or false if never executed.
4267
+ *
4268
+ * @author Vova Feldman (@svovaf)
4269
+ * @since 1.1.7.3
4270
+ *
4271
+ * @return int|false
4272
+ */
4273
+ function last_sync_cron() {
4274
+ $this->_logger->entrance();
4275
+
4276
+ return $this->_storage->get( 'sync_timestamp' );
4277
+ }
4278
+
4279
+ #endregion Daily Sync Cron ------------------------------------------------------------------
4280
+
4281
+ #----------------------------------------------------------------------------------
4282
+ #region Async Install Sync
4283
+ #----------------------------------------------------------------------------------
4284
+
4285
+ /**
4286
+ * @author Vova Feldman (@svovaf)
4287
+ * @since 1.1.7.3
4288
+ *
4289
+ * @return bool
4290
+ */
4291
+ private function is_install_sync_scheduled() {
4292
+ /**
4293
+ * @var object $cron_data
4294
+ */
4295
+ $cron_data = $this->_storage->get( 'install_sync_cron', null );
4296
+
4297
+ return ( ! is_null( $cron_data ) && true === $cron_data->on );
4298
+ }
4299
+
4300
+ /**
4301
+ * Instead of running blocking install sync event, execute non blocking scheduled wp-cron.
4302
+ *
4303
+ * @author Vova Feldman (@svovaf)
4304
+ * @since 1.1.7.3
4305
+ */
4306
+ private function schedule_install_sync() {
4307
+ $this->_logger->entrance();
4308
+
4309
+ $this->clear_install_sync_cron();
4310
+
4311
+ // Schedule immediate install sync.
4312
+ wp_schedule_single_event(
4313
+ WP_FS__SCRIPT_START_TIME,
4314
+ $this->get_action_tag( 'install_sync' )
4315
+ );
4316
+
4317
+ $this->_storage->store( 'install_sync_cron', (object) array(
4318
+ 'version' => $this->get_plugin_version(),
4319
+ 'sdk_version' => $this->version,
4320
+ 'timestamp' => WP_FS__SCRIPT_START_TIME,
4321
+ 'on' => true,
4322
+ ) );
4323
+ }
4324
+
4325
+ /**
4326
+ * Unix timestamp for previous install sync cron execution or false if never executed.
4327
+ *
4328
+ * @todo There's some very strange bug that $this->_storage->install_sync_timestamp value is not being
4329
+ * updated. But for sure the sync event is working.
4330
+ *
4331
+ * @author Vova Feldman (@svovaf)
4332
+ * @since 1.1.7.3
4333
+ *
4334
+ * @return int|false
4335
+ */
4336
+ function last_install_sync() {
4337
+ $this->_logger->entrance();
4338
+
4339
+ return $this->_storage->get( 'install_sync_timestamp' );
4340
+ }
4341
+
4342
+ /**
4343
+ * Unix timestamp for next install sync cron execution or false if not scheduled.
4344
+ *
4345
+ * @author Vova Feldman (@svovaf)
4346
+ * @since 1.1.7.3
4347
+ *
4348
+ * @return int|false
4349
+ */
4350
+ function next_install_sync() {
4351
+ $this->_logger->entrance();
4352
+
4353
+ if ( ! $this->is_install_sync_scheduled() ) {
4354
+ return false;
4355
+ }
4356
+
4357
+ return wp_next_scheduled( $this->get_action_tag( 'install_sync' ) );
4358
+ }
4359
+
4360
+ /**
4361
+ * Add the actual install sync function to the cron job hook.
4362
+ *
4363
+ * @author Vova Feldman (@svovaf)
4364
+ * @since 1.1.7.3
4365
+ */
4366
+ private function hook_callback_to_install_sync() {
4367
+ $this->add_action( 'install_sync', array( &$this, '_run_sync_install' ) );
4368
+ }
4369
+
4370
+ /**
4371
+ * @author Vova Feldman (@svovaf)
4372
+ * @since 1.1.7.3
4373
+ */
4374
+ private function clear_install_sync_cron() {
4375
+ $this->_logger->entrance();
4376
+
4377
+ if ( ! $this->is_install_sync_scheduled() ) {
4378
+ return;
4379
+ }
4380
+
4381
+ $this->_storage->remove( 'install_sync_cron' );
4382
+
4383
+ wp_clear_scheduled_hook( $this->get_action_tag( 'install_sync' ) );
4384
+ }
4385
+
4386
+ /**
4387
+ * @author Vova Feldman (@svovaf)
4388
+ * @since 1.1.7.3
4389
+ */
4390
+ public function _run_sync_install() {
4391
+ $this->_logger->entrance();
4392
+
4393
+ // Update last install sync timestamp.
4394
+ $this->_storage->install_sync_timestamp = time();
4395
+
4396
+ $this->sync_install( array(), true );
4397
+ }
4398
+
4399
+ #endregion Async Install Sync ------------------------------------------------------------------
4400
+
4401
+ /**
4402
+ * Show a notice that activation is currently pending.
4403
+ *
4404
+ * @author Vova Feldman (@svovaf)
4405
+ * @since 1.0.7
4406
+ *
4407
+ * @param bool|string $email
4408
+ * @param bool $is_pending_trial Since 1.2.1.5
4409
+ */
4410
+ function _add_pending_activation_notice( $email = false, $is_pending_trial = false ) {
4411
+ if ( ! is_string( $email ) ) {
4412
+ $current_user = self::_get_current_wp_user();
4413
+ $email = $current_user->user_email;
4414
+ }
4415
+
4416
+ $this->_admin_notices->add_sticky(
4417
+ sprintf(
4418
+ $this->get_text_inline( 'You should receive an activation email for %s to your mailbox at %s. Please make sure you click the activation button in that email to %s.', 'pending-activation-message' ),
4419
+ '<b>' . $this->get_plugin_name() . '</b>',
4420
+ '<b>' . $email . '</b>',
4421
+ ( $is_pending_trial ?
4422
+ $this->get_text_inline( 'start the trial', 'start-the-trial' ) :
4423
+ $this->get_text_inline( 'complete the install', 'complete-the-install' ) )
4424
+ ),
4425
+ 'activation_pending',
4426
+ 'Thanks!'
4427
+ );
4428
+ }
4429
+
4430
+ /**
4431
+ * Check if currently in plugin activation.
4432
+ *
4433
+ * @author Vova Feldman (@svovaf)
4434
+ * @since 1.1.4
4435
+ *
4436
+ * @return bool
4437
+ */
4438
+ function is_plugin_activation() {
4439
+ return get_option( 'fs_'
4440
+ . ( $this->is_plugin() ? '' : $this->_module_type . '_' )
4441
+ . "{$this->_slug}_activated", false );
4442
+ }
4443
+
4444
+ /**
4445
+ *
4446
+ * NOTE: admin_menu action executed before admin_init.
4447
+ *
4448
+ * @author Vova Feldman (@svovaf)
4449
+ * @since 1.0.7
4450
+ */
4451
+ function _admin_init_action() {
4452
+ /**
4453
+ * Automatically redirect to connect/activation page after plugin activation.
4454
+ *
4455
+ * @since 1.1.7 Do NOT redirect to opt-in when running in network admin mode.
4456
+ */
4457
+ if ( $this->is_plugin_activation() ) {
4458
+ delete_option( 'fs_'
4459
+ . ( $this->is_plugin() ? '' : $this->_module_type . '_' )
4460
+ . "{$this->_slug}_activated" );
4461
+
4462
+ if ( ! function_exists( 'is_network_admin' ) || ! is_network_admin() ) {
4463
+ $this->_redirect_on_activation_hook();
4464
+
4465
+ return;
4466
+ }
4467
+ }
4468
+
4469
+ if ( fs_request_is_action( $this->get_unique_affix() . '_skip_activation' ) ) {
4470
+ check_admin_referer( $this->get_unique_affix() . '_skip_activation' );
4471
+
4472
+ $this->skip_connection();
4473
+
4474
+ fs_redirect( $this->get_after_activation_url( 'after_skip_url' ) );
4475
+ }
4476
+
4477
+ if ( ! $this->is_addon() && ! $this->is_registered() && ! $this->is_anonymous() ) {
4478
+ if ( ! $this->is_pending_activation() ) {
4479
+ if ( ! $this->_menu->is_main_settings_page() ) {
4480
+ /**
4481
+ * If a user visits any other admin page before activating the premium-only theme with a valid
4482
+ * license, reactivate the previous theme.
4483
+ *
4484
+ * @author Leo Fajardo (@leorw)
4485
+ * @since 1.2.2
4486
+ */
4487
+ if ( $this->is_theme()
4488
+ && $this->is_only_premium()
4489
+ && ! $this->has_settings_menu()
4490
+ && ! isset( $_REQUEST['fs_action'] )
4491
+ && $this->can_activate_previous_theme()
4492
+ ) {
4493
+ $this->activate_previous_theme();
4494
+
4495
+ return;
4496
+ }
4497
+
4498
+ if ( $this->is_plugin_new_install() || $this->is_only_premium() ) {
4499
+ // Show notice for new plugin installations.
4500
+ $this->_admin_notices->add(
4501
+ sprintf(
4502
+ $this->get_text_inline( 'You are just one step away - %s', 'you-are-step-away' ),
4503
+ sprintf( '<b><a href="%s">%s</a></b>',
4504
+ $this->get_activation_url(),
4505
+ sprintf( $this->get_text_x_inline( 'Complete "%s" Activation Now',
4506
+ '%s - plugin name. As complete "PluginX" activation now', 'activate-x-now' ), $this->get_plugin_name() )
4507
+ )
4508
+ ),
4509
+ '',
4510
+ 'update-nag'
4511
+ );
4512
+ } else {
4513
+ if ( ! isset( $this->_storage->sticky_optin_added ) ) {
4514
+ $this->_storage->sticky_optin_added = true;
4515
+
4516
+ // Show notice for new plugin installations.
4517
+ $this->_admin_notices->add_sticky(
4518
+ sprintf(
4519
+ $this->get_text_inline( 'We made a few tweaks to the %s, %s', 'few-plugin-tweaks' ),
4520
+ $this->_module_type,
4521
+ sprintf( '<b><a href="%s">%s</a></b>',
4522
+ $this->get_activation_url(),
4523
+ sprintf( $this->get_text_inline( 'Opt in to make "%s" Better!', 'optin-x-now' ), $this->get_plugin_name() )
4524
+ )
4525
+ ),
4526
+ 'connect_account',
4527
+ '',
4528
+ 'update-nag'
4529
+ );
4530
+ }
4531
+
4532
+ if ( $this->has_filter( 'optin_pointer_element' ) ) {
4533
+ // Don't show admin nag if plugin update.
4534
+ wp_enqueue_script( 'wp-pointer' );
4535
+ wp_enqueue_style( 'wp-pointer' );
4536
+
4537
+ $this->_enqueue_connect_essentials();
4538
+
4539
+ add_action( 'admin_print_footer_scripts', array(
4540
+ $this,
4541
+ '_add_connect_pointer_script'
4542
+ ) );
4543
+ }
4544
+ }
4545
+ }
4546
+ }
4547
+
4548
+ if ( $this->is_theme() &&
4549
+ $this->_menu->is_main_settings_page()
4550
+ ) {
4551
+ $this->_show_theme_activation_optin_dialog();
4552
+ }
4553
+ }
4554
+
4555
+ $this->_add_upgrade_action_link();
4556
+ }
4557
+
4558
+ /**
4559
+ * Enqueue connect requires scripts and styles.
4560
+ *
4561
+ * @author Vova Feldman (@svovaf)
4562
+ * @since 1.1.4
4563
+ */
4564
+ function _enqueue_connect_essentials() {
4565
+ wp_enqueue_script( 'jquery' );
4566
+ wp_enqueue_script( 'json2' );
4567
+
4568
+ fs_enqueue_local_script( 'postmessage', 'nojquery.ba-postmessage.min.js' );
4569
+ fs_enqueue_local_script( 'fs-postmessage', 'postmessage.js' );
4570
+
4571
+ fs_enqueue_local_style( 'fs_connect', '/admin/connect.css' );
4572
+ }
4573
+
4574
+ /**
4575
+ * Add connect / opt-in pointer.
4576
+ *
4577
+ * @author Vova Feldman (@svovaf)
4578
+ * @since 1.1.4
4579
+ */
4580
+ function _add_connect_pointer_script() {
4581
+ $vars = array( 'id' => $this->_module_id );
4582
+ $pointer_content = fs_get_template( 'connect.php', $vars );
4583
+ ?>
4584
+ <script type="text/javascript">// <![CDATA[
4585
+ jQuery(document).ready(function ($) {
4586
+ if ('undefined' !== typeof(jQuery().pointer)) {
4587
+
4588
+ var element = <?php echo $this->apply_filters( 'optin_pointer_element', '$("#non_existing_element");' ) ?>;
4589
+
4590
+ if (element.length > 0) {
4591
+ var optin = $(element).pointer($.extend(true, {}, {
4592
+ content : <?php echo json_encode( $pointer_content ) ?>,
4593
+ position : {
4594
+ edge : 'left',
4595
+ align: 'center'
4596
+ },
4597
+ buttons : function () {
4598
+ // Don't show pointer buttons.
4599
+ return '';
4600
+ },
4601
+ pointerWidth: 482
4602
+ }, <?php echo $this->apply_filters( 'optin_pointer_options_json', '{}' ) ?>));
4603
+
4604
+ <?php
4605
+ echo $this->apply_filters( 'optin_pointer_execute', "
4606
+
4607
+ optin.pointer('open');
4608
+
4609
+ // Tag the opt-in pointer with custom class.
4610
+ $('.wp-pointer #fs_connect')
4611
+ .parents('.wp-pointer.wp-pointer-top')
4612
+ .addClass('fs-opt-in-pointer');
4613
+
4614
+ ", 'element', 'optin' ) ?>
4615
+ }
4616
+ }
4617
+ });
4618
+ // ]]></script>
4619
+ <?php
4620
+ }
4621
+
4622
+ /**
4623
+ * Return current page's URL.
4624
+ *
4625
+ * @author Vova Feldman (@svovaf)
4626
+ * @since 1.0.7
4627
+ *
4628
+ * @return string
4629
+ */
4630
+ function current_page_url() {
4631
+ $url = 'http';
4632
+
4633
+ if ( isset( $_SERVER["HTTPS"] ) ) {
4634
+ if ( $_SERVER["HTTPS"] == "on" ) {
4635
+ $url .= "s";
4636
+ }
4637
+ }
4638
+ $url .= "://";
4639
+ if ( $_SERVER["SERVER_PORT"] != "80" ) {
4640
+ $url .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
4641
+ } else {
4642
+ $url .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
4643
+ }
4644
+
4645
+ return esc_url( $url );
4646
+ }
4647
+
4648
+ /**
4649
+ * Check if the current page is the plugin's main admin settings page.
4650
+ *
4651
+ * @author Vova Feldman (@svovaf)
4652
+ * @since 1.0.7
4653
+ *
4654
+ * @return bool
4655
+ */
4656
+ function _is_plugin_page() {
4657
+ return fs_is_plugin_page( $this->_menu->get_raw_slug() ) ||
4658
+ fs_is_plugin_page( $this->_slug );
4659
+ }
4660
+
4661
+ /* Events
4662
+ ------------------------------------------------------------------------------------------------------------------*/
4663
+ /**
4664
+ * Delete site install from Database.
4665
+ *
4666
+ * @author Vova Feldman (@svovaf)
4667
+ * @since 1.0.1
4668
+ *
4669
+ * @param bool $store
4670
+ */
4671
+ function _delete_site( $store = true ) {
4672
+ self::_delete_site_by_slug( $this->_slug, $this->_module_type, $store );
4673
+ }
4674
+
4675
+ /**
4676
+ * Delete site install from Database.
4677
+ *
4678
+ * @author Vova Feldman (@svovaf)
4679
+ * @since 1.2.2.7
4680
+ *
4681
+ * @param string $slug
4682
+ * @param string $module_type
4683
+ * @param bool $store
4684
+ */
4685
+ static function _delete_site_by_slug($slug, $module_type, $store = true ) {
4686
+ $sites = self::get_all_sites( $module_type );
4687
+
4688
+ if ( isset( $sites[ $slug ] ) ) {
4689
+ unset( $sites[ $slug ] );
4690
+ }
4691
+
4692
+ self::set_account_option_by_module( $module_type, 'sites', $sites, $store );
4693
+ }
4694
+
4695
+ /**
4696
+ * Delete plugin's plans information.
4697
+ *
4698
+ * @param bool $store Flush to Database if true.
4699
+ *
4700
+ * @author Vova Feldman (@svovaf)
4701
+ * @since 1.0.9
4702
+ */
4703
+ private function _delete_plans( $store = true ) {
4704
+ $this->_logger->entrance();
4705
+
4706
+ $plans = self::get_all_plans( $this->_module_type );
4707
+
4708
+ unset( $plans[ $this->_slug ] );
4709
+
4710
+ $this->set_account_option( 'plans', $plans, $store );
4711
+ }
4712
+
4713
+ /**
4714
+ * Delete all plugin licenses.
4715
+ *
4716
+ * @author Vova Feldman (@svovaf)
4717
+ * @since 1.0.9
4718
+ *
4719
+ * @param bool $store
4720
+ * @param string|bool $plugin_slug
4721
+ */
4722
+ private function _delete_licenses( $store = true, $plugin_slug = false ) {
4723
+ $this->_logger->entrance();
4724
+
4725
+ $all_licenses = self::get_all_licenses( $this->_module_type );
4726
+
4727
+ if ( ! is_string( $plugin_slug ) ) {
4728
+ $plugin_slug = $this->_slug;
4729
+ }
4730
+
4731
+ unset( $all_licenses[ $plugin_slug ] );
4732
+
4733
+ $this->set_account_option( 'licenses', $all_licenses, $store );
4734
+ }
4735
+
4736
+ /**
4737
+ * Check if Freemius was added on new plugin installation.
4738
+ *
4739
+ * @author Vova Feldman (@svovaf)
4740
+ * @since 1.1.5
4741
+ *
4742
+ * @return bool
4743
+ */
4744
+ function is_plugin_new_install() {
4745
+ return isset( $this->_storage->is_plugin_new_install ) &&
4746
+ $this->_storage->is_plugin_new_install;
4747
+ }
4748
+
4749
+ /**
4750
+ * Check if it's the first plugin release that is running Freemius.
4751
+ *
4752
+ * @author Vova Feldman (@svovaf)
4753
+ * @since 1.2.1.5
4754
+ *
4755
+ * @return bool
4756
+ */
4757
+ function is_first_freemius_powered_version() {
4758
+ return empty( $this->_storage->plugin_last_version );
4759
+ }
4760
+
4761
+ /**
4762
+ * @author Leo Fajardo (@leorw)
4763
+ * @since 1.2.2
4764
+ *
4765
+ * @return bool|string
4766
+ */
4767
+ private function get_previous_theme_slug() {
4768
+ return isset( $this->_storage->previous_theme ) ?
4769
+ $this->_storage->previous_theme :
4770
+ false;
4771
+ }
4772
+
4773
+ /**
4774
+ * @author Leo Fajardo (@leorw)
4775
+ * @since 1.2.2
4776
+ *
4777
+ * @return string
4778
+ */
4779
+ private function can_activate_previous_theme() {
4780
+ $slug = $this->get_previous_theme_slug();
4781
+ if ( false !== $slug && current_user_can( 'switch_themes' ) ) {
4782
+ $theme_instance = wp_get_theme( $slug );
4783
+
4784
+ return $theme_instance->exists();
4785
+ }
4786
+
4787
+ return false;
4788
+ }
4789
+
4790
+ /**
4791
+ * @author Leo Fajardo (@leorw)
4792
+ * @since 1.2.2
4793
+ *
4794
+ * @return string
4795
+ */
4796
+ private function activate_previous_theme() {
4797
+ switch_theme( $this->get_previous_theme_slug() );
4798
+ unset( $this->_storage->previous_theme );
4799
+
4800
+ global $pagenow;
4801
+ if ( 'themes.php' === $pagenow ) {
4802
+ /**
4803
+ * Refresh the active theme information.
4804
+ *
4805
+ * @author Leo Fajardo (@leorw)
4806
+ * @since 1.2.2
4807
+ */
4808
+ fs_redirect( admin_url( $pagenow ) );
4809
+ }
4810
+ }
4811
+
4812
+ /**
4813
+ * @author Leo Fajardo (@leorw)
4814
+ * @since 1.2.2
4815
+ *
4816
+ * @return string
4817
+ */
4818
+ function get_previous_theme_activation_url() {
4819
+ if ( ! $this->can_activate_previous_theme() ) {
4820
+ return '';
4821
+ }
4822
+
4823
+ /**
4824
+ * Activation URL
4825
+ *
4826
+ * @author Leo Fajardo (@leorw)
4827
+ * @since 1.2.2
4828
+ */
4829
+ return wp_nonce_url(
4830
+ admin_url( 'themes.php?action=activate&stylesheet=' . urlencode( $this->get_previous_theme_slug() ) ),
4831
+ 'switch-theme_' . $this->get_previous_theme_slug()
4832
+ );
4833
+ }
4834
+
4835
+ /**
4836
+ * Saves the slug of the previous theme if it still exists so that it can be used by the logic in the opt-in
4837
+ * form that decides whether to add a close button to the opt-in dialog or not. So after a premium-only theme is
4838
+ * activated, the close button will appear and will reactivate the previous theme if clicked. If the previous
4839
+ * theme doesn't exist, then there will be no close button.
4840
+ *
4841
+ * @author Leo Fajardo (@leorw)
4842
+ * @since 1.2.2
4843
+ *
4844
+ * @param string $slug_or_name Old theme's slug or name.
4845
+ * @param bool|WP_Theme $old_theme WP_Theme instance of the old theme if it still exists.
4846
+ */
4847
+ function _activate_theme_event_hook( $slug_or_name, $old_theme = false ) {
4848
+ $this->_storage->previous_theme = ( false !== $old_theme ) ?
4849
+ $old_theme->get_stylesheet() :
4850
+ $slug_or_name;
4851
+
4852
+ $this->_activate_plugin_event_hook();
4853
+ }
4854
+
4855
+ /**
4856
+ * Plugin activated hook.
4857
+ *
4858
+ * @author Vova Feldman (@svovaf)
4859
+ * @since 1.0.1
4860
+ *
4861
+ * @uses FS_Api
4862
+ */
4863
+ function _activate_plugin_event_hook() {
4864
+ $this->_logger->entrance( 'slug = ' . $this->_slug );
4865
+
4866
+ if ( ! $this->is_user_admin() ) {
4867
+ return;
4868
+ }
4869
+
4870
+ $this->unregister_uninstall_hook();
4871
+
4872
+ // Clear API cache on activation.
4873
+ FS_Api::clear_cache();
4874
+
4875
+ $is_premium_version_activation = ( current_filter() !== ( 'activate_' . $this->_free_plugin_basename ) );
4876
+
4877
+ $this->_logger->info( 'Activating ' . ( $is_premium_version_activation ? 'premium' : 'free' ) . ' plugin version.' );
4878
+
4879
+ // 1. If running in the activation of the FREE module, get the basename of the PREMIUM.
4880
+ // 2. If running in the activation of the PREMIUM module, get the basename of the FREE.
4881
+ $other_version_basename = $is_premium_version_activation ?
4882
+ $this->_free_plugin_basename :
4883
+ $this->premium_plugin_basename();
4884
+
4885
+ /**
4886
+ * If the other module version is activate, deactivate it.
4887
+ *
4888
+ * @author Leo Fajardo (@leorw)
4889
+ * @since 1.2.2
4890
+ */
4891
+ if ( is_plugin_active( $other_version_basename ) ) {
4892
+ deactivate_plugins( $other_version_basename );
4893
+ }
4894
+
4895
+ if ( $this->is_registered() ) {
4896
+ if ( $is_premium_version_activation ) {
4897
+ $this->reconnect_locally();
4898
+ }
4899
+
4900
+
4901
+ // Schedule re-activation event and sync.
4902
+ // $this->sync_install( array(), true );
4903
+ $this->schedule_install_sync();
4904
+
4905
+ // If activating the premium module version, add an admin notice to congratulate for an upgrade completion.
4906
+ if ( $is_premium_version_activation ) {
4907
+ $this->_admin_notices->add(
4908
+ sprintf( $this->get_text_inline( 'The upgrade of %s was successfully completed.', 'successful-version-upgrade-message' ), sprintf( '<b>%s</b>', $this->_plugin->title ) ),
4909
+ $this->get_text_x_inline( 'W00t',
4910
+ 'Used to express elation, enthusiasm, or triumph (especially in electronic communication).', 'woot' ) . '!'
4911
+ );
4912
+ }
4913
+ } else if ( $this->is_anonymous() ) {
4914
+ /**
4915
+ * Reset "skipped" click cache on the following:
4916
+ * 1. Freemius DEV mode.
4917
+ * 2. WordPress DEBUG mode.
4918
+ * 3. If a plugin and the user skipped the exact same version before.
4919
+ *
4920
+ * @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).
4921
+ *
4922
+ * @todo 4. If explicitly asked to retry after every activation.
4923
+ */
4924
+ if ( WP_FS__DEV_MODE ||
4925
+ (
4926
+ ( $this->is_plugin() || ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ) &&
4927
+ $this->get_plugin_version() == $this->_storage->is_anonymous['version']
4928
+ )
4929
+ ) {
4930
+ $this->reset_anonymous_mode();
4931
+ }
4932
+ }
4933
+
4934
+ if ( ! isset( $this->_storage->is_plugin_new_install ) ) {
4935
+ /**
4936
+ * If no previous version of plugin's version exist, it means that it's either
4937
+ * the first time that the plugin installed on the site, or the plugin was installed
4938
+ * before but didn't have Freemius integrated.
4939
+ *
4940
+ * Since register_activation_hook() do NOT fires on updates since 3.1, and only fires
4941
+ * on manual activation via the dashboard, is_plugin_activation() is TRUE
4942
+ * only after immediate activation.
4943
+ *
4944
+ * @since 1.1.4
4945
+ * @link https://make.wordpress.org/core/2010/10/27/plugin-activation-hooks-no-longer-fire-for-updates/
4946
+ */
4947
+ $this->_storage->is_plugin_new_install = empty( $this->_storage->plugin_last_version );
4948
+ }
4949
+
4950
+ if ( ! $this->_anonymous_mode &&
4951
+ $this->has_api_connectivity( WP_FS__DEV_MODE ) &&
4952
+ ! $this->_isAutoInstall
4953
+ ) {
4954
+ // Store hint that the plugin was just activated to enable auto-redirection to settings.
4955
+ add_option( 'fs_'
4956
+ . ( $this->is_plugin() ? '' : $this->_module_type . '_' )
4957
+ . "{$this->_slug}_activated", true );
4958
+ }
4959
+
4960
+ /**
4961
+ * Activation hook is executed after the plugin's main file is loaded, therefore,
4962
+ * after the plugin was loaded. The logic is located at activate_plugin()
4963
+ * ./wp-admin/includes/plugin.php.
4964
+ *
4965
+ * @author Vova Feldman (@svovaf)
4966
+ * @since 1.1.9
4967
+ */
4968
+ $this->_storage->was_plugin_loaded = true;
4969
+ }
4970
+
4971
+ /**
4972
+ * Delete account.
4973
+ *
4974
+ * @author Vova Feldman (@svovaf)
4975
+ * @since 1.0.3
4976
+ *
4977
+ * @param bool $check_user Enforce checking if user have plugins activation privileges.
4978
+ */
4979
+ function delete_account_event( $check_user = true ) {
4980
+ $this->_logger->entrance( 'slug = ' . $this->_slug );
4981
+
4982
+ if ( $check_user && ! $this->is_user_admin() ) {
4983
+ return;
4984
+ }
4985
+
4986
+ $this->do_action( 'before_account_delete' );
4987
+
4988
+ // Clear all admin notices.
4989
+ $this->_admin_notices->clear_all_sticky();
4990
+
4991
+ $this->_delete_site( false );
4992
+
4993
+ $this->_delete_plans( false );
4994
+
4995
+ $this->_delete_licenses( false );
4996
+
4997
+ // Delete add-ons related to plugin's account.
4998
+ $this->_delete_account_addons( false );
4999
+
5000
+ // @todo Delete plans and licenses of add-ons.
5001
+
5002
+ self::$_accounts->store();
5003
+
5004
+ /**
5005
+ * IMPORTANT:
5006
+ * Clear crons must be executed before clearing all storage.
5007
+ * Otherwise, the cron will not be cleared.
5008
+ */
5009
+ $this->clear_sync_cron();
5010
+ $this->clear_install_sync_cron();
5011
+
5012
+ // Clear all storage data.
5013
+ $this->_storage->clear_all( true, array(
5014
+ 'connectivity_test',
5015
+ 'is_on',
5016
+ ) );
5017
+
5018
+ // Send delete event.
5019
+ $this->get_api_site_scope()->call( '/', 'delete' );
5020
+
5021
+ $this->do_action( 'after_account_delete' );
5022
+ }
5023
+
5024
+ /**
5025
+ * Plugin deactivation hook.
5026
+ *
5027
+ * @author Vova Feldman (@svovaf)
5028
+ * @since 1.0.1
5029
+ */
5030
+ function _deactivate_plugin_hook() {
5031
+ $this->_logger->entrance( 'slug = ' . $this->_slug );
5032
+
5033
+ if ( ! current_user_can( 'activate_plugins' ) ) {
5034
+ return;
5035
+ }
5036
+
5037
+ $this->_admin_notices->clear_all_sticky();
5038
+ if ( isset( $this->_storage->sticky_optin_added ) ) {
5039
+ unset( $this->_storage->sticky_optin_added );
5040
+ }
5041
+
5042
+ if ( ! isset( $this->_storage->is_plugin_new_install ) ) {
5043
+ // Remember that plugin was already installed.
5044
+ $this->_storage->is_plugin_new_install = false;
5045
+ }
5046
+
5047
+ // Hook to plugin uninstall.
5048
+ register_uninstall_hook( $this->_plugin_main_file_path, array( 'Freemius', '_uninstall_plugin_hook' ) );
5049
+
5050
+ $this->clear_module_main_file_cache();
5051
+ $this->clear_sync_cron();
5052
+ $this->clear_install_sync_cron();
5053
+
5054
+ if ( $this->is_registered() ) {
5055
+ // Send deactivation event.
5056
+ $this->sync_install( array(
5057
+ 'is_active' => false,
5058
+ ) );
5059
+ } else {
5060
+ if ( ! $this->has_api_connectivity() ) {
5061
+ // Reset connectivity test cache.
5062
+ unset( $this->_storage->connectivity_test );
5063
+ }
5064
+ }
5065
+
5066
+ // Clear API cache on deactivation.
5067
+ FS_Api::clear_cache();
5068
+
5069
+ $this->remove_sdk_reference();
5070
+ }
5071
+
5072
+ /**
5073
+ * @author Vova Feldman (@svovaf)
5074
+ * @since 1.1.6
5075
+ */
5076
+ private function remove_sdk_reference() {
5077
+ global $fs_active_plugins;
5078
+
5079
+ foreach ( $fs_active_plugins->plugins as $sdk_path => &$data ) {
5080
+ if ( $this->_plugin_basename == $data->plugin_path ) {
5081
+ unset( $fs_active_plugins->plugins[ $sdk_path ] );
5082
+ break;
5083
+ }
5084
+ }
5085
+
5086
+ fs_fallback_to_newest_active_sdk();
5087
+ }
5088
+
5089
+ /**
5090
+ * @author Vova Feldman (@svovaf)
5091
+ * @since 1.1.3
5092
+ *
5093
+ * @param bool $is_anonymous
5094
+ */
5095
+ private function set_anonymous_mode( $is_anonymous = true ) {
5096
+ // Store information regarding skip to try and opt-in the user
5097
+ // again in the future.
5098
+ $this->_storage->is_anonymous = array(
5099
+ 'is' => $is_anonymous,
5100
+ 'timestamp' => WP_FS__SCRIPT_START_TIME,
5101
+ 'version' => $this->get_plugin_version(),
5102
+ );
5103
+
5104
+ // Update anonymous mode cache.
5105
+ $this->_is_anonymous = $is_anonymous;
5106
+ }
5107
+
5108
+ /**
5109
+ * @author Vova Feldman (@svovaf)
5110
+ * @since 1.1.3
5111
+ */
5112
+ private function reset_anonymous_mode() {
5113
+ unset( $this->_storage->is_anonymous );
5114
+
5115
+ /**
5116
+ * Ensure that this field is also "false", otherwise, if the current module's type is "theme" and the module
5117
+ * has no menus, the opt-in popup will not be shown immediately (in this case, the user will have to click
5118
+ * on the admin notice that contains the opt-in link in order to trigger the opt-in popup).
5119
+ *
5120
+ * @author Leo Fajardo (@leorw)
5121
+ * @since 1.2.2
5122
+ */
5123
+ unset( $this->_is_anonymous );
5124
+ }
5125
+
5126
+ /**
5127
+ * Clears the anonymous mode and redirects to the opt-in screen.
5128
+ *
5129
+ * @author Vova Feldman (@svovaf)
5130
+ * @since 1.1.7
5131
+ */
5132
+ function connect_again() {
5133
+ if ( ! $this->is_anonymous() ) {
5134
+ return;
5135
+ }
5136
+
5137
+ $this->reset_anonymous_mode();
5138
+
5139
+ fs_redirect( $this->get_activation_url() );
5140
+ }
5141
+
5142
+ /**
5143
+ * Skip account connect, and set anonymous mode.
5144
+ *
5145
+ * @author Vova Feldman (@svovaf)
5146
+ * @since 1.1.1
5147
+ */
5148
+ private function skip_connection() {
5149
+ $this->_logger->entrance();
5150
+
5151
+ $this->_admin_notices->remove_sticky( 'connect_account' );
5152
+
5153
+ $this->set_anonymous_mode();
5154
+
5155
+ // Send anonymous skip event.
5156
+ // No user identified info nor any tracking will be sent after the user skips the opt-in.
5157
+ $this->get_api_plugin_scope()->call( 'skip.json', 'put', array(
5158
+ 'uid' => $this->get_anonymous_id(),
5159
+ ) );
5160
+ }
5161
+
5162
+ /**
5163
+ * Plugin version update hook.
5164
+ *
5165
+ * @author Vova Feldman (@svovaf)
5166
+ * @since 1.0.4
5167
+ */
5168
+ private function update_plugin_version_event() {
5169
+ $this->_logger->entrance();
5170
+
5171
+ if ( ! $this->is_registered() ) {
5172
+ return;
5173
+ }
5174
+
5175
+ $this->schedule_install_sync();
5176
+ // $this->sync_install( array(), true );
5177
+ }
5178
+
5179
+ /**
5180
+ * Return a list of modified plugins since the last sync.
5181
+ *
5182
+ * Note:
5183
+ * There's no point to store a plugins counter since even if the number of
5184
+ * plugins didn't change, we still need to check if the versions are all the
5185
+ * same and the activity state is similar.
5186
+ *
5187
+ * @author Vova Feldman (@svovaf)
5188
+ * @since 1.1.8
5189
+ *
5190
+ * @return array|false
5191
+ */
5192
+ private function get_plugins_data_for_api() {
5193
+ // Alias.
5194
+ $option_name = 'all_plugins';
5195
+
5196
+ $all_cached_plugins = self::$_accounts->get_option( $option_name );
5197
+
5198
+ if ( ! is_object( $all_cached_plugins ) ) {
5199
+ $all_cached_plugins = (object) array(
5200
+ 'timestamp' => '',
5201
+ 'md5' => '',
5202
+ 'plugins' => array(),
5203
+ );
5204
+ }
5205
+
5206
+ $time = time();
5207
+
5208
+ if ( ! empty( $all_cached_plugins->timestamp ) &&
5209
+ ( $time - $all_cached_plugins->timestamp ) < WP_FS__TIME_5_MIN_IN_SEC
5210
+ ) {
5211
+ // Don't send plugin updates if last update was in the past 5 min.
5212
+ return false;
5213
+ }
5214
+
5215
+ // Write timestamp to lock the logic.
5216
+ $all_cached_plugins->timestamp = $time;
5217
+ self::$_accounts->set_option( $option_name, $all_cached_plugins, true );
5218
+
5219
+ // Reload options from DB.
5220
+ self::$_accounts->load( true );
5221
+ $all_cached_plugins = self::$_accounts->get_option( $option_name );
5222
+
5223
+ if ( $time != $all_cached_plugins->timestamp ) {
5224
+ // If timestamp is different, then another thread captured the lock.
5225
+ return false;
5226
+ }
5227
+
5228
+ // Check if there's a change in plugins.
5229
+ $all_plugins = self::get_all_plugins();
5230
+
5231
+ // Check if plugins changed.
5232
+ ksort( $all_plugins );
5233
+
5234
+ $plugins_signature = '';
5235
+ foreach ( $all_plugins as $basename => $data ) {
5236
+ $plugins_signature .= $data['slug'] . ',' .
5237
+ $data['Version'] . ',' .
5238
+ ( $data['is_active'] ? '1' : '0' ) . ';';
5239
+ }
5240
+
5241
+ // Check if plugins status changed (version or active/inactive).
5242
+ $plugins_changed = ( $all_cached_plugins->md5 !== md5( $plugins_signature ) );
5243
+
5244
+ $plugins_update_data = array();
5245
+
5246
+ if ( $plugins_changed ) {
5247
+ // Change in plugins, report changes.
5248
+
5249
+ // Update existing plugins info.
5250
+ foreach ( $all_cached_plugins->plugins as $basename => $data ) {
5251
+ if ( ! isset( $all_plugins[ $basename ] ) ) {
5252
+ // Plugin uninstalled.
5253
+ $uninstalled_plugin_data = $data;
5254
+ $uninstalled_plugin_data['is_active'] = false;
5255
+ $uninstalled_plugin_data['is_uninstalled'] = true;
5256
+ $plugins_update_data[] = $uninstalled_plugin_data;
5257
+
5258
+ unset( $all_plugins[ $basename ] );
5259
+ unset( $all_cached_plugins->plugins[ $basename ] );
5260
+ } else if ( $data['is_active'] !== $all_plugins[ $basename ]['is_active'] ||
5261
+ $data['version'] !== $all_plugins[ $basename ]['Version']
5262
+ ) {
5263
+ // Plugin activated or deactivated, or version changed.
5264
+ $all_cached_plugins->plugins[ $basename ]['is_active'] = $all_plugins[ $basename ]['is_active'];
5265
+ $all_cached_plugins->plugins[ $basename ]['version'] = $all_plugins[ $basename ]['Version'];
5266
+
5267
+ $plugins_update_data[] = $all_cached_plugins->plugins[ $basename ];
5268
+ }
5269
+ }
5270
+
5271
+ // Find new plugins that weren't yet seen before.
5272
+ foreach ( $all_plugins as $basename => $data ) {
5273
+ if ( ! isset( $all_cached_plugins->plugins[ $basename ] ) ) {
5274
+ // New plugin.
5275
+ $new_plugin = array(
5276
+ 'slug' => $data['slug'],
5277
+ 'version' => $data['Version'],
5278
+ 'title' => $data['Name'],
5279
+ 'is_active' => $data['is_active'],
5280
+ 'is_uninstalled' => false,
5281
+ );
5282
+
5283
+ $plugins_update_data[] = $new_plugin;
5284
+ $all_cached_plugins->plugins[ $basename ] = $new_plugin;
5285
+ }
5286
+ }
5287
+
5288
+ $all_cached_plugins->md5 = md5( $plugins_signature );
5289
+ $all_cached_plugins->timestamp = $time;
5290
+ self::$_accounts->set_option( $option_name, $all_cached_plugins, true );
5291
+ }
5292
+
5293
+ return $plugins_update_data;
5294
+ }
5295
+
5296
+ /**
5297
+ * Return a list of modified themes since the last sync.
5298
+ *
5299
+ * Note:
5300
+ * There's no point to store a themes counter since even if the number of
5301
+ * themes didn't change, we still need to check if the versions are all the
5302
+ * same and the activity state is similar.
5303
+ *
5304
+ * @author Vova Feldman (@svovaf)
5305
+ * @since 1.1.8
5306
+ *
5307
+ * @return array|false
5308
+ */
5309
+ private function get_themes_data_for_api() {
5310
+ // Alias.
5311
+ $option_name = 'all_themes';
5312
+
5313
+ $all_cached_themes = self::$_accounts->get_option( $option_name );
5314
+
5315
+ if ( ! is_object( $all_cached_themes ) ) {
5316
+ $all_cached_themes = (object) array(
5317
+ 'timestamp' => '',
5318
+ 'md5' => '',
5319
+ 'themes' => array(),
5320
+ );
5321
+ }
5322
+
5323
+ $time = time();
5324
+
5325
+ if ( ! empty( $all_cached_themes->timestamp ) &&
5326
+ ( $time - $all_cached_themes->timestamp ) < WP_FS__TIME_5_MIN_IN_SEC
5327
+ ) {
5328
+ // Don't send theme updates if last update was in the past 5 min.
5329
+ return false;
5330
+ }
5331
+
5332
+ // Write timestamp to lock the logic.
5333
+ $all_cached_themes->timestamp = $time;
5334
+ self::$_accounts->set_option( $option_name, $all_cached_themes, true );
5335
+
5336
+ // Reload options from DB.
5337
+ self::$_accounts->load( true );
5338
+ $all_cached_themes = self::$_accounts->get_option( $option_name );
5339
+
5340
+ if ( $time != $all_cached_themes->timestamp ) {
5341
+ // If timestamp is different, then another thread captured the lock.
5342
+ return false;
5343
+ }
5344
+
5345
+ // Get active theme.
5346
+ $active_theme = wp_get_theme();
5347
+ $active_theme_stylesheet = $active_theme->get_stylesheet();
5348
+
5349
+ // Check if there's a change in themes.
5350
+ $all_themes = wp_get_themes();
5351
+
5352
+ // Check if themes changed.
5353
+ ksort( $all_themes );
5354
+
5355
+ $themes_signature = '';
5356
+ foreach ( $all_themes as $slug => $data ) {
5357
+ $is_active = ( $slug === $active_theme_stylesheet );
5358
+ $themes_signature .= $slug . ',' .
5359
+ $data->version . ',' .
5360
+ ( $is_active ? '1' : '0' ) . ';';
5361
+ }
5362
+
5363
+ // Check if themes status changed (version or active/inactive).
5364
+ $themes_changed = ( $all_cached_themes->md5 !== md5( $themes_signature ) );
5365
+
5366
+ $themes_update_data = array();
5367
+
5368
+ if ( $themes_changed ) {
5369
+ // Change in themes, report changes.
5370
+
5371
+ // Update existing themes info.
5372
+ foreach ( $all_cached_themes->themes as $slug => $data ) {
5373
+ $is_active = ( $slug === $active_theme_stylesheet );
5374
+
5375
+ if ( ! isset( $all_themes[ $slug ] ) ) {
5376
+ // Plugin uninstalled.
5377
+ $uninstalled_theme_data = $data;
5378
+ $uninstalled_theme_data['is_active'] = false;
5379
+ $uninstalled_theme_data['is_uninstalled'] = true;
5380
+ $themes_update_data[] = $uninstalled_theme_data;
5381
+
5382
+ unset( $all_themes[ $slug ] );
5383
+ unset( $all_cached_themes->themes[ $slug ] );
5384
+ } else if ( $data['is_active'] !== $is_active ||
5385
+ $data['version'] !== $all_themes[ $slug ]->version
5386
+ ) {
5387
+ // Plugin activated or deactivated, or version changed.
5388
+
5389
+ $all_cached_themes->themes[ $slug ]['is_active'] = $is_active;
5390
+ $all_cached_themes->themes[ $slug ]['version'] = $all_themes[ $slug ]->version;
5391
+
5392
+ $themes_update_data[] = $all_cached_themes->themes[ $slug ];
5393
+ }
5394
+ }
5395
+
5396
+ // Find new themes that weren't yet seen before.
5397
+ foreach ( $all_themes as $slug => $data ) {
5398
+ if ( ! isset( $all_cached_themes->themes[ $slug ] ) ) {
5399
+ $is_active = ( $slug === $active_theme_stylesheet );
5400
+
5401
+ // New plugin.
5402
+ $new_plugin = array(
5403
+ 'slug' => $slug,
5404
+ 'version' => $data->version,
5405
+ 'title' => $data->name,
5406
+ 'is_active' => $is_active,
5407
+ 'is_uninstalled' => false,
5408
+ );
5409
+
5410
+ $themes_update_data[] = $new_plugin;
5411
+ $all_cached_themes->themes[ $slug ] = $new_plugin;
5412
+ }
5413
+ }
5414
+
5415
+ $all_cached_themes->md5 = md5( $themes_signature );
5416
+ $all_cached_themes->timestamp = time();
5417
+ self::$_accounts->set_option( $option_name, $all_cached_themes, true );
5418
+ }
5419
+
5420
+ return $themes_update_data;
5421
+ }
5422
+
5423
+ /**
5424
+ * Update install details.
5425
+ *
5426
+ * @author Vova Feldman (@svovaf)
5427
+ * @since 1.1.2
5428
+ *
5429
+ * @param string[] string $override
5430
+ * @param bool $include_plugins Since 1.1.8 by default include plugin changes.
5431
+ * @param bool $include_themes Since 1.1.8 by default include plugin changes.
5432
+ *
5433
+ * @return array
5434
+ */
5435
+ private function get_install_data_for_api(
5436
+ array $override,
5437
+ $include_plugins = true,
5438
+ $include_themes = true
5439
+ ) {
5440
+ /**
5441
+ * @since 1.1.8 Also send plugin updates.
5442
+ */
5443
+ if ( $include_plugins && ! isset( $override['plugins'] ) ) {
5444
+ $plugins = $this->get_plugins_data_for_api();
5445
+ if ( ! empty( $plugins ) ) {
5446
+ $override['plugins'] = $plugins;
5447
+ }
5448
+ }
5449
+ /**
5450
+ * @since 1.1.8 Also send themes updates.
5451
+ */
5452
+ if ( $include_themes && ! isset( $override['themes'] ) ) {
5453
+ $themes = $this->get_themes_data_for_api();
5454
+ if ( ! empty( $themes ) ) {
5455
+ $override['themes'] = $themes;
5456
+ }
5457
+ }
5458
+
5459
+ return array_merge( array(
5460
+ 'version' => $this->get_plugin_version(),
5461
+ 'is_premium' => $this->is_premium(),
5462
+ 'language' => get_bloginfo( 'language' ),
5463
+ 'charset' => get_bloginfo( 'charset' ),
5464
+ 'platform_version' => get_bloginfo( 'version' ),
5465
+ 'sdk_version' => $this->version,
5466
+ 'programming_language_version' => phpversion(),
5467
+ 'title' => get_bloginfo( 'name' ),
5468
+ 'url' => get_site_url(),
5469
+ // Special params.
5470
+ 'is_active' => true,
5471
+ 'is_disconnected' => $this->is_tracking_prohibited(),
5472
+ 'is_uninstalled' => false,
5473
+ ), $override );
5474
+ }
5475
+
5476
+ /**
5477
+ * Update install only if changed.
5478
+ *
5479
+ * @author Vova Feldman (@svovaf)
5480
+ * @since 1.0.9
5481
+ *
5482
+ * @param string[] string $override
5483
+ * @param bool $flush
5484
+ *
5485
+ * @return false|object|string
5486
+ */
5487
+ private function send_install_update( $override = array(), $flush = false ) {
5488
+ $this->_logger->entrance();
5489
+
5490
+ $check_properties = $this->get_install_data_for_api( $override );
5491
+
5492
+ if ( $flush ) {
5493
+ $params = $check_properties;
5494
+ } else {
5495
+ $params = array();
5496
+ $special = array();
5497
+ $special_override = false;
5498
+
5499
+ foreach ( $check_properties as $p => $v ) {
5500
+ if ( property_exists( $this->_site, $p ) ) {
5501
+ if ( ( is_bool( $this->_site->{$p} ) || ! empty( $this->_site->{$p} ) ) &&
5502
+ $this->_site->{$p} != $v
5503
+ ) {
5504
+ $this->_site->{$p} = $v;
5505
+ $params[ $p ] = $v;
5506
+ }
5507
+ } else {
5508
+ $special[ $p ] = $v;
5509
+
5510
+ if ( isset( $override[ $p ] ) ||
5511
+ 'plugins' === $p ||
5512
+ 'themes' === $p
5513
+ ) {
5514
+ $special_override = true;
5515
+ }
5516
+ }
5517
+ }
5518
+
5519
+ if ( $special_override || 0 < count( $params ) ) {
5520
+ // Add special params only if has at least one
5521
+ // standard param, or if explicitly requested to
5522
+ // override a special param or a param which is not exist
5523
+ // in the install object.
5524
+ $params = array_merge( $params, $special );
5525
+ }
5526
+ }
5527
+
5528
+ if ( 0 < count( $params ) ) {
5529
+ // Update last install sync timestamp.
5530
+ $this->_storage->install_sync_timestamp = time();
5531
+
5532
+ $params['uid'] = $this->get_anonymous_id();
5533
+
5534
+ // Send updated values to FS.
5535
+ $site = $this->get_api_site_scope()->call( '/', 'put', $params );
5536
+
5537
+ if ( $this->is_api_result_entity( $site ) ) {
5538
+ // I successfully sent install update, clear scheduled sync if exist.
5539
+ $this->clear_install_sync_cron();
5540
+ }
5541
+
5542
+ return $site;
5543
+ }
5544
+
5545
+ return false;
5546
+ }
5547
+
5548
+ /**
5549
+ * Update install only if changed.
5550
+ *
5551
+ * @author Vova Feldman (@svovaf)
5552
+ * @since 1.0.9
5553
+ *
5554
+ * @param string[] string $override
5555
+ * @param bool $flush
5556
+ */
5557
+ private function sync_install( $override = array(), $flush = false ) {
5558
+ $this->_logger->entrance();
5559
+
5560
+ $site = $this->send_install_update( $override, $flush );
5561
+
5562
+ if ( false === $site ) {
5563
+ // No sync required.
5564
+ return;
5565
+ }
5566
+
5567
+ if ( ! $this->is_api_result_entity( $site ) ) {
5568
+ // Failed to sync, don't update locally.
5569
+ return;
5570
+ }
5571
+
5572
+ $plan = $this->get_plan();
5573
+ $this->_site = new FS_Site( $site );
5574
+ $this->_site->plan = $plan;
5575
+
5576
+ $this->_store_site( true );
5577
+ }
5578
+
5579
+ /**
5580
+ * Track install's custom event.
5581
+ *
5582
+ * IMPORTANT:
5583
+ * Custom event tracking is currently only supported for specific clients.
5584
+ * If you are not one of them, please don't use this method. If you will,
5585
+ * the API will simply ignore your request based on the plugin ID.
5586
+ *
5587
+ * Need custom tracking for your plugin or theme?
5588
+ * If you are interested in custom event tracking please contact yo@freemius.com
5589
+ * for further details.
5590
+ *
5591
+ * @author Vova Feldman (@svovaf)
5592
+ * @since 1.2.1
5593
+ *
5594
+ * @param string $name Event name.
5595
+ * @param array $properties Associative key/value array with primitive values only
5596
+ * @param bool $process_at A valid future date-time in the following format Y-m-d H:i:s.
5597
+ * @param bool $once If true, event will be tracked only once. IMPORTANT: Still trigger the API call.
5598
+ *
5599
+ * @return object|false Event data or FALSE on failure.
5600
+ *
5601
+ * @throws \Freemius_InvalidArgumentException
5602
+ */
5603
+ public function track_event( $name, $properties = array(), $process_at = false, $once = false ) {
5604
+ $this->_logger->entrance( http_build_query( array( 'name' => $name, 'once' => $once ) ) );
5605
+
5606
+ if ( ! $this->is_registered() ) {
5607
+ return false;
5608
+ }
5609
+
5610
+ $event = array( 'type' => $name );
5611
+
5612
+ if ( is_numeric( $process_at ) && $process_at > time() ) {
5613
+ $event['process_at'] = $process_at;
5614
+ }
5615
+
5616
+ if ( $once ) {
5617
+ $event['once'] = true;
5618
+ }
5619
+
5620
+ if ( ! empty( $properties ) ) {
5621
+ // Verify associative array values are primitive.
5622
+ foreach ( $properties as $k => $v ) {
5623
+ if ( ! is_scalar( $v ) ) {
5624
+ throw new Freemius_InvalidArgumentException( 'The $properties argument must be an associative key/value array with primitive values only.' );
5625
+ }
5626
+ }
5627
+
5628
+ $event['properties'] = $properties;
5629
+ }
5630
+
5631
+ $result = $this->get_api_site_scope()->call( 'events.json', 'post', $event );
5632
+
5633
+ return $this->is_api_error( $result ) ?
5634
+ false :
5635
+ $result;
5636
+ }
5637
+
5638
+ /**
5639
+ * Track install's custom event only once, but it still triggers the API call.
5640
+ *
5641
+ * IMPORTANT:
5642
+ * Custom event tracking is currently only supported for specific clients.
5643
+ * If you are not one of them, please don't use this method. If you will,
5644
+ * the API will simply ignore your request based on the plugin ID.
5645
+ *
5646
+ * Need custom tracking for your plugin or theme?
5647
+ * If you are interested in custom event tracking please contact yo@freemius.com
5648
+ * for further details.
5649
+ *
5650
+ * @author Vova Feldman (@svovaf)
5651
+ * @since 1.2.1
5652
+ *
5653
+ * @param string $name Event name.
5654
+ * @param array $properties Associative key/value array with primitive values only
5655
+ * @param bool $process_at A valid future date-time in the following format Y-m-d H:i:s.
5656
+ *
5657
+ * @return object|false Event data or FALSE on failure.
5658
+ *
5659
+ * @throws \Freemius_InvalidArgumentException
5660
+ *
5661
+ * @user Freemius::track_event()
5662
+ */
5663
+ public function track_event_once( $name, $properties = array(), $process_at = false ) {
5664
+ return $this->track_event( $name, $properties, $process_at, true );
5665
+ }
5666
+
5667
+ /**
5668
+ * Plugin uninstall hook.
5669
+ *
5670
+ * @author Vova Feldman (@svovaf)
5671
+ * @since 1.0.1
5672
+ *
5673
+ * @param bool $check_user Enforce checking if user have plugins activation privileges.
5674
+ */
5675
+ function _uninstall_plugin_event( $check_user = true ) {
5676
+ $this->_logger->entrance( 'slug = ' . $this->_slug );
5677
+
5678
+ if ( $check_user && ! current_user_can( 'activate_plugins' ) ) {
5679
+ return;
5680
+ }
5681
+
5682
+ $params = array();
5683
+ $uninstall_reason = null;
5684
+ if ( isset( $this->_storage->uninstall_reason ) ) {
5685
+ $uninstall_reason = $this->_storage->uninstall_reason;
5686
+ $params['reason_id'] = $uninstall_reason->id;
5687
+ $params['reason_info'] = $uninstall_reason->info;
5688
+ }
5689
+
5690
+ if ( ! $this->is_registered() ) {
5691
+ // Send anonymous uninstall event only if user submitted a feedback.
5692
+ if ( isset( $uninstall_reason ) ) {
5693
+ if ( isset( $uninstall_reason->is_anonymous ) && ! $uninstall_reason->is_anonymous ) {
5694
+ $this->opt_in( false, false, false, false, true );
5695
+ } else {
5696
+ $params['uid'] = $this->get_anonymous_id();
5697
+ $this->get_api_plugin_scope()->call( 'uninstall.json', 'put', $params );
5698
+ }
5699
+ }
5700
+ } else {
5701
+ // Send uninstall event.
5702
+ $this->send_install_update( array_merge( $params, array(
5703
+ 'is_active' => false,
5704
+ 'is_uninstalled' => true,
5705
+ ) ) );
5706
+ }
5707
+
5708
+ // @todo Decide if we want to delete plugin information from db.
5709
+ }
5710
+
5711
+ /**
5712
+ * @author Vova Feldman (@svovaf)
5713
+ * @since 1.1.1
5714
+ *
5715
+ * @return string
5716
+ */
5717
+ function premium_plugin_basename() {
5718
+ return "{$this->_slug}-premium/" . basename( $this->_free_plugin_basename );
5719
+ }
5720
+
5721
+ /**
5722
+ * Uninstall plugin hook. Called only when connected his account with Freemius for active sites tracking.
5723
+ *
5724
+ * @author Vova Feldman (@svovaf)
5725
+ * @since 1.0.2
5726
+ */
5727
+ public static function _uninstall_plugin_hook() {
5728
+ self::_load_required_static();
5729
+
5730
+ self::$_static_logger->entrance();
5731
+
5732
+ if ( ! current_user_can( 'activate_plugins' ) ) {
5733
+ return;
5734
+ }
5735
+
5736
+ $plugin_file = substr( current_filter(), strlen( 'uninstall_' ) );
5737
+
5738
+ self::$_static_logger->info( 'plugin = ' . $plugin_file );
5739
+
5740
+ define( 'WP_FS__UNINSTALL_MODE', true );
5741
+
5742
+ $fs = self::get_instance_by_file( $plugin_file );
5743
+
5744
+ if ( is_object( $fs ) ) {
5745
+ self::require_plugin_essentials();
5746
+
5747
+ if ( is_plugin_active( $fs->_free_plugin_basename ) ||
5748
+ is_plugin_active( $fs->premium_plugin_basename() )
5749
+ ) {
5750
+ // Deleting Free or Premium plugin version while the other version still installed.
5751
+ return;
5752
+ }
5753
+
5754
+ $fs->_uninstall_plugin_event();
5755
+
5756
+ $fs->do_action( 'after_uninstall' );
5757
+ }
5758
+ }
5759
+
5760
+ #----------------------------------------------------------------------------------
5761
+ #region Plugin Information
5762
+ #----------------------------------------------------------------------------------
5763
+
5764
+ /**
5765
+ * Load WordPress core plugin.php essential module.
5766
+ *
5767
+ * @author Vova Feldman (@svovaf)
5768
+ * @since 1.1.1
5769
+ */
5770
+ private static function require_plugin_essentials() {
5771
+ if ( ! function_exists( 'get_plugins' ) ) {
5772
+ self::$_static_logger->log( 'Including wp-admin/includes/plugin.php...' );
5773
+
5774
+ require_once ABSPATH . 'wp-admin/includes/plugin.php';
5775
+ }
5776
+ }
5777
+
5778
+ /**
5779
+ * Load WordPress core pluggable.php module.
5780
+ *
5781
+ * @author Vova Feldman (@svovaf)
5782
+ * @since 1.1.2
5783
+ */
5784
+ private static function require_pluggable_essentials() {
5785
+ if ( ! function_exists( 'wp_get_current_user' ) ) {
5786
+ require_once ABSPATH . 'wp-includes/pluggable.php';
5787
+ }
5788
+ }
5789
+
5790
+ /**
5791
+ * Return plugin data.
5792
+ *
5793
+ * @author Vova Feldman (@svovaf)
5794
+ * @since 1.0.1
5795
+ *
5796
+ * @return array
5797
+ */
5798
+ function get_plugin_data() {
5799
+ if ( ! isset( $this->_plugin_data ) ) {
5800
+ self::require_plugin_essentials();
5801
+
5802
+ if ( $this->is_plugin() ) {
5803
+ /**
5804
+ * @author Vova Feldman (@svovaf)
5805
+ * @since 1.2.0 When using get_plugin_data() do NOT translate plugin data.
5806
+ *
5807
+ * @link https://github.com/Freemius/wordpress-sdk/issues/77
5808
+ */
5809
+ $plugin_data = get_plugin_data(
5810
+ $this->_plugin_main_file_path,
5811
+ false,
5812
+ false
5813
+ );
5814
+ } else {
5815
+ $theme_data = wp_get_theme();
5816
+
5817
+ $plugin_data = array(
5818
+ 'Name' => $theme_data->get( 'Name' ),
5819
+ 'Version' => $theme_data->get( 'Version' ),
5820
+ 'Author' => $theme_data->get( 'Author' ),
5821
+ 'Description' => $theme_data->get( 'Description' ),
5822
+ 'PluginURI' => $theme_data->get( 'ThemeURI' ),
5823
+ );
5824
+ }
5825
+
5826
+ $this->_plugin_data = $plugin_data;
5827
+ }
5828
+
5829
+ return $this->_plugin_data;
5830
+ }
5831
+
5832
+ /**
5833
+ * @author Vova Feldman (@svovaf)
5834
+ * @since 1.0.1
5835
+ * @since 1.2.2.5 If slug not set load slug by module ID.
5836
+ *
5837
+ * @return string Plugin slug.
5838
+ */
5839
+ function get_slug() {
5840
+ if ( ! isset( $this->_slug ) ) {
5841
+ $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
5842
+ $this->_slug = $id_slug_type_path_map[ $this->_module_id ]['slug'];
5843
+ }
5844
+
5845
+ return $this->_slug;
5846
+ }
5847
+
5848
+ /**
5849
+ * @author Vova Feldman (@svovaf)
5850
+ * @since 1.2.1.7
5851
+ *
5852
+ * @return string Plugin slug.
5853
+ */
5854
+ function get_target_folder_name() {
5855
+ return $this->_slug . ( $this->can_use_premium_code() ? '-premium' : '' );
5856
+ }
5857
+
5858
+ /**
5859
+ * @author Vova Feldman (@svovaf)
5860
+ * @since 1.0.1
5861
+ *
5862
+ * @return number Plugin ID.
5863
+ */
5864
+ function get_id() {
5865
+ return $this->_plugin->id;
5866
+ }
5867
+
5868
+ /**
5869
+ * @author Vova Feldman (@svovaf)
5870
+ * @since 1.2.1.5
5871
+ *
5872
+ * @return string Freemius SDK version
5873
+ */
5874
+ function get_sdk_version() {
5875
+ return $this->version;
5876
+ }
5877
+
5878
+ /**
5879
+ * @author Vova Feldman (@svovaf)
5880
+ * @since 1.2.1.5
5881
+ *
5882
+ * @return number Parent plugin ID (if parent exist).
5883
+ */
5884
+ function get_parent_id() {
5885
+ return $this->is_addon() ?
5886
+ $this->get_parent_instance()->get_id() :
5887
+ $this->_plugin->id;
5888
+ }
5889
+
5890
+ /**
5891
+ * @author Vova Feldman (@svovaf)
5892
+ * @since 1.0.1
5893
+ *
5894
+ * @return string Plugin public key.
5895
+ */
5896
+ function get_public_key() {
5897
+ return $this->_plugin->public_key;
5898
+ }
5899
+
5900
+ /**
5901
+ * Will be available only on sandbox mode.
5902
+ *
5903
+ * @author Vova Feldman (@svovaf)
5904
+ * @since 1.0.4
5905
+ *
5906
+ * @return mixed Plugin secret key.
5907
+ */
5908
+ function get_secret_key() {
5909
+ return $this->_plugin->secret_key;
5910
+ }
5911
+
5912
+ /**
5913
+ * @author Vova Feldman (@svovaf)
5914
+ * @since 1.1.1
5915
+ *
5916
+ * @return bool
5917
+ */
5918
+ function has_secret_key() {
5919
+ return ! empty( $this->_plugin->secret_key );
5920
+ }
5921
+
5922
+ /**
5923
+ * @author Vova Feldman (@svovaf)
5924
+ * @since 1.0.9
5925
+ *
5926
+ * @return string
5927
+ */
5928
+ function get_plugin_name() {
5929
+ $this->_logger->entrance();
5930
+
5931
+ if ( ! isset( $this->_plugin_name ) ) {
5932
+ $plugin_data = $this->get_plugin_data();
5933
+
5934
+ // Get name.
5935
+ $this->_plugin_name = $plugin_data['Name'];
5936
+
5937
+ // Check if plugin name contains "(Premium)" suffix and remove it.
5938
+ $suffix = ' (premium)';
5939
+ $suffix_len = strlen( $suffix );
5940
+
5941
+ if ( strlen( $plugin_data['Name'] ) > $suffix_len &&
5942
+ $suffix === substr( strtolower( $plugin_data['Name'] ), - $suffix_len )
5943
+ ) {
5944
+ $this->_plugin_name = substr( $plugin_data['Name'], 0, - $suffix_len );
5945
+ }
5946
+
5947
+ $this->_logger->departure( 'Name = ' . $this->_plugin_name );
5948
+ }
5949
+
5950
+ return $this->_plugin_name;
5951
+ }
5952
+
5953
+ /**
5954
+ * @author Vova Feldman (@svovaf)
5955
+ * @since 1.0.0
5956
+ *
5957
+ * @return string
5958
+ */
5959
+ function get_plugin_version() {
5960
+ $this->_logger->entrance();
5961
+
5962
+ $plugin_data = $this->get_plugin_data();
5963
+
5964
+ $this->_logger->departure( 'Version = ' . $plugin_data['Version'] );
5965
+
5966
+ return $this->apply_filters( 'plugin_version', $plugin_data['Version'] );
5967
+ }
5968
+
5969
+ /**
5970
+ * @author Vova Feldman (@svovaf)
5971
+ * @since 1.2.1.7
5972
+ *
5973
+ * @return string
5974
+ */
5975
+ function get_plugin_title() {
5976
+ $this->_logger->entrance();
5977
+
5978
+ $title = $this->_plugin->title;
5979
+
5980
+ return $this->apply_filters( 'plugin_title', $title );
5981
+ }
5982
+
5983
+ /**
5984
+ * @author Vova Feldman (@svovaf)
5985
+ * @since 1.2.2.7
5986
+ *
5987
+ * @param bool $lowercase
5988
+ *
5989
+ * @return string
5990
+ */
5991
+ function get_module_label( $lowercase = false ) {
5992
+ $label = $this->is_addon() ?
5993
+ $this->get_text_inline( 'Add-On', 'addon' ) :
5994
+ ( $this->is_plugin() ?
5995
+ $this->get_text_inline( 'Plugin', 'plugin' ) :
5996
+ $this->get_text_inline( 'Theme', 'theme' ) );
5997
+
5998
+ if ( $lowercase ) {
5999
+ $label = strtolower( $label );
6000
+ }
6001
+
6002
+ return $label;
6003
+ }
6004
+
6005
+ /**
6006
+ * @author Vova Feldman (@svovaf)
6007
+ * @since 1.0.4
6008
+ *
6009
+ * @return string
6010
+ */
6011
+ function get_plugin_basename() {
6012
+ if ( ! isset( $this->_plugin_basename ) ) {
6013
+ if ( $this->is_plugin() ) {
6014
+ $this->_plugin_basename = plugin_basename( $this->_plugin_main_file_path );
6015
+ } else {
6016
+ $this->_plugin_basename = basename( dirname( $this->_plugin_main_file_path ) );
6017
+ }
6018
+ }
6019
+
6020
+ return $this->_plugin_basename;
6021
+ }
6022
+
6023
+ function get_plugin_folder_name() {
6024
+ $this->_logger->entrance();
6025
+
6026
+ $plugin_folder = $this->_plugin_basename;
6027
+
6028
+ while ( '.' !== dirname( $plugin_folder ) ) {
6029
+ $plugin_folder = dirname( $plugin_folder );
6030
+ }
6031
+
6032
+ $this->_logger->departure( 'Folder Name = ' . $plugin_folder );
6033
+
6034
+ return $plugin_folder;
6035
+ }
6036
+
6037
+ #endregion ------------------------------------------------------------------
6038
+
6039
+ /* Account
6040
+ ------------------------------------------------------------------------------------------------------------------*/
6041
+
6042
+ /**
6043
+ * Find plugin's slug by plugin's basename.
6044
+ *
6045
+ * @author Vova Feldman (@svovaf)
6046
+ * @since 1.0.9
6047
+ *
6048
+ * @param string $plugin_base_name
6049
+ *
6050
+ * @return false|string
6051
+ */
6052
+ private static function find_slug_by_basename( $plugin_base_name ) {
6053
+ $file_slug_map = self::$_accounts->get_option( 'file_slug_map', array() );
6054
+
6055
+ if ( ! array( $file_slug_map ) || ! isset( $file_slug_map[ $plugin_base_name ] ) ) {
6056
+ return false;
6057
+ }
6058
+
6059
+ return $file_slug_map[ $plugin_base_name ];
6060
+ }
6061
+
6062
+ /**
6063
+ * Store the map between the plugin's basename to the slug.
6064
+ *
6065
+ * @author Vova Feldman (@svovaf)
6066
+ * @since 1.0.9
6067
+ */
6068
+ private function store_file_slug_map() {
6069
+ $file_slug_map = self::$_accounts->get_option( 'file_slug_map', array() );
6070
+
6071
+ if ( ! array( $file_slug_map ) ) {
6072
+ $file_slug_map = array();
6073
+ }
6074
+
6075
+ if ( ! isset( $file_slug_map[ $this->_plugin_basename ] ) ||
6076
+ $file_slug_map[ $this->_plugin_basename ] !== $this->_slug
6077
+ ) {
6078
+ $file_slug_map[ $this->_plugin_basename ] = $this->_slug;
6079
+ self::$_accounts->set_option( 'file_slug_map', $file_slug_map, true );
6080
+ }
6081
+ }
6082
+
6083
+ /**
6084
+ * @return FS_User[]
6085
+ */
6086
+ static function get_all_users() {
6087
+ $users = self::$_accounts->get_option( 'users', array() );
6088
+
6089
+ if ( ! is_array( $users ) ) {
6090
+ $users = array();
6091
+ }
6092
+
6093
+ return $users;
6094
+ }
6095
+
6096
+ /**
6097
+ * @param string $module_type
6098
+ *
6099
+ * @return FS_Site[]
6100
+ */
6101
+ private static function get_all_sites( $module_type = WP_FS__MODULE_TYPE_PLUGIN ) {
6102
+ $sites = self::get_account_option( 'sites', $module_type );
6103
+
6104
+ if ( ! is_array( $sites ) ) {
6105
+ $sites = array();
6106
+ }
6107
+
6108
+ return $sites;
6109
+ }
6110
+
6111
+ /**
6112
+ * @author Leo Fajardo (@leorw)
6113
+ *
6114
+ * @since 1.2.2
6115
+ *
6116
+ * @param string $option_name
6117
+ * @param string $module_type
6118
+ *
6119
+ * @return mixed
6120
+ */
6121
+ private static function get_account_option( $option_name, $module_type ) {
6122
+ if ( WP_FS__MODULE_TYPE_PLUGIN !== $module_type ) {
6123
+ $option_name = $module_type . '_' . $option_name;
6124
+ }
6125
+
6126
+ return self::$_accounts->get_option( $option_name, array() );
6127
+ }
6128
+
6129
+ /**
6130
+ * @author Leo Fajardo (@leorw)
6131
+ *
6132
+ * @since 1.2.2
6133
+ *
6134
+ * @param string $option_name
6135
+ * @param mixed $option_value
6136
+ * @param bool $store
6137
+ */
6138
+ private function set_account_option( $option_name, $option_value, $store ) {
6139
+ self::set_account_option_by_module(
6140
+ $this->_module_type,
6141
+ $option_name,
6142
+ $option_value,
6143
+ $store
6144
+ );
6145
+ }
6146
+
6147
+ /**
6148
+ * @author Vova Feldman (@svovaf)
6149
+ *
6150
+ * @since 1.2.2.7
6151
+ *
6152
+ * @param string $module_type
6153
+ * @param string $option_name
6154
+ * @param mixed $option_value
6155
+ * @param bool $store
6156
+ */
6157
+ private static function set_account_option_by_module( $module_type, $option_name, $option_value, $store ) {
6158
+ if ( WP_FS__MODULE_TYPE_PLUGIN != $module_type ) {
6159
+ $option_name = $module_type . '_' . $option_name;
6160
+ }
6161
+
6162
+ self::$_accounts->set_option( $option_name, $option_value, $store );
6163
+ }
6164
+
6165
+ /**
6166
+ * @author Vova Feldman (@svovaf)
6167
+ * @since 1.0.6
6168
+ *
6169
+ * @param string $module_type
6170
+ *
6171
+ * @return FS_Plugin_License[]
6172
+ */
6173
+ private static function get_all_licenses( $module_type = WP_FS__MODULE_TYPE_PLUGIN ) {
6174
+ $licenses = self::get_account_option( 'licenses', $module_type );
6175
+
6176
+ if ( ! is_array( $licenses ) ) {
6177
+ $licenses = array();
6178
+ }
6179
+
6180
+ return $licenses;
6181
+ }
6182
+
6183
+ /**
6184
+ * @param string|bool $module_type
6185
+ *
6186
+ * @return FS_Plugin_Plan[]
6187
+ */
6188
+ private static function get_all_plans( $module_type = false ) {
6189
+ $plans = self::get_account_option( 'plans', $module_type );
6190
+
6191
+ if ( ! is_array( $plans ) ) {
6192
+ $plans = array();
6193
+ }
6194
+
6195
+ return $plans;
6196
+ }
6197
+
6198
+ /**
6199
+ * @author Vova Feldman (@svovaf)
6200
+ * @since 1.0.4
6201
+ *
6202
+ * @return FS_Plugin_Tag[]
6203
+ */
6204
+ private static function get_all_updates() {
6205
+ $updates = self::$_accounts->get_option( 'updates', array() );
6206
+
6207
+ if ( ! is_array( $updates ) ) {
6208
+ $updates = array();
6209
+ }
6210
+
6211
+ return $updates;
6212
+ }
6213
+
6214
+ /**
6215
+ * @author Vova Feldman (@svovaf)
6216
+ * @since 1.0.6
6217
+ *
6218
+ * @return array<number,FS_Plugin[]>|false
6219
+ */
6220
+ private static function get_all_addons() {
6221
+ $addons = self::$_accounts->get_option( 'addons', array() );
6222
+
6223
+ if ( ! is_array( $addons ) ) {
6224
+ $addons = array();
6225
+ }
6226
+
6227
+ return $addons;
6228
+ }
6229
+
6230
+ /**
6231
+ * @author Vova Feldman (@svovaf)
6232
+ * @since 1.0.6
6233
+ *
6234
+ * @return FS_Plugin[]|false
6235
+ */
6236
+ private static function get_all_account_addons() {
6237
+ $addons = self::$_accounts->get_option( 'account_addons', array() );
6238
+
6239
+ if ( ! is_array( $addons ) ) {
6240
+ $addons = array();
6241
+ }
6242
+
6243
+ return $addons;
6244
+ }
6245
+
6246
+ /**
6247
+ * Check if user has connected his account (opted-in).
6248
+ *
6249
+ * Note:
6250
+ * If the user opted-in and opted-out on a later stage,
6251
+ * this will still return true. If you want to check if the
6252
+ * user is currently opted-in, use:
6253
+ * `$fs->is_registered() && $fs->is_tracking_allowed()`
6254
+ *
6255
+ * @author Vova Feldman (@svovaf)
6256
+ * @since 1.0.1
6257
+ * @return bool
6258
+ */
6259
+ function is_registered() {
6260
+ return is_object( $this->_user );
6261
+ }
6262
+
6263
+ /**
6264
+ * Returns TRUE if the user opted-in and didn't disconnect (opt-out).
6265
+ *
6266
+ * @author Leo Fajardo (@leorw)
6267
+ * @since 1.2.1.5
6268
+ *
6269
+ * @return bool
6270
+ */
6271
+ function is_tracking_allowed() {
6272
+ return ( is_object( $this->_site ) && true !== $this->_site->is_disconnected );
6273
+ }
6274
+
6275
+ /**
6276
+ * @author Vova Feldman (@svovaf)
6277
+ * @since 1.0.4
6278
+ *
6279
+ * @return FS_Plugin
6280
+ */
6281
+ function get_plugin() {
6282
+ return $this->_plugin;
6283
+ }
6284
+
6285
+ /**
6286
+ * @author Vova Feldman (@svovaf)
6287
+ * @since 1.0.3
6288
+ *
6289
+ * @return FS_User
6290
+ */
6291
+ function get_user() {
6292
+ return $this->_user;
6293
+ }
6294
+
6295
+ /**
6296
+ * @author Vova Feldman (@svovaf)
6297
+ * @since 1.0.3
6298
+ *
6299
+ * @return FS_Site
6300
+ */
6301
+ function get_site() {
6302
+ return $this->_site;
6303
+ }
6304
+
6305
+ /**
6306
+ * Get plugin add-ons.
6307
+ *
6308
+ * @author Vova Feldman (@svovaf)
6309
+ * @since 1.0.6
6310
+ *
6311
+ * @since 1.1.7.3 If not yet loaded, fetch data from the API.
6312
+ *
6313
+ * @param bool $flush
6314
+ *
6315
+ * @return FS_Plugin[]|false
6316
+ */
6317
+ function get_addons( $flush = false ) {
6318
+ $this->_logger->entrance();
6319
+
6320
+ if ( ! $this->_has_addons ) {
6321
+ return false;
6322
+ }
6323
+
6324
+ $addons = $this->sync_addons( $flush );
6325
+
6326
+ return ( ! is_array( $addons ) || empty( $addons ) ) ?
6327
+ false :
6328
+ $addons;
6329
+ }
6330
+
6331
+ /**
6332
+ * @author Vova Feldman (@svovaf)
6333
+ * @since 1.0.6
6334
+ *
6335
+ * @return FS_Plugin[]|false
6336
+ */
6337
+ function get_account_addons() {
6338
+ $this->_logger->entrance();
6339
+
6340
+ $addons = self::get_all_account_addons();
6341
+
6342
+ if ( ! is_array( $addons ) ||
6343
+ ! isset( $addons[ $this->_plugin->id ] ) ||
6344
+ ! is_array( $addons[ $this->_plugin->id ] ) ||
6345
+ 0 === count( $addons[ $this->_plugin->id ] )
6346
+ ) {
6347
+ return false;
6348
+ }
6349
+
6350
+ return $addons[ $this->_plugin->id ];
6351
+ }
6352
+
6353
+ /**
6354
+ * Check if user has any
6355
+ *
6356
+ * @author Vova Feldman (@svovaf)
6357
+ * @since 1.1.6
6358
+ *
6359
+ * @return bool
6360
+ */
6361
+ function has_account_addons() {
6362
+ $addons = $this->get_account_addons();
6363
+
6364
+ return is_array( $addons ) && ( 0 < count( $addons ) );
6365
+ }
6366
+
6367
+
6368
+ /**
6369
+ * Get add-on by ID (from local data).
6370
+ *
6371
+ * @author Vova Feldman (@svovaf)
6372
+ * @since 1.0.6
6373
+ *
6374
+ * @param number $id
6375
+ *
6376
+ * @return FS_Plugin|false
6377
+ */
6378
+ function get_addon( $id ) {
6379
+ $this->_logger->entrance();
6380
+
6381
+ $addons = $this->get_addons();
6382
+
6383
+ if ( is_array( $addons ) ) {
6384
+ foreach ( $addons as $addon ) {
6385
+ if ( $id == $addon->id ) {
6386
+ return $addon;
6387
+ }
6388
+ }
6389
+ }
6390
+
6391
+ return false;
6392
+ }
6393
+
6394
+ /**
6395
+ * Get add-on by slug (from local data).
6396
+ *
6397
+ * @author Vova Feldman (@svovaf)
6398
+ * @since 1.0.6
6399
+ *
6400
+ * @param string $slug
6401
+ *
6402
+ * @param bool $flush
6403
+ *
6404
+ * @return FS_Plugin|false
6405
+ */
6406
+ function get_addon_by_slug( $slug, $flush = false ) {
6407
+ $this->_logger->entrance();
6408
+
6409
+ $addons = $this->get_addons( $flush );
6410
+
6411
+ if ( is_array( $addons ) ) {
6412
+ foreach ( $addons as $addon ) {
6413
+ if ( $slug === $addon->slug ) {
6414
+ return $addon;
6415
+ }
6416
+ }
6417
+ }
6418
+
6419
+ return false;
6420
+ }
6421
+
6422
+ #----------------------------------------------------------------------------------
6423
+ #region Plans & Licensing
6424
+ #----------------------------------------------------------------------------------
6425
+
6426
+ /**
6427
+ * Check if running premium plugin code.
6428
+ *
6429
+ * @author Vova Feldman (@svovaf)
6430
+ * @since 1.0.5
6431
+ *
6432
+ * @return bool
6433
+ */
6434
+ function is_premium() {
6435
+ return $this->_plugin->is_premium;
6436
+ }
6437
+
6438
+ /**
6439
+ * Get site's plan ID.
6440
+ *
6441
+ * @author Vova Feldman (@svovaf)
6442
+ * @since 1.0.2
6443
+ *
6444
+ * @return number
6445
+ */
6446
+ function get_plan_id() {
6447
+ return $this->_site->plan->id;
6448
+ }
6449
+
6450
+ /**
6451
+ * Get site's plan title.
6452
+ *
6453
+ * @author Vova Feldman (@svovaf)
6454
+ * @since 1.0.2
6455
+ *
6456
+ * @return string
6457
+ */
6458
+ function get_plan_title() {
6459
+ return $this->_site->plan->title;
6460
+ }
6461
+
6462
+ /**
6463
+ * @author Vova Feldman (@svovaf)
6464
+ * @since 1.0.9
6465
+ *
6466
+ * @return FS_Plugin_Plan|false
6467
+ */
6468
+ function get_plan() {
6469
+ return is_object( $this->_site->plan ) ?
6470
+ $this->_site->plan :
6471
+ false;
6472
+ }
6473
+
6474
+ /**
6475
+ * @author Vova Feldman (@svovaf)
6476
+ * @since 1.0.3
6477
+ *
6478
+ * @return bool
6479
+ */
6480
+ function is_trial() {
6481
+ $this->_logger->entrance();
6482
+
6483
+ if ( ! $this->is_registered() ) {
6484
+ return false;
6485
+ }
6486
+
6487
+ return $this->_site->is_trial();
6488
+ }
6489
+
6490
+ /**
6491
+ * Check if currently in a trial with payment method (credit card or paypal).
6492
+ *
6493
+ * @author Vova Feldman (@svovaf)
6494
+ * @since 1.1.7
6495
+ *
6496
+ * @return bool
6497
+ */
6498
+ function is_paid_trial() {
6499
+ $this->_logger->entrance();
6500
+
6501
+ if ( ! $this->is_trial() ) {
6502
+ return false;
6503
+ }
6504
+
6505
+ return $this->has_active_valid_license() && ( $this->_site->trial_plan_id == $this->_license->plan_id );
6506
+ }
6507
+
6508
+ /**
6509
+ * Check if trial already utilized.
6510
+ *
6511
+ * @since 1.0.9
6512
+ *
6513
+ * @return bool
6514
+ */
6515
+ function is_trial_utilized() {
6516
+ $this->_logger->entrance();
6517
+
6518
+ if ( ! $this->is_registered() ) {
6519
+ return false;
6520
+ }
6521
+
6522
+ return $this->_site->is_trial_utilized();
6523
+ }
6524
+
6525
+ /**
6526
+ * Get trial plan information (if in trial).
6527
+ *
6528
+ * @author Vova Feldman (@svovaf)
6529
+ * @since 1.0.9
6530
+ *
6531
+ * @return bool|FS_Plugin_Plan
6532
+ */
6533
+ function get_trial_plan() {
6534
+ $this->_logger->entrance();
6535
+
6536
+ if ( ! $this->is_trial() ) {
6537
+ return false;
6538
+ }
6539
+
6540
+ return $this->_storage->trial_plan;
6541
+ }
6542
+
6543
+ /**
6544
+ * Check if the user has an activate, non-expired license on current plugin's install.
6545
+ *
6546
+ * @since 1.0.9
6547
+ *
6548
+ * @return bool
6549
+ */
6550
+ function is_paying() {
6551
+ $this->_logger->entrance();
6552
+
6553
+ if ( ! $this->is_registered() ) {
6554
+ return false;
6555
+ }
6556
+
6557
+ if ( ! $this->has_paid_plan() ) {
6558
+ return false;
6559
+ }
6560
+
6561
+ return (
6562
+ ! $this->is_trial() &&
6563
+ 'free' !== $this->_site->plan->name &&
6564
+ $this->has_active_valid_license()
6565
+ );
6566
+ }
6567
+
6568
+ /**
6569
+ * @author Vova Feldman (@svovaf)
6570
+ * @since 1.0.4
6571
+ *
6572
+ * @return bool
6573
+ */
6574
+ function is_free_plan() {
6575
+ if ( ! $this->is_registered() ) {
6576
+ return true;
6577
+ }
6578
+
6579
+ if ( ! $this->has_paid_plan() ) {
6580
+ return true;
6581
+ }
6582
+
6583
+ return (
6584
+ 'free' === $this->_site->plan->name ||
6585
+ ! $this->has_features_enabled_license()
6586
+ );
6587
+ }
6588
+
6589
+ /**
6590
+ * @author Vova Feldman (@svovaf)
6591
+ * @since 1.0.5
6592
+ *
6593
+ * @return bool
6594
+ */
6595
+ function _has_premium_license() {
6596
+ $this->_logger->entrance();
6597
+
6598
+ $premium_license = $this->_get_available_premium_license();
6599
+
6600
+ return ( false !== $premium_license );
6601
+ }
6602
+
6603
+ /**
6604
+ * Check if user has any licenses associated with the plugin (including expired or blocking).
6605
+ *
6606
+ * @author Vova Feldman (@svovaf)
6607
+ * @since 1.1.7.3
6608
+ *
6609
+ * @return bool
6610
+ */
6611
+ private function has_any_license() {
6612
+ return is_array( $this->_licenses ) && ( 0 < count( $this->_licenses ) );
6613
+ }
6614
+
6615
+ /**
6616
+ * @author Vova Feldman (@svovaf)
6617
+ * @since 1.0.5
6618
+ *
6619
+ * @return FS_Plugin_License|false
6620
+ */
6621
+ function _get_available_premium_license() {
6622
+ $this->_logger->entrance();
6623
+
6624
+ if ( ! $this->has_paid_plan() ) {
6625
+ return false;
6626
+ }
6627
+
6628
+ if ( is_array( $this->_licenses ) ) {
6629
+ foreach ( $this->_licenses as $license ) {
6630
+ if ( ! $license->is_utilized() && $license->is_features_enabled() ) {
6631
+ return $license;
6632
+ }
6633
+ }
6634
+ }
6635
+
6636
+ return false;
6637
+ }
6638
+
6639
+ /**
6640
+ * Sync local plugin plans with remote server.
6641
+ *
6642
+ * @author Vova Feldman (@svovaf)
6643
+ * @since 1.0.5
6644
+ *
6645
+ * @return FS_Plugin_Plan[]|object
6646
+ */
6647
+ function _sync_plans() {
6648
+ $plans = $this->_fetch_plugin_plans();
6649
+
6650
+ if ( $this->is_array_instanceof( $plans, 'FS_Plugin_Plan' ) ) {
6651
+ $this->_plans = $plans;
6652
+ $this->_store_plans();
6653
+ }
6654
+
6655
+ $this->do_action( 'after_plans_sync', $plans );
6656
+
6657
+ return $this->_plans;
6658
+ }
6659
+
6660
+ /**
6661
+ * @author Vova Feldman (@svovaf)
6662
+ * @since 1.0.5
6663
+ *
6664
+ * @param number $id
6665
+ *
6666
+ * @return FS_Plugin_Plan|false
6667
+ */
6668
+ function _get_plan_by_id( $id ) {
6669
+ $this->_logger->entrance();
6670
+
6671
+ if ( ! is_array( $this->_plans ) || 0 === count( $this->_plans ) ) {
6672
+ $this->_sync_plans();
6673
+ }
6674
+
6675
+ foreach ( $this->_plans as $plan ) {
6676
+ if ( $id == $plan->id ) {
6677
+ return $plan;
6678
+ }
6679
+ }
6680
+
6681
+ return false;
6682
+ }
6683
+
6684
+ /**
6685
+ * @author Vova Feldman (@svovaf)
6686
+ * @since 1.1.8.1
6687
+ *
6688
+ * @param string $name
6689
+ *
6690
+ * @return FS_Plugin_Plan|false
6691
+ */
6692
+ private function get_plan_by_name( $name ) {
6693
+ $this->_logger->entrance();
6694
+
6695
+ if ( ! is_array( $this->_plans ) || 0 === count( $this->_plans ) ) {
6696
+ $this->_sync_plans();
6697
+ }
6698
+
6699
+ foreach ( $this->_plans as $plan ) {
6700
+ if ( $name == $plan->name ) {
6701
+ return $plan;
6702
+ }
6703
+ }
6704
+
6705
+ return false;
6706
+ }
6707
+
6708
+ /**
6709
+ * Sync local plugin plans with remote server.
6710
+ *
6711
+ * @author Vova Feldman (@svovaf)
6712
+ * @since 1.0.6
6713
+ *
6714
+ * @param number|bool $site_license_id
6715
+ *
6716
+ * @return FS_Plugin_License[]|object
6717
+ */
6718
+ function _sync_licenses( $site_license_id = false ) {
6719
+ $licenses = $this->_fetch_licenses( false, $site_license_id );
6720
+
6721
+ if ( $this->is_array_instanceof( $licenses, 'FS_Plugin_License' ) ) {
6722
+ $this->_licenses = $licenses;
6723
+ $this->_store_licenses();
6724
+ }
6725
+
6726
+ // Update current license.
6727
+ if ( is_object( $this->_license ) ) {
6728
+ $this->_license = $this->_get_license_by_id( $this->_license->id );
6729
+ }
6730
+
6731
+ return $this->_licenses;
6732
+ }
6733
+
6734
+ /**
6735
+ * @author Vova Feldman (@svovaf)
6736
+ * @since 1.0.5
6737
+ *
6738
+ * @param number $id
6739
+ *
6740
+ * @return FS_Plugin_License|false
6741
+ */
6742
+ function _get_license_by_id( $id ) {
6743
+ $this->_logger->entrance();
6744
+
6745
+ if ( ! is_numeric( $id ) ) {
6746
+ return false;
6747
+ }
6748
+
6749
+ if ( ! $this->has_any_license() ) {
6750
+ $this->_sync_licenses();
6751
+ }
6752
+
6753
+ foreach ( $this->_licenses as $license ) {
6754
+ if ( $id == $license->id ) {
6755
+ return $license;
6756
+ }
6757
+ }
6758
+
6759
+ return false;
6760
+ }
6761
+
6762
+ /**
6763
+ * Sync site's license with user licenses.
6764
+ *
6765
+ * @author Vova Feldman (@svovaf)
6766
+ * @since 1.0.6
6767
+ *
6768
+ * @param FS_Plugin_License|null $new_license
6769
+ */
6770
+ function _update_site_license( $new_license ) {
6771
+ $this->_logger->entrance();
6772
+
6773
+ $this->_license = $new_license;
6774
+
6775
+ if ( ! is_object( $new_license ) ) {
6776
+ $this->_site->license_id = null;
6777
+ $this->_sync_site_subscription( null );
6778
+
6779
+ return;
6780
+ }
6781
+
6782
+ $this->_site->license_id = $this->_license->id;
6783
+
6784
+ if ( ! is_array( $this->_licenses ) ) {
6785
+ $this->_licenses = array();
6786
+ }
6787
+
6788
+ $is_license_found = false;
6789
+ for ( $i = 0, $len = count( $this->_licenses ); $i < $len; $i ++ ) {
6790
+ if ( $new_license->id == $this->_licenses[ $i ]->id ) {
6791
+ $this->_licenses[ $i ] = $new_license;
6792
+
6793
+ $is_license_found = true;
6794
+ break;
6795
+ }
6796
+ }
6797
+
6798
+ // If new license just append.
6799
+ if ( ! $is_license_found ) {
6800
+ $this->_licenses[] = $new_license;
6801
+ }
6802
+
6803
+ $this->_sync_site_subscription( $new_license );
6804
+ }
6805
+
6806
+ /**
6807
+ * Sync site's subscription.
6808
+ *
6809
+ * @author Vova Feldman (@svovaf)
6810
+ * @since 1.0.9
6811
+ *
6812
+ * @param FS_Plugin_License|null $license
6813
+ *
6814
+ * @return bool|\FS_Subscription
6815
+ */
6816
+ private function _sync_site_subscription( $license ) {
6817
+ if ( ! is_object( $license ) ) {
6818
+ unset( $this->_storage->subscription );
6819
+
6820
+ return false;
6821
+ }
6822
+
6823
+ // Load subscription details if not lifetime.
6824
+ $subscription = $license->is_lifetime() ?
6825
+ false :
6826
+ $this->_fetch_site_license_subscription();
6827
+
6828
+ if ( is_object( $subscription ) && ! isset( $subscription->error ) ) {
6829
+ $this->_storage->subscription = $subscription;
6830
+ } else {
6831
+ unset( $this->_storage->subscription );
6832
+ }
6833
+
6834
+ return $subscription;
6835
+ }
6836
+
6837
+ /**
6838
+ * @author Vova Feldman (@svovaf)
6839
+ * @since 1.0.6
6840
+ *
6841
+ * @return bool|\FS_Plugin_License
6842
+ */
6843
+ function _get_license() {
6844
+ return $this->_license;
6845
+ }
6846
+
6847
+ /**
6848
+ * @return bool|\FS_Subscription
6849
+ */
6850
+ function _get_subscription() {
6851
+ return isset( $this->_storage->subscription ) ?
6852
+ $this->_storage->subscription :
6853
+ false;
6854
+ }
6855
+
6856
+ /**
6857
+ * @author Vova Feldman (@svovaf)
6858
+ * @since 1.0.2
6859
+ *
6860
+ * @param string $plan Plan name
6861
+ * @param bool $exact If true, looks for exact plan. If false, also check "higher" plans.
6862
+ *
6863
+ * @return bool
6864
+ */
6865
+ function is_plan( $plan, $exact = false ) {
6866
+ $this->_logger->entrance();
6867
+
6868
+ if ( ! $this->is_registered() ) {
6869
+ return false;
6870
+ }
6871
+
6872
+ $plan = strtolower( $plan );
6873
+
6874
+ if ( $this->_site->plan->name === $plan ) // Exact plan.
6875
+ {
6876
+ return true;
6877
+ } else if ( $exact ) // Required exact, but plans are different.
6878
+ {
6879
+ return false;
6880
+ }
6881
+
6882
+ $current_plan_order = - 1;
6883
+ $required_plan_order = - 1;
6884
+ for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
6885
+ if ( $plan === $this->_plans[ $i ]->name ) {
6886
+ $required_plan_order = $i;
6887
+ } else if ( $this->_site->plan->name === $this->_plans[ $i ]->name ) {
6888
+ $current_plan_order = $i;
6889
+ }
6890
+ }
6891
+
6892
+ return ( $current_plan_order > $required_plan_order );
6893
+ }
6894
+
6895
+ /**
6896
+ * Check if module has only one plan.
6897
+ *
6898
+ * @author Vova Feldman (@svovaf)
6899
+ * @since 1.2.1.7
6900
+ *
6901
+ * @return bool
6902
+ */
6903
+ function is_single_plan() {
6904
+ $this->_logger->entrance();
6905
+
6906
+ if ( ! $this->is_registered() ||
6907
+ ! is_array( $this->_plans ) ||
6908
+ 0 === count( $this->_plans )
6909
+ ) {
6910
+ return true;
6911
+ }
6912
+
6913
+ return ( 1 === count( $this->_plans ) );
6914
+ }
6915
+
6916
+ /**
6917
+ * Check if plan based on trial. If not in trial mode, should return false.
6918
+ *
6919
+ * @since 1.0.9
6920
+ *
6921
+ * @param string $plan Plan name
6922
+ * @param bool $exact If true, looks for exact plan. If false, also check "higher" plans.
6923
+ *
6924
+ * @return bool
6925
+ */
6926
+ function is_trial_plan( $plan, $exact = false ) {
6927
+ $this->_logger->entrance();
6928
+
6929
+ if ( ! $this->is_registered() ) {
6930
+ return false;
6931
+ }
6932
+
6933
+ if ( ! $this->is_trial() ) {
6934
+ return false;
6935
+ }
6936
+
6937
+ if ( ! isset( $this->_storage->trial_plan ) ) {
6938
+ // Store trial plan information.
6939
+ $this->_enrich_site_trial_plan( true );
6940
+ }
6941
+
6942
+ if ( $this->_storage->trial_plan->name === $plan ) // Exact plan.
6943
+ {
6944
+ return true;
6945
+ } else if ( $exact ) // Required exact, but plans are different.
6946
+ {
6947
+ return false;
6948
+ }
6949
+
6950
+ $current_plan_order = - 1;
6951
+ $required_plan_order = - 1;
6952
+ for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
6953
+ if ( $plan === $this->_plans[ $i ]->name ) {
6954
+ $required_plan_order = $i;
6955
+ } else if ( $this->_storage->trial_plan->name === $this->_plans[ $i ]->name ) {
6956
+ $current_plan_order = $i;
6957
+ }
6958
+ }
6959
+
6960
+ return ( $current_plan_order > $required_plan_order );
6961
+ }
6962
+
6963
+ /**
6964
+ * Check if plugin has any paid plans.
6965
+ *
6966
+ * @author Vova Feldman (@svovaf)
6967
+ * @since 1.0.7
6968
+ *
6969
+ * @return bool
6970
+ */
6971
+ function has_paid_plan() {
6972
+ return $this->_has_paid_plans ||
6973
+ FS_Plan_Manager::instance()->has_paid_plan( $this->_plans );
6974
+ }
6975
+
6976
+ /**
6977
+ * Check if plugin has any plan with a trail.
6978
+ *
6979
+ * @author Vova Feldman (@svovaf)
6980
+ * @since 1.0.9
6981
+ *
6982
+ * @return bool
6983
+ */
6984
+ function has_trial_plan() {
6985
+ if ( ! $this->is_registered() ) {
6986
+ /**
6987
+ * @author Vova Feldman(@svovaf)
6988
+ * @since 1.2.1.5
6989
+ *
6990
+ * Allow setting a trial from the SDK without calling the API.
6991
+ * But, if the user did opt-in, continue using the real data from the API.
6992
+ */
6993
+ if ( $this->_trial_days >= 0 ) {
6994
+ return true;
6995
+ }
6996
+
6997
+ return false;
6998
+ }
6999
+
7000
+ return $this->_storage->get( 'has_trial_plan', false );
7001
+ }
7002
+
7003
+ /**
7004
+ * Check if plugin has any free plan, or is it premium only.
7005
+ *
7006
+ * Note: If no plans configured, assume plugin is free.
7007
+ *
7008
+ * @author Vova Feldman (@svovaf)
7009
+ * @since 1.0.7
7010
+ *
7011
+ * @return bool
7012
+ */
7013
+ function has_free_plan() {
7014
+ return ! $this->is_only_premium();
7015
+ }
7016
+
7017
+ /**
7018
+ * Displays a license activation dialog box when the user clicks on the "Activate License"
7019
+ * or "Change License" link on the plugins
7020
+ * page.
7021
+ *
7022
+ * @author Leo Fajardo (@leorw)
7023
+ * @since 1.1.9
7024
+ */
7025
+ function _add_license_activation_dialog_box() {
7026
+ $vars = array(
7027
+ 'id' => $this->_module_id,
7028
+ );
7029
+
7030
+ fs_require_template( 'forms/license-activation.php', $vars );
7031
+ fs_require_template( 'forms/resend-key.php', $vars );
7032
+ }
7033
+
7034
+ /**
7035
+ * Displays the opt-out dialog box when the user clicks on the "Opt Out" link on the "Plugins"
7036
+ * page.
7037
+ *
7038
+ * @author Leo Fajardo (@leorw)
7039
+ * @since 1.2.1.5
7040
+ */
7041
+ function _add_optout_dialog() {
7042
+ if ( $this->is_theme() ) {
7043
+ $vars = null;
7044
+ fs_require_once_template( '/js/jquery.content-change.php', $vars );
7045
+ }
7046
+
7047
+ $vars = array( 'id' => $this->_module_id );
7048
+ fs_require_template( 'forms/optout.php', $vars );
7049
+ }
7050
+
7051
+ /**
7052
+ * Prepare page to include all required UI and logic for the license activation dialog.
7053
+ *
7054
+ * @author Vova Feldman (@svovaf)
7055
+ * @since 1.2.0
7056
+ */
7057
+ function _add_license_activation() {
7058
+ if ( ! $this->is_user_admin() ) {
7059
+ // Only admins can activate a license.
7060
+ return;
7061
+ }
7062
+
7063
+ if ( ! $this->has_paid_plan() ) {
7064
+ // Module doesn't have any paid plans.
7065
+ return;
7066
+ }
7067
+
7068
+ if ( ! $this->is_premium() ) {
7069
+ // Only add license activation logic to the premium version.
7070
+ return;
7071
+ }
7072
+
7073
+ // Add license activation link and AJAX request handler.
7074
+ if ( self::is_plugins_page() ) {
7075
+ /**
7076
+ * @since 1.2.0 Add license action link only on plugins page.
7077
+ */
7078
+ $this->_add_license_action_link();
7079
+ }
7080
+
7081
+ // Add license activation AJAX callback.
7082
+ $this->add_ajax_action( 'activate_license', array( &$this, '_activate_license_ajax_action' ) );
7083
+
7084
+ // Add resend license AJAX callback.
7085
+ $this->add_ajax_action( 'resend_license_key', array( &$this, '_resend_license_key_ajax_action' ) );
7086
+ }
7087
+
7088
+ /**
7089
+ * @author Leo Fajardo (@leorw)
7090
+ * @since 1.1.9
7091
+ */
7092
+ function _activate_license_ajax_action() {
7093
+ $this->_logger->entrance();
7094
+
7095
+ $this->check_ajax_referer( 'activate_license' );
7096
+
7097
+ $license_key = trim( fs_request_get( 'license_key' ) );
7098
+
7099
+ if ( empty( $license_key ) ) {
7100
+ exit;
7101
+ }
7102
+
7103
+ $plugin_id = fs_request_get( 'module_id', '', 'post' );
7104
+ $fs = ( $plugin_id == $this->_module_id ) ?
7105
+ $this :
7106
+ $this->get_addon_instance( $plugin_id );
7107
+
7108
+ $error = false;
7109
+ $next_page = false;
7110
+
7111
+ if ( $fs->is_registered() ) {
7112
+ $api = $fs->get_api_site_scope();
7113
+ $install = $api->call( '/', 'put', array(
7114
+ 'license_key' => $fs->apply_filters( 'license_key', $license_key )
7115
+ ) );
7116
+
7117
+ if ( isset( $install->error ) ) {
7118
+ $error = $install->error->message;
7119
+ } else {
7120
+ $fs->_sync_license( true );
7121
+
7122
+ $next_page = $fs->is_addon() ?
7123
+ $fs->get_parent_instance()->get_account_url() :
7124
+ $fs->get_account_url();
7125
+
7126
+ $fs->reconnect_locally();
7127
+ }
7128
+ } else {
7129
+ $next_page = $fs->opt_in( false, false, false, $license_key );
7130
+
7131
+ if ( isset( $next_page->error ) ) {
7132
+ $error = $next_page->error;
7133
+ }
7134
+ }
7135
+
7136
+ $result = array(
7137
+ 'success' => ( false === $error )
7138
+ );
7139
+
7140
+ if ( false !== $error ) {
7141
+ $result['error'] = $error;
7142
+ } else {
7143
+ $result['next_page'] = $next_page;
7144
+ }
7145
+
7146
+ echo json_encode( $result );
7147
+
7148
+ exit;
7149
+ }
7150
+
7151
+ /**
7152
+ * Billing update AJAX callback.
7153
+ *
7154
+ * @author Vova Feldman (@svovaf)
7155
+ * @since 1.2.1.5
7156
+ */
7157
+ function _update_billing_ajax_action() {
7158
+ $this->_logger->entrance();
7159
+
7160
+ $this->check_ajax_referer( 'update_billing' );
7161
+
7162
+ if ( ! $this->is_user_admin() ) {
7163
+ // Only for admins.
7164
+ self::shoot_ajax_failure();
7165
+ }
7166
+
7167
+ $billing = fs_request_get( 'billing' );
7168
+
7169
+ $api = $this->get_api_user_scope();
7170
+ $result = $api->call( '/billing.json', 'put', array_merge( $billing, array(
7171
+ 'plugin_id' => $this->get_parent_id(),
7172
+ ) ) );
7173
+
7174
+ if ( ! $this->is_api_result_entity( $result ) ) {
7175
+ self::shoot_ajax_failure();
7176
+ }
7177
+
7178
+ // Purge cached billing.
7179
+ $this->get_api_user_scope()->purge_cache( 'billing.json' );
7180
+
7181
+ self::shoot_ajax_success();
7182
+ }
7183
+
7184
+ /**
7185
+ * Trial start for anonymous users (AJAX callback).
7186
+ *
7187
+ * @author Vova Feldman (@svovaf)
7188
+ * @since 1.2.1.5
7189
+ */
7190
+ function _start_trial_ajax_action() {
7191
+ $this->_logger->entrance();
7192
+
7193
+ $this->check_ajax_referer( 'start_trial' );
7194
+
7195
+ if ( ! $this->is_user_admin() ) {
7196
+ // Only for admins.
7197
+ self::shoot_ajax_failure();
7198
+ }
7199
+
7200
+ $trial_data = fs_request_get( 'trial' );
7201
+
7202
+ $next_page = $this->opt_in(
7203
+ false,
7204
+ false,
7205
+ false,
7206
+ false,
7207
+ false,
7208
+ $trial_data['plan_id']
7209
+ );
7210
+
7211
+ if ( is_object( $next_page ) && $this->is_api_error( $next_page ) ) {
7212
+ self::shoot_ajax_failure(
7213
+ isset( $next_page->error ) ?
7214
+ $next_page->error->message :
7215
+ var_export( $next_page, true )
7216
+ );
7217
+ }
7218
+
7219
+ $this->shoot_ajax_success( array(
7220
+ 'next_page' => $next_page,
7221
+ ) );
7222
+ }
7223
+
7224
+ /**
7225
+ * @author Leo Fajardo (@leorw)
7226
+ * @since 1.2.0
7227
+ */
7228
+ function _resend_license_key_ajax_action() {
7229
+ $this->_logger->entrance();
7230
+
7231
+ $this->check_ajax_referer( 'resend_license_key' );
7232
+
7233
+ $email_address = sanitize_email( trim( fs_request_get( 'email', '', 'post' ) ) );
7234
+
7235
+ if ( empty( $email_address ) ) {
7236
+ exit;
7237
+ }
7238
+
7239
+ $error = false;
7240
+
7241
+ $api = $this->get_api_plugin_scope();
7242
+ $result = $api->call( '/licenses/resend.json', 'post',
7243
+ array(
7244
+ 'email' => $email_address,
7245
+ 'url' => home_url(),
7246
+ )
7247
+ );
7248
+
7249
+ if ( is_object( $result ) && isset( $result->error ) ) {
7250
+ $error = $result->error;
7251
+
7252
+ if ( in_array( $error->code, array( 'invalid_email', 'no_user' ) ) ) {
7253
+ $error = $this->get_text_inline( "We couldn't find your email address in the system, are you sure it's the right address?", 'email-not-found' );
7254
+ } else if ( 'no_license' === $error->code ) {
7255
+ $error = $this->get_text_inline( "We can't see any active licenses associated with that email address, are you sure it's the right address?", 'no-active-licenses' );
7256
+ } else {
7257
+ $error = $error->message;
7258
+ }
7259
+ }
7260
+
7261
+ $licenses = array(
7262
+ 'success' => ( false === $error )
7263
+ );
7264
+
7265
+ if ( false !== $error ) {
7266
+ $licenses['error'] = sprintf( '%s... %s', $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ), strtolower( $error ) );
7267
+ }
7268
+
7269
+ echo json_encode( $licenses );
7270
+
7271
+ exit;
7272
+ }
7273
+
7274
+ /**
7275
+ * @author Vova Feldman (@svovaf)
7276
+ * @since 1.2.1.8
7277
+ *
7278
+ * @var string
7279
+ */
7280
+ private static $_pagenow;
7281
+
7282
+ /**
7283
+ * Get current page or the referer if executing a WP AJAX request.
7284
+ *
7285
+ * @author Vova Feldman (@svovaf)
7286
+ * @since 1.2.1.8
7287
+ *
7288
+ * @return string
7289
+ */
7290
+ static function get_current_page() {
7291
+ if ( ! isset( self::$_pagenow ) ) {
7292
+ global $pagenow;
7293
+
7294
+ self::$_pagenow = $pagenow;
7295
+
7296
+ if ( self::is_ajax() &&
7297
+ 'admin-ajax.php' === $pagenow
7298
+ ) {
7299
+ $referer = fs_get_raw_referer();
7300
+
7301
+ if ( is_string( $referer ) ) {
7302
+ $parts = explode( '?', $referer );
7303
+
7304
+ self::$_pagenow = basename( $parts[0] );
7305
+ }
7306
+ }
7307
+ }
7308
+
7309
+ return self::$_pagenow;
7310
+ }
7311
+
7312
+ /**
7313
+ * Helper method to check if user in the plugins page.
7314
+ *
7315
+ * @author Vova Feldman (@svovaf)
7316
+ * @since 1.2.1.5
7317
+ *
7318
+ * @return bool
7319
+ */
7320
+ static function is_plugins_page() {
7321
+ return ( 'plugins.php' === self::get_current_page() );
7322
+ }
7323
+
7324
+ /**
7325
+ * Helper method to check if user in the themes page.
7326
+ *
7327
+ * @author Vova Feldman (@svovaf)
7328
+ * @since 1.2.2.6
7329
+ *
7330
+ * @return bool
7331
+ */
7332
+ static function is_themes_page() {
7333
+ return ( 'themes.php' === self::get_current_page() );
7334
+ }
7335
+
7336
+ #----------------------------------------------------------------------------------
7337
+ #region Affiliation
7338
+ #----------------------------------------------------------------------------------
7339
+
7340
+ /**
7341
+ * @author Leo Fajardo
7342
+ * @since 1.2.3
7343
+ *
7344
+ * @return bool
7345
+ */
7346
+ function has_affiliate_program() {
7347
+ if ( ! is_object( $this->_plugin ) ) {
7348
+ return false;
7349
+ }
7350
+
7351
+ return $this->_plugin->has_affiliate_program();
7352
+ }
7353
+
7354
+ /**
7355
+ * @author Leo Fajardo (@leorw)
7356
+ * @since 1.2.4
7357
+ */
7358
+ private function fetch_affiliate_terms() {
7359
+ if ( ! is_object( $this->plugin_affiliate_terms ) ) {
7360
+ $plugins_api = $this->get_api_plugin_scope();
7361
+ $affiliate_terms = $plugins_api->get( '/aff.json?type=affiliation', false, WP_FS__TIME_WEEK_IN_SEC );
7362
+
7363
+ if ( ! $this->is_api_result_entity( $affiliate_terms ) ) {
7364
+ return;
7365
+ }
7366
+
7367
+ $this->plugin_affiliate_terms = new FS_AffiliateTerms( $affiliate_terms );
7368
+ }
7369
+ }
7370
+
7371
+ /**
7372
+ * @author Leo Fajardo (@leorw)
7373
+ * @since 1.2.4
7374
+ */
7375
+ private function fetch_affiliate_and_custom_terms() {
7376
+ if ( ! empty( $this->_storage->affiliate_application_data ) ) {
7377
+ $application_data = $this->_storage->affiliate_application_data;
7378
+ $flush = ( ! isset( $application_data['status'] ) || 'pending' === $application_data['status'] );
7379
+
7380
+ $users_api = $this->get_api_user_scope();
7381
+ $result = $users_api->get( "/plugins/{$this->_plugin->id}/aff/{$this->plugin_affiliate_terms->id}/affiliates.json", $flush, WP_FS__TIME_WEEK_IN_SEC );
7382
+ if ( $this->is_api_result_object( $result, 'affiliates' ) ) {
7383
+ if ( ! empty( $result->affiliates ) ) {
7384
+ $affiliate = new FS_Affiliate( $result->affiliates[0] );
7385
+
7386
+ if ( ! isset( $application_data['status'] ) || $application_data['status'] !== $affiliate->status ) {
7387
+ $application_data['status'] = $affiliate->status;
7388
+ $this->_storage->affiliate_application_data = $application_data;
7389
+ }
7390
+
7391
+ if ( $affiliate->is_using_custom_terms ) {
7392
+ $affiliate_terms = $users_api->get( "/plugins/{$this->_plugin->id}/affiliates/{$affiliate->id}/aff/{$affiliate->custom_affiliate_terms_id}.json", $flush, WP_FS__TIME_WEEK_IN_SEC );
7393
+ if ( $this->is_api_result_entity( $affiliate_terms ) ) {
7394
+ $this->custom_affiliate_terms = new FS_AffiliateTerms( $affiliate_terms );
7395
+ }
7396
+ }
7397
+
7398
+ $this->affiliate = $affiliate;
7399
+ }
7400
+ }
7401
+ }
7402
+ }
7403
+
7404
+ /**
7405
+ * @author Leo Fajardo (@leorw)
7406
+ * @since 1.2.3
7407
+ */
7408
+ private function fetch_affiliate_and_terms() {
7409
+ $this->_logger->entrance();
7410
+
7411
+ $this->fetch_affiliate_terms();
7412
+ $this->fetch_affiliate_and_custom_terms();
7413
+ }
7414
+
7415
+ /**
7416
+ * @author Leo Fajardo
7417
+ * @since 1.2.3
7418
+ *
7419
+ * @return FS_Affiliate
7420
+ */
7421
+ function get_affiliate() {
7422
+ return $this->affiliate;
7423
+ }
7424
+
7425
+
7426
+ /**
7427
+ * @author Leo Fajardo
7428
+ * @since 1.2.3
7429
+ *
7430
+ * @return FS_AffiliateTerms
7431
+ */
7432
+ function get_affiliate_terms() {
7433
+ return is_object( $this->custom_affiliate_terms ) ?
7434
+ $this->custom_affiliate_terms :
7435
+ $this->plugin_affiliate_terms;
7436
+ }
7437
+
7438
+ /**
7439
+ * @author Leo Fajardo
7440
+ * @since 1.2.3
7441
+ *
7442
+ * @return FS_Affiliate|null
7443
+ */
7444
+ function _submit_affiliate_application() {
7445
+ $this->_logger->entrance();
7446
+
7447
+ $this->check_ajax_referer( 'submit_affiliate_application' );
7448
+
7449
+ if ( ! $this->is_user_admin() ) {
7450
+ // Only for admins.
7451
+ self::shoot_ajax_failure();
7452
+ }
7453
+
7454
+ $affiliate = fs_request_get( 'affiliate' );
7455
+
7456
+ if ( empty( $affiliate['promotion_methods'] ) ) {
7457
+ unset( $affiliate['promotion_methods'] );
7458
+ }
7459
+
7460
+ if ( ! empty( $affiliate['additional_domains'] ) ) {
7461
+ $affiliate['additional_domains'] = array_unique( $affiliate['additional_domains'] );
7462
+ }
7463
+
7464
+ if ( ! $this->is_registered() ) {
7465
+ // Opt in but don't track usage.
7466
+ $next_page = $this->opt_in(
7467
+ false,
7468
+ false,
7469
+ false,
7470
+ false,
7471
+ false,
7472
+ false,
7473
+ true
7474
+ );
7475
+
7476
+ if ( is_object( $next_page ) && $this->is_api_error( $next_page ) ) {
7477
+ self::shoot_ajax_failure(
7478
+ isset( $next_page->error ) ?
7479
+ $next_page->error->message :
7480
+ var_export( $next_page, true )
7481
+ );
7482
+ } else if ( $this->is_pending_activation() ) {
7483
+ self::shoot_ajax_failure( $this->get_text_inline( 'Account is pending activation.', 'account-is-pending-activation' ) );
7484
+ }
7485
+ }
7486
+
7487
+ $this->fetch_affiliate_terms();
7488
+
7489
+ $api = $this->get_api_user_scope();
7490
+ $result = $api->call(
7491
+ ( "/plugins/{$this->_plugin->id}/aff/{$this->plugin_affiliate_terms->id}/affiliates.json" ),
7492
+ 'post',
7493
+ $affiliate
7494
+ );
7495
+
7496
+ if ( $this->is_api_error( $result ) ) {
7497
+ self::shoot_ajax_failure(
7498
+ isset( $result->error ) ?
7499
+ $result->error->message :
7500
+ var_export( $result, true )
7501
+ );
7502
+ }
7503
+ else
7504
+ {
7505
+ if ( $this->_admin_notices->has_sticky( 'affiliate_program' ) ) {
7506
+ $this->_admin_notices->remove_sticky( 'affiliate_program' );
7507
+ }
7508
+
7509
+ $affiliate_application_data = array(
7510
+ 'status' => 'pending',
7511
+ 'stats_description' => $affiliate['stats_description'],
7512
+ 'promotion_method_description' => $affiliate['promotion_method_description'],
7513
+ );
7514
+
7515
+ if ( ! empty( $affiliate['promotion_methods'] ) ) {
7516
+ $affiliate_application_data['promotion_methods'] = $affiliate['promotion_methods'];
7517
+ }
7518
+
7519
+ if ( ! empty( $affiliate['domain'] ) ) {
7520
+ $affiliate_application_data['domain'] = $affiliate['domain'];
7521
+ }
7522
+
7523
+ if ( ! empty( $affiliate['additional_domains'] ) ) {
7524
+ $affiliate_application_data['additional_domains'] = $affiliate['additional_domains'];
7525
+ }
7526
+
7527
+ $this->_storage->affiliate_application_data = $affiliate_application_data;
7528
+ }
7529
+
7530
+ // Purge cached affiliate.
7531
+ $api->purge_cache( 'affiliate.json' );
7532
+
7533
+ self::shoot_ajax_success( $result );
7534
+ }
7535
+
7536
+ /**
7537
+ * @author Leo Fajardo
7538
+ * @since 1.2.3
7539
+ *
7540
+ * @return array|null
7541
+ */
7542
+ function get_affiliate_application_data() {
7543
+ if ( empty( $this->_storage->affiliate_application_data ) ) {
7544
+ return null;
7545
+ }
7546
+
7547
+ return $this->_storage->affiliate_application_data;
7548
+ }
7549
+
7550
+ #endregion Affiliation ------------------------------------------------------------
7551
+
7552
+ #----------------------------------------------------------------------------------
7553
+ #region URL Generators
7554
+ #----------------------------------------------------------------------------------
7555
+
7556
+ /**
7557
+ * Alias to pricing_url().
7558
+ *
7559
+ * @author Vova Feldman (@svovaf)
7560
+ * @since 1.0.2
7561
+ *
7562
+ * @uses pricing_url()
7563
+ *
7564
+ * @param string $period Billing cycle
7565
+ * @param bool $is_trial
7566
+ *
7567
+ * @return string
7568
+ */
7569
+ function get_upgrade_url( $period = WP_FS__PERIOD_ANNUALLY, $is_trial = false ) {
7570
+ return $this->pricing_url( $period, $is_trial );
7571
+ }
7572
+
7573
+ /**
7574
+ * @author Vova Feldman (@svovaf)
7575
+ * @since 1.0.9
7576
+ *
7577
+ * @uses get_upgrade_url()
7578
+ *
7579
+ * @return string
7580
+ */
7581
+ function get_trial_url() {
7582
+ return $this->get_upgrade_url( WP_FS__PERIOD_ANNUALLY, true );
7583
+ }
7584
+
7585
+ /**
7586
+ * Plugin's pricing URL.
7587
+ *
7588
+ * @author Vova Feldman (@svovaf)
7589
+ * @since 1.0.4
7590
+ *
7591
+ * @param string $billing_cycle Billing cycle
7592
+ *
7593
+ * @param bool $is_trial
7594
+ *
7595
+ * @return string
7596
+ */
7597
+ function pricing_url( $billing_cycle = WP_FS__PERIOD_ANNUALLY, $is_trial = false ) {
7598
+ $this->_logger->entrance();
7599
+
7600
+ $params = array(
7601
+ 'billing_cycle' => $billing_cycle
7602
+ );
7603
+
7604
+ if ( $is_trial ) {
7605
+ $params['trial'] = 'true';
7606
+ }
7607
+
7608
+ return $this->_get_admin_page_url( 'pricing', $params );
7609
+ }
7610
+
7611
+ /**
7612
+ * Checkout page URL.
7613
+ *
7614
+ * @author Vova Feldman (@svovaf)
7615
+ * @since 1.0.6
7616
+ *
7617
+ * @param string $billing_cycle Billing cycle
7618
+ * @param bool $is_trial
7619
+ * @param array $extra (optional) Extra parameters, override other query params.
7620
+ *
7621
+ * @return string
7622
+ */
7623
+ function checkout_url(
7624
+ $billing_cycle = WP_FS__PERIOD_ANNUALLY,
7625
+ $is_trial = false,
7626
+ $extra = array()
7627
+ ) {
7628
+ $this->_logger->entrance();
7629
+
7630
+ $params = array(
7631
+ 'checkout' => 'true',
7632
+ 'billing_cycle' => $billing_cycle,
7633
+ );
7634
+
7635
+ if ( $is_trial ) {
7636
+ $params['trial'] = 'true';
7637
+ }
7638
+
7639
+ /**
7640
+ * Params in extra override other params.
7641
+ */
7642
+ $params = array_merge( $params, $extra );
7643
+
7644
+ return $this->_get_admin_page_url( 'pricing', $params );
7645
+ }
7646
+
7647
+ /**
7648
+ * Add-on checkout URL.
7649
+ *
7650
+ * @author Vova Feldman (@svovaf)
7651
+ * @since 1.1.7
7652
+ *
7653
+ * @param number $addon_id
7654
+ * @param number $pricing_id
7655
+ * @param string $billing_cycle
7656
+ * @param bool $is_trial
7657
+ *
7658
+ * @return string
7659
+ */
7660
+ function addon_checkout_url(
7661
+ $addon_id,
7662
+ $pricing_id,
7663
+ $billing_cycle = WP_FS__PERIOD_ANNUALLY,
7664
+ $is_trial = false
7665
+ ) {
7666
+ return $this->checkout_url( $billing_cycle, $is_trial, array(
7667
+ 'plugin_id' => $addon_id,
7668
+ 'pricing_id' => $pricing_id,
7669
+ ) );
7670
+ }
7671
+
7672
+ #endregion
7673
+
7674
+ #endregion ------------------------------------------------------------------
7675
+
7676
+ /**
7677
+ * Check if plugin has any add-ons.
7678
+ *
7679
+ * @author Vova Feldman (@svovaf)
7680
+ * @since 1.0.5
7681
+ *
7682
+ * @since 1.1.7.3 Base logic only on the parameter provided by the developer in the init function.
7683
+ *
7684
+ * @return bool
7685
+ */
7686
+ function has_addons() {
7687
+ $this->_logger->entrance();
7688
+
7689
+ return $this->_has_addons;
7690
+ }
7691
+
7692
+ /**
7693
+ * Check if plugin can work in anonymous mode.
7694
+ *
7695
+ * @author Vova Feldman (@svovaf)
7696
+ * @since 1.0.9
7697
+ *
7698
+ * @return bool
7699
+ *
7700
+ * @deprecated Please use is_enable_anonymous() instead
7701
+ */
7702
+ function enable_anonymous() {
7703
+ return $this->_enable_anonymous;
7704
+ }
7705
+
7706
+ /**
7707
+ * Check if plugin can work in anonymous mode.
7708
+ *
7709
+ * @author Vova Feldman (@svovaf)
7710
+ * @since 1.1.9
7711
+ *
7712
+ * @return bool
7713
+ */
7714
+ function is_enable_anonymous() {
7715
+ return $this->_enable_anonymous;
7716
+ }
7717
+
7718
+ /**
7719
+ * Check if plugin is premium only (no free plans).
7720
+ *
7721
+ * @author Vova Feldman (@svovaf)
7722
+ * @since 1.1.9
7723
+ *
7724
+ * @return bool
7725
+ */
7726
+ function is_only_premium() {
7727
+ return $this->_is_premium_only;
7728
+ }
7729
+
7730
+ /**
7731
+ * Checks if the plugin's type is "plugin". The other type is "theme".
7732
+ *
7733
+ * @author Leo Fajardo (@leorw)
7734
+ * @since 1.2.2
7735
+ *
7736
+ * @return bool
7737
+ */
7738
+ function is_plugin() {
7739
+ return ( WP_FS__MODULE_TYPE_PLUGIN === $this->_module_type );
7740
+ }
7741
+
7742
+ /**
7743
+ * @author Leo Fajardo (@leorw)
7744
+ * @since 1.2.2
7745
+ *
7746
+ * @return string
7747
+ */
7748
+ function get_module_type() {
7749
+ if ( ! isset( $this->_module_type ) ) {
7750
+ $id_slug_type_path_map = self::$_accounts->get_option( 'id_slug_type_path_map', array() );
7751
+ $this->_module_type = $id_slug_type_path_map[ $this->_module_id ]['type'];
7752
+ }
7753
+
7754
+ return $this->_module_type;
7755
+ }
7756
+
7757
+ /**
7758
+ * @author Leo Fajardo (@leorw)
7759
+ * @since 1.2.2
7760
+ *
7761
+ * @return string
7762
+ */
7763
+ function get_plugin_main_file_path() {
7764
+ return $this->_plugin_main_file_path;
7765
+ }
7766
+
7767
+ /**
7768
+ * Check if module has a premium code version.
7769
+ *
7770
+ * Serviceware module might be freemium without any
7771
+ * premium code version, where the paid features
7772
+ * are all part of the service.
7773
+ *
7774
+ * @author Vova Feldman (@svovaf)
7775
+ * @since 1.2.1.6
7776
+ *
7777
+ * @return bool
7778
+ */
7779
+ function has_premium_version() {
7780
+ return $this->_has_premium_version;
7781
+ }
7782
+
7783
+ /**
7784
+ * Check if feature supported with current site's plan.
7785
+ *
7786
+ * @author Vova Feldman (@svovaf)
7787
+ * @since 1.0.1
7788
+ *
7789
+ * @todo IMPLEMENT
7790
+ *
7791
+ * @param number $feature_id
7792
+ *
7793
+ * @throws Exception
7794
+ */
7795
+ function is_feature_supported( $feature_id ) {
7796
+ throw new Exception( 'not implemented' );
7797
+ }
7798
+
7799
+ /**
7800
+ * @author Vova Feldman (@svovaf)
7801
+ * @since 1.0.1
7802
+ *
7803
+ * @return bool Is running in SSL/HTTPS
7804
+ */
7805
+ function is_ssl() {
7806
+ return WP_FS__IS_HTTPS;
7807
+ }
7808
+
7809
+ /**
7810
+ * @author Vova Feldman (@svovaf)
7811
+ * @since 1.0.9
7812
+ *
7813
+ * @return bool Is running in AJAX call.
7814
+ *
7815
+ * @link http://wordpress.stackexchange.com/questions/70676/how-to-check-if-i-am-in-admin-ajax
7816
+ */
7817
+ static function is_ajax() {
7818
+ return ( defined( 'DOING_AJAX' ) && DOING_AJAX );
7819
+ }
7820
+
7821
+ /**
7822
+ * Check if it's an AJAX call targeted for the current module.
7823
+ *
7824
+ * @author Vova Feldman (@svovaf)
7825
+ * @since 1.2.0
7826
+ *
7827
+ * @param array|string $actions Collection of AJAX actions.
7828
+ *
7829
+ * @return bool
7830
+ */
7831
+ function is_ajax_action( $actions ) {
7832
+ // Verify it's an ajax call.
7833
+ if ( ! self::is_ajax() ) {
7834
+ return false;
7835
+ }
7836
+
7837
+ // Verify the call is relevant for the plugin.
7838
+ if ( $this->_module_id != fs_request_get( 'module_id' ) ) {
7839
+ return false;
7840
+ }
7841
+
7842
+ // Verify it's one of the specified actions.
7843
+ if ( is_string( $actions ) ) {
7844
+ $actions = explode( ',', $actions );
7845
+ }
7846
+
7847
+ if ( is_array( $actions ) && 0 < count( $actions ) ) {
7848
+ $ajax_action = fs_request_get( 'action' );
7849
+
7850
+ foreach ( $actions as $action ) {
7851
+ if ( $ajax_action === $this->get_action_tag( $action ) ) {
7852
+ return true;
7853
+ }
7854
+ }
7855
+ }
7856
+
7857
+ return false;
7858
+ }
7859
+
7860
+ /**
7861
+ * Check if it's an AJAX call targeted for current request.
7862
+ *
7863
+ * @author Vova Feldman (@svovaf)
7864
+ * @since 1.2.0
7865
+ *
7866
+ * @param array|string $actions Collection of AJAX actions.
7867
+ * @param number|null $module_id
7868
+ *
7869
+ * @return bool
7870
+ */
7871
+ static function is_ajax_action_static( $actions, $module_id = null ) {
7872
+ // Verify it's an ajax call.
7873
+ if ( ! self::is_ajax() ) {
7874
+ return false;
7875
+ }
7876
+
7877
+
7878
+ if ( ! empty( $module_id ) ) {
7879
+ // Verify the call is relevant for the plugin.
7880
+ if ( $module_id != fs_request_get( 'module_id' ) ) {
7881
+ return false;
7882
+ }
7883
+ }
7884
+
7885
+ // Verify it's one of the specified actions.
7886
+ if ( is_string( $actions ) ) {
7887
+ $actions = explode( ',', $actions );
7888
+ }
7889
+
7890
+ if ( is_array( $actions ) && 0 < count( $actions ) ) {
7891
+ $ajax_action = fs_request_get( 'action' );
7892
+
7893
+ foreach ( $actions as $action ) {
7894
+ if ( $ajax_action === self::get_ajax_action_static( $action, $module_id ) ) {
7895
+ return true;
7896
+ }
7897
+ }
7898
+ }
7899
+
7900
+ return false;
7901
+ }
7902
+
7903
+ /**
7904
+ * @author Vova Feldman (@svovaf)
7905
+ * @since 1.1.7
7906
+ *
7907
+ * @return bool
7908
+ */
7909
+ static function is_cron() {
7910
+ return ( defined( 'DOING_CRON' ) && DOING_CRON );
7911
+ }
7912
+
7913
+ /**
7914
+ * Check if a real user is visiting the admin dashboard.
7915
+ *
7916
+ * @author Vova Feldman (@svovaf)
7917
+ * @since 1.1.7
7918
+ *
7919
+ * @return bool
7920
+ */
7921
+ function is_user_in_admin() {
7922
+ return is_admin() && ! self::is_ajax() && ! self::is_cron();
7923
+ }
7924
+
7925
+ /**
7926
+ * Check if a real user is in the customizer view.
7927
+ *
7928
+ * @author Vova Feldman (@svovaf)
7929
+ * @since 1.2.2.7
7930
+ *
7931
+ * @return bool
7932
+ */
7933
+ static function is_customizer() {
7934
+ return is_customize_preview();
7935
+ }
7936
+
7937
+ /**
7938
+ * Check if running in HTTPS and if site's plan matching the specified plan.
7939
+ *
7940
+ * @param string $plan
7941
+ * @param bool $exact
7942
+ *
7943
+ * @return bool
7944
+ */
7945
+ function is_ssl_and_plan( $plan, $exact = false ) {
7946
+ return ( $this->is_ssl() && $this->is_plan( $plan, $exact ) );
7947
+ }
7948
+
7949
+ /**
7950
+ * Construct plugin's settings page URL.
7951
+ *
7952
+ * @author Vova Feldman (@svovaf)
7953
+ * @since 1.0.4
7954
+ *
7955
+ * @param string $page
7956
+ * @param array $params
7957
+ *
7958
+ * @return string
7959
+ */
7960
+ function _get_admin_page_url( $page = '', $params = array() ) {
7961
+ if ( 0 < count( $params ) ) {
7962
+ foreach ( $params as $k => $v ) {
7963
+ $params[ $k ] = urlencode( $v );
7964
+ }
7965
+ }
7966
+
7967
+ $page_param = $this->_menu->get_slug( $page );
7968
+
7969
+ if ( empty( $page ) &&
7970
+ $this->is_theme() &&
7971
+ // Show the opt-in as an overlay for free wp.org themes or themes without any settings page.
7972
+ ( $this->is_free_wp_org_theme() || ! $this->has_settings_menu() ) ) {
7973
+ $params[ $this->get_unique_affix() . '_show_optin' ] = 'true';
7974
+
7975
+ return add_query_arg(
7976
+ $params,
7977
+ admin_url( 'themes.php' )
7978
+ );
7979
+ }
7980
+
7981
+ if ( ! $this->has_settings_menu() ) {
7982
+ if ( ! empty( $page ) ) {
7983
+ // Module doesn't have a setting page, but since the request is for
7984
+ // a specific Freemius page, use the admin.php path.
7985
+ return add_query_arg( array_merge( $params, array(
7986
+ 'page' => $page_param,
7987
+ ) ), admin_url( 'admin.php' ) );
7988
+ } else {
7989
+ if ( $this->is_activation_mode() ) {
7990
+ /**
7991
+ * @author Vova Feldman
7992
+ * @since 1.2.1.6
7993
+ *
7994
+ * If plugin doesn't have a settings page, create one for the opt-in screen.
7995
+ */
7996
+ return add_query_arg( array_merge( $params, array(
7997
+ 'page' => $this->_slug,
7998
+ ) ), admin_url( 'admin.php', 'admin' ) );
7999
+ } else {
8000
+ // Plugin without a settings page.
8001
+ return add_query_arg(
8002
+ $params,
8003
+ admin_url( 'plugins.php' )
8004
+ );
8005
+ }
8006
+ }
8007
+ }
8008
+
8009
+ // Module has a submenu settings page.
8010
+ if ( ! $this->_menu->is_top_level() ) {
8011
+ $parent_slug = $this->_menu->get_parent_slug();
8012
+ $menu_file = ( false !== strpos( $parent_slug, '.php' ) ) ?
8013
+ $parent_slug :
8014
+ 'admin.php';
8015
+
8016
+ return add_query_arg( array_merge( $params, array(
8017
+ 'page' => $page_param,
8018
+ ) ), admin_url( $menu_file, 'admin' ) );
8019
+ }
8020
+
8021
+ // Module has a top level CPT settings page.
8022
+ if ( $this->_menu->is_cpt() ) {
8023
+ if ( empty( $page ) && $this->is_activation_mode() ) {
8024
+ return add_query_arg( array_merge( $params, array(
8025
+ 'page' => $page_param
8026
+ ) ), admin_url( 'admin.php', 'admin' ) );
8027
+ } else {
8028
+ if ( ! empty( $page ) ) {
8029
+ $params['page'] = $page_param;
8030
+ }
8031
+
8032
+ return add_query_arg(
8033
+ $params,
8034
+ admin_url( $this->_menu->get_raw_slug(), 'admin' )
8035
+ );
8036
+ }
8037
+ }
8038
+
8039
+ // Module has a custom top level settings page.
8040
+ return add_query_arg( array_merge( $params, array(
8041
+ 'page' => $page_param,
8042
+ ) ), admin_url( 'admin.php', 'admin' ) );
8043
+ }
8044
+
8045
+ /**
8046
+ * Check if currently in a specified admin page.
8047
+ *
8048
+ * @author Vova Feldman (@svovaf)
8049
+ * @since 1.2.2.7
8050
+ *
8051
+ * @param string $page
8052
+ *
8053
+ * @return bool
8054
+ */
8055
+ function is_admin_page( $page ) {
8056
+ return ( $this->_menu->get_slug( $page ) === fs_request_get( 'page', '', 'get' ) );
8057
+ }
8058
+
8059
+ /**
8060
+ * Get module's main admin setting page URL.
8061
+ *
8062
+ * @author Vova Feldman (@svovaf)
8063
+ * @since 1.2.2.7
8064
+ *
8065
+ * @return string
8066
+ */
8067
+ function main_menu_url() {
8068
+ return $this->_menu->main_menu_url();
8069
+ }
8070
+
8071
+ /**
8072
+ * Check if currently on the theme's setting page or
8073
+ * on any of the Freemius added pages (via tabs).
8074
+ *
8075
+ * @author Vova Feldman (@svovaf)
8076
+ * @since 1.2.2.7
8077
+ *
8078
+ * @return bool
8079
+ */
8080
+ function is_theme_settings_page() {
8081
+ return fs_starts_with(
8082
+ fs_request_get( 'page', '', 'get' ),
8083
+ $this->_menu->get_slug()
8084
+ );
8085
+ }
8086
+
8087
+ /**
8088
+ * Plugin's account page + sync license URL.
8089
+ *
8090
+ * @author Vova Feldman (@svovaf)
8091
+ * @since 1.1.9.1
8092
+ *
8093
+ * @param bool|number $plugin_id
8094
+ * @param bool $add_action_nonce
8095
+ * @param array $params
8096
+ *
8097
+ * @return string
8098
+ */
8099
+ function _get_sync_license_url( $plugin_id = false, $add_action_nonce = true, $params = array() ) {
8100
+ if ( is_numeric( $plugin_id ) ) {
8101
+ $params['plugin_id'] = $plugin_id;
8102
+ }
8103
+
8104
+ return $this->get_account_url(
8105
+ $this->get_unique_affix() . '_sync_license',
8106
+ $params,
8107
+ $add_action_nonce
8108
+ );
8109
+ }
8110
+
8111
+ /**
8112
+ * Plugin's account URL.
8113
+ *
8114
+ * @author Vova Feldman (@svovaf)
8115
+ * @since 1.0.4
8116
+ *
8117
+ * @param bool|string $action
8118
+ * @param array $params
8119
+ *
8120
+ * @param bool $add_action_nonce
8121
+ *
8122
+ * @return string
8123
+ */
8124
+ function get_account_url( $action = false, $params = array(), $add_action_nonce = true ) {
8125
+ if ( is_string( $action ) ) {
8126
+ $params['fs_action'] = $action;
8127
+ }
8128
+
8129
+ self::require_pluggable_essentials();
8130
+
8131
+ return ( $add_action_nonce && is_string( $action ) ) ?
8132
+ fs_nonce_url( $this->_get_admin_page_url( 'account', $params ), $action ) :
8133
+ $this->_get_admin_page_url( 'account', $params );
8134
+ }
8135
+
8136
+ /**
8137
+ * @author Vova Feldman (@svovaf)
8138
+ * @since 1.2.0
8139
+ *
8140
+ * @param string $tab
8141
+ * @param bool $action
8142
+ * @param array $params
8143
+ * @param bool $add_action_nonce
8144
+ *
8145
+ * @return string
8146
+ *
8147
+ * @uses get_account_url()
8148
+ */
8149
+ function get_account_tab_url( $tab, $action = false, $params = array(), $add_action_nonce = true ) {
8150
+ $params['tab'] = $tab;
8151
+
8152
+ return $this->get_account_url( $action, $params, $add_action_nonce );
8153
+ }
8154
+
8155
+ /**
8156
+ * Plugin's account URL.
8157
+ *
8158
+ * @author Vova Feldman (@svovaf)
8159
+ * @since 1.0.4
8160
+ *
8161
+ * @param bool|string $topic
8162
+ * @param bool|string $message
8163
+ *
8164
+ * @return string
8165
+ */
8166
+ function contact_url( $topic = false, $message = false ) {
8167
+ $params = array();
8168
+ if ( is_string( $topic ) ) {
8169
+ $params['topic'] = $topic;
8170
+ }
8171
+ if ( is_string( $message ) ) {
8172
+ $params['message'] = $message;
8173
+ }
8174
+
8175
+ if ( $this->is_addon() ) {
8176
+ $params['addon_id'] = $this->get_id();
8177
+
8178
+ return $this->get_parent_instance()->_get_admin_page_url( 'contact', $params );
8179
+ } else {
8180
+ return $this->_get_admin_page_url( 'contact', $params );
8181
+ }
8182
+ }
8183
+
8184
+ /**
8185
+ * Add-on direct info URL.
8186
+ *
8187
+ * @author Vova Feldman (@svovaf)
8188
+ * @since 1.1.0
8189
+ *
8190
+ * @param string $slug
8191
+ *
8192
+ * @return string
8193
+ */
8194
+ function addon_url( $slug ) {
8195
+ return $this->_get_admin_page_url( 'addons', array(
8196
+ 'slug' => $slug
8197
+ ) );
8198
+ }
8199
+
8200
+ /* Logger
8201
+ ------------------------------------------------------------------------------------------------------------------*/
8202
+ /**
8203
+ * @param string $id
8204
+ * @param bool $prefix_slug
8205
+ *
8206
+ * @return FS_Logger
8207
+ */
8208
+ function get_logger( $id = '', $prefix_slug = true ) {
8209
+ return FS_Logger::get_logger( ( $prefix_slug ? $this->_slug : '' ) . ( ( ! $prefix_slug || empty( $id ) ) ? '' : '_' ) . $id );
8210
+ }
8211
+
8212
+ /**
8213
+ * @param $id
8214
+ * @param bool $load_options
8215
+ * @param bool $prefix_slug
8216
+ *
8217
+ * @return FS_Option_Manager
8218
+ */
8219
+ function get_options_manager( $id, $load_options = false, $prefix_slug = true ) {
8220
+ return FS_Option_Manager::get_manager( ( $prefix_slug ? $this->_slug : '' ) . ( ( ! $prefix_slug || empty( $id ) ) ? '' : '_' ) . $id, $load_options );
8221
+ }
8222
+
8223
+ /* Security
8224
+ ------------------------------------------------------------------------------------------------------------------*/
8225
+ private static function _encrypt( $str ) {
8226
+ if ( is_null( $str ) ) {
8227
+ return null;
8228
+ }
8229
+
8230
+ /**
8231
+ * The encrypt/decrypt functions are used to protect
8232
+ * the user from messing up with some of the sensitive
8233
+ * data stored for the module as a JSON in the database.
8234
+ *
8235
+ * I used the same suggested hack by the theme review team.
8236
+ * For more details, look at the function `Base64UrlDecode()`
8237
+ * in `./sdk/FreemiusBase.php`.
8238
+ *
8239
+ * @todo Remove this hack once the base64 error is removed from the Theme Check.
8240
+ *
8241
+ * @author Vova Feldman (@svovaf)
8242
+ * @since 1.2.2
8243
+ */
8244
+ $fn = 'base64' . '_encode';
8245
+
8246
+ return $fn( $str );
8247
+ }
8248
+
8249
+ static function _decrypt( $str ) {
8250
+ if ( is_null( $str ) ) {
8251
+ return null;
8252
+ }
8253
+
8254
+ /**
8255
+ * The encrypt/decrypt functions are used to protect
8256
+ * the user from messing up with some of the sensitive
8257
+ * data stored for the module as a JSON in the database.
8258
+ *
8259
+ * I used the same suggested hack by the theme review team.
8260
+ * For more details, look at the function `Base64UrlDecode()`
8261
+ * in `./sdk/FreemiusBase.php`.
8262
+ *
8263
+ * @todo Remove this hack once the base64 error is removed from the Theme Check.
8264
+ *
8265
+ * @author Vova Feldman (@svovaf)
8266
+ * @since 1.2.2
8267
+ */
8268
+ $fn = 'base64' . '_decode';
8269
+
8270
+ return $fn( $str );
8271
+ }
8272
+
8273
+ /**
8274
+ * @author Vova Feldman (@svovaf)
8275
+ * @since 1.0.5
8276
+ *
8277
+ * @param FS_Entity $entity
8278
+ *
8279
+ * @return FS_Entity Return an encrypted clone entity.
8280
+ */
8281
+ private static function _encrypt_entity( FS_Entity $entity ) {
8282
+ $clone = clone $entity;
8283
+ $props = get_object_vars( $entity );
8284
+
8285
+ foreach ( $props as $key => $val ) {
8286
+ $clone->{$key} = self::_encrypt( $val );
8287
+ }
8288
+
8289
+ return $clone;
8290
+ }
8291
+
8292
+ /**
8293
+ * @author Vova Feldman (@svovaf)
8294
+ * @since 1.0.5
8295
+ *
8296
+ * @param FS_Entity $entity
8297
+ *
8298
+ * @return FS_Entity Return an decrypted clone entity.
8299
+ */
8300
+ private static function decrypt_entity( FS_Entity $entity ) {
8301
+ $clone = clone $entity;
8302
+ $props = get_object_vars( $entity );
8303
+
8304
+ foreach ( $props as $key => $val ) {
8305
+ $clone->{$key} = self::_decrypt( $val );
8306
+ }
8307
+
8308
+ return $clone;
8309
+ }
8310
+
8311
+ /**
8312
+ * Tries to activate account based on POST params.
8313
+ *
8314
+ * @author Vova Feldman (@svovaf)
8315
+ * @since 1.0.2
8316
+ */
8317
+ function _activate_account() {
8318
+ if ( $this->is_registered() ) {
8319
+ // Already activated.
8320
+ return;
8321
+ }
8322
+
8323
+ self::_clean_admin_content_section();
8324
+
8325
+ if ( fs_request_is_action( 'activate' ) && fs_request_is_post() ) {
8326
+ // check_admin_referer( 'activate_' . $this->_plugin->public_key );
8327
+
8328
+ // Verify matching plugin details.
8329
+ if ( $this->_plugin->id != fs_request_get( 'plugin_id' ) || $this->_slug != fs_request_get( 'plugin_slug' ) ) {
8330
+ return;
8331
+ }
8332
+
8333
+ $user = new FS_User();
8334
+ $user->id = fs_request_get( 'user_id' );
8335
+ $user->public_key = fs_request_get( 'user_public_key' );
8336
+ $user->secret_key = fs_request_get( 'user_secret_key' );
8337
+ $user->email = fs_request_get( 'user_email' );
8338
+ $user->first = fs_request_get( 'user_first' );
8339
+ $user->last = fs_request_get( 'user_last' );
8340
+ $user->is_verified = fs_request_get_bool( 'user_is_verified' );
8341
+
8342
+ $site = new FS_Site();
8343
+ $site->id = fs_request_get( 'install_id' );
8344
+ $site->public_key = fs_request_get( 'install_public_key' );
8345
+ $site->secret_key = fs_request_get( 'install_secret_key' );
8346
+ $site->plan->id = fs_request_get( 'plan_id' );
8347
+ $site->plan->title = fs_request_get( 'plan_title' );
8348
+ $site->plan->name = fs_request_get( 'plan_name' );
8349
+
8350
+ $plans = array();
8351
+ $plans_data = json_decode( urldecode( fs_request_get( 'plans' ) ) );
8352
+ foreach ( $plans_data as $p ) {
8353
+ $plans[] = new FS_Plugin_Plan( $p );
8354
+ }
8355
+
8356
+ $this->_set_account( $user, $site, $plans );
8357
+
8358
+ // Reload the page with the keys.
8359
+ fs_redirect( $this->_get_admin_page_url() );
8360
+ }
8361
+ }
8362
+
8363
+ /**
8364
+ * @author Vova Feldman (@svovaf)
8365
+ * @since 1.0.7
8366
+ *
8367
+ * @param string $email
8368
+ *
8369
+ * @return FS_User|bool
8370
+ */
8371
+ static function _get_user_by_email( $email ) {
8372
+ self::$_static_logger->entrance();
8373
+
8374
+ $email = trim( strtolower( $email ) );
8375
+ $users = self::get_all_users();
8376
+ if ( is_array( $users ) ) {
8377
+ foreach ( $users as $u ) {
8378
+ if ( $email === trim( strtolower( $u->email ) ) ) {
8379
+ return $u;
8380
+ }
8381
+ }
8382
+ }
8383
+
8384
+ return false;
8385
+ }
8386
+
8387
+ #----------------------------------------------------------------------------------
8388
+ #region Account (Loading, Updates & Activation)
8389
+ #----------------------------------------------------------------------------------
8390
+
8391
+ /***
8392
+ * Load account information (user + site).
8393
+ *
8394
+ * @author Vova Feldman (@svovaf)
8395
+ * @since 1.0.1
8396
+ */
8397
+ private function _load_account() {
8398
+ $this->_logger->entrance();
8399
+
8400
+ $this->do_action( 'before_account_load' );
8401
+
8402
+ $sites = self::get_all_sites( $this->_module_type );
8403
+ $users = self::get_all_users();
8404
+ $plans = self::get_all_plans( $this->_module_type );
8405
+ $licenses = self::get_all_licenses( $this->_module_type );
8406
+
8407
+ if ( $this->_logger->is_on() && is_admin() ) {
8408
+ $this->_logger->log( 'sites = ' . var_export( $sites, true ) );
8409
+ $this->_logger->log( 'users = ' . var_export( $users, true ) );
8410
+ $this->_logger->log( 'plans = ' . var_export( $plans, true ) );
8411
+ $this->_logger->log( 'licenses = ' . var_export( $licenses, true ) );
8412
+ }
8413
+
8414
+ $site = isset( $sites[ $this->_slug ] ) ? $sites[ $this->_slug ] : false;
8415
+
8416
+ if ( is_object( $site ) &&
8417
+ is_numeric( $site->id ) &&
8418
+ is_numeric( $site->user_id ) &&
8419
+ is_object( $site->plan )
8420
+ ) {
8421
+ // Load site.
8422
+ $this->_site = clone $site;
8423
+ $this->_site->plan = self::decrypt_entity( $this->_site->plan );
8424
+
8425
+ /**
8426
+ * If the install owner's details are not stored locally, use the previous user's details if available.
8427
+ *
8428
+ * @author Leo Fajardo (@leorw)
8429
+ */
8430
+ if ( ! isset( $users[ $this->_site->user_id ] ) && FS_User::is_valid_id( $this->_storage->prev_user_id ) ) {
8431
+ $user_id = $this->_storage->prev_user_id;
8432
+ } else {
8433
+ $user_id = $this->_site->user_id;
8434
+ }
8435
+
8436
+ // Load relevant user.
8437
+ $this->_user = clone $users[ $user_id ];
8438
+
8439
+ // Load plans.
8440
+ $this->_plans = $plans[ $this->_slug ];
8441
+ if ( ! is_array( $this->_plans ) || empty( $this->_plans ) ) {
8442
+ $this->_sync_plans();
8443
+ } else {
8444
+ for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
8445
+ if ( $this->_plans[ $i ] instanceof FS_Plugin_Plan ) {
8446
+ $this->_plans[ $i ] = self::decrypt_entity( $this->_plans[ $i ] );
8447
+ } else {
8448
+ unset( $this->_plans[ $i ] );
8449
+ }
8450
+ }
8451
+ }
8452
+
8453
+ // Load licenses.
8454
+ $this->_licenses = array();
8455
+ if ( is_array( $licenses ) &&
8456
+ isset( $licenses[ $this->_slug ] ) &&
8457
+ isset( $licenses[ $this->_slug ][ $this->_user->id ] )
8458
+ ) {
8459
+ $this->_licenses = $licenses[ $this->_slug ][ $this->_user->id ];
8460
+ }
8461
+
8462
+ $this->_license = $this->_get_license_by_id( $this->_site->license_id );
8463
+
8464
+ if ( $this->_site->version != $this->get_plugin_version() ) {
8465
+ // If stored install version is different than current installed plugin version,
8466
+ // then update plugin version event.
8467
+ $this->update_plugin_version_event();
8468
+ }
8469
+ }
8470
+
8471
+ $this->_register_account_hooks();
8472
+ }
8473
+
8474
+ /**
8475
+ * @author Vova Feldman (@svovaf)
8476
+ * @since 1.0.1
8477
+ *
8478
+ * @param FS_User $user
8479
+ * @param FS_Site $site
8480
+ * @param bool|array $plans
8481
+ */
8482
+ private function _set_account( FS_User $user, FS_Site $site, $plans = false ) {
8483
+ $site->slug = $this->_slug;
8484
+ $site->user_id = $user->id;
8485
+
8486
+ $this->_site = $site;
8487
+ $this->_user = $user;
8488
+ if ( false !== $plans ) {
8489
+ $this->_plans = $plans;
8490
+ }
8491
+
8492
+ $this->send_install_update();
8493
+
8494
+ $this->_store_account();
8495
+
8496
+ }
8497
+
8498
+ /**
8499
+ * @author Vova Feldman (@svovaf)
8500
+ * @since 1.1.7.4
8501
+ *
8502
+ * @param array $override_with
8503
+ *
8504
+ * @return array
8505
+ */
8506
+ function get_opt_in_params( $override_with = array() ) {
8507
+ $this->_logger->entrance();
8508
+
8509
+ $current_user = self::_get_current_wp_user();
8510
+
8511
+ $activation_action = $this->get_unique_affix() . '_activate_new';
8512
+ $return_url = $this->is_anonymous() ?
8513
+ // If skipped already, then return to the account page.
8514
+ $this->get_account_url( $activation_action, array(), false ) :
8515
+ // Return to the module's main page.
8516
+ $this->get_after_activation_url( 'after_connect_url', array( 'fs_action' => $activation_action ) );
8517
+
8518
+ $params = array(
8519
+ 'user_firstname' => $current_user->user_firstname,
8520
+ 'user_lastname' => $current_user->user_lastname,
8521
+ 'user_nickname' => $current_user->user_nicename,
8522
+ 'user_email' => $current_user->user_email,
8523
+ 'user_ip' => WP_FS__REMOTE_ADDR,
8524
+ 'plugin_slug' => $this->_slug,
8525
+ 'plugin_id' => $this->get_id(),
8526
+ 'plugin_public_key' => $this->get_public_key(),
8527
+ 'plugin_version' => $this->get_plugin_version(),
8528
+ 'return_url' => fs_nonce_url( $return_url, $activation_action ),
8529
+ 'account_url' => fs_nonce_url( $this->_get_admin_page_url(
8530
+ 'account',
8531
+ array( 'fs_action' => 'sync_user' )
8532
+ ), 'sync_user' ),
8533
+ 'site_uid' => $this->get_anonymous_id(),
8534
+ 'site_url' => get_site_url(),
8535
+ 'site_name' => get_bloginfo( 'name' ),
8536
+ 'platform_version' => get_bloginfo( 'version' ),
8537
+ 'sdk_version' => $this->version,
8538
+ 'programming_language_version' => phpversion(),
8539
+ 'language' => get_bloginfo( 'language' ),
8540
+ 'charset' => get_bloginfo( 'charset' ),
8541
+ 'is_premium' => $this->is_premium(),
8542
+ 'is_active' => true,
8543
+ 'is_uninstalled' => false,
8544
+ );
8545
+
8546
+ if ( $this->is_pending_activation() &&
8547
+ ! empty( $this->_storage->pending_license_key )
8548
+ ) {
8549
+ $params['license_key'] = $this->_storage->pending_license_key;
8550
+ }
8551
+
8552
+ if ( WP_FS__SKIP_EMAIL_ACTIVATION && $this->has_secret_key() ) {
8553
+ // Even though rand() is known for its security issues,
8554
+ // the timestamp adds another layer of protection.
8555
+ // It would be very hard for an attacker to get the secret key form here.
8556
+ // Plus, this should never run in production since the secret should never
8557
+ // be included in the production version.
8558
+ $params['ts'] = WP_FS__SCRIPT_START_TIME;
8559
+ $params['salt'] = md5( uniqid( rand() ) );
8560
+ $params['secure'] = md5(
8561
+ $params['ts'] .
8562
+ $params['salt'] .
8563
+ $this->get_secret_key()
8564
+ );
8565
+ }
8566
+
8567
+ return array_merge( $params, $override_with );
8568
+ }
8569
+
8570
+ /**
8571
+ * 1. If successful opt-in or pending activation returns the next page that the user should be redirected to.
8572
+ * 2. If there was an API error, return the API result.
8573
+ *
8574
+ * @author Vova Feldman (@svovaf)
8575
+ * @since 1.1.7.4
8576
+ *
8577
+ * @param string|bool $email
8578
+ * @param string|bool $first
8579
+ * @param string|bool $last
8580
+ * @param string|bool $license_key
8581
+ * @param bool $is_uninstall If "true", this means that the module is currently being uninstalled.
8582
+ * In this case, the user and site info will be sent to the server but no
8583
+ * data will be saved to the WP installation's database.
8584
+ * @param number|bool $trial_plan_id
8585
+ * @param bool $is_disconnected Whether or not to opt in without tracking.
8586
+ *
8587
+ * @return string|object
8588
+ * @use WP_Error
8589
+ */
8590
+ function opt_in(
8591
+ $email = false,
8592
+ $first = false,
8593
+ $last = false,
8594
+ $license_key = false,
8595
+ $is_uninstall = false,
8596
+ $trial_plan_id = false,
8597
+ $is_disconnected = false
8598
+ ) {
8599
+ $this->_logger->entrance();
8600
+
8601
+ if ( false === $email ) {
8602
+ $current_user = self::_get_current_wp_user();
8603
+ $email = $current_user->user_email;
8604
+ }
8605
+
8606
+ /**
8607
+ * @since 1.2.1 If activating with license key, ignore the context-user
8608
+ * since the user will be automatically loaded from the license.
8609
+ */
8610
+ if ( empty( $license_key ) ) {
8611
+ // Clean up pending license if opt-ing in again.
8612
+ $this->_storage->remove( 'pending_license_key' );
8613
+
8614
+ if ( ! $is_uninstall ) {
8615
+ $fs_user = Freemius::_get_user_by_email( $email );
8616
+ if ( is_object( $fs_user ) && ! $this->is_pending_activation() ) {
8617
+ return $this->install_with_current_user( false, $trial_plan_id );
8618
+ }
8619
+ }
8620
+ }
8621
+
8622
+ $user_info = array();
8623
+ if ( ! empty( $email ) ) {
8624
+ $user_info['user_email'] = $email;
8625
+ }
8626
+ if ( ! empty( $first ) ) {
8627
+ $user_info['user_firstname'] = $first;
8628
+ }
8629
+ if ( ! empty( $last ) ) {
8630
+ $user_info['user_lastname'] = $last;
8631
+ }
8632
+
8633
+ $params = $this->get_opt_in_params( $user_info );
8634
+
8635
+ $filtered_license_key = false;
8636
+ if ( is_string( $license_key ) ) {
8637
+ $filtered_license_key = $this->apply_filters( 'license_key', $license_key );
8638
+ $params['license_key'] = $filtered_license_key;
8639
+ } else if ( FS_Plugin_Plan::is_valid_id( $trial_plan_id ) ) {
8640
+ $params['trial_plan_id'] = $trial_plan_id;
8641
+ }
8642
+
8643
+ if ( $is_uninstall ) {
8644
+ $params['uninstall_params'] = array(
8645
+ 'reason_id' => $this->_storage->uninstall_reason->id,
8646
+ 'reason_info' => $this->_storage->uninstall_reason->info
8647
+ );
8648
+ }
8649
+
8650
+ if ( isset( $params['license_key'] ) ) {
8651
+ $fs_user = Freemius::_get_user_by_email( $email );
8652
+
8653
+ if ( is_object( $fs_user ) ) {
8654
+ /**
8655
+ * If opting in with a context license and the context WP Admin user already opted in
8656
+ * before from the current site, add the user context security params to avoid the
8657
+ * unnecessry email activation when the context license is owned by the same context user.
8658
+ *
8659
+ * @author Leo Fajardo (@leorw)
8660
+ * @since 1.2.3
8661
+ */
8662
+ $params = array_merge( $params, FS_Security::instance()->get_context_params(
8663
+ $fs_user,
8664
+ false,
8665
+ 'install_with_existing_user'
8666
+ ) );
8667
+ }
8668
+ }
8669
+
8670
+ $params['is_disconnected'] = $is_disconnected;
8671
+ $params['format'] = 'json';
8672
+
8673
+ $url = WP_FS__ADDRESS . '/action/service/user/install/';
8674
+ if ( isset( $_COOKIE['XDEBUG_SESSION'] ) ) {
8675
+ $url = add_query_arg( 'XDEBUG_SESSION', 'PHPSTORM', $url );
8676
+ }
8677
+
8678
+ $response = wp_remote_post( $url, array(
8679
+ 'method' => 'POST',
8680
+ 'body' => $params,
8681
+ 'timeout' => 15,
8682
+ ) );
8683
+
8684
+ if ( $response instanceof WP_Error ) {
8685
+ if ( 'https://' === substr( $url, 0, 8 ) &&
8686
+ isset( $response->errors ) &&
8687
+ isset( $response->errors['http_request_failed'] )
8688
+ ) {
8689
+ $http_error = strtolower( $response->errors['http_request_failed'][0] );
8690
+
8691
+ if ( false !== strpos( $http_error, 'ssl' ) ) {
8692
+ // Failed due to old version of cURL or Open SSL (SSLv3 is not supported by CloudFlare).
8693
+ $url = 'http://' . substr( $url, 8 );
8694
+
8695
+ $response = wp_remote_post( $url, array(
8696
+ 'method' => 'POST',
8697
+ 'body' => $params,
8698
+ 'timeout' => 15,
8699
+ ) );
8700
+ }
8701
+ }
8702
+ }
8703
+
8704
+ if ( is_wp_error( $response ) ) {
8705
+ /**
8706
+ * @var WP_Error $response
8707
+ */
8708
+ $result = new stdClass();
8709
+
8710
+ $error_code = $response->get_error_code();
8711
+ $error_type = str_replace( ' ', '', ucwords( str_replace( '_', ' ', $error_code ) ) );
8712
+
8713
+ $result->error = (object) array(
8714
+ 'type' => $error_type,
8715
+ 'message' => $response->get_error_message(),
8716
+ 'code' => $error_code,
8717
+ 'http' => 402
8718
+ );
8719
+
8720
+ return $result;
8721
+ }
8722
+
8723
+ // Module is being uninstalled, don't handle the returned data.
8724
+ if ( $is_uninstall ) {
8725
+ return true;
8726
+ }
8727
+
8728
+ /**
8729
+ * When json_decode() executed on PHP 5.2 with an invalid JSON, it will throw a PHP warning. Unfortunately, the new Theme Check doesn't allow PHP silencing and the theme review team isn't open to change that, therefore, instead of using `@json_decode()` we had to use the method without the `@` directive.
8730
+ *
8731
+ * @author Vova Feldman (@svovaf)
8732
+ * @since 1.2.3
8733
+ * @link https://themes.trac.wordpress.org/ticket/46134#comment:5
8734
+ * @link https://themes.trac.wordpress.org/ticket/46134#comment:9
8735
+ * @link https://themes.trac.wordpress.org/ticket/46134#comment:12
8736
+ * @link https://themes.trac.wordpress.org/ticket/46134#comment:14
8737
+ */
8738
+ $decoded = is_string( $response['body'] ) ?
8739
+ json_decode( $response['body'] ) :
8740
+ null;
8741
+
8742
+ if ( empty( $decoded ) ) {
8743
+ return false;
8744
+ }
8745
+
8746
+ if ( ! $this->is_api_result_object( $decoded ) ) {
8747
+ if ( ! empty( $params['license_key'] ) ) {
8748
+ // Pass the fully entered license key to the failure handler.
8749
+ $params['license_key'] = $license_key;
8750
+ }
8751
+
8752
+ return $is_uninstall ?
8753
+ $decoded :
8754
+ $this->apply_filters( 'after_install_failure', $decoded, $params );
8755
+ } else if ( isset( $decoded->pending_activation ) && $decoded->pending_activation ) {
8756
+ // Pending activation, add message.
8757
+ return $this->set_pending_confirmation(
8758
+ ( isset( $decoded->email ) ?
8759
+ $decoded->email :
8760
+ true ),
8761
+ false,
8762
+ $filtered_license_key,
8763
+ ! empty( $params['trial_plan_id'] )
8764
+ );
8765
+ } else if ( isset( $decoded->install_secret_key ) ) {
8766
+ return $this->install_with_new_user(
8767
+ $decoded->user_id,
8768
+ $decoded->user_public_key,
8769
+ $decoded->user_secret_key,
8770
+ $decoded->install_id,
8771
+ $decoded->install_public_key,
8772
+ $decoded->install_secret_key,
8773
+ false
8774
+ );
8775
+ }
8776
+
8777
+ return $decoded;
8778
+ }
8779
+
8780
+ /**
8781
+ * Set user and site identities.
8782
+ *
8783
+ * @author Vova Feldman (@svovaf)
8784
+ * @since 1.0.9
8785
+ *
8786
+ * @param FS_User $user
8787
+ * @param FS_Site $site
8788
+ * @param bool $redirect
8789
+ * @param bool $auto_install Since 1.2.1.7 If `true` and setting up an account with a valid license, will
8790
+ * redirect (or return a URL) to the account page with a special parameter to
8791
+ * trigger the auto installation processes.
8792
+ *
8793
+ * @return string If redirect is `false`, returns the next page the user should be redirected to.
8794
+ */
8795
+ function setup_account(
8796
+ FS_User $user,
8797
+ FS_Site $site,
8798
+ $redirect = true,
8799
+ $auto_install = false
8800
+ ) {
8801
+ $this->_user = $user;
8802
+ $this->_site = $site;
8803
+
8804
+ $this->_sync_plans();
8805
+
8806
+ $this->_enrich_site_plan( false );
8807
+
8808
+ $this->_set_account( $user, $site );
8809
+
8810
+ if ( $this->is_trial() ) {
8811
+ // Store trial plan information.
8812
+ $this->_enrich_site_trial_plan( true );
8813
+ }
8814
+
8815
+ // If Freemius was OFF before, turn it on.
8816
+ $this->turn_on();
8817
+
8818
+ $this->do_action( 'after_account_connection', $user, $site );
8819
+
8820
+ if ( is_numeric( $site->license_id ) ) {
8821
+ $this->_license = $this->_get_license_by_id( $site->license_id );
8822
+ }
8823
+
8824
+ $this->_admin_notices->remove_sticky( 'connect_account' );
8825
+
8826
+ if ( $this->is_pending_activation() || ! $this->has_settings_menu() ) {
8827
+ // Remove pending activation sticky notice (if still exist).
8828
+ $this->_admin_notices->remove_sticky( 'activation_pending' );
8829
+
8830
+ // Remove plugin from pending activation mode.
8831
+ unset( $this->_storage->is_pending_activation );
8832
+
8833
+ if ( ! $this->is_paying_or_trial() ) {
8834
+ $this->_admin_notices->add_sticky(
8835
+ sprintf( $this->get_text_x_inline( '%s activation was successfully completed.',
8836
+ 'pluginX activation was successfully...', 'plugin-x-activation-message' ), '<b>' . $this->get_plugin_name() . '</b>' ),
8837
+ 'activation_complete'
8838
+ );
8839
+ }
8840
+ }
8841
+
8842
+ if ( $this->is_paying_or_trial() ) {
8843
+ if ( ! $this->is_premium() || ! $this->has_premium_version() || ! $this->has_settings_menu() ) {
8844
+ if ( $this->is_paying() ) {
8845
+ $this->_admin_notices->add_sticky(
8846
+ sprintf(
8847
+ $this->get_text_inline( 'Your account was successfully activated with the %s plan.', 'activation-with-plan-x-message' ),
8848
+ $this->_site->plan->title
8849
+ ) . $this->get_complete_upgrade_instructions(),
8850
+ 'plan_upgraded',
8851
+ $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
8852
+ );
8853
+ } else {
8854
+ $this->_admin_notices->add_sticky(
8855
+ sprintf(
8856
+ $this->get_text_inline( 'Your trial has been successfully started.', 'trial-started-message' ),
8857
+ '<i>' . $this->get_plugin_name() . '</i>'
8858
+ ) . $this->get_complete_upgrade_instructions( $this->_storage->trial_plan->title ),
8859
+ 'trial_started',
8860
+ $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
8861
+ );
8862
+ }
8863
+ }
8864
+
8865
+ $this->_admin_notices->remove_sticky( array(
8866
+ 'trial_promotion',
8867
+ ) );
8868
+ }
8869
+
8870
+ $plugin_id = fs_request_get( 'plugin_id', false );
8871
+
8872
+ // Store activation time ONLY for plugins (not add-ons).
8873
+ if ( ! is_numeric( $plugin_id ) || ( $plugin_id == $this->_plugin->id ) ) {
8874
+ $this->_storage->activation_timestamp = WP_FS__SCRIPT_START_TIME;
8875
+ }
8876
+
8877
+ $next_page = '';
8878
+
8879
+ $extra = array();
8880
+ if ( $auto_install ) {
8881
+ $extra['auto_install'] = 'true';
8882
+ }
8883
+
8884
+ if ( is_numeric( $plugin_id ) ) {
8885
+ /**
8886
+ * @author Leo Fajardo
8887
+ * @since 1.2.1.6
8888
+ *
8889
+ * Also sync the license after an anonymous user subscribes.
8890
+ */
8891
+ if ( $this->is_anonymous() || $plugin_id != $this->_plugin->id ) {
8892
+ // Add-on was installed - sync license right after install.
8893
+ $next_page = $this->_get_sync_license_url( $plugin_id, true, $extra );
8894
+ }
8895
+ } else {
8896
+ /**
8897
+ * @author Vova Feldman (@svovaf)
8898
+ * @since 1.1.9 If site installed with a valid license, sync license.
8899
+ */
8900
+ if ( $this->is_paying() ) {
8901
+ $this->_sync_plugin_license( true );
8902
+ }
8903
+
8904
+ // Reload the page with the keys.
8905
+ $next_page = $this->is_anonymous() ?
8906
+ // If user previously skipped, redirect to account page.
8907
+ $this->get_account_url( false, $extra ) :
8908
+ $this->get_after_activation_url( 'after_connect_url' );
8909
+ }
8910
+
8911
+ if ( ! empty( $next_page ) && $redirect ) {
8912
+ fs_redirect( $next_page );
8913
+ }
8914
+
8915
+ return $next_page;
8916
+ }
8917
+
8918
+ /**
8919
+ * Install plugin with new user information after approval.
8920
+ *
8921
+ * @author Vova Feldman (@svovaf)
8922
+ * @since 1.0.7
8923
+ */
8924
+ function _install_with_new_user() {
8925
+ $this->_logger->entrance();
8926
+
8927
+ if ( $this->is_registered() ) {
8928
+ return;
8929
+ }
8930
+
8931
+ if ( ( $this->is_plugin() && fs_request_is_action( $this->get_unique_affix() . '_activate_new' ) ) ||
8932
+ // @todo This logic should be improved because it's executed on every load of a theme.
8933
+ $this->is_theme()
8934
+ ) {
8935
+ // check_admin_referer( $this->_slug . '_activate_new' );
8936
+
8937
+ if ( fs_request_has( 'user_secret_key' ) ) {
8938
+ $this->install_with_new_user(
8939
+ fs_request_get( 'user_id' ),
8940
+ fs_request_get( 'user_public_key' ),
8941
+ fs_request_get( 'user_secret_key' ),
8942
+ fs_request_get( 'install_id' ),
8943
+ fs_request_get( 'install_public_key' ),
8944
+ fs_request_get( 'install_secret_key' ),
8945
+ true,
8946
+ fs_request_get_bool( 'auto_install' )
8947
+ );
8948
+ } else if ( fs_request_has( 'pending_activation' ) ) {
8949
+ $this->set_pending_confirmation( fs_request_get( 'user_email' ), true );
8950
+ }
8951
+ }
8952
+ }
8953
+
8954
+ /**
8955
+ * Install plugin with new user.
8956
+ *
8957
+ * @author Vova Feldman (@svovaf)
8958
+ * @since 1.1.7.4
8959
+ *
8960
+ * @param number $user_id
8961
+ * @param string $user_public_key
8962
+ * @param string $user_secret_key
8963
+ * @param number $install_id
8964
+ * @param string $install_public_key
8965
+ * @param string $install_secret_key
8966
+ * @param bool $redirect
8967
+ * @param bool $auto_install Since 1.2.1.7 If `true` and setting up an account with a valid license, will
8968
+ * redirect (or return a URL) to the account page with a special parameter to
8969
+ * trigger the auto installation processes.
8970
+ *
8971
+ * @return string If redirect is `false`, returns the next page the user should be redirected to.
8972
+ */
8973
+ private function install_with_new_user(
8974
+ $user_id,
8975
+ $user_public_key,
8976
+ $user_secret_key,
8977
+ $install_id,
8978
+ $install_public_key,
8979
+ $install_secret_key,
8980
+ $redirect = true,
8981
+ $auto_install = false
8982
+ ) {
8983
+ $user = new FS_User();
8984
+ $user->id = $user_id;
8985
+ $user->public_key = $user_public_key;
8986
+ $user->secret_key = $user_secret_key;
8987
+
8988
+ $this->_user = $user;
8989
+ $user_result = $this->get_api_user_scope()->get();
8990
+ $user = new FS_User( $user_result );
8991
+ $this->_user = $user;
8992
+
8993
+ $site = new FS_Site();
8994
+ $site->id = $install_id;
8995
+ $site->public_key = $install_public_key;
8996
+ $site->secret_key = $install_secret_key;
8997
+
8998
+ $this->_site = $site;
8999
+ $site_result = $this->get_api_site_scope()->get();
9000
+ $site = new FS_Site( $site_result );
9001
+ $this->_site = $site;
9002
+
9003
+ return $this->setup_account(
9004
+ $this->_user,
9005
+ $this->_site,
9006
+ $redirect,
9007
+ $auto_install
9008
+ );
9009
+ }
9010
+
9011
+ /**
9012
+ * @author Vova Feldman (@svovaf)
9013
+ * @since 1.1.7.4
9014
+ *
9015
+ * @param string|bool $email
9016
+ * @param bool $redirect
9017
+ * @param string|bool $license_key Since 1.2.1.5
9018
+ * @param bool $is_pending_trial Since 1.2.1.5
9019
+ *
9020
+ * @return string Since 1.2.1.5 if $redirect is `false`, return the pending activation page.
9021
+ */
9022
+ private function set_pending_confirmation(
9023
+ $email = false,
9024
+ $redirect = true,
9025
+ $license_key = false,
9026
+ $is_pending_trial = false
9027
+ ) {
9028
+ if ( $this->_ignore_pending_mode ) {
9029
+ /**
9030
+ * If explicitly asked to ignore pending mode, set to anonymous mode
9031
+ * if require confirmation before finalizing the opt-in.
9032
+ *
9033
+ * @author Vova Feldman
9034
+ * @since 1.2.1.6
9035
+ */
9036
+ $this->skip_connection();
9037
+ } else {
9038
+ // Install must be activated via email since
9039
+ // user with the same email already exist.
9040
+ $this->_storage->is_pending_activation = true;
9041
+ $this->_add_pending_activation_notice( $email, $is_pending_trial );
9042
+ }
9043
+
9044
+ if ( ! empty( $license_key ) ) {
9045
+ $this->_storage->pending_license_key = $license_key;
9046
+ }
9047
+
9048
+ // Remove the opt-in sticky notice.
9049
+ $this->_admin_notices->remove_sticky( array(
9050
+ 'connect_account',
9051
+ 'trial_promotion',
9052
+ ) );
9053
+
9054
+ $next_page = $this->get_after_activation_url( 'after_pending_connect_url' );
9055
+
9056
+ // Reload the page with with pending activation message.
9057
+ if ( $redirect ) {
9058
+ fs_redirect( $next_page );
9059
+ }
9060
+
9061
+ return $next_page;
9062
+ }
9063
+
9064
+ /**
9065
+ * Install plugin with current logged WP user info.
9066
+ *
9067
+ * @author Vova Feldman (@svovaf)
9068
+ * @since 1.0.7
9069
+ */
9070
+ function _install_with_current_user() {
9071
+ $this->_logger->entrance();
9072
+
9073
+ if ( $this->is_registered() ) {
9074
+ return;
9075
+ }
9076
+
9077
+ if ( fs_request_is_action( $this->get_unique_affix() . '_activate_existing' ) && fs_request_is_post() ) {
9078
+ // check_admin_referer( 'activate_existing_' . $this->_plugin->public_key );
9079
+
9080
+ /**
9081
+ * @author Vova Feldman (@svovaf)
9082
+ * @since 1.1.9 Add license key if given.
9083
+ */
9084
+ $license_key = fs_request_get( 'license_secret_key' );
9085
+
9086
+ $this->install_with_current_user( $license_key );
9087
+ }
9088
+ }
9089
+
9090
+
9091
+ /**
9092
+ * @author Vova Feldman (@svovaf)
9093
+ * @since 1.1.7.4
9094
+ *
9095
+ * @param string|bool $license_key
9096
+ * @param number|bool $trial_plan_id
9097
+ * @param bool $redirect
9098
+ *
9099
+ * @return string|object If redirect is `false`, returns the next page the user should be redirected to, or the
9100
+ * API error object if failed to install.
9101
+ */
9102
+ private function install_with_current_user(
9103
+ $license_key = false,
9104
+ $trial_plan_id = false,
9105
+ $redirect = true
9106
+ ) {
9107
+ // Get current logged WP user.
9108
+ $current_user = self::_get_current_wp_user();
9109
+
9110
+ // Find the relevant FS user by the email.
9111
+ $user = self::_get_user_by_email( $current_user->user_email );
9112
+
9113
+ // We have to set the user before getting user scope API handler.
9114
+ $this->_user = $user;
9115
+
9116
+ $extra_install_params = array(
9117
+ 'uid' => $this->get_anonymous_id(),
9118
+ );
9119
+
9120
+ if ( ! empty( $license_key ) ) {
9121
+ $filtered_license_key = $this->apply_filters( 'license_key', $license_key );
9122
+ $extra_install_params['license_key'] = $filtered_license_key;
9123
+ } else if ( FS_Plugin_Plan::is_valid_id( $trial_plan_id ) ) {
9124
+ $extra_install_params['trial_plan_id'] = $trial_plan_id;
9125
+ }
9126
+
9127
+ $args = $this->get_install_data_for_api( $extra_install_params, false, false );
9128
+
9129
+ // Install the plugin.
9130
+ $install = $this->get_api_user_scope()->call(
9131
+ "/plugins/{$this->get_id()}/installs.json",
9132
+ 'post',
9133
+ $args
9134
+ );
9135
+
9136
+ if ( ! $this->is_api_result_entity( $install ) ) {
9137
+ if ( ! empty( $args['license_key'] ) ) {
9138
+ // Pass full the fully entered license key to the failure handler.
9139
+ $args['license_key'] = $license_key;
9140
+ }
9141
+
9142
+ $install = $this->apply_filters( 'after_install_failure', $install, $args );
9143
+
9144
+ $this->_admin_notices->add(
9145
+ sprintf( $this->get_text_inline( 'Couldn\'t activate %s.', 'could-not-activate-x' ), $this->get_plugin_name() ) . ' ' .
9146
+ $this->get_text_inline( 'Please contact us with the following message:', 'contact-us-with-error-message' ) . ' ' . '<b>' . $install->error->message . '</b>',
9147
+ $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
9148
+ 'error'
9149
+ );
9150
+
9151
+ if ( $redirect ) {
9152
+ /**
9153
+ * We set the user before getting the user scope API handler, so the user became temporarily
9154
+ * registered (`is_registered() = true`). Since the API returned an error and we will redirect,
9155
+ * we have to set the user to `null`, otherwise, the user will be redirected to the wrong
9156
+ * activation page based on the return value of `is_registered()`. In addition, in case the
9157
+ * context plugin doesn't have a settings menu and the default page is the `Plugins` page,
9158
+ * misleading plugin activation errors will be shown on the `Plugins` page.
9159
+ *
9160
+ * @author Leo Fajardo (@leorw)
9161
+ */
9162
+ $this->_user = null;
9163
+
9164
+ fs_redirect( $this->get_activation_url( array( 'error' => $install->error->message ) ) );
9165
+ }
9166
+
9167
+ return $install;
9168
+ }
9169
+
9170
+ $site = new FS_Site( $install );
9171
+ $this->_site = $site;
9172
+
9173
+ return $this->setup_account( $this->_user, $this->_site, $redirect );
9174
+ }
9175
+
9176
+ /**
9177
+ * Tries to activate add-on account based on parent plugin info.
9178
+ *
9179
+ * @author Vova Feldman (@svovaf)
9180
+ * @since 1.0.6
9181
+ *
9182
+ * @param Freemius $parent_fs
9183
+ */
9184
+ private function _activate_addon_account( Freemius $parent_fs ) {
9185
+ if ( $this->is_registered() ) {
9186
+ // Already activated.
9187
+ return;
9188
+ }
9189
+
9190
+ // Activate add-on with parent plugin credentials.
9191
+ $addon_install = $parent_fs->get_api_site_scope()->call(
9192
+ "/addons/{$this->_plugin->id}/installs.json",
9193
+ 'post',
9194
+ $this->get_install_data_for_api( array(
9195
+ 'uid' => $this->get_anonymous_id(),
9196
+ ), false, false )
9197
+ );
9198
+
9199
+ if ( isset( $addon_install->error ) ) {
9200
+ $this->_admin_notices->add(
9201
+ sprintf( $this->get_text_inline( 'Couldn\'t activate %s.', 'could-not-activate-x' ), $this->get_plugin_name() ) . ' ' .
9202
+ $this->get_text_inline( 'Please contact us with the following message:', 'contact-us-with-error-message' ) . ' ' . '<b>' . $addon_install->error->message . '</b>',
9203
+ $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
9204
+ 'error'
9205
+ );
9206
+
9207
+ return;
9208
+ }
9209
+
9210
+ // First of all, set site info - otherwise we won't
9211
+ // be able to invoke API calls.
9212
+ $this->_site = new FS_Site( $addon_install );
9213
+
9214
+ // Sync add-on plans.
9215
+ $this->_sync_plans();
9216
+
9217
+ // Get site's current plan.
9218
+ $this->_site->plan = $this->_get_plan_by_id( $this->_site->plan->id );
9219
+
9220
+ // Get user information based on parent's plugin.
9221
+ $user = $parent_fs->get_user();
9222
+
9223
+ $this->_set_account( $user, $this->_site );
9224
+
9225
+ // Sync licenses.
9226
+ $this->_sync_licenses();
9227
+
9228
+ // Try to activate premium license.
9229
+ $this->_activate_license( true );
9230
+ }
9231
+
9232
+ /**
9233
+ * Tries to activate parent account based on add-on's info.
9234
+ *
9235
+ * @author Vova Feldman (@svovaf)
9236
+ * @since 1.2.2.7
9237
+ *
9238
+ * @param Freemius $parent_fs
9239
+ */
9240
+ private function activate_parent_account( Freemius $parent_fs ) {
9241
+ if ( ! $this->is_addon() ) {
9242
+ // This is not an add-on.
9243
+ return;
9244
+ }
9245
+
9246
+ if ( $parent_fs->is_registered() ) {
9247
+ // Already activated.
9248
+ return;
9249
+ }
9250
+
9251
+ // Activate parent with add-on's user credentials.
9252
+ $parent_install = $this->get_api_user_scope()->call(
9253
+ "/plugins/{$parent_fs->_plugin->id}/installs.json",
9254
+ 'post',
9255
+ $parent_fs->get_install_data_for_api( array(
9256
+ 'uid' => $parent_fs->get_anonymous_id(),
9257
+ ), false, false )
9258
+ );
9259
+
9260
+ if ( isset( $parent_install->error ) ) {
9261
+ $this->_admin_notices->add(
9262
+ sprintf( $this->get_text_inline( 'Couldn\'t activate %s.', 'could-not-activate-x' ), $this->get_plugin_name() ) . ' ' .
9263
+ $this->get_text_inline( 'Please contact us with the following message:', 'contact-us-with-error-message' ) . ' ' . '<b>' . $parent_install->error->message . '</b>',
9264
+ $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
9265
+ 'error'
9266
+ );
9267
+
9268
+ return;
9269
+ }
9270
+
9271
+ $parent_fs->_admin_notices->remove_sticky( 'connect_account' );
9272
+
9273
+ if ( $parent_fs->is_pending_activation() ) {
9274
+ $parent_fs->_admin_notices->remove_sticky( 'activation_pending' );
9275
+
9276
+ unset( $parent_fs->_storage->is_pending_activation );
9277
+ }
9278
+
9279
+ // First of all, set site info - otherwise we won't
9280
+ // be able to invoke API calls.
9281
+ $parent_fs->_site = new FS_Site( $parent_install );
9282
+
9283
+ // Sync add-on plans.
9284
+ $parent_fs->_sync_plans();
9285
+
9286
+ // Get site's current plan.
9287
+ $parent_fs->_site->plan = $parent_fs->_get_plan_by_id( $parent_fs->_site->plan->id );
9288
+
9289
+ // Get user information based on parent's plugin.
9290
+ $user = $this->get_user();
9291
+
9292
+ $parent_fs->_set_account( $user, $parent_fs->_site );
9293
+ }
9294
+
9295
+ #endregion
9296
+
9297
+ #----------------------------------------------------------------------------------
9298
+ #region Admin Menu Items
9299
+ #----------------------------------------------------------------------------------
9300
+
9301
+ private $_menu_items = array();
9302
+
9303
+ /**
9304
+ * @author Vova Feldman (@svovaf)
9305
+ * @since 1.2.1.8
9306
+ *
9307
+ * @return array
9308
+ */
9309
+ function get_menu_items() {
9310
+ return $this->_menu_items;
9311
+ }
9312
+
9313
+ /**
9314
+ * @author Vova Feldman (@svovaf)
9315
+ * @since 1.0.7
9316
+ *
9317
+ * @return string
9318
+ */
9319
+ function get_menu_slug() {
9320
+ return $this->_menu->get_slug();
9321
+ }
9322
+
9323
+ /**
9324
+ * @author Vova Feldman (@svovaf)
9325
+ * @since 1.0.9
9326
+ */
9327
+ function _prepare_admin_menu() {
9328
+ // if ( ! $this->is_on() ) {
9329
+ // return;
9330
+ // }
9331
+
9332
+ if ( ! $this->has_api_connectivity() && ! $this->is_enable_anonymous() ) {
9333
+ $this->_menu->remove_menu_item();
9334
+ } else {
9335
+ $this->do_action( 'before_admin_menu_init' );
9336
+
9337
+ $this->add_menu_action();
9338
+ $this->add_submenu_items();
9339
+ }
9340
+ }
9341
+
9342
+ /**
9343
+ * Admin dashboard menu items modifications.
9344
+ *
9345
+ * NOTE: admin_menu action executed before admin_init.
9346
+ *
9347
+ * @author Vova Feldman (@svovaf)
9348
+ * @since 1.0.7
9349
+ *
9350
+ */
9351
+ private function add_menu_action() {
9352
+ if ( $this->is_activation_mode() ) {
9353
+ if ( $this->is_plugin() || ( $this->has_settings_menu() && ! $this->is_free_wp_org_theme() ) ) {
9354
+ $this->override_plugin_menu_with_activation();
9355
+ } else {
9356
+ /**
9357
+ * Handle theme opt-in when the opt-in form shows as a dialog box in the themes page.
9358
  */
9359
+ if ( fs_request_is_action( $this->get_unique_affix() . '_activate_existing' ) ) {
9360
+ add_action( 'load-themes.php', array( &$this, '_install_with_current_user' ) );
9361
+ } else if ( fs_request_is_action( $this->get_unique_affix() . '_activate_new' ) ||
9362
+ fs_request_get_bool( 'pending_activation' )
9363
+ ) {
9364
+ add_action( 'load-themes.php', array( &$this, '_install_with_new_user' ) );
9365
+ }
9366
+ }
9367
+ } else {
9368
+ if ( ! $this->is_registered() ) {
9369
+ // If not registered try to install user.
9370
+ if ( fs_request_is_action( $this->get_unique_affix() . '_activate_new' ) ) {
9371
+ $this->_install_with_new_user();
9372
+ }
9373
+ } else if (
9374
+ fs_request_is_action( 'sync_user' ) &&
9375
+ ( ! $this->has_settings_menu() || $this->is_free_wp_org_theme() )
9376
+ ) {
9377
+ $this->_handle_account_user_sync();
9378
+ }
9379
+ }
9380
+ }
9381
+
9382
+ /**
9383
+ * @author Vova Feldman (@svovaf)
9384
+ * @since 1.0.1
9385
+ */
9386
+ function _redirect_on_clicked_menu_link() {
9387
+ $this->_logger->entrance();
9388
+
9389
+ $page = strtolower( isset( $_REQUEST['page'] ) ? $_REQUEST['page'] : '' );
9390
+
9391
+ $this->_logger->log( 'page = ' . $page );
9392
+
9393
+ foreach ( $this->_menu_items as $priority => $items ) {
9394
+ foreach ( $items as $item ) {
9395
+ if ( isset( $item['url'] ) ) {
9396
+ if ( $page === $this->_menu->get_slug( strtolower( $item['menu_slug'] ) ) ) {
9397
+ $this->_logger->log( 'Redirecting to ' . $item['url'] );
9398
+
9399
+ fs_redirect( $item['url'] );
9400
+ }
9401
+ }
9402
+ }
9403
+ }
9404
+ }
9405
+
9406
+ /**
9407
+ * Remove plugin's all admin menu items & pages, and replace with activation page.
9408
+ *
9409
+ * @author Vova Feldman (@svovaf)
9410
+ * @since 1.0.1
9411
+ */
9412
+ private function override_plugin_menu_with_activation() {
9413
+ $this->_logger->entrance();
9414
+
9415
+ $hook = false;
9416
+
9417
+ if ( ! $this->_menu->has_menu() ) {
9418
+ // Add the opt-in page without a menu item.
9419
+ $hook = FS_Admin_Menu_Manager::add_subpage(
9420
+ null,
9421
+ $this->get_plugin_name(),
9422
+ $this->get_plugin_name(),
9423
+ 'manage_options',
9424
+ $this->_slug,
9425
+ array( &$this, '_connect_page_render' )
9426
+ );
9427
+ } else if ( $this->_menu->is_top_level() ) {
9428
+ $hook = $this->_menu->override_menu_item( array( &$this, '_connect_page_render' ) );
9429
+
9430
+ if ( false === $hook ) {
9431
+ // Create new menu item just for the opt-in.
9432
+ $hook = FS_Admin_Menu_Manager::add_page(
9433
+ $this->get_plugin_name(),
9434
+ $this->get_plugin_name(),
9435
+ 'manage_options',
9436
+ $this->_menu->get_slug(),
9437
+ array( &$this, '_connect_page_render' )
9438
+ );
9439
+ }
9440
+ } else {
9441
+ $menus = array( $this->_menu->get_parent_slug() );
9442
+
9443
+ if ( $this->_menu->is_override_exact() ) {
9444
+ // Make sure the current page is matching the activation page.
9445
+ if ( ! $this->is_matching_url( $this->get_activation_url() ) ) {
9446
+ return;
9447
+ }
9448
+ }
9449
+
9450
+ foreach ( $menus as $parent_slug ) {
9451
+ $hook = $this->_menu->override_submenu_action(
9452
+ $parent_slug,
9453
+ $this->_menu->get_raw_slug(),
9454
+ array( &$this, '_connect_page_render' )
9455
+ );
9456
+
9457
+ if ( false !== $hook ) {
9458
+ // Found plugin's submenu item.
9459
+ break;
9460
+ }
9461
+ }
9462
+ }
9463
+
9464
+ if ( $this->is_activation_page() ) {
9465
+ // Clean admin page from distracting content.
9466
+ self::_clean_admin_content_section();
9467
+ }
9468
+
9469
+ if ( false !== $hook ) {
9470
+ if ( fs_request_is_action( $this->get_unique_affix() . '_activate_existing' ) ) {
9471
+ $this->_install_with_current_user();
9472
+ } else if ( fs_request_is_action( $this->get_unique_affix() . '_activate_new' ) ) {
9473
+ $this->_install_with_new_user();
9474
+ }
9475
+ }
9476
+ }
9477
+
9478
+ /**
9479
+ * @author Leo Fajardo (leorw)
9480
+ * @since 1.2.1
9481
+ *
9482
+ * return string
9483
+ */
9484
+ function get_top_level_menu_capability() {
9485
+ global $menu;
9486
+
9487
+ $top_level_menu_slug = $this->get_top_level_menu_slug();
9488
+
9489
+ foreach ( $menu as $menu_info ) {
9490
+ /**
9491
+ * The second element in the menu info array is the capability/role that has access to the menu and the
9492
+ * third element is the menu slug.
9493
+ */
9494
+ if ( $menu_info[2] === $top_level_menu_slug ) {
9495
+ return $menu_info[1];
9496
+ }
9497
+ }
9498
+
9499
+ return 'read';
9500
+ }
9501
+
9502
+ /**
9503
+ * @author Vova Feldman (@svovaf)
9504
+ * @since 1.0.0
9505
+ *
9506
+ * @return string
9507
+ */
9508
+ private function get_top_level_menu_slug() {
9509
+ return ( $this->is_addon() ?
9510
+ $this->get_parent_instance()->_menu->get_top_level_menu_slug() :
9511
+ $this->_menu->get_top_level_menu_slug() );
9512
+ }
9513
+
9514
+ /**
9515
+ * @author Vova Feldman (@svovaf)
9516
+ * @since 1.2.2.7
9517
+ *
9518
+ * @return string
9519
+ */
9520
+ function get_pricing_cta_label() {
9521
+ $label = $this->get_text_inline( 'Upgrade', 'upgrade' );
9522
+
9523
+ if ( $this->is_in_trial_promotion() &&
9524
+ ! $this->is_paying_or_trial()
9525
+ ) {
9526
+ // If running a trial promotion, modify the pricing to load the trial.
9527
+ $label = $this->get_text_inline( 'Start Trial', 'start-trial' );
9528
+ } else if ( $this->is_paying() ) {
9529
+ $label = $this->get_text_inline( 'Pricing', 'pricing' );
9530
+ }
9531
+
9532
+ return $label;
9533
+ }
9534
+
9535
+ /**
9536
+ * @author Vova Feldman (@svovaf)
9537
+ * @since 1.2.2.7
9538
+ *
9539
+ * @return bool
9540
+ */
9541
+ function is_pricing_page_visible() {
9542
+ return (
9543
+ // Has at least one paid plan.
9544
+ $this->has_paid_plan() &&
9545
+ // Didn't ask to hide the pricing page.
9546
+ $this->is_page_visible( 'pricing' ) &&
9547
+ // Don't have a valid active license or has more than one plan.
9548
+ ( ! $this->is_paying() || ! $this->is_single_plan() )
9549
+ );
9550
+ }
9551
+
9552
+ /**
9553
+ * Add default Freemius menu items.
9554
+ *
9555
+ * @author Vova Feldman (@svovaf)
9556
+ * @since 1.0.0
9557
+ */
9558
+ private function add_submenu_items() {
9559
+ $this->_logger->entrance();
9560
+
9561
+ if ( ! $this->is_addon() ) {
9562
+ /**
9563
+ * @since 1.2.2.7 Also add submenu items when running in a free .org theme so the tabs will be visible.
9564
+ */
9565
+ if ( ! $this->is_activation_mode() || $this->is_free_wp_org_theme() ) {
9566
+ if ( $this->has_affiliate_program() ) {
9567
+ // Add affiliation page.
9568
+ $this->add_submenu_item(
9569
+ $this->get_text_inline( 'Affiliation', 'affiliation' ),
9570
+ array( &$this, '_affiliation_page_render' ),
9571
+ $this->get_plugin_name() . ' &ndash; ' . $this->get_text_inline( 'Affiliation', 'affiliation' ),
9572
+ 'manage_options',
9573
+ 'affiliation',
9574
+ 'Freemius::_clean_admin_content_section',
9575
+ WP_FS__DEFAULT_PRIORITY,
9576
+ $this->is_submenu_item_visible( 'affiliation' )
9577
+ );
9578
+ }
9579
+
9580
+ if ( $this->is_registered() ) {
9581
+ $show_account = (
9582
+ $this->is_submenu_item_visible( 'account' ) &&
9583
+ /**
9584
+ * @since 1.2.2.7 Don't show the Account for free WP.org themes without any paid plans.
9585
+ */
9586
+ ( ! $this->is_free_wp_org_theme() || $this->has_paid_plan() )
9587
+ );
9588
+
9589
+ // Add user account page.
9590
+ $this->add_submenu_item(
9591
+ $this->get_text_inline( 'Account', 'account' ),
9592
+ array( &$this, '_account_page_render' ),
9593
+ $this->get_plugin_name() . ' &ndash; ' . $this->get_text_inline( 'Account', 'account' ),
9594
+ 'manage_options',
9595
+ 'account',
9596
+ array( &$this, '_account_page_load' ),
9597
+ WP_FS__DEFAULT_PRIORITY,
9598
+ $show_account
9599
+ );
9600
+ }
9601
+
9602
+ // Add contact page.
9603
+ $this->add_submenu_item(
9604
+ $this->get_text_inline( 'Contact Us', 'contact-us' ),
9605
+ array( &$this, '_contact_page_render' ),
9606
+ $this->get_plugin_name() . ' &ndash; ' . $this->get_text_inline( 'Contact Us', 'contact-us' ),
9607
+ 'manage_options',
9608
+ 'contact',
9609
+ 'Freemius::_clean_admin_content_section',
9610
+ WP_FS__DEFAULT_PRIORITY,
9611
+ $this->is_submenu_item_visible( 'contact' )
9612
+ );
9613
+
9614
+ if ( $this->has_addons() ) {
9615
+ $this->add_submenu_item(
9616
+ $this->get_text_inline( 'Add-Ons', 'add-ons' ),
9617
+ array( &$this, '_addons_page_render' ),
9618
+ $this->get_plugin_name() . ' &ndash; ' . $this->get_text_inline( 'Add-Ons', 'add-ons' ),
9619
+ 'manage_options',
9620
+ 'addons',
9621
+ array( &$this, '_addons_page_load' ),
9622
+ WP_FS__LOWEST_PRIORITY - 1,
9623
+ $this->is_submenu_item_visible( 'addons' )
9624
+ );
9625
+ }
9626
+
9627
+ $show_pricing = (
9628
+ $this->is_submenu_item_visible( 'pricing' ) &&
9629
+ $this->is_pricing_page_visible()
9630
+ );
9631
+
9632
+ $pricing_cta_text = $this->get_pricing_cta_label();
9633
+ $pricing_class = 'upgrade-mode';
9634
+ if ( $show_pricing ) {
9635
+ if ( $this->is_in_trial_promotion() &&
9636
+ ! $this->is_paying_or_trial()
9637
+ ) {
9638
+ // If running a trial promotion, modify the pricing to load the trial.
9639
+ $pricing_class = 'trial-mode';
9640
+ } else if ( $this->is_paying() ) {
9641
+ $pricing_class = '';
9642
+ }
9643
+ }
9644
+
9645
+ // Add upgrade/pricing page.
9646
+ $this->add_submenu_item(
9647
+ $pricing_cta_text . '&nbsp;&nbsp;' . ( is_rtl() ? '&#x2190;' : '&#x27a4;' ),
9648
+ array( &$this, '_pricing_page_render' ),
9649
+ $this->get_plugin_name() . ' &ndash; ' . $this->get_text_x_inline( 'Pricing', 'noun', 'pricing' ),
9650
+ 'manage_options',
9651
+ 'pricing',
9652
+ 'Freemius::_clean_admin_content_section',
9653
+ WP_FS__LOWEST_PRIORITY,
9654
+ $show_pricing,
9655
+ $pricing_class
9656
+ );
9657
+ }
9658
+ }
9659
+
9660
+
9661
+ if ( 0 < count( $this->_menu_items ) ) {
9662
+ if ( ! $this->_menu->is_top_level() ) {
9663
+ fs_enqueue_local_style( 'fs_common', '/admin/common.css' );
9664
+
9665
+ // Append submenu items right after the plugin's submenu item.
9666
+ $this->order_sub_submenu_items();
9667
+ } else {
9668
+ // Append submenu items.
9669
+ $this->embed_submenu_items();
9670
+ }
9671
+ }
9672
+ }
9673
+
9674
+ /**
9675
+ * Moved the actual submenu item additions to a separated function,
9676
+ * in order to support sub-submenu items when the plugin's settings
9677
+ * only have a submenu and not top-level menu item.
9678
+ *
9679
+ * @author Vova Feldman (@svovaf)
9680
+ * @since 1.1.4
9681
+ */
9682
+ private function embed_submenu_items() {
9683
+ $item_template = $this->_menu->is_top_level() ?
9684
+ '<span class="fs-submenu-item %s %s %s">%s</span>' :
9685
+ '<span class="fs-submenu-item fs-sub %s %s %s">%s</span>';
9686
+
9687
+ $top_level_menu_capability = $this->get_top_level_menu_capability();
9688
+
9689
+ ksort( $this->_menu_items );
9690
+
9691
+ foreach ( $this->_menu_items as $priority => $items ) {
9692
+ foreach ( $items as $item ) {
9693
+ $capability = ( ! empty( $item['capability'] ) ? $item['capability'] : $top_level_menu_capability );
9694
+
9695
+ $menu_item = sprintf(
9696
+ $item_template,
9697
+ $this->get_unique_affix(),
9698
+ $item['menu_slug'],
9699
+ ! empty( $item['class'] ) ? $item['class'] : '',
9700
+ $item['menu_title']
9701
+ );
9702
+
9703
+ $menu_slug = $this->_menu->get_slug( $item['menu_slug'] );
9704
+
9705
+ if ( ! isset( $item['url'] ) ) {
9706
+ $hook = FS_Admin_Menu_Manager::add_subpage(
9707
+ $item['show_submenu'] ?
9708
+ $this->get_top_level_menu_slug() :
9709
+ null,
9710
+ $item['page_title'],
9711
+ $menu_item,
9712
+ $capability,
9713
+ $menu_slug,
9714
+ $item['render_function']
9715
+ );
9716
+
9717
+ if ( false !== $item['before_render_function'] ) {
9718
+ add_action( "load-$hook", $item['before_render_function'] );
9719
+ }
9720
+ } else {
9721
+ FS_Admin_Menu_Manager::add_subpage(
9722
+ $item['show_submenu'] ?
9723
+ $this->get_top_level_menu_slug() :
9724
+ null,
9725
+ $item['page_title'],
9726
+ $menu_item,
9727
+ $capability,
9728
+ $menu_slug,
9729
+ array( $this, '' )
9730
+ );
9731
+ }
9732
+ }
9733
+ }
9734
+ }
9735
+
9736
+ /**
9737
+ * Re-order the submenu items so all Freemius added new submenu items
9738
+ * are added right after the plugin's settings submenu item.
9739
+ *
9740
+ * @author Vova Feldman (@svovaf)
9741
+ * @since 1.1.4
9742
+ */
9743
+ private function order_sub_submenu_items() {
9744
+ global $submenu;
9745
+
9746
+ $menu_slug = $this->_menu->get_top_level_menu_slug();
9747
+
9748
+ /**
9749
+ * Before "admin_menu" fires, WordPress will loop over the default submenus and remove pages for which the user
9750
+ * does not have permissions. So in case a plugin does not have top-level menu but does have submenus under any
9751
+ * of the default menus, only users that have the right role can access its sub-submenus (Account, Contact Us,
9752
+ * Support Forum, etc.) since $submenu[ $menu_slug ] will be empty if the user doesn't have permission.
9753
+ *
9754
+ * In case a plugin does not have submenus under any of the default menus but does have submenus under the menu
9755
+ * of another plugin, only users that have the right role can access its sub-submenus since we will use the
9756
+ * capability needed to access the parent menu as the capability for the submenus that we will add.
9757
+ */
9758
+ if ( empty( $submenu[ $menu_slug ] ) ) {
9759
+ return;
9760
+ }
9761
+
9762
+ $top_level_menu = &$submenu[ $menu_slug ];
9763
+
9764
+ $all_submenu_items_after = array();
9765
+
9766
+ $found_submenu_item = false;
9767
+
9768
+ foreach ( $top_level_menu as $submenu_id => $meta ) {
9769
+ if ( $found_submenu_item ) {
9770
+ // Remove all submenu items after the plugin's submenu item.
9771
+ $all_submenu_items_after[] = $meta;
9772
+ unset( $top_level_menu[ $submenu_id ] );
9773
+ }
9774
+
9775
+ if ( $this->_menu->get_raw_slug() === $meta[2] ) {
9776
+ // Found the submenu item, put all below.
9777
+ $found_submenu_item = true;
9778
+ continue;
9779
+ }
9780
+ }
9781
+
9782
+ // Embed all plugin's new submenu items.
9783
+ $this->embed_submenu_items();
9784
+
9785
+ // Start with specially high number to make sure it's appended.
9786
+ $i = max( 10000, max( array_keys( $top_level_menu ) ) + 1 );
9787
+ foreach ( $all_submenu_items_after as $meta ) {
9788
+ $top_level_menu[ $i ] = $meta;
9789
+ $i ++;
9790
+ }
9791
+
9792
+ // Sort submenu items.
9793
+ ksort( $top_level_menu );
9794
+ }
9795
+
9796
+ /**
9797
+ * Helper method to return the module's support forum URL.
9798
+ *
9799
+ * @author Vova Feldman (@svovaf)
9800
+ * @since 1.2.2.7
9801
+ *
9802
+ * @return string
9803
+ */
9804
+ function get_support_forum_url() {
9805
+ return $this->apply_filters( 'support_forum_url', "https://wordpress.org/support/{$this->_module_type}/{$this->_slug}" );
9806
+ }
9807
+
9808
+ /**
9809
+ * Displays the Support Forum link when enabled.
9810
+ *
9811
+ * Can be filtered like so:
9812
+ *
9813
+ * function _fs_show_support_menu( $is_visible, $menu_id ) {
9814
+ * if ( 'support' === $menu_id ) {
9815
+ * return _fs->is_registered();
9816
+ * }
9817
+ * return $is_visible;
9818
+ * }
9819
+ * _fs()->add_filter('is_submenu_visible', '_fs_show_support_menu', 10, 2);
9820
+ *
9821
+ */
9822
+ function _add_default_submenu_items() {
9823
+ if ( ! $this->is_on() ) {
9824
+ return;
9825
+ }
9826
+
9827
+ if ( ! $this->is_activation_mode() ) {
9828
+ $this->add_submenu_link_item(
9829
+ $this->apply_filters( 'support_forum_submenu', $this->get_text_inline( 'Support Forum', 'support-forum' ) ),
9830
+ $this->get_support_forum_url(),
9831
+ 'wp-support-forum',
9832
+ null,
9833
+ 50,
9834
+ $this->is_submenu_item_visible( 'support' )
9835
+ );
9836
+ }
9837
+ }
9838
+
9839
+ /**
9840
+ * @author Vova Feldman (@svovaf)
9841
+ * @since 1.0.1
9842
+ *
9843
+ * @param string $menu_title
9844
+ * @param callable $render_function
9845
+ * @param bool|string $page_title
9846
+ * @param string $capability
9847
+ * @param bool|string $menu_slug
9848
+ * @param bool|callable $before_render_function
9849
+ * @param int $priority
9850
+ * @param bool $show_submenu
9851
+ * @param string $class Since 1.2.1.5 can add custom classes to menu items.
9852
+ */
9853
+ function add_submenu_item(
9854
+ $menu_title,
9855
+ $render_function,
9856
+ $page_title = false,
9857
+ $capability = 'manage_options',
9858
+ $menu_slug = false,
9859
+ $before_render_function = false,
9860
+ $priority = WP_FS__DEFAULT_PRIORITY,
9861
+ $show_submenu = true,
9862
+ $class = ''
9863
+ ) {
9864
+ $this->_logger->entrance( 'Title = ' . $menu_title );
9865
+
9866
+ if ( $this->is_addon() ) {
9867
+ $parent_fs = $this->get_parent_instance();
9868
+
9869
+ if ( is_object( $parent_fs ) ) {
9870
+ $parent_fs->add_submenu_item(
9871
+ $menu_title,
9872
+ $render_function,
9873
+ $page_title,
9874
+ $capability,
9875
+ $menu_slug,
9876
+ $before_render_function,
9877
+ $priority,
9878
+ $show_submenu,
9879
+ $class
9880
+ );
9881
+
9882
+ return;
9883
+ }
9884
+ }
9885
+
9886
+ if ( ! isset( $this->_menu_items[ $priority ] ) ) {
9887
+ $this->_menu_items[ $priority ] = array();
9888
+ }
9889
+
9890
+ $this->_menu_items[ $priority ][] = array(
9891
+ 'page_title' => is_string( $page_title ) ? $page_title : $menu_title,
9892
+ 'menu_title' => $menu_title,
9893
+ 'capability' => $capability,
9894
+ 'menu_slug' => is_string( $menu_slug ) ? $menu_slug : strtolower( $menu_title ),
9895
+ 'render_function' => $render_function,
9896
+ 'before_render_function' => $before_render_function,
9897
+ 'show_submenu' => $show_submenu,
9898
+ 'class' => $class,
9899
+ );
9900
+ }
9901
+
9902
+ /**
9903
+ * @author Vova Feldman (@svovaf)
9904
+ * @since 1.0.1
9905
+ *
9906
+ * @param string $menu_title
9907
+ * @param string $url
9908
+ * @param bool $menu_slug
9909
+ * @param string $capability
9910
+ * @param int $priority
9911
+ * @param bool $show_submenu
9912
+ */
9913
+ function add_submenu_link_item(
9914
+ $menu_title,
9915
+ $url,
9916
+ $menu_slug = false,
9917
+ $capability = 'read',
9918
+ $priority = WP_FS__DEFAULT_PRIORITY,
9919
+ $show_submenu = true
9920
+ ) {
9921
+ $this->_logger->entrance( 'Title = ' . $menu_title . '; Url = ' . $url );
9922
+
9923
+ if ( $this->is_addon() ) {
9924
+ $parent_fs = $this->get_parent_instance();
9925
+
9926
+ if ( is_object( $parent_fs ) ) {
9927
+ $parent_fs->add_submenu_link_item(
9928
+ $menu_title,
9929
+ $url,
9930
+ $menu_slug,
9931
+ $capability,
9932
+ $priority,
9933
+ $show_submenu
9934
+ );
9935
+
9936
+ return;
9937
+ }
9938
+ }
9939
+
9940
+ if ( ! isset( $this->_menu_items[ $priority ] ) ) {
9941
+ $this->_menu_items[ $priority ] = array();
9942
+ }
9943
+
9944
+ $this->_menu_items[ $priority ][] = array(
9945
+ 'menu_title' => $menu_title,
9946
+ 'capability' => $capability,
9947
+ 'menu_slug' => is_string( $menu_slug ) ? $menu_slug : strtolower( $menu_title ),
9948
+ 'url' => $url,
9949
+ 'page_title' => $menu_title,
9950
+ 'render_function' => 'fs_dummy',
9951
+ 'before_render_function' => '',
9952
+ 'show_submenu' => $show_submenu,
9953
+ );
9954
+ }
9955
+
9956
+ #endregion ------------------------------------------------------------------
9957
+
9958
+
9959
+ #--------------------------------------------------------------------------------
9960
+ #region Actions / Hooks / Filters
9961
+ #--------------------------------------------------------------------------------
9962
+
9963
+ /**
9964
+ * @author Vova Feldman (@svovaf)
9965
+ * @since 1.1.7
9966
+ *
9967
+ * @param string $tag
9968
+ *
9969
+ * @return string
9970
+ */
9971
+ public function get_action_tag( $tag ) {
9972
+ return self::get_action_tag_static( $tag, $this->_slug, $this->is_plugin() );
9973
+ }
9974
+
9975
+ /**
9976
+ * @author Vova Feldman (@svovaf)
9977
+ * @since 1.2.1.6
9978
+ *
9979
+ * @param string $tag
9980
+ * @param string $slug
9981
+ * @param bool $is_plugin
9982
+ *
9983
+ * @return string
9984
+ */
9985
+ static function get_action_tag_static( $tag, $slug = '', $is_plugin = true ) {
9986
+ $action = "fs_{$tag}";
9987
+
9988
+ if ( ! empty( $slug ) ) {
9989
+ $action .= '_' . self::get_module_unique_affix( $slug, $is_plugin );
9990
+ }
9991
+
9992
+ return $action;
9993
+ }
9994
+
9995
+ /**
9996
+ * Returns a string that can be used to generate a unique action name,
9997
+ * option name, HTML element ID, or HTML element class.
9998
+ *
9999
+ * @author Leo Fajardo (@leorw)
10000
+ * @since 1.2.2
10001
+ *
10002
+ * @return string
10003
+ */
10004
+ public function get_unique_affix() {
10005
+ return self::get_module_unique_affix( $this->_slug, $this->is_plugin() );
10006
+ }
10007
+
10008
+ /**
10009
+ * Returns a string that can be used to generate a unique action name,
10010
+ * option name, HTML element ID, or HTML element class.
10011
+ *
10012
+ * @author Vova Feldman (@svovaf)
10013
+ * @since 1.2.2.5
10014
+ *
10015
+ * @param string $slug
10016
+ * @param bool $is_plugin
10017
+ *
10018
+ * @return string
10019
+ */
10020
+ static function get_module_unique_affix( $slug, $is_plugin = true ) {
10021
+ $affix = $slug;
10022
+
10023
+ if ( ! $is_plugin ) {
10024
+ $affix .= '-' . WP_FS__MODULE_TYPE_THEME;
10025
+ }
10026
+
10027
+ return $affix;
10028
+ }
10029
+
10030
+ /**
10031
+ * @author Vova Feldman (@svovaf)
10032
+ * @since 1.2.1
10033
+ * @since 1.2.2.5 The AJAX action names are based on the module ID, not like the non-AJAX actions that are
10034
+ * based on the slug for backward compatibility.
10035
+ *
10036
+ * @param string $tag
10037
+ *
10038
+ * @return string
10039
+ */
10040
+ function get_ajax_action( $tag ) {
10041
+ return self::get_ajax_action_static( $tag, $this->_module_id );
10042
+ }
10043
+
10044
+ /**
10045
+ * @author Vova Feldman (@svovaf)
10046
+ * @since 1.2.1.7
10047
+ *
10048
+ * @param string $tag
10049
+ *
10050
+ * @return string
10051
+ */
10052
+ function get_ajax_security( $tag ) {
10053
+ return wp_create_nonce( $this->get_ajax_action( $tag ) );
10054
+ }
10055
+
10056
+ /**
10057
+ * @author Vova Feldman (@svovaf)
10058
+ * @since 1.2.1.7
10059
+ *
10060
+ * @param string $tag
10061
+ */
10062
+ function check_ajax_referer( $tag ) {
10063
+ check_ajax_referer( $this->get_ajax_action( $tag ), 'security' );
10064
+ }
10065
+
10066
+ /**
10067
+ * @author Vova Feldman (@svovaf)
10068
+ * @since 1.2.1.6
10069
+ * @since 1.2.2.5 The AJAX action names are based on the module ID, not like the non-AJAX actions that are
10070
+ * based on the slug for backward compatibility.
10071
+ *
10072
+ * @param string $tag
10073
+ * @param number|null $module_id
10074
+ *
10075
+ * @return string
10076
+ */
10077
+ private static function get_ajax_action_static( $tag, $module_id = null ) {
10078
+ $action = "fs_{$tag}";
10079
+
10080
+ if ( ! empty( $module_id ) ) {
10081
+ $action .= "_{$module_id}";
10082
+ }
10083
+
10084
+ return $action;
10085
+ }
10086
+
10087
+ /**
10088
+ * Do action, specific for the current context plugin.
10089
+ *
10090
+ * @author Vova Feldman (@svovaf)
10091
+ * @since 1.0.1
10092
+ *
10093
+ * @param string $tag The name of the action to be executed.
10094
+ * @param mixed $arg,... Optional. Additional arguments which are passed on to the
10095
+ * functions hooked to the action. Default empty.
10096
+ *
10097
+ * @uses do_action()
10098
+ */
10099
+ function do_action( $tag, $arg = '' ) {
10100
+ $this->_logger->entrance( $tag );
10101
+
10102
+ $args = func_get_args();
10103
+
10104
+ call_user_func_array( 'do_action', array_merge(
10105
+ array( $this->get_action_tag( $tag ) ),
10106
+ array_slice( $args, 1 ) )
10107
+ );
10108
+ }
10109
+
10110
+ /**
10111
+ * Add action, specific for the current context plugin.
10112
+ *
10113
+ * @author Vova Feldman (@svovaf)
10114
+ * @since 1.0.1
10115
+ *
10116
+ * @param string $tag
10117
+ * @param callable $function_to_add
10118
+ * @param int $priority
10119
+ * @param int $accepted_args
10120
+ *
10121
+ * @uses add_action()
10122
+ */
10123
+ function add_action(
10124
+ $tag,
10125
+ $function_to_add,
10126
+ $priority = WP_FS__DEFAULT_PRIORITY,
10127
+ $accepted_args = 1
10128
+ ) {
10129
+ $this->_logger->entrance( $tag );
10130
+
10131
+ add_action( $this->get_action_tag( $tag ), $function_to_add, $priority, $accepted_args );
10132
+ }
10133
+
10134
+ /**
10135
+ * Add AJAX action, specific for the current context plugin.
10136
+ *
10137
+ * @author Vova Feldman (@svovaf)
10138
+ * @since 1.2.1
10139
+ *
10140
+ * @param string $tag
10141
+ * @param callable $function_to_add
10142
+ * @param int $priority
10143
+ *
10144
+ * @uses add_action()
10145
+ *
10146
+ * @return bool True if action added, false if no need to add the action since the AJAX call isn't matching.
10147
+ */
10148
+ function add_ajax_action(
10149
+ $tag,
10150
+ $function_to_add,
10151
+ $priority = WP_FS__DEFAULT_PRIORITY
10152
+ ) {
10153
+ $this->_logger->entrance( $tag );
10154
+
10155
+ return self::add_ajax_action_static(
10156
+ $tag,
10157
+ $function_to_add,
10158
+ $priority,
10159
+ $this->_module_id
10160
+ );
10161
+ }
10162
+
10163
+ /**
10164
+ * Add AJAX action.
10165
+ *
10166
+ * @author Vova Feldman (@svovaf)
10167
+ * @since 1.2.1.6
10168
+ *
10169
+ * @param string $tag
10170
+ * @param callable $function_to_add
10171
+ * @param int $priority
10172
+ * @param number|null $module_id
10173
+ *
10174
+ * @return bool True if action added, false if no need to add the action since the AJAX call isn't matching.
10175
+ * @uses add_action()
10176
+ *
10177
+ */
10178
+ static function add_ajax_action_static(
10179
+ $tag,
10180
+ $function_to_add,
10181
+ $priority = WP_FS__DEFAULT_PRIORITY,
10182
+ $module_id = null
10183
+ ) {
10184
+ self::$_static_logger->entrance( $tag );
10185
+
10186
+ if ( ! self::is_ajax_action_static( $tag, $module_id ) ) {
10187
+ return false;
10188
+ }
10189
+
10190
+ add_action(
10191
+ 'wp_ajax_' . self::get_ajax_action_static( $tag, $module_id ),
10192
+ $function_to_add,
10193
+ $priority,
10194
+ 0
10195
+ );
10196
+
10197
+ self::$_static_logger->info( "$tag AJAX callback action added." );
10198
+
10199
+ return true;
10200
+ }
10201
+
10202
+ /**
10203
+ * Send a JSON response back to an Ajax request.
10204
+ *
10205
+ * @author Vova Feldman (@svovaf)
10206
+ * @since 1.2.1.5
10207
+ *
10208
+ * @param mixed $response
10209
+ */
10210
+ static function shoot_ajax_response( $response ) {
10211
+ wp_send_json( $response );
10212
+ }
10213
+
10214
+ /**
10215
+ * Send a JSON response back to an Ajax request, indicating success.
10216
+ *
10217
+ * @author Vova Feldman (@svovaf)
10218
+ * @since 1.2.1.5
10219
+ *
10220
+ * @param mixed $data Data to encode as JSON, then print and exit.
10221
+ */
10222
+ static function shoot_ajax_success( $data = null ) {
10223
+ wp_send_json_success( $data );
10224
+ }
10225
+
10226
+ /**
10227
+ * Send a JSON response back to an Ajax request, indicating failure.
10228
+ *
10229
+ * @author Vova Feldman (@svovaf)
10230
+ * @since 1.2.1.5
10231
+ *
10232
+ * @param mixed $error Optional error message.
10233
+ */
10234
+ static function shoot_ajax_failure( $error = '' ) {
10235
+ $result = array( 'success' => false );
10236
+ if ( ! empty( $error ) ) {
10237
+ $result['error'] = $error;
10238
+ }
10239
+
10240
+ wp_send_json( $result );
10241
+ }
10242
+
10243
+ /**
10244
+ * Apply filter, specific for the current context plugin.
10245
+ *
10246
+ * @author Vova Feldman (@svovaf)
10247
+ * @since 1.0.9
10248
+ *
10249
+ * @param string $tag The name of the filter hook.
10250
+ * @param mixed $value The value on which the filters hooked to `$tag` are applied on.
10251
+ *
10252
+ * @return mixed The filtered value after all hooked functions are applied to it.
10253
+ *
10254
+ * @uses apply_filters()
10255
+ */
10256
+ function apply_filters( $tag, $value ) {
10257
+ $this->_logger->entrance( $tag );
10258
+
10259
+ $args = func_get_args();
10260
+ array_unshift( $args, $this->get_unique_affix() );
10261
+
10262
+ return call_user_func_array( 'fs_apply_filter', $args );
10263
+ }
10264
+
10265
+ /**
10266
+ * Add filter, specific for the current context plugin.
10267
+ *
10268
+ * @author Vova Feldman (@svovaf)
10269
+ * @since 1.0.9
10270
+ *
10271
+ * @param string $tag
10272
+ * @param callable $function_to_add
10273
+ * @param int $priority
10274
+ * @param int $accepted_args
10275
+ *
10276
+ * @uses add_filter()
10277
+ */
10278
+ function add_filter( $tag, $function_to_add, $priority = WP_FS__DEFAULT_PRIORITY, $accepted_args = 1 ) {
10279
+ $this->_logger->entrance( $tag );
10280
+
10281
+ add_filter( $this->get_action_tag( $tag ), $function_to_add, $priority, $accepted_args );
10282
+ }
10283
+
10284
+ /**
10285
+ * Check if has filter.
10286
+ *
10287
+ * @author Vova Feldman (@svovaf)
10288
+ * @since 1.1.4
10289
+ *
10290
+ * @param string $tag
10291
+ * @param callable|bool $function_to_check Optional. The callback to check for. Default false.
10292
+ *
10293
+ * @return false|int
10294
+ *
10295
+ * @uses has_filter()
10296
+ */
10297
+ function has_filter( $tag, $function_to_check = false ) {
10298
+ $this->_logger->entrance( $tag );
10299
+
10300
+ return has_filter( $this->get_action_tag( $tag ), $function_to_check );
10301
+ }
10302
+
10303
+ #endregion
10304
+
10305
+ /**
10306
+ * Override default i18n text phrases.
10307
+ *
10308
+ * @author Vova Feldman (@svovaf)
10309
+ * @since 1.1.6
10310
+ *
10311
+ * @param string[] string $key_value
10312
+ *
10313
+ * @uses fs_override_i18n()
10314
+ */
10315
+ function override_i18n( $key_value ) {
10316
+ fs_override_i18n( $key_value, $this->_slug );
10317
+ }
10318
+
10319
+ /* Account Page
10320
+ ------------------------------------------------------------------------------------------------------------------*/
10321
+ /**
10322
+ * Update site information.
10323
+ *
10324
+ * @author Vova Feldman (@svovaf)
10325
+ * @since 1.0.1
10326
+ *
10327
+ * @param bool $store Flush to Database if true.
10328
+ */
10329
+ private function _store_site( $store = true ) {
10330
+ $this->_logger->entrance();
10331
+
10332
+ if ( empty( $this->_site->id ) ) {
10333
+ $this->_logger->error( "Empty install ID, can't store site." );
10334
+
10335
+ return;
10336
+ }
10337
+
10338
+ $encrypted_site = clone $this->_site;
10339
+ $encrypted_site->plan = self::_encrypt_entity( $this->_site->plan );
10340
+
10341
+ $sites = self::get_all_sites( $this->_module_type );
10342
+
10343
+ if ( empty( $this->_storage->prev_user_id ) && $this->_user->id != $this->_site->user_id ) {
10344
+ /**
10345
+ * Store the current user ID as the previous user ID so that the previous user can be used
10346
+ * as the install's owner while the new owner's details are not yet available.
10347
+ *
10348
+ * This will be executed only in the `replica` site. For example, there are 2 sites, namely `original`
10349
+ * and `replica`, then an ownership change was initiated and completed in the `original`, the `replica`
10350
+ * will be using the previous user until it is updated again (e.g.: until the next clone of `original`
10351
+ * into `replica`.
10352
+ *
10353
+ * @author Leo Fajardo (@leorw)
10354
+ */
10355
+ $this->_storage->prev_user_id = $sites[ $this->_slug ]->user_id;
10356
+ }
10357
+
10358
+ $sites[ $this->_slug ] = $encrypted_site;
10359
+
10360
+ $this->set_account_option( 'sites', $sites, $store );
10361
+ }
10362
+
10363
+ /**
10364
+ * Update plugin's plans information.
10365
+ *
10366
+ * @author Vova Feldman (@svovaf)
10367
+ * @since 1.0.2
10368
+ *
10369
+ * @param bool $store Flush to Database if true.
10370
+ */
10371
+ private function _store_plans( $store = true ) {
10372
+ $this->_logger->entrance();
10373
+
10374
+ $plans = self::get_all_plans( $this->_module_type );
10375
+
10376
+ // Copy plans.
10377
+ $encrypted_plans = array();
10378
+ for ( $i = 0, $len = count( $this->_plans ); $i < $len; $i ++ ) {
10379
+ $encrypted_plans[] = self::_encrypt_entity( $this->_plans[ $i ] );
10380
+ }
10381
+
10382
+ $plans[ $this->_slug ] = $encrypted_plans;
10383
+
10384
+ $this->set_account_option( 'plans', $plans, $store );
10385
+ }
10386
+
10387
+ /**
10388
+ * Update user's plugin licenses.
10389
+ *
10390
+ * @author Vova Feldman (@svovaf)
10391
+ * @since 1.0.5
10392
+ *
10393
+ * @param bool $store
10394
+ * @param string|bool $plugin_slug
10395
+ * @param FS_Plugin_License[] $licenses
10396
+ */
10397
+ private function _store_licenses( $store = true, $plugin_slug = false, $licenses = array() ) {
10398
+ $this->_logger->entrance();
10399
+
10400
+ $all_licenses = self::get_all_licenses( $this->_module_type );
10401
+
10402
+ if ( ! is_string( $plugin_slug ) ) {
10403
+ $plugin_slug = $this->_slug;
10404
+ $licenses = $this->_licenses;
10405
+ }
10406
+
10407
+ if ( ! isset( $all_licenses[ $plugin_slug ] ) ) {
10408
+ $all_licenses[ $plugin_slug ] = array();
10409
+ }
10410
+
10411
+ $all_licenses[ $plugin_slug ][ $this->_user->id ] = $licenses;
10412
+
10413
+ $this->set_account_option( 'licenses', $all_licenses, $store );
10414
+ }
10415
+
10416
+ /**
10417
+ * Update user information.
10418
+ *
10419
+ * @author Vova Feldman (@svovaf)
10420
+ * @since 1.0.1
10421
+ *
10422
+ * @param bool $store Flush to Database if true.
10423
+ */
10424
+ private function _store_user( $store = true ) {
10425
+ $this->_logger->entrance();
10426
+
10427
+ if ( empty( $this->_user->id ) ) {
10428
+ $this->_logger->error( "Empty user ID, can't store user." );
10429
+
10430
+ return;
10431
+ }
10432
+
10433
+ $users = self::get_all_users();
10434
+ $users[ $this->_user->id ] = $this->_user;
10435
+ self::$_accounts->set_option( 'users', $users, $store );
10436
+ }
10437
+
10438
+ /**
10439
+ * Update new updates information.
10440
+ *
10441
+ * @author Vova Feldman (@svovaf)
10442
+ * @since 1.0.4
10443
+ *
10444
+ * @param FS_Plugin_Tag|null $update
10445
+ * @param bool $store Flush to Database if true.
10446
+ * @param bool|number $plugin_id
10447
+ */
10448
+ private function _store_update( $update, $store = true, $plugin_id = false ) {
10449
+ $this->_logger->entrance();
10450
+
10451
+ if ( $update instanceof FS_Plugin_Tag ) {
10452
+ $update->updated = time();
10453
+ }
10454
+
10455
+ if ( ! is_numeric( $plugin_id ) ) {
10456
+ $plugin_id = $this->_plugin->id;
10457
+ }
10458
+
10459
+ $updates = self::get_all_updates();
10460
+ $updates[ $plugin_id ] = $update;
10461
+ self::$_accounts->set_option( 'updates', $updates, $store );
10462
+ }
10463
+
10464
+ /**
10465
+ * Update new updates information.
10466
+ *
10467
+ * @author Vova Feldman (@svovaf)
10468
+ * @since 1.0.6
10469
+ *
10470
+ * @param FS_Plugin[] $plugin_addons
10471
+ * @param bool $store Flush to Database if true.
10472
+ */
10473
+ private function _store_addons( $plugin_addons, $store = true ) {
10474
+ $this->_logger->entrance();
10475
+
10476
+ $addons = self::get_all_addons();
10477
+ $addons[ $this->_plugin->id ] = $plugin_addons;
10478
+ self::$_accounts->set_option( 'addons', $addons, $store );
10479
+ }
10480
+
10481
+ /**
10482
+ * Delete plugin's associated add-ons.
10483
+ *
10484
+ * @author Vova Feldman (@svovaf)
10485
+ * @since 1.0.8
10486
+ *
10487
+ * @param bool $store
10488
+ *
10489
+ * @return bool
10490
+ */
10491
+ private function _delete_account_addons( $store = true ) {
10492
+ $all_addons = self::get_all_account_addons();
10493
+
10494
+ if ( ! isset( $all_addons[ $this->_plugin->id ] ) ) {
10495
+ return false;
10496
+ }
10497
+
10498
+ unset( $all_addons[ $this->_plugin->id ] );
10499
+
10500
+ self::$_accounts->set_option( 'account_addons', $all_addons, $store );
10501
+
10502
+ return true;
10503
+ }
10504
+
10505
+ /**
10506
+ * Update account add-ons list.
10507
+ *
10508
+ * @author Vova Feldman (@svovaf)
10509
+ * @since 1.0.6
10510
+ *
10511
+ * @param FS_Plugin[] $addons
10512
+ * @param bool $store Flush to Database if true.
10513
+ */
10514
+ private function _store_account_addons( $addons, $store = true ) {
10515
+ $this->_logger->entrance();
10516
+
10517
+ $all_addons = self::get_all_account_addons();
10518
+ $all_addons[ $this->_plugin->id ] = $addons;
10519
+ self::$_accounts->set_option( 'account_addons', $all_addons, $store );
10520
+ }
10521
+
10522
+ /**
10523
+ * Store account params in the Database.
10524
+ *
10525
+ * @author Vova Feldman (@svovaf)
10526
+ * @since 1.0.1
10527
+ */
10528
+ private function _store_account() {
10529
+ $this->_logger->entrance();
10530
+
10531
+ $this->_store_site( false );
10532
+ $this->_store_user( false );
10533
+ $this->_store_plans( false );
10534
+ $this->_store_licenses( false );
10535
+
10536
+ self::$_accounts->store();
10537
+ }
10538
+
10539
+ /**
10540
+ * Sync user's information.
10541
+ *
10542
+ * @author Vova Feldman (@svovaf)
10543
+ * @since 1.0.3
10544
+ * @uses FS_Api
10545
+ */
10546
+ private function _handle_account_user_sync() {
10547
+ $this->_logger->entrance();
10548
+
10549
+ $api = $this->get_api_user_scope();
10550
+
10551
+ // Get user's information.
10552
+ $user = $api->get( '/', true );
10553
+
10554
+ if ( isset( $user->id ) ) {
10555
+ $this->_user->first = $user->first;
10556
+ $this->_user->last = $user->last;
10557
+ $this->_user->email = $user->email;
10558
+
10559
+ $is_menu_item_account_visible = $this->is_submenu_item_visible( 'account' );
10560
+
10561
+ if ( $user->is_verified &&
10562
+ ( ! isset( $this->_user->is_verified ) || false === $this->_user->is_verified )
10563
+ ) {
10564
+ $this->_user->is_verified = true;
10565
+
10566
+ $this->do_action( 'account_email_verified', $user->email );
10567
+
10568
+ $this->_admin_notices->add(
10569
+ $this->get_text_inline( 'Your email has been successfully verified - you are AWESOME!', 'email-verified-message' ),
10570
+ $this->get_text_x_inline( 'Right on', 'a positive response', 'right-on' ) . '!',
10571
+ 'success',
10572
+ // Make admin sticky if account menu item is invisible,
10573
+ // since the page will be auto redirected to the plugin's
10574
+ // main settings page, and the non-sticky message
10575
+ // will disappear.
10576
+ ! $is_menu_item_account_visible,
10577
+ false,
10578
+ 'email_verified'
10579
+ );
10580
+ }
10581
+
10582
+ // Flush user details to DB.
10583
+ $this->_store_user();
10584
+
10585
+ $this->do_action( 'after_account_user_sync', $user );
10586
+
10587
+ /**
10588
+ * If account menu item is hidden, redirect to plugin's main settings page.
10589
+ *
10590
+ * @author Vova Feldman (@svovaf)
10591
+ * @since 1.1.6
10592
+ *
10593
+ * @link https://github.com/Freemius/wordpress-sdk/issues/6
10594
+ */
10595
+ if ( ! $is_menu_item_account_visible ) {
10596
+ fs_redirect( $this->_get_admin_page_url() );
10597
+ }
10598
+ }
10599
+ }
10600
+
10601
+ /**
10602
+ * @param bool $store
10603
+ *
10604
+ * @return FS_Plugin_Plan|object|false
10605
+ */
10606
+ private function _enrich_site_plan( $store = true ) {
10607
+ // Try to load plan from local cache.
10608
+ $plan = $this->_get_plan_by_id( $this->_site->plan->id );
10609
+
10610
+ if ( false === $plan ) {
10611
+ $plan = $this->_fetch_site_plan();
10612
+ }
10613
+
10614
+ if ( $plan instanceof FS_Plugin_Plan ) {
10615
+ $this->_update_plan( $plan, $store );
10616
+ }
10617
+
10618
+ return $plan;
10619
+ }
10620
+
10621
+ /**
10622
+ * @author Vova Feldman (@svovaf)
10623
+ * @since 1.0.9
10624
+ * @uses FS_Api
10625
+ *
10626
+ * @param bool $store
10627
+ *
10628
+ * @return FS_Plugin_Plan|object|false
10629
+ */
10630
+ private function _enrich_site_trial_plan( $store = true ) {
10631
+ // Try to load plan from local cache.
10632
+ $trial_plan = $this->_get_plan_by_id( $this->_site->trial_plan_id );
10633
+
10634
+ if ( false === $trial_plan ) {
10635
+ $trial_plan = $this->_fetch_site_plan( $this->_site->trial_plan_id );
10636
+ }
10637
+
10638
+ if ( $trial_plan instanceof FS_Plugin_Plan ) {
10639
+ $this->_storage->store( 'trial_plan', $trial_plan, $store );
10640
+ }
10641
+
10642
+ return $trial_plan;
10643
+ }
10644
+
10645
+ /**
10646
+ * @author Vova Feldman (@svovaf)
10647
+ * @since 1.0.9
10648
+ * @uses FS_Api
10649
+ *
10650
+ * @param number|bool $license_id
10651
+ *
10652
+ * @return FS_Subscription|object|bool
10653
+ */
10654
+ private function _fetch_site_license_subscription( $license_id = false ) {
10655
+ $this->_logger->entrance();
10656
+ $api = $this->get_api_site_scope();
10657
+
10658
+ if ( ! is_numeric( $license_id ) ) {
10659
+ $license_id = $this->_license->id;
10660
+ }
10661
+
10662
+ $result = $api->get( "/licenses/{$license_id}/subscriptions.json", true );
10663
+
10664
+ return ! isset( $result->error ) ?
10665
+ ( ( is_array( $result->subscriptions ) && 0 < count( $result->subscriptions ) ) ?
10666
+ new FS_Subscription( $result->subscriptions[0] ) :
10667
+ false
10668
+ ) :
10669
+ $result;
10670
+ }
10671
+
10672
+ /**
10673
+ * @author Vova Feldman (@svovaf)
10674
+ * @since 1.0.4
10675
+ * @uses FS_Api
10676
+ *
10677
+ * @param number|bool $plan_id
10678
+ *
10679
+ * @return FS_Plugin_Plan|object
10680
+ */
10681
+ private function _fetch_site_plan( $plan_id = false ) {
10682
+ $this->_logger->entrance();
10683
+ $api = $this->get_api_site_scope();
10684
+
10685
+ if ( ! is_numeric( $plan_id ) ) {
10686
+ $plan_id = $this->_site->plan->id;
10687
+ }
10688
+
10689
+ $plan = $api->get( "/plans/{$plan_id}.json", true );
10690
+
10691
+ return ! isset( $plan->error ) ? new FS_Plugin_Plan( $plan ) : $plan;
10692
+ }
10693
+
10694
+ /**
10695
+ * @author Vova Feldman (@svovaf)
10696
+ * @since 1.0.5
10697
+ * @uses FS_Api
10698
+ *
10699
+ * @return FS_Plugin_Plan[]|object
10700
+ */
10701
+ private function _fetch_plugin_plans() {
10702
+ $this->_logger->entrance();
10703
+ $api = $this->get_api_site_scope();
10704
+
10705
+ /**
10706
+ * @since 1.2.3 When running in DEV mode, retrieve pending plans as well.
10707
+ */
10708
+ $result = $api->get( '/plans.json?show_pending=' . ( $this->has_secret_key() ? 'true' : 'false' ), true );
10709
+
10710
+ if ( $this->is_api_result_object( $result, 'plans' ) && is_array( $result->plans ) ) {
10711
+ for ( $i = 0, $len = count( $result->plans ); $i < $len; $i ++ ) {
10712
+ $result->plans[ $i ] = new FS_Plugin_Plan( $result->plans[ $i ] );
10713
+ }
10714
+
10715
+ $result = $result->plans;
10716
+ }
10717
+
10718
+ return $result;
10719
+ }
10720
+
10721
+ /**
10722
+ * @author Vova Feldman (@svovaf)
10723
+ * @since 1.0.5
10724
+ * @uses FS_Api
10725
+ *
10726
+ * @param number|bool $plugin_id
10727
+ * @param number|bool $site_license_id
10728
+ *
10729
+ * @return FS_Plugin_License[]|object
10730
+ */
10731
+ private function _fetch_licenses( $plugin_id = false, $site_license_id = false ) {
10732
+ $this->_logger->entrance();
10733
+
10734
+ $api = $this->get_api_user_scope();
10735
+
10736
+ if ( ! is_numeric( $plugin_id ) ) {
10737
+ $plugin_id = $this->_plugin->id;
10738
+ }
10739
+
10740
+ $result = $api->get( "/plugins/{$plugin_id}/licenses.json", true );
10741
+
10742
+ $is_site_license_synced = false;
10743
+
10744
+ $api_errors = array();
10745
+
10746
+ if ( $this->is_api_result_object( $result, 'licenses' ) &&
10747
+ is_array( $result->licenses )
10748
+ ) {
10749
+ for ( $i = 0, $len = count( $result->licenses ); $i < $len; $i ++ ) {
10750
+ $result->licenses[ $i ] = new FS_Plugin_License( $result->licenses[ $i ] );
10751
+
10752
+ if ( ( ! $is_site_license_synced ) && is_numeric( $site_license_id ) ) {
10753
+ $is_site_license_synced = ( $site_license_id == $result->licenses[ $i ]->id );
10754
+ }
10755
+ }
10756
+
10757
+ $result = $result->licenses;
10758
+ } else {
10759
+ $api_errors[] = $result;
10760
+ $result = array();
10761
+ }
10762
+
10763
+ if ( ! $is_site_license_synced ) {
10764
+ $api = $this->get_api_site_scope();
10765
+
10766
+ if ( is_numeric( $site_license_id ) ) {
10767
+ // Try to retrieve a foreign license that is linked to the install.
10768
+ $api_result = $api->call( '/licenses.json' );
10769
+
10770
+ if ( $this->is_api_result_object( $api_result, 'licenses' ) &&
10771
+ is_array( $api_result->licenses )
10772
+ ) {
10773
+ $licenses = $api_result->licenses;
10774
+
10775
+ if ( ! empty( $licenses ) ) {
10776
+ $result[] = new FS_Plugin_License( $licenses[0] );
10777
+ }
10778
+ } else {
10779
+ $api_errors[] = $api_result;
10780
+ }
10781
+ } else if ( is_object( $this->_license ) ) {
10782
+ // Fetch foreign license by ID and license key.
10783
+ $license = $api->get( "/licenses/{$this->_license->id}.json?license_key=" .
10784
+ urlencode( $this->_license->secret_key ) );
10785
+
10786
+ if ( $this->is_api_result_entity( $license ) ) {
10787
+ $result[] = new FS_Plugin_License( $license );
10788
+ } else {
10789
+ $api_errors[] = $license;
10790
+ }
10791
+ }
10792
+ }
10793
+
10794
+ if ( is_array( $result ) && 0 < count( $result ) ) {
10795
+ // If found at least one license, return license collection even if there are errors.
10796
+ return $result;
10797
+ }
10798
+
10799
+ if ( ! empty( $api_errors ) ) {
10800
+ // If found any errors and no licenses, return first error.
10801
+ return $api_errors[0];
10802
+ }
10803
+
10804
+ // Fallback to empty licenses list.
10805
+ return $result;
10806
+ }
10807
+
10808
+ /**
10809
+ * @author Vova Feldman (@svovaf)
10810
+ * @since 1.2.0
10811
+ * @uses FS_Api
10812
+ *
10813
+ * @param number|bool $plugin_id
10814
+ * @param bool $flush
10815
+ *
10816
+ * @return FS_Payment[]|object
10817
+ */
10818
+ function _fetch_payments( $plugin_id = false, $flush = false ) {
10819
+ $this->_logger->entrance();
10820
+
10821
+ $api = $this->get_api_user_scope();
10822
+
10823
+ if ( ! is_numeric( $plugin_id ) ) {
10824
+ $plugin_id = $this->_plugin->id;
10825
+ }
10826
+
10827
+ $result = $api->get( "/plugins/{$plugin_id}/payments.json?include_addons=true", $flush );
10828
+
10829
+ if ( ! isset( $result->error ) ) {
10830
+ for ( $i = 0, $len = count( $result->payments ); $i < $len; $i ++ ) {
10831
+ $result->payments[ $i ] = new FS_Payment( $result->payments[ $i ] );
10832
+ }
10833
+ $result = $result->payments;
10834
+ }
10835
+
10836
+ return $result;
10837
+ }
10838
+
10839
+ /**
10840
+ * @author Vova Feldman (@svovaf)
10841
+ * @since 1.2.1.5
10842
+ * @uses FS_Api
10843
+ *
10844
+ * @param bool $flush
10845
+ *
10846
+ * @return \FS_Billing|mixed
10847
+ */
10848
+ function _fetch_billing( $flush = false ) {
10849
+ require_once WP_FS__DIR_INCLUDES . '/entities/class-fs-billing.php';
10850
+
10851
+ $billing = $this->get_api_user_scope()->get( 'billing.json', $flush );
10852
+
10853
+ if ( $this->is_api_result_entity( $billing ) ) {
10854
+ $billing = new FS_Billing( $billing );
10855
+ }
10856
+
10857
+ return $billing;
10858
+ }
10859
+
10860
+ /**
10861
+ * @author Vova Feldman (@svovaf)
10862
+ * @since 1.0.4
10863
+ *
10864
+ * @param FS_Plugin_Plan $plan
10865
+ * @param bool $store
10866
+ */
10867
+ private function _update_plan( $plan, $store = false ) {
10868
+ $this->_logger->entrance();
10869
+
10870
+ $this->_site->plan = $plan;
10871
+ $this->_store_site( $store );
10872
+ }
10873
+
10874
+ /**
10875
+ * @author Vova Feldman (@svovaf)
10876
+ * @since 1.0.5
10877
+ *
10878
+ * @param FS_Plugin_License[] $licenses
10879
+ * @param string|bool $plugin_slug
10880
+ */
10881
+ private function _update_licenses( $licenses, $plugin_slug = false ) {
10882
+ $this->_logger->entrance();
10883
+
10884
+ if ( is_array( $licenses ) ) {
10885
+ for ( $i = 0, $len = count( $licenses ); $i < $len; $i ++ ) {
10886
+ $licenses[ $i ]->updated = time();
10887
+ }
10888
+ }
10889
+
10890
+ if ( ! is_string( $plugin_slug ) ) {
10891
+ $this->_licenses = $licenses;
10892
+ }
10893
+
10894
+ $this->_store_licenses( true, $plugin_slug, $licenses );
10895
+ }
10896
+
10897
+ /**
10898
+ * @author Vova Feldman (@svovaf)
10899
+ * @since 1.0.4
10900
+ *
10901
+ * @param bool|number $plugin_id
10902
+ * @param bool $flush Since 1.1.7.3
10903
+ * @param int $expiration Since 1.2.2.7
10904
+ *
10905
+ * @return object|false New plugin tag info if exist.
10906
+ */
10907
+ private function _fetch_newer_version( $plugin_id = false, $flush = true, $expiration = WP_FS__TIME_24_HOURS_IN_SEC ) {
10908
+ $latest_tag = $this->_fetch_latest_version( $plugin_id, $flush, $expiration );
10909
+
10910
+ if ( ! is_object( $latest_tag ) ) {
10911
+ return false;
10912
+ }
10913
+
10914
+ // Check if version is actually newer.
10915
+ $has_new_version =
10916
+ // If it's an non-installed add-on then always return latest.
10917
+ ( $this->_is_addon_id( $plugin_id ) && ! $this->is_addon_activated( $plugin_id ) ) ||
10918
+ // Compare versions.
10919
+ version_compare( $this->get_plugin_version(), $latest_tag->version, '<' );
10920
+
10921
+ $this->_logger->departure( $has_new_version ? 'Found newer plugin version ' . $latest_tag->version : 'No new version' );
10922
+
10923
+ return $has_new_version ? $latest_tag : false;
10924
+ }
10925
+
10926
+ /**
10927
+ * @author Vova Feldman (@svovaf)
10928
+ * @since 1.0.5
10929
+ *
10930
+ * @param bool|number $plugin_id
10931
+ * @param bool $flush Since 1.1.7.3
10932
+ * @param int $expiration Since 1.2.2.7
10933
+ *
10934
+ * @return bool|FS_Plugin_Tag
10935
+ */
10936
+ function get_update( $plugin_id = false, $flush = true, $expiration = WP_FS__TIME_24_HOURS_IN_SEC ) {
10937
+ $this->_logger->entrance();
10938
+
10939
+ if ( ! is_numeric( $plugin_id ) ) {
10940
+ $plugin_id = $this->_plugin->id;
10941
+ }
10942
+
10943
+ $this->check_updates( true, $plugin_id, $flush, $expiration );
10944
+ $updates = $this->get_all_updates();
10945
+
10946
+ return isset( $updates[ $plugin_id ] ) && is_object( $updates[ $plugin_id ] ) ? $updates[ $plugin_id ] : false;
10947
+ }
10948
+
10949
+ /**
10950
+ * Check if site assigned with active license.
10951
+ *
10952
+ * @author Vova Feldman (@svovaf)
10953
+ * @since 1.0.6
10954
+ *
10955
+ * @deprecated Please use has_active_valid_license() instead because license can be cancelled.
10956
+ */
10957
+ function has_active_license() {
10958
+ return (
10959
+ is_object( $this->_license ) &&
10960
+ is_numeric( $this->_license->id ) &&
10961
+ ! $this->_license->is_expired()
10962
+ );
10963
+ }
10964
+
10965
+ /**
10966
+ * Check if site assigned with active & valid (not expired) license.
10967
+ *
10968
+ * @author Vova Feldman (@svovaf)
10969
+ * @since 1.2.1
10970
+ */
10971
+ function has_active_valid_license() {
10972
+ return (
10973
+ is_object( $this->_license ) &&
10974
+ is_numeric( $this->_license->id ) &&
10975
+ $this->_license->is_active() &&
10976
+ $this->_license->is_valid()
10977
+ );
10978
+ }
10979
+
10980
+ /**
10981
+ * Check if site assigned with license with enabled features.
10982
+ *
10983
+ * @author Vova Feldman (@svovaf)
10984
+ * @since 1.0.6
10985
+ *
10986
+ * @return bool
10987
+ */
10988
+ function has_features_enabled_license() {
10989
+ return (
10990
+ is_object( $this->_license ) &&
10991
+ is_numeric( $this->_license->id ) &&
10992
+ $this->_license->is_features_enabled()
10993
+ );
10994
+ }
10995
+
10996
+ /**
10997
+ * Check if user is a trial or have feature enabled license.
10998
+ *
10999
+ * @author Vova Feldman (@svovaf)
11000
+ * @since 1.1.7
11001
+ *
11002
+ * @return bool
11003
+ */
11004
+ function can_use_premium_code() {
11005
+ return $this->is_trial() || $this->has_features_enabled_license();
11006
+ }
11007
+
11008
+ /**
11009
+ * Checks if the current user can activate plugins or switch themes. Note that this method should only be used
11010
+ * after the `init` action is triggered because it is using `current_user_can()` which is only functional after
11011
+ * the context user is authenticated.
11012
+ *
11013
+ * @author Leo Fajardo (@leorw)
11014
+ * @since 1.2.2
11015
+ *
11016
+ * @return bool
11017
+ */
11018
+ function is_user_admin() {
11019
+ return ( $this->is_plugin() && current_user_can( 'activate_plugins' ) )
11020
+ || ( $this->is_theme() && current_user_can( 'switch_themes' ) );
11021
+ }
11022
+
11023
+ /**
11024
+ * Sync site's plan.
11025
+ *
11026
+ * @author Vova Feldman (@svovaf)
11027
+ * @since 1.0.3
11028
+ *
11029
+ * @uses FS_Api
11030
+ *
11031
+ * @param bool $background Hints the method if it's a background sync. If false, it means that was initiated by
11032
+ * the admin.
11033
+ */
11034
+ private function _sync_license( $background = false ) {
11035
+ $this->_logger->entrance();
11036
+
11037
+ $plugin_id = fs_request_get( 'plugin_id', $this->get_id() );
11038
+
11039
+ $is_addon_sync = ( ! $this->_plugin->is_addon() && $plugin_id != $this->get_id() );
11040
+
11041
+ if ( $is_addon_sync ) {
11042
+ $this->_sync_addon_license( $plugin_id, $background );
11043
+ } else {
11044
+ $this->_sync_plugin_license( $background );
11045
+ }
11046
+
11047
+ $this->do_action( 'after_account_plan_sync', $this->_site->plan->name );
11048
+ }
11049
+
11050
+ /**
11051
+ * Sync plugin's add-on license.
11052
+ *
11053
+ * @author Vova Feldman (@svovaf)
11054
+ * @since 1.0.6
11055
+ * @uses FS_Api
11056
+ *
11057
+ * @param number $addon_id
11058
+ * @param bool $background
11059
+ */
11060
+ private function _sync_addon_license( $addon_id, $background ) {
11061
+ $this->_logger->entrance();
11062
+
11063
+ if ( $this->is_addon_activated( $addon_id ) ) {
11064
+ // If already installed, use add-on sync.
11065
+ $fs_addon = self::get_instance_by_id( $addon_id );
11066
+ $fs_addon->_sync_license( $background );
11067
+
11068
+ return;
11069
+ }
11070
+
11071
+ // Validate add-on exists.
11072
+ $addon = $this->get_addon( $addon_id );
11073
+
11074
+ if ( ! is_object( $addon ) ) {
11075
+ return;
11076
+ }
11077
+
11078
+ // Add add-on into account add-ons.
11079
+ $account_addons = $this->get_account_addons();
11080
+ if ( ! is_array( $account_addons ) ) {
11081
+ $account_addons = array();
11082
+ }
11083
+ $account_addons[] = $addon->id;
11084
+ $account_addons = array_unique( $account_addons );
11085
+ $this->_store_account_addons( $account_addons );
11086
+
11087
+ // Load add-on licenses.
11088
+ $licenses = $this->_fetch_licenses( $addon->id );
11089
+
11090
+ // Sync add-on licenses.
11091
+ if ( $this->is_array_instanceof( $licenses, 'FS_Plugin_License' ) ) {
11092
+ $this->_update_licenses( $licenses, $addon->slug );
11093
+
11094
+ if ( ! $this->is_addon_installed( $addon->id ) && FS_License_Manager::has_premium_license( $licenses ) ) {
11095
+ $plans_result = $this->get_api_site_or_plugin_scope()->get( "/addons/{$addon_id}/plans.json" );
11096
+
11097
+ if ( ! isset( $plans_result->error ) ) {
11098
+ $plans = array();
11099
+ foreach ( $plans_result->plans as $plan ) {
11100
+ $plans[] = new FS_Plugin_Plan( $plan );
11101
+ }
11102
+
11103
+ $this->_admin_notices->add_sticky(
11104
+ sprintf(
11105
+ ( FS_Plan_Manager::instance()->has_free_plan( $plans ) ?
11106
+ $this->get_text_inline( 'Your %s Add-on plan was successfully upgraded.', 'addon-successfully-upgraded-message' ) :
11107
+ /* translators: %s:product name, e.g. Facebook add-on was successfully... */
11108
+ $this->get_text_inline( '%s Add-on was successfully purchased.', 'addon-successfully-purchased-message' ) ),
11109
+ $addon->title
11110
+ ) . ' ' . $this->get_latest_download_link(
11111
+ $this->get_text_inline( 'Download the latest version', 'download-latest-version' ),
11112
+ $addon_id
11113
+ ),
11114
+ 'addon_plan_upgraded_' . $addon->slug,
11115
+ $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
11116
+ );
11117
+ }
11118
+ }
11119
+ }
11120
+ }
11121
+
11122
+ /**
11123
+ * Sync site's plugin plan.
11124
+ *
11125
+ * @author Vova Feldman (@svovaf)
11126
+ * @since 1.0.6
11127
+ * @uses FS_Api
11128
+ *
11129
+ * @param bool $background Hints the method if it's a background sync. If false, it means that was initiated by
11130
+ * the admin.
11131
+ */
11132
+ private function _sync_plugin_license( $background = false ) {
11133
+ $this->_logger->entrance();
11134
+
11135
+ /**
11136
+ * Sync site info.
11137
+ *
11138
+ * @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.
11139
+ */
11140
+ $site = $this->send_install_update( array(), true );
11141
+
11142
+ $plan_change = 'none';
11143
+
11144
+ if ( ! $this->is_api_result_entity( $site ) ) {
11145
+ // Show API messages only if not background sync or if paying customer.
11146
+ if ( ! $background || $this->is_paying() ) {
11147
+ // Try to ping API to see if not blocked.
11148
+ if ( ! FS_Api::test() ) {
11149
+ /**
11150
+ * Failed to ping API - blocked!
11151
+ *
11152
+ * @author Vova Feldman (@svovaf)
11153
+ * @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.
11154
+ */
11155
+ $api = $this->get_api_site_scope();
11156
+
11157
+ if ( ! self::$_global_admin_notices->has_sticky( 'api_blocked' ) ) {
11158
+ self::$_global_admin_notices->add(
11159
+ sprintf(
11160
+ $this->get_text_x_inline( 'Your server is blocking the access to Freemius\' API, which is crucial for %1s synchronization. Please contact your host to whitelist %2s', '%1s - plugin title, %2s - API domain', 'server-blocking-access' ),
11161
+ $this->get_plugin_name(),
11162
+ '<a href="' . $api->get_url() . '" target="_blank">' . $api->get_url() . '</a>'
11163
+ ) . '<br> ' . $this->get_text_inline( 'Error received from the server:', 'server-error-message' ) . var_export( $site->error, true ),
11164
+ $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
11165
+ 'error',
11166
+ $background,
11167
+ false,
11168
+ 'api_blocked'
11169
+ );
11170
+ }
11171
+ } else {
11172
+ // Authentication params are broken.
11173
+ $this->_admin_notices->add(
11174
+ $this->get_text_inline( 'It seems like one of the authentication parameters is wrong. Update your Public Key, Secret Key & User ID, and try again.', 'wrong-authentication-param-message' ),
11175
+ $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
11176
+ 'error'
11177
+ );
11178
+ }
11179
+ }
11180
+
11181
+ // No reason to continue with license sync while there are API issues.
11182
+ return;
11183
+ }
11184
+
11185
+ // Remove sticky API connectivity message.
11186
+ self::$_global_admin_notices->remove_sticky( 'api_blocked' );
11187
+
11188
+ $site = new FS_Site( $site );
11189
+
11190
+ // Sync plans.
11191
+ $this->_sync_plans();
11192
+
11193
+ if ( ! $this->has_paid_plan() ) {
11194
+ $this->_site = $site;
11195
+ $this->_enrich_site_plan( true );
11196
+ $this->_store_site();
11197
+ } else {
11198
+ /**
11199
+ * Sync licenses. Pass the site's license ID so that the foreign licenses will be fetched if the license
11200
+ * associated with that ID is not included in the user's licenses collection.
11201
+ */
11202
+ $this->_sync_licenses( $site->license_id );
11203
+
11204
+ // Check if plan / license changed.
11205
+ if ( ! FS_Entity::equals( $site->plan, $this->_site->plan ) ||
11206
+ // Check if trial started.
11207
+ $site->trial_plan_id != $this->_site->trial_plan_id ||
11208
+ $site->trial_ends != $this->_site->trial_ends ||
11209
+ // Check if license changed.
11210
+ $site->license_id != $this->_site->license_id
11211
+ ) {
11212
+ if ( $site->is_trial() && ( ! $this->_site->is_trial() || $site->trial_ends != $this->_site->trial_ends ) ) {
11213
+ // New trial started.
11214
+ $this->_site = $site;
11215
+ $plan_change = 'trial_started';
11216
+
11217
+ // Store trial plan information.
11218
+ $this->_enrich_site_trial_plan( true );
11219
+
11220
+ // For trial with subscription use-case.
11221
+ $new_license = is_null( $site->license_id ) ? null : $this->_get_license_by_id( $site->license_id );
11222
+
11223
+ if ( is_object( $new_license ) && $new_license->is_valid() ) {
11224
+ $this->_site = $site;
11225
+ $this->_update_site_license( $new_license );
11226
+ $this->_store_licenses();
11227
+ $this->_enrich_site_plan( true );
11228
+
11229
+ $this->_sync_site_subscription( $this->_license );
11230
+ }
11231
+ } else if ( $this->_site->is_trial() && ! $site->is_trial() && ! is_numeric( $site->license_id ) ) {
11232
+ // Was in trial, but now trial expired and no license ID.
11233
+ // New trial started.
11234
+ $this->_site = $site;
11235
+ $plan_change = 'trial_expired';
11236
+
11237
+ // Clear trial plan information.
11238
+ $this->_storage->trial_plan = null;
11239
+
11240
+ } else {
11241
+ $is_free = $this->is_free_plan();
11242
+
11243
+ // Make sure license exist and not expired.
11244
+ $new_license = is_null( $site->license_id ) ?
11245
+ null :
11246
+ $this->_get_license_by_id( $site->license_id );
11247
+
11248
+ if ( $is_free && is_null( $new_license ) && $this->has_any_license() && $this->_license->is_cancelled ) {
11249
+ // License cancelled.
11250
+ $this->_site = $site;
11251
+ $this->_update_site_license( $new_license );
11252
+ $this->_store_licenses();
11253
+ $this->_enrich_site_plan( true );
11254
+
11255
+ $plan_change = 'cancelled';
11256
+ } else if ( $is_free && ( ( ! is_object( $new_license ) || $new_license->is_expired() ) ) ) {
11257
+ // The license is expired, so ignore upgrade method.
11258
+ } else {
11259
+ // License changed.
11260
+ $this->_site = $site;
11261
+ $this->_update_site_license( $new_license );
11262
+ $this->_store_licenses();
11263
+ $this->_enrich_site_plan( true );
11264
+
11265
+ $plan_change = $is_free ?
11266
+ 'upgraded' :
11267
+ ( is_object( $new_license ) ?
11268
+ 'changed' :
11269
+ 'downgraded' );
11270
+ }
11271
+ }
11272
+
11273
+ // Store updated site info.
11274
+ $this->_store_site();
11275
+ } else {
11276
+ if ( is_object( $this->_license ) && $this->_license->is_expired() ) {
11277
+ if ( ! $this->has_features_enabled_license() ) {
11278
+ $this->_deactivate_license();
11279
+ $plan_change = 'downgraded';
11280
+ } else {
11281
+ $plan_change = 'expired';
11282
+ }
11283
+ }
11284
+
11285
+ if ( is_numeric( $site->license_id ) && is_object( $this->_license ) ) {
11286
+ $this->_sync_site_subscription( $this->_license );
11287
+ }
11288
+ }
11289
+ }
11290
+
11291
+ $hmm_text = $this->get_text_x_inline( 'Hmm', 'something somebody says when they are thinking about what you have just said.', 'hmm' ) . '...';
11292
+
11293
+ if ( $this->has_paid_plan() ) {
11294
+ switch ( $plan_change ) {
11295
+ case 'none':
11296
+ if ( ! $background && is_admin() ) {
11297
+ $plan = $this->is_trial() ?
11298
+ $this->_storage->trial_plan :
11299
+ $this->_site->plan;
11300
+
11301
+ if ( $plan->is_free() ) {
11302
+ $this->_admin_notices->add(
11303
+ sprintf(
11304
+ $this->get_text_inline( 'It looks like you are still on the %s plan. If you did upgrade or change your plan, it\'s probably an issue on our side - sorry.', 'plan-did-not-change-message' ),
11305
+ '<i><b>' . $plan->title . ( $this->is_trial() ? ' ' . $this->get_text_x_inline( 'Trial', 'trial period', 'trial' ) : '' ) . '</b></i>'
11306
+ ) . ' ' . sprintf(
11307
+ '<a href="%s">%s</a>',
11308
+ $this->contact_url(
11309
+ 'bug',
11310
+ sprintf( $this->get_text_inline( 'I have upgraded my account but when I try to Sync the License, the plan remains %s.', 'plan-did-not-change-email-message' ),
11311
+ strtoupper( $plan->name )
11312
+ )
11313
+ ),
11314
+ $this->get_text_inline( 'Please contact us here', 'contact-us-here' )
11315
+ ),
11316
+ $hmm_text
11317
+ );
11318
+ }
11319
+ }
11320
+ break;
11321
+ case 'upgraded':
11322
+ $this->_admin_notices->add_sticky(
11323
+ sprintf(
11324
+ $this->get_text_inline( 'Your plan was successfully upgraded.', 'plan-upgraded-message' ),
11325
+ '<i>' . $this->get_plugin_name() . '</i>'
11326
+ ) . $this->get_complete_upgrade_instructions(),
11327
+ 'plan_upgraded',
11328
+ $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
11329
+ );
11330
+
11331
+ $this->_admin_notices->remove_sticky( array(
11332
+ 'trial_started',
11333
+ 'trial_promotion',
11334
+ 'trial_expired',
11335
+ 'activation_complete',
11336
+ ) );
11337
+ break;
11338
+ case 'changed':
11339
+ $this->_admin_notices->add_sticky(
11340
+ sprintf(
11341
+ $this->get_text_inline( 'Your plan was successfully changed to %s.', 'plan-changed-to-x-message' ),
11342
+ $this->_site->plan->title
11343
+ ),
11344
+ 'plan_changed'
11345
+ );
11346
+
11347
+ $this->_admin_notices->remove_sticky( array(
11348
+ 'trial_started',
11349
+ 'trial_promotion',
11350
+ 'trial_expired',
11351
+ 'activation_complete',
11352
+ ) );
11353
+ break;
11354
+ case 'downgraded':
11355
+ $this->_admin_notices->add_sticky(
11356
+ sprintf( $this->get_text_inline( 'Your license has expired. You can still continue using the free %s forever.', 'license-expired-blocking-message' ), $this->_module_type ),
11357
+ 'license_expired',
11358
+ $hmm_text
11359
+ );
11360
+ $this->_admin_notices->remove_sticky( 'plan_upgraded' );
11361
+ break;
11362
+ case 'cancelled':
11363
+ $this->_admin_notices->add(
11364
+ $this->get_text_inline( 'Your license has been cancelled. If you think it\'s a mistake, please contact support.', 'license-cancelled' ) . ' ' .
11365
+ sprintf(
11366
+ '<a href="%s">%s</a>',
11367
+ $this->contact_url( 'bug' ),
11368
+ $this->get_text_inline( 'Please contact us here', 'contact-us-here' )
11369
+ ),
11370
+ $hmm_text,
11371
+ 'error'
11372
+ );
11373
+ $this->_admin_notices->remove_sticky( 'plan_upgraded' );
11374
+ break;
11375
+ case 'expired':
11376
+ $this->_admin_notices->add_sticky(
11377
+ sprintf( $this->get_text_inline( 'Your license has expired. You can still continue using all the %s features, but you\'ll need to renew your license to continue getting updates and support.', 'license-expired-non-blocking-message' ), $this->_site->plan->title ),
11378
+ 'license_expired',
11379
+ $hmm_text
11380
+ );
11381
+ $this->_admin_notices->remove_sticky( 'plan_upgraded' );
11382
+ break;
11383
+ case 'trial_started':
11384
+ $this->_admin_notices->add_sticky(
11385
+ sprintf(
11386
+ $this->get_text_inline( 'Your trial has been successfully started.', 'trial-started-message' ),
11387
+ '<i>' . $this->get_plugin_name() . '</i>'
11388
+ ) . $this->get_complete_upgrade_instructions( $this->_storage->trial_plan->title ),
11389
+ 'trial_started',
11390
+ $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
11391
+ );
11392
+
11393
+ $this->_admin_notices->remove_sticky( array(
11394
+ 'trial_promotion',
11395
+ ) );
11396
+ break;
11397
+ case 'trial_expired':
11398
+ $this->_admin_notices->add_sticky(
11399
+ $this->get_text_inline( 'Your trial has expired. You can still continue using all our free features.', 'trial-expired-message' ),
11400
+ 'trial_expired',
11401
+ $hmm_text
11402
+ );
11403
+ $this->_admin_notices->remove_sticky( array(
11404
+ 'trial_started',
11405
+ 'trial_promotion',
11406
+ 'plan_upgraded',
11407
+ ) );
11408
+ break;
11409
+ }
11410
+ }
11411
+
11412
+ if ( 'none' !== $plan_change ) {
11413
+ $this->do_action( 'after_license_change', $plan_change, $this->_site->plan );
11414
+ }
11415
+ }
11416
+
11417
+ /**
11418
+ * @author Vova Feldman (@svovaf)
11419
+ * @since 1.0.5
11420
+ *
11421
+ * @param bool $background
11422
+ */
11423
+ protected function _activate_license( $background = false ) {
11424
+ $this->_logger->entrance();
11425
+
11426
+ $license_id = fs_request_get( 'license_id' );
11427
+
11428
+ if ( FS_Plugin_License::is_valid_id( $license_id ) && $license_id == $this->_site->license_id ) {
11429
+ // License is already activated.
11430
+ return;
11431
+ }
11432
+
11433
+ $premium_license = FS_Plugin_License::is_valid_id( $license_id ) ?
11434
+ $this->_get_license_by_id( $license_id ) :
11435
+ $this->_get_available_premium_license();
11436
+
11437
+ if ( ! is_object( $premium_license ) ) {
11438
+ return;
11439
+ }
11440
+
11441
+ /**
11442
+ * If the premium license is already associated with the install, just
11443
+ * update the license reference (activation is not required).
11444
+ *
11445
+ * @since 1.1.9
11446
+ */
11447
+ if ( $premium_license->id == $this->_site->license_id ) {
11448
+ // License is already activated.
11449
+ $this->_update_site_license( $premium_license );
11450
+ $this->_enrich_site_plan( false );
11451
+ $this->_store_account();
11452
+
11453
+ return;
11454
+ }
11455
+
11456
+ if ( $this->_site->user_id != $premium_license->user_id ) {
11457
+ $api_request_params = array( 'license_key' => $premium_license->secret_key );
11458
+ } else {
11459
+ $api_request_params = array();
11460
+ }
11461
+
11462
+ $api = $this->get_api_site_scope();
11463
+ $license = $api->call( "/licenses/{$premium_license->id}.json", 'put', $api_request_params );
11464
+
11465
+ if ( ! $this->is_api_result_entity( $license ) ) {
11466
+ if ( ! $background ) {
11467
+ $this->_admin_notices->add( sprintf(
11468
+ '%s %s',
11469
+ $this->get_text_inline( 'It looks like the license could not be activated.', 'license-activation-failed-message' ),
11470
+ ( is_object( $license ) && isset( $license->error ) ?
11471
+ $license->error->message :
11472
+ sprintf( '%s<br><code>%s</code>',
11473
+ $this->get_text_inline( 'Error received from the server:', 'server-error-message' ),
11474
+ var_export( $license, true )
11475
+ )
11476
+ )
11477
+ ),
11478
+ $this->get_text_x_inline( 'Hmm', 'something somebody says when they are thinking about what you have just said.', 'hmm' ) . '...',
11479
+ 'error'
11480
+ );
11481
+ }
11482
+
11483
+ return;
11484
+ }
11485
+ $premium_license = new FS_Plugin_License( $license );
11486
+
11487
+ // Updated site plan.
11488
+ $site = $this->get_api_site_scope()->get( '/', true );
11489
+ if ( $this->is_api_result_entity( $site ) ) {
11490
+ $this->_site = new FS_Site( $site );
11491
+ }
11492
+ $this->_update_site_license( $premium_license );
11493
+ $this->_enrich_site_plan( false );
11494
+
11495
+ $this->_store_account();
11496
+
11497
+ if ( ! $background ) {
11498
+ $this->_admin_notices->add_sticky(
11499
+ $this->get_text_inline( 'Your license was successfully activated.', 'license-activated-message' ) .
11500
+ $this->get_complete_upgrade_instructions(),
11501
+ 'license_activated',
11502
+ $this->get_text_x_inline( 'Yee-haw', 'interjection expressing joy or exuberance', 'yee-haw' ) . '!'
11503
+ );
11504
+ }
11505
+
11506
+ $this->_admin_notices->remove_sticky( array(
11507
+ 'trial_promotion',
11508
+ 'license_expired',
11509
+ ) );
11510
+ }
11511
+
11512
+ /**
11513
+ * @author Vova Feldman (@svovaf)
11514
+ * @since 1.0.5
11515
+ *
11516
+ * @param bool $show_notice
11517
+ */
11518
+ protected function _deactivate_license( $show_notice = true ) {
11519
+ $this->_logger->entrance();
11520
+
11521
+ $hmm_text = $this->get_text_x_inline( 'Hmm', 'something somebody says when they are thinking about what you have just said.', 'hmm' ) . '...';
11522
+
11523
+ if ( ! is_object( $this->_license ) ) {
11524
+ $this->_admin_notices->add(
11525
+ sprintf( $this->get_text_inline( 'It looks like your site currently doesn\'t have an active license.', 'no-active-license-message' ), $this->_site->plan->title ),
11526
+ $hmm_text
11527
+ );
11528
+
11529
+ return;
11530
+ }
11531
+
11532
+ $api = $this->get_api_site_scope();
11533
+ $license = $api->call( "/licenses/{$this->_site->license_id}.json", 'delete' );
11534
+
11535
+ if ( isset( $license->error ) ) {
11536
+ $this->_admin_notices->add(
11537
+ $this->get_text_inline( 'It looks like the license deactivation failed.', 'license-deactivation-failed-message' ) . '<br> ' .
11538
+ $this->get_text_inline( 'Error received from the server:', 'server-error-message' ) . ' ' . var_export( $license->error, true ),
11539
+ $hmm_text,
11540
+ 'error'
11541
+ );
11542
+
11543
+ return;
11544
+ }
11545
+
11546
+ // Update license cache.
11547
+ for ( $i = 0, $len = count( $this->_licenses ); $i < $len; $i ++ ) {
11548
+ if ( $license->id == $this->_licenses[ $i ]->id ) {
11549
+ $this->_licenses[ $i ] = new FS_Plugin_License( $license );
11550
+ }
11551
+ }
11552
+
11553
+ // Updated site plan to default.
11554
+ $this->_sync_plans();
11555
+ $this->_site->plan->id = $this->_plans[0]->id;
11556
+ // Unlink license from site.
11557
+ $this->_update_site_license( null );
11558
+ $this->_enrich_site_plan( false );
11559
+
11560
+ $this->_store_account();
11561
+
11562
+ if ( $show_notice ) {
11563
+ $this->_admin_notices->add(
11564
+ sprintf( $this->get_text_inline( 'Your license was successfully deactivated, you are back to the %s plan.', 'license-deactivation-message' ), $this->_site->plan->title ),
11565
+ $this->get_text_inline( 'O.K', 'ok' )
11566
+ );
11567
+ }
11568
+
11569
+ $this->_admin_notices->remove_sticky( array(
11570
+ 'plan_upgraded',
11571
+ 'license_activated',
11572
+ ) );
11573
+ }
11574
+
11575
+ /**
11576
+ * Site plan downgrade.
11577
+ *
11578
+ * @author Vova Feldman (@svovaf)
11579
+ * @since 1.0.4
11580
+ *
11581
+ * @uses FS_Api
11582
+ */
11583
+ private function _downgrade_site() {
11584
+ $this->_logger->entrance();
11585
+
11586
+ $api = $this->get_api_site_scope();
11587
+ $site = $api->call( 'downgrade.json', 'put' );
11588
+
11589
+ $plan_downgraded = false;
11590
+ $plan = false;
11591
+ if ( $this->is_api_result_entity( $site ) ) {
11592
+ $prev_plan_id = $this->_site->plan->id;
11593
+
11594
+ // Update new site plan id.
11595
+ $this->_site->plan->id = $site->plan_id;
11596
+
11597
+ $plan = $this->_enrich_site_plan();
11598
+ $subscription = $this->_sync_site_subscription( $this->_license );
11599
+
11600
+ // Plan downgraded if plan was changed or subscription was cancelled.
11601
+ $plan_downgraded = ( $plan instanceof FS_Plugin_Plan && $prev_plan_id != $plan->id ) ||
11602
+ ( is_object( $subscription ) && ! isset( $subscription->error ) && ! $subscription->is_active() );
11603
+ } else {
11604
+ // handle different error cases.
11605
+
11606
+ }
11607
+
11608
+ if ( $plan_downgraded ) {
11609
+ // Remove previous sticky message about upgrade (if exist).
11610
+ $this->_admin_notices->remove_sticky( 'plan_upgraded' );
11611
+
11612
+ $this->_admin_notices->add(
11613
+ sprintf( $this->get_text_inline( 'Your plan was successfully downgraded. Your %s plan license will expire in %s.', 'plan-x-downgraded-message' ),
11614
+ $plan->title,
11615
+ human_time_diff( time(), strtotime( $this->_license->expiration ) )
11616
+ )
11617
+ );
11618
+
11619
+ // Store site updates.
11620
+ $this->_store_site();
11621
+ } else {
11622
+ $this->_admin_notices->add(
11623
+ $this->get_text_inline( 'Seems like we are having some temporary issue with your plan downgrade. Please try again in few minutes.', 'plan-downgraded-failure-message' ),
11624
+ $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...',
11625
+ 'error'
11626
+ );
11627
+ }
11628
+ }
11629
+
11630
+ /**
11631
+ * @author Vova Feldman (@svovaf)
11632
+ * @since 1.1.8.1
11633
+ *
11634
+ * @param bool|string $plan_name
11635
+ *
11636
+ * @return bool If trial was successfully started.
11637
+ */
11638
+ function start_trial( $plan_name = false ) {
11639
+ $this->_logger->entrance();
11640
+
11641
+ // Alias.
11642
+ $oops_text = $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...';
11643
+
11644
+ if ( $this->is_trial() ) {
11645
+ // Already in trial mode.
11646
+ $this->_admin_notices->add(
11647
+ sprintf( $this->get_text_inline( 'You are already running the %s in a trial mode.', 'in-trial-mode' ), $this->_module_type ),
11648
+ $oops_text,
11649
+ 'error'
11650
+ );
11651
+
11652
+ return false;
11653
+ }
11654
+
11655
+ if ( $this->_site->is_trial_utilized() ) {
11656
+ // Trial was already utilized.
11657
+ $this->_admin_notices->add(
11658
+ $this->get_text_inline( 'You already utilized a trial before.', 'trial-utilized' ),
11659
+ $oops_text,
11660
+ 'error'
11661
+ );
11662
+
11663
+ return false;
11664
+ }
11665
+
11666
+ if ( false !== $plan_name ) {
11667
+ $plan = $this->get_plan_by_name( $plan_name );
11668
+
11669
+ if ( false === $plan ) {
11670
+ // Plan doesn't exist.
11671
+ $this->_admin_notices->add(
11672
+ sprintf( $this->get_text_inline( 'Plan %s do not exist, therefore, can\'t start a trial.', 'trial-plan-x-not-exist' ), $plan_name ),
11673
+ $oops_text,
11674
+ 'error'
11675
+ );
11676
+
11677
+ return false;
11678
+ }
11679
+
11680
+ if ( ! $plan->has_trial() ) {
11681
+ // Plan doesn't exist.
11682
+ $this->_admin_notices->add(
11683
+ sprintf( $this->get_text_inline( 'Plan %s does not support a trial period.', 'plan-x-no-trial' ), $plan_name ),
11684
+ $oops_text,
11685
+ 'error'
11686
+ );
11687
+
11688
+ return false;
11689
+ }
11690
+ } else {
11691
+ if ( ! $this->has_trial_plan() ) {
11692
+ // None of the plans have a trial.
11693
+ $this->_admin_notices->add(
11694
+ sprintf( $this->get_text_inline( 'None of the %s\'s plans supports a trial period.', 'no-trials' ), $this->_module_type ),
11695
+ $oops_text,
11696
+ 'error'
11697
+ );
11698
+
11699
+ return false;
11700
+ }
11701
+
11702
+ $plans_with_trial = FS_Plan_Manager::instance()->get_trial_plans( $this->_plans );
11703
+
11704
+ $plan = $plans_with_trial[0];
11705
+ }
11706
+
11707
+ $api = $this->get_api_site_scope();
11708
+ $plan = $api->call( "plans/{$plan->id}/trials.json", 'post' );
11709
+
11710
+ if ( ! $this->is_api_result_entity( $plan ) ) {
11711
+ // Some API error while trying to start the trial.
11712
+ $this->_admin_notices->add(
11713
+ sprintf( $this->get_text_inline( 'Unexpected API error. Please contact the %s\'s author with the following error.', 'unexpected-api-error' ), $this->_module_type )
11714
+ . ' ' . var_export( $plan, true ),
11715
+ $oops_text,
11716
+ 'error'
11717
+ );
11718
+
11719
+ return false;
11720
+ }
11721
+
11722
+ // Sync license.
11723
+ $this->_sync_license();
11724
+
11725
+ return $this->is_trial();
11726
+ }
11727
+
11728
+ /**
11729
+ * Cancel site trial.
11730
+ *
11731
+ * @author Vova Feldman (@svovaf)
11732
+ * @since 1.0.9
11733
+ *
11734
+ * @uses FS_Api
11735
+ */
11736
+ private function _cancel_trial() {
11737
+ $this->_logger->entrance();
11738
+
11739
+ // Alias.
11740
+ $oops_text = $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...';
11741
+
11742
+ if ( ! $this->is_trial() ) {
11743
+ $this->_admin_notices->add(
11744
+ $this->get_text_inline( 'It looks like you are not in trial mode anymore so there\'s nothing to cancel :)', 'trial-cancel-no-trial-message' ),
11745
+ $oops_text,
11746
+ 'error'
11747
+ );
11748
+
11749
+ return;
11750
+ }
11751
+
11752
+ $api = $this->get_api_site_scope();
11753
+ $site = $api->call( 'trials.json', 'delete' );
11754
+
11755
+ $trial_cancelled = false;
11756
+
11757
+ if ( $this->is_api_result_entity( $site ) ) {
11758
+ $prev_trial_ends = $this->_site->trial_ends;
11759
+
11760
+ if ( $this->is_paid_trial() ) {
11761
+ $this->_license->expiration = $site->trial_ends;
11762
+ $this->_license->is_cancelled = true;
11763
+ $this->_update_site_license( $this->_license );
11764
+ $this->_store_licenses();
11765
+
11766
+ // Clear subscription reference.
11767
+ $this->_sync_site_subscription( null );
11768
+ }
11769
+
11770
+ // Update site info.
11771
+ $this->_site = new FS_Site( $site );
11772
+ $this->_enrich_site_plan();
11773
+
11774
+ $trial_cancelled = ( $prev_trial_ends != $site->trial_ends );
11775
+ } else {
11776
+ // handle different error cases.
11777
+
11778
+ }
11779
+
11780
+ if ( $trial_cancelled ) {
11781
+ // Remove previous sticky messages about upgrade or trial (if exist).
11782
+ $this->_admin_notices->remove_sticky( array(
11783
+ 'trial_started',
11784
+ 'trial_promotion',
11785
+ 'plan_upgraded',
11786
+ ) );
11787
+
11788
+ // Store site updates.
11789
+ $this->_store_site();
11790
+
11791
+ if ( ! $this->is_addon() ||
11792
+ ! $this->deactivate_premium_only_addon_without_license( true )
11793
+ ) {
11794
+ $this->_admin_notices->add(
11795
+ sprintf( $this->get_text_inline( 'Your %s free trial was successfully cancelled.', 'trial-cancel-message' ), $this->_storage->trial_plan->title )
11796
+ );
11797
+ }
11798
+
11799
+ // Clear trial plan information.
11800
+ unset( $this->_storage->trial_plan );
11801
+ } else {
11802
+ $this->_admin_notices->add(
11803
+ $this->get_text_inline( 'Seems like we are having some temporary issue with your trial cancellation. Please try again in few minutes.', 'trial-cancel-failure-message' ),
11804
+ $oops_text,
11805
+ 'error'
11806
+ );
11807
+ }
11808
+ }
11809
+
11810
+ /**
11811
+ * @author Vova Feldman (@svovaf)
11812
+ * @since 1.0.6
11813
+ *
11814
+ * @param bool|number $plugin_id
11815
+ *
11816
+ * @return bool
11817
+ */
11818
+ private function _is_addon_id( $plugin_id ) {
11819
+ return is_numeric( $plugin_id ) && ( $this->get_id() != $plugin_id );
11820
+ }
11821
+
11822
+ /**
11823
+ * Check if user eligible to download premium version updates.
11824
+ *
11825
+ * @author Vova Feldman (@svovaf)
11826
+ * @since 1.0.6
11827
+ *
11828
+ * @return bool
11829
+ */
11830
+ private function _can_download_premium() {
11831
+ return $this->has_active_valid_license() ||
11832
+ ( $this->is_trial() && ! $this->get_trial_plan()->is_free() );
11833
+ }
11834
+
11835
+ /**
11836
+ *
11837
+ * @author Vova Feldman (@svovaf)
11838
+ * @since 1.0.6
11839
+ *
11840
+ * @param bool|number $addon_id
11841
+ * @param string $type "json" or "zip"
11842
+ *
11843
+ * @return string
11844
+ */
11845
+ private function _get_latest_version_endpoint( $addon_id = false, $type = 'json' ) {
11846
+
11847
+ $is_addon = $this->_is_addon_id( $addon_id );
11848
+
11849
+ $is_premium = null;
11850
+ if ( ! $is_addon ) {
11851
+ $is_premium = $this->_can_download_premium();
11852
+ } else if ( $this->is_addon_activated( $addon_id ) ) {
11853
+ $is_premium = self::get_instance_by_id( $addon_id )->_can_download_premium();
11854
+ }
11855
+
11856
+ // If add-on, then append add-on ID.
11857
+ $endpoint = ( $is_addon ? "/addons/$addon_id" : '' ) .
11858
+ '/updates/latest.' . $type;
11859
+
11860
+ // If add-on and not yet activated, try to fetch based on server licensing.
11861
+ if ( is_bool( $is_premium ) ) {
11862
+ $endpoint = add_query_arg( 'is_premium', json_encode( $is_premium ), $endpoint );
11863
+ }
11864
+
11865
+ if ( $this->has_secret_key() ) {
11866
+ $endpoint = add_query_arg( 'type', 'all', $endpoint );
11867
+ }
11868
+
11869
+ return $endpoint;
11870
+ }
11871
+
11872
+ /**
11873
+ * @author Vova Feldman (@svovaf)
11874
+ * @since 1.0.4
11875
+ *
11876
+ * @param bool|number $addon_id
11877
+ * @param bool $flush Since 1.1.7.3
11878
+ * @param int $expiration Since 1.2.2.7
11879
+ *
11880
+ * @return object|false Plugin latest tag info.
11881
+ */
11882
+ function _fetch_latest_version( $addon_id = false, $flush = true, $expiration = WP_FS__TIME_24_HOURS_IN_SEC ) {
11883
+ $this->_logger->entrance();
11884
+
11885
+ /**
11886
+ * @since 1.1.7.3 Check for plugin updates from Freemius only if opted-in.
11887
+ * @since 1.1.7.4 Also check updates for add-ons.
11888
+ */
11889
+ if ( ! $this->is_registered() &&
11890
+ ! $this->_is_addon_id( $addon_id )
11891
+ ) {
11892
+ return false;
11893
+ }
11894
+
11895
+ $tag = $this->get_api_site_or_plugin_scope()->get(
11896
+ $this->_get_latest_version_endpoint( $addon_id, 'json' ),
11897
+ $flush,
11898
+ $expiration
11899
+ );
11900
+
11901
+ $latest_version = ( is_object( $tag ) && isset( $tag->version ) ) ? $tag->version : 'couldn\'t get';
11902
+
11903
+ $this->_logger->departure( 'Latest version ' . $latest_version );
11904
+
11905
+ return ( is_object( $tag ) && isset( $tag->version ) ) ? $tag : false;
11906
+ }
11907
+
11908
+ #----------------------------------------------------------------------------------
11909
+ #region Download Plugin
11910
+ #----------------------------------------------------------------------------------
11911
+
11912
+ /**
11913
+ * Download latest plugin version, based on plan.
11914
+ *
11915
+ * Not like _download_latest(), this will redirect the page
11916
+ * to secure download url to prevent dual download (from FS to WP server,
11917
+ * and then from WP server to the client / browser).
11918
+ *
11919
+ * @author Vova Feldman (@svovaf)
11920
+ * @since 1.0.9
11921
+ *
11922
+ * @param bool|number $plugin_id
11923
+ *
11924
+ * @uses FS_Api
11925
+ * @uses wp_redirect()
11926
+ */
11927
+ private function download_latest_directly( $plugin_id = false ) {
11928
+ $this->_logger->entrance();
11929
+
11930
+ wp_redirect( $this->get_latest_download_api_url( $plugin_id ) );
11931
+ }
11932
+
11933
+ /**
11934
+ * Get latest plugin FS API download URL.
11935
+ *
11936
+ * @author Vova Feldman (@svovaf)
11937
+ * @since 1.0.9
11938
+ *
11939
+ * @param bool|number $plugin_id
11940
+ *
11941
+ * @return string
11942
+ */
11943
+ private function get_latest_download_api_url( $plugin_id = false ) {
11944
+ $this->_logger->entrance();
11945
+
11946
+ return $this->get_api_site_scope()->get_signed_url(
11947
+ $this->_get_latest_version_endpoint( $plugin_id, 'zip' )
11948
+ );
11949
+ }
11950
+
11951
+ /**
11952
+ * Get payment invoice URL.
11953
+ *
11954
+ * @author Vova Feldman (@svovaf)
11955
+ * @since 1.2.0
11956
+ *
11957
+ * @param bool|number $payment_id
11958
+ *
11959
+ * @return string
11960
+ */
11961
+ function _get_invoice_api_url( $payment_id = false ) {
11962
+ $this->_logger->entrance();
11963
+
11964
+ return $this->get_api_user_scope()->get_signed_url(
11965
+ "/payments/{$payment_id}/invoice.pdf"
11966
+ );
11967
+ }
11968
+
11969
+ /**
11970
+ * Get latest plugin download link.
11971
+ *
11972
+ * @author Vova Feldman (@svovaf)
11973
+ * @since 1.0.9
11974
+ *
11975
+ * @param string $label
11976
+ * @param bool|number $plugin_id
11977
+ *
11978
+ * @return string
11979
+ */
11980
+ private function get_latest_download_link( $label, $plugin_id = false ) {
11981
+ return sprintf(
11982
+ '<a target="_blank" href="%s">%s</a>',
11983
+ $this->_get_latest_download_local_url( $plugin_id ),
11984
+ $label
11985
+ );
11986
+ }
11987
+
11988
+ /**
11989
+ * Get latest plugin download local URL.
11990
+ *
11991
+ * @author Vova Feldman (@svovaf)
11992
+ * @since 1.0.9
11993
+ *
11994
+ * @param bool|number $plugin_id
11995
+ *
11996
+ * @return string
11997
+ */
11998
+ function _get_latest_download_local_url( $plugin_id = false ) {
11999
+ // Add timestamp to protect from caching.
12000
+ $params = array( 'ts' => WP_FS__SCRIPT_START_TIME );
12001
+
12002
+ if ( ! empty( $plugin_id ) ) {
12003
+ $params['plugin_id'] = $plugin_id;
12004
+ }
12005
+
12006
+ return $this->get_account_url( 'download_latest', $params );
12007
+ }
12008
+
12009
+ #endregion Download Plugin ------------------------------------------------------------------
12010
+
12011
+ /**
12012
+ * @author Vova Feldman (@svovaf)
12013
+ * @since 1.0.4
12014
+ *
12015
+ * @uses FS_Api
12016
+ *
12017
+ * @param bool $background Hints the method if it's a background updates check. If false, it means that
12018
+ * was initiated by the admin.
12019
+ * @param bool|number $plugin_id
12020
+ * @param bool $flush Since 1.1.7.3
12021
+ * @param int $expiration Since 1.2.2.7
12022
+ */
12023
+ private function check_updates(
12024
+ $background = false,
12025
+ $plugin_id = false,
12026
+ $flush = true,
12027
+ $expiration = WP_FS__TIME_24_HOURS_IN_SEC
12028
+ ) {
12029
+ $this->_logger->entrance();
12030
+
12031
+ // Check if there's a newer version for download.
12032
+ $new_version = $this->_fetch_newer_version( $plugin_id, $flush, $expiration );
12033
+
12034
+ $update = null;
12035
+ if ( is_object( $new_version ) ) {
12036
+ $update = new FS_Plugin_Tag( $new_version );
12037
+
12038
+ if ( ! $background ) {
12039
+ $this->_admin_notices->add(
12040
+ sprintf(
12041
+ /* translators: %s: Numeric version number (e.g. '2.1.9' */
12042
+ $this->get_text_inline( 'Version %s was released.', 'version-x-released' ) . ' ' . $this->get_text_inline( 'Please download %s.', 'please-download-x' ),
12043
+ $update->version,
12044
+ sprintf(
12045
+ '<a href="%s" target="_blank">%s</a>',
12046
+ $this->get_account_url( 'download_latest' ),
12047
+ sprintf(
12048
+ /* translators: %s: plan name (e.g. latest "Professional" version) */
12049
+ $this->get_text_inline( 'the latest %s version here', 'latest-x-version' ),
12050
+ $this->_site->plan->title
12051
+ )
12052
+ )
12053
+ ),
12054
+ $this->get_text_inline( 'New', 'new' ) . '!'
12055
+ );
12056
+ }
12057
+ } else if ( false === $new_version && ! $background ) {
12058
+ $this->_admin_notices->add(
12059
+ $this->get_text_inline( 'Seems like you got the latest release.', 'you-have-latest' ),
12060
+ $this->get_text_inline( 'You are all good!', 'you-are-good' )
12061
+ );
12062
+ }
12063
+
12064
+ $this->_store_update( $update, true, $plugin_id );
12065
+ }
12066
+
12067
+ /**
12068
+ * @author Vova Feldman (@svovaf)
12069
+ * @since 1.0.4
12070
+ *
12071
+ * @param bool $flush Since 1.1.7.3 add 24 hour cache by default.
12072
+ *
12073
+ * @return FS_Plugin[]
12074
+ *
12075
+ * @uses FS_Api
12076
+ */
12077
+ private function sync_addons( $flush = false ) {
12078
+ $this->_logger->entrance();
12079
+
12080
+ $api = $this->get_api_site_or_plugin_scope();
12081
+
12082
+ /**
12083
+ * @since 1.2.1
12084
+ *
12085
+ * If there's a cached version of the add-ons and not asking
12086
+ * for a flush, just use the currently stored add-ons.
12087
+ */
12088
+ if ( ! $flush && $api->is_cached( '/addons.json?enriched=true' ) ) {
12089
+ $addons = self::get_all_addons();
12090
+
12091
+ return $addons[ $this->_plugin->id ];
12092
+ }
12093
+
12094
+ $result = $api->get( '/addons.json?enriched=true', $flush );
12095
+
12096
+ $addons = array();
12097
+ if ( $this->is_api_result_object( $result, 'plugins' ) &&
12098
+ is_array( $result->plugins )
12099
+ ) {
12100
+ for ( $i = 0, $len = count( $result->plugins ); $i < $len; $i ++ ) {
12101
+ $addons[ $i ] = new FS_Plugin( $result->plugins[ $i ] );
12102
+ }
12103
+
12104
+ $this->_store_addons( $addons, true );
12105
+ }
12106
+
12107
+ return $addons;
12108
+ }
12109
+
12110
+ /**
12111
+ * Handle user email update.
12112
+ *
12113
+ * @author Vova Feldman (@svovaf)
12114
+ * @since 1.0.3
12115
+ * @uses FS_Api
12116
+ *
12117
+ * @param string $new_email
12118
+ *
12119
+ * @return object
12120
+ */
12121
+ private function update_email( $new_email ) {
12122
+ $this->_logger->entrance();
12123
+
12124
+
12125
+ $api = $this->get_api_user_scope();
12126
+ $user = $api->call( "?plugin_id={$this->_plugin->id}&fields=id,email,is_verified", 'put', array(
12127
+ 'email' => $new_email,
12128
+ 'after_email_confirm_url' => $this->_get_admin_page_url(
12129
+ 'account',
12130
+ array( 'fs_action' => 'sync_user' )
12131
+ ),
12132
+ ) );
12133
+
12134
+ if ( ! isset( $user->error ) ) {
12135
+ $this->_user->email = $user->email;
12136
+ $this->_user->is_verified = $user->is_verified;
12137
+ $this->_store_user();
12138
+ } else {
12139
+ // handle different error cases.
12140
+
12141
+ }
12142
+
12143
+ return $user;
12144
+ }
12145
+
12146
+ #----------------------------------------------------------------------------------
12147
+ #region API Error Handling
12148
+ #----------------------------------------------------------------------------------
12149
+
12150
+ /**
12151
+ * @author Vova Feldman (@svovaf)
12152
+ * @since 1.1.1
12153
+ *
12154
+ * @param mixed $result
12155
+ *
12156
+ * @return bool Is API result contains an error.
12157
+ */
12158
+ private function is_api_error( $result ) {
12159
+ return FS_Api::is_api_error( $result );
12160
+ }
12161
+
12162
+ /**
12163
+ * Checks if given API result is a non-empty and not an error object.
12164
+ *
12165
+ * @author Vova Feldman (@svovaf)
12166
+ * @since 1.2.1.5
12167
+ *
12168
+ * @param mixed $result
12169
+ * @param string|null $required_property Optional property we want to verify that is set.
12170
+ *
12171
+ * @return bool
12172
+ */
12173
+ function is_api_result_object( $result, $required_property = null ) {
12174
+ return FS_Api::is_api_result_object( $result, $required_property );
12175
+ }
12176
+
12177
+ /**
12178
+ * Checks if given API result is a non-empty entity object with non-empty ID.
12179
+ *
12180
+ * @author Vova Feldman (@svovaf)
12181
+ * @since 1.2.1.5
12182
+ *
12183
+ * @param mixed $result
12184
+ *
12185
+ * @return bool
12186
+ */
12187
+ private function is_api_result_entity( $result ) {
12188
+ return FS_Api::is_api_result_entity( $result );
12189
+ }
12190
+
12191
+ #endregion
12192
+
12193
+ /**
12194
+ * Make sure a given argument is an array of a specific type.
12195
+ *
12196
+ * @author Vova Feldman (@svovaf)
12197
+ * @since 1.2.1.5
12198
+ *
12199
+ * @param mixed $array
12200
+ * @param string $class
12201
+ *
12202
+ * @return bool
12203
+ */
12204
+ private function is_array_instanceof( $array, $class ) {
12205
+ return ( is_array( $array ) && ( empty( $array ) || $array[0] instanceof $class ) );
12206
+ }
12207
+
12208
+ /**
12209
+ * Start install ownership change.
12210
+ *
12211
+ * @author Vova Feldman (@svovaf)
12212
+ * @since 1.1.1
12213
+ * @uses FS_Api
12214
+ *
12215
+ * @param string $candidate_email
12216
+ *
12217
+ * @return bool Is ownership change successfully initiated.
12218
+ */
12219
+ private function init_change_owner( $candidate_email ) {
12220
+ $this->_logger->entrance();
12221
+
12222
+ $api = $this->get_api_site_scope();
12223
+ $result = $api->call( "/users/{$this->_user->id}.json", 'put', array(
12224
+ 'email' => $candidate_email,
12225
+ 'after_confirm_url' => $this->_get_admin_page_url(
12226
+ 'account',
12227
+ array( 'fs_action' => 'change_owner' )
12228
+ ),
12229
+ ) );
12230
+
12231
+ return ! $this->is_api_error( $result );
12232
+ }
12233
+
12234
+ /**
12235
+ * Handle install ownership change.
12236
+ *
12237
+ * @author Vova Feldman (@svovaf)
12238
+ * @since 1.1.1
12239
+ * @uses FS_Api
12240
+ *
12241
+ * @return bool Was ownership change successfully complete.
12242
+ */
12243
+ private function complete_change_owner() {
12244
+ $this->_logger->entrance();
12245
+
12246
+ $site_result = $this->get_api_site_scope( true )->get();
12247
+ $site = new FS_Site( $site_result );
12248
+ $this->_site = $site;
12249
+
12250
+ $user = new FS_User();
12251
+ $user->id = fs_request_get( 'user_id' );
12252
+
12253
+ // Validate install's user and given user.
12254
+ if ( $user->id != $this->_site->user_id ) {
12255
+ return false;
12256
+ }
12257
+
12258
+ $user->public_key = fs_request_get( 'user_public_key' );
12259
+ $user->secret_key = fs_request_get( 'user_secret_key' );
12260
+
12261
+ // Fetch new user information.
12262
+ $this->_user = $user;
12263
+ $user_result = $this->get_api_user_scope( true )->get();
12264
+ $user = new FS_User( $user_result );
12265
+ $this->_user = $user;
12266
+
12267
+ $this->_set_account( $user, $site );
12268
+
12269
+ return true;
12270
+ }
12271
+
12272
+ /**
12273
+ * Handle user name update.
12274
+ *
12275
+ * @author Vova Feldman (@svovaf)
12276
+ * @since 1.0.9
12277
+ * @uses FS_Api
12278
+ *
12279
+ * @return object
12280
+ */
12281
+ private function update_user_name() {
12282
+ $this->_logger->entrance();
12283
+ $name = fs_request_get( 'fs_user_name_' . $this->get_unique_affix(), '' );
12284
+
12285
+ $api = $this->get_api_user_scope();
12286
+ $user = $api->call( "?plugin_id={$this->_plugin->id}&fields=id,first,last", 'put', array(
12287
+ 'name' => $name,
12288
+ ) );
12289
+
12290
+ if ( ! isset( $user->error ) ) {
12291
+ $this->_user->first = $user->first;
12292
+ $this->_user->last = $user->last;
12293
+ $this->_store_user();
12294
+ } else {
12295
+ // handle different error cases.
12296
+
12297
+ }
12298
+
12299
+ return $user;
12300
+ }
12301
+
12302
+ /**
12303
+ * Verify user email.
12304
+ *
12305
+ * @author Vova Feldman (@svovaf)
12306
+ * @since 1.0.3
12307
+ * @uses FS_Api
12308
+ */
12309
+ private function verify_email() {
12310
+ $this->_handle_account_user_sync();
12311
+
12312
+ if ( $this->_user->is_verified() ) {
12313
+ return;
12314
+ }
12315
+
12316
+ $api = $this->get_api_site_scope();
12317
+ $result = $api->call( "/users/{$this->_user->id}/verify.json", 'put', array(
12318
+ 'after_email_confirm_url' => $this->_get_admin_page_url(
12319
+ 'account',
12320
+ array( 'fs_action' => 'sync_user' )
12321
+ )
12322
+ ) );
12323
+
12324
+ if ( ! isset( $result->error ) ) {
12325
+ $this->_admin_notices->add( sprintf(
12326
+ $this->get_text_inline( 'Verification mail was just sent to %s. If you can\'t find it after 5 min, please check your spam box.', 'verification-email-sent-message' ),
12327
+ sprintf( '<a href="mailto:%1s">%2s</a>', esc_url( $this->_user->email ), $this->_user->email )
12328
+ ) );
12329
+ } else {
12330
+ // handle different error cases.
12331
+
12332
+ }
12333
+ }
12334
+
12335
+ /**
12336
+ * @author Vova Feldman (@svovaf)
12337
+ * @since 1.1.2
12338
+ *
12339
+ * @param array $params
12340
+ *
12341
+ * @return string
12342
+ */
12343
+ function get_activation_url( $params = array() ) {
12344
+ if ( $this->is_addon() && $this->has_free_plan() ) {
12345
+ /**
12346
+ * @author Vova Feldman (@svovaf)
12347
+ * @since 1.2.1.7 Add-on's activation is the parent's module activation.
12348
+ */
12349
+ return $this->get_parent_instance()->get_activation_url( $params );
12350
+ }
12351
+
12352
+ return $this->apply_filters( 'connect_url', $this->_get_admin_page_url( '', $params ) );
12353
+ }
12354
+
12355
+ /**
12356
+ * @author Vova Feldman (@svovaf)
12357
+ * @since 1.2.1.5
12358
+ *
12359
+ * @param array $params
12360
+ *
12361
+ * @return string
12362
+ */
12363
+ function get_reconnect_url( $params = array() ) {
12364
+ $params['fs_action'] = 'reset_anonymous_mode';
12365
+ $params['fs_unique_affix'] = $this->get_unique_affix();
12366
+
12367
+ return $this->get_activation_url( $params );
12368
+ }
12369
+
12370
+ /**
12371
+ * Get the URL of the page that should be loaded after the user connect
12372
+ * or skip in the opt-in screen.
12373
+ *
12374
+ * @author Vova Feldman (@svovaf)
12375
+ * @since 1.1.3
12376
+ *
12377
+ * @param string $filter Filter name.
12378
+ * @param array $params Since 1.2.2.7
12379
+ *
12380
+ * @return string
12381
+ */
12382
+ function get_after_activation_url( $filter, $params = array() ) {
12383
+ if ( $this->is_free_wp_org_theme() &&
12384
+ fs_request_has( 'pending_activation' )
12385
+ ) {
12386
+ $first_time_path = '';
12387
+ } else {
12388
+ $first_time_path = $this->_menu->get_first_time_path();
12389
+ }
12390
+
12391
+ return add_query_arg( $params, $this->apply_filters(
12392
+ $filter,
12393
+ empty( $first_time_path ) ?
12394
+ $this->_get_admin_page_url() :
12395
+ $first_time_path
12396
+ ) );
12397
+ }
12398
+
12399
+ /**
12400
+ * Handle account page updates / edits / actions.
12401
+ *
12402
+ * @author Vova Feldman (@svovaf)
12403
+ * @since 1.0.2
12404
+ *
12405
+ */
12406
+ private function _handle_account_edits() {
12407
+ if ( ! $this->is_user_admin() ) {
12408
+ return;
12409
+ }
12410
+
12411
+ $plugin_id = fs_request_get( 'plugin_id', $this->get_id() );
12412
+ $action = fs_get_action();
12413
+
12414
+ // Alias.
12415
+ $oops_text = $this->get_text_x_inline( 'Oops', 'exclamation', 'oops' ) . '...';
12416
+
12417
+ switch ( $action ) {
12418
+ case 'delete_account':
12419
+ check_admin_referer( $action );
12420
+
12421
+ if ( $plugin_id == $this->get_id() ) {
12422
+ $this->delete_account_event();
12423
+
12424
+ // Clear user and site.
12425
+ $this->_site = null;
12426
+ $this->_user = null;
12427
+
12428
+ fs_redirect( $this->get_activation_url() );
12429
+ } else {
12430
+ if ( $this->is_addon_activated( $plugin_id ) ) {
12431
+ $fs_addon = self::get_instance_by_id( $plugin_id );
12432
+ $fs_addon->delete_account_event();
12433
+
12434
+ fs_redirect( $this->_get_admin_page_url( 'account' ) );
12435
+ }
12436
+ }
12437
+
12438
+ return;
12439
+
12440
+ case 'downgrade_account':
12441
+ check_admin_referer( $action );
12442
+
12443
+ if ( $plugin_id == $this->get_id() ) {
12444
+ $this->_downgrade_site();
12445
+ } else if ( $this->is_addon_activated( $plugin_id ) ) {
12446
+ $fs_addon = self::get_instance_by_id( $plugin_id );
12447
+ $fs_addon->_downgrade_site();
12448
+ }
12449
+
12450
+ return;
12451
+
12452
+ case 'activate_license':
12453
+ check_admin_referer( $action );
12454
+
12455
+ if ( $plugin_id == $this->get_id() ) {
12456
+ $this->_activate_license();
12457
+ } else {
12458
+ if ( $this->is_addon_activated( $plugin_id ) ) {
12459
+ $fs_addon = self::get_instance_by_id( $plugin_id );
12460
+ $fs_addon->_activate_license();
12461
+ }
12462
+ }
12463
+
12464
+ return;
12465
+
12466
+ case 'deactivate_license':
12467
+ check_admin_referer( $action );
12468
+
12469
+ if ( $plugin_id == $this->get_id() ) {
12470
+ $this->_deactivate_license();
12471
+
12472
+ if ( $this->is_only_premium() ) {
12473
+ // Clear user and site.
12474
+ $this->_site = null;
12475
+ $this->_user = null;
12476
+
12477
+ fs_redirect( $this->get_activation_url() );
12478
+ }
12479
+ } else {
12480
+ if ( $this->is_addon_activated( $plugin_id ) ) {
12481
+ $fs_addon = self::get_instance_by_id( $plugin_id );
12482
+ $fs_addon->_deactivate_license();
12483
+ }
12484
+ }
12485
+
12486
+ return;
12487
+
12488
+ case 'check_updates':
12489
+ check_admin_referer( $action );
12490
+ $this->check_updates();
12491
+
12492
+ return;
12493
+
12494
+ case 'change_owner':
12495
+ $state = fs_request_get( 'state', 'init' );
12496
+ switch ( $state ) {
12497
+ case 'init':
12498
+ $candidate_email = fs_request_get( 'candidate_email', '' );
12499
+
12500
+ if ( $this->init_change_owner( $candidate_email ) ) {
12501
+ $this->_admin_notices->add( sprintf( $this->get_text_inline( 'Please check your mailbox, you should receive an email via %s to confirm the ownership change. From security reasons, you must confirm the change within the next 15 min. If you cannot find the email, please check your spam folder.', 'change-owner-request-sent-x' ), '<b>' . $this->_user->email . '</b>' ) );
12502
+ }
12503
+ break;
12504
+ case 'owner_confirmed':
12505
+ $candidate_email = fs_request_get( 'candidate_email', '' );
12506
+
12507
+ $this->_admin_notices->add( sprintf( $this->get_text_inline( 'Thanks for confirming the ownership change. An email was just sent to %s for final approval.', 'change-owner-request_owner-confirmed' ), '<b>' . $candidate_email . '</b>' ) );
12508
+ break;
12509
+ case 'candidate_confirmed':
12510
+ if ( $this->complete_change_owner() ) {
12511
+ $this->_admin_notices->add_sticky(
12512
+ sprintf( $this->get_text_inline( '%s is the new owner of the account.', 'change-owner-request_candidate-confirmed' ), '<b>' . $this->_user->email . '</b>' ),
12513
+ 'ownership_changed',
12514
+ $this->get_text_x_inline( 'Congrats', 'as congratulations', 'congrats' ) . '!'
12515
+ );
12516
+ } else {
12517
+ // @todo Handle failed ownership change message.
12518
+ }
12519
+ break;
12520
+ }
12521
+
12522
+ return;
12523
+
12524
+ case 'update_email':
12525
+ check_admin_referer( 'update_email' );
12526
+
12527
+ $new_email = fs_request_get( 'fs_email_' . $this->get_unique_affix(), '' );
12528
+ $result = $this->update_email( $new_email );
12529
+
12530
+ if ( isset( $result->error ) ) {
12531
+ switch ( $result->error->code ) {
12532
+ case 'user_exist':
12533
+ $this->_admin_notices->add(
12534
+ $this->get_text_inline( 'Sorry, we could not complete the email update. Another user with the same email is already registered.', 'user-exist-message' ) . ' ' .
12535
+ sprintf( $this->get_text_inline( 'If you would like to give up the ownership of the %s\'s account to %s click the Change Ownership button.', 'user-exist-message_ownership' ), $this->_module_type, '<b>' . $new_email . '</b>' ) .
12536
+ sprintf(
12537
+ '<a style="margin-left: 10px;" href="%s"><button class="button button-primary">%s &nbsp;&#10140;</button></a>',
12538
+ $this->get_account_url( 'change_owner', array(
12539
+ 'state' => 'init',
12540
+ 'candidate_email' => $new_email
12541
+ ) ),
12542
+ $this->get_text_inline( 'Change Ownership', 'change-ownership' )
12543
+ ),
12544
+ $oops_text,
12545
+ 'error'
12546
+ );
12547
+ break;
12548
+ }
12549
+ } else {
12550
+ $this->_admin_notices->add( $this->get_text_inline( 'Your email was successfully updated. You should receive an email with confirmation instructions in few moments.', 'email-updated-message' ) );
12551
+ }
12552
+
12553
+ return;
12554
+
12555
+ case 'update_user_name':
12556
+ check_admin_referer( 'update_user_name' );
12557
+
12558
+ $result = $this->update_user_name();
12559
+
12560
+ if ( isset( $result->error ) ) {
12561
+ $this->_admin_notices->add(
12562
+ $this->get_text_inline( 'Please provide your full name.', 'name-update-failed-message' ),
12563
+ $oops_text,
12564
+ 'error'
12565
+ );
12566
+ } else {
12567
+ $this->_admin_notices->add( $this->get_text_inline( 'Your name was successfully updated.', 'name-updated-message' ) );
12568
+ }
12569
+
12570
+ return;
12571
+
12572
+ #region Actions that might be called from external links (e.g. email)
12573
+
12574
+ case 'cancel_trial':
12575
+ if ( $plugin_id == $this->get_id() ) {
12576
+ $this->_cancel_trial();
12577
+ } else {
12578
+ if ( $this->is_addon_activated( $plugin_id ) ) {
12579
+ $fs_addon = self::get_instance_by_id( $plugin_id );
12580
+ $fs_addon->_cancel_trial();
12581
+ }
12582
+ }
12583
+
12584
+ return;
12585
+
12586
+ case 'verify_email':
12587
+ $this->verify_email();
12588
+
12589
+ return;
12590
+
12591
+ case 'sync_user':
12592
+ $this->_handle_account_user_sync();
12593
+
12594
+ return;
12595
+
12596
+ case $this->get_unique_affix() . '_sync_license':
12597
+ $this->_sync_license();
12598
+
12599
+ return;
12600
+
12601
+ case 'download_latest':
12602
+ $this->download_latest_directly( $plugin_id );
12603
+
12604
+ return;
12605
+
12606
+ #endregion
12607
+ }
12608
+
12609
+ if ( WP_FS__IS_POST_REQUEST ) {
12610
+ $properties = array( 'site_secret_key', 'site_id', 'site_public_key' );
12611
+ foreach ( $properties as $p ) {
12612
+ if ( 'update_' . $p === $action ) {
12613
+ check_admin_referer( $action );
12614
+
12615
+ $this->_logger->log( $action );
12616
+
12617
+ $site_property = substr( $p, strlen( 'site_' ) );
12618
+ $site_property_value = fs_request_get( 'fs_' . $p . '_' . $this->get_unique_affix(), '' );
12619
+ $this->get_site()->{$site_property} = $site_property_value;
12620
+
12621
+ // Store account after modification.
12622
+ $this->_store_site();
12623
+
12624
+ $this->do_action( 'account_property_edit', 'site', $site_property, $site_property_value );
12625
+
12626
+ $this->_admin_notices->add( sprintf(
12627
+ /* translators: %s: User's account property (e.g. email address, name) */
12628
+ $this->get_text_inline( 'You have successfully updated your %s.', 'x-updated' ),
12629
+ '<b>' . str_replace( '_', ' ', $p ) . '</b>'
12630
+ ) );
12631
+
12632
+ return;
12633
+ }
12634
+ }
12635
+ }
12636
+ }
12637
+
12638
+ /**
12639
+ * Account page resources load.
12640
+ *
12641
+ * @author Vova Feldman (@svovaf)
12642
+ * @since 1.0.6
12643
+ */
12644
+ function _account_page_load() {
12645
+ $this->_logger->entrance();
12646
+
12647
+ $this->_logger->info( var_export( $_REQUEST, true ) );
12648
+
12649
+ fs_enqueue_local_style( 'fs_account', '/admin/account.css' );
12650
+
12651
+ if ( $this->has_addons() ) {
12652
+ wp_enqueue_script( 'plugin-install' );
12653
+ add_thickbox();
12654
+
12655
+ function fs_addons_body_class( $classes ) {
12656
+ $classes .= ' plugins-php';
12657
+
12658
+ return $classes;
12659
+ }
12660
+
12661
+ add_filter( 'admin_body_class', 'fs_addons_body_class' );
12662
+ }
12663
+
12664
+ if ( $this->has_paid_plan() &&
12665
+ ! $this->has_any_license() &&
12666
+ ! $this->is_sync_executed() &&
12667
+ $this->is_tracking_allowed()
12668
+ ) {
12669
+ /**
12670
+ * If no licenses found and no sync job was executed during the last 24 hours,
12671
+ * just execute the sync job right away (blocking execution).
12672
+ *
12673
+ * @since 1.1.7.3
12674
+ */
12675
+ $this->run_manual_sync();
12676
+ }
12677
+
12678
+ $this->_handle_account_edits();
12679
+
12680
+ $this->do_action( 'account_page_load_before_departure' );
12681
+ }
12682
+
12683
+ /**
12684
+ * Renders the "Affiliation" page.
12685
+ *
12686
+ * @author Leo Fajardo (@leorw)
12687
+ * @since 1.2.3
12688
+ */
12689
+ function _affiliation_page_render() {
12690
+ $this->_logger->entrance();
12691
+
12692
+ $this->fetch_affiliate_and_terms();
12693
+
12694
+ fs_enqueue_local_style( 'fs_affiliation', '/admin/affiliation.css' );
12695
+
12696
+ $vars = array( 'id' => $this->_module_id );
12697
+ echo $this->apply_filters( "/forms/affiliation.php", fs_get_template( '/forms/affiliation.php', $vars ) );
12698
+ }
12699
+
12700
+
12701
+ /**
12702
+ * Render account page.
12703
+ *
12704
+ * @author Vova Feldman (@svovaf)
12705
+ * @since 1.0.0
12706
+ */
12707
+ function _account_page_render() {
12708
+ $this->_logger->entrance();
12709
+
12710
+ $template = 'account.php';
12711
+ $vars = array( 'id' => $this->_module_id );
12712
+
12713
+ /**
12714
+ * Added filter to the template to allow developers wrapping the template
12715
+ * in custom HTML (e.g. within a wizard/tabs).
12716
+ *
12717
+ * @author Vova Feldman (@svovaf)
12718
+ * @since 1.2.1.6
12719
+ */
12720
+ echo $this->apply_filters( "templates/{$template}", fs_get_template( $template, $vars ) );
12721
+ }
12722
+
12723
+ /**
12724
+ * Render account connect page.
12725
+ *
12726
+ * @author Vova Feldman (@svovaf)
12727
+ * @since 1.0.7
12728
+ */
12729
+ function _connect_page_render() {
12730
+ $this->_logger->entrance();
12731
+
12732
+ $vars = array( 'id' => $this->_module_id );
12733
+
12734
+ /**
12735
+ * Added filter to the template to allow developers wrapping the template
12736
+ * in custom HTML (e.g. within a wizard/tabs).
12737
+ *
12738
+ * @author Vova Feldman (@svovaf)
12739
+ * @since 1.2.1.6
12740
+ */
12741
+ echo $this->apply_filters( 'templates/connect.php', fs_get_template( 'connect.php', $vars ) );
12742
+ }
12743
+
12744
+ /**
12745
+ * Load required resources before add-ons page render.
12746
+ *
12747
+ * @author Vova Feldman (@svovaf)
12748
+ * @since 1.0.6
12749
+ */
12750
+ function _addons_page_load() {
12751
+ $this->_logger->entrance();
12752
+
12753
+ fs_enqueue_local_style( 'fs_addons', '/admin/add-ons.css' );
12754
+
12755
+ wp_enqueue_script( 'plugin-install' );
12756
+ add_thickbox();
12757
+
12758
+ function fs_addons_body_class( $classes ) {
12759
+ $classes .= ' plugins-php';
12760
+
12761
+ return $classes;
12762
+ }
12763
+
12764
+ add_filter( 'admin_body_class', 'fs_addons_body_class' );
12765
+
12766
+ if ( ! $this->is_registered() && $this->is_org_repo_compliant() ) {
12767
+ $this->_admin_notices->add(
12768
+ sprintf( $this->get_text_inline( 'Just letting you know that the add-ons information of %s is being pulled from an external server.', 'addons-info-external-message' ), '<b>' . $this->get_plugin_name() . '</b>' ),
12769
+ $this->get_text_x_inline( 'Heads up', 'advance notice of something that will need attention.', 'heads-up' ),
12770
+ 'update-nag'
12771
+ );
12772
+ }
12773
+ }
12774
+
12775
+ /**
12776
+ * Render add-ons page.
12777
+ *
12778
+ * @author Vova Feldman (@svovaf)
12779
+ * @since 1.0.6
12780
+ */
12781
+ function _addons_page_render() {
12782
+ $this->_logger->entrance();
12783
+
12784
+ $vars = array( 'id' => $this->_module_id );
12785
+
12786
+ /**
12787
+ * Added filter to the template to allow developers wrapping the template
12788
+ * in custom HTML (e.g. within a wizard/tabs).
12789
+ *
12790
+ * @author Vova Feldman (@svovaf)
12791
+ * @since 1.2.1.6
12792
+ */
12793
+ echo $this->apply_filters( 'templates/add-ons.php', fs_get_template( 'add-ons.php', $vars ) );
12794
+ }
12795
+
12796
+ /* Pricing & Upgrade
12797
+ ------------------------------------------------------------------------------------------------------------------*/
12798
+ /**
12799
+ * Render pricing page.
12800
+ *
12801
+ * @author Vova Feldman (@svovaf)
12802
+ * @since 1.0.0
12803
+ */
12804
+ function _pricing_page_render() {
12805
+ $this->_logger->entrance();
12806
+
12807
+ $vars = array( 'id' => $this->_module_id );
12808
+
12809
+ if ( 'true' === fs_request_get( 'checkout', false ) ) {
12810
+ fs_require_once_template( 'checkout.php', $vars );
12811
+ } else {
12812
+ fs_require_once_template( 'pricing.php', $vars );
12813
+ }
12814
+ }
12815
+
12816
+ #----------------------------------------------------------------------------------
12817
+ #region Contact Us
12818
+ #----------------------------------------------------------------------------------
12819
+
12820
+ /**
12821
+ * Render contact-us page.
12822
+ *
12823
+ * @author Vova Feldman (@svovaf)
12824
+ * @since 1.0.3
12825
+ */
12826
+ function _contact_page_render() {
12827
+ $this->_logger->entrance();
12828
+
12829
+ $vars = array( 'id' => $this->_module_id );
12830
+ fs_require_once_template( 'contact.php', $vars );
12831
+ }
12832
+
12833
+ #endregion ------------------------------------------------------------------------
12834
+
12835
+ /**
12836
+ * Hide all admin notices to prevent distractions.
12837
+ *
12838
+ * @author Vova Feldman (@svovaf)
12839
+ * @since 1.0.3
12840
+ *
12841
+ * @uses remove_all_actions()
12842
+ */
12843
+ private static function _hide_admin_notices() {
12844
+ remove_all_actions( 'admin_notices' );
12845
+ remove_all_actions( 'network_admin_notices' );
12846
+ remove_all_actions( 'all_admin_notices' );
12847
+ remove_all_actions( 'user_admin_notices' );
12848
+ }
12849
+
12850
+ static function _clean_admin_content_section_hook() {
12851
+ self::_hide_admin_notices();
12852
+
12853
+ // Hide footer.
12854
+ echo '<style>#wpfooter { display: none !important; }</style>';
12855
+ }
12856
+
12857
+ /**
12858
+ * Attach to admin_head hook to hide all admin notices.
12859
+ *
12860
+ * @author Vova Feldman (@svovaf)
12861
+ * @since 1.0.3
12862
+ */
12863
+ static function _clean_admin_content_section() {
12864
+ add_action( 'admin_head', 'Freemius::_clean_admin_content_section_hook' );
12865
+ }
12866
+
12867
+ /* CSS & JavaScript
12868
+ ------------------------------------------------------------------------------------------------------------------*/
12869
+ /* function _enqueue_script($handle, $src) {
12870
+ $url = plugins_url( substr( WP_FS__DIR_JS, strlen( $this->_plugin_dir_path ) ) . '/assets/js/' . $src );
12871
+
12872
+ $this->_logger->entrance( 'script = ' . $url );
12873
+
12874
+ wp_enqueue_script( $handle, $url );
12875
+ }*/
12876
+
12877
+ /* SDK
12878
+ ------------------------------------------------------------------------------------------------------------------*/
12879
+ private $_user_api;
12880
+
12881
+ /**
12882
+ *
12883
+ * @author Vova Feldman (@svovaf)
12884
+ * @since 1.0.2
12885
+ *
12886
+ * @param bool $flush
12887
+ *
12888
+ * @return FS_Api
12889
+ */
12890
+ function get_api_user_scope( $flush = false ) {
12891
+ if ( ! isset( $this->_user_api ) || $flush ) {
12892
+ $this->_user_api = FS_Api::instance(
12893
+ $this->_module_id,
12894
+ 'user',
12895
+ $this->_user->id,
12896
+ $this->_user->public_key,
12897
+ ! $this->is_live(),
12898
+ $this->_user->secret_key
12899
+ );
12900
+ }
12901
+
12902
+ return $this->_user_api;
12903
+ }
12904
+
12905
+ private $_site_api;
12906
+
12907
+ /**
12908
+ *
12909
+ * @author Vova Feldman (@svovaf)
12910
+ * @since 1.0.2
12911
+ *
12912
+ * @param bool $flush
12913
+ *
12914
+ * @return FS_Api
12915
+ */
12916
+ function get_api_site_scope( $flush = false ) {
12917
+ if ( ! isset( $this->_site_api ) || $flush ) {
12918
+ $this->_site_api = FS_Api::instance(
12919
+ $this->_module_id,
12920
+ 'install',
12921
+ $this->_site->id,
12922
+ $this->_site->public_key,
12923
+ ! $this->is_live(),
12924
+ $this->_site->secret_key
12925
+ );
12926
+ }
12927
+
12928
+ return $this->_site_api;
12929
+ }
12930
+
12931
+ private $_plugin_api;
12932
+
12933
+ /**
12934
+ * Get plugin public API scope.
12935
+ *
12936
+ * @author Vova Feldman (@svovaf)
12937
+ * @since 1.0.7
12938
+ *
12939
+ * @return FS_Api
12940
+ */
12941
+ function get_api_plugin_scope() {
12942
+ if ( ! isset( $this->_plugin_api ) ) {
12943
+ $this->_plugin_api = FS_Api::instance(
12944
+ $this->_module_id,
12945
+ 'plugin',
12946
+ $this->_plugin->id,
12947
+ $this->_plugin->public_key,
12948
+ ! $this->is_live()
12949
+ );
12950
+ }
12951
+
12952
+ return $this->_plugin_api;
12953
+ }
12954
+
12955
+ /**
12956
+ * Get site API scope object (fallback to public plugin scope when not registered).
12957
+ *
12958
+ * @author Vova Feldman (@svovaf)
12959
+ * @since 1.0.7
12960
+ *
12961
+ * @return FS_Api
12962
+ */
12963
+ function get_api_site_or_plugin_scope() {
12964
+ return $this->is_registered() ?
12965
+ $this->get_api_site_scope() :
12966
+ $this->get_api_plugin_scope();
12967
+ }
12968
+
12969
+ /**
12970
+ * Show trial promotional notice (if any trial exist).
12971
+ *
12972
+ * @author Vova Feldman (@svovaf)
12973
+ * @since 1.0.9
12974
+ *
12975
+ * @param $plans
12976
+ */
12977
+ function _check_for_trial_plans( $plans ) {
12978
+ $this->_storage->has_trial_plan = FS_Plan_Manager::instance()->has_trial_plan( $plans );
12979
+ }
12980
+
12981
+ /**
12982
+ * During trial promotion the "upgrade" submenu item turns to
12983
+ * "start trial" to encourage the trial. Since we want to keep
12984
+ * the same menu item handler and there's no robust way to
12985
+ * add new arguments to the menu item link's querystring,
12986
+ * use JavaScript to find the menu item and update the href of
12987
+ * the link.
12988
+ *
12989
+ * @author Vova Feldman (@svovaf)
12990
+ * @since 1.2.1.5
12991
+ */
12992
+ function _fix_start_trial_menu_item_url() {
12993
+ $template_args = array( 'id' => $this->_module_id );
12994
+ fs_require_template( 'add-trial-to-pricing.php', $template_args );
12995
+ }
12996
+
12997
+ /**
12998
+ * Check if module is currently in a trial promotion mode.
12999
+ *
13000
+ * @author Vova Feldman (@svovaf)
13001
+ * @since 1.2.2.7
13002
+ *
13003
+ * @return bool
13004
+ */
13005
+ function is_in_trial_promotion() {
13006
+ return $this->_admin_notices->has_sticky( 'trial_promotion' );
13007
+ }
13008
+
13009
+ /**
13010
+ * Show trial promotional notice (if any trial exist).
13011
+ *
13012
+ * @author Vova Feldman (@svovaf)
13013
+ * @since 1.0.9
13014
+ *
13015
+ * @return bool If trial notice added.
13016
+ */
13017
+ function _add_trial_notice() {
13018
+ if ( ! $this->is_user_admin() ) {
13019
+ return false;
13020
+ }
13021
+
13022
+ if ( ! $this->is_user_in_admin() ) {
13023
+ return false;
13024
+ }
13025
+
13026
+ // Check if trial message is already shown.
13027
+ if ( $this->is_in_trial_promotion() ) {
13028
+ add_action( 'admin_footer', array( &$this, '_fix_start_trial_menu_item_url' ) );
13029
+
13030
+ $this->_menu->add_counter_to_menu_item( 1, 'fs-trial' );
13031
+
13032
+ return false;
13033
+ }
13034
+
13035
+ if ( $this->is_premium() && ! WP_FS__DEV_MODE ) {
13036
+ // Don't show trial if running the premium code, unless running in DEV mode.
13037
+ return false;
13038
+ }
13039
+
13040
+ if ( ! $this->has_trial_plan() ) {
13041
+ // No plans with trial.
13042
+ return false;
13043
+ }
13044
+
13045
+ if ( ! $this->apply_filters( 'show_trial', true ) ) {
13046
+ // Developer explicitly asked not to show the trial promo.
13047
+ return false;
13048
+ }
13049
+
13050
+ if ( $this->is_registered() ) {
13051
+ // Check if trial already utilized.
13052
+ if ( $this->_site->is_trial_utilized() ) {
13053
+ return false;
13054
+ }
13055
+
13056
+ if ( $this->is_paying_or_trial() ) {
13057
+ // Don't show trial if paying or already in trial.
13058
+ return false;
13059
+ }
13060
+ }
13061
+
13062
+ if ( $this->is_activation_mode() || $this->is_pending_activation() ) {
13063
+ // If not yet opted-in/skipped, or pending activation, don't show trial.
13064
+ return false;
13065
+ }
13066
+
13067
+ $last_time_trial_promotion_shown = $this->_storage->get( 'trial_promotion_shown', false );
13068
+ $was_promotion_shown_before = ( false !== $last_time_trial_promotion_shown );
13069
+
13070
+ // Show promotion if never shown before and 24 hours after initial activation with FS.
13071
+ if ( ! $was_promotion_shown_before &&
13072
+ $this->_storage->install_timestamp > ( time() - WP_FS__TIME_24_HOURS_IN_SEC )
13073
+ ) {
13074
+ return false;
13075
+ }
13076
+
13077
+ // OR if promotion was shown before, try showing it every 30 days.
13078
+ if ( $was_promotion_shown_before &&
13079
+ 30 * WP_FS__TIME_24_HOURS_IN_SEC > time() - $last_time_trial_promotion_shown
13080
+ ) {
13081
+ return false;
13082
+ }
13083
+
13084
+ $trial_period = $this->_trial_days;
13085
+ $require_payment = $this->_is_trial_require_payment;
13086
+ $trial_url = $this->get_trial_url();
13087
+ $plans_string = strtolower( $this->get_text_inline( 'Awesome', 'awesome' ) );
13088
+
13089
+ if ( $this->is_registered() ) {
13090
+ // If opted-in, override trial with up to date data from API.
13091
+ $trial_plans = FS_Plan_Manager::instance()->get_trial_plans( $this->_plans );
13092
+ $trial_plans_count = count( $trial_plans );
13093
+
13094
+ if ( 0 === $trial_plans_count ) {
13095
+ // If there's no plans with a trial just exit.
13096
+ return false;
13097
+ }
13098
+
13099
+ /**
13100
+ * @var FS_Plugin_Plan $paid_plan
13101
+ */
13102
+ $paid_plan = $trial_plans[0];
13103
+ $require_payment = $paid_plan->is_require_subscription;
13104
+ $trial_period = $paid_plan->trial_period;
13105
+
13106
+ $total_paid_plans = count( $this->_plans ) - ( FS_Plan_Manager::instance()->has_free_plan( $this->_plans ) ? 1 : 0 );
13107
+
13108
+ if ( $total_paid_plans !== $trial_plans_count ) {
13109
+ // Not all paid plans have a trial - generate a string of those that have it.
13110
+ for ( $i = 0; $i < $trial_plans_count; $i ++ ) {
13111
+ $plans_string .= sprintf(
13112
+ ' <a href="%s">%s</a>',
13113
+ $trial_url,
13114
+ $trial_plans[ $i ]->title
13115
+ );
13116
+
13117
+ if ( $i < $trial_plans_count - 2 ) {
13118
+ $plans_string .= ', ';
13119
+ } else if ( $i == $trial_plans_count - 2 ) {
13120
+ $plans_string .= ' and ';
13121
+ }
13122
+ }
13123
+ }
13124
+ }
13125
+
13126
+ $message = sprintf(
13127
+ $this->get_text_x_inline( 'Hey', 'exclamation', 'hey' ) . '! ' . $this->get_text_inline( 'How do you like %s so far? Test all our %s premium features with a %d-day free trial.', 'trial-x-promotion-message' ),
13128
+ sprintf( '<b>%s</b>', $this->get_plugin_name() ),
13129
+ $plans_string,
13130
+ $trial_period
13131
+ );
13132
+
13133
+ // "No Credit-Card Required" or "No Commitment for N Days".
13134
+ $cc_string = $require_payment ?
13135
+ sprintf( $this->get_text_inline( 'No commitment for %s days - cancel anytime!', 'no-commitment-for-x-days' ), $trial_period ) :
13136
+ $this->get_text_inline( 'No credit card required', 'no-cc-required' ) . '!';
13137
+
13138
+
13139
+ // Start trial button.
13140
+ $button = ' ' . sprintf(
13141
+ '<a style="margin-left: 10px; vertical-align: super;" href="%s"><button class="button button-primary">%s &nbsp;&#10140;</button></a>',
13142
+ $trial_url,
13143
+ $this->get_text_x_inline( 'Start free trial', 'call to action', 'start-free-trial' )
13144
+ );
13145
+
13146
+ $this->_admin_notices->add_sticky(
13147
+ $this->apply_filters( 'trial_promotion_message', "{$message} {$cc_string} {$button}" ),
13148
+ 'trial_promotion',
13149
+ '',
13150
+ 'promotion'
13151
+ );
13152
+
13153
+ $this->_storage->trial_promotion_shown = WP_FS__SCRIPT_START_TIME;
13154
+
13155
+ return true;
13156
+ }
13157
+
13158
+ /**
13159
+ * Lets users/customers know that the product has an affiliate program.
13160
+ *
13161
+ * @author Leo Fajardo (@leorw)
13162
+ * @since 1.2.2.11
13163
+ *
13164
+ * @return bool Returns true if the notice has been added.
13165
+ */
13166
+ function _add_affiliate_program_notice() {
13167
+ if ( ! $this->is_user_admin() ) {
13168
+ return false;
13169
+ }
13170
+
13171
+ if ( ! $this->is_user_in_admin() ) {
13172
+ return false;
13173
+ }
13174
+
13175
+ // Check if the notice is already shown.
13176
+ if ( $this->_admin_notices->has_sticky( 'affiliate_program' ) ) {
13177
+ return false;
13178
+ }
13179
+
13180
+ if (
13181
+ // Product has no affiliate program.
13182
+ ! $this->has_affiliate_program() ||
13183
+ // User has applied for an affiliate account.
13184
+ ! empty( $this->_storage->affiliate_application_data ) ) {
13185
+ return false;
13186
+ }
13187
+
13188
+ if ( ! $this->apply_filters( 'show_affiliate_program_notice', true ) ) {
13189
+ // Developer explicitly asked not to show the notice about the affiliate program.
13190
+ return false;
13191
+ }
13192
+
13193
+ if ( $this->is_activation_mode() || $this->is_pending_activation() ) {
13194
+ // If not yet opted in/skipped, or pending activation, don't show the notice.
13195
+ return false;
13196
+ }
13197
+
13198
+ $last_time_notice_was_shown = $this->_storage->get( 'affiliate_program_notice_shown', false );
13199
+ $was_notice_shown_before = ( false !== $last_time_notice_was_shown );
13200
+
13201
+ /**
13202
+ * Do not show the notice if it was already shown before or less than 30 days have passed since the initial
13203
+ * activation with FS.
13204
+ */
13205
+ if ( $was_notice_shown_before ||
13206
+ $this->_storage->install_timestamp > ( time() - ( WP_FS__TIME_24_HOURS_IN_SEC * 30 ) )
13207
+ ) {
13208
+ return false;
13209
+ }
13210
+
13211
+ if ( ! $this->is_paying() &&
13212
+ FS_Plugin::AFFILIATE_MODERATION_CUSTOMERS == $this->_plugin->affiliate_moderation ) {
13213
+ // If the user is not a customer and the affiliate program is only for customers, don't show the notice.
13214
+ return false;
13215
+ }
13216
+
13217
+ $message = sprintf(
13218
+ $this->get_text_inline( 'Hey there, did you know that %s has an affiliate program? If you like the %s you can become our ambassador and earn some cash!', 'become-an-ambassador-admin-notice' ),
13219
+ sprintf( '<strong>%s</strong>', $this->get_plugin_name() ),
13220
+ $this->get_module_label( true )
13221
+ );
13222
+
13223
+ // HTML code for the "Learn more..." button.
13224
+ $button = ' ' . sprintf(
13225
+ '<a style="display: block; margin-top: 10px;" href="%s"><button class="button button-primary">%s &nbsp;&#10140;</button></a>',
13226
+ $this->_get_admin_page_url( 'affiliation' ),
13227
+ $this->get_text_inline( 'Learn more', 'learn-more' ) . '...'
13228
+ );
13229
+
13230
+ $this->_admin_notices->add_sticky(
13231
+ $this->apply_filters( 'affiliate_program_notice', "{$message} {$button}" ),
13232
+ 'affiliate_program',
13233
+ '',
13234
+ 'promotion'
13235
+ );
13236
+
13237
+ $this->_storage->affiliate_program_notice_shown = WP_FS__SCRIPT_START_TIME;
13238
+
13239
+ return true;
13240
+ }
13241
+
13242
+ /**
13243
+ * @author Vova Feldman (@svovaf)
13244
+ * @since 1.2.1.5
13245
+ */
13246
+ function _enqueue_common_css() {
13247
+ if ( $this->has_paid_plan() && ! $this->is_paying() ) {
13248
+ // Add basic CSS for admin-notices and menu-item colors.
13249
+ fs_enqueue_local_style( 'fs_common', '/admin/common.css' );
13250
+ }
13251
+ }
13252
+
13253
+ /**
13254
+ * @author Leo Fajardo (leorw)
13255
+ * @since 1.2.2
13256
+ */
13257
+ function _show_theme_activation_optin_dialog() {
13258
+ fs_enqueue_local_style( 'fs_connect', '/admin/connect.css' );
13259
+
13260
+ add_action( 'admin_footer-themes.php', array( &$this, '_add_fs_theme_activation_dialog' ) );
13261
+ }
13262
+
13263
+ /**
13264
+ * @author Leo Fajardo (leorw)
13265
+ * @since 1.2.2
13266
+ */
13267
+ function _add_fs_theme_activation_dialog() {
13268
+ $vars = array( 'id' => $this->_module_id );
13269
+ fs_require_once_template( 'connect.php', $vars );
13270
+ }
13271
+
13272
+ /* Action Links
13273
+ ------------------------------------------------------------------------------------------------------------------*/
13274
+ private $_action_links_hooked = false;
13275
+ private $_action_links = array();
13276
+
13277
+ /**
13278
+ * Hook to plugin action links filter.
13279
+ *
13280
+ * @author Vova Feldman (@svovaf)
13281
+ * @since 1.0.0
13282
+ */
13283
+ private function hook_plugin_action_links() {
13284
+ $this->_logger->entrance();
13285
+
13286
+ $this->_action_links_hooked = true;
13287
+
13288
+ $this->_logger->log( 'Adding action links hooks.' );
13289
+
13290
+ // Add action link to settings page.
13291
+ add_filter( 'plugin_action_links_' . $this->_plugin_basename, array(
13292
+ &$this,
13293
+ '_modify_plugin_action_links_hook'
13294
+ ), WP_FS__DEFAULT_PRIORITY, 2 );
13295
+ add_filter( 'network_admin_plugin_action_links_' . $this->_plugin_basename, array(
13296
+ &$this,
13297
+ '_modify_plugin_action_links_hook'
13298
+ ), WP_FS__DEFAULT_PRIORITY, 2 );
13299
+ }
13300
+
13301
+ /**
13302
+ * Add plugin action link.
13303
+ *
13304
+ * @author Vova Feldman (@svovaf)
13305
+ * @since 1.0.0
13306
+ *
13307
+ * @param $label
13308
+ * @param $url
13309
+ * @param bool $external
13310
+ * @param int $priority
13311
+ * @param bool $key
13312
+ */
13313
+ function add_plugin_action_link( $label, $url, $external = false, $priority = WP_FS__DEFAULT_PRIORITY, $key = false ) {
13314
+ $this->_logger->entrance();
13315
+
13316
+ if ( ! isset( $this->_action_links[ $priority ] ) ) {
13317
+ $this->_action_links[ $priority ] = array();
13318
+ }
13319
+
13320
+ if ( false === $key ) {
13321
+ $key = preg_replace( "/[^A-Za-z0-9 ]/", '', strtolower( $label ) );
13322
+ }
13323
+
13324
+ $this->_action_links[ $priority ][] = array(
13325
+ 'label' => $label,
13326
+ 'href' => $url,
13327
+ 'key' => $key,
13328
+ 'external' => $external
13329
+ );
13330
+ }
13331
+
13332
+ /**
13333
+ * Adds Upgrade and Add-Ons links to the main Plugins page link actions collection.
13334
+ *
13335
+ * @author Vova Feldman (@svovaf)
13336
+ * @since 1.0.0
13337
+ */
13338
+ function _add_upgrade_action_link() {
13339
+ $this->_logger->entrance();
13340
+
13341
+ if ( $this->is_registered() ) {
13342
+ if ( ! $this->is_paying() && $this->has_paid_plan() ) {
13343
+ $this->add_plugin_action_link(
13344
+ $this->get_text_inline( 'Upgrade', 'upgrade' ),
13345
+ $this->get_upgrade_url(),
13346
+ false,
13347
+ 7,
13348
+ 'upgrade'
13349
+ );
13350
+ }
13351
+
13352
+ if ( $this->has_addons() ) {
13353
+ $this->add_plugin_action_link(
13354
+ $this->get_text_inline( 'Add-Ons', 'add-ons' ),
13355
+ $this->_get_admin_page_url( 'addons' ),
13356
+ false,
13357
+ 9,
13358
+ 'addons'
13359
+ );
13360
+ }
13361
+ }
13362
+ }
13363
+
13364
+ /**
13365
+ * Adds "Activate License" or "Change License" link to the main Plugins page link actions collection.
13366
+ *
13367
+ * @author Leo Fajardo (@leorw)
13368
+ * @since 1.1.9
13369
+ */
13370
+ function _add_license_action_link() {
13371
+ $this->_logger->entrance();
13372
+
13373
+ if ( $this->is_free_plan() && $this->is_addon() ) {
13374
+ return;
13375
+ }
13376
+
13377
+ if ( ! self::is_ajax() ) {
13378
+ // Inject license activation dialog UI and client side code.
13379
+ add_action( 'admin_footer', array( &$this, '_add_license_activation_dialog_box' ) );
13380
+ }
13381
+
13382
+ $link_text = $this->is_free_plan() ?
13383
+ $this->get_text_inline( 'Activate License', 'activate-license' ) :
13384
+ $this->get_text_inline( 'Change License', 'change-license' );
13385
+
13386
+ $this->add_plugin_action_link(
13387
+ $link_text,
13388
+ '#',
13389
+ false,
13390
+ 11,
13391
+ ( 'activate-license ' . $this->get_unique_affix() )
13392
+ );
13393
+ }
13394
+
13395
+ /**
13396
+ * Adds "Opt in" or "Opt out" link to the main "Plugins" page link actions collection.
13397
+ *
13398
+ * @author Leo Fajardo (@leorw)
13399
+ * @since 1.2.1.5
13400
+ */
13401
+ function _add_tracking_links() {
13402
+ if ( ! current_user_can( 'activate_plugins' ) ) {
13403
+ return;
13404
+ }
13405
+
13406
+ $this->_logger->entrance();
13407
+
13408
+ if ( fs_request_is_action_secure( $this->get_unique_affix() . '_reconnect' ) ) {
13409
+ if ( ! $this->is_registered() && $this->is_anonymous() ) {
13410
+ $this->connect_again();
13411
+
13412
+ return;
13413
+ }
13414
+ }
13415
+
13416
+ if ( ( $this->is_plugin() && ! self::is_plugins_page() ) ||
13417
+ ( $this->is_theme() && ! self::is_themes_page() )
13418
+ ) {
13419
+ // Only show tracking links on the plugins and themes pages.
13420
+ return;
13421
+ }
13422
+
13423
+ if ( ! $this->is_enable_anonymous() ) {
13424
+ // Don't allow to opt-out if anonymous mode is disabled.
13425
+ return;
13426
+ }
13427
+
13428
+ if ( ! $this->is_free_plan() ) {
13429
+ // Don't allow to opt-out if running in paid plan.
13430
+ return;
13431
+ }
13432
+
13433
+ if ( $this->add_ajax_action( 'stop_tracking', array( &$this, '_stop_tracking_callback' ) ) ) {
13434
+ return;
13435
+ }
13436
+
13437
+ if ( $this->add_ajax_action( 'allow_tracking', array( &$this, '_allow_tracking_callback' ) ) ) {
13438
+ return;
13439
+ }
13440
+
13441
+ $url = '#';
13442
+
13443
+ if ( $this->is_registered() ) {
13444
+ if ( $this->is_tracking_allowed() ) {
13445
+ $link_text_id = $this->get_text_inline( 'Opt Out', 'opt-out' );
13446
+ } else {
13447
+ $link_text_id = $this->get_text_inline( 'Opt In', 'opt-in' );
13448
+ }
13449
+
13450
+ add_action( 'admin_footer', array( &$this, '_add_optout_dialog' ) );
13451
+ } else {
13452
+ $link_text_id = $this->get_text_inline( 'Opt In', 'opt-in' );
13453
+
13454
+ $params = ! $this->is_anonymous() ?
13455
+ array() :
13456
+ array(
13457
+ 'nonce' => wp_create_nonce( $this->get_unique_affix() . '_reconnect' ),
13458
+ 'fs_action' => ( $this->get_unique_affix() . '_reconnect' ),
13459
+ );
13460
+
13461
+ $url = $this->get_activation_url( $params );
13462
+ }
13463
+
13464
+ if ( $this->is_plugin() && self::is_plugins_page() ) {
13465
+ $this->add_plugin_action_link(
13466
+ $link_text_id,
13467
+ $url,
13468
+ false,
13469
+ 13,
13470
+ "opt-in-or-opt-out {$this->_slug}"
13471
+ );
13472
+ }
13473
+ }
13474
+
13475
+ /**
13476
+ * Get the URL of the page that should be loaded right after the plugin activation.
13477
+ *
13478
+ * @author Vova Feldman (@svovaf)
13479
+ * @since 1.1.7.4
13480
+ *
13481
+ * @return string
13482
+ */
13483
+ function get_after_plugin_activation_redirect_url() {
13484
+ $url = false;
13485
+
13486
+ if ( ! $this->is_addon() || ! $this->has_free_plan() ) {
13487
+ $first_time_path = $this->_menu->get_first_time_path();
13488
+ $url = $this->is_activation_mode() ?
13489
+ $this->get_activation_url() :
13490
+ ( empty( $first_time_path ) ?
13491
+ $this->_get_admin_page_url() :
13492
+ $first_time_path );
13493
+ } else {
13494
+ $plugin_fs = false;
13495
+
13496
+ if ( $this->is_parent_plugin_installed() ) {
13497
+ $plugin_fs = self::get_parent_instance();
13498
+ }
13499
+
13500
+ if ( is_object( $plugin_fs ) ) {
13501
+ if ( ! $plugin_fs->is_registered() ) {
13502
+ // Forward to parent plugin connect when parent not registered.
13503
+ $url = $plugin_fs->get_activation_url();
13504
+ } else {
13505
+ // Forward to account page.
13506
+ $url = $plugin_fs->_get_admin_page_url( 'account' );
13507
+ }
13508
+ }
13509
+ }
13510
+
13511
+ return $url;
13512
+ }
13513
+
13514
+ /**
13515
+ * Forward page to activation page.
13516
+ *
13517
+ * @author Vova Feldman (@svovaf)
13518
+ * @since 1.0.3
13519
+ */
13520
+ function _redirect_on_activation_hook() {
13521
+ $url = $this->get_after_plugin_activation_redirect_url();
13522
+
13523
+ if ( is_string( $url ) ) {
13524
+ fs_redirect( $url );
13525
+ }
13526
+ }
13527
+
13528
+ /**
13529
+ * Modify plugin's page action links collection.
13530
+ *
13531
+ * @author Vova Feldman (@svovaf)
13532
+ * @since 1.0.0
13533
+ *
13534
+ * @param array $links
13535
+ * @param $file
13536
+ *
13537
+ * @return array
13538
+ */
13539
+ function _modify_plugin_action_links_hook( $links, $file ) {
13540
+ $this->_logger->entrance();
13541
+
13542
+ $passed_deactivate = false;
13543
+ $deactivate_link = '';
13544
+ $before_deactivate = array();
13545
+ $after_deactivate = array();
13546
+ foreach ( $links as $key => $link ) {
13547
+ if ( 'deactivate' === $key ) {
13548
+ $deactivate_link = $link;
13549
+ $passed_deactivate = true;
13550
+ continue;
13551
+ }
13552
+
13553
+ if ( ! $passed_deactivate ) {
13554
+ $before_deactivate[ $key ] = $link;
13555
+ } else {
13556
+ $after_deactivate[ $key ] = $link;
13557
+ }
13558
+ }
13559
+
13560
+ ksort( $this->_action_links );
13561
+
13562
+ foreach ( $this->_action_links as $new_links ) {
13563
+ foreach ( $new_links as $link ) {
13564
+ $before_deactivate[ $link['key'] ] = '<a href="' . $link['href'] . '"' . ( $link['external'] ? ' target="_blank"' : '' ) . '>' . $link['label'] . '</a>';
13565
+ }
13566
+ }
13567
+
13568
+ if ( ! empty( $deactivate_link ) ) {
13569
+ /**
13570
+ * This HTML element is used to identify the correct plugin when attaching an event to its Deactivate link.
13571
+ *
13572
+ * @since 1.2.1.6 Always show the deactivation feedback form since we added automatic free version deactivation upon premium code activation.
13573
+ */
13574
+ $deactivate_link .= '<i class="fs-module-id" data-module-id="' . $this->_module_id . '"></i>';
13575
+
13576
+ // Append deactivation link.
13577
+ $before_deactivate['deactivate'] = $deactivate_link;
13578
+ }
13579
+
13580
+ return array_merge( $before_deactivate, $after_deactivate );
13581
+ }
13582
+
13583
+ /**
13584
+ * Adds admin message.
13585
+ *
13586
+ * @author Vova Feldman (@svovaf)
13587
+ * @since 1.0.4
13588
+ *
13589
+ * @param string $message
13590
+ * @param string $title
13591
+ * @param string $type
13592
+ */
13593
+ function add_admin_message( $message, $title = '', $type = 'success' ) {
13594
+ $this->_admin_notices->add( $message, $title, $type );
13595
+ }
13596
+
13597
+ /**
13598
+ * Adds sticky admin message.
13599
+ *
13600
+ * @author Vova Feldman (@svovaf)
13601
+ * @since 1.1.0
13602
+ *
13603
+ * @param string $message
13604
+ * @param string $id
13605
+ * @param string $title
13606
+ * @param string $type
13607
+ */
13608
+ function add_sticky_admin_message( $message, $id, $title = '', $type = 'success' ) {
13609
+ $this->_admin_notices->add_sticky( $message, $id, $title, $type );
13610
+ }
13611
+
13612
+ /**
13613
+ * Helper function that returns the final steps for the upgrade completion.
13614
+ *
13615
+ * If the module is already running the premium code, returns an empty string.
13616
+ *
13617
+ * @author Vova Feldman (@svovaf)
13618
+ * @since 1.2.1
13619
+ *
13620
+ * @param string $plan_title
13621
+ *
13622
+ * @return string
13623
+ */
13624
+ private function get_complete_upgrade_instructions( $plan_title = '' ) {
13625
+ if ( ! $this->has_premium_version() || $this->is_premium() ) {
13626
+ return '';
13627
+ }
13628
+
13629
+ if ( empty( $plan_title ) ) {
13630
+ $plan_title = $this->_site->plan->title;
13631
+ }
13632
+
13633
+ // @since 1.2.1.5 The free version is auto deactivated.
13634
+ $deactivation_step = version_compare( $this->version, '1.2.1.5', '<' ) ?
13635
+ ( '<li>' . $this->esc_html_inline( 'Deactivate the free version', 'deactivate-free-version' ) . '.</li>' ) :
13636
+ '';
13637
+
13638
+ return sprintf(
13639
+ ' %s: <ol><li>%s.</li>%s<li>%s (<a href="%s" target="_blank">%s</a>).</li></ol>',
13640
+ $this->get_text_inline( 'Please follow these steps to complete the upgrade', 'follow-steps-to-complete-upgrade' ),
13641
+ $this->get_latest_download_link( sprintf(
13642
+ /* translators: %s: Plan title */
13643
+ $this->get_text_inline( 'Download the latest %s version', 'download-latest-x-version' ),
13644
+ $plan_title
13645
+ ) ),
13646
+ $deactivation_step,
13647
+ $this->get_text_inline( 'Upload and activate the downloaded version', 'upload-and-activate' ),
13648
+ '//bit.ly/upload-wp-' . $this->_module_type . 's',
13649
+ $this->get_text_inline( 'How to upload and activate?', 'howto-upload-activate' )
13650
+ );
13651
+ }
13652
+
13653
+ /**
13654
+ * @author Vova Feldman (@svovaf)
13655
+ * @since 1.2.1.7
13656
+ *
13657
+ * @param string $key
13658
+ *
13659
+ * @return string
13660
+ */
13661
+ function get_text( $key ) {
13662
+ return fs_text( $key, $this->_slug );
13663
+ }
13664
+
13665
+ /**
13666
+ * @author Vova Feldman (@svovaf)
13667
+ * @since 1.2.3
13668
+ *
13669
+ * @param string $text Translatable string.
13670
+ * @param string $key String key for overrides.
13671
+ *
13672
+ * @return string
13673
+ */
13674
+ function get_text_inline( $text, $key = '' ) {
13675
+ return _fs_text_inline( $text, $key, $this->_slug );
13676
+ }
13677
+
13678
+ /**
13679
+ * @author Vova Feldman (@svovaf)
13680
+ * @since 1.2.3
13681
+ *
13682
+ * @param string $text Translatable string.
13683
+ * @param string $context Context information for the translators.
13684
+ * @param string $key String key for overrides.
13685
+ *
13686
+ * @return string
13687
+ */
13688
+ function get_text_x_inline( $text, $context, $key ) {
13689
+ return _fs_text_x_inline( $text, $context, $key, $this->_slug );
13690
+ }
13691
+
13692
+ /**
13693
+ * @author Vova Feldman (@svovaf)
13694
+ * @since 1.2.3
13695
+ *
13696
+ * @param string $text Translatable string.
13697
+ * @param string $key String key for overrides.
13698
+ *
13699
+ * @return string
13700
+ */
13701
+ function esc_html_inline( $text, $key ) {
13702
+ return esc_html( _fs_text_inline( $text, $key, $this->_slug ) );
13703
+ }
13704
+
13705
+ #----------------------------------------------------------------------------------
13706
+ #region Versioning
13707
+ #----------------------------------------------------------------------------------
13708
+
13709
+ /**
13710
+ * Check if Freemius in SDK upgrade mode.
13711
+ *
13712
+ * @author Vova Feldman (@svovaf)
13713
+ * @since 1.0.9
13714
+ *
13715
+ * @return bool
13716
+ */
13717
+ function is_sdk_upgrade_mode() {
13718
+ return isset( $this->_storage->sdk_upgrade_mode ) ?
13719
+ $this->_storage->sdk_upgrade_mode :
13720
+ false;
13721
+ }
13722
+
13723
+ /**
13724
+ * Turn SDK upgrade mode off.
13725
+ *
13726
+ * @author Vova Feldman (@svovaf)
13727
+ * @since 1.0.9
13728
+ */
13729
+ function set_sdk_upgrade_complete() {
13730
+ $this->_storage->sdk_upgrade_mode = false;
13731
+ }
13732
+
13733
+ /**
13734
+ * Check if plugin upgrade mode.
13735
+ *
13736
+ * @author Vova Feldman (@svovaf)
13737
+ * @since 1.0.9
13738
+ *
13739
+ * @return bool
13740
+ */
13741
+ function is_plugin_upgrade_mode() {
13742
+ return isset( $this->_storage->plugin_upgrade_mode ) ?
13743
+ $this->_storage->plugin_upgrade_mode :
13744
+ false;
13745
+ }
13746
+
13747
+ /**
13748
+ * Turn plugin upgrade mode off.
13749
+ *
13750
+ * @author Vova Feldman (@svovaf)
13751
+ * @since 1.0.9
13752
+ */
13753
+ function set_plugin_upgrade_complete() {
13754
+ $this->_storage->plugin_upgrade_mode = false;
13755
+ }
13756
+
13757
+ #endregion
13758
+
13759
+ #----------------------------------------------------------------------------------
13760
+ #region Permissions
13761
+ #----------------------------------------------------------------------------------
13762
+
13763
+ /**
13764
+ * Check if specific permission requested.
13765
+ *
13766
+ * @author Vova Feldman (@svovaf)
13767
+ * @since 1.1.6
13768
+ *
13769
+ * @param string $permission
13770
+ *
13771
+ * @return bool
13772
+ */
13773
+ function is_permission_requested( $permission ) {
13774
+ return isset( $this->_permissions[ $permission ] ) && ( true === $this->_permissions[ $permission ] );
13775
+ }
13776
+
13777
+ #endregion
13778
+
13779
+ #----------------------------------------------------------------------------------
13780
+ #region Auto Activation
13781
+ #----------------------------------------------------------------------------------
13782
+
13783
+ /**
13784
+ * Hints the SDK if running an auto-installation.
13785
+ *
13786
+ * @var bool
13787
+ */
13788
+ private $_isAutoInstall = false;
13789
+
13790
+ /**
13791
+ * After upgrade callback to install and auto activate a plugin.
13792
+ * This code will only be executed on explicit request from the user,
13793
+ * following the practice Jetpack are using with their theme installations.
13794
+ *
13795
+ * @link https://make.wordpress.org/plugins/2017/03/16/clarification-of-guideline-8-executable-code-and-installs/
13796
+ *
13797
+ * @author Vova Feldman (@svovaf)
13798
+ * @since 1.2.1.7
13799
+ */
13800
+ function _install_premium_version_ajax_action() {
13801
+ $this->_logger->entrance();
13802
+
13803
+ $this->check_ajax_referer( 'install_premium_version' );
13804
+
13805
+ if ( ! $this->is_registered() ) {
13806
+ // Not registered.
13807
+ self::shoot_ajax_failure( array(
13808
+ 'message' => $this->get_text_inline( 'Auto installation only works for opted-in users.', 'auto-install-error-not-opted-in' ),
13809
+ 'code' => 'premium_installed',
13810
+ ) );
13811
+ }
13812
+
13813
+ $plugin_id = fs_request_get( 'target_module_id', $this->get_id() );
13814
+
13815
+ if ( ! FS_Plugin::is_valid_id( $plugin_id ) ) {
13816
+ // Invalid ID.
13817
+ self::shoot_ajax_failure( array(
13818
+ 'message' => $this->get_text_inline( 'Invalid module ID.', 'auto-install-error-invalid-id' ),
13819
+ 'code' => 'invalid_module_id',
13820
+ ) );
13821
+ }
13822
+
13823
+ if ( $plugin_id == $this->get_id() ) {
13824
+ if ( $this->is_premium() ) {
13825
+ // Already using the premium code version.
13826
+ self::shoot_ajax_failure( array(
13827
+ 'message' => $this->get_text_inline( 'Premium version already active.', 'auto-install-error-premium-activated' ),
13828
+ 'code' => 'premium_installed',
13829
+ ) );
13830
+ }
13831
+ if ( ! $this->can_use_premium_code() ) {
13832
+ // Don't have access to the premium code.
13833
+ self::shoot_ajax_failure( array(
13834
+ 'message' => $this->get_text_inline( 'You do not have a valid license to access the premium version.', 'auto-install-error-invalid-license' ),
13835
+ 'code' => 'invalid_license',
13836
+ ) );
13837
+ }
13838
+ if ( ! $this->has_release_on_freemius() ) {
13839
+ // Plugin is a serviceware, no premium code version.
13840
+ self::shoot_ajax_failure( array(
13841
+ 'message' => $this->get_text_inline( 'Plugin is a "Serviceware" which means it does not have a premium code version.', 'auto-install-error-serviceware' ),
13842
+ 'code' => 'premium_version_missing',
13843
+ ) );
13844
+ }
13845
+ } else {
13846
+ $addon = $this->get_addon( $plugin_id );
13847
+
13848
+ if ( ! is_object( $addon ) ) {
13849
+ // Invalid add-on ID.
13850
+ self::shoot_ajax_failure( array(
13851
+ 'message' => $this->get_text_inline( 'Invalid module ID.', 'auto-install-error-invalid-id' ),
13852
+ 'code' => 'invalid_module_id',
13853
+ ) );
13854
+ }
13855
+
13856
+ if ( $this->is_addon_activated( $plugin_id, true ) ) {
13857
+ // Premium add-on version is already activated.
13858
+ self::shoot_ajax_failure( array(
13859
+ 'message' => $this->get_text_inline( 'Premium add-on version already installed.', 'auto-install-error-premium-addon-activated' ),
13860
+ 'code' => 'premium_installed',
13861
+ ) );
13862
+ }
13863
+ }
13864
+
13865
+ $this->_isAutoInstall = true;
13866
+
13867
+ // Try to install and activate.
13868
+ $updater = new FS_Plugin_Updater( $this );
13869
+ $result = $updater->install_and_activate_plugin( $plugin_id );
13870
+
13871
+ if ( is_array( $result ) && ! empty( $result['message'] ) ) {
13872
+ self::shoot_ajax_failure( array(
13873
+ 'message' => $result['message'],
13874
+ 'code' => $result['code'],
13875
+ ) );
13876
+ }
13877
+
13878
+ self::shoot_ajax_success( $result );
13879
+ }
13880
+
13881
+ /**
13882
+ * Displays module activation dialog box after a successful upgrade
13883
+ * where the user explicitly requested to auto download and install
13884
+ * the premium version.
13885
+ *
13886
+ * @author Vova Feldman (@svovaf)
13887
+ * @since 1.2.1.7
13888
+ */
13889
+ function _add_auto_installation_dialog_box() {
13890
+ $this->_logger->entrance();
13891
+
13892
+ if ( ! $this->is_registered() ) {
13893
+ // Not registered.
13894
+ return;
13895
+ }
13896
+
13897
+ $plugin_id = fs_request_get( 'plugin_id', $this->get_id() );
13898
+
13899
+ if ( ! FS_Plugin::is_valid_id( $plugin_id ) ) {
13900
+ // Invalid module ID.
13901
+ return;
13902
+ }
13903
+
13904
+ if ( $plugin_id == $this->get_id() ) {
13905
+ if ( $this->is_premium() ) {
13906
+ // Already using the premium code version.
13907
+ return;
13908
+ }
13909
+ if ( ! $this->can_use_premium_code() ) {
13910
+ // Don't have access to the premium code.
13911
+ return;
13912
+ }
13913
+ if ( ! $this->has_release_on_freemius() ) {
13914
+ // Plugin is a serviceware, no premium code version.
13915
+ return;
13916
+ }
13917
+ } else {
13918
+ $addon = $this->get_addon( $plugin_id );
13919
+
13920
+ if ( ! is_object( $addon ) ) {
13921
+ // Invalid add-on ID.
13922
+ return;
13923
+ }
13924
+
13925
+ if ( $this->is_addon_activated( $plugin_id, true ) ) {
13926
+ // Premium add-on version is already activated.
13927
+ return;
13928
+ }
13929
+ }
13930
+
13931
+ $vars = array(
13932
+ 'id' => $this->_module_id,
13933
+ 'target_module_id' => $plugin_id,
13934
+ 'slug' => $this->_slug,
13935
+ );
13936
+
13937
+ fs_require_template( 'auto-installation.php', $vars );
13938
+ }
13939
+
13940
+ #endregion
13941
+
13942
+ #--------------------------------------------------------------------------------
13943
+ #region Tabs Integration
13944
+ #--------------------------------------------------------------------------------
13945
+
13946
+ #region Module's Original Tabs
13947
+
13948
+ /**
13949
+ * Inject a JavaScript logic to capture the theme tabs HTML.
13950
+ *
13951
+ * @author Vova Feldman (@svovaf)
13952
+ * @since 1.2.2.7
13953
+ */
13954
+ function _tabs_capture() {
13955
+ $this->_logger->entrance();
13956
+
13957
+ if ( ! $this->is_theme_settings_page() ||
13958
+ ! $this->is_matching_url( $this->main_menu_url() )
13959
+ ) {
13960
+ return;
13961
+ }
13962
+
13963
+ $params = array(
13964
+ 'id' => $this->_module_id,
13965
+ );
13966
+
13967
+ fs_require_once_template( 'tabs-capture-js.php', $params );
13968
+ }
13969
+
13970
+ /**
13971
+ * Cache theme's tabs HTML for a week. The cache will also be set as expired
13972
+ * after version and type (free/premium) changes, in addition to the week period.
13973
+ *
13974
+ * @author Vova Feldman (@svovaf)
13975
+ * @since 1.2.2.7
13976
+ */
13977
+ function _store_tabs_ajax_action() {
13978
+ $this->_logger->entrance();
13979
+
13980
+ $this->check_ajax_referer( 'store_tabs' );
13981
+
13982
+ // Init filesystem if not yet initiated.
13983
+ WP_Filesystem();
13984
+
13985
+ // Get POST body HTML data.
13986
+ global $wp_filesystem;
13987
+ $tabs_html = $wp_filesystem->get_contents( "php://input" );
13988
+
13989
+ if ( is_string( $tabs_html ) ) {
13990
+ $tabs_html = trim( $tabs_html );
13991
+ }
13992
+
13993
+ if ( ! is_string( $tabs_html ) || empty( $tabs_html ) ) {
13994
+ self::shoot_ajax_failure();
13995
+ }
13996
+
13997
+ $this->_cache->set( 'tabs', $tabs_html, 7 * WP_FS__TIME_24_HOURS_IN_SEC );
13998
+
13999
+ self::shoot_ajax_success();
14000
+ }
14001
+
14002
+ /**
14003
+ * Cache theme's settings page custom styles. The cache will also be set as expired
14004
+ * after version and type (free/premium) changes, in addition to the week period.
14005
+ *
14006
+ * @author Vova Feldman (@svovaf)
14007
+ * @since 1.2.2.7
14008
+ */
14009
+ function _store_tabs_styles() {
14010
+ $this->_logger->entrance();
14011
+
14012
+ if ( ! $this->is_theme_settings_page() ||
14013
+ ! $this->is_matching_url( $this->main_menu_url() )
14014
+ ) {
14015
+ return;
14016
+ }
14017
+
14018
+ $wp_styles = wp_styles();
14019
+
14020
+ $theme_styles_url = get_template_directory_uri();
14021
+
14022
+ $stylesheets = array();
14023
+ foreach ( $wp_styles->queue as $handler ) {
14024
+ if ( fs_starts_with( $handler, 'fs_' ) ) {
14025
+ // Assume that stylesheets that their handler starts with "fs_" belong to the SDK.
14026
  continue;
14027
  }
14028
 
14029
+ /**
14030
+ * @var _WP_Dependency $stylesheet
14031
+ */
14032
+ $stylesheet = $wp_styles->registered[ $handler ];
14033
+
14034
+ if ( fs_starts_with( $stylesheet->src, $theme_styles_url ) ) {
14035
+ $stylesheets[] = $stylesheet->src;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14036
  }
14037
+ }
14038
 
14039
+ if ( ! empty( $stylesheets ) ) {
14040
+ $this->_cache->set( 'tabs_stylesheets', $stylesheets, 7 * WP_FS__TIME_24_HOURS_IN_SEC );
14041
+ }
14042
+ }
14043
 
14044
+ /**
14045
+ * Check if module's original settings page has any tabs.
14046
+ *
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14047
  * @author Vova Feldman (@svovaf)
14048
+ * @since 1.2.2.7
14049
  *
14050
+ * @return bool
14051
  */
14052
+ private function has_tabs() {
14053
+ return $this->_cache->has( 'tabs' );
14054
+ }
14055
 
14056
  /**
14057
+ * Get module's settings page HTML content, starting
14058
+ * from the beginning of the <div class="wrap"> element,
14059
+ * until the tabs HTML (including).
14060
  *
14061
  * @author Vova Feldman (@svovaf)
14062
+ * @since 1.2.2.7
14063
  *
14064
  * @return string
14065
  */
14066
+ private function get_tabs_html() {
14067
+ $this->_logger->entrance();
 
14068
 
14069
+ return $this->_cache->get( 'tabs' );
14070
+ }
14071
 
14072
+ /**
14073
+ * Check if page should include tabs.
14074
+ *
14075
+ * @author Vova Feldman (@svovaf)
14076
+ * @since 1.2.2.7
14077
+ *
14078
+ * @return bool
14079
+ */
14080
+ private function should_page_include_tabs()
14081
+ {
14082
+ if ( ! $this->has_settings_menu() ) {
14083
+ // Don't add tabs if no settings at all.
14084
+ return false;
14085
+ }
14086
 
14087
+ if ( ! $this->is_theme() ) {
14088
+ // Only add tabs to themes for now.
14089
+ return false;
14090
+ }
14091
 
14092
+ if ( ! $this->has_paid_plan() && ! $this->has_addons() ) {
14093
+ // Only add tabs to monetizing themes.
14094
+ return false;
14095
  }
14096
 
14097
+ if ( ! $this->is_theme_settings_page() ) {
14098
+ // Only add tabs if browsing one of the theme's setting pages.
14099
+ return false;
14100
+ }
14101
+
14102
+ if ( $this->is_admin_page( 'pricing' ) && fs_request_get_bool( 'checkout' ) ) {
14103
+ // Don't add tabs on checkout page, we want to reduce distractions
14104
+ // as much as possible.
14105
+ return false;
14106
+ }
14107
+
14108
+ return true;
14109
  }
14110
 
14111
  /**
14112
+ * Add the tabs HTML before the setting's page content and
14113
+ * enqueue any required stylesheets.
14114
+ *
14115
+ * @author Vova Feldman (@svovaf)
14116
+ * @since 1.2.2.7
14117
+ *
14118
+ * @return bool If tabs were included.
14119
+ */
14120
+ function _add_tabs_before_content() {
14121
+ $this->_logger->entrance();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14122
 
14123
+ if ( ! $this->should_page_include_tabs() ) {
14124
+ return false;
14125
+ }
14126
+
14127
+ /**
14128
+ * Enqueue the original stylesheets that are included in the
14129
+ * theme settings page. That way, if the theme settings has
14130
+ * some custom _styled_ content above the tabs UI, this
14131
+ * will make sure that the styling is preserved.
14132
+ */
14133
+ $stylesheets = $this->_cache->get( 'tabs_stylesheets', array() );
14134
+ if ( is_array( $stylesheets ) ) {
14135
+ for ( $i = 0, $len = count( $stylesheets ); $i < $len; $i ++ ) {
14136
+ wp_enqueue_style( "fs_{$this->_module_id}_tabs_{$i}", $stylesheets[ $i ] );
14137
  }
14138
  }
14139
 
14140
+ // Cut closing </div> tag.
14141
+ echo substr( trim( $this->get_tabs_html() ), 0, - 6 );
14142
+
14143
+ return true;
14144
+ }
14145
+
14146
+ /**
14147
+ * Add the tabs closing HTML after the setting's page content.
14148
+ *
14149
+ * @author Vova Feldman (@svovaf)
14150
+ * @since 1.2.2.7
14151
+ *
14152
+ * @return bool If tabs closing HTML was included.
14153
+ */
14154
+ function _add_tabs_after_content() {
14155
+ $this->_logger->entrance();
14156
+
14157
+ if ( ! $this->should_page_include_tabs() ) {
14158
+ return false;
14159
+ }
14160
+
14161
+ echo '</div>';
14162
+
14163
+ return true;
14164
+ }
14165
+
14166
+ #endregion
14167
+
14168
+ /**
14169
+ * Add in-page JavaScript to inject the Freemius tabs into
14170
+ * the module's setting tabs section.
14171
+ *
14172
+ * @author Vova Feldman (@svovaf)
14173
+ * @since 1.2.2.7
14174
+ */
14175
+ function _add_freemius_tabs() {
14176
+ $this->_logger->entrance();
14177
+
14178
+ if ( ! $this->should_page_include_tabs() ) {
14179
+ return;
14180
+ }
14181
+
14182
+ $params = array( 'id' => $this->_module_id );
14183
+ fs_require_once_template( 'tabs.php', $params );
14184
+ }
14185
+
14186
+ #endregion
14187
+
14188
+ #--------------------------------------------------------------------------------
14189
+ #region Customizer Integration for Themes
14190
+ #--------------------------------------------------------------------------------
14191
+
14192
+ /**
14193
+ * @author Vova Feldman (@svovaf)
14194
+ * @since 1.2.2.7
14195
+ *
14196
+ * @param WP_Customize_Manager $customizer
14197
+ */
14198
+ function _customizer_register($customizer) {
14199
+ $this->_logger->entrance();
14200
+
14201
+ if ( $this->is_pricing_page_visible() ) {
14202
+ require_once WP_FS__DIR_INCLUDES . '/customizer/class-fs-customizer-upsell-control.php';
14203
+
14204
+ $customizer->add_section( 'freemius_upsell', array(
14205
+ 'title' => '&#9733; ' . $this->get_text_inline( 'View paid features', 'view-paid-features' ),
14206
+ 'priority' => 1,
14207
+ ) );
14208
+ $customizer->add_setting( 'freemius_upsell', array(
14209
+ 'sanitize_callback' => 'esc_html',
14210
+ ) );
14211
+
14212
+ $customizer->add_control( new FS_Customizer_Upsell_Control( $customizer, 'freemius_upsell', array(
14213
+ 'fs' => $this,
14214
+ 'section' => 'freemius_upsell',
14215
+ 'priority' => 100,
14216
+ ) ) );
14217
+ }
14218
+
14219
+ if ( $this->is_page_visible( 'contact' ) || $this->is_page_visible( 'support' ) ) {
14220
+ require_once WP_FS__DIR_INCLUDES . '/customizer/class-fs-customizer-support-section.php';
14221
+
14222
+ // Main Documentation Link In Customizer Root.
14223
+ $customizer->add_section( new FS_Customizer_Support_Section( $customizer, 'freemius_support', array(
14224
+ 'fs' => $this,
14225
+ 'priority' => 1000,
14226
+ ) ) );
14227
+ }
14228
+ }
14229
+
14230
+ #endregion
14231
+
14232
+ /**
14233
+ * If the theme has a paid version, add some custom
14234
+ * styling to the theme's premium version (if exists)
14235
+ * to highlight that it's the premium version of the
14236
+ * same theme, making it easier for identification
14237
+ * after the user upgrades and upload it to the site.
14238
+ *
14239
+ * @author Vova Feldman (@svovaf)
14240
+ * @since 1.2.2.7
14241
+ */
14242
+ function _style_premium_theme() {
14243
+ $this->_logger->entrance();
14244
+
14245
+ if ( ! self::is_themes_page() ) {
14246
+ // Only include in the themes page.
14247
+ return;
14248
+ }
14249
+
14250
+ if ( ! $this->has_paid_plan() ) {
14251
+ // Only include if has any paid plans.
14252
  return;
14253
  }
14254
 
14255
+ $params = null;
14256
+ fs_require_once_template( '/js/jquery.content-change.php', $params );
14257
+
14258
+ $params = array(
14259
+ 'slug' => $this->_slug,
14260
+ 'id' => $this->_module_id,
14261
+ );
14262
+
14263
+ fs_require_template( '/js/style-premium-theme.php', $params );
14264
+ }
14265
+
14266
+ #----------------------------------------------------------------------------------
14267
+ #region Marketing
14268
+ #----------------------------------------------------------------------------------
14269
+
14270
+ /**
14271
+ * Check if current user purchased any other plugins before.
14272
+ *
14273
+ * @author Vova Feldman (@svovaf)
14274
+ * @since 1.0.9
14275
+ *
14276
+ * @return bool
14277
+ */
14278
+ function has_purchased_before() {
14279
+ // TODO: Implement has_purchased_before() method.
14280
+ throw new Exception( 'not implemented' );
14281
+ }
14282
+
14283
+ /**
14284
+ * Check if current user classified as an agency.
14285
+ *
14286
+ * @author Vova Feldman (@svovaf)
14287
+ * @since 1.0.9
14288
+ *
14289
+ * @return bool
14290
+ */
14291
+ function is_agency() {
14292
+ // TODO: Implement is_agency() method.
14293
+ throw new Exception( 'not implemented' );
14294
+ }
14295
+
14296
+ /**
14297
+ * Check if current user classified as a developer.
14298
+ *
14299
+ * @author Vova Feldman (@svovaf)
14300
+ * @since 1.0.9
14301
+ *
14302
+ * @return bool
14303
+ */
14304
+ function is_developer() {
14305
+ // TODO: Implement is_developer() method.
14306
+ throw new Exception( 'not implemented' );
14307
+ }
14308
+
14309
+ /**
14310
+ * Check if current user classified as a business.
14311
+ *
14312
+ * @author Vova Feldman (@svovaf)
14313
+ * @since 1.0.9
14314
+ *
14315
+ * @return bool
14316
+ */
14317
+ function is_business() {
14318
+ // TODO: Implement is_business() method.
14319
+ throw new Exception( 'not implemented' );
14320
+ }
14321
+
14322
+ #endregion
14323
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
freemius/includes/class-fs-api.php CHANGED
@@ -1,558 +1,558 @@
1
- <?php
2
- /**
3
- * @package Freemius
4
- * @copyright Copyright (c) 2015, Freemius, Inc.
5
- * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
6
- * @since 1.0.4
7
- */
8
-
9
- if ( ! defined( 'ABSPATH' ) ) {
10
- exit;
11
- }
12
-
13
- /**
14
- * Class FS_Api
15
- *
16
- * Wraps Freemius API SDK to handle:
17
- * 1. Clock sync.
18
- * 2. Fallback to HTTP when HTTPS fails.
19
- * 3. Adds caching layer to GET requests.
20
- * 4. Adds consistency for failed requests by using last cached version.
21
- */
22
- class FS_Api {
23
- /**
24
- * @var FS_Api[]
25
- */
26
- private static $_instances = array();
27
-
28
- /**
29
- * @var FS_Option_Manager Freemius options, options-manager.
30
- */
31
- private static $_options;
32
-
33
- /**
34
- * @var FS_Cache_Manager API Caching layer
35
- */
36
- private static $_cache;
37
-
38
- /**
39
- * @var int Clock diff in seconds between current server to API server.
40
- */
41
- private static $_clock_diff;
42
-
43
- /**
44
- * @var Freemius_Api
45
- */
46
- private $_api;
47
-
48
- /**
49
- * @var string
50
- */
51
- private $_slug;
52
-
53
- /**
54
- * @var FS_Logger
55
- * @since 1.0.4
56
- */
57
- private $_logger;
58
-
59
- /**
60
- * @param string $slug
61
- * @param string $scope 'app', 'developer', 'user' or 'install'.
62
- * @param number $id Element's id.
63
- * @param string $public_key Public key.
64
- * @param bool $is_sandbox
65
- * @param bool|string $secret_key Element's secret key.
66
- *
67
- * @return FS_Api
68
- */
69
- static function instance( $slug, $scope, $id, $public_key, $is_sandbox, $secret_key = false ) {
70
- $identifier = md5( $slug . $scope . $id . $public_key . ( is_string( $secret_key ) ? $secret_key : '' ) . json_encode( $is_sandbox ) );
71
-
72
- if ( ! isset( self::$_instances[ $identifier ] ) ) {
73
- self::_init();
74
-
75
- self::$_instances[ $identifier ] = new FS_Api( $slug, $scope, $id, $public_key, $secret_key, $is_sandbox );
76
- }
77
-
78
- return self::$_instances[ $identifier ];
79
- }
80
-
81
- private static function _init() {
82
- if ( isset( self::$_options ) ) {
83
- return;
84
- }
85
-
86
- if ( ! class_exists( 'Freemius_Api' ) ) {
87
- require_once WP_FS__DIR_SDK . '/Freemius.php';
88
- }
89
-
90
- self::$_options = FS_Option_Manager::get_manager( WP_FS__OPTIONS_OPTION_NAME, true );
91
- self::$_cache = FS_Cache_Manager::get_manager( WP_FS__API_CACHE_OPTION_NAME );
92
-
93
- self::$_clock_diff = self::$_options->get_option( 'api_clock_diff', 0 );
94
- Freemius_Api::SetClockDiff( self::$_clock_diff );
95
-
96
- if ( self::$_options->get_option( 'api_force_http', false ) ) {
97
- Freemius_Api::SetHttp();
98
- }
99
- }
100
-
101
- /**
102
- * @param string $slug
103
- * @param string $scope 'app', 'developer', 'user' or 'install'.
104
- * @param number $id Element's id.
105
- * @param string $public_key Public key.
106
- * @param bool|string $secret_key Element's secret key.
107
- * @param bool $is_sandbox
108
- */
109
- private function __construct( $slug, $scope, $id, $public_key, $secret_key, $is_sandbox ) {
110
- $this->_api = new Freemius_Api( $scope, $id, $public_key, $secret_key, $is_sandbox );
111
-
112
- $this->_slug = $slug;
113
- $this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_' . $slug . '_api', WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
114
- }
115
-
116
- /**
117
- * Find clock diff between server and API server, and store the diff locally.
118
- *
119
- * @param bool|int $diff
120
- *
121
- * @return bool|int False if clock diff didn't change, otherwise returns the clock diff in seconds.
122
- */
123
- private function _sync_clock_diff( $diff = false ) {
124
- $this->_logger->entrance();
125
-
126
- // Sync clock and store.
127
- $new_clock_diff = ( false === $diff ) ?
128
- Freemius_Api::FindClockDiff() :
129
- $diff;
130
-
131
- if ( $new_clock_diff === self::$_clock_diff ) {
132
- return false;
133
- }
134
-
135
- self::$_clock_diff = $new_clock_diff;
136
-
137
- // Update API clock's diff.
138
- Freemius_Api::SetClockDiff( self::$_clock_diff );
139
-
140
- // Store new clock diff in storage.
141
- self::$_options->set_option( 'api_clock_diff', self::$_clock_diff, true );
142
-
143
- return $new_clock_diff;
144
- }
145
-
146
- /**
147
- * Override API call to enable retry with servers' clock auto sync method.
148
- *
149
- * @param string $path
150
- * @param string $method
151
- * @param array $params
152
- * @param bool $retry Is in retry or first call attempt.
153
- *
154
- * @return array|mixed|string|void
155
- */
156
- private function _call( $path, $method = 'GET', $params = array(), $retry = false ) {
157
- $this->_logger->entrance( $method . ':' . $path );
158
-
159
- if ( self::is_temporary_down() ) {
160
- $result = $this->get_temporary_unavailable_error();
161
- } else {
162
- $result = $this->_api->Api( $path, $method, $params );
163
-
164
- if ( null !== $result &&
165
- isset( $result->error ) &&
166
- isset( $result->error->code ) &&
167
- 'request_expired' === $result->error->code
168
- ) {
169
- if ( ! $retry ) {
170
- $diff = isset( $result->error->timestamp ) ?
171
- ( time() - strtotime( $result->error->timestamp ) ) :
172
- false;
173
-
174
- // Try to sync clock diff.
175
- if ( false !== $this->_sync_clock_diff( $diff ) ) {
176
- // Retry call with new synced clock.
177
- return $this->_call( $path, $method, $params, true );
178
- }
179
- }
180
- }
181
- }
182
-
183
- if ( $this->_logger->is_on() && self::is_api_error( $result ) ) {
184
- // Log API errors.
185
- $this->_logger->api_error( $result );
186
- }
187
-
188
- return $result;
189
- }
190
-
191
- /**
192
- * Override API call to wrap it in servers' clock sync method.
193
- *
194
- * @param string $path
195
- * @param string $method
196
- * @param array $params
197
- *
198
- * @return array|mixed|string|void
199
- * @throws Freemius_Exception
200
- */
201
- function call( $path, $method = 'GET', $params = array() ) {
202
- return $this->_call( $path, $method, $params );
203
- }
204
-
205
- /**
206
- * Get API request URL signed via query string.
207
- *
208
- * @param string $path
209
- *
210
- * @return string
211
- */
212
- function get_signed_url( $path ) {
213
- return $this->_api->GetSignedUrl( $path );
214
- }
215
-
216
- /**
217
- * @param string $path
218
- * @param bool $flush
219
- * @param int $expiration (optional) Time until expiration in seconds from now, defaults to 24 hours
220
- *
221
- * @return stdClass|mixed
222
- */
223
- function get( $path = '/', $flush = false, $expiration = WP_FS__TIME_24_HOURS_IN_SEC ) {
224
- $this->_logger->entrance( $path );
225
-
226
- $cache_key = $this->get_cache_key( $path );
227
-
228
- // Always flush during development.
229
- if ( WP_FS__DEV_MODE || $this->_api->IsSandbox() ) {
230
- $flush = true;
231
- }
232
-
233
- $cached_result = self::$_cache->get( $cache_key );
234
-
235
- if ( $flush || ! self::$_cache->has_valid( $cache_key ) ) {
236
- $result = $this->call( $path );
237
-
238
- if ( ! is_object( $result ) || isset( $result->error ) ) {
239
- // Api returned an error.
240
- if ( is_object( $cached_result ) &&
241
- ! isset( $cached_result )
242
- ) {
243
- // If there was an error during a newer data fetch,
244
- // fallback to older data version.
245
- $result = $cached_result;
246
-
247
- if ( $this->_logger->is_on() ) {
248
- $this->_logger->warn( 'Fallback to cached API result: ' . var_export( $cached_result, true ) );
249
- }
250
- } else {
251
- // If no older data version, return result without
252
- // caching the error.
253
- return $result;
254
- }
255
- }
256
-
257
- self::$_cache->set( $cache_key, $result, $expiration );
258
-
259
- $cached_result = $result;
260
- } else {
261
- $this->_logger->log( 'Using cached API result.' );
262
- }
263
-
264
- return $cached_result;
265
- }
266
-
267
- /**
268
- * Check if there's a cached version of the API request.
269
- *
270
- * @author Vova Feldman (@svovaf)
271
- * @since 1.2.1
272
- *
273
- * @param string $path
274
- * @param string $method
275
- * @param array $params
276
- *
277
- * @return bool
278
- */
279
- function is_cached( $path, $method = 'GET', $params = array() ) {
280
- $cache_key = $this->get_cache_key( $path, $method, $params );
281
-
282
- return self::$_cache->has_valid( $cache_key );
283
- }
284
-
285
- /**
286
- * Invalidate a cached version of the API request.
287
- *
288
- * @author Vova Feldman (@svovaf)
289
- * @since 1.2.1.5
290
- *
291
- * @param string $path
292
- * @param string $method
293
- * @param array $params
294
- */
295
- function purge_cache( $path, $method = 'GET', $params = array() ) {
296
- $this->_logger->entrance( "{$method}:{$path}" );
297
-
298
- $cache_key = $this->get_cache_key( $path, $method, $params );
299
-
300
- self::$_cache->purge( $cache_key );
301
- }
302
-
303
- /**
304
- * @param string $path
305
- * @param string $method
306
- * @param array $params
307
- *
308
- * @return string
309
- * @throws \Freemius_Exception
310
- */
311
- private function get_cache_key( $path, $method = 'GET', $params = array() ) {
312
- $canonized = $this->_api->CanonizePath( $path );
313
- // $exploded = explode('/', $canonized);
314
- // return $method . '_' . array_pop($exploded) . '_' . md5($canonized . json_encode($params));
315
- return strtolower( $method . ':' . $canonized ) . ( ! empty( $params ) ? '#' . md5( json_encode( $params ) ) : '' );
316
- }
317
-
318
- /**
319
- * Test API connectivity.
320
- *
321
- * @author Vova Feldman (@svovaf)
322
- * @since 1.0.9 If fails, try to fallback to HTTP.
323
- * @since 1.1.6 Added a 5-min caching mechanism, to prevent from overloading the server if the API if
324
- * temporary down.
325
- *
326
- * @return bool True if successful connectivity to the API.
327
- */
328
- static function test() {
329
- self::_init();
330
-
331
- $cache_key = 'ping_test';
332
-
333
- $test = self::$_cache->get_valid( $cache_key, null );
334
-
335
- if ( is_null( $test ) ) {
336
- $test = Freemius_Api::Test();
337
-
338
- if ( false === $test && Freemius_Api::IsHttps() ) {
339
- // Fallback to HTTP, since HTTPS fails.
340
- Freemius_Api::SetHttp();
341
-
342
- self::$_options->set_option( 'api_force_http', true, true );
343
-
344
- $test = Freemius_Api::Test();
345
-
346
- if ( false === $test ) {
347
- /**
348
- * API connectivity test fail also in HTTP request, therefore,
349
- * fallback to HTTPS to keep connection secure.
350
- *
351
- * @since 1.1.6
352
- */
353
- self::$_options->set_option( 'api_force_http', false, true );
354
- }
355
- }
356
-
357
- self::$_cache->set( $cache_key, $test, WP_FS__TIME_5_MIN_IN_SEC );
358
- }
359
-
360
- return $test;
361
- }
362
-
363
- /**
364
- * Check if API is temporary down.
365
- *
366
- * @author Vova Feldman (@svovaf)
367
- * @since 1.1.6
368
- *
369
- * @return bool
370
- */
371
- static function is_temporary_down() {
372
- self::_init();
373
-
374
- $test = self::$_cache->get_valid( 'ping_test', null );
375
-
376
- return ( false === $test );
377
- }
378
-
379
- /**
380
- * @author Vova Feldman (@svovaf)
381
- * @since 1.1.6
382
- *
383
- * @return object
384
- */
385
- private function get_temporary_unavailable_error() {
386
- return (object) array(
387
- 'error' => (object) array(
388
- 'type' => 'TemporaryUnavailable',
389
- 'message' => 'API is temporary unavailable, please retry in ' . ( self::$_cache->get_record_expiration( 'ping_test' ) - WP_FS__SCRIPT_START_TIME ) . ' sec.',
390
- 'code' => 'temporary_unavailable',
391
- 'http' => 503
392
- )
393
- );
394
- }
395
-
396
- /**
397
- * Ping API for connectivity test, and return result object.
398
- *
399
- * @author Vova Feldman (@svovaf)
400
- * @since 1.0.9
401
- *
402
- * @param null|string $unique_anonymous_id
403
- * @param array $params
404
- *
405
- * @return object
406
- */
407
- function ping( $unique_anonymous_id = null, $params = array() ) {
408
- $this->_logger->entrance();
409
-
410
- if ( self::is_temporary_down() ) {
411
- return $this->get_temporary_unavailable_error();
412
- }
413
-
414
- $pong = is_null( $unique_anonymous_id ) ?
415
- Freemius_Api::Ping() :
416
- $this->_call( 'ping.json?' . http_build_query( array_merge(
417
- array( 'uid' => $unique_anonymous_id ),
418
- $params
419
- ) ) );
420
-
421
- if ( $this->is_valid_ping( $pong ) ) {
422
- return $pong;
423
- }
424
-
425
- if ( self::should_try_with_http( $pong ) ) {
426
- // Fallback to HTTP, since HTTPS fails.
427
- Freemius_Api::SetHttp();
428
-
429
- self::$_options->set_option( 'api_force_http', true, true );
430
-
431
- $pong = is_null( $unique_anonymous_id ) ?
432
- Freemius_Api::Ping() :
433
- $this->_call( 'ping.json?' . http_build_query( array_merge(
434
- array( 'uid' => $unique_anonymous_id ),
435
- $params
436
- ) ) );
437
-
438
- if ( ! $this->is_valid_ping( $pong ) ) {
439
- self::$_options->set_option( 'api_force_http', false, true );
440
- }
441
- }
442
-
443
- return $pong;
444
- }
445
-
446
- /**
447
- * Check if based on the API result we should try
448
- * to re-run the same request with HTTP instead of HTTPS.
449
- *
450
- * @author Vova Feldman (@svovaf)
451
- * @since 1.1.6
452
- *
453
- * @param $result
454
- *
455
- * @return bool
456
- */
457
- private static function should_try_with_http( $result ) {
458
- if ( ! Freemius_Api::IsHttps() ) {
459
- return false;
460
- }
461
-
462
- return ( ! is_object( $result ) ||
463
- ! isset( $result->error ) ||
464
- ! isset( $result->error->code ) ||
465
- ! in_array( $result->error->code, array(
466
- 'curl_missing',
467
- 'cloudflare_ddos_protection',
468
- 'maintenance_mode',
469
- 'squid_cache_block',
470
- 'too_many_requests',
471
- ) ) );
472
-
473
- }
474
-
475
- /**
476
- * Check if valid ping request result.
477
- *
478
- * @author Vova Feldman (@svovaf)
479
- * @since 1.1.1
480
- *
481
- * @param mixed $pong
482
- *
483
- * @return bool
484
- */
485
- function is_valid_ping( $pong ) {
486
- return Freemius_Api::Test( $pong );
487
- }
488
-
489
- function get_url( $path = '' ) {
490
- return Freemius_Api::GetUrl( $path, $this->_api->IsSandbox() );
491
- }
492
-
493
- /**
494
- * Clear API cache.
495
- *
496
- * @author Vova Feldman (@svovaf)
497
- * @since 1.0.9
498
- */
499
- static function clear_cache() {
500
- self::_init();
501
-
502
- self::$_cache = FS_Cache_Manager::get_manager( WP_FS__API_CACHE_OPTION_NAME );
503
- self::$_cache->clear();
504
- }
505
-
506
- #----------------------------------------------------------------------------------
507
- #region Error Handling
508
- #----------------------------------------------------------------------------------
509
-
510
- /**
511
- * @author Vova Feldman (@svovaf)
512
- * @since 1.2.1.5
513
- *
514
- * @param mixed $result
515
- *
516
- * @return bool Is API result contains an error.
517
- */
518
- static function is_api_error( $result ) {
519
- return ( is_object( $result ) && isset( $result->error ) ) ||
520
- is_string( $result );
521
- }
522
-
523
- /**
524
- * Checks if given API result is a non-empty and not an error object.
525
- *
526
- * @author Vova Feldman (@svovaf)
527
- * @since 1.2.1.5
528
- *
529
- * @param mixed $result
530
- * @param string|null $required_property Optional property we want to verify that is set.
531
- *
532
- * @return bool
533
- */
534
- static function is_api_result_object( $result, $required_property = null ) {
535
- return (
536
- is_object( $result ) &&
537
- ! isset( $result->error ) &&
538
- ( empty( $required_property ) || isset( $result->{$required_property} ) )
539
- );
540
- }
541
-
542
- /**
543
- * Checks if given API result is a non-empty entity object with non-empty ID.
544
- *
545
- * @author Vova Feldman (@svovaf)
546
- * @since 1.2.1.5
547
- *
548
- * @param mixed $result
549
- *
550
- * @return bool
551
- */
552
- static function is_api_result_entity( $result ) {
553
- return self::is_api_result_object( $result, 'id' ) &&
554
- FS_Entity::is_valid_id( $result->id );
555
- }
556
-
557
- #endregion
558
  }
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.4
7
+ */
8
+
9
+ if ( ! defined( 'ABSPATH' ) ) {
10
+ exit;
11
+ }
12
+
13
+ /**
14
+ * Class FS_Api
15
+ *
16
+ * Wraps Freemius API SDK to handle:
17
+ * 1. Clock sync.
18
+ * 2. Fallback to HTTP when HTTPS fails.
19
+ * 3. Adds caching layer to GET requests.
20
+ * 4. Adds consistency for failed requests by using last cached version.
21
+ */
22
+ class FS_Api {
23
+ /**
24
+ * @var FS_Api[]
25
+ */
26
+ private static $_instances = array();
27
+
28
+ /**
29
+ * @var FS_Option_Manager Freemius options, options-manager.
30
+ */
31
+ private static $_options;
32
+
33
+ /**
34
+ * @var FS_Cache_Manager API Caching layer
35
+ */
36
+ private static $_cache;
37
+
38
+ /**
39
+ * @var int Clock diff in seconds between current server to API server.
40
+ */
41
+ private static $_clock_diff;
42
+
43
+ /**
44
+ * @var Freemius_Api_WordPress
45
+ */
46
+ private $_api;
47
+
48
+ /**
49
+ * @var string
50
+ */
51
+ private $_slug;
52
+
53
+ /**
54
+ * @var FS_Logger
55
+ * @since 1.0.4
56
+ */
57
+ private $_logger;
58
+
59
+ /**
60
+ * @param string $slug
61
+ * @param string $scope 'app', 'developer', 'user' or 'install'.
62
+ * @param number $id Element's id.
63
+ * @param string $public_key Public key.
64
+ * @param bool $is_sandbox
65
+ * @param bool|string $secret_key Element's secret key.
66
+ *
67
+ * @return FS_Api
68
+ */
69
+ static function instance( $slug, $scope, $id, $public_key, $is_sandbox, $secret_key = false ) {
70
+ $identifier = md5( $slug . $scope . $id . $public_key . ( is_string( $secret_key ) ? $secret_key : '' ) . json_encode( $is_sandbox ) );
71
+
72
+ if ( ! isset( self::$_instances[ $identifier ] ) ) {
73
+ self::_init();
74
+
75
+ self::$_instances[ $identifier ] = new FS_Api( $slug, $scope, $id, $public_key, $secret_key, $is_sandbox );
76
+ }
77
+
78
+ return self::$_instances[ $identifier ];
79
+ }
80
+
81
+ private static function _init() {
82
+ if ( isset( self::$_options ) ) {
83
+ return;
84
+ }
85
+
86
+ if ( ! class_exists( 'Freemius_Api_WordPress' ) ) {
87
+ require_once WP_FS__DIR_SDK . '/FreemiusWordPress.php';
88
+ }
89
+
90
+ self::$_options = FS_Option_Manager::get_manager( WP_FS__OPTIONS_OPTION_NAME, true );
91
+ self::$_cache = FS_Cache_Manager::get_manager( WP_FS__API_CACHE_OPTION_NAME );
92
+
93
+ self::$_clock_diff = self::$_options->get_option( 'api_clock_diff', 0 );
94
+ Freemius_Api_WordPress::SetClockDiff( self::$_clock_diff );
95
+
96
+ if ( self::$_options->get_option( 'api_force_http', false ) ) {
97
+ Freemius_Api_WordPress::SetHttp();
98
+ }
99
+ }
100
+
101
+ /**
102
+ * @param string $slug
103
+ * @param string $scope 'app', 'developer', 'user' or 'install'.
104
+ * @param number $id Element's id.
105
+ * @param string $public_key Public key.
106
+ * @param bool|string $secret_key Element's secret key.
107
+ * @param bool $is_sandbox
108
+ */
109
+ private function __construct( $slug, $scope, $id, $public_key, $secret_key, $is_sandbox ) {
110
+ $this->_api = new Freemius_Api_WordPress( $scope, $id, $public_key, $secret_key, $is_sandbox );
111
+
112
+ $this->_slug = $slug;
113
+ $this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_' . $slug . '_api', WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
114
+ }
115
+
116
+ /**
117
+ * Find clock diff between server and API server, and store the diff locally.
118
+ *
119
+ * @param bool|int $diff
120
+ *
121
+ * @return bool|int False if clock diff didn't change, otherwise returns the clock diff in seconds.
122
+ */
123
+ private function _sync_clock_diff( $diff = false ) {
124
+ $this->_logger->entrance();
125
+
126
+ // Sync clock and store.
127
+ $new_clock_diff = ( false === $diff ) ?
128
+ Freemius_Api_WordPress::FindClockDiff() :
129
+ $diff;
130
+
131
+ if ( $new_clock_diff === self::$_clock_diff ) {
132
+ return false;
133
+ }
134
+
135
+ self::$_clock_diff = $new_clock_diff;
136
+
137
+ // Update API clock's diff.
138
+ Freemius_Api_WordPress::SetClockDiff( self::$_clock_diff );
139
+
140
+ // Store new clock diff in storage.
141
+ self::$_options->set_option( 'api_clock_diff', self::$_clock_diff, true );
142
+
143
+ return $new_clock_diff;
144
+ }
145
+
146
+ /**
147
+ * Override API call to enable retry with servers' clock auto sync method.
148
+ *
149
+ * @param string $path
150
+ * @param string $method
151
+ * @param array $params
152
+ * @param bool $retry Is in retry or first call attempt.
153
+ *
154
+ * @return array|mixed|string|void
155
+ */
156
+ private function _call( $path, $method = 'GET', $params = array(), $retry = false ) {
157
+ $this->_logger->entrance( $method . ':' . $path );
158
+
159
+ if ( self::is_temporary_down() ) {
160
+ $result = $this->get_temporary_unavailable_error();
161
+ } else {
162
+ $result = $this->_api->Api( $path, $method, $params );
163
+
164
+ if ( null !== $result &&
165
+ isset( $result->error ) &&
166
+ isset( $result->error->code ) &&
167
+ 'request_expired' === $result->error->code
168
+ ) {
169
+ if ( ! $retry ) {
170
+ $diff = isset( $result->error->timestamp ) ?
171
+ ( time() - strtotime( $result->error->timestamp ) ) :
172
+ false;
173
+
174
+ // Try to sync clock diff.
175
+ if ( false !== $this->_sync_clock_diff( $diff ) ) {
176
+ // Retry call with new synced clock.
177
+ return $this->_call( $path, $method, $params, true );
178
+ }
179
+ }
180
+ }
181
+ }
182
+
183
+ if ( $this->_logger->is_on() && self::is_api_error( $result ) ) {
184
+ // Log API errors.
185
+ $this->_logger->api_error( $result );
186
+ }
187
+
188
+ return $result;
189
+ }
190
+
191
+ /**
192
+ * Override API call to wrap it in servers' clock sync method.
193
+ *
194
+ * @param string $path
195
+ * @param string $method
196
+ * @param array $params
197
+ *
198
+ * @return array|mixed|string|void
199
+ * @throws Freemius_Exception
200
+ */
201
+ function call( $path, $method = 'GET', $params = array() ) {
202
+ return $this->_call( $path, $method, $params );
203
+ }
204
+
205
+ /**
206
+ * Get API request URL signed via query string.
207
+ *
208
+ * @param string $path
209
+ *
210
+ * @return string
211
+ */
212
+ function get_signed_url( $path ) {
213
+ return $this->_api->GetSignedUrl( $path );
214
+ }
215
+
216
+ /**
217
+ * @param string $path
218
+ * @param bool $flush
219
+ * @param int $expiration (optional) Time until expiration in seconds from now, defaults to 24 hours
220
+ *
221
+ * @return stdClass|mixed
222
+ */
223
+ function get( $path = '/', $flush = false, $expiration = WP_FS__TIME_24_HOURS_IN_SEC ) {
224
+ $this->_logger->entrance( $path );
225
+
226
+ $cache_key = $this->get_cache_key( $path );
227
+
228
+ // Always flush during development.
229
+ if ( WP_FS__DEV_MODE || $this->_api->IsSandbox() ) {
230
+ $flush = true;
231
+ }
232
+
233
+ $cached_result = self::$_cache->get( $cache_key );
234
+
235
+ if ( $flush || ! self::$_cache->has_valid( $cache_key, $expiration ) ) {
236
+ $result = $this->call( $path );
237
+
238
+ if ( ! is_object( $result ) || isset( $result->error ) ) {
239
+ // Api returned an error.
240
+ if ( is_object( $cached_result ) &&
241
+ ! isset( $cached_result )
242
+ ) {
243
+ // If there was an error during a newer data fetch,
244
+ // fallback to older data version.
245
+ $result = $cached_result;
246
+
247
+ if ( $this->_logger->is_on() ) {
248
+ $this->_logger->warn( 'Fallback to cached API result: ' . var_export( $cached_result, true ) );
249
+ }
250
+ } else {
251
+ // If no older data version, return result without
252
+ // caching the error.
253
+ return $result;
254
+ }
255
+ }
256
+
257
+ self::$_cache->set( $cache_key, $result, $expiration );
258
+
259
+ $cached_result = $result;
260
+ } else {
261
+ $this->_logger->log( 'Using cached API result.' );
262
+ }
263
+
264
+ return $cached_result;
265
+ }
266
+
267
+ /**
268
+ * Check if there's a cached version of the API request.
269
+ *
270
+ * @author Vova Feldman (@svovaf)
271
+ * @since 1.2.1
272
+ *
273
+ * @param string $path
274
+ * @param string $method
275
+ * @param array $params
276
+ *
277
+ * @return bool
278
+ */
279
+ function is_cached( $path, $method = 'GET', $params = array() ) {
280
+ $cache_key = $this->get_cache_key( $path, $method, $params );
281
+
282
+ return self::$_cache->has_valid( $cache_key );
283
+ }
284
+
285
+ /**
286
+ * Invalidate a cached version of the API request.
287
+ *
288
+ * @author Vova Feldman (@svovaf)
289
+ * @since 1.2.1.5
290
+ *
291
+ * @param string $path
292
+ * @param string $method
293
+ * @param array $params
294
+ */
295
+ function purge_cache( $path, $method = 'GET', $params = array() ) {
296
+ $this->_logger->entrance( "{$method}:{$path}" );
297
+
298
+ $cache_key = $this->get_cache_key( $path, $method, $params );
299
+
300
+ self::$_cache->purge( $cache_key );
301
+ }
302
+
303
+ /**
304
+ * @param string $path
305
+ * @param string $method
306
+ * @param array $params
307
+ *
308
+ * @return string
309
+ * @throws \Freemius_Exception
310
+ */
311
+ private function get_cache_key( $path, $method = 'GET', $params = array() ) {
312
+ $canonized = $this->_api->CanonizePath( $path );
313
+ // $exploded = explode('/', $canonized);
314
+ // return $method . '_' . array_pop($exploded) . '_' . md5($canonized . json_encode($params));
315
+ return strtolower( $method . ':' . $canonized ) . ( ! empty( $params ) ? '#' . md5( json_encode( $params ) ) : '' );
316
+ }
317
+
318
+ /**
319
+ * Test API connectivity.
320
+ *
321
+ * @author Vova Feldman (@svovaf)
322
+ * @since 1.0.9 If fails, try to fallback to HTTP.
323
+ * @since 1.1.6 Added a 5-min caching mechanism, to prevent from overloading the server if the API if
324
+ * temporary down.
325
+ *
326
+ * @return bool True if successful connectivity to the API.
327
+ */
328
+ static function test() {
329
+ self::_init();
330
+
331
+ $cache_key = 'ping_test';
332
+
333
+ $test = self::$_cache->get_valid( $cache_key, null );
334
+
335
+ if ( is_null( $test ) ) {
336
+ $test = Freemius_Api_WordPress::Test();
337
+
338
+ if ( false === $test && Freemius_Api_WordPress::IsHttps() ) {
339
+ // Fallback to HTTP, since HTTPS fails.
340
+ Freemius_Api_WordPress::SetHttp();
341
+
342
+ self::$_options->set_option( 'api_force_http', true, true );
343
+
344
+ $test = Freemius_Api_WordPress::Test();
345
+
346
+ if ( false === $test ) {
347
+ /**
348
+ * API connectivity test fail also in HTTP request, therefore,
349
+ * fallback to HTTPS to keep connection secure.
350
+ *
351
+ * @since 1.1.6
352
+ */
353
+ self::$_options->set_option( 'api_force_http', false, true );
354
+ }
355
+ }
356
+
357
+ self::$_cache->set( $cache_key, $test, WP_FS__TIME_5_MIN_IN_SEC );
358
+ }
359
+
360
+ return $test;
361
+ }
362
+
363
+ /**
364
+ * Check if API is temporary down.
365
+ *
366
+ * @author Vova Feldman (@svovaf)
367
+ * @since 1.1.6
368
+ *
369
+ * @return bool
370
+ */
371
+ static function is_temporary_down() {
372
+ self::_init();
373
+
374
+ $test = self::$_cache->get_valid( 'ping_test', null );
375
+
376
+ return ( false === $test );
377
+ }
378
+
379
+ /**
380
+ * @author Vova Feldman (@svovaf)
381
+ * @since 1.1.6
382
+ *
383
+ * @return object
384
+ */
385
+ private function get_temporary_unavailable_error() {
386
+ return (object) array(
387
+ 'error' => (object) array(
388
+ 'type' => 'TemporaryUnavailable',
389
+ 'message' => 'API is temporary unavailable, please retry in ' . ( self::$_cache->get_record_expiration( 'ping_test' ) - WP_FS__SCRIPT_START_TIME ) . ' sec.',
390
+ 'code' => 'temporary_unavailable',
391
+ 'http' => 503
392
+ )
393
+ );
394
+ }
395
+
396
+ /**
397
+ * Ping API for connectivity test, and return result object.
398
+ *
399
+ * @author Vova Feldman (@svovaf)
400
+ * @since 1.0.9
401
+ *
402
+ * @param null|string $unique_anonymous_id
403
+ * @param array $params
404
+ *
405
+ * @return object
406
+ */
407
+ function ping( $unique_anonymous_id = null, $params = array() ) {
408
+ $this->_logger->entrance();
409
+
410
+ if ( self::is_temporary_down() ) {
411
+ return $this->get_temporary_unavailable_error();
412
+ }
413
+
414
+ $pong = is_null( $unique_anonymous_id ) ?
415
+ Freemius_Api_WordPress::Ping() :
416
+ $this->_call( 'ping.json?' . http_build_query( array_merge(
417
+ array( 'uid' => $unique_anonymous_id ),
418
+ $params
419
+ ) ) );
420
+
421
+ if ( $this->is_valid_ping( $pong ) ) {
422
+ return $pong;
423
+ }
424
+
425
+ if ( self::should_try_with_http( $pong ) ) {
426
+ // Fallback to HTTP, since HTTPS fails.
427
+ Freemius_Api_WordPress::SetHttp();
428
+
429
+ self::$_options->set_option( 'api_force_http', true, true );
430
+
431
+ $pong = is_null( $unique_anonymous_id ) ?
432
+ Freemius_Api_WordPress::Ping() :
433
+ $this->_call( 'ping.json?' . http_build_query( array_merge(
434
+ array( 'uid' => $unique_anonymous_id ),
435
+ $params
436
+ ) ) );
437
+
438
+ if ( ! $this->is_valid_ping( $pong ) ) {
439
+ self::$_options->set_option( 'api_force_http', false, true );
440
+ }
441
+ }
442
+
443
+ return $pong;
444
+ }
445
+
446
+ /**
447
+ * Check if based on the API result we should try
448
+ * to re-run the same request with HTTP instead of HTTPS.
449
+ *
450
+ * @author Vova Feldman (@svovaf)
451
+ * @since 1.1.6
452
+ *
453
+ * @param $result
454
+ *
455
+ * @return bool
456
+ */
457
+ private static function should_try_with_http( $result ) {
458
+ if ( ! Freemius_Api_WordPress::IsHttps() ) {
459
+ return false;
460
+ }
461
+
462
+ return ( ! is_object( $result ) ||
463
+ ! isset( $result->error ) ||
464
+ ! isset( $result->error->code ) ||
465
+ ! in_array( $result->error->code, array(
466
+ 'curl_missing',
467
+ 'cloudflare_ddos_protection',
468
+ 'maintenance_mode',
469
+ 'squid_cache_block',
470
+ 'too_many_requests',
471
+ ) ) );
472
+
473
+ }
474
+
475
+ /**
476
+ * Check if valid ping request result.
477
+ *
478
+ * @author Vova Feldman (@svovaf)
479
+ * @since 1.1.1
480
+ *
481
+ * @param mixed $pong
482
+ *
483
+ * @return bool
484
+ */
485
+ function is_valid_ping( $pong ) {
486
+ return Freemius_Api_WordPress::Test( $pong );
487
+ }
488
+
489
+ function get_url( $path = '' ) {
490
+ return Freemius_Api_WordPress::GetUrl( $path, $this->_api->IsSandbox() );
491
+ }
492
+
493
+ /**
494
+ * Clear API cache.
495
+ *
496
+ * @author Vova Feldman (@svovaf)
497
+ * @since 1.0.9
498
+ */
499
+ static function clear_cache() {
500
+ self::_init();
501
+
502
+ self::$_cache = FS_Cache_Manager::get_manager( WP_FS__API_CACHE_OPTION_NAME );
503
+ self::$_cache->clear();
504
+ }
505
+
506
+ #----------------------------------------------------------------------------------
507
+ #region Error Handling
508
+ #----------------------------------------------------------------------------------
509
+
510
+ /**
511
+ * @author Vova Feldman (@svovaf)
512
+ * @since 1.2.1.5
513
+ *
514
+ * @param mixed $result
515
+ *
516
+ * @return bool Is API result contains an error.
517
+ */
518
+ static function is_api_error( $result ) {
519
+ return ( is_object( $result ) && isset( $result->error ) ) ||
520
+ is_string( $result );
521
+ }
522
+
523
+ /**
524
+ * Checks if given API result is a non-empty and not an error object.
525
+ *
526
+ * @author Vova Feldman (@svovaf)
527
+ * @since 1.2.1.5
528
+ *
529
+ * @param mixed $result
530
+ * @param string|null $required_property Optional property we want to verify that is set.
531
+ *
532
+ * @return bool
533
+ */
534
+ static function is_api_result_object( $result, $required_property = null ) {
535
+ return (
536
+ is_object( $result ) &&
537
+ ! isset( $result->error ) &&
538
+ ( empty( $required_property ) || isset( $result->{$required_property} ) )
539
+ );
540
+ }
541
+
542
+ /**
543
+ * Checks if given API result is a non-empty entity object with non-empty ID.
544
+ *
545
+ * @author Vova Feldman (@svovaf)
546
+ * @since 1.2.1.5
547
+ *
548
+ * @param mixed $result
549
+ *
550
+ * @return bool
551
+ */
552
+ static function is_api_result_entity( $result ) {
553
+ return self::is_api_result_object( $result, 'id' ) &&
554
+ FS_Entity::is_valid_id( $result->id );
555
+ }
556
+
557
+ #endregion
558
  }
freemius/includes/class-fs-logger.php CHANGED
@@ -2,7 +2,7 @@
2
  /**
3
  * @package Freemius
4
  * @copyright Copyright (c) 2015, Freemius, Inc.
5
- * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
6
  * @since 1.0.3
7
  */
8
 
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
 
freemius/includes/class-fs-plugin-updater.php CHANGED
@@ -1,683 +1,712 @@
1
- <?php
2
- /**
3
- * @package Freemius
4
- * @copyright Copyright (c) 2015, Freemius, Inc.
5
- * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
6
- * @since 1.0.4
7
- *
8
- * @link https://github.com/easydigitaldownloads/EDD-License-handler/blob/master/EDD_SL_Plugin_Updater.php
9
- */
10
-
11
- if ( ! defined( 'ABSPATH' ) ) {
12
- exit;
13
- }
14
-
15
- // Uncomment this line for testing.
16
- // set_site_transient( 'update_plugins', null );
17
-
18
- class FS_Plugin_Updater {
19
-
20
- /**
21
- * @var Freemius
22
- * @since 1.0.4
23
- */
24
- private $_fs;
25
- /**
26
- * @var FS_Logger
27
- * @since 1.0.4
28
- */
29
- private $_logger;
30
- /**
31
- * @var object
32
- * @since 1.1.8.1
33
- */
34
- private $_update_details;
35
-
36
- function __construct( Freemius $freemius ) {
37
- $this->_fs = $freemius;
38
-
39
- $this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_' . $freemius->get_slug() . '_updater', WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
40
-
41
- $this->_filters();
42
- }
43
-
44
- /**
45
- * Initiate required filters.
46
- *
47
- * @author Vova Feldman (@svovaf)
48
- * @since 1.0.4
49
- */
50
- private function _filters() {
51
- // Override request for plugin information
52
- add_filter( 'plugins_api', array( &$this, 'plugins_api_filter' ), 10, 3 );
53
-
54
- // WP 3.0+
55
- add_filter( 'pre_set_site_transient_update_plugins', array(
56
- &$this,
57
- 'pre_set_site_transient_update_plugins_filter'
58
- ) );
59
-
60
- if ( ! $this->_fs->has_active_valid_license() ) {
61
- /**
62
- * If user has the premium plugin's code but do NOT have an active license,
63
- * encourage him to upgrade by showing that there's a new release, but instead
64
- * of showing an update link, show upgrade link to the pricing page.
65
- *
66
- * @since 1.1.6
67
- *
68
- */
69
- // WP 2.9+
70
- add_action( "after_plugin_row_{$this->_fs->get_plugin_basename()}", array(
71
- &$this,
72
- 'catch_plugin_update_row'
73
- ), 9 );
74
- add_action( "after_plugin_row_{$this->_fs->get_plugin_basename()}", array(
75
- &$this,
76
- 'edit_and_echo_plugin_update_row'
77
- ), 11, 2 );
78
- }
79
-
80
- if ( ! WP_FS__IS_PRODUCTION_MODE ) {
81
- add_filter( 'http_request_host_is_external', array(
82
- $this,
83
- 'http_request_host_is_external_filter'
84
- ), 10, 3 );
85
- }
86
-
87
- if ( $this->_fs->is_premium() && $this->is_correct_folder_name() ) {
88
- add_filter( 'upgrader_post_install', array( &$this, '_maybe_update_folder_name' ), 10, 3 );
89
- }
90
- }
91
-
92
- /**
93
- * Capture plugin update row by turning output buffering.
94
- *
95
- * @author Vova Feldman (@svovaf)
96
- * @since 1.1.6
97
- */
98
- function catch_plugin_update_row() {
99
- ob_start();
100
- }
101
-
102
- /**
103
- * Overrides default update message format with "renew your license" message.
104
- *
105
- * @author Vova Feldman (@svovaf)
106
- * @since 1.1.6
107
- *
108
- * @param string $file
109
- * @param array $plugin_data
110
- */
111
- function edit_and_echo_plugin_update_row( $file, $plugin_data ) {
112
- $plugin_update_row = ob_get_clean();
113
-
114
- $current = get_site_transient( 'update_plugins' );
115
- if ( ! isset( $current->response[ $file ] ) ) {
116
- echo $plugin_update_row;
117
-
118
- return;
119
- }
120
-
121
- $r = $current->response[ $file ];
122
-
123
- $plugin_update_row = preg_replace(
124
- '/(\<div.+>)(.+)(\<a.+\<a.+)\<\/div\>/is',
125
- '$1 $2 ' . sprintf(
126
- $this->_fs->get_text( 'renew-license-now' ),
127
- '<a href="' . $this->_fs->pricing_url() . '">', '</a>',
128
- $r->new_version ) .
129
- '$4',
130
- $plugin_update_row
131
- );
132
-
133
- echo $plugin_update_row;
134
- }
135
-
136
- /**
137
- * Since WP version 3.6, a new security feature was added that denies access to repository with a local ip.
138
- * During development mode we want to be able updating plugin versions via our localhost repository. This
139
- * filter white-list all domains including "api.freemius".
140
- *
141
- * @link http://www.emanueletessore.com/wordpress-download-failed-valid-url-provided/
142
- *
143
- * @author Vova Feldman (@svovaf)
144
- * @since 1.0.4
145
- *
146
- * @param bool $allow
147
- * @param string $host
148
- * @param string $url
149
- *
150
- * @return bool
151
- */
152
- function http_request_host_is_external_filter( $allow, $host, $url ) {
153
- return ( false !== strpos( $host, 'freemius' ) ) ? true : $allow;
154
- }
155
-
156
- /**
157
- * Check for Updates at the defined API endpoint and modify the update array.
158
- *
159
- * This function dives into the update api just when WordPress creates its update array,
160
- * then adds a custom API call and injects the custom plugin data retrieved from the API.
161
- * It is reassembled from parts of the native WordPress plugin update code.
162
- * See wp-includes/update.php line 121 for the original wp_update_plugins() function.
163
- *
164
- * @author Vova Feldman (@svovaf)
165
- * @since 1.0.4
166
- *
167
- * @uses FS_Api
168
- *
169
- * @param object $transient_data Update array build by WordPress.
170
- *
171
- * @return object Modified update array with custom plugin data.
172
- */
173
- function pre_set_site_transient_update_plugins_filter( $transient_data ) {
174
- $this->_logger->entrance();
175
-
176
- if ( empty( $transient_data ) ||
177
- defined( 'WP_FS__UNINSTALL_MODE' )
178
- ) {
179
- return $transient_data;
180
- }
181
-
182
- if ( ! isset( $this->_update_details ) ) {
183
- // Get plugin's newest update.
184
- $new_version = $this->_fs->get_update( false, false );
185
-
186
- $this->_update_details = false;
187
-
188
- if ( is_object( $new_version ) ) {
189
- $this->_logger->log( 'Found newer plugin version ' . $new_version->version );
190
-
191
- $plugin_details = new stdClass();
192
- $plugin_details->slug = $this->_fs->get_slug();
193
- $plugin_details->new_version = $new_version->version;
194
- $plugin_details->url = WP_FS__ADDRESS;
195
- $plugin_details->package = $new_version->url;
196
- $plugin_details->plugin = $this->_fs->get_plugin_basename();
197
-
198
- /**
199
- * Cache plugin details locally since set_site_transient( 'update_plugins' )
200
- * called multiple times and the non wp.org plugins are filtered after the
201
- * call to .org.
202
- *
203
- * @since 1.1.8.1
204
- */
205
- $this->_update_details = $plugin_details;
206
- }
207
- }
208
-
209
- if ( is_object( $this->_update_details ) ) {
210
- // Add plugin to transient data.
211
- $transient_data->response[ $this->_fs->get_plugin_basename() ] = $this->_update_details;
212
- }
213
-
214
- return $transient_data;
215
- }
216
-
217
- /**
218
- * Try to fetch plugin's info from .org repository.
219
- *
220
- * @author Vova Feldman (@svovaf)
221
- * @since 1.0.5
222
- *
223
- * @param string $action
224
- * @param object $args
225
- *
226
- * @return bool|mixed
227
- */
228
- static function _fetch_plugin_info_from_repository( $action, $args ) {
229
- $url = $http_url = 'http://api.wordpress.org/plugins/info/1.0/';
230
- if ( $ssl = wp_http_supports( array( 'ssl' ) ) ) {
231
- $url = set_url_scheme( $url, 'https' );
232
- }
233
-
234
- $args = array(
235
- 'timeout' => 15,
236
- 'body' => array(
237
- 'action' => $action,
238
- 'request' => serialize( $args )
239
- )
240
- );
241
-
242
- $request = wp_remote_post( $url, $args );
243
-
244
- if ( is_wp_error( $request ) ) {
245
- return false;
246
- }
247
-
248
- $res = maybe_unserialize( wp_remote_retrieve_body( $request ) );
249
-
250
- if ( ! is_object( $res ) && ! is_array( $res ) ) {
251
- return false;
252
- }
253
-
254
- return $res;
255
- }
256
-
257
- /**
258
- * Updates information on the "View version x.x details" page with custom data.
259
- *
260
- * @author Vova Feldman (@svovaf)
261
- * @since 1.0.4
262
- *
263
- * @uses FS_Api
264
- *
265
- * @param object $data
266
- * @param string $action
267
- * @param mixed $args
268
- *
269
- * @return object
270
- */
271
- function plugins_api_filter( $data, $action = '', $args = null ) {
272
- $this->_logger->entrance();
273
-
274
- if ( ( 'plugin_information' !== $action ) ||
275
- ! isset( $args->slug )
276
- ) {
277
- return $data;
278
- }
279
-
280
- $addon = false;
281
- $is_addon = false;
282
-
283
- if ( $this->_fs->get_slug() !== $args->slug ) {
284
- $addon = $this->_fs->get_addon_by_slug( $args->slug );
285
-
286
- if ( ! is_object( $addon ) ) {
287
- return $data;
288
- }
289
-
290
- $is_addon = true;
291
- }
292
-
293
- $plugin_in_repo = false;
294
- if ( ! $is_addon ) {
295
- // Try to fetch info from .org repository.
296
- $data = self::_fetch_plugin_info_from_repository( $action, $args );
297
-
298
- $plugin_in_repo = ( false !== $data );
299
- }
300
-
301
- if ( ! $plugin_in_repo ) {
302
- $data = $args;
303
-
304
- // Fetch as much as possible info from local files.
305
- $plugin_local_data = $this->_fs->get_plugin_data();
306
- $data->name = $plugin_local_data['Name'];
307
- $data->author = $plugin_local_data['Author'];
308
- $data->sections = array(
309
- 'description' => 'Upgrade ' . $plugin_local_data['Name'] . ' to latest.',
310
- );
311
-
312
- // @todo Store extra plugin info on Freemius or parse readme.txt markup.
313
- /*$info = $this->_fs->get_api_site_scope()->call('/information.json');
314
-
315
- if ( !isset($info->error) ) {
316
- $data = $info;
317
- }*/
318
- }
319
-
320
- // Get plugin's newest update.
321
- $new_version = $this->get_latest_download_details( $is_addon ? $addon->id : false );
322
-
323
- if ( ! is_object( $new_version ) || empty( $new_version->version ) ) {
324
- $data->version = $this->_fs->get_plugin_version();
325
- } else {
326
- if ( $is_addon ) {
327
- $data->name = $addon->title . ' ' . $this->_fs->get_text( 'addon' );
328
- $data->slug = $addon->slug;
329
- $data->url = WP_FS__ADDRESS;
330
- $data->package = $new_version->url;
331
- }
332
-
333
- if ( ! $plugin_in_repo ) {
334
- $data->last_updated = ! is_null( $new_version->updated ) ? $new_version->updated : $new_version->created;
335
- $data->requires = $new_version->requires_platform_version;
336
- $data->tested = $new_version->tested_up_to_version;
337
- }
338
-
339
- $data->version = $new_version->version;
340
- $data->download_link = $new_version->url;
341
- }
342
-
343
- return $data;
344
- }
345
-
346
- /**
347
- * @author Vova Feldman (@svovaf)
348
- * @since 1.2.1.7
349
- *
350
- * @param number|bool $addon_id
351
- *
352
- * @return object
353
- */
354
- private function get_latest_download_details( $addon_id = false ) {
355
- return $this->_fs->_fetch_latest_version( $addon_id );
356
- }
357
-
358
- /**
359
- * Checks if a given basename has a matching folder name
360
- * with the current context plugin.
361
- *
362
- * @author Vova Feldman (@svovaf)
363
- * @since 1.2.1.6
364
- *
365
- * @param string $basename Current plugin's basename.
366
- *
367
- * @return bool
368
- */
369
- private function is_correct_folder_name( $basename = '' ) {
370
- if ( empty( $basename ) ) {
371
- $basename = $this->_fs->get_plugin_basename();
372
- }
373
-
374
- return ( $this->_fs->get_target_folder_name() != trim( dirname( $basename ), '/\\' ) );
375
- }
376
-
377
- /**
378
- * This is a special after upgrade handler for migrating modules
379
- * that didn't use the '-premium' suffix folder structure before
380
- * the migration.
381
- *
382
- * @author Vova Feldman (@svovaf)
383
- * @since 1.2.1.6
384
- *
385
- * @param bool $response Install response.
386
- * @param array $hook_extra Extra arguments passed to hooked filters.
387
- * @param array $result Installation result data.
388
- *
389
- * @return bool
390
- */
391
- function _maybe_update_folder_name( $response, $hook_extra, $result ) {
392
- $basename = $this->_fs->get_plugin_basename();
393
-
394
- if ( true !== $response ||
395
- empty( $hook_extra ) ||
396
- empty( $hook_extra['plugin'] ) ||
397
- $basename !== $hook_extra['plugin']
398
- ) {
399
- return $response;
400
- }
401
-
402
- $active_plugins_basenames = get_option( 'active_plugins' );
403
-
404
- for ( $i = 0, $len = count( $active_plugins_basenames ); $i < $len; $i ++ ) {
405
- if ( $basename === $active_plugins_basenames[ $i ] ) {
406
- // Get filename including extension.
407
- $filename = basename( $basename );
408
-
409
- $new_basename = plugin_basename(
410
- trailingslashit( $this->_fs->get_slug() . ( $this->_fs->is_premium() ? '-premium' : '' ) ) .
411
- $filename
412
- );
413
-
414
- // Verify that the expected correct path exists.
415
- if ( file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $new_basename ) ) ) {
416
- // Override active plugin name.
417
- $active_plugins_basenames[ $i ] = $new_basename;
418
- update_option( 'active_plugins', $active_plugins_basenames );
419
- }
420
-
421
- break;
422
- }
423
- }
424
-
425
- return $response;
426
- }
427
-
428
- #----------------------------------------------------------------------------------
429
- #region Auto Activation
430
- #----------------------------------------------------------------------------------
431
-
432
- /**
433
- * Installs and active a plugin when explicitly requested that from a 3rd party service.
434
- *
435
- * This logic was inspired by the TGMPA GPL licensed library by Thomas Griffin.
436
- *
437
- * @link http://tgmpluginactivation.com/
438
- *
439
- * @author Vova Feldman
440
- * @since 1.2.1.7
441
- *
442
- * @link https://make.wordpress.org/plugins/2017/03/16/clarification-of-guideline-8-executable-code-and-installs/
443
- *
444
- * @uses WP_Filesystem
445
- * @uses WP_Error
446
- * @uses WP_Upgrader
447
- * @uses Plugin_Upgrader
448
- * @uses Plugin_Installer_Skin
449
- * @uses Plugin_Upgrader_Skin
450
- *
451
- * @param number|bool $plugin_id
452
- *
453
- * @return array
454
- */
455
- function install_and_activate_plugin( $plugin_id = false ) {
456
- if ( ! empty( $plugin_id ) && ! FS_Plugin::is_valid_id( $plugin_id ) ) {
457
- // Invalid plugin ID.
458
- return array(
459
- 'message' => $this->_fs->get_text( 'auto-install-error-invalid-id' ),
460
- 'code' => 'invalid_module_id',
461
- );
462
- }
463
-
464
- $is_addon = false;
465
- if ( FS_Plugin::is_valid_id( $plugin_id ) &&
466
- $plugin_id != $this->_fs->get_id()
467
- ) {
468
- $addon = $this->_fs->get_addon( $plugin_id );
469
-
470
- if ( ! is_object( $addon ) ) {
471
- // Invalid add-on ID.
472
- return array(
473
- 'message' => $this->_fs->get_text( 'auto-install-error-invalid-id' ),
474
- 'code' => 'invalid_module_id',
475
- );
476
- }
477
-
478
- $slug = $addon->slug;
479
- $title = $addon->title . ' ' . $this->_fs->get_text( 'addon' );
480
-
481
- $is_addon = true;
482
- } else {
483
- $slug = $this->_fs->get_slug();
484
- $title = $this->_fs->get_plugin_title() .
485
- ( $this->_fs->is_addon() ? ' ' . $this->_fs->get_text( 'addon' ) : '' );
486
- }
487
-
488
- if ( $this->is_premium_plugin_active( $plugin_id ) ) {
489
- // Premium version already activated.
490
- return array(
491
- 'message' => $this->_fs->get_text(
492
- $is_addon ?
493
- 'auto-install-error-premium-addon-activated' :
494
- 'auto-install-error-premium-activated'
495
- ),
496
- 'code' => 'premium_installed',
497
- );
498
- }
499
-
500
- $latest_version = $this->get_latest_download_details( $plugin_id );
501
- $target_folder = "{$slug}-premium";
502
-
503
- // Prep variables for Plugin_Installer_Skin class.
504
- $extra = array();
505
- $extra['slug'] = $target_folder;
506
- $source = $latest_version->url;
507
- $api = null;
508
-
509
- $install_url = add_query_arg(
510
- array(
511
- 'action' => 'install-plugin',
512
- 'plugin' => urlencode( $slug ),
513
- ),
514
- 'update.php'
515
- );
516
-
517
- if ( ! class_exists( 'Plugin_Upgrader', false ) ) {
518
- // Include required resources for the installation.
519
- require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
520
- }
521
-
522
- $skin_args = array(
523
- 'type' => 'web',
524
- 'title' => sprintf( fs_text( 'installing-plugin-x', $slug ), $title ),
525
- 'url' => esc_url_raw( $install_url ),
526
- 'nonce' => 'install-plugin_' . $slug,
527
- 'plugin' => '',
528
- 'api' => $api,
529
- 'extra' => $extra,
530
- );
531
-
532
- // $skin = new Automatic_Upgrader_Skin( $skin_args );
533
- // $skin = new Plugin_Installer_Skin( $skin_args );
534
- $skin = new WP_Ajax_Upgrader_Skin( $skin_args );
535
-
536
- // Create a new instance of Plugin_Upgrader.
537
- $upgrader = new Plugin_Upgrader( $skin );
538
-
539
- // Perform the action and install the plugin from the $source urldecode().
540
- add_filter( 'upgrader_source_selection', array( &$this, '_maybe_adjust_source_dir' ), 1, 3 );
541
-
542
- $install_result = $upgrader->install( $source );
543
-
544
- remove_filter( 'upgrader_source_selection', array( &$this, '_maybe_adjust_source_dir' ), 1 );
545
-
546
- if ( is_wp_error( $install_result ) ) {
547
- return array(
548
- 'message' => $install_result->get_error_message(),
549
- 'code' => $install_result->get_error_code(),
550
- );
551
- } elseif ( is_wp_error( $skin->result ) ) {
552
- return array(
553
- 'message' => $skin->result->get_error_message(),
554
- 'code' => $skin->result->get_error_code(),
555
- );
556
- } elseif ( $skin->get_errors()->get_error_code() ) {
557
- return array(
558
- 'message' => $skin->get_error_messages(),
559
- 'code' => 'unknown',
560
- );
561
- } elseif ( is_null( $install_result ) ) {
562
- global $wp_filesystem;
563
-
564
- $error_code = 'unable_to_connect_to_filesystem';
565
- $error_message = __( 'Unable to connect to the filesystem. Please confirm your credentials.' );
566
-
567
- // Pass through the error from WP_Filesystem if one was raised.
568
- if ( $wp_filesystem instanceof WP_Filesystem_Base &&
569
- is_wp_error( $wp_filesystem->errors ) &&
570
- $wp_filesystem->errors->get_error_code()
571
- ) {
572
- $error_message = $wp_filesystem->errors->get_error_message();
573
- }
574
-
575
- return array(
576
- 'message' => $error_message,
577
- 'code' => $error_code,
578
- );
579
- }
580
-
581
- // Grab the full path to the main plugin's file.
582
- $plugin_activate = $upgrader->plugin_info();
583
-
584
- // Try to activate the plugin.
585
- $activation_result = $this->try_activate_plugin( $plugin_activate );
586
-
587
- if ( is_wp_error( $activation_result ) ) {
588
- return array(
589
- 'message' => $activation_result->get_error_message(),
590
- 'code' => $activation_result->get_error_code(),
591
- );
592
- }
593
-
594
- return $skin->get_upgrade_messages();
595
- }
596
-
597
- /**
598
- * Tries to activate a plugin. If fails, returns the error.
599
- *
600
- * @author Vova Feldman
601
- * @since 1.2.1.7
602
- *
603
- * @param string $file_path Path within wp-plugins/ to main plugin file.
604
- * This determines the styling of the output messages.
605
- *
606
- * @return bool|WP_Error
607
- */
608
- protected function try_activate_plugin( $file_path ) {
609
- $activate = activate_plugin( $file_path );
610
-
611
- return is_wp_error( $activate ) ?
612
- $activate :
613
- true;
614
- }
615
-
616
- /**
617
- * Check if a premium module version is already active.
618
- *
619
- * @author Vova Feldman
620
- * @since 1.2.1.7
621
- *
622
- * @param number|bool $plugin_id
623
- *
624
- * @return bool
625
- */
626
- private function is_premium_plugin_active( $plugin_id = false ) {
627
- if ( $plugin_id != $this->_fs->get_id() ) {
628
- return $this->_fs->is_addon_activated( $plugin_id, true );
629
- }
630
-
631
- return is_plugin_active( $this->_fs->premium_plugin_basename() );
632
- }
633
-
634
- /**
635
- * Adjust the plugin directory name if necessary.
636
- * Assumes plugin has a folder (not a single file plugin).
637
- *
638
- * The final destination directory of a plugin is based on the subdirectory name found in the
639
- * (un)zipped source. In some cases this subdirectory name is not the same as the expected
640
- * slug and the plugin will not be recognized as installed. This is fixed by adjusting
641
- * the temporary unzipped source subdirectory name to the expected plugin slug.
642
- *
643
- * @author Vova Feldman
644
- * @since 1.2.1.7
645
- *
646
- * @param string $source Path to upgrade/zip-file-name.tmp/subdirectory/.
647
- * @param string $remote_source Path to upgrade/zip-file-name.tmp.
648
- * @param \WP_Upgrader $upgrader Instance of the upgrader which installs the plugin.
649
- *
650
- * @return string|WP_Error
651
- */
652
- function _maybe_adjust_source_dir( $source, $remote_source, $upgrader ) {
653
- if ( ! is_object( $GLOBALS['wp_filesystem'] ) ) {
654
- return $source;
655
- }
656
-
657
- // Figure out what the slug is supposed to be.
658
- $desired_slug = $upgrader->skin->options['extra']['slug'];
659
-
660
- $subdir_name = untrailingslashit( str_replace( trailingslashit( $remote_source ), '', $source ) );
661
-
662
- if ( ! empty( $subdir_name ) && $subdir_name !== $desired_slug ) {
663
- $from_path = untrailingslashit( $source );
664
- $to_path = trailingslashit( $remote_source ) . $desired_slug;
665
-
666
- if ( true === $GLOBALS['wp_filesystem']->move( $from_path, $to_path ) ) {
667
- return trailingslashit( $to_path );
668
- } else {
669
- return new WP_Error(
670
- 'rename_failed',
671
- $this->_fs->get_text( 'module-package-rename-failure' ),
672
- array(
673
- 'found' => $subdir_name,
674
- 'expected' => $desired_slug
675
- ) );
676
- }
677
- }
678
-
679
- return $source;
680
- }
681
-
682
- #endregion
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
683
  }
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.4
7
+ *
8
+ * @link https://github.com/easydigitaldownloads/EDD-License-handler/blob/master/EDD_SL_Plugin_Updater.php
9
+ */
10
+
11
+ if ( ! defined( 'ABSPATH' ) ) {
12
+ exit;
13
+ }
14
+
15
+ // Uncomment this line for testing.
16
+ // set_site_transient( 'update_plugins', null );
17
+
18
+ class FS_Plugin_Updater {
19
+
20
+ /**
21
+ * @var Freemius
22
+ * @since 1.0.4
23
+ */
24
+ private $_fs;
25
+ /**
26
+ * @var FS_Logger
27
+ * @since 1.0.4
28
+ */
29
+ private $_logger;
30
+ /**
31
+ * @var object
32
+ * @since 1.1.8.1
33
+ */
34
+ private $_update_details;
35
+
36
+ function __construct( Freemius $freemius ) {
37
+ $this->_fs = $freemius;
38
+
39
+ $this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_' . $freemius->get_slug() . '_updater', WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
40
+
41
+ $this->_filters();
42
+ }
43
+
44
+ /**
45
+ * Initiate required filters.
46
+ *
47
+ * @author Vova Feldman (@svovaf)
48
+ * @since 1.0.4
49
+ */
50
+ private function _filters() {
51
+ // Override request for plugin information
52
+ add_filter( 'plugins_api', array( &$this, 'plugins_api_filter' ), 10, 3 );
53
+
54
+ // WP 3.0+
55
+ add_filter( 'pre_set_site_transient_update_plugins', array(
56
+ &$this,
57
+ 'pre_set_site_transient_update_plugins_filter'
58
+ ) );
59
+
60
+ add_filter( 'pre_set_site_transient_update_themes', array(
61
+ &$this,
62
+ 'pre_set_site_transient_update_plugins_filter'
63
+ ) );
64
+
65
+ if ( ! $this->_fs->has_active_valid_license() ) {
66
+ /**
67
+ * If user has the premium plugin's code but do NOT have an active license,
68
+ * encourage him to upgrade by showing that there's a new release, but instead
69
+ * of showing an update link, show upgrade link to the pricing page.
70
+ *
71
+ * @since 1.1.6
72
+ *
73
+ */
74
+ // WP 2.9+
75
+ add_action( "after_plugin_row_{$this->_fs->get_plugin_basename()}", array(
76
+ &$this,
77
+ 'catch_plugin_update_row'
78
+ ), 9 );
79
+ add_action( "after_plugin_row_{$this->_fs->get_plugin_basename()}", array(
80
+ &$this,
81
+ 'edit_and_echo_plugin_update_row'
82
+ ), 11, 2 );
83
+ }
84
+
85
+ if ( ! WP_FS__IS_PRODUCTION_MODE ) {
86
+ add_filter( 'http_request_host_is_external', array(
87
+ $this,
88
+ 'http_request_host_is_external_filter'
89
+ ), 10, 3 );
90
+ }
91
+
92
+ if ( $this->_fs->is_premium() && $this->is_correct_folder_name() ) {
93
+ add_filter( 'upgrader_post_install', array( &$this, '_maybe_update_folder_name' ), 10, 3 );
94
+ }
95
+ }
96
+
97
+ /**
98
+ * Capture plugin update row by turning output buffering.
99
+ *
100
+ * @author Vova Feldman (@svovaf)
101
+ * @since 1.1.6
102
+ */
103
+ function catch_plugin_update_row() {
104
+ ob_start();
105
+ }
106
+
107
+ /**
108
+ * Overrides default update message format with "renew your license" message.
109
+ *
110
+ * @author Vova Feldman (@svovaf)
111
+ * @since 1.1.6
112
+ *
113
+ * @param string $file
114
+ * @param array $plugin_data
115
+ */
116
+ function edit_and_echo_plugin_update_row( $file, $plugin_data ) {
117
+ $plugin_update_row = ob_get_clean();
118
+
119
+ $current = get_site_transient( 'update_plugins' );
120
+ if ( ! isset( $current->response[ $file ] ) ) {
121
+ echo $plugin_update_row;
122
+
123
+ return;
124
+ }
125
+
126
+ $r = $current->response[ $file ];
127
+
128
+ $plugin_update_row = preg_replace(
129
+ '/(\<div.+>)(.+)(\<a.+\<a.+)\<\/div\>/is',
130
+ '$1 $2 ' . sprintf(
131
+ $this->_fs->get_text_inline( '%sRenew your license now%s to access version %s features and support.', 'renew-license-now' ),
132
+ '<a href="' . $this->_fs->pricing_url() . '">', '</a>',
133
+ $r->new_version ) .
134
+ '$4',
135
+ $plugin_update_row
136
+ );
137
+
138
+ echo $plugin_update_row;
139
+ }
140
+
141
+ /**
142
+ * Since WP version 3.6, a new security feature was added that denies access to repository with a local ip.
143
+ * During development mode we want to be able updating plugin versions via our localhost repository. This
144
+ * filter white-list all domains including "api.freemius".
145
+ *
146
+ * @link http://www.emanueletessore.com/wordpress-download-failed-valid-url-provided/
147
+ *
148
+ * @author Vova Feldman (@svovaf)
149
+ * @since 1.0.4
150
+ *
151
+ * @param bool $allow
152
+ * @param string $host
153
+ * @param string $url
154
+ *
155
+ * @return bool
156
+ */
157
+ function http_request_host_is_external_filter( $allow, $host, $url ) {
158
+ return ( false !== strpos( $host, 'freemius' ) ) ? true : $allow;
159
+ }
160
+
161
+ /**
162
+ * Check for Updates at the defined API endpoint and modify the update array.
163
+ *
164
+ * This function dives into the update api just when WordPress creates its update array,
165
+ * then adds a custom API call and injects the custom plugin data retrieved from the API.
166
+ * It is reassembled from parts of the native WordPress plugin update code.
167
+ * See wp-includes/update.php line 121 for the original wp_update_plugins() function.
168
+ *
169
+ * @author Vova Feldman (@svovaf)
170
+ * @since 1.0.4
171
+ *
172
+ * @uses FS_Api
173
+ *
174
+ * @param object $transient_data Update array build by WordPress.
175
+ *
176
+ * @return object Modified update array with custom plugin data.
177
+ */
178
+ function pre_set_site_transient_update_plugins_filter( $transient_data ) {
179
+ $this->_logger->entrance();
180
+
181
+ /**
182
+ * "plugins" or "themes".
183
+ *
184
+ * @author Leo Fajardo (@leorw)
185
+ * @since 1.2.2
186
+ */
187
+ $module_type = $this->_fs->get_module_type() . 's';
188
+
189
+ /**
190
+ * Ensure that we don't mix plugins update info with themes update info.
191
+ *
192
+ * @author Leo Fajardo (@leorw)
193
+ * @since 1.2.2
194
+ */
195
+ if ( "pre_set_site_transient_update_{$module_type}" !== current_filter() ) {
196
+ return $transient_data;
197
+ }
198
+
199
+ if ( empty( $transient_data ) ||
200
+ defined( 'WP_FS__UNINSTALL_MODE' )
201
+ ) {
202
+ return $transient_data;
203
+ }
204
+
205
+ if ( ! isset( $this->_update_details ) ) {
206
+ // Get plugin's newest update.
207
+ $new_version = $this->_fs->get_update(
208
+ false,
209
+ fs_request_get_bool( 'force-check' ),
210
+ WP_FS__TIME_24_HOURS_IN_SEC / 24
211
+ );
212
+
213
+ $this->_update_details = false;
214
+
215
+ if ( is_object( $new_version ) ) {
216
+ $this->_logger->log( 'Found newer plugin version ' . $new_version->version );
217
+
218
+ $plugin_details = new stdClass();
219
+ $plugin_details->slug = $this->_fs->get_slug();
220
+ $plugin_details->new_version = $new_version->version;
221
+ $plugin_details->url = WP_FS__ADDRESS;
222
+ $plugin_details->package = $new_version->url;
223
+ $plugin_details->{ $this->_fs->get_module_type() } = $this->_fs->get_plugin_basename();
224
+
225
+ /**
226
+ * Cache plugin details locally since set_site_transient( 'update_plugins' )
227
+ * called multiple times and the non wp.org plugins are filtered after the
228
+ * call to .org.
229
+ *
230
+ * @since 1.1.8.1
231
+ */
232
+ $this->_update_details = $plugin_details;
233
+ }
234
+ }
235
+
236
+ if ( is_object( $this->_update_details ) ) {
237
+ // Add plugin to transient data.
238
+ if ( $this->_fs->is_plugin() ) {
239
+ $transient_data->response[ $this->_fs->get_plugin_basename() ] = $this->_update_details;
240
+ } else {
241
+ $transient_data->response[ $this->_update_details->theme ] = (array) $this->_update_details;
242
+ }
243
+ }
244
+
245
+ return $transient_data;
246
+ }
247
+
248
+ /**
249
+ * Try to fetch plugin's info from .org repository.
250
+ *
251
+ * @author Vova Feldman (@svovaf)
252
+ * @since 1.0.5
253
+ *
254
+ * @param string $action
255
+ * @param object $args
256
+ *
257
+ * @return bool|mixed
258
+ */
259
+ static function _fetch_plugin_info_from_repository( $action, $args ) {
260
+ $url = $http_url = 'http://api.wordpress.org/plugins/info/1.0/';
261
+ if ( $ssl = wp_http_supports( array( 'ssl' ) ) ) {
262
+ $url = set_url_scheme( $url, 'https' );
263
+ }
264
+
265
+ $args = array(
266
+ 'timeout' => 15,
267
+ 'body' => array(
268
+ 'action' => $action,
269
+ 'request' => serialize( $args )
270
+ )
271
+ );
272
+
273
+ $request = wp_remote_post( $url, $args );
274
+
275
+ if ( is_wp_error( $request ) ) {
276
+ return false;
277
+ }
278
+
279
+ $res = maybe_unserialize( wp_remote_retrieve_body( $request ) );
280
+
281
+ if ( ! is_object( $res ) && ! is_array( $res ) ) {
282
+ return false;
283
+ }
284
+
285
+ return $res;
286
+ }
287
+
288
+ /**
289
+ * Updates information on the "View version x.x details" page with custom data.
290
+ *
291
+ * @author Vova Feldman (@svovaf)
292
+ * @since 1.0.4
293
+ *
294
+ * @uses FS_Api
295
+ *
296
+ * @param object $data
297
+ * @param string $action
298
+ * @param mixed $args
299
+ *
300
+ * @return object
301
+ */
302
+ function plugins_api_filter( $data, $action = '', $args = null ) {
303
+ $this->_logger->entrance();
304
+
305
+ if ( ( 'plugin_information' !== $action ) ||
306
+ ! isset( $args->slug )
307
+ ) {
308
+ return $data;
309
+ }
310
+
311
+ $addon = false;
312
+ $is_addon = false;
313
+
314
+ if ( $this->_fs->get_slug() !== $args->slug ) {
315
+ $addon = $this->_fs->get_addon_by_slug( $args->slug );
316
+
317
+ if ( ! is_object( $addon ) ) {
318
+ return $data;
319
+ }
320
+
321
+ $is_addon = true;
322
+ }
323
+
324
+ $plugin_in_repo = false;
325
+ if ( ! $is_addon ) {
326
+ // Try to fetch info from .org repository.
327
+ $data = self::_fetch_plugin_info_from_repository( $action, $args );
328
+
329
+ $plugin_in_repo = ( false !== $data );
330
+ }
331
+
332
+ if ( ! $plugin_in_repo ) {
333
+ $data = $args;
334
+
335
+ // Fetch as much as possible info from local files.
336
+ $plugin_local_data = $this->_fs->get_plugin_data();
337
+ $data->name = $plugin_local_data['Name'];
338
+ $data->author = $plugin_local_data['Author'];
339
+ $data->sections = array(
340
+ 'description' => 'Upgrade ' . $plugin_local_data['Name'] . ' to latest.',
341
+ );
342
+
343
+ // @todo Store extra plugin info on Freemius or parse readme.txt markup.
344
+ /*$info = $this->_fs->get_api_site_scope()->call('/information.json');
345
+
346
+ if ( !isset($info->error) ) {
347
+ $data = $info;
348
+ }*/
349
+ }
350
+
351
+ // Get plugin's newest update.
352
+ $new_version = $this->get_latest_download_details( $is_addon ? $addon->id : false );
353
+
354
+ if ( ! is_object( $new_version ) || empty( $new_version->version ) ) {
355
+ $data->version = $this->_fs->get_plugin_version();
356
+ } else {
357
+ if ( $is_addon ) {
358
+ $data->name = $addon->title . ' ' . $this->_fs->get_text_inline( 'Add-On', 'addon' );
359
+ $data->slug = $addon->slug;
360
+ $data->url = WP_FS__ADDRESS;
361
+ $data->package = $new_version->url;
362
+ }
363
+
364
+ if ( ! $plugin_in_repo ) {
365
+ $data->last_updated = ! is_null( $new_version->updated ) ? $new_version->updated : $new_version->created;
366
+ $data->requires = $new_version->requires_platform_version;
367
+ $data->tested = $new_version->tested_up_to_version;
368
+ }
369
+
370
+ $data->version = $new_version->version;
371
+ $data->download_link = $new_version->url;
372
+ }
373
+
374
+ return $data;
375
+ }
376
+
377
+ /**
378
+ * @author Vova Feldman (@svovaf)
379
+ * @since 1.2.1.7
380
+ *
381
+ * @param number|bool $addon_id
382
+ *
383
+ * @return object
384
+ */
385
+ private function get_latest_download_details( $addon_id = false ) {
386
+ return $this->_fs->_fetch_latest_version( $addon_id );
387
+ }
388
+
389
+ /**
390
+ * Checks if a given basename has a matching folder name
391
+ * with the current context plugin.
392
+ *
393
+ * @author Vova Feldman (@svovaf)
394
+ * @since 1.2.1.6
395
+ *
396
+ * @param string $basename Current plugin's basename.
397
+ *
398
+ * @return bool
399
+ */
400
+ private function is_correct_folder_name( $basename = '' ) {
401
+ if ( empty( $basename ) ) {
402
+ $basename = $this->_fs->get_plugin_basename();
403
+ }
404
+
405
+ return ( $this->_fs->get_target_folder_name() != trim( dirname( $basename ), '/\\' ) );
406
+ }
407
+
408
+ /**
409
+ * This is a special after upgrade handler for migrating modules
410
+ * that didn't use the '-premium' suffix folder structure before
411
+ * the migration.
412
+ *
413
+ * @author Vova Feldman (@svovaf)
414
+ * @since 1.2.1.6
415
+ *
416
+ * @param bool $response Install response.
417
+ * @param array $hook_extra Extra arguments passed to hooked filters.
418
+ * @param array $result Installation result data.
419
+ *
420
+ * @return bool
421
+ */
422
+ function _maybe_update_folder_name( $response, $hook_extra, $result ) {
423
+ $basename = $this->_fs->get_plugin_basename();
424
+
425
+ if ( true !== $response ||
426
+ empty( $hook_extra ) ||
427
+ empty( $hook_extra['plugin'] ) ||
428
+ $basename !== $hook_extra['plugin']
429
+ ) {
430
+ return $response;
431
+ }
432
+
433
+ $active_plugins_basenames = get_option( 'active_plugins' );
434
+
435
+ for ( $i = 0, $len = count( $active_plugins_basenames ); $i < $len; $i ++ ) {
436
+ if ( $basename === $active_plugins_basenames[ $i ] ) {
437
+ // Get filename including extension.
438
+ $filename = basename( $basename );
439
+
440
+ $new_basename = plugin_basename(
441
+ trailingslashit( $this->_fs->get_slug() . ( $this->_fs->is_premium() ? '-premium' : '' ) ) .
442
+ $filename
443
+ );
444
+
445
+ // Verify that the expected correct path exists.
446
+ if ( file_exists( fs_normalize_path( WP_PLUGIN_DIR . '/' . $new_basename ) ) ) {
447
+ // Override active plugin name.
448
+ $active_plugins_basenames[ $i ] = $new_basename;
449
+ update_option( 'active_plugins', $active_plugins_basenames );
450
+ }
451
+
452
+ break;
453
+ }
454
+ }
455
+
456
+ return $response;
457
+ }
458
+
459
+ #----------------------------------------------------------------------------------
460
+ #region Auto Activation
461
+ #----------------------------------------------------------------------------------
462
+
463
+ /**
464
+ * Installs and active a plugin when explicitly requested that from a 3rd party service.
465
+ *
466
+ * This logic was inspired by the TGMPA GPL licensed library by Thomas Griffin.
467
+ *
468
+ * @link http://tgmpluginactivation.com/
469
+ *
470
+ * @author Vova Feldman
471
+ * @since 1.2.1.7
472
+ *
473
+ * @link https://make.wordpress.org/plugins/2017/03/16/clarification-of-guideline-8-executable-code-and-installs/
474
+ *
475
+ * @uses WP_Filesystem
476
+ * @uses WP_Error
477
+ * @uses WP_Upgrader
478
+ * @uses Plugin_Upgrader
479
+ * @uses Plugin_Installer_Skin
480
+ * @uses Plugin_Upgrader_Skin
481
+ *
482
+ * @param number|bool $plugin_id
483
+ *
484
+ * @return array
485
+ */
486
+ function install_and_activate_plugin( $plugin_id = false ) {
487
+ if ( ! empty( $plugin_id ) && ! FS_Plugin::is_valid_id( $plugin_id ) ) {
488
+ // Invalid plugin ID.
489
+ return array(
490
+ 'message' => $this->_fs->get_text_inline( 'Invalid module ID.', 'auto-install-error-invalid-id' ),
491
+ 'code' => 'invalid_module_id',
492
+ );
493
+ }
494
+
495
+ $is_addon = false;
496
+ if ( FS_Plugin::is_valid_id( $plugin_id ) &&
497
+ $plugin_id != $this->_fs->get_id()
498
+ ) {
499
+ $addon = $this->_fs->get_addon( $plugin_id );
500
+
501
+ if ( ! is_object( $addon ) ) {
502
+ // Invalid add-on ID.
503
+ return array(
504
+ 'message' => $this->_fs->get_text_inline( 'Invalid module ID.', 'auto-install-error-invalid-id' ),
505
+ 'code' => 'invalid_module_id',
506
+ );
507
+ }
508
+
509
+ $slug = $addon->slug;
510
+ $title = $addon->title . ' ' . $this->_fs->get_text_inline( 'Add-On', 'addon' );
511
+
512
+ $is_addon = true;
513
+ } else {
514
+ $slug = $this->_fs->get_slug();
515
+ $title = $this->_fs->get_plugin_title() .
516
+ ( $this->_fs->is_addon() ? ' ' . $this->_fs->get_text_inline( 'Add-On', 'addon' ) : '' );
517
+ }
518
+
519
+ if ( $this->is_premium_plugin_active( $plugin_id ) ) {
520
+ // Premium version already activated.
521
+ return array(
522
+ 'message' => $is_addon ?
523
+ $this->_fs->get_text_inline( 'Premium add-on version already installed.', 'auto-install-error-premium-addon-activated' ) :
524
+ $this->_fs->get_text_inline( 'Premium version already active.', 'auto-install-error-premium-activated' ),
525
+ 'code' => 'premium_installed',
526
+ );
527
+ }
528
+
529
+ $latest_version = $this->get_latest_download_details( $plugin_id );
530
+ $target_folder = "{$slug}-premium";
531
+
532
+ // Prep variables for Plugin_Installer_Skin class.
533
+ $extra = array();
534
+ $extra['slug'] = $target_folder;
535
+ $source = $latest_version->url;
536
+ $api = null;
537
+
538
+ $install_url = add_query_arg(
539
+ array(
540
+ 'action' => 'install-plugin',
541
+ 'plugin' => urlencode( $slug ),
542
+ ),
543
+ 'update.php'
544
+ );
545
+
546
+ if ( ! class_exists( 'Plugin_Upgrader', false ) ) {
547
+ // Include required resources for the installation.
548
+ require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
549
+ }
550
+
551
+ $skin_args = array(
552
+ 'type' => 'web',
553
+ 'title' => sprintf( $this->_fs->get_text_inline( 'Installing plugin: %s', 'installing-plugin-x' ), $title ),
554
+ 'url' => esc_url_raw( $install_url ),
555
+ 'nonce' => 'install-plugin_' . $slug,
556
+ 'plugin' => '',
557
+ 'api' => $api,
558
+ 'extra' => $extra,
559
+ );
560
+
561
+ // $skin = new Automatic_Upgrader_Skin( $skin_args );
562
+ // $skin = new Plugin_Installer_Skin( $skin_args );
563
+ $skin = new WP_Ajax_Upgrader_Skin( $skin_args );
564
+
565
+ // Create a new instance of Plugin_Upgrader.
566
+ $upgrader = new Plugin_Upgrader( $skin );
567
+
568
+ // Perform the action and install the plugin from the $source urldecode().
569
+ add_filter( 'upgrader_source_selection', array( &$this, '_maybe_adjust_source_dir' ), 1, 3 );
570
+
571
+ $install_result = $upgrader->install( $source );
572
+
573
+ remove_filter( 'upgrader_source_selection', array( &$this, '_maybe_adjust_source_dir' ), 1 );
574
+
575
+ if ( is_wp_error( $install_result ) ) {
576
+ return array(
577
+ 'message' => $install_result->get_error_message(),
578
+ 'code' => $install_result->get_error_code(),
579
+ );
580
+ } elseif ( is_wp_error( $skin->result ) ) {
581
+ return array(
582
+ 'message' => $skin->result->get_error_message(),
583
+ 'code' => $skin->result->get_error_code(),
584
+ );
585
+ } elseif ( $skin->get_errors()->get_error_code() ) {
586
+ return array(
587
+ 'message' => $skin->get_error_messages(),
588
+ 'code' => 'unknown',
589
+ );
590
+ } elseif ( is_null( $install_result ) ) {
591
+ global $wp_filesystem;
592
+
593
+ $error_code = 'unable_to_connect_to_filesystem';
594
+ $error_message = $this->_fs->get_text_inline( 'Unable to connect to the filesystem. Please confirm your credentials.' );
595
+
596
+ // Pass through the error from WP_Filesystem if one was raised.
597
+ if ( $wp_filesystem instanceof WP_Filesystem_Base &&
598
+ is_wp_error( $wp_filesystem->errors ) &&
599
+ $wp_filesystem->errors->get_error_code()
600
+ ) {
601
+ $error_message = $wp_filesystem->errors->get_error_message();
602
+ }
603
+
604
+ return array(
605
+ 'message' => $error_message,
606
+ 'code' => $error_code,
607
+ );
608
+ }
609
+
610
+ // Grab the full path to the main plugin's file.
611
+ $plugin_activate = $upgrader->plugin_info();
612
+
613
+ // Try to activate the plugin.
614
+ $activation_result = $this->try_activate_plugin( $plugin_activate );
615
+
616
+ if ( is_wp_error( $activation_result ) ) {
617
+ return array(
618
+ 'message' => $activation_result->get_error_message(),
619
+ 'code' => $activation_result->get_error_code(),
620
+ );
621
+ }
622
+
623
+ return $skin->get_upgrade_messages();
624
+ }
625
+
626
+ /**
627
+ * Tries to activate a plugin. If fails, returns the error.
628
+ *
629
+ * @author Vova Feldman
630
+ * @since 1.2.1.7
631
+ *
632
+ * @param string $file_path Path within wp-plugins/ to main plugin file.
633
+ * This determines the styling of the output messages.
634
+ *
635
+ * @return bool|WP_Error
636
+ */
637
+ protected function try_activate_plugin( $file_path ) {
638
+ $activate = activate_plugin( $file_path );
639
+
640
+ return is_wp_error( $activate ) ?
641
+ $activate :
642
+ true;
643
+ }
644
+
645
+ /**
646
+ * Check if a premium module version is already active.
647
+ *
648
+ * @author Vova Feldman
649
+ * @since 1.2.1.7
650
+ *
651
+ * @param number|bool $plugin_id
652
+ *
653
+ * @return bool
654
+ */
655
+ private function is_premium_plugin_active( $plugin_id = false ) {
656
+ if ( $plugin_id != $this->_fs->get_id() ) {
657
+ return $this->_fs->is_addon_activated( $plugin_id, true );
658
+ }
659
+
660
+ return is_plugin_active( $this->_fs->premium_plugin_basename() );
661
+ }
662
+
663
+ /**
664
+ * Adjust the plugin directory name if necessary.
665
+ * Assumes plugin has a folder (not a single file plugin).
666
+ *
667
+ * The final destination directory of a plugin is based on the subdirectory name found in the
668
+ * (un)zipped source. In some cases this subdirectory name is not the same as the expected
669
+ * slug and the plugin will not be recognized as installed. This is fixed by adjusting
670
+ * the temporary unzipped source subdirectory name to the expected plugin slug.
671
+ *
672
+ * @author Vova Feldman
673
+ * @since 1.2.1.7
674
+ *
675
+ * @param string $source Path to upgrade/zip-file-name.tmp/subdirectory/.
676
+ * @param string $remote_source Path to upgrade/zip-file-name.tmp.
677
+ * @param \WP_Upgrader $upgrader Instance of the upgrader which installs the plugin.
678
+ *
679
+ * @return string|WP_Error
680
+ */
681
+ function _maybe_adjust_source_dir( $source, $remote_source, $upgrader ) {
682
+ if ( ! is_object( $GLOBALS['wp_filesystem'] ) ) {
683
+ return $source;
684
+ }
685
+
686
+ // Figure out what the slug is supposed to be.
687
+ $desired_slug = $upgrader->skin->options['extra']['slug'];
688
+
689
+ $subdir_name = untrailingslashit( str_replace( trailingslashit( $remote_source ), '', $source ) );
690
+
691
+ if ( ! empty( $subdir_name ) && $subdir_name !== $desired_slug ) {
692
+ $from_path = untrailingslashit( $source );
693
+ $to_path = trailingslashit( $remote_source ) . $desired_slug;
694
+
695
+ if ( true === $GLOBALS['wp_filesystem']->move( $from_path, $to_path ) ) {
696
+ return trailingslashit( $to_path );
697
+ } else {
698
+ return new WP_Error(
699
+ 'rename_failed',
700
+ $this->_fs->get_text_inline( 'The remote plugin package does not contain a folder with the desired slug and renaming did not work.', 'module-package-rename-failure' ),
701
+ array(
702
+ 'found' => $subdir_name,
703
+ 'expected' => $desired_slug
704
+ ) );
705
+ }
706
+ }
707
+
708
+ return $source;
709
+ }
710
+
711
+ #endregion
712
  }
freemius/includes/class-fs-security.php CHANGED
@@ -2,7 +2,7 @@
2
  /**
3
  * @package Freemius
4
  * @copyright Copyright (c) 2015, Freemius, Inc.
5
- * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
6
  * @since 1.0.3
7
  */
8
 
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
 
freemius/includes/customizer/class-fs-customizer-support-section.php ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.2.2.7
7
+ */
8
+
9
+ if ( ! defined( 'ABSPATH' ) ) {
10
+ exit;
11
+ }
12
+
13
+ /**
14
+ * Class Zerif_Customizer_Theme_Info_Main
15
+ *
16
+ * @since 1.0.0
17
+ * @access public
18
+ */
19
+ class FS_Customizer_Support_Section extends WP_Customize_Section {
20
+
21
+ function __construct( $manager, $id, $args = array() ) {
22
+ $manager->register_section_type( 'FS_Customizer_Support_Section' );
23
+
24
+ parent::__construct( $manager, $id, $args );
25
+ }
26
+
27
+ /**
28
+ * The type of customize section being rendered.
29
+ *
30
+ * @since 1.0.0
31
+ * @access public
32
+ * @var string
33
+ */
34
+ public $type = 'freemius-support-section';
35
+
36
+ /**
37
+ * @var Freemius
38
+ */
39
+ public $fs = null;
40
+
41
+ /**
42
+ * Add custom parameters to pass to the JS via JSON.
43
+ *
44
+ * @since 1.0.0
45
+ */
46
+ public function json() {
47
+ $json = parent::json();
48
+
49
+ $is_contact_visible = $this->fs->is_page_visible( 'contact' );
50
+ $is_support_visible = $this->fs->is_page_visible( 'support' );
51
+
52
+ $json['theme_title'] = $this->fs->get_plugin_name();
53
+
54
+ if ( $is_contact_visible && $is_support_visible ) {
55
+ $json['theme_title'] .= ' ' . $this->fs->get_text_inline( 'Support', 'support' );
56
+ }
57
+
58
+ if ( $is_contact_visible ) {
59
+ $json['contact'] = array(
60
+ 'label' => $this->fs->get_text_inline( 'Contact Us', 'contact-us' ),
61
+ 'url' => $this->fs->contact_url(),
62
+ );
63
+ }
64
+
65
+ if ( $is_support_visible ) {
66
+ $json['support'] = array(
67
+ 'label' => $this->fs->get_text_inline( 'Support Forum', 'support-forum' ),
68
+ 'url' => $this->fs->get_support_forum_url()
69
+ );
70
+ }
71
+
72
+ return $json;
73
+ }
74
+
75
+ /**
76
+ * Outputs the Underscore.js template.
77
+ *
78
+ * @since 1.0.0
79
+ */
80
+ protected function render_template() {
81
+ ?>
82
+ <li id="fs_customizer_support"
83
+ class="accordion-section control-section control-section-{{ data.type }} cannot-expand">
84
+ <h3 class="accordion-section-title">
85
+ <span>{{ data.theme_title }}</span>
86
+ <# if ( data.contact && data.support ) { #>
87
+ <div class="button-group">
88
+ <# } #>
89
+ <# if ( data.contact ) { #>
90
+ <a class="button" href="{{ data.contact.url }}" target="_blank">{{ data.contact.label }} </a>
91
+ <# } #>
92
+ <# if ( data.support ) { #>
93
+ <a class="button" href="{{ data.support.url }}" target="_blank">{{ data.support.label }} </a>
94
+ <# } #>
95
+ <# if ( data.contact && data.support ) { #>
96
+ </div>
97
+ <# } #>
98
+ </h3>
99
+ </li>
100
+ <?php
101
+ }
102
+ }
freemius/includes/customizer/class-fs-customizer-upsell-control.php ADDED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.2.2.7
7
+ */
8
+
9
+ if ( ! defined( 'ABSPATH' ) ) {
10
+ exit;
11
+ }
12
+
13
+ /**
14
+ * Class FS_Customizer_Upsell_Control
15
+ */
16
+ class FS_Customizer_Upsell_Control extends WP_Customize_Control {
17
+
18
+ /**
19
+ * Control type
20
+ *
21
+ * @var string control type
22
+ */
23
+ public $type = 'freemius-upsell-control';
24
+
25
+ /**
26
+ * @var Freemius
27
+ */
28
+ public $fs = null;
29
+
30
+ /**
31
+ * @param WP_Customize_Manager $manager the customize manager class.
32
+ * @param string $id id.
33
+ * @param array $args customizer manager parameters.
34
+ */
35
+ public function __construct( WP_Customize_Manager $manager, $id, array $args ) {
36
+ $manager->register_control_type( 'FS_Customizer_Upsell_Control' );
37
+
38
+ parent::__construct( $manager, $id, $args );
39
+ }
40
+
41
+ /**
42
+ * Enqueue resources for the control.
43
+ */
44
+ public function enqueue() {
45
+ fs_enqueue_local_style( 'fs_customizer', 'customizer.css' );
46
+ }
47
+
48
+ /**
49
+ * Json conversion
50
+ */
51
+ public function to_json() {
52
+ $pricing_cta = esc_html( $this->fs->get_pricing_cta_label() ) . '&nbsp;&nbsp;' . ( is_rtl() ? '&#x2190;' : '&#x27a4;' );
53
+
54
+ parent::to_json();
55
+
56
+ $this->json['button_text'] = $pricing_cta;
57
+ $this->json['button_url'] = $this->fs->is_in_trial_promotion() ?
58
+ $this->fs->get_trial_url() :
59
+ $this->fs->get_upgrade_url();
60
+
61
+ // Load features.
62
+ $pricing = $this->fs->get_api_plugin_scope()->get( 'pricing.json' );
63
+
64
+ if ( $this->fs->is_api_result_object( $pricing, 'plans' ) ) {
65
+ // Add support features.
66
+ if ( is_array( $pricing->plans ) && 0 < count( $pricing->plans ) ) {
67
+ $support_features = array(
68
+ 'kb' => 'Help Center',
69
+ 'forum' => 'Support Forum',
70
+ 'email' => 'Priority Email Support',
71
+ 'phone' => 'Phone Support',
72
+ 'skype' => 'Skype Support',
73
+ 'is_success_manager' => 'Personal Success Manager',
74
+ );
75
+
76
+ for ( $i = 0, $len = count( $pricing->plans ); $i < $len; $i ++ ) {
77
+ if ( 'free' == $pricing->plans[$i]->name ) {
78
+ continue;
79
+ }
80
+
81
+ if ( ! isset( $pricing->plans[ $i ]->features ) ||
82
+ ! is_array( $pricing->plans[ $i ]->features ) ) {
83
+ $pricing->plans[$i]->features = array();
84
+ }
85
+
86
+ foreach ( $support_features as $key => $label ) {
87
+ $key = ( 'is_success_manager' !== $key ) ?
88
+ "support_{$key}" :
89
+ $key;
90
+
91
+ if ( ! empty( $pricing->plans[ $i ]->{$key} ) ) {
92
+
93
+ $support_feature = new stdClass();
94
+ $support_feature->title = $label;
95
+
96
+ $pricing->plans[ $i ]->features[] = $support_feature;
97
+ }
98
+ }
99
+ }
100
+ }
101
+ }
102
+
103
+ $this->json['plans'] = $pricing->plans;
104
+
105
+ $this->json['strings'] = array(
106
+ 'plan' => $this->fs->get_text_x_inline( 'Plan', 'as product pricing plan', 'plan' ),
107
+ );
108
+ }
109
+
110
+ /**
111
+ * Control content
112
+ */
113
+ public function content_template() {
114
+ ?>
115
+ <div id="fs_customizer_upsell">
116
+ <# if ( data.plans ) { #>
117
+ <ul class="fs-customizer-plans">
118
+ <# for (i in data.plans) { #>
119
+ <# if ( 'free' != data.plans[i].name && (null != data.plans[i].features && 0 < data.plans[i].features.length) ) { #>
120
+ <li class="fs-customizer-plan">
121
+ <div class="fs-accordion-section-open">
122
+ <h2 class="fs-accordion-section-title menu-item">
123
+ <span>{{ data.plans[i].title }}</span>
124
+ <button type="button" class="button-link item-edit" aria-expanded="true">
125
+ <span class="screen-reader-text">Toggle section: {{ data.plans[i].title }} {{ data.strings.plan }}</span>
126
+ <span class="toggle-indicator" aria-hidden="true"></span>
127
+ </button>
128
+ </h2>
129
+ <div class="fs-accordion-section-content">
130
+ <# if ( data.plans[i].description ) { #>
131
+ <h3>{{ data.plans[i].description }}</h3>
132
+ <# } #>
133
+ <# if ( data.plans[i].features ) { #>
134
+ <ul>
135
+ <# for ( j in data.plans[i].features ) { #>
136
+ <li><div class="fs-feature">
137
+ <span class="dashicons dashicons-yes"></span><span><# if ( data.plans[i].features[j].value ) { #>{{ data.plans[i].features[j].value }} <# } #>{{ data.plans[i].features[j].title }}</span>
138
+ <# if ( data.plans[i].features[j].description ) { #>
139
+ <span class="dashicons dashicons-editor-help"><span class="fs-feature-desc">{{ data.plans[i].features[j].description }}</span></span>
140
+ <# } #>
141
+ </div></li>
142
+ <# } #>
143
+ </ul>
144
+ <# } #>
145
+ <# if ( 'free' != data.plans[i].name ) { #>
146
+ <a href="{{ data.button_url }}" class="button button-primary" target="_blank">{{{ data.button_text }}}</a>
147
+ <# } #>
148
+ </div>
149
+ </div>
150
+ </li>
151
+ <# } #>
152
+ <# } #>
153
+ </ul>
154
+ <# } #>
155
+ </div>
156
+ <?php }
157
+ }
freemius/includes/customizer/index.php ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ <?php
2
+ // Silence is golden.
3
+ // Hide file structure from users on unprotected servers.
freemius/includes/debug/class-fs-debug-bar-panel.php CHANGED
@@ -2,7 +2,7 @@
2
  /**
3
  * @package Freemius
4
  * @copyright Copyright (c) 2015, Freemius, Inc.
5
- * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
6
  * @since 1.1.7.3
7
  */
8
 
@@ -24,8 +24,8 @@
24
  }
25
 
26
  static function requests_count() {
27
- if ( class_exists( 'Freemius_Api' ) ) {
28
- $logger = Freemius_Api::GetLogger();
29
  } else {
30
  $logger = array();
31
  }
@@ -34,8 +34,8 @@
34
  }
35
 
36
  static function total_time() {
37
- if ( class_exists( 'Freemius_Api' ) ) {
38
- $logger = Freemius_Api::GetLogger();
39
  } else {
40
  $logger = array();
41
  }
@@ -45,7 +45,7 @@
45
  $total_time += $l['total'];
46
  }
47
 
48
- return number_format( 100 * $total_time, 2 ) . ' ' . fs_text( 'ms' );
49
  }
50
 
51
  function render() {
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.1.7.3
7
  */
8
 
24
  }
25
 
26
  static function requests_count() {
27
+ if ( class_exists( 'Freemius_Api_WordPress' ) ) {
28
+ $logger = Freemius_Api_WordPress::GetLogger();
29
  } else {
30
  $logger = array();
31
  }
34
  }
35
 
36
  static function total_time() {
37
+ if ( class_exists( 'Freemius_Api_WordPress' ) ) {
38
+ $logger = Freemius_Api_WordPress::GetLogger();
39
  } else {
40
  $logger = array();
41
  }
45
  $total_time += $l['total'];
46
  }
47
 
48
+ return number_format( 100 * $total_time, 2 ) . ' ' . fs_text_x_inline( 'ms', 'milliseconds' );
49
  }
50
 
51
  function render() {
freemius/includes/debug/debug-bar-start.php CHANGED
@@ -2,7 +2,7 @@
2
  /**
3
  * @package Freemius
4
  * @copyright Copyright (c) 2015, Freemius, Inc.
5
- * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
6
  * @since 1.1.7.3
7
  */
8
 
@@ -38,8 +38,8 @@
38
  require_once dirname( __FILE__ ) . '/class-fs-debug-bar-panel.php';
39
  $statuses[] = array(
40
  'fs_api_requests',
41
- fs_text( 'Freemius API' ),
42
- Freemius_Debug_Bar_Panel::requests_count() . ' ' . fs_text( 'Requests' ) .
43
  ' (' . Freemius_Debug_Bar_Panel::total_time() . ')'
44
  );
45
  }
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.1.7.3
7
  */
8
 
38
  require_once dirname( __FILE__ ) . '/class-fs-debug-bar-panel.php';
39
  $statuses[] = array(
40
  'fs_api_requests',
41
+ fs_text_inline( 'Freemius API' ),
42
+ Freemius_Debug_Bar_Panel::requests_count() . ' ' . fs_text_inline( 'Requests' ) .
43
  ' (' . Freemius_Debug_Bar_Panel::total_time() . ')'
44
  );
45
  }
freemius/includes/entities/class-fs-affiliate-terms.php ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.2.3
7
+ */
8
+
9
+ if ( ! defined( 'ABSPATH' ) ) {
10
+ exit;
11
+ }
12
+
13
+ class FS_AffiliateTerms extends FS_Scope_Entity {
14
+
15
+ #region Properties
16
+
17
+ /**
18
+ * @var bool
19
+ */
20
+ public $is_active;
21
+ /**
22
+ * @var string Enum: `affiliation` or `rewards`. Defaults to `affiliation`.
23
+ */
24
+ public $type;
25
+ /**
26
+ * @var string Enum: `payout` or `credit`. Defaults to `payout`.
27
+ */
28
+ public $reward_type;
29
+ /**
30
+ * If `first`, the referral will be attributed to the first visited source containing the affiliation link that
31
+ * was clicked.
32
+ *
33
+ * @var string Enum: `first` or `last`. Defaults to `first`.
34
+ */
35
+ public $referral_attribution;
36
+ /**
37
+ * @var int Defaults to `30`, `0` for session cookie, and `null` for endless cookie (until cookies are cleaned).
38
+ */
39
+ public $cookie_days;
40
+ /**
41
+ * @var int
42
+ */
43
+ public $commission;
44
+ /**
45
+ * @var string Enum: `percentage` or `dollar`. Defaults to `percentage`.
46
+ */
47
+ public $commission_type;
48
+ /**
49
+ * @var null|int Defaults to `0` (affiliate only on first payment). `null` for commission for all renewals. If
50
+ * greater than `0`, affiliate will get paid for all renewals for `commission_renewals_days` days after
51
+ * the initial upgrade/purchase.
52
+ */
53
+ public $commission_renewals_days;
54
+ /**
55
+ * @var int Only cents and no percentage. In US cents, e.g.: 100 = $1.00. Defaults to `null`.
56
+ */
57
+ public $install_commission;
58
+ /**
59
+ * @var string Required default target link, e.g.: pricing page.
60
+ */
61
+ public $default_url;
62
+ /**
63
+ * @var string One of the following: 'all', 'new_customer', 'new_user'.
64
+ * If 'all' - reward for any user type.
65
+ * If 'new_customer' - reward only for new customers.
66
+ * If 'new_user' - reward only for new users.
67
+ */
68
+ public $reward_customer_type;
69
+ /**
70
+ * @var int Defaults to `0` (affiliate only on directly affiliated links). `null` if an affiliate will get
71
+ * paid for all customers' lifetime payments. If greater than `0`, an affiliate will get paid for all
72
+ * customer payments for `future_payments_days` days after the initial payment.
73
+ */
74
+ public $future_payments_days;
75
+ /**
76
+ * @var bool If `true`, allow referrals from social sites.
77
+ */
78
+ public $is_social_allowed;
79
+ /**
80
+ * @var bool If `true`, allow conversions without HTTP referrer header at all.
81
+ */
82
+ public $is_app_allowed;
83
+ /**
84
+ * @var bool If `true`, allow referrals from any site.
85
+ */
86
+ public $is_any_site_allowed;
87
+
88
+ #endregion Properties
89
+
90
+ /**
91
+ * @author Leo Fajardo (@leorw)
92
+ *
93
+ * @return string
94
+ */
95
+ function get_formatted_commission()
96
+ {
97
+ return ( 'dollar' === $this->commission_type ) ?
98
+ ( '$' . $this->commission ) :
99
+ ( $this->commission . '%' );
100
+ }
101
+
102
+ /**
103
+ * @author Leo Fajardo (@leorw)
104
+ *
105
+ * @return bool
106
+ */
107
+ function has_lifetime_commission() {
108
+ return ( 0 !== $this->future_payments_days );
109
+ }
110
+
111
+ /**
112
+ * @author Leo Fajardo (@leorw)
113
+ *
114
+ * @return bool
115
+ */
116
+ function is_session_cookie() {
117
+ return ( 0 == $this->cookie_days );
118
+ }
119
+
120
+ /**
121
+ * @author Leo Fajardo (@leorw)
122
+ *
123
+ * @return bool
124
+ */
125
+ function has_renewals_commission() {
126
+ return ( is_null( $this->commission_renewals_days ) || $this->commission_renewals_days > 0 );
127
+ }
128
+ }
freemius/includes/entities/class-fs-affiliate.php ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.2.3
7
+ */
8
+
9
+ if ( ! defined( 'ABSPATH' ) ) {
10
+ exit;
11
+ }
12
+
13
+ class FS_Affiliate extends FS_Scope_Entity {
14
+
15
+ #region Properties
16
+
17
+ /**
18
+ * @var string
19
+ */
20
+ public $paypal_email;
21
+ /**
22
+ * @var number
23
+ */
24
+ public $custom_affiliate_terms_id;
25
+ /**
26
+ * @var boolean
27
+ */
28
+ public $is_using_custom_terms;
29
+ /**
30
+ * @var string status Enum: `pending`, `rejected`, `suspended`, or `active`. Defaults to `pending`.
31
+ */
32
+ public $status;
33
+ /**
34
+ * @var string
35
+ */
36
+ public $domain;
37
+
38
+ #endregion Properties
39
+
40
+ /**
41
+ * @author Leo Fajardo
42
+ *
43
+ * @return bool
44
+ */
45
+ function is_active() {
46
+ return ( 'active' === $this->status );
47
+ }
48
+
49
+ /**
50
+ * @author Leo Fajardo
51
+ *
52
+ * @return bool
53
+ */
54
+ function is_pending() {
55
+ return ( 'pending' === $this->status );
56
+ }
57
+
58
+ /**
59
+ * @author Leo Fajardo
60
+ *
61
+ * @return bool
62
+ */
63
+ function is_suspended() {
64
+ return ( 'suspended' === $this->status );
65
+ }
66
+
67
+ /**
68
+ * @author Leo Fajardo
69
+ *
70
+ * @return bool
71
+ */
72
+ function is_rejected() {
73
+ return ( 'rejected' === $this->status );
74
+ }
75
+
76
+ /**
77
+ * @author Leo Fajardo
78
+ *
79
+ * @return bool
80
+ */
81
+ function is_blocked() {
82
+ return ( 'blocked' === $this->status );
83
+ }
84
+ }
freemius/includes/entities/class-fs-billing.php CHANGED
@@ -2,7 +2,7 @@
2
  /**
3
  * @package Freemius for EDD Add-On
4
  * @copyright Copyright (c) 2016, Freemius, Inc.
5
- * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
6
  * @since 1.0.0
7
  */
8
 
2
  /**
3
  * @package Freemius for EDD Add-On
4
  * @copyright Copyright (c) 2016, Freemius, Inc.
5
+ * @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3
6
  * @since 1.0.0
7
  */
8
 
freemius/includes/entities/class-fs-entity.php CHANGED
@@ -2,7 +2,7 @@
2
  /**
3
  * @package Freemius
4
  * @copyright Copyright (c) 2015, Freemius, Inc.
5
- * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
6
  * @since 1.0.3
7
  */
8
 
@@ -42,9 +42,9 @@
42
  * @param bool|object $entity
43
  */
44
  function __construct( $entity = false ) {
45
- if ( ! ( $entity instanceof stdClass ) ) {
46
- return;
47
- }
48
 
49
  $props = fs_get_object_public_vars( $this );
50
 
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
 
42
  * @param bool|object $entity
43
  */
44
  function __construct( $entity = false ) {
45
+ if ( ! ( $entity instanceof stdClass ) && ! ( $entity instanceof FS_Entity ) ) {
46
+ return;
47
+ }
48
 
49
  $props = fs_get_object_public_vars( $this );
50
 
freemius/includes/entities/class-fs-payment.php CHANGED
@@ -2,7 +2,7 @@
2
  /**
3
  * @package Freemius
4
  * @copyright Copyright (c) 2016, Freemius, Inc.
5
- * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
6
  * @since 1.0.0
7
  */
8
 
2
  /**
3
  * @package Freemius
4
  * @copyright Copyright (c) 2016, Freemius, Inc.
5
+ * @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3
6
  * @since 1.0.0
7
  */
8
 
freemius/includes/entities/class-fs-plugin-info.php CHANGED
@@ -2,7 +2,7 @@
2
  /**
3
  * @package Freemius
4
  * @copyright Copyright (c) 2015, Freemius, Inc.
5
- * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
6
  * @since 1.0.3
7
  */
8
 
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
 
freemius/includes/entities/class-fs-plugin-license.php CHANGED
@@ -2,7 +2,7 @@
2
  /**
3
  * @package Freemius
4
  * @copyright Copyright (c) 2015, Freemius, Inc.
5
- * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
6
  * @since 1.0.5
7
  */
8
 
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.5
7
  */
8
 
freemius/includes/entities/class-fs-plugin-plan.php CHANGED
@@ -2,7 +2,7 @@
2
  /**
3
  * @package Freemius
4
  * @copyright Copyright (c) 2015, Freemius, Inc.
5
- * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
6
  * @since 1.0.5
7
  */
8
 
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.5
7
  */
8
 
freemius/includes/entities/class-fs-plugin-tag.php CHANGED
@@ -2,7 +2,7 @@
2
  /**
3
  * @package Freemius
4
  * @copyright Copyright (c) 2015, Freemius, Inc.
5
- * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
6
  * @since 1.0.4
7
  */
8
 
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.4
7
  */
8
 
freemius/includes/entities/class-fs-plugin.php CHANGED
@@ -2,7 +2,7 @@
2
  /**
3
  * @package Freemius
4
  * @copyright Copyright (c) 2015, Freemius, Inc.
5
- * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
6
  * @since 1.0.3
7
  */
8
 
@@ -25,6 +25,8 @@
25
  */
26
  public $slug;
27
  /**
 
 
28
  * @var string 'plugin' or 'theme'
29
  */
30
  public $type;
@@ -59,6 +61,17 @@
59
  * @var bool
60
  */
61
  public $is_live;
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  #endregion Install Specific Properties
64
 
@@ -88,6 +101,16 @@
88
  return isset( $this->parent_plugin_id ) && is_numeric( $this->parent_plugin_id );
89
  }
90
 
 
 
 
 
 
 
 
 
 
 
91
  static function get_type() {
92
  return 'plugin';
93
  }
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
 
25
  */
26
  public $slug;
27
  /**
28
+ * @since 1.2.2
29
+ *
30
  * @var string 'plugin' or 'theme'
31
  */
32
  public $type;
61
  * @var bool
62
  */
63
  public $is_live;
64
+ /**
65
+ * @author Leo Fajardo (@leorw)
66
+ *
67
+ * @since 1.2.3
68
+ *
69
+ * @var string|false false if the module doesn't have an affiliate program or one of the following:
70
+ * 'selected', 'customers', or 'all'.
71
+ */
72
+ public $affiliate_moderation;
73
+
74
+ const AFFILIATE_MODERATION_CUSTOMERS = 'customers';
75
 
76
  #endregion Install Specific Properties
77
 
101
  return isset( $this->parent_plugin_id ) && is_numeric( $this->parent_plugin_id );
102
  }
103
 
104
+ /**
105
+ * @author Leo Fajardo (@leorw)
106
+ * @since 1.2.3
107
+ *
108
+ * @return bool
109
+ */
110
+ function has_affiliate_program() {
111
+ return ( ! empty( $this->affiliate_moderation ) );
112
+ }
113
+
114
  static function get_type() {
115
  return 'plugin';
116
  }
freemius/includes/entities/class-fs-pricing.php CHANGED
@@ -2,7 +2,7 @@
2
  /**
3
  * @package Freemius for EDD Add-On
4
  * @copyright Copyright (c) 2015, Freemius, Inc.
5
- * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
6
  * @since 1.0.0
7
  */
8
 
2
  /**
3
  * @package Freemius for EDD Add-On
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.0
7
  */
8
 
freemius/includes/entities/class-fs-scope-entity.php CHANGED
@@ -2,7 +2,7 @@
2
  /**
3
  * @package Freemius
4
  * @copyright Copyright (c) 2015, Freemius, Inc.
5
- * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
6
  * @since 1.0.4
7
  */
8
 
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.4
7
  */
8
 
freemius/includes/entities/class-fs-site.php CHANGED
@@ -2,7 +2,7 @@
2
  /**
3
  * @package Freemius
4
  * @copyright Copyright (c) 2015, Freemius, Inc.
5
- * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
6
  * @since 1.0.3
7
  */
8
 
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
 
freemius/includes/entities/class-fs-subscription.php CHANGED
@@ -2,7 +2,7 @@
2
  /**
3
  * @package Freemius
4
  * @copyright Copyright (c) 2015, Freemius, Inc.
5
- * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
6
  * @since 1.0.9
7
  */
8
 
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.9
7
  */
8
 
freemius/includes/entities/class-fs-user.php CHANGED
@@ -2,7 +2,7 @@
2
  /**
3
  * @package Freemius
4
  * @copyright Copyright (c) 2015, Freemius, Inc.
5
- * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
6
  * @since 1.0.3
7
  */
8
 
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
 
freemius/includes/fs-core-functions.php CHANGED
@@ -1,629 +1,1118 @@
1
- <?php
2
- /**
3
- * @package Freemius
4
- * @copyright Copyright (c) 2015, Freemius, Inc.
5
- * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
6
- * @since 1.0.3
7
- */
8
-
9
- if ( ! defined( 'ABSPATH' ) ) {
10
- exit;
11
- }
12
-
13
- if ( ! function_exists( 'fs_dummy' ) ) {
14
- function fs_dummy() {
15
- }
16
- }
17
-
18
- /* Url.
19
- --------------------------------------------------------------------------------------------*/
20
- function fs_get_url_daily_cache_killer() {
21
- return date( '\YY\Mm\Dd' );
22
- }
23
-
24
- /* Templates / Views.
25
- --------------------------------------------------------------------------------------------*/
26
- if ( ! function_exists( 'fs_get_template_path' ) ) {
27
- function fs_get_template_path( $path ) {
28
- return WP_FS__DIR_TEMPLATES . '/' . trim( $path, '/' );
29
- }
30
-
31
- function fs_include_template( $path, &$params = null ) {
32
- $VARS = &$params;
33
- include fs_get_template_path( $path );
34
- }
35
-
36
- function fs_include_once_template( $path, &$params = null ) {
37
- $VARS = &$params;
38
- include_once fs_get_template_path( $path );
39
- }
40
-
41
- function fs_require_template( $path, &$params = null ) {
42
- $VARS = &$params;
43
- require fs_get_template_path( $path );
44
- }
45
-
46
- function fs_require_once_template( $path, &$params = null ) {
47
- $VARS = &$params;
48
- require_once fs_get_template_path( $path );
49
- }
50
-
51
- function fs_get_template( $path, &$params = null ) {
52
- ob_start();
53
-
54
- $VARS = &$params;
55
- require fs_get_template_path( $path );
56
-
57
- return ob_get_clean();
58
- }
59
- }
60
-
61
- /* Scripts and styles including.
62
- --------------------------------------------------------------------------------------------*/
63
- function fs_enqueue_local_style( $handle, $path, $deps = array(), $ver = false, $media = 'all' ) {
64
- wp_enqueue_style( $handle, plugins_url( plugin_basename( WP_FS__DIR_CSS . '/' . trim( $path, '/' ) ) ), $deps, $ver, $media );
65
- }
66
-
67
- function fs_enqueue_local_script( $handle, $path, $deps = array(), $ver = false, $in_footer = 'all' ) {
68
- wp_enqueue_script( $handle, plugins_url( plugin_basename( WP_FS__DIR_JS . '/' . trim( $path, '/' ) ) ), $deps, $ver, $in_footer );
69
- }
70
-
71
- function fs_img_url( $path, $img_dir = WP_FS__DIR_IMG ) {
72
- return plugins_url( plugin_basename( $img_dir . '/' . trim( $path, '/' ) ) );
73
- }
74
-
75
- /* Request handlers.
76
- --------------------------------------------------------------------------------------------*/
77
- /**
78
- * @param string $key
79
- * @param mixed $def
80
- * @param string|bool $type Since 1.2.1.7 - when set to 'get' will look for the value passed via querystring, when
81
- * set to 'post' will look for the value passed via the POST request's body, otherwise,
82
- * will check if the parameter was passed in any of the two.
83
- *
84
- * @return mixed
85
- */
86
- function fs_request_get( $key, $def = false, $type = false ) {
87
- if ( is_string( $type ) ) {
88
- $type = strtolower( $type );
89
- }
90
-
91
- switch ( $type ) {
92
- case 'post':
93
- $value = isset( $_POST[ $key ] ) ? $_POST[ $key ] : $def;
94
- break;
95
- case 'get':
96
- $value = isset( $_GET[ $key ] ) ? $_GET[ $key ] : $def;
97
- break;
98
- default:
99
- $value = isset( $_REQUEST[ $key ] ) ? $_REQUEST[ $key ] : $def;
100
- break;
101
- }
102
-
103
- return $value;
104
- }
105
-
106
- function fs_request_has( $key ) {
107
- return isset( $_REQUEST[ $key ] );
108
- }
109
-
110
- function fs_request_get_bool( $key, $def = false ) {
111
- if ( ! isset( $_REQUEST[ $key ] ) ) {
112
- return $def;
113
- }
114
-
115
- if ( 1 == $_REQUEST[ $key ] || 'true' === strtolower( $_REQUEST[ $key ] ) ) {
116
- return true;
117
- }
118
-
119
- if ( 0 == $_REQUEST[ $key ] || 'false' === strtolower( $_REQUEST[ $key ] ) ) {
120
- return false;
121
- }
122
-
123
- return $def;
124
- }
125
-
126
- function fs_request_is_post() {
127
- return ( 'post' === strtolower( $_SERVER['REQUEST_METHOD'] ) );
128
- }
129
-
130
- function fs_request_is_get() {
131
- return ( 'get' === strtolower( $_SERVER['REQUEST_METHOD'] ) );
132
- }
133
-
134
- function fs_get_action( $action_key = 'action' ) {
135
- if ( ! empty( $_REQUEST[ $action_key ] ) ) {
136
- return strtolower( $_REQUEST[ $action_key ] );
137
- }
138
-
139
- if ( 'action' == $action_key ) {
140
- $action_key = 'fs_action';
141
-
142
- if ( ! empty( $_REQUEST[ $action_key ] ) ) {
143
- return strtolower( $_REQUEST[ $action_key ] );
144
- }
145
- }
146
-
147
- return false;
148
- }
149
-
150
- function fs_request_is_action( $action, $action_key = 'action' ) {
151
- return ( strtolower( $action ) === fs_get_action( $action_key ) );
152
- }
153
-
154
- /**
155
- * @author Vova Feldman (@svovaf)
156
- * @since 1.0.0
157
- *
158
- * @since 1.2.1.5 Allow nonce verification.
159
- *
160
- * @param string $action
161
- * @param string $action_key
162
- * @param string $nonce_key
163
- *
164
- * @return bool
165
- */
166
- function fs_request_is_action_secure(
167
- $action,
168
- $action_key = 'action',
169
- $nonce_key = 'nonce'
170
- ) {
171
- if ( strtolower( $action ) !== fs_get_action( $action_key ) ) {
172
- return false;
173
- }
174
-
175
- $nonce = ! empty( $_REQUEST[ $nonce_key ] ) ?
176
- $_REQUEST[ $nonce_key ] :
177
- '';
178
-
179
- if ( empty( $nonce ) ||
180
- ( false === wp_verify_nonce( $nonce, $action ) )
181
- ) {
182
- return false;
183
- }
184
-
185
- return true;
186
- }
187
-
188
- function fs_is_plugin_page( $page_slug ) {
189
- return ( is_admin() && $page_slug === fs_request_get('page') );
190
- }
191
-
192
- /* Core UI.
193
- --------------------------------------------------------------------------------------------*/
194
- /**
195
- * @param string $slug
196
- * @param string $page
197
- * @param string $action
198
- * @param string $title
199
- * @param array $params
200
- * @param bool $is_primary
201
- * @param string|bool $icon_class Optional class for an icon (since 1.1.7).
202
- * @param string|bool $confirmation Optional confirmation message before submit (since 1.1.7).
203
- * @param string $method Since 1.1.7
204
- *
205
- * @uses fs_ui_get_action_button()
206
- */
207
- function fs_ui_action_button(
208
- $slug,
209
- $page,
210
- $action,
211
- $title,
212
- $params = array(),
213
- $is_primary = true,
214
- $icon_class = false,
215
- $confirmation = false,
216
- $method = 'GET'
217
- ) {
218
- echo fs_ui_get_action_button(
219
- $slug,
220
- $page,
221
- $action,
222
- $title,
223
- $params,
224
- $is_primary,
225
- $icon_class,
226
- $confirmation,
227
- $method
228
- );
229
- }
230
-
231
- /**
232
- * @author Vova Feldman (@svovaf)
233
- * @since 1.1.7
234
- *
235
- * @param string $slug
236
- * @param string $page
237
- * @param string $action
238
- * @param string $title
239
- * @param array $params
240
- * @param bool $is_primary
241
- * @param string|bool $icon_class Optional class for an icon.
242
- * @param string|bool $confirmation Optional confirmation message before submit.
243
- * @param string $method
244
- *
245
- * @return string
246
- */
247
- function fs_ui_get_action_button(
248
- $slug,
249
- $page,
250
- $action,
251
- $title,
252
- $params = array(),
253
- $is_primary = true,
254
- $icon_class = false,
255
- $confirmation = false,
256
- $method = 'GET'
257
- ) {
258
- // Prepend icon (if set).
259
- $title = ( is_string( $icon_class ) ? '<i class="' . $icon_class . '"></i> ' : '' ) . $title;
260
-
261
- if ( is_string( $confirmation ) ) {
262
- return sprintf( '<form action="%s" method="%s"><input type="hidden" name="fs_action" value="%s">%s<a href="#" class="%s" onclick="if (confirm(\'%s\')) this.parentNode.submit(); return false;">%s</a></form>',
263
- freemius( $slug )->_get_admin_page_url( $page, $params ),
264
- $method,
265
- $action,
266
- wp_nonce_field( $action, '_wpnonce', true, false ),
267
- 'button' . ( $is_primary ? ' button-primary' : '' ),
268
- $confirmation,
269
- $title
270
- );
271
- } else if ( 'GET' !== strtoupper( $method ) ) {
272
- return sprintf( '<form action="%s" method="%s"><input type="hidden" name="fs_action" value="%s">%s<a href="#" class="%s" onclick="this.parentNode.submit(); return false;">%s</a></form>',
273
- freemius( $slug )->_get_admin_page_url( $page, $params ),
274
- $method,
275
- $action,
276
- wp_nonce_field( $action, '_wpnonce', true, false ),
277
- 'button' . ( $is_primary ? ' button-primary' : '' ),
278
- $title
279
- );
280
- } else {
281
- return sprintf( '<a href="%s" class="%s">%s</a></form>',
282
- wp_nonce_url( freemius( $slug )->_get_admin_page_url( $page, array_merge( $params, array( 'fs_action' => $action ) ) ), $action ),
283
- 'button' . ( $is_primary ? ' button-primary' : '' ),
284
- $title
285
- );
286
- }
287
- }
288
-
289
- function fs_ui_action_link( $slug, $page, $action, $title, $params = array() ) {
290
- ?><a class=""
291
- href="<?php echo wp_nonce_url( freemius( $slug )->_get_admin_page_url( $page, array_merge( $params, array( 'fs_action' => $action ) ) ), $action ) ?>"><?php echo $title ?></a><?php
292
- }
293
-
294
- /*function fs_error_handler($errno, $errstr, $errfile, $errline)
295
- {
296
- if (false === strpos($errfile, 'freemius/'))
297
- {
298
- // @todo Dump Freemius errors to local log.
299
- }
300
-
301
- // switch ($errno) {
302
- // case E_USER_ERROR:
303
- // break;
304
- // case E_WARNING:
305
- // case E_USER_WARNING:
306
- // break;
307
- // case E_NOTICE:
308
- // case E_USER_NOTICE:
309
- // break;
310
- // default:
311
- // break;
312
- // }
313
- }
314
-
315
- set_error_handler('fs_error_handler');*/
316
-
317
- if ( ! function_exists( 'fs_nonce_url' ) ) {
318
- /**
319
- * Retrieve URL with nonce added to URL query.
320
- *
321
- * Originally was using `wp_nonce_url()` but the new version
322
- * changed the return value to escaped URL, that's not the expected
323
- * behaviour.
324
- *
325
- * @author Vova Feldman (@svovaf)
326
- * @since ~1.1.3
327
- *
328
- * @param string $actionurl URL to add nonce action.
329
- * @param int|string $action Optional. Nonce action name. Default -1.
330
- * @param string $name Optional. Nonce name. Default '_wpnonce'.
331
- *
332
- * @return string Escaped URL with nonce action added.
333
- */
334
- function fs_nonce_url( $actionurl, $action = - 1, $name = '_wpnonce' ) {
335
- return add_query_arg( $name, wp_create_nonce( $action ), $actionurl );
336
- }
337
- }
338
-
339
- if ( ! function_exists( 'fs_starts_with' ) ) {
340
- /**
341
- * Check if string starts with.
342
- *
343
- * @author Vova Feldman (@svovaf)
344
- * @since 1.1.3
345
- *
346
- * @param string $haystack
347
- * @param string $needle
348
- *
349
- * @return bool
350
- */
351
- function fs_starts_with( $haystack, $needle ) {
352
- $length = strlen( $needle );
353
-
354
- return ( substr( $haystack, 0, $length ) === $needle );
355
- }
356
- }
357
-
358
- #region Url Canonization ------------------------------------------------------------------
359
-
360
- if ( ! function_exists( 'fs_canonize_url' ) ) {
361
- /**
362
- * @author Vova Feldman (@svovaf)
363
- * @since 1.1.3
364
- *
365
- * @param string $url
366
- * @param bool $omit_host
367
- * @param array $ignore_params
368
- *
369
- * @return string
370
- */
371
- function fs_canonize_url( $url, $omit_host = false, $ignore_params = array() ) {
372
- $parsed_url = parse_url( strtolower( $url ) );
373
-
374
- // if ( ! isset( $parsed_url['host'] ) ) {
375
- // return $url;
376
- // }
377
-
378
- $canonical = ( ( $omit_host || ! isset( $parsed_url['host'] ) ) ? '' : $parsed_url['host'] ) . $parsed_url['path'];
379
-
380
- if ( isset( $parsed_url['query'] ) ) {
381
- parse_str( $parsed_url['query'], $queryString );
382
- $canonical .= '?' . fs_canonize_query_string( $queryString, $ignore_params );
383
- }
384
-
385
- return $canonical;
386
- }
387
- }
388
-
389
- if ( ! function_exists( 'fs_canonize_query_string' ) ) {
390
- /**
391
- * @author Vova Feldman (@svovaf)
392
- * @since 1.1.3
393
- *
394
- * @param array $params
395
- * @param array $ignore_params
396
- * @param bool $params_prefix
397
- *
398
- * @return string
399
- */
400
- function fs_canonize_query_string( array $params, array &$ignore_params, $params_prefix = false ) {
401
- if ( ! is_array( $params ) || 0 === count( $params ) ) {
402
- return '';
403
- }
404
-
405
- // Url encode both keys and values
406
- $keys = fs_urlencode_rfc3986( array_keys( $params ) );
407
- $values = fs_urlencode_rfc3986( array_values( $params ) );
408
- $params = array_combine( $keys, $values );
409
-
410
- // Parameters are sorted by name, using lexicographical byte value ordering.
411
- // Ref: Spec: 9.1.1 (1)
412
- uksort( $params, 'strcmp' );
413
-
414
- $pairs = array();
415
- foreach ( $params as $parameter => $value ) {
416
- $lower_param = strtolower( $parameter );
417
-
418
- // Skip ignore params.
419
- if ( in_array( $lower_param, $ignore_params ) ||
420
- ( false !== $params_prefix && fs_starts_with( $lower_param, $params_prefix ) )
421
- ) {
422
- continue;
423
- }
424
-
425
- if ( is_array( $value ) ) {
426
- // If two or more parameters share the same name, they are sorted by their value
427
- // Ref: Spec: 9.1.1 (1)
428
- natsort( $value );
429
- foreach ( $value as $duplicate_value ) {
430
- $pairs[] = $lower_param . '=' . $duplicate_value;
431
- }
432
- } else {
433
- $pairs[] = $lower_param . '=' . $value;
434
- }
435
- }
436
-
437
- if ( 0 === count( $pairs ) ) {
438
- return '';
439
- }
440
-
441
- return implode( "&", $pairs );
442
- }
443
- }
444
-
445
- if ( ! function_exists( 'fs_urlencode_rfc3986' ) ) {
446
- /**
447
- * @author Vova Feldman (@svovaf)
448
- * @since 1.1.3
449
- *
450
- * @param string|string[] $input
451
- *
452
- * @return array|mixed|string
453
- */
454
- function fs_urlencode_rfc3986( $input ) {
455
- if ( is_array( $input ) ) {
456
- return array_map( 'fs_urlencode_rfc3986', $input );
457
- } else if ( is_scalar( $input ) ) {
458
- return str_replace( '+', ' ', str_replace( '%7E', '~', rawurlencode( $input ) ) );
459
- }
460
-
461
- return '';
462
- }
463
- }
464
-
465
- #endregion Url Canonization ------------------------------------------------------------------
466
-
467
- function fs_download_image( $from, $to ) {
468
- $ch = curl_init( $from );
469
- $fp = fopen( fs_normalize_path( $to ), 'wb' );
470
- curl_setopt( $ch, CURLOPT_FILE, $fp );
471
- curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
472
- curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
473
- curl_setopt( $ch, CURLOPT_HEADER, 0 );
474
- curl_exec( $ch );
475
- curl_close( $ch );
476
- fclose( $fp );
477
- }
478
-
479
- /* General Utilities
480
- --------------------------------------------------------------------------------------------*/
481
-
482
- /**
483
- * Sorts an array by the value of the priority key.
484
- *
485
- * @author Daniel Iser (@danieliser)
486
- * @since 1.1.7
487
- *
488
- * @param $a
489
- * @param $b
490
- *
491
- * @return int
492
- */
493
- function fs_sort_by_priority( $a, $b ) {
494
-
495
- // If b has a priority and a does not, b wins.
496
- if ( ! isset( $a['priority'] ) && isset( $b['priority'] ) ) {
497
- return 1;
498
- } // If b has a priority and a does not, b wins.
499
- elseif ( isset( $a['priority'] ) && ! isset( $b['priority'] ) ) {
500
- return - 1;
501
- } // If neither has a priority or both priorities are equal its a tie.
502
- elseif ( ( ! isset( $a['priority'] ) && ! isset( $b['priority'] ) ) || $a['priority'] === $b['priority'] ) {
503
- return 0;
504
- }
505
-
506
- // If both have priority return the winner.
507
- return ( $a['priority'] < $b['priority'] ) ? - 1 : 1;
508
- }
509
-
510
- #--------------------------------------------------------------------------------
511
- #region Localization
512
- #--------------------------------------------------------------------------------
513
-
514
- if ( ! function_exists( 'fs_text' ) ) {
515
- /**
516
- * Retrieve a translated text by key.
517
- *
518
- * @author Vova Feldman (@svovaf)
519
- * @since 1.2.1.7
520
- *
521
- * @param string $key
522
- * @param string $slug
523
- *
524
- * @return string
525
- *
526
- * @global $fs_text, $fs_text_overrides
527
- */
528
- function fs_text( $key, $slug = 'freemius' ) {
529
- return __fs( $key, $slug );
530
- }
531
-
532
- /**
533
- * Output a translated text by key.
534
- *
535
- * @author Vova Feldman (@svovaf)
536
- * @since 1.2.1.7
537
- *
538
- * @param string $key
539
- * @param string $slug
540
- */
541
- function fs_echo( $key, $slug = 'freemius' ) {
542
- echo fs_text( $key, $slug );
543
- }
544
- }
545
-
546
- /**
547
- * @author Vova Feldman
548
- * @since 1.2.1.6
549
- *
550
- * @param string $key
551
- * @param string $slug
552
- *
553
- * @return string
554
- */
555
- function fs_esc_attr( $key, $slug ) {
556
- return esc_attr( fs_text( $key, $slug ) );
557
- }
558
-
559
- /**
560
- * @author Vova Feldman
561
- * @since 1.2.1.6
562
- *
563
- * @param string $key
564
- * @param string $slug
565
- */
566
- function fs_esc_attr_echo( $key, $slug ) {
567
- echo esc_attr( fs_text( $key, $slug ) );
568
- }
569
-
570
- /**
571
- * @author Vova Feldman
572
- * @since 1.2.1.6
573
- *
574
- * @param string $key
575
- * @param string $slug
576
- *
577
- * @return string
578
- */
579
- function fs_esc_js( $key, $slug ) {
580
- return esc_js( fs_text( $key, $slug ) );
581
- }
582
-
583
- /**
584
- * @author Vova Feldman
585
- * @since 1.2.1.6
586
- *
587
- * @param string $key
588
- * @param string $slug
589
- */
590
- function fs_esc_js_echo( $key, $slug ) {
591
- echo esc_js( fs_text( $key, $slug ) );
592
- }
593
-
594
- /**
595
- * @author Vova Feldman
596
- * @since 1.2.1.6
597
- *
598
- * @param string $key
599
- * @param string $slug
600
- */
601
- function fs_json_encode_echo( $key, $slug ) {
602
- echo json_encode( fs_text( $key, $slug ) );
603
- }
604
-
605
- /**
606
- * @author Vova Feldman
607
- * @since 1.2.1.6
608
- *
609
- * @param string $key
610
- * @param string $slug
611
- *
612
- * @return string
613
- */
614
- function fs_esc_html( $key, $slug ) {
615
- return esc_html( fs_text( $key, $slug ) );
616
- }
617
-
618
- /**
619
- * @author Vova Feldman
620
- * @since 1.2.1.6
621
- *
622
- * @param string $key
623
- * @param string $slug
624
- */
625
- function fs_esc_html_echo( $key, $slug ) {
626
- echo esc_html( fs_text( $key, $slug ) );
627
- }
628
-
629
- #endregion
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
9
+ if ( ! defined( 'ABSPATH' ) ) {
10
+ exit;
11
+ }
12
+
13
+ if ( ! function_exists( 'fs_dummy' ) ) {
14
+ function fs_dummy() {
15
+ }
16
+ }
17
+
18
+ /* Url.
19
+ --------------------------------------------------------------------------------------------*/
20
+ if ( ! function_exists( 'fs_get_url_daily_cache_killer' ) ) {
21
+ function fs_get_url_daily_cache_killer() {
22
+ return date( '\YY\Mm\Dd' );
23
+ }
24
+ }
25
+
26
+ /* Templates / Views.
27
+ --------------------------------------------------------------------------------------------*/
28
+ if ( ! function_exists( 'fs_get_template_path' ) ) {
29
+ function fs_get_template_path( $path ) {
30
+ return WP_FS__DIR_TEMPLATES . '/' . trim( $path, '/' );
31
+ }
32
+
33
+ function fs_include_template( $path, &$params = null ) {
34
+ $VARS = &$params;
35
+ include fs_get_template_path( $path );
36
+ }
37
+
38
+ function fs_include_once_template( $path, &$params = null ) {
39
+ $VARS = &$params;
40
+ include_once fs_get_template_path( $path );
41
+ }
42
+
43
+ function fs_require_template( $path, &$params = null ) {
44
+ $VARS = &$params;
45
+ require fs_get_template_path( $path );
46
+ }
47
+
48
+ function fs_require_once_template( $path, &$params = null ) {
49
+ $VARS = &$params;
50
+ require_once fs_get_template_path( $path );
51
+ }
52
+
53
+ function fs_get_template( $path, &$params = null ) {
54
+ ob_start();
55
+
56
+ $VARS = &$params;
57
+ require fs_get_template_path( $path );
58
+
59
+ return ob_get_clean();
60
+ }
61
+ }
62
+
63
+ /* Scripts and styles including.
64
+ --------------------------------------------------------------------------------------------*/
65
+
66
+ /**
67
+ * Generates an absolute URL to the given path. This function ensures that the URL will be correct whether the asset
68
+ * is inside a plugin's folder or a theme's folder.
69
+ *
70
+ * Examples:
71
+ * 1. "themes" folder
72
+ * Path: C:/xampp/htdocs/fswp/wp-content/themes/twentytwelve/freemius/assets/css/admin/common.css
73
+ * URL: http://fswp:8080/wp-content/themes/twentytwelve/freemius/assets/css/admin/common.css
74
+ *
75
+ * 2. "plugins" folder
76
+ * Path: C:/xampp/htdocs/fswp/wp-content/plugins/rating-widget-premium/freemius/assets/css/admin/common.css
77
+ * URL: http://fswp:8080/wp-content/plugins/rating-widget-premium/freemius/assets/css/admin/common.css
78
+ *
79
+ * @author Leo Fajardo (@leorw)
80
+ * @since 1.2.2
81
+ *
82
+ * @param string $asset_abs_path Asset's absolute path.
83
+ *
84
+ * @return string Asset's URL.
85
+ */
86
+ function fs_asset_url( $asset_abs_path ) {
87
+ $wp_content_dir = fs_normalize_path( WP_CONTENT_DIR );
88
+ $asset_abs_path = fs_normalize_path( $asset_abs_path );
89
+ $asset_rel_path = str_replace( $wp_content_dir, '', $asset_abs_path );
90
+
91
+ $asset_url = content_url( fs_normalize_path( $asset_rel_path ) );
92
+
93
+ return $asset_url;
94
+ }
95
+
96
+ function fs_enqueue_local_style( $handle, $path, $deps = array(), $ver = false, $media = 'all' ) {
97
+ wp_enqueue_style( $handle, fs_asset_url( WP_FS__DIR_CSS . '/' . trim( $path, '/' ) ), $deps, $ver, $media );
98
+ }
99
+
100
+ function fs_enqueue_local_script( $handle, $path, $deps = array(), $ver = false, $in_footer = 'all' ) {
101
+ wp_enqueue_script( $handle, fs_asset_url( WP_FS__DIR_JS . '/' . trim( $path, '/' ) ), $deps, $ver, $in_footer );
102
+ }
103
+
104
+ function fs_img_url( $path, $img_dir = WP_FS__DIR_IMG ) {
105
+ return ( fs_asset_url( $img_dir . '/' . trim( $path, '/' ) ) );
106
+ }
107
+
108
+ #--------------------------------------------------------------------------------
109
+ #region Request handlers.
110
+ #--------------------------------------------------------------------------------
111
+
112
+ if ( ! function_exists( 'fs_request_get' ) ) {
113
+ /**
114
+ * @param string $key
115
+ * @param mixed $def
116
+ * @param string|bool $type Since 1.2.1.7 - when set to 'get' will look for the value passed via querystring, when
117
+ * set to 'post' will look for the value passed via the POST request's body, otherwise,
118
+ * will check if the parameter was passed in any of the two.
119
+ *
120
+ * @return mixed
121
+ */
122
+ function fs_request_get( $key, $def = false, $type = false ) {
123
+ if ( is_string( $type ) ) {
124
+ $type = strtolower( $type );
125
+ }
126
+
127
+ switch ( $type ) {
128
+ case 'post':
129
+ $value = isset( $_POST[ $key ] ) ? $_POST[ $key ] : $def;
130
+ break;
131
+ case 'get':
132
+ $value = isset( $_GET[ $key ] ) ? $_GET[ $key ] : $def;
133
+ break;
134
+ default:
135
+ $value = isset( $_REQUEST[ $key ] ) ? $_REQUEST[ $key ] : $def;
136
+ break;
137
+ }
138
+
139
+ return $value;
140
+ }
141
+ }
142
+
143
+ if ( ! function_exists( 'fs_request_has' ) ) {
144
+ function fs_request_has( $key ) {
145
+ return isset( $_REQUEST[ $key ] );
146
+ }
147
+ }
148
+
149
+ if ( ! function_exists( 'fs_request_get_bool' ) ) {
150
+ function fs_request_get_bool( $key, $def = false ) {
151
+ if ( ! isset( $_REQUEST[ $key ] ) ) {
152
+ return $def;
153
+ }
154
+
155
+ if ( 1 == $_REQUEST[ $key ] || 'true' === strtolower( $_REQUEST[ $key ] ) ) {
156
+ return true;
157
+ }
158
+
159
+ if ( 0 == $_REQUEST[ $key ] || 'false' === strtolower( $_REQUEST[ $key ] ) ) {
160
+ return false;
161
+ }
162
+
163
+ return $def;
164
+ }
165
+ }
166
+
167
+ if ( ! function_exists( 'fs_request_is_post' ) ) {
168
+ function fs_request_is_post() {
169
+ return ( 'post' === strtolower( $_SERVER['REQUEST_METHOD'] ) );
170
+ }
171
+ }
172
+
173
+ if ( ! function_exists( 'fs_request_is_get' ) ) {
174
+ function fs_request_is_get() {
175
+ return ( 'get' === strtolower( $_SERVER['REQUEST_METHOD'] ) );
176
+ }
177
+ }
178
+
179
+ if ( ! function_exists( 'fs_get_action' ) ) {
180
+ function fs_get_action( $action_key = 'action' ) {
181
+ if ( ! empty( $_REQUEST[ $action_key ] ) && is_string( $_REQUEST[ $action_key ] ) ) {
182
+ return strtolower( $_REQUEST[ $action_key ] );
183
+ }
184
+
185
+ if ( 'action' == $action_key ) {
186
+ $action_key = 'fs_action';
187
+
188
+ if ( ! empty( $_REQUEST[ $action_key ] ) && is_string( $_REQUEST[ $action_key ] ) ) {
189
+ return strtolower( $_REQUEST[ $action_key ] );
190
+ }
191
+ }
192
+
193
+ return false;
194
+ }
195
+ }
196
+
197
+ if ( ! function_exists( 'fs_request_is_action' ) ) {
198
+ function fs_request_is_action( $action, $action_key = 'action' ) {
199
+ return ( strtolower( $action ) === fs_get_action( $action_key ) );
200
+ }
201
+ }
202
+
203
+ if ( ! function_exists( 'fs_request_is_action_secure' ) ) {
204
+ /**
205
+ * @author Vova Feldman (@svovaf)
206
+ * @since 1.0.0
207
+ *
208
+ * @since 1.2.1.5 Allow nonce verification.
209
+ *
210
+ * @param string $action
211
+ * @param string $action_key
212
+ * @param string $nonce_key
213
+ *
214
+ * @return bool
215
+ */
216
+ function fs_request_is_action_secure(
217
+ $action,
218
+ $action_key = 'action',
219
+ $nonce_key = 'nonce'
220
+ ) {
221
+ if ( strtolower( $action ) !== fs_get_action( $action_key ) ) {
222
+ return false;
223
+ }
224
+
225
+ $nonce = ! empty( $_REQUEST[ $nonce_key ] ) ?
226
+ $_REQUEST[ $nonce_key ] :
227
+ '';
228
+
229
+ if ( empty( $nonce ) ||
230
+ ( false === wp_verify_nonce( $nonce, $action ) )
231
+ ) {
232
+ return false;
233
+ }
234
+
235
+ return true;
236
+ }
237
+ }
238
+
239
+ #endregion
240
+
241
+ if ( ! function_exists( 'fs_is_plugin_page' ) ) {
242
+ function fs_is_plugin_page( $page_slug ) {
243
+ return ( is_admin() && $page_slug === fs_request_get( 'page' ) );
244
+ }
245
+ }
246
+
247
+ if ( ! function_exists( 'fs_get_raw_referer' ) ) {
248
+ /**
249
+ * Retrieves unvalidated referer from '_wp_http_referer' or HTTP referer.
250
+ *
251
+ * Do not use for redirects, use {@see wp_get_referer()} instead.
252
+ *
253
+ * @since 1.2.3
254
+ *
255
+ * @return string|false Referer URL on success, false on failure.
256
+ */
257
+ function fs_get_raw_referer() {
258
+ if ( function_exists( 'wp_get_raw_referer' ) ) {
259
+ return wp_get_raw_referer();
260
+ }
261
+ if ( ! empty( $_REQUEST['_wp_http_referer'] ) ) {
262
+ return wp_unslash( $_REQUEST['_wp_http_referer'] );
263
+ } else if ( ! empty( $_SERVER['HTTP_REFERER'] ) ) {
264
+ return wp_unslash( $_SERVER['HTTP_REFERER'] );
265
+ }
266
+
267
+ return false;
268
+ }
269
+ }
270
+
271
+ /* Core UI.
272
+ --------------------------------------------------------------------------------------------*/
273
+ /**
274
+ * @param number $module_id
275
+ * @param string $page
276
+ * @param string $action
277
+ * @param string $title
278
+ * @param array $params
279
+ * @param bool $is_primary
280
+ * @param string|bool $icon_class Optional class for an icon (since 1.1.7).
281
+ * @param string|bool $confirmation Optional confirmation message before submit (since 1.1.7).
282
+ * @param string $method Since 1.1.7
283
+ *
284
+ * @uses fs_ui_get_action_button()
285
+ */
286
+ function fs_ui_action_button(
287
+ $module_id,
288
+ $page,
289
+ $action,
290
+ $title,
291
+ $params = array(),
292
+ $is_primary = true,
293
+ $icon_class = false,
294
+ $confirmation = false,
295
+ $method = 'GET'
296
+ ) {
297
+ echo fs_ui_get_action_button(
298
+ $module_id,
299
+ $page,
300
+ $action,
301
+ $title,
302
+ $params,
303
+ $is_primary,
304
+ $icon_class,
305
+ $confirmation,
306
+ $method
307
+ );
308
+ }
309
+
310
+ /**
311
+ * @author Vova Feldman (@svovaf)
312
+ * @since 1.1.7
313
+ *
314
+ * @param number $module_id
315
+ * @param string $page
316
+ * @param string $action
317
+ * @param string $title
318
+ * @param array $params
319
+ * @param bool $is_primary
320
+ * @param string|bool $icon_class Optional class for an icon.
321
+ * @param string|bool $confirmation Optional confirmation message before submit.
322
+ * @param string $method
323
+ *
324
+ * @return string
325
+ */
326
+ function fs_ui_get_action_button(
327
+ $module_id,
328
+ $page,
329
+ $action,
330
+ $title,
331
+ $params = array(),
332
+ $is_primary = true,
333
+ $icon_class = false,
334
+ $confirmation = false,
335
+ $method = 'GET'
336
+ ) {
337
+ // Prepend icon (if set).
338
+ $title = ( is_string( $icon_class ) ? '<i class="' . $icon_class . '"></i> ' : '' ) . $title;
339
+
340
+ if ( is_string( $confirmation ) ) {
341
+ return sprintf( '<form action="%s" method="%s"><input type="hidden" name="fs_action" value="%s">%s<a href="#" class="%s" onclick="if (confirm(\'%s\')) this.parentNode.submit(); return false;">%s</a></form>',
342
+ freemius( $module_id )->_get_admin_page_url( $page, $params ),
343
+ $method,
344
+ $action,
345
+ wp_nonce_field( $action, '_wpnonce', true, false ),
346
+ 'button' . ( $is_primary ? ' button-primary' : '' ),
347
+ $confirmation,
348
+ $title
349
+ );
350
+ } else if ( 'GET' !== strtoupper( $method ) ) {
351
+ return sprintf( '<form action="%s" method="%s"><input type="hidden" name="fs_action" value="%s">%s<a href="#" class="%s" onclick="this.parentNode.submit(); return false;">%s</a></form>',
352
+ freemius( $module_id )->_get_admin_page_url( $page, $params ),
353
+ $method,
354
+ $action,
355
+ wp_nonce_field( $action, '_wpnonce', true, false ),
356
+ 'button' . ( $is_primary ? ' button-primary' : '' ),
357
+ $title
358
+ );
359
+ } else {
360
+ return sprintf( '<a href="%s" class="%s">%s</a></form>',
361
+ wp_nonce_url( freemius( $module_id )->_get_admin_page_url( $page, array_merge( $params, array( 'fs_action' => $action ) ) ), $action ),
362
+ 'button' . ( $is_primary ? ' button-primary' : '' ),
363
+ $title
364
+ );
365
+ }
366
+ }
367
+
368
+ function fs_ui_action_link( $module_id, $page, $action, $title, $params = array() ) {
369
+ ?><a class=""
370
+ href="<?php echo wp_nonce_url( freemius( $module_id )->_get_admin_page_url( $page, array_merge( $params, array( 'fs_action' => $action ) ) ), $action ) ?>"><?php echo $title ?></a><?php
371
+ }
372
+
373
+ /*function fs_error_handler($errno, $errstr, $errfile, $errline)
374
+ {
375
+ if (false === strpos($errfile, 'freemius/'))
376
+ {
377
+ // @todo Dump Freemius errors to local log.
378
+ }
379
+
380
+ // switch ($errno) {
381
+ // case E_USER_ERROR:
382
+ // break;
383
+ // case E_WARNING:
384
+ // case E_USER_WARNING:
385
+ // break;
386
+ // case E_NOTICE:
387
+ // case E_USER_NOTICE:
388
+ // break;
389
+ // default:
390
+ // break;
391
+ // }
392
+ }
393
+
394
+ set_error_handler('fs_error_handler');*/
395
+
396
+ if ( ! function_exists( 'fs_nonce_url' ) ) {
397
+ /**
398
+ * Retrieve URL with nonce added to URL query.
399
+ *
400
+ * Originally was using `wp_nonce_url()` but the new version
401
+ * changed the return value to escaped URL, that's not the expected
402
+ * behaviour.
403
+ *
404
+ * @author Vova Feldman (@svovaf)
405
+ * @since ~1.1.3
406
+ *
407
+ * @param string $actionurl URL to add nonce action.
408
+ * @param int|string $action Optional. Nonce action name. Default -1.
409
+ * @param string $name Optional. Nonce name. Default '_wpnonce'.
410
+ *
411
+ * @return string Escaped URL with nonce action added.
412
+ */
413
+ function fs_nonce_url( $actionurl, $action = - 1, $name = '_wpnonce' ) {
414
+ return add_query_arg( $name, wp_create_nonce( $action ), $actionurl );
415
+ }
416
+ }
417
+
418
+ if ( ! function_exists( 'fs_starts_with' ) ) {
419
+ /**
420
+ * Check if string starts with.
421
+ *
422
+ * @author Vova Feldman (@svovaf)
423
+ * @since 1.1.3
424
+ *
425
+ * @param string $haystack
426
+ * @param string $needle
427
+ *
428
+ * @return bool
429
+ */
430
+ function fs_starts_with( $haystack, $needle ) {
431
+ $length = strlen( $needle );
432
+
433
+ return ( substr( $haystack, 0, $length ) === $needle );
434
+ }
435
+ }
436
+
437
+ #region Url Canonization ------------------------------------------------------------------
438
+
439
+ if ( ! function_exists( 'fs_canonize_url' ) ) {
440
+ /**
441
+ * @author Vova Feldman (@svovaf)
442
+ * @since 1.1.3
443
+ *
444
+ * @param string $url
445
+ * @param bool $omit_host
446
+ * @param array $ignore_params
447
+ *
448
+ * @return string
449
+ */
450
+ function fs_canonize_url( $url, $omit_host = false, $ignore_params = array() ) {
451
+ $parsed_url = parse_url( strtolower( $url ) );
452
+
453
+ // if ( ! isset( $parsed_url['host'] ) ) {
454
+ // return $url;
455
+ // }
456
+
457
+ $canonical = ( ( $omit_host || ! isset( $parsed_url['host'] ) ) ? '' : $parsed_url['host'] ) . $parsed_url['path'];
458
+
459
+ if ( isset( $parsed_url['query'] ) ) {
460
+ parse_str( $parsed_url['query'], $queryString );
461
+ $canonical .= '?' . fs_canonize_query_string( $queryString, $ignore_params );
462
+ }
463
+
464
+ return $canonical;
465
+ }
466
+ }
467
+
468
+ if ( ! function_exists( 'fs_canonize_query_string' ) ) {
469
+ /**
470
+ * @author Vova Feldman (@svovaf)
471
+ * @since 1.1.3
472
+ *
473
+ * @param array $params
474
+ * @param array $ignore_params
475
+ * @param bool $params_prefix
476
+ *
477
+ * @return string
478
+ */
479
+ function fs_canonize_query_string( array $params, array &$ignore_params, $params_prefix = false ) {
480
+ if ( ! is_array( $params ) || 0 === count( $params ) ) {
481
+ return '';
482
+ }
483
+
484
+ // Url encode both keys and values
485
+ $keys = fs_urlencode_rfc3986( array_keys( $params ) );
486
+ $values = fs_urlencode_rfc3986( array_values( $params ) );
487
+ $params = array_combine( $keys, $values );
488
+
489
+ // Parameters are sorted by name, using lexicographical byte value ordering.
490
+ // Ref: Spec: 9.1.1 (1)
491
+ uksort( $params, 'strcmp' );
492
+
493
+ $pairs = array();
494
+ foreach ( $params as $parameter => $value ) {
495
+ $lower_param = strtolower( $parameter );
496
+
497
+ // Skip ignore params.
498
+ if ( in_array( $lower_param, $ignore_params ) ||
499
+ ( false !== $params_prefix && fs_starts_with( $lower_param, $params_prefix ) )
500
+ ) {
501
+ continue;
502
+ }
503
+
504
+ if ( is_array( $value ) ) {
505
+ // If two or more parameters share the same name, they are sorted by their value
506
+ // Ref: Spec: 9.1.1 (1)
507
+ natsort( $value );
508
+ foreach ( $value as $duplicate_value ) {
509
+ $pairs[] = $lower_param . '=' . $duplicate_value;
510
+ }
511
+ } else {
512
+ $pairs[] = $lower_param . '=' . $value;
513
+ }
514
+ }
515
+
516
+ if ( 0 === count( $pairs ) ) {
517
+ return '';
518
+ }
519
+
520
+ return implode( "&", $pairs );
521
+ }
522
+ }
523
+
524
+ if ( ! function_exists( 'fs_urlencode_rfc3986' ) ) {
525
+ /**
526
+ * @author Vova Feldman (@svovaf)
527
+ * @since 1.1.3
528
+ *
529
+ * @param string|string[] $input
530
+ *
531
+ * @return array|mixed|string
532
+ */
533
+ function fs_urlencode_rfc3986( $input ) {
534
+ if ( is_array( $input ) ) {
535
+ return array_map( 'fs_urlencode_rfc3986', $input );
536
+ } else if ( is_scalar( $input ) ) {
537
+ return str_replace( '+', ' ', str_replace( '%7E', '~', rawurlencode( $input ) ) );
538
+ }
539
+
540
+ return '';
541
+ }
542
+ }
543
+
544
+ #endregion Url Canonization ------------------------------------------------------------------
545
+
546
+ /**
547
+ * @author Vova Feldman (@svovaf)
548
+ *
549
+ * @since 1.2.2 Changed to usage of WP_Filesystem_Direct.
550
+ *
551
+ * @param string $from URL
552
+ * @param string $to File path.
553
+ *
554
+ * @return bool Is successfully downloaded.
555
+ */
556
+ function fs_download_image( $from, $to ) {
557
+ $dir = dirname( $to );
558
+
559
+ if ( 'direct' !== get_filesystem_method( array(), $dir ) ) {
560
+ return false;
561
+ }
562
+
563
+ if ( ! class_exists( 'WP_Filesystem_Direct' ) ) {
564
+ require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php';
565
+ require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php';
566
+ }
567
+
568
+ $fs = new WP_Filesystem_Direct( '' );
569
+ $tmpfile = download_url( $from );
570
+
571
+ if ( $tmpfile instanceof WP_Error ) {
572
+ // Issue downloading the file.
573
+ return false;
574
+ }
575
+
576
+ $fs->copy( $tmpfile, $to );
577
+ $fs->delete( $tmpfile );
578
+
579
+ return true;
580
+ }
581
+
582
+ /* General Utilities
583
+ --------------------------------------------------------------------------------------------*/
584
+
585
+ /**
586
+ * Sorts an array by the value of the priority key.
587
+ *
588
+ * @author Daniel Iser (@danieliser)
589
+ * @since 1.1.7
590
+ *
591
+ * @param $a
592
+ * @param $b
593
+ *
594
+ * @return int
595
+ */
596
+ function fs_sort_by_priority( $a, $b ) {
597
+
598
+ // If b has a priority and a does not, b wins.
599
+ if ( ! isset( $a['priority'] ) && isset( $b['priority'] ) ) {
600
+ return 1;
601
+ } // If b has a priority and a does not, b wins.
602
+ elseif ( isset( $a['priority'] ) && ! isset( $b['priority'] ) ) {
603
+ return - 1;
604
+ } // If neither has a priority or both priorities are equal its a tie.
605
+ elseif ( ( ! isset( $a['priority'] ) && ! isset( $b['priority'] ) ) || $a['priority'] === $b['priority'] ) {
606
+ return 0;
607
+ }
608
+
609
+ // If both have priority return the winner.
610
+ return ( $a['priority'] < $b['priority'] ) ? - 1 : 1;
611
+ }
612
+
613
+ #--------------------------------------------------------------------------------
614
+ #region Localization
615
+ #--------------------------------------------------------------------------------
616
+
617
+ if ( ! function_exists( 'fs_text' ) ) {
618
+ /**
619
+ * Retrieve a translated text by key.
620
+ *
621
+ * @author Vova Feldman (@svovaf)
622
+ * @since 1.2.1.7
623
+ *
624
+ * @param string $key
625
+ * @param string $slug
626
+ *
627
+ * @return string
628
+ *
629
+ * @global $fs_text , $fs_text_overrides
630
+ */
631
+ function fs_text( $key, $slug = 'freemius' ) {
632
+ return __fs( $key, $slug );
633
+ }
634
+
635
+ /**
636
+ * Get a translatable text override if exists, or `false`.
637
+ *
638
+ * @author Vova Feldman (@svovaf)
639
+ * @since 1.2.1.7
640
+ *
641
+ * @param string $text Translatable string.
642
+ * @param string $key String key for overrides.
643
+ * @param string $slug Module slug for overrides.
644
+ *
645
+ * @return string|false
646
+ */
647
+ function fs_text_override( $text, $key, $slug ) {
648
+ global $fs_text_overrides;
649
+
650
+ /**
651
+ * Check if string is overridden.
652
+ */
653
+ if ( ! isset( $fs_text_overrides[ $slug ] ) ) {
654
+ return false;
655
+ }
656
+
657
+ if ( empty( $key ) ) {
658
+ $key = strtolower( str_replace( ' ', '-', $text ) );
659
+ }
660
+
661
+ if ( isset( $fs_text_overrides[ $slug ][ $key ] ) ) {
662
+ return $fs_text_overrides[ $slug ][ $key ];
663
+ }
664
+
665
+ $lower_key = strtolower( $key );
666
+ if ( isset( $fs_text_overrides[ $slug ][ $lower_key ] ) ) {
667
+ return $fs_text_overrides[ $slug ][ $lower_key ];
668
+ }
669
+
670
+ return false;
671
+ }
672
+
673
+ /**
674
+ * Get a translatable text and its text domain.
675
+ *
676
+ * When the text is overridden by the module, returns the overridden text and the text domain of the module. Otherwise, returns the original text and 'freemius' as the text domain.
677
+ *
678
+ * @author Vova Feldman (@svovaf)
679
+ * @since 1.2.1.7
680
+ *
681
+ * @param string $text Translatable string.
682
+ * @param string $key String key for overrides.
683
+ * @param string $slug Module slug for overrides.
684
+ *
685
+ * @return string[]
686
+ */
687
+ function fs_text_and_domain( $text, $key, $slug ) {
688
+ $override = fs_text_override( $text, $key, $slug );
689
+
690
+ if ( false === $override ) {
691
+ // No override, use FS text domain.
692
+ $text_domain = 'freemius';
693
+ } else {
694
+ // Found an override.
695
+ $text = $override;
696
+ // Use the module's text domain.
697
+ $text_domain = $slug;
698
+ }
699
+
700
+ return array( $text, $text_domain );
701
+ }
702
+
703
+ #region Private
704
+
705
+ /**
706
+ * Retrieve an inline translated text by key.
707
+ *
708
+ * @author Vova Feldman (@svovaf)
709
+ * @since 1.2.3
710
+ *
711
+ * @param string $text Translatable string.
712
+ * @param string $key String key for overrides.
713
+ * @param string $slug Module slug for overrides.
714
+ *
715
+ * @return string
716
+ *
717
+ * @global $fs_text_overrides
718
+ */
719
+ function _fs_text_inline( $text, $key = '', $slug = 'freemius' ) {
720
+ list( $text, $text_domain ) = fs_text_and_domain( $text, $key, $slug );
721
+
722
+ // Avoid misleading Theme Check warning.
723
+ $fn = 'translate';
724
+
725
+ return $fn( $text, $text_domain );
726
+ }
727
+
728
+ /**
729
+ * Retrieve an inline translated text by key with a context.
730
+ *
731
+ * @author Vova Feldman (@svovaf)
732
+ * @since 1.2.3
733
+ *
734
+ * @param string $text Translatable string.
735
+ * @param string $context Context information for the translators.
736
+ * @param string $key String key for overrides.
737
+ * @param string $slug Module slug for overrides.
738
+ *
739
+ * @return string
740
+ *
741
+ * @global $fs_text_overrides
742
+ */
743
+ function _fs_text_x_inline( $text, $context, $key = '', $slug = 'freemius' ) {
744
+ list( $text, $text_domain ) = fs_text_and_domain( $text, $key, $slug );
745
+
746
+ // Avoid misleading Theme Check warning.
747
+ $fn = 'translate_with_gettext_context';
748
+
749
+ return $fn( $text, $context, $text_domain );
750
+ }
751
+
752
+ #endregion
753
+
754
+ /**
755
+ * Retrieve an inline translated text by key.
756
+ *
757
+ * @author Vova Feldman (@svovaf)
758
+ * @since 1.2.3
759
+ *
760
+ * @param string $text Translatable string.
761
+ * @param string $key String key for overrides.
762
+ * @param string $slug Module slug for overrides.
763
+ *
764
+ * @return string
765
+ *
766
+ * @global $fs_text_overrides
767
+ */
768
+ function fs_text_inline( $text, $key = '', $slug = 'freemius' ) {
769
+ return _fs_text_inline( $text, $key, $slug );
770
+ }
771
+
772
+ /**
773
+ * Retrieve an inline translated text by key with a context.
774
+ *
775
+ * @author Vova Feldman (@svovaf)
776
+ * @since 1.2.3
777
+ *
778
+ * @param string $text Translatable string.
779
+ * @param string $context Context information for the translators.
780
+ * @param string $key String key for overrides.
781
+ * @param string $slug Module slug for overrides.
782
+ *
783
+ * @return string
784
+ *
785
+ * @global $fs_text_overrides
786
+ */
787
+ function fs_text_x_inline( $text, $context, $key = '', $slug = 'freemius' ) {
788
+ return _fs_text_x_inline( $text, $context, $key, $slug );
789
+ }
790
+
791
+ /**
792
+ * Output a translated text by key.
793
+ *
794
+ * @author Vova Feldman (@svovaf)
795
+ * @since 1.2.1.7
796
+ *
797
+ * @param string $key
798
+ * @param string $slug
799
+ */
800
+ function fs_echo( $key, $slug = 'freemius' ) {
801
+ echo fs_text( $key, $slug );
802
+ }
803
+
804
+ /**
805
+ * Output an inline translated text.
806
+ *
807
+ * @author Vova Feldman (@svovaf)
808
+ * @since 1.2.3
809
+ *
810
+ * @param string $text Translatable string.
811
+ * @param string $key String key for overrides.
812
+ * @param string $slug Module slug for overrides.
813
+ */
814
+ function fs_echo_inline( $text, $key = '', $slug = 'freemius' ) {
815
+ echo _fs_text_inline( $text, $key, $slug );
816
+ }
817
+
818
+ /**
819
+ * Output an inline translated text with a context.
820
+ *
821
+ * @author Vova Feldman (@svovaf)
822
+ * @since 1.2.3
823
+ *
824
+ * @param string $text Translatable string.
825
+ * @param string $context Context information for the translators.
826
+ * @param string $key String key for overrides.
827
+ * @param string $slug Module slug for overrides.
828
+ */
829
+ function fs_echo_x_inline( $text, $context, $key = '', $slug = 'freemius' ) {
830
+ echo _fs_text_x_inline( $text, $context, $key, $slug );
831
+ }
832
+ }
833
+
834
+ if ( ! function_exists( 'fs_esc_attr' ) ) {
835
+ /**
836
+ * @author Vova Feldman
837
+ * @since 1.2.1.6
838
+ *
839
+ * @param string $key
840
+ * @param string $slug
841
+ *
842
+ * @return string
843
+ */
844
+ function fs_esc_attr( $key, $slug ) {
845
+ return esc_attr( fs_text( $key, $slug ) );
846
+ }
847
+ }
848
+
849
+ if ( ! function_exists( 'fs_esc_attr_inline' ) ) {
850
+ /**
851
+ * @author Vova Feldman (@svovaf)
852
+ * @since 1.2.3
853
+ *
854
+ * @param string $text Translatable string.
855
+ * @param string $key String key for overrides.
856
+ * @param string $slug Module slug for overrides.
857
+ *
858
+ * @return string
859
+ */
860
+ function fs_esc_attr_inline( $text, $key = '', $slug = 'freemius' ) {
861
+ return esc_attr( _fs_text_inline( $text, $key, $slug ) );
862
+ }
863
+ }
864
+
865
+ if ( ! function_exists( 'fs_esc_attr_x_inline' ) ) {
866
+ /**
867
+ * @author Vova Feldman (@svovaf)
868
+ * @since 1.2.3
869
+ *
870
+ * @param string $text Translatable string.
871
+ * @param string $context Context information for the translators.
872
+ * @param string $key String key for overrides.
873
+ * @param string $slug Module slug for overrides.
874
+ *
875
+ * @return string
876
+ */
877
+ function fs_esc_attr_x_inline( $text, $context, $key = '', $slug = 'freemius' ) {
878
+ return esc_attr( _fs_text_x_inline( $text, $context, $key, $slug ) );
879
+ }
880
+ }
881
+
882
+ if ( ! function_exists( 'fs_esc_attr_echo' ) ) {
883
+ /**
884
+ * @author Vova Feldman
885
+ * @since 1.2.1.6
886
+ *
887
+ * @param string $key
888
+ * @param string $slug
889
+ */
890
+ function fs_esc_attr_echo( $key, $slug ) {
891
+ echo esc_attr( fs_text( $key, $slug ) );
892
+ }
893
+ }
894
+
895
+ if ( ! function_exists( 'fs_esc_attr_echo_inline' ) ) {
896
+ /**
897
+ * @author Vova Feldman (@svovaf)
898
+ * @since 1.2.3
899
+ *
900
+ * @param string $text Translatable string.
901
+ * @param string $key String key for overrides.
902
+ * @param string $slug Module slug for overrides.
903
+ */
904
+ function fs_esc_attr_echo_inline( $text, $key = '', $slug = 'freemius' ) {
905
+ echo esc_attr( _fs_text_inline( $text, $key, $slug ) );
906
+ }
907
+ }
908
+
909
+ if ( ! function_exists( 'fs_esc_js' ) ) {
910
+ /**
911
+ * @author Vova Feldman
912
+ * @since 1.2.1.6
913
+ *
914
+ * @param string $key
915
+ * @param string $slug
916
+ *
917
+ * @return string
918
+ */
919
+ function fs_esc_js( $key, $slug ) {
920
+ return esc_js( fs_text( $key, $slug ) );
921
+ }
922
+ }
923
+
924
+ if ( ! function_exists( 'fs_esc_js_inline' ) ) {
925
+ /**
926
+ * @author Vova Feldman (@svovaf)
927
+ * @since 1.2.3
928
+ *
929
+ * @param string $text Translatable string.
930
+ * @param string $key String key for overrides.
931
+ * @param string $slug Module slug for overrides.
932
+ *
933
+ * @return string
934
+ */
935
+ function fs_esc_js_inline( $text, $key = '', $slug = 'freemius' ) {
936
+ return esc_js( _fs_text_inline( $text, $key, $slug ) );
937
+ }
938
+ }
939
+
940
+ if ( ! function_exists( 'fs_esc_js_x_inline' ) ) {
941
+ /**
942
+ * @author Vova Feldman (@svovaf)
943
+ * @since 1.2.3
944
+ *
945
+ * @param string $text Translatable string.
946
+ * @param string $context Context information for the translators.
947
+ * @param string $key String key for overrides.
948
+ * @param string $slug Module slug for overrides.
949
+ *
950
+ * @return string
951
+ */
952
+ function fs_esc_js_x_inline( $text, $context, $key = '', $slug = 'freemius' ) {
953
+ return esc_js( _fs_text_x_inline( $text, $context, $key, $slug ) );
954
+ }
955
+ }
956
+
957
+ if ( ! function_exists( 'fs_esc_js_echo_x_inline' ) ) {
958
+ /**
959
+ * @author Vova Feldman (@svovaf)
960
+ * @since 1.2.3
961
+ *
962
+ * @param string $text Translatable string.
963
+ * @param string $context Context information for the translators.
964
+ * @param string $key String key for overrides.
965
+ * @param string $slug Module slug for overrides.
966
+ *
967
+ * @return string
968
+ */
969
+ function fs_esc_js_echo_x_inline( $text, $context, $key = '', $slug = 'freemius' ) {
970
+ echo esc_js( _fs_text_x_inline( $text, $context, $key, $slug ) );
971
+ }
972
+ }
973
+
974
+ if ( ! function_exists( 'fs_esc_js_echo' ) ) {
975
+ /**
976
+ * @author Vova Feldman
977
+ * @since 1.2.1.6
978
+ *
979
+ * @param string $key
980
+ * @param string $slug
981
+ */
982
+ function fs_esc_js_echo( $key, $slug ) {
983
+ echo esc_js( fs_text( $key, $slug ) );
984
+ }
985
+ }
986
+
987
+ if ( ! function_exists( 'fs_esc_js_echo_inline' ) ) {
988
+ /**
989
+ * @author Vova Feldman (@svovaf)
990
+ * @since 1.2.3
991
+ *
992
+ * @param string $text Translatable string.
993
+ * @param string $key String key for overrides.
994
+ * @param string $slug Module slug for overrides.
995
+ */
996
+ function fs_esc_js_echo_inline( $text, $key = '', $slug = 'freemius' ) {
997
+ echo esc_js( _fs_text_inline( $text, $key, $slug ) );
998
+ }
999
+ }
1000
+
1001
+ if ( ! function_exists( 'fs_json_encode_echo' ) ) {
1002
+ /**
1003
+ * @author Vova Feldman
1004
+ * @since 1.2.1.6
1005
+ *
1006
+ * @param string $key
1007
+ * @param string $slug
1008
+ */
1009
+ function fs_json_encode_echo( $key, $slug ) {
1010
+ echo json_encode( fs_text( $key, $slug ) );
1011
+ }
1012
+ }
1013
+
1014
+ if ( ! function_exists( 'fs_json_encode_echo_inline' ) ) {
1015
+ /**
1016
+ * @author Vova Feldman (@svovaf)
1017
+ * @since 1.2.3
1018
+ *
1019
+ * @param string $text Translatable string.
1020
+ * @param string $key String key for overrides.
1021
+ * @param string $slug Module slug for overrides.
1022
+ */
1023
+ function fs_json_encode_echo_inline( $text, $key = '', $slug = 'freemius' ) {
1024
+ echo json_encode( _fs_text_inline( $text, $key, $slug ) );
1025
+ }
1026
+ }
1027
+
1028
+ if ( ! function_exists( 'fs_esc_html' ) ) {
1029
+ /**
1030
+ * @author Vova Feldman
1031
+ * @since 1.2.1.6
1032
+ *
1033
+ * @param string $key
1034
+ * @param string $slug
1035
+ *
1036
+ * @return string
1037
+ */
1038
+ function fs_esc_html( $key, $slug ) {
1039
+ return esc_html( fs_text( $key, $slug ) );
1040
+ }
1041
+ }
1042
+
1043
+ if ( ! function_exists( 'fs_esc_html_inline' ) ) {
1044
+ /**
1045
+ * @author Vova Feldman (@svovaf)
1046
+ * @since 1.2.3
1047
+ *
1048
+ * @param string $text Translatable string.
1049
+ * @param string $key String key for overrides.
1050
+ * @param string $slug Module slug for overrides.
1051
+ *
1052
+ * @return string
1053
+ */
1054
+ function fs_esc_html_inline( $text, $key = '', $slug = 'freemius' ) {
1055
+ return esc_html( _fs_text_inline( $text, $key, $slug ) );
1056
+ }
1057
+ }
1058
+
1059
+ if ( ! function_exists( 'fs_esc_html_x_inline' ) ) {
1060
+ /**
1061
+ * @author Vova Feldman (@svovaf)
1062
+ * @since 1.2.3
1063
+ *
1064
+ * @param string $text Translatable string.
1065
+ * @param string $context Context information for the translators.
1066
+ * @param string $key String key for overrides.
1067
+ * @param string $slug Module slug for overrides.
1068
+ *
1069
+ * @return string
1070
+ */
1071
+ function fs_esc_html_x_inline( $text, $context, $key = '', $slug = 'freemius' ) {
1072
+ return esc_html( _fs_text_x_inline( $text, $context, $key, $slug ) );
1073
+ }
1074
+ }
1075
+
1076
+ if ( ! function_exists( 'fs_esc_html_echo_x_inline' ) ) {
1077
+ /**
1078
+ * @author Vova Feldman (@svovaf)
1079
+ * @since 1.2.3
1080
+ *
1081
+ * @param string $text Translatable string.
1082
+ * @param string $context Context information for the translators.
1083
+ * @param string $key String key for overrides.
1084
+ * @param string $slug Module slug for overrides.
1085
+ */
1086
+ function fs_esc_html_echo_x_inline( $text, $context, $key = '', $slu